~ubuntu-branches/ubuntu/oneiric/gnome-session/oneiric-proposed

« back to all changes in this revision

Viewing changes to .pc/80_new_upstream_session_dialog.patch/gnome-session/gsm-logout-dialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-07-11 16:39:56 UTC
  • Revision ID: james.westby@ubuntu.com-20110711163956-16xi409pbfc58ox4
Tags: 3.1.3-0ubuntu2
* 03_display_session_capplet.patch: display the session properties capplet, 
  the list is cleaned from the system services now and it can be useful 
  for users who want to start softwares with the session (lp: #802218)
* series: comment 80_new_upstream_session_dialog, the modified version never
  went in upstream git and has issues with gtk3, if the upstream dialog is
  not working fine in our desktop we should discuss how to maintain a custom
  session dialog in a better way (lp:807503)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2006 Vincent Untz
4
 
 * Copyright (C) 2008 Red Hat, Inc.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 * 02111-1307, USA.
20
 
 *
21
 
 * Authors:
22
 
 *      Vincent Untz <vuntz@gnome.org>
23
 
 */
24
 
 
25
 
#include <config.h>
26
 
 
27
 
#include <glib/gi18n.h>
28
 
#include <gtk/gtk.h>
29
 
 
30
 
#include <upower.h>
31
 
 
32
 
#include "gsm-logout-dialog.h"
33
 
#include "gsm-consolekit.h"
34
 
#include "gsm-icon-names.h"
35
 
#include "gdm.h"
36
 
 
37
 
#define GSM_LOGOUT_DIALOG_GET_PRIVATE(o)                                \
38
 
        (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSM_TYPE_LOGOUT_DIALOG, GsmLogoutDialogPrivate))
39
 
 
40
 
/* Shared with gsm-fail-whale-dialog.c */
41
 
#define AUTOMATIC_ACTION_TIMEOUT 60
42
 
 
43
 
#define LOCKDOWN_SCHEMA            "org.gnome.desktop.lockdown"
44
 
#define KEY_DISABLE_USER_SWITCHING "disable-user-switching"
45
 
 
46
 
typedef enum {
47
 
        GSM_DIALOG_LOGOUT_TYPE_LOGOUT,
48
 
        GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN
49
 
} GsmDialogLogoutType;
50
 
 
51
 
struct _GsmLogoutDialogPrivate
52
 
{
53
 
        GsmDialogLogoutType  type;
54
 
 
55
 
        UpClient            *up_client;
56
 
        GsmConsolekit       *consolekit;
57
 
 
58
 
        int                  timeout;
59
 
        unsigned int         timeout_id;
60
 
 
61
 
        unsigned int         default_response;
62
 
};
63
 
 
64
 
static GsmLogoutDialog *current_dialog = NULL;
65
 
 
66
 
static void gsm_logout_dialog_set_timeout  (GsmLogoutDialog *logout_dialog);
67
 
 
68
 
static void gsm_logout_dialog_destroy  (GsmLogoutDialog *logout_dialog,
69
 
                                        gpointer         data);
70
 
 
71
 
static void gsm_logout_dialog_show     (GsmLogoutDialog *logout_dialog,
72
 
                                        gpointer         data);
73
 
 
74
 
enum {
75
 
        PROP_0,
76
 
        PROP_MESSAGE_TYPE
77
 
};
78
 
 
79
 
G_DEFINE_TYPE (GsmLogoutDialog, gsm_logout_dialog, GTK_TYPE_MESSAGE_DIALOG);
80
 
 
81
 
static void
82
 
gsm_logout_dialog_set_property (GObject      *object,
83
 
                                guint         prop_id,
84
 
                                const GValue *value,
85
 
                                GParamSpec   *pspec)
86
 
{
87
 
        switch (prop_id) {
88
 
        case PROP_MESSAGE_TYPE:
89
 
                break;
90
 
        default:
91
 
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92
 
                break;
93
 
        }
94
 
}
95
 
 
96
 
static void
97
 
gsm_logout_dialog_get_property (GObject     *object,
98
 
                                guint        prop_id,
99
 
                                GValue      *value,
100
 
                                GParamSpec  *pspec)
101
 
{
102
 
        switch (prop_id) {
103
 
        case PROP_MESSAGE_TYPE:
104
 
                g_value_set_enum (value, GTK_MESSAGE_WARNING);
105
 
                break;
106
 
        default:
107
 
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108
 
                break;
109
 
        }
110
 
}
111
 
 
112
 
static void
113
 
gsm_logout_dialog_class_init (GsmLogoutDialogClass *klass)
114
 
