gsm-consolekit.c 35.3 KB
Newer Older
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) 2008 Jon McCann <jmccann@redhat.com>
 *
 * 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, 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 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/>.
17 18 19 20 21 22 23 24 25 26 27 28 29
 */

#include "config.h"

#include <errno.h>
#include <string.h>
#include <unistd.h>

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>

#include <dbus/dbus-glib.h>
30
#include <dbus/dbus-glib-lowlevel.h>
31

32
#ifdef HAVE_OLD_UPOWER
33
#define UPOWER_ENABLE_DEPRECATED 1
34
#include <upower.h>
35
#endif
36

37
#include "gsm-system.h"
38 39 40 41 42 43 44 45 46 47
#include "gsm-consolekit.h"

#define CK_NAME      "org.freedesktop.ConsoleKit"
#define CK_PATH      "/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"

48 49
#define GSM_CONSOLEKIT_SESSION_TYPE_LOGIN_WINDOW "LoginWindow"

50 51 52 53 54 55 56 57
#define GSM_CONSOLEKIT_GET_PRIVATE(o)                                   \
        (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_CONSOLEKIT, GsmConsolekitPrivate))

struct _GsmConsolekitPrivate
{
        DBusGConnection *dbus_connection;
        DBusGProxy      *bus_proxy;
        DBusGProxy      *ck_proxy;
58
        DBusGProxy      *session_proxy;
59
#ifdef HAVE_OLD_UPOWER
60
        UpClient        *up_client;
61
#endif
62 63

        gboolean         is_active;
64
        gboolean         restarting;
65 66

        gchar            *session_id;
67 68 69 70 71
};

enum {
        PROP_0,
        PROP_ACTIVE
72 73 74 75
};

static void     gsm_consolekit_class_init   (GsmConsolekitClass *klass);
static void     gsm_consolekit_init         (GsmConsolekit      *ck);
76 77 78 79 80 81 82
static void     gsm_consolekit_finalize     (GObject            *object);

static void     gsm_consolekit_free_dbus    (GsmConsolekit      *manager);

static DBusHandlerResult gsm_consolekit_dbus_filter (DBusConnection *connection,
                                                     DBusMessage    *message,
                                                     void           *user_data);
83 84 85 86 87 88 89

static void     gsm_consolekit_on_name_owner_changed (DBusGProxy        *bus_proxy,
                                                      const char        *name,
                                                      const char        *prev_owner,
                                                      const char        *new_owner,
                                                      GsmConsolekit   *manager);

