glib already prints a verbose error message when execute fails. don't add a second...
[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->prompt);
83         g_free(o);
84     }
85 }
86
87 static Options* dup_options(Options *in)
88 {
89     Options *o = g_new(Options, 1);
90     o->cmd = g_strdup(in->cmd);
91     o->sn = in->sn;
92     o->sn_name = g_strdup(in->sn_name);
93     o->sn_icon = g_strdup(in->sn_icon);
94     o->sn_wmclass = g_strdup(in->sn_wmclass);
95     o->prompt = NULL;
96     return o;
97 }
98
99 static gboolean run_func(ObActionsData *data, gpointer options);
100
101 static void prompt_cb(ObPrompt *p, gint result, gpointer options)
102 {
103     if (result)
104         run_func(NULL, options);
105
106     prompt_unref(p);
107     free_func(options);
108 }
109
110 /* Always return FALSE because its not interactive */
111 static gboolean run_func(ObActionsData *data, gpointer options)
112 {
113     GError *e = NULL;
114     gchar **argv = NULL;
115     gchar *cmd;
116     Options *o = options;
117
118     if (!o->cmd) return FALSE;
119
120     if (o->prompt) {
121         ObPrompt *p;
122         Options *ocp;
123         ObPromptAnswer answers[] = {
124             { _("No"), 0 },
125             { _("Yes"), 1 }
126         };
127
128         ocp = dup_options(options);
129         p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
130         prompt_show(p, NULL, FALSE);
131
132         return FALSE;
133     }
134
135     cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
136     if (!cmd) {
137         g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
138         return FALSE;
139     }
140
141     /* If there is a keyboard grab going on then we need to cancel
142        it so the application can grab things */
143     event_cancel_all_key_grabs();
144
145     if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
146         g_message(e->message, o->cmd);
147         g_error_free(e);
148     }
149     else {
150         gchar *program = NULL;
151
152         if (o->sn) {
153             program = g_path_get_basename(argv[0]);
154             /* sets up the environment */
155             sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
156                                        o->sn_wmclass,
157                                        /* launch it on the current desktop */
158                                        screen_desktop);
159         }
160
161         if (!g_spawn_async(NULL, argv, NULL,
162                            G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
163                            NULL, NULL, NULL, &e))
164         {
165             g_message(e->message, o->cmd);
166             g_error_free(e);
167
168             if (o->sn)
169                 sn_spawn_cancel();
170         }
171         if (o->sn)
172             unsetenv("DESKTOP_STARTUP_ID");
173
174         g_free(program);
175         g_strfreev(argv);
176     }
177
178     g_free(cmd);
179
180     return FALSE;
181 }