~charlesk/indicator-session/use-gslice

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Charles Kerr, Lars Uebernickel
  • Date: 2013-07-12 14:58:31 UTC
  • mfrom: (384.2.112 ng-login1)
  • Revision ID: tarmac-20130712145831-n2bqrachsd9qa90n
This is the GMenu, login1 version of indicator-session.

This resubmission removes the prerequisite branch because the entire diff is contained in this ng-login1 branch.

Approved by PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
A small wrapper utility to load indicators and put them as menu items
3
 
into the gnome-panel using it's applet interface.
4
 
 
5
 
Copyright 2009 Canonical Ltd.
6
 
 
7
 
Authors:
8
 
    Ted Gould <ted@canonical.com>
9
 
    Christoph Korn <c_korn@gmx.de>
10
 
 
11
 
This program is free software: you can redistribute it and/or modify it
12
 
under the terms of the GNU General Public License version 3, as published
13
 
by the Free Software Foundation.
14
 
 
15
 
This program is distributed in the hope that it will be useful, but
16
 
WITHOUT ANY WARRANTY; without even the implied warranties of
17
 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
18
 
PURPOSE.  See the GNU General Public License for more details.
19
 
 
20
 
You should have received a copy of the GNU General Public License along
21
 
with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
*/
23
 
 
24
 
#include "config.h"
25
 
 
26
 
#include <locale.h>
27
 
#include <glib.h>
28
 
#include <glib/gi18n.h> /* textdomain(), bindtextdomain() */
29
 
#include <gtk/gtk.h>
30
 
#include "dialog.h"
31
 
#include "shared-names.h"
32
 
 
33
 
static GVariant *
34
 
call_logind (const gchar *method, GVariant *parameters, GError **error)
35
 
{
36
 
        GDBusConnection * bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, error);
37
 
        if (!bus)
38
 
        {
39
 
                g_variant_unref (parameters);
40
 
                return NULL;
41
 
        }
42
 
 
43
 
        GVariant *result = g_dbus_connection_call_sync(bus,
44
 
                                                       "org.freedesktop.login1",
45
 
                                                       "/org/freedesktop/login1",
46
 
                                                       "org.freedesktop.login1.Manager",
47
 
                                                       method,
48
 
                                                       parameters,
49
 
                                                       NULL,
50
 
                                                       G_DBUS_CALL_FLAGS_NONE,
51
 
                                                       -1,
52
 
                                                       NULL,
53
 
                                                       error);
54
 
        g_object_unref (bus);
55
 
 
56
 
        return result;
57
 
}
58
 
 
59
 
static void
60
 
logind_fallback (LogoutDialogType action)
61
 
{
62
 
        GError * error = NULL;
63
 
        GVariant *result = NULL;
64
 
 
65
 
        g_debug("Falling back to using logind for action");
66
 
 
67
 
        switch (action) {
68
 
                case LOGOUT_DIALOG_TYPE_LOG_OUT:
69
 
                        g_warning("Unable to fallback to logind for logout as it's a session issue.  We need some sort of session handler.");
70
 
                        break;
71
 
                case LOGOUT_DIALOG_TYPE_SHUTDOWN:
72
 
                        g_debug("Telling logind to 'PowerOff'");
73
 
                        result = call_logind ("PowerOff", g_variant_new ("(b)", FALSE), &error);
74
 
                        break;
75
 
                case LOGOUT_DIALOG_TYPE_RESTART:
76
 
                        g_debug("Telling logind to 'Reboot'");
77
 
                        result = call_logind ("Reboot", g_variant_new ("(b)", FALSE), &error);
78
 
                        break;
79
 
                default:
80
 
                        g_warning("Unknown action");
81
 
                        break;
82
 
        }
83
 
 
84
 
        if (!result) {
85
 
                if (error != NULL) {
86
 
                        g_warning ("logind action failed: %s", error->message);
87
 
                } else {
88
 
                        g_warning ("logind action failed: unknown error");
89
 
                }
90
 
        }
91
 
        else
92
 
                g_variant_unref (result);
93
 
        g_clear_error (&error);
94
 
 
95
 
        return;
96
 
}
97
 
 
98
 
static GVariant *
99
 
call_gnome_session (const gchar *method, GVariant *parameters, GError **error)
100
 
{
101
 
        GDBusConnection * bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, error);
102
 
        if (!bus)
103
 
        {
104
 
                g_variant_unref (parameters);
105
 
                return NULL;
106
 
        }
107
 
  
108
 
        GVariant *result = g_dbus_connection_call_sync(bus,
109
 
                                                       "org.gnome.SessionManager",
110
 
                                                       "/org/gnome/SessionManager",
111
 
                                                       "org.gnome.SessionManager",
112
 
                                                       method,
113
 
                                                       parameters,
114
 
                                                       NULL,
115
 
                                                       G_DBUS_CALL_FLAGS_NONE,
116
 
                                                       G_MAXINT,
117
 
                                                       NULL,
118
 
                                                       error);
119
 
        g_object_unref (bus);
120
 
 
121
 
        return result;
122
 
}
123
 
 
124
 
static void
125
 
session_action (LogoutDialogType action)
126
 
