~charlesk/indicator-session/lp-1027952

1
2
3
4
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
A dialog to ask the user about the various logout options that
are available.

Copyright 2010 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib/gi18n.h>

#include "dbus-consolekit-manager.h"
#include "dialog.h"

/* Strings */

static const gchar * title_strings[LOGOUT_DIALOG_TYPE_CNT] = {
	/* LOGOUT_DIALOG_LOGOUT, */ 	NC_("title", "Log Out"),
	/* LOGOUT_DIALOG_RESTART, */	NC_("title", "Restart"),
	/* LOGOUT_DIALOG_SHUTDOWN, */	NC_("title", "Switch Off")
};

static const gchar * body_strings[LOGOUT_DIALOG_TYPE_CNT] = {
	/* LOGOUT_DIALOG_LOGOUT, */ 	N_("Are you sure you want to close all programs and log out of the computer?"),
	/* LOGOUT_DIALOG_RESTART, */	N_("Are you sure you want to close all programs and restart the computer?"),
	/* LOGOUT_DIALOG_SHUTDOWN, */	N_("Are you sure you want to close all programs and shut down the computer?")
};

static const gchar * button_strings[LOGOUT_DIALOG_TYPE_CNT] = {
	/* LOGOUT_DIALOG_LOGOUT, */ 	NC_("button", "Log Out"),
	/* LOGOUT_DIALOG_RESTART, */	NC_("button", "Restart"),
	/* LOGOUT_DIALOG_SHUTDOWN, */	NC_("button", "Switch Off")
};

/* TRANSLATORS: These strings have an ellipsis so that the user knows
   they are also going to get a password dialog to do the action. */
static const gchar * button_auth_strings[LOGOUT_DIALOG_TYPE_CNT] = {
	/* LOGOUT_DIALOG_LOGOUT, */ 	NC_("button auth", "Log Out"),
	/* LOGOUT_DIALOG_RESTART, */	NC_("button auth", "Restart…"),
	/* LOGOUT_DIALOG_SHUTDOWN, */	NC_("button auth", "Switch Off…")
};

/* TRANSLATORS: This button appears on the logout dialog when
   there are updates that require restart.  It will do a restart
   in place of a log out. */
static const gchar * restart_updates = N_("Restart Instead");
static const gchar * restart_auth = N_("Restart Instead…");
static const gchar * body_logout_update = N_("Some software updates won’t apply until the computer next restarts.");

static const gchar * icon_strings[LOGOUT_DIALOG_TYPE_CNT] = {
	/* LOGOUT_DIALOG_LOGOUT, */ 	"system-log-out",
	/* LOGOUT_DIALOG_RESTART, */	"system-restart",
	/* LOGOUT_DIALOG_SHUTDOWN, */	"system-shutdown"
};



typedef struct _LogoutDialogPrivate LogoutDialogPrivate;
struct _LogoutDialogPrivate {
	guint type;
};

#define LOGOUT_DIALOG_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), LOGOUT_DIALOG_TYPE, LogoutDialogPrivate))

static void logout_dialog_class_init (LogoutDialogClass *klass);
static void logout_dialog_init       (LogoutDialog *self);
static void logout_dialog_dispose    (GObject *object);
static void logout_dialog_finalize   (GObject *object);

G_DEFINE_TYPE (LogoutDialog, logout_dialog, GTK_TYPE_MESSAGE_DIALOG);

static void
logout_dialog_class_init (LogoutDialogClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (LogoutDialogPrivate));

	object_class->dispose = logout_dialog_dispose;
	object_class->finalize = logout_dialog_finalize;

	return;
}

static void
logout_dialog_init (LogoutDialog *self)
{

	return;
}

static void
logout_dialog_dispose (GObject *object)
{


	G_OBJECT_CLASS (logout_dialog_parent_class)->dispose (object);
	return;
}

static void
logout_dialog_finalize (GObject *object)
{


	G_OBJECT_CLASS (logout_dialog_parent_class)->finalize (object);
	return;
}

/* Checks for updates that would signal that a restart is
   required for them to apply */
static gboolean
check_restart_required (void)
{
	return g_file_test("/var/run/reboot-required", G_FILE_TEST_EXISTS);
}

