[gnome-user-share] user-share: Use gnome-session to track the active session



commit b459856c3eea5b40cd5a8aee7e9a1ac2778ad850
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Feb 21 09:18:21 2013 +0100

    user-share: Use gnome-session to track the active session
    
    Removes the direct dependency on the obsolete ConsoleKit.

 configure.ac     |    2 +-
 src/user_share.c |  176 ++++++++++++++++--------------------------------------
 2 files changed, 53 insertions(+), 125 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2e5878c..44c2ff7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -60,7 +60,7 @@ if $have_dbus_1_1 ; then
    AC_DEFINE(HAVE_DBUS_1_1, 1, [Set to true if we have D-BUS 1.1])
 fi
 
-PKG_CHECK_MODULES(USER_SHARE, glib-2.0 >= 2.28 gio-2.0 gtk+-3.0 dbus-glib-1 libnotify libcanberra-gtk3 
$DBUS_MODULES gnome-bluetooth-1.0 >= 2.91.5)
+PKG_CHECK_MODULES(USER_SHARE, glib-2.0 >= 2.28 gio-2.0 >= 2.26 gtk+-3.0 dbus-glib-1 libnotify 
libcanberra-gtk3 $DBUS_MODULES gnome-bluetooth-1.0 >= 2.91.5)
 AC_SUBST(USER_SHARE_CFLAGS)
 AC_SUBST(USER_SHARE_LIBS)
 
diff --git a/src/user_share.c b/src/user_share.c
index 0eac8b2..3bcec7f 100644
--- a/src/user_share.c
+++ b/src/user_share.c
@@ -28,6 +28,7 @@
 #include <glib/gi18n.h>
 #include <glib/gstdio.h>
 #include <bluetooth-client.h>
+#include <gio/gio.h>
 #include <X11/Xlib.h>
 
 #include "user_share.h"
@@ -47,17 +48,15 @@
 #include <signal.h>
 #include <unistd.h>
 
-/* ConsoleKit */
-#define CK_NAME                        "org.freedesktop.ConsoleKit"
-#define CK_INTERFACE           "org.freedesktop.ConsoleKit"
-#define CK_MANAGER_PATH                "/org/freedesktop/ConsoleKit/Manager"
-#define CK_MANAGER_INTERFACE   "org.freedesktop.ConsoleKit.Manager"
-#define CK_SEAT_INTERFACE      "org.freedesktop.ConsoleKit.Seat"
-#define CK_SESSION_INTERFACE   "org.freedesktop.ConsoleKit.Session"
+/* GNOME Session */
+#define GNOME_SESSION_DBUS_NAME      "org.gnome.SessionManager"
+#define GNOME_SESSION_DBUS_OBJECT    "/org/gnome/SessionManager"
+#define GNOME_SESSION_DBUS_INTERFACE "org.gnome.SessionManager"
 
 static GSettings *settings = NULL;
 
 static guint disabled_timeout_tag = 0;
+static GDBusProxy *session_proxy = NULL;
 static gboolean has_console = TRUE;
 
 static BluetoothClient *client = NULL;
@@ -88,136 +87,65 @@ obex_services_shutdown (void)
 }
 
 static void
