gsm-manager.c 134 KB
Newer Older
1 2 3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 Novell, Inc.
4
 * Copyright (C) 2008 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * Copyright (C) 2008 William 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
40
#include <librsvg/rsvg.h>
41

42
#include <upower.h>
43

44 45 46 47 48
#include <gtk/gtk.h> /* for logout dialog */

#include "gsm-manager.h"
#include "gsm-manager-glue.h"

49
#include "gsm-store.h"
50
#include "gsm-inhibitor.h"
William Jon McCann's avatar
William Jon McCann committed
51
#include "gsm-presence.h"
52
#include "gsm-shell.h"
53

54
#include "gsm-xsmp-client.h"
55
#include "gsm-dbus-client.h"
56

57
#include "gsm-autostart-app.h"
58

59
#include "gsm-util.h"
60
#include "gdm.h"
61
#include "gsm-logout-dialog.h"
62
#include "gsm-fail-whale-dialog.h"
63
#include "gsm-inhibit-dialog.h"
64
#include "gsm-consolekit.h"
65
#include "gsm-session-save.h"
66 67 68

#define GSM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_MANAGER, GsmManagerPrivate))

69 70
#define GSM_MANAGER_DBUS_PATH "/org/gnome/SessionManager"
#define GSM_MANAGER_DBUS_NAME "org.gnome.SessionManager"
71 72 73

#define GSM_MANAGER_PHASE_TIMEOUT 10 /* seconds */

74 75 76
#define GDM_FLEXISERVER_COMMAND "gdmflexiserver"
#define GDM_FLEXISERVER_ARGS    "--startnew Standard"

77 78
#define SESSION_SCHEMA            "org.gnome.desktop.session"
#define KEY_IDLE_DELAY            "idle-delay"
79
#define KEY_SESSION_NAME          "session-name"
80

81 82 83
#define GSM_MANAGER_SCHEMA        "org.gnome.SessionManager"
#define KEY_AUTOSAVE              "auto-save-session"
#define KEY_LOGOUT_PROMPT         "logout-prompt"
Vincent Untz's avatar
Vincent Untz committed
84
#define KEY_SHOW_FALLBACK_WARNING "show-fallback-warning"
85

86
#define SCREENSAVER_SCHEMA        "org.gnome.desktop.screensaver"
87
#define KEY_SLEEP_LOCK            "lock-enabled"
88

89 90
static void app_registered (GsmApp     *app, GsmManager *manager);

91 92 93 94 95 96 97 98 99 100 101 102
typedef enum
{
        GSM_MANAGER_LOGOUT_NONE,
        GSM_MANAGER_LOGOUT_LOGOUT,
        GSM_MANAGER_LOGOUT_REBOOT,
        GSM_MANAGER_LOGOUT_REBOOT_INTERACT,
        GSM_MANAGER_LOGOUT_REBOOT_GDM,
        GSM_MANAGER_LOGOUT_SHUTDOWN,
        GSM_MANAGER_LOGOUT_SHUTDOWN_INTERACT,
        GSM_MANAGER_LOGOUT_SHUTDOWN_GDM
} GsmManagerLogoutType;

103 104 105
struct GsmManagerPrivate
{
        gboolean                failsafe;
106 107
        GsmStore               *clients;
        GsmStore               *inhibitors;
108
        GsmStore               *apps;
William Jon McCann's avatar
William Jon McCann committed
109
        GsmPresence            *presence;
110

111
        char                   *session_name;
112 113
        gboolean                is_fallback_session : 1;

114 115
        /* Current status */
        GsmManagerPhase         phase;
116
        guint                   phase_timeout_id;
117
        GSList                 *required_apps;
118
        GSList                 *pending_apps;
119
        GsmManagerLogoutMode    logout_mode;
120 121
        GSList                 *query_clients;
        guint                   query_timeout_id;
122 123 124
        /* This is used for GSM_MANAGER_PHASE_END_SESSION only at the moment,
         * since it uses a sublist of all running client that replied in a
         * specific way */
125
        GSList                 *next_query_clients;
126 127
        /* This is the action that will be done just before we exit */
        GsmManagerLogoutType    logout_type;
128

129
        GtkWidget              *inhibit_dialog;
130

131 132 133 134
        /* List of clients which were disconnected due to disabled condition
         * and shouldn't be automatically restarted */
        GSList                 *condition_clients;

135 136 137
        GSettings              *settings;
        GSettings              *session_settings;
        GSettings              *screensaver_settings;
138

139 140
        DBusGProxy             *bus_proxy;
        DBusGConnection        *connection;
141 142

        /* Interface with other parts of the system */
143
        UpClient               *up_client;
144 145 146 147 148

        GsmShell               *shell;
        guint                   shell_end_session_dialog_canceled_id;
        guint                   shell_end_session_dialog_open_failed_id;
        guint                   shell_end_session_dialog_confirmed_id;
149 150 151 152 153
};

enum {
        PROP_0,
        PROP_CLIENT_STORE,
154
        PROP_SESSION_NAME,
155
        PROP_FALLBACK,
156
        PROP_FAILSAFE
157 158 159 160
};

enum {
        PHASE_CHANGED,
161 162 163 164
        CLIENT_ADDED,
        CLIENT_REMOVED,
        INHIBITOR_ADDED,
        INHIBITOR_REMOVED,
165 166 167 168 169
        SESSION_RUNNING,
        SESSION_OVER,
        LAST_SIGNAL
};

170
static guint signals [LAST_SIGNAL] = { 0 };
171 172 173 174 175

static void     gsm_manager_class_init  (GsmManagerClass *klass);
static void     gsm_manager_init        (GsmManager      *manager);
static void     gsm_manager_finalize    (GObject         *object);

176
static gboolean auto_save_is_enabled (GsmManager *manager);
177
static void     maybe_save_session   (GsmManager *manager);
178

179 180 181 182 183 184
static void     _handle_client_end_session_response (GsmManager *manager,
                                                     GsmClient  *client,
                                                     gboolean    is_ok,
                                                     gboolean    do_last,
                                                     gboolean    cancel,
                                                     const char *reason);
185 186
static void     show_shell_end_session_dialog (GsmManager                   *manager,
                                               GsmShellEndSessionDialogType  type);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static gpointer manager_object = NULL;

G_DEFINE_TYPE (GsmManager, gsm_manager, G_TYPE_OBJECT)

GQuark
gsm_manager_error_quark (void)
{
        static GQuark ret = 0;
        if (ret == 0) {
                ret = g_quark_from_static_string ("gsm_manager_error");
        }

        return ret;
}

202 203 204 205 206 207 208 209 210 211 212 213 214
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }

