[Xfce4-commits] r29911 - in thunar/branches/migration-to-gio: . thunar

Jannis Pohlmann jannis at xfce.org
Tue Apr 28 18:45:32 CEST 2009


Author: jannis
Date: 2009-04-28 16:45:32 +0000 (Tue, 28 Apr 2009)
New Revision: 29911

Modified:
   thunar/branches/migration-to-gio/ChangeLog
   thunar/branches/migration-to-gio/thunar/thunar-file.c
   thunar/branches/migration-to-gio/thunar/thunar-file.h
   thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.c
   thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.h
Log:
	* thunar/thunar-file.{c,h}: Make the ThunarFile cache somewhat
	  thread-safe.
	* thunar/thunar-gio-extensions.{c,h}: Add new functions
	  g_volume_is_removable() and g_volume_is_mounted().

Modified: thunar/branches/migration-to-gio/ChangeLog
===================================================================
--- thunar/branches/migration-to-gio/ChangeLog	2009-04-27 14:41:42 UTC (rev 29910)
+++ thunar/branches/migration-to-gio/ChangeLog	2009-04-28 16:45:32 UTC (rev 29911)
@@ -1,3 +1,10 @@
+2009-04-28	Jannis Pohlmann <jannis at xfce.org>
+
+	* thunar/thunar-file.{c,h}: Make the ThunarFile cache somewhat 
+	  thread-safe.
+	* thunar/thunar-gio-extensions.{c,h}: Add new functions
+	  g_volume_is_removable() and g_volume_is_mounted().
+
 2009-04-27	Jannis Pohlmann <jannis at xfce.org>
 
 	* thunar/thunar-io-jobs-util.{c,h}: Add missing files.

Modified: thunar/branches/migration-to-gio/thunar/thunar-file.c
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-file.c	2009-04-27 14:41:42 UTC (rev 29910)
+++ thunar/branches/migration-to-gio/thunar/thunar-file.c	2009-04-28 16:45:32 UTC (rev 29911)
@@ -122,6 +122,10 @@
 
 
 
+G_LOCK_DEFINE_STATIC (file_cache_mutex);
+
+
+
 static ThunarUserManager *user_manager;
 static ThunarMetafile    *metafile;
 static GObjectClass      *thunar_file_parent_class;
@@ -189,8 +193,13 @@
 static void
 thunar_file_atexit (void)
 {
+  G_LOCK (file_cache_mutex);
+
   if (file_cache == NULL || g_hash_table_size (file_cache) == 0)
-    return;
+    {
+      G_UNLOCK (file_cache_mutex);
+      return;
+    }
 
   g_print ("--- Leaked a total of %u ThunarFile objects:\n",
            g_hash_table_size (file_cache));
@@ -198,6 +207,8 @@
   g_hash_table_foreach (file_cache, thunar_file_atexit_foreach, NULL);
 
   g_print ("\n");
+
+  G_UNLOCK (file_cache_mutex);
 }
 #endif
 
@@ -303,7 +314,9 @@
 #endif
 
   /* drop the entry from the cache */
+  G_LOCK (file_cache_mutex);
   g_hash_table_remove (file_cache, file->gfile);
+  G_UNLOCK (file_cache_mutex);
 
   /* drop a reference on the metadata if we own one */
   if ((file->flags & THUNAR_FILE_OWNS_METAFILE_REFERENCE) != 0)
@@ -537,9 +550,12 @@
         case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
           thunar_file_reload (file);
           break;
+
+        case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
         case G_FILE_MONITOR_EVENT_DELETED:
           thunar_file_destroy (file);
           break;
+
         default:
           break;
         }
@@ -627,7 +643,9 @@
       thunar_file_load (file, NULL, error);
 
       /* insert the file into the cache */
+      G_LOCK (file_cache_mutex);
       g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
+      G_UNLOCK (file_cache_mutex);
     }
 
   return file;
@@ -706,7 +724,9 @@
       thunar_file_load (file, NULL, NULL);
 
       /* insert the file into the cache */
+      G_LOCK (file_cache_mutex);
       g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
+      G_UNLOCK (file_cache_mutex);
     }
 
   g_object_unref (gfile);
@@ -1088,12 +1108,16 @@
           THUNAR_FILE_SET_WATCH_COUNT (file, watch_count);
         }
 
