[gnome-settings-daemon/gnome-3-2] print-notifications: Unown name on the system bus when session goes idle



commit de7ed790905a7b42dec39cbc4813f5a1d04b0099
Author: Marek Kasik <mkasik redhat com>
Date:   Fri Oct 14 20:28:50 2011 +0200

    print-notifications: Unown name on the system bus when session goes idle
    
    Connect to org.gnome.SessionManager.Presence and listen to StatusChanged
    signal. Unown names and unregister objects when sesion goes idle. Own the
    names and register objects when become available (#660158).

 plugins/print-notifications/gsd-printer.c |  191 ++++++++++++++++++++++-------
 1 files changed, 149 insertions(+), 42 deletions(-)
---
diff --git a/plugins/print-notifications/gsd-printer.c b/plugins/print-notifications/gsd-printer.c
index f61ad4d..c1c2235 100644
--- a/plugins/print-notifications/gsd-printer.c
+++ b/plugins/print-notifications/gsd-printer.c
@@ -60,6 +60,17 @@ static GDBusNodeInfo *pdi_introspection_data = NULL;
 #define GNOME_SESSION_DBUS_IFACE                "org.gnome.SessionManager"
 #define GNOME_SESSION_CLIENT_PRIVATE_DBUS_IFACE "org.gnome.SessionManager.ClientPrivate"
 
+#define GNOME_SESSION_PRESENCE_DBUS_PATH  "/org/gnome/SessionManager/Presence"
+#define GNOME_SESSION_PRESENCE_DBUS_IFACE "org.gnome.SessionManager.Presence"
+
+enum {
+  PRESENCE_STATUS_AVAILABLE = 0,
+  PRESENCE_STATUS_INVISIBLE,
+  PRESENCE_STATUS_BUSY,
+  PRESENCE_STATUS_IDLE,
+  PRESENCE_STATUS_UNKNOWN
+};
+
 static const gchar npn_introspection_xml[] =
   "<node name='/com/redhat/NewPrinterNotification'>"
   "  <interface name='com.redhat.NewPrinterNotification'>"
@@ -88,6 +99,10 @@ static const gchar pdi_introspection_xml[] =
   "</node>";
 
 static GMainLoop *main_loop;
+static guint      npn_registration_id;
+static guint      pdi_registration_id;
+static guint      npn_owner_id;
+static guint      pdi_owner_id;
 
 static GHashTable *
 get_missing_executables (const gchar *ppd_file_name)
@@ -1088,20 +1103,51 @@ static const GDBusInterfaceVTable interface_vtable =
 };
 
 static void
+unregister_objects ()
+{
+        GDBusConnection *system_connection;
+        GError          *error = NULL;
+
+        system_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
+
+        if (npn_registration_id > 0) {
+                g_dbus_connection_unregister_object (system_connection, npn_registration_id);
+                npn_registration_id = 0;
+        }
+
+        if (pdi_registration_id > 0) {
+                g_dbus_connection_unregister_object (system_connection, pdi_registration_id);
+                pdi_registration_id = 0;
+        }
+}
+
+static void
+unown_names ()
+{
+        if (npn_owner_id > 0) {
+                g_bus_unown_name (npn_owner_id);
+                npn_owner_id = 0;
+        }
+
+        if (pdi_owner_id > 0) {
+                g_bus_unown_name (pdi_owner_id);
+                pdi_owner_id = 0;
+        }
+}
+
+static void
 on_npn_bus_acquired (GDBusConnection *connection,
                      const gchar     *name,
                      gpointer         user_data)
 {
-  guint registration_id;
-
-  registration_id = g_dbus_connection_register_object (connection,
-                                                       SCP_DBUS_NPN_PATH,
-                                                       npn_introspection_data->interfaces[0],
-                                                       &interface_vtable,
-                                                       NULL,
-                                                       NULL,
-                                                       NULL);
-  g_assert (registration_id > 0);
+        npn_registration_id = g_dbus_connection_register_object (connection,
+                                                                 SCP_DBUS_NPN_PATH,
+                                                                 npn_introspection_data->interfaces[0],
+                                                                 &interface_vtable,
+                                                                 NULL,
+                                                                 NULL,
+                                                                 NULL);
+        g_assert (npn_registration_id > 0);
 }
 
 static void
@@ -1109,16 +1155,14 @@ on_pdi_bus_acquired (GDBusConnection *connection,
                      const gchar     *name,
                      gpointer         user_data)
 {
-  guint registration_id;
-
-  registration_id = g_dbus_connection_register_object (connection,
-                                                       SCP_DBUS_PDI_PATH,
-                                                       pdi_introspection_data->interfaces[0],
-                                                       &interface_vtable,
-                                                       NULL,
-                                                       NULL,
-                                                       NULL);
-  g_assert (registration_id > 0);
+        pdi_registration_id = g_dbus_connection_register_object (connection,
+                                                                 SCP_DBUS_PDI_PATH,
+                                                                 npn_introspection_data->interfaces[0],
+                                                                 &interface_vtable,
+                                                                 NULL,
+                                                                 NULL,
+                                                                 NULL);
+        g_assert (npn_registration_id > 0);
 }
 
 static void
