]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/commitdiff
xfce4-hdaps-dialogs.c: consolidate the URI launcher.
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 29 Oct 2019 23:47:20 +0000 (19:47 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 29 Oct 2019 23:47:20 +0000 (19:47 -0400)
Now that the signal handler for the "about" dialog works, let's
consolidate the two identical URI launcher routines. The new
factored-out launch_uri() function tries all three approaches
in succession:

  1. whatever gtk_show_uri_on_window() does,
  2. exo-open, and
  3. xdg-open.

If those all fail, it fails. We had to twiddle some things to allow us
to pass a GtkWindow pointer into the launch_uri() function because
gtk_show_uri_on_window() needs one, but that wasn't a big deal.

panel-plugin/xfce4-hdaps-dialogs.c

index c43eafa6d7602e0b5f5411be968f0aae783c8351..8851614a97a095b2cd9654f8f957aed7726d6c4b 100644 (file)
  */
 #define PLUGIN_WEBSITE "http://michael.orlitzky.com/code/xfce4-hdaps.xhtml"
 
+/* Launch a URI using either gtk_show_uri_on_window (gvfs), exo-open
+ * or xdg-open. This is used both when you click the author's email
+ * address, and when you visit the plugin's homepage.
+ */
+static gboolean launch_uri(GtkWindow *parent, const gchar *uri) {
+  GError *error = NULL;
+  gboolean result = gtk_show_uri_on_window(parent,
+                                           uri,
+                                           GDK_CURRENT_TIME,
+                                           &error);
+  /* First, let's try what the GTK docs say the default is. */
+  if (result == TRUE) {
+    return result;
+  }
+  else {
+    g_warning(_("Error launching URI %s: %s"), uri, error->message);
+    g_error_free(error);
+  }
 
-static void hdaps_configure_response(GtkWidget   *dialog,
-                                     gint         response,
-                                     HdapsPlugin *hdaps) {
-
-  if (response == GTK_RESPONSE_HELP) {
-    /* Launch the user's web browser and direct them to the plugin webpage. */
-    gboolean spawn_result = g_spawn_command_line_async("xdg-open " PLUGIN_WEBSITE, NULL);
+  /* Ok, GTK failed, on to the fallbacks. Let's try exo-open first,
+     since it's part of XFCE. */
+  gchar *cmd = g_strdup_printf("%s %s","exo-open", uri);
+  result = g_spawn_command_line_async(cmd, NULL);
 
-    if (G_UNLIKELY(spawn_result == FALSE)) {
-      g_warning(_("Unable to open the following url: %s"), PLUGIN_WEBSITE);
+  if (G_UNLIKELY(result == FALSE)) {
+    g_warning(_("Unable to open URI with exo-open, trying xdg-open: %s"), uri);
+    g_free(cmd);
+    cmd = g_strdup_printf("%s %s","xdg-open", uri);
+    result = g_spawn_command_line_async(cmd, NULL);
+    if (G_UNLIKELY(result == FALSE)) {
+      g_warning(_("Unable to open URI with xdg-open, giving up: %s"), uri);
     }
+  }
+
+  g_free(cmd);
 
+  return result;
+}
+
+
+static void hdaps_configure_response(GtkDialog   *dialog,
+                                     gint         response,
+                                     HdapsPlugin *hdaps) {
+  if (response == GTK_RESPONSE_HELP) {
+    launch_uri(GTK_WINDOW(dialog), PLUGIN_WEBSITE);
     return;
   }
 
@@ -76,7 +108,7 @@ static void hdaps_configure_response(GtkWidget   *dialog,
      instead. */
   g_object_set_data(G_OBJECT(hdaps->plugin), "dialog", NULL);
   xfce_panel_plugin_unblock_menu(hdaps->plugin);
-  gtk_widget_destroy(dialog);
+  gtk_widget_destroy(GTK_WIDGET(dialog));
 
   return;
 }
@@ -204,14 +236,7 @@ static void hdaps_uri_handler(GtkAboutDialog *about,
                               const gchar *uri,
                               gpointer data) {
 
-    gchar *cmd = g_strdup_printf("%s %s","xdg-open", uri);
-    gboolean spawn_result = g_spawn_command_line_async(cmd, NULL);
-
-    if (G_UNLIKELY(spawn_result == FALSE)) {
-      g_warning(_("Unable to open the following uri: %s"), uri);
-    }
-
-    g_free(cmd);
+  launch_uri(GTK_WINDOW(about), uri);
 }
 
 /* "Close" button handler for the About dialog. */