+      G_LOCK (file_cache_mutex);
+
       /* drop the previous entry from the cache */
       g_hash_table_remove (file_cache, previous_file);
 
       /* insert the new entry */
       g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
 
+      G_UNLOCK (file_cache_mutex);
+
       /* tell the associated folder that the file was renamed */
       thunarx_file_info_renamed (THUNARX_FILE_INFO (file));
 
@@ -2382,7 +2406,7 @@
   if (++watch_count == 1)
     {
       /* create a file or directory monitor */
-      file->monitor = g_file_monitor (file->gfile, G_FILE_MONITOR_NONE, NULL, NULL);
+      file->monitor = g_file_monitor (file->gfile, G_FILE_MONITOR_WATCH_MOUNTS, NULL, NULL);
       if (G_LIKELY (file->monitor != NULL))
         {
           /* make sure the pointer is set to NULL once the monitor is destroyed */
@@ -2519,6 +2543,8 @@
 
       /* release our reference */
       g_object_unref (G_OBJECT (file));
+
+      g_debug ("thunar_file_destroy: file is object: %d", G_IS_OBJECT (file));
     }
 }
 
@@ -2725,8 +2751,12 @@
 ThunarFile *
 thunar_file_cache_lookup (const GFile *file)
 {
+  ThunarFile *cached_file;
+
   _thunar_return_val_if_fail (G_IS_FILE (file), NULL);
 
+  G_LOCK (file_cache_mutex);
+
   /* allocate the ThunarFile cache on-demand */
   if (G_UNLIKELY (file_cache == NULL))
     {
@@ -2736,7 +2766,11 @@
                                           NULL);
     }
 
-  return g_hash_table_lookup (file_cache, file);
+  cached_file = g_hash_table_lookup (file_cache, file);
+
+  G_UNLOCK (file_cache_mutex);
+
+  return cached_file;
 }
 
 
@@ -2768,19 +2802,10 @@
 
   uri = thunar_vfs_path_dup_uri (path);
   gfile = g_file_new_for_uri (uri);
+  file = thunar_file_cache_lookup (gfile);
+  g_object_unref (gfile);
   g_free (uri);
 
-  /* allocate the ThunarFile cache on-demand */
-  if (G_UNLIKELY (file_cache == NULL))
-    {
-      file_cache = g_hash_table_new_full (g_file_hash, 
-                                          (GEqualFunc) g_file_equal, 
-                                          (GDestroyNotify) g_object_unref, 
-                                          NULL);
-    }
-
-  file = g_hash_table_lookup (file_cache, gfile);
-  g_object_unref (gfile);
   return file;
 }
 