GType
gsm_manager_error_get_type (void)
{
        static GType etype = 0;

        if (etype == 0) {
                static const GEnumValue values[] = {
                        ENUM_ENTRY (GSM_MANAGER_ERROR_GENERAL, "GeneralError"),
                        ENUM_ENTRY (GSM_MANAGER_ERROR_NOT_IN_INITIALIZATION, "NotInInitialization"),
                        ENUM_ENTRY (GSM_MANAGER_ERROR_NOT_IN_RUNNING, "NotInRunning"),
                        ENUM_ENTRY (GSM_MANAGER_ERROR_ALREADY_REGISTERED, "AlreadyRegistered"),
215
                        ENUM_ENTRY (GSM_MANAGER_ERROR_NOT_REGISTERED, "NotRegistered"),
216
                        ENUM_ENTRY (GSM_MANAGER_ERROR_INVALID_OPTION, "InvalidOption"),
217 218 219 220 221 222 223 224 225 226 227
                        { 0, 0, 0 }
                };

                g_assert (GSM_MANAGER_NUM_ERRORS == G_N_ELEMENTS (values) - 1);

                etype = g_enum_register_static ("GsmManagerError", values);
        }

        return etype;
}

228 229 230 231 232 233 234 235 236 237 238
static gboolean
start_app_or_warn (GsmManager *manager,
                   GsmApp     *app)
{
        gboolean res;
        GError *error = NULL;

        g_debug ("GsmManager: starting app '%s'", gsm_app_peek_id (app));

        res = gsm_app_start (app, &error);
        if (error != NULL) {
239
                g_warning ("Failed to start app: %s", error->message);
240 241 242 243 244
                g_clear_error (&error);
        }
        return res;
}

245 246 247 248 249 250 251 252 253 254 255 256 257
static gboolean
is_app_required (GsmManager *manager,
                 GsmApp     *app)
{
        return g_slist_find (manager->priv->required_apps, app) != NULL;
}

static void
on_required_app_failure (GsmManager  *manager,
                         GsmApp      *app,
                         const char  *msg)
{
        char *full_msg;
258 259 260
        /* Translators: This will look like: Component 'gnome-shell.desktop': Killed by signal 9.
         * It is admittedly mostly technical goop. */
        full_msg = g_strdup_printf (_("Component '%s': %s"),
261 262
                                    gsm_app_peek_app_id (app),
                                    msg);
263
        gsm_fail_whale_dialog_we_failed (GSM_FAIL_WHALE_DIALOG_FAIL_TYPE_RECOVERABLE,
264 265 266 267 268
                                         full_msg);
        g_free (full_msg);
}


269 270 271 272 273
static gboolean
_debug_client (const char *id,
               GsmClient  *client,
               GsmManager *manager)
{
274
        g_debug ("GsmManager: Client %s", gsm_client_peek_id (client));
275 276 277 278 279 280
        return FALSE;
}

static void
debug_clients (GsmManager *manager)
{
281 282 283
        gsm_store_foreach (manager->priv->clients,
                           (GsmStoreFunc)_debug_client,
                           manager);
284 285
}

286 287 288 289 290 291
static gboolean
_debug_inhibitor (const char    *id,
                  GsmInhibitor  *inhibitor,
                  GsmManager    *manager)
{
        g_debug ("GsmManager: Inhibitor app:%s client:%s bus-name:%s reason:%s",
292 293 294 295
                 gsm_inhibitor_peek_app_id (inhibitor),
                 gsm_inhibitor_peek_client_id (inhibitor),
                 gsm_inhibitor_peek_bus_name (inhibitor),
                 gsm_inhibitor_peek_reason (inhibitor));
296 297 298 299 300 301
        return FALSE;
}

static void
debug_inhibitors (GsmManager *manager)
{
302 303 304 305 306 307 308 309 310 311 312 313
        gsm_store_foreach (manager->priv->inhibitors,
                           (GsmStoreFunc)_debug_inhibitor,
                           manager);
}

static gboolean
_find_by_cookie (const char   *id,
                 GsmInhibitor *inhibitor,
                 guint        *cookie_ap)
{
        guint cookie_b;

314
        cookie_b = gsm_inhibitor_peek_cookie (inhibitor);
315 316

        return (*cookie_ap == cookie_b);
317 318
}

319
static gboolean
320 321 322
_find_by_startup_id (const char *id,
                     GsmClient  *client,
                     const char *startup_id_a)
323
{
324
        const char *startup_id_b;
325

326
        startup_id_b = gsm_client_peek_startup_id (client);
327
        if (IS_STRING_EMPTY (startup_id_b)) {
328 329 330
                return FALSE;
        }

331
        return (strcmp (startup_id_a, startup_id_b) == 0);
332 333
}

334 335 336 337 338
static void
app_condition_changed (GsmApp     *app,
                       gboolean    condition,
                       GsmManager *manager)
{
339
        GsmClient *client;
340

341 342 343 344
        g_debug ("GsmManager: app:%s condition changed condition:%d",
                 gsm_app_peek_id (app),
                 condition);

345 346
        client = (GsmClient *)gsm_store_find (manager->priv->clients,
                                              (GsmStoreFunc)_find_by_startup_id,
347
                                              (char *)gsm_app_peek_startup_id (app));
348 349

        if (condition) {
350
                if (!gsm_app_is_running (app) && client == NULL) {
351
                        start_app_or_warn (manager, app);
352 353
                } else {
                        g_debug ("GsmManager: not starting - app still running '%s'", gsm_app_peek_id (app));
354 355
                }
        } else {
356 357 358
                GError  *error;
                gboolean res;

359 360 361 362
                if (client != NULL) {
                        /* Kill client in case condition if false and make sure it won't
                         * be automatically restarted by adding the client to
                         * condition_clients */
363 364 365
                        manager->priv->condition_clients =
                                g_slist_prepend (manager->priv->condition_clients, client);

366
                        g_debug ("GsmManager: stopping client %s for app", gsm_client_peek_id (client));
367

368 369
                        error = NULL;
                        res = gsm_client_stop (client, &error);
370 371 372 373 374 375 376 377 378 379 380
                        if (error != NULL) {
                                g_warning ("Not able to stop app client from its condition: %s",
                                           error->message);
                                g_error_free (error);
                        }
                } else {
                        g_debug ("GsmManager: stopping app %s", gsm_app_peek_id (app));

                        /* If we don't have a client then we should try to kill the app */
                        error = NULL;
                        res = gsm_app_stop (app, &error);
381 382 383 384 385
                        if (error != NULL) {
                                g_warning ("Not able to stop app from its condition: %s",
                                           error->message);
                                g_error_free (error);
                        }
386
                }
387 388 389
        }
}

