gsm-presence.c 18.4 KB
Newer Older
William Jon McCann's avatar
William Jon McCann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2009 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
William Jon McCann's avatar
William Jon McCann committed
17 18 19 20 21 22 23 24 25 26
 */

#include "config.h"

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

27 28
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-idle-monitor.h>
William Jon McCann's avatar
William Jon McCann committed
29 30

#include "gsm-presence.h"
31
#include "org.gnome.SessionManager.Presence.h"
William Jon McCann's avatar
William Jon McCann committed
32

33
#define GSM_PRESENCE_DBUS_IFACE "org.gnome.SessionManager.Presence"
William Jon McCann's avatar
William Jon McCann committed
34 35
#define GSM_PRESENCE_DBUS_PATH "/org/gnome/SessionManager/Presence"

36 37 38 39
#define GS_NAME      "org.gnome.ScreenSaver"
#define GS_PATH      "/org/gnome/ScreenSaver"
#define GS_INTERFACE "org.gnome.ScreenSaver"

40 41
#define MAX_STATUS_TEXT 140

William Jon McCann's avatar
William Jon McCann committed
42 43 44 45
#define GSM_PRESENCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_PRESENCE, GsmPresencePrivate))

struct GsmPresencePrivate
{
46 47 48 49 50 51 52 53
        guint             status;
        guint             saved_status;
        char             *status_text;
        gboolean          idle_enabled;
        GnomeIdleMonitor *idle_monitor;
        guint             idle_watch_id;
        guint             idle_timeout;
        gboolean          screensaver_active;
54 55 56 57
        GDBusConnection  *connection;
        GDBusProxy       *screensaver_proxy;

        GsmExportedPresence *skeleton;
William Jon McCann's avatar
William Jon McCann committed
58 59 60 61 62 63 64 65 66 67 68 69 70
};

enum {
        PROP_0,
        PROP_IDLE_ENABLED,
        PROP_IDLE_TIMEOUT,
};

enum {
        STATUS_CHANGED,
        LAST_SIGNAL
};

71
static guint signals[LAST_SIGNAL] = { 0, };
William Jon McCann's avatar
William Jon McCann committed
72 73 74

G_DEFINE_TYPE (GsmPresence, gsm_presence, G_TYPE_OBJECT)

75 76 77 78
static const GDBusErrorEntry gsm_presence_error_entries[] = {
        { GSM_PRESENCE_ERROR_GENERAL, GSM_PRESENCE_DBUS_IFACE ".GeneralError" }
};

