From 5f8faba840a48e54ee2fbd679ab35aa38969bd97 Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage <emmanuel.lepage@savoirfairelinux.com>
Date: Tue, 27 Sep 2011 15:30:09 -0400
Subject: [PATCH] Revert "Don't reference count DBus clients, exit core
 immediately when one of them request it"

It make more sense to keep it as it was. Un the case of a Dataengine, Canonical AppIndicator / KDE Plasmoid systray RFC, the daemon can run event if the client have not been openned or have been closed.

This reverts commit 66a2b8eede0045aafa14d2560e793089199accef.
---
 daemon/src/dbus/instance-introspec.xml | 28 ++++++++++++++++++++++----
 daemon/src/dbus/instance.cpp           | 19 ++++++++++++++---
 daemon/src/dbus/instance.h             |  8 +++++++-
 gnome/src/actions.c                    |  4 ++--
 gnome/src/dbus/dbus.c                  | 10 +++++++--
 gnome/src/dbus/dbus.h                  | 12 ++++++++++-
 gnome/src/dbus/instance-introspec.xml  | 28 ++++++++++++++++++++++----
 7 files changed, 92 insertions(+), 17 deletions(-)

diff --git a/daemon/src/dbus/instance-introspec.xml b/daemon/src/dbus/instance-introspec.xml
index ad343b217d..f564d54cc1 100644
--- a/daemon/src/dbus/instance-introspec.xml
+++ b/daemon/src/dbus/instance-introspec.xml
@@ -2,12 +2,32 @@
 <node name="/instance-introspec" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0">
 	<interface name="org.sflphone.SFLphone.Instance">
 		<tp:docstring xmlns="http://www.w3.org/1999/xhtml">
-		<p></p>
+		<p>Count the number of clients actually registered to the core. When initializing your client, you need to register it against the core by using this interface.</p>
 		</tp:docstring>
-		<method name="Quit" tp:name-for-bindings="Quit">
+		<method name="Register" tp:name-for-bindings="Register">
 			<tp:docstring>
-				Properly quits the core.
-            </tp:docstring>
+				Register a new client to the core. Increments the registration count.
+			</tp:docstring>
+			<arg type="i" name="pid" direction="in">
+				<tp:docstring>
+					The pid of the client process
+				</tp:docstring>
+			</arg>
+			<arg type="s" name="name" direction="in">
+				<tp:docstring>
+					The name of the client
+				</tp:docstring>
+			</arg>
+		</method>
+		<method name="Unregister" tp:name-for-bindings="Unregister">
+			<tp:docstring>
+				Unregister a connected client from the core. Decrements the registration count. If no more clients are connected, ie the registration count equals 0, the core properly quits.
+			</tp:docstring>
+			<arg type="i" name="pid" direction="in">
+				<tp:docstring>
+					The pid of the client process
+				</tp:docstring>
+			</arg>
 		</method>
     </interface>
 </node>
diff --git a/daemon/src/dbus/instance.cpp b/daemon/src/dbus/instance.cpp
index 8f45db1dbe..266b1b86cf 100644
--- a/daemon/src/dbus/instance.cpp
+++ b/daemon/src/dbus/instance.cpp
@@ -34,11 +34,24 @@
 Instance::Instance (DBus::Connection& connection)
     : DBus::ObjectAdaptor (connection, "/org/sflphone/SFLphone/Instance")
 {
+    count = 0;
 }
 
 void
-Instance::Quit(void)
+Instance::Register (const int32_t& pid UNUSED,
+                    const std::string& name UNUSED)
 {
-    Manager::instance().terminate();
-    Manager::instance().getDbusManager()->exit();
+    count++;
+}
+
+
+void
+Instance::Unregister (const int32_t& pid UNUSED)
+{
+    count --;
+
+    if (count <= 0) {
+        Manager::instance().terminate();
+        Manager::instance().getDbusManager()->exit();
+    }
 }
diff --git a/daemon/src/dbus/instance.h b/daemon/src/dbus/instance.h
index 7bed4504b9..0258d7fdcd 100644
--- a/daemon/src/dbus/instance.h
+++ b/daemon/src/dbus/instance.h
@@ -52,10 +52,16 @@ class Instance
   public DBus::IntrospectableAdaptor,
   public DBus::ObjectAdaptor
 {
+    private:
+        int count;
+
     public:
         Instance (DBus::Connection& connection);
+        static const char* SERVER_PATH;
 
-        void Quit(void);
+        void Register (const int32_t& pid, const std::string& name);
+        void Unregister (const int32_t& pid);
+        int32_t getRegistrationCount (void);
 
 };
 
