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)
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
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
28 #include <sys/param.h>
34 #include "gui_support.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
,
53 static gboolean
run_desktop(const char *full_path
,
54 const char **args
, const char *dir
);
55 static gboolean
type_open(const char *path
, MIME_type
*type
);
57 typedef struct _PipedData PipedData
;
68 /****************************************************************
69 * EXTERNAL INTERFACE *
70 ****************************************************************/
73 /* An application has been double-clicked (or run in some other way) */
74 void run_app(const char *path
)
77 const char *argv
[] = {NULL
, NULL
};
79 apprun
= g_string_new(path
);
80 argv
[0] = g_string_append(apprun
, "/AppRun")->str
;
82 rox_spawn(home_dir
, argv
);
84 g_string_free(apprun
, TRUE
);
87 /* Execute this program, passing all the URIs in the list as arguments.
88 * URIs that are files on the local machine will be passed as simple
89 * pathnames. The uri_list should be freed after this function returns.
91 void run_with_files(const char *path
, GList
*uri_list
)
98 if (stat(path
, &info
))
100 delayed_error(_("Program %s not found - deleted?"), path
);
104 argv
= g_malloc(sizeof(char *) * (g_list_length(uri_list
) + 2));
106 if (S_ISDIR(info
.st_mode
))
107 argv
[argc
++] = make_path(path
, "AppRun");
113 const EscapedPath
*uri
= uri_list
->data
;
116 local
= get_local_path(uri
);
118 argv
[argc
++] = local
;
120 argv
[argc
++] = unescape_uri(uri
);
121 uri_list
= uri_list
->next
;
126 type
= type_from_path(argv
[0]);
127 if (type
&& type
== application_x_desktop
)
129 run_desktop(argv
[0], argv
+ 1, home_dir
);
133 rox_spawn(home_dir
, argv
);
136 for (i
= 1; i
< argc
; i
++)
137 g_free((gchar
*) argv
[i
]);
141 /* Run the program as '<path> -', piping the data to it via stdin.
142 * You can g_free() the data as soon as this returns.
144 void run_with_data(const char *path
, gpointer data
, gulong length
)
146 const char *argv
[] = {NULL
, "-", NULL
};
151 if (stat(path
, &info
))
153 delayed_error(_("Program %s not found - deleted?"), path
);
157 if (S_ISDIR(info
.st_mode
))
158 argv
[0] = make_path(path
, "AppRun");
164 delayed_error("pipe: %s", g_strerror(errno
));
167 close_on_exec(fds
[1], TRUE
);
168 close_on_exec(fds
[0], TRUE
);
173 delayed_error("fork: %s", g_strerror(errno
));
177 /* We are the child */
179 if (dup2(fds
[0], 0) == -1)
180 g_warning("dup2() failed: %s\n",
184 close_on_exec(0, FALSE
);
185 if (execv(argv
[0], (char **) argv
))
186 g_warning("execv(%s) failed: %s\n",
187 argv
[0], g_strerror(errno
));
191 /* We are the parent */
192 set_blocking(fds
[1], FALSE
);
193 pd
= g_new(PipedData
, 1);
194 pd
->data
= g_malloc(length
);
195 memcpy(pd
->data
, data
, length
);
198 pd
->tag
= gdk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
199 write_data
, pd
, NULL
);
206 /* Splits args into an argument vector, and runs the program. Must be
209 void run_with_args(const char *path
, DirItem
*item
, const char *args
)
211 GError
*error
= NULL
;
215 if (item
->base_type
!= TYPE_DIRECTORY
&& item
->base_type
!= TYPE_FILE
)
217 delayed_error("Arguments (%s) given for non-executable item %s",
222 if (!g_shell_parse_argv(args
, &n_args
, &argv
, &error
))
224 delayed_error("Failed to parse argument string '%s':\n%s",
225 args
, error
->message
);
230 g_return_if_fail(argv
!= NULL
);
231 g_return_if_fail(error
== NULL
);
233 argv
= g_realloc(argv
, (n_args
+ 2) * sizeof(gchar
*));
234 memmove(argv
+ 1, argv
, (n_args
+ 1) * sizeof(gchar
*));
236 if (item
->base_type
== TYPE_DIRECTORY
)
237 argv
[0] = g_strconcat(path
, "/AppRun", NULL
);
239 argv
[0] = g_strdup(path
);
241 rox_spawn(home_dir
, (const gchar
**) argv
);
246 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
247 * edit a file, open an application, follow a symlink or mount a device.
249 * filer_window is the window to use for displaying a directory.
250 * NULL will always use a new directory when needed.
251 * src_window is the window to copy options from, or NULL.
253 * Returns TRUE on success.
255 gboolean
run_diritem(const guchar
*full_path
,
257 FilerWindow
*filer_window
,
258 FilerWindow
*src_window
,
261 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
262 return follow_symlink(full_path
, filer_window
, src_window
);
264 switch (item
->base_type
)
267 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
273 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
275 open_mountpoint(full_path
, item
,
276 filer_window
, src_window
, edit
);
278 else if (filer_window
)
279 filer_change_to(filer_window
, full_path
, NULL
);
281 filer_opendir(full_path
, src_window
, NULL
);
284 if (EXECUTABLE_FILE(item
) && !edit
)
286 const char *argv
[] = {NULL
, NULL
};
287 guchar
*dir
= filer_window
288 ? filer_window
->sym_path
291 if (item
->mime_type
== application_x_desktop
)
292 return run_desktop(full_path
,
297 return rox_spawn(dir
, argv
) != 0;
300 return open_file(full_path
, edit
? text_plain
303 delayed_error(_("File doesn't exist, or I can't "
304 "access it: %s"), full_path
);
308 _("I don't know how to open '%s'"), full_path
);
313 /* Attempt to open this item */
314 gboolean
run_by_path(const guchar
*full_path
)
319 /* XXX: Loads an image - wasteful */
320 item
= diritem_new("");
321 diritem_restat(full_path
, item
, NULL
);
322 retval
= run_diritem(full_path
, item
, NULL
, NULL
, FALSE
);
328 /* Open dir/Help, or show a message if missing */
329 void show_help_files(const char *dir
)
331 const char *help_dir
;
333 help_dir
= make_path(dir
, "Help");
335 if (file_exists(help_dir
))
336 filer_opendir(help_dir
, NULL
, NULL
);
340 "This is an application directory - you can "
341 "run it as a program, or open it (hold down "
342 "Shift while you open it). Most applications provide "
343 "their own help here, but this one doesn't."));
346 /* Open a directory viewer showing this file, and wink it */
347 void open_to_show(const guchar
*path
)
352 g_return_if_fail(path
!= NULL
);
354 dir
= g_strdup(path
);
355 slash
= strrchr(dir
, '/');
356 if (slash
== dir
|| !slash
)
358 /* Item in the root (or root itself!) */
359 new = filer_opendir("/", NULL
, NULL
);
361 display_set_autoselect(new, dir
+ 1);
366 new = filer_opendir(dir
, NULL
, NULL
);
370 display_set_hidden(new, TRUE
);
371 display_set_autoselect(new, slash
+ 1);
378 /* Invoked using -x, this indicates that the filesystem has been modified
379 * and we should look at this item again.
381 void examine(const guchar
*path
)
385 if (mc_stat(path
, &info
) != 0)
387 /* Deleted? Do a paranoid update of everything... */
388 filer_check_mounted(path
);
392 /* Update directory containing this item... */
393 dir_check_this(path
);
395 /* If this is itself a directory then rescan its contents... */
396 if (S_ISDIR(info
.st_mode
))
399 /* If it's on the pinboard, update the icon... */
400 icons_may_update(path
);
404 /****************************************************************
405 * INTERNAL FUNCTIONS *
406 ****************************************************************/
409 static void write_data(gpointer data
, gint fd
, GdkInputCondition cond
)
411 PipedData
*pd
= (PipedData
*) data
;
413 while (pd
->sent
< pd
->length
)
417 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
423 delayed_error(_("Could not send data to program: %s"),
432 g_source_remove(pd
->tag
);
438 /* Follow the link 'full_path' and display it in filer_window, or a
439 * new window if that is NULL.
441 static gboolean
follow_symlink(const char *full_path
,
442 FilerWindow
*filer_window
,
443 FilerWindow
*src_window
)
447 char path
[MAXPATHLEN
+ 1];
450 got
= readlink(full_path
, path
, MAXPATHLEN
);
453 delayed_error(_("Could not read link: %s"),
458 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
461 /* Make a relative path absolute */
465 slash
= strrchr(full_path
, '/');
466 g_return_val_if_fail(slash
!= NULL
, FALSE
);
468 tmp
= g_strndup(full_path
, slash
- full_path
);
469 real
= pathdup(make_path(tmp
, path
));
470 /* NB: full_path may be invalid here... */
474 real
= pathdup(path
);
476 slash
= strrchr(real
, '/');
481 _("Broken symlink (or you don't have permission "
482 "to follow it): %s"), full_path
);
494 filer_change_to(filer_window
, new_dir
, slash
+ 1);
499 new = filer_opendir(new_dir
, src_window
, NULL
);
501 display_set_autoselect(new, slash
+ 1);
509 /* Load this file into an appropriate editor */
510 static gboolean
open_file(const guchar
*path
, MIME_type
*type
)
512 g_return_val_if_fail(type
!= NULL
, FALSE
);
514 if (type_open(path
, type
))
518 _("No run action specified for files of this type (%s/%s) - "
519 "you can set a run action by choosing `Set Run Action' "
520 "from the File menu, or you can just drag the file to an "
524 type
->executable
? _("\n\nNote: If this is a computer program which "
525 "you want to run, you need to set the execute bit "
526 "by choosing Permissions from the File menu.")
532 /* Called like run_diritem, when a mount-point is opened */
533 static void open_mountpoint(const guchar
*full_path
, DirItem
*item
,
534 FilerWindow
*filer_window
, FilerWindow
*src_window
,
537 gboolean mounted
= (item
->flags
& ITEM_FLAG_MOUNTED
) != 0;
543 paths
= g_list_prepend(NULL
, (gpointer
) full_path
);
544 action_mount(paths
, filer_window
== NULL
, !mounted
, -1);
546 if (filer_window
&& !mounted
)
547 filer_change_to(filer_window
, full_path
, NULL
);
552 filer_change_to(filer_window
, full_path
, NULL
);
554 filer_opendir(full_path
, src_window
, NULL
);
558 /* full_path is a .desktop file. Execute the application, using the Exec line
560 * Returns TRUE on success.
562 static gboolean
run_desktop(const char *full_path
,
566 GError
*error
= NULL
;
570 GPtrArray
*expanded
= NULL
;
572 gboolean success
= FALSE
;
574 exec
= get_value_from_desktop_file(full_path
, "Desktop Entry", "Exec",
578 delayed_error("Failed to parse .desktop file '%s':\n%s",
579 full_path
, error
->message
);
585 delayed_error("Can't find Exec command in .desktop file '%s'",
590 if (!g_shell_parse_argv(exec
, &argc
, &argv
, &error
))
592 delayed_error("Failed to parse '%s' from '%s':\n%s",
593 exec
, full_path
, error
->message
);
597 expanded
= g_ptr_array_new();
598 for (i
= 0; i
< argc
; i
++)
600 const char *src
= argv
[i
];
602 if (src
[0] == '%' && src
[1] != '\0' && src
[2] == '\0')
604 /* We should treat these four differently. */
605 if (src
[1] == 'f' || src
[1] == 'F' ||
606 src
[1] == 'u' || src
[1] == 'U')
609 for (j
= 0; args
&& args
[j
]; j
++)
610 g_ptr_array_add(expanded
, g_strdup(args
[j
]));
614 delayed_error("Unsupported escape character in '%s' in '%s'",
621 g_ptr_array_add(expanded
, g_strdup(src
));
624 g_ptr_array_add(expanded
, NULL
);
626 success
= rox_spawn(dir
, (const gchar
**) expanded
->pdata
);
634 if (expanded
!= NULL
)
636 g_ptr_array_foreach(expanded
, (GFunc
) g_free
, NULL
);
637 g_ptr_array_free(expanded
, TRUE
);
643 /* Returns FALSE is no run action is set for this type. */
644 static gboolean
type_open(const char *path
, MIME_type
*type
)
646 gchar
*argv
[] = {NULL
, NULL
, NULL
};
650 argv
[1] = (char *) path
;
652 open
= handler_for(type
);
656 if (stat(open
, &info
))
658 report_error("stat(%s): %s", open
, g_strerror(errno
));
663 if (info
.st_mode
& S_IWOTH
)
668 report_error(_("Executable '%s' is world-writeable! Refusing "
669 "to run. Please change the permissions now (this "
670 "problem may have been caused by a bug in earlier "
671 "versions of the filer).\n\n"
672 "Having (non-symlink) run actions world-writeable "
673 "means that other people who use your computer can "
674 "replace your run actions with malicious versions.\n\n"
675 "If you trust everyone who could write to these files "
676 "then you needn't worry. Otherwise, you should check, "
677 "or even just delete, all the existing run actions."),
679 choices_dir
= g_path_get_dirname(open
);
680 paths
= g_list_append(NULL
, choices_dir
);
681 action_chmod(paths
, TRUE
, _("go-w (Fix security problem)"));
688 if (S_ISDIR(info
.st_mode
))
690 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
691 rox_spawn(home_dir
, (const gchar
**) argv
) != 0;
693 else if (type_get_type(open
) == application_x_desktop
)
696 run_desktop(open
, (const char **) (argv
+ 1), home_dir
);
701 rox_spawn(home_dir
, (const gchar
**) argv
) != 0;