4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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 char *uri
= uri_list
->data
;
112 local
= get_local_path(uri
);
114 argv
[argc
++] = local
;
117 uri_list
= uri_list
->next
;
122 rox_spawn(home_dir
, argv
);
125 /* Run the program as '<path> -', piping the data to it via stdin.
126 * You can g_free() the data as soon as this returns.
128 void run_with_data(const char *path
, gpointer data
, gulong length
)
130 const char *argv
[] = {NULL
, "-", NULL
};
135 if (stat(path
, &info
))
137 delayed_error(_("Program %s not found - deleted?"), path
);
141 if (S_ISDIR(info
.st_mode
))
142 argv
[0] = make_path(path
, "AppRun");
148 delayed_error("pipe: %s", g_strerror(errno
));
151 close_on_exec(fds
[1], TRUE
);
152 close_on_exec(fds
[0], TRUE
);
157 delayed_error("fork: %s", g_strerror(errno
));
161 /* We are the child */
163 if (dup2(fds
[0], 0) == -1)
164 g_warning("dup2() failed: %s\n",
168 close_on_exec(0, FALSE
);
169 if (execv(argv
[0], (char **) argv
))
170 g_warning("execv(%s) failed: %s\n",
171 argv
[0], g_strerror(errno
));
175 /* We are the parent */
176 set_blocking(fds
[1], FALSE
);
177 pd
= g_new(PipedData
, 1);
178 pd
->data
= g_malloc(length
);
179 memcpy(pd
->data
, data
, length
);
182 pd
->tag
= gtk_input_add_full(fds
[1], GDK_INPUT_WRITE
,
183 write_data
, NULL
, pd
, NULL
);
190 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
191 * edit a file, open an application, follow a symlink or mount a device.
193 * filer_window is the window to use for displaying a directory.
194 * NULL will always use a new directory when needed.
195 * src_window is the window to copy options from, or NULL.
197 * Returns TRUE on success.
199 gboolean
run_diritem(const guchar
*full_path
,
201 FilerWindow
*filer_window
,
202 FilerWindow
*src_window
,
205 if (item
->flags
& ITEM_FLAG_SYMLINK
&& edit
)
206 return follow_symlink(full_path
, filer_window
, src_window
);
208 switch (item
->base_type
)
211 if (item
->flags
& ITEM_FLAG_APPDIR
&& !edit
)
217 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
219 open_mountpoint(full_path
, item
,
220 filer_window
, src_window
, edit
);
222 else if (filer_window
)
223 filer_change_to(filer_window
, full_path
, NULL
);
225 filer_opendir(full_path
, src_window
, NULL
);
228 if (item
->mime_type
== application_executable
&& !edit
)
230 const char *argv
[] = {NULL
, NULL
};
231 guchar
*dir
= filer_window
232 ? filer_window
->sym_path
237 return rox_spawn(dir
, argv
) != 0;
240 return open_file(full_path
, edit
? text_plain
243 delayed_error(_("File doesn't exist, or I can't "
244 "access it: %s"), full_path
);
248 _("I don't know how to open '%s'"), full_path
);
253 /* Attempt to open this item */
254 gboolean
run_by_path(const guchar
*full_path
)
259 /* XXX: Loads an image - wasteful */
260 item
= diritem_new("");
261 diritem_restat(full_path
, item
, NULL
);
262 retval
= run_diritem(full_path
, item
, NULL
, NULL
, FALSE
);
268 /* Open dir/Help, or show a message if missing */
269 void show_help_files(const char *dir
)
271 const char *help_dir
;
273 help_dir
= make_path(dir
, "Help");
275 if (file_exists(help_dir
))
276 filer_opendir(help_dir
, NULL
, NULL
);
280 "This is an application directory - you can "
281 "run it as a program, or open it (hold down "
282 "Shift while you open it). Most applications provide "
283 "their own help here, but this one doesn't."));
286 /* Open a directory viewer showing this file, and wink it */
287 void open_to_show(const guchar
*path
)
292 g_return_if_fail(path
!= NULL
);
294 dir
= g_strdup(path
);
295 slash
= strrchr(dir
, '/');
296 if (slash
== dir
|| !slash
)
298 /* Item in the root (or root itself!) */
299 new = filer_opendir("/", NULL
, NULL
);
301 display_set_autoselect(new, dir
+ 1);
306 new = filer_opendir(dir
, NULL
, NULL
);
310 display_set_hidden(new, TRUE
);
311 display_set_autoselect(new, slash
+ 1);
318 /* Invoked using -x, this indicates that the filesystem has been modified
319 * and we should look at this item again.
321 void examine(const guchar
*path
)
325 if (mc_stat(path
, &info
) != 0)
327 /* Deleted? Do a paranoid update of everything... */
328 filer_check_mounted(path
);
332 /* Update directory containing this item... */
333 dir_check_this(path
);
335 /* If this is itself a directory then rescan its contents... */
336 if (S_ISDIR(info
.st_mode
))
339 /* If it's on the pinboard, update the icon... */
340 icons_may_update(path
);
344 /****************************************************************
345 * INTERNAL FUNCTIONS *
346 ****************************************************************/
349 static void write_data(gpointer data
, gint fd
, GdkInputCondition cond
)
351 PipedData
*pd
= (PipedData
*) data
;
353 while (pd
->sent
< pd
->length
)
357 sent
= write(fd
, pd
->data
+ pd
->sent
, pd
->length
- pd
->sent
);
363 delayed_error(_("Could not send data to program: %s"),
372 g_source_remove(pd
->tag
);
378 /* Follow the link 'full_path' and display it in filer_window, or a
379 * new window if that is NULL.
381 static gboolean
follow_symlink(const char *full_path
,
382 FilerWindow
*filer_window
,
383 FilerWindow
*src_window
)
387 char path
[MAXPATHLEN
+ 1];
390 got
= readlink(full_path
, path
, MAXPATHLEN
);
393 delayed_error(_("Could not read link: %s"),
398 g_return_val_if_fail(got
<= MAXPATHLEN
, FALSE
);
401 /* Make a relative path absolute */
405 slash
= strrchr(full_path
, '/');
406 g_return_val_if_fail(slash
!= NULL
, FALSE
);
408 tmp
= g_strndup(full_path
, slash
- full_path
);
409 real
= pathdup(make_path(tmp
, path
));
410 /* NB: full_path may be invalid here... */
414 real
= pathdup(path
);
416 slash
= strrchr(real
, '/');
421 _("Broken symlink (or you don't have permission "
422 "to follow it): %s"), full_path
);
434 filer_change_to(filer_window
, new_dir
, slash
+ 1);
439 new = filer_opendir(new_dir
, src_window
, NULL
);
441 display_set_autoselect(new, slash
+ 1);
449 /* Load this file into an appropriate editor */
450 static gboolean
open_file(const guchar
*path
, MIME_type
*type
)
452 g_return_val_if_fail(type
!= NULL
, FALSE
);
454 if (type_open(path
, type
))
458 _("No run action specified for files of this type (%s/%s) - "
459 "you can set a run action by choosing `Set Run Action' "
460 "from the File menu, or you can just drag the file to an "
468 /* Called like run_diritem, when a mount-point is opened */
469 static void open_mountpoint(const guchar
*full_path
, DirItem
*item
,
470 FilerWindow
*filer_window
, FilerWindow
*src_window
,
473 gboolean mounted
= (item
->flags
& ITEM_FLAG_MOUNTED
) != 0;
479 paths
= g_list_prepend(NULL
, (gpointer
) full_path
);
480 action_mount(paths
, filer_window
== NULL
, -1);
482 if (filer_window
&& !mounted
)
483 filer_change_to(filer_window
, full_path
, NULL
);
488 filer_change_to(filer_window
, full_path
, NULL
);
490 filer_opendir(full_path
, src_window
, NULL
);