[nautilus] application: centralize dbus service handling into GApplication



commit cfaa8f7fb65304cf5903bc2025784ef82cfb60f3
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Dec 5 22:08:13 2012 -0500

    application: centralize dbus service handling into GApplication
    
    Instead of each service installing its own timeout, use the service
    features of GApplication to do this automatically.

 libnautilus-private/nautilus-dbus-manager.c |  148 ++-------------------------
 libnautilus-private/nautilus-dbus-manager.h |    7 +-
 src/nautilus-application.c                  |   11 ++-
 src/nautilus-freedesktop-dbus.c             |  133 ++++--------------------
 src/nautilus-freedesktop-dbus.h             |    9 +-
 src/nautilus-main.c                         |   11 ++-
 6 files changed, 59 insertions(+), 260 deletions(-)
---
diff --git a/libnautilus-private/nautilus-dbus-manager.c b/libnautilus-private/nautilus-dbus-manager.c
index 13fc38c..0d90c18 100644
--- a/libnautilus-private/nautilus-dbus-manager.c
+++ b/libnautilus-private/nautilus-dbus-manager.c
@@ -33,51 +33,24 @@
 
 #include <gio/gio.h>
 
-typedef struct _NautilusDBusManager NautilusDBusManager;
-typedef struct _NautilusDBusManagerClass NautilusDBusManagerClass;
-
 struct _NautilusDBusManager {
   GObject parent;
 
-  GDBusConnection *connection;
-  GApplication *application;
-
   GDBusObjectManagerServer *object_manager;
   NautilusDBusFileOperations *file_operations;
-
-  guint owner_id;
 };
 
 struct _NautilusDBusManagerClass {
   GObjectClass parent_class;
 };
 
-enum {
-  PROP_APPLICATION = 1,
-  NUM_PROPERTIES
-};
-
-#define SERVICE_TIMEOUT 5
-
-static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
-
-static GType nautilus_dbus_manager_get_type (void) G_GNUC_CONST;
 G_DEFINE_TYPE (NautilusDBusManager, nautilus_dbus_manager, G_TYPE_OBJECT);
 
-static NautilusDBusManager *singleton = NULL;
-
 static void
 nautilus_dbus_manager_dispose (GObject *object)
 {
   NautilusDBusManager *self = (NautilusDBusManager *) object;
 
-  /* Unown before unregistering so we're not registred in a partial state */
-  if (self->owner_id != 0)
-    {
-      g_bus_unown_name (self->owner_id);
-      self->owner_id = 0;
-    }
-
   if (self->file_operations) {
     g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (self->file_operations));
     g_object_unref (self->file_operations);
@@ -89,27 +62,10 @@ nautilus_dbus_manager_dispose (GObject *object)
     self->object_manager = NULL;
   }
 
-  g_clear_object (&self->connection);
-
   G_OBJECT_CLASS (nautilus_dbus_manager_parent_class)->dispose (object);
 }
 
 static gboolean
-service_timeout_handler (gpointer user_data)
-{
-  NautilusDBusManager *self = user_data;
-
-  DEBUG ("Reached the DBus service timeout");
-
-  /* just unconditionally release here, as if an operation has been
-   * called, its progress handler will hold it alive for all the task duration.
-   */
-  g_application_release (self->application);
-
-  return FALSE;
-}
-
-static gboolean
 handle_copy_file (NautilusDBusFileOperations *object,
 		  GDBusMethodInvocation *invocation,
 		  const gchar *source_uri,
@@ -177,18 +133,13 @@ handle_empty_trash (NautilusDBusFileOperations *object,
 }
 
 static void
-bus_acquired_handler_cb (GDBusConnection *conn,
-                         const gchar *name,
-                         gpointer user_data)
+nautilus_dbus_manager_init (NautilusDBusManager *self)
 {
-  NautilusDBusManager *self = user_data;
-
-  DEBUG ("Bus acquired at %s", name);
+  GDBusConnection *connection;
 
-  self->connection = g_object_ref (conn);
+  connection = g_application_get_dbus_connection (g_application_get_default ());
 
   self->object_manager = g_dbus_object_manager_server_new ("/org/gnome/Nautilus");
-
   self->file_operations = nautilus_dbus_file_operations_skeleton_new ();
 
   g_signal_connect (self->file_operations,
@@ -204,72 +155,10 @@ bus_acquired_handler_cb (GDBusConnection *conn,
 		    G_CALLBACK (handle_empty_trash),
 		    self);
 
-  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->file_operations), self->connection,
+  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->file_operations), connection,
 				    "/org/gnome/Nautilus", NULL);
 
