4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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 dir_show_help(DirItem
*item
, const char *path
);
51 static void open_mountpoint(const guchar
*full_path
, DirItem
*item
,
52 FilerWindow
*filer_window
, FilerWindow
*src_window
,
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
)
95 if (stat(path
, &info
))
97 delayed_error(_("Program %s not found - deleted?"), path
);
101 argv
= g_malloc(sizeof(char *) * (g_list_length(uri_list
) + 2));
103 if (S_ISDIR(info
.st_mode
))
104 argv
[argc
++] = make_path(path
, "AppRun");
110 const char *uri
= uri_list
->data
;
113 local
= get_local_path(uri
);
115 argv
[argc
++] = local
;
118 uri_list
= uri_list
->next
;
123 rox_spawn(home_dir
, argv
);
126 /* Run the program as '<path> -', piping the data to it via stdin.
127 * You can g_free() the data as soon as this returns.
129 void run_with_data(const char *path
, gpointer data
, gulong length
)
131 const char *argv
[] = {NULL
, "-", NULL
};
136 if (stat(path
, &info
))
138 delayed_error(_("Program %s not found - deleted?"), path
);
142 if (S_ISDIR(info
.st_mode
))
143 argv
[0] = make_path(path
, "AppRun");
149 delayed_error("pipe: %s", g_strerror(errno
));
152 close_on_exec(fds
[1], TRUE
);
153 close_on_exec(fds
[0], TRUE
);
158 delayed_error("fork: %s", g_strerror(errno
));
162 /* We are the child */
164 if (dup2(fds
[0], 0) == -1)
165 g_warning("dup2() failed: %s\n",
169 close_on_exec(0, FALSE
);
170 if (execv(argv
[0], (char **) argv
))
171 g_warning("execv(%s) failed: %s\n",
172 argv
[0], g_strerror(errno
));
176 /* We are the parent */
177 set_blocking(fds
[1], FALSE
);
178 pd
= g_new(PipedData
, 1);
179 pd
->data
= g_malloc(length
);
180 memcpy(pd
->data
, data
, length
);
183 pd
->tag
= gtk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
184 write_data
, NULL
, pd
, NULL
);
191 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
192 * edit a file, open an application, follow a symlink or mount a device.
194 * filer_window is the window to use for displaying a directory.
195 * NULL will always use a new directory when needed.
196 * src_window is the window to copy options from, or NULL.
198 * Returns TRUE on success.
200 gboolean
run_diritem(const guchar
*full_path
,
202 FilerWindow
*filer_window
,
203 FilerWindow
*src_window
,
206 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
207 return follow_symlink(full_path
, filer_window
, src_window
);
209 switch (item
->base_type
)
212 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
218 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
220 open_mountpoint(full_path
, item
,
221 filer_window
, src_window
, edit
);
223 else if (filer_window
)
224 filer_change_to(filer_window
, full_path
, NULL
);
226 filer_opendir(full_path
, src_window
, NULL
);
229 if (item
->mime_type
== application_executable
&& !edit
)
231 const char *argv
[] = {NULL
, NULL
};
232 guchar
*dir
= filer_window
233 ? filer_window
->sym_path
238 return rox_spawn(dir
, argv
);
241 return open_file(full_path
, edit
? text_plain
244 delayed_error(_("File doesn't exist, or I can't "
245 "access it: %s"), full_path
);
249 _("I don't know how to open '%s'"), full_path
);
254 /* Attempt to open this item */
255 gboolean
run_by_path(const guchar
*full_path
)
260 /* XXX: Loads an image - wasteful */
261 item
= diritem_new("");
262 diritem_restat(full_path
, item
, NULL
);
263 retval
= run_diritem(full_path
, item
, NULL
, NULL
, FALSE
);
269 void show_item_help(const guchar
*path
, DirItem
*item
)
271 switch (item
->base_type
)
274 if (item
->flags
& ITEM_FLAG_EXEC_FILE
)
276 _("Executable file:\n"
277 "This is a file with an eXecute bit "
278 "set - it can be run as a program."));
282 "This is a data file. Try using the "
283 "Info menu item to find out more..."));
286 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
289 "A mount point is a directory which another "
290 "filing system can be mounted on. Everything "
291 "on the mounted filesystem then appears to be "
292 "inside the directory."));
294 dir_show_help(item
, path
);
296 case TYPE_CHAR_DEVICE
:
297 case TYPE_BLOCK_DEVICE
:
300 "Device files allow you to read from or write "
301 "to a device driver as though it was an "
307 "Pipes allow different programs to "
308 "communicate. One program writes data to the "
309 "pipe while another one reads it out again."));
314 "Sockets allow processes to communicate."));
319 "Doors are a little-used Solaris method for "
320 "processes to communicate."));
325 "I couldn't find out what kind of file this "
326 "is. Maybe it doesn't exist anymore or you "
327 "don't have search permission on the directory "
333 /* Open a directory viewer showing this file, and wink it */
334 void open_to_show(const guchar
*path
)
339 g_return_if_fail(path
!= NULL
);
341 dir
= g_strdup(path
);
342 slash
= strrchr(dir
, '/');
343 if (slash
== dir
|| !slash
)
345 /* Item in the root (or root itself!) */
346 new = filer_opendir("/", NULL
, NULL
);
348 display_set_autoselect(new, dir
+ 1);
353 new = filer_opendir(dir
, NULL
, NULL
);
357 display_set_hidden(new, TRUE
);
358 display_set_autoselect(new, slash
+ 1);
365 /* Invoked using -x, this indicates that the filesystem has been modified
366 * and we should look at this item again.
368 void examine(const guchar
*path
)
372 if (mc_stat(path
, &info
) != 0)
374 /* Deleted? Do a paranoid update of everything... */
375 filer_check_mounted(path
);
379 /* Update directory containing this item... */
380 dir_check_this(path
);
382 /* If this is itself a directory then rescan its contents... */
383 if (S_ISDIR(info
.st_mode
))
386 /* If it's on the pinboard, update the icon... */
387 icons_may_update(path
);
391 /****************************************************************
392 * INTERNAL FUNCTIONS *
393 ****************************************************************/
396 static void write_data(gpointer data
, gint fd
, GdkInputCondition cond
)
398 PipedData
*pd
= (PipedData
*) data
;
400 while (pd
->sent
< pd
->length
)
404 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
410 delayed_error(_("Could not send data to program: %s"),
419 g_source_remove(pd
->tag
);
425 /* Follow the link 'full_path' and display it in filer_window, or a
426 * new window if that is NULL.
428 static gboolean
follow_symlink(const char *full_path
,
429 FilerWindow
*filer_window
,
430 FilerWindow
*src_window
)
434 char path
[MAXPATHLEN
+ 1];
437 got
= readlink(full_path
, path
, MAXPATHLEN
);
440 delayed_error(_("Could not read link: %s"),
445 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
448 /* Make a relative path absolute */
452 slash
= strrchr(full_path
, '/');
453 g_return_val_if_fail(slash
!= NULL
, FALSE
);
455 tmp
= g_strndup(full_path
, slash
- full_path
);
456 real
= pathdup(make_path(tmp
, path
));
457 /* NB: full_path may be invalid here... */
461 real
= pathdup(path
);
463 slash
= strrchr(real
, '/');
468 _("Broken symlink (or you don't have permission "
469 "to follow it): %s"), full_path
);
481 filer_change_to(filer_window
, new_dir
, slash
+ 1);
486 new = filer_opendir(new_dir
, src_window
, NULL
);
488 display_set_autoselect(new, slash
+ 1);
496 /* Load this file into an appropriate editor */
497 static gboolean
open_file(const guchar
*path
, MIME_type
*type
)
499 g_return_val_if_fail(type
!= NULL
, FALSE
);
501 if (type_open(path
, type
))
505 _("No run action specified for files of this type (%s/%s) - "
506 "you can set a run action by choosing `Set Run Action' "
507 "from the File menu, or you can just drag the file to an "
515 /* Show the help for a directory - tries to open App/Help, but if
516 * that doesn't work then it displays a default message.
518 static void dir_show_help(DirItem
*item
, const char *path
)
522 help_dir
= g_strconcat(path
, "/Help", NULL
);
524 if (file_exists(help_dir
) == 0)
525 filer_opendir(help_dir
, NULL
, NULL
);
526 else if (item
->flags
& ITEM_FLAG_APPDIR
)
529 "This is an application directory - you can "
530 "run it as a program, or open it (hold down "
531 "Shift while you open it). Most applications provide "
532 "their own help here, but this one doesn't."));
534 info_message(_("Directory:\n"
535 "This is a directory. It contains an index to "
536 "other items - open it to see the list."));
539 /* Called like run_diritem, when a mount-point is opened */
540 static void open_mountpoint(const guchar
*full_path
, DirItem
*item
,
541 FilerWindow
*filer_window
, FilerWindow
*src_window
,
544 gboolean mounted
= (item
->flags
& ITEM_FLAG_MOUNTED
) != 0;
550 paths
= g_list_prepend(NULL
, (gpointer
) full_path
);
551 action_mount(paths
, filer_window
== NULL
, -1);
553 if (filer_window
&& !mounted
)
554 filer_change_to(filer_window
, full_path
, NULL
);
559 filer_change_to(filer_window
, full_path
, NULL
);
561 filer_opendir(full_path
, src_window
, NULL
);