Line 0
Link Here
|
|
|
1 |
/* |
2 |
* panel-logout.c: |
3 |
* |
4 |
* Copyright (C) 2006 Vincent Untz |
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 |
|
26 |
#include <config.h> |
27 |
|
28 |
#include <string.h> |
29 |
|
30 |
#include <glib/gi18n.h> |
31 |
#include <gtk/gtkimage.h> |
32 |
#include <gtk/gtklabel.h> |
33 |
#include <gtk/gtkstock.h> |
34 |
|
35 |
#include <libpanel-util/panel-power-manager.h> |
36 |
|
37 |
#include "panel-logout.h" |
38 |
#include "panel-gdm.h" |
39 |
#include "panel-session.h" |
40 |
#include "panel-icon-names.h" |
41 |
|
42 |
#define PANEL_LOGOUT_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PANEL_TYPE_LOGOUT_DIALOG, PanelLogoutDialogPrivate)) |
43 |
|
44 |
#define AUTOMATIC_ACTION_TIMEOUT 60 |
45 |
|
46 |
enum { |
47 |
PANEL_LOGOUT_RESPONSE_LOGOUT, |
48 |
PANEL_LOGOUT_RESPONSE_SWITCH_USER, |
49 |
PANEL_LOGOUT_RESPONSE_SHUTDOWN, |
50 |
PANEL_LOGOUT_RESPONSE_REBOOT, |
51 |
PANEL_LOGOUT_RESPONSE_STD, |
52 |
PANEL_LOGOUT_RESPONSE_STR |
53 |
}; |
54 |
|
55 |
struct _PanelLogoutDialogPrivate { |
56 |
PanelLogoutDialogType type; |
57 |
|
58 |
PanelPowerManager *power_manager; |
59 |
|
60 |
int timeout; |
61 |
unsigned int timeout_id; |
62 |
|
63 |
unsigned int default_response; |
64 |
}; |
65 |
|
66 |
static PanelLogoutDialog *current_dialog = NULL; |
67 |
|
68 |
static void panel_logout_destroy (PanelLogoutDialog *logout_dialog, |
69 |
gpointer data); |
70 |
static void panel_logout_response (PanelLogoutDialog *logout_dialog, |
71 |
guint response_id, |
72 |
gpointer data); |
73 |
|
74 |
enum { |
75 |
PROP_0, |
76 |
PROP_MESSAGE_TYPE |
77 |
}; |
78 |
G_DEFINE_TYPE (PanelLogoutDialog, panel_logout, GTK_TYPE_MESSAGE_DIALOG); |
79 |
|
80 |
static void |
81 |
panel_logout_set_property (GObject *object, |
82 |
guint prop_id, |
83 |
const GValue *value, |
84 |
GParamSpec *pspec) |
85 |
{ |
86 |
switch (prop_id) { |
87 |
case PROP_MESSAGE_TYPE: |
88 |
break; |
89 |
default: |
90 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
91 |
break; |
92 |
} |
93 |
} |
94 |
|
95 |
static void |
96 |
panel_logout_get_property (GObject *object, |
97 |
guint prop_id, |
98 |
GValue *value, |
99 |
GParamSpec *pspec) |
100 |
{ |
101 |
switch (prop_id) { |
102 |
case PROP_MESSAGE_TYPE: |
103 |
g_value_set_enum (value, GTK_MESSAGE_WARNING); |
104 |
break; |
105 |
default: |
106 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
107 |
break; |
108 |
} |
109 |
} |
110 |
|
111 |
static void |
112 |
panel_logout_class_init (PanelLogoutDialogClass *klass) |
113 |
{ |
114 |
GObjectClass *gobject_class; |
115 |
|
116 |
gobject_class = G_OBJECT_CLASS (klass); |
117 |
|
118 |
/* This is a workaround to avoid a stupid crash: libgnomeui |
119 |
* listens for the "show" signal on all GtkMessageDialog and |
120 |
* gets the "message-type" of the dialogs. We will crash when |
121 |
* it accesses this property if we don't override it since we |
122 |
* didn't define it. */ |
123 |
gobject_class->set_property = panel_logout_set_property; |
124 |
gobject_class->get_property = panel_logout_get_property; |
125 |
|
126 |
g_object_class_override_property (gobject_class, |
127 |
PROP_MESSAGE_TYPE, |
128 |
"message-type"); |
129 |
|
130 |
g_type_class_add_private (klass, sizeof (PanelLogoutDialogPrivate)); |
131 |
} |
132 |
|
133 |
static void |
134 |
panel_logout_init (PanelLogoutDialog *logout_dialog) |
135 |
{ |
136 |
logout_dialog->priv = PANEL_LOGOUT_DIALOG_GET_PRIVATE (logout_dialog); |
137 |
|
138 |
logout_dialog->priv->timeout_id = 0; |
139 |
logout_dialog->priv->timeout = 0; |
140 |
logout_dialog->priv->default_response = GTK_RESPONSE_CANCEL; |
141 |
|
142 |
/* FIXME: we should most probably use gtk_window_set_transient_for(PANEL) */ |
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 |
gtk_window_set_position (GTK_WINDOW (logout_dialog), |
147 |
GTK_WIN_POS_CENTER_ALWAYS); |
148 |
|
149 |
logout_dialog->priv->power_manager = panel_power_manager_get (); |
150 |
|
151 |
g_signal_connect (logout_dialog, "destroy", |
152 |
G_CALLBACK (panel_logout_destroy), NULL); |
153 |
g_signal_connect (logout_dialog, "response", |
154 |
G_CALLBACK (panel_logout_response), NULL); |
155 |
} |
156 |
|
157 |
static void |
158 |
panel_logout_destroy (PanelLogoutDialog *logout_dialog, |
159 |
gpointer data) |
160 |
{ |
161 |
if (logout_dialog->priv->timeout_id != 0) |
162 |
g_source_remove (logout_dialog->priv->timeout_id); |
163 |
logout_dialog->priv->timeout_id = 0; |
164 |
|
165 |
g_object_unref (logout_dialog->priv->power_manager); |
166 |
logout_dialog->priv->power_manager = NULL; |
167 |
|
168 |
current_dialog = NULL; |
169 |
} |
170 |
|
171 |
static void |
172 |
panel_logout_response (PanelLogoutDialog *logout_dialog, |
173 |
guint response_id, |
174 |
gpointer data) |
175 |
{ |
176 |
PanelPowerManager *power_manager; |
177 |
|
178 |
power_manager = g_object_ref (logout_dialog->priv->power_manager); |
179 |
gtk_widget_destroy (GTK_WIDGET (logout_dialog)); |
180 |
|
181 |
switch (response_id) { |
182 |
case GTK_RESPONSE_CANCEL: |
183 |
break; |
184 |
case PANEL_LOGOUT_RESPONSE_LOGOUT: |
185 |
gdm_set_logout_action (GDM_LOGOUT_ACTION_NONE); |
186 |
panel_session_request_logout (); |
187 |
break; |
188 |
case PANEL_LOGOUT_RESPONSE_SWITCH_USER: |
189 |
gdm_new_login (); |
190 |
break; |
191 |
case PANEL_LOGOUT_RESPONSE_SHUTDOWN: |
192 |
gdm_set_logout_action (GDM_LOGOUT_ACTION_SHUTDOWN); |
193 |
panel_session_request_logout (); |
194 |
break; |
195 |
case PANEL_LOGOUT_RESPONSE_REBOOT: |
196 |
gdm_set_logout_action (GDM_LOGOUT_ACTION_REBOOT); |
197 |
panel_session_request_logout (); |
198 |
break; |
199 |
case PANEL_LOGOUT_RESPONSE_STD: |
200 |
if (panel_power_manager_can_hibernate (power_manager)) |
201 |
panel_power_manager_attempt_hibernate (power_manager); |
202 |
break; |
203 |
case PANEL_LOGOUT_RESPONSE_STR: |
204 |
if (panel_power_manager_can_suspend (power_manager)) |
205 |
panel_power_manager_attempt_suspend (power_manager); |
206 |
break; |
207 |
case GTK_RESPONSE_NONE: |
208 |
case GTK_RESPONSE_DELETE_EVENT: |
209 |
break; |
210 |
default: |
211 |
g_assert_not_reached (); |
212 |
} |
213 |
g_object_unref (power_manager); |
214 |
} |
215 |
|
216 |
static gboolean |
217 |
panel_logout_timeout (gpointer data) |
218 |
{ |
219 |
PanelLogoutDialog *logout_dialog; |
220 |
char *secondary_text; |
221 |
char *name; |
222 |
int seconds_to_show; |
223 |
|
224 |
logout_dialog = (PanelLogoutDialog *) data; |
225 |
|
226 |
if (!logout_dialog->priv->timeout) { |
227 |
gtk_dialog_response (GTK_DIALOG (logout_dialog), |
228 |
logout_dialog->priv->default_response); |
229 |
|
230 |
return FALSE; |
231 |
} |
232 |
|
233 |
if (logout_dialog->priv->timeout <= 30) |
234 |
seconds_to_show = logout_dialog->priv->timeout; |
235 |
else { |
236 |
seconds_to_show = (logout_dialog->priv->timeout/10) * 10; |
237 |
if (logout_dialog->priv->timeout % 10) |
238 |
seconds_to_show += 10; |
239 |
} |
240 |
|
241 |
switch (logout_dialog->priv->type) { |
242 |
case PANEL_LOGOUT_DIALOG_LOGOUT: |
243 |
secondary_text = ngettext ("You are currently logged in as " |
244 |
"\"%s\".\n" |
245 |
"You will be automatically logged " |
246 |
"out in %d second.", |
247 |
"You are currently logged in as " |
248 |
"\"%s\".\n" |
249 |
"You will be automatically logged " |
250 |
"out in %d seconds.", |
251 |
seconds_to_show); |
252 |
break; |
253 |
case PANEL_LOGOUT_DIALOG_SHUTDOWN: |
254 |
secondary_text = ngettext ("You are currently logged in as " |
255 |
"\"%s\".\n" |
256 |
"This system will be automatically " |
257 |
"shut down in %d second.", |
258 |
"You are currently logged in as " |
259 |
"\"%s\".\n" |
260 |
"This system will be automatically " |
261 |
"shut down in %d seconds.", |
262 |
seconds_to_show); |
263 |
break; |
264 |
default: |
265 |
g_assert_not_reached (); |
266 |
} |
267 |
|
268 |
name = g_locale_to_utf8 (g_get_real_name (), -1, NULL, NULL, NULL); |
269 |
if (!name || name[0] == '\0' || strcmp (name, "Unknown") == 0) |
270 |
name = g_locale_to_utf8 (g_get_user_name (), -1 , NULL, NULL, NULL); |
271 |
|
272 |
if (!name) |
273 |
name = g_strdup (g_get_user_name ()); |
274 |
|
275 |
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (logout_dialog), |
276 |
secondary_text, |
277 |
name, |
278 |
seconds_to_show, |
279 |
NULL); |
280 |
|
281 |
logout_dialog->priv->timeout--; |
282 |
|
283 |
g_free (name); |
284 |
|
285 |
return TRUE; |
286 |
} |
287 |
|
288 |
static void |
289 |
panel_logout_set_timeout (PanelLogoutDialog *logout_dialog) |
290 |
{ |
291 |
logout_dialog->priv->timeout = AUTOMATIC_ACTION_TIMEOUT; |
292 |
|
293 |
/* Sets the secondary text */ |
294 |
panel_logout_timeout (logout_dialog); |
295 |
|
296 |
if (logout_dialog->priv->timeout_id != 0) |
297 |
g_source_remove (logout_dialog->priv->timeout_id); |
298 |
|
299 |
logout_dialog->priv->timeout_id = g_timeout_add (1000, |
300 |
panel_logout_timeout, |
301 |
logout_dialog); |
302 |
} |
303 |
|
304 |
void |
305 |
panel_logout_new (PanelLogoutDialogType type, |
306 |
GdkScreen *screen, |
307 |
guint32 activate_time) |
308 |
{ |
309 |
PanelLogoutDialog *logout_dialog; |
310 |
char *icon_name; |
311 |
char *primary_text; |
312 |
|
313 |
if (current_dialog != NULL) { |
314 |
if (current_dialog->priv->type == type) { |
315 |
gtk_window_set_screen (GTK_WINDOW (current_dialog), |
316 |
screen); |
317 |
gtk_window_present_with_time (GTK_WINDOW (current_dialog), |
318 |
activate_time); |
319 |
panel_logout_set_timeout (current_dialog); |
320 |
//FIXME center the dialog on screen, and reset sticky and above_all? |
321 |
return; |
322 |
} else { |
323 |
gtk_widget_destroy (GTK_WIDGET (current_dialog)); |
324 |
} |
325 |
} |
326 |
|
327 |
logout_dialog = g_object_new (PANEL_TYPE_LOGOUT_DIALOG, NULL); |
328 |
current_dialog = logout_dialog; |
329 |
|
330 |
gtk_window_set_title (GTK_WINDOW (logout_dialog), ""); |
331 |
|
332 |
logout_dialog->priv->type = type; |
333 |
|
334 |
icon_name = NULL; |
335 |
primary_text = NULL; |
336 |
|
337 |
switch (type) { |
338 |
case PANEL_LOGOUT_DIALOG_LOGOUT: |
339 |
icon_name = PANEL_ICON_LOGOUT; |
340 |
primary_text = _("Log out of this system now?"); |
341 |
// FIXME need to verify that this response can be used |
342 |
logout_dialog->priv->default_response = PANEL_LOGOUT_DIALOG_LOGOUT; |
343 |
|
344 |
//FIXME is gdm running? |
345 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
346 |
_("_Switch User"), |
347 |
PANEL_LOGOUT_RESPONSE_SWITCH_USER); |
348 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
349 |
GTK_STOCK_CANCEL, |
350 |
GTK_RESPONSE_CANCEL); |
351 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
352 |
_("_Log Out"), |
353 |
PANEL_LOGOUT_RESPONSE_LOGOUT); |
354 |
break; |
355 |
case PANEL_LOGOUT_DIALOG_SHUTDOWN: |
356 |
icon_name = PANEL_ICON_SHUTDOWN; |
357 |
primary_text = _("Shut down this system now?"); |
358 |
|
359 |
logout_dialog->priv->default_response = PANEL_LOGOUT_RESPONSE_SHUTDOWN; |
360 |
if (panel_power_manager_can_suspend (logout_dialog->priv->power_manager)) |
361 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
362 |
_("S_uspend"), |
363 |
PANEL_LOGOUT_RESPONSE_STR); |
364 |
|
365 |
if (panel_power_manager_can_hibernate (logout_dialog->priv->power_manager)) |
366 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
367 |
_("_Hibernate"), |
368 |
PANEL_LOGOUT_RESPONSE_STD); |
369 |
|
370 |
if (gdm_supports_logout_action (GDM_LOGOUT_ACTION_REBOOT)) |
371 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
372 |
_("_Restart"), |
373 |
PANEL_LOGOUT_RESPONSE_REBOOT); |
374 |
|
375 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
376 |
GTK_STOCK_CANCEL, |
377 |
GTK_RESPONSE_CANCEL); |
378 |
|
379 |
if (gdm_supports_logout_action (GDM_LOGOUT_ACTION_SHUTDOWN)) |
380 |
gtk_dialog_add_button (GTK_DIALOG (logout_dialog), |
381 |
_("_Shut Down"), |
382 |
PANEL_LOGOUT_RESPONSE_SHUTDOWN); |
383 |
break; |
384 |
default: |
385 |
g_assert_not_reached (); |
386 |
} |
387 |
|
388 |
gtk_image_set_from_icon_name (GTK_IMAGE (GTK_MESSAGE_DIALOG (logout_dialog)->image), |
389 |
icon_name, GTK_ICON_SIZE_DIALOG); |
390 |
|
391 |
gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (logout_dialog)->label), |
392 |
primary_text); |
393 |
|
394 |
gtk_dialog_set_default_response (GTK_DIALOG (logout_dialog), |
395 |
logout_dialog->priv->default_response); |
396 |
|
397 |
panel_logout_set_timeout (logout_dialog); |
398 |
|
399 |
gtk_window_set_screen (GTK_WINDOW (logout_dialog), screen); |
400 |
gtk_widget_show (GTK_WIDGET (logout_dialog)); |
401 |
gdk_window_focus (GTK_WIDGET (current_dialog)->window, activate_time); |
402 |
} |