file monitors: use new giomodule function
authorRyan Lortie <desrt@desrt.ca>
Fri, 18 Jan 2013 23:30:36 +0000 (18:30 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 19 Jan 2013 19:04:49 +0000 (14:04 -0500)
Get rid of the complicated default module detection code in
GLocalFileMonitor and GLocalDirectoryMonitor and use the new
_gio_module_get_default_type() function instead.

This change also adds the ability to override the default file monitor
via the GIO_USE_FILE_MONITOR environment variable in the same way as can
be done for GIO_USE_VFS.

https://bugzilla.gnome.org/show_bug.cgi?id=592211

gio/glocaldirectorymonitor.c
gio/glocalfilemonitor.c

index c5e7088..319b87a 100644 (file)
@@ -226,76 +226,24 @@ mounts_changed (GUnixMountMonitor *mount_monitor,
     }
 }
 
-static gpointer
-get_default_local_directory_monitor (gpointer data)
-{
-  GLocalDirectoryMonitorClass *chosen_class;
-  GLocalDirectoryMonitorClass **ret = data;
-  GIOExtensionPoint *ep;
-  GList *extensions, *l;
-
-  _g_io_modules_ensure_loaded ();
-
-  ep = g_io_extension_point_lookup (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
-
-  extensions = g_io_extension_point_get_extensions (ep);
-  
-  chosen_class = NULL;
-  for (l = extensions; l != NULL; l = l->next)
-    {
-      GIOExtension *extension = l->data;
-      GLocalDirectoryMonitorClass *klass;
-      
-      klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_io_extension_ref_class (extension));
-      
-      if (klass->is_supported ())
-       {
-         chosen_class = klass;
-         break;
-       }
-      else
-       g_type_class_unref (klass);
-    }
-  
-  if (chosen_class)
-    {
-      *ret = chosen_class;
-      return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
-    }
-  else
-    return (gpointer)G_TYPE_INVALID;
-}
-
 GFileMonitor*
 _g_local_directory_monitor_new (const char         *dirname,
                                GFileMonitorFlags   flags,
                                GError            **error)
 {
-  static GOnce once_init = G_ONCE_INIT;
-  GTypeClass *type_class;
-  GFileMonitor *monitor;
+  GFileMonitor *monitor = NULL;
   GType type;
 
-  type_class = NULL;
-  g_once (&once_init, get_default_local_directory_monitor, &type_class);
-  type = (GType)once_init.retval;
+  type = _g_io_module_get_default_type (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
+                                        "GIO_USE_FILE_MONITOR",
+                                        G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
 
-  monitor = NULL;
   if (type != G_TYPE_INVALID)
     monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, NULL));
   else
     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                          _("Unable to find default local directory monitor type"));
 
-  /* This is non-null on first pass here. Unref the class now.
-   * This is to avoid unloading the module and then loading it
-   * again which would happen if we unrefed the class
-   * before creating the monitor.
-   */
-  
-  if (type_class)
-    g_type_class_unref (type_class);
-  
   return monitor;
 }
 
index 91d524f..a8925c0 100644 (file)
@@ -150,75 +150,23 @@ static void g_local_file_monitor_class_init (GLocalFileMonitorClass *klass)
                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
 }
 
-static gpointer
-get_default_local_file_monitor (gpointer data)
-{
-  GLocalFileMonitorClass *chosen_class;
-  GLocalFileMonitorClass **ret = data;
-  GIOExtensionPoint *ep;
-  GList *extensions, *l;
-
-  _g_io_modules_ensure_loaded ();
-
-  ep = g_io_extension_point_lookup (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
-
-  extensions = g_io_extension_point_get_extensions (ep);
-  
-  chosen_class = NULL;
-  for (l = extensions; l != NULL; l = l->next)
-    {
-      GIOExtension *extension = l->data;
-      GLocalFileMonitorClass *klass;
-      
-      klass = G_LOCAL_FILE_MONITOR_CLASS (g_io_extension_ref_class (extension));
-      
-      if (klass->is_supported ())
-       {
-         chosen_class = klass;
-         break;
-       }
-      else
-       g_type_class_unref (klass);
-    }
-  
-  if (chosen_class)
-    {
-      *ret = chosen_class;
-      return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
-    }
-  else
-    return (gpointer)G_TYPE_INVALID;
-}
-
 GFileMonitor*
 _g_local_file_monitor_new (const char         *pathname,
                           GFileMonitorFlags   flags,
                           GError            **error)
 {
-  static GOnce once_init = G_ONCE_INIT;
-  GTypeClass *type_class;
-  GFileMonitor *monitor;
+  GFileMonitor *monitor = NULL;
   GType type;
 
-  type_class = NULL;
-  g_once (&once_init, get_default_local_file_monitor, &type_class);
-  type = (GType)once_init.retval;
+  type = _g_io_module_get_default_type (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
+                                        "GIO_USE_FILE_MONITOR",
+                                        G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported));
 
-  monitor = NULL;
   if (type != G_TYPE_INVALID)
     monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, NULL));
   else
     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                          _("Unable to find default local file monitor type"));
 
-  /* This is non-null on first pass here. Unref the class now.
-   * This is to avoid unloading the module and then loading it
-   * again which would happen if we unrefed the class
-   * before creating the monitor.
-   */
-  
-  if (type_class)
-    g_type_class_unref (type_class);
-  
   return monitor;
 }