From 192084d48e20edfe801401c18f311264f04c9b61 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Mon, 20 Apr 2009 00:39:36 +0200 Subject: [PATCH] Unify how search URI and keywords are combined This handles the presence of a %1 and encoding the keywords. --- midori/midori-browser.c | 14 ++----------- midori/midori-locationaction.c | 5 +---- midori/midori-view.c | 5 +---- midori/sokoke.c | 37 ++++++++++++++++++++++++++++------ 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/midori/midori-browser.c b/midori/midori-browser.c index 32fd16ec..1a89ff45 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -2587,14 +2587,7 @@ _action_location_submit_uri (GtkAction* action, new_uri = sokoke_magic_uri (uri, browser->search_engines); if (!new_uri) - { - gchar* uri_ = g_uri_escape_string (uri, " :/", TRUE); - if (strstr (browser->location_entry_search, "%s")) - new_uri = g_strdup_printf (browser->location_entry_search, uri_); - else if (!new_uri) - new_uri = g_strconcat (browser->location_entry_search, uri_, NULL); - g_free (uri_); - } + new_uri = sokoke_search_uri (browser->location_entry_search, uri); if (new_tab) { @@ -2648,10 +2641,7 @@ _action_search_submit (GtkAction* action, else /* The location entry search is our fallback */ url = browser->location_entry_search; - if (strstr (url, "%s")) - search = g_strdup_printf (url, keywords); - else - search = g_strconcat (url, " ", keywords, NULL); + search = sokoke_search_uri (url, keywords); if (new_tab) midori_browser_add_uri (browser, search); diff --git a/midori/midori-locationaction.c b/midori/midori-locationaction.c index 7d4ae733..54cdaeda 100644 --- a/midori/midori-locationaction.c +++ b/midori/midori-locationaction.c @@ -750,10 +750,7 @@ midori_location_entry_action_activated_cb (GtkEntryCompletion* completion, gchar* search; if (!item) return; - if (strstr (uri, "%s")) - search = g_strdup_printf (uri, keywords); - else - search = g_strconcat (uri, " ", keywords, NULL); + search = sokoke_search_uri (uri, keywords); midori_location_action_set_uri (location_action, search); g_signal_emit (location_action, signals[SUBMIT_URI], 0, search, FALSE); g_free (search); diff --git a/midori/midori-view.c b/midori/midori-view.c index bb10bfe5..8b1e4d23 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -893,10 +893,7 @@ midori_web_view_menu_search_web_activate_cb (GtkWidget* widget, else g_object_get (view->settings, "location-entry-search", &search, NULL); - if (strstr (search, "%s")) - uri = g_strdup_printf (search, view->selected_text); - else - uri = g_strconcat (search, view->selected_text, NULL); + uri = sokoke_search_uri (search, view->selected_text); g_free (search); g_signal_emit (view, signals[NEW_TAB], 0, uri, diff --git a/midori/sokoke.c b/midori/sokoke.c index adda6469..914d99dc 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -213,6 +213,36 @@ sokoke_idn_to_punycode (gchar* uri) #endif } +/** + * sokoke_search_uri: + * @uri: a search URI with or without %s + * @keywords: keywords + * + * Takes a search engine URI and inserts the specified + * keywords. The @keywords are percent encoded. If the + * search URI contains a %s they keywords are inserted + * in that place, otherwise appended to the URI. + * + * Return value: a newly allocated search URI + **/ +gchar* sokoke_search_uri (const gchar* uri, + const gchar* keywords) +{ + gchar* escaped; + gchar* search; + + g_return_val_if_fail (uri != NULL, NULL); + g_return_val_if_fail (keywords != NULL, NULL); + + escaped = g_uri_escape_string (keywords, " :/", TRUE); + if (strstr (uri, "%s")) + search = g_strdup_printf (uri, escaped); + else + search = g_strconcat (uri, escaped, NULL); + g_free (escaped); + return search; +} + gchar* sokoke_magic_uri (const gchar* uri, KatzeArray* search_engines) @@ -279,13 +309,8 @@ sokoke_magic_uri (const gchar* uri, if (parts[0] && parts[1]) if ((item = katze_array_find_token (search_engines, parts[0]))) { - gchar* uri_ = g_uri_escape_string (parts[1], " :/", TRUE); search_uri = katze_item_get_uri (item); - if (strstr (search_uri, "%s")) - search = g_strdup_printf (search_uri, uri_); - else - search = g_strconcat (search_uri, uri_, NULL); - g_free (uri_); + search = sokoke_search_uri (search_uri, parts[1]); } g_strfreev (parts); return search; -- 2.39.5