William Jon McCann's avatar
William Jon McCann committed
79 80 81
GQuark
gsm_presence_error_quark (void)
{
82
        static volatile gsize quark_volatile = 0;
William Jon McCann's avatar
William Jon McCann committed
83

84 85 86 87 88
        g_dbus_error_register_error_domain ("gsm_presence_error",
                                            &quark_volatile,
                                            gsm_presence_error_entries,
                                            G_N_ELEMENTS (gsm_presence_error_entries));
        return quark_volatile;
William Jon McCann's avatar
William Jon McCann committed
89 90
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void idle_became_active_cb (GnomeIdleMonitor *idle_monitor,
                                   guint             id,
                                   gpointer          user_data);

static void
gsm_presence_set_status (GsmPresence  *presence,
                         guint         status)
{
        if (status != presence->priv->status) {
                presence->priv->status = status;
                gsm_exported_presence_set_status (presence->priv->skeleton, status);
                gsm_exported_presence_emit_status_changed (presence->priv->skeleton, presence->priv->status);
                g_signal_emit (presence, signals[STATUS_CHANGED], 0, presence->priv->status);
        }
}
William Jon McCann's avatar
William Jon McCann committed
106

107 108 109 110
static gboolean
gsm_presence_set_status_text (GsmPresence  *presence,
                              const char   *status_text,
                              GError      **error)
William Jon McCann's avatar
William Jon McCann committed
111
{
112
        g_return_val_if_fail (GSM_IS_PRESENCE (presence), FALSE);
William Jon McCann's avatar
William Jon McCann committed
113

114
        g_free (presence->priv->status_text);
Ray Strode's avatar
Ray Strode committed
115
        presence->priv->status_text = NULL;
William Jon McCann's avatar
William Jon McCann committed
116

117 118 119 120 121 122 123 124
        /* check length */
        if (status_text != NULL && strlen (status_text) > MAX_STATUS_TEXT) {
                g_set_error (error,
                             GSM_PRESENCE_ERROR,
                             GSM_PRESENCE_ERROR_GENERAL,
                             "Status text too long");
                return FALSE;
        }
William Jon McCann's avatar
William Jon McCann committed
125

126 127 128 129
        if (status_text != NULL) {
                presence->priv->status_text = g_strdup (status_text);
        } else {
                presence->priv->status_text = g_strdup ("");
William Jon McCann's avatar
William Jon McCann committed
130 131
        }

132 133 134
        gsm_exported_presence_set_status_text (presence->priv->skeleton, presence->priv->status_text);
        gsm_exported_presence_emit_status_text_changed (presence->priv->skeleton, presence->priv->status_text);
        return TRUE;
William Jon McCann's avatar
William Jon McCann committed
135 136
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150
static void
set_session_idle (GsmPresence   *presence,
                  gboolean       is_idle)
{
        g_debug ("GsmPresence: setting idle: %d", is_idle);

        if (is_idle) {
                if (presence->priv->status == GSM_PRESENCE_STATUS_IDLE) {
                        g_debug ("GsmPresence: already idle, ignoring");
                        return;
                }

                /* save current status */
                presence->priv->saved_status = presence->priv->status;
151
                gsm_presence_set_status (presence, GSM_PRESENCE_STATUS_IDLE);
152 153 154 155 156

                gnome_idle_monitor_add_user_active_watch (presence->priv->idle_monitor,
                                                          idle_became_active_cb,
                                                          presence,
                                                          NULL);
157 158 159 160 161 162 163
        } else {
                if (presence->priv->status != GSM_PRESENCE_STATUS_IDLE) {
                        g_debug ("GsmPresence: already not idle, ignoring");
                        return;
                }

                /* restore saved status */
164
                gsm_presence_set_status (presence, presence->priv->saved_status);
165
                g_debug ("GsmPresence: setting non-idle status %d", presence->priv->saved_status);
166 167 168 169
                presence->priv->saved_status = GSM_PRESENCE_STATUS_AVAILABLE;
        }
}

170 171 172 173
static void
idle_became_idle_cb (GnomeIdleMonitor *idle_monitor,
                     guint             id,
                     gpointer          user_data)
174
{
175
        GsmPresence *presence = user_data;
176 177 178 179
        /* We should already be idle,
         * see on_screensaver_active_changed() */
        if (!presence->priv->screensaver_active)
                set_session_idle (presence, TRUE);
180
}
181

182 183
static void
idle_became_active_cb (GnomeIdleMonitor *idle_monitor,
184 185
                       guint             id,
                       gpointer          user_data)
186
{
187
        GsmPresence *presence = user_data;
188 189 190
        /* We can only be non-idle if the screensaver is gone */
        if (!presence->priv->screensaver_active)
                set_session_idle (presence, FALSE);
191 192 193 194 195 196
}

static void
reset_idle_watch (GsmPresence  *presence)
{
        if (presence->priv->idle_watch_id > 0) {
197
                g_debug ("GsmPresence: removing idle watch (%i)", presence->priv->idle_watch_id);
198 199
                gnome_idle_monitor_remove_watch (presence->priv->idle_monitor,
                                                 presence->priv->idle_watch_id);
200 201 202
                presence->priv->idle_watch_id = 0;
        }

203
        if (presence->priv->idle_enabled
204
            && presence->priv->idle_timeout > 0) {
205 206 207 208 209
                presence->priv->idle_watch_id = gnome_idle_monitor_add_idle_watch (presence->priv->idle_monitor,
                                                                                   presence->priv->idle_timeout,
                                                                                   idle_became_idle_cb,
                                                                                   presence,
                                                                                   NULL);
210 211 212
                g_debug ("GsmPresence: adding idle watch (%i) for %d secs",
                         presence->priv->idle_watch_id,
                         presence->priv->idle_timeout / 1000);
213 214 215 216
        }
}

static void
217 218 219 220 221
on_screensaver_dbus_signal (GDBusProxy  *proxy,
                            gchar       *sender_name,
                            gchar       *signal_name,
                            GVariant    *parameters,
                            GsmPresence *presence)
222
{
223 224 225 226 227 228 229 230
        gboolean is_active;

        if (g_strcmp0 (signal_name, "ActiveChanged") != 0) {
                return;
        }

        g_variant_get (parameters, "(b)", &is_active);

231 232 233 234 235 236 237
        if (presence->priv->screensaver_active != is_active) {
                presence->priv->screensaver_active = is_active;
                set_session_idle (presence, is_active);
        }
}

static void
238 239 240
on_screensaver_name_owner_changed (GDBusProxy  *screensaver_proxy,
                                   GParamSpec  *pspec,
                                   GsmPresence *presence)
241
{
242 243 244 245 246 247 248 249 250 251
        gchar *name_owner;

        name_owner = g_dbus_proxy_get_name_owner (screensaver_proxy);
        if (name_owner == NULL) {
                g_debug ("Detected that screensaver has left the bus");

                presence->priv->screensaver_proxy = NULL;
                presence->priv->screensaver_active = FALSE;
                set_session_idle (presence, FALSE);
        }
252

253
        g_free (name_owner);
254 255
}

256 257 258 259 260
static gboolean
gsm_presence_set_status_text_dbus (GsmExportedPresence   *skeleton,
                                   GDBusMethodInvocation *invocation,
                                   gchar                 *status_text,
                                   GsmPresence           *presence)
261
{
262
        GError *error = NULL;
263

264 265 266 267
        if (gsm_presence_set_status_text (presence, status_text, &error)) {
                gsm_exported_presence_complete_set_status_text (skeleton, invocation);
        } else {
                g_dbus_method_invocation_take_error (invocation, error);
268 269
        }

270 271 272 273 274 275 276 277 278 279 280 281
        return TRUE;
}

static gboolean
gsm_presence_set_status_dbus (GsmExportedPresence   *skeleton,
                              GDBusMethodInvocation *invocation,
                              guint                  status,
                              GsmPresence           *presence)
{
        gsm_presence_set_status (presence, status);
        gsm_exported_presence_complete_set_status (skeleton, invocation);
        return TRUE;
282 283
}

William Jon McCann's avatar
William Jon McCann committed
284 285 286
static gboolean
register_presence (GsmPresence *presence)
{
287
        GError *error;
288
        GsmExportedPresence *skeleton;
William Jon McCann's avatar
William Jon McCann committed
289 290

        error = NULL;
291 292 293 294
        presence->priv->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
        if (error != NULL) {
                g_critical ("error getting session bus: %s", error->message);
                g_error_free (error);
William Jon McCann's avatar
William Jon McCann committed
295 296 297
                return FALSE;
        }

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        skeleton = gsm_exported_presence_skeleton_new ();
        presence->priv->skeleton = skeleton;
        g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
                                          presence->priv->connection,
                                          GSM_PRESENCE_DBUS_PATH, &error);
        if (error != NULL) {
                g_critical ("error registering presence object on session bus: %s", error->message);
                g_error_free (error);
                return FALSE;
        }

        g_signal_connect (skeleton, "handle-set-status",
                          G_CALLBACK (gsm_presence_set_status_dbus), presence);
        g_signal_connect (skeleton, "handle-set-status-text",
                          G_CALLBACK (gsm_presence_set_status_text_dbus), presence);
William Jon McCann's avatar
William Jon McCann committed
313 314 315 316 317 318 319 320 321 322 323

        return TRUE;
}

static GObject *
gsm_presence_constructor (GType                  type,
                          guint                  n_construct_properties,
                          GObjectConstructParam *construct_properties)
{
        GsmPresence *presence;
        gboolean     res;
324
        GError      *error = NULL;
William Jon McCann's avatar
William Jon McCann committed
325 326 327 328 329 330 331 332 333 334

        presence = GSM_PRESENCE (G_OBJECT_CLASS (gsm_presence_parent_class)->constructor (type,
                                                                                             n_construct_properties,
                                                                                             construct_properties));

        res = register_presence (presence);
        if (! res) {
                g_warning ("Unable to register presence with session bus");
        }

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        presence->priv->screensaver_proxy = g_dbus_proxy_new_sync (presence->priv->connection,
                                                                   G_DBUS_PROXY_FLAGS_NONE,
                                                                   NULL,
                                                                   GS_NAME,
                                                                   GS_PATH,
                                                                   GS_INTERFACE,
                                                                   NULL, &error);
        if (error != NULL) {
                g_critical ("Unable to create a DBus proxy for GnomeScreensaver: %s",
                            error->message);
                g_error_free (error);
        } else {
                g_signal_connect (presence->priv->screensaver_proxy, "notify::g-name-owner",
                                  G_CALLBACK (on_screensaver_name_owner_changed), presence);
                g_signal_connect (presence->priv->screensaver_proxy, "g-signal",
                                  G_CALLBACK (on_screensaver_dbus_signal), presence);
351 352
        }

William Jon McCann's avatar
William Jon McCann committed
353 354 355 356 357 358 359 360
        return G_OBJECT (presence);
}

static void
gsm_presence_init (GsmPresence *presence)
{
        presence->priv = GSM_PRESENCE_GET_PRIVATE (presence);

361
        presence->priv->idle_monitor = gnome_idle_monitor_new ();
William Jon McCann's avatar
William Jon McCann committed
362 363 364 365 366 367 368 369 370
}

void
gsm_presence_set_idle_enabled (GsmPresence  *presence,
                               gboolean      enabled)
{
        g_return_if_fail (GSM_IS_PRESENCE (presence));

        if (presence->priv->idle_enabled != enabled) {
371 372
                presence->priv->idle_enabled = enabled;
                reset_idle_watch (presence);
William Jon McCann's avatar
William Jon McCann committed
373 374 375 376 377 378 379 380 381 382 383 384 385
                g_object_notify (G_OBJECT (presence), "idle-enabled");

        }
}

void
gsm_presence_set_idle_timeout (GsmPresence  *presence,
                               guint         timeout)
{
        g_return_if_fail (GSM_IS_PRESENCE (presence));

        if (timeout != presence->priv->idle_timeout) {
                presence->priv->idle_timeout = timeout;
386
                reset_idle_watch (presence);
William Jon McCann's avatar
William Jon McCann committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
                g_object_notify (G_OBJECT (presence), "idle-timeout");
        }
}

static void
gsm_presence_set_property (GObject       *object,
                           guint          prop_id,
                           const GValue  *value,
                           GParamSpec    *pspec)
{
        GsmPresence *self;

        self = GSM_PRESENCE (object);

        switch (prop_id) {
        case PROP_IDLE_ENABLED:
                gsm_presence_set_idle_enabled (self, g_value_get_boolean (value));
                break;
        case PROP_IDLE_TIMEOUT:
                gsm_presence_set_idle_timeout (self, g_value_get_uint (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gsm_presence_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
        GsmPresence *self;

        self = GSM_PRESENCE (object);

        switch (prop_id) {
        case PROP_IDLE_ENABLED:
                g_value_set_boolean (value, self->priv->idle_enabled);
                break;
        case PROP_IDLE_TIMEOUT:
                g_value_set_uint (value, self->priv->idle_timeout);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gsm_presence_finalize (GObject *object)
{
        GsmPresence *presence = (GsmPresence *) object;

        if (presence->priv->idle_watch_id > 0) {
443 444
                gnome_idle_monitor_remove_watch (presence->priv->idle_monitor,
                                                 presence->priv->idle_watch_id);
445
                presence->priv->idle_watch_id = 0;
William Jon McCann's avatar
William Jon McCann committed
446 447
        }

448 449
        g_clear_pointer (&presence->priv->status_text, g_free);
        g_clear_object (&presence->priv->idle_monitor);
William Jon McCann's avatar
William Jon McCann committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

        G_OBJECT_CLASS (gsm_presence_parent_class)->finalize (object);
}

static void
gsm_presence_class_init (GsmPresenceClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize             = gsm_presence_finalize;
        object_class->constructor          = gsm_presence_constructor;
        object_class->get_property         = gsm_presence_get_property;
        object_class->set_property         = gsm_presence_set_property;

        signals [STATUS_CHANGED] =
                g_signal_new ("status-changed",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GsmPresenceClass, status_changed),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__UINT,
                              G_TYPE_NONE,
                              1, G_TYPE_UINT);

        g_object_class_install_property (object_class,
                                         PROP_IDLE_ENABLED,
                                         g_param_spec_boolean ("idle-enabled",
                                                               NULL,
                                                               NULL,
                                                               FALSE,
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
        g_object_class_install_property (object_class,
                                         PROP_IDLE_TIMEOUT,
                                         g_param_spec_uint ("idle-timeout",
                                                            "idle timeout",
                                                            "idle timeout",
                                                            0,
                                                            G_MAXINT,
489
                                                            120000,
William Jon McCann's avatar
William Jon McCann committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
                                                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

        g_type_class_add_private (klass, sizeof (GsmPresencePrivate));
}

GsmPresence *
gsm_presence_new (void)
{
        GsmPresence *presence;

        presence = g_object_new (GSM_TYPE_PRESENCE,
                                 NULL);

        return presence;
}