]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/exit.c
Provide operations to get the size of a ObClientSet, and get the clients in it. Also...
[dana/openbox.git] / openbox / actions / exit.c
1 #include "openbox/action.h"
2 #include "openbox/action_list_run.h"
3 #include "openbox/action_value.h"
4 #include "openbox/openbox.h"
5 #include "openbox/prompt.h"
6 #include "openbox/session.h"
7 #include "gettext.h"
8
9 typedef struct {
10     gboolean prompt;
11 } Options;
12
13 static gpointer setup_func(GHashTable *config);
14 static void free_func(gpointer o);
15 static gboolean run_func(const ObActionListRun *data, gpointer options);
16
17 void action_exit_startup(void)
18 {
19     action_register("Exit", OB_ACTION_DEFAULT_FILTER_EMPTY,
20                     setup_func, free_func, run_func);
21 }
22
23 static gpointer setup_func(GHashTable *config)
24 {
25     ObActionValue *v;
26     Options *o;
27
28     o = g_slice_new0(Options);
29     o->prompt = TRUE;
30
31     v = g_hash_table_lookup(config, "prompt");
32     if (v && action_value_is_string(v))
33         o->prompt = action_value_bool(v);
34
35     return o;
36 }
37
38 static void free_func(gpointer o)
39 {
40     g_slice_free(Options, o);
41 }
42
43 static void do_exit(void)
44 {
45     if (session_connected())
46         session_request_logout(FALSE);
47     else
48         ob_exit(0);
49 }
50
51 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer data)
52 {
53     if (result)
54         do_exit();
55     return TRUE; /* call the cleanup func */
56 }
57
58 static void prompt_cleanup(ObPrompt *p, gpointer data)
59 {
60     prompt_unref(p);
61 }
62
63
64 /* Always return FALSE because its not interactive */
65 static gboolean run_func(const ObActionListRun *data, gpointer options)
66 {
67     Options *o = options;
68
69     if (o->prompt) {
70         ObPrompt *p;
71         ObPromptAnswer answers[] = {
72             { _("Cancel"), 0 },
73             { _("Exit"), 1 }
74         };
75
76         if (session_connected())
77             p = prompt_new(_("Are you sure you want to log out?"),
78                            _("Log Out"),
79                            answers, 2, 0, 0, prompt_cb, prompt_cleanup, NULL);
80         else
81             p = prompt_new(_("Are you sure you want to exit Openbox?"),
82                            _("Exit Openbox"),
83                            answers, 2, 0, 0, prompt_cb, prompt_cleanup, NULL);
84
85         prompt_show(p, NULL, FALSE);
86     }
87     else
88         do_exit();
89
90     return FALSE;
91 }