/* Checks with console kit to see if we can do what we want */
static gboolean
ck_check_allowed (LogoutDialogType type)
{
	gboolean allowed = TRUE;

	ConsoleKitManager * ck_proxy = console_kit_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
	                                                                           G_DBUS_PROXY_FLAGS_NONE,
	                                                                           "org.freedesktop.ConsoleKit",
	                                                                           "/org/freedesktop/ConsoleKit/Manager",
	                                                                           NULL,
	                                                                           NULL);
	if (ck_proxy != NULL)
	{
		switch (type) {
		case LOGOUT_DIALOG_TYPE_RESTART:
			console_kit_manager_call_can_restart_sync (ck_proxy, &allowed, NULL, NULL);
			break;
		case LOGOUT_DIALOG_TYPE_SHUTDOWN:
			console_kit_manager_call_can_stop_sync (ck_proxy, &allowed, NULL, NULL);
			break;
		default:
			break;
		}

		g_object_unref(ck_proxy);
	}

	return allowed;
}

LogoutDialog *
logout_dialog_new (LogoutDialogType type)
{
	GtkWidget * image = gtk_image_new_from_icon_name(icon_strings[type], GTK_ICON_SIZE_DIALOG);
	gtk_widget_show(image);

	LogoutDialog * dialog = LOGOUT_DIALOG(g_object_new(LOGOUT_DIALOG_TYPE,
	                                      /* Window */
	                                      "icon-name", icon_strings[type],
	                                      "modal", TRUE,
	                                      "resizable", FALSE,
	                                      "title", g_dpgettext2 (NULL, "title", title_strings[type]),
	                                      "window-position", GTK_WIN_POS_CENTER_ALWAYS,
	                                      /* Message Dialog */
	                                      "buttons", GTK_BUTTONS_NONE,
	                                      "image", image,
	                                      "message-type", GTK_MESSAGE_OTHER,
	                                      "text", _(body_strings[type]),
	                                      NULL));

	gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);

	gboolean allowed = FALSE;
	if (type == LOGOUT_DIALOG_TYPE_LOG_OUT) {
		allowed = ck_check_allowed(LOGOUT_DIALOG_TYPE_RESTART);
	} else {
		allowed = ck_check_allowed(type);
	}

	gboolean restart_required = FALSE;
	if (type == LOGOUT_DIALOG_TYPE_LOG_OUT) {
		restart_required = check_restart_required();
	}

	const gchar * button_text;
	if (allowed) {
		button_text = g_dpgettext2 (NULL, "button", button_strings[type]);
	} else {
		button_text = g_dpgettext2 (NULL, "button auth", button_auth_strings[type]);
	}

	if (restart_required) {
		const gchar * restart_req;
		if (allowed) {
			restart_req = restart_updates;
		} else {
			restart_req = restart_auth;
		}

		g_object_set(dialog, "secondary-text", _(body_logout_update), NULL);

		gtk_dialog_add_buttons(GTK_DIALOG(dialog),
		                       _(restart_req), GTK_RESPONSE_HELP,
		                       _("Cancel"), GTK_RESPONSE_CANCEL,
		                       button_text, GTK_RESPONSE_OK,
		                       NULL);
	} else {
		gtk_dialog_add_buttons(GTK_DIALOG(dialog),
		                       _("Cancel"), GTK_RESPONSE_CANCEL,
		                       button_text, GTK_RESPONSE_OK,
		                       NULL);
	}

	if (type == LOGOUT_DIALOG_TYPE_SHUTDOWN) {
		const gchar * restart_text;
		restart_text = g_dpgettext2 (NULL, "button", button_strings[LOGOUT_DIALOG_TYPE_RESTART]);
		gtk_dialog_add_button (GTK_DIALOG(dialog), restart_text, GTK_RESPONSE_HELP);
	}

	gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);

        /* The following  is a workaround to fix an issue in GtkMessageDialog
           in which the user can tab through the text in addition to
           the buttons. */
        GtkWidget *message_area = gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog));
        GList *children = gtk_container_get_children(GTK_CONTAINER(message_area));
        GList *l;

        for (l = children; l != NULL; l = g_list_next (l))
        {
                GtkWidget *child = l->data;
                gtk_widget_set_can_focus(child, FALSE);
        }

        g_list_free (children);

	return dialog;
}