[totem] Port TotemScrsaver to GDBus



commit 199d02674bcfb105cb6c2e4f2cc4e00585966cfd
Author: Christian Persch <chpe gnome org>
Date:   Mon May 10 12:13:51 2010 +0200

    Port TotemScrsaver to GDBus
    
    (Untested)
    
    Bug #618244.

 configure.in         |    2 +-
 lib/totem-scrsaver.c |  200 ++++++++++++++++++++++++--------------------------
 2 files changed, 98 insertions(+), 104 deletions(-)
---
diff --git a/configure.in b/configure.in
index f88562d..770c83f 100644
--- a/configure.in
+++ b/configure.in
@@ -37,7 +37,7 @@ AC_PATH_PROG([GLIB_GENMARSHAL],[glib-genmarshal])
 AC_PATH_PROG([GLIB_MKENUMS],[glib-mkenums])
 
 # Requirements
-GLIB_REQS=2.22
+GLIB_REQS=2.25.4
 GTK_REQS=2.19.5
 TOTEM_PLPARSER_REQS=2.29.1
 GNOMEICON_REQS=2.15.90
diff --git a/lib/totem-scrsaver.c b/lib/totem-scrsaver.c
index 0888532..420cd48 100644
--- a/lib/totem-scrsaver.c
+++ b/lib/totem-scrsaver.c
@@ -37,9 +37,6 @@
 #endif /* GDK_WINDOWING_X11 */
 
 #ifdef WITH_DBUS
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib.h>
-
 #define GS_SERVICE   "org.gnome.ScreenSaver"
 #define GS_PATH      "/org/gnome/ScreenSaver"
 #define GS_INTERFACE "org.gnome.ScreenSaver"
@@ -58,8 +55,8 @@ struct TotemScrsaverPrivate {
 	gboolean disabled;
 
 #ifdef WITH_DBUS
-	DBusGConnection *connection;
-	DBusGProxy *gs_proxy;
+        GDBusConnection *connection;
+        guint watch_id;
 	guint32 cookie;
 #endif /* WITH_DBUS */
 
@@ -81,13 +78,7 @@ static gboolean
 screensaver_is_running_dbus (TotemScrsaver *scr)
 {
 #ifdef WITH_DBUS
-	if (! scr->priv->connection)
-		return FALSE;
-
-	if (! scr->priv->gs_proxy)
-		return FALSE;
-
-	return TRUE;
+        return scr->priv->connection != NULL;
 #else
 	return FALSE;
 #endif /* WITH_DBUS */
@@ -98,74 +89,83 @@ screensaver_inhibit_dbus (TotemScrsaver *scr,
 			  gboolean	 inhibit)
 {
 #ifdef WITH_DBUS
-	GError *error;
-	gboolean res;
+	GError *error = NULL;
+        GVariant *value;
 
-	g_return_if_fail (scr != NULL);
-	g_return_if_fail (scr->priv->connection != NULL);
-	g_return_if_fail (scr->priv->gs_proxy != NULL);
+        if (scr->priv->connection == NULL)
+                return;
 
-	error = NULL;
 	if (inhibit) {
-		char   *application;
-		char   *reason;
-		guint32 cookie;
-
-		application = g_strdup ("Totem");
-		reason = g_strdup (_("Playing a movie"));
-
-		res = dbus_g_proxy_call (scr->priv->gs_proxy,
-					 "Inhibit",
-					 &error,
-					 G_TYPE_STRING, application,
-					 G_TYPE_STRING, reason,
-					 G_TYPE_INVALID,
-					 G_TYPE_UINT, &cookie,
-					 G_TYPE_INVALID);
-
-		if (res) {
+                value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
+                                                              GS_SERVICE,
+                                                              GS_PATH,
+                                                              GS_INTERFACE,
+                                                              "Inhibit",
+                                                              g_variant_new ("(ss)",
+                                                                             "Totem",
+                                                                             _("Playing a movie")),
+                                                              G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
+                                                              -1,
+                                                              NULL,
+                                                              &error);
+		if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
+			/* try the old API */
+                        g_clear_error (&error);
+                        value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
+                                                                      GS_SERVICE,
+                                                                      GS_PATH,
+                                                                      GS_INTERFACE,
+                                                                      "InhibitActivation",
+                                                                      g_variant_new ("(s)",
+                                                                                     _("Playing a movie")),
+                                                                      G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
+                                                                      -1,
+                                                                      NULL,
+                                                                      &error);
+                }
+                if (value != NULL) {
 			/* save the cookie */
-			scr->priv->cookie = cookie;
+                        if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(u)")))
+			       g_variant_get (value, "(u)", &scr->priv->cookie);
+                        else
+                                scr->priv->cookie = 0;
+                        g_variant_unref (value);
 		} else {
-			/* try the old API */
-			res = dbus_g_proxy_call (scr->priv->gs_proxy,
-						 "InhibitActivation",
-						 NULL,
-						 G_TYPE_STRING, reason,
-						 G_TYPE_INVALID,
-						 G_TYPE_INVALID);
-			if (res)
-				g_error_free (error);
+			g_warning ("Problem inhibiting the screensaver: %s", error->message);
+                        g_error_free (error);
 		}
 
-		g_free (reason);
-		g_free (application);
-
 	} else {
-		res = dbus_g_proxy_call (scr->priv->gs_proxy,
-					 "UnInhibit",
-					 &error,
-					 G_TYPE_UINT, scr->priv->cookie,
-					 G_TYPE_INVALID,
-					 G_TYPE_INVALID);
-		if (res) {
+                value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
+                                                              GS_SERVICE,
+                                                              GS_PATH,
+                                                              GS_INTERFACE,
+                                                              "UnInhibit",
+                                                              g_variant_new ("(u)", scr->priv->cookie),
+                                                              G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
+                                                              -1,
+                                                              NULL,
+                                                              &error);
+		if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
+			/* try the old API */
+                        g_clear_error (&error);
+                        value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
+                                                                      GS_SERVICE,
+                                                                      GS_PATH,
+                                                                      GS_INTERFACE,
+                                                                      "AllowActivation",
+                                                                      g_variant_new ("()"),
+                                                                      G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
+                                                                      -1,
+                                                                      NULL,
+                                                                      &error);
+                }
+                if (value != NULL) {
 			/* clear the cookie */
 			scr->priv->cookie = 0;
+                        g_variant_unref (value);
 		} else {
-			/* try the old API */
-			res = dbus_g_proxy_call (scr->priv->gs_proxy,
-						 "AllowActivation",
-						 NULL,
-						 G_TYPE_INVALID,
-						 G_TYPE_INVALID);
-			if (res)
-				g_error_free (error);
-		}
-	}
-
-	if (! res) {
-		if (error) {
-			g_warning ("Problem inhibiting the screensaver: %s", error->message);
+			g_warning ("Problem uninhibiting the screensaver: %s", error->message);
 			g_error_free (error);
 		}
 	}
