~mefrio-g/+junk/indicator-session-pantheon-shutdown

« back to all changes in this revision

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

  • Committer: cody at elementaryos
  • Date: 2012-12-10 00:13:38 UTC
  • Revision ID: cody@elementaryos.org-20121210001338-379sxx4jo6r003d6
Initial import, version 0.3.96-0ubuntu1

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
#include <locale.h>
 
26
#include <glib.h>
 
27
#include <gtk/gtk.h>
 
28
#include <dbus/dbus-glib.h>
 
29
#include "dialog.h"
 
30
#include "settings-helper.h"
 
31
 
 
32
static void
 
33
consolekit_fallback (LogoutDialogType action)
 
34
{
 
35
        g_debug("Falling back to using ConsoleKit for action");
 
36
 
 
37
        DBusGConnection * sbus = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
 
38
        g_return_if_fail(sbus != NULL); /* worst case */
 
39
        DBusGProxy * proxy = dbus_g_proxy_new_for_name(sbus, "org.freedesktop.ConsoleKit",
 
40
                                                             "/org/freedesktop/ConsoleKit/Manager",
 
41
                                                             "org.freedesktop.ConsoleKit.Manager");
 
42
 
 
43
        if (proxy == NULL) {
 
44
                g_warning("Unable to get consolekit proxy");
 
45
                return;
 
46
        }
 
47
 
 
48
        GError * error = NULL;
 
49
 
 
50
        switch (action) {
 
51
                case LOGOUT_DIALOG_TYPE_LOG_OUT:
 
52
                        g_warning("Unable to fallback to ConsoleKit for logout as it's a session issue.  We need some sort of session handler.");
 
53
                        break;
 
54
                case LOGOUT_DIALOG_TYPE_SHUTDOWN:
 
55
                        g_debug("Telling ConsoleKit to 'Stop'");
 
56
                        dbus_g_proxy_call(proxy,
 
57
                                          "Stop",
 
58
                                          &error,
 
59
                                          G_TYPE_INVALID,
 
60
                                          G_TYPE_INVALID);
 
61
                        break;
 
62
                case LOGOUT_DIALOG_TYPE_RESTART:
 
63
                        g_debug("Telling ConsoleKit to 'Restart'");
 
64
                        dbus_g_proxy_call(proxy,
 
65
                                          "Restart",
 
66
                                          &error,
 
67
                                          G_TYPE_INVALID,
 
68
                                          G_TYPE_INVALID);
 
69
                        break;
 
70
                default:
 
71
                        g_warning("Unknown action");
 
72
                        break;
 
73
        }
 
74
 
 
75
        g_object_unref(proxy);
 
76
 
 
77
        if (error != NULL) {
 
78
                g_warning("Unable to signal ConsoleKit");
 
79
                g_error_free(error);
 
80
        }
 
81
 
 
82
        return;
 
83
}
 
84
 
 
85
static void
 
86
session_action (LogoutDialogType action)
 
87
{
 
88
        DBusGConnection * sbus;
 
89
        DBusGProxy * sm_proxy;
 
90
        GError * error = NULL;
 
91
        gboolean res = FALSE;
 
92
        
 
93
        sbus = dbus_g_bus_get(DBUS_BUS_SESSION, NULL); 
 
94
        if (sbus == NULL) {
 
95
                g_warning("Unable to get DBus session bus.");
 
96
                return;
 
97
        }
 
98
        sm_proxy = dbus_g_proxy_new_for_name_owner (sbus,
 
99
                                                "org.gnome.SessionManager",
 
100
                                                "/org/gnome/SessionManager",
 
101
                                                "org.gnome.SessionManager",
 
102
                                                        &error);
 
103
        if (sm_proxy == NULL) {
 
104
                g_warning("Unable to get DBus proxy to SessionManager interface: %s", error->message);
 
105
                g_error_free(error);
 
106
 
 
107
                consolekit_fallback(action);
 
108
                return;
 
109
        }               
 
110
        
 
111
        g_clear_error (&error);
 
112
        
 
113
        if (action == LOGOUT_DIALOG_TYPE_LOG_OUT) {
 
114
                g_debug("Asking Session manager to 'Logout'");
 
115
                res = dbus_g_proxy_call_with_timeout (sm_proxy, "Logout", INT_MAX, &error, 
 
116
                                                                                          G_TYPE_UINT, 1, G_TYPE_INVALID, G_TYPE_INVALID);
 
117
        } else if (action == LOGOUT_DIALOG_TYPE_SHUTDOWN) {
 
118
                g_debug("Asking Session manager to 'RequestShutdown'");
 
119
                res = dbus_g_proxy_call_with_timeout (sm_proxy, "RequestShutdown", INT_MAX, &error, 
 
120
                                                                                          G_TYPE_INVALID, G_TYPE_INVALID);
 
121
        } else if (action == LOGOUT_DIALOG_TYPE_RESTART) {
 
122
                g_debug("Asking Session manager to 'RequestReboot'");
 
123
                res = dbus_g_proxy_call_with_timeout (sm_proxy, "RequestReboot", INT_MAX, &error, 
 
124
                                                                                          G_TYPE_INVALID, G_TYPE_INVALID);
 
125
        } else {
 
126
                g_warning ("Unknown session action");
 
127
        }
 
128
        
 
129
        if (!res) {
 
130
                if (error != NULL) {
 
131
                        g_warning ("SessionManager action failed: %s", error->message);
 
132
                } else {
 
133
                        g_warning ("SessionManager action failed: unknown error");
 
134
                }
 
135
 
 
136
                consolekit_fallback(action);
 
137
        }
 
138
        
 
139
        g_object_unref(sm_proxy);
 
140
        
 
141
        if (error != NULL) {
 
142
                g_error_free(error);
 
143
        }
 
144
        
 
145
        return;
 
146
}       
 
147
 
 
148
static LogoutDialogType type = LOGOUT_DIALOG_TYPE_LOG_OUT;
 
149
 
 
150
static gboolean
 
151
option_logout (const gchar * arg, const gchar * value, gpointer data, GError * error)
 
152
{
 
153
        type = LOGOUT_DIALOG_TYPE_LOG_OUT;
 
154
        g_debug("Dialog type: logout");
 
155
        return TRUE;
 
156
}
 
157
 
 
158
static gboolean
 
159
option_shutdown (const gchar * arg, const gchar * value, gpointer data, GError * error)
 
160
{
 
161
        type = LOGOUT_DIALOG_TYPE_SHUTDOWN;
 
162
        g_debug("Dialog type: shutdown");
 
163
        return TRUE;
 
164
}
 
165
 
 
166
static gboolean
 
167
option_restart (const gchar * arg, const gchar * value, gpointer data, GError * error)
 
168
{
 
169
        type = LOGOUT_DIALOG_TYPE_RESTART;
 
170
        g_debug("Dialog type: restart");
 
171
        return TRUE;
 
172
}
 
173
 
 
174
static GOptionEntry options[] = {
 
175
        {"logout",     'l',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_logout,   "Log out of the current session",   NULL},
 
176
        {"shutdown",   's',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_shutdown, "Switch off the entire system",     NULL},
 
177
        {"restart",    'r',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_restart,  "Restart the system",               NULL},
 
178
 
 
179
        {NULL}
 
180
};
 
181
 
 
182
int
 
183
main (int argc, char * argv[])
 
184
{
 
185
        gtk_init(&argc, &argv);
 
186
 
 
187
        /* Setting up i18n and gettext.  Apparently, we need
 
188
           all of these. */
 
189
        setlocale (LC_ALL, "");
 
190
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
 
191
        textdomain (GETTEXT_PACKAGE);
 
192
 
 
193
        GError * error = NULL;
 
194
        GOptionContext * context = g_option_context_new(" - logout of the current session");
 
195
        g_option_context_add_main_entries(context, options, "gtk-logout-helper");
 
196
        g_option_context_add_group(context, gtk_get_option_group(TRUE));
 
197
        g_option_context_set_help_enabled(context, TRUE);
 
198
 
 
199
        if (!g_option_context_parse(context, &argc, &argv, &error)) {
 
200
                g_debug("Option parsing failed: %s", error->message);
 
201
                g_error_free(error);
 
202
                return 1;
 
203
        }
 
204
 
 
205
        /* Init some theme/icon stuff */
 
206
        gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
 
207
                                          INDICATOR_ICONS_DIR);
 
208
 
 
209
        GtkWidget * dialog = NULL;
 
210
        if (!supress_confirmations()) {
 
211
                g_debug("Showing dialog to ask for user confirmation");
 
212
                dialog = GTK_WIDGET(logout_dialog_new(type));
 
213
        }
 
214
 
 
215
        if (dialog != NULL) {
 
216
                GtkResponseType response = gtk_dialog_run(GTK_DIALOG(dialog));
 
217
                gtk_widget_hide(dialog);
 
218
 
 
219
                if (response == GTK_RESPONSE_OK) {
 
220
                        g_debug("Dialog return response: 'okay'");
 
221
                } else if (response == GTK_RESPONSE_HELP) {
 
222
                        g_debug("Dialog return response: 'help'");
 
223
                } else {
 
224
                        g_debug("Dialog return response: %d", response);
 
225
                }
 
226
 
 
227
                if (response == GTK_RESPONSE_HELP) {
 
228
                        type = LOGOUT_DIALOG_TYPE_RESTART;
 
229
                        response = GTK_RESPONSE_OK;
 
230
                }
 
231
 
 
232
                if (response != GTK_RESPONSE_OK) {
 
233
                        g_debug("Final response was not okay, quiting");
 
234
                        return 0;
 
235
                }
 
236
        }
 
237
 
 
238
        session_action(type);
 
239
        g_debug("Finished action, quiting");
 
240
 
 
241
        return 0;
 
242
}