]> www.fi.muni.cz Git - evince.git/blobdiff - cut-n-paste/totem-screensaver/totem-scrsaver.c
[shell] Remove DBUS conditionals from TotemScrsaver
[evince.git] / cut-n-paste / totem-screensaver / totem-scrsaver.c
index 6c0ba44cb8e2f3509ef7d6eb34a1cab718772f87..88ac99352d3f4c05c8264f922a66f4d11f769ed7 100644 (file)
 #endif /* HAVE_XTEST */
 #endif /* GDK_WINDOWING_X11 */
 
-#ifdef ENABLE_DBUS
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib.h>
+#include "totem-scrsaver.h"
 
 #define GS_SERVICE   "org.gnome.ScreenSaver"
 #define GS_PATH      "/org/gnome/ScreenSaver"
 #define GS_INTERFACE "org.gnome.ScreenSaver"
-#endif /* ENABLE_DBUS */
-
-#include "totem-scrsaver.h"
 
 #define XSCREENSAVER_MIN_TIMEOUT 60
 
 static GObjectClass *parent_class = NULL;
 static void totem_scrsaver_finalize   (GObject *object);
 
-
 struct TotemScrsaverPrivate {
        /* Whether the screensaver is disabled */
        gboolean disabled;
 
-#ifdef ENABLE_DBUS
-       DBusGConnection *connection;
-       DBusGProxy *gs_proxy;
+        GDBusConnection *connection;
+        gboolean have_screensaver_dbus;
+        guint watch_id;
        guint32 cookie;
-#endif /* ENABLE_DBUS */
 
        /* To save the screensaver info */
        int timeout;
@@ -75,101 +68,104 @@ struct TotemScrsaverPrivate {
        gboolean have_xtest;
 };
 
+enum {
+        PROP_0,
+        PROP_CONNECTION
+};
+
 G_DEFINE_TYPE(TotemScrsaver, totem_scrsaver, G_TYPE_OBJECT)
 
 static gboolean
 screensaver_is_running_dbus (TotemScrsaver *scr)
 {
-#ifdef ENABLE_DBUS
-       if (! scr->priv->connection)
-               return FALSE;
-
-       if (! scr->priv->gs_proxy)
-               return FALSE;
-
-       return TRUE;
-#else
-       return FALSE;
-#endif /* ENABLE_DBUS */
+        return scr->priv->have_screensaver_dbus;
 }
 
 static void
 screensaver_inhibit_dbus (TotemScrsaver *scr,
                          gboolean       inhibit)
 {
-#ifdef ENABLE_DBUS
-       GError *error;
-       gboolean res;
+        TotemScrsaverPrivate *priv = scr->priv;
+       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 (!priv->have_screensaver_dbus)
+                return;
 
-       error = NULL;
        if (inhibit) {
-               char   *application;
-               char   *reason;
-               guint32 cookie;
-
-               application = g_strdup ("Evince");
-               reason = g_strdup (_("Running in presentation mode"));
-
-               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_call_sync (priv->connection,
+                                                     GS_SERVICE,
+                                                     GS_PATH,
+                                                     GS_INTERFACE,
+                                                     "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)) {
+                       /* try the old API */
+                        g_clear_error (&error);
+                        value = g_dbus_connection_call_sync (priv->connection,
+                                                             GS_SERVICE,
+                                                             GS_PATH,
+                                                             GS_INTERFACE,
+                                                             "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 */
-                       scr->priv->cookie = 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);
                } 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_call_sync (priv->connection,
+                                                     GS_SERVICE,
+                                                     GS_PATH,
+                                                     GS_INTERFACE,
+                                                     "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)) {
+                       /* try the old API */
+                        g_clear_error (&error);
+                        value = g_dbus_connection_call_sync (priv->connection,
+                                                             GS_SERVICE,
+                                                             GS_PATH,
+                                                             GS_INTERFACE,
+                                                             "AllowActivation",
+                                                             g_variant_new ("()"),
+                                                             G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                                                             -1,
+                                                             NULL,
+                                                             &error);
+                }
+                if (value != NULL) {
                        /* clear the cookie */
-                       scr->priv->cookie = 0;
+                       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);
                }
        }
-#endif /* ENABLE_DBUS */
 }
 
 static void
@@ -184,58 +180,44 @@ screensaver_disable_dbus (TotemScrsaver *scr)
        screensaver_inhibit_dbus (scr, TRUE);
 }
 
-#ifdef ENABLE_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);
+        TotemScrsaverPrivate *priv = scr->priv;
+
+        g_assert (connection == priv->connection);
 
-       /* just invalidate for now */
-       scr->priv->gs_proxy = NULL;
+        priv->have_screensaver_dbus = TRUE;
 }
-#endif
 
 static void
