[evince] [totem-screensaver] Use the async GDBus api



commit 2085e3e413cc926551b9ba6ce91689f9b9e236ce
Author: Carlos Garcia Campos <carlosgc gnome org>
Date:   Tue May 25 13:05:59 2010 +0200

    [totem-screensaver] Use the async GDBus api

 cut-n-paste/totem-screensaver/totem-scrsaver.c |  160 +++++++++++++++---------
 1 files changed, 99 insertions(+), 61 deletions(-)
---
diff --git a/cut-n-paste/totem-screensaver/totem-scrsaver.c b/cut-n-paste/totem-screensaver/totem-scrsaver.c
index ba05d3f..2b6c0d3 100644
--- a/cut-n-paste/totem-screensaver/totem-scrsaver.c
+++ b/cut-n-paste/totem-screensaver/totem-scrsaver.c
@@ -55,6 +55,7 @@ struct TotemScrsaverPrivate {
         gboolean have_screensaver_dbus;
         guint watch_id;
 	guint32 cookie;
+	gboolean old_dbus_api;
 
 	/* To save the screensaver info */
 	int timeout;
@@ -77,77 +78,114 @@ screensaver_is_running_dbus (TotemScrsaver *scr)
 }
 
 static void
-screensaver_inhibit_dbus (TotemScrsaver *scr,
-			  gboolean	 inhibit)
+on_inhibit_cb (GObject      *source_object,
+	       GAsyncResult *res,
+	       gpointer      user_data)
 {
-        TotemScrsaverPrivate *priv = scr->priv;
-	GError *error = NULL;
-        GVariant *value;
-
-        if (!priv->have_screensaver_dbus)
-                return;
-
-	if (inhibit) {
-                value = g_dbus_proxy_call_sync (priv->gs_proxy,
-						"Inhibit",
-						g_variant_new ("(ss)",
-							       "Evince",
-							       _("Running in presentation mode")),
-						G_DBUS_CALL_FLAGS_NO_AUTO_START,
-						-1,
-						NULL,
-						&error);
-		if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
+	GDBusProxy    *proxy = G_DBUS_PROXY (source_object);
+	TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
+	GVariant      *value;
+	GError        *error = NULL;
+
+	value = g_dbus_proxy_call_finish (proxy, res, &error);
+	if (!value) {
+		if (!scr->priv->old_dbus_api &&
+		    g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
 			/* try the old API */
-                        g_clear_error (&error);
-                        value = g_dbus_proxy_call_sync (priv->gs_proxy,
-							"InhibitActivation",
-							g_variant_new ("(s)",
-								       _("Running in presentation mode")),
-							G_DBUS_CALL_FLAGS_NO_AUTO_START,
-							-1,
-							NULL,
-							&error);
-                }
-                if (value != NULL) {
-			/* save the cookie */
-                        if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(u)")))
-			       g_variant_get (value, "(u)", &priv->cookie);
-                        else
-                                priv->cookie = 0;
-                        g_variant_unref (value);
+			scr->priv->old_dbus_api = TRUE;
+			g_dbus_proxy_call (proxy,
+					   "InhibitActivation",
+					   g_variant_new ("(s)",
+							  _("Running in presentation mode")),
+					   G_DBUS_CALL_FLAGS_NO_AUTO_START,
+					   -1,
+					   NULL,
+					   on_inhibit_cb,
+					   scr);
 		} else {
 			g_warning ("Problem inhibiting the screensaver: %s", error->message);
-                        g_error_free (error);
 		}
+		g_error_free (error);
 
-	} else {
-                value = g_dbus_proxy_call_sync (priv->gs_proxy,
-						"UnInhibit",
-						g_variant_new ("(u)", priv->cookie),
-						G_DBUS_CALL_FLAGS_NO_AUTO_START,
-						-1,
-						NULL,
-						&error);
-		if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
+		return;
+	}
+
+	/* save the 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);
+}
+
+static void
+on_uninhibit_cb (GObject      *source_object,
+		 GAsyncResult *res,
+		 gpointer      user_data)
+{
+	GDBusProxy    *proxy = G_DBUS_PROXY (source_object);
+	TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
+	GVariant      *value;
+	GError        *error = NULL;
+
+	value = g_dbus_proxy_call_finish (proxy, res, &error);
+	if (!value) {
+		if (!scr->priv->old_dbus_api &&
+		    g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
 			/* try the old API */
-                        g_clear_error (&error);
-                        value = g_dbus_proxy_call_sync (priv->gs_proxy,
-							"AllowActivation",
-							g_variant_new ("()"),
-							G_DBUS_CALL_FLAGS_NO_AUTO_START,
-							-1,
-							NULL,
-							&error);
-                }
-                if (value != NULL) {
-			/* clear the cookie */
-			priv->cookie = 0;
-                        g_variant_unref (value);
+			scr->priv->old_dbus_api = TRUE;
+			g_dbus_proxy_call (proxy,
+					   "AllowActivation",
+					   g_variant_new ("()"),
+					   G_DBUS_CALL_FLAGS_NO_AUTO_START,
+					   -1,
+					   NULL,
+					   on_uninhibit_cb,
+					   scr);
 		} else {
 			g_warning ("Problem uninhibiting the screensaver: %s", error->message);
-			g_error_free (error);
 		}
+		g_error_free (error);
+
+		return;
+	}
+
+	/* clear the cookie */
+	scr->priv->cookie = 0;
+	g_variant_unref (value);
+}
+
+static void
+screensaver_inhibit_dbus (TotemScrsaver *scr,
+			  gboolean	 inhibit)
+{
+        TotemScrsaverPrivate *priv = scr->priv;
+
+        if (!priv->have_screensaver_dbus)
+                return;
+
+	scr->priv->old_dbus_api = FALSE;
+
+	if (inhibit) {
+		g_dbus_proxy_call (priv->gs_proxy,
+				   "Inhibit",
+				   g_variant_new ("(ss)",
+						  "Evince",
+						  _("Running in presentation mode")),
+				   G_DBUS_CALL_FLAGS_NO_AUTO_START,
+				   -1,
+				   NULL,
+				   on_inhibit_cb,
+				   scr);
+	} else {
+                g_dbus_proxy_call (priv->gs_proxy,
+				   "UnInhibit",
+				   g_variant_new ("(u)", priv->cookie),
+				   G_DBUS_CALL_FLAGS_NO_AUTO_START,
+				   -1,
+				   NULL,
+				   on_uninhibit_cb,
+				   scr);
 	}
 }
 



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