90 91 92 93 94
static void gsm_consolekit_system_init (GsmSystemInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GsmConsolekit, gsm_consolekit, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GSM_TYPE_SYSTEM,
                                                gsm_consolekit_system_init))
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void
gsm_consolekit_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
        GsmConsolekit *self = GSM_CONSOLEKIT (object);

        switch (prop_id) {
        case PROP_ACTIVE:
                self->priv->is_active = g_value_get_boolean (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gsm_consolekit_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
        GsmConsolekit *self = GSM_CONSOLEKIT (object);

        switch (prop_id) {
        case PROP_ACTIVE:
                g_value_set_boolean (value, self->priv->is_active);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

131 132 133 134 135 136 137
static void
gsm_consolekit_class_init (GsmConsolekitClass *manager_class)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (manager_class);

138 139
        object_class->get_property = gsm_consolekit_get_property;
        object_class->set_property = gsm_consolekit_set_property;
140 141
        object_class->finalize = gsm_consolekit_finalize;

142 143
        g_object_class_override_property (object_class, PROP_ACTIVE, "active");

144 145 146
        g_type_class_add_private (manager_class, sizeof (GsmConsolekitPrivate));
}

147 148 149 150 151 152 153 154 155 156 157 158 159
static DBusHandlerResult
gsm_consolekit_dbus_filter (DBusConnection *connection,
                            DBusMessage    *message,
                            void           *user_data)
{
        GsmConsolekit *manager;

        manager = GSM_CONSOLEKIT (user_data);

        if (dbus_message_is_signal (message,
                                    DBUS_INTERFACE_LOCAL, "Disconnected") &&
            strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0) {
                gsm_consolekit_free_dbus (manager);
160 161
                /* let other filters get this disconnected signal, so that they
                 * can handle it too */
162 163 164 165 166
        }

        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

167 168 169 170 171 172 173 174 175 176 177 178
static void
is_active_cb (DBusGProxy     *proxy,
              DBusGProxyCall *call,
              gpointer        data)
{
        GsmConsolekit *self = data;
        GError *local_error = NULL;
        gboolean is_active;

        if (!dbus_g_proxy_end_call (proxy, call, &local_error,
                                    G_TYPE_BOOLEAN, &is_active,
                                    G_TYPE_INVALID)) {
179
                g_warning ("Failed IsActive call to ConsoleKit: %s",
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                           local_error->message);
                g_clear_error (&local_error);
                return;
        }

        if (is_active != self->priv->is_active) {
                self->priv->is_active = is_active;
                g_object_notify ((GObject*) self, "active");
        }
}

static void
on_active_changed (DBusGProxy   *proxy,
                   gboolean      is_active,
                   GsmConsolekit *self)
{
        if (is_active != self->priv->is_active) {
                self->priv->is_active = is_active;
                g_object_notify ((GObject*) self, "active");
        }
}

202 203 204 205 206 207
static gboolean
gsm_consolekit_ensure_ck_connection (GsmConsolekit  *manager,
                                     GError        **error)
{
        GError  *connection_error;
        gboolean is_connected;
208 209
        gboolean ret;
        guint32 pid;
210 211

        connection_error = NULL;
212 213
        manager->priv->session_id = NULL;
        is_connected = FALSE;
214 215

        if (manager->priv->dbus_connection == NULL) {
216 217
                DBusConnection *connection;

218 219 220 221 222 223 224 225
                manager->priv->dbus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM,
                                                                 &connection_error);

                if (manager->priv->dbus_connection == NULL) {
                        g_propagate_error (error, connection_error);
                        is_connected = FALSE;
                        goto out;
                }
226 227 228 229 230 231

                connection = dbus_g_connection_get_connection (manager->priv->dbus_connection);
                dbus_connection_set_exit_on_disconnect (connection, FALSE);
                dbus_connection_add_filter (connection,
                                            gsm_consolekit_dbus_filter,
                                            manager, NULL);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        }

        if (manager->priv->bus_proxy == NULL) {
                manager->priv->bus_proxy =
                        dbus_g_proxy_new_for_name_owner (manager->priv->dbus_connection,
                                                         DBUS_SERVICE_DBUS,
                                                         DBUS_PATH_DBUS,
                                                         DBUS_INTERFACE_DBUS,
                                                         &connection_error);

                if (manager->priv->bus_proxy == NULL) {
                        g_propagate_error (error, connection_error);
                        is_connected = FALSE;
                        goto out;
                }

                dbus_g_proxy_add_signal (manager->priv->bus_proxy,
                                         "NameOwnerChanged",
                                         G_TYPE_STRING,
                                         G_TYPE_STRING,
                                         G_TYPE_STRING,
                                         G_TYPE_INVALID);

                dbus_g_proxy_connect_signal (manager->priv->bus_proxy,
                                             "NameOwnerChanged",
                                             G_CALLBACK (gsm_consolekit_on_name_owner_changed),
                                             manager, NULL);
        }

        if (manager->priv->ck_proxy == NULL) {
                manager->priv->ck_proxy =
                        dbus_g_proxy_new_for_name_owner (manager->priv->dbus_connection,
264 265 266
                                                         CK_NAME,
                                                         CK_MANAGER_PATH,
                                                         CK_MANAGER_INTERFACE,
267 268 269 270 271 272 273
                                                         &connection_error);

                if (manager->priv->ck_proxy == NULL) {
                        g_propagate_error (error, connection_error);
                        is_connected = FALSE;
                        goto out;
                }
Ray Strode's avatar
Ray Strode committed
274
        }
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

        pid = getpid ();
        ret = dbus_g_proxy_call (manager->priv->ck_proxy, "GetSessionForUnixProcess", &connection_error,
                                 G_TYPE_UINT, pid,
                                 G_TYPE_INVALID,
                                 DBUS_TYPE_G_OBJECT_PATH, &manager->priv->session_id,
                                 G_TYPE_INVALID);
        if (!ret) {
                g_propagate_error (error, connection_error);
                goto out;
        }

        if (manager->priv->session_proxy == NULL) {
                manager->priv->session_proxy =
                        dbus_g_proxy_new_for_name_owner (manager->priv->dbus_connection,
                                                         CK_NAME,
                                                         manager->priv->session_id,
                                                         CK_SESSION_INTERFACE,
                                                         &connection_error);

                if (manager->priv->session_proxy == NULL) {
                        g_propagate_error (error, connection_error);
                        is_connected = FALSE;
                        goto out;
                }

                dbus_g_proxy_begin_call (manager->priv->session_proxy,
302 303 304 305
                                         "IsActive",
                                         is_active_cb, g_object_ref (manager),
                                         (GDestroyNotify)g_object_unref,
                                         G_TYPE_INVALID);
306 307
                dbus_g_proxy_add_signal (manager->priv->session_proxy, "ActiveChanged", G_TYPE_BOOLEAN, G_TYPE_INVALID);
                dbus_g_proxy_connect_signal (manager->priv->session_proxy, "ActiveChanged",
308
                                             G_CALLBACK (on_active_changed), manager, NULL);
309 310
        }

311
#ifdef HAVE_OLD_UPOWER
312 313
        g_clear_object (&manager->priv->up_client);
        manager->priv->up_client = up_client_new ();
314
#endif
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329
        is_connected = TRUE;

 out:
        if (!is_connected) {
                if (manager->priv->dbus_connection == NULL) {
                        if (manager->priv->bus_proxy != NULL) {
                                g_object_unref (manager->priv->bus_proxy);
                                manager->priv->bus_proxy = NULL;
                        }

                        if (manager->priv->ck_proxy != NULL) {
                                g_object_unref (manager->priv->ck_proxy);
                                manager->priv->ck_proxy = NULL;
                        }
330 331 332

                        g_clear_object (&manager->priv->session_proxy);
                        g_clear_object (&manager->priv->session_id);
333 334 335 336 337
                } else if (manager->priv->bus_proxy == NULL) {
                        if (manager->priv->ck_proxy != NULL) {
                                g_object_unref (manager->priv->ck_proxy);
                                manager->priv->ck_proxy = NULL;
                        }
338 339 340

                        g_clear_object (&manager->priv->session_proxy);
                        g_clear_object (&manager->priv->session_id);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
                }
        }

        return is_connected;
}

static void
gsm_consolekit_on_name_owner_changed (DBusGProxy    *bus_proxy,
                                      const char    *name,
                                      const char    *prev_owner,
                                      const char    *new_owner,
                                      GsmConsolekit *manager)
{
        if (name != NULL && strcmp (name, "org.freedesktop.ConsoleKit") != 0) {
                return;
        }

358
        g_clear_object (&manager->priv->ck_proxy);
359
        g_clear_object (&manager->priv->session_proxy);
360 361

        gsm_consolekit_ensure_ck_connection (manager, NULL);
362

363 364 365 366 367 368 369 370 371 372 373 374
}

static void
gsm_consolekit_init (GsmConsolekit *manager)
{
        GError *error;

        manager->priv = GSM_CONSOLEKIT_GET_PRIVATE (manager);

        error = NULL;

        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
375
                g_warning ("Could not connect to ConsoleKit: %s",
376 377 378 379 380
                           error->message);
                g_error_free (error);
        }
}

381 382 383
static void
gsm_consolekit_free_dbus (GsmConsolekit *manager)
{
384 385
        g_clear_object (&manager->priv->bus_proxy);
        g_clear_object (&manager->priv->ck_proxy);
386 387
        g_clear_object (&manager->priv->session_proxy);
        g_clear_object (&manager->priv->session_id);
388
#ifdef HAVE_OLD_UPOWER
389
        g_clear_object (&manager->priv->up_client);
390
#endif
391 392 393 394 395 396 397 398 399 400 401 402 403

        if (manager->priv->dbus_connection != NULL) {
                DBusConnection *connection;
                connection = dbus_g_connection_get_connection (manager->priv->dbus_connection);
                dbus_connection_remove_filter (connection,
                                               gsm_consolekit_dbus_filter,
                                               manager);

                dbus_g_connection_unref (manager->priv->dbus_connection);
                manager->priv->dbus_connection = NULL;
        }
}

404 405 406 407 408 409 410 411 412 413
static void
gsm_consolekit_finalize (GObject *object)
{
        GsmConsolekit *manager;
        GObjectClass  *parent_class;

        manager = GSM_CONSOLEKIT (object);

        parent_class = G_OBJECT_CLASS (gsm_consolekit_parent_class);

414
        gsm_consolekit_free_dbus (manager);
415 416 417 418 419 420 421 422

        if (parent_class->finalize != NULL) {
                parent_class->finalize (object);
        }
}

static void
emit_restart_complete (GsmConsolekit *manager,
423
                       GError        *error)
424 425 426 427 428
{
        GError *call_error;

        call_error = NULL;

429
        if (error != NULL) {
430 431
                call_error = g_error_new_literal (GSM_SYSTEM_ERROR,
                                                  GSM_SYSTEM_ERROR_RESTARTING,
432
                                                  error->message);
433 434
        }

435 436
        g_signal_emit_by_name (G_OBJECT (manager),
                               "request_completed", call_error);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

        if (call_error != NULL) {
                g_error_free (call_error);
        }
}

static void
emit_stop_complete (GsmConsolekit *manager,
                    GError        *error)
{
        GError *call_error;

        call_error = NULL;

        if (error != NULL) {
452 453
                call_error = g_error_new_literal (GSM_SYSTEM_ERROR,
                                                  GSM_SYSTEM_ERROR_STOPPING,
454 455 456
                                                  error->message);
        }

457 458
        g_signal_emit_by_name (G_OBJECT (manager),
                               "request_completed", call_error);
459 460 461 462 463 464

        if (call_error != NULL) {
                g_error_free (call_error);
        }
}

465 466
static void
gsm_consolekit_attempt_restart (GsmSystem *system)
467
{
468
        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
469 470 471 472 473 474 475 476
        gboolean res;
        GError  *error;

        error = NULL;

        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
                g_warning ("Could not connect to ConsoleKit: %s",
                           error->message);
477
                emit_restart_complete (manager, error);
478 479 480 481
                g_error_free (error);
                return;
        }

482 483 484
        res = dbus_g_proxy_call_with_timeout (manager->priv->ck_proxy,
                                              "Restart",
                                              INT_MAX,
485
                                              &error,
486 487
                                              G_TYPE_INVALID,
                                              G_TYPE_INVALID);
488 489

        if (!res) {
490 491
                g_warning ("Unable to restart system: %s", error->message);
                emit_restart_complete (manager, error);
492
                g_error_free (error);
493 494
        } else {
                emit_restart_complete (manager, NULL);
495 496 497
        }
}

