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 void run_desktop(const char *full_path
, const char **args
, const char *dir
);
55 typedef struct _PipedData PipedData
;
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
)
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
)
96 if (stat(path
, &info
))
98 delayed_error(_("Program %s not found - deleted?"), path
);
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");
111 const EscapedPath
*uri
= uri_list
->data
;
114 local
= get_local_path(uri
);
116 argv
[argc
++] = local
;
118 argv
[argc
++] = unescape_uri(uri
);
119 uri_list
= uri_list
->next
;
124 type
= type_from_path(argv
[0]);
125 if (type
&& type
== application_x_desktop
)
127 run_desktop(argv
[0], argv
+ 1, home_dir
);
131 rox_spawn(home_dir
, argv
);
134 for (i
= 1; i
< argc
; i
++)
135 g_free((gchar
*) argv
[i
]);
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
};
149 if (stat(path
, &info
))
151 delayed_error(_("Program %s not found - deleted?"), path
);
155 if (S_ISDIR(info
.st_mode
))
156 argv
[0] = make_path(path
, "AppRun");
162 delayed_error("pipe: %s", g_strerror(errno
));
165 close_on_exec(fds
[1], TRUE
);
166 close_on_exec(fds
[0], TRUE
);
171 delayed_error("fork: %s", g_strerror(errno
));
175 /* We are the child */
177 if (dup2(fds
[0], 0) == -1)
178 g_warning("dup2() failed: %s\n",
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
));
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
);
196 pd
->tag
= gdk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
197 write_data
, pd
, NULL
);
204 /* Splits args into an argument vector, and runs the program. Must be
207 void run_with_args(const char *path
, DirItem
*item
, const char *args
)
209 GError
*error
= NULL
;
213 if (item
->base_type
!= TYPE_DIRECTORY
&& item
->base_type
!= TYPE_FILE
)
215 delayed_error("Arguments (%s) given for non-executable item %s",
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
);
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
);
237 argv
[0] = g_strdup(path
);
239 rox_spawn(home_dir
, (const gchar
**) 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
,
255 FilerWindow
*filer_window
,
256 FilerWindow
*src_window
,
259 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
260 return follow_symlink(full_path
, filer_window
, src_window
);
262 switch (item
->base_type
)
265 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
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
);
279 filer_opendir(full_path
, src_window
, NULL
);
282 if (EXECUTABLE_FILE(item
) && !edit
)
284 const char *argv
[] = {NULL
, NULL
};
285 guchar
*dir
= filer_window
286 ? filer_window
->sym_path
289 if (item
->mime_type
== application_x_desktop
)
290 run_desktop(full_path
, NULL
, dir
);
294 return rox_spawn(dir
, argv
) != 0;
297 return open_file(full_path
, edit
? text_plain
300 delayed_error(_("File doesn't exist, or I can't "
301 "access it: %s"), full_path
);
305 _("I don't know how to open '%s'"), full_path
);
310 /* Attempt to open this item */
311 gboolean
run_by_path(const guchar
*full_path
)
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
);
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
);
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
)
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
);
358 display_set_autoselect(new, dir
+ 1);
363 new = filer_opendir(dir
, NULL
, NULL
);
367 display_set_hidden(new, TRUE
);
368 display_set_autoselect(new, slash
+ 1);
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
)
382 if (mc_stat(path
, &info
) != 0)
384 /* Deleted? Do a paranoid update of everything... */
385 filer_check_mounted(path
);
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
))
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
)
414 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
420 delayed_error(_("Could not send data to program: %s"),
429 g_source_remove(pd
->tag
);
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
)
444 char path
[MAXPATHLEN
+ 1];
447 got
= readlink(full_path
, path
, MAXPATHLEN
);
450 delayed_error(_("Could not read link: %s"),
455 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
458 /* Make a relative path absolute */
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... */
471 real
= pathdup(path
);
473 slash
= strrchr(real
, '/');
478 _("Broken symlink (or you don't have permission "
479 "to follow it): %s"), full_path
);
491 filer_change_to(filer_window
, new_dir
, slash
+ 1);
496 new = filer_opendir(new_dir
, src_window
, NULL
);
498 display_set_autoselect(new, slash
+ 1);
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
))
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 "
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.")
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
,
534 gboolean mounted
= (item
->flags
& ITEM_FLAG_MOUNTED
) != 0;
540 paths
= g_list_prepend(NULL
, (gpointer
) full_path
);
541 action_mount(paths
, filer_window
== NULL
, !mounted
, -1);
543 if (filer_window
&& !mounted
)
544 filer_change_to(filer_window
, full_path
, NULL
);
549 filer_change_to(filer_window
, full_path
, NULL
);
551 filer_opendir(full_path
, src_window
, NULL
);
555 /* full_path is a .desktop file. Execute the application, using the Exec line from
558 static void run_desktop(const char *full_path
, const char **args
, const char *dir
)
560 GError
*error
= NULL
;
564 GPtrArray
*expanded
= NULL
;
567 exec
= get_value_from_desktop_file(full_path
, "Desktop Entry", "Exec",
571 delayed_error("Failed to parse .desktop file '%s':\n%s",
572 full_path
, error
->message
);
578 delayed_error("Can't find Exec command in .desktop file '%s'",
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
);
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')
602 for (j
= 0; args
&& args
[j
]; j
++)
603 g_ptr_array_add(expanded
, g_strdup(args
[j
]));
607 delayed_error("Unsupported escape character in '%s' in '%s'",
614 g_ptr_array_add(expanded
, g_strdup(src
));
617 g_ptr_array_add(expanded
, NULL
);
619 rox_spawn(dir
, (const gchar
**) expanded
->pdata
);
627 if (expanded
!= NULL
)
629 g_ptr_array_foreach(expanded
, (GFunc
) g_free
, NULL
);
630 g_ptr_array_free(expanded
, TRUE
);