]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/smclient/eggsmclient.c
8e2254f0389b9d6502cb596b8a275f17d6d53c8a
[evince.git] / cut-n-paste / smclient / eggsmclient.c
1 /*
2  * Copyright (C) 2007 Novell, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <glib/gi18n.h>
24
25 #include "eggsmclient.h"
26 #include "eggsmclient-private.h"
27
28 static void egg_sm_client_debug_handler (const char *log_domain,
29                                          GLogLevelFlags log_level,
30                                          const char *message,
31                                          gpointer user_data);
32
33 enum {
34   SAVE_STATE,
35   QUIT_REQUESTED,
36   QUIT_CANCELLED,
37   QUIT,
38   LAST_SIGNAL
39 };
40
41 static guint signals[LAST_SIGNAL];
42
43 struct _EggSMClientPrivate {
44   GKeyFile *state_file;
45 };
46
47 #define EGG_SM_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_SM_CLIENT, EggSMClientPrivate))
48
49 G_DEFINE_TYPE (EggSMClient, egg_sm_client, G_TYPE_OBJECT)
50
51 static EggSMClient *global_client;
52 static EggSMClientMode global_client_mode = EGG_SM_CLIENT_MODE_NORMAL;
53
54 static void
55 egg_sm_client_init (EggSMClient *client)
56 {
57   ;
58 }
59
60 static void
61 egg_sm_client_class_init (EggSMClientClass *klass)
62 {
63   GObjectClass *object_class = G_OBJECT_CLASS (klass);
64
65   g_type_class_add_private (klass, sizeof (EggSMClientPrivate));
66
67   /**
68    * EggSMClient::save_state:
69    * @client: the client
70    * @state_file: a #GKeyFile to save state information into
71    *
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.
77    *
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.
83    *
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).
88    **/
89   signals[SAVE_STATE] =
90     g_signal_new ("save_state",
91                   G_OBJECT_CLASS_TYPE (object_class),
92                   G_SIGNAL_RUN_LAST,
93                   G_STRUCT_OFFSET (EggSMClientClass, save_state),
94                   NULL, NULL,
95                   g_cclosure_marshal_VOID__POINTER,
96                   G_TYPE_NONE,
97                   1, G_TYPE_POINTER);
98
99   /**
100    * EggSMClient::quit_requested:
101    * @client: the client
102    *
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
113    * behalf.
114    *
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
118    * separately.
119    * 
120    * If the application agrees to quit, it should then wait for either
121    * the ::quit_cancelled or ::quit signals to be emitted.
122    **/
123   signals[QUIT_REQUESTED] =
124     g_signal_new ("quit_requested",
125                   G_OBJECT_CLASS_TYPE (object_class),
126                   G_SIGNAL_RUN_LAST,
127                   G_STRUCT_OFFSET (EggSMClientClass, quit_requested),
128                   NULL, NULL,
129                   g_cclosure_marshal_VOID__VOID,
130                   G_TYPE_NONE,
131                   0);
132
133   /**
134    * EggSMClient::quit_cancelled:
135    * @client: the client
136    *
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.
141    **/
142   signals[QUIT_CANCELLED] =
143     g_signal_new ("quit_cancelled",
144                   G_OBJECT_CLASS_TYPE (object_class),
145                   G_SIGNAL_RUN_LAST,
146                   G_STRUCT_OFFSET (EggSMClientClass, quit_cancelled),
147                   NULL, NULL,
148                   g_cclosure_marshal_VOID__VOID,
149                   G_TYPE_NONE,
150                   0);
151
152   /**
153    * EggSMClient::quit:
154    * @client: the client
155    *
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.
160    *
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
165    * chance to object.
166    **/
167   signals[QUIT] =
168     g_signal_new ("quit",
169                   G_OBJECT_CLASS_TYPE (object_class),
170                   G_SIGNAL_RUN_LAST,
171                   G_STRUCT_OFFSET (EggSMClientClass, quit),
172                   NULL, NULL,
173                   g_cclosure_marshal_VOID__VOID,
174                   G_TYPE_NONE,
175                   0);
176 }
177
178 static gboolean sm_client_disable = FALSE;
179 static char *sm_client_state_file = NULL;
180 static char *sm_client_id = NULL;
181
182 static gboolean
183 sm_client_post_parse_func (GOptionContext  *context,
184                            GOptionGroup    *group,
185                            gpointer         data,
186                            GError         **error)
187 {
188   EggSMClient *client = egg_sm_client_get ();
189
190   if (sm_client_id == NULL)
191     {
192       const gchar *desktop_autostart_id;
193
194       desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
195
196       if (desktop_autostart_id != NULL)
197         sm_client_id = g_strdup (desktop_autostart_id);
198     }
199
200   /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
201    * use the same client id. */
202   g_unsetenv ("DESKTOP_AUTOSTART_ID");
203
204   if (EGG_SM_CLIENT_GET_CLASS (client)->startup)
205     EGG_SM_CLIENT_GET_CLASS (client)->startup (client, sm_client_id);
206   return TRUE;
207 }
208
209 /**
210  * egg_sm_client_get_option_group:
211  *
212  * Creates a %GOptionGroup containing the session-management-related
213  * options. You should add this group to the application's
214  * %GOptionContext if you want to use #EggSMClient.
215  *
216  * Return value: the %GOptionGroup
217  **/
218 GOptionGroup *
219 egg_sm_client_get_option_group (void)
220 {
221   const GOptionEntry entries[] = {
222     { "sm-client-disable", 0, 0,
223       G_OPTION_ARG_NONE, &sm_client_disable,
224       N_("Disable connection to session manager"), NULL },
225     { "sm-client-state-file", 0, 0,
226       G_OPTION_ARG_FILENAME, &sm_client_state_file,
227       N_("Specify file containing saved configuration"), N_("FILE") },
228     { "sm-client-id", 0, 0,
229       G_OPTION_ARG_STRING, &sm_client_id,
230       N_("Specify session management ID"), N_("ID") },
231     /* Compatibility options */
232     { "sm-disable", 0, G_OPTION_FLAG_HIDDEN,
233       G_OPTION_ARG_NONE, &sm_client_disable,
234       NULL, NULL },
235     { NULL }
236   };
237   GOptionGroup *group;
238
239   /* Use our own debug handler for the "EggSMClient" domain. */
240   g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
241                      egg_sm_client_debug_handler, NULL);
242
243   group = g_option_group_new ("sm-client",
244                               _("Session management options:"),
245                               _("Show session management options"),
246                               NULL, NULL);
247   g_option_group_add_entries (group, entries);
248   g_option_group_set_parse_hooks (group, NULL, sm_client_post_parse_func);
249
250   return group;
251 }
252
253 /**
254  * egg_sm_client_set_mode:
255  * @mode: an #EggSMClient mode
256  *
257  * Sets the "mode" of #EggSMClient as follows:
258  *
259  *    %EGG_SM_CLIENT_MODE_DISABLED: Session management is completely
260  *    disabled. The application will not even connect to the session
261  *    manager. (egg_sm_client_get() will still return an #EggSMClient,
262  *    but it will just be a dummy object.)
263  *
264  *    %EGG_SM_CLIENT_MODE_NO_RESTART: The application will connect to
265  *    the session manager (and thus will receive notification when the
266  *    user is logging out, etc), but will request to not be
267  *    automatically restarted with saved state in future sessions.
268  *
269  *    %EGG_SM_CLIENT_MODE_NORMAL: The default. #EggSMCLient will
270  *    function normally.
271  *
272  * This must be called before the application's main loop begins.
273  **/
274 void
275 egg_sm_client_set_mode (EggSMClientMode mode)
276 {
277   global_client_mode = mode;
278 }
279
280 /**
281  * egg_sm_client_get_mode:
282  *
283  * Gets the global #EggSMClientMode. See egg_sm_client_set_mode()
284  * for details.
285  *
286  * Return value: the global #EggSMClientMode
287  **/
288 EggSMClientMode
289 egg_sm_client_get_mode (void)
290 {
291   return global_client_mode;
292 }
293
294 /**
295  * egg_sm_client_get:
296  *
297  * Returns the master #EggSMClient for the application.
298  *
299  * On platforms that support saved sessions (ie, POSIX/X11), the
300  * application will only request to be restarted by the session
301  * manager if you call egg_set_desktop_file() to set an application
302  * desktop file. In particular, if the desktop file contains the key
303  * "X
304  *
305  * Return value: the master #EggSMClient.
306  **/
307 EggSMClient *
308 egg_sm_client_get (void)
309 {
310   if (!global_client)
311     {
312       if (global_client_mode != EGG_SM_CLIENT_MODE_DISABLED &&
313           !sm_client_disable)
314         {
315 #if defined (GDK_WINDOWING_WIN32)
316           global_client = egg_sm_client_win32_new ();
317 #elif defined (GDK_WINDOWING_QUARTZ)
318           global_client = egg_sm_client_osx_new ();
319 #else
320           /* If both D-Bus and XSMP are compiled in, try XSMP first
321            * (since it supports state saving) and fall back to D-Bus
322            * if XSMP isn't available.
323            */
324 # ifdef EGG_SM_CLIENT_BACKEND_XSMP
325           global_client = egg_sm_client_xsmp_new ();
326 # endif
327 # ifdef EGG_SM_CLIENT_BACKEND_DBUS
328           if (!global_client)
329             global_client = egg_sm_client_dbus_new ();
330 # endif
331 #endif
332         }
333
334       /* Fallback: create a dummy client, so that callers don't have
335        * to worry about a %NULL return value.
336        */
337       if (!global_client)
338         global_client = g_object_new (EGG_TYPE_SM_CLIENT, NULL);
339     }
340
341   return global_client;
342 }
343
344 /**
345  * egg_sm_client_is_resumed:
346  * @client: the client
347  *
348  * Checks whether or not the current session has been resumed from
349  * a previous saved session. If so, the application should call
350  * egg_sm_client_get_state_file() and restore its state from the
351  * returned #GKeyFile.
352  *
353  * Return value: %TRUE if the session has been resumed
354  **/
355 gboolean
356 egg_sm_client_is_resumed (EggSMClient *client)
357 {
358   g_return_val_if_fail (client == global_client, FALSE);
359
360   return sm_client_state_file != NULL;
361 }
362
363 /**
364  * egg_sm_client_get_state_file:
365  * @client: the client
366  *
367  * If the application was resumed by the session manager, this will
368  * return the #GKeyFile containing its state from the previous
369  * session.
370  *
371  * Note that other libraries and #EggSMClient itself may also store
372  * state in the key file, so if you call egg_sm_client_get_groups(),
373  * on it, the return value will likely include groups that you did not
374  * put there yourself. (It is also not guaranteed that the first
375  * group created by the application will still be the "start group"
376  * when it is resumed.)
377  *
378  * Return value: the #GKeyFile containing the application's earlier
379  * state, or %NULL on error. You should not free this key file; it
380  * is owned by @client.
381  **/
382 GKeyFile *
383 egg_sm_client_get_state_file (EggSMClient *client)
384 {
385   EggSMClientPrivate *priv = EGG_SM_CLIENT_GET_PRIVATE (client);
386   char *state_file_path;
387   GError *err = NULL;
388
389   g_return_val_if_fail (client == global_client, NULL);
390
391   if (!sm_client_state_file)
392     return NULL;
393   if (priv->state_file)
394     return priv->state_file;
395
396   if (!strncmp (sm_client_state_file, "file://", 7))
397     state_file_path = g_filename_from_uri (sm_client_state_file, NULL, NULL);
398   else
399     state_file_path = g_strdup (sm_client_state_file);
400
401   priv->state_file = g_key_file_new ();
402   if (!g_key_file_load_from_file (priv->state_file, state_file_path, 0, &err))
403     {
404       g_warning ("Could not load SM state file '%s': %s",
405                  sm_client_state_file, err->message);
406       g_clear_error (&err);
407       g_key_file_free (priv->state_file);
408       priv->state_file = NULL;
409     }
410
411   g_free (state_file_path);
412   return priv->state_file;
413 }
414
415 /**
416  * egg_sm_client_set_restart_command:
417  * @client: the client
418  * @argc: the length of @argv
419  * @argv: argument vector
420  *
421  * Sets the command used to restart @client if it does not have a
422  * .desktop file that can be used to find its restart command.
423  *
424  * This can also be used when handling the ::save_state signal, to
425  * save the current state via an updated command line. (Eg, providing
426  * a list of filenames to open when the application is resumed.)
427  **/
428 void
429 egg_sm_client_set_restart_command (EggSMClient  *client,
430                                    int           argc,
431                                    const char  **argv)
432 {
433   g_return_if_fail (EGG_IS_SM_CLIENT (client));
434
435   if (EGG_SM_CLIENT_GET_CLASS (client)->set_restart_command)
436     EGG_SM_CLIENT_GET_CLASS (client)->set_restart_command (client, argc, argv);
437 }
438
439 /**
440  * egg_sm_client_will_quit:
441  * @client: the client
442  * @will_quit: whether or not the application is willing to quit
443  *
444  * This MUST be called in response to the ::quit_requested signal, to
445  * indicate whether or not the application is willing to quit. The
446  * application may call it either directly from the signal handler, or
447  * at some later point (eg, after asynchronously interacting with the
448  * user).
449  *
450  * If the application does not connect to ::quit_requested,
451  * #EggSMClient will call this method on its behalf (passing %TRUE
452  * for @will_quit).
453  *
454  * After calling this method, the application should wait to receive
455  * either ::quit_cancelled or ::quit.
456  **/
457 void
458 egg_sm_client_will_quit (EggSMClient *client,
459                          gboolean     will_quit)
460 {
461   g_return_if_fail (EGG_IS_SM_CLIENT (client));
462
463   if (EGG_SM_CLIENT_GET_CLASS (client)->will_quit)
464     EGG_SM_CLIENT_GET_CLASS (client)->will_quit (client, will_quit);
465 }
466
467 /**
468  * egg_sm_client_end_session:
469  * @style: a hint at how to end the session
470  * @request_confirmation: whether or not the user should get a chance
471  * to confirm the action
472  *
473  * Requests that the session manager end the current session. @style
474  * indicates how the session should be ended, and
475  * @request_confirmation indicates whether or not the user should be
476  * given a chance to confirm the logout/reboot/shutdown. Both of these
477  * flags are merely hints though; the session manager may choose to
478  * ignore them.
479  *
480  * Return value: %TRUE if the request was sent; %FALSE if it could not
481  * be (eg, because it could not connect to the session manager).
482  **/
483 gboolean
484 egg_sm_client_end_session (EggSMClientEndStyle  style,
485                            gboolean             request_confirmation)
486 {
487   EggSMClient *client = egg_sm_client_get ();
488
489   g_return_val_if_fail (EGG_IS_SM_CLIENT (client), FALSE);
490
491   if (EGG_SM_CLIENT_GET_CLASS (client)->end_session)
492     {
493       return EGG_SM_CLIENT_GET_CLASS (client)->end_session (client, style,
494                                                             request_confirmation);
495     }
496   else
497     return FALSE;
498 }
499
500 /* Signal-emitting callbacks from platform-specific code */
501
502 GKeyFile *
503 egg_sm_client_save_state (EggSMClient *client)
504 {
505   GKeyFile *state_file;
506   char *group;
507
508   g_return_val_if_fail (client == global_client, NULL);
509
510   state_file = g_key_file_new ();
511
512   g_debug ("Emitting save_state");
513   g_signal_emit (client, signals[SAVE_STATE], 0, state_file);
514   g_debug ("Done emitting save_state");
515
516   group = g_key_file_get_start_group (state_file);
517   if (group)
518     {
519       g_free (group);
520       return state_file;
521     }
522   else
523     {
524       g_key_file_free (state_file);
525       return NULL;
526     }
527 }
528
529 void
530 egg_sm_client_quit_requested (EggSMClient *client)
531 {
532   g_return_if_fail (client == global_client);
533
534   if (!g_signal_has_handler_pending (client, signals[QUIT_REQUESTED], 0, FALSE))
535     {
536       g_debug ("Not emitting quit_requested because no one is listening");
537       egg_sm_client_will_quit (client, TRUE);
538       return;
539     }
540
541   g_debug ("Emitting quit_requested");
542   g_signal_emit (client, signals[QUIT_REQUESTED], 0);
543   g_debug ("Done emitting quit_requested");
544 }
545
546 void
547 egg_sm_client_quit_cancelled (EggSMClient *client)
548 {
549   g_return_if_fail (client == global_client);
550
551   g_debug ("Emitting quit_cancelled");
552   g_signal_emit (client, signals[QUIT_CANCELLED], 0);
553   g_debug ("Done emitting quit_cancelled");
554 }
555
556 void
557 egg_sm_client_quit (EggSMClient *client)
558 {
559   g_return_if_fail (client == global_client);
560
561   g_debug ("Emitting quit");
562   g_signal_emit (client, signals[QUIT], 0);
563   g_debug ("Done emitting quit");
564
565   /* FIXME: should we just call gtk_main_quit() here? */
566 }
567
568 static void
569 egg_sm_client_debug_handler (const char *log_domain,
570                              GLogLevelFlags log_level,
571                              const char *message,
572                              gpointer user_data)
573 {
574   static int debug = -1;
575
576   if (debug < 0)
577     debug = (g_getenv ("EGG_SM_CLIENT_DEBUG") != NULL);
578
579   if (debug)
580     g_log_default_handler (log_domain, log_level, message, NULL);
581 }