498 499
static void
gsm_consolekit_attempt_stop (GsmSystem *system)
500
{
501
        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
502 503 504 505 506 507 508 509
        gboolean res;
        GError  *error;

        error = NULL;

        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
                g_warning ("Could not connect to ConsoleKit: %s",
                           error->message);
510
                emit_stop_complete (manager, error);
511 512 513 514
                g_error_free (error);
                return;
        }

515 516 517
        res = dbus_g_proxy_call_with_timeout (manager->priv->ck_proxy,
                                              "Stop",
                                              INT_MAX,
518
                                              &error,
519 520
                                              G_TYPE_INVALID,
                                              G_TYPE_INVALID);
521 522

        if (!res) {
523 524
                g_warning ("Unable to stop system: %s", error->message);
                emit_stop_complete (manager, error);
525
                g_error_free (error);
526 527
        } else {
                emit_stop_complete (manager, NULL);
528 529 530
        }
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
static gboolean
get_current_session_id (DBusConnection *connection,
                        char          **session_id)
{
        DBusError       local_error;
        DBusMessage    *message;
        DBusMessage    *reply;
        gboolean        ret;
        DBusMessageIter iter;
        const char     *value;

        ret = FALSE;
        reply = NULL;

        dbus_error_init (&local_error);
        message = dbus_message_new_method_call (CK_NAME,
                                                CK_MANAGER_PATH,
                                                CK_MANAGER_INTERFACE,
                                                "GetCurrentSession");
        if (message == NULL) {
                goto out;
        }

        dbus_error_init (&local_error);
        reply = dbus_connection_send_with_reply_and_block (connection,
                                                           message,
                                                           -1,
                                                           &local_error);
        if (reply == NULL) {
                if (dbus_error_is_set (&local_error)) {
                        g_warning ("Unable to determine session: %s", local_error.message);
                        dbus_error_free (&local_error);
                        goto out;
                }
        }

        dbus_message_iter_init (reply, &iter);
        dbus_message_iter_get_basic (&iter, &value);
        if (session_id != NULL) {
                *session_id = g_strdup (value);
        }

        ret = TRUE;
 out:
        if (message != NULL) {
                dbus_message_unref (message);
        }
        if (reply != NULL) {
                dbus_message_unref (reply);
        }

        return ret;
}

