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
);
56 typedef struct _PipedData PipedData
;
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
)
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
)
97 if (stat(path
, &info
))
99 delayed_error(_("Program %s not found - deleted?"), path
);
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");
112 const EscapedPath
*uri
= uri_list
->data
;
115 local
= get_local_path(uri
);
117 argv
[argc
++] = local
;
119 argv
[argc
++] = unescape_uri(uri
);
120 uri_list
= uri_list
->next
;
125 type
= type_from_path(argv
[0]);
126 if (type
&& type
== application_x_desktop
)
128 run_desktop(argv
[0], argv
+ 1, home_dir
);
132 rox_spawn(home_dir
, argv
);
135 for (i
= 1; i
< argc
; i
++)
136 g_free((gchar
*) argv
[i
]);
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
};
150 if (stat(path
, &info
))
152 delayed_error(_("Program %s not found - deleted?"), path
);
156 if (S_ISDIR(info
.st_mode
))
157 argv
[0] = make_path(path
, "AppRun");
163 delayed_error("pipe: %s", g_strerror(errno
));
166 close_on_exec(fds
[1], TRUE
);
167 close_on_exec(fds
[0], TRUE
);
172 delayed_error("fork: %s", g_strerror(errno
));
176 /* We are the child */
178 if (dup2(fds
[0], 0) == -1)
179 g_warning("dup2() failed: %s\n",
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
));
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
);
197 pd
->tag
= gdk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
198 write_data
, pd
, NULL
);
205 /* Splits args into an argument vector, and runs the program. Must be
208 void run_with_args(const char *path
, DirItem
*item
, const char *args
)
210 GError
*error
= NULL
;
214 if (item
->base_type
!= TYPE_DIRECTORY
&& item
->base_type
!= TYPE_FILE
)
216 delayed_error("Arguments (%s) given for non-executable item %s",
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
);
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
);
238 argv
[0] = g_strdup(path
);
240 rox_spawn(home_dir
, (const gchar
**) 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
,
256 FilerWindow
*filer_window
,
257 FilerWindow
*src_window
,
260 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
261 return follow_symlink(full_path
, filer_window
, src_window
);
263 switch (item
->base_type
)
266 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
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
);
280 filer_opendir(full_path
, src_window
, NULL
);
283 if (EXECUTABLE_FILE(item
) && !edit
)
285 const char *argv
[] = {NULL
, NULL
};
286 guchar
*dir
= filer_window
287 ? filer_window
->sym_path
290 if (item
->mime_type
== application_x_desktop
)
291 return run_desktop(full_path
,
296 return rox_spawn(dir
, argv
) != 0;
299 return open_file(full_path
, edit
? text_plain
302 delayed_error(_("File doesn't exist, or I can't "
303 "access it: %s"), full_path
);
307 _("I don't know how to open '%s'"), full_path
);
312 /* Attempt to open this item */
313 gboolean
run_by_path(const guchar
*full_path
)
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
);
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
);
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
)
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
);
360 display_set_autoselect(new, dir
+ 1);
365 new = filer_opendir(dir
, NULL
, NULL
);
369 display_set_hidden(new, TRUE
);
370 display_set_autoselect(new, slash
+ 1);
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
)
384 if (mc_stat(path
, &info
) != 0)
386 /* Deleted? Do a paranoid update of everything... */
387 filer_check_mounted(path
);
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
))
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
)
416 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
422 delayed_error(_("Could not send data to program: %s"),
431 g_source_remove(pd
->tag
);
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
)
446 char path
[MAXPATHLEN
+ 1];
449 got
= readlink(full_path
, path
, MAXPATHLEN
);
452 delayed_error(_("Could not read link: %s"),
457 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
460 /* Make a relative path absolute */
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... */
473 real
= pathdup(path
);
475 slash
= strrchr(real
, '/');
480 _("Broken symlink (or you don't have permission "
481 "to follow it): %s"), full_path
);
493 filer_change_to(filer_window
, new_dir
, slash
+ 1);
498 new = filer_opendir(new_dir
, src_window
, NULL
);
500 display_set_autoselect(new, slash
+ 1);
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
))
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 "
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.")
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
,
536 gboolean mounted
= (item
->flags
& ITEM_FLAG_MOUNTED
) != 0;
542 paths
= g_list_prepend(NULL
, (gpointer
) full_path
);
543 action_mount(paths
, filer_window
== NULL
, !mounted
, -1);
545 if (filer_window
&& !mounted
)
546 filer_change_to(filer_window
, full_path
, NULL
);
551 filer_change_to(filer_window
, full_path
, NULL
);
553 filer_opendir(full_path
, src_window
, NULL
);
557 /* full_path is a .desktop file. Execute the application, using the Exec line
559 * Returns TRUE on success.
561 static gboolean
run_desktop(const char *full_path
,
565 GError
*error
= NULL
;
569 GPtrArray
*expanded
= NULL
;
571 gboolean success
= FALSE
;
573 exec
= get_value_from_desktop_file(full_path
, "Desktop Entry", "Exec",
577 delayed_error("Failed to parse .desktop file '%s':\n%s",
578 full_path
, error
->message
);
584 delayed_error("Can't find Exec command in .desktop file '%s'",
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
);
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')
608 for (j
= 0; args
&& args
[j
]; j
++)
609 g_ptr_array_add(expanded
, g_strdup(args
[j
]));
613 delayed_error("Unsupported escape character in '%s' in '%s'",
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
);
633 if (expanded
!= NULL
)
635 g_ptr_array_foreach(expanded
, (GFunc
) g_free
, NULL
);
636 g_ptr_array_free(expanded
, TRUE
);