@@ -1133,6 +1177,49 @@ on_name_lost (GDBusConnection *connection,
               const gchar     *name,
               gpointer         user_data)
 {
+        unregister_objects ();
+}
+
+static void
+session_signal_handler (GDBusConnection  *connection,
+                        const gchar      *sender_name,
+                        const gchar      *object_path,
+                        const gchar      *interface_name,
+                        const gchar      *signal_name,
+                        GVariant         *parameters,
+                        gpointer          user_data)
+{
+        guint            new_status;
+
+        g_variant_get (parameters, "(u)", &new_status);
+
+        if (new_status == PRESENCE_STATUS_IDLE ||
+            new_status == PRESENCE_STATUS_AVAILABLE) {
+                unregister_objects ();
+                unown_names ();
+
+                if (new_status == PRESENCE_STATUS_AVAILABLE) {
+                        npn_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
+                                                       SCP_DBUS_NPN_NAME,
+                                                       G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
+                                                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                                                       on_npn_bus_acquired,
+                                                       on_name_acquired,
+                                                       on_name_lost,
+                                                       NULL,
+                                                       NULL);
+
+                        pdi_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
+                                                       SCP_DBUS_PDI_NAME,
+                                                       G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
+                                                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                                                       on_pdi_bus_acquired,
+                                                       on_name_acquired,
+                                                       on_name_lost,
+                                                       NULL,
+                                                       NULL);
+                }
+        }
 }
 
 static void
@@ -1245,9 +1332,8 @@ main (int argc, char *argv[])
   GDBusConnection *connection;
   gboolean         client_signal_subscription_set = FALSE;
   GError          *error = NULL;
-  guint            npn_owner_id;
-  guint            pdi_owner_id;
   guint            client_signal_subscription_id;
+  guint            session_signal_subscription_id;
   gchar           *object_path;
 
   bindtextdomain (GETTEXT_PACKAGE, GNOME_SETTINGS_LOCALEDIR);
@@ -1255,6 +1341,11 @@ main (int argc, char *argv[])
   textdomain (GETTEXT_PACKAGE);
   setlocale (LC_ALL, "");
 
+  npn_registration_id = 0;
+  pdi_registration_id = 0;
+  npn_owner_id = 0;
+  pdi_owner_id = 0;
+
   g_type_init ();
 
   notify_init ("gnome-settings-daemon-printer");
@@ -1269,6 +1360,18 @@ main (int argc, char *argv[])
 
   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
+  session_signal_subscription_id =
+    g_dbus_connection_signal_subscribe (connection,
+                                        NULL,
+                                        GNOME_SESSION_PRESENCE_DBUS_IFACE,
+                                        "StatusChanged",
+                                        GNOME_SESSION_PRESENCE_DBUS_PATH,
+                                        NULL,
+                                        G_DBUS_SIGNAL_FLAGS_NONE,
+                                        session_signal_handler,
+                                        NULL,
+                                        NULL);
+
   object_path = register_gnome_session_client ("gsd-printer", "");
   if (object_path) {
           client_signal_subscription_id =
@@ -1285,33 +1388,37 @@ main (int argc, char *argv[])
           client_signal_subscription_set = TRUE;
   }
 
-
-  npn_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
-                                 SCP_DBUS_NPN_NAME,
-                                 G_BUS_NAME_OWNER_FLAGS_NONE,
-                                 on_npn_bus_acquired,
-                                 on_name_acquired,
-                                 on_name_lost,
-                                 NULL,
-                                 NULL);
-
-  pdi_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
-                                 SCP_DBUS_PDI_NAME,
-                                 G_BUS_NAME_OWNER_FLAGS_NONE,
-                                 on_pdi_bus_acquired,
-                                 on_name_acquired,
-                                 on_name_lost,
-                                 NULL,
-                                 NULL);
+  if (npn_owner_id == 0)
+          npn_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
+                                         SCP_DBUS_NPN_NAME,
+                                         G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
+                                         G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                                         on_npn_bus_acquired,
+                                         on_name_acquired,
+                                         on_name_lost,
+                                         NULL,
+                                         NULL);
+
+  if (pdi_owner_id == 0)
+          pdi_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
+                                         SCP_DBUS_PDI_NAME,
+                                         G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
+                                         G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                                         on_pdi_bus_acquired,
+                                         on_name_acquired,
+                                         on_name_lost,
+                                         NULL,
+                                         NULL);
 
   main_loop = g_main_loop_new (NULL, FALSE);
   g_main_loop_run (main_loop);
 
-  g_bus_unown_name (npn_owner_id);
-  g_bus_unown_name (pdi_owner_id);
+  unregister_objects ();
+  unown_names ();
 
   if (client_signal_subscription_set)
           g_dbus_connection_signal_unsubscribe (connection, client_signal_subscription_id);
+  g_dbus_connection_signal_unsubscribe (connection, session_signal_subscription_id);
 
   g_free (object_path);
 



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