static gboolean
get_seat_id_for_session (DBusConnection *connection,
                         const char     *session_id,
                         char          **seat_id)
{
        DBusError       local_error;
        DBusMessage    *message;
        DBusMessage    *reply;
        gboolean        ret;
        DBusMessageIter iter;
        const char     *value;

        ret = FALSE;
        reply = NULL;

        dbus_error_init (&local_error);
        message = dbus_message_new_method_call (CK_NAME,
                                                session_id,
                                                CK_SESSION_INTERFACE,
                                                "GetSeatId");
        if (message == NULL) {
                goto out;
        }

        dbus_error_init (&local_error);
        reply = dbus_connection_send_with_reply_and_block (connection,
                                                           message,
                                                           -1,
                                                           &local_error);
        if (reply == NULL) {
                if (dbus_error_is_set (&local_error)) {
                        g_warning ("Unable to determine seat: %s", local_error.message);
                        dbus_error_free (&local_error);
                        goto out;
                }
        }

        dbus_message_iter_init (reply, &iter);
        dbus_message_iter_get_basic (&iter, &value);
        if (seat_id != NULL) {
                *seat_id = g_strdup (value);
        }

        ret = TRUE;
 out:
        if (message != NULL) {
                dbus_message_unref (message);
        }
        if (reply != NULL) {
                dbus_message_unref (reply);
        }

        return ret;
}