390 391 392 393 394 395 396 397
static const char *
phase_num_to_name (guint phase)
{
        const char *name;

        switch (phase) {
        case GSM_MANAGER_PHASE_STARTUP:
                name = "STARTUP";
William Jon McCann's avatar
William Jon McCann committed
398
                break;
399 400
        case GSM_MANAGER_PHASE_INITIALIZATION:
                name = "INITIALIZATION";
William Jon McCann's avatar
William Jon McCann committed
401
                break;
402 403
        case GSM_MANAGER_PHASE_WINDOW_MANAGER:
                name = "WINDOW_MANAGER";
William Jon McCann's avatar
William Jon McCann committed
404
                break;
405 406
        case GSM_MANAGER_PHASE_PANEL:
                name = "PANEL";
William Jon McCann's avatar
William Jon McCann committed
407
                break;
408 409
        case GSM_MANAGER_PHASE_DESKTOP:
                name = "DESKTOP";
William Jon McCann's avatar
William Jon McCann committed
410
                break;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
        case GSM_MANAGER_PHASE_APPLICATION:
                name = "APPLICATION";
                break;
        case GSM_MANAGER_PHASE_RUNNING:
                name = "RUNNING";
                break;
        case GSM_MANAGER_PHASE_QUERY_END_SESSION:
                name = "QUERY_END_SESSION";
                break;
        case GSM_MANAGER_PHASE_END_SESSION:
                name = "END_SESSION";
                break;
        case GSM_MANAGER_PHASE_EXIT:
                name = "EXIT";
                break;
        default:
                g_assert_not_reached ();
                break;
        }

        return name;
}

434 435
static void start_phase (GsmManager *manager);

436 437 438 439 440 441 442 443 444 445 446 447 448 449 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 489 490 491 492 493 494 495 496 497 498 499
static void
quit_request_completed (GsmConsolekit *consolekit,
                        GError        *error,
                        gpointer       user_data)
{
        GdmLogoutAction fallback_action = GPOINTER_TO_INT (user_data);

        if (error != NULL) {
                gdm_set_logout_action (fallback_action);
        }

        g_object_unref (consolekit);

        gtk_main_quit ();
}

static void
gsm_manager_quit (GsmManager *manager)
{
        GsmConsolekit *consolekit;

        /* See the comment in request_reboot() for some more details about how
         * this works. */

        switch (manager->priv->logout_type) {
        case GSM_MANAGER_LOGOUT_LOGOUT:
                gtk_main_quit ();
                break;
        case GSM_MANAGER_LOGOUT_REBOOT:
        case GSM_MANAGER_LOGOUT_REBOOT_INTERACT:
                gdm_set_logout_action (GDM_LOGOUT_ACTION_NONE);

                consolekit = gsm_get_consolekit ();
                g_signal_connect (consolekit,
                                  "request-completed",
                                  G_CALLBACK (quit_request_completed),
                                  GINT_TO_POINTER (GDM_LOGOUT_ACTION_REBOOT));
                gsm_consolekit_attempt_restart (consolekit);
                break;
        case GSM_MANAGER_LOGOUT_REBOOT_GDM:
                gdm_set_logout_action (GDM_LOGOUT_ACTION_REBOOT);
                gtk_main_quit ();
                break;
        case GSM_MANAGER_LOGOUT_SHUTDOWN:
        case GSM_MANAGER_LOGOUT_SHUTDOWN_INTERACT:
                gdm_set_logout_action (GDM_LOGOUT_ACTION_NONE);

                consolekit = gsm_get_consolekit ();
                g_signal_connect (consolekit,
                                  "request-completed",
                                  G_CALLBACK (quit_request_completed),
                                  GINT_TO_POINTER (GDM_LOGOUT_ACTION_SHUTDOWN));
                gsm_consolekit_attempt_stop (consolekit);
                break;
        case GSM_MANAGER_LOGOUT_SHUTDOWN_GDM:
                gdm_set_logout_action (GDM_LOGOUT_ACTION_SHUTDOWN);
                gtk_main_quit ();
                break;
        default:
                g_assert_not_reached ();
                break;
        }
}

500 501 502
static void
end_phase (GsmManager *manager)
{
503 504 505
        g_debug ("GsmManager: ending phase %s\n",
                 phase_num_to_name (manager->priv->phase));

506 507 508
        g_slist_free (manager->priv->pending_apps);
        manager->priv->pending_apps = NULL;

509 510 511
        g_slist_free (manager->priv->query_clients);
        manager->priv->query_clients = NULL;

512 513
        g_slist_free (manager->priv->next_query_clients);
        manager->priv->next_query_clients = NULL;
514

515 516 517 518
        if (manager->priv->query_timeout_id > 0) {
                g_source_remove (manager->priv->query_timeout_id);
                manager->priv->query_timeout_id = 0;
        }
519 520 521 522
        if (manager->priv->phase_timeout_id > 0) {
                g_source_remove (manager->priv->phase_timeout_id);
                manager->priv->phase_timeout_id = 0;
        }
523

524 525 526 527 528 529 530 531 532
        switch (manager->priv->phase) {
        case GSM_MANAGER_PHASE_STARTUP:
        case GSM_MANAGER_PHASE_INITIALIZATION:
        case GSM_MANAGER_PHASE_WINDOW_MANAGER:
        case GSM_MANAGER_PHASE_PANEL:
        case GSM_MANAGER_PHASE_DESKTOP:
        case GSM_MANAGER_PHASE_APPLICATION:
        case GSM_MANAGER_PHASE_RUNNING:
        case GSM_MANAGER_PHASE_QUERY_END_SESSION:
533 534 535
                manager->priv->phase++;
                start_phase (manager);
                break;
536
        case GSM_MANAGER_PHASE_END_SESSION:
537 538 539
                if (auto_save_is_enabled (manager)) {
                        maybe_save_session (manager);
                }
540
                manager->priv->phase++;
541
                start_phase (manager);
542 543
                break;
        case GSM_MANAGER_PHASE_EXIT:
544
                gsm_manager_quit (manager);
545 546 547 548
                break;
        default:
                g_assert_not_reached ();
                break;
549 550 551
        }
}

552 553 554 555 556 557 558 559 560
static void
app_died (GsmApp     *app,
          GsmManager *manager)
{
        manager->priv->pending_apps = g_slist_remove (manager->priv->pending_apps, app);
        g_signal_handlers_disconnect_by_func (app, app_registered, manager);

        g_warning ("Application '%s' killed by signal", gsm_app_peek_app_id (app));
        if (is_app_required (manager, app))
561
                on_required_app_failure (manager, app, _("Killed by signal"));
562 563 564 565 566 567 568
        /* For now, we don't do anything with crashes from non-required apps;
         * practically speaking they will be caught by ABRT/apport type
         * infrastructure, and it'd be better to pick up the crash from
         * there and do something un-intrusive about it generically.
         */
}

