Merge branch 'backport' into work
[mikachu/openbox.git] / openbox / actions / exit.c
1 #include "openbox/actions.h"
2 #include "openbox/openbox.h"
3 #include "openbox/prompt.h"
4 #include "openbox/session.h"
5 #include "gettext.h"
6
7 typedef struct {
8     gboolean prompt;
9 } Options;
10
11 static gpointer setup_func(xmlNodePtr node);
12 static gboolean run_func(ObActionsData *data, gpointer options);
13
14 void action_exit_startup(void)
15 {
16     actions_register("Exit", setup_func, NULL, run_func);
17     actions_register("SessionLogout", setup_func, NULL, run_func);
18 }
19
20 static gpointer setup_func(xmlNodePtr node)
21 {
22     xmlNodePtr n;
23     Options *o;
24
25     o = g_new0(Options, 1);
26     o->prompt = TRUE;
27
28     if ((n = obt_parse_find_node(node, "prompt")))
29         o->prompt = obt_parse_node_bool(n);
30
31     return o;
32 }
33
34 static void do_exit(void)
35 {
36     if (session_connected())
37         session_request_logout(FALSE);
38     else
39         ob_exit(0);
40 }
41
42 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer data)
43 {
44     if (result)
45         do_exit();
46     return TRUE; /* call the cleanup func */
47 }
48
49 static void prompt_cleanup(ObPrompt *p, gpointer data)
50 {
51     prompt_unref(p);
52 }
53
54 /* Always return FALSE because its not interactive */
55 static gboolean run_func(ObActionsData *data, gpointer options)
56 {
57     Options *o = options;
58
59     if (o->prompt) {
60         ObPrompt *p;
61         ObPromptAnswer answers[] = {
62             { _("Cancel"), 0 },
63             { _("Exit"), 1 }
64         };
65
66         if (session_connected())
67             p = prompt_new(_("Are you sure you want to log out?"),
68                            _("Log Out"),
69                            answers, 2, 0, 0, prompt_cb, prompt_cleanup, NULL);
70         else
71             p = prompt_new(_("Are you sure you want to exit Openbox?"),
72                            _("Exit Openbox"),
73                            answers, 2, 0, 0, prompt_cb, prompt_cleanup, NULL);
74
75         prompt_show(p, NULL, FALSE);
76     }
77     else
78         do_exit();
79
80     return FALSE;
81 }