static char *
get_current_seat_id (DBusConnection *connection)
{
        gboolean res;
        char    *session_id;
        char    *seat_id;

        session_id = NULL;
        seat_id = NULL;

        res = get_current_session_id (connection, &session_id);
        if (res) {
                res = get_seat_id_for_session (connection, session_id, &seat_id);
        }
        g_free (session_id);

        return seat_id;
}

659 660 661
static void
gsm_consolekit_set_session_idle (GsmSystem *system,
                                 gboolean   is_idle)
662
{
663
        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        gboolean        res;
        GError         *error;
        char           *session_id;
        DBusMessage    *message;
        DBusMessage    *reply;
        DBusError       dbus_error;
        DBusMessageIter iter;

        error = NULL;

        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
                g_warning ("Could not connect to ConsoleKit: %s",
                           error->message);
                g_error_free (error);
                return;
        }

        session_id = NULL;
        res = get_current_session_id (dbus_g_connection_get_connection (manager->priv->dbus_connection),
                                      &session_id);
        if (!res) {
                goto out;
        }


        g_debug ("Updating ConsoleKit idle status: %d", is_idle);
        message = dbus_message_new_method_call (CK_NAME,
                                                session_id,
                                                CK_SESSION_INTERFACE,
                                                "SetIdleHint");
        if (message == NULL) {
                g_debug ("Couldn't allocate the D-Bus message");
                return;
        }

        dbus_message_iter_init_append (message, &iter);
        dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &is_idle);

        /* FIXME: use async? */
        dbus_error_init (&dbus_error);
        reply = dbus_connection_send_with_reply_and_block (dbus_g_connection_get_connection (manager->priv->dbus_connection),
                                                           message,
                                                           -1,
                                                           &dbus_error);
        dbus_message_unref (message);

        if (reply != NULL) {
                dbus_message_unref (reply);
        }

        if (dbus_error_is_set (&dbus_error)) {
                g_debug ("%s raised:\n %s\n\n", dbus_error.name, dbus_error.message);
                dbus_error_free (&dbus_error);
        }