569 570 571 572
static void
app_registered (GsmApp     *app,
                GsmManager *manager)
{
573 574
        g_debug ("App %s registered", gsm_app_peek_app_id (app));

575 576 577 578
        manager->priv->pending_apps = g_slist_remove (manager->priv->pending_apps, app);
        g_signal_handlers_disconnect_by_func (app, app_registered, manager);

        if (manager->priv->pending_apps == NULL) {
579 580 581
                if (manager->priv->phase_timeout_id > 0) {
                        g_source_remove (manager->priv->phase_timeout_id);
                        manager->priv->phase_timeout_id = 0;
582 583 584 585 586 587
                }

                end_phase (manager);
        }
}

588 589 590 591 592 593 594 595 596
static gboolean
_client_failed_to_stop (const char *id,
                        GsmClient  *client,
                        gpointer    user_data)
{
        g_debug ("GsmManager: client failed to stop: %s, %s", gsm_client_peek_id (client), gsm_client_peek_app_id (client));
        return FALSE;
}

597
static gboolean
598
on_phase_timeout (GsmManager *manager)
599 600 601
{
        GSList *a;

602
        manager->priv->phase_timeout_id = 0;
603

604 605 606 607 608 609 610 611
        switch (manager->priv->phase) {
        case GSM_MANAGER_PHASE_STARTUP:
        case GSM_MANAGER_PHASE_INITIALIZATION:
        case GSM_MANAGER_PHASE_WINDOW_MANAGER:
        case GSM_MANAGER_PHASE_PANEL:
        case GSM_MANAGER_PHASE_DESKTOP:
        case GSM_MANAGER_PHASE_APPLICATION:
                for (a = manager->priv->pending_apps; a; a = a->next) {
612
                        GsmApp *app = a->data;
613
                        g_warning ("Application '%s' failed to register before timeout",
614 615 616
                                   gsm_app_peek_app_id (app));
                        g_signal_handlers_disconnect_by_func (app, app_registered, manager);
                        if (is_app_required (manager, app))
617 618
                        /* Translators: "Timed out" refers to a non-responsive application not answering.
                        In case the string is problematic it might be translated to the noun "Timeout". */
619
                                on_required_app_failure (manager, app, _("Timed out"));
620 621 622 623 624 625 626 627
                }
                break;
        case GSM_MANAGER_PHASE_RUNNING:
                break;
        case GSM_MANAGER_PHASE_QUERY_END_SESSION:
        case GSM_MANAGER_PHASE_END_SESSION:
                break;
        case GSM_MANAGER_PHASE_EXIT:
628 629 630
                gsm_store_foreach (manager->priv->clients,
                                   (GsmStoreFunc)_client_failed_to_stop,
                                   NULL);
631 632 633 634
                break;
        default:
                g_assert_not_reached ();
                break;
635 636 637
        }

        end_phase (manager);
638

639 640 641
        return FALSE;
}

642
static gboolean
643 644 645
_start_app (const char *id,
            GsmApp     *app,
            GsmManager *manager)
646
{
647
        gboolean res;
648

649
        if (gsm_app_peek_phase (app) != manager->priv->phase) {
650
                goto out;
651
        }
652

653 654 655 656 657 658
        /* Keep track of app autostart condition in order to react
         * accordingly in the future. */
        g_signal_connect (app,
                          "condition-changed",
                          G_CALLBACK (app_condition_changed),
                          manager);
659

660 661
        if (gsm_app_peek_is_disabled (app)
            || gsm_app_peek_is_conditionally_disabled (app)) {
662
                g_debug ("GsmManager: Skipping disabled app: %s", id);
663
                goto out;
664
        }
665

666
        if (!start_app_or_warn (manager, app))
667
                goto out;
668

669
        if (manager->priv->phase < GSM_MANAGER_PHASE_APPLICATION) {
670 671 672 673 674 675 676 677 678
                /* Historical note - apparently,
                 * e.g. gnome-settings-daemon used to "daemonize", and
                 * so gnome-session assumes process exit means "ok
                 * we're done".  Of course this is broken, we don't
                 * even distinguish between exit code 0 versus not-0,
                 * nor do we have any metadata which tells us a
                 * process is going to "daemonize" or not (and
                 * basically nothing should be anyways).
                 */
679 680 681 682
                g_signal_connect (app,
                                  "exited",
                                  G_CALLBACK (app_registered),
                                  manager);
683 684 685 686
                g_signal_connect (app,
                                  "registered",
                                  G_CALLBACK (app_registered),
                                  manager);
687 688 689 690
                g_signal_connect (app,
                                  "died",
                                  G_CALLBACK (app_died),
                                  manager);
691 692
                manager->priv->pending_apps = g_slist_prepend (manager->priv->pending_apps, app);
        }
693 694
 out:
        return FALSE;
695
}
696

697
static void
698
do_phase_startup (GsmManager *manager)
699
{
700 701 702
        gsm_store_foreach (manager->priv->apps,
                           (GsmStoreFunc)_start_app,
                           manager);
703 704 705

        if (manager->priv->pending_apps != NULL) {
                if (manager->priv->phase < GSM_MANAGER_PHASE_APPLICATION) {
706
                        manager->priv->phase_timeout_id = g_timeout_add_seconds (GSM_MANAGER_PHASE_TIMEOUT,
707
                                                                                 (GSourceFunc)on_phase_timeout,
708
                                                                                 manager);
709 710 711 712 713 714
                }
        } else {
                end_phase (manager);
        }
}

715 716 717 718
typedef struct {
        GsmManager *manager;
        guint       flags;
} ClientEndSessionData;
719 720


721
static gboolean
722
_client_end_session (GsmClient            *client,
723
                     ClientEndSessionData *data)
724
{
725 726
        gboolean ret;
        GError  *error;
727

728 729 730 731 732 733 734 735 736 737 738
        error = NULL;
        ret = gsm_client_end_session (client, data->flags, &error);
        if (! ret) {
                g_warning ("Unable to query client: %s", error->message);
                g_error_free (error);
                /* FIXME: what should we do if we can't communicate with client? */
        } else {
                g_debug ("GsmManager: adding client to end-session clients: %s", gsm_client_peek_id (client));
                data->manager->priv->query_clients = g_slist_prepend (data->manager->priv->query_clients,
                                                                      client);
        }
739 740

        return FALSE;
741 742
}

743 744 745 746 747 748 749 750
static gboolean
_client_end_session_helper (const char           *id,
                            GsmClient            *client,
                            ClientEndSessionData *data)
{
        return _client_end_session (client, data);
}

