From a11bba966aac187122a7f04c4e111b58655ff1ec Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 29 Oct 2019 19:47:20 -0400 Subject: [PATCH] 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. --- panel-plugin/xfce4-hdaps-dialogs.c | 61 +++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 18 deletions(-) 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. */ -- 2.43.2