~indicator-applet-developers/indicator-session/trunk.0.1

« back to all changes in this revision

Viewing changes to src/gtk-dialog/logout-dialog.c

  • Committer: Ted Gould
  • Date: 2009-07-27 04:47:27 UTC
  • mfrom: (9.1.94 fusa.libindicator)
  • Revision ID: ted@canonical.com-20090727044727-m04ul5d8sxqt2pd4
Merging in the branch to port everything to libindicator and fleshing thigns out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libgksuui -- Gtk+ widget and convenience functions for requesting passwords
 
3
 * Copyright (C) 2004 Gustavo Noronha Silva
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include <string.h>
 
22
#include <math.h>
 
23
 
 
24
#include <gtk/gtk.h>
 
25
#include <gdk/gdkx.h>
 
26
#include <glib/gi18n.h>
 
27
#include <X11/XKBlib.h>
 
28
 
 
29
#include "logout-dialog.h"
 
30
#include "ck-pk-helper.h"
 
31
 
 
32
enum {
 
33
        PROP_ZERO,
 
34
        PROP_ACTION
 
35
};
 
36
 
 
37
 
 
38
static void
 
39
logout_dialog_class_init (LogoutDialogClass *klass);
 
40
 
 
41
static void
 
42
logout_dialog_init (LogoutDialog *logout_dialog);
 
43
 
 
44
static void
 
45
set_property (GObject * object, guint param_id, const GValue * value, GParamSpec *pspec);
 
46
 
 
47
static void
 
48
get_property (GObject * object, guint param_id, GValue * value, GParamSpec *pspec);
 
49
 
 
50
static gboolean
 
51
timer_cb (gpointer data);
 
52
 
 
53
static void
 
54
show_cb (GtkWidget * widget, gpointer data);
 
55
 
 
56
static void
 
57
check_restart (LogoutDialog * dialog);
 
58
 
 
59
static gchar*
 
60
get_plural_string (LogoutDialog * dialog);
 
61
 
 
62
static const gchar * title_strings[LOGOUT_DIALOG_ACTION_CNT] = {
 
63
        /* LOGOUT_DIALOG_LOGOUT, */     NC_("title", "Log Out"),
 
64
        /* LOGOUT_DIALOG_RESTART, */    NC_("title", "Restart"),
 
65
        /* LOGOUT_DIALOG_SHUTDOWN, */   NC_("title", "Shut Down")
 
66
};
 
67
 
 
68
static const gchar * button_strings[LOGOUT_DIALOG_ACTION_CNT] = {
 
69
        /* LOGOUT_DIALOG_LOGOUT, */     NC_("button", "Log Out"),
 
70
        /* LOGOUT_DIALOG_RESTART, */    NC_("button", "Restart"),
 
71
        /* LOGOUT_DIALOG_SHUTDOWN, */   NC_("button", "Shut Down")
 
72
};
 
73
 
 
74
static const gchar * restart_auth = N_("Restart...");
 
75
 
 
76
static const gchar * body_logout_update = N_("You recently installed updates which will only take effect after a restart.  Restart to apply software updates.");
 
77
 
 
78
static const gchar * icon_strings[LOGOUT_DIALOG_ACTION_CNT] = {
 
79
        /* LOGOUT_DIALOG_LOGOUT, */     "system-log-out",
 
80
        /* LOGOUT_DIALOG_RESTART, */    "system-restart",
 
81
        /* LOGOUT_DIALOG_SHUTDOWN, */   "system-shutdown"
 
82
};
 
83
 
 
84
GType
 
85
logout_dialog_get_type (void)
 
86
{
 
87
  static GType type = 0;
 
88
 
 
89
  if (type == 0)
 
90
    {
 
91
      static const GTypeInfo info =
 
92
        {
 
93
          sizeof (LogoutDialogClass), /* size of class */
 
94
          NULL, /* base_init */
 
95
          NULL, /* base_finalize */
 
96
          (GClassInitFunc) logout_dialog_class_init,
 
97
          NULL, /* class_finalize */
 
98
          NULL, /* class_data */
 
99
          sizeof (LogoutDialog), /* size of object */
 
100
          0, /* n_preallocs */
 
101
          (GInstanceInitFunc) logout_dialog_init /* instance_init */
 
102
        };
 
103
      type = g_type_register_static (gtk_dialog_get_type (),
 
104
                                     "LogoutDialogType",
 
105
                                     &info, 0);
 
106
    }
 
107
 
 
108
  return type;
 
109
}
 