751
static void
752
do_phase_end_session (GsmManager *manager)
753
{
754
        ClientEndSessionData data;
755

756 757
        data.manager = manager;
        data.flags = 0;
758

759
        if (manager->priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE) {
760
                data.flags |= GSM_CLIENT_END_SESSION_FLAG_FORCEFUL;
761
        }
762 763 764 765
        if (auto_save_is_enabled (manager)) {
                data.flags |= GSM_CLIENT_END_SESSION_FLAG_SAVE;
        }

766 767 768
        if (manager->priv->phase_timeout_id > 0) {
                g_source_remove (manager->priv->phase_timeout_id);
                manager->priv->phase_timeout_id = 0;
769 770
        }

771
        if (gsm_store_size (manager->priv->clients) > 0) {
772
                manager->priv->phase_timeout_id = g_timeout_add_seconds (GSM_MANAGER_PHASE_TIMEOUT,
773 774
                                                                         (GSourceFunc)on_phase_timeout,
                                                                         manager);
775

776
                gsm_store_foreach (manager->priv->clients,
777
                                   (GsmStoreFunc)_client_end_session_helper,
778
                                   &data);
779
        } else {
780
                end_phase (manager);
781 782 783 784
        }
}

static void
785
do_phase_end_session_part_2 (GsmManager *manager)
786 787 788 789 790 791
{
        ClientEndSessionData data;

        data.manager = manager;
        data.flags = 0;

792
        if (manager->priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE) {
793 794 795 796 797 798 799
                data.flags |= GSM_CLIENT_END_SESSION_FLAG_FORCEFUL;
        }
        if (auto_save_is_enabled (manager)) {
                data.flags |= GSM_CLIENT_END_SESSION_FLAG_SAVE;
        }
        data.flags |= GSM_CLIENT_END_SESSION_FLAG_LAST;

800 801
        /* keep the timeout that was started at the beginning of the
         * GSM_MANAGER_PHASE_END_SESSION phase */
802 803 804

        if (g_slist_length (manager->priv->next_query_clients) > 0) {
                g_slist_foreach (manager->priv->next_query_clients,
805
                                 (GFunc)_client_end_session,
806
                                 &data);
807 808 809

                g_slist_free (manager->priv->next_query_clients);
                manager->priv->next_query_clients = NULL;
810 811 812 813 814
        } else {
                end_phase (manager);
        }
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static gboolean
_client_stop (const char *id,
              GsmClient  *client,
              gpointer    user_data)
{
        gboolean ret;
        GError  *error;

        error = NULL;
        ret = gsm_client_stop (client, &error);
        if (! ret) {
                g_warning ("Unable to stop client: %s", error->message);
                g_error_free (error);
                /* FIXME: what should we do if we can't communicate with client? */
        } else {
                g_debug ("GsmManager: stopped client: %s", gsm_client_peek_id (client));
        }

        return FALSE;
}

static void
do_phase_exit (GsmManager *manager)
{
        if (gsm_store_size (manager->priv->clients) > 0) {
840
                manager->priv->phase_timeout_id = g_timeout_add_seconds (GSM_MANAGER_PHASE_TIMEOUT,
841 842 843 844 845 846 847 848 849 850 851
                                                                         (GSourceFunc)on_phase_timeout,
                                                                         manager);

                gsm_store_foreach (manager->priv->clients,
                                   (GsmStoreFunc)_client_stop,
                                   NULL);
        } else {
                end_phase (manager);
        }
}

852 853 854 855 856
static gboolean
_client_query_end_session (const char           *id,
                           GsmClient            *client,
                           ClientEndSessionData *data)
{
857 858 859 860 861 862 863 864 865 866 867 868 869 870
        gboolean ret;
        GError  *error;

        error = NULL;
        ret = gsm_client_query_end_session (client, data->flags, &error);
        if (! ret) {
                g_warning ("Unable to query client: %s", error->message);
                g_error_free (error);
                /* FIXME: what should we do if we can't communicate with client? */
        } else {
                g_debug ("GsmManager: adding client to query clients: %s", gsm_client_peek_id (client));
                data->manager->priv->query_clients = g_slist_prepend (data->manager->priv->query_clients,
                                                                      client);
        }
871

872 873
        return FALSE;
}
874

875 876 877 878 879
static gboolean
inhibitor_has_flag (gpointer      key,
                    GsmInhibitor *inhibitor,
                    gpointer      data)
{
880 881 882 883
        guint flag;
        guint flags;

        flag = GPOINTER_TO_UINT (data);
884

885
        flags = gsm_inhibitor_peek_flags (inhibitor);
886

887
        return (flags & flag);
888 889
}

890
static gboolean
891
gsm_manager_is_logout_inhibited (GsmManager *manager)
892
{
893
        GsmInhibitor *inhibitor;
894

895
        if (manager->priv->inhibitors == NULL) {
896 897 898
                return FALSE;
        }

899 900
        inhibitor = (GsmInhibitor *)gsm_store_find (manager->priv->inhibitors,
                                                    (GsmStoreFunc)inhibitor_has_flag,
901
                                                    GUINT_TO_POINTER (GSM_INHIBITOR_FLAG_LOGOUT));
902
        if (inhibitor == NULL) {
903 904
                return FALSE;
        }
905
        return TRUE;
906 907
}

William Jon McCann's avatar
William Jon McCann committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
static gboolean
gsm_manager_is_idle_inhibited (GsmManager *manager)
{
        GsmInhibitor *inhibitor;

        if (manager->priv->inhibitors == NULL) {
                return FALSE;
        }

        inhibitor = (GsmInhibitor *)gsm_store_find (manager->priv->inhibitors,
                                                    (GsmStoreFunc)inhibitor_has_flag,
                                                    GUINT_TO_POINTER (GSM_INHIBITOR_FLAG_IDLE));
        if (inhibitor == NULL) {
                return FALSE;
        }
        return TRUE;
}

926 927 928 929
static gboolean
_client_cancel_end_session (const char *id,
                            GsmClient  *client,
                            GsmManager *manager)
930
{
931 932 933 934 935 936 937 938 939
        gboolean res;
        GError  *error;

        error = NULL;
        res = gsm_client_cancel_end_session (client, &error);
        if (! res) {
                g_warning ("Unable to cancel end session: %s", error->message);
                g_error_free (error);
        }
940

941
        return FALSE;
942 943
}

944
static gboolean
945 946 947
inhibitor_is_jit (gpointer      key,
                  GsmInhibitor *inhibitor,
                  GsmManager   *manager)
948
{
949
        gboolean    matches;
950
        const char *id;
951

952
        id = gsm_inhibitor_peek_client_id (inhibitor);
953

954
        matches = (id != NULL && id[0] != '\0');
955 956 957 958 959

        return matches;
}

static void
960
cancel_end_session (GsmManager *manager)
961
{
962 963 964 965 966
        /* just ignore if received outside of shutdown */
        if (manager->priv->phase < GSM_MANAGER_PHASE_QUERY_END_SESSION) {
                return;
        }

967
        /* switch back to running phase */
968
        g_debug ("GsmManager: Cancelling the end of session");
969

970 971 972 973 974 975 976
        /* remove the dialog before we remove the inhibitors, else the dialog
         * will activate itself automatically when the last inhibitor will be
         * removed */
        if (manager->priv->inhibit_dialog)
                gtk_widget_destroy (GTK_WIDGET (manager->priv->inhibit_dialog));
        manager->priv->inhibit_dialog = NULL;

977
        /* clear all JIT inhibitors */
978 979 980
        gsm_store_foreach_remove (manager->priv->inhibitors,
                                  (GsmStoreFunc)inhibitor_is_jit,
                                  (gpointer)manager);
981

982 983 984
        gsm_store_foreach (manager->priv->clients,
                           (GsmStoreFunc)_client_cancel_end_session,
                           NULL);
985

986
        gsm_manager_set_phase (manager, GSM_MANAGER_PHASE_RUNNING);
987
        manager->priv->logout_mode = GSM_MANAGER_LOGOUT_MODE_NORMAL;
988

989 990 991
        manager->priv->logout_type = GSM_MANAGER_LOGOUT_NONE;
        gdm_set_logout_action (GDM_LOGOUT_ACTION_NONE);

992
        start_phase (manager);
993 994
}

995 996

static void
997 998
manager_switch_user (GdkDisplay *display,
                     GsmManager *manager)
999
{
1000 1001
        GError  *error;
        char    *command;
1002 1003
        GAppLaunchContext *context;
        GAppInfo *app;
1004

1005 1006 1007
        command = g_strdup_printf ("%s %s",
                                   GDM_FLEXISERVER_COMMAND,
                                   GDM_FLEXISERVER_ARGS);
1008

1009
        error = NULL;
1010
        context = (GAppLaunchContext*) gdk_display_get_app_launch_context (display);
1011 1012 1013
        app = g_app_info_create_from_commandline (command, GDM_FLEXISERVER_COMMAND, 0, &error);

        if (app) {
1014
                g_app_info_launch (app, NULL, context, &error);
1015 1016
                g_object_unref (app);
        }
1017

1018
        g_free (command);
1019
        g_object_unref (context);
1020

1021
        if (error) {
1022 1023 1024
                g_debug ("GsmManager: Unable to start GDM greeter: %s", error->message);
                g_error_free (error);
        }
1025 1026
}

1027 1028 1029
static gboolean
sleep_lock_is_enabled (GsmManager *manager)
{
1030 1031
        return g_settings_get_boolean (manager->priv->screensaver_settings,
                                       KEY_SLEEP_LOCK);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
}

static void
manager_perhaps_lock (GsmManager *manager)
{
        GError   *error;
        gboolean  ret;

        /* only lock if gnome-screensaver is set to lock */
        if (!sleep_lock_is_enabled (manager)) {
                return;
        }

        /* do this sync to ensure it's on the screen when we start suspending */
        error = NULL;
        ret = g_spawn_command_line_sync ("gnome-screensaver-command --lock", NULL, NULL, NULL, &error);
        if (!ret) {
                g_warning ("Couldn't lock screen: %s", error->message);
                g_error_free (error);
        }
}

1054 1055 1056
static void
manager_attempt_hibernate (GsmManager *manager)
{
1057 1058 1059
        gboolean  can_hibernate;
        GError   *error;
        gboolean  ret;
1060

1061
        can_hibernate = up_client_get_can_hibernate (manager->priv->up_client);
1062
        if (can_hibernate) {
1063 1064 1065 1066

                /* lock the screen before we suspend */
                manager_perhaps_lock (manager);

1067
                error = NULL;
1068
                ret = up_client_hibernate_sync (manager->priv->up_client, NULL, &error);
1069 1070 1071 1072 1073 1074
                if (!ret) {
                        g_warning ("Unexpected hibernate failure: %s",
                                   error->message);
                        g_error_free (error);
                }
        }
1075 1076 1077 1078 1079
}

static void
manager_attempt_suspend (GsmManager *manager)
{
1080 1081 1082
        gboolean  can_suspend;
        GError   *error;
        gboolean  ret;
1083

1084
        can_suspend = up_client_get_can_suspend (manager->priv->up_client);
1085
        if (can_suspend) {
1086 1087 1088 1089

                /* lock the screen before we suspend */
                manager_perhaps_lock (manager);

1090
                error = NULL;
1091
                ret = up_client_suspend_sync (manager->priv->up_client, NULL, &error);
1092 1093 1094 1095 1096 1097
                if (!ret) {
                        g_warning ("Unexpected suspend failure: %s",
                                   error->message);
                        g_error_free (error);
                }
        }
1098 1099 1100
}

static void
1101 1102
do_inhibit_dialog_action (GdkDisplay *display,
                          GsmManager *manager,
1103
                          int         action)
1104 1105 1106
{
        switch (action) {
        case GSM_LOGOUT_ACTION_SWITCH_USER:
1107
                manager_switch_user (display, manager);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
                break;
        case GSM_LOGOUT_ACTION_HIBERNATE:
                manager_attempt_hibernate (manager);
                break;
        case GSM_LOGOUT_ACTION_SLEEP:
                manager_attempt_suspend (manager);
                break;
        case GSM_LOGOUT_ACTION_SHUTDOWN:
        case GSM_LOGOUT_ACTION_REBOOT:
        case GSM_LOGOUT_ACTION_LOGOUT:
1118
                manager->priv->logout_mode = GSM_MANAGER_LOGOUT_MODE_FORCE;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
                end_phase (manager);
                break;
        default:
                g_assert_not_reached ();
                break;
        }
}

static void
inhibit_dialog_response (GsmInhibitDialog *dialog,
                         guint             response_id,
                         GsmManager       *manager)
{
1132
        GdkDisplay *display;
1133 1134 1135 1136
        int action;

        g_debug ("GsmManager: Inhibit dialog response: %d", response_id);

1137 1138
        display = gtk_widget_get_display (GTK_WIDGET (dialog));

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
        /* must destroy dialog before cancelling since we'll
           remove JIT inhibitors and we don't want to trigger
           action. */
        g_object_get (dialog, "action", &action, NULL);
        gtk_widget_destroy (GTK_WIDGET (dialog));
        manager->priv->inhibit_dialog = NULL;

        /* In case of dialog cancel, switch user, hibernate and
         * suspend, we just perform the respective action and return,
         * without shutting down the session. */
        switch (response_id) {
        case GTK_RESPONSE_CANCEL:
        case GTK_RESPONSE_NONE:
        case GTK_RESPONSE_DELETE_EVENT:
                if (action == GSM_LOGOUT_ACTION_LOGOUT
                    || action == GSM_LOGOUT_ACTION_SHUTDOWN
                    || action == GSM_LOGOUT_ACTION_REBOOT) {
                        cancel_end_session (manager);
                }
                break;
        case GTK_RESPONSE_ACCEPT:
                g_debug ("GsmManager: doing action %d", action);
1161
                do_inhibit_dialog_action (display, manager, action);
1162 1163 1164 1165 1166 1167 1168 1169
                break;
        default:
                g_assert_not_reached ();
                break;
        }
}

static void
1170
end_session_or_show_fallback_dialog (GsmManager *manager)
1171
{
1172 1173
        GsmLogoutAction action;

1174 1175 1176 1177 1178
        if (! gsm_manager_is_logout_inhibited (manager)) {
                end_phase (manager);
                return;
        }

1179 1180 1181 1182 1183 1184
        if (manager->priv->inhibit_dialog != NULL) {
                g_debug ("GsmManager: inhibit dialog already up");
                gtk_window_present (GTK_WINDOW (manager->priv->inhibit_dialog));
                return;
        }

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
        switch (manager->priv->logout_type) {
        case GSM_MANAGER_LOGOUT_LOGOUT:
                action = GSM_LOGOUT_ACTION_LOGOUT;
                break;
        case GSM_MANAGER_LOGOUT_REBOOT:
        case GSM_MANAGER_LOGOUT_REBOOT_INTERACT:
        case GSM_MANAGER_LOGOUT_REBOOT_GDM:
                action = GSM_LOGOUT_ACTION_REBOOT;
                break;
        case GSM_MANAGER_LOGOUT_SHUTDOWN:
        case GSM_MANAGER_LOGOUT_SHUTDOWN_INTERACT:
        case GSM_MANAGER_LOGOUT_SHUTDOWN_GDM:
                action = GSM_LOGOUT_ACTION_SHUTDOWN;
                break;
        default:
                g_warning ("Unexpected logout type %d when creating inhibit dialog",
                           manager->priv->logout_type);
                action = GSM_LOGOUT_ACTION_LOGOUT;
                break;
        }

1206 1207 1208 1209
        /* Note: GSM_LOGOUT_ACTION_SHUTDOWN and GSM_LOGOUT_ACTION_REBOOT are
         * actually handled the same way as GSM_LOGOUT_ACTION_LOGOUT in the
         * inhibit dialog; the action, if the button is clicked, will be to
         * simply go to the next phase. */
1210
        manager->priv->inhibit_dialog = gsm_inhibit_dialog_new (manager->priv->inhibitors,
1211
                                                                manager->priv->clients,
1212
                                                                action);
1213 1214 1215 1216 1217 1218

        g_signal_connect (manager->priv->inhibit_dialog,
                          "response",
                          G_CALLBACK (inhibit_dialog_response),
                          manager);
        gtk_widget_show (manager->priv->inhibit_dialog);
1219 1220 1221 1222 1223
}

static void
end_session_or_show_shell_dialog (GsmManager *manager)
{
1224
        gboolean logout_prompt;
1225
        GsmShellEndSessionDialogType type;
1226
        gboolean logout_inhibited;
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

        switch (manager->priv->logout_type) {
        case GSM_MANAGER_LOGOUT_LOGOUT:
                type = GSM_SHELL_END_SESSION_DIALOG_TYPE_LOGOUT;
                break;
        case GSM_MANAGER_LOGOUT_REBOOT:
        case GSM_MANAGER_LOGOUT_REBOOT_INTERACT:
        case GSM_MANAGER_LOGOUT_REBOOT_GDM:
                type = GSM_SHELL_END_SESSION_DIALOG_TYPE_RESTART;
                break;
        case GSM_MANAGER_LOGOUT_SHUTDOWN:
        case GSM_MANAGER_LOGOUT_SHUTDOWN_INTERACT:
        case GSM_MANAGER_LOGOUT_SHUTDOWN_GDM:
                type = GSM_SHELL_END_SESSION_DIALOG_TYPE_SHUTDOWN;
                break;
        default:
                g_warning ("Unexpected logout type %d when creating end session dialog",
                           manager->priv->logout_type);
                type = GSM_SHELL_END_SESSION_DIALOG_TYPE_LOGOUT;
                break;
        }

1249 1250 1251
        logout_inhibited = gsm_manager_is_logout_inhibited (manager);
        logout_prompt = g_settings_get_boolean (manager->priv->settings,
                                                KEY_LOGOUT_PROMPT);
1252

1253 1254 1255
        switch (manager->priv->logout_mode) {
        case GSM_MANAGER_LOGOUT_MODE_NORMAL:
                if (logout_inhibited || logout_prompt) {
1256 1257 1258 1259
                        show_shell_end_session_dialog (manager, type);
                } else {
                        end_phase (manager);
                }
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
                break;

        case GSM_MANAGER_LOGOUT_MODE_NO_CONFIRMATION:
                if (logout_inhibited) {
                        show_shell_end_session_dialog (manager, type);
                } else {
                        end_phase (manager);
                }
                break;

        case GSM_MANAGER_LOGOUT_MODE_FORCE:
                end_phase (manager);
                break;
        default:
                g_assert_not_reached ();
                break;
1276 1277 1278 1279
        }

}

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
static void
show_fallback_dialog (const char *title,
                      const char *description,
                      const char *link_text,
                      const char *uri)
{
        GtkWidget *dialog, *image, *link, *hbox;
        GdkPixbuf *pix;

        dialog = gtk_message_dialog_new (NULL, 0,
                                         GTK_MESSAGE_WARNING,
                                         GTK_BUTTONS_CLOSE,
                                         title);

Alexander Larsson's avatar
Alexander Larsson committed
1294
        pix = rsvg_pixbuf_from_file_at_size (DATA_DIR "/sad-computer.svgz", 128, 128, NULL);
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
        if (pix != NULL) {
                image = gtk_image_new_from_pixbuf (pix);
                gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
                g_object_unref (pix);
        }

        if (description) {
                gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
                                                            description);
        }

        hbox = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog));

        if (uri) {
                if (link_text) {
                        link = gtk_link_button_new_with_label (uri, link_text);
                } else {
                        link = gtk_link_button_new (uri);
                }
                gtk_box_pack_start (GTK_BOX (hbox), link, FALSE, FALSE, 0);
        }

        gtk_widget_show_all (dialog);

        g_signal_connect (dialog,
                          "response",
                          G_CALLBACK (gtk_widget_destroy),
                          NULL);
}

