Merge branch 'backport' into work
[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 #ifndef USE_SM
7 void action_logout_startup(void) {}
8 #else
9
10 typedef struct {
11     gboolean prompt;
12     gboolean silent;
13 } Options;
14
15 static gpointer setup_func(xmlNodePtr node);
16 static gboolean logout_func(ObActionsData *data, gpointer options);
17
18 void action_session_startup(void)
19 {
20     actions_register("SessionLogout", setup_func, NULL, logout_func,
21                      NULL, NULL);
22 }
23
24 static gpointer setup_func(xmlNodePtr node)
25 {
26     xmlNodePtr n;
27     Options *o;
28
29     o = g_new0(Options, 1);
30     o->prompt = TRUE;
31
32     if ((n = obt_parse_find_node(node, "prompt")))
33         o->prompt = obt_parse_node_bool(n);
34
35     return o;
36 }
37
38 static void prompt_cb(ObPrompt *p, gint result, gpointer data)
39 {
40     Options *o = data;
41     if (result)
42         session_request_logout(o->silent);
43     g_free(o);
44     prompt_unref(p);
45 }
46
47 /* Always return FALSE because its not interactive */
48 static gboolean logout_func(ObActionsData *data, gpointer options)
49 {
50     Options *o = options;
51
52     if (o->prompt) {
53         Options *o2;
54         ObPrompt *p;
55         ObPromptAnswer answers[] = {
56             { _("Cancel"), 0 },
57             { _("Log out"), 1 }
58         };
59
60         o2 = g_memdup(o, sizeof(Options));
61         p = prompt_new(_("Are you sure you want to log out?"),
62                        answers, 2, 0, 0, prompt_cb, o2);
63         prompt_show(p, NULL, FALSE);
64     }
65     else
66         prompt_cb(NULL, 1, NULL);
67
68     return FALSE;
69 }
70
71 #endif