Give the Execute action the <prompt> option
[mikachu/openbox.git] / openbox / actions / execute.c
1 #include "openbox/actions.h"
2 #include "openbox/event.h"
3 #include "openbox/startupnotify.h"
4 #include "openbox/prompt.h"
5 #include "openbox/screen.h"
6 #include "gettext.h"
7
8 #ifdef HAVE_STDLIB_H
9 #  include <stdlib.h>
10 #endif
11
12 typedef struct {
13     gchar   *cmd;
14     gboolean sn;
15     gchar   *sn_name;
16     gchar   *sn_icon;
17     gchar   *sn_wmclass;
18     gchar   *prompt;
19 } Options;
20
21 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
22 static void     free_func(gpointer options);
23 static gboolean run_func(ObActionsData *data, gpointer options);
24 /*
25 static gboolean i_input_func(guint initial_state,
26                              XEvent *e,
27                              gpointer options,
28                              gboolean *used);
29 static void     i_cancel_func(gpointer options);
30 */
31
32 void action_execute_startup(void)
33 {
34     actions_register("Execute",
35                      setup_func,
36                      free_func,
37                      run_func,
38                      NULL, NULL);
39 }
40
41 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
42 {
43     xmlNodePtr n;
44     Options *o;
45
46     o = g_new0(Options, 1);
47
48     if ((n = parse_find_node("command", node)) ||
49         (n = parse_find_node("execute", node)))
50     {
51         gchar *s = parse_string(doc, n);
52         o->cmd = parse_expand_tilde(s);
53         g_free(s);
54     }
55
56     if ((n = parse_find_node("prompt", node)))
57         o->prompt = parse_string(doc, n);
58
59     if ((n = parse_find_node("startupnotify", node))) {
60         xmlNodePtr m;
61         if ((m = parse_find_node("enabled", n->xmlChildrenNode)))
62             o->sn = parse_bool(doc, m);
63         if ((m = parse_find_node("name", n->xmlChildrenNode)))
64             o->sn_name = parse_string(doc, m);
65         if ((m = parse_find_node("icon", n->xmlChildrenNode)))
66             o->sn_icon = parse_string(doc, m);
67         if ((m = parse_find_node("wmclass", n->xmlChildrenNode)))
68             o->sn_wmclass = parse_string(doc, m);
69     }
70     return o;
71 }
72
73 static void free_func(gpointer options)
74 {
75     Options *o = options;
76
77     if (o) {
78         g_free(o->cmd);
79         g_free(o->sn_name);
80         g_free(o->sn_icon);
81         g_free(o->sn_wmclass);
82         g_free(o);
83     }
84 }
85
86 static Options* dup_options(Options *in)
87 {
88     Options *o = g_new(Options, 1);
89     o->cmd = g_strdup(in->cmd);
90     o->sn = in->sn;
91     o->sn_name = g_strdup(in->sn_name);
92     o->sn_icon = g_strdup(in->sn_icon);
93     o->sn_wmclass = g_strdup(in->sn_wmclass);
94     o->prompt = NULL;
95     return o;
96 }
97
98 static gboolean run_func(ObActionsData *data, gpointer options);
99
100 static void prompt_cb(ObPrompt *p, gint result, gpointer data)
101 {
102     Options *options = data;
103
104     if (result)
105         run_func(NULL, options);
106
107     prompt_unref(p);
108
109     g_free(options->cmd);
110     g_free(options->sn_name);
111     g_free(options->sn_icon);
112     g_free(options->sn_wmclass);
113     g_free(options);
114 }
115
116 /* Always return FALSE because its not interactive */
117 static gboolean run_func(ObActionsData *data, gpointer options)
118 {
119     GError *e = NULL;
120     gchar **argv = NULL;
121     gchar *cmd;
122     Options *o = options;
123
124     if (!o->cmd) return FALSE;
125
126     if (o->prompt) {
127         ObPrompt *p;
128         Options *ocp;
129         ObPromptAnswer answers[] = {
130             { _("No"), 0 },
131             { _("Yes"), 1 }
132         };
133
134         ocp = dup_options(options);
135         p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
136         prompt_show(p, NULL, FALSE);
137
138         return FALSE;
139     }
140
141     cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
142     if (!cmd) {
143         g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
144         return FALSE;
145     }
146
147     /* If there is a keyboard grab going on then we need to cancel
148        it so the application can grab things */
149     event_cancel_all_key_grabs();
150
151     if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
152         g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
153         g_error_free(e);
154     }
155     else {
156         gchar *program = NULL;
157
158         if (o->sn) {
159             program = g_path_get_basename(argv[0]);
160             /* sets up the environment */
161             sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
162                                        o->sn_wmclass,
163                                        /* launch it on the current desktop */
164                                        screen_desktop);
165         }
166
167         if (!g_spawn_async(NULL, argv, NULL,
168                            G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
169                            NULL, NULL, NULL, &e))
170         {
171             g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
172             g_error_free(e);
173
174             if (o->sn)
175                 sn_spawn_cancel();
176         }
177         if (o->sn)
178             unsetenv("DESKTOP_STARTUP_ID");
179
180         g_free(program);
181         g_strfreev(argv);
182     }
183
184     g_free(cmd);
185
186     return FALSE;
187 }