Search code examples
cgtkgnomexfce

Alternative to gnome_url_show() in xfce


I would like to know if xfce has a function which works similar to gnome's gnome_url_show().

If this isn't the case I would like to know what would be another convenient way of opening a url? For example in case you added a signal handler to a button which upon clicking it opens a website.

Does gtk have such a function? I know it does have something built in at least, because when you create a label with a url in it it can be clicked to open the url. I have looked through various documentation but did not find a solution yet.

gtk_show_uri() is not going to work on xfce because it requires gvfs and I'm not using gnome.


Solution

  • Thanks to the xfce4-weather-plugin source I found this as a practical solution:

    gchar *str = g_strdup_printf("exo-open --launch WebBrowser %s", url);
    g_spawn_command_line_async(str, NULL);
    g_free(str);
    

    Considering the plugin is part of the core xfce apps I will assume it's the appropriate way to do it. I also found out about exo_execute_preferred_application() however I didn't feel like including another library.

    A variation on this theme can be found here: http://git.xfce.org/xfce/libxfce4ui/tree/libxfce4ui/xfce-dialogs.c#n66 Which may be preferable because it will fall back to gtk_show_uri() in case exo is not installed. Although it's possible gtk_show_uri() may not work due to lack of gvfs on xfce systems (user is using xfce after all, not gnome). The xfce panel uses this function to load a url.

    static void
    xfce_dialog_show_help_uri (GdkScreen *screen,
                               GtkWindow *parent,
                               GString   *uri)
    {
      GError   *error = NULL;
      gchar    *path;
      gchar    *cmd;
      gboolean  result;
    
      g_return_if_fail (GDK_IS_SCREEN (screen));
      g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
    
      path = g_find_program_in_path ("exo-open");
      if (G_LIKELY (path != NULL))
        {
          cmd = g_strdup_printf ("%s --launch WebBrowser '%s'", path, uri->str);
    
          result = xfce_spawn_command_line_on_screen (screen, cmd, FALSE, TRUE, &error);
    
          g_free (path);
          g_free (cmd);
        }
      else
        {
          /* not very likely to happen, but it is possible exo is not installed */
          result = gtk_show_uri (screen, uri->str, gtk_get_current_event_time (), &error);
        }
    
      if (!result)
        {
          xfce_dialog_show_error (parent, error,
              _("Failed to open web browser for online documentation"));
          g_error_free (error);
        }
    }