-  g_dbus_object_manager_server_set_connection (self->object_manager, self->connection);
-
-  g_timeout_add_seconds (SERVICE_TIMEOUT, service_timeout_handler, self);
-}
-
-static void
-on_name_lost (GDBusConnection *connection,
-	      const gchar     *name,
-	      gpointer         user_data)
-{
-  DEBUG ("Lost (or failed to acquire) the name %s on the session message bus\n", name);
-}
-
-static void
-on_name_acquired (GDBusConnection *connection,
-		  const gchar     *name,
-		  gpointer         user_data)
-{
-  DEBUG ("Acquired the name %s on the session message bus\n", name);
-}
-
-static void
-nautilus_dbus_manager_init (NautilusDBusManager *self)
-{
-  /* do nothing */
-}
-
-static void
-nautilus_dbus_manager_set_property (GObject *object,
-                                    guint property_id,
-                                    const GValue *value,
-                                    GParamSpec *pspec)
-{
-  NautilusDBusManager *self = (NautilusDBusManager *) (object);
-
-  switch (property_id)
-    {
-    case PROP_APPLICATION:
-      self->application = g_value_get_object (value);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-      break;
-    }
-}
-
-static void
-nautilus_dbus_manager_constructed (GObject *object)
-{
-  NautilusDBusManager *self = (NautilusDBusManager *) (object);
-
-  G_OBJECT_CLASS (nautilus_dbus_manager_parent_class)->constructed (object);
-
-  g_application_hold (self->application);
-
-  self->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
-				   "org.gnome.Nautilus",
-				   G_BUS_NAME_OWNER_FLAGS_NONE,
-				   bus_acquired_handler_cb,
-				   on_name_acquired,
-				   on_name_lost,
-				   self,
-				   NULL);
+  g_dbus_object_manager_server_set_connection (self->object_manager, connection);
 }
 
 static void
@@ -278,30 +167,11 @@ nautilus_dbus_manager_class_init (NautilusDBusManagerClass *klass)
   GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
   oclass->dispose = nautilus_dbus_manager_dispose;
-  oclass->constructed = nautilus_dbus_manager_constructed;
-  oclass->set_property = nautilus_dbus_manager_set_property;
-
-  properties[PROP_APPLICATION] =
-    g_param_spec_object ("application",
-                         "GApplication instance",
-                         "The owning GApplication instance",
-                         G_TYPE_APPLICATION,
-                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
-                         G_PARAM_STATIC_STRINGS);
-
-  g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
-}
-
-void
-nautilus_dbus_manager_start (GApplication *application)
-{
-  singleton = g_object_new (nautilus_dbus_manager_get_type (),
-                            "application", application,
-                            NULL);
 }
 
-void
-nautilus_dbus_manager_stop (void)
+NautilusDBusManager *
+nautilus_dbus_manager_new (void)
 {
-  g_clear_object (&singleton);
+  return g_object_new (nautilus_dbus_manager_get_type (),
+                       NULL);
 }
diff --git a/libnautilus-private/nautilus-dbus-manager.h b/libnautilus-private/nautilus-dbus-manager.h
index 16ea8c4..2ac5b57 100644
--- a/libnautilus-private/nautilus-dbus-manager.h
+++ b/libnautilus-private/nautilus-dbus-manager.h
@@ -27,7 +27,10 @@
 #include <glib-object.h>
 #include <gio/gio.h>
 
-void nautilus_dbus_manager_start (GApplication *application);
-void nautilus_dbus_manager_stop (void);
+typedef struct _NautilusDBusManager NautilusDBusManager;
+typedef struct _NautilusDBusManagerClass NautilusDBusManagerClass;
+
+GType nautilus_dbus_manager_get_type (void);
+NautilusDBusManager * nautilus_dbus_manager_new (void);
 
 #endif /* __NAUTILUS_DBUS_MANAGER_H__ */