-sessionchanged_cb (void)
+session_properties_changed_cb (GDBusProxy      *session,
+                              GVariant        *changed,
+                              char           **invalidated,
+                              gpointer         user_data)
 {
-       DBusGConnection *dbus_connection;
-       DBusGProxy *proxy_ck_manager;
-       DBusGProxy *proxy_ck_session;
+       GVariant *v;
 
-       gchar *ck_session_path;
-       gboolean is_active = FALSE;
-       GError *error = NULL;
+       v = g_variant_lookup_value (changed, "SessionIsActive", G_VARIANT_TYPE_BOOLEAN);
+       if (v) {
+               has_console = g_variant_get_boolean (v);
+               g_debug ("Received session is active change: now %s", has_console ? "active" : "inactive");
 
-       g_message ("Active session changed");
+               if (has_console)
+                       obex_services_start ();
+               else
+                       obex_services_shutdown ();
 
-       dbus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
-       if (!dbus_connection) {
-           g_warning ("Unable to connect to dbus");
-           dbus_g_connection_unref (dbus_connection);
-           return;
-       }
-    
-       proxy_ck_manager = dbus_g_proxy_new_for_name (dbus_connection,
-                                                     CK_NAME,
-                                                     CK_MANAGER_PATH,
-                                                     CK_MANAGER_INTERFACE);
-       if (dbus_g_proxy_call (proxy_ck_manager, "GetCurrentSession",
-                              &error, G_TYPE_INVALID,
-                              DBUS_TYPE_G_OBJECT_PATH, &ck_session_path,
-                              G_TYPE_INVALID) == FALSE) {
-           g_warning ("Couldn't request the name: %s", error->message);
-           dbus_g_connection_unref (dbus_connection);
-           g_object_unref (proxy_ck_manager);
-           g_error_free (error);
-           return;
-       }
-       
-       proxy_ck_session = dbus_g_proxy_new_for_name (dbus_connection,
-                                                     CK_NAME,
-                                                     ck_session_path,
-                                                     CK_SESSION_INTERFACE);
-
-       if (dbus_g_proxy_call (proxy_ck_session, "IsActive",
-                              &error, G_TYPE_INVALID,
-                              G_TYPE_BOOLEAN, &is_active, 
-                              G_TYPE_INVALID) == FALSE) {
-           
-           g_warning ("Couldn't request the name: %s", error->message);
-           dbus_g_connection_unref (dbus_connection);
-           g_object_unref (proxy_ck_manager);
-           g_object_unref (proxy_ck_session);
-           g_error_free (error);
-           return;
-       }
-       
-       has_console = is_active;
-       if (is_active) {
-               obex_services_start ();
-       } else {
-               obex_services_shutdown (); 
+               g_variant_unref (v);
        }
+}
 
-       dbus_g_connection_unref (dbus_connection);
-       g_free (ck_session_path);
-       g_object_unref (proxy_ck_manager);
-       g_object_unref (proxy_ck_session);
-       if (error != NULL) {
-           g_error_free (error);
+static gboolean
+is_session_active (void)
+{
+       GVariant *variant;
+       gboolean is_session_active = FALSE;
+
+       variant = g_dbus_proxy_get_cached_property (session_proxy,
+                                                   "SessionIsActive");
+       if (variant) {
+               is_session_active = g_variant_get_boolean (variant);
+               g_variant_unref (variant);
        }
+
+       return is_session_active;
 }
 
 static void
-consolekit_init (void)
+session_init (void)
 {
-       DBusGConnection *dbus_connection;
-       DBusGProxy *proxy_ck_manager;
-       DBusGProxy *proxy_ck_session;
-       DBusGProxy *proxy_ck_seat;
-       gchar *ck_session_path;
-       gchar *ck_seat_path;
        GError *error = NULL;
 
-       dbus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
-
-       if (!dbus_connection) {
-           g_warning ("Unable to connect to dbus");
-           dbus_g_connection_unref (dbus_connection);
-           return;
-       }
-    
-       proxy_ck_manager = dbus_g_proxy_new_for_name (dbus_connection,
-                                                     CK_NAME,
-                                                     CK_MANAGER_PATH,
-                                                     CK_MANAGER_INTERFACE);
-       if (dbus_g_proxy_call (proxy_ck_manager, "GetCurrentSession",
-                              &error, G_TYPE_INVALID,
-                              DBUS_TYPE_G_OBJECT_PATH, &ck_session_path,
-                              G_TYPE_INVALID) == FALSE) {
-           
-           g_warning ("Couldn't request the name: %s", error->message);
-           g_object_unref (proxy_ck_manager);
-           return;
-       }
-       
-       proxy_ck_session = dbus_g_proxy_new_for_name (dbus_connection,
-                                                     CK_NAME,
-                                                     ck_session_path,
-                                                     CK_SESSION_INTERFACE);
-       if (dbus_g_proxy_call (proxy_ck_session, "GetSeatId",
-                              &error, G_TYPE_INVALID,
-                              DBUS_TYPE_G_OBJECT_PATH, &ck_seat_path,
-                              G_TYPE_INVALID) == FALSE) {
-           
-           g_warning ("Couldn't request the name: %s", error->message);
-           g_object_unref (proxy_ck_session); 
-           return;
-       }
-       
-       proxy_ck_seat = dbus_g_proxy_new_for_name (dbus_connection,
-                                                  CK_NAME,
-                                                  ck_seat_path,
-                                                  CK_SEAT_INTERFACE);
-       dbus_g_proxy_add_signal (proxy_ck_seat, "ActiveSessionChanged",
-                                G_TYPE_STRING, G_TYPE_INVALID);
-       dbus_g_proxy_connect_signal (proxy_ck_seat, "ActiveSessionChanged",
-                                    G_CALLBACK (sessionchanged_cb), NULL, NULL);
-       if (error != NULL) {
-           g_error_free (error);
+       session_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                      G_DBUS_PROXY_FLAGS_NONE,
+                                                      NULL,
+                                                      GNOME_SESSION_DBUS_NAME,
+                                                      GNOME_SESSION_DBUS_OBJECT,
+                                                      GNOME_SESSION_DBUS_INTERFACE,
+                                                      NULL,
+                                                      &error);
+       if (session_proxy == NULL) {
+               g_warning ("Failed to get session proxy: %s", error->message);
+               g_error_free (error);
+               return;
        }
-       g_object_unref (proxy_ck_manager);
-       g_object_unref (proxy_ck_session);
-       g_free (ck_seat_path);
-       dbus_g_connection_unref (dbus_connection);
+       g_signal_connect (session_proxy, "g-properties-changed",
+                         G_CALLBACK (session_properties_changed_cb),
+                         NULL);
+       has_console = is_session_active ();
 }
 
 static void
@@ -502,7 +430,7 @@ main (int argc, char **argv)
        g_signal_connect (settings, "changed", G_CALLBACK(setttings_changed), NULL);
 
        bluez_init ();
-       consolekit_init ();
+       session_init ();
 
        /* Initial setting */
        file_sharing_enabled_changed ();


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