static void
possibly_show_fallback_dialog (GsmManager *manager)
{
        if (manager->priv->is_fallback_session &&
1329
            g_strcmp0 (manager->priv->session_name, "gnome-fallback") == 0 &&
1330 1331 1332 1333
            g_settings_get_boolean (manager->priv->settings,
                                    KEY_SHOW_FALLBACK_WARNING)) {
                show_fallback_dialog (_("GNOME 3 Failed to Load"),
                                      _("Unfortunately GNOME 3 failed to start properly and started in the <i>fallback mode</i>.\n\n"
Matthias Clasen's avatar
Matthias Clasen committed
1334
                                        "This most likely means your system (graphics hardware or driver) is not capable of delivering the full GNOME 3 experience."),
1335 1336 1337 1338
                                      _("Learn more about GNOME 3"),
                                      "http://www.gnome3.org");
                g_settings_set_boolean (manager->priv->settings,
                                        KEY_SHOW_FALLBACK_WARNING, FALSE);
1339
        } else if (g_strcmp0 (manager->priv->session_name, "gnome") == 0 &&
1340 1341 1342 1343 1344 1345 1346
                   g_settings_get_boolean (manager->priv->settings,
                                           KEY_SHOW_FALLBACK_WARNING)) {
                /* Reset the setting if we ever manage to log into gnome 3 */
                g_settings_reset (manager->priv->settings, KEY_SHOW_FALLBACK_WARNING);
        }
}

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
static void
query_end_session_complete (GsmManager *manager)
{

        g_debug ("GsmManager: query end session complete");

        /* Remove the timeout since this can be called from outside the timer
         * and we don't want to have it called twice */
        if (manager->priv->query_timeout_id > 0) {
                g_source_remove (manager->priv->query_timeout_id);
                manager->priv->query_timeout_id = 0;
        }

        if (gsm_shell_is_running (manager->priv->shell)) {
                end_session_or_show_shell_dialog (manager);
        } else {
                end_session_or_show_fallback_dialog (manager);
        }
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

}

