1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2006 Vincent Untz
4
* Copyright (C) 2008 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License as
8
* published by the Free Software Foundation; either version 2 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22
* Vincent Untz <vuntz@gnome.org>
27
#include <glib/gi18n.h>
32
#include "gsm-logout-dialog.h"
33
#include "gsm-consolekit.h"
34
#include "gsm-icon-names.h"
37
#define GSM_LOGOUT_DIALOG_GET_PRIVATE(o) \
38
(G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_LOGOUT_DIALOG, GsmLogoutDialogPrivate))
40
/* Shared with gsm-fail-whale-dialog.c */
41
#define AUTOMATIC_ACTION_TIMEOUT 60
43
#define LOCKDOWN_SCHEMA "org.gnome.desktop.lockdown"
44
#define KEY_DISABLE_USER_SWITCHING "disable-user-switching"
47
GSM_DIALOG_LOGOUT_TYPE_LOGOUT,
48
GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN
49
} GsmDialogLogoutType;
51
struct _GsmLogoutDialogPrivate
53
GsmDialogLogoutType type;
56
GsmConsolekit *consolekit;
59
unsigned int timeout_id;
61
unsigned int default_response;
64
static GsmLogoutDialog *current_dialog = NULL;
66
static void gsm_logout_dialog_set_timeout (GsmLogoutDialog *logout_dialog);
68
static void gsm_logout_dialog_destroy (GsmLogoutDialog *logout_dialog,
71
static void gsm_logout_dialog_show (GsmLogoutDialog *logout_dialog,
79
G_DEFINE_TYPE (GsmLogoutDialog, gsm_logout_dialog, GTK_TYPE_MESSAGE_DIALOG);
82
gsm_logout_dialog_set_property (GObject *object,
88
case PROP_MESSAGE_TYPE:
91
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97
gsm_logout_dialog_get_property (GObject *object,
103
case PROP_MESSAGE_TYPE:
104
g_value_set_enum (value, GTK_MESSAGE_WARNING);
107
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113
gsm_logout_dialog_class_init (GsmLogoutDialogClass *klass)
115
GObjectClass *gobject_class;
117
gobject_class = G_OBJECT_CLASS (klass);
119
/* This is a workaround to avoid a stupid crash: libgnomeui
120
* listens for the "show" signal on all GtkMessageDialog and
121
* gets the "message-type" of the dialogs. We will crash when
122
* it accesses this property if we don't override it since we
123
* didn't define it. */
124
gobject_class->set_property = gsm_logout_dialog_set_property;
125
gobject_class->get_property = gsm_logout_dialog_get_property;
127
g_object_class_override_property (gobject_class,
131
g_type_class_add_private (klass, sizeof (GsmLogoutDialogPrivate));
135
gsm_logout_dialog_init (GsmLogoutDialog *logout_dialog)
137
logout_dialog->priv = GSM_LOGOUT_DIALOG_GET_PRIVATE (logout_dialog);
139
logout_dialog->priv->timeout_id = 0;
140
logout_dialog->priv->timeout = 0;
141
logout_dialog->priv->default_response = GTK_RESPONSE_CANCEL;
143
gtk_window_set_skip_taskbar_hint (GTK_WINDOW (logout_dialog), TRUE);
144
gtk_window_set_keep_above (GTK_WINDOW (logout_dialog), TRUE);
145
gtk_window_stick (GTK_WINDOW (logout_dialog));
147
logout_dialog->priv->up_client = up_client_new ();
149
logout_dialog->priv->consolekit = gsm_get_consolekit ();
151
g_signal_connect (logout_dialog,
153
G_CALLBACK (gsm_logout_dialog_destroy),
156
g_signal_connect (logout_dialog,
158
G_CALLBACK (gsm_logout_dialog_show),
163
gsm_logout_dialog_destroy (GsmLogoutDialog *logout_dialog,
166
if (logout_dialog->priv->timeout_id != 0) {
167
g_source_remove (logout_dialog->priv->timeout_id);
168
logout_dialog->priv->timeout_id = 0;
171
if (logout_dialog->priv->up_client) {
172
g_object_unref (logout_dialog->priv->up_client);
173
logout_dialog->priv->up_client = NULL;
176
if (logout_dialog->priv->consolekit) {
177
g_object_unref (logout_dialog->priv->consolekit);
178
logout_dialog->priv->consolekit = NULL;
181
current_dialog = NULL;
185
gsm_logout_supports_system_suspend (GsmLogoutDialog *logout_dialog)
187
return up_client_get_can_suspend (logout_dialog->priv->up_client);
191
gsm_logout_supports_system_hibernate (GsmLogoutDialog *logout_dialog)
193
return up_client_get_can_hibernate (logout_dialog->priv->up_client);
197
gsm_logout_supports_switch_user (GsmLogoutDialog *logout_dialog)
202
settings = g_settings_new (LOCKDOWN_SCHEMA);
203
if (g_settings_get_boolean (settings, KEY_DISABLE_USER_SWITCHING))
205
g_object_unref (settings);
208
ret = gsm_consolekit_can_switch_user (logout_dialog->priv->consolekit);
214
gsm_logout_supports_reboot (GsmLogoutDialog *logout_dialog)
218
ret = gsm_consolekit_can_restart (logout_dialog->priv->consolekit);
220
ret = gdm_supports_logout_action (GDM_LOGOUT_ACTION_REBOOT);
227
gsm_logout_supports_shutdown (GsmLogoutDialog *logout_dialog)
231
ret = gsm_consolekit_can_stop (logout_dialog->priv->consolekit);
233
ret = gdm_supports_logout_action (GDM_LOGOUT_ACTION_SHUTDOWN);
240
gsm_logout_dialog_show (GsmLogoutDialog *logout_dialog, gpointer user_data)
242
gsm_logout_dialog_set_timeout (logout_dialog);
246
gsm_logout_dialog_timeout (gpointer data)
248
GsmLogoutDialog *logout_dialog;
249
char *seconds_warning;
250
char *secondary_text;
252
static char *session_type = NULL;
254
logout_dialog = (GsmLogoutDialog *) data;
256
if (!logout_dialog->priv->timeout) {
257
gtk_dialog_response (GTK_DIALOG (logout_dialog),
258
logout_dialog->priv->default_response);
263
if (logout_dialog->priv->timeout <= 30) {
264
seconds_to_show = logout_dialog->priv->timeout;
266
seconds_to_show = (logout_dialog->priv->timeout/10) * 10;
268
if (logout_dialog->priv->timeout % 10)
269
seconds_to_show += 10;
272
switch (logout_dialog->priv->type) {
273
case GSM_DIALOG_LOGOUT_TYPE_LOGOUT:
274
/* This string is shared with gsm-fail-whale-dialog.c */
275
seconds_warning = ngettext ("You will be automatically logged "
277
"You will be automatically logged "
278
"out in %d seconds.",
282
case GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN:
283
seconds_warning = ngettext ("This system will be automatically "
284
"shut down in %d second.",
285
"This system will be automatically "
286
"shut down in %d seconds.",
291
g_assert_not_reached ();
294
if (session_type == NULL) {
295
GsmConsolekit *consolekit;
297
consolekit = gsm_get_consolekit ();
298
session_type = gsm_consolekit_get_current_session_type (consolekit);
299
g_object_unref (consolekit);
302
if (g_strcmp0 (session_type, GSM_CONSOLEKIT_SESSION_TYPE_LOGIN_WINDOW) != 0) {
305
name = g_locale_to_utf8 (g_get_real_name (), -1, NULL, NULL, NULL);
307
if (!name || name[0] == '\0' || strcmp (name, "Unknown") == 0) {
308
name = g_locale_to_utf8 (g_get_user_name (), -1 , NULL, NULL, NULL);
312
name = g_strdup (g_get_user_name ());
315
tmp = g_strdup_printf (_("You are currently logged in as \"%s\"."), name);
316
secondary_text = g_strconcat (tmp, "\n", seconds_warning, NULL);
321
secondary_text = g_strdup (seconds_warning);
324
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (logout_dialog),
329
logout_dialog->priv->timeout--;
331
g_free (secondary_text);
337
gsm_logout_dialog_set_timeout (GsmLogoutDialog *logout_dialog)
339
logout_dialog->priv->timeout = AUTOMATIC_ACTION_TIMEOUT;
341
/* Sets the secondary text */
342
gsm_logout_dialog_timeout (logout_dialog);
344
if (logout_dialog->priv->timeout_id != 0) {
345
g_source_remove (logout_dialog->priv->timeout_id);
348
logout_dialog->priv->timeout_id = g_timeout_add (1000,
349
gsm_logout_dialog_timeout,
354
gsm_get_dialog (GsmDialogLogoutType type,
356
guint32 activate_time)
358
GsmLogoutDialog *logout_dialog;
359
GtkWidget *dialog_image;
360
const char *primary_text;
361
const char *icon_name;
363
if (current_dialog != NULL) {
364
gtk_widget_destroy (GTK_WIDGET (current_dialog));
367
logout_dialog = g_object_new (GSM_TYPE_LOGOUT_DIALOG, NULL);
369
current_dialog = logout_dialog;
371
gtk_window_set_title (GTK_WINDOW (logout_dialog), "");
373
logout_dialog->priv->type = type;
379
case GSM_DIALOG_LOGOUT_TYPE_LOGOUT:
380
icon_name = GSM_ICON_LOGOUT;
381
primary_text = _("Log out of this system now?");
383
logout_dialog->priv->default_response = GSM_LOGOUT_RESPONSE_LOGOUT;
385
if (gsm_logout_supports_switch_user (logout_dialog)) {
386
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
388
GSM_LOGOUT_RESPONSE_SWITCH_USER);
391
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
393
GTK_RESPONSE_CANCEL);
395
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
397
GSM_LOGOUT_RESPONSE_LOGOUT);
400
case GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN:
401
icon_name = GSM_ICON_SHUTDOWN;
402
primary_text = _("Shut down this system now?");
404
logout_dialog->priv->default_response = GSM_LOGOUT_RESPONSE_SHUTDOWN;
406
if (gsm_logout_supports_system_suspend (logout_dialog)) {
407
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
409
GSM_LOGOUT_RESPONSE_SLEEP);
412
if (gsm_logout_supports_system_hibernate (logout_dialog)) {
413
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
415
GSM_LOGOUT_RESPONSE_HIBERNATE);
418
if (gsm_logout_supports_reboot (logout_dialog)) {
419
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
421
GSM_LOGOUT_RESPONSE_REBOOT);
424
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
426
GTK_RESPONSE_CANCEL);
428
if (gsm_logout_supports_shutdown (logout_dialog)) {
429
gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
431
GSM_LOGOUT_RESPONSE_SHUTDOWN);
435
g_assert_not_reached ();
438
dialog_image = gtk_message_dialog_get_image (GTK_MESSAGE_DIALOG (logout_dialog));
440
gtk_image_set_from_icon_name (GTK_IMAGE (dialog_image),
441
icon_name, GTK_ICON_SIZE_DIALOG);
442
gtk_window_set_icon_name (GTK_WINDOW (logout_dialog), icon_name);
443
gtk_window_set_position (GTK_WINDOW (logout_dialog), GTK_WIN_POS_CENTER_ALWAYS);
444
gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (logout_dialog), primary_text);
446
gtk_dialog_set_default_response (GTK_DIALOG (logout_dialog),
447
logout_dialog->priv->default_response);
449
gtk_window_set_screen (GTK_WINDOW (logout_dialog), screen);
451
return GTK_WIDGET (logout_dialog);
455
gsm_get_shutdown_dialog (GdkScreen *screen,
456
guint32 activate_time)
458
return gsm_get_dialog (GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN,
464
gsm_get_logout_dialog (GdkScreen *screen,
465
guint32 activate_time)
467
return gsm_get_dialog (GSM_DIALOG_LOGOUT_TYPE_LOGOUT,