diff --git a/gnome/src/actions.c b/gnome/src/actions.c
index e84346e4db..84e3bb0a36 100644
--- a/gnome/src/actions.c
+++ b/gnome/src/actions.c
@@ -184,7 +184,7 @@ sflphone_quit ()
         // Save the history
         sflphone_save_history ();
 
-        dbus_quit();
+        dbus_unregister (getpid());
         dbus_clean ();
         calllist_clean (current_calls);
         calllist_clean (contacts);
@@ -315,7 +315,7 @@ void sflphone_fill_account_list (void)
 
 gboolean sflphone_init (GError **error)
 {
-    if (!dbus_connect (error))
+    if (!dbus_connect (error) || !dbus_register (getpid (), "Gtk+ Client", error))
         return FALSE;
 
     abook_init();
diff --git a/gnome/src/dbus/dbus.c b/gnome/src/dbus/dbus.c
index ff6356868b..0873e07ded 100644
--- a/gnome/src/dbus/dbus.c
+++ b/gnome/src/dbus/dbus.c
@@ -1098,12 +1098,18 @@ dbus_start_tone (const int start, const guint type)
     }
 }
 
+gboolean
+dbus_register (int pid, gchar *name, GError **error)
+{
+    return org_sflphone_SFLphone_Instance_register (instanceProxy, pid, name, error);
+}
+
 void
-dbus_quit(void)
+dbus_unregister (int pid)
 {
     GError *error = NULL;
 
-    org_sflphone_SFLphone_Instance_quit(instanceProxy, &error);
+    org_sflphone_SFLphone_Instance_unregister (instanceProxy, pid, &error);
 
     if (error) {
         ERROR ("Failed to call unregister() on instanceProxy: %s",
diff --git a/gnome/src/dbus/dbus.h b/gnome/src/dbus/dbus.h
index 157e4ae8d3..1eaebc4ede 100644
--- a/gnome/src/dbus/dbus.h
+++ b/gnome/src/dbus/dbus.h
@@ -359,10 +359,20 @@ void dbus_set_audio_manager (const gchar *api);
  */
 void dbus_start_tone (const int start , const guint type);
 
+/**
+ * Instance - Send registration request to dbus service.
+ * Manage the instances of clients connected to the server
+ * @param pid The pid of the processus client
+ * @param name The string description of the client. Here : GTK+ Client
+ * @param error return location for a GError or NULL
+ */
+gboolean dbus_register (int pid, gchar * name, GError **error);
+
 /**
  * Instance - Send unregistration request to dbus services
+ * @param pid The pid of the processus
  */
-void dbus_quit(void);
+void dbus_unregister (int pid);
 
 void dbus_set_sip_address (const gchar* address);
 
diff --git a/gnome/src/dbus/instance-introspec.xml b/gnome/src/dbus/instance-introspec.xml
index ad343b217d..f564d54cc1 100644
--- a/gnome/src/dbus/instance-introspec.xml
+++ b/gnome/src/dbus/instance-introspec.xml
@@ -2,12 +2,32 @@
 <node name="/instance-introspec" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0">
 	<interface name="org.sflphone.SFLphone.Instance">
 		<tp:docstring xmlns="http://www.w3.org/1999/xhtml">
-		<p></p>
+		<p>Count the number of clients actually registered to the core. When initializing your client, you need to register it against the core by using this interface.</p>
 		</tp:docstring>
-		<method name="Quit" tp:name-for-bindings="Quit">
+		<method name="Register" tp:name-for-bindings="Register">
 			<tp:docstring>
-				Properly quits the core.
-            </tp:docstring>
+				Register a new client to the core. Increments the registration count.
+			</tp:docstring>
+			<arg type="i" name="pid" direction="in">
+				<tp:docstring>
+					The pid of the client process
+				</tp:docstring>
+			</arg>
+			<arg type="s" name="name" direction="in">
+				<tp:docstring>
+					The name of the client
+				</tp:docstring>
+			</arg>
+		</method>
+		<method name="Unregister" tp:name-for-bindings="Unregister">
+			<tp:docstring>
+				Unregister a connected client from the core. Decrements the registration count. If no more clients are connected, ie the registration count equals 0, the core properly quits.
+			</tp:docstring>
+			<arg type="i" name="pid" direction="in">
+				<tp:docstring>
+					The pid of the client process
+				</tp:docstring>
+			</arg>
 		</method>
     </interface>
 </node>
-- 
GitLab