file monitors: use new giomodule function

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
This commit is contained in:
Ryan Lortie 2013-01-18 18:30:36 -05:00 committed by Matthias Clasen
parent 3a7b44c007
commit c83600e8ae
2 changed files with 8 additions and 112 deletions

View 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* GFileMonitor*
_g_local_directory_monitor_new (const char *dirname, _g_local_directory_monitor_new (const char *dirname,
GFileMonitorFlags flags, GFileMonitorFlags flags,
GError **error) GError **error)
{ {
static GOnce once_init = G_ONCE_INIT; GFileMonitor *monitor = NULL;
GTypeClass *type_class;
GFileMonitor *monitor;
GType type; GType type;
type_class = NULL; type = _g_io_module_get_default_type (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
g_once (&once_init, get_default_local_directory_monitor, &type_class); "GIO_USE_FILE_MONITOR",
type = (GType)once_init.retval; G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
monitor = NULL;
if (type != G_TYPE_INVALID) if (type != G_TYPE_INVALID)
monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, NULL)); monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, NULL));
else else
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Unable to find default local directory monitor type")); _("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; return monitor;
} }

View 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)); 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* GFileMonitor*
_g_local_file_monitor_new (const char *pathname, _g_local_file_monitor_new (const char *pathname,
GFileMonitorFlags flags, GFileMonitorFlags flags,
GError **error) GError **error)
{ {
static GOnce once_init = G_ONCE_INIT; GFileMonitor *monitor = NULL;
GTypeClass *type_class;
GFileMonitor *monitor;
GType type; GType type;
type_class = NULL; type = _g_io_module_get_default_type (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
g_once (&once_init, get_default_local_file_monitor, &type_class); "GIO_USE_FILE_MONITOR",
type = (GType)once_init.retval; G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported));
monitor = NULL;
if (type != G_TYPE_INVALID) if (type != G_TYPE_INVALID)
monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, NULL)); monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, NULL));
else else
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Unable to find default local file monitor type")); _("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; return monitor;
} }