out:
        g_free (session_id);
}

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
static gboolean
seat_can_activate_sessions (DBusConnection *connection,
                            const char     *seat_id)
{
        DBusError       local_error;
        DBusMessage    *message;
        DBusMessage    *reply;
        DBusMessageIter iter;
        gboolean        can_activate;

        can_activate = FALSE;
        reply = NULL;

        dbus_error_init (&local_error);
        message = dbus_message_new_method_call (CK_NAME,
                                                seat_id,
                                                CK_SEAT_INTERFACE,
                                                "CanActivateSessions");
        if (message == NULL) {
                goto out;
        }

        dbus_error_init (&local_error);
        reply = dbus_connection_send_with_reply_and_block (connection,
                                                           message,
                                                           -1,
                                                           &local_error);
        if (reply == NULL) {
                if (dbus_error_is_set (&local_error)) {
                        g_warning ("Unable to activate session: %s", local_error.message);
                        dbus_error_free (&local_error);
                        goto out;
                }
        }

        dbus_message_iter_init (reply, &iter);
        dbus_message_iter_get_basic (&iter, &can_activate);

 out:
        if (message != NULL) {
                dbus_message_unref (message);
        }
        if (reply != NULL) {
                dbus_message_unref (reply);
        }

        return can_activate;
}

772 773
static gboolean
gsm_consolekit_can_switch_user (GsmSystem *system)
774
{
775
        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
776
        GError  *error;
777 778 779
        char    *seat_id;
        gboolean ret;

780 781 782 783 784 785 786 787 788
        error = NULL;

        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
                g_warning ("Could not connect to ConsoleKit: %s",
                           error->message);
                g_error_free (error);
                return FALSE;
        }

789 790 791 792 793 794 795 796 797 798 799 800 801
        seat_id = get_current_seat_id (dbus_g_connection_get_connection (manager->priv->dbus_connection));
        if (seat_id == NULL || seat_id[0] == '\0') {
                g_debug ("seat id is not set; can't switch sessions");
                return FALSE;
        }

        ret = seat_can_activate_sessions (dbus_g_connection_get_connection (manager->priv->dbus_connection),
                                          seat_id);
        g_free (seat_id);

        return ret;
}

802 803
static gboolean
gsm_consolekit_can_restart (GsmSystem *system)
804
{
805
        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
806
        gboolean res;
Ray Strode's avatar
Ray Strode committed
807
        gboolean can_restart;
808
        GError  *error;
809 810

        error = NULL;
811 812

        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
813 814 815
                g_warning ("Could not connect to ConsoleKit: %s",
                           error->message);
                g_error_free (error);
816
                return FALSE;
817 818
        }

819 820
        res = dbus_g_proxy_call_with_timeout (manager->priv->ck_proxy,
                                              "CanRestart",
821
                                              INT_MAX,
822
                                              &error,
823
                                              G_TYPE_INVALID,
824
                                              G_TYPE_BOOLEAN, &can_restart,
825 826
                                              G_TYPE_INVALID);