static guint32
generate_cookie (void)
{
        guint32 cookie;

        cookie = (guint32)g_random_int_range (1, G_MAXINT32);

        return cookie;
}

static guint32
_generate_unique_cookie (GsmManager *manager)
{
        guint32 cookie;

        do {
                cookie = generate_cookie ();
1385
        } while (gsm_store_find (manager->priv->inhibitors, (GsmStoreFunc)_find_by_cookie, &cookie) != NULL);
1386 1387 1388 1389 1390

        return cookie;
}

static gboolean
1391
_on_query_end_session_timeout (GsmManager *manager)
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
{
        GSList *l;

        manager->priv->query_timeout_id = 0;

        g_debug ("GsmManager: query end session timed out");

        for (l = manager->priv->query_clients; l != NULL; l = l->next) {
                guint         cookie;
                GsmInhibitor *inhibitor;
                const char   *bus_name;
1403
                char         *app_id;
1404 1405

                g_warning ("Client '%s' failed to reply before timeout",
1406
                           gsm_client_peek_id (l->data));
1407

1408 1409
                /* Don't add "not responding" inhibitors if logout is forced
                 */
1410
                if (manager->priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE) {
1411 1412 1413
                        continue;
                }

1414 1415 1416 1417 1418 1419 1420
                /* Add JIT inhibit for unresponsive client */
                if (GSM_IS_DBUS_CLIENT (l->data)) {
                        bus_name = gsm_dbus_client_get_bus_name (l->data);
                } else {
                        bus_name = NULL;
                }

1421
                app_id = g_strdup (gsm_client_peek_app_id (l->data));
1422
                if (IS_STRING_EMPTY (app_id)) {
1423
                        /* XSMP clients don't give us an app id unless we start them */
1424
                        g_free (app_id);
1425 1426 1427
                        app_id = gsm_client_get_app_name (l->data);
                }

1428
                cookie = _generate_unique_cookie (manager);
1429
                inhibitor = gsm_inhibitor_new_for_client (gsm_client_peek_id (l->data),
1430
                                                          app_id,
1431 1432 1433 1434
                                                          GSM_INHIBITOR_FLAG_LOGOUT,
                                                          _("Not responding"),
                                                          bus_name,
                                                          cookie);
1435
                g_free (app_id);
1436
                gsm_store_add (manager->priv->inhibitors, gsm_inhibitor_peek_id (inhibitor), G_OBJECT (inhibitor));
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
                g_object_unref (inhibitor);
        }

        g_slist_free (manager->priv->query_clients);
        manager->priv->query_clients = NULL;

        query_end_session_complete (manager);

        return FALSE;
}

