local monitors: make more idiomatic use of GObject

Stop abusing constructor() to do startup work, adding _start() calls
instead.

The backends themselves still use constructor() although a patch will be
following to also fix inotify.

The reason for using a separate start() call instead of constructed()
will become apparent in future commits.

https://bugzilla.gnome.org/show_bug.cgi?id=704887
This commit is contained in:
Ryan Lortie 2013-07-25 13:29:16 -04:00
parent 1ddfd9d98e
commit b050dc3c0a
4 changed files with 48 additions and 96 deletions

View File

@ -69,60 +69,32 @@ g_local_directory_monitor_set_property (GObject *object,
const GValue *value,
GParamSpec *pspec)
{
GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
switch (property_id)
{
case PROP_DIRNAME:
/* Do nothing */
local_monitor->dirname = g_value_dup_string (value);
break;
case PROP_FLAGS:
/* Do nothing */
local_monitor->flags = g_value_get_flags (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static GObject *
g_local_directory_monitor_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_properties)
void
g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor)
{
GObject *obj;
GLocalDirectoryMonitorClass *klass;
GObjectClass *parent_class;
GLocalDirectoryMonitor *local_monitor;
GFileMonitorFlags flags = 0;
const gchar *dirname = NULL;
gint i;
GLocalDirectoryMonitorClass *class;
klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
obj = parent_class->constructor (type,
n_construct_properties,
construct_properties);
class = G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor);
local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
for (i = 0; i < n_construct_properties; i++)
{
if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
{
g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
dirname = g_value_get_string (construct_properties[i].value);
}
if (strcmp ("flags", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
{
g_warn_if_fail (G_VALUE_HOLDS_FLAGS (construct_properties[i].value));
flags = g_value_get_flags (construct_properties[i].value);
}
}
local_monitor->dirname = g_strdup (dirname);
local_monitor->flags = flags;
if (!G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor)->mount_notify &&
(flags & G_FILE_MONITOR_WATCH_MOUNTS))
if (!class->mount_notify && (local_monitor->flags & G_FILE_MONITOR_WATCH_MOUNTS))
{
#ifdef G_OS_WIN32
/*claim everything was mounted */
@ -145,7 +117,8 @@ g_local_directory_monitor_constructor (GType type,
#endif
}
return obj;
if (class->start)
class->start (local_monitor);
}
static void
@ -156,7 +129,6 @@ g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
gobject_class->finalize = g_local_directory_monitor_finalize;
gobject_class->set_property = g_local_directory_monitor_set_property;
gobject_class->constructor = g_local_directory_monitor_constructor;
file_monitor_class->cancel = g_local_directory_monitor_cancel;
@ -251,6 +223,9 @@ _g_local_directory_monitor_new (const char *dirname,
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Unable to find default local directory monitor type"));
if (monitor)
g_local_directory_monitor_start (G_LOCAL_DIRECTORY_MONITOR (monitor));
return monitor;
}

View File

@ -60,6 +60,7 @@ struct _GLocalDirectoryMonitorClass
gboolean mount_notify;
gboolean (* is_supported) (void);
void (* start) (GLocalDirectoryMonitor *local_monitor);
};
#ifdef G_OS_UNIX
@ -71,6 +72,7 @@ GFileMonitor * _g_local_directory_monitor_new (const char *dirname,
GFileMonitorFlags flags,
gboolean is_remote_fs,
GError **error);
void g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor);
G_END_DECLS

View File

@ -51,71 +51,41 @@ g_local_file_monitor_set_property (GObject *object,
const GValue *value,
GParamSpec *pspec)
{
GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
switch (property_id)
{
case PROP_FILENAME:
/* Do nothing */
local_monitor->filename = g_value_dup_string (value);
break;
case PROP_FLAGS:
/* Do nothing as well */
local_monitor->flags = g_value_get_flags (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static GObject *
g_local_file_monitor_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_properties)
void
g_local_file_monitor_start (GLocalFileMonitor *local_monitor)
{
GObject *obj;
GLocalFileMonitorClass *klass;
GObjectClass *parent_class;
GLocalFileMonitor *local_monitor;
const gchar *filename = NULL;
GFileMonitorFlags flags = 0;
gint i;
GLocalFileMonitorClass *class;
klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_FILE_MONITOR));
parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
obj = parent_class->constructor (type,
n_construct_properties,
construct_properties);
class = G_LOCAL_FILE_MONITOR_GET_CLASS (local_monitor);
local_monitor = G_LOCAL_FILE_MONITOR (obj);
for (i = 0; i < n_construct_properties; i++)
{
if (strcmp ("filename", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
{
g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
filename = g_value_get_string (construct_properties[i].value);
}
else if (strcmp ("flags", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
{
g_warn_if_fail (G_VALUE_HOLDS_FLAGS (construct_properties[i].value));
flags = g_value_get_flags (construct_properties[i].value);
}
}
g_warn_if_fail (filename != NULL);
local_monitor->filename = g_strdup (filename);
local_monitor->flags = flags;
return obj;
if (class->start)
class->start (local_monitor);
}
static void
g_local_file_monitor_finalize (GObject *object)
{
GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
if (local_monitor->filename)
{
g_free (local_monitor->filename);
local_monitor->filename = NULL;
}
G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize (object);
}
@ -126,7 +96,6 @@ static void g_local_file_monitor_class_init (GLocalFileMonitorClass *klass)
gobject_class->set_property = g_local_file_monitor_set_property;
gobject_class->finalize = g_local_file_monitor_finalize;
gobject_class->constructor = g_local_file_monitor_constructor;
g_object_class_install_property (gobject_class,
PROP_FILENAME,
@ -175,5 +144,8 @@ _g_local_file_monitor_new (const char *pathname,
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Unable to find default local file monitor type"));
if (monitor)
g_local_file_monitor_start (G_LOCAL_FILE_MONITOR (monitor));
return monitor;
}

View File

@ -32,6 +32,7 @@ G_BEGIN_DECLS
#define G_LOCAL_FILE_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), G_TYPE_LOCAL_FILE_MONITOR, GLocalFileMonitorClass))
#define G_IS_LOCAL_FILE_MONITOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_LOCAL_FILE_MONITOR))
#define G_IS_LOCAL_FILE_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_LOCAL_FILE_MONITOR))
#define G_LOCAL_FILE_MONITOR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_LOCAL_FILE_MONITOR, GLocalFileMonitorClass))
#define G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME "gio-local-file-monitor"
#define G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME "gio-nfs-file-monitor"
@ -52,6 +53,7 @@ struct _GLocalFileMonitorClass
GFileMonitorClass parent_class;
gboolean (* is_supported) (void);
void (* start) (GLocalFileMonitor *local_monitor);
};
#ifdef G_OS_UNIX
@ -63,6 +65,7 @@ GFileMonitor * _g_local_file_monitor_new (const char *pathname,
GFileMonitorFlags flags,
gboolean is_remote_fs,
GError **error);
void g_local_file_monitor_start (GLocalFileMonitor *local_monitor);
G_END_DECLS