827 828 829 830 831 832 833
        if (!res) {
                g_warning ("Could not query CanRestart from ConsoleKit: %s",
                           error->message);
                g_error_free (error);
                return FALSE;
        }

Ray Strode's avatar
Ray Strode committed
834
        return can_restart;
835 836
}

837 838
static gboolean
gsm_consolekit_can_stop (GsmSystem *system)
839
{
840
        GsmConsolekit *manager = GSM_CONSOLEKIT (system);
841
        gboolean res;
Ray Strode's avatar
Ray Strode committed
842
        gboolean can_stop;
843
        GError  *error;
844 845 846

        error = NULL;

847
        if (!gsm_consolekit_ensure_ck_connection (manager, &error)) {
848 849 850
                g_warning ("Could not connect to ConsoleKit: %s",
                           error->message);
                g_error_free (error);
851
                return FALSE;
852 853
        }

854 855 856
        res = dbus_g_proxy_call_with_timeout (manager->priv->ck_proxy,
                                              "CanStop",
                                              INT_MAX,
857
                                              &error,
858 859 860
                                              G_TYPE_INVALID,
                                              G_TYPE_BOOLEAN, &can_stop,
                                              G_TYPE_INVALID);
861

862 863 864 865 866 867
        if (!res) {
                g_warning ("Could not query CanStop from ConsoleKit: %s",
                           error->message);
                g_error_free (error);
                return FALSE;
        }
Ray Strode's avatar
Ray Strode committed
868
        return can_stop;
869 870
}

871
static gchar *
872 873
gsm_consolekit_get_current_session_type (GsmConsolekit *manager)
{
874
        GError *gerror;
Ray Strode's avatar
Ray Strode committed
875 876 877 878 879 880 881 882 883 884 885
        DBusConnection *connection;
        DBusError error;
        DBusMessage *message = NULL;
        DBusMessage *reply = NULL;
        gchar *session_id;
        gchar *ret;
        DBusMessageIter iter;
        const char *value;

        session_id = NULL;
        ret = NULL;
886 887 888 889 890 891
        gerror = NULL;

        if (!gsm_consolekit_ensure_ck_connection (manager, &gerror)) {
                g_warning ("Could not connect to ConsoleKit: %s",
                           gerror->message);
                g_error_free (gerror);
Ray Strode's avatar
Ray Strode committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
                goto out;
        }

        connection = dbus_g_connection_get_connection (manager->priv->dbus_connection);
        if (!get_current_session_id (connection, &session_id)) {
                goto out;
        }

        dbus_error_init (&error);
        message = dbus_message_new_method_call (CK_NAME,
                                                session_id,
                                                CK_SESSION_INTERFACE,
                                                "GetSessionType");
        if (message == NULL) {
                goto out;
        }

        reply = dbus_connection_send_with_reply_and_block (connection,
                                                           message,
                                                           -1,
                                                           &error);

        if (reply == NULL) {
                if (dbus_error_is_set (&error)) {
                        g_warning ("Unable to determine session type: %s", error.message);
                        dbus_error_free (&error);
                }
                goto out;
        }

        dbus_message_iter_init (reply, &iter);
        dbus_message_iter_get_basic (&iter, &value);
        ret = g_strdup (value);
925 926

out:
Ray Strode's avatar
Ray Strode committed
927 928 929 930 931 932 933 934 935
        if (message != NULL) {
                dbus_message_unref (message);
        }
        if (reply != NULL) {
                dbus_message_unref (reply);
        }
        g_free (session_id);

        return ret;
936 937
}

938 939
static gboolean
gsm_consolekit_is_login_session (GsmSystem *system)
940
{
941
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);
942 943 944
        char *session_type;
        gboolean ret;

945
        session_type = gsm_consolekit_get_current_session_type (consolekit);
946 947 948 949 950 951 952

        ret = (g_strcmp0 (session_type, GSM_CONSOLEKIT_SESSION_TYPE_LOGIN_WINDOW) == 0);

        g_free (session_type);

        return ret;
}
953

