]> spindle.queued.net Git - midori/commitdiff
Allow marking a search engine as Default engine
authorChristian Dywan <christian@twotoasts.de>
Sat, 18 Apr 2009 20:41:37 +0000 (22:41 +0200)
committerChristian Dywan <christian@twotoasts.de>
Sat, 18 Apr 2009 20:41:37 +0000 (22:41 +0200)
The default engine is used when search terms are typed in the
address entry without tokens and for the 'Search the Web' item
in the selection context menu.

The 'Location entry search' is removed from the preferences. The
setting 'location-entry-search' remains functional however.

midori/midori-browser.c
midori/midori-preferences.c
midori/midori-searchaction.c
midori/midori-searchaction.h

index 48b205c91af3f1cc2795ca13fa55e8ec028cb4d5..f329c896ea9e48128c3e40cfe9001e44c42cac73 100644 (file)
@@ -85,6 +85,7 @@ struct _MidoriBrowser
     gboolean show_statusbar;
     gboolean progress_in_location;
     gboolean remember_last_visited_pages;
+    gchar* location_entry_search;
     gchar* news_aggregator;
 };
 
@@ -2542,18 +2543,15 @@ _action_location_submit_uri (GtkAction*     action,
                              gboolean       new_tab,
                              MidoriBrowser* browser)
 {
-    gchar* location_entry_search;
     gchar* new_uri;
     gint n;
 
-    g_object_get (browser->settings, "location-entry-search",
-                  &location_entry_search, NULL);
     new_uri = sokoke_magic_uri (uri, browser->search_engines);
-    if (!new_uri && strstr (location_entry_search, "%s"))
-        new_uri = g_strdup_printf (location_entry_search, uri);
+    if (!new_uri && strstr (browser->location_entry_search, "%s"))
+        new_uri = g_strdup_printf (browser->location_entry_search, uri);
     else if (!new_uri)
-        new_uri = g_strdup (location_entry_search);
-    g_free (location_entry_search);
+        new_uri = g_strconcat (browser->location_entry_search, uri, NULL);
+
     if (new_tab)
     {
         n = midori_browser_add_uri (browser, new_uri);
@@ -2597,22 +2595,15 @@ _action_search_submit (GtkAction*     action,
     guint last_web_search;
     KatzeItem* item;
     const gchar* url;
-    gchar* location_entry_search;
     gchar* search;
 
     g_object_get (browser->settings, "last-web-search", &last_web_search, NULL);
     item = katze_array_get_nth_item (browser->search_engines, last_web_search);
     if (item)
-    {
-        location_entry_search = NULL;
         url = katze_item_get_uri (item);
-    }
     else /* The location entry search is our fallback */
-    {
-        g_object_get (browser->settings, "location-entry-search",
-                      &location_entry_search, NULL);
-        url = location_entry_search;
-    }
+        url = browser->location_entry_search;
+
     if (strstr (url, "%s"))
         search = g_strdup_printf (url, keywords);
     else
@@ -2624,7 +2615,6 @@ _action_search_submit (GtkAction*     action,
         midori_browser_set_current_uri (browser, search);
 
     g_free (search);
-    g_free (location_entry_search);
 }
 
 static void
@@ -2646,6 +2636,21 @@ _action_search_notify_current_item (GtkAction*     action,
     g_object_set (browser->settings, "last-web-search", idx, NULL);
 }
 
+static void
+_action_search_notify_default_item (GtkAction*     action,
+                                    GParamSpec*    pspec,
+                                    MidoriBrowser* browser)
+{
+    MidoriSearchAction* search_action;
+    KatzeItem* item;
+
+    search_action = MIDORI_SEARCH_ACTION (action);
+    item = midori_search_action_get_default_item (search_action);
+    if (item)
+        g_object_set (browser->settings, "location-entry-search",
+                      katze_item_get_uri (item), NULL);
+}
+
 static void
 _action_search_focus_out (GtkAction*     action,
                           MidoriBrowser* browser)
@@ -3970,6 +3975,8 @@ midori_browser_init (MidoriBrowser* browser)
                       _action_search_focus_out, browser,
                       "signal::notify::current-item",
                       _action_search_notify_current_item, browser,
+                      "signal::notify::default-item",
+                      _action_search_notify_default_item, browser,
                       NULL);
     gtk_action_group_add_action_with_accel (browser->action_group,
         action, "<Ctrl>K");
@@ -4457,6 +4464,7 @@ _midori_browser_update_settings (MidoriBrowser* browser)
                   "toolbar-style", &toolbar_style,
                   "toolbar-items", &toolbar_items,
                   "last-web-search", &last_web_search,
+                  "location-entry-search", &browser->location_entry_search,
                   "close-buttons-on-tabs", &close_buttons_on_tabs,
                   "progress-in-location", &browser->progress_in_location,
                   "remember-last-visited-pages", &browser->remember_last_visited_pages,
@@ -4497,11 +4505,22 @@ _midori_browser_update_settings (MidoriBrowser* browser)
 
     if (browser->search_engines)
     {
+        guint i;
+
         item = katze_array_get_nth_item (browser->search_engines,
                                          last_web_search);
         if (item)
             midori_search_action_set_current_item (MIDORI_SEARCH_ACTION (
                 _action_by_name (browser, "Search")), item);
+
+        i = 0;
+        while ((item = katze_array_get_nth_item (browser->search_engines, i++)))
+            if (!g_strcmp0 (katze_item_get_uri (item), browser->location_entry_search))
+            {
+                midori_search_action_set_default_item (MIDORI_SEARCH_ACTION (
+                _action_by_name (browser, "Search")), item);
+                break;
+            }
     }
 
     midori_panel_set_compact (MIDORI_PANEL (browser->panel), compact_sidepanel);
@@ -4550,6 +4569,10 @@ midori_browser_settings_notify (MidoriWebSettings* web_settings,
         browser->show_statusbar = g_value_get_boolean (&value);
     else if (name == g_intern_string ("progress-in-location"))
         browser->progress_in_location = g_value_get_boolean (&value);
+    else if (name == g_intern_string ("location-entry-search"))
+    {
+        katze_assign (browser->location_entry_search, g_value_dup_string (&value));
+    }
     else if (name == g_intern_string ("remember-last-visited-pages"))
         browser->remember_last_visited_pages = g_value_get_boolean (&value);
     else if (name == g_intern_string ("news-aggregator"))
@@ -4711,12 +4734,23 @@ midori_browser_set_property (GObject*      object,
         /* FIXME: Connect to updates */
         if (browser->settings)
         {
+            guint i;
+
             g_object_get (browser->settings, "last-web-search",
                           &last_web_search, NULL);
             item = katze_array_get_nth_item (browser->search_engines,
                                              last_web_search);
             midori_search_action_set_current_item (MIDORI_SEARCH_ACTION (
                 _action_by_name (browser, "Search")), item);
+
+            i = 0;
+            while ((item = katze_array_get_nth_item (browser->search_engines, i++)))
+                if (!g_strcmp0 (katze_item_get_uri (item), browser->location_entry_search))
+                {
+                    midori_search_action_set_default_item (MIDORI_SEARCH_ACTION (
+                    _action_by_name (browser, "Search")), item);
+                    break;
+                }
         }
         break;
     case PROP_HISTORY:
index e9c058850c81eaee766f211b04436f6f97e1d3c6..3c492e1a6e4d79ddfb26aa77875b62d580429df2 100644 (file)
@@ -506,7 +506,7 @@ midori_preferences_set_settings (MidoriPreferences* preferences,
     /* Page "Behavior" */
     PAGE_NEW (GTK_STOCK_SELECT_COLOR, _("Behavior"));
     FRAME_NEW (_("Features"));
-    TABLE_NEW (7, 2);
+    TABLE_NEW (6, 2);
     button = katze_property_proxy (settings, "auto-load-images", NULL);
     gtk_button_set_label (GTK_BUTTON (button), _("Load images automatically"));
     gtk_widget_set_tooltip_text (button, _("Load and display images automatically"));
@@ -542,11 +542,6 @@ midori_preferences_set_settings (MidoriPreferences* preferences,
     button = katze_property_proxy (settings, "zoom-text-and-images", NULL);
     SPANNED_ADD (button, 0, 1, 4, 5);
     button = katze_property_proxy (settings, "find-while-typing", NULL);
-    SPANNED_ADD (button, 1, 2, 4, 5);
-    label = katze_property_label (settings, "location-entry-search");
-    INDENTED_ADD (label, 0, 1, 5, 6);
-    entry = katze_property_proxy (settings, "location-entry-search", NULL);
-    FILLED_ADD (entry, 1, 2, 5, 6);
 
     /* Page "Interface" */
     PAGE_NEW (GTK_STOCK_CONVERT, _("Interface"));
index e8e860dc1abba53211fd7d14df4b05b15a1b30aa..104121e30fb0addf1eb1f2ebccd2195c3aa36c8f 100644 (file)
@@ -25,6 +25,7 @@ struct _MidoriSearchAction
 
     KatzeArray* search_engines;
     KatzeItem* current_item;
+    KatzeItem* default_item;
     gchar* text;
 
     KatzeNet* net;
@@ -35,6 +36,7 @@ struct _MidoriSearchAction
     GtkWidget* treeview;
     GtkWidget* edit_button;
     GtkWidget* remove_button;
+    GtkWidget* default_button;
 };
 
 struct _MidoriSearchActionClass
@@ -50,6 +52,7 @@ enum
 
     PROP_SEARCH_ENGINES,
     PROP_CURRENT_ITEM,
+    PROP_DEFAULT_ITEM,
     PROP_TEXT,
     PROP_DIALOG
 };
@@ -147,6 +150,22 @@ midori_search_action_class_init (MidoriSearchActionClass* class)
                                      KATZE_TYPE_ITEM,
                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+    /**
+     * MidoriSearchAction:default-item:
+     *
+     * The default search engine.
+     *
+     * Since: 0.1.6
+     */
+    g_object_class_install_property (gobject_class,
+                                     PROP_DEFAULT_ITEM,
+                                     g_param_spec_object (
+                                     "default-item",
+                                     "Default Item",
+                                     "The default search engine",
+                                     KATZE_TYPE_ITEM,
+                                     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
     g_object_class_install_property (gobject_class,
                                      PROP_TEXT,
                                      g_param_spec_string (
@@ -171,6 +190,7 @@ midori_search_action_init (MidoriSearchAction* search_action)
 {
     search_action->search_engines = NULL;
     search_action->current_item = NULL;
+    search_action->default_item = NULL;
     search_action->text = NULL;
 
     search_action->net = katze_net_new ();
@@ -181,6 +201,7 @@ midori_search_action_init (MidoriSearchAction* search_action)
     search_action->treeview = NULL;
     search_action->edit_button = NULL;
     search_action->remove_button = NULL;
+    search_action->default_button = NULL;
 }
 
 static void
@@ -213,6 +234,10 @@ midori_search_action_set_property (GObject*      object,
         midori_search_action_set_current_item (search_action,
                                                g_value_get_object (value));
         break;
+    case PROP_DEFAULT_ITEM:
+        midori_search_action_set_default_item (search_action,
+                                               g_value_get_object (value));
+        break;
     case PROP_TEXT:
         midori_search_action_set_text (search_action,
                                        g_value_get_string (value));
@@ -239,6 +264,9 @@ midori_search_action_get_property (GObject*    object,
     case PROP_CURRENT_ITEM:
         g_value_set_object (value, search_action->current_item);
         break;
+    case PROP_DEFAULT_ITEM:
+        g_value_set_object (value, search_action->default_item);
+        break;
     case PROP_TEXT:
         g_value_set_string (value, search_action->text);
         break;
@@ -716,6 +744,67 @@ midori_search_action_set_current_item (MidoriSearchAction* search_action,
     while ((proxies = g_slist_next (proxies)));
 }
 
+/**
+ * midori_search_action_get_default_item:
+ * @search_action: a #MidoriSearchAction
+ *
+ * Retrieves the default search engine.
+ *
+ * Since 0.1.6
+ *
+ * Return value: a #KatzeItem
+ **/
+KatzeItem*
+midori_search_action_get_default_item (MidoriSearchAction* search_action)
+{
+    g_return_val_if_fail (MIDORI_IS_SEARCH_ACTION (search_action), NULL);
+
+    return search_action->default_item;
+}
+
+/**
+ * midori_search_action_set_default_item:
+ * @search_action: a #MidoriSearchAction
+ * @item: a #KatzeItem
+ *
+ * Sets the default search engine.
+ *
+ * Since 0.1.6
+ **/
+void
+midori_search_action_set_default_item (MidoriSearchAction* search_action,
+                                       KatzeItem*          item)
+{
+    g_return_if_fail (MIDORI_IS_SEARCH_ACTION (search_action));
+    g_return_if_fail (!item || KATZE_IS_ITEM (item));
+
+    if (item)
+        g_object_ref (item);
+    katze_object_assign (search_action->default_item, item);
+    g_object_notify (G_OBJECT (search_action), "default-item");
+}
+
+static void
+midori_search_action_dialog_render_tick_cb (GtkTreeViewColumn* column,
+                                            GtkCellRenderer*   renderer,
+                                            GtkTreeModel*      model,
+                                            GtkTreeIter*       iter,
+                                            GtkWidget*         treeview)
+{
+    KatzeItem* item;
+    MidoriSearchAction* search_action;
+    gint width;
+
+    gtk_tree_model_get (model, iter, 0, &item, -1);
+
+    search_action = g_object_get_data (G_OBJECT (treeview), "search-action");
+    gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, NULL);
+    g_object_set (renderer, "stock-id",
+        search_action->default_item == item ? GTK_STOCK_YES : NULL,
+        "width", width + 4, NULL);
+    g_object_unref (item);
+}
+
 static void
 midori_search_action_dialog_render_icon_cb (GtkTreeViewColumn* column,
                                             GtkCellRenderer*   renderer,
@@ -733,6 +822,7 @@ midori_search_action_dialog_render_icon_cb (GtkTreeViewColumn* column,
     icon = midori_search_action_get_icon (search_action->net, item, treeview);
     g_object_set (renderer, "pixbuf", icon, "yalign", 0.25, NULL);
     g_object_unref (icon);
+    g_object_unref (item);
 }
 
 static void
@@ -753,6 +843,7 @@ midori_search_action_dialog_render_text (GtkTreeViewColumn* column,
     markup = g_markup_printf_escaped ("<b>%s</b>\n%s", name, text ? text : "");
     g_object_set (renderer, "markup", markup, NULL);
     g_free (markup);
+    g_object_unref (item);
 }
 
 static void
@@ -771,6 +862,7 @@ midori_search_action_dialog_render_token (GtkTreeViewColumn* column,
     markup = g_markup_printf_escaped ("<b>%s</b>", token ? token : "");
     g_object_set (renderer, "markup", markup, "yalign", 0.0, NULL);
     g_free (markup);
+    g_object_unref (item);
 }
 
 static void
@@ -967,6 +1059,29 @@ midori_search_action_dialog_remove_cb (GtkWidget*          widget,
     }
 }
 
+static void
+midori_search_action_dialog_default_cb (GtkWidget*          widget,
+                                        MidoriSearchAction* search_action)
+{
+    KatzeArray* search_engines;
+    GtkWidget* treeview;
+    GtkTreeSelection* selection;
+    GtkTreeModel* liststore;
+    GtkTreeIter iter;
+    KatzeItem* item;
+
+    search_engines = search_action->search_engines;
+    treeview = search_action->treeview;
+    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
+    if (gtk_tree_selection_get_selected (selection, &liststore, &iter))
+    {
+        gtk_tree_model_get (liststore, &iter, 0, &item, -1);
+        midori_search_action_set_default_item (search_action, item);
+        g_object_unref (item);
+        gtk_widget_queue_draw (treeview);
+    }
+}
+
 static void
 midori_search_action_treeview_selection_cb (GtkTreeSelection*   selection,
                                             MidoriSearchAction* search_action)
@@ -977,6 +1092,7 @@ midori_search_action_treeview_selection_cb (GtkTreeSelection*   selection,
 
     gtk_widget_set_sensitive (search_action->edit_button, selected);
     gtk_widget_set_sensitive (search_action->remove_button, selected);
+    gtk_widget_set_sensitive (search_action->default_button, selected);
 }
 
 static void
@@ -1068,6 +1184,7 @@ midori_search_action_get_dialog (MidoriSearchAction* search_action)
     KatzeItem* item;
     GtkWidget* vbox;
     GtkWidget* button;
+    GtkWidget* image;
 
     g_return_val_if_fail (MIDORI_IS_SEARCH_ACTION (search_action), NULL);
 
@@ -1093,7 +1210,7 @@ midori_search_action_get_dialog (MidoriSearchAction* search_action)
     gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                        GTK_RESPONSE_HELP, FALSE);
     sokoke_widget_get_text_size (dialog, "M", &width, &height);
-    gtk_window_set_default_size (GTK_WINDOW (dialog), width * 42, -1);
+    gtk_window_set_default_size (GTK_WINDOW (dialog), width * 52, -1);
     g_signal_connect (dialog, "response",
                       G_CALLBACK (gtk_widget_destroy), dialog);
     /* TODO: Do we want tooltips for explainations or can we omit that?
@@ -1112,10 +1229,15 @@ midori_search_action_get_dialog (MidoriSearchAction* search_action)
         "changed", G_CALLBACK (midori_search_action_treeview_selection_cb),
         search_action);
     gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
+    g_object_set_data (G_OBJECT (treeview), "search-action", search_action);
     column = gtk_tree_view_column_new ();
     renderer_pixbuf = gtk_cell_renderer_pixbuf_new ();
     gtk_tree_view_column_pack_start (column, renderer_pixbuf, FALSE);
-    g_object_set_data (G_OBJECT (treeview), "search-action", search_action);
+    gtk_tree_view_column_set_cell_data_func (column, renderer_pixbuf,
+        (GtkTreeCellDataFunc)midori_search_action_dialog_render_tick_cb,
+        treeview, NULL);
+    renderer_pixbuf = gtk_cell_renderer_pixbuf_new ();
+    gtk_tree_view_column_pack_start (column, renderer_pixbuf, FALSE);
     gtk_tree_view_column_set_cell_data_func (column, renderer_pixbuf,
         (GtkTreeCellDataFunc)midori_search_action_dialog_render_icon_cb,
         treeview, NULL);
@@ -1166,6 +1288,17 @@ midori_search_action_get_dialog (MidoriSearchAction* search_action)
     if (!i)
         gtk_widget_set_sensitive (button, FALSE);
     button = gtk_label_new (""); /* This is an invisible separator */
+    gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 8);
+    button = gtk_button_new_with_mnemonic ("Use as _default");
+    image = gtk_image_new_from_stock (GTK_STOCK_YES, GTK_ICON_SIZE_BUTTON);
+    gtk_button_set_image (GTK_BUTTON (button), image);
+    search_action->default_button = button;
+    g_signal_connect (button, "clicked",
+        G_CALLBACK (midori_search_action_dialog_default_cb), search_action);
+    gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
+    if (!i)
+        gtk_widget_set_sensitive (button, FALSE);
+    button = gtk_label_new (""); /* This is an invisible separator */
     gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 12);
     button = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
     gtk_widget_set_sensitive (button, FALSE);
index a2cecaf231f9ba1f21ea45cb0717572d6994257a..760dd4e519c71f4ad53716e0642ffc6bf9fb825f 100644 (file)
@@ -56,6 +56,13 @@ void
 midori_search_action_set_current_item      (MidoriSearchAction* search_action,
                                             KatzeItem*          item);
 
+KatzeItem*
+midori_search_action_get_default_item      (MidoriSearchAction* search_action);
+
+void
+midori_search_action_set_default_item      (MidoriSearchAction* search_action,
+                                            KatzeItem*          item);
+
 GtkWidget*
 midori_search_action_get_dialog            (MidoriSearchAction* search_action);