[Xfce4-commits] <thunar-volman:master> Implement autoopen (requires an .autoopen or autoopen file).

Jannis Pohlmann noreply at xfce.org
Sun Jul 25 19:44:44 CEST 2010


Updating branch refs/heads/master
         to d7fb1ad549e9b090b03ccfde1df46771c4b7863c (commit)
       from a1cb43a6485957a74c35f58dd42022d7116f00b7 (commit)

commit d7fb1ad549e9b090b03ccfde1df46771c4b7863c
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Tue Jul 20 13:06:34 2010 +0200

    Implement autoopen (requires an .autoopen or autoopen file).
    
    This finishes our implementation of the Desktop Application Autostart
    Specification.

 thunar-volman/tvm-block-device.c |  119 +++++++++++++++++++++++++++++++++----
 1 files changed, 106 insertions(+), 13 deletions(-)

diff --git a/thunar-volman/tvm-block-device.c b/thunar-volman/tvm-block-device.c
index d9c4d9f..81cffb2 100644
--- a/thunar-volman/tvm-block-device.c
+++ b/thunar-volman/tvm-block-device.c
@@ -1,6 +1,7 @@
 /* vi:set et ai sw=2 sts=2 ts=2: */
 /*-
- * Copyright (c) 2010 Jannis Pohlmann <jannis at xfce.org>
+ * Copyright (c) 2007-2008 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2010      Jannis Pohlmann <jannis at xfce.org>
  *
  * This program is free software; you can redistribute it and/or 
  * modify it under the terms of the GNU General Public License as
@@ -22,6 +23,10 @@
 #include <config.h>
 #endif
 
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
 #include <gio/gio.h>
 
 #include <gtk/gtk.h>
@@ -125,18 +130,24 @@ tvm_block_device_autorun (TvmContext *context,
                           GMount     *mount,
                           GError    **error)
 {
-  gboolean autoplay;
-  gboolean autorun;
-  gboolean result = FALSE;
-  GError  *err = NULL;
-  GFile   *mount_point;
-  gchar  **argv;
-  gchar   *autoplay_command;
-  gchar   *message;
-  gchar   *mount_path;
-  gchar   *wine;
-  guint    n;
-  gint     response;
+  struct stat statb_mount_point;
+  struct stat statb_autoopen;
+  gboolean    autoopen;
+  gboolean    autoplay;
+  gboolean    autorun;
+  gboolean    result = FALSE;
+  GError     *err = NULL;
+  GFile      *mount_point;
+  gchar     **argv;
+  gchar      *autoplay_command;
+  gchar      *message;
+  gchar      *mount_path;
+  gchar      *path_autoopen;
+  gchar      *wine;
+  gchar       line[1024];
+  guint       n;
+  FILE       *fp;
+  gint        response;
 
   g_return_val_if_fail (context != NULL, FALSE);
   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
@@ -273,6 +284,88 @@ tvm_block_device_autorun (TvmContext *context,
         }
     }
 
+ /* check if autoopen support is enabled */
+  autoopen = xfconf_channel_get_bool (context->channel, "/autoopen/enabled", FALSE);
+  if (autoopen)
+    {
+      /* "Autoopen Files" (Desktop Application Autostart Specification) */
+      static const gchar *autoopen_files[] = { ".autoopen", "autoopen" };
+      for (n = 0; n < G_N_ELEMENTS (autoopen_files); ++n)
+        {
+          /* determine the moint point path */
+          mount_point = g_mount_get_root (mount);
+          mount_path = g_file_get_path (mount_point);
+          g_object_unref (mount_point);
+
+          /* check if one of the autoopen files is present */
+          path_autoopen = g_build_filename (mount_path, autoopen_files[n], NULL);
+          fp = fopen (path_autoopen, "r");
+          g_free (path_autoopen);
+
+          /* check if the file could be opened */
+          if (G_UNLIKELY (fp != NULL))
+            {
+              /* read the first line of the file (MUST NOT be an absolute path) */
+              if (fgets (line, sizeof (line), fp) != NULL && !g_path_is_absolute (line))
+                {
+                  /* determine the absolute path of the file */
+                  path_autoopen = g_build_filename (mount_path, g_strstrip (line), NULL);
+
+                  /* the file must exist on exactly this device */
+                  if (stat (mount_path, &statb_mount_point) == 0 
+                      && stat (path_autoopen, &statb_autoopen) == 0
+                      && S_ISREG (statb_autoopen.st_mode) 
+                      && (statb_autoopen.st_mode & 0111) == 0
+                      && (statb_mount_point.st_dev == statb_autoopen.st_dev))
+                    {
+                      /* prompt the user whether to autoopen this file */
+                      message = g_strdup_printf (_("Would you like to open \"%s\"?"), 
+                                                 autoopen_files[n]);
+                      response = tvm_prompt (context, "gnome-fs-executable", 
+                                             _("Auto-Open Confirmation"),
+                                             _("Auto-Open capability detected"), message,
+                                             _("Ig_nore"), GTK_RESPONSE_CANCEL,
+                                             _("_Open"), TVM_RESPONSE_AUTORUN,
+                                             NULL);
+                      g_free (message);
+
+                      /* check if we should autoopen */
+                      if (response == TVM_RESPONSE_AUTORUN)
+                        {
+                          /* prepare the command to autoopen */
+                          argv = g_new (gchar *, 3);
+                          argv[0] = g_strdup ("Thunar");
+                          argv[1] = path_autoopen;
+                          argv[2] = NULL;
+
+                          /* let Thunar open the file */
+                          result = g_spawn_async (mount_path, argv, NULL, 0, NULL, NULL, 
+                                                  NULL, error);
+
+                          /* cleanup */
+                          g_free (path_autoopen);
+                          fclose (fp);
+          
+                          /* free the mount point path */
+                          g_free (mount_path);
+
+                          return result;
+                        }
+                    }
+
+                  /* cleanup */
+                  g_free (path_autoopen);
+                }
+
+              /* close the file handle */
+              fclose (fp);
+            }
+
+          /* free the mount point path */
+          g_free (mount_path);
+        }
+    }
+
   return FALSE;
 }
 



More information about the Xfce4-commits mailing list