-screensaver_init_dbus (TotemScrsaver *scr)
+screensaver_dbus_disappeared_cb (GDBusConnection *connection,
+                                 const char      *name,
+                                 gpointer         user_data)
 {
-#ifdef ENABLE_DBUS
-       GError *error = NULL;
-
-       scr->priv->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+        TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
+        TotemScrsaverPrivate *priv = scr->priv;
 
-       if (! scr->priv->connection) {
-               if (error) {
-                       g_warning ("Failed to connect to the session bus: %s", error->message);
-                       g_error_free (error);
-               }
-               return;
-       }
+        g_assert (connection == priv->connection);
 
-       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);
-
-       }
-#endif /* ENABLE_DBUS */
+        priv->have_screensaver_dbus = FALSE;
 }
 
 static void
 screensaver_finalize_dbus (TotemScrsaver *scr)
 {
-#ifdef ENABLE_DBUS
-       if (scr->priv->gs_proxy) {
-               g_object_unref (scr->priv->gs_proxy);
-       }
-#endif /* ENABLE_DBUS */
+        TotemScrsaverPrivate *priv = scr->priv;
+
+        if (priv->connection == NULL)
+                return;
+
+        g_bus_unwatch_name (priv->watch_id);
+
+        g_object_unref (priv->connection);
 }
 
 #ifdef GDK_WINDOWING_X11
@@ -353,28 +335,84 @@ screensaver_finalize_x11 (TotemScrsaver *scr)
 }
 #endif
 
+static void
+totem_scrsaver_constructed (GObject *object)
+{
+        TotemScrsaver *scr = TOTEM_SCRSAVER (object);
+        TotemScrsaverPrivate *priv = scr->priv;
+
+        if (priv->connection == NULL)
+                return;
+
+        priv->watch_id = g_bus_watch_name_on_connection (priv->connection,
+                                                         GS_SERVICE,
+                                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                                         screensaver_dbus_appeared_cb,
+                                                         screensaver_dbus_disappeared_cb,
+                                                         scr, NULL);
+}
+
+static void
+totem_scrsaver_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+        TotemScrsaver *scr = TOTEM_SCRSAVER (object);
+        TotemScrsaverPrivate *priv = scr->priv;
+
+       switch (prop_id) {
+        case PROP_CONNECTION:
+                priv->connection = g_value_dup_object (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
 static void
 totem_scrsaver_class_init (TotemScrsaverClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-       parent_class = g_type_class_peek_parent (klass);
-
+        object_class->set_property = totem_scrsaver_set_property;
+        object_class->constructed = totem_scrsaver_constructed;
        object_class->finalize = totem_scrsaver_finalize;
+
+       g_object_class_install_property (object_class,
+                                        PROP_CONNECTION,
+                                        g_param_spec_object ("connection", NULL, NULL,
+                                                              G_TYPE_DBUS_CONNECTION,
+                                                             G_PARAM_WRITABLE |
+                                                              G_PARAM_CONSTRUCT_ONLY |
+                                                              G_PARAM_STATIC_STRINGS));
 }
 
+/**
+ * totem_scrsaver_new:
+ * @connection: (allow-none): a #GDBusConnection, or %NULL
+ *
+ * Creates a #TotemScrsaver object. If @connection is non-%NULL,
+ * and the GNOME screen saver is running, it uses its DBUS interface to
+ * inhibit the screensaver; otherwise it falls back to using the X
+ * screensaver functionality for this.
+ *
+ * Returns: a newly created #TotemScrsaver
+ */
 TotemScrsaver *
-totem_scrsaver_new (void)
+totem_scrsaver_new (GDBusConnection *connection)
 {
-       return TOTEM_SCRSAVER (g_object_new (TOTEM_TYPE_SCRSAVER, NULL));
+       return g_object_new (TOTEM_TYPE_SCRSAVER,
+                             "connection", connection,
+                             NULL);
 }
 
 static void
 totem_scrsaver_init (TotemScrsaver *scr)
 {
-       scr->priv = g_new0 (TotemScrsaverPrivate, 1);
+       scr->priv = G_TYPE_INSTANCE_GET_PRIVATE (scr, TOTEM_TYPE_SCRSAVER, TotemScrsaverPrivate);
 
-       screensaver_init_dbus (scr);
 #ifdef GDK_WINDOWING_X11
        screensaver_init_x11 (scr);
 #else
@@ -451,10 +489,5 @@ totem_scrsaver_finalize (GObject *object)
        {}
 #endif
 
-       g_free (scr->priv);
-
-       if (G_OBJECT_CLASS (parent_class)->finalize != NULL) {
-               (* G_OBJECT_CLASS (parent_class)->finalize) (object);
-       }
+        G_OBJECT_CLASS (parent_class)->finalize (object);
 }
-