r4231: Moved GKeyFile stuff into one place in support.c.
[rox-filer/th.git] / ROX-Filer / src / run.c
blob59e340b7f9f59e17dd4207402f4aed772db905e9
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* run.c */
24 #include "config.h"
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/param.h>
30 #include "global.h"
32 #include "run.h"
33 #include "support.h"
34 #include "gui_support.h"
35 #include "filer.h"
36 #include "display.h"
37 #include "main.h"
38 #include "type.h"
39 #include "dir.h"
40 #include "diritem.h"
41 #include "action.h"
42 #include "icon.h"
44 /* Static prototypes */
45 static void write_data(gpointer data, gint fd, GdkInputCondition cond);
46 static gboolean follow_symlink(const char *full_path,
47 FilerWindow *filer_window,
48 FilerWindow *src_window);
49 static gboolean open_file(const guchar *path, MIME_type *type);
50 static void open_mountpoint(const guchar *full_path, DirItem *item,
51 FilerWindow *filer_window, FilerWindow *src_window,
52 gboolean edit);
53 static void run_desktop(const char *full_path, const char **args, const char *dir);
55 typedef struct _PipedData PipedData;
57 struct _PipedData
59 guchar *data;
60 gint tag;
61 gulong sent;
62 gulong length;
66 /****************************************************************
67 * EXTERNAL INTERFACE *
68 ****************************************************************/
71 /* An application has been double-clicked (or run in some other way) */
72 void run_app(const char *path)
74 GString *apprun;
75 const char *argv[] = {NULL, NULL};
77 apprun = g_string_new(path);
78 argv[0] = g_string_append(apprun, "/AppRun")->str;
80 rox_spawn(home_dir, argv);
82 g_string_free(apprun, TRUE);
85 /* Execute this program, passing all the URIs in the list as arguments.
86 * URIs that are files on the local machine will be passed as simple
87 * pathnames. The uri_list should be freed after this function returns.
89 void run_with_files(const char *path, GList *uri_list)
91 const char **argv;
92 int argc = 0, i;
93 struct stat info;
94 MIME_type *type;
96 if (stat(path, &info))
98 delayed_error(_("Program %s not found - deleted?"), path);
99 return;
102 argv = g_malloc(sizeof(char *) * (g_list_length(uri_list) + 2));
104 if (S_ISDIR(info.st_mode))
105 argv[argc++] = make_path(path, "AppRun");
106 else
107 argv[argc++] = path;
109 while (uri_list)
111 const EscapedPath *uri = uri_list->data;
112 char *local;
114 local = get_local_path(uri);
115 if (local)
116 argv[argc++] = local;
117 else
118 argv[argc++] = unescape_uri(uri);
119 uri_list = uri_list->next;
122 argv[argc++] = NULL;
124 type = type_from_path(argv[0]);
125 if (type && type == application_x_desktop)
127 run_desktop(argv[0], argv + 1, home_dir);
129 else
131 rox_spawn(home_dir, argv);
134 for (i = 1; i < argc; i++)
135 g_free((gchar *) argv[i]);
136 g_free(argv);
139 /* Run the program as '<path> -', piping the data to it via stdin.
140 * You can g_free() the data as soon as this returns.
142 void run_with_data(const char *path, gpointer data, gulong length)
144 const char *argv[] = {NULL, "-", NULL};
145 struct stat info;
146 int fds[2];
147 PipedData *pd;
149 if (stat(path, &info))
151 delayed_error(_("Program %s not found - deleted?"), path);
152 return;
155 if (S_ISDIR(info.st_mode))
156 argv[0] = make_path(path, "AppRun");
157 else
158 argv[0] = path;
160 if (pipe(fds))
162 delayed_error("pipe: %s", g_strerror(errno));
163 return;
165 close_on_exec(fds[1], TRUE);
166 close_on_exec(fds[0], TRUE);
168 switch (fork())
170 case -1:
171 delayed_error("fork: %s", g_strerror(errno));
172 close(fds[1]);
173 break;
174 case 0:
175 /* We are the child */
176 chdir(home_dir);
177 if (dup2(fds[0], 0) == -1)
178 g_warning("dup2() failed: %s\n",
179 g_strerror(errno));
180 else
182 close_on_exec(0, FALSE);
183 if (execv(argv[0], (char **) argv))
184 g_warning("execv(%s) failed: %s\n",
185 argv[0], g_strerror(errno));
187 _exit(1);
188 default:
189 /* We are the parent */
190 set_blocking(fds[1], FALSE);
191 pd = g_new(PipedData, 1);
192 pd->data = g_malloc(length);
193 memcpy(pd->data, data, length);
194 pd->length = length;
195 pd->sent = 0;
196 pd->tag = gdk_input_add_full(fds[1], GDK_INPUT_WRITE,
197 write_data, pd, NULL);
198 break;
201 close(fds[0]);
204 /* Splits args into an argument vector, and runs the program. Must be
205 * executable.
207 void run_with_args(const char *path, DirItem *item, const char *args)
209 GError *error = NULL;
210 gchar **argv = NULL;
211 int n_args = 0;
213 if (item->base_type != TYPE_DIRECTORY && item->base_type != TYPE_FILE)
215 delayed_error("Arguments (%s) given for non-executable item %s",
216 args, path);
217 return;
220 if (!g_shell_parse_argv(args, &n_args, &argv, &error))
222 delayed_error("Failed to parse argument string '%s':\n%s",
223 args, error->message);
224 g_error_free(error);
225 return;
228 g_return_if_fail(argv != NULL);
229 g_return_if_fail(error == NULL);
231 argv = g_realloc(argv, (n_args + 2) * sizeof(gchar *));
232 memmove(argv + 1, argv, (n_args + 1) * sizeof(gchar *));
234 if (item->base_type == TYPE_DIRECTORY)
235 argv[0] = g_strconcat(path, "/AppRun", NULL);
236 else
237 argv[0] = g_strdup(path);
239 rox_spawn(home_dir, (const gchar **) argv);
241 g_strfreev(argv);
244 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
245 * edit a file, open an application, follow a symlink or mount a device.
247 * filer_window is the window to use for displaying a directory.
248 * NULL will always use a new directory when needed.
249 * src_window is the window to copy options from, or NULL.
251 * Returns TRUE on success.
253 gboolean run_diritem(const guchar *full_path,
254 DirItem *item,
255 FilerWindow *filer_window,
256 FilerWindow *src_window,
257 gboolean edit)
259 if (item->flags & ITEM_FLAG_SYMLINK && edit)
260 return follow_symlink(full_path, filer_window, src_window);
262 switch (item->base_type)
264 case TYPE_DIRECTORY:
265 if (item->flags & ITEM_FLAG_APPDIR && !edit)
267 run_app(full_path);
268 return TRUE;
271 if (item->flags & ITEM_FLAG_MOUNT_POINT)
273 open_mountpoint(full_path, item,
274 filer_window, src_window, edit);
276 else if (filer_window)
277 filer_change_to(filer_window, full_path, NULL);
278 else
279 filer_opendir(full_path, src_window, NULL);
280 return TRUE;
281 case TYPE_FILE:
282 if (EXECUTABLE_FILE(item) && !edit)
284 const char *argv[] = {NULL, NULL};
285 guchar *dir = filer_window
286 ? filer_window->sym_path
287 : NULL;
289 if (item->mime_type == application_x_desktop)
290 run_desktop(full_path, NULL, dir);
291 else
292 argv[0] = full_path;
294 return rox_spawn(dir, argv) != 0;
297 return open_file(full_path, edit ? text_plain
298 : item->mime_type);
299 case TYPE_ERROR:
300 delayed_error(_("File doesn't exist, or I can't "
301 "access it: %s"), full_path);
302 return FALSE;
303 default:
304 delayed_error(
305 _("I don't know how to open '%s'"), full_path);
306 return FALSE;
310 /* Attempt to open this item */
311 gboolean run_by_path(const guchar *full_path)
313 gboolean retval;
314 DirItem *item;
316 /* XXX: Loads an image - wasteful */
317 item = diritem_new("");
318 diritem_restat(full_path, item, NULL);
319 retval = run_diritem(full_path, item, NULL, NULL, FALSE);
320 diritem_free(item);
322 return retval;
325 /* Open dir/Help, or show a message if missing */
326 void show_help_files(const char *dir)
328 const char *help_dir;
330 help_dir = make_path(dir, "Help");
332 if (file_exists(help_dir))
333 filer_opendir(help_dir, NULL, NULL);
334 else
335 info_message(
336 _("Application:\n"
337 "This is an application directory - you can "
338 "run it as a program, or open it (hold down "
339 "Shift while you open it). Most applications provide "
340 "their own help here, but this one doesn't."));
343 /* Open a directory viewer showing this file, and wink it */
344 void open_to_show(const guchar *path)
346 FilerWindow *new;
347 guchar *dir, *slash;
349 g_return_if_fail(path != NULL);
351 dir = g_strdup(path);
352 slash = strrchr(dir, '/');
353 if (slash == dir || !slash)
355 /* Item in the root (or root itself!) */
356 new = filer_opendir("/", NULL, NULL);
357 if (new && dir[1])
358 display_set_autoselect(new, dir + 1);
360 else
362 *slash = '\0';
363 new = filer_opendir(dir, NULL, NULL);
364 if (new)
366 if (slash[1] == '.')
367 display_set_hidden(new, TRUE);
368 display_set_autoselect(new, slash + 1);
372 g_free(dir);
375 /* Invoked using -x, this indicates that the filesystem has been modified
376 * and we should look at this item again.
378 void examine(const guchar *path)
380 struct stat info;
382 if (mc_stat(path, &info) != 0)
384 /* Deleted? Do a paranoid update of everything... */
385 filer_check_mounted(path);
387 else
389 /* Update directory containing this item... */
390 dir_check_this(path);
392 /* If this is itself a directory then rescan its contents... */
393 if (S_ISDIR(info.st_mode))
394 refresh_dirs(path);
396 /* If it's on the pinboard, update the icon... */
397 icons_may_update(path);
401 /****************************************************************
402 * INTERNAL FUNCTIONS *
403 ****************************************************************/
406 static void write_data(gpointer data, gint fd, GdkInputCondition cond)
408 PipedData *pd = (PipedData *) data;
410 while (pd->sent < pd->length)
412 int sent;
414 sent = write(fd, pd->data + pd->sent, pd->length - pd->sent);
416 if (sent < 0)
418 if (errno == EAGAIN)
419 return;
420 delayed_error(_("Could not send data to program: %s"),
421 g_strerror(errno));
422 goto finish;
425 pd->sent += sent;
428 finish:
429 g_source_remove(pd->tag);
430 g_free(pd->data);
431 g_free(pd);
432 close(fd);
435 /* Follow the link 'full_path' and display it in filer_window, or a
436 * new window if that is NULL.
438 static gboolean follow_symlink(const char *full_path,
439 FilerWindow *filer_window,
440 FilerWindow *src_window)
442 char *real, *slash;
443 char *new_dir;
444 char path[MAXPATHLEN + 1];
445 int got;
447 got = readlink(full_path, path, MAXPATHLEN);
448 if (got < 0)
450 delayed_error(_("Could not read link: %s"),
451 g_strerror(errno));
452 return FALSE;
455 g_return_val_if_fail(got <= MAXPATHLEN, FALSE);
456 path[got] = '\0';
458 /* Make a relative path absolute */
459 if (path[0] != '/')
461 guchar *tmp;
462 slash = strrchr(full_path, '/');
463 g_return_val_if_fail(slash != NULL, FALSE);
465 tmp = g_strndup(full_path, slash - full_path);
466 real = pathdup(make_path(tmp, path));
467 /* NB: full_path may be invalid here... */
468 g_free(tmp);
470 else
471 real = pathdup(path);
473 slash = strrchr(real, '/');
474 if (!slash)
476 g_free(real);
477 delayed_error(
478 _("Broken symlink (or you don't have permission "
479 "to follow it): %s"), full_path);
480 return FALSE;
483 *slash = '\0';
485 if (*real)
486 new_dir = real;
487 else
488 new_dir = "/";
490 if (filer_window)
491 filer_change_to(filer_window, new_dir, slash + 1);
492 else
494 FilerWindow *new;
496 new = filer_opendir(new_dir, src_window, NULL);
497 if (new)
498 display_set_autoselect(new, slash + 1);
501 g_free(real);
503 return TRUE;
506 /* Load this file into an appropriate editor */
507 static gboolean open_file(const guchar *path, MIME_type *type)
509 g_return_val_if_fail(type != NULL, FALSE);
511 if (type_open(path, type))
512 return TRUE;
514 report_error(
515 _("No run action specified for files of this type (%s/%s) - "
516 "you can set a run action by choosing `Set Run Action' "
517 "from the File menu, or you can just drag the file to an "
518 "application.%s"),
519 type->media_type,
520 type->subtype,
521 type->executable ? _("\n\nNote: If this is a computer program which "
522 "you want to run, you need to set the execute bit "
523 "by choosing Permissions from the File menu.")
524 : "");
526 return FALSE;
529 /* Called like run_diritem, when a mount-point is opened */
530 static void open_mountpoint(const guchar *full_path, DirItem *item,
531 FilerWindow *filer_window, FilerWindow *src_window,
532 gboolean edit)
534 gboolean mounted = (item->flags & ITEM_FLAG_MOUNTED) != 0;
536 if (mounted == edit)
538 GList *paths;
540 paths = g_list_prepend(NULL, (gpointer) full_path);
541 action_mount(paths, filer_window == NULL, !mounted, -1);
542 g_list_free(paths);
543 if (filer_window && !mounted)
544 filer_change_to(filer_window, full_path, NULL);
546 else
548 if (filer_window)
549 filer_change_to(filer_window, full_path, NULL);
550 else
551 filer_opendir(full_path, src_window, NULL);
555 /* full_path is a .desktop file. Execute the application, using the Exec line from
556 * the file.
558 static void run_desktop(const char *full_path, const char **args, const char *dir)
560 GError *error = NULL;
561 char *exec = NULL;
562 gint argc = 0;
563 gchar **argv = NULL;
564 GPtrArray *expanded = NULL;
565 int i;
567 exec = get_value_from_desktop_file(full_path, "Desktop Entry", "Exec",
568 &error);
569 if (error)
571 delayed_error("Failed to parse .desktop file '%s':\n%s",
572 full_path, error->message);
573 goto err;
576 if (!exec)
578 delayed_error("Can't find Exec command in .desktop file '%s'",
579 full_path);
580 goto err;
583 if (!g_shell_parse_argv(exec, &argc, &argv, &error))
585 delayed_error("Failed to parse '%s' from '%s':\n%s",
586 exec, full_path, error->message);
587 goto err;
590 expanded = g_ptr_array_new();
591 for (i = 0; i < argc; i++)
593 const char *src = argv[i];
595 if (src[0] == '%' && src[1] != '\0' && src[2] == '\0')
597 /* We should treat these four differently. */
598 if (src[1] == 'f' || src[1] == 'F' ||
599 src[1] == 'u' || src[1] == 'U')
601 int j;
602 for (j = 0; args && args[j]; j++)
603 g_ptr_array_add(expanded, g_strdup(args[j]));
605 else
607 delayed_error("Unsupported escape character in '%s' in '%s'",
608 exec, full_path);
609 goto err;
612 else
614 g_ptr_array_add(expanded, g_strdup(src));
617 g_ptr_array_add(expanded, NULL);
619 rox_spawn(dir, (const gchar **) expanded->pdata);
620 err:
621 if (error != NULL)
622 g_error_free(error);
623 if (exec != NULL)
624 g_free(exec);
625 if (argv != NULL)
626 g_strfreev(argv);
627 if (expanded != NULL)
629 g_ptr_array_foreach(expanded, (GFunc) g_free, NULL);
630 g_ptr_array_free(expanded, TRUE);