From 68e0e521f4f9e0a08b4288376a0a9842d0ee974d Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sun, 20 Apr 2008 21:44:56 +0200 Subject: [PATCH] Resolve helpers and globals into sokoke. --- src/Makefile.am | 5 +- src/global.h | 65 ------------------- src/helpers.c | 145 ------------------------------------------- src/helpers.h | 43 ------------- src/main.c | 4 +- src/main.h | 58 +++++++++++++++-- src/midori-browser.c | 17 +++-- src/midori-webview.c | 13 ++-- src/sokoke.c | 69 ++++++++++++++++++++ src/sokoke.h | 11 ++++ src/webSearch.c | 39 +++++++++++- 11 files changed, 187 insertions(+), 282 deletions(-) delete mode 100644 src/global.h delete mode 100644 src/helpers.c delete mode 100644 src/helpers.h diff --git a/src/Makefile.am b/src/Makefile.am index 8202314b..042ef9be 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,8 +26,5 @@ midori_SOURCES = \ midori-websettings.c midori-websettings.h \ midori-preferences.c midori-preferences.h \ webSearch.c webSearch.h \ - helpers.c helpers.h \ sokoke.c sokoke.h \ - search.c search.h \ - global.h \ - ui.h + search.c search.h diff --git a/src/global.h b/src/global.h deleted file mode 100644 index 40f003c4..00000000 --- a/src/global.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - Copyright (C) 2007 Christian Dywan - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - See the file COPYING for the full license text. -*/ - -#ifndef __GLOBAL_H__ -#define __GLOBAL_H__ 1 - -#include "midori-websettings.h" -#include - -#include -#include - -#include - -// FIXME: Remove these globals - -GList* searchEngines; // Items of type 'SearchEngine' -KatzeXbelItem* bookmarks; - -// Custom stock items - -// We should distribute these -// Names should match with epiphany and/ or xdg spec -/* NOTE: Those uncommented were replaced with remotely related icons - in order to reduce the amount of warnings :D */ - -#define STOCK_BOOKMARK GTK_STOCK_FILE // "stock_bookmark" "bookmark-web" -#define STOCK_FORM_FILL GTK_STOCK_JUSTIFY_FILL // "insert-text" "form-fill" -#define STOCK_NEWSFEED GTK_STOCK_INDEX - -// We assume that these legacy icon names are usually present - -#define STOCK_BOOKMARK_NEW "stock_add-bookmark" -#define STOCK_HOMEPAGE GTK_STOCK_HOME -#define STOCK_IMAGE "gnome-mime-image" -#define STOCK_LOCK_OPEN "stock_lock-open" -#define STOCK_LOCK_SECURE "stock_lock" -#define STOCK_LOCK_BROKEN "stock_lock-broken" -#define STOCK_NETWORK_OFFLINE "network-offline" -#define STOCK_SCRIPT "stock_script" -#define STOCK_SEND "stock_mail-send" -#define STOCK_TAB_NEW "stock_new-tab" -#define STOCK_THEME "gnome-settings-theme" -#define STOCK_USER_TRASH "gnome-stock-trash" -#define STOCK_WINDOW_NEW "stock_new-window" - -// For backwards compatibility - -#if !GTK_CHECK_VERSION(2, 10, 0) -#define GTK_STOCK_SELECT_ALL "gtk-select-all" -#endif -#if !GTK_CHECK_VERSION(2, 8, 0) -#define GTK_STOCK_FULLSCREEN "gtk-fullscreen" -#define GTK_STOCK_LEAVE_FULLSCREEN "gtk-leave-fullscreen" -#endif - -#endif /* !__GLOBAL_H__ */ diff --git a/src/helpers.c b/src/helpers.c deleted file mode 100644 index cae95325..00000000 --- a/src/helpers.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - Copyright (C) 2007 Christian Dywan - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - See the file COPYING for the full license text. -*/ - -#include "helpers.h" - -#include "global.h" -#include "search.h" -#include "sokoke.h" - -#include "midori-webview.h" -#include - -#include -#include - -GtkWidget* check_menu_item_new(const gchar* text - , GCallback signal, gboolean sensitive, gboolean active, gpointer userdata) -{ - GtkWidget* menuitem = gtk_check_menu_item_new_with_mnemonic(text); - gtk_widget_set_sensitive(menuitem, sensitive && signal); - gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); - if(signal) - g_signal_connect(menuitem, "activate", signal, userdata); - return menuitem; -} - -GtkWidget* radio_button_new(GtkRadioButton* radio_button, const gchar* label) -{ - return gtk_radio_button_new_with_mnemonic_from_widget(radio_button, label); -} - -void show_error(const gchar* text, const gchar* text2, MidoriBrowser* browser) -{ - GtkWidget* dialog = gtk_message_dialog_new( - browser ? GTK_WINDOW(browser) : NULL - , 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, text); - if(text2) - gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), text2); - gtk_dialog_run(GTK_DIALOG(dialog)); - gtk_widget_destroy(dialog); -} - -GdkPixbuf* load_web_icon(const gchar* icon, GtkIconSize size, GtkWidget* widget) -{ - g_return_val_if_fail(GTK_IS_WIDGET(widget), NULL); - GdkPixbuf* pixbuf = NULL; - if(icon && *icon) - { - // TODO: We want to allow http as well, maybe also base64? - const gchar* iconReady = g_str_has_prefix(icon, "file://") ? &icon[7] : icon; - GtkStockItem stockItem; - if(gtk_stock_lookup(icon, &stockItem)) - pixbuf = gtk_widget_render_icon(widget, iconReady, size, NULL); - else - { - gint width; gint height; - gtk_icon_size_lookup(size, &width, &height); - pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default() - , icon, MAX(width, height), GTK_ICON_LOOKUP_USE_BUILTIN, NULL); - } - if(!pixbuf) - pixbuf = gdk_pixbuf_new_from_file_at_size(iconReady, 16, 16, NULL); - } - if(!pixbuf) - pixbuf = gtk_widget_render_icon(widget, GTK_STOCK_FIND, size, NULL); - return pixbuf; -} - -void entry_setup_completion(GtkEntry* entry) -{ - /* TODO: The current behavior works only with the beginning of strings - But we want to match "localhost" with "loc" and "hos" */ - GtkEntryCompletion* completion = gtk_entry_completion_new(); - gtk_entry_completion_set_model(completion - , GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_STRING))); - gtk_entry_completion_set_text_column(completion, 0); - gtk_entry_completion_set_minimum_key_length(completion, 3); - gtk_entry_set_completion(entry, completion); - gtk_entry_completion_set_popup_completion(completion, FALSE); //... -} - -void entry_completion_append(GtkEntry* entry, const gchar* text) -{ - GtkEntryCompletion* completion = gtk_entry_get_completion(entry); - GtkTreeModel* completion_store = gtk_entry_completion_get_model(completion); - GtkTreeIter iter; - gtk_list_store_insert(GTK_LIST_STORE(completion_store), &iter, 0); - gtk_list_store_set(GTK_LIST_STORE(completion_store), &iter, 0, text, -1); -} - -gchar* magic_uri(const gchar* uri, gboolean search) -{ - // Add file:// if we have a local path - if(g_path_is_absolute(uri)) - return g_strconcat("file://", uri, NULL); - // Do we need to add a protocol? - if(!strstr(uri, "://")) - { - // Do we have a domain, ip address or localhost? - if(strchr(uri, '.') != NULL || !strcmp(uri, "localhost")) - return g_strconcat("http://", uri, NULL); - // We don't want to search? So return early. - if(!search) - return g_strdup(uri); - gchar search[256]; - const gchar* searchUrl = NULL; - // Do we have a keyword and a string? - gchar** parts = g_strsplit(uri, " ", 2); - if(parts[0] && parts[1]) - { - guint n = g_list_length(searchEngines); - guint i; - for(i = 0; i < n; i++) - { - SearchEngine* searchEngine = (SearchEngine*)g_list_nth_data(searchEngines, i); - if(!strcmp(search_engine_get_keyword(searchEngine), parts[0])) - searchUrl = searchEngine->url; - } - if(searchUrl != NULL) - g_snprintf(search, 255, searchUrl, parts[1]); - } - //g_strfreev(sParts); - // We only have a word or there is no matching keyowrd, so search for it - if(searchUrl == NULL) - g_snprintf(search, 255, ""/*config->locationSearch*/, uri); - return g_strdup(search); - } - return g_strdup(uri); -} - -gchar* get_default_font(void) -{ - GtkSettings* gtksettings = gtk_settings_get_default(); - gchar* defaultFont; - g_object_get(gtksettings, "gtk-font-name", &defaultFont, NULL); - return defaultFont; -} diff --git a/src/helpers.h b/src/helpers.h deleted file mode 100644 index dde12642..00000000 --- a/src/helpers.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - Copyright (C) 2007 Christian Dywan - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - See the file COPYING for the full license text. -*/ - -#ifndef __HELPERS_H__ -#define __HELPERS_H__ 1 - -#include - -#include "midori-browser.h" - -GtkWidget* -check_menu_item_new(const gchar*, GCallback, gboolean, gboolean, gpointer); - -GtkWidget* -radio_button_new(GtkRadioButton*, const gchar*); - -void -show_error(const gchar*, const gchar*, MidoriBrowser*); - -GdkPixbuf* -load_web_icon(const gchar*, GtkIconSize, GtkWidget*); - -void -entry_setup_completion(GtkEntry*); - -void -entry_completion_append(GtkEntry*, const gchar*); - -gchar* -magic_uri(const gchar*, gboolean bSearch); - -gchar* -get_default_font(void); - -#endif /* !__HELPERS_H__ */ diff --git a/src/main.c b/src/main.c index 1c3a86f7..2fabe7cd 100644 --- a/src/main.c +++ b/src/main.c @@ -11,8 +11,6 @@ #include "main.h" -#include "global.h" -#include "helpers.h" #include "sokoke.h" #include "search.h" @@ -409,7 +407,7 @@ int main(int argc, char** argv) while(uri != NULL) { KatzeXbelItem* item = katze_xbel_bookmark_new(); - gchar* uriReady = magic_uri(uri, FALSE); + gchar* uriReady = sokoke_magic_uri (uri, NULL); katze_xbel_bookmark_set_href(item, uriReady); g_free(uriReady); katze_xbel_folder_append_item(_session, item); diff --git a/src/main.h b/src/main.h index 9364748e..6e603014 100644 --- a/src/main.h +++ b/src/main.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2007 Christian Dywan + Copyright (C) 2007-2008 Christian Dywan This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -9,7 +9,57 @@ See the file COPYING for the full license text. */ -#ifndef __MIDORI_H__ -#define __MIDORI_H__ 1 +#ifndef __MAIN_H__ +#define __MAIN_H__ 1 -#endif /* !__MIDORI_H__ */ +#include "midori-websettings.h" +#include + +#include +#include + +#include + +// FIXME: Remove these globals + +GList* searchEngines; // Items of type 'SearchEngine' +KatzeXbelItem* bookmarks; + +// Custom stock items + +// We should distribute these +// Names should match with epiphany and/ or xdg spec +/* NOTE: Those uncommented were replaced with remotely related icons + in order to reduce the amount of warnings :D */ + +#define STOCK_BOOKMARK GTK_STOCK_FILE // "stock_bookmark" "bookmark-web" +#define STOCK_FORM_FILL GTK_STOCK_JUSTIFY_FILL // "insert-text" "form-fill" +#define STOCK_NEWSFEED GTK_STOCK_INDEX + +// We assume that these legacy icon names are usually present + +#define STOCK_BOOKMARK_NEW "stock_add-bookmark" +#define STOCK_HOMEPAGE GTK_STOCK_HOME +#define STOCK_IMAGE "gnome-mime-image" +#define STOCK_LOCK_OPEN "stock_lock-open" +#define STOCK_LOCK_SECURE "stock_lock" +#define STOCK_LOCK_BROKEN "stock_lock-broken" +#define STOCK_NETWORK_OFFLINE "network-offline" +#define STOCK_SCRIPT "stock_script" +#define STOCK_SEND "stock_mail-send" +#define STOCK_TAB_NEW "stock_new-tab" +#define STOCK_THEME "gnome-settings-theme" +#define STOCK_USER_TRASH "gnome-stock-trash" +#define STOCK_WINDOW_NEW "stock_new-window" + +// For backwards compatibility + +#if !GTK_CHECK_VERSION(2, 10, 0) +#define GTK_STOCK_SELECT_ALL "gtk-select-all" +#endif +#if !GTK_CHECK_VERSION(2, 8, 0) +#define GTK_STOCK_FULLSCREEN "gtk-fullscreen" +#define GTK_STOCK_LEAVE_FULLSCREEN "gtk-leave-fullscreen" +#endif + +#endif /* !__MAIN_H__ */ diff --git a/src/midori-browser.c b/src/midori-browser.c index 576e230c..cbb86920 100644 --- a/src/midori-browser.c +++ b/src/midori-browser.c @@ -13,10 +13,9 @@ #include "midori-browser.h" -#include "global.h" -#include "helpers.h" #include "webSearch.h" +#include "main.h" #include "sokoke.h" #include "midori-webview.h" #include "midori-preferences.h" @@ -1020,6 +1019,9 @@ midori_browser_location_key_press_event_cb (GtkWidget* widget, GdkEventKey* event, MidoriBrowser* browser) { + MidoriBrowserPrivate* priv = browser->priv; + gchar* location_entry_search; + switch (event->keyval) { case GDK_ISO_Enter: @@ -1029,11 +1031,14 @@ midori_browser_location_key_press_event_cb (GtkWidget* widget, const gchar* uri = gtk_entry_get_text (GTK_ENTRY (widget)); if (uri) { - gchar* new_uri = magic_uri (uri, TRUE); + g_object_get (priv->settings, "location-entry-search", + &location_entry_search, NULL); + gchar* new_uri = sokoke_magic_uri (uri, location_entry_search); + g_free (location_entry_search); // TODO: Use new_uri intermediately when completion is better /* TODO Completion should be generated from history, that is the uri as well as the title. */ - entry_completion_append (GTK_ENTRY (widget), uri); + sokoke_entry_append_completion (GTK_ENTRY (widget), uri); GtkWidget* web_view = midori_browser_get_current_web_view (browser); g_object_set (web_view, "uri", new_uri, NULL); g_free (new_uri); @@ -2359,7 +2364,7 @@ midori_browser_init (MidoriBrowser* browser) // Location priv->location = sexy_icon_entry_new(); - entry_setup_completion (GTK_ENTRY (priv->location)); + sokoke_entry_setup_completion (GTK_ENTRY (priv->location)); priv->location_icon = gtk_image_new (); sexy_icon_entry_set_icon (SEXY_ICON_ENTRY (priv->location) , SEXY_ICON_ENTRY_PRIMARY, GTK_IMAGE (priv->location_icon)); @@ -2381,7 +2386,7 @@ midori_browser_init (MidoriBrowser* browser) // TODO: Make this actively resizable or enlarge to fit contents? // FIXME: The interface is somewhat awkward and ought to be rethought // TODO: Display "show in context menu" search engines as "completion actions" - entry_setup_completion (GTK_ENTRY (priv->search)); + sokoke_entry_setup_completion (GTK_ENTRY (priv->search)); g_object_connect (priv->search, "signal::icon-released", on_webSearch_icon_released, browser, diff --git a/src/midori-webview.c b/src/midori-webview.c index 77f319c3..591b46c2 100644 --- a/src/midori-webview.c +++ b/src/midori-webview.c @@ -11,7 +11,7 @@ #include "midori-webview.h" -#include "global.h" +#include "main.h" #include "sokoke.h" #include @@ -245,14 +245,9 @@ midori_web_view_class_init (MidoriWebViewClass* class) "", flags)); - g_object_class_install_property (gobject_class, - PROP_SETTINGS, - g_param_spec_object ( - "settings", - "Settings", - _("The associated settings"), - MIDORI_TYPE_WEB_SETTINGS, - G_PARAM_READWRITE)); + g_object_class_override_property (gobject_class, + PROP_SETTINGS, + "settings"); g_type_class_add_private (class, sizeof (MidoriWebViewPrivate)); } diff --git a/src/sokoke.c b/src/sokoke.c index bb38b30d..403dae6b 100644 --- a/src/sokoke.c +++ b/src/sokoke.c @@ -11,7 +11,10 @@ #include "sokoke.h" +#include "search.h" + #include "config.h" +#include "main.h" #include #ifdef HAVE_UNISTD_H @@ -21,6 +24,72 @@ #include #include +gchar* +sokoke_magic_uri (const gchar* uri, const gchar* default_search_uri) +{ + // Add file:// if we have a local path + if (g_path_is_absolute (uri)) + return g_strconcat ("file://", uri, NULL); + // Do we need to add a protocol? + if (!strstr (uri, "://")) + { + // Do we have a domain, ip address or localhost? + if (strchr (uri, '.') != NULL || !strcmp (uri, "localhost")) + return g_strconcat ("http://", uri, NULL); + // We don't want to search? So return early. + if (!default_search_uri) + return g_strdup (uri); + gchar* search; + const gchar* search_uri = NULL; + // Do we have a keyword and a string? + gchar** parts = g_strsplit (uri, " ", 2); + if (parts[0] && parts[1]) + { + guint n = g_list_length (searchEngines); + guint i; + for (i = 0; i < n; i++) + { + SearchEngine* search_engine = (SearchEngine*)g_list_nth_data ( + searchEngines, i); + if (!strcmp (search_engine_get_keyword (search_engine), + parts[0])) + search_uri = search_engine->url; + } + if (search_uri) + search = g_strdup_printf (search_uri, parts[1]); + } + // We only have a word or there is no matching keyword, so search for it + if (!search_uri) + search = g_strdup_printf (default_search_uri, uri); + return search; + } + return g_strdup (uri); +} + +void +sokoke_entry_setup_completion (GtkEntry* entry) +{ + /* TODO: The current behavior works only with the beginning of strings + But we want to match "localhost" with "loc" and "hos" */ + GtkEntryCompletion* completion = gtk_entry_completion_new (); + gtk_entry_completion_set_model (completion, + GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING))); + gtk_entry_completion_set_text_column (completion, 0); + gtk_entry_completion_set_minimum_key_length (completion, 3); + gtk_entry_set_completion (entry, completion); + gtk_entry_completion_set_popup_completion (completion, FALSE); //... +} + +void +sokoke_entry_append_completion (GtkEntry* entry, const gchar* text) +{ + GtkEntryCompletion* completion = gtk_entry_get_completion (entry); + GtkTreeModel* completion_store = gtk_entry_completion_get_model (completion); + GtkTreeIter iter; + gtk_list_store_insert (GTK_LIST_STORE (completion_store), &iter, 0); + gtk_list_store_set (GTK_LIST_STORE (completion_store), &iter, 0, text, -1); +} + #if SOKOKE_DEBUG > 1 #define UNIMPLEMENTED g_print(" * Unimplemented: %s\n", G_STRFUNC); #else diff --git a/src/sokoke.h b/src/sokoke.h index bbb5e7a2..3a89f4b2 100644 --- a/src/sokoke.h +++ b/src/sokoke.h @@ -17,6 +17,17 @@ // Many themes need this hack for small toolbars to work #define GTK_ICON_SIZE_SMALL_TOOLBAR GTK_ICON_SIZE_BUTTON +gchar* +sokoke_magic_uri (const gchar* uri, + const gchar* search); + +void +sokoke_entry_setup_completion (GtkEntry* entry); + +void +sokoke_entry_append_completion (GtkEntry* entry, + const gchar* text); + typedef enum { SOKOKE_MENU_POSITION_CURSOR = 0, SOKOKE_MENU_POSITION_LEFT, diff --git a/src/webSearch.c b/src/webSearch.c index d8040577..000c66ed 100644 --- a/src/webSearch.c +++ b/src/webSearch.c @@ -11,15 +11,48 @@ #include "webSearch.h" -#include "global.h" -#include "helpers.h" #include "search.h" + +#include "main.h" #include "sokoke.h" #include #include #include +static GdkPixbuf* +load_web_icon (const gchar* icon, GtkIconSize size, GtkWidget* widget) +{ + g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL); + GdkPixbuf* pixbuf = NULL; + if (icon && *icon) + { + // TODO: We want to allow http as well, maybe also base64? + const gchar* icon_ready = g_str_has_prefix (icon, "file://") + ? &icon[7] : icon; + GtkStockItem stock_id; + if (gtk_stock_lookup (icon, &stock_id)) + pixbuf = gtk_widget_render_icon (widget, icon_ready, size, NULL); + else + { + gint width, height; + gtk_icon_size_lookup (size, &width, &height); + if (gtk_widget_has_screen (widget)) + { + GdkScreen* screen = gtk_widget_get_screen (widget); + pixbuf = gtk_icon_theme_load_icon ( + gtk_icon_theme_get_for_screen (screen), icon, + MAX (width, height), GTK_ICON_LOOKUP_USE_BUILTIN, NULL); + } + } + if (!pixbuf) + pixbuf = gdk_pixbuf_new_from_file_at_size (icon_ready, 16, 16, NULL); + } + if (!pixbuf) + pixbuf = gtk_widget_render_icon (widget, GTK_STOCK_FIND, size, NULL); + return pixbuf; +} + void update_searchEngine(guint index, GtkWidget* search) { guint n = g_list_length(searchEngines); @@ -437,7 +470,7 @@ void on_webSearch_activate(GtkWidget* widget, MidoriBrowser* browser) search = g_strdup_printf(url, keywords); else search = g_strconcat(url, " ", keywords, NULL); - entry_completion_append(GTK_ENTRY(widget), keywords); + sokoke_entry_append_completion(GTK_ENTRY(widget), keywords); GtkWidget* webView = midori_browser_get_current_web_view(browser); webkit_web_view_open(WEBKIT_WEB_VIEW(webView), search); g_free(search);