[glib/application-replace] Listen to NameLost



commit c0f71be35d95f46e769436d6ecb4d4c970318486
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 18 15:56:51 2018 -0400

    Listen to NameLost
    
    When we lose the name because somebody replaced us,
    we need to stop thinking of ourselves as the primary
    instance, and quit.

 gio/gapplicationimpl-dbus.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
---
diff --git a/gio/gapplicationimpl-dbus.c b/gio/gapplicationimpl-dbus.c
index 877816b59..25ac49231 100644
--- a/gio/gapplicationimpl-dbus.c
+++ b/gio/gapplicationimpl-dbus.c
@@ -111,6 +111,7 @@ struct _GApplicationImpl
   GDBusConnection *session_bus;
   GActionGroup    *exported_actions;
   const gchar     *bus_name;
+  guint            name_lost_signal;
 
   gchar           *object_path;
   guint            object_id;
@@ -327,6 +328,24 @@ application_path_from_appid (const gchar *appid)
   return appid_path;
 }
 
+static void g_application_impl_stop_primary (GApplicationImpl *impl);
+
+static void
+name_lost (GDBusConnection *bus,
+           const char      *sender_name,
+           const char      *object_path,
+           const char      *interface_name,
+           const char      *signal_name,
+           GVariant        *parameters,
+           gpointer         user_data)
+{
+  GApplicationImpl *impl = user_data;
+
+  impl->primary = FALSE;
+  g_application_impl_stop_primary (impl);
+  g_application_quit (impl->app);
+}
+
 /* Attempt to become the primary instance.
  *
  * Returns %TRUE if everything went OK, regardless of if we became the
@@ -432,7 +451,13 @@ g_application_impl_attempt_primary (GApplicationImpl  *impl,
   app_flags = g_application_get_flags (impl->app);
 
   if (app_flags & G_APPLICATION_FLAGS_ALLOW_REPLACEMENT)
-    flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
+    {
+      impl->name_lost_signal = g_dbus_connection_signal_subscribe (impl->session_bus, "org.freeesktop.DBus, 
"org.freedesktop.DBus",
+                                                                   "NameLost", impl->bus_name, NULL, 
G_DBUS_SIGNAL_FLAGS_NONE,
+                                                                   name_lost, impl, NULL);
+
+      flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
+    }
   if (app_flags & G_APPLICATION_FLAGS_REPLACE)
     flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
 
@@ -450,6 +475,12 @@ g_application_impl_attempt_primary (GApplicationImpl  *impl,
   /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
   impl->primary = (rval != 3);
 
+  if (!impl->primary && impl->name_lost_signal)
+    {
+      g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
+      impl->name_lost_signal = 0;
+    }
+
   return TRUE;
 }
 
@@ -492,6 +523,12 @@ g_application_impl_stop_primary (GApplicationImpl *impl)
       impl->actions_id = 0;
     }
 
+  if (impl->name_lost_signal)
+    {
+      g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
+      impl->name_lost_signal = 0;
+    }
+
   if (impl->primary && impl->bus_name)
     {
       g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",


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