Modified: thunar/branches/migration-to-gio/thunar/thunar-file.h
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-file.h	2009-04-27 14:41:42 UTC (rev 29910)
+++ thunar/branches/migration-to-gio/thunar/thunar-file.h	2009-04-28 16:45:32 UTC (rev 29911)
@@ -108,7 +108,7 @@
 
 struct _ThunarFile
 {
-  GObject __parent__;
+  GObject        __parent__;
 
   /*< private >*/
   ThunarVfsInfo *info;

Modified: thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.c
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.c	2009-04-27 14:41:42 UTC (rev 29910)
+++ thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.c	2009-04-28 16:45:32 UTC (rev 29911)
@@ -107,6 +107,42 @@
 
 
 
+gchar *
+g_file_size_humanize (guint64 size)
+{
+  gchar *buffer;
+
+  if (G_UNLIKELY (size > 1024ul * 1024ul * 1024ul))
+    buffer = g_strdup_printf ("%0.1f GB", size / (1024.0 * 1024.0 * 1024.0));
+  else if (size > 1024ul * 1024ul)
+    buffer = g_strdup_printf ("%0.1f MB", size / (1024.0 * 1024.0));
+  else if (size > 1024ul)
+    buffer = g_strdup_printf ("%0.1f kB", size / 1024.0);
+  else
+    buffer = g_strdup_printf ("%lu B", (gulong) size);
+
+  return buffer;
+}
+
+
+
+GType
+g_file_list_get_type (void)
+{
+  static GType type = G_TYPE_INVALID;
+
+  if (G_UNLIKELY (type == G_TYPE_INVALID))
+    {
+      type = g_boxed_type_register_static (I_("GFileList"),
+                                           (GBoxedCopyFunc) g_file_list_copy,
+                                           (GBoxedFreeFunc) g_file_list_free);
+    }
+
+  return type;
+}
+
+
+
 /**
  * g_file_list_new_from_string:
  * @string : a string representation of an URI list.
@@ -234,36 +270,49 @@
 
 
 
-gchar *
-g_file_size_humanize (guint64 size)
+gboolean
+g_volume_is_removable (GVolume *volume)
 {
-  gchar *buffer;
+  gboolean can_eject = FALSE;
+  gboolean can_unmount = FALSE;
+  GMount  *mount;
 
-  if (G_UNLIKELY (size > 1024ul * 1024ul * 1024ul))
-    buffer = g_strdup_printf ("%0.1f GB", size / (1024.0 * 1024.0 * 1024.0));
-  else if (size > 1024ul * 1024ul)
-    buffer = g_strdup_printf ("%0.1f MB", size / (1024.0 * 1024.0));
-  else if (size > 1024ul)
-    buffer = g_strdup_printf ("%0.1f kB", size / 1024.0);
-  else
-    buffer = g_strdup_printf ("%lu B", (gulong) size);
+  _thunar_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
 
-  return buffer;
+  /* check if the volume can be ejected */
+  can_eject = g_volume_can_eject (volume);
+
+  /* determine the mount for the volume (if it is mounted at all) */
+  mount = g_volume_get_mount (volume);
+  if (mount != NULL)
+    {
+      /* check if the volume can be unmounted */
+      can_unmount = g_mount_can_unmount (mount);
+
+      /* release the mount */
+      g_object_unref (mount);
+    }
+
+  return can_eject || can_unmount;
 }
 
 
 
-GType
-g_file_list_get_type (void)
+gboolean
+g_volume_is_mounted (GVolume *volume)
 {
-  static GType type = G_TYPE_INVALID;
+  gboolean is_mounted = FALSE;
+  GMount  *mount;
 
-  if (G_UNLIKELY (type == G_TYPE_INVALID))
+  _thunar_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
+
+  /* determine the mount for this volume (if it is mounted at all) */
+  mount = g_volume_get_mount (volume);
+  if (mount != NULL)
     {
-      type = g_boxed_type_register_static (I_("GFileList"),
-                                           (GBoxedCopyFunc) g_file_list_copy,
-                                           (GBoxedFreeFunc) g_file_list_free);
+      is_mounted = TRUE;
+      g_object_unref (mount);
     }
 
-  return type;
+  return is_mounted;
 }

Modified: thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.h
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.h	2009-04-27 14:41:42 UTC (rev 29910)
+++ thunar/branches/migration-to-gio/thunar/thunar-gio-extensions.h	2009-04-28 16:45:32 UTC (rev 29911)
@@ -33,15 +33,6 @@
 gboolean g_file_is_trashed           (GFile       *file);
 gboolean g_file_is_desktop           (GFile       *file);
 
-GList   *g_file_list_new_from_string (const gchar *string);
-gchar   *g_file_list_to_string       (GList       *list);
-GList   *g_file_list_append          (GList       *list,
-                                      GFile       *file);
-GList   *g_file_list_prepend         (GList       *list,
-                                      GFile       *file);
-GList   *g_file_list_copy            (GList       *list);
-void     g_file_list_free            (GList       *list);
-
 gchar   *g_file_size_humanize        (guint64      size);
 
 /**
@@ -54,6 +45,18 @@
 
 GType g_file_list_get_type (void);
 
+GList   *g_file_list_new_from_string (const gchar *string);
+gchar   *g_file_list_to_string       (GList       *list);
+GList   *g_file_list_append          (GList       *list,
+                                      GFile       *file);
+GList   *g_file_list_prepend         (GList       *list,
+                                      GFile       *file);
+GList   *g_file_list_copy            (GList       *list);
+void     g_file_list_free            (GList       *list);
+
+gboolean g_volume_is_removable       (GVolume     *volume);
+gboolean g_volume_is_mounted         (GVolume     *volume);
+
 G_END_DECLS
 
 #endif /* !__THUNAR_GLIB_EXTENSIONS_H__ */




More information about the Xfce4-commits mailing list