make code to show a prompt when you just want to display and message and not do anyth...
[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 void prompt_cb(ObPrompt *p, gint result, gpointer data)
35 {
36     Options *o = data;
37     if (result) {
38 #ifndef 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     g_free(o);
45     prompt_unref(p);
46 }
47
48 /* Always return FALSE because its not interactive */
49 static gboolean logout_func(ObActionsData *data, gpointer options)
50 {
51     Options *o = options;
52
53     if (o->prompt) {
54         Options *o2;
55         ObPrompt *p;
56         ObPromptAnswer answers[] = {
57             { _("Cancel"), 0 },
58             { _("Log out"), 1 }
59         };
60
61         o2 = g_memdup(o, sizeof(Options));
62         p = prompt_new(_("Are you sure you want to log out?"),
63                        answers, 2, 0, 0, prompt_cb, o2);
64         prompt_show(p, NULL, FALSE);
65     }
66     else
67         prompt_cb(NULL, 1, NULL);
68
69     return FALSE;
70 }