From: Paweł Forysiuk Date: Fri, 20 Jul 2012 21:46:26 +0000 (+0200) Subject: Add "Add search engine" context menu to forms X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b1abeed69847f79b3f78fa7dc951081014425de;p=midori Add "Add search engine" context menu to forms Fixes: https://bugs.launchpad.net/midori/+bug/802279 --- diff --git a/midori/midori-searchaction.c b/midori/midori-searchaction.c index a6d3dbf1..438db203 100644 --- a/midori/midori-searchaction.c +++ b/midori/midori-searchaction.c @@ -877,15 +877,15 @@ midori_search_action_editor_name_changed_cb (GtkWidget* entry, GTK_RESPONSE_ACCEPT, text && *text); } -static void +void midori_search_action_get_editor (MidoriSearchAction* search_action, + KatzeItem* item, gboolean new_engine) { GtkWidget* toplevel; GtkWidget* dialog; GtkWidget* content_area; GtkSizeGroup* sizegroup; - KatzeItem* item; GtkWidget* hbox; GtkWidget* label; GtkTreeModel* liststore; @@ -914,7 +914,6 @@ midori_search_action_get_editor (MidoriSearchAction* search_action, if (new_engine) { - item = katze_item_new (); gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT, FALSE); } @@ -935,9 +934,8 @@ midori_search_action_get_editor (MidoriSearchAction* search_action, g_signal_connect (entry_name, "changed", G_CALLBACK (midori_search_action_editor_name_changed_cb), dialog); gtk_entry_set_activates_default (GTK_ENTRY (entry_name), TRUE); - if (!new_engine) - gtk_entry_set_text (GTK_ENTRY (entry_name), - katze_str_non_null (katze_item_get_name (item))); + gtk_entry_set_text (GTK_ENTRY (entry_name), + katze_str_non_null (katze_item_get_name (item))); gtk_box_pack_start (GTK_BOX (hbox), entry_name, TRUE, TRUE, 0); gtk_container_add (GTK_CONTAINER (content_area), hbox); gtk_widget_show_all (hbox); @@ -969,9 +967,8 @@ midori_search_action_get_editor (MidoriSearchAction* search_action, #endif g_object_set_data (G_OBJECT (entry_uri), "allow_%s", (void*)1); gtk_entry_set_activates_default (GTK_ENTRY (entry_uri), TRUE); - if (!new_engine) - gtk_entry_set_text (GTK_ENTRY (entry_uri) - , katze_str_non_null (katze_item_get_uri (item))); + gtk_entry_set_text (GTK_ENTRY (entry_uri), + katze_str_non_null (katze_item_get_uri (item))); gtk_box_pack_start (GTK_BOX (hbox), entry_uri, TRUE, TRUE, 0); gtk_container_add (GTK_CONTAINER (content_area), hbox); gtk_widget_show_all (hbox); @@ -1036,7 +1033,7 @@ midori_search_action_activate_edit_cb (GtkTreeView *treeview, selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); if (gtk_tree_selection_get_selected (selection, NULL, NULL)) - midori_search_action_get_editor (search_action, FALSE); + midori_search_action_get_editor (search_action, NULL, FALSE); } @@ -1044,7 +1041,7 @@ static void midori_search_action_dialog_add_cb (GtkWidget* widget, MidoriSearchAction* search_action) { - midori_search_action_get_editor (search_action, TRUE); + midori_search_action_get_editor (search_action, katze_item_new (), TRUE); } static void @@ -1057,7 +1054,7 @@ midori_search_action_dialog_edit_cb (GtkWidget* widget, treeview = search_action->treeview; selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); if (gtk_tree_selection_get_selected (selection, NULL, NULL)) - midori_search_action_get_editor (search_action, FALSE); + midori_search_action_get_editor (search_action, NULL, FALSE); } static void diff --git a/midori/midori-view.c b/midori/midori-view.c index 71c44a5f..b37c8cfe 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -2424,6 +2424,93 @@ midori_web_view_menu_inspect_element_activate_cb (GtkWidget* widget, webkit_web_inspector_show (inspector); } +#if WEBKIT_CHECK_VERSION (1, 5, 0) +void +midori_search_action_get_editor (MidoriSearchAction* search_action, + KatzeItem* item, + gboolean new_engine); + +static void +midori_view_menu_add_search_engine_cb (GtkWidget* widget, + MidoriView* view) +{ + MidoriBrowser* browser = midori_browser_get_for_widget (view->web_view); + GtkActionGroup* actions = midori_browser_get_action_group (browser); + GtkAction* action = gtk_action_group_get_action (actions, "Search"); + KatzeItem* item = g_object_get_data (G_OBJECT (widget), "item"); + midori_search_action_get_editor (MIDORI_SEARCH_ACTION (action), item, TRUE); +} + +static KatzeItem* +midori_view_search_engine_for_form (WebKitWebView* web_view) +{ + + WebKitDOMDocument* doc; + WebKitDOMHTMLFormElement* active_form; + WebKitDOMHTMLCollection* form_nodes; + WebKitDOMElement* active_element; + gchar* token_element; + GString* uri_str; + gulong form_len; + guint i; + + #if WEBKIT_CHECK_VERSION (1, 9, 5) + doc = webkit_web_frame_get_dom_document (web_view); + #else + if (webkit_web_view_get_focused_frame (web_view) != webkit_web_view_get_main_frame (web_view)) + return NULL; + doc = webkit_web_view_get_dom_document (web_view); + #endif + + active_element = webkit_dom_html_document_get_active_element ((WebKitDOMHTMLDocument*)doc); + active_form = webkit_dom_html_input_element_get_form ((WebKitDOMHTMLInputElement*)active_element); + token_element = webkit_dom_element_get_attribute (active_element, "name"); + + form_nodes = webkit_dom_html_form_element_get_elements (active_form); + form_len = webkit_dom_html_form_element_get_length (active_form); + + uri_str = g_string_new (webkit_dom_html_form_element_get_action (active_form)); + g_string_append (uri_str, "?"); + + for (i = 0; i < form_len; i++) + { + WebKitDOMNode* form_node = webkit_dom_html_collection_item (form_nodes, i); + WebKitDOMElement* form_element = (WebKitDOMElement*) form_node; + gchar* name = webkit_dom_element_get_attribute (form_element, "name"); + + if (name && *name) + { + if (!g_strcmp0 (token_element, name)) + g_string_append_printf (uri_str, "%s=%s&", name, "\%s"); + else + { + gchar* value; + if (!g_strcmp0 (webkit_dom_element_get_tag_name (form_element), "SELECT")) + { + WebKitDOMHTMLSelectElement* select_element = (WebKitDOMHTMLSelectElement*) form_element; + gulong pos = webkit_dom_html_select_element_get_selected_index (select_element); + WebKitDOMNode* selected_node = webkit_dom_html_select_element_item (select_element, pos); + WebKitDOMElement* selected_element = (WebKitDOMElement*) selected_node; + + value = webkit_dom_element_get_attribute (selected_element, "value"); + } + else + value = webkit_dom_element_get_attribute (form_element, "value"); + + g_string_append_printf (uri_str, "%s=%s&", name, value); + g_free (value); + } + g_free (name); + } + } + + g_free (token_element); + return g_object_new (KATZE_TYPE_ITEM, + "uri", g_string_free (uri_str, FALSE), + "name", webkit_web_view_get_title (web_view), NULL); +} +#endif + static GtkWidget* midori_view_insert_menu_item (GtkMenuShell* menu, gint position, @@ -2529,6 +2616,22 @@ midori_view_populate_popup (MidoriView* view, gtk_widget_set_sensitive (menuitem, webkit_web_view_can_undo (web_view)); gtk_menu_shell_prepend (menu_shell, menuitem); + + #if WEBKIT_CHECK_VERSION (1, 5, 0) + { + KatzeItem* item = midori_view_search_engine_for_form (web_view); + if (item != NULL) + { + menuitem = midori_view_insert_menu_item (menu_shell, -1, + _("Add _search engine..."), NULL, + G_CALLBACK (midori_view_menu_add_search_engine_cb), widget); + g_object_set_data (G_OBJECT (menuitem), "item", item); + gtk_menu_shell_append (menu_shell, menuitem); + gtk_widget_show (menuitem); + } + } + #endif + if (manual) { menuitem = sokoke_action_create_popup_menu_item (