r189: Effective permissions are shown in brackets.
[rox-filer.git] / ROX-Filer / src / menu.c
blob1f50c7c6c42dab6f58ba16c4c097441ac162e959
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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)
10 * any later version.
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
15 * more details.
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
22 /* menu.c - code for handling the popup menu */
24 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <time.h>
36 #include <gdk/gdkx.h>
37 #include <gtk/gtk.h>
39 #include "run.h"
40 #include "action.h"
41 #include "filer.h"
42 #include "type.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "options.h"
46 #include "choices.h"
47 #include "savebox.h"
48 #include "mount.h"
49 #include "minibuffer.h"
51 #define C_ "<control>"
53 #define MENU_MARGIN 32
55 GtkAccelGroup *filer_keys;
56 GtkAccelGroup *panel_keys;
58 static GtkWidget *popup_menu = NULL; /* Currently open menu */
60 /* Options */
61 static GtkWidget *xterm_here_entry;
62 static char *xterm_here_value;
64 /* Static prototypes */
66 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data);
67 static void menu_closed(GtkWidget *widget);
68 static void items_sensitive(GtkWidget *menu, int from, int n, gboolean state);
69 static char *load_xterm_here(char *data);
71 /* Note that for these callbacks none of the arguments are used. */
72 static void not_yet(gpointer data, guint action, GtkWidget *widget);
74 static void large(gpointer data, guint action, GtkWidget *widget);
75 static void small(gpointer data, guint action, GtkWidget *widget);
76 static void full_info(gpointer data, guint action, GtkWidget *widget);
78 static void sort_name(gpointer data, guint action, GtkWidget *widget);
79 static void sort_type(gpointer data, guint action, GtkWidget *widget);
80 static void sort_size(gpointer data, guint action, GtkWidget *widget);
81 static void sort_date(gpointer data, guint action, GtkWidget *widget);
83 static void hidden(gpointer data, guint action, GtkWidget *widget);
84 static void refresh(gpointer data, guint action, GtkWidget *widget);
86 static void copy_item(gpointer data, guint action, GtkWidget *widget);
87 static void rename_item(gpointer data, guint action, GtkWidget *widget);
88 static void link_item(gpointer data, guint action, GtkWidget *widget);
89 static void open_file(gpointer data, guint action, GtkWidget *widget);
90 static void help(gpointer data, guint action, GtkWidget *widget);
91 static void show_file_info(gpointer data, guint action, GtkWidget *widget);
92 static void mount(gpointer data, guint action, GtkWidget *widget);
93 static void delete(gpointer data, guint action, GtkWidget *widget);
94 static void usage(gpointer data, guint action, GtkWidget *widget);
96 static void select_all(gpointer data, guint action, GtkWidget *widget);
97 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
98 static void show_options(gpointer data, guint action, GtkWidget *widget);
99 static void new_directory(gpointer data, guint action, GtkWidget *widget);
100 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
102 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
103 static void open_parent(gpointer data, guint action, GtkWidget *widget);
104 static void new_window(gpointer data, guint action, GtkWidget *widget);
105 static void close_window(gpointer data, guint action, GtkWidget *widget);
106 static void enter_path(gpointer data, guint action, GtkWidget *widget);
107 static void rox_help(gpointer data, guint action, GtkWidget *widget);
109 static void open_as_dir(gpointer data, guint action, GtkWidget *widget);
110 static void close_panel(gpointer data, guint action, GtkWidget *widget);
112 static GtkWidget *create_options();
113 static void update_options();
114 static void set_options();
115 static void save_options();
117 static OptionsSection options =
119 "Menu options",
120 create_options,
121 update_options,
122 set_options,
123 save_options
127 static GtkWidget *filer_menu; /* The popup filer menu */
128 static GtkWidget *filer_file_item; /* The File '' label */
129 static GtkWidget *filer_file_menu; /* The File '' menu */
130 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
131 static GtkWidget *panel_menu; /* The popup panel menu */
132 static GtkWidget *panel_file_item; /* The File '' label */
133 static GtkWidget *panel_file_menu; /* The File '' menu */
134 static GtkWidget *panel_hidden_menu; /* The Show Hidden item */
136 static gint screen_width, screen_height;
138 static GtkItemFactoryEntry filer_menu_def[] = {
139 {"/Display", NULL, NULL, 0, "<Branch>"},
140 {"/Display/Large Icons", NULL, large, 0, NULL},
141 {"/Display/Small Icons", NULL, small, 0, NULL},
142 {"/Display/Full Info", NULL, full_info, 0, NULL},
143 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
144 {"/Display/Sort by Name", NULL, sort_name, 0, NULL},
145 {"/Display/Sort by Type", NULL, sort_type, 0, NULL},
146 {"/Display/Sort by Date", NULL, sort_date, 0, NULL},
147 {"/Display/Sort by Size", NULL, sort_size, 0, NULL},
148 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
149 {"/Display/Show Hidden", C_"H", hidden, 0, NULL},
150 {"/Display/Refresh", C_"L", refresh, 0, NULL},
151 {"/File", NULL, NULL, 0, "<Branch>"},
152 {"/File/Copy...", NULL, copy_item, 0, NULL},
153 {"/File/Rename...", NULL, rename_item, 0, NULL},
154 {"/File/Link...", NULL, link_item, 0, NULL},
155 {"/File/Shift Open", NULL, open_file, 0, NULL},
156 {"/File/Help", "F1", help, 0, NULL},
157 {"/File/Info", "I", show_file_info, 0, NULL},
158 {"/File/Separator", NULL, NULL, 0, "<Separator>"},
159 {"/File/Mount", "M", mount, 0, NULL},
160 {"/File/Delete", C_"X", delete, 0, NULL},
161 {"/File/Disk Usage", "U", usage, 0, NULL},
162 {"/File/Permissions", NULL, not_yet, 0, NULL},
163 {"/File/Touch", NULL, not_yet, 0, NULL},
164 {"/File/Find", NULL, not_yet, 0, NULL},
165 {"/Select All", C_"A", select_all, 0, NULL},
166 {"/Clear Selection", C_"Z", clear_selection, 0, NULL},
167 {"/Options...", NULL, show_options, 0, NULL},
168 {"/New Directory...", NULL, new_directory, 0, NULL},
169 {"/Xterm Here", NULL, xterm_here, 0, NULL},
170 {"/Window", NULL, NULL, 0, "<Branch>"},
171 {"/Window/Parent, New Window", NULL, open_parent, 0, NULL},
172 {"/Window/Parent, Same Window", NULL, open_parent_same, 0, NULL},
173 {"/Window/New Window", NULL, new_window, 0, NULL},
174 {"/Window/Close Window", C_"Q", close_window, 0, NULL},
175 {"/Window/Enter Path", NULL, enter_path, 0, NULL},
176 {"/Window/Separator", NULL, NULL, 0, "<Separator>"},
177 {"/Window/Show ROX-Filer help", NULL, rox_help, 0, NULL},
180 static GtkItemFactoryEntry panel_menu_def[] = {
181 {"/Display", NULL, NULL, 0, "<Branch>"},
182 {"/Display/Large Icons", NULL, large, 0, NULL},
183 {"/Display/Small Icons", NULL, small, 0, NULL},
184 {"/Display/Full Info", NULL, full_info, 0, NULL},
185 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
186 {"/Display/Sort by Name", NULL, sort_name, 0, NULL},
187 {"/Display/Sort by Type", NULL, sort_type, 0, NULL},
188 {"/Display/Sort by Date", NULL, sort_date, 0, NULL},
189 {"/Display/Sort by Size", NULL, sort_size, 0, NULL},
190 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
191 {"/Display/Show Hidden", NULL, hidden, 0, NULL},
192 {"/Display/Refresh", NULL, refresh, 0, NULL},
193 {"/File", NULL, NULL, 0, "<Branch>"},
194 {"/File/Help", NULL, help, 0, NULL},
195 {"/File/Info", NULL, show_file_info, 0, NULL},
196 {"/File/Delete", NULL, delete, 0, NULL},
197 {"/Open as directory", NULL, open_as_dir, 0, NULL},
198 {"/Options...", NULL, show_options, 0, NULL},
199 {"/Close panel", NULL, close_panel, 0, NULL},
200 {"/Separator", NULL, NULL, 0, "<Separator>"},
201 {"/Show ROX-Filer help", NULL, rox_help, 0, NULL},
204 typedef struct _FileStatus FileStatus;
206 /* This is for the 'file(1) says...' thing */
207 struct _FileStatus
209 int fd; /* FD to read from, -1 if closed */
210 int input; /* Input watcher tag if fd valid */
211 GtkLabel *label; /* Widget to output to */
212 gboolean start; /* No output yet */
215 void menu_init()
217 GtkItemFactory *item_factory;
218 char *menurc;
219 GList *items;
221 /* This call starts returning strange values after a while, so get
222 * the result here during init.
224 gdk_window_get_size(GDK_ROOT_PARENT(), &screen_width, &screen_height);
226 filer_keys = gtk_accel_group_new();
227 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
228 "<filer>",
229 filer_keys);
230 gtk_item_factory_create_items(item_factory,
231 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
232 filer_menu_def,
233 NULL);
234 filer_menu = gtk_item_factory_get_widget(item_factory, "<filer>");
235 filer_file_menu = gtk_item_factory_get_widget(item_factory,
236 "<filer>/File");
237 filer_hidden_menu = gtk_item_factory_get_widget(item_factory,
238 "<filer>/Display/Show Hidden");
239 items = gtk_container_children(GTK_CONTAINER(filer_menu));
240 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
241 g_list_free(items);
243 panel_keys = gtk_accel_group_new();
244 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
245 "<panel>",
246 panel_keys);
247 gtk_item_factory_create_items(item_factory,
248 sizeof(panel_menu_def) / sizeof(*panel_menu_def),
249 panel_menu_def,
250 NULL);
251 panel_menu = gtk_item_factory_get_widget(item_factory, "<panel>");
252 panel_file_menu = gtk_item_factory_get_widget(item_factory,
253 "<panel>/File");
254 panel_hidden_menu = gtk_item_factory_get_widget(item_factory,
255 "<panel>/Display/Show Hidden");
256 items = gtk_container_children(GTK_CONTAINER(panel_menu));
257 panel_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
258 g_list_free(items);
260 menurc = choices_find_path_load("menus");
261 if (menurc)
262 gtk_item_factory_parse_rc(menurc);
264 gtk_accel_group_lock(panel_keys);
266 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
267 GTK_SIGNAL_FUNC(menu_closed), NULL);
268 gtk_signal_connect(GTK_OBJECT(panel_menu), "unmap_event",
269 GTK_SIGNAL_FUNC(menu_closed), NULL);
270 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
271 GTK_SIGNAL_FUNC(menu_closed), NULL);
273 options_sections = g_slist_prepend(options_sections, &options);
274 xterm_here_value = g_strdup("xterm");
275 option_register("xterm_here", load_xterm_here);
278 /* Build up some option widgets to go in the options dialog, but don't
279 * fill them in yet.
281 static GtkWidget *create_options()
283 GtkWidget *table, *label;
285 table = gtk_table_new(2, 2, FALSE);
286 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
288 label = gtk_label_new("To set the keyboard short-cuts you simply open "
289 "the menu over a filer window, move the pointer over "
290 "the item you want to use and press a key. The key "
291 "will appear next to the menu item and you can just "
292 "press that key without opening the menu in future. "
293 "To save the current menu short-cuts for next time, "
294 "click the Save button at the bottom of this window.");
295 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
296 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 2, 0, 1);
298 label = gtk_label_new("'Xterm here' program:");
299 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
300 xterm_here_entry = gtk_entry_new();
301 gtk_table_attach_defaults(GTK_TABLE(table), xterm_here_entry,
302 1, 2, 1, 2);
304 return table;
307 static char *load_xterm_here(char *data)
309 g_free(xterm_here_value);
310 xterm_here_value = g_strdup(data);
311 return NULL;
314 static void update_options()
316 gtk_entry_set_text(GTK_ENTRY(xterm_here_entry), xterm_here_value);
319 static void set_options()
321 g_free(xterm_here_value);
322 xterm_here_value = g_strdup(gtk_entry_get_text(
323 GTK_ENTRY(xterm_here_entry)));
326 static void save_options()
328 char *menurc;
330 menurc = choices_find_path_save("menus", TRUE);
331 if (menurc)
332 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
334 option_write("xterm_here", xterm_here_value);
338 static void items_sensitive(GtkWidget *menu, int from, int n, gboolean state)
340 GList *items, *item;
342 items = gtk_container_children(GTK_CONTAINER(menu));
344 item = g_list_nth(items, from);
345 while (item && n--)
347 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
348 item = item->next;
351 g_list_free(items);
354 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data)
356 int *pos = (int *) data;
357 GtkRequisition requisition;
359 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
361 if (pos[0] == -1)
362 *x = screen_width - MENU_MARGIN - requisition.width;
363 else if (pos[0] == -2)
364 *x = MENU_MARGIN;
365 else
366 *x = pos[0] - (requisition.width >> 2);
368 if (pos[1] == -1)
369 *y = screen_height - MENU_MARGIN - requisition.height;
370 else if (pos[1] == -2)
371 *y = MENU_MARGIN;
372 else
373 *y = pos[1] - (requisition.height >> 2);
375 *x = CLAMP(*x, 0, screen_width - requisition.width);
376 *y = CLAMP(*y, 0, screen_height - requisition.height);
379 void show_filer_menu(FilerWindow *filer_window, GdkEventButton *event,
380 int item)
382 GString *buffer;
383 GtkWidget *file_label, *file_menu;
384 DirItem *file_item;
385 int pos[2];
387 pos[0] = event->x_root;
388 pos[1] = event->y_root;
390 window_with_focus = filer_window;
392 if (filer_window->panel)
394 switch (filer_window->panel_side)
396 case TOP: pos[1] = -2; break;
397 case BOTTOM: pos[1] = -1; break;
398 case LEFT: pos[0] = -2; break;
399 case RIGHT: pos[0] = -1; break;
403 if (filer_window->panel)
404 collection_clear_selection(filer_window->collection); /* ??? */
406 if (filer_window->collection->number_selected == 0 && item >= 0)
408 collection_select_item(filer_window->collection, item);
409 filer_window->temp_item_selected = TRUE;
411 else
412 filer_window->temp_item_selected = FALSE;
414 if (filer_window->panel)
416 file_label = panel_file_item;
417 file_menu = panel_file_menu;
419 else
421 file_label = filer_file_item;
422 file_menu = filer_file_menu;
425 buffer = g_string_new(NULL);
426 switch (filer_window->collection->number_selected)
428 case 0:
429 g_string_assign(buffer, "Next Click");
430 items_sensitive(file_menu, 0, 6, TRUE);
431 break;
432 case 1:
433 items_sensitive(file_menu, 0, 6, TRUE);
434 file_item = selected_item(filer_window->collection);
435 g_string_sprintf(buffer, "%s '%s'",
436 basetype_name(file_item),
437 file_item->leafname);
438 break;
439 default:
440 items_sensitive(file_menu, 0, 6, FALSE);
441 g_string_sprintf(buffer, "%d items",
442 filer_window->collection->number_selected);
443 break;
446 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
448 g_string_free(buffer, TRUE);
450 if (filer_window->panel)
451 popup_menu = panel_menu;
452 else
453 popup_menu = (event->state & GDK_CONTROL_MASK)
454 ? filer_file_menu
455 : filer_menu;
457 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
458 (gpointer) pos, event->button, event->time);
461 static void menu_closed(GtkWidget *widget)
463 if (window_with_focus == NULL || widget != popup_menu)
464 return; /* Close panel item chosen? */
466 if (window_with_focus->temp_item_selected)
468 collection_clear_selection(window_with_focus->collection);
469 window_with_focus->temp_item_selected = FALSE;
473 void target_callback(Collection *collection, gint item, gpointer real_fn)
475 g_return_if_fail(window_with_focus != NULL);
476 g_return_if_fail(window_with_focus->collection == collection);
477 g_return_if_fail(real_fn != NULL);
479 collection_wink_item(collection, item);
480 collection_clear_selection(collection);
481 collection_select_item(collection, item);
482 ((CollectionTargetFunc)real_fn)(NULL, 0, collection);
483 if (item < collection->number_of_items)
484 collection_unselect_item(collection, item);
487 /* Actions */
489 /* Fake action to warn when a menu item does nothing */
490 static void not_yet(gpointer data, guint action, GtkWidget *widget)
492 delayed_error("ROX-Filer", "Sorry, that feature isn't implemented yet");
495 static void large(gpointer data, guint action, GtkWidget *widget)
497 g_return_if_fail(window_with_focus != NULL);
499 filer_style_set(window_with_focus, LARGE_ICONS);
502 static void small(gpointer data, guint action, GtkWidget *widget)
504 g_return_if_fail(window_with_focus != NULL);
506 filer_style_set(window_with_focus, SMALL_ICONS);
509 static void full_info(gpointer data, guint action, GtkWidget *widget)
511 g_return_if_fail(window_with_focus != NULL);
513 filer_style_set(window_with_focus, FULL_INFO);
516 static void sort_name(gpointer data, guint action, GtkWidget *widget)
518 g_return_if_fail(window_with_focus != NULL);
520 filer_set_sort_fn(window_with_focus, sort_by_name);
523 static void sort_type(gpointer data, guint action, GtkWidget *widget)
525 g_return_if_fail(window_with_focus != NULL);
527 filer_set_sort_fn(window_with_focus, sort_by_type);
530 static void sort_date(gpointer data, guint action, GtkWidget *widget)
532 g_return_if_fail(window_with_focus != NULL);
534 filer_set_sort_fn(window_with_focus, sort_by_date);
537 static void sort_size(gpointer data, guint action, GtkWidget *widget)
539 g_return_if_fail(window_with_focus != NULL);
541 filer_set_sort_fn(window_with_focus, sort_by_size);
544 static void hidden(gpointer data, guint action, GtkWidget *widget)
546 g_return_if_fail(window_with_focus != NULL);
548 filer_set_hidden(window_with_focus, !window_with_focus->show_hidden);
551 static void refresh(gpointer data, guint action, GtkWidget *widget)
553 g_return_if_fail(window_with_focus != NULL);
555 full_refresh();
556 update_dir(window_with_focus, TRUE);
559 static void mount(gpointer data, guint action, GtkWidget *widget)
561 g_return_if_fail(window_with_focus != NULL);
563 if (window_with_focus->collection->number_selected == 0)
564 collection_target(window_with_focus->collection,
565 target_callback, mount);
566 else
567 action_mount(window_with_focus, NULL);
570 static void delete(gpointer data, guint action, GtkWidget *widget)
572 g_return_if_fail(window_with_focus != NULL);
574 if (window_with_focus->collection->number_selected == 0)
575 collection_target(window_with_focus->collection,
576 target_callback, delete);
577 else
578 action_delete(window_with_focus);
581 static void usage(gpointer data, guint action, GtkWidget *widget)
583 g_return_if_fail(window_with_focus != NULL);
585 if (window_with_focus->collection->number_selected == 0)
586 collection_target(window_with_focus->collection,
587 target_callback, usage);
588 else
589 action_usage(window_with_focus);
592 static gboolean copy_cb(char *initial, char *path)
594 char *new_dir, *slash;
595 int len;
596 GString *command;
597 gboolean retval = TRUE;
599 slash = strrchr(path, '/');
600 if (!slash)
602 report_error("ROX-Filer", "Missing '/' in new pathname");
603 return FALSE;
606 if (access(path, F_OK) == 0)
608 report_error("ROX-Filer",
609 "An item with this name already exists");
610 return FALSE;
613 len = slash - path;
614 new_dir = g_malloc(len + 1);
615 memcpy(new_dir, path, len);
616 new_dir[len] = '\0';
618 command = g_string_new(NULL);
619 g_string_sprintf(command, "cp -a %s %s", initial, path);
620 /* XXX: Use system. In fact, use action! */
622 if (system(command->str))
624 g_string_append(command, " failed!");
625 report_error("ROX-Filer", command->str);
626 retval = FALSE;
629 g_string_free(command, TRUE);
631 refresh_dirs(new_dir);
632 return retval;
635 static void copy_item(gpointer data, guint action, GtkWidget *widget)
637 Collection *collection;
639 g_return_if_fail(window_with_focus != NULL);
641 collection = window_with_focus->collection;
642 if (collection->number_selected > 1)
644 report_error("ROX-Filer", "You cannot do this to more than "
645 "one item at a time");
646 return;
648 else if (collection->number_selected != 1)
649 collection_target(collection, target_callback, copy_item);
650 else
652 DirItem *item = selected_item(collection);
654 savebox_show(window_with_focus, "Copy",
655 window_with_focus->path,
656 item->leafname,
657 item->image, copy_cb);
661 static gboolean rename_cb(char *initial, char *path)
663 if (rename(initial, path))
665 report_error("ROX-Filer: rename()", g_strerror(errno));
666 return FALSE;
668 return TRUE;
671 static void rename_item(gpointer data, guint action, GtkWidget *widget)
673 Collection *collection;
675 g_return_if_fail(window_with_focus != NULL);
677 collection = window_with_focus->collection;
678 if (collection->number_selected > 1)
680 report_error("ROX-Filer", "You cannot do this to more than "
681 "one item at a time");
682 return;
684 else if (collection->number_selected != 1)
685 collection_target(collection, target_callback, rename_item);
686 else
688 DirItem *item = selected_item(collection);
690 savebox_show(window_with_focus, "Rename",
691 window_with_focus->path,
692 item->leafname,
693 item->image, rename_cb);
697 static gboolean link_cb(char *initial, char *path)
699 if (symlink(initial, path))
701 report_error("ROX-Filer: symlink()", g_strerror(errno));
702 return FALSE;
704 return TRUE;
707 static void link_item(gpointer data, guint action, GtkWidget *widget)
709 Collection *collection;
711 g_return_if_fail(window_with_focus != NULL);
713 collection = window_with_focus->collection;
714 if (collection->number_selected > 1)
716 report_error("ROX-Filer", "You cannot do this to more than "
717 "one item at a time");
718 return;
720 else if (collection->number_selected != 1)
721 collection_target(collection, target_callback, link_item);
722 else
724 DirItem *item = selected_item(collection);
726 savebox_show(window_with_focus, "Symlink",
727 window_with_focus->path,
728 item->leafname,
729 item->image, link_cb);
733 static void open_file(gpointer data, guint action, GtkWidget *widget)
735 Collection *collection;
737 g_return_if_fail(window_with_focus != NULL);
739 collection = window_with_focus->collection;
740 if (collection->number_selected > 1)
742 report_error("ROX-Filer", "You cannot do this to more than "
743 "one item at a time");
744 return;
746 else if (collection->number_selected != 1)
747 collection_target(collection, target_callback, open_file);
748 else
749 filer_openitem(window_with_focus,
750 selected_item_number(collection),
751 OPEN_SAME_WINDOW | OPEN_SHIFT);
754 /* Got some data from file(1) - stick it in the window. */
755 static void add_file_output(FileStatus *fs,
756 gint source, GdkInputCondition condition)
758 char buffer[20];
759 char *str;
760 int got;
762 got = read(source, buffer, sizeof(buffer) - 1);
763 if (got <= 0)
765 int err = errno;
766 gtk_input_remove(fs->input);
767 close(source);
768 fs->fd = -1;
769 if (got < 0)
770 delayed_error("ROX-Filer: file(1) says...",
771 g_strerror(err));
772 return;
774 buffer[got] = '\0';
776 if (fs->start)
778 str = "";
779 fs->start = FALSE;
781 else
782 gtk_label_get(fs->label, &str);
784 str = g_strconcat(str, buffer, NULL);
785 gtk_label_set_text(fs->label, str);
786 g_free(str);
789 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs)
791 if (fs->fd != -1)
793 gtk_input_remove(fs->input);
794 close(fs->fd);
797 g_free(fs);
800 static void show_file_info(gpointer data, guint action, GtkWidget *widget)
802 GtkWidget *window, *table, *label, *button, *frame;
803 GtkWidget *file_label;
804 GString *gstring;
805 char *string;
806 int file_data[2];
807 char *path;
808 char *argv[] = {"file", "-b", NULL, NULL};
809 Collection *collection;
810 DirItem *file;
811 struct stat info;
812 FileStatus *fs = NULL;
814 g_return_if_fail(window_with_focus != NULL);
816 collection = window_with_focus->collection;
817 if (collection->number_selected > 1)
819 report_error("ROX-Filer", "You cannot do this to more than "
820 "one item at a time");
821 return;
823 else if (collection->number_selected != 1)
825 collection_target(collection, target_callback, show_file_info);
826 return;
828 file = selected_item(collection);
829 path = make_path(window_with_focus->path,
830 file->leafname)->str;
831 if (lstat(path, &info))
833 delayed_error("ROX-Filer", g_strerror(errno));
834 return;
837 gstring = g_string_new(NULL);
839 window = gtk_window_new(GTK_WINDOW_DIALOG);
840 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
841 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
842 gtk_window_set_title(GTK_WINDOW(window), path);
844 table = gtk_table_new(9, 2, FALSE);
845 gtk_container_add(GTK_CONTAINER(window), table);
846 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
847 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
849 label = gtk_label_new("Owner, group:");
850 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
851 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
852 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
853 g_string_sprintf(gstring, "%s, %s", user_name(info.st_uid),
854 group_name(info.st_gid));
855 label = gtk_label_new(gstring->str);
856 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
857 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
859 label = gtk_label_new("Size:");
860 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
861 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
862 if (info.st_size >= PRETTY_SIZE_LIMIT)
864 g_string_sprintf(gstring, "%s (%ld bytes)",
865 format_size((unsigned long) info.st_size),
866 (unsigned long) info.st_size);
868 else
870 g_string_sprintf(gstring, "%ld bytes",
871 (unsigned long) info.st_size);
873 label = gtk_label_new(gstring->str);
874 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
875 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
877 label = gtk_label_new("Change time:");
878 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
879 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
880 g_string_sprintf(gstring, "%s", ctime(&info.st_ctime));
881 g_string_truncate(gstring, gstring->len - 1);
882 label = gtk_label_new(gstring->str);
883 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
884 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
886 label = gtk_label_new("Modify time:");
887 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
888 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
889 g_string_sprintf(gstring, "%s", ctime(&info.st_mtime));
890 g_string_truncate(gstring, gstring->len - 1);
891 label = gtk_label_new(gstring->str);
892 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
893 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 3, 4);
895 label = gtk_label_new("Access time:");
896 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
897 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5);
898 g_string_sprintf(gstring, "%s", ctime(&info.st_atime));
899 g_string_truncate(gstring, gstring->len - 1);
900 label = gtk_label_new(gstring->str);
901 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
902 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 4, 5);
904 label = gtk_label_new("Permissions:");
905 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
906 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 5, 6);
907 label = gtk_label_new(pretty_permissions(info.st_uid, info.st_gid,
908 info.st_mode));
909 gtk_widget_set_style(label, fixed_style);
910 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
911 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 5, 6);
913 label = gtk_label_new("MIME type:");
914 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
915 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 6, 7);
916 if (file->mime_type)
918 string = g_strconcat(file->mime_type->media_type, "/",
919 file->mime_type->subtype, NULL);
920 label = gtk_label_new(string);
921 g_free(string);
923 else
924 label = gtk_label_new("-");
925 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
926 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 6, 7);
928 frame = gtk_frame_new("file(1) says...");
929 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 2, 7, 8);
930 file_label = gtk_label_new("<nothing yet>");
931 gtk_misc_set_padding(GTK_MISC(file_label), 4, 4);
932 gtk_label_set_line_wrap(GTK_LABEL(file_label), TRUE);
933 gtk_container_add(GTK_CONTAINER(frame), file_label);
935 button = gtk_button_new_with_label("OK");
936 gtk_window_set_focus(GTK_WINDOW(window), button);
937 gtk_table_attach(GTK_TABLE(table), button, 0, 2, 8, 9,
938 GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 40, 4);
939 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
940 gtk_widget_destroy, GTK_OBJECT(window));
942 gtk_widget_show_all(window);
943 gdk_flush();
945 if (pipe(file_data))
947 g_string_sprintf(gstring, "pipe(): %s", g_strerror(errno));
948 g_string_free(gstring, TRUE);
949 return;
951 switch (fork())
953 case -1:
954 g_string_sprintf(gstring, "fork(): %s",
955 g_strerror(errno));
956 gtk_label_set_text(GTK_LABEL(file_label), gstring->str);
957 g_string_free(gstring, TRUE);
958 close(file_data[0]);
959 close(file_data[1]);
960 break;
961 case 0:
962 /* We are the child */
963 close(file_data[0]);
964 dup2(file_data[1], STDOUT_FILENO);
965 dup2(file_data[1], STDERR_FILENO);
966 #ifdef FILE_B_FLAG
967 argv[2] = path;
968 #else
969 argv[1] = path;
970 #endif
971 if (execvp(argv[0], argv))
972 fprintf(stderr, "execvp() error: %s\n",
973 g_strerror(errno));
974 _exit(0);
975 default:
976 /* We are the parent */
977 close(file_data[1]);
978 fs = g_new(FileStatus, 1);
979 fs->label = GTK_LABEL(file_label);
980 fs->fd = file_data[0];
981 fs->start = TRUE;
982 fs->input = gdk_input_add(fs->fd, GDK_INPUT_READ,
983 (GdkInputFunction) add_file_output, fs);
984 gtk_signal_connect(GTK_OBJECT(window), "destroy",
985 GTK_SIGNAL_FUNC(file_info_destroyed), fs);
986 g_string_free(gstring, TRUE);
987 break;
991 static void app_show_help(char *path)
993 char *help_dir;
994 struct stat info;
996 help_dir = g_strconcat(path, "/Help", NULL);
998 if (stat(help_dir, &info))
999 delayed_error("Application",
1000 "This is an application directory - you can "
1001 "run it as a program, or open it (hold down "
1002 "Shift while you open it). Most applications provide "
1003 "their own help here, but this one doesn't.");
1004 else
1005 filer_opendir(help_dir, FALSE, BOTTOM);
1008 static void help(gpointer data, guint action, GtkWidget *widget)
1010 Collection *collection;
1011 DirItem *item;
1013 g_return_if_fail(window_with_focus != NULL);
1015 collection = window_with_focus->collection;
1016 if (collection->number_selected > 1)
1018 report_error("ROX-Filer", "You cannot do this to more than "
1019 "one item at a time");
1020 return;
1022 else if (collection->number_selected != 1)
1024 collection_target(collection, target_callback, help);
1025 return;
1027 item = selected_item(collection);
1028 switch (item->base_type)
1030 case TYPE_FILE:
1031 if (item->flags & ITEM_FLAG_EXEC_FILE)
1032 delayed_error("Executable file",
1033 "This is a file with an eXecute bit "
1034 "set - it can be run as a program.");
1035 else
1036 delayed_error("File",
1037 "This is a data file. Try using the "
1038 "Info menu item to find out more...");
1039 break;
1040 case TYPE_DIRECTORY:
1041 if (item->flags & ITEM_FLAG_APPDIR)
1042 app_show_help(
1043 make_path(window_with_focus->path,
1044 item->leafname)->str);
1045 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
1046 delayed_error("Mount point",
1047 "A mount point is a directory which another "
1048 "filing system can be mounted on. Everything "
1049 "on the mounted filesystem then appears to be "
1050 "inside the directory.");
1051 else
1052 delayed_error("Directory",
1053 "This is a directory. It contains an index to "
1054 "other items - open it to see the list.");
1055 break;
1056 case TYPE_CHAR_DEVICE:
1057 case TYPE_BLOCK_DEVICE:
1058 delayed_error("Device file",
1059 "Device files allow you to read from or write "
1060 "to a device driver as though it was an "
1061 "ordinary file.");
1062 break;
1063 case TYPE_PIPE:
1064 delayed_error("Named pipe",
1065 "Pipes allow different programs to "
1066 "communicate. One program writes data to the "
1067 "pipe while another one reads it out again.");
1068 break;
1069 case TYPE_SOCKET:
1070 delayed_error("Socket",
1071 "Sockets allow processes to communicate.");
1072 break;
1073 default:
1074 delayed_error("Unknown type",
1075 "I couldn't find out what kind of file this "
1076 "is. Maybe it doesn't exist anymore or you "
1077 "don't have search permission on the directory "
1078 "it's in?");
1079 break;
1083 static void select_all(gpointer data, guint action, GtkWidget *widget)
1085 g_return_if_fail(window_with_focus != NULL);
1087 collection_select_all(window_with_focus->collection);
1088 window_with_focus->temp_item_selected = FALSE;
1091 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1093 g_return_if_fail(window_with_focus != NULL);
1095 collection_clear_selection(window_with_focus->collection);
1096 window_with_focus->temp_item_selected = FALSE;
1099 static void show_options(gpointer data, guint action, GtkWidget *widget)
1101 g_return_if_fail(window_with_focus != NULL);
1103 options_show(window_with_focus);
1106 static gboolean new_directory_cb(char *initial, char *path)
1108 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1110 report_error("mkdir", g_strerror(errno));
1111 return FALSE;
1113 return TRUE;
1116 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1118 g_return_if_fail(window_with_focus != NULL);
1120 savebox_show(window_with_focus, "Create directory",
1121 window_with_focus->path, "NewDir",
1122 default_pixmap + TYPE_DIRECTORY, new_directory_cb);
1125 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1127 char *argv[] = {NULL, NULL};
1129 argv[0] = xterm_here_value;
1131 g_return_if_fail(window_with_focus != NULL);
1133 if (!spawn_full(argv, window_with_focus->path))
1134 report_error("ROX-Filer", "Failed to fork() child "
1135 "process");
1138 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1140 g_return_if_fail(window_with_focus != NULL);
1142 filer_opendir(make_path(window_with_focus->path, "/..")->str,
1143 FALSE, BOTTOM);
1146 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1148 g_return_if_fail(window_with_focus != NULL);
1150 change_to_parent(window_with_focus);
1153 static void new_window(gpointer data, guint action, GtkWidget *widget)
1155 g_return_if_fail(window_with_focus != NULL);
1157 filer_opendir(window_with_focus->path, FALSE, BOTTOM);
1160 static void close_window(gpointer data, guint action, GtkWidget *widget)
1162 g_return_if_fail(window_with_focus != NULL);
1164 gtk_widget_destroy(window_with_focus->window);
1167 static void enter_path(gpointer data, guint action, GtkWidget *widget)
1169 g_return_if_fail(window_with_focus != NULL);
1171 minibuffer_show(window_with_focus);
1174 static void rox_help(gpointer data, guint action, GtkWidget *widget)
1176 g_return_if_fail(window_with_focus != NULL);
1178 filer_opendir(make_path(getenv("APP_DIR"), "Help")->str, FALSE, BOTTOM);
1181 static void open_as_dir(gpointer data, guint action, GtkWidget *widget)
1183 g_return_if_fail(window_with_focus != NULL);
1185 filer_opendir(window_with_focus->path, FALSE, BOTTOM);
1188 static void close_panel(gpointer data, guint action, GtkWidget *widget)
1190 g_return_if_fail(window_with_focus != NULL);
1192 gtk_widget_destroy(window_with_focus->window);