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
);
52 typedef struct _PipedData PipedData
;
63 /****************************************************************
64 * EXTERNAL INTERFACE *
65 ****************************************************************/
68 /* An application has been double-clicked (or run in some other way) */
69 void run_app(const char *path
)
72 const char *argv
[] = {NULL
, NULL
};
74 apprun
= g_string_new(path
);
75 argv
[0] = g_string_append(apprun
, "/AppRun")->str
;
77 rox_spawn(home_dir
, argv
);
79 g_string_free(apprun
, TRUE
);
82 /* Execute this program, passing all the URIs in the list as arguments.
83 * URIs that are files on the local machine will be passed as simple
84 * pathnames. The uri_list should be freed after this function returns.
86 void run_with_files(const char *path
, GList
*uri_list
)
92 if (stat(path
, &info
))
94 delayed_error(_("Program %s not found - deleted?"), path
);
98 argv
= g_malloc(sizeof(char *) * (g_list_length(uri_list
) + 2));
100 if (S_ISDIR(info
.st_mode
))
101 argv
[argc
++] = make_path(path
, "AppRun")->str
;
107 const char *uri
= uri_list
->data
;
110 local
= get_local_path(uri
);
112 argv
[argc
++] = local
;
115 uri_list
= uri_list
->next
;
120 rox_spawn(home_dir
, argv
);
123 /* Run the program as '<path> -', piping the data to it via stdin.
124 * You can g_free() the data as soon as this returns.
126 void run_with_data(const char *path
, gpointer data
, gulong length
)
128 const char *argv
[] = {NULL
, "-", NULL
};
133 if (stat(path
, &info
))
135 delayed_error(_("Program %s not found - deleted?"), path
);
139 if (S_ISDIR(info
.st_mode
))
140 argv
[0] = make_path(path
, "AppRun")->str
;
146 delayed_error("pipe: %s", g_strerror(errno
));
149 close_on_exec(fds
[1], TRUE
);
150 close_on_exec(fds
[0], TRUE
);
155 delayed_error("fork: %s", g_strerror(errno
));
159 /* We are the child */
161 if (dup2(fds
[0], 0) == -1)
162 g_warning("dup2() failed: %s\n",
166 close_on_exec(0, FALSE
);
167 if (execv(argv
[0], (char **) argv
))
168 g_warning("execv(%s) failed: %s\n",
169 argv
[0], g_strerror(errno
));
173 /* We are the parent */
174 set_blocking(fds
[1], FALSE
);
175 pd
= g_new(PipedData
, 1);
176 pd
->data
= g_malloc(length
);
177 memcpy(pd
->data
, data
, length
);
180 pd
->tag
= gtk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
181 write_data
, NULL
, pd
, NULL
);
188 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
189 * edit a file, open an application, follow a symlink or mount a device.
191 * filer_window is the window to use for displaying a directory.
192 * NULL will always use a new directory when needed.
193 * src_window is the window to copy options from, or NULL.
195 * Returns TRUE on success.
197 gboolean
run_diritem(const guchar
*full_path
,
199 FilerWindow
*filer_window
,
200 FilerWindow
*src_window
,
203 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
204 return follow_symlink(full_path
, filer_window
, src_window
);
206 switch (item
->base_type
)
209 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
215 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
&& edit
)
219 paths
= g_list_prepend(NULL
,
220 (gpointer
) full_path
);
221 action_mount(paths
, filer_window
== NULL
, -1);
223 if (item
->flags
& ITEM_FLAG_MOUNTED
||
229 filer_change_to(filer_window
, full_path
, NULL
);
231 filer_opendir(full_path
, src_window
);
234 if ((item
->mime_type
== application_executable
) && !edit
)
236 const char *argv
[] = {NULL
, NULL
};
237 guchar
*dir
= filer_window
238 ? filer_window
->sym_path
243 return rox_spawn(dir
, argv
);
246 return open_file(full_path
, edit
? text_plain
249 delayed_error(_("File doesn't exist, or I can't "
250 "access it: %s"), full_path
);
254 _("I don't know how to open '%s'"), full_path
);
259 /* Attempt to open this item */
260 gboolean
run_by_path(const guchar
*full_path
)
265 /* XXX: Loads an image - wasteful */
266 item
= diritem_new("");
267 diritem_restat(full_path
, item
, NULL
);
268 retval
= run_diritem(full_path
, item
, NULL
, NULL
, FALSE
);
274 void show_item_help(const guchar
*path
, DirItem
*item
)
276 switch (item
->base_type
)
279 if (item
->flags
& ITEM_FLAG_EXEC_FILE
)
281 _("Executable file:\n"
282 "This is a file with an eXecute bit "
283 "set - it can be run as a program."));
287 "This is a data file. Try using the "
288 "Info menu item to find out more..."));
291 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
294 "A mount point is a directory which another "
295 "filing system can be mounted on. Everything "
296 "on the mounted filesystem then appears to be "
297 "inside the directory."));
299 dir_show_help(item
, path
);
301 case TYPE_CHAR_DEVICE
:
302 case TYPE_BLOCK_DEVICE
:
305 "Device files allow you to read from or write "
306 "to a device driver as though it was an "
312 "Pipes allow different programs to "
313 "communicate. One program writes data to the "
314 "pipe while another one reads it out again."));
319 "Sockets allow processes to communicate."));
324 "I couldn't find out what kind of file this "
325 "is. Maybe it doesn't exist anymore or you "
326 "don't have search permission on the directory "
332 /* Open a directory viewer showing this file, and wink it */
333 void open_to_show(const guchar
*path
)
338 g_return_if_fail(path
!= NULL
);
340 dir
= g_strdup(path
);
341 slash
= strrchr(dir
, '/');
342 if (slash
== dir
|| !slash
)
344 /* Item in the root (or root itself!) */
345 new = filer_opendir("/", NULL
);
347 display_set_autoselect(new, dir
+ 1);
352 new = filer_opendir(dir
, NULL
);
356 display_set_hidden(new, TRUE
);
357 display_set_autoselect(new, slash
+ 1);
364 /* Invoked using -x, this indicates that the filesystem has been modified
365 * and we should look at this item again.
367 void examine(const guchar
*path
)
371 if (mc_stat(path
, &info
) != 0)
373 /* Deleted? Do a paranoid update of everything... */
374 filer_check_mounted(path
);
378 /* Update directory containing this item... */
379 dir_check_this(path
);
381 /* If this is itself a directory then rescan its contents... */
382 if (S_ISDIR(info
.st_mode
))
385 /* If it's on the pinboard, update the icon... */
386 icons_may_update(path
);
390 /****************************************************************
391 * INTERNAL FUNCTIONS *
392 ****************************************************************/
395 static void write_data(gpointer data
, gint fd
, GdkInputCondition cond
)
397 PipedData
*pd
= (PipedData
*) data
;
399 while (pd
->sent
< pd
->length
)
403 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
409 delayed_error(_("Could not send data to program: %s"),
418 g_source_remove(pd
->tag
);
424 /* Follow the link 'full_path' and display it in filer_window, or a
425 * new window if that is NULL.
427 static gboolean
follow_symlink(const char *full_path
,
428 FilerWindow
*filer_window
,
429 FilerWindow
*src_window
)
433 char path
[MAXPATHLEN
+ 1];
436 got
= readlink(full_path
, path
, MAXPATHLEN
);
439 delayed_error(_("Could not read link: %s"),
444 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
447 /* Make a relative path absolute */
451 slash
= strrchr(full_path
, '/');
452 g_return_val_if_fail(slash
!= NULL
, FALSE
);
454 tmp
= g_strndup(full_path
, slash
- full_path
);
455 real
= pathdup(make_path(tmp
, path
)->str
);
456 /* NB: full_path may be invalid here... */
460 real
= pathdup(path
);
462 slash
= strrchr(real
, '/');
467 _("Broken symlink (or you don't have permission "
468 "to follow it): %s"), full_path
);
480 filer_change_to(filer_window
, new_dir
, slash
+ 1);
485 new = filer_opendir(new_dir
, src_window
);
487 display_set_autoselect(new, slash
+ 1);
495 /* Load this file into an appropriate editor */
496 static gboolean
open_file(const guchar
*path
, MIME_type
*type
)
498 g_return_val_if_fail(type
!= NULL
, FALSE
);
500 if (type_open(path
, type
))
504 _("No run action specified for files of this type (%s/%s) - "
505 "you can set a run action by choosing `Set Run Action' "
506 "from the File menu, or you can just drag the file to an "
514 /* Show the help for a directory - tries to open App/Help, but if
515 * that doesn't work then it displays a default message.
517 static void dir_show_help(DirItem
*item
, const char *path
)
522 help_dir
= g_strconcat(path
, "/Help", NULL
);
524 if (mc_stat(help_dir
, &info
) == 0)
525 filer_opendir(help_dir
, 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."));