[Xfce4-commits] <exo:exo-helper-overhaul> Add FileManager support to exo-open and known URI completion.

Nick Schermer nick at xfce.org
Wed Aug 26 18:44:05 CEST 2009


Updating branch refs/heads/exo-helper-overhaul
         to c07a72487229b8ab3b75326dd1a29211b9455914 (commit)
       from a16361e2f7684ac84d9e80008969e3a7db5a5838 (commit)

commit c07a72487229b8ab3b75326dd1a29211b9455914
Author: Nick Schermer <nick at xfce.org>
Date:   Wed Aug 26 18:09:09 2009 +0200

    Add FileManager support to exo-open and known URI completion.
    
    Move some code from exo-url to exo-open to complete non-uris
    when passing for example some filenames to exo-open.

 exo-open/main.c |  110 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 100 insertions(+), 10 deletions(-)

diff --git a/exo-open/main.c b/exo-open/main.c
index d93d197..f2585c3 100644
--- a/exo-open/main.c
+++ b/exo-open/main.c
@@ -37,8 +37,13 @@
 
 
 
+#define MATCH_BROWSER "^(([^:/?#]+)://)?([^/?#])([^?#]*)(\\?([^#]*))?(#(.*))?"
+#define MATCH_MAILER  "^[a-z0-9][a-z0-9_.-]*@[a-z0-9][a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+$"
+
+
+
 /**
- * For testing this code the following commands should work:
+ * For testing this code, the following commands should work:
  *
  * exo-open --launch WebBrowser http://xfce.org (bug #5461).
  * exo-open --launch WebBrowser http://xfce.org bugs.xfce.org 'http://www.google.com/search?q=what is a space' 'http://wiki.xfce.org'
@@ -51,10 +56,10 @@
 
 
 
-static gboolean opt_help = FALSE;
-static gboolean opt_version = FALSE;
-static gchar   *opt_launch = NULL;
-static gchar   *opt_working_directory = NULL;
+static gboolean  opt_help = FALSE;
+static gboolean  opt_version = FALSE;
+static gchar    *opt_launch = NULL;
+static gchar    *opt_working_directory = NULL;
 
 static GOptionEntry entries[] =
 {
@@ -91,6 +96,7 @@ usage (void)
    */
   g_print ("%s\n", _("  WebBrowser       - The preferred Web Browser.\n"
                      "  MailReader       - The preferred Mail Reader.\n"
+                     "  FileManager      - The preferred File Manager.\n"
                      "  TerminalEmulator - The preferred Terminal Emulator."));
   g_print ("\n");
   g_print ("%s\n", _("If you don't specify the --launch option, exo-open will open all specified\n"
@@ -103,6 +109,60 @@ usage (void)
 
 
 
+static gboolean
+exo_open_looks_like_an_uri (const gchar *string)
+{
+  const gchar *s = string;
+
+  /* <scheme> starts with an alpha character */
+  if (g_ascii_isalpha (*s))
+    {
+      /* <scheme> continues with (alpha | digit | "+" | "-" | ".")* */
+      for (++s; g_ascii_isalnum (*s) || *s == '+' || *s == '-' || *s == '.'; ++s);
+
+      /* <scheme> must be followed by ":" */
+      return (*s == ':');
+    }
+
+  return FALSE;
+}
+
+
+
+static const gchar *
+exo_open_find_scheme (const gchar *string)
+{
+  gboolean  exists;
+  gchar    *current_dir, *path;
+
+  /* is an absolute path, return file uri */
+  if (g_path_is_absolute (string))
+    return "file://";
+
+  /* treat it like a relative path */
+  current_dir = g_get_current_dir ();
+  path = g_build_filename (current_dir, string, NULL);
+  g_free (current_dir);
+
+  /* verify that a file of the given name exists */
+  exists = g_file_test (path, G_FILE_TEST_EXISTS);
+  g_free (path);
+  if (exists)
+    return "file://";
+
+  /* regular expression to check if it looks like an email address */
+  if (g_regex_match_simple (MATCH_MAILER, string, G_REGEX_CASELESS, 0))
+    return "mailto:";
+
+  /* regular expression to check if it looks like an url */
+  if (g_regex_match_simple (MATCH_BROWSER, string, G_REGEX_CASELESS, 0))
+    return "http://";
+
+  return NULL;
+}
+
+
+
 int
 main (int argc, char **argv)
 {
@@ -113,6 +173,8 @@ main (int argc, char **argv)
   gint            result = EXIT_SUCCESS;
   GString        *join;
   guint           i;
+  gchar          *uri;
+  const gchar    *scheme;
 
 #ifdef GETTEXT_PACKAGE
   /* setup i18n support */
@@ -212,20 +274,48 @@ main (int argc, char **argv)
   else if (argc > 1)
     {
       /* open all specified urls */
-      for (argv += 1; *argv != NULL; ++argv)
+      for (argv += 1; result == EXIT_SUCCESS && *argv != NULL; ++argv)
         {
-          if (!exo_url_show (*argv, NULL, &err))
+          if (exo_open_looks_like_an_uri (*argv))
+            {
+              /* use the argument directly */
+              uri = g_strdup (*argv);
+            }
+          else
+            {
+              /* try to find a valid scheme */
+              scheme = exo_open_find_scheme (*argv);
+              if (G_LIKELY (scheme != NULL))
+                uri = g_strconcat (scheme, *argv, NULL);
+              else
+                uri = NULL;
+            }
+
+          if (uri == NULL)
             {
               /* display an error dialog */
               dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
-                                               _("Failed to open URL \"%s\"."), *argv);
-              gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s.", err->message);
+                                               _("Unable to detect the URI-scheme of \"%s\"."), *argv);
               gtk_dialog_run (GTK_DIALOG (dialog));
               gtk_widget_destroy (dialog);
+
               result = EXIT_FAILURE;
+            }
+          else if (!gtk_show_uri (NULL, uri, 0, &err))
+            {
+              /* display an error dialog */
+              dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
+                                               _("Failed to open URI \"%s\"."), uri);
+              gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s.", err->message);
               g_error_free (err);
-              break;
+              gtk_dialog_run (GTK_DIALOG (dialog));
+              gtk_widget_destroy (dialog);
+
+              result = EXIT_FAILURE;
             }
+
+          /* cleanup */
+          g_free (uri);
         }
     }
   else



More information about the Xfce4-commits mailing list