@@ -186,13 +186,26 @@ screensaver_disable_dbus (TotemScrsaver *scr)
 
 #ifdef WITH_DBUS
 static void
-gs_proxy_destroy_cb (GObject *proxy,
-		     TotemScrsaver *scr)
+screensaver_dbus_appeared_cb (GDBusConnection *connection,
+                              const char      *name,
+                              const char      *name_owner,
+                              gpointer         user_data)
 {
-	g_warning ("Detected that GNOME screensaver has left the bus");
+        TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
 
-	/* just invalidate for now */
-	scr->priv->gs_proxy = NULL;
+        scr->priv->connection = g_object_ref (connection);
+}
+
+static void
+screensaver_dbus_disappeared_cb (GDBusConnection *connection,
+                                 const char      *name,
+                                 gpointer         user_data)
+{
+        TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
+
+        g_assert (scr->priv->connection == connection);
+        g_object_unref (scr->priv->connection);
+        scr->priv->connection = NULL;
 }
 #endif
 
@@ -200,32 +213,12 @@ static void
 screensaver_init_dbus (TotemScrsaver *scr)
 {
 #ifdef WITH_DBUS
-	GError *error = NULL;
-
-	scr->priv->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-
-	if (! scr->priv->connection) {
-		if (error) {
-			g_warning ("Failed to connect to the session bus: %s", error->message);
-			g_error_free (error);
-		}
-		return;
-	}
-
-	scr->priv->gs_proxy = dbus_g_proxy_new_for_name_owner (scr->priv->connection,
-							       GS_SERVICE,
-							       GS_PATH,
-							       GS_INTERFACE,
-							       NULL);
-	if (scr->priv->gs_proxy != NULL) {
-		g_signal_connect_object (scr->priv->gs_proxy,
-					 "destroy",
-					 G_CALLBACK (gs_proxy_destroy_cb),
-					 scr,
-					 0);
-
-	}
-
+        scr->priv->watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
+                                                GS_SERVICE,
+                                                G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                                screensaver_dbus_appeared_cb,
+                                                screensaver_dbus_disappeared_cb,
+                                                scr, NULL);
 #endif /* WITH_DBUS */
 }
 
@@ -233,9 +226,10 @@ static void
 screensaver_finalize_dbus (TotemScrsaver *scr)
 {
 #ifdef WITH_DBUS
-	if (scr->priv->gs_proxy) {
-		g_object_unref (scr->priv->gs_proxy);
-	}
+        g_bus_unwatch_name (scr->priv->watch_id);
+
+        if (scr->priv->connection != NULL)
+                g_object_unref (scr->priv->connection);
 #endif /* WITH_DBUS */
 }
 



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