954 955 956
static gboolean
gsm_consolekit_can_suspend (GsmSystem *system)
{
957
#ifdef HAVE_OLD_UPOWER
958
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);
959
        return up_client_get_can_suspend (consolekit->priv->up_client);
960 961 962
#else
        return FALSE;
#endif
963 964 965 966 967
}

static gboolean
gsm_consolekit_can_hibernate (GsmSystem *system)
{
968
#ifdef HAVE_OLD_UPOWER
969
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);
970
        return up_client_get_can_hibernate (consolekit->priv->up_client);
971 972 973
#else
        return FALSE;
#endif
974 975 976 977 978
}

static void
gsm_consolekit_suspend (GsmSystem *system)
{
979
#ifdef HAVE_OLD_UPOWER
980 981 982 983 984 985 986 987 988
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);
        GError *error = NULL;
        gboolean ret;

        ret = up_client_suspend_sync (consolekit->priv->up_client, NULL, &error);
        if (!ret) {
                g_warning ("Unexpected suspend failure: %s", error->message);
                g_error_free (error);
        }
989
#endif
990 991 992 993 994
}

static void
gsm_consolekit_hibernate (GsmSystem *system)
{
995
#ifdef HAVE_OLD_UPOWER
996 997 998 999 1000 1001 1002 1003 1004
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);
        GError *error = NULL;
        gboolean ret;

        ret = up_client_hibernate_sync (consolekit->priv->up_client, NULL, &error);
        if (!ret) {
                g_warning ("Unexpected hibernate failure: %s", error->message);
                g_error_free (error);
        }
1005
#endif
1006 1007
}

Matthias Clasen's avatar
Matthias Clasen committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
static void
gsm_consolekit_add_inhibitor (GsmSystem        *system,
                              const gchar      *id,
                              GsmInhibitorFlag  flag)
{
}

static void
gsm_consolekit_remove_inhibitor (GsmSystem   *system,
                                 const gchar *id)
{
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
static void
gsm_consolekit_prepare_shutdown (GsmSystem *system,
                                 gboolean   restart)
{
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);

        consolekit->priv->restarting = restart;
        g_signal_emit_by_name (system, "shutdown-prepared", TRUE);
}

static void
gsm_consolekit_complete_shutdown (GsmSystem *system)
{
        GsmConsolekit *consolekit = GSM_CONSOLEKIT (system);

        if (consolekit->priv->restarting)
                gsm_consolekit_attempt_restart (system);
        else
                gsm_consolekit_attempt_stop (system);
}

1042 1043 1044 1045 1046 1047
static void
gsm_consolekit_system_init (GsmSystemInterface *iface)
{
        iface->can_switch_user = gsm_consolekit_can_switch_user;
        iface->can_stop = gsm_consolekit_can_stop;
        iface->can_restart = gsm_consolekit_can_restart;
1048 1049
        iface->can_suspend = gsm_consolekit_can_suspend;
        iface->can_hibernate = gsm_consolekit_can_hibernate;
1050 1051
        iface->attempt_stop = gsm_consolekit_attempt_stop;
        iface->attempt_restart = gsm_consolekit_attempt_restart;
1052 1053
        iface->suspend = gsm_consolekit_suspend;
        iface->hibernate = gsm_consolekit_hibernate;
1054 1055
        iface->set_session_idle = gsm_consolekit_set_session_idle;
        iface->is_login_session = gsm_consolekit_is_login_session;
Matthias Clasen's avatar
Matthias Clasen committed
1056 1057
        iface->add_inhibitor = gsm_consolekit_add_inhibitor;
        iface->remove_inhibitor = gsm_consolekit_remove_inhibitor;
1058 1059
        iface->prepare_shutdown = gsm_consolekit_prepare_shutdown;
        iface->complete_shutdown = gsm_consolekit_complete_shutdown;
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
}

GsmConsolekit *
gsm_consolekit_new (void)
{
        GsmConsolekit *manager;

        manager = g_object_new (GSM_TYPE_CONSOLEKIT, NULL);

        return manager;
}