r208: Added Bernard Jungen's patch:
[rox-filer.git] / ROX-Filer / src / menu.c
blob3cc91bbbaa810d3cc7f768db752e92432d8bd846
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>
35 #include <gdk/gdkx.h>
36 #include <gtk/gtk.h>
38 #include "run.h"
39 #include "action.h"
40 #include "filer.h"
41 #include "type.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "options.h"
45 #include "choices.h"
46 #include "savebox.h"
47 #include "mount.h"
48 #include "minibuffer.h"
50 #define C_ "<control>"
52 #define MENU_MARGIN 32
54 GtkAccelGroup *filer_keys;
55 GtkAccelGroup *panel_keys;
57 static GtkWidget *popup_menu = NULL; /* Currently open menu */
59 static gint updating_menu = 0; /* Non-zero => ignore activations */
61 /* Options */
62 static GtkWidget *xterm_here_entry;
63 static char *xterm_here_value;
65 /* Static prototypes */
67 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data);
68 static void menu_closed(GtkWidget *widget);
69 static void items_sensitive(GtkWidget *menu, int from, int n, gboolean state);
70 static char *load_xterm_here(char *data);
72 /* Note that for these callbacks none of the arguments are used. */
73 static void not_yet(gpointer data, guint action, GtkWidget *widget);
75 static void large(gpointer data, guint action, GtkWidget *widget);
76 static void small(gpointer data, guint action, GtkWidget *widget);
77 static void full_info(gpointer data, guint action, GtkWidget *widget);
79 static void sort_name(gpointer data, guint action, GtkWidget *widget);
80 static void sort_type(gpointer data, guint action, GtkWidget *widget);
81 static void sort_size(gpointer data, guint action, GtkWidget *widget);
82 static void sort_date(gpointer data, guint action, GtkWidget *widget);
84 static void hidden(gpointer data, guint action, GtkWidget *widget);
85 static void refresh(gpointer data, guint action, GtkWidget *widget);
87 static void copy_item(gpointer data, guint action, GtkWidget *widget);
88 static void rename_item(gpointer data, guint action, GtkWidget *widget);
89 static void link_item(gpointer data, guint action, GtkWidget *widget);
90 static void open_file(gpointer data, guint action, GtkWidget *widget);
91 static void help(gpointer data, guint action, GtkWidget *widget);
92 static void show_file_info(gpointer data, guint action, GtkWidget *widget);
93 static void mount(gpointer data, guint action, GtkWidget *widget);
94 static void delete(gpointer data, guint action, GtkWidget *widget);
95 static void usage(gpointer data, guint action, GtkWidget *widget);
97 static void select_all(gpointer data, guint action, GtkWidget *widget);
98 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
99 static void show_options(gpointer data, guint action, GtkWidget *widget);
100 static void new_directory(gpointer data, guint action, GtkWidget *widget);
101 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
103 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
104 static void open_parent(gpointer data, guint action, GtkWidget *widget);
105 static void new_window(gpointer data, guint action, GtkWidget *widget);
106 static void close_window(gpointer data, guint action, GtkWidget *widget);
107 static void enter_path(gpointer data, guint action, GtkWidget *widget);
108 static void rox_help(gpointer data, guint action, GtkWidget *widget);
110 static void open_as_dir(gpointer data, guint action, GtkWidget *widget);
111 static void close_panel(gpointer data, guint action, GtkWidget *widget);
113 static GtkWidget *create_options();
114 static void update_options();
115 static void set_options();
116 static void save_options();
118 static OptionsSection options =
120 "Menu options",
121 create_options,
122 update_options,
123 set_options,
124 save_options
128 static GtkWidget *filer_menu; /* The popup filer menu */
129 static GtkWidget *filer_file_item; /* The File '' label */
130 static GtkWidget *filer_file_menu; /* The File '' menu */
131 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
132 static GtkWidget *panel_menu; /* The popup panel menu */
133 static GtkWidget *panel_file_item; /* The File '' label */
134 static GtkWidget *panel_file_menu; /* The File '' menu */
135 static GtkWidget *panel_hidden_menu; /* The Show Hidden item */
137 static gint screen_width, screen_height;
139 static GtkItemFactoryEntry filer_menu_def[] = {
140 {"/Display", NULL, NULL, 0, "<Branch>"},
141 {"/Display/Large Icons", NULL, large, 0, NULL},
142 {"/Display/Small Icons", NULL, small, 0, NULL},
143 {"/Display/Full Info", NULL, full_info, 0, NULL},
144 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
145 {"/Display/Sort by Name", NULL, sort_name, 0, NULL},
146 {"/Display/Sort by Type", NULL, sort_type, 0, NULL},
147 {"/Display/Sort by Date", NULL, sort_date, 0, NULL},
148 {"/Display/Sort by Size", NULL, sort_size, 0, NULL},
149 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
150 {"/Display/Show Hidden", C_"H", hidden, 0, "<ToggleItem>"},
151 {"/Display/Refresh", C_"L", refresh, 0, NULL},
152 {"/File", NULL, NULL, 0, "<Branch>"},
153 {"/File/Copy...", NULL, copy_item, 0, NULL},
154 {"/File/Rename...", NULL, rename_item, 0, NULL},
155 {"/File/Link...", NULL, link_item, 0, NULL},
156 {"/File/Shift Open", NULL, open_file, 0, NULL},
157 {"/File/Help", "F1", help, 0, NULL},
158 {"/File/Info", "I", show_file_info, 0, NULL},
159 {"/File/Separator", NULL, NULL, 0, "<Separator>"},
160 {"/File/Mount", "M", mount, 0, NULL},
161 {"/File/Delete", C_"X", delete, 0, NULL},
162 {"/File/Disk Usage", "U", usage, 0, NULL},
163 {"/File/Permissions", NULL, not_yet, 0, NULL},
164 {"/File/Touch", NULL, not_yet, 0, NULL},
165 {"/File/Find", NULL, not_yet, 0, NULL},
166 {"/Select All", C_"A", select_all, 0, NULL},
167 {"/Clear Selection", C_"Z", clear_selection, 0, NULL},
168 {"/Options...", NULL, show_options, 0, NULL},
169 {"/New Directory...", NULL, new_directory, 0, NULL},
170 {"/Xterm Here", NULL, xterm_here, 0, NULL},
171 {"/Window", NULL, NULL, 0, "<Branch>"},
172 {"/Window/Parent, New Window", NULL, open_parent, 0, NULL},
173 {"/Window/Parent, Same Window", NULL, open_parent_same, 0, NULL},
174 {"/Window/New Window", NULL, new_window, 0, NULL},
175 {"/Window/Close Window", C_"Q", close_window, 0, NULL},
176 {"/Window/Enter Path", NULL, enter_path, 0, NULL},
177 {"/Window/Separator", NULL, NULL, 0, "<Separator>"},
178 {"/Window/Show ROX-Filer help", NULL, rox_help, 0, NULL},
181 static GtkItemFactoryEntry panel_menu_def[] = {
182 {"/Display", NULL, NULL, 0, "<Branch>"},
183 {"/Display/Sort by Name", NULL, sort_name, 0, NULL},
184 {"/Display/Sort by Type", NULL, sort_type, 0, NULL},
185 {"/Display/Sort by Date", NULL, sort_date, 0, NULL},
186 {"/Display/Sort by Size", NULL, sort_size, 0, NULL},
187 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
188 {"/Display/Show Hidden", NULL, hidden, 0, "<ToggleItem>"},
189 {"/Display/Refresh", NULL, refresh, 0, NULL},
190 {"/File", NULL, NULL, 0, "<Branch>"},
191 {"/File/Help", NULL, help, 0, NULL},
192 {"/File/Info", NULL, show_file_info, 0, NULL},
193 {"/File/Delete", NULL, delete, 0, NULL},
194 {"/Open as directory", NULL, open_as_dir, 0, NULL},
195 {"/Options...", NULL, show_options, 0, NULL},
196 {"/Close panel", NULL, close_panel, 0, NULL},
197 {"/Separator", NULL, NULL, 0, "<Separator>"},
198 {"/Show ROX-Filer help", NULL, rox_help, 0, NULL},
201 typedef struct _FileStatus FileStatus;
203 /* This is for the 'file(1) says...' thing */
204 struct _FileStatus
206 int fd; /* FD to read from, -1 if closed */
207 int input; /* Input watcher tag if fd valid */
208 GtkLabel *label; /* Widget to output to */
209 gboolean start; /* No output yet */
212 void menu_init()
214 GtkItemFactory *item_factory;
215 char *menurc;
216 GList *items;
218 /* This call starts returning strange values after a while, so get
219 * the result here during init.
221 gdk_window_get_size(GDK_ROOT_PARENT(), &screen_width, &screen_height);
223 filer_keys = gtk_accel_group_new();
224 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
225 "<filer>",
226 filer_keys);
227 gtk_item_factory_create_items(item_factory,
228 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
229 filer_menu_def,
230 NULL);
231 filer_menu = gtk_item_factory_get_widget(item_factory, "<filer>");
232 filer_file_menu = gtk_item_factory_get_widget(item_factory,
233 "<filer>/File");
234 filer_hidden_menu = gtk_item_factory_get_widget(item_factory,
235 "<filer>/Display/Show Hidden");
236 items = gtk_container_children(GTK_CONTAINER(filer_menu));
237 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
238 g_list_free(items);
240 panel_keys = gtk_accel_group_new();
241 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
242 "<panel>",
243 panel_keys);
244 gtk_item_factory_create_items(item_factory,
245 sizeof(panel_menu_def) / sizeof(*panel_menu_def),
246 panel_menu_def,
247 NULL);
248 panel_menu = gtk_item_factory_get_widget(item_factory, "<panel>");
249 panel_file_menu = gtk_item_factory_get_widget(item_factory,
250 "<panel>/File");
251 panel_hidden_menu = gtk_item_factory_get_widget(item_factory,
252 "<panel>/Display/Show Hidden");
253 items = gtk_container_children(GTK_CONTAINER(panel_menu));
254 panel_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
255 g_list_free(items);
257 menurc = choices_find_path_load("menus");
258 if (menurc)
259 gtk_item_factory_parse_rc(menurc);
261 gtk_accel_group_lock(panel_keys);
263 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
264 GTK_SIGNAL_FUNC(menu_closed), NULL);
265 gtk_signal_connect(GTK_OBJECT(panel_menu), "unmap_event",
266 GTK_SIGNAL_FUNC(menu_closed), NULL);
267 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
268 GTK_SIGNAL_FUNC(menu_closed), NULL);
270 options_sections = g_slist_prepend(options_sections, &options);
271 xterm_here_value = g_strdup("xterm");
272 option_register("xterm_here", load_xterm_here);
275 /* Build up some option widgets to go in the options dialog, but don't
276 * fill them in yet.
278 static GtkWidget *create_options()
280 GtkWidget *table, *label;
282 table = gtk_table_new(2, 2, FALSE);
283 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
285 label = gtk_label_new("To set the keyboard short-cuts you simply open "
286 "the menu over a filer window, move the pointer over "
287 "the item you want to use and press a key. The key "
288 "will appear next to the menu item and you can just "
289 "press that key without opening the menu in future. "
290 "To save the current menu short-cuts for next time, "
291 "click the Save button at the bottom of this window.");
292 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
293 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 2, 0, 1);
295 label = gtk_label_new("'Xterm here' program:");
296 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
297 xterm_here_entry = gtk_entry_new();
298 gtk_table_attach_defaults(GTK_TABLE(table), xterm_here_entry,
299 1, 2, 1, 2);
301 return table;
304 static char *load_xterm_here(char *data)
306 g_free(xterm_here_value);
307 xterm_here_value = g_strdup(data);
308 return NULL;
311 static void update_options()
313 gtk_entry_set_text(GTK_ENTRY(xterm_here_entry), xterm_here_value);
316 static void set_options()
318 g_free(xterm_here_value);
319 xterm_here_value = g_strdup(gtk_entry_get_text(
320 GTK_ENTRY(xterm_here_entry)));
323 static void save_options()
325 char *menurc;
327 menurc = choices_find_path_save("menus", TRUE);
328 if (menurc)
329 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
331 option_write("xterm_here", xterm_here_value);
335 static void items_sensitive(GtkWidget *menu, int from, int n, gboolean state)
337 GList *items, *item;
339 items = gtk_container_children(GTK_CONTAINER(menu));
341 item = g_list_nth(items, from);
342 while (item && n--)
344 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
345 item = item->next;
348 g_list_free(items);
351 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data)
353 int *pos = (int *) data;
354 GtkRequisition requisition;
356 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
358 if (pos[0] == -1)
359 *x = screen_width - MENU_MARGIN - requisition.width;
360 else if (pos[0] == -2)
361 *x = MENU_MARGIN;
362 else
363 *x = pos[0] - (requisition.width >> 2);
365 if (pos[1] == -1)
366 *y = screen_height - MENU_MARGIN - requisition.height;
367 else if (pos[1] == -2)
368 *y = MENU_MARGIN;
369 else
370 *y = pos[1] - (requisition.height >> 2);
372 *x = CLAMP(*x, 0, screen_width - requisition.width);
373 *y = CLAMP(*y, 0, screen_height - requisition.height);
376 void show_filer_menu(FilerWindow *filer_window, GdkEventButton *event,
377 int item)
379 GString *buffer;
380 GtkWidget *file_label, *file_menu;
381 DirItem *file_item;
382 int pos[2];
384 updating_menu++;
386 pos[0] = event->x_root;
387 pos[1] = event->y_root;
389 window_with_focus = filer_window;
391 switch (filer_window->panel_type)
393 case PANEL_TOP:
394 pos[1] = -2;
395 break;
396 case PANEL_BOTTOM:
397 pos[1] = -1;
398 break;
399 default:
400 break;
403 if (filer_window->panel_type)
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_type)
416 file_label = panel_file_item;
417 file_menu = panel_file_menu;
418 gtk_check_menu_item_set_active(
419 GTK_CHECK_MENU_ITEM(panel_hidden_menu),
420 filer_window->show_hidden);
422 else
424 file_label = filer_file_item;
425 file_menu = filer_file_menu;
426 gtk_check_menu_item_set_active(
427 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
428 filer_window->show_hidden);
431 buffer = g_string_new(NULL);
432 switch (filer_window->collection->number_selected)
434 case 0:
435 g_string_assign(buffer, "Next Click");
436 items_sensitive(file_menu, 0, 6, TRUE);
437 break;
438 case 1:
439 items_sensitive(file_menu, 0, 6, TRUE);
440 file_item = selected_item(filer_window->collection);
441 g_string_sprintf(buffer, "%s '%s'",
442 basetype_name(file_item),
443 file_item->leafname);
444 break;
445 default:
446 items_sensitive(file_menu, 0, 6, FALSE);
447 g_string_sprintf(buffer, "%d items",
448 filer_window->collection->number_selected);
449 break;
452 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
454 g_string_free(buffer, TRUE);
456 if (filer_window->panel_type)
457 popup_menu = panel_menu;
458 else
459 popup_menu = (event->state & GDK_CONTROL_MASK)
460 ? filer_file_menu
461 : filer_menu;
463 updating_menu--;
465 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
466 (gpointer) pos, event->button, event->time);
469 static void menu_closed(GtkWidget *widget)
471 if (window_with_focus == NULL || widget != popup_menu)
472 return; /* Close panel item chosen? */
474 if (window_with_focus->temp_item_selected)
476 collection_clear_selection(window_with_focus->collection);
477 window_with_focus->temp_item_selected = FALSE;
481 void target_callback(Collection *collection, gint item, gpointer real_fn)
483 g_return_if_fail(window_with_focus != NULL);
484 g_return_if_fail(window_with_focus->collection == collection);
485 g_return_if_fail(real_fn != NULL);
487 collection_wink_item(collection, item);
488 collection_clear_selection(collection);
489 collection_select_item(collection, item);
490 ((CollectionTargetFunc)real_fn)(NULL, 0, collection);
491 if (item < collection->number_of_items)
492 collection_unselect_item(collection, item);
495 /* Actions */
497 /* Fake action to warn when a menu item does nothing */
498 static void not_yet(gpointer data, guint action, GtkWidget *widget)
500 delayed_error("ROX-Filer", "Sorry, that feature isn't implemented yet");
503 static void large(gpointer data, guint action, GtkWidget *widget)
505 g_return_if_fail(window_with_focus != NULL);
507 filer_style_set(window_with_focus, LARGE_ICONS);
510 static void small(gpointer data, guint action, GtkWidget *widget)
512 g_return_if_fail(window_with_focus != NULL);
514 filer_style_set(window_with_focus, SMALL_ICONS);
517 static void full_info(gpointer data, guint action, GtkWidget *widget)
519 g_return_if_fail(window_with_focus != NULL);
521 filer_style_set(window_with_focus, FULL_INFO);
524 static void sort_name(gpointer data, guint action, GtkWidget *widget)
526 g_return_if_fail(window_with_focus != NULL);
528 filer_set_sort_fn(window_with_focus, sort_by_name);
531 static void sort_type(gpointer data, guint action, GtkWidget *widget)
533 g_return_if_fail(window_with_focus != NULL);
535 filer_set_sort_fn(window_with_focus, sort_by_type);
538 static void sort_date(gpointer data, guint action, GtkWidget *widget)
540 g_return_if_fail(window_with_focus != NULL);
542 filer_set_sort_fn(window_with_focus, sort_by_date);
545 static void sort_size(gpointer data, guint action, GtkWidget *widget)
547 g_return_if_fail(window_with_focus != NULL);
549 filer_set_sort_fn(window_with_focus, sort_by_size);
552 static void hidden(gpointer data, guint action, GtkWidget *widget)
554 if (updating_menu)
555 return;
557 g_return_if_fail(window_with_focus != NULL);
559 filer_set_hidden(window_with_focus, !window_with_focus->show_hidden);
562 static void refresh(gpointer data, guint action, GtkWidget *widget)
564 g_return_if_fail(window_with_focus != NULL);
566 full_refresh();
567 update_dir(window_with_focus, TRUE);
570 static void mount(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, mount);
577 else
578 action_mount(window_with_focus, NULL);
581 static void delete(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, delete);
588 else
589 action_delete(window_with_focus);
592 static void usage(gpointer data, guint action, GtkWidget *widget)
594 g_return_if_fail(window_with_focus != NULL);
596 if (window_with_focus->collection->number_selected == 0)
597 collection_target(window_with_focus->collection,
598 target_callback, usage);
599 else
600 action_usage(window_with_focus);
603 static gboolean copy_cb(char *initial, char *path)
605 char *new_dir, *slash;
606 int len;
607 GString *command;
608 gboolean retval = TRUE;
610 slash = strrchr(path, '/');
611 if (!slash)
613 report_error("ROX-Filer", "Missing '/' in new pathname");
614 return FALSE;
617 if (access(path, F_OK) == 0)
619 report_error("ROX-Filer",
620 "An item with this name already exists");
621 return FALSE;
624 len = slash - path;
625 new_dir = g_malloc(len + 1);
626 memcpy(new_dir, path, len);
627 new_dir[len] = '\0';
629 command = g_string_new(NULL);
630 g_string_sprintf(command, "cp -a %s %s", initial, path);
631 /* XXX: Use system. In fact, use action! */
633 if (system(command->str))
635 g_string_append(command, " failed!");
636 report_error("ROX-Filer", command->str);
637 retval = FALSE;
640 g_string_free(command, TRUE);
642 refresh_dirs(new_dir);
643 return retval;
646 static void copy_item(gpointer data, guint action, GtkWidget *widget)
648 Collection *collection;
650 g_return_if_fail(window_with_focus != NULL);
652 collection = window_with_focus->collection;
653 if (collection->number_selected > 1)
655 report_error("ROX-Filer", "You cannot do this to more than "
656 "one item at a time");
657 return;
659 else if (collection->number_selected != 1)
660 collection_target(collection, target_callback, copy_item);
661 else
663 DirItem *item = selected_item(collection);
665 savebox_show(window_with_focus, "Copy",
666 window_with_focus->path,
667 item->leafname,
668 item->image, copy_cb);
672 static gboolean rename_cb(char *initial, char *path)
674 if (rename(initial, path))
676 report_error("ROX-Filer: rename()", g_strerror(errno));
677 return FALSE;
679 return TRUE;
682 static void rename_item(gpointer data, guint action, GtkWidget *widget)
684 Collection *collection;
686 g_return_if_fail(window_with_focus != NULL);
688 collection = window_with_focus->collection;
689 if (collection->number_selected > 1)
691 report_error("ROX-Filer", "You cannot do this to more than "
692 "one item at a time");
693 return;
695 else if (collection->number_selected != 1)
696 collection_target(collection, target_callback, rename_item);
697 else
699 DirItem *item = selected_item(collection);
701 savebox_show(window_with_focus, "Rename",
702 window_with_focus->path,
703 item->leafname,
704 item->image, rename_cb);
708 static gboolean link_cb(char *initial, char *path)
710 if (symlink(initial, path))
712 report_error("ROX-Filer: symlink()", g_strerror(errno));
713 return FALSE;
715 return TRUE;
718 static void link_item(gpointer data, guint action, GtkWidget *widget)
720 Collection *collection;
722 g_return_if_fail(window_with_focus != NULL);
724 collection = window_with_focus->collection;
725 if (collection->number_selected > 1)
727 report_error("ROX-Filer", "You cannot do this to more than "
728 "one item at a time");
729 return;
731 else if (collection->number_selected != 1)
732 collection_target(collection, target_callback, link_item);
733 else
735 DirItem *item = selected_item(collection);
737 savebox_show(window_with_focus, "Symlink",
738 window_with_focus->path,
739 item->leafname,
740 item->image, link_cb);
744 static void open_file(gpointer data, guint action, GtkWidget *widget)
746 Collection *collection;
748 g_return_if_fail(window_with_focus != NULL);
750 collection = window_with_focus->collection;
751 if (collection->number_selected > 1)
753 report_error("ROX-Filer", "You cannot do this to more than "
754 "one item at a time");
755 return;
757 else if (collection->number_selected != 1)
758 collection_target(collection, target_callback, open_file);
759 else
760 filer_openitem(window_with_focus,
761 selected_item_number(collection),
762 OPEN_SAME_WINDOW | OPEN_SHIFT);
765 /* Got some data from file(1) - stick it in the window. */
766 static void add_file_output(FileStatus *fs,
767 gint source, GdkInputCondition condition)
769 char buffer[20];
770 char *str;
771 int got;
773 got = read(source, buffer, sizeof(buffer) - 1);
774 if (got <= 0)
776 int err = errno;
777 gtk_input_remove(fs->input);
778 close(source);
779 fs->fd = -1;
780 if (got < 0)
781 delayed_error("ROX-Filer: file(1) says...",
782 g_strerror(err));
783 return;
785 buffer[got] = '\0';
787 if (fs->start)
789 str = "";
790 fs->start = FALSE;
792 else
793 gtk_label_get(fs->label, &str);
795 str = g_strconcat(str, buffer, NULL);
796 gtk_label_set_text(fs->label, str);
797 g_free(str);
800 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs)
802 if (fs->fd != -1)
804 gtk_input_remove(fs->input);
805 close(fs->fd);
808 g_free(fs);
811 static void show_file_info(gpointer data, guint action, GtkWidget *widget)
813 GtkWidget *window, *table, *label, *button, *frame;
814 GtkWidget *file_label;
815 GString *gstring;
816 char *string;
817 int file_data[2];
818 char *path;
819 char *argv[] = {"file", "-b", NULL, NULL};
820 Collection *collection;
821 DirItem *file;
822 struct stat info;
823 FileStatus *fs = NULL;
824 guint perm;
826 g_return_if_fail(window_with_focus != NULL);
828 collection = window_with_focus->collection;
829 if (collection->number_selected > 1)
831 report_error("ROX-Filer", "You cannot do this to more than "
832 "one item at a time");
833 return;
835 else if (collection->number_selected != 1)
837 collection_target(collection, target_callback, show_file_info);
838 return;
840 file = selected_item(collection);
841 path = make_path(window_with_focus->path,
842 file->leafname)->str;
843 if (lstat(path, &info))
845 delayed_error("ROX-Filer", g_strerror(errno));
846 return;
849 gstring = g_string_new(NULL);
851 window = gtk_window_new(GTK_WINDOW_DIALOG);
852 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
853 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
854 gtk_window_set_title(GTK_WINDOW(window), path);
856 table = gtk_table_new(9, 2, FALSE);
857 gtk_container_add(GTK_CONTAINER(window), table);
858 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
859 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
861 label = gtk_label_new("Owner, group:");
862 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
863 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
864 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
865 g_string_sprintf(gstring, "%s, %s", user_name(info.st_uid),
866 group_name(info.st_gid));
867 label = gtk_label_new(gstring->str);
868 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
869 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
871 label = gtk_label_new("Size:");
872 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
873 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
874 if (info.st_size >= PRETTY_SIZE_LIMIT)
876 g_string_sprintf(gstring, "%s (%ld bytes)",
877 format_size((unsigned long) info.st_size),
878 (unsigned long) info.st_size);
880 else
882 g_string_sprintf(gstring, "%ld bytes",
883 (unsigned long) info.st_size);
885 label = gtk_label_new(gstring->str);
886 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
887 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
889 label = gtk_label_new("Change time:");
890 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
891 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
892 label = gtk_label_new(pretty_time(&info.st_ctime));
893 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
894 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
896 label = gtk_label_new("Modify time:");
897 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
898 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
899 label = gtk_label_new(pretty_time(&info.st_mtime));
900 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
901 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 3, 4);
903 label = gtk_label_new("Access time:");
904 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
905 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5);
906 label = gtk_label_new(pretty_time(&info.st_atime));
907 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
908 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 4, 5);
910 label = gtk_label_new("Permissions:");
911 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
912 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 5, 6);
913 label = gtk_label_new(pretty_permissions(info.st_mode));
914 perm = applicable(info.st_uid, info.st_gid);
915 gtk_label_set_pattern(GTK_LABEL(label),
916 perm == 0 ? "___ " :
917 perm == 1 ? " ___ " :
918 " ___");
919 gtk_widget_set_style(label, fixed_style);
920 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
921 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 5, 6);
923 label = gtk_label_new("MIME type:");
924 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
925 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 6, 7);
926 if (file->mime_type)
928 string = g_strconcat(file->mime_type->media_type, "/",
929 file->mime_type->subtype, NULL);
930 label = gtk_label_new(string);
931 g_free(string);
933 else
934 label = gtk_label_new("-");
935 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
936 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 6, 7);
938 frame = gtk_frame_new("file(1) says...");
939 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 2, 7, 8);
940 file_label = gtk_label_new("<nothing yet>");
941 gtk_misc_set_padding(GTK_MISC(file_label), 4, 4);
942 gtk_label_set_line_wrap(GTK_LABEL(file_label), TRUE);
943 gtk_container_add(GTK_CONTAINER(frame), file_label);
945 button = gtk_button_new_with_label("OK");
946 gtk_window_set_focus(GTK_WINDOW(window), button);
947 gtk_table_attach(GTK_TABLE(table), button, 0, 2, 8, 9,
948 GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 40, 4);
949 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
950 gtk_widget_destroy, GTK_OBJECT(window));
952 gtk_widget_show_all(window);
953 gdk_flush();
955 if (pipe(file_data))
957 g_string_sprintf(gstring, "pipe(): %s", g_strerror(errno));
958 g_string_free(gstring, TRUE);
959 return;
961 switch (fork())
963 case -1:
964 g_string_sprintf(gstring, "fork(): %s",
965 g_strerror(errno));
966 gtk_label_set_text(GTK_LABEL(file_label), gstring->str);
967 g_string_free(gstring, TRUE);
968 close(file_data[0]);
969 close(file_data[1]);
970 break;
971 case 0:
972 /* We are the child */
973 close(file_data[0]);
974 dup2(file_data[1], STDOUT_FILENO);
975 dup2(file_data[1], STDERR_FILENO);
976 #ifdef FILE_B_FLAG
977 argv[2] = path;
978 #else
979 argv[1] = file->leafname;
980 chdir(window_with_focus->path);
981 #endif
982 if (execvp(argv[0], argv))
983 fprintf(stderr, "execvp() error: %s\n",
984 g_strerror(errno));
985 _exit(0);
986 default:
987 /* We are the parent */
988 close(file_data[1]);
989 fs = g_new(FileStatus, 1);
990 fs->label = GTK_LABEL(file_label);
991 fs->fd = file_data[0];
992 fs->start = TRUE;
993 fs->input = gdk_input_add(fs->fd, GDK_INPUT_READ,
994 (GdkInputFunction) add_file_output, fs);
995 gtk_signal_connect(GTK_OBJECT(window), "destroy",
996 GTK_SIGNAL_FUNC(file_info_destroyed), fs);
997 g_string_free(gstring, TRUE);
998 break;
1002 static void app_show_help(char *path)
1004 char *help_dir;
1005 struct stat info;
1007 help_dir = g_strconcat(path, "/Help", NULL);
1009 if (stat(help_dir, &info))
1010 delayed_error("Application",
1011 "This is an application directory - you can "
1012 "run it as a program, or open it (hold down "
1013 "Shift while you open it). Most applications provide "
1014 "their own help here, but this one doesn't.");
1015 else
1016 filer_opendir(help_dir, PANEL_NO);
1019 static void help(gpointer data, guint action, GtkWidget *widget)
1021 Collection *collection;
1022 DirItem *item;
1024 g_return_if_fail(window_with_focus != NULL);
1026 collection = window_with_focus->collection;
1027 if (collection->number_selected > 1)
1029 report_error("ROX-Filer", "You cannot do this to more than "
1030 "one item at a time");
1031 return;
1033 else if (collection->number_selected != 1)
1035 collection_target(collection, target_callback, help);
1036 return;
1038 item = selected_item(collection);
1039 switch (item->base_type)
1041 case TYPE_FILE:
1042 if (item->flags & ITEM_FLAG_EXEC_FILE)
1043 delayed_error("Executable file",
1044 "This is a file with an eXecute bit "
1045 "set - it can be run as a program.");
1046 else
1047 delayed_error("File",
1048 "This is a data file. Try using the "
1049 "Info menu item to find out more...");
1050 break;
1051 case TYPE_DIRECTORY:
1052 if (item->flags & ITEM_FLAG_APPDIR)
1053 app_show_help(
1054 make_path(window_with_focus->path,
1055 item->leafname)->str);
1056 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
1057 delayed_error("Mount point",
1058 "A mount point is a directory which another "
1059 "filing system can be mounted on. Everything "
1060 "on the mounted filesystem then appears to be "
1061 "inside the directory.");
1062 else
1063 delayed_error("Directory",
1064 "This is a directory. It contains an index to "
1065 "other items - open it to see the list.");
1066 break;
1067 case TYPE_CHAR_DEVICE:
1068 case TYPE_BLOCK_DEVICE:
1069 delayed_error("Device file",
1070 "Device files allow you to read from or write "
1071 "to a device driver as though it was an "
1072 "ordinary file.");
1073 break;
1074 case TYPE_PIPE:
1075 delayed_error("Named pipe",
1076 "Pipes allow different programs to "
1077 "communicate. One program writes data to the "
1078 "pipe while another one reads it out again.");
1079 break;
1080 case TYPE_SOCKET:
1081 delayed_error("Socket",
1082 "Sockets allow processes to communicate.");
1083 break;
1084 default:
1085 delayed_error("Unknown type",
1086 "I couldn't find out what kind of file this "
1087 "is. Maybe it doesn't exist anymore or you "
1088 "don't have search permission on the directory "
1089 "it's in?");
1090 break;
1094 static void select_all(gpointer data, guint action, GtkWidget *widget)
1096 g_return_if_fail(window_with_focus != NULL);
1098 collection_select_all(window_with_focus->collection);
1099 window_with_focus->temp_item_selected = FALSE;
1102 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1104 g_return_if_fail(window_with_focus != NULL);
1106 collection_clear_selection(window_with_focus->collection);
1107 window_with_focus->temp_item_selected = FALSE;
1110 static void show_options(gpointer data, guint action, GtkWidget *widget)
1112 g_return_if_fail(window_with_focus != NULL);
1114 options_show(window_with_focus);
1117 static gboolean new_directory_cb(char *initial, char *path)
1119 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1121 report_error("mkdir", g_strerror(errno));
1122 return FALSE;
1124 return TRUE;
1127 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1129 g_return_if_fail(window_with_focus != NULL);
1131 savebox_show(window_with_focus, "Create directory",
1132 window_with_focus->path, "NewDir",
1133 default_pixmap + TYPE_DIRECTORY, new_directory_cb);
1136 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1138 char *argv[] = {NULL, NULL};
1140 argv[0] = xterm_here_value;
1142 g_return_if_fail(window_with_focus != NULL);
1144 if (!spawn_full(argv, window_with_focus->path))
1145 report_error("ROX-Filer", "Failed to fork() child "
1146 "process");
1149 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1151 char *copy;
1152 char *slash;
1154 g_return_if_fail(window_with_focus != NULL);
1156 if (window_with_focus->path[0] == '/'
1157 && window_with_focus->path[1] == '\0')
1158 return; /* Already in the root */
1160 copy = g_strdup(window_with_focus->path);
1161 slash = strrchr(copy, '/');
1163 if (slash)
1165 *slash = '\0';
1166 filer_opendir(*copy ? copy : "/", PANEL_NO);
1168 else
1169 g_warning("No / in directory path!\n");
1171 g_free(copy);
1174 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1176 g_return_if_fail(window_with_focus != NULL);
1178 change_to_parent(window_with_focus);
1181 static void new_window(gpointer data, guint action, GtkWidget *widget)
1183 g_return_if_fail(window_with_focus != NULL);
1185 filer_opendir(window_with_focus->path, PANEL_NO);
1188 static void close_window(gpointer data, guint action, GtkWidget *widget)
1190 g_return_if_fail(window_with_focus != NULL);
1192 gtk_widget_destroy(window_with_focus->window);
1195 static void enter_path(gpointer data, guint action, GtkWidget *widget)
1197 g_return_if_fail(window_with_focus != NULL);
1199 minibuffer_show(window_with_focus);
1202 static void rox_help(gpointer data, guint action, GtkWidget *widget)
1204 g_return_if_fail(window_with_focus != NULL);
1206 filer_opendir(make_path(getenv("APP_DIR"), "Help")->str, PANEL_NO);
1209 static void open_as_dir(gpointer data, guint action, GtkWidget *widget)
1211 g_return_if_fail(window_with_focus != NULL);
1213 filer_opendir(window_with_focus->path, PANEL_NO);
1216 static void close_panel(gpointer data, guint action, GtkWidget *widget)
1218 g_return_if_fail(window_with_focus != NULL);
1220 gtk_widget_destroy(window_with_focus->window);