{
115
 
        GObjectClass *gobject_class;
116
 
 
117
 
        gobject_class = G_OBJECT_CLASS (klass);
118
 
 
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;
126
 
 
127
 
        g_object_class_override_property (gobject_class,
128
 
                                          PROP_MESSAGE_TYPE,
129
 
                                          "message-type");
130
 
 
131
 
        g_type_class_add_private (klass, sizeof (GsmLogoutDialogPrivate));
132
 
}
133
 
 
134
 
static void
135
 
gsm_logout_dialog_init (GsmLogoutDialog *logout_dialog)
136
 
{
137
 
        logout_dialog->priv = GSM_LOGOUT_DIALOG_GET_PRIVATE (logout_dialog);
138
 
 
139
 
        logout_dialog->priv->timeout_id = 0;
140
 
        logout_dialog->priv->timeout = 0;
141
 
        logout_dialog->priv->default_response = GTK_RESPONSE_CANCEL;
142
 
 
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));
146
 
 
147
 
        logout_dialog->priv->up_client = up_client_new ();
148
 
 
149
 
        logout_dialog->priv->consolekit = gsm_get_consolekit ();
150
 
 
151
 
        g_signal_connect (logout_dialog,
152
 
                          "destroy",
153
 
                          G_CALLBACK (gsm_logout_dialog_destroy),
154
 
                          NULL);
155
 
 
156
 
        g_signal_connect (logout_dialog,
157
 
                          "show",
158
 
                          G_CALLBACK (gsm_logout_dialog_show),
159
 
                          NULL);
160
 
}
161
 
 
162
 
static void
163
 
gsm_logout_dialog_destroy (GsmLogoutDialog *logout_dialog,
164
 
                           gpointer         data)
165
 
{
166
 
        if (logout_dialog->priv->timeout_id != 0) {
167
 
                g_source_remove (logout_dialog->priv->timeout_id);
168
 
                logout_dialog->priv->timeout_id = 0;
169
 
        }
170
 
 
171
 
        if (logout_dialog->priv->up_client) {
172
 
                g_object_unref (logout_dialog->priv->up_client);
173
 
                logout_dialog->priv->up_client = NULL;
174
 
        }
175
 
 
176
 
        if (logout_dialog->priv->consolekit) {
177
 
                g_object_unref (logout_dialog->priv->consolekit);
178
 
                logout_dialog->priv->consolekit = NULL;
179
 
        }
180
 
 
181
 
        current_dialog = NULL;
182
 
}
183
 
 
184
 
static gboolean
185
 
gsm_logout_supports_system_suspend (GsmLogoutDialog *logout_dialog)
186
 
{
187
 
        return up_client_get_can_suspend (logout_dialog->priv->up_client);
188
 
}
189
 
 
190
 
static gboolean
191
 
gsm_logout_supports_system_hibernate (GsmLogoutDialog *logout_dialog)
192
 
{
193
 
        return up_client_get_can_hibernate (logout_dialog->priv->up_client);
194
 
}
195
 
 
196
 
static gboolean
197
 
gsm_logout_supports_switch_user (GsmLogoutDialog *logout_dialog)
198
 
{
199
 
        GSettings *settings;
200
 
        gboolean   ret;
201
 
 
202
 
        settings = g_settings_new (LOCKDOWN_SCHEMA);
203
 
        if (g_settings_get_boolean (settings, KEY_DISABLE_USER_SWITCHING))
204
 
                ret = FALSE;
205
 
        g_object_unref (settings);
206
 
 
207
 
        if (ret)
208
 
                ret = gsm_consolekit_can_switch_user (logout_dialog->priv->consolekit);
209
 
 
210
 
        return ret;
211
 
}
212
 
 
213
 
static gboolean
214
 
gsm_logout_supports_reboot (GsmLogoutDialog *logout_dialog)
215
 
{
216
 
        gboolean ret;
217
 
 
218
 
        ret = gsm_consolekit_can_restart (logout_dialog->priv->consolekit);
219
 
        if (!ret) {
220
 
                ret = gdm_supports_logout_action (GDM_LOGOUT_ACTION_REBOOT);
221
 
        }
222
 
 
223
 
        return ret;
224
 
}
225
 
 
226
 
static gboolean
227
 
gsm_logout_supports_shutdown (GsmLogoutDialog *logout_dialog)
228
 
{
229
 
        gboolean ret;
230
 
 
231
 
        ret = gsm_consolekit_can_stop (logout_dialog->priv->consolekit);
232
 
        if (!ret) {
233
 
                ret = gdm_supports_logout_action (GDM_LOGOUT_ACTION_SHUTDOWN);
234
 
        }
235
 
 
236
 
        return ret;
237
 
}
238
 
 
239
 
