From 75d7a7024c481cd2fd9c8742312ea36e20df77b6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Forysiuk?= Date: Wed, 25 Jul 2012 23:03:37 +0200 Subject: [PATCH] Guess a suitable token for a form-based search --- midori/midori-searchaction.c | 60 ++++++++++++++++++++++++++++++++++-- midori/midori-view.c | 4 +++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/midori/midori-searchaction.c b/midori/midori-searchaction.c index 95685739..da4f9bd6 100644 --- a/midori/midori-searchaction.c +++ b/midori/midori-searchaction.c @@ -877,6 +877,63 @@ midori_search_action_editor_name_changed_cb (GtkWidget* entry, GTK_RESPONSE_ACCEPT, text && *text); } +gchar* +midori_search_action_token_for_uri (const gchar* uri) +{ + guint len, i; + gchar** parts; + gchar* hostname = NULL, *path = NULL; + + path = midori_uri_parse_hostname (uri, NULL); + parts = g_strsplit (path, ".", -1); + g_free (path); + + len = g_strv_length (parts); + if (len > 2) + { + for (i = len; i == 0; i--) + { + if (parts[i] && *parts[i]) + if (strlen (parts[i]) > 3) + { + hostname = g_strdup (parts[i]); + break; + } + } + } + else + hostname = g_strdup (parts[0]); + + if (!hostname) + hostname = g_strdup (parts[1]); + + g_strfreev (parts); + + if (strlen (hostname) > 4) + { + GString* str = g_string_new (NULL); + int j, count = 0; + + for (j = 0; count < 4; j++) + { + if (hostname[j] == 'a' + || hostname[j] == 'e' + || hostname[j] == 'i' + || hostname[j] == 'o' + || hostname[j] == 'u') + continue; + else + { + g_string_append_c (str, hostname[j]); + count++; + } + } + return g_string_free (str, FALSE); + } + return g_strdup (hostname); +} + + void midori_search_action_get_editor (MidoriSearchAction* search_action, KatzeItem* item, @@ -978,8 +1035,7 @@ midori_search_action_get_editor (MidoriSearchAction* search_action, gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); entry_token = gtk_entry_new (); gtk_entry_set_activates_default (GTK_ENTRY (entry_token), TRUE); - if (!new_engine) - gtk_entry_set_text (GTK_ENTRY (entry_token) + gtk_entry_set_text (GTK_ENTRY (entry_token) , katze_str_non_null (katze_item_get_token (item))); gtk_box_pack_start (GTK_BOX (hbox), entry_token, TRUE, TRUE, 0); gtk_container_add (GTK_CONTAINER (content_area), hbox); diff --git a/midori/midori-view.c b/midori/midori-view.c index 41669ed0..5b13ff17 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -2441,6 +2441,9 @@ midori_view_menu_add_search_engine_cb (GtkWidget* widget, midori_search_action_get_editor (MIDORI_SEARCH_ACTION (action), item, TRUE); } +gchar* +midori_search_action_token_for_uri (const gchar* uri); + static KatzeItem* midori_view_search_engine_for_form (MidoriView* view, WebKitWebView* web_view) @@ -2512,6 +2515,7 @@ midori_view_search_engine_for_form (MidoriView* view, item = katze_item_new (); katze_item_set_uri (item, g_string_free (uri_str, FALSE)); + item->token = midori_search_action_token_for_uri (view->uri); if (strstr (title, " - ")) parts = g_strsplit (title, " - ", 2); -- 2.39.5