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
,
54 typedef struct _PipedData PipedData
;
65 /****************************************************************
66 * EXTERNAL INTERFACE *
67 ****************************************************************/
70 /* An application has been double-clicked (or run in some other way) */
71 void run_app(const char *path
)
74 const char *argv
[] = {NULL
, NULL
};
76 apprun
= g_string_new(path
);
77 argv
[0] = g_string_append(apprun
, "/AppRun")->str
;
79 rox_spawn(home_dir
, argv
);
81 g_string_free(apprun
, TRUE
);
84 /* Execute this program, passing all the URIs in the list as arguments.
85 * URIs that are files on the local machine will be passed as simple
86 * pathnames. The uri_list should be freed after this function returns.
88 void run_with_files(const char *path
, GList
*uri_list
)
94 if (stat(path
, &info
))
96 delayed_error(_("Program %s not found - deleted?"), path
);
100 argv
= g_malloc(sizeof(char *) * (g_list_length(uri_list
) + 2));
102 if (S_ISDIR(info
.st_mode
))
103 argv
[argc
++] = make_path(path
, "AppRun");
109 const EscapedPath
*uri
= uri_list
->data
;
112 local
= get_local_path(uri
);
114 argv
[argc
++] = local
;
116 argv
[argc
++] = unescape_uri(uri
);
117 uri_list
= uri_list
->next
;
122 rox_spawn(home_dir
, argv
);
124 for (i
= 1; i
< argc
; i
++)
125 g_free((gchar
*) argv
[i
]);
129 /* Run the program as '<path> -', piping the data to it via stdin.
130 * You can g_free() the data as soon as this returns.
132 void run_with_data(const char *path
, gpointer data
, gulong length
)
134 const char *argv
[] = {NULL
, "-", NULL
};
139 if (stat(path
, &info
))
141 delayed_error(_("Program %s not found - deleted?"), path
);
145 if (S_ISDIR(info
.st_mode
))
146 argv
[0] = make_path(path
, "AppRun");
152 delayed_error("pipe: %s", g_strerror(errno
));
155 close_on_exec(fds
[1], TRUE
);
156 close_on_exec(fds
[0], TRUE
);
161 delayed_error("fork: %s", g_strerror(errno
));
165 /* We are the child */
167 if (dup2(fds
[0], 0) == -1)
168 g_warning("dup2() failed: %s\n",
172 close_on_exec(0, FALSE
);
173 if (execv(argv
[0], (char **) argv
))
174 g_warning("execv(%s) failed: %s\n",
175 argv
[0], g_strerror(errno
));
179 /* We are the parent */
180 set_blocking(fds
[1], FALSE
);
181 pd
= g_new(PipedData
, 1);
182 pd
->data
= g_malloc(length
);
183 memcpy(pd
->data
, data
, length
);
186 pd
->tag
= gdk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
187 write_data
, pd
, NULL
);
194 /* Splits args into an argument vector, and runs the program. Must be
197 void run_with_args(const char *path
, DirItem
*item
, const char *args
)
199 GError
*error
= NULL
;
203 if (item
->base_type
!= TYPE_DIRECTORY
&& item
->base_type
!= TYPE_FILE
)
205 delayed_error("Arguments (%s) given for non-executable item %s",
210 if (!g_shell_parse_argv(args
, &n_args
, &argv
, &error
))
212 delayed_error("Failed to parse argument string '%s':\n%s",
213 args
, error
->message
);
218 g_return_if_fail(argv
!= NULL
);
219 g_return_if_fail(error
== NULL
);
221 argv
= g_realloc(argv
, (n_args
+ 2) * sizeof(gchar
*));
222 memmove(argv
+ 1, argv
, (n_args
+ 1) * sizeof(gchar
*));
224 if (item
->base_type
== TYPE_DIRECTORY
)
225 argv
[0] = g_strconcat(path
, "/AppRun", NULL
);
227 argv
[0] = g_strdup(path
);
229 rox_spawn(home_dir
, (const gchar
**) argv
);
234 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
235 * edit a file, open an application, follow a symlink or mount a device.
237 * filer_window is the window to use for displaying a directory.
238 * NULL will always use a new directory when needed.
239 * src_window is the window to copy options from, or NULL.
241 * Returns TRUE on success.
243 gboolean
run_diritem(const guchar
*full_path
,
245 FilerWindow
*filer_window
,
246 FilerWindow
*src_window
,
249 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
250 return follow_symlink(full_path
, filer_window
, src_window
);
252 switch (item
->base_type
)
255 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
261 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
263 open_mountpoint(full_path
, item
,
264 filer_window
, src_window
, edit
);
266 else if (filer_window
)
267 filer_change_to(filer_window
, full_path
, NULL
);
269 filer_opendir(full_path
, src_window
, NULL
);
272 if (item
->mime_type
== application_executable
&& !edit
)
274 const char *argv
[] = {NULL
, NULL
};
275 guchar
*dir
= filer_window
276 ? filer_window
->sym_path
281 return rox_spawn(dir
, argv
) != 0;
284 return open_file(full_path
, edit
? text_plain
287 delayed_error(_("File doesn't exist, or I can't "
288 "access it: %s"), full_path
);
292 _("I don't know how to open '%s'"), full_path
);
297 /* Attempt to open this item */
298 gboolean
run_by_path(const guchar
*full_path
)
303 /* XXX: Loads an image - wasteful */
304 item
= diritem_new("");
305 diritem_restat(full_path
, item
, NULL
);
306 retval
= run_diritem(full_path
, item
, NULL
, NULL
, FALSE
);
312 /* Open dir/Help, or show a message if missing */
313 void show_help_files(const char *dir
)
315 const char *help_dir
;
317 help_dir
= make_path(dir
, "Help");
319 if (file_exists(help_dir
))
320 filer_opendir(help_dir
, NULL
, NULL
);
324 "This is an application directory - you can "
325 "run it as a program, or open it (hold down "
326 "Shift while you open it). Most applications provide "
327 "their own help here, but this one doesn't."));
330 /* Open a directory viewer showing this file, and wink it */
331 void open_to_show(const guchar
*path
)
336 g_return_if_fail(path
!= NULL
);
338 dir
= g_strdup(path
);
339 slash
= strrchr(dir
, '/');
340 if (slash
== dir
|| !slash
)
342 /* Item in the root (or root itself!) */
343 new = filer_opendir("/", NULL
, NULL
);
345 display_set_autoselect(new, dir
+ 1);
350 new = filer_opendir(dir
, NULL
, NULL
);
354 display_set_hidden(new, TRUE
);
355 display_set_autoselect(new, slash
+ 1);
362 /* Invoked using -x, this indicates that the filesystem has been modified
363 * and we should look at this item again.
365 void examine(const guchar
*path
)
369 if (mc_stat(path
, &info
) != 0)
371 /* Deleted? Do a paranoid update of everything... */
372 filer_check_mounted(path
);
376 /* Update directory containing this item... */
377 dir_check_this(path
);
379 /* If this is itself a directory then rescan its contents... */
380 if (S_ISDIR(info
.st_mode
))
383 /* If it's on the pinboard, update the icon... */
384 icons_may_update(path
);
388 /****************************************************************
389 * INTERNAL FUNCTIONS *
390 ****************************************************************/
393 static void write_data(gpointer data
, gint fd
, GdkInputCondition cond
)
395 PipedData
*pd
= (PipedData
*) data
;
397 while (pd
->sent
< pd
->length
)
401 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
407 delayed_error(_("Could not send data to program: %s"),
416 g_source_remove(pd
->tag
);
422 /* Follow the link 'full_path' and display it in filer_window, or a
423 * new window if that is NULL.
425 static gboolean
follow_symlink(const char *full_path
,
426 FilerWindow
*filer_window
,
427 FilerWindow
*src_window
)
431 char path
[MAXPATHLEN
+ 1];
434 got
= readlink(full_path
, path
, MAXPATHLEN
);
437 delayed_error(_("Could not read link: %s"),
442 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
445 /* Make a relative path absolute */
449 slash
= strrchr(full_path
, '/');
450 g_return_val_if_fail(slash
!= NULL
, FALSE
);
452 tmp
= g_strndup(full_path
, slash
- full_path
);
453 real
= pathdup(make_path(tmp
, path
));
454 /* NB: full_path may be invalid here... */
458 real
= pathdup(path
);
460 slash
= strrchr(real
, '/');
465 _("Broken symlink (or you don't have permission "
466 "to follow it): %s"), full_path
);
478 filer_change_to(filer_window
, new_dir
, slash
+ 1);
483 new = filer_opendir(new_dir
, src_window
, NULL
);
485 display_set_autoselect(new, slash
+ 1);
493 /* Load this file into an appropriate editor */
494 static gboolean
open_file(const guchar
*path
, MIME_type
*type
)
496 g_return_val_if_fail(type
!= NULL
, FALSE
);
498 if (type_open(path
, type
))
502 _("No run action specified for files of this type (%s/%s) - "
503 "you can set a run action by choosing `Set Run Action' "
504 "from the File menu, or you can just drag the file to an "
512 /* Called like run_diritem, when a mount-point is opened */
513 static void open_mountpoint(const guchar
*full_path
, DirItem
*item
,
514 FilerWindow
*filer_window
, FilerWindow
*src_window
,
517 gboolean mounted
= (item
->flags
& ITEM_FLAG_MOUNTED
) != 0;
523 paths
= g_list_prepend(NULL
, (gpointer
) full_path
);
524 action_mount(paths
, filer_window
== NULL
, -1);
526 if (filer_window
&& !mounted
)
527 filer_change_to(filer_window
, full_path
, NULL
);
532 filer_change_to(filer_window
, full_path
, NULL
);
534 filer_opendir(full_path
, src_window
, NULL
);