9bc9ea80a943553dd4c5241c65e342a5508d5176
[dana/openbox.git] / openbox / actions / session.c
1 #include "openbox/actions.h"
2 #include "openbox/prompt.h"
3 #include "openbox/session.h"
4 #include "gettext.h"
5
6 typedef struct {
7     gboolean prompt;
8     gboolean silent;
9 } Options;
10
11 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
12 static gboolean logout_func(ObActionsData *data, gpointer options);
13
14 void action_session_startup(void)
15 {
16     actions_register("SessionLogout", setup_func, NULL, logout_func,
17                      NULL, NULL);
18 }
19
20 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
21 {
22     xmlNodePtr n;
23     Options *o;
24
25     o = g_new0(Options, 1);
26     o->prompt = TRUE;
27
28     if ((n = parse_find_node("prompt", node)))
29         o->prompt = parse_bool(doc, n);
30
31     return o;
32 }
33
34 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer data)
35 {
36     Options *o = data;
37     if (result) {
38 #ifdef USE_SM
39         session_request_logout(o->silent);
40 #else
41         g_message(_("The SessionLogout actions is not available since Openbox was built without session management support"));
42 #endif
43     }
44     return TRUE; /* call cleanup func */
45 }
46
47 static void prompt_cleanup(ObPrompt *p, gpointer data)
48 {
49     g_free(data);
50     prompt_unref(p);
51 }
52
53 /* Always return FALSE because its not interactive */
54 static gboolean logout_func(ObActionsData *data, gpointer options)
55 {
56     Options *o = options;
57
58     if (o->prompt) {
59         Options *o2;
60         ObPrompt *p;
61         ObPromptAnswer answers[] = {
62             { _("Cancel"), 0 },
63             { _("Log out"), 1 }
64         };
65
66         o2 = g_memdup(o, sizeof(Options));
67         p = prompt_new(_("Are you sure you want to log out?"),
68                        _("Log out"),
69                        answers, 2, 0, 0, prompt_cb, prompt_cleanup, o2);
70         prompt_show(p, NULL, FALSE);
71     }
72     else
73         prompt_cb(NULL, 1, NULL);
74
75     return FALSE;
76 }