summaryrefslogtreecommitdiff
path: root/tools/mate-session-save.c
blob: 3b76373f473e6b8b7f8f6884cf8d73f88e860971 (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 * save-session.c - Small program to talk to session manager.

   Copyright (C) 1998 Tom Tromey
   Copyright (C) 2008 Red Hat, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY 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, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#include <config.h>

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>

#define GSM_SERVICE_DBUS "org.gnome.SessionManager"
#define GSM_PATH_DBUS "/org/gnome/SessionManager"
#define GSM_INTERFACE_DBUS "org.gnome.SessionManager"

#define GSM_SERVICE_DBUS_OLD "org.mate.SessionManager"
#define GSM_PATH_DBUS_OLD "/org/mate/SessionManager"
#define GSM_INTERFACE_DBUS_OLD "org.mate.SessionManager"

enum {
	GSM_LOGOUT_MODE_NORMAL = 0,
	GSM_LOGOUT_MODE_NO_CONFIRMATION,
	GSM_LOGOUT_MODE_FORCE
};

/* True if killing. This is deprecated, but we keep it for compatibility
 * reasons. */
static gboolean kill_session = FALSE;

/* The real options that should be used now. They are not ambiguous. */
static gboolean logout = FALSE;
static gboolean force_logout = FALSE;
static gboolean logout_dialog = FALSE;
static gboolean shutdown_dialog = FALSE;

/* True if we should use dialog boxes */
static gboolean show_error_dialogs = FALSE;

/* True if we should do the requested action without confirmation */
static gboolean no_interaction = FALSE;

static char* session_name = NULL;

static GOptionEntry options[] = {
	{"logout", '\0', 0, G_OPTION_ARG_NONE, &logout, N_("Log out"), NULL},
	{"force-logout", '\0', 0, G_OPTION_ARG_NONE, &force_logout, N_("Log out, ignoring any existing inhibitors"), NULL},
	{"logout-dialog", '\0', 0, G_OPTION_ARG_NONE, &logout_dialog, N_("Show logout dialog"), NULL},
	{"shutdown-dialog", '\0', 0, G_OPTION_ARG_NONE, &shutdown_dialog, N_("Show shutdown dialog"), NULL},
	{"gui",  '\0', 0, G_OPTION_ARG_NONE, &show_error_dialogs, N_("Use dialog boxes for errors"), NULL},
	/* deprecated options */
	{"session-name", 's', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &session_name, N_("Set the current session name"), N_("NAME")},
	{"kill", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &kill_session, N_("Kill session"), NULL},
	{"silent", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &no_interaction, N_("Do not require confirmation"), NULL},
	{NULL}
};

static void display_error(const char* message)
{
	if (show_error_dialogs && !no_interaction)
	{
		GtkWidget* dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", message);

		gtk_window_set_default_icon_name ("dialog-error");

		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
	}
	else
	{
		g_printerr("%s\n", message);
	}
}

static GDBusProxy* get_sm_proxy(void)
{
	GError *error = NULL;
	GDBusProxy *sm_proxy = NULL;

	sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
	                                          G_DBUS_PROXY_FLAGS_NONE,
	                                          NULL,
	                                          GSM_SERVICE_DBUS,
	                                          GSM_PATH_DBUS,
	                                          GSM_INTERFACE_DBUS,
	                                          NULL,
	                                          &error);
	if (sm_proxy == NULL)
	{
		g_warning ("Couldn't create DBus proxy: %s", error->message);
		g_error_free (error);

		/* Try the old name - for the case when we've just upgraded from 1.10
		 * so the old m-s-m is currently running */
		sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
		                                          G_DBUS_PROXY_FLAGS_NONE,
		                                          NULL,
		                                          GSM_SERVICE_DBUS_OLD,
		                                          GSM_PATH_DBUS_OLD,
		                                          GSM_INTERFACE_DBUS_OLD,
		                                          NULL,
		                                          NULL);
		if (sm_proxy == NULL)
		{
			/* Okay, it wasn't the upgrade case, so now we can give up. */
			display_error (_("Could not connect to the session manager"));
			return NULL;
		}
	}

	return sm_proxy;
}

static void do_logout(unsigned int mode)
{
	GDBusProxy* sm_proxy;
	GError* error;
	GVariant *ret;

	sm_proxy = get_sm_proxy ();

	if (sm_proxy == NULL)
	{
		return;
	}

	error = NULL;
	ret = g_dbus_proxy_call_sync (sm_proxy, "Logout",
	                              g_variant_new ("(u)", mode),
	                              G_DBUS_CALL_FLAGS_NONE,
	                              -1,
	                              NULL,
	                              &error);

	if (ret == NULL)
	{
		g_warning ("Failed to call logout: %s", error->message);
		g_error_free (error);
	} else {
		g_variant_unref (ret);
	}

	if (sm_proxy != NULL)
	{
		g_object_unref (sm_proxy);
	}
}

static void do_shutdown_dialog(void)
{
	GDBusProxy* sm_proxy;
	GError* error;
	GVariant *ret;

	sm_proxy = get_sm_proxy ();

	if (sm_proxy == NULL)
	{
		return;
	}

	error = NULL;
	ret = g_dbus_proxy_call_sync (sm_proxy, "Shutdown",
	                              g_variant_new ("()"),
	                              G_DBUS_CALL_FLAGS_NONE,
	                              -1,
	                              NULL,
	                              &error);
	if (ret == NULL)
	{
		g_warning ("Failed to call shutdown: %s", error->message);
		g_error_free (error);
	} else {
		g_variant_unref (ret);
	}

	if (sm_proxy != NULL)
	{
		g_object_unref (sm_proxy);
	}
}

int main(int argc, char* argv[])
{
	GError* error;
	int conflicting_options;

	/* Initialize the i18n stuff */
#ifdef ENABLE_NLS
	bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
	textdomain(GETTEXT_PACKAGE);
#endif /* ENABLE_NLS */

	error = NULL;

	if (!gtk_init_with_args(&argc, &argv, NULL, options, NULL, &error))
	{
		g_warning("Unable to start: %s", error->message);
		g_error_free(error);
		exit(1);
	}

	conflicting_options = 0;

	if (kill_session)
	{
		conflicting_options++;
	}

	if (logout)
	{
		conflicting_options++;
	}

	if (force_logout)
	{
		conflicting_options++;
	}

	if (logout_dialog)
	{
		conflicting_options++;
	}

	if (shutdown_dialog)
	{
		conflicting_options++;
	}

	if (conflicting_options > 1)
	{
		display_error(_("Program called with conflicting options"));
	}

	if (kill_session)
	{
		if (no_interaction)
		{
			force_logout = TRUE;
		}
		else
		{
			logout_dialog = TRUE;
		}
	}

	if (logout)
	{
		do_logout(GSM_LOGOUT_MODE_NO_CONFIRMATION);
	}
	else if (force_logout)
	{
		do_logout(GSM_LOGOUT_MODE_FORCE);
	}
	else if (logout_dialog)
	{
		do_logout(GSM_LOGOUT_MODE_NORMAL);
	}
	else if (shutdown_dialog)
	{
		do_shutdown_dialog();
	}

	return 0;
}