110
 
 
111
static gchar*
 
112
get_plural_string (LogoutDialog * dialog)
 
113
{
 
114
  static gchar *plural_string = "";
 
115
 
 
116
  switch (dialog->action)
 
117
    {
 
118
    case LOGOUT_DIALOG_LOGOUT:
 
119
      plural_string = ngettext("You will be logged out in %d second.",
 
120
                               "You will be logged out in %d seconds.", 
 
121
                               dialog->timeout);
 
122
      break;
 
123
    case LOGOUT_DIALOG_RESTART:
 
124
      plural_string = ngettext("The computer will restart in %d second.",
 
125
                               "The computer will restart in %d seconds.", 
 
126
                               dialog->timeout);
 
127
      break;
 
128
    case LOGOUT_DIALOG_SHUTDOWN:
 
129
      plural_string = ngettext("The computer will be shut down in %d second.",
 
130
                               "The computer will be shut down in %d seconds.",
 
131
                               dialog->timeout);
 
132
      break;
 
133
        default:
 
134
          break;
 
135
    }
 
136
  
 
137
  return plural_string;
 
138
}
 
139
 
 
140
static void
 
141
logout_dialog_class_init (LogoutDialogClass *klass)
 
142
{
 
143
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
144
 
 
145
  gobject_class->set_property = set_property;
 
146
  gobject_class->get_property = get_property;
 
147
 
 
148
  g_object_class_install_property(gobject_class, PROP_ACTION,
 
149
                                  g_param_spec_int("action", NULL, NULL,
 
150
                                                   LOGOUT_DIALOG_LOGOUT, LOGOUT_DIALOG_SHUTDOWN,
 
151
                                                   LOGOUT_DIALOG_LOGOUT, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
152
 
 
153
  return;
 
154
}
 
155
 
 
156
static void
 
157
set_property (GObject * object, guint param_id, const GValue * value, GParamSpec *pspec)
 
158
{
 
159
        g_return_if_fail(param_id == PROP_ACTION);
 
160
 
 
161
        LogoutDialog * dialog = LOGOUT_DIALOG(object);
 
162
        dialog->action = (LogoutDialogAction)g_value_get_int(value);
 
163
 
 
164
        gtk_image_set_from_icon_name(GTK_IMAGE(dialog->image), icon_strings[dialog->action], GTK_ICON_SIZE_DIALOG);
 
165
        gtk_window_set_title (GTK_WINDOW(dialog), _(title_strings[dialog->action]));
 
166
        gtk_widget_hide(dialog->message);
 
167
        gtk_button_set_label(GTK_BUTTON(dialog->ok_button), _(button_strings[dialog->action]));
 
168
 
 
169
        gchar * timeouttxt = g_strdup_printf(get_plural_string(dialog), dialog->timeout);
 
170
        gtk_label_set_text(GTK_LABEL(dialog->timeout_text), timeouttxt);
 
171
        g_free(timeouttxt);
 
172
 
 
173
        check_restart(dialog);
 
174
 
 
175
        return;
 
176
}
 
177
 
 
178
static void
 
179
get_property (GObject * object, guint param_id, GValue * value, GParamSpec *pspec)
 
180
{
 
181
        g_return_if_fail(param_id == PROP_ACTION);
 
182
        g_value_set_int(value, LOGOUT_DIALOG(object)->action);
 
183
}
 
184
 
 
185
static gboolean
 
186
timer_cb (gpointer data)
 
187
{
 
188
        LogoutDialog * dialog = LOGOUT_DIALOG(data);
 
189
 
 
190
        if (dialog->timeout == 0) {
 
191
                gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
 
192
                dialog->timerfunc = 0;
 
193
                return FALSE;
 
194
        } else {
 
195
                dialog->timeout--;
 
196
 
 
197
                gchar * timeouttxt = g_strdup_printf(get_plural_string(dialog), dialog->timeout);
 
198
                gtk_label_set_text(GTK_LABEL(dialog->timeout_text), timeouttxt);
 
199
                g_free(timeouttxt);
 
200
        }
 
201
 
 
202
        return TRUE;
 
203
}
 
204
 
 
205
static void
 
206
show_cb (GtkWidget * widget, gpointer data)
 
207
{
 
208
        LogoutDialog * dialog = LOGOUT_DIALOG(widget);
 
209
 
 
210
        if (dialog->timerfunc != 0) {
 
211
                g_source_remove(dialog->timerfunc);
 
212
                dialog->timerfunc = 0;
 
213
        }
 
214
 
 
215
        dialog->timerfunc = g_timeout_add_seconds(1, timer_cb, dialog);
 
216
        return;
 
217
}
 
218
 
 
219
static void
 
220
check_restart (LogoutDialog * dialog)
 
221
{
 
222
        if (dialog->action != LOGOUT_DIALOG_LOGOUT) {
 
223
                return;
 
224
        }
 
225
 
 
226
        if (g_file_test("/var/run/reboot-required", G_FILE_TEST_EXISTS)) {
 
227
                if (pk_can_do_action("org.freedesktop.consolekit.system.restart", NULL) ||
 
228
                        pk_can_do_action("org.freedesktop.consolekit.system.restart-multiple-users", NULL)) {
 
229
 
 
230
                        gtk_label_set_text(GTK_LABEL(dialog->message), _(body_logout_update));
 
231
                        gtk_widget_show(dialog->message);
 
232
                        if (pk_require_auth(LOGOUT_DIALOG_RESTART)) {
 
233
                          gtk_button_set_label(GTK_BUTTON(dialog->restart_button), _(restart_auth));
 
234
                        } else {
 
235
                          gtk_button_set_label(GTK_BUTTON(dialog->restart_button), _(button_strings[LOGOUT_DIALOG_RESTART]));
 
236
                        }
 
237
                        gtk_widget_show(dialog->restart_button);
 
238
                }
 
239
        }
 
240
 
 
241
        return;
 
242
}
 
243
 
 
244
static gboolean
 
245
focus_out_cb (GtkWidget *widget, GdkEventFocus *event, gpointer user_data)
 
246
{
 
247
  gtk_window_present (GTK_WINDOW(widget));
 
248
  return TRUE;
 
249
}
 
250
 
 
251
static void
 
252
logout_dialog_init (LogoutDialog *logout_dialog)
 
253
{
 
254
  GtkDialog *dialog;
 
255
  gint      border_width = 6;
 
256
 
 
257
  logout_dialog->timeout = 60;
 
258
  logout_dialog->timerfunc = 0;
 
259
 
 
260
  /* dialog window */
 
261
  dialog = GTK_DIALOG(logout_dialog);
 
262
 
 
263
  /* make sure that our window will always have the focus */
 
264
  g_signal_connect (G_OBJECT(dialog), "focus-out-event",
 
265
                    G_CALLBACK(focus_out_cb), NULL);
 
266
 
 
267
  logout_dialog->main_vbox = dialog->vbox;
 
268
 
 
269
  gtk_window_set_title (GTK_WINDOW(logout_dialog), "");
 
270
  gtk_dialog_set_has_separator (GTK_DIALOG(logout_dialog), FALSE);
 
271
  gtk_container_set_border_width (GTK_CONTAINER(logout_dialog), border_width);
 
272
  gtk_box_set_spacing (GTK_BOX(logout_dialog->main_vbox), 12);
 
273
  gtk_window_set_resizable (GTK_WINDOW(logout_dialog), FALSE);
 
274
 
 
275
  gtk_window_stick(GTK_WINDOW(logout_dialog));
 
276
  gtk_window_set_keep_above(GTK_WINDOW(logout_dialog), TRUE);
 
277
  gtk_widget_realize(GTK_WIDGET(logout_dialog));
 
278
  gdk_window_set_functions(GTK_WIDGET(logout_dialog)->window, GDK_FUNC_CLOSE);
 
279
 
 
280
  /* center window */
 
281
  gtk_window_set_position (GTK_WINDOW(logout_dialog), GTK_WIN_POS_CENTER);
 
282
 
 
283
  /* the action buttons */
 
284
  /*  the cancel button  */
 
285
  logout_dialog->restart_button = gtk_dialog_add_button (dialog,
 
286
                                                      GTK_STOCK_HELP,
 
287
                                                      GTK_RESPONSE_HELP);
 
288
  gtk_button_set_label(GTK_BUTTON(logout_dialog->restart_button), _(button_strings[LOGOUT_DIALOG_RESTART]));
 
289
  gtk_widget_hide(logout_dialog->restart_button);
 
290
 
 
291
  /*  the cancel button  */
 
292
  logout_dialog->cancel_button = gtk_dialog_add_button (dialog,
 
293
                                                      GTK_STOCK_CANCEL,
 
294
                                                      GTK_RESPONSE_CANCEL);
 
295
  /*  the ok button  */
 
296
  logout_dialog->ok_button = gtk_dialog_add_button (dialog,
 
297
                                                  GTK_STOCK_OK,
 
298
                                                  GTK_RESPONSE_OK);
 
299
  gtk_widget_grab_default (logout_dialog->ok_button);
 
300
 
 
301
  /* Title */
 
302
  gtk_window_set_title (GTK_WINDOW(logout_dialog), _(title_strings[logout_dialog->action]));
 
303
 
 
304
  /* hbox */
 
305
  logout_dialog->hbox = gtk_hbox_new (FALSE, 12);
 
306
  gtk_container_set_border_width (GTK_CONTAINER(logout_dialog->hbox), 6);
 
307
  gtk_box_pack_start (GTK_BOX(logout_dialog->main_vbox),
 
308
                      logout_dialog->hbox, FALSE, FALSE, 0);
 
309
  gtk_widget_show (logout_dialog->hbox);
 
310
 
 
311
  /* image */
 
312
  logout_dialog->image =
 
313
    gtk_image_new_from_icon_name (icon_strings[logout_dialog->action],
 
314
                              GTK_ICON_SIZE_DIALOG);
 
315
  gtk_misc_set_alignment (GTK_MISC(logout_dialog->image), 0.5, 0);
 
316
  gtk_box_pack_start (GTK_BOX(logout_dialog->hbox), logout_dialog->image,
 
317
                      FALSE, FALSE, 0);
 
318
  gtk_widget_show (logout_dialog->image);
 
319
 
 
320
  /* vbox for text */
 
321
  logout_dialog->vbox_text = gtk_vbox_new(FALSE, 12);
 
322
  gtk_box_pack_start(GTK_BOX(logout_dialog->hbox), logout_dialog->vbox_text, TRUE, TRUE, 0);
 
323
  gtk_widget_show(logout_dialog->vbox_text);
 
324
 
 
325
  /* Message */
 
326
  logout_dialog->message = gtk_label_new("");
 
327
  gtk_label_set_line_wrap(GTK_LABEL(logout_dialog->message), TRUE);
 
328
  gtk_label_set_single_line_mode(GTK_LABEL(logout_dialog->message), FALSE);
 
329
  gtk_label_set_selectable(GTK_LABEL(logout_dialog->message), TRUE);
 
330
  gtk_misc_set_alignment (GTK_MISC(logout_dialog->message), 0.0, 0.0);
 
331
  gtk_box_pack_start(GTK_BOX(logout_dialog->vbox_text), logout_dialog->message, TRUE, TRUE, 0);
 
332
  gtk_widget_show(logout_dialog->message);
 
333
 
 
334
  /* timeout */
 
335
  logout_dialog->timeout_text = gtk_label_new("");
 
336
  gtk_label_set_line_wrap(GTK_LABEL(logout_dialog->timeout_text), TRUE);
 
337
  gtk_label_set_single_line_mode(GTK_LABEL(logout_dialog->timeout_text), FALSE);
 
338
  gtk_label_set_selectable(GTK_LABEL(logout_dialog->timeout_text), FALSE);
 
339
  gtk_misc_set_alignment (GTK_MISC(logout_dialog->timeout_text), 0.0, 0.5);
 
340
  gtk_box_pack_start(GTK_BOX(logout_dialog->vbox_text), logout_dialog->timeout_text, TRUE, TRUE, 0);
 
341
  gtk_widget_show(logout_dialog->timeout_text);
 
342
 
 
343
  g_signal_connect(G_OBJECT(logout_dialog), "show", G_CALLBACK(show_cb), logout_dialog);
 
344
 
 
345
  return;
 
346
}
 
347
 
 
348
/**
 
349
 * logout_dialog_new:
 
350
 *
 
351
 * Creates a new #LogoutDialog.
 
352
 *
 
353
 * Returns: the new #LogoutDialog
 
354
 */
 
355
GtkWidget*
 
356
logout_dialog_new (LogoutDialogAction action)
 
357
{
 
358
  LogoutDialog * dialog = g_object_new (LOGOUT_TYPE_DIALOG, "action", action, NULL);
 
359
  return GTK_WIDGET(dialog);
 
360
}
 
361
 
 
362
LogoutDialogAction
 
363
logout_dialog_get_action (LogoutDialog * dialog)
 
364
{
 
365
        return dialog->action;
 
366
}
 
367