Allow marking a search engine as Default engine

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.
This commit is contained in:
Christian Dywan 2009-04-18 22:41:37 +02:00
parent 59f4a3acdb
commit 18b66ce7d1
4 changed files with 194 additions and 25 deletions

View file

@ -85,6 +85,7 @@ struct _MidoriBrowser
gboolean show_statusbar; gboolean show_statusbar;
gboolean progress_in_location; gboolean progress_in_location;
gboolean remember_last_visited_pages; gboolean remember_last_visited_pages;
gchar* location_entry_search;
gchar* news_aggregator; gchar* news_aggregator;
}; };
@ -2542,18 +2543,15 @@ _action_location_submit_uri (GtkAction* action,
gboolean new_tab, gboolean new_tab,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
gchar* location_entry_search;
gchar* new_uri; gchar* new_uri;
gint n; gint n;
g_object_get (browser->settings, "location-entry-search",
&location_entry_search, NULL);
new_uri = sokoke_magic_uri (uri, browser->search_engines); new_uri = sokoke_magic_uri (uri, browser->search_engines);
if (!new_uri && strstr (location_entry_search, "%s")) if (!new_uri && strstr (browser->location_entry_search, "%s"))
new_uri = g_strdup_printf (location_entry_search, uri); new_uri = g_strdup_printf (browser->location_entry_search, uri);
else if (!new_uri) else if (!new_uri)
new_uri = g_strdup (location_entry_search); new_uri = g_strconcat (browser->location_entry_search, uri, NULL);
g_free (location_entry_search);
if (new_tab) if (new_tab)
{ {
n = midori_browser_add_uri (browser, new_uri); n = midori_browser_add_uri (browser, new_uri);
@ -2597,22 +2595,15 @@ _action_search_submit (GtkAction* action,
guint last_web_search; guint last_web_search;
KatzeItem* item; KatzeItem* item;
const gchar* url; const gchar* url;
gchar* location_entry_search;
gchar* search; gchar* search;
g_object_get (browser->settings, "last-web-search", &last_web_search, NULL); g_object_get (browser->settings, "last-web-search", &last_web_search, NULL);
item = katze_array_get_nth_item (browser->search_engines, last_web_search); item = katze_array_get_nth_item (browser->search_engines, last_web_search);
if (item) if (item)
{
location_entry_search = NULL;
url = katze_item_get_uri (item); url = katze_item_get_uri (item);
}
else /* The location entry search is our fallback */ else /* The location entry search is our fallback */
{ url = browser->location_entry_search;
g_object_get (browser->settings, "location-entry-search",
&location_entry_search, NULL);
url = location_entry_search;
}
if (strstr (url, "%s")) if (strstr (url, "%s"))
search = g_strdup_printf (url, keywords); search = g_strdup_printf (url, keywords);
else else
@ -2624,7 +2615,6 @@ _action_search_submit (GtkAction* action,
midori_browser_set_current_uri (browser, search); midori_browser_set_current_uri (browser, search);
g_free (search); g_free (search);
g_free (location_entry_search);
} }
static void static void
@ -2646,6 +2636,21 @@ _action_search_notify_current_item (GtkAction* action,
g_object_set (browser->settings, "last-web-search", idx, NULL); 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 static void
_action_search_focus_out (GtkAction* action, _action_search_focus_out (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
@ -3970,6 +3975,8 @@ midori_browser_init (MidoriBrowser* browser)
_action_search_focus_out, browser, _action_search_focus_out, browser,
"signal::notify::current-item", "signal::notify::current-item",
_action_search_notify_current_item, browser, _action_search_notify_current_item, browser,
"signal::notify::default-item",
_action_search_notify_default_item, browser,
NULL); NULL);
gtk_action_group_add_action_with_accel (browser->action_group, gtk_action_group_add_action_with_accel (browser->action_group,
action, "<Ctrl>K"); action, "<Ctrl>K");
@ -4457,6 +4464,7 @@ _midori_browser_update_settings (MidoriBrowser* browser)
"toolbar-style", &toolbar_style, "toolbar-style", &toolbar_style,
"toolbar-items", &toolbar_items, "toolbar-items", &toolbar_items,
"last-web-search", &last_web_search, "last-web-search", &last_web_search,
"location-entry-search", &browser->location_entry_search,
"close-buttons-on-tabs", &close_buttons_on_tabs, "close-buttons-on-tabs", &close_buttons_on_tabs,
"progress-in-location", &browser->progress_in_location, "progress-in-location", &browser->progress_in_location,
"remember-last-visited-pages", &browser->remember_last_visited_pages, "remember-last-visited-pages", &browser->remember_last_visited_pages,
@ -4497,11 +4505,22 @@ _midori_browser_update_settings (MidoriBrowser* browser)
if (browser->search_engines) if (browser->search_engines)
{ {
guint i;
item = katze_array_get_nth_item (browser->search_engines, item = katze_array_get_nth_item (browser->search_engines,
last_web_search); last_web_search);
if (item) if (item)
midori_search_action_set_current_item (MIDORI_SEARCH_ACTION ( midori_search_action_set_current_item (MIDORI_SEARCH_ACTION (
_action_by_name (browser, "Search")), item); _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); 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); browser->show_statusbar = g_value_get_boolean (&value);
else if (name == g_intern_string ("progress-in-location")) else if (name == g_intern_string ("progress-in-location"))
browser->progress_in_location = g_value_get_boolean (&value); 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")) else if (name == g_intern_string ("remember-last-visited-pages"))
browser->remember_last_visited_pages = g_value_get_boolean (&value); browser->remember_last_visited_pages = g_value_get_boolean (&value);
else if (name == g_intern_string ("news-aggregator")) else if (name == g_intern_string ("news-aggregator"))
@ -4711,12 +4734,23 @@ midori_browser_set_property (GObject* object,
/* FIXME: Connect to updates */ /* FIXME: Connect to updates */
if (browser->settings) if (browser->settings)
{ {
guint i;
g_object_get (browser->settings, "last-web-search", g_object_get (browser->settings, "last-web-search",
&last_web_search, NULL); &last_web_search, NULL);
item = katze_array_get_nth_item (browser->search_engines, item = katze_array_get_nth_item (browser->search_engines,
last_web_search); last_web_search);
midori_search_action_set_current_item (MIDORI_SEARCH_ACTION ( midori_search_action_set_current_item (MIDORI_SEARCH_ACTION (
_action_by_name (browser, "Search")), item); _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; break;
case PROP_HISTORY: case PROP_HISTORY:

View file

@ -506,7 +506,7 @@ midori_preferences_set_settings (MidoriPreferences* preferences,
/* Page "Behavior" */ /* Page "Behavior" */
PAGE_NEW (GTK_STOCK_SELECT_COLOR, _("Behavior")); PAGE_NEW (GTK_STOCK_SELECT_COLOR, _("Behavior"));
FRAME_NEW (_("Features")); FRAME_NEW (_("Features"));
TABLE_NEW (7, 2); TABLE_NEW (6, 2);
button = katze_property_proxy (settings, "auto-load-images", NULL); button = katze_property_proxy (settings, "auto-load-images", NULL);
gtk_button_set_label (GTK_BUTTON (button), _("Load images automatically")); gtk_button_set_label (GTK_BUTTON (button), _("Load images automatically"));
gtk_widget_set_tooltip_text (button, _("Load and display 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); button = katze_property_proxy (settings, "zoom-text-and-images", NULL);
SPANNED_ADD (button, 0, 1, 4, 5); SPANNED_ADD (button, 0, 1, 4, 5);
button = katze_property_proxy (settings, "find-while-typing", NULL); 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 "Interface" */
PAGE_NEW (GTK_STOCK_CONVERT, _("Interface")); PAGE_NEW (GTK_STOCK_CONVERT, _("Interface"));

View file

@ -25,6 +25,7 @@ struct _MidoriSearchAction
KatzeArray* search_engines; KatzeArray* search_engines;
KatzeItem* current_item; KatzeItem* current_item;
KatzeItem* default_item;
gchar* text; gchar* text;
KatzeNet* net; KatzeNet* net;
@ -35,6 +36,7 @@ struct _MidoriSearchAction
GtkWidget* treeview; GtkWidget* treeview;
GtkWidget* edit_button; GtkWidget* edit_button;
GtkWidget* remove_button; GtkWidget* remove_button;
GtkWidget* default_button;
}; };
struct _MidoriSearchActionClass struct _MidoriSearchActionClass
@ -50,6 +52,7 @@ enum
PROP_SEARCH_ENGINES, PROP_SEARCH_ENGINES,
PROP_CURRENT_ITEM, PROP_CURRENT_ITEM,
PROP_DEFAULT_ITEM,
PROP_TEXT, PROP_TEXT,
PROP_DIALOG PROP_DIALOG
}; };
@ -147,6 +150,22 @@ midori_search_action_class_init (MidoriSearchActionClass* class)
KATZE_TYPE_ITEM, KATZE_TYPE_ITEM,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); 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, g_object_class_install_property (gobject_class,
PROP_TEXT, PROP_TEXT,
g_param_spec_string ( g_param_spec_string (
@ -171,6 +190,7 @@ midori_search_action_init (MidoriSearchAction* search_action)
{ {
search_action->search_engines = NULL; search_action->search_engines = NULL;
search_action->current_item = NULL; search_action->current_item = NULL;
search_action->default_item = NULL;
search_action->text = NULL; search_action->text = NULL;
search_action->net = katze_net_new (); search_action->net = katze_net_new ();
@ -181,6 +201,7 @@ midori_search_action_init (MidoriSearchAction* search_action)
search_action->treeview = NULL; search_action->treeview = NULL;
search_action->edit_button = NULL; search_action->edit_button = NULL;
search_action->remove_button = NULL; search_action->remove_button = NULL;
search_action->default_button = NULL;
} }
static void static void
@ -213,6 +234,10 @@ midori_search_action_set_property (GObject* object,
midori_search_action_set_current_item (search_action, midori_search_action_set_current_item (search_action,
g_value_get_object (value)); g_value_get_object (value));
break; break;
case PROP_DEFAULT_ITEM:
midori_search_action_set_default_item (search_action,
g_value_get_object (value));
break;
case PROP_TEXT: case PROP_TEXT:
midori_search_action_set_text (search_action, midori_search_action_set_text (search_action,
g_value_get_string (value)); g_value_get_string (value));
@ -239,6 +264,9 @@ midori_search_action_get_property (GObject* object,
case PROP_CURRENT_ITEM: case PROP_CURRENT_ITEM:
g_value_set_object (value, search_action->current_item); g_value_set_object (value, search_action->current_item);
break; break;
case PROP_DEFAULT_ITEM:
g_value_set_object (value, search_action->default_item);
break;
case PROP_TEXT: case PROP_TEXT:
g_value_set_string (value, search_action->text); g_value_set_string (value, search_action->text);
break; break;
@ -716,6 +744,67 @@ midori_search_action_set_current_item (MidoriSearchAction* search_action,
while ((proxies = g_slist_next (proxies))); 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 static void
midori_search_action_dialog_render_icon_cb (GtkTreeViewColumn* column, midori_search_action_dialog_render_icon_cb (GtkTreeViewColumn* column,
GtkCellRenderer* renderer, 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); icon = midori_search_action_get_icon (search_action->net, item, treeview);
g_object_set (renderer, "pixbuf", icon, "yalign", 0.25, NULL); g_object_set (renderer, "pixbuf", icon, "yalign", 0.25, NULL);
g_object_unref (icon); g_object_unref (icon);
g_object_unref (item);
} }
static void 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 : ""); markup = g_markup_printf_escaped ("<b>%s</b>\n%s", name, text ? text : "");
g_object_set (renderer, "markup", markup, NULL); g_object_set (renderer, "markup", markup, NULL);
g_free (markup); g_free (markup);
g_object_unref (item);
} }
static void static void
@ -771,6 +862,7 @@ midori_search_action_dialog_render_token (GtkTreeViewColumn* column,
markup = g_markup_printf_escaped ("<b>%s</b>", token ? token : ""); markup = g_markup_printf_escaped ("<b>%s</b>", token ? token : "");
g_object_set (renderer, "markup", markup, "yalign", 0.0, NULL); g_object_set (renderer, "markup", markup, "yalign", 0.0, NULL);
g_free (markup); g_free (markup);
g_object_unref (item);
} }
static void 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 static void
midori_search_action_treeview_selection_cb (GtkTreeSelection* selection, midori_search_action_treeview_selection_cb (GtkTreeSelection* selection,
MidoriSearchAction* search_action) 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->edit_button, selected);
gtk_widget_set_sensitive (search_action->remove_button, selected); gtk_widget_set_sensitive (search_action->remove_button, selected);
gtk_widget_set_sensitive (search_action->default_button, selected);
} }
static void static void
@ -1068,6 +1184,7 @@ midori_search_action_get_dialog (MidoriSearchAction* search_action)
KatzeItem* item; KatzeItem* item;
GtkWidget* vbox; GtkWidget* vbox;
GtkWidget* button; GtkWidget* button;
GtkWidget* image;
g_return_val_if_fail (MIDORI_IS_SEARCH_ACTION (search_action), NULL); 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_dialog_set_response_sensitive (GTK_DIALOG (dialog),
GTK_RESPONSE_HELP, FALSE); GTK_RESPONSE_HELP, FALSE);
sokoke_widget_get_text_size (dialog, "M", &width, &height); 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_signal_connect (dialog, "response",
G_CALLBACK (gtk_widget_destroy), dialog); G_CALLBACK (gtk_widget_destroy), dialog);
/* TODO: Do we want tooltips for explainations or can we omit that? /* 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), "changed", G_CALLBACK (midori_search_action_treeview_selection_cb),
search_action); search_action);
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE); 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 (); column = gtk_tree_view_column_new ();
renderer_pixbuf = gtk_cell_renderer_pixbuf_new (); renderer_pixbuf = gtk_cell_renderer_pixbuf_new ();
gtk_tree_view_column_pack_start (column, renderer_pixbuf, FALSE); 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, gtk_tree_view_column_set_cell_data_func (column, renderer_pixbuf,
(GtkTreeCellDataFunc)midori_search_action_dialog_render_icon_cb, (GtkTreeCellDataFunc)midori_search_action_dialog_render_icon_cb,
treeview, NULL); treeview, NULL);
@ -1166,6 +1288,17 @@ midori_search_action_get_dialog (MidoriSearchAction* search_action)
if (!i) if (!i)
gtk_widget_set_sensitive (button, FALSE); gtk_widget_set_sensitive (button, FALSE);
button = gtk_label_new (""); /* This is an invisible separator */ 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); gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 12);
button = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN); button = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
gtk_widget_set_sensitive (button, FALSE); gtk_widget_set_sensitive (button, FALSE);

View file

@ -56,6 +56,13 @@ void
midori_search_action_set_current_item (MidoriSearchAction* search_action, midori_search_action_set_current_item (MidoriSearchAction* search_action,
KatzeItem* item); 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* GtkWidget*
midori_search_action_get_dialog (MidoriSearchAction* search_action); midori_search_action_get_dialog (MidoriSearchAction* search_action);