diff --git a/terminal/terminal-screen.c b/terminal/terminal-screen.c index 8831573..11107ae 100644 --- a/terminal/terminal-screen.c +++ b/terminal/terminal-screen.c @@ -2254,3 +2254,59 @@ terminal_screen_search_find_previous (TerminalScreen *screen) terminal_return_if_fail (TERMINAL_IS_SCREEN (screen)); vte_terminal_search_find_previous (VTE_TERMINAL (screen->terminal)); } + + + +/* FIXME: This is totally unsafe and ugly, we need another way to sanitize the query text */ +gchar *clear_text(const gchar * str) +{ + gchar *c; + gchar *s = g_new (gchar, strlen(str) + 1); + g_stpcpy(s, str); + for (c = s; *c; c++) + if(*c == ' ' || *c == '\t' || *c == '\r' || *c == '\n' || *c == '\x0b' || *c == '\'') + *c = '+'; + + return s; +} + + +/* As the above function, this one must be renamed and put in a more adequate place */ +void get_clipboard_callback(GtkClipboard *clipboard, + const gchar *text, + gpointer data) +{ + GError *error = NULL; + GdkScreen *screen; + gchar *uri; + gchar *query; + + query = clear_text(text); + /* TODO: This search uri could be user-defined at preferences */ + uri = g_strconcat("http://www.google.com/search?q=", query, NULL); + + screen = gtk_widget_get_screen (GTK_WIDGET (data)); + + /* When firefox is the default browser, I get errors like: + * Glib-CRITICAL Failed to send command: 500 command not parseable */ + if (!gtk_show_uri (screen, uri, gtk_get_current_event_time (), &error)) + { + /* tell the user that we were unable to open the responsible application */ + xfce_dialog_show_error (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (data))), + error, _("Failed to open the URL `%s'"), uri); + g_error_free (error); + } + + g_free (query); + g_free (uri); +} + + +void +terminal_screen_web_search (TerminalScreen *screen) +{ + terminal_return_val_if_fail (TERMINAL_IS_SCREEN (screen), FALSE); + /* FIXME: Is there a better way to get the selected text? */ + vte_terminal_copy_primary (VTE_TERMINAL (screen->terminal)); + gtk_clipboard_request_text (gtk_clipboard_get(GDK_SELECTION_PRIMARY), get_clipboard_callback, screen); +} diff --git a/terminal/terminal-screen.h b/terminal/terminal-screen.h index 9da1506..dd88dde 100644 --- a/terminal/terminal-screen.h +++ b/terminal/terminal-screen.h @@ -110,6 +110,8 @@ void terminal_screen_search_find_next (TerminalScreen *scree void terminal_screen_search_find_previous (TerminalScreen *screen); +void terminal_screen_web_search (TerminalScreen *screen); + G_END_DECLS diff --git a/terminal/terminal-window-ui.xml b/terminal/terminal-window-ui.xml index 16dda85..bf3bee2 100644 --- a/terminal/terminal-window-ui.xml +++ b/terminal/terminal-window-ui.xml @@ -67,6 +67,8 @@ + + diff --git a/terminal/terminal-window.c b/terminal/terminal-window.c index 8dd564d..e3228dd 100644 --- a/terminal/terminal-window.c +++ b/terminal/terminal-window.c @@ -144,6 +144,8 @@ static void terminal_window_action_paste (GtkAction TerminalWindow *window); static void terminal_window_action_paste_selection (GtkAction *action, TerminalWindow *window); +static void terminal_window_action_web_search (GtkAction *action, + TerminalWindow *window); static void terminal_window_action_select_all (GtkAction *action, TerminalWindow *window); static void terminal_window_action_prefs (GtkAction *action, @@ -203,6 +205,7 @@ static const GtkActionEntry action_entries[] = { "copy", GTK_STOCK_COPY, N_ ("_Copy"), "c", N_ ("Copy to clipboard"), G_CALLBACK (terminal_window_action_copy), }, { "paste", GTK_STOCK_PASTE, N_ ("_Paste"), "v", N_ ("Paste from clipboard"), G_CALLBACK (terminal_window_action_paste), }, { "paste-selection", NULL, N_ ("Paste _Selection"), NULL, NULL, G_CALLBACK (terminal_window_action_paste_selection), }, + { "web-search", GTK_STOCK_FIND, N_ ("Web Search"), NULL, NULL, G_CALLBACK (terminal_window_action_web_search), }, { "select-all", GTK_STOCK_SELECT_ALL, N_ ("Select _All"), "a", NULL, G_CALLBACK (terminal_window_action_select_all), }, { "preferences", GTK_STOCK_PREFERENCES, N_ ("Pr_eferences..."), NULL, N_ ("Open the preferences dialog"), G_CALLBACK (terminal_window_action_prefs), }, { "view-menu", NULL, N_ ("_View"), NULL, NULL, NULL, }, @@ -376,6 +379,7 @@ terminal_window_init (TerminalWindow *window) window->action_move_tab_left = gtk_action_group_get_action (window->action_group, "move-tab-left"); window->action_move_tab_right = gtk_action_group_get_action (window->action_group, "move-tab-right"); window->action_copy = gtk_action_group_get_action (window->action_group, "copy"); + window->action_web_search = gtk_action_group_get_action (window->action_group, "web-search"); window->action_search_next = gtk_action_group_get_action (window->action_group, "search-next"); window->action_search_prev = gtk_action_group_get_action (window->action_group, "search-prev"); window->action_fullscreen = gtk_action_group_get_action (window->action_group, "fullscreen"); @@ -694,6 +698,9 @@ terminal_window_update_actions (TerminalWindow *window) gtk_action_set_sensitive (window->action_copy, terminal_screen_has_selection (window->active)); + gtk_action_set_sensitive (window->action_web_search, + terminal_screen_has_selection (window->active)); + can_search = terminal_screen_search_has_gregex (window->active); gtk_action_set_sensitive (window->action_search_next, can_search); gtk_action_set_sensitive (window->action_search_prev, can_search); @@ -1327,6 +1334,16 @@ terminal_window_action_paste_selection (GtkAction *action, static void +terminal_window_action_web_search (GtkAction *action, + TerminalWindow *window) +{ + if (G_LIKELY (window->active != NULL)) + terminal_screen_web_search (window->active); +} + + + +static void terminal_window_action_select_all (GtkAction *action, TerminalWindow *window) { diff --git a/terminal/terminal-window.h b/terminal/terminal-window.h index 7d5034f..5527da2 100644 --- a/terminal/terminal-window.h +++ b/terminal/terminal-window.h @@ -83,6 +83,7 @@ struct _TerminalWindow GtkAction *action_move_tab_left; GtkAction *action_move_tab_right; GtkAction *action_copy; + GtkAction *action_web_search; GtkAction *action_search_next; GtkAction *action_search_prev; GtkAction *action_fullscreen;