diff --git a/src/nautilus-application.c b/src/nautilus-application.c
index f362cec..81f2d14 100644
--- a/src/nautilus-application.c
+++ b/src/nautilus-application.c
@@ -102,6 +102,8 @@ G_DEFINE_TYPE (NautilusApplication, nautilus_application, GTK_TYPE_APPLICATION);
 struct _NautilusApplicationPriv {
 	GVolumeMonitor *volume_monitor;
 	NautilusProgressUIHandler *progress_handler;
+	NautilusDBusManager *dbus_manager;
+	NautilusFreedesktopDBus *fdb_manager;
 
 	gboolean no_desktop;
 	gchar *geometry;
@@ -972,8 +974,9 @@ nautilus_application_finalize (GObject *object)
 
 	g_free (application->priv->geometry);
 
-	nautilus_dbus_manager_stop ();
-	nautilus_freedesktop_dbus_stop ();
+	g_clear_object (&application->priv->dbus_manager);
+	g_clear_object (&application->priv->fdb_manager);
+
 	notify_uninit ();
 
         G_OBJECT_CLASS (nautilus_application_parent_class)->finalize (object);
@@ -1412,8 +1415,8 @@ nautilus_application_startup (GApplication *app)
 	nautilus_previewer_get_singleton ();
 
 	/* create DBus manager */
-	nautilus_dbus_manager_start (app);
-	nautilus_freedesktop_dbus_start (self);
+	self->priv->dbus_manager = nautilus_dbus_manager_new ();
+	self->priv->fdb_manager = nautilus_freedesktop_dbus_new ();
 
 	/* initialize preferences and create the global GSettings objects */
 	nautilus_global_preferences_init ();
diff --git a/src/nautilus-freedesktop-dbus.c b/src/nautilus-freedesktop-dbus.c
index f9f1ba9..6754885 100644
--- a/src/nautilus-freedesktop-dbus.c
+++ b/src/nautilus-freedesktop-dbus.c
@@ -21,6 +21,7 @@
 
 #include <config.h>
 
+#include "nautilus-application.h"
 #include "nautilus-freedesktop-dbus.h"
 #include "nautilus-freedesktop-generated.h"
 
@@ -32,21 +33,13 @@
 
 #include <gio/gio.h>
 
-
-typedef struct _NautilusFreedesktopDBus NautilusFreedesktopDBus;
-typedef struct _NautilusFreedesktopDBusClass NautilusFreedesktopDBusClass;
-
 struct _NautilusFreedesktopDBus {
 	GObject parent;
 
-	/* Parent application */
-	NautilusApplication *application;
-
 	/* Id from g_dbus_own_name() */
 	guint owner_id;
 
 	/* DBus paraphernalia */
-	GDBusConnection *connection;
 	GDBusObjectManagerServer *object_manager;
 
 	/* Our DBus implementation skeleton */
@@ -57,17 +50,8 @@ struct _NautilusFreedesktopDBusClass {
 	GObjectClass parent_class;
 };
 
-enum {
-	PROP_APPLICATION = 1
-};
-
-#define SERVICE_TIMEOUT 5
-
-static GType nautilus_freedesktop_dbus_get_type (void) G_GNUC_CONST;
 G_DEFINE_TYPE (NautilusFreedesktopDBus, nautilus_freedesktop_dbus, G_TYPE_OBJECT);
 
-static NautilusFreedesktopDBus *singleton = NULL;
-
 static gboolean
 skeleton_handle_show_items_cb (NautilusFreedesktopFileManager1 *object,
 			       GDBusMethodInvocation *invocation,
@@ -75,9 +59,11 @@ skeleton_handle_show_items_cb (NautilusFreedesktopFileManager1 *object,
 			       const gchar *startup_id,
 			       gpointer data)
 {
-	NautilusFreedesktopDBus *fdb = data;
+        NautilusApplication *application;
 	int i;
 
+        application = NAUTILUS_APPLICATION (g_application_get_default ());
+
 	for (i = 0; uris[i] != NULL; i++) {
 		GFile *file;
 		GFile *parent;
@@ -86,10 +72,10 @@ skeleton_handle_show_items_cb (NautilusFreedesktopFileManager1 *object,
 		parent = g_file_get_parent (file);
 
 		if (parent != NULL) {
-			nautilus_application_open_location (fdb->application, parent, file, startup_id);
+			nautilus_application_open_location (application, parent, file, startup_id);
 			g_object_unref (parent);
 		} else {
-			nautilus_application_open_location (fdb->application, file, NULL, startup_id);
+			nautilus_application_open_location (application, file, NULL, startup_id);
 		}
 
 		g_object_unref (file);
@@ -106,15 +92,17 @@ skeleton_handle_show_folders_cb (NautilusFreedesktopFileManager1 *object,
 				 const gchar *startup_id,
 				 gpointer data)
 {
-	NautilusFreedesktopDBus *fdb = data;
+        NautilusApplication *application;
 	int i;
 
+        application = NAUTILUS_APPLICATION (g_application_get_default ());
+
 	for (i = 0; uris[i] != NULL; i++) {
 		GFile *file;
 
 		file = g_file_new_for_uri (uris[i]);
 
-		nautilus_application_open_location (fdb->application, file, NULL, startup_id);
+		nautilus_application_open_location (application, file, NULL, startup_id);
 
 		g_object_unref (file);
 	}
@@ -149,21 +137,6 @@ skeleton_handle_show_item_properties_cb (NautilusFreedesktopFileManager1 *object
 	return TRUE;
 }
 
-static gboolean
-service_timeout_cb (gpointer data)
-{
-	NautilusFreedesktopDBus *fdb = data;
-
-	DEBUG ("Reached the DBus service timeout");
-
-	/* just unconditionally release here, as if an operation has been
-	 * called, its progress handler will hold it alive for all the task duration.
-	 */
-	g_application_release (G_APPLICATION (fdb->application));
-
-	return FALSE;
-}
-
 static void
 bus_acquired_cb (GDBusConnection *conn,
 		 const gchar     *name,
@@ -173,7 +146,6 @@ bus_acquired_cb (GDBusConnection *conn,
 
 	DEBUG ("Bus acquired at %s", name);
 
-	fdb->connection = g_object_ref (conn);
 	fdb->object_manager = g_dbus_object_manager_server_new ("/org/freedesktop/FileManager1");
 
 	fdb->skeleton = nautilus_freedesktop_file_manager1_skeleton_new ();
@@ -185,11 +157,9 @@ bus_acquired_cb (GDBusConnection *conn,
 	g_signal_connect (fdb->skeleton, "handle-show-item-properties",
 			  G_CALLBACK (skeleton_handle_show_item_properties_cb), fdb);
 
-	g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (fdb->skeleton), fdb->connection, "/org/freedesktop/FileManager1", NULL);
+	g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (fdb->skeleton), conn, "/org/freedesktop/FileManager1", NULL);
 
-	g_dbus_object_manager_server_set_connection (fdb->object_manager, fdb->connection);
-
-	g_timeout_add_seconds (SERVICE_TIMEOUT, service_timeout_cb, fdb);
+	g_dbus_object_manager_server_set_connection (fdb->object_manager, conn);
 }
 
 static void
@@ -225,21 +195,21 @@ nautilus_freedesktop_dbus_dispose (GObject *object)
 	}
 
 	g_clear_object (&fdb->object_manager);
-	g_clear_object (&fdb->connection);
-	fdb->application = NULL;
 
 	G_OBJECT_CLASS (nautilus_freedesktop_dbus_parent_class)->dispose (object);
 }
 
 static void
-nautilus_freedesktop_dbus_constructed (GObject *object)
+nautilus_freedesktop_dbus_class_init (NautilusFreedesktopDBusClass *klass)
 {
-	NautilusFreedesktopDBus *fdb = (NautilusFreedesktopDBus *) object;
-
-	G_OBJECT_CLASS (nautilus_freedesktop_dbus_parent_class)->constructed (object);
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-	g_application_hold (G_APPLICATION (fdb->application));
+	object_class->dispose = nautilus_freedesktop_dbus_dispose;
+}
 
+static void
+nautilus_freedesktop_dbus_init (NautilusFreedesktopDBus *fdb)
+{
 	fdb->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
 					"org.freedesktop.FileManager1",
 					G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -250,67 +220,10 @@ nautilus_freedesktop_dbus_constructed (GObject *object)
 					NULL);
 }
 
-static void
-nautilus_freedesktop_dbus_set_property (GObject *object,
-					guint property_id,
-					const GValue *value,
-					GParamSpec *pspec)
-{
-	NautilusFreedesktopDBus *fdb = (NautilusFreedesktopDBus *) object;
-
-	switch (property_id) {
-	case PROP_APPLICATION:
-		fdb->application = g_value_get_object (value);
-		break;
-
-	default:
-		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-		break;
-	}
-}
-
-
-static void
-nautilus_freedesktop_dbus_class_init (NautilusFreedesktopDBusClass *klass)
-{
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-	object_class->dispose		= nautilus_freedesktop_dbus_dispose;
-	object_class->constructed	= nautilus_freedesktop_dbus_constructed;
-	object_class->set_property	= nautilus_freedesktop_dbus_set_property;
-
-	g_object_class_install_property (object_class,
-					 PROP_APPLICATION,
-					 g_param_spec_object ("application",
-							      "NautilusApplication instance",
-							      "The owning NautilusApplication instance",
-							      NAUTILUS_TYPE_APPLICATION,
-							      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
-
-}
-
-static void
-nautilus_freedesktop_dbus_init (NautilusFreedesktopDBus *fdb)
-{
-	/* nothing */
-}
-
 /* Tries to own the org.freedesktop.FileManager1 service name */
-void
-nautilus_freedesktop_dbus_start (NautilusApplication *app)
+NautilusFreedesktopDBus *
+nautilus_freedesktop_dbus_new (void)
 {	
-	if (singleton != NULL) {
-		return;
-	}
-
-	singleton = g_object_new (nautilus_freedesktop_dbus_get_type (),
-				  "application", app,
-				  NULL);
-}
-
-/* Releases the org.freedesktop.FileManager1 service name */
-void
-nautilus_freedesktop_dbus_stop (void)
-{
-	g_clear_object (&singleton);
+	return g_object_new (nautilus_freedesktop_dbus_get_type (),
+                             NULL);
 }
diff --git a/src/nautilus-freedesktop-dbus.h b/src/nautilus-freedesktop-dbus.h
index 2437286..14e8eb2 100644
--- a/src/nautilus-freedesktop-dbus.h
+++ b/src/nautilus-freedesktop-dbus.h
@@ -23,9 +23,12 @@
 #ifndef __NAUTILUS_FREEDESKTOP_DBUS_H__
 #define __NAUTILUS_FREEDESKTOP_DBUS_H__
 
-#include "nautilus-application.h"
+#include <glib-object.h>
 
-void nautilus_freedesktop_dbus_start (NautilusApplication *app);
-void nautilus_freedesktop_dbus_stop (void);
+typedef struct _NautilusFreedesktopDBus NautilusFreedesktopDBus;
+typedef struct _NautilusFreedesktopDBusClass NautilusFreedesktopDBusClass;
+
+GType nautilus_freedesktop_dbus_get_type (void);
+NautilusFreedesktopDBus * nautilus_freedesktop_dbus_new (void);
 
 #endif /* __NAUTILUS_FREEDESKTOP_DBUS_H__ */
diff --git a/src/nautilus-main.c b/src/nautilus-main.c
index 81f578c..8528009 100644
--- a/src/nautilus-main.c
+++ b/src/nautilus-main.c
@@ -95,9 +95,16 @@ main (int argc, char *argv[])
 
 	/* Run the nautilus application. */
 	application = g_object_new (NAUTILUS_TYPE_APPLICATION,
-				    "application-id", "org.gnome.NautilusApplication",
-				    "flags", G_APPLICATION_HANDLES_OPEN,
+				    "application-id", "org.gnome.Nautilus",
+				    "flags", G_APPLICATION_HANDLES_OPEN | G_APPLICATION_IS_SERVICE,
+				    "inactivity-timeout", 12000,
 				    NULL);
+
+	/* hold indefinitely if we're asked to persist */
+	if (g_getenv ("NAUTILUS_PERSIST") != NULL) {
+		g_application_hold (G_APPLICATION (application));
+	}
+
 	retval = g_application_run (G_APPLICATION (application),
 				    argc, argv);
 



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