static void
240
 
gsm_logout_dialog_show (GsmLogoutDialog *logout_dialog, gpointer user_data)
241
 
{
242
 
        gsm_logout_dialog_set_timeout (logout_dialog);
243
 
}
244
 
 
245
 
static gboolean
246
 
gsm_logout_dialog_timeout (gpointer data)
247
 
{
248
 
        GsmLogoutDialog *logout_dialog;
249
 
        char            *seconds_warning;
250
 
        char            *secondary_text;
251
 
        int              seconds_to_show;
252
 
        static char     *session_type = NULL;
253
 
 
254
 
        logout_dialog = (GsmLogoutDialog *) data;
255
 
 
256
 
        if (!logout_dialog->priv->timeout) {
257
 
                gtk_dialog_response (GTK_DIALOG (logout_dialog),
258
 
                                     logout_dialog->priv->default_response);
259
 
 
260
 
                return FALSE;
261
 
        }
262
 
 
263
 
        if (logout_dialog->priv->timeout <= 30) {
264
 
                seconds_to_show = logout_dialog->priv->timeout;
265
 
        } else {
266
 
                seconds_to_show = (logout_dialog->priv->timeout/10) * 10;
267
 
 
268
 
                if (logout_dialog->priv->timeout % 10)
269
 
                        seconds_to_show += 10;
270
 
        }
271
 
 
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 "
276
 
                                            "out in %d second.",
277
 
                                            "You will be automatically logged "
278
 
                                            "out in %d seconds.",
279
 
                                            seconds_to_show);
280
 
                break;
281
 
 
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.",
287
 
                                            seconds_to_show);
288
 
                break;
289
 
 
290
 
        default:
291
 
                g_assert_not_reached ();
292
 
        }
293
 
 
294
 
        if (session_type == NULL) {
295
 
                GsmConsolekit *consolekit;
296
 
 
297
 
                consolekit = gsm_get_consolekit ();
298
 
                session_type = gsm_consolekit_get_current_session_type (consolekit);
299
 
                g_object_unref (consolekit);
300
 
        }
301
 
 
302
 
        if (g_strcmp0 (session_type, GSM_CONSOLEKIT_SESSION_TYPE_LOGIN_WINDOW) != 0) {
303
 
                char *name, *tmp;
304
 
 
305
 
                name = g_locale_to_utf8 (g_get_real_name (), -1, NULL, NULL, NULL);
306
 
 
307
 
                if (!name || name[0] == '\0' || strcmp (name, "Unknown") == 0) {
308
 
                        name = g_locale_to_utf8 (g_get_user_name (), -1 , NULL, NULL, NULL);
309
 
                }
310
 
 
311
 
                if (!name) {
312
 
                        name = g_strdup (g_get_user_name ());
313
 
                }
314
 
 
315
 
                tmp = g_strdup_printf (_("You are currently logged in as \"%s\"."), name);
316
 
                secondary_text = g_strconcat (tmp, "\n", seconds_warning, NULL);
317
 
                g_free (tmp);
318
 
 
319
 
                g_free (name);
320
 
        } else {
321
 
                secondary_text = g_strdup (seconds_warning);
322
 
        }
323
 
 
324
 
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (logout_dialog),
325
 
                                                  secondary_text,
326
 
                                                  seconds_to_show,
327
 
                                                  NULL);
328
 
 
329
 
        logout_dialog->priv->timeout--;
330
 
 
331
 
        g_free (secondary_text);
332
 
 
333
 
        return TRUE;
334
 
}
335
 
 
336
 
static void
337
 
gsm_logout_dialog_set_timeout (GsmLogoutDialog *logout_dialog)
338
 
{
339
 
        logout_dialog->priv->timeout = AUTOMATIC_ACTION_TIMEOUT;
340
 
 
341
 
        /* Sets the secondary text */
342
 
        gsm_logout_dialog_timeout (logout_dialog);
343
 
 
344
 
        if (logout_dialog->priv->timeout_id != 0) {
345
 
                g_source_remove (logout_dialog->priv->timeout_id);
346
 
        }
347
 
 
348
 
        logout_dialog->priv->timeout_id = g_timeout_add (1000,
349
 
                                                         gsm_logout_dialog_timeout,
350
 
                                                         logout_dialog);
351
 
}
352
 
 
353
 
static GtkWidget *
354
 
gsm_get_dialog (GsmDialogLogoutType type,
355
 
                GdkScreen          *screen,
356
 
                guint32             activate_time)
357
 
