]> spindle.queued.net Git - midori/commitdiff
Unify opening URIs externally with good fallbacks
authorChristian Dywan <christian@twotoasts.de>
Mon, 20 Apr 2009 17:10:13 +0000 (19:10 +0200)
committerChristian Dywan <christian@twotoasts.de>
Mon, 20 Apr 2009 17:10:13 +0000 (19:10 +0200)
midori/midori-browser.c
midori/midori-view.c
midori/sokoke.c
midori/sokoke.h
panels/midori-transfers.c

index 1a89ff45f679731cc4fc8fcd42bb32a82af45e29..02f5a3ed046b6873e53ef3b826c356cb8f8721c5 100644 (file)
@@ -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
index 8b1e4d232d78795d915c6a11d3bee2acd3cdaa06..0c4d203b6952ee41f5d617e6199da1c1322a4132 100644 (file)
@@ -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
         {
index 914d99dcda92609487b1c8e60cd372e99645c2f3..0cc7bf439ce7bb7c4eae520c84bb0db4adc4a68c 100644 (file)
@@ -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)
index 0c9312ce9a759c20a58b558d5b4524bc555cc65a..5a685e02713d85af3ff64d7113a47b5d53e3aa17 100644 (file)
@@ -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);
index 43434a05bae31b74f0366a878c9ca2d9db1e7770..d81a6beb39702d5f03e24c83fec708efad6b573f 100644 (file)
@@ -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: