Unify how search URI and keywords are combined

This handles the presence of a %1 and encoding the keywords.
This commit is contained in:
Christian Dywan 2009-04-20 00:39:36 +02:00
parent 8f58355014
commit 192084d48e
4 changed files with 35 additions and 26 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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,

View file

@ -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;