static void
do_phase_query_end_session (GsmManager *manager)
{
        ClientEndSessionData data;

        data.manager = manager;
        data.flags = 0;

1456
        if (manager->priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE) {
1457 1458
                data.flags |= GSM_CLIENT_END_SESSION_FLAG_FORCEFUL;
        }
1459 1460 1461
        /* We only query if an app is ready to log out, so we don't use
         * GSM_CLIENT_END_SESSION_FLAG_SAVE here.
         */
1462 1463

        debug_clients (manager);
1464 1465 1466 1467
        g_debug ("GsmManager: sending query-end-session to clients (logout mode: %s)",
                 manager->priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_NORMAL? "normal" :
                 manager->priv->logout_mode == GSM_MANAGER_LOGOUT_MODE_FORCE? "forceful":
                 "no confirmation");
1468 1469 1470
        gsm_store_foreach (manager->priv->clients,
                           (GsmStoreFunc)_client_query_end_session,
                           &data);
1471

1472 1473
        /* This phase doesn't time out unless logout is forced. Typically, this
         * separate timer is only used to show UI. */
1474
        manager->priv->query_timeout_id = g_timeout_add_seconds (1, (GSourceFunc)_on_query_end_session_timeout, manager);
1475 1476
}

William Jon McCann's avatar
William Jon McCann committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
static void
update_idle (GsmManager *manager)
{
        if (gsm_manager_is_idle_inhibited (manager)) {
                gsm_presence_set_idle_enabled (manager->priv->presence, FALSE);
        } else {
                gsm_presence_set_idle_enabled (manager->priv->presence, TRUE);
        }
}

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
static void
start_phase (GsmManager *manager)
{

        g_debug ("GsmManager: starting phase %s\n",
                 phase_num_to_name (manager->priv->phase));

        /* reset state */
        g_slist_free (manager->priv->pending_apps);
        manager->priv->pending_apps = NULL;
        g_slist_free (manager->priv->query_clients);
        manager->priv->query_clients = NULL;
1499 1500
        g_slist_free (manager->priv->next_query_clients);
        manager->priv->next_query_clients = NULL;
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520

        if (manager->priv->query_timeout_id > 0) {
                g_source_remove (manager->priv-><