From ca8d1813292bdbc1c00f5233497e2c18f2fb74e4 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Mon, 20 Apr 2009 19:10:13 +0200 Subject: [PATCH] Unify opening URIs externally with good fallbacks --- midori/midori-browser.c | 30 ++++++---------------------- midori/midori-view.c | 8 +------- midori/sokoke.c | 42 +++++++++++++++++++++++++++++++++++++++ midori/sokoke.h | 6 ++++++ panels/midori-transfers.c | 8 +------- 5 files changed, 56 insertions(+), 38 deletions(-) diff --git a/midori/midori-browser.c b/midori/midori-browser.c index 1a89ff45..02f5a3ed 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -939,15 +939,8 @@ midori_browser_download_button_clicked_cb (GtkWidget* button, case WEBKIT_DOWNLOAD_STATUS_FINISHED: { const gchar* uri = webkit_download_get_destination_uri (download); - gboolean success = gtk_show_uri (gtk_widget_get_screen (button), - uri, gtk_get_current_event_time (), NULL); - if (!success) - { - gchar* command = g_strconcat ("exo-open ", uri, NULL); - success = g_spawn_command_line_async (command, NULL); - g_free (command); - } - if (success) + if (sokoke_show_uri (gtk_widget_get_screen (button), + uri, gtk_get_current_event_time (), NULL)) gtk_widget_destroy (gtk_widget_get_parent (button)); break; } @@ -2402,8 +2395,6 @@ midori_browser_source_transfer_cb (KatzeNetRequest* request, { if ((fp = fdopen (fd, "w"))) { - gboolean success; - ret = fwrite (request->data, 1, request->length, fp); fclose (fp); if ((ret - request->length) != 0) @@ -2415,13 +2406,10 @@ midori_browser_source_transfer_cb (KatzeNetRequest* request, g_object_get (browser->settings, "text-editor", &text_editor, NULL); if (text_editor && *text_editor) - success = sokoke_spawn_program (text_editor, unique_filename); + sokoke_spawn_program (text_editor, unique_filename); else - { - gchar* command = g_strconcat ("exo-open ", unique_filename, NULL); - success = g_spawn_command_line_async (command, NULL); - g_free (command); - } + sokoke_show_uri (NULL, unique_filename, + gtk_get_current_event_time (), NULL); g_free (unique_filename); g_free (text_editor); @@ -3100,13 +3088,7 @@ _action_about_activate_email (GtkAboutDialog* about, const gchar* uri, gpointer user_data) { - if (!gtk_show_uri (NULL, uri, GDK_CURRENT_TIME, NULL)) - { - /* Fallback to Exo for example if GConf isn't setup */ - gchar* command = g_strconcat ("exo-open ", uri, NULL); - g_spawn_command_line_async (command, NULL); - g_free (command); - } + sokoke_show_uri (NULL, uri, GDK_CURRENT_TIME, NULL); } static void diff --git a/midori/midori-view.c b/midori/midori-view.c index 8b1e4d23..0c4d203b 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -1847,13 +1847,7 @@ midori_view_set_uri (MidoriView* view, } else if (g_str_has_prefix (uri, "mailto:")) { - if (!gtk_show_uri (NULL, uri, GDK_CURRENT_TIME, NULL)) - { - /* Fallback to Exo for example if GConf isn't setup */ - gchar* command = g_strconcat ("exo-open ", uri, NULL); - g_spawn_command_line_async (command, NULL); - g_free (command); - } + sokoke_show_uri (NULL, uri, GDK_CURRENT_TIME, NULL); } else { diff --git a/midori/sokoke.c b/midori/sokoke.c index 914d99dc..0cc7bf43 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -93,6 +93,48 @@ error_dialog (const gchar* short_message, } +/** + * sokoke_show_uri: + * @screen: a #GdkScreen, or %NULL + * @uri: the URI to show + * @timestamp: the timestamp of the event + * @error: the location of a #GError, or %NULL + * + * Shows the specified URI with an appropriate application. This + * supports xdg-open, exo-open and gnome-open as fallbacks if + * GIO doesn't do the trick. + * + * Return value: %TRUE on success, %FALSE if an error occurred + **/ +gboolean +sokoke_show_uri (GdkScreen* screen, + const gchar* uri, + guint32 timestamp, + GError** error) +{ + const gchar* fallbacks [] = { "xdg-open", "exo-open", "gnome-open" }; + gsize i; + + g_return_val_if_fail (GDK_IS_SCREEN (screen) || !screen, FALSE); + g_return_val_if_fail (uri != NULL, FALSE); + g_return_val_if_fail (!error || !*error, FALSE); + + if (gtk_show_uri (screen, uri, timestamp, error)) + return TRUE; + + for (i = 0; i < G_N_ELEMENTS (fallbacks); i++) + { + gchar* command = g_strconcat (fallbacks[i], " ", uri, NULL); + gboolean result = g_spawn_command_line_async (command, error); + g_free (command); + if (result) + return TRUE; + *error = NULL; + } + + return FALSE; +} + gboolean sokoke_spawn_program (const gchar* command, const gchar* argument) diff --git a/midori/sokoke.h b/midori/sokoke.h index 0c9312ce..5a685e02 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -26,6 +26,12 @@ sokoke_js_script_eval (JSContextRef js_context, /* Many themes need this hack for small toolbars to work */ #define GTK_ICON_SIZE_SMALL_TOOLBAR GTK_ICON_SIZE_BUTTON +gboolean +sokoke_show_uri (GdkScreen* screen, + const gchar* uri, + guint32 timestamp, + GError** error); + gboolean sokoke_spawn_program (const gchar* command, const gchar* argument); diff --git a/panels/midori-transfers.c b/panels/midori-transfers.c index 43434a05..d81a6beb 100644 --- a/panels/midori-transfers.c +++ b/panels/midori-transfers.c @@ -329,14 +329,8 @@ midori_transfers_treeview_row_activated_cb (GtkTreeView* treeview, gboolean success; uri = webkit_download_get_destination_uri (download); - success = gtk_show_uri (gtk_widget_get_screen (GTK_WIDGET ( + sokoke_show_uri (gtk_widget_get_screen (GTK_WIDGET ( treeview)), uri, gtk_get_current_event_time (), NULL); - if (!success) - { - gchar* command = g_strconcat ("exo-open ", uri, NULL); - success = g_spawn_command_line_async (command, NULL); - g_free (command); - } break; } case WEBKIT_DOWNLOAD_STATUS_CANCELLED: