File: | tools/cafe-session-save.c |
Warning: | line 251, column 3 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
2 | * save-session.c - Small program to talk to session manager. |
3 | |
4 | Copyright (C) 1998 Tom Tromey |
5 | Copyright (C) 2008 Red Hat, Inc. |
6 | |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2, or (at your option) |
10 | any later version. |
11 | |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
20 | 02110-1301, USA. |
21 | */ |
22 | |
23 | #include <config.h> |
24 | |
25 | #include <unistd.h> |
26 | #include <stdlib.h> |
27 | #include <stdio.h> |
28 | #include <string.h> |
29 | |
30 | #include <glib/gi18n.h> |
31 | #include <ctk/ctk.h> |
32 | |
33 | #define CSM_SERVICE_DBUS"org.gnome.SessionManager" "org.gnome.SessionManager" |
34 | #define CSM_PATH_DBUS"/org/gnome/SessionManager" "/org/gnome/SessionManager" |
35 | #define CSM_INTERFACE_DBUS"org.gnome.SessionManager" "org.gnome.SessionManager" |
36 | |
37 | #define CSM_SERVICE_DBUS_OLD"org.cafe.SessionManager" "org.cafe.SessionManager" |
38 | #define CSM_PATH_DBUS_OLD"/org/cafe/SessionManager" "/org/cafe/SessionManager" |
39 | #define CSM_INTERFACE_DBUS_OLD"org.cafe.SessionManager" "org.cafe.SessionManager" |
40 | |
41 | enum { |
42 | CSM_LOGOUT_MODE_NORMAL = 0, |
43 | CSM_LOGOUT_MODE_NO_CONFIRMATION, |
44 | CSM_LOGOUT_MODE_FORCE |
45 | }; |
46 | |
47 | /* True if killing. This is deprecated, but we keep it for compatibility |
48 | * reasons. */ |
49 | static gboolean kill_session = FALSE(0); |
50 | |
51 | /* The real options that should be used now. They are not ambiguous. */ |
52 | static gboolean logout = FALSE(0); |
53 | static gboolean force_logout = FALSE(0); |
54 | static gboolean logout_dialog = FALSE(0); |
55 | static gboolean shutdown_dialog = FALSE(0); |
56 | |
57 | /* True if we should use dialog boxes */ |
58 | static gboolean show_error_dialogs = FALSE(0); |
59 | |
60 | /* True if we should do the requested action without confirmation */ |
61 | static gboolean no_interaction = FALSE(0); |
62 | |
63 | static char* session_name = NULL((void*)0); |
64 | |
65 | static GOptionEntry options[] = { |
66 | {"logout", '\0', 0, G_OPTION_ARG_NONE, &logout, N_("Log out")("Log out"), NULL((void*)0)}, |
67 | {"force-logout", '\0', 0, G_OPTION_ARG_NONE, &force_logout, N_("Log out, ignoring any existing inhibitors")("Log out, ignoring any existing inhibitors"), NULL((void*)0)}, |
68 | {"logout-dialog", '\0', 0, G_OPTION_ARG_NONE, &logout_dialog, N_("Show logout dialog")("Show logout dialog"), NULL((void*)0)}, |
69 | {"shutdown-dialog", '\0', 0, G_OPTION_ARG_NONE, &shutdown_dialog, N_("Show shutdown dialog")("Show shutdown dialog"), NULL((void*)0)}, |
70 | {"gui", '\0', 0, G_OPTION_ARG_NONE, &show_error_dialogs, N_("Use dialog boxes for errors")("Use dialog boxes for errors"), NULL((void*)0)}, |
71 | /* deprecated options */ |
72 | {"session-name", 's', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &session_name, N_("Set the current session name")("Set the current session name"), N_("NAME")("NAME")}, |
73 | {"kill", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &kill_session, N_("Kill session")("Kill session"), NULL((void*)0)}, |
74 | {"silent", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &no_interaction, N_("Do not require confirmation")("Do not require confirmation"), NULL((void*)0)}, |
75 | {NULL((void*)0)} |
76 | }; |
77 | |
78 | static void display_error(const char* message) |
79 | { |
80 | if (show_error_dialogs && !no_interaction) |
81 | { |
82 | CtkWidget* dialog = ctk_message_dialog_new(NULL((void*)0), 0, CTK_MESSAGE_ERROR, CTK_BUTTONS_CLOSE, "%s", message); |
83 | |
84 | ctk_window_set_default_icon_name ("dialog-error"); |
85 | |
86 | ctk_dialog_run(CTK_DIALOG(dialog)((((CtkDialog*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((dialog)), ((ctk_dialog_get_type ()))))))); |
87 | ctk_widget_destroy(dialog); |
88 | } |
89 | else |
90 | { |
91 | g_printerr("%s\n", message); |
92 | } |
93 | } |
94 | |
95 | static GDBusProxy* get_sm_proxy(void) |
96 | { |
97 | GError *error = NULL((void*)0); |
98 | GDBusProxy *sm_proxy = NULL((void*)0); |
99 | |
100 | sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, |
101 | G_DBUS_PROXY_FLAGS_NONE, |
102 | NULL((void*)0), |
103 | CSM_SERVICE_DBUS"org.gnome.SessionManager", |
104 | CSM_PATH_DBUS"/org/gnome/SessionManager", |
105 | CSM_INTERFACE_DBUS"org.gnome.SessionManager", |
106 | NULL((void*)0), |
107 | &error); |
108 | if (sm_proxy == NULL((void*)0)) |
109 | { |
110 | g_warning ("Couldn't create DBus proxy: %s", error->message); |
111 | g_error_free (error); |
112 | |
113 | /* Try the old name - for the case when we've just upgraded from 1.10 |
114 | * so the old m-s-m is currently running */ |
115 | sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, |
116 | G_DBUS_PROXY_FLAGS_NONE, |
117 | NULL((void*)0), |
118 | CSM_SERVICE_DBUS_OLD"org.cafe.SessionManager", |
119 | CSM_PATH_DBUS_OLD"/org/cafe/SessionManager", |
120 | CSM_INTERFACE_DBUS_OLD"org.cafe.SessionManager", |
121 | NULL((void*)0), |
122 | NULL((void*)0)); |
123 | if (sm_proxy == NULL((void*)0)) |
124 | { |
125 | /* Okay, it wasn't the upgrade case, so now we can give up. */ |
126 | display_error (_("Could not connect to the session manager")gettext ("Could not connect to the session manager")); |
127 | return NULL((void*)0); |
128 | } |
129 | } |
130 | |
131 | return sm_proxy; |
132 | } |
133 | |
134 | static void do_logout(unsigned int mode) |
135 | { |
136 | GDBusProxy* sm_proxy; |
137 | GError* error; |
138 | GVariant *ret; |
139 | |
140 | sm_proxy = get_sm_proxy (); |
141 | |
142 | if (sm_proxy == NULL((void*)0)) |
143 | { |
144 | return; |
145 | } |
146 | |
147 | error = NULL((void*)0); |
148 | ret = g_dbus_proxy_call_sync (sm_proxy, "Logout", |
149 | g_variant_new ("(u)", mode), |
150 | G_DBUS_CALL_FLAGS_NONE, |
151 | -1, |
152 | NULL((void*)0), |
153 | &error); |
154 | |
155 | if (ret == NULL((void*)0)) |
156 | { |
157 | g_warning ("Failed to call logout: %s", error->message); |
158 | g_error_free (error); |
159 | } else { |
160 | g_variant_unref (ret); |
161 | } |
162 | |
163 | if (sm_proxy != NULL((void*)0)) |
164 | { |
165 | g_object_unref (sm_proxy); |
166 | } |
167 | } |
168 | |
169 | static void do_shutdown_dialog(void) |
170 | { |
171 | GDBusProxy* sm_proxy; |
172 | GError* error; |
173 | GVariant *ret; |
174 | |
175 | sm_proxy = get_sm_proxy (); |
176 | |
177 | if (sm_proxy == NULL((void*)0)) |
178 | { |
179 | return; |
180 | } |
181 | |
182 | error = NULL((void*)0); |
183 | ret = g_dbus_proxy_call_sync (sm_proxy, "Shutdown", |
184 | g_variant_new ("()"), |
185 | G_DBUS_CALL_FLAGS_NONE, |
186 | -1, |
187 | NULL((void*)0), |
188 | &error); |
189 | if (ret == NULL((void*)0)) |
190 | { |
191 | g_warning ("Failed to call shutdown: %s", error->message); |
192 | g_error_free (error); |
193 | } else { |
194 | g_variant_unref (ret); |
195 | } |
196 | |
197 | if (sm_proxy != NULL((void*)0)) |
198 | { |
199 | g_object_unref (sm_proxy); |
200 | } |
201 | } |
202 | |
203 | int main(int argc, char* argv[]) |
204 | { |
205 | GError* error; |
206 | int conflicting_options; |
207 | |
208 | /* Initialize the i18n stuff */ |
209 | bindtextdomain(GETTEXT_PACKAGE"cafe-session-manager", LOCALE_DIR"/usr/local/share/locale"); |
210 | bind_textdomain_codeset(GETTEXT_PACKAGE"cafe-session-manager", "UTF-8"); |
211 | textdomain(GETTEXT_PACKAGE"cafe-session-manager"); |
212 | |
213 | error = NULL((void*)0); |
214 | |
215 | if (!ctk_init_with_args(&argc, &argv, NULL((void*)0), options, NULL((void*)0), &error)) |
216 | { |
217 | g_warning("Unable to start: %s", error->message); |
218 | g_error_free(error); |
219 | exit(1); |
220 | } |
221 | |
222 | conflicting_options = 0; |
223 | |
224 | if (kill_session) |
225 | { |
226 | conflicting_options++; |
227 | } |
228 | |
229 | if (logout) |
230 | { |
231 | conflicting_options++; |
232 | } |
233 | |
234 | if (force_logout) |
235 | { |
236 | conflicting_options++; |
237 | } |
238 | |
239 | if (logout_dialog) |
240 | { |
241 | conflicting_options++; |
242 | } |
243 | |
244 | if (shutdown_dialog) |
245 | { |
246 | conflicting_options++; |
247 | } |
248 | |
249 | if (conflicting_options > 1) |
250 | { |
251 | display_error(_("Program called with conflicting options")gettext ("Program called with conflicting options")); |
This statement is never executed | |
252 | } |
253 | |
254 | if (kill_session) |
255 | { |
256 | if (no_interaction) |
257 | { |
258 | force_logout = TRUE(!(0)); |
259 | } |
260 | else |
261 | { |
262 | logout_dialog = TRUE(!(0)); |
263 | } |
264 | } |
265 | |
266 | if (logout) |
267 | { |
268 | do_logout(CSM_LOGOUT_MODE_NO_CONFIRMATION); |
269 | } |
270 | else if (force_logout) |
271 | { |
272 | do_logout(CSM_LOGOUT_MODE_FORCE); |
273 | } |
274 | else if (logout_dialog) |
275 | { |
276 | do_logout(CSM_LOGOUT_MODE_NORMAL); |
277 | } |
278 | else if (shutdown_dialog) |
279 | { |
280 | do_shutdown_dialog(); |
281 | } |
282 | |
283 | return 0; |
284 | } |