{
358
 
        GsmLogoutDialog *logout_dialog;
359
 
        GtkWidget       *dialog_image;
360
 
        const char      *primary_text;
361
 
        const char      *icon_name;
362
 
 
363
 
        if (current_dialog != NULL) {
364
 
                gtk_widget_destroy (GTK_WIDGET (current_dialog));
365
 
        }
366
 
 
367
 
        logout_dialog = g_object_new (GSM_TYPE_LOGOUT_DIALOG, NULL);
368
 
 
369
 
        current_dialog = logout_dialog;
370
 
 
371
 
        gtk_window_set_title (GTK_WINDOW (logout_dialog), "");
372
 
 
373
 
        logout_dialog->priv->type = type;
374
 
 
375
 
        icon_name = NULL;
376
 
        primary_text = NULL;
377
 
 
378
 
        switch (type) {
379
 
        case GSM_DIALOG_LOGOUT_TYPE_LOGOUT:
380
 
                icon_name    = GSM_ICON_LOGOUT;
381
 
                primary_text = _("Log out of this system now?");
382
 
 
383
 
                logout_dialog->priv->default_response = GSM_LOGOUT_RESPONSE_LOGOUT;
384
 
 
385
 
                if (gsm_logout_supports_switch_user (logout_dialog)) {
386
 
                        gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
387
 
                                               _("_Switch User"),
388
 
                                               GSM_LOGOUT_RESPONSE_SWITCH_USER);
389
 
                }
390
 
 
391
 
                gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
392
 
                                       GTK_STOCK_CANCEL,
393
 
                                       GTK_RESPONSE_CANCEL);
394
 
 
395
 
                gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
396
 
                                       _("_Log Out"),
397
 
                                       GSM_LOGOUT_RESPONSE_LOGOUT);
398
 
 
399
 
                break;
400
 
        case GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN:
401
 
                icon_name    = GSM_ICON_SHUTDOWN;
402
 
                primary_text = _("Shut down this system now?");
403
 
 
404
 
                logout_dialog->priv->default_response = GSM_LOGOUT_RESPONSE_SHUTDOWN;
405
 
 
406
 
                if (gsm_logout_supports_system_suspend (logout_dialog)) {
407
 
                        gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
408
 
                                               _("S_uspend"),
409
 
                                               GSM_LOGOUT_RESPONSE_SLEEP);
410
 
                }
411
 
 
412
 
                if (gsm_logout_supports_system_hibernate (logout_dialog)) {
413
 
                        gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
414
 
                                               _("_Hibernate"),
415
 
                                               GSM_LOGOUT_RESPONSE_HIBERNATE);
416
 
                }
417
 
 
418
 
                if (gsm_logout_supports_reboot (logout_dialog)) {
419
 
                        gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
420
 
                                               _("_Restart"),
421
 
                                               GSM_LOGOUT_RESPONSE_REBOOT);
422
 
                }
423
 
 
424
 
                gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
425
 
                                       GTK_STOCK_CANCEL,
426
 
                                       GTK_RESPONSE_CANCEL);
427
 
 
428
 
                if (gsm_logout_supports_shutdown (logout_dialog)) {
429
 
                        gtk_dialog_add_button (GTK_DIALOG (logout_dialog),
430
 
                                               _("_Shut Down"),
431
 
                                               GSM_LOGOUT_RESPONSE_SHUTDOWN);
432
 
                }
433
 
                break;
434
 
        default:
435
 
                g_assert_not_reached ();
436
 
        }
437
 
 
438
 
        dialog_image = gtk_message_dialog_get_image (GTK_MESSAGE_DIALOG (logout_dialog));
439
 
 
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);
445
 
 
446
 
        gtk_dialog_set_default_response (GTK_DIALOG (logout_dialog),
447
 
                                         logout_dialog->priv->default_response);
448
 
 
449
 
        gtk_window_set_screen (GTK_WINDOW (logout_dialog), screen);
450
 
 
451
 
        return GTK_WIDGET (logout_dialog);
452
 
}
453
 
 
454
 
GtkWidget *
455
 
gsm_get_shutdown_dialog (GdkScreen *screen,
456
 
                         guint32    activate_time)
457
 
{
458
 
        return gsm_get_dialog (GSM_DIALOG_LOGOUT_TYPE_SHUTDOWN,
459
 
                               screen,
460
 
                               activate_time);
461
 
}
462
 
 
463
 
GtkWidget *
464
 
gsm_get_logout_dialog (GdkScreen *screen,
465
 
                       guint32    activate_time)
466
 
{
467
 
        return gsm_get_dialog (GSM_DIALOG_LOGOUT_TYPE_LOGOUT,
468
 
                               screen,
469
 
                               activate_time);
470
 
}