{
127
 
        GError * error = NULL;
128
 
        GVariant *result = NULL;
129
 
 
130
 
        if (action == LOGOUT_DIALOG_TYPE_LOG_OUT) {
131
 
                g_debug("Asking Session manager to 'Logout'");
132
 
                result = call_gnome_session ("Logout", g_variant_new ("(u)", 1), &error);
133
 
        } else if (action == LOGOUT_DIALOG_TYPE_SHUTDOWN) {
134
 
                g_debug("Asking Session manager to 'RequestShutdown'");
135
 
                result = call_gnome_session ("RequestShutdown", g_variant_new ("()"), &error);
136
 
        } else if (action == LOGOUT_DIALOG_TYPE_RESTART) {
137
 
                g_debug("Asking Session manager to 'RequestReboot'");
138
 
                result = call_gnome_session ("RequestReboot", g_variant_new ("()"), &error);
139
 
        } else {
140
 
                g_warning ("Unknown session action");
141
 
        }
142
 
        
143
 
        if (!result) {
144
 
                if (error != NULL) {
145
 
                        g_warning ("SessionManager action failed: %s", error->message);
146
 
                } else {
147
 
                        g_warning ("SessionManager action failed: unknown error");
148
 
                }
149
 
 
150
 
                logind_fallback(action);
151
 
        }
152
 
        else
153
 
                g_variant_unref (result);
154
 
        g_clear_error (&error);
155
 
        
156
 
        return;
157
 
}       
158
 
 
159
 
static LogoutDialogType type = LOGOUT_DIALOG_TYPE_LOG_OUT;
160
 
 
161
 
static gboolean
162
 
option_logout (const gchar * arg, const gchar * value, gpointer data, GError * error)
163
 
{
164
 
        type = LOGOUT_DIALOG_TYPE_LOG_OUT;
165
 
        g_debug("Dialog type: logout");
166
 
        return TRUE;
167
 
}
168
 
 
169
 
static gboolean
170
 
option_shutdown (const gchar * arg, const gchar * value, gpointer data, GError * error)
171
 
{
172
 
        type = LOGOUT_DIALOG_TYPE_SHUTDOWN;
173
 
        g_debug("Dialog type: shutdown");
174
 
        return TRUE;
175
 
}
176
 
 
177
 
static gboolean
178
 
option_restart (const gchar * arg, const gchar * value, gpointer data, GError * error)
179
 
{
180
 
        type = LOGOUT_DIALOG_TYPE_RESTART;
181
 
        g_debug("Dialog type: restart");
182
 
        return TRUE;
183
 
}
184
 
 
185
 
static GOptionEntry options[] = {
186
 
        {"logout",     'l',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_logout,   "Log out of the current session",   NULL},
187
 
        {"shutdown",   's',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_shutdown, "Switch off the entire system",     NULL},
188
 
        {"restart",    'r',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_restart,  "Restart the system",               NULL},
189
 
 
190
 
        {NULL}
191
 
};
192
 
 
193
 
static gboolean
194
 
suppress_confirmations (void)
195
 
{
196
 
  GSettings * s = g_settings_new (SESSION_SCHEMA);
197
 
  const gboolean suppress = g_settings_get_boolean (s, SUPPRESS_KEY);
198
 
  g_clear_object (&s);
199
 
  return suppress;
200
 
}
201
 
 
202
 
 
203
 
 
204
 
int
205
 
main (int argc, char * argv[])
206
 
{
207
 
        gtk_init(&argc, &argv);
208
 
 
209
 
        /* Setting up i18n and gettext.  Apparently, we need
210
 
           all of these. */
211
 
        setlocale (LC_ALL, "");
212
 
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
213
 
        textdomain (GETTEXT_PACKAGE);
214
 
 
215
 
        GError * error = NULL;
216
 
        GOptionContext * context = g_option_context_new(" - logout of the current session");
217
 
        g_option_context_add_main_entries(context, options, "gtk-logout-helper");
218
 
        g_option_context_add_group(context, gtk_get_option_group(TRUE));
219
 
        g_option_context_set_help_enabled(context, TRUE);
220
 
 
221
 
        if (!g_option_context_parse(context, &argc, &argv, &error)) {
222
 
                g_debug("Option parsing failed: %s", error->message);
223
 
                g_error_free(error);
224
 
                return 1;
225
 
        }
226
 
 
227
 
        /* Init some theme/icon stuff */
228
 
        gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
229
 
                                          INDICATOR_ICONS_DIR);
230
 
 
231
 
        GtkWidget * dialog = NULL;
232
 
        if (!suppress_confirmations()) {
233
 
                g_debug("Showing dialog to ask for user confirmation");
234
 
                dialog = GTK_WIDGET(logout_dialog_new(type));
235
 
        }
236
 
 
237
 
        if (dialog != NULL) {
238
 
                GtkResponseType response = gtk_dialog_run(GTK_DIALOG(dialog));
239
 
                gtk_widget_hide(dialog);
240
 
 
241
 
                if (response == GTK_RESPONSE_OK) {
242
 
                        g_debug("Dialog return response: 'okay'");
243
 
                } else if (response == GTK_RESPONSE_HELP) {
244
 
                        g_debug("Dialog return response: 'help'");
245
 
                } else {
246
 
                        g_debug("Dialog return response: %d", response);
247
 
                }
248
 
 
249
 
                if (response == GTK_RESPONSE_HELP) {
250
 
                        type = LOGOUT_DIALOG_TYPE_RESTART;
251
 
                        response = GTK_RESPONSE_OK;
252
 
                }
253
 
 
254
 
                if (response != GTK_RESPONSE_OK) {
255
 
                        g_debug("Final response was not okay, quiting");
256
 
                        return 0;
257
 
                }
258
 
        }
259
 
 
260
 
        session_action(type);
261
 
        g_debug("Finished action, quiting");
262
 
 
263
 
        return 0;
264
 
}