r4269: Bugfix: New detail crashes cleanice theme. Try another value... (reported
[rox-filer/translations.git] / ROX-Filer / src / run.c
blob211198f843569b42accd931450154f85c5a5a899
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 gboolean run_desktop(const char *full_path,
54 const char **args, const char *dir);
56 typedef struct _PipedData PipedData;
58 struct _PipedData
60 guchar *data;
61 gint tag;
62 gulong sent;
63 gulong length;
67 /****************************************************************
68 * EXTERNAL INTERFACE *
69 ****************************************************************/
72 /* An application has been double-clicked (or run in some other way) */
73 void run_app(const char *path)
75 GString *apprun;
76 const char *argv[] = {NULL, NULL};
78 apprun = g_string_new(path);
79 argv[0] = g_string_append(apprun, "/AppRun")->str;
81 rox_spawn(home_dir, argv);
83 g_string_free(apprun, TRUE);
86 /* Execute this program, passing all the URIs in the list as arguments.
87 * URIs that are files on the local machine will be passed as simple
88 * pathnames. The uri_list should be freed after this function returns.
90 void run_with_files(const char *path, GList *uri_list)
92 const char **argv;
93 int argc = 0, i;
94 struct stat info;
95 MIME_type *type;
97 if (stat(path, &info))
99 delayed_error(_("Program %s not found - deleted?"), path);
100 return;
103 argv = g_malloc(sizeof(char *) * (g_list_length(uri_list) + 2));
105 if (S_ISDIR(info.st_mode))
106 argv[argc++] = make_path(path, "AppRun");
107 else
108 argv[argc++] = path;
110 while (uri_list)
112 const EscapedPath *uri = uri_list->data;
113 char *local;
115 local = get_local_path(uri);
116 if (local)
117 argv[argc++] = local;
118 else
119 argv[argc++] = unescape_uri(uri);
120 uri_list = uri_list->next;
123 argv[argc++] = NULL;
125 type = type_from_path(argv[0]);
126 if (type && type == application_x_desktop)
128 run_desktop(argv[0], argv + 1, home_dir);
130 else
132 rox_spawn(home_dir, argv);
135 for (i = 1; i < argc; i++)
136 g_free((gchar *) argv[i]);
137 g_free(argv);
140 /* Run the program as '<path> -', piping the data to it via stdin.
141 * You can g_free() the data as soon as this returns.
143 void run_with_data(const char *path, gpointer data, gulong length)
145 const char *argv[] = {NULL, "-", NULL};
146 struct stat info;
147 int fds[2];
148 PipedData *pd;
150 if (stat(path, &info))
152 delayed_error(_("Program %s not found - deleted?"), path);
153 return;
156 if (S_ISDIR(info.st_mode))
157 argv[0] = make_path(path, "AppRun");
158 else
159 argv[0] = path;
161 if (pipe(fds))
163 delayed_error("pipe: %s", g_strerror(errno));
164 return;
166 close_on_exec(fds[1], TRUE);
167 close_on_exec(fds[0], TRUE);
169 switch (fork())
171 case -1:
172 delayed_error("fork: %s", g_strerror(errno));
173 close(fds[1]);
174 break;
175 case 0:
176 /* We are the child */
177 chdir(home_dir);
178 if (dup2(fds[0], 0) == -1)
179 g_warning("dup2() failed: %s\n",
180 g_strerror(errno));
181 else
183 close_on_exec(0, FALSE);
184 if (execv(argv[0], (char **) argv))
185 g_warning("execv(%s) failed: %s\n",
186 argv[0], g_strerror(errno));
188 _exit(1);
189 default:
190 /* We are the parent */
191 set_blocking(fds[1], FALSE);
192 pd = g_new(PipedData, 1);
193 pd->data = g_malloc(length);
194 memcpy(pd->data, data, length);
195 pd->length = length;
196 pd->sent = 0;
197 pd->tag = gdk_input_add_full(fds[1], GDK_INPUT_WRITE,
198 write_data, pd, NULL);
199 break;
202 close(fds[0]);
205 /* Splits args into an argument vector, and runs the program. Must be
206 * executable.
208 void run_with_args(const char *path, DirItem *item, const char *args)
210 GError *error = NULL;
211 gchar **argv = NULL;
212 int n_args = 0;
214 if (item->base_type != TYPE_DIRECTORY && item->base_type != TYPE_FILE)
216 delayed_error("Arguments (%s) given for non-executable item %s",
217 args, path);
218 return;
221 if (!g_shell_parse_argv(args, &n_args, &argv, &error))
223 delayed_error("Failed to parse argument string '%s':\n%s",
224 args, error->message);
225 g_error_free(error);
226 return;
229 g_return_if_fail(argv != NULL);
230 g_return_if_fail(error == NULL);
232 argv = g_realloc(argv, (n_args + 2) * sizeof(gchar *));
233 memmove(argv + 1, argv, (n_args + 1) * sizeof(gchar *));
235 if (item->base_type == TYPE_DIRECTORY)
236 argv[0] = g_strconcat(path, "/AppRun", NULL);
237 else
238 argv[0] = g_strdup(path);
240 rox_spawn(home_dir, (const gchar **) argv);
242 g_strfreev(argv);
245 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
246 * edit a file, open an application, follow a symlink or mount a device.
248 * filer_window is the window to use for displaying a directory.
249 * NULL will always use a new directory when needed.
250 * src_window is the window to copy options from, or NULL.
252 * Returns TRUE on success.
254 gboolean run_diritem(const guchar *full_path,
255 DirItem *item,
256 FilerWindow *filer_window,
257 FilerWindow *src_window,
258 gboolean edit)
260 if (item->flags & ITEM_FLAG_SYMLINK && edit)
261 return follow_symlink(full_path, filer_window, src_window);
263 switch (item->base_type)
265 case TYPE_DIRECTORY:
266 if (item->flags & ITEM_FLAG_APPDIR && !edit)
268 run_app(full_path);
269 return TRUE;
272 if (item->flags & ITEM_FLAG_MOUNT_POINT)
274 open_mountpoint(full_path, item,
275 filer_window, src_window, edit);
277 else if (filer_window)
278 filer_change_to(filer_window, full_path, NULL);
279 else
280 filer_opendir(full_path, src_window, NULL);
281 return TRUE;
282 case TYPE_FILE:
283 if (EXECUTABLE_FILE(item) && !edit)
285 const char *argv[] = {NULL, NULL};
286 guchar *dir = filer_window
287 ? filer_window->sym_path
288 : NULL;
290 if (item->mime_type == application_x_desktop)
291 return run_desktop(full_path,
292 NULL, dir);
293 else
294 argv[0] = full_path;
296 return rox_spawn(dir, argv) != 0;
299 return open_file(full_path, edit ? text_plain
300 : item->mime_type);
301 case TYPE_ERROR:
302 delayed_error(_("File doesn't exist, or I can't "
303 "access it: %s"), full_path);
304 return FALSE;
305 default:
306 delayed_error(
307 _("I don't know how to open '%s'"), full_path);
308 return FALSE;
312 /* Attempt to open this item */
313 gboolean run_by_path(const guchar *full_path)
315 gboolean retval;
316 DirItem *item;
318 /* XXX: Loads an image - wasteful */
319 item = diritem_new("");
320 diritem_restat(full_path, item, NULL);
321 retval = run_diritem(full_path, item, NULL, NULL, FALSE);
322 diritem_free(item);
324 return retval;
327 /* Open dir/Help, or show a message if missing */
328 void show_help_files(const char *dir)
330 const char *help_dir;
332 help_dir = make_path(dir, "Help");
334 if (file_exists(help_dir))
335 filer_opendir(help_dir, NULL, NULL);
336 else
337 info_message(
338 _("Application:\n"
339 "This is an application directory - you can "
340 "run it as a program, or open it (hold down "
341 "Shift while you open it). Most applications provide "
342 "their own help here, but this one doesn't."));
345 /* Open a directory viewer showing this file, and wink it */
346 void open_to_show(const guchar *path)
348 FilerWindow *new;
349 guchar *dir, *slash;
351 g_return_if_fail(path != NULL);
353 dir = g_strdup(path);
354 slash = strrchr(dir, '/');
355 if (slash == dir || !slash)
357 /* Item in the root (or root itself!) */
358 new = filer_opendir("/", NULL, NULL);
359 if (new && dir[1])
360 display_set_autoselect(new, dir + 1);
362 else
364 *slash = '\0';
365 new = filer_opendir(dir, NULL, NULL);
366 if (new)
368 if (slash[1] == '.')
369 display_set_hidden(new, TRUE);
370 display_set_autoselect(new, slash + 1);
374 g_free(dir);
377 /* Invoked using -x, this indicates that the filesystem has been modified
378 * and we should look at this item again.
380 void examine(const guchar *path)
382 struct stat info;
384 if (mc_stat(path, &info) != 0)
386 /* Deleted? Do a paranoid update of everything... */
387 filer_check_mounted(path);
389 else
391 /* Update directory containing this item... */
392 dir_check_this(path);
394 /* If this is itself a directory then rescan its contents... */
395 if (S_ISDIR(info.st_mode))
396 refresh_dirs(path);
398 /* If it's on the pinboard, update the icon... */
399 icons_may_update(path);
403 /****************************************************************
404 * INTERNAL FUNCTIONS *
405 ****************************************************************/
408 static void write_data(gpointer data, gint fd, GdkInputCondition cond)
410 PipedData *pd = (PipedData *) data;
412 while (pd->sent < pd->length)
414 int sent;
416 sent = write(fd, pd->data + pd->sent, pd->length - pd->sent);
418 if (sent < 0)
420 if (errno == EAGAIN)
421 return;
422 delayed_error(_("Could not send data to program: %s"),
423 g_strerror(errno));
424 goto finish;
427 pd->sent += sent;
430 finish:
431 g_source_remove(pd->tag);
432 g_free(pd->data);
433 g_free(pd);
434 close(fd);
437 /* Follow the link 'full_path' and display it in filer_window, or a
438 * new window if that is NULL.
440 static gboolean follow_symlink(const char *full_path,
441 FilerWindow *filer_window,
442 FilerWindow *src_window)
444 char *real, *slash;
445 char *new_dir;
446 char path[MAXPATHLEN + 1];
447 int got;
449 got = readlink(full_path, path, MAXPATHLEN);
450 if (got < 0)
452 delayed_error(_("Could not read link: %s"),
453 g_strerror(errno));
454 return FALSE;
457 g_return_val_if_fail(got <= MAXPATHLEN, FALSE);
458 path[got] = '\0';
460 /* Make a relative path absolute */
461 if (path[0] != '/')
463 guchar *tmp;
464 slash = strrchr(full_path, '/');
465 g_return_val_if_fail(slash != NULL, FALSE);
467 tmp = g_strndup(full_path, slash - full_path);
468 real = pathdup(make_path(tmp, path));
469 /* NB: full_path may be invalid here... */
470 g_free(tmp);
472 else
473 real = pathdup(path);
475 slash = strrchr(real, '/');
476 if (!slash)
478 g_free(real);
479 delayed_error(
480 _("Broken symlink (or you don't have permission "
481 "to follow it): %s"), full_path);
482 return FALSE;
485 *slash = '\0';
487 if (*real)
488 new_dir = real;
489 else
490 new_dir = "/";
492 if (filer_window)
493 filer_change_to(filer_window, new_dir, slash + 1);
494 else
496 FilerWindow *new;
498 new = filer_opendir(new_dir, src_window, NULL);
499 if (new)
500 display_set_autoselect(new, slash + 1);
503 g_free(real);
505 return TRUE;
508 /* Load this file into an appropriate editor */
509 static gboolean open_file(const guchar *path, MIME_type *type)
511 g_return_val_if_fail(type != NULL, FALSE);
513 if (type_open(path, type))
514 return TRUE;
516 report_error(
517 _("No run action specified for files of this type (%s/%s) - "
518 "you can set a run action by choosing `Set Run Action' "
519 "from the File menu, or you can just drag the file to an "
520 "application.%s"),
521 type->media_type,
522 type->subtype,
523 type->executable ? _("\n\nNote: If this is a computer program which "
524 "you want to run, you need to set the execute bit "
525 "by choosing Permissions from the File menu.")
526 : "");
528 return FALSE;
531 /* Called like run_diritem, when a mount-point is opened */
532 static void open_mountpoint(const guchar *full_path, DirItem *item,
533 FilerWindow *filer_window, FilerWindow *src_window,
534 gboolean edit)
536 gboolean mounted = (item->flags & ITEM_FLAG_MOUNTED) != 0;
538 if (mounted == edit)
540 GList *paths;
542 paths = g_list_prepend(NULL, (gpointer) full_path);
543 action_mount(paths, filer_window == NULL, !mounted, -1);
544 g_list_free(paths);
545 if (filer_window && !mounted)
546 filer_change_to(filer_window, full_path, NULL);
548 else
550 if (filer_window)
551 filer_change_to(filer_window, full_path, NULL);
552 else
553 filer_opendir(full_path, src_window, NULL);
557 /* full_path is a .desktop file. Execute the application, using the Exec line
558 * from the file.
559 * Returns TRUE on success.
561 static gboolean run_desktop(const char *full_path,
562 const char **args,
563 const char *dir)
565 GError *error = NULL;
566 char *exec = NULL;
567 gint argc = 0;
568 gchar **argv = NULL;
569 GPtrArray *expanded = NULL;
570 int i;
571 gboolean success = FALSE;
573 exec = get_value_from_desktop_file(full_path, "Desktop Entry", "Exec",
574 &error);
575 if (error)
577 delayed_error("Failed to parse .desktop file '%s':\n%s",
578 full_path, error->message);
579 goto err;
582 if (!exec)
584 delayed_error("Can't find Exec command in .desktop file '%s'",
585 full_path);
586 goto err;
589 if (!g_shell_parse_argv(exec, &argc, &argv, &error))
591 delayed_error("Failed to parse '%s' from '%s':\n%s",
592 exec, full_path, error->message);
593 goto err;
596 expanded = g_ptr_array_new();
597 for (i = 0; i < argc; i++)
599 const char *src = argv[i];
601 if (src[0] == '%' && src[1] != '\0' && src[2] == '\0')
603 /* We should treat these four differently. */
604 if (src[1] == 'f' || src[1] == 'F' ||
605 src[1] == 'u' || src[1] == 'U')
607 int j;
608 for (j = 0; args && args[j]; j++)
609 g_ptr_array_add(expanded, g_strdup(args[j]));
611 else
613 delayed_error("Unsupported escape character in '%s' in '%s'",
614 exec, full_path);
615 goto err;
618 else
620 g_ptr_array_add(expanded, g_strdup(src));
623 g_ptr_array_add(expanded, NULL);
625 success = rox_spawn(dir, (const gchar **) expanded->pdata);
626 err:
627 if (error != NULL)
628 g_error_free(error);
629 if (exec != NULL)
630 g_free(exec);
631 if (argv != NULL)
632 g_strfreev(argv);
633 if (expanded != NULL)
635 g_ptr_array_foreach(expanded, (GFunc) g_free, NULL);
636 g_ptr_array_free(expanded, TRUE);
639 return success;