[glib/wip/gdesktopappinfo: 3/20] local monitors: make more idiomatic use of GObject



commit 08dd609731d431cf193cad3135cb701bae7363e4
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jul 25 13:29:16 2013 -0400

    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.

 gio/glocaldirectorymonitor.c |   73 ++++++++++++++----------------------------
 gio/glocaldirectorymonitor.h |    2 +
 gio/glocalfilemonitor.c      |   66 +++++++++++---------------------------
 gio/glocalfilemonitor.h      |    3 ++
 4 files changed, 48 insertions(+), 96 deletions(-)
---
diff --git a/gio/glocaldirectorymonitor.c b/gio/glocaldirectorymonitor.c
index 64f69ec..588f6e1 100644
--- a/gio/glocaldirectorymonitor.c
+++ b/gio/glocaldirectorymonitor.c
@@ -69,83 +69,56 @@ 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;
-  
-  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);
-
-  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);
-        }
-    }
+  GLocalDirectoryMonitorClass *class;
 
-  local_monitor->dirname = g_strdup (dirname);
-  local_monitor->flags = flags;
+  class = G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor);
 
-  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 */
       local_monitor->was_mounted = TRUE;
 #else
       GUnixMountEntry *mount;
-      
+
       /* Emulate unmount detection */
-      
+
       mount = g_unix_mount_at (local_monitor->dirname, NULL);
-      
+
       local_monitor->was_mounted = mount != NULL;
-      
+
       if (mount)
         g_unix_mount_free (mount);
 
       local_monitor->mount_monitor = g_unix_mount_monitor_new ();
       g_signal_connect_object (local_monitor->mount_monitor, "mounts-changed",
-                              G_CALLBACK (mounts_changed), local_monitor, 0);
+                               G_CALLBACK (mounts_changed), local_monitor, 0);
 #endif
     }
 
-  return obj;
+  if (class->start)
+    class->start (local_monitor);
 }
 
 static void
@@ -153,10 +126,9 @@ g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
 {
   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (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;
 }
 
diff --git a/gio/glocaldirectorymonitor.h b/gio/glocaldirectorymonitor.h
index 95a3f6f..16d8ac1 100644
--- a/gio/glocaldirectorymonitor.h
+++ b/gio/glocaldirectorymonitor.h
@@ -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
 
diff --git a/gio/glocalfilemonitor.c b/gio/glocalfilemonitor.c
index e33369d..765b98f 100644
--- a/gio/glocalfilemonitor.c
+++ b/gio/glocalfilemonitor.c
@@ -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;
-  
-  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);
-
-  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);
-        }
-    }
+  GLocalFileMonitorClass *class;
 
-  g_warn_if_fail (filename != NULL);
+  class = G_LOCAL_FILE_MONITOR_GET_CLASS (local_monitor);
 
-  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_free (local_monitor->filename);
 
   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;
 }
diff --git a/gio/glocalfilemonitor.h b/gio/glocalfilemonitor.h
index 2c1b794..e6bb623 100644
--- a/gio/glocalfilemonitor.h
+++ b/gio/glocalfilemonitor.h
@@ -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
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]