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