2
* Copyright (C) 2007 Novell, Inc.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
23
#include <glib/gi18n.h>
25
#include "eggsmclient.h"
26
#include "eggsmclient-private.h"
28
static void egg_sm_client_debug_handler (const char *log_domain,
29
GLogLevelFlags log_level,
41
static guint signals[LAST_SIGNAL];
43
struct _EggSMClientPrivate {
47
#define EGG_SM_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_SM_CLIENT, EggSMClientPrivate))
49
G_DEFINE_TYPE (EggSMClient, egg_sm_client, G_TYPE_OBJECT)
51
static EggSMClient *global_client;
52
static EggSMClientMode global_client_mode = EGG_SM_CLIENT_MODE_NORMAL;
55
egg_sm_client_init (EggSMClient *client)
61
egg_sm_client_class_init (EggSMClientClass *klass)
63
GObjectClass *object_class = G_OBJECT_CLASS (klass);
65
g_type_class_add_private (klass, sizeof (EggSMClientPrivate));
68
* EggSMClient::save_state:
70
* @state_file: a #GKeyFile to save state information into
72
* Emitted when the session manager has requested that the
73
* application save information about its current state. The
74
* application should save its state into @state_file, and then the
75
* session manager may then restart the application in a future
76
* session and tell it to initialize itself from that state.
78
* You should not save any data into @state_file's "start group"
79
* (ie, the %NULL group). Instead, applications should save their
80
* data into groups with names that start with the application name,
81
* and libraries that connect to this signal should save their data
82
* into groups with names that start with the library name.
84
* Alternatively, rather than (or in addition to) using @state_file,
85
* the application can save its state by calling
86
* egg_sm_client_set_restart_command() during the processing of this
87
* signal (eg, to include a list of files to open).
90
g_signal_new ("save_state",
91
G_OBJECT_CLASS_TYPE (object_class),
93
G_STRUCT_OFFSET (EggSMClientClass, save_state),
95
g_cclosure_marshal_VOID__POINTER,
100
* EggSMClient::quit_requested:
101
* @client: the client
103
* Emitted when the session manager requests that the application
104
* exit (generally because the user is logging out). The application
105
* should decide whether or not it is willing to quit (perhaps after
106
* asking the user what to do with documents that have unsaved
107
* changes) and then call egg_sm_client_will_quit(), passing %TRUE
108
* or %FALSE to give its answer to the session manager. (It does not
109
* need to give an answer before returning from the signal handler;
110
* it can interact with the user asynchronously and then give its
111
* answer later on.) If the application does not connect to this
112
* signal, then #EggSMClient will automatically return %TRUE on its
115
* The application should not save its session state as part of
116
* handling this signal; if the user has requested that the session
117
* be saved when logging out, then ::save_state will be emitted
120
* If the application agrees to quit, it should then wait for either
121
* the ::quit_cancelled or ::quit signals to be emitted.
123
signals[QUIT_REQUESTED] =
124
g_signal_new ("quit_requested",
125
G_OBJECT_CLASS_TYPE (object_class),
127
G_STRUCT_OFFSET (EggSMClientClass, quit_requested),
129
g_cclosure_marshal_VOID__VOID,
134
* EggSMClient::quit_cancelled:
135
* @client: the client
137
* Emitted when the session manager decides to cancel a logout after
138
* the application has already agreed to quit. After receiving this
139
* signal, the application can go back to what it was doing before
140
* receiving the ::quit_requested signal.
142
signals[QUIT_CANCELLED] =
143
g_signal_new ("quit_cancelled",
144
G_OBJECT_CLASS_TYPE (object_class),
146
G_STRUCT_OFFSET (EggSMClientClass, quit_cancelled),
148
g_cclosure_marshal_VOID__VOID,
154
* @client: the client
156
* Emitted when the session manager wants the application to quit
157
* (generally because the user is logging out). The application
158
* should exit as soon as possible after receiving this signal; if
159
* it does not, the session manager may choose to forcibly kill it.
161
* Normally a GUI application would only be sent a ::quit if it
162
* agreed to quit in response to a ::quit_requested signal. However,
163
* this is not guaranteed; in some situations the session manager
164
* may decide to end the session without giving applications a
168
g_signal_new ("quit",
169
G_OBJECT_CLASS_TYPE (object_class),
171
G_STRUCT_OFFSET (EggSMClientClass, quit),
173
g_cclosure_marshal_VOID__VOID,
178
static gboolean sm_client_disable = FALSE;
179
static char *sm_client_state_file = NULL;
180
static char *sm_client_id = NULL;
181
static char *sm_config_prefix = NULL;
184
sm_client_post_parse_func (GOptionContext *context,
189
EggSMClient *client = egg_sm_client_get ();
191
if (sm_client_id == NULL)
193
const gchar *desktop_autostart_id;
195
desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
197
if (desktop_autostart_id != NULL)
198
sm_client_id = g_strdup (desktop_autostart_id);
201
/* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
202
* use the same client id. */
203
g_unsetenv ("DESKTOP_AUTOSTART_ID");
205
if (EGG_SM_CLIENT_GET_CLASS (client)->startup)
206
EGG_SM_CLIENT_GET_CLASS (client)->startup (client, sm_client_id);
211
* egg_sm_client_get_option_group:
213
* Creates a %GOptionGroup containing the session-management-related
214
* options. You should add this group to the application's
215
* %GOptionContext if you want to use #EggSMClient.
217
* Return value: the %GOptionGroup
220
egg_sm_client_get_option_group (void)
222
const GOptionEntry entries[] = {
223
{ "sm-client-disable", 0, 0,
224
G_OPTION_ARG_NONE, &sm_client_disable,
225
N_("Disable connection to session manager"), NULL },
226
{ "sm-client-state-file", 0, 0,
227
G_OPTION_ARG_FILENAME, &sm_client_state_file,
228
N_("Specify file containing saved configuration"), N_("FILE") },
229
{ "sm-client-id", 0, 0,
230
G_OPTION_ARG_STRING, &sm_client_id,
231
N_("Specify session management ID"), N_("ID") },
232
/* GnomeClient compatibility option */
233
{ "sm-disable", 0, G_OPTION_FLAG_HIDDEN,
234
G_OPTION_ARG_NONE, &sm_client_disable,
236
/* GnomeClient compatibility option. This is a dummy option that only
237
* exists so that sessions saved by apps with GnomeClient can be restored
238
* later when they've switched to EggSMClient. See bug #575308.
240
{ "sm-config-prefix", 0, G_OPTION_FLAG_HIDDEN,
241
G_OPTION_ARG_STRING, &sm_config_prefix,
247
/* Use our own debug handler for the "EggSMClient" domain. */
248
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
249
egg_sm_client_debug_handler, NULL);
251
group = g_option_group_new ("sm-client",
252
_("Session management options:"),
253
_("Show session management options"),
255
g_option_group_add_entries (group, entries);
256
g_option_group_set_parse_hooks (group, NULL, sm_client_post_parse_func);
262
* egg_sm_client_set_mode:
263
* @mode: an #EggSMClient mode
265
* Sets the "mode" of #EggSMClient as follows:
267
* %EGG_SM_CLIENT_MODE_DISABLED: Session management is completely
268
* disabled. The application will not even connect to the session
269
* manager. (egg_sm_client_get() will still return an #EggSMClient,
270
* but it will just be a dummy object.)
272
* %EGG_SM_CLIENT_MODE_NO_RESTART: The application will connect to
273
* the session manager (and thus will receive notification when the
274
* user is logging out, etc), but will request to not be
275
* automatically restarted with saved state in future sessions.
277
* %EGG_SM_CLIENT_MODE_NORMAL: The default. #EggSMCLient will
280
* This must be called before the application's main loop begins.
283
egg_sm_client_set_mode (EggSMClientMode mode)
285
global_client_mode = mode;
289
* egg_sm_client_get_mode:
291
* Gets the global #EggSMClientMode. See egg_sm_client_set_mode()
294
* Return value: the global #EggSMClientMode
297
egg_sm_client_get_mode (void)
299
return global_client_mode;
305
* Returns the master #EggSMClient for the application.
307
* On platforms that support saved sessions (ie, POSIX/X11), the
308
* application will only request to be restarted by the session
309
* manager if you call egg_set_desktop_file() to set an application
310
* desktop file. In particular, if the desktop file contains the key
313
* Return value: the master #EggSMClient.
316
egg_sm_client_get (void)
320
if (global_client_mode != EGG_SM_CLIENT_MODE_DISABLED &&
323
#if defined (GDK_WINDOWING_WIN32)
324
global_client = egg_sm_client_win32_new ();
325
#elif defined (GDK_WINDOWING_QUARTZ)
326
global_client = egg_sm_client_osx_new ();
328
/* If both D-Bus and XSMP are compiled in, try XSMP first
329
* (since it supports state saving) and fall back to D-Bus
330
* if XSMP isn't available.
332
# ifdef EGG_SM_CLIENT_BACKEND_XSMP
333
global_client = egg_sm_client_xsmp_new ();
335
# ifdef EGG_SM_CLIENT_BACKEND_DBUS
337
global_client = egg_sm_client_dbus_new ();
342
/* Fallback: create a dummy client, so that callers don't have
343
* to worry about a %NULL return value.
346
global_client = g_object_new (EGG_TYPE_SM_CLIENT, NULL);
349
return global_client;
353
* egg_sm_client_is_resumed:
354
* @client: the client
356
* Checks whether or not the current session has been resumed from
357
* a previous saved session. If so, the application should call
358
* egg_sm_client_get_state_file() and restore its state from the
359
* returned #GKeyFile.
361
* Return value: %TRUE if the session has been resumed
364
egg_sm_client_is_resumed (EggSMClient *client)
366
g_return_val_if_fail (client == global_client, FALSE);
368
return sm_client_state_file != NULL;
372
* egg_sm_client_get_state_file:
373
* @client: the client
375
* If the application was resumed by the session manager, this will
376
* return the #GKeyFile containing its state from the previous
379
* Note that other libraries and #EggSMClient itself may also store
380
* state in the key file, so if you call egg_sm_client_get_groups(),
381
* on it, the return value will likely include groups that you did not
382
* put there yourself. (It is also not guaranteed that the first
383
* group created by the application will still be the "start group"
384
* when it is resumed.)
386
* Return value: the #GKeyFile containing the application's earlier
387
* state, or %NULL on error. You should not free this key file; it
388
* is owned by @client.
391
egg_sm_client_get_state_file (EggSMClient *client)
393
EggSMClientPrivate *priv = EGG_SM_CLIENT_GET_PRIVATE (client);
394
char *state_file_path;
397
g_return_val_if_fail (client == global_client, NULL);
399
if (!sm_client_state_file)
401
if (priv->state_file)
402
return priv->state_file;
404
if (!strncmp (sm_client_state_file, "file://", 7))
405
state_file_path = g_filename_from_uri (sm_client_state_file, NULL, NULL);
407
state_file_path = g_strdup (sm_client_state_file);
409
priv->state_file = g_key_file_new ();
410
if (!g_key_file_load_from_file (priv->state_file, state_file_path, 0, &err))
412
g_warning ("Could not load SM state file '%s': %s",
413
sm_client_state_file, err->message);
414
g_clear_error (&err);
415
g_key_file_free (priv->state_file);
416
priv->state_file = NULL;
419
g_free (state_file_path);
420
return priv->state_file;
424
* egg_sm_client_set_restart_command:
425
* @client: the client
426
* @argc: the length of @argv
427
* @argv: argument vector
429
* Sets the command used to restart @client if it does not have a
430
* .desktop file that can be used to find its restart command.
432
* This can also be used when handling the ::save_state signal, to
433
* save the current state via an updated command line. (Eg, providing
434
* a list of filenames to open when the application is resumed.)
437
egg_sm_client_set_restart_command (EggSMClient *client,
441
g_return_if_fail (EGG_IS_SM_CLIENT (client));
443
if (EGG_SM_CLIENT_GET_CLASS (client)->set_restart_command)
444
EGG_SM_CLIENT_GET_CLASS (client)->set_restart_command (client, argc, argv);
448
* egg_sm_client_set_discard_command:
449
* @client: the client
450
* @argc: the length of @argv
451
* @argv: argument vector
453
* Sets the command used to discard a custom state file if using
454
* egg_sm_client_set_restart_command(), which must be called before
455
* using this function.
458
egg_sm_client_set_discard_command (EggSMClient *client,
462
g_return_if_fail (EGG_IS_SM_CLIENT (client));
464
if (EGG_SM_CLIENT_GET_CLASS (client)->set_discard_command)
465
EGG_SM_CLIENT_GET_CLASS (client)->set_discard_command (client, argc, argv);
469
* egg_sm_client_will_quit:
470
* @client: the client
471
* @will_quit: whether or not the application is willing to quit
473
* This MUST be called in response to the ::quit_requested signal, to
474
* indicate whether or not the application is willing to quit. The
475
* application may call it either directly from the signal handler, or
476
* at some later point (eg, after asynchronously interacting with the
479
* If the application does not connect to ::quit_requested,
480
* #EggSMClient will call this method on its behalf (passing %TRUE
483
* After calling this method, the application should wait to receive
484
* either ::quit_cancelled or ::quit.
487
egg_sm_client_will_quit (EggSMClient *client,
490
g_return_if_fail (EGG_IS_SM_CLIENT (client));
492
if (EGG_SM_CLIENT_GET_CLASS (client)->will_quit)
493
EGG_SM_CLIENT_GET_CLASS (client)->will_quit (client, will_quit);
497
* egg_sm_client_end_session:
498
* @style: a hint at how to end the session
499
* @request_confirmation: whether or not the user should get a chance
500
* to confirm the action
502
* Requests that the session manager end the current session. @style
503
* indicates how the session should be ended, and
504
* @request_confirmation indicates whether or not the user should be
505
* given a chance to confirm the logout/reboot/shutdown. Both of these
506
* flags are merely hints though; the session manager may choose to
509
* Return value: %TRUE if the request was sent; %FALSE if it could not
510
* be (eg, because it could not connect to the session manager).
513
egg_sm_client_end_session (EggSMClientEndStyle style,
514
gboolean request_confirmation)
516
EggSMClient *client = egg_sm_client_get ();
518
g_return_val_if_fail (EGG_IS_SM_CLIENT (client), FALSE);
520
if (EGG_SM_CLIENT_GET_CLASS (client)->end_session)
522
return EGG_SM_CLIENT_GET_CLASS (client)->end_session (client, style,
523
request_confirmation);
529
/* Signal-emitting callbacks from platform-specific code */
532
egg_sm_client_save_state (EggSMClient *client)
534
GKeyFile *state_file;
537
g_return_val_if_fail (client == global_client, NULL);
539
state_file = g_key_file_new ();
541
g_debug ("Emitting save_state");
542
g_signal_emit (client, signals[SAVE_STATE], 0, state_file);
543
g_debug ("Done emitting save_state");
545
group = g_key_file_get_start_group (state_file);
553
g_key_file_free (state_file);
559
egg_sm_client_quit_requested (EggSMClient *client)
561
g_return_if_fail (client == global_client);
563
if (!g_signal_has_handler_pending (client, signals[QUIT_REQUESTED], 0, FALSE))
565
g_debug ("Not emitting quit_requested because no one is listening");
566
egg_sm_client_will_quit (client, TRUE);
570
g_debug ("Emitting quit_requested");
571
g_signal_emit (client, signals[QUIT_REQUESTED], 0);
572
g_debug ("Done emitting quit_requested");
576
egg_sm_client_quit_cancelled (EggSMClient *client)
578
g_return_if_fail (client == global_client);
580
g_debug ("Emitting quit_cancelled");
581
g_signal_emit (client, signals[QUIT_CANCELLED], 0);
582
g_debug ("Done emitting quit_cancelled");
586
egg_sm_client_quit (EggSMClient *client)
588
g_return_if_fail (client == global_client);
590
g_debug ("Emitting quit");
591
g_signal_emit (client, signals[QUIT], 0);
592
g_debug ("Done emitting quit");
594
/* FIXME: should we just call gtk_main_quit() here? */
598
egg_sm_client_debug_handler (const char *log_domain,
599
GLogLevelFlags log_level,
603
static int debug = -1;
606
debug = (g_getenv ("EGG_SM_CLIENT_DEBUG") != NULL);
609
g_log_default_handler (log_domain, log_level, message, NULL);