From: Michael Orlitzky Date: Tue, 29 Oct 2019 23:47:20 +0000 (-0400) Subject: xfce4-hdaps-dialogs.c: consolidate the URI launcher. X-Git-Tag: v1.0.0~3 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=a11bba966aac187122a7f04c4e111b58655ff1ec;p=xfce4-hdaps.git xfce4-hdaps-dialogs.c: consolidate the URI launcher. 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. --- diff --git a/panel-plugin/xfce4-hdaps-dialogs.c b/panel-plugin/xfce4-hdaps-dialogs.c index c43eafa..8851614 100644 --- a/panel-plugin/xfce4-hdaps-dialogs.c +++ b/panel-plugin/xfce4-hdaps-dialogs.c @@ -39,19 +39,51 @@ */ #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. */