r4895: New MIME type for Jar archives.
[rox-filer.git] / ROX-Filer / src / filer.c
blob3e8d4c9910015672e57e5750c78a59b766cb5d4f
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* filer.c - code for handling filer windows */
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <netdb.h>
30 #include <sys/param.h>
31 #include <fnmatch.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkx.h>
35 #include <gdk/gdkkeysyms.h>
37 #include "global.h"
39 #include "filer.h"
40 #include "display.h"
41 #include "main.h"
42 #include "fscache.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "choices.h"
46 #include "pixmaps.h"
47 #include "menu.h"
48 #include "dnd.h"
49 #include "dir.h"
50 #include "diritem.h"
51 #include "run.h"
52 #include "type.h"
53 #include "options.h"
54 #include "minibuffer.h"
55 #include "icon.h"
56 #include "toolbar.h"
57 #include "bind.h"
58 #include "appinfo.h"
59 #include "mount.h"
60 #include "xml.h"
61 #include "view_iface.h"
62 #include "view_collection.h"
63 #include "view_details.h"
64 #include "action.h"
65 #include "bookmarks.h"
66 #include "xtypes.h"
68 static XMLwrapper *groups = NULL;
70 /* Item we are about to display a tooltip for */
71 static DirItem *tip_item = NULL;
73 /* The window which the motion event for the tooltip came from. Use this
74 * to get the correct widget for finding the item under the pointer.
76 static GdkWindow *motion_window = NULL;
78 /* This is rather badly named. It's actually the filer window which received
79 * the last key press or Menu click event.
81 FilerWindow *window_with_focus = NULL;
83 GList *all_filer_windows = NULL;
85 static GHashTable *window_with_id = NULL;
87 static FilerWindow *window_with_primary = NULL;
89 static GHashTable *settings_table=NULL;
91 typedef struct {
92 gchar *path;
94 guint flags; /* Which parts are valid, see below */
96 gint x, y;
97 gint width, height;
98 gboolean show_hidden;
99 ViewType view_type;
100 SortType sort_type;
101 GtkSortType sort_order;
102 gboolean show_thumbs;
104 DetailsType details_type;
105 DisplayStyle display_style;
107 FilterType filter_type;
108 char *filter;
109 gboolean filter_directories;
110 } Settings;
112 enum settings_flags{
113 SET_POSITION=1, /* x, y */
114 SET_SIZE=2, /* width, height */
115 SET_HIDDEN=4, /* show_hidden */
116 SET_STYLE=8, /* display_style */
117 SET_SORT=16, /* sort_type, sort_order */
118 SET_DETAILS=32, /* view_type, details_type */
119 SET_THUMBS=64, /* show_thumbs */
120 SET_FILTER=128, /* filter_type, filter */
123 /* Static prototypes */
124 static void attach(FilerWindow *filer_window);
125 static void detach(FilerWindow *filer_window);
126 static void filer_window_destroyed(GtkWidget *widget,
127 FilerWindow *filer_window);
128 static void update_display(Directory *dir,
129 DirAction action,
130 GPtrArray *items,
131 FilerWindow *filer_window);
132 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning);
133 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning);
134 static gboolean minibuffer_show_cb(FilerWindow *filer_window);
135 static FilerWindow *find_filer_window(const char *sym_path, FilerWindow *diff);
136 static void filer_add_widgets(FilerWindow *filer_window, const gchar *wm_class);
137 static void filer_add_signals(FilerWindow *filer_window);
139 static void set_selection_state(FilerWindow *filer_window, gboolean normal);
140 static void filer_next_thumb(GObject *window, const gchar *path);
141 static void start_thumb_scanning(FilerWindow *filer_window);
142 static void filer_options_changed(void);
143 static void drag_end(GtkWidget *widget, GdkDragContext *context,
144 FilerWindow *filer_window);
145 static void drag_leave(GtkWidget *widget,
146 GdkDragContext *context,
147 guint32 time,
148 FilerWindow *filer_window);
149 static gboolean drag_motion(GtkWidget *widget,
150 GdkDragContext *context,
151 gint x,
152 gint y,
153 guint time,
154 FilerWindow *filer_window);
156 static void load_settings(void);
157 static void save_settings(void);
158 static void check_settings(FilerWindow *filer_window);
159 static char *tip_from_desktop_file(const char *full_path);
161 static GdkCursor *busy_cursor = NULL;
162 static GdkCursor *crosshair = NULL;
164 /* Indicates whether the filer's display is different to the machine it
165 * is actually running on.
167 static gboolean not_local = FALSE;
169 static Option o_short_flag_names;
170 static Option o_filer_view_type;
171 Option o_filer_auto_resize, o_unique_filer_windows;
172 Option o_filer_size_limit;
174 #define ROX_RESPONSE_EJECT 99 /**< User clicked on Eject button */
176 void filer_init(void)
178 const gchar *ohost;
179 const gchar *dpy;
180 gchar *dpyhost, *tmp;
182 option_add_int(&o_filer_size_limit, "filer_size_limit", 75);
183 option_add_int(&o_filer_auto_resize, "filer_auto_resize",
184 RESIZE_ALWAYS);
185 option_add_int(&o_unique_filer_windows, "filer_unique_windows", 0);
187 option_add_int(&o_short_flag_names, "filer_short_flag_names", FALSE);
189 option_add_int(&o_filer_view_type, "filer_view_type",
190 VIEW_TYPE_COLLECTION);
192 option_add_notify(filer_options_changed);
194 busy_cursor = gdk_cursor_new(GDK_WATCH);
195 crosshair = gdk_cursor_new(GDK_CROSSHAIR);
197 window_with_id = g_hash_table_new_full(g_str_hash, g_str_equal,
198 NULL, NULL);
200 /* Is the display on the local machine, or are we being
201 * run remotely? See filer_set_title().
203 ohost = our_host_name();
204 dpy = gdk_get_display();
205 dpyhost = g_strdup(dpy);
206 tmp = strchr(dpyhost, ':');
207 if (tmp)
208 *tmp = '\0';
210 if (dpyhost[0] && strcmp(ohost, dpyhost) != 0)
212 /* Try the cannonical name for dpyhost (see our_host_name()
213 * in support.c).
215 struct hostent *ent;
217 ent = gethostbyname(dpyhost);
218 if (!ent || strcmp(ohost, ent->h_name) != 0)
219 not_local = TRUE;
222 g_free(dpyhost);
224 load_settings();
227 static gboolean if_deleted(gpointer item, gpointer removed)
229 int i = ((GPtrArray *) removed)->len;
230 DirItem **r = (DirItem **) ((GPtrArray *) removed)->pdata;
231 char *leafname = ((DirItem *) item)->leafname;
233 while (i--)
235 if (strcmp(leafname, r[i]->leafname) == 0)
236 return TRUE;
239 return FALSE;
242 #define DECOR_BORDER 32
244 /* Resize the filer window to w x h pixels, plus border (not clamped).
245 * If triggered by a key event, warp the pointer (for SloppyFocus users).
247 void filer_window_set_size(FilerWindow *filer_window, int w, int h)
249 GtkWidget *window;
251 g_return_if_fail(filer_window != NULL);
253 if (filer_window->scrollbar)
254 w += filer_window->scrollbar->allocation.width;
256 if (o_toolbar.int_value != TOOLBAR_NONE)
257 h += filer_window->toolbar->allocation.height;
258 if (filer_window->message)
259 h += filer_window->message->allocation.height;
261 window = filer_window->window;
263 if (GTK_WIDGET_VISIBLE(window))
265 gint x, y, m;
266 GtkRequisition *req = &window->requisition;
267 GdkWindow *gdk_window = window->window;
268 GdkEvent *event;
270 w = MAX(req->width, w);
271 h = MAX(req->height, h);
272 gdk_window_get_pointer(NULL, &x, &y, NULL);
273 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(),
274 x, y);
275 gdk_window_get_position(gdk_window, &x, &y);
277 if (x + w + DECOR_BORDER >
278 monitor_geom[m].x + monitor_geom[m].width ||
279 y + h + DECOR_BORDER >
280 monitor_geom[m].y + monitor_geom[m].height)
282 if (x + w + DECOR_BORDER >
283 monitor_geom[m].x + monitor_geom[m].width)
285 x = monitor_geom[m].x + monitor_geom[m].width -
286 w - 4 - DECOR_BORDER;
288 if (y + h + DECOR_BORDER >
289 monitor_geom[m].y + monitor_geom[m].height)
291 y = monitor_geom[m].y + monitor_geom[m].height -
292 h - 4 - DECOR_BORDER;
294 gdk_window_move_resize(gdk_window, x, y, w, h);
296 else
297 gdk_window_resize(gdk_window, w, h);
299 /* If the resize was triggered by a key press, keep
300 * the pointer inside the window so that it doesn't
301 * lose focus when using pointer-follows-mouse.
303 event = gtk_get_current_event();
304 if (event && event->type == GDK_KEY_PRESS)
306 int x, y;
307 int nx, ny;
309 GdkWindow *win = filer_window->window->window;
311 gdk_window_get_pointer(filer_window->window->window,
312 &x, &y, NULL);
314 nx = CLAMP(x, 4, w - 4);
315 ny = CLAMP(y, 4, h - 4);
317 if (nx != x || ny != y)
319 XWarpPointer(gdk_x11_drawable_get_xdisplay(win),
320 None,
321 gdk_x11_drawable_get_xid(win),
322 0, 0, 0, 0,
323 nx, ny);
326 if (event)
327 gdk_event_free(event);
329 else
330 gtk_window_set_default_size(GTK_WINDOW(window), w, h);
333 /* Called on a timeout while scanning or when scanning ends
334 * (whichever happens first).
336 static gint open_filer_window(FilerWindow *filer_window)
338 Settings *dir_settings;
339 gboolean force_resize;
341 dir_settings = (Settings *) g_hash_table_lookup(settings_table,
342 filer_window->sym_path);
344 force_resize = !(o_filer_auto_resize.int_value == RESIZE_NEVER &&
345 dir_settings && dir_settings->flags & SET_POSITION);
347 view_style_changed(filer_window->view, 0);
349 if (filer_window->open_timeout)
351 g_source_remove(filer_window->open_timeout);
352 filer_window->open_timeout = 0;
355 if (!GTK_WIDGET_VISIBLE(filer_window->window))
357 display_set_actual_size(filer_window, force_resize);
358 gtk_widget_show(filer_window->window);
361 return FALSE;
364 /* Look through all items we want to display, and queue a recheck on any
365 * that require it.
367 static void queue_interesting(FilerWindow *filer_window)
369 DirItem *item;
370 ViewIter iter;
372 view_get_iter(filer_window->view, &iter, 0);
373 while ((item = iter.next(&iter)))
375 if (item->flags & ITEM_FLAG_NEED_RESCAN_QUEUE)
376 dir_queue_recheck(filer_window->directory, item);
380 static void update_display(Directory *dir,
381 DirAction action,
382 GPtrArray *items,
383 FilerWindow *filer_window)
385 ViewIface *view = (ViewIface *) filer_window->view;
387 switch (action)
389 case DIR_ADD:
390 view_add_items(view, items);
391 /* Open and resize if currently hidden */
392 open_filer_window(filer_window);
393 break;
394 case DIR_REMOVE:
395 view_delete_if(view, if_deleted, items);
396 toolbar_update_info(filer_window);
397 break;
398 case DIR_START_SCAN:
399 set_scanning_display(filer_window, TRUE);
400 toolbar_update_info(filer_window);
401 break;
402 case DIR_END_SCAN:
403 if (filer_window->window->window)
404 gdk_window_set_cursor(
405 filer_window->window->window,
406 NULL);
407 set_scanning_display(filer_window, FALSE);
408 toolbar_update_info(filer_window);
409 open_filer_window(filer_window);
411 if (filer_window->had_cursor &&
412 !view_cursor_visible(view))
414 ViewIter start;
415 view_get_iter(view, &start, 0);
416 if (start.next(&start))
417 view_cursor_to_iter(view, &start);
418 view_show_cursor(view);
419 filer_window->had_cursor = FALSE;
421 if (filer_window->auto_select)
422 display_set_autoselect(filer_window,
423 filer_window->auto_select);
424 null_g_free(&filer_window->auto_select);
426 filer_create_thumbs(filer_window);
428 if (filer_window->thumb_queue)
429 start_thumb_scanning(filer_window);
430 break;
431 case DIR_UPDATE:
432 view_update_items(view, items);
433 break;
434 case DIR_ERROR_CHANGED:
435 filer_set_title(filer_window);
436 break;
437 case DIR_QUEUE_INTERESTING:
438 queue_interesting(filer_window);
439 break;
443 static void attach(FilerWindow *filer_window)
445 gdk_window_set_cursor(filer_window->window->window, busy_cursor);
446 view_clear(filer_window->view);
447 filer_window->scanning = TRUE;
448 dir_attach(filer_window->directory, (DirCallback) update_display,
449 filer_window);
450 filer_set_title(filer_window);
451 bookmarks_add_history(filer_window->sym_path);
453 if (filer_window->directory->error)
455 if (spring_in_progress)
456 g_printerr(_("Error scanning '%s':\n%s\n"),
457 filer_window->sym_path,
458 filer_window->directory->error);
459 else
460 delayed_error(_("Error scanning '%s':\n%s"),
461 filer_window->sym_path,
462 filer_window->directory->error);
466 static void detach(FilerWindow *filer_window)
468 g_return_if_fail(filer_window->directory != NULL);
470 dir_detach(filer_window->directory,
471 (DirCallback) update_display, filer_window);
472 g_object_unref(filer_window->directory);
473 filer_window->directory = NULL;
476 /* If 'start' was mounted by ROX-Filer, return it. Otherwise, try the
477 * parents up the tree.
478 * NULL if we're not in a user mount point.
479 * g_free() the result.
481 static char *get_ancestor_user_mount_point(const char *start)
483 char *path;
485 path = strdup(start);
487 while (1)
489 char *slash;
491 if (mount_is_user_mounted(path))
492 return path;
494 if (!path[1])
496 g_free(path);
497 return NULL;
500 slash = strrchr(path + 1, '/');
501 if (!slash)
502 slash = path + 1;
503 *slash = '\0';
507 static void umount_dialog_response(GtkWidget *dialog, int response, char *mount)
509 GList *list;
511 switch (response)
513 case GTK_RESPONSE_OK:
514 list = g_list_prepend(NULL, mount);
515 action_mount(list, FALSE, FALSE, TRUE);
516 g_list_free(list);
517 break;
519 case ROX_RESPONSE_EJECT:
520 list = g_list_prepend(NULL, mount);
521 action_eject(list);
522 g_list_free(list);
523 break;
525 default:
526 break;
529 g_free(mount);
531 gtk_widget_destroy(dialog);
533 one_less_window();
536 /* 'filer_window' shows a directory under 'mount'. If no other window also
537 * shows a directory under it, display a non-modal dialog offering to
538 * unmount the directory.
539 * 'mount' is freed by this function, either directly, or after the dialog
540 * closes.
542 static void may_offer_unmount(FilerWindow *filer_window, char *mount)
544 GtkWidget *dialog, *button;
545 GList *next;
546 int len;
548 len = strlen(mount);
550 for (next = all_filer_windows; next; next = next->next)
552 FilerWindow *other = (FilerWindow *) next->data;
554 if (other == filer_window)
555 continue;
557 if (strncmp(filer_window->real_path, other->real_path,
558 len) != 0)
559 continue;
561 g_return_if_fail(
562 filer_window->real_path[len] != '/' ||
563 filer_window->real_path[len] != '\0');
565 if (other->real_path[len] != '/' &&
566 other->real_path[len] != '\0')
567 continue;
569 /* Found another window. Don't offer to unmount. */
570 g_free(mount);
571 return;
574 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
575 GTK_BUTTONS_NONE,
576 _("Do you want to unmount this device?\n\n"
577 "Unmounting a device makes it safe to remove "
578 "the disk."), mount);
580 button = button_new_mixed(ROX_STOCK_MOUNTED, _("No change"));
581 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
582 gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button,
583 GTK_RESPONSE_CANCEL);
584 gtk_widget_show(button);
586 button = button_new_mixed(ROX_STOCK_MOUNT, _("Unmount"));
587 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
588 gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button,
589 GTK_RESPONSE_OK);
590 gtk_widget_show(button);
592 /* We need a better icon, but I can't draw */
593 button = button_new_mixed(GTK_STOCK_UNDO, _("Eject"));
594 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
595 gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button,
596 ROX_RESPONSE_EJECT);
597 gtk_widget_show(button);
599 g_signal_connect(G_OBJECT(dialog), "response",
600 G_CALLBACK(umount_dialog_response), mount);
602 gtk_dialog_set_default_response(GTK_DIALOG(dialog),
603 GTK_RESPONSE_OK);
605 number_of_windows++;
606 gtk_widget_show(dialog);
609 /* Returns TRUE to prevent closing the window. May offer to unmount a
610 * device.
612 gboolean filer_window_delete(GtkWidget *window,
613 GdkEvent *unused, /* (may be NULL) */
614 FilerWindow *filer_window)
616 char *mount;
618 mount = get_ancestor_user_mount_point(filer_window->real_path);
620 if (mount)
621 may_offer_unmount(filer_window, mount);
623 return FALSE;
626 static void filer_window_destroyed(GtkWidget *widget, FilerWindow *filer_window)
628 all_filer_windows = g_list_remove(all_filer_windows, filer_window);
630 g_object_set_data(G_OBJECT(widget), "filer_window", NULL);
632 if (window_with_primary == filer_window)
633 window_with_primary = NULL;
635 if (window_with_focus == filer_window)
637 menu_popdown();
638 window_with_focus = NULL;
641 if (filer_window->directory)
642 detach(filer_window);
644 if (filer_window->open_timeout)
646 g_source_remove(filer_window->open_timeout);
647 filer_window->open_timeout = 0;
650 if (filer_window->auto_scroll != -1)
652 g_source_remove(filer_window->auto_scroll);
653 filer_window->auto_scroll = -1;
656 if (filer_window->thumb_queue)
657 destroy_glist(&filer_window->thumb_queue);
659 tooltip_show(NULL);
661 filer_set_id(filer_window, NULL);
663 if(filer_window->filter_string)
664 g_free(filer_window->filter_string);
665 if(filer_window->regexp)
666 g_free(filer_window->regexp);
668 g_free(filer_window->auto_select);
669 g_free(filer_window->real_path);
670 g_free(filer_window->sym_path);
671 g_free(filer_window);
673 one_less_window();
676 /* Returns TRUE iff the directory still exists. */
677 static gboolean may_rescan(FilerWindow *filer_window, gboolean warning)
679 Directory *dir;
681 g_return_val_if_fail(filer_window != NULL, FALSE);
683 /* We do a fresh lookup (rather than update) because the inode may
684 * have changed.
686 dir = g_fscache_lookup(dir_cache, filer_window->real_path);
687 if (!dir)
689 if (warning)
690 info_message(_("Directory missing/deleted"));
691 gtk_widget_destroy(filer_window->window);
692 return FALSE;
694 if (dir == filer_window->directory)
695 g_object_unref(dir);
696 else
698 detach(filer_window);
699 filer_window->directory = dir;
700 attach(filer_window);
703 return TRUE;
706 /* No items are now selected. This might be because another app claimed
707 * the selection or because the user unselected all the items.
709 void filer_lost_selection(FilerWindow *filer_window, guint time)
711 if (window_with_primary == filer_window)
713 window_with_primary = NULL;
714 gtk_selection_owner_set(NULL, GDK_SELECTION_PRIMARY, time);
718 /* Another app has claimed the primary selection */
719 static void filer_lost_primary(GtkWidget *window,
720 GdkEventSelection *event,
721 gpointer user_data)
723 FilerWindow *filer_window = (FilerWindow *) user_data;
725 if (window_with_primary && window_with_primary == filer_window)
727 window_with_primary = NULL;
728 set_selection_state(filer_window, FALSE);
732 /* Someone wants us to send them the selection */
733 static void selection_get(GtkWidget *widget,
734 GtkSelectionData *selection_data,
735 guint info,
736 guint time,
737 gpointer data)
739 GString *reply, *header;
740 FilerWindow *filer_window = (FilerWindow *) data;
741 ViewIter iter;
742 DirItem *item;
744 reply = g_string_new(NULL);
745 header = g_string_new(NULL);
747 switch (info)
749 case TARGET_STRING:
750 g_string_printf(header, " %s",
751 make_path(filer_window->sym_path, ""));
752 break;
753 case TARGET_URI_LIST:
754 g_string_printf(header, " file://%s%s",
755 our_host_name_for_dnd(),
756 make_path(filer_window->sym_path, ""));
757 break;
760 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
762 while ((item = iter.next(&iter)))
764 g_string_append(reply, header->str);
765 g_string_append(reply, item->leafname);
768 if (reply->len > 0)
769 gtk_selection_data_set_text(selection_data,
770 reply->str + 1, reply->len - 1);
771 else
773 g_warning("Attempt to paste empty selection!");
774 gtk_selection_data_set_text(selection_data, "", 0);
777 g_string_free(reply, TRUE);
778 g_string_free(header, TRUE);
781 /* Selection has been changed -- try to grab the primary selection
782 * if we don't have it. Also called when clicking on an insensitive selection
783 * to regain primary.
784 * Also updates toolbar info.
786 void filer_selection_changed(FilerWindow *filer_window, gint time)
788 g_return_if_fail(filer_window != NULL);
790 toolbar_update_info(filer_window);
792 if (window_with_primary == filer_window)
793 return; /* Already got primary */
795 if (!view_count_selected(filer_window->view))
796 return; /* Nothing selected */
798 if (filer_window->temp_item_selected == FALSE &&
799 gtk_selection_owner_set(GTK_WIDGET(filer_window->window),
800 GDK_SELECTION_PRIMARY,
801 time))
803 window_with_primary = filer_window;
804 set_selection_state(filer_window, TRUE);
806 else
807 set_selection_state(filer_window, FALSE);
810 /* Open the item (or add it to the shell command minibuffer) */
811 void filer_openitem(FilerWindow *filer_window, ViewIter *iter, OpenFlags flags)
813 gboolean shift = (flags & OPEN_SHIFT) != 0;
814 gboolean close_mini = flags & OPEN_FROM_MINI;
815 gboolean close_window = (flags & OPEN_CLOSE_WINDOW) != 0;
816 DirItem *item;
817 const guchar *full_path;
818 gboolean wink = TRUE;
819 Directory *old_dir;
821 g_return_if_fail(filer_window != NULL && iter != NULL);
823 item = iter->peek(iter);
825 g_return_if_fail(item != NULL);
827 if (filer_window->mini_type == MINI_SHELL)
829 minibuffer_add(filer_window, item->leafname);
830 return;
833 if (item->base_type == TYPE_UNKNOWN)
834 dir_update_item(filer_window->directory, item->leafname);
836 if (item->base_type == TYPE_DIRECTORY)
838 /* Never close a filer window when opening a directory
839 * (click on a dir or click on an app with shift).
841 if (shift || !(item->flags & ITEM_FLAG_APPDIR))
842 close_window = FALSE;
845 full_path = make_path(filer_window->sym_path, item->leafname);
846 if (shift && (item->flags & ITEM_FLAG_SYMLINK))
847 wink = FALSE;
849 old_dir = filer_window->directory;
850 if (run_diritem(full_path, item,
851 flags & OPEN_SAME_WINDOW ? filer_window : NULL,
852 filer_window,
853 shift))
855 if (old_dir != filer_window->directory)
856 return;
858 if (close_window)
859 gtk_widget_destroy(filer_window->window);
860 else
862 if (wink)
863 view_wink_item(filer_window->view, iter);
864 if (close_mini)
865 minibuffer_hide(filer_window);
870 static gint pointer_in(GtkWidget *widget,
871 GdkEventCrossing *event,
872 FilerWindow *filer_window)
874 may_rescan(filer_window, TRUE);
875 return FALSE;
878 static gint pointer_out(GtkWidget *widget,
879 GdkEventCrossing *event,
880 FilerWindow *filer_window)
882 tooltip_show(NULL);
883 return FALSE;
886 /* Move the cursor to the next selected item in direction 'dir'
887 * (+1 or -1).
889 void filer_next_selected(FilerWindow *filer_window, int dir)
891 ViewIter iter, cursor;
892 gboolean have_cursor;
893 ViewIface *view = filer_window->view;
895 g_return_if_fail(dir == 1 || dir == -1);
897 view_get_cursor(view, &cursor);
898 have_cursor = cursor.peek(&cursor) != NULL;
900 view_get_iter(view, &iter,
901 VIEW_ITER_SELECTED |
902 (have_cursor ? VIEW_ITER_FROM_CURSOR : 0) |
903 (dir < 0 ? VIEW_ITER_BACKWARDS : 0));
905 if (have_cursor && view_get_selected(view, &cursor))
906 iter.next(&iter); /* Skip the cursor itself */
908 if (iter.next(&iter))
909 view_cursor_to_iter(view, &iter);
910 else
911 gdk_beep();
913 return;
916 static void return_pressed(FilerWindow *filer_window, GdkEventKey *event)
918 TargetFunc cb = filer_window->target_cb;
919 gpointer data = filer_window->target_data;
920 OpenFlags flags = 0;
921 ViewIter iter;
923 filer_target_mode(filer_window, NULL, NULL, NULL);
925 view_get_cursor(filer_window->view, &iter);
926 if (!iter.peek(&iter))
927 return;
929 if (cb)
931 cb(filer_window, &iter, data);
932 return;
935 if (event->state & GDK_SHIFT_MASK)
936 flags |= OPEN_SHIFT;
937 if (event->state & GDK_MOD1_MASK)
938 flags |= OPEN_CLOSE_WINDOW;
939 else
940 flags |= OPEN_SAME_WINDOW;
942 filer_openitem(filer_window, &iter, flags);
945 /* Makes sure that 'groups' is up-to-date, reloading from file if it has
946 * changed. If no groups were loaded and there is no file then initialised
947 * groups to an empty document.
948 * Return the node for the 'name' group.
950 static xmlNode *group_find(char *name)
952 xmlNode *node;
953 gchar *path;
955 /* Update the groups, if possible */
956 path = choices_find_xdg_path_load("Groups.xml", PROJECT, SITE);
957 if (path)
959 XMLwrapper *wrapper;
960 wrapper = xml_cache_load(path);
961 if (wrapper)
963 if (groups)
964 g_object_unref(groups);
965 groups = wrapper;
968 g_free(path);
971 if (!groups)
973 groups = xml_new(NULL);
974 groups->doc = xmlNewDoc("1.0");
976 xmlDocSetRootElement(groups->doc,
977 xmlNewDocNode(groups->doc, NULL, "groups", NULL));
978 return NULL;
981 node = xmlDocGetRootElement(groups->doc);
983 for (node = node->xmlChildrenNode; node; node = node->next)
985 guchar *gid;
987 gid = xmlGetProp(node, "name");
989 if (!gid)
990 continue;
992 if (strcmp(name, gid) != 0)
993 continue;
995 g_free(gid);
997 return node;
1000 return NULL;
1003 static void group_save(FilerWindow *filer_window, char *name)
1005 xmlNode *group;
1006 guchar *save_path;
1007 DirItem *item;
1008 ViewIter iter;
1010 group = group_find(name);
1011 if (group)
1013 xmlUnlinkNode(group);
1014 xmlFreeNode(group);
1016 group = xmlNewChild(xmlDocGetRootElement(groups->doc),
1017 NULL, "group", NULL);
1018 xmlSetProp(group, "name", name);
1020 xmlNewTextChild(group, NULL, "directory", filer_window->sym_path);
1022 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
1024 while ((item = iter.next(&iter)))
1025 xmlNewTextChild(group, NULL, "item", item->leafname);
1027 save_path = choices_find_xdg_path_save("Groups.xml", PROJECT, SITE,
1028 TRUE);
1029 if (save_path)
1031 save_xml_file(groups->doc, save_path);
1032 g_free(save_path);
1036 static gboolean group_restore_cb(ViewIter *iter, gpointer data)
1038 GHashTable *in_group = (GHashTable *) data;
1040 return g_hash_table_lookup(in_group,
1041 iter->peek(iter)->leafname) != NULL;
1044 static void group_restore(FilerWindow *filer_window, char *name)
1046 GHashTable *in_group;
1047 char *path;
1048 xmlNode *group, *node;
1050 group = group_find(name);
1052 if (!group)
1054 report_error(_("Group %s is not set. Select some files "
1055 "and press Ctrl+%s to set the group. Press %s "
1056 "on its own to reselect the files later.\n"
1057 "Make sure NumLock is on if you use the keypad."),
1058 name, name, name);
1059 return;
1062 node = get_subnode(group, NULL, "directory");
1063 g_return_if_fail(node != NULL);
1064 path = xmlNodeListGetString(groups->doc, node->xmlChildrenNode, 1);
1065 g_return_if_fail(path != NULL);
1067 if (strcmp(path, filer_window->sym_path) != 0)
1068 filer_change_to(filer_window, path, NULL);
1069 g_free(path);
1071 in_group = g_hash_table_new(g_str_hash, g_str_equal);
1072 for (node = group->xmlChildrenNode; node; node = node->next)
1074 gchar *leaf;
1075 if (node->type != XML_ELEMENT_NODE)
1076 continue;
1077 if (strcmp(node->name, "item") != 0)
1078 continue;
1080 leaf = xmlNodeListGetString(groups->doc,
1081 node->xmlChildrenNode, 1);
1082 if (!leaf)
1083 g_warning("Missing leafname!\n");
1084 else
1085 g_hash_table_insert(in_group, leaf, filer_window);
1088 view_select_if(filer_window->view, &group_restore_cb, in_group);
1090 g_hash_table_foreach(in_group, (GHFunc) g_free, NULL);
1091 g_hash_table_destroy(in_group);
1094 static gboolean popup_menu(GtkWidget *widget, FilerWindow *filer_window)
1096 ViewIter iter;
1097 GdkEvent *event;
1099 view_get_cursor(filer_window->view, &iter);
1101 event = gtk_get_current_event();
1102 show_filer_menu(filer_window, event, &iter);
1103 if (event)
1104 gdk_event_free(event);
1106 return TRUE;
1109 void filer_window_toggle_cursor_item_selected(FilerWindow *filer_window)
1111 ViewIface *view = filer_window->view;
1112 ViewIter iter;
1114 view_get_iter(view, &iter, VIEW_ITER_FROM_CURSOR);
1115 if (!iter.next(&iter))
1116 return; /* No cursor */
1118 if (view_get_selected(view, &iter))
1119 view_set_selected(view, &iter, FALSE);
1120 else
1121 view_set_selected(view, &iter, TRUE);
1123 if (iter.next(&iter))
1124 view_cursor_to_iter(view, &iter);
1127 gint filer_key_press_event(GtkWidget *widget,
1128 GdkEventKey *event,
1129 FilerWindow *filer_window)
1131 ViewIface *view = filer_window->view;
1132 ViewIter cursor;
1133 GtkWidget *focus = GTK_WINDOW(widget)->focus_widget;
1134 guint key = event->keyval;
1135 char group[2] = "1";
1137 window_with_focus = filer_window;
1139 /* Delay setting up the keys until now to speed loading... */
1140 if (ensure_filer_menu())
1142 /* Gtk updates in an idle-handler, so force a recheck now */
1143 g_signal_emit_by_name(widget, "keys_changed");
1146 if (focus && focus == filer_window->minibuffer)
1147 if (gtk_widget_event(focus, (GdkEvent *) event))
1148 return TRUE; /* Handled */
1150 if (!focus)
1151 gtk_widget_grab_focus(GTK_WIDGET(view));
1153 view_get_cursor(view, &cursor);
1154 if (!cursor.peek(&cursor) && (key == GDK_Up || key == GDK_Down))
1156 ViewIter iter;
1157 view_get_iter(view, &iter, 0);
1158 if (iter.next(&iter))
1159 view_cursor_to_iter(view, &iter);
1160 gtk_widget_grab_focus(GTK_WIDGET(view)); /* Needed? */
1161 return TRUE;
1164 switch (key)
1166 case GDK_Escape:
1167 filer_target_mode(filer_window, NULL, NULL, NULL);
1168 view_cursor_to_iter(filer_window->view, NULL);
1169 view_clear_selection(filer_window->view);
1170 return FALSE;
1171 case GDK_Return:
1172 return_pressed(filer_window, event);
1173 break;
1174 case GDK_ISO_Left_Tab:
1175 filer_next_selected(filer_window, -1);
1176 break;
1177 case GDK_Tab:
1178 filer_next_selected(filer_window, 1);
1179 break;
1180 case GDK_BackSpace:
1181 change_to_parent(filer_window);
1182 break;
1183 case GDK_backslash:
1185 ViewIter iter;
1187 tooltip_show(NULL);
1189 view_get_cursor(filer_window->view, &iter);
1190 show_filer_menu(filer_window,
1191 (GdkEvent *) event, &iter);
1192 break;
1194 case ' ':
1195 filer_window_toggle_cursor_item_selected(filer_window);
1196 break;
1197 default:
1198 if (key >= GDK_0 && key <= GDK_9)
1199 group[0] = key - GDK_0 + '0';
1200 else if (key >= GDK_KP_0 && key <= GDK_KP_9)
1201 group[0] = key - GDK_KP_0 + '0';
1202 else
1204 if (focus && focus != widget &&
1205 gtk_widget_get_toplevel(focus) == widget)
1206 if (gtk_widget_event(focus,
1207 (GdkEvent *) event))
1208 return TRUE; /* Handled */
1209 return FALSE;
1212 if (event->state & GDK_CONTROL_MASK)
1213 group_save(filer_window, group);
1214 else
1215 group_restore(filer_window, group);
1218 return TRUE;
1221 void filer_open_parent(FilerWindow *filer_window)
1223 char *dir;
1224 const char *current = filer_window->sym_path;
1226 if (current[0] == '/' && current[1] == '\0')
1227 return; /* Already in the root */
1229 dir = g_path_get_dirname(current);
1230 filer_opendir(dir, filer_window, NULL);
1231 g_free(dir);
1234 void change_to_parent(FilerWindow *filer_window)
1236 char *dir;
1237 const char *current = filer_window->sym_path;
1239 if (current[0] == '/' && current[1] == '\0')
1240 return; /* Already in the root */
1242 if (mount_is_user_mounted(filer_window->real_path))
1243 may_offer_unmount(filer_window,
1244 g_strdup(filer_window->real_path));
1246 dir = g_path_get_dirname(current);
1247 filer_change_to(filer_window, dir, g_basename(current));
1248 g_free(dir);
1251 /* Removes trailing /s from path (modified in place) */
1252 static void tidy_sympath(gchar *path)
1254 int l;
1256 g_return_if_fail(path != NULL);
1258 l = strlen(path);
1259 while (l > 1 && path[l - 1] == '/')
1261 l--;
1262 path[l] = '\0';
1266 /* Make filer_window display path. When finished, highlight item 'from', or
1267 * the first item if from is NULL. If there is currently no cursor then
1268 * simply wink 'from' (if not NULL).
1269 * If the cause was a key event and we resize, warp the pointer.
1271 void filer_change_to(FilerWindow *filer_window,
1272 const char *path, const char *from)
1274 char *from_dup;
1275 char *sym_path, *real_path;
1276 Directory *new_dir;
1278 g_return_if_fail(filer_window != NULL);
1280 filer_cancel_thumbnails(filer_window);
1282 tooltip_show(NULL);
1284 sym_path = g_strdup(path);
1285 real_path = pathdup(path);
1286 new_dir = g_fscache_lookup(dir_cache, real_path);
1288 if (!new_dir)
1290 delayed_error(_("Directory '%s' is not accessible"),
1291 sym_path);
1292 g_free(real_path);
1293 g_free(sym_path);
1294 return;
1297 if (o_unique_filer_windows.int_value && !spring_in_progress)
1299 FilerWindow *fw;
1301 fw = find_filer_window(sym_path, filer_window);
1302 if (fw)
1303 gtk_widget_destroy(fw->window);
1306 from_dup = from && *from ? g_strdup(from) : NULL;
1308 detach(filer_window);
1309 g_free(filer_window->real_path);
1310 g_free(filer_window->sym_path);
1311 filer_window->real_path = real_path;
1312 filer_window->sym_path = sym_path;
1313 tidy_sympath(filer_window->sym_path);
1315 filer_window->directory = new_dir;
1317 g_free(filer_window->auto_select);
1318 filer_window->auto_select = from_dup;
1320 filer_window->had_cursor = filer_window->had_cursor ||
1321 view_cursor_visible(filer_window->view);
1323 filer_set_title(filer_window);
1324 if (filer_window->window->window)
1325 gdk_window_set_role(filer_window->window->window,
1326 filer_window->sym_path);
1327 view_cursor_to_iter(filer_window->view, NULL);
1329 attach(filer_window);
1331 check_settings(filer_window);
1333 display_set_actual_size(filer_window, FALSE);
1335 if (o_filer_auto_resize.int_value == RESIZE_ALWAYS)
1336 view_autosize(filer_window->view);
1338 if (filer_window->mini_type == MINI_PATH)
1339 g_idle_add((GSourceFunc) minibuffer_show_cb, filer_window);
1342 /* Returns a list containing the full (sym) pathname of every selected item.
1343 * You must g_free() each item in the list.
1345 GList *filer_selected_items(FilerWindow *filer_window)
1347 GList *retval = NULL;
1348 guchar *dir = filer_window->sym_path;
1349 ViewIter iter;
1350 DirItem *item;
1352 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
1353 while ((item = iter.next(&iter)))
1355 retval = g_list_prepend(retval,
1356 g_strdup(make_path(dir, item->leafname)));
1359 return g_list_reverse(retval);
1362 /* Return the single selected item. Error if nothing is selected. */
1363 DirItem *filer_selected_item(FilerWindow *filer_window)
1365 ViewIter iter;
1366 DirItem *item;
1368 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
1370 item = iter.next(&iter);
1371 g_return_val_if_fail(item != NULL, NULL);
1372 g_return_val_if_fail(iter.next(&iter) == NULL, NULL);
1374 return item;
1377 /* Creates and shows a new filer window.
1378 * If src_win != NULL then display options can be taken from that source window.
1379 * Returns the new filer window, or NULL on error.
1380 * Note: if unique windows is in use, may return an existing window.
1382 FilerWindow *filer_opendir(const char *path, FilerWindow *src_win,
1383 const gchar *wm_class)
1385 FilerWindow *filer_window;
1386 char *real_path;
1387 DisplayStyle dstyle;
1388 DetailsType dtype;
1389 SortType s_type;
1390 GtkSortType s_order;
1391 Settings *dir_settings = NULL;
1392 gboolean force_resize = TRUE;
1394 /* Get the real pathname of the directory and copy it */
1395 real_path = pathdup(path);
1397 if (o_unique_filer_windows.int_value && !spring_in_progress)
1399 FilerWindow *same_dir_window;
1401 same_dir_window = find_filer_window(path, NULL);
1403 if (same_dir_window)
1405 gtk_window_present(GTK_WINDOW(same_dir_window->window));
1406 return same_dir_window;
1410 filer_window = g_new(FilerWindow, 1);
1411 filer_window->message = NULL;
1412 filer_window->minibuffer = NULL;
1413 filer_window->minibuffer_label = NULL;
1414 filer_window->minibuffer_area = NULL;
1415 filer_window->temp_show_hidden = FALSE;
1416 filer_window->sym_path = g_strdup(path);
1417 filer_window->real_path = real_path;
1418 filer_window->scanning = FALSE;
1419 filer_window->had_cursor = FALSE;
1420 filer_window->auto_select = NULL;
1421 filer_window->toolbar_text = NULL;
1422 filer_window->target_cb = NULL;
1423 filer_window->mini_type = MINI_NONE;
1424 filer_window->selection_state = GTK_STATE_INSENSITIVE;
1425 filer_window->toolbar = NULL;
1426 filer_window->toplevel_vbox = NULL;
1427 filer_window->view_hbox = NULL;
1428 filer_window->view = NULL;
1429 filer_window->scrollbar = NULL;
1430 filer_window->auto_scroll = -1;
1431 filer_window->window_id = NULL;
1433 tidy_sympath(filer_window->sym_path);
1435 /* Finds the entry for this directory in the dir cache, creating
1436 * a new one if needed. This does not cause a scan to start,
1437 * so if a new entry is created then it will be empty.
1439 filer_window->directory = g_fscache_lookup(dir_cache, real_path);
1440 if (!filer_window->directory)
1442 delayed_error(_("Directory '%s' not found."), path);
1443 g_free(filer_window->real_path);
1444 g_free(filer_window->sym_path);
1445 g_free(filer_window);
1446 return NULL;
1449 filer_window->temp_item_selected = FALSE;
1450 filer_window->flags = (FilerFlags) 0;
1451 filer_window->details_type = DETAILS_TIMES;
1452 filer_window->display_style = UNKNOWN_STYLE;
1453 filer_window->display_style_wanted = UNKNOWN_STYLE;
1454 filer_window->thumb_queue = NULL;
1455 filer_window->max_thumbs = 0;
1456 filer_window->sort_type = -1;
1458 filer_window->filter = FILER_SHOW_ALL;
1459 filer_window->filter_string = NULL;
1460 filer_window->regexp = NULL;
1461 filer_window->filter_directories = FALSE;
1463 if (src_win && o_display_inherit_options.int_value)
1465 s_type = src_win->sort_type;
1466 s_order = src_win->sort_order;
1467 dstyle = src_win->display_style_wanted;
1468 dtype = src_win->details_type;
1469 filer_window->show_hidden = src_win->show_hidden;
1470 filer_window->show_thumbs = src_win->show_thumbs;
1471 filer_window->view_type = src_win->view_type;
1473 filer_window->filter_directories = src_win->filter_directories;
1474 filer_set_filter(filer_window, src_win->filter,
1475 src_win->filter_string);
1477 else
1479 s_type = o_display_sort_by.int_value;
1480 s_order = GTK_SORT_ASCENDING;
1481 dstyle = o_display_size.int_value;
1482 dtype = o_display_details.int_value;
1483 filer_window->show_hidden = o_display_show_hidden.int_value;
1484 filer_window->show_thumbs = o_display_show_thumbs.int_value;
1485 filer_window->view_type = o_filer_view_type.int_value;
1488 dir_settings = (Settings *) g_hash_table_lookup(settings_table,
1489 filer_window->sym_path);
1490 if (dir_settings)
1492 /* Override the current defaults with the per-directory
1493 * user settings.
1495 if (dir_settings->flags & SET_HIDDEN)
1496 filer_window->show_hidden = dir_settings->show_hidden;
1498 if (dir_settings->flags & SET_STYLE)
1499 dstyle = dir_settings->display_style;
1501 if (dir_settings->flags & SET_DETAILS)
1503 dtype = dir_settings->details_type;
1504 filer_window->view_type = dir_settings->view_type;
1507 if (dir_settings->flags & SET_SORT)
1509 s_type = dir_settings->sort_type;
1510 s_order = dir_settings->sort_order;
1513 if (dir_settings->flags & SET_THUMBS)
1514 filer_window->show_thumbs = dir_settings->show_thumbs;
1516 if (dir_settings->flags & SET_FILTER)
1518 filer_set_filter(filer_window,
1519 dir_settings->filter_type,
1520 dir_settings->filter);
1521 filer_set_filter_directories(filer_window,
1522 dir_settings->filter_directories);
1526 /* Add all the user-interface elements & realise */
1527 filer_add_widgets(filer_window, wm_class);
1528 if (src_win)
1529 gtk_window_set_position(GTK_WINDOW(filer_window->window),
1530 GTK_WIN_POS_MOUSE);
1532 if (dir_settings)
1534 if (dir_settings->flags & SET_POSITION)
1536 gtk_window_move(GTK_WINDOW(filer_window->window),
1537 dir_settings->x, dir_settings->y);
1539 if (dir_settings->flags & SET_SIZE)
1541 filer_window_set_size(filer_window,
1542 dir_settings->width,
1543 dir_settings->height);
1544 force_resize = o_filer_auto_resize.int_value != RESIZE_NEVER;
1548 /* Connect to all the signal handlers */
1549 filer_add_signals(filer_window);
1551 display_set_layout(filer_window, dstyle, dtype, force_resize);
1552 display_set_sort_type(filer_window, s_type, s_order);
1554 /* Open the window after a timeout, or when scanning stops.
1555 * Do this before attaching, because attach() might tell us to
1556 * stop scanning (if a scan isn't needed).
1558 filer_window->open_timeout = g_timeout_add(500,
1559 (GSourceFunc) open_filer_window,
1560 filer_window);
1562 /* The view is created empty and then attach() is called, which
1563 * links the filer window to the entry in the directory cache we
1564 * looked up / created above.
1566 * The attach() function will immediately callback to the filer window
1567 * to deliver a list of all known entries in the directory (so,
1568 * the number of items will be known after attach() returns).
1570 * If the directory was not in the cache (because it hadn't been
1571 * opened it before) then the types and icons for the entries are
1572 * not know, but the list of names is.
1575 attach(filer_window);
1577 number_of_windows++;
1578 all_filer_windows = g_list_prepend(all_filer_windows, filer_window);
1580 return filer_window;
1583 void filer_set_view_type(FilerWindow *filer_window, ViewType type)
1585 GtkWidget *view = NULL;
1586 Directory *dir = NULL;
1587 GHashTable *selected = NULL;
1589 g_return_if_fail(filer_window != NULL);
1591 motion_window = NULL;
1593 if (filer_window->view)
1595 /* Save the current selection */
1596 ViewIter iter;
1597 DirItem *item;
1599 selected = g_hash_table_new(g_str_hash, g_str_equal);
1600 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
1601 while ((item = iter.next(&iter)))
1602 g_hash_table_insert(selected, item->leafname, "yes");
1604 /* Destroy the old view */
1605 gtk_widget_destroy(GTK_WIDGET(filer_window->view));
1606 filer_window->view = NULL;
1608 dir = filer_window->directory;
1609 g_object_ref(dir);
1610 detach(filer_window);
1613 switch (type)
1615 case VIEW_TYPE_COLLECTION:
1616 view = view_collection_new(filer_window);
1617 break;
1618 case VIEW_TYPE_DETAILS:
1619 view = view_details_new(filer_window);
1620 break;
1623 g_return_if_fail(view != NULL);
1625 filer_window->view = VIEW(view);
1626 filer_window->view_type = type;
1628 gtk_box_pack_start(filer_window->view_hbox, view, TRUE, TRUE, 0);
1629 gtk_widget_show(view);
1631 /* Drag and drop events */
1632 make_drop_target(view, 0);
1633 g_signal_connect(view, "drag_motion",
1634 G_CALLBACK(drag_motion), filer_window);
1635 g_signal_connect(view, "drag_leave",
1636 G_CALLBACK(drag_leave), filer_window);
1637 g_signal_connect(view, "drag_end",
1638 G_CALLBACK(drag_end), filer_window);
1639 /* Dragging from us... */
1640 g_signal_connect(view, "drag_data_get",
1641 GTK_SIGNAL_FUNC(drag_data_get), NULL);
1643 if (dir)
1645 /* Only when changing type. Otherwise, will attach later. */
1646 filer_window->directory = dir;
1647 attach(filer_window);
1649 if (o_filer_auto_resize.int_value != RESIZE_NEVER)
1650 view_autosize(filer_window->view);
1653 if (selected)
1655 ViewIter iter;
1656 DirItem *item;
1658 view_get_iter(filer_window->view, &iter, 0);
1659 while ((item = iter.next(&iter)))
1661 if (g_hash_table_lookup(selected, item->leafname))
1662 view_set_selected(filer_window->view,
1663 &iter, TRUE);
1665 g_hash_table_destroy(selected);
1669 /* This adds all the widgets to a new filer window. It is in a separate
1670 * function because filer_opendir() was getting too long...
1672 static void filer_add_widgets(FilerWindow *filer_window, const gchar *wm_class)
1674 GtkWidget *hbox, *vbox;
1676 /* Create the top-level window widget */
1677 filer_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1678 filer_set_title(filer_window);
1679 gtk_widget_set_name(filer_window->window, "rox-filer");
1681 if (wm_class)
1682 gtk_window_set_wmclass(GTK_WINDOW(filer_window->window),
1683 wm_class, PROJECT);
1685 /* This property is cleared when the window is destroyed.
1686 * You can thus ref filer_window->window and use this to see
1687 * if the window no longer exists.
1689 g_object_set_data(G_OBJECT(filer_window->window),
1690 "filer_window", filer_window);
1692 /* Create this now to make the Adjustment before the View */
1693 filer_window->scrollbar = gtk_vscrollbar_new(NULL);
1695 vbox = gtk_vbox_new(FALSE, 0);
1696 gtk_container_add(GTK_CONTAINER(filer_window->window), vbox);
1698 filer_window->toplevel_vbox = GTK_BOX(vbox);
1700 /* If there's a message that should be displayed in each window (eg
1701 * 'Running as root'), add it here.
1703 if (show_user_message)
1705 filer_window->message = gtk_label_new(show_user_message);
1706 gtk_box_pack_start(GTK_BOX(vbox), filer_window->message,
1707 FALSE, TRUE, 0);
1708 gtk_widget_show(filer_window->message);
1711 hbox = gtk_hbox_new(FALSE, 0);
1712 filer_window->view_hbox = GTK_BOX(hbox);
1713 gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox);
1714 /* Add the main View widget */
1715 filer_set_view_type(filer_window, filer_window->view_type);
1716 /* Put the scrollbar next to the View */
1717 gtk_box_pack_end(GTK_BOX(hbox),
1718 filer_window->scrollbar, FALSE, TRUE, 0);
1719 gtk_widget_show(hbox);
1721 /* If we want a toolbar, create it now */
1722 toolbar_update_toolbar(filer_window);
1724 /* And the minibuffer (hidden to start with) */
1725 create_minibuffer(filer_window);
1726 gtk_box_pack_end(GTK_BOX(vbox), filer_window->minibuffer_area,
1727 FALSE, TRUE, 0);
1729 /* And the thumbnail progress bar (also hidden) */
1731 GtkWidget *cancel;
1733 filer_window->thumb_bar = gtk_hbox_new(FALSE, 2);
1734 gtk_box_pack_end(GTK_BOX(vbox), filer_window->thumb_bar,
1735 FALSE, TRUE, 0);
1737 filer_window->thumb_progress = gtk_progress_bar_new();
1739 gtk_box_pack_start(GTK_BOX(filer_window->thumb_bar),
1740 filer_window->thumb_progress, TRUE, TRUE, 0);
1742 cancel = gtk_button_new_with_label(_("Cancel"));
1743 GTK_WIDGET_UNSET_FLAGS(cancel, GTK_CAN_FOCUS);
1744 gtk_box_pack_start(GTK_BOX(filer_window->thumb_bar),
1745 cancel, FALSE, TRUE, 0);
1746 g_signal_connect_swapped(cancel, "clicked",
1747 G_CALLBACK(filer_cancel_thumbnails),
1748 filer_window);
1751 gtk_widget_show(vbox);
1752 gtk_widget_show(filer_window->scrollbar);
1754 gtk_widget_realize(filer_window->window);
1756 gdk_window_set_role(filer_window->window->window,
1757 filer_window->sym_path);
1759 filer_window_set_size(filer_window, 4, 4);
1762 static void filer_add_signals(FilerWindow *filer_window)
1764 GtkTargetEntry target_table[] =
1766 {"text/uri-list", 0, TARGET_URI_LIST},
1767 {"UTF8_STRING", 0, TARGET_STRING},
1768 {"STRING", 0, TARGET_STRING},
1769 {"COMPOUND_TEXT", 0, TARGET_STRING},/* XXX: Treats as STRING */
1772 /* Events on the top-level window */
1773 gtk_widget_add_events(filer_window->window, GDK_ENTER_NOTIFY);
1774 g_signal_connect(filer_window->window, "enter-notify-event",
1775 G_CALLBACK(pointer_in), filer_window);
1776 g_signal_connect(filer_window->window, "leave-notify-event",
1777 G_CALLBACK(pointer_out), filer_window);
1778 g_signal_connect(filer_window->window, "destroy",
1779 G_CALLBACK(filer_window_destroyed), filer_window);
1780 g_signal_connect(filer_window->window, "delete-event",
1781 G_CALLBACK(filer_window_delete), filer_window);
1783 g_signal_connect(filer_window->window, "selection_clear_event",
1784 G_CALLBACK(filer_lost_primary), filer_window);
1786 g_signal_connect(filer_window->window, "selection_get",
1787 G_CALLBACK(selection_get), filer_window);
1788 gtk_selection_add_targets(GTK_WIDGET(filer_window->window),
1789 GDK_SELECTION_PRIMARY,
1790 target_table,
1791 sizeof(target_table) / sizeof(*target_table));
1793 g_signal_connect(filer_window->window, "popup-menu",
1794 G_CALLBACK(popup_menu), filer_window);
1795 g_signal_connect(filer_window->window, "key_press_event",
1796 G_CALLBACK(filer_key_press_event), filer_window);
1798 gtk_window_add_accel_group(GTK_WINDOW(filer_window->window),
1799 filer_keys);
1802 static gint clear_scanning_display(FilerWindow *filer_window)
1804 if (filer_exists(filer_window))
1805 filer_set_title(filer_window);
1806 return FALSE;
1809 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning)
1811 if (scanning == filer_window->scanning)
1812 return;
1813 filer_window->scanning = scanning;
1815 if (scanning)
1816 filer_set_title(filer_window);
1817 else
1818 g_timeout_add(300, (GSourceFunc) clear_scanning_display,
1819 filer_window);
1822 /* Note that filer_window may not exist after this call.
1823 * Returns TRUE iff the directory still exists.
1825 gboolean filer_update_dir(FilerWindow *filer_window, gboolean warning)
1827 gboolean still_exists;
1829 still_exists = may_rescan(filer_window, warning);
1831 if (still_exists)
1832 dir_update(filer_window->directory, filer_window->sym_path);
1834 return still_exists;
1837 void filer_update_all(void)
1839 GList *next = all_filer_windows;
1841 while (next)
1843 FilerWindow *filer_window = (FilerWindow *) next->data;
1845 /* Updating directory may remove it from list -- stop sending
1846 * patches to move this line!
1848 next = next->next;
1850 /* Don't trigger a refresh if we're already scanning.
1851 * Otherwise, two views of a single directory will trigger
1852 * two scans.
1854 if (filer_window->directory &&
1855 !filer_window->directory->scanning)
1856 filer_update_dir(filer_window, TRUE);
1860 /* Refresh the various caches even if we don't think we need to */
1861 void full_refresh(void)
1863 mount_update(TRUE);
1864 reread_mime_files(); /* Refreshes all windows */
1867 /* See whether a filer window with a given path already exists
1868 * and is different from diff.
1870 static FilerWindow *find_filer_window(const char *sym_path, FilerWindow *diff)
1872 GList *next;
1874 for (next = all_filer_windows; next; next = next->next)
1876 FilerWindow *filer_window = (FilerWindow *) next->data;
1878 if (filer_window != diff &&
1879 strcmp(sym_path, filer_window->sym_path) == 0)
1880 return filer_window;
1883 return NULL;
1886 /* This path has been mounted/umounted/deleted some files - update all dirs */
1887 void filer_check_mounted(const char *real_path)
1889 GList *next = all_filer_windows;
1890 gchar *parent;
1891 int len;
1892 gboolean resize = o_filer_auto_resize.int_value == RESIZE_ALWAYS;
1894 /* DOS disks, etc, often don't change the mtime of the root directory
1895 * on modification, so force a refresh now.
1897 g_fscache_update(dir_cache, real_path);
1899 len = strlen(real_path);
1901 while (next)
1903 FilerWindow *filer_window = (FilerWindow *) next->data;
1905 next = next->next;
1907 if (strncmp(real_path, filer_window->real_path, len) == 0)
1909 char s = filer_window->real_path[len];
1911 if (s == '/' || s == '\0')
1913 if (filer_update_dir(filer_window, FALSE) &&
1914 resize)
1915 view_autosize(filer_window->view);
1920 parent = g_path_get_dirname(real_path);
1921 refresh_dirs(parent);
1922 g_free(parent);
1924 icons_may_update(real_path);
1927 /* Close all windows displaying 'path' or subdirectories of 'path' */
1928 void filer_close_recursive(const char *path)
1930 GList *next = all_filer_windows;
1931 gchar *real;
1932 int len;
1934 real = pathdup(path);
1935 len = strlen(real);
1937 while (next)
1939 FilerWindow *filer_window = (FilerWindow *) next->data;
1941 next = next->next;
1943 if (strncmp(real, filer_window->real_path, len) == 0)
1945 char s = filer_window->real_path[len];
1947 if (len == 1 || s == '/' || s == '\0')
1948 gtk_widget_destroy(filer_window->window);
1953 /* Like minibuffer_show(), except that:
1954 * - It returns FALSE (to be used from an idle callback)
1955 * - It checks that the filer window still exists.
1957 static gboolean minibuffer_show_cb(FilerWindow *filer_window)
1959 if (filer_exists(filer_window))
1960 minibuffer_show(filer_window, MINI_PATH);
1961 return FALSE;
1964 /* TRUE iff filer_window points to an existing FilerWindow
1965 * structure.
1967 gboolean filer_exists(FilerWindow *filer_window)
1969 GList *next;
1971 for (next = all_filer_windows; next; next = next->next)
1973 FilerWindow *fw = (FilerWindow *) next->data;
1975 if (fw == filer_window)
1976 return TRUE;
1979 return FALSE;
1982 FilerWindow *filer_get_by_id(const char *id)
1984 return g_hash_table_lookup(window_with_id, id);
1987 void filer_set_id(FilerWindow *filer_window, const char *id)
1989 g_return_if_fail(filer_window != NULL);
1991 if (filer_window->window_id)
1993 g_hash_table_remove(window_with_id, filer_window->window_id);
1994 g_free(filer_window->window_id);
1995 filer_window->window_id = NULL;
1998 if (id)
2000 filer_window->window_id = g_strdup(id);
2001 g_hash_table_insert(window_with_id,
2002 filer_window->window_id,
2003 filer_window);
2007 /* Make sure the window title is up-to-date */
2008 void filer_set_title(FilerWindow *filer_window)
2010 gchar *title = NULL;
2011 guchar *flags = "";
2013 if (filer_window->scanning ||
2014 filer_window->filter != FILER_SHOW_ALL ||
2015 filer_window->show_hidden || filer_window->show_thumbs)
2017 if (o_short_flag_names.int_value)
2019 const gchar *hidden = "";
2021 switch(filer_window->filter) {
2022 case FILER_SHOW_ALL:
2023 hidden=filer_window->show_hidden? _("A") : "";
2024 break;
2025 case FILER_SHOW_GLOB: hidden = _("G"); break;
2026 default: break;
2029 flags = g_strconcat(" +",
2030 filer_window->scanning ? _("S") : "",
2031 hidden,
2032 filer_window->show_thumbs ? _("T") : "",
2033 NULL);
2035 else
2037 gchar *hidden = NULL;
2039 switch(filer_window->filter) {
2040 case FILER_SHOW_ALL:
2041 hidden = g_strdup(filer_window->show_hidden
2042 ? _("All, ") : "");
2043 break;
2044 case FILER_SHOW_GLOB:
2045 hidden = g_strdup_printf(_("Glob (%s), "),
2046 filer_window->filter_string);
2047 break;
2048 default:
2049 hidden =g_strdup("");
2050 break;
2052 flags = g_strconcat(" (",
2053 filer_window->scanning ? _("Scanning, ") : "",
2054 hidden,
2055 filer_window->show_thumbs ? _("Thumbs, ") : "",
2056 NULL);
2057 flags[strlen(flags) - 2] = ')';
2058 g_free(hidden);
2062 if (not_local)
2063 title = g_strconcat("//", our_host_name(),
2064 filer_window->sym_path, flags, NULL);
2066 if (!title && home_dir_len > 1 &&
2067 strncmp(filer_window->sym_path, home_dir, home_dir_len) == 0)
2069 guchar sep = filer_window->sym_path[home_dir_len];
2071 if (sep == '\0' || sep == '/')
2072 title = g_strconcat("~",
2073 filer_window->sym_path + home_dir_len,
2074 flags,
2075 NULL);
2078 if (!title)
2079 title = g_strconcat(filer_window->sym_path, flags, NULL);
2081 ensure_utf8(&title);
2083 if (filer_window->directory->error)
2085 gchar *old = title;
2086 title = g_strconcat(old, ": ", filer_window->directory->error,
2087 NULL);
2088 g_free(old);
2091 gtk_window_set_title(GTK_WINDOW(filer_window->window), title);
2093 g_free(title);
2095 if (flags[0] != '\0')
2096 g_free(flags);
2099 /* Reconnect to the same directory (used when the Show Hidden option is
2100 * toggled). This has the side-effect of updating the window title.
2102 void filer_detach_rescan(FilerWindow *filer_window)
2104 Directory *dir = filer_window->directory;
2106 g_object_ref(dir);
2107 detach(filer_window);
2108 filer_window->directory = dir;
2109 attach(filer_window);
2112 /* Puts the filer window into target mode. When an item is chosen,
2113 * fn(filer_window, iter, data) is called. 'reason' will be displayed
2114 * on the toolbar while target mode is active.
2116 * Use fn == NULL to cancel target mode.
2118 void filer_target_mode(FilerWindow *filer_window,
2119 TargetFunc fn,
2120 gpointer data,
2121 const char *reason)
2123 TargetFunc old_fn = filer_window->target_cb;
2125 if (fn != old_fn)
2126 gdk_window_set_cursor(
2127 GTK_WIDGET(filer_window->view)->window,
2128 fn ? crosshair : NULL);
2130 filer_window->target_cb = fn;
2131 filer_window->target_data = data;
2133 if (filer_window->toolbar_text == NULL)
2134 return;
2136 if (fn)
2137 gtk_label_set_text(
2138 GTK_LABEL(filer_window->toolbar_text), reason);
2139 else if (o_toolbar_info.int_value)
2141 if (old_fn)
2142 toolbar_update_info(filer_window);
2144 else
2145 gtk_label_set_text(GTK_LABEL(filer_window->toolbar_text), "");
2148 static void set_selection_state(FilerWindow *filer_window, gboolean normal)
2150 GtkStateType old_state = filer_window->selection_state;
2152 filer_window->selection_state = normal
2153 ? GTK_STATE_SELECTED : GTK_STATE_INSENSITIVE;
2155 if (old_state != filer_window->selection_state
2156 && view_count_selected(filer_window->view))
2157 gtk_widget_queue_draw(GTK_WIDGET(filer_window->view));
2160 void filer_cancel_thumbnails(FilerWindow *filer_window)
2162 gtk_widget_hide(filer_window->thumb_bar);
2164 destroy_glist(&filer_window->thumb_queue);
2165 filer_window->max_thumbs = 0;
2168 /* Generate the next thumb for this window. The window object is
2169 * unref'd when there is nothing more to do.
2170 * If the window no longer has a filer window, nothing is done.
2172 static gboolean filer_next_thumb_real(GObject *window)
2174 FilerWindow *filer_window;
2175 gchar *path;
2176 int done, total;
2178 filer_window = g_object_get_data(window, "filer_window");
2180 if (!filer_window)
2182 g_object_unref(window);
2183 return FALSE;
2186 if (!filer_window->thumb_queue)
2188 filer_cancel_thumbnails(filer_window);
2189 g_object_unref(window);
2190 return FALSE;
2193 total = filer_window->max_thumbs;
2194 done = total - g_list_length(filer_window->thumb_queue);
2196 path = (gchar *) filer_window->thumb_queue->data;
2198 pixmap_background_thumb(path, (GFunc) filer_next_thumb, window);
2200 filer_window->thumb_queue = g_list_remove(filer_window->thumb_queue,
2201 path);
2202 g_free(path);
2204 gtk_progress_bar_set_fraction(
2205 GTK_PROGRESS_BAR(filer_window->thumb_progress),
2206 done / (float) total);
2208 return FALSE;
2211 /* path is the thumb just loaded, if any.
2212 * window is unref'd (eventually).
2214 static void filer_next_thumb(GObject *window, const gchar *path)
2216 if (path)
2217 dir_force_update_path(path);
2219 g_idle_add((GSourceFunc) filer_next_thumb_real, window);
2222 static void start_thumb_scanning(FilerWindow *filer_window)
2224 if (GTK_WIDGET_VISIBLE(filer_window->thumb_bar))
2225 return; /* Already scanning */
2227 gtk_widget_show_all(filer_window->thumb_bar);
2229 g_object_ref(G_OBJECT(filer_window->window));
2230 filer_next_thumb(G_OBJECT(filer_window->window), NULL);
2233 /* Set this image to be loaded some time in the future */
2234 void filer_create_thumb(FilerWindow *filer_window, const gchar *path)
2236 if (g_list_find_custom(filer_window->thumb_queue, path,
2237 (GCompareFunc) strcmp))
2238 return;
2240 if (!filer_window->thumb_queue)
2241 filer_window->max_thumbs=0;
2242 filer_window->max_thumbs++;
2244 filer_window->thumb_queue = g_list_append(filer_window->thumb_queue,
2245 g_strdup(path));
2247 if (filer_window->scanning)
2248 return; /* Will start when scan ends */
2250 start_thumb_scanning(filer_window);
2253 /* If thumbnail display is on, look through all the items in this directory
2254 * and start creating or updating the thumbnails as needed.
2256 void filer_create_thumbs(FilerWindow *filer_window)
2258 DirItem *item;
2259 ViewIter iter;
2261 if (!filer_window->show_thumbs)
2262 return;
2264 view_get_iter(filer_window->view, &iter, 0);
2266 while ((item = iter.next(&iter)))
2268 MaskedPixmap *pixmap;
2269 const guchar *path;
2270 gboolean found;
2272 if (item->base_type != TYPE_FILE)
2273 continue;
2275 /*if (strcmp(item->mime_type->media_type, "image") != 0)
2276 continue;*/
2278 path = make_path(filer_window->real_path, item->leafname);
2280 pixmap = g_fscache_lookup_full(pixmap_cache, path,
2281 FSCACHE_LOOKUP_ONLY_NEW, &found);
2282 if (pixmap)
2283 g_object_unref(pixmap);
2285 /* If we didn't get an image, it could be because:
2287 * - We're loading the image now. found is TRUE,
2288 * and we'll update the item later.
2289 * - We tried to load the image and failed. found
2290 * is TRUE.
2291 * - We haven't tried loading the image. found is
2292 * FALSE, and we start creating the thumb here.
2294 if (!found)
2295 filer_create_thumb(filer_window, path);
2299 static void filer_options_changed(void)
2301 if (o_short_flag_names.has_changed)
2303 GList *next;
2305 for (next = all_filer_windows; next; next = next->next)
2307 FilerWindow *filer_window = (FilerWindow *) next->data;
2309 filer_set_title(filer_window);
2314 /* Append interesting information to this GString */
2315 void filer_add_tip_details(FilerWindow *filer_window,
2316 GString *tip, DirItem *item)
2318 const guchar *fullpath = NULL;
2320 fullpath = make_path(filer_window->real_path, item->leafname);
2322 if (item->flags & ITEM_FLAG_SYMLINK)
2324 char *target;
2326 target = readlink_dup(fullpath);
2327 if (target)
2329 ensure_utf8(&target);
2331 g_string_append(tip, _("Symbolic link to "));
2332 g_string_append(tip, target);
2333 g_string_append_c(tip, '\n');
2334 g_free(target);
2338 if (item->flags & ITEM_FLAG_APPDIR)
2340 XMLwrapper *info;
2341 xmlNode *node;
2343 info = appinfo_get(fullpath, item);
2344 if (info && ((node = xml_get_section(info, NULL, "Summary"))))
2346 guchar *str;
2347 str = xmlNodeListGetString(node->doc,
2348 node->xmlChildrenNode, 1);
2349 if (str)
2351 g_string_append(tip, str);
2352 g_string_append_c(tip, '\n');
2353 g_free(str);
2356 if (info)
2357 g_object_unref(info);
2359 else if (item->mime_type == application_x_desktop)
2361 char *summary;
2362 summary = tip_from_desktop_file(fullpath);
2363 if (summary)
2365 g_string_append(tip, summary);
2366 g_string_append_c(tip, '\n');
2367 g_free(summary);
2371 if (!g_utf8_validate(item->leafname, -1, NULL))
2372 g_string_append(tip,
2373 _("This filename is not valid UTF-8. "
2374 "You should rename it.\n"));
2377 /* Return the selection as a text/uri-list.
2378 * g_free() the result.
2380 static guchar *filer_create_uri_list(FilerWindow *filer_window)
2382 GString *string;
2383 GString *leader;
2384 ViewIter iter;
2385 DirItem *item;
2386 guchar *retval;
2388 g_return_val_if_fail(filer_window != NULL, NULL);
2390 string = g_string_new(NULL);
2392 leader = g_string_new(filer_window->sym_path);
2393 if (leader->str[leader->len - 1] != '/')
2394 g_string_append_c(leader, '/');
2396 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
2397 while ((item = iter.next(&iter)))
2399 EscapedPath *uri;
2400 char *path;
2402 path = g_strconcat(leader->str, item->leafname, NULL);
2403 uri = encode_path_as_uri(path);
2404 g_string_append(string, (char *) uri);
2405 g_string_append(string, "\r\n");
2406 g_free(path);
2407 g_free(uri);
2410 g_string_free(leader, TRUE);
2411 retval = string->str;
2412 g_string_free(string, FALSE);
2414 return retval;
2417 void filer_perform_action(FilerWindow *filer_window, GdkEventButton *event)
2419 BindAction action;
2420 ViewIface *view = filer_window->view;
2421 DirItem *item = NULL;
2422 gboolean press = event->type == GDK_BUTTON_PRESS;
2423 ViewIter iter;
2424 OpenFlags flags = 0;
2426 if (event->button > 3)
2427 return;
2429 view_get_iter_at_point(view, &iter, event->window, event->x, event->y);
2430 item = iter.peek(&iter);
2432 if (item && view_cursor_visible(view))
2433 view_cursor_to_iter(view, &iter);
2435 if (item && event->button == 1 &&
2436 view_get_selected(view, &iter) &&
2437 filer_window->selection_state == GTK_STATE_INSENSITIVE)
2439 /* Possibly a really slow DnD operation? */
2440 filer_window->temp_item_selected = FALSE;
2442 filer_selection_changed(filer_window, event->time);
2443 return;
2446 if (filer_window->target_cb)
2448 dnd_motion_ungrab();
2449 if (item && press && event->button == 1)
2450 filer_window->target_cb(filer_window, &iter,
2451 filer_window->target_data);
2453 filer_target_mode(filer_window, NULL, NULL, NULL);
2455 return;
2458 if (!o_single_click.int_value)
2460 /* Make sure both parts of a double-click fall on
2461 * the same file.
2463 static guchar *first_click = NULL;
2464 static guchar *second_click = NULL;
2466 if (event->type == GDK_BUTTON_PRESS)
2468 g_free(first_click);
2469 first_click = second_click;
2471 if (item)
2472 second_click = g_strdup(item->leafname);
2473 else
2474 second_click = NULL;
2477 if (event->type == GDK_2BUTTON_PRESS)
2479 if (first_click && second_click &&
2480 strcmp(first_click, second_click) != 0)
2481 return;
2482 if ((first_click || second_click) &&
2483 !(first_click && second_click))
2484 return;
2488 action = bind_lookup_bev(
2489 item ? BIND_DIRECTORY_ICON : BIND_DIRECTORY,
2490 event);
2492 switch (action)
2494 case ACT_CLEAR_SELECTION:
2495 view_clear_selection(view);
2496 break;
2497 case ACT_TOGGLE_SELECTED:
2498 view_set_selected(view, &iter,
2499 !view_get_selected(view, &iter));
2500 break;
2501 case ACT_SELECT_EXCL:
2502 view_select_only(view, &iter);
2503 break;
2504 case ACT_EDIT_ITEM:
2505 flags |= OPEN_SHIFT;
2506 /* (no break) */
2507 case ACT_OPEN_ITEM:
2508 if (event->button != 1 || event->state & GDK_MOD1_MASK)
2509 flags |= OPEN_CLOSE_WINDOW;
2510 else
2511 flags |= OPEN_SAME_WINDOW;
2512 if (o_new_button_1.int_value)
2513 flags ^= OPEN_SAME_WINDOW;
2514 if (event->type == GDK_2BUTTON_PRESS)
2515 view_set_selected(view, &iter, FALSE);
2516 dnd_motion_ungrab();
2518 filer_openitem(filer_window, &iter, flags);
2519 break;
2520 case ACT_POPUP_MENU:
2521 dnd_motion_ungrab();
2522 tooltip_show(NULL);
2523 show_filer_menu(filer_window,
2524 (GdkEvent *) event, &iter);
2525 break;
2526 case ACT_PRIME_AND_SELECT:
2527 if (item && !view_get_selected(view, &iter))
2528 view_select_only(view, &iter);
2529 dnd_motion_start(MOTION_READY_FOR_DND);
2530 break;
2531 case ACT_PRIME_AND_TOGGLE:
2532 view_set_selected(view, &iter,
2533 !view_get_selected(view, &iter));
2534 dnd_motion_start(MOTION_READY_FOR_DND);
2535 break;
2536 case ACT_PRIME_FOR_DND:
2537 dnd_motion_start(MOTION_READY_FOR_DND);
2538 break;
2539 case ACT_IGNORE:
2540 if (press && event->button < 4)
2542 if (item)
2543 view_wink_item(view, &iter);
2544 dnd_motion_start(MOTION_NONE);
2546 break;
2547 case ACT_LASSO_CLEAR:
2548 view_clear_selection(view);
2549 /* (no break) */
2550 case ACT_LASSO_MODIFY:
2551 view_start_lasso_box(view, event);
2552 break;
2553 case ACT_RESIZE:
2554 view_autosize(filer_window->view);
2555 break;
2556 default:
2557 g_warning("Unsupported action : %d\n", action);
2558 break;
2562 /* It's time to make the tooltip appear. If we're not over the item any
2563 * more, or the item doesn't need a tooltip, do nothing.
2565 static gboolean tooltip_activate(GtkWidget *window)
2567 FilerWindow *filer_window;
2568 ViewIface *view;
2569 ViewIter iter;
2570 gint x, y;
2571 DirItem *item = NULL;
2572 GString *tip = NULL;
2574 g_return_val_if_fail(tip_item != NULL, 0);
2576 filer_window = g_object_get_data(G_OBJECT(window), "filer_window");
2578 if (!motion_window || !filer_window)
2579 return FALSE; /* Window has been destroyed */
2581 view = filer_window->view;
2583 tooltip_show(NULL);
2585 gdk_window_get_pointer(motion_window, &x, &y, NULL);
2586 view_get_iter_at_point(view, &iter, motion_window, x, y);
2588 item = iter.peek(&iter);
2589 if (item != tip_item)
2590 return FALSE; /* Not still under the pointer */
2592 /* OK, the filer window still exists and the pointer is still
2593 * over the same item. Do we need to show a tip?
2596 tip = g_string_new(NULL);
2598 view_extend_tip(filer_window->view, &iter, tip);
2600 filer_add_tip_details(filer_window, tip, tip_item);
2602 if (tip->len > 1)
2604 g_string_truncate(tip, tip->len - 1);
2606 tooltip_show(tip->str);
2609 g_string_free(tip, TRUE);
2611 return FALSE;
2614 /* Motion detected on the View widget */
2615 gint filer_motion_notify(FilerWindow *filer_window, GdkEventMotion *event)
2617 ViewIface *view = filer_window->view;
2618 ViewIter iter;
2619 DirItem *item;
2621 view_get_iter_at_point(view, &iter, event->window, event->x, event->y);
2622 item = iter.peek(&iter);
2624 if (item)
2626 if (item != tip_item)
2628 tooltip_show(NULL);
2630 tip_item = item;
2631 motion_window = event->window;
2632 tooltip_prime((GtkFunction) tooltip_activate,
2633 G_OBJECT(filer_window->window));
2636 else
2638 tooltip_show(NULL);
2639 tip_item = NULL;
2642 if (motion_state != MOTION_READY_FOR_DND)
2643 return FALSE;
2645 if (!dnd_motion_moved(event))
2646 return FALSE;
2648 view_get_iter_at_point(view, &iter,
2649 event->window,
2650 event->x - (event->x_root - drag_start_x),
2651 event->y - (event->y_root - drag_start_y));
2652 item = iter.peek(&iter);
2653 if (!item)
2654 return FALSE;
2656 view_wink_item(view, NULL);
2658 if (!view_get_selected(view, &iter))
2660 if (event->state & GDK_BUTTON1_MASK)
2662 /* Select just this one */
2663 filer_window->temp_item_selected = TRUE;
2664 view_select_only(view, &iter);
2666 else
2668 if (view_count_selected(view) == 0)
2669 filer_window->temp_item_selected = TRUE;
2670 view_set_selected(view, &iter, TRUE);
2674 g_return_val_if_fail(view_count_selected(view) > 0, TRUE);
2676 if (view_count_selected(view) == 1)
2678 if (item->base_type == TYPE_UNKNOWN)
2679 item = dir_update_item(filer_window->directory,
2680 item->leafname);
2682 if (!item)
2684 report_error(_("Item no longer exists!"));
2685 return FALSE;
2688 drag_one_item(GTK_WIDGET(view), event,
2689 make_path(filer_window->sym_path, item->leafname),
2690 item, di_image(item));
2691 #if 0
2692 /* XXX: Use thumbnail */
2693 item, view ? view->image : NULL);
2694 #endif
2696 else
2698 guchar *uris;
2700 uris = filer_create_uri_list(filer_window);
2701 drag_selection(GTK_WIDGET(view), event, uris);
2702 g_free(uris);
2705 return FALSE;
2708 static void drag_end(GtkWidget *widget, GdkDragContext *context,
2709 FilerWindow *filer_window)
2711 filer_set_autoscroll(filer_window, FALSE);
2713 if (filer_window->temp_item_selected)
2715 view_clear_selection(filer_window->view);
2716 filer_window->temp_item_selected = FALSE;
2720 /* Remove highlights */
2721 static void drag_leave(GtkWidget *widget,
2722 GdkDragContext *context,
2723 guint32 time,
2724 FilerWindow *filer_window)
2726 dnd_spring_abort();
2729 /* Called during the drag when the mouse is in a widget registered
2730 * as a drop target. Returns TRUE if we can accept the drop.
2732 static gboolean drag_motion(GtkWidget *widget,
2733 GdkDragContext *context,
2734 gint x,
2735 gint y,
2736 guint time,
2737 FilerWindow *filer_window)
2739 DirItem *item;
2740 ViewIface *view = filer_window->view;
2741 ViewIter iter;
2742 GdkDragAction action = context->suggested_action;
2743 const guchar *new_path = NULL;
2744 const char *type = NULL;
2745 gboolean retval = FALSE;
2746 gboolean same_window;
2748 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value)
2750 guint state;
2751 gdk_window_get_pointer(NULL, NULL, NULL, &state);
2752 if (state & GDK_BUTTON1_MASK)
2753 action = GDK_ACTION_ASK;
2756 same_window = gtk_drag_get_source_widget(context) == widget;
2758 filer_set_autoscroll(filer_window, TRUE);
2760 if (filer_window->view_type == VIEW_TYPE_DETAILS)
2762 GdkWindow *bin;
2763 int bin_y;
2764 /* Correct for position of bin window */
2765 bin = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(view));
2766 gdk_window_get_position(bin, NULL, &bin_y);
2767 y -= bin_y;
2770 if (o_dnd_drag_to_icons.int_value)
2772 view_get_iter_at_point(view, &iter, widget->window, x, y);
2773 item = iter.peek(&iter);
2775 else
2776 item = NULL;
2778 if (item && same_window && view_get_selected(view, &iter))
2779 type = NULL;
2780 else
2781 type = dnd_motion_item(context, &item);
2783 if (!type)
2784 item = NULL;
2786 /* Don't allow drops to non-writeable directories. BUT, still
2787 * allow drops on non-writeable SUBdirectories so that we can
2788 * do the spring-open thing.
2790 if (item && type == drop_dest_dir &&
2791 !(item->flags & ITEM_FLAG_APPDIR))
2793 dnd_spring_load(context, filer_window);
2795 else
2796 dnd_spring_abort();
2798 if (item)
2799 view_cursor_to_iter(view, &iter);
2800 else
2802 view_cursor_to_iter(view, NULL);
2804 /* Disallow background drops within a single window */
2805 if (type && same_window)
2806 type = NULL;
2809 if (type)
2811 if (item)
2812 new_path = make_path(filer_window->sym_path,
2813 item->leafname);
2814 else
2815 new_path = filer_window->sym_path;
2818 /* Don't ask about dragging to an application! */
2819 if (type == drop_dest_prog && action == GDK_ACTION_ASK)
2820 action = GDK_ACTION_COPY;
2822 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
2823 if (type)
2825 gdk_drag_status(context, action, time);
2826 g_dataset_set_data_full(context, "drop_dest_path",
2827 g_strdup(new_path), g_free);
2828 retval = TRUE;
2831 return retval;
2834 static gboolean as_timeout(FilerWindow *filer_window)
2836 gboolean retval;
2838 retval = view_auto_scroll_callback(filer_window->view);
2840 if (!retval)
2841 filer_window->auto_scroll = -1;
2843 return retval;
2846 /* When autoscroll is on, a timer keeps track of the pointer position.
2847 * While it's near the top or bottom of the window, the window scrolls.
2849 * If the mouse buttons are released, the pointer leaves the window, or
2850 * a drag-and-drop operation finishes, auto_scroll is turned off.
2852 void filer_set_autoscroll(FilerWindow *filer_window, gboolean auto_scroll)
2854 g_return_if_fail(filer_window != NULL);
2856 if (auto_scroll)
2858 if (filer_window->auto_scroll != -1)
2859 return; /* Already on! */
2861 filer_window->auto_scroll = g_timeout_add(50,
2862 (GSourceFunc) as_timeout,
2863 filer_window);
2865 else
2867 if (filer_window->auto_scroll == -1)
2868 return; /* Already off! */
2870 g_source_remove(filer_window->auto_scroll);
2871 filer_window->auto_scroll = -1;
2875 #define ZERO_MNT "/uri/0install"
2877 static void refresh_done(FilerWindow *filer_window)
2879 if (filer_exists(filer_window))
2880 filer_update_dir(filer_window, TRUE);
2883 void filer_refresh(FilerWindow *filer_window)
2885 if (!strncmp(ZERO_MNT "/", filer_window->real_path, sizeof(ZERO_MNT)))
2887 /* Try to run 0refresh */
2888 gint pid;
2889 gchar *argv[] = {"0refresh", NULL, NULL};
2890 const char *host = filer_window->real_path + sizeof(ZERO_MNT);
2891 const char *slash;
2893 slash = strchr(host, '/');
2894 if (slash)
2895 argv[1] = g_strndup(host, slash - host);
2896 else
2897 argv[1] = g_strdup(host);
2898 pid = rox_spawn(filer_window->real_path, (const char **) argv);
2899 g_free(argv[1]);
2900 if (pid)
2901 on_child_death(pid, (CallbackFn) refresh_done,
2902 filer_window);
2905 full_refresh();
2908 static inline gboolean is_hidden(const char *dir, DirItem *item)
2910 /* If the leaf name starts with '.' then the item is hidden */
2911 if(item->leafname[0]=='.')
2912 return TRUE;
2914 /*** Test disabled for now. The flags aren't set on the first pass...
2916 #if 0
2917 /* Most files will not have extended attributes, so this should
2918 * be quick. */
2919 if(!o_xattr_ignore.int_value && (item->flags & ITEM_FLAG_HAS_XATTR)) {
2920 gchar *path, *val;
2921 int len;
2922 gboolean hidden=FALSE;
2924 path=g_build_filename(dir, item->leafname, NULL);
2925 val=xattr_get(path, XATTR_HIDDEN, &len);
2926 if(val) {
2927 hidden=atoi(val) || (strcmp(val, "true")==0);
2928 g_free(val);
2930 g_free(path);
2932 if(hidden)
2933 return TRUE;
2935 #endif
2937 /* Otherwise not hidden */
2938 return FALSE;
2941 gboolean filer_match_filter(FilerWindow *filer_window, DirItem *item)
2943 g_return_val_if_fail(item != NULL, FALSE);
2945 if(is_hidden(filer_window->real_path, item) &&
2946 (!filer_window->temp_show_hidden && !filer_window->show_hidden))
2947 return FALSE;
2949 switch(filer_window->filter) {
2950 case FILER_SHOW_GLOB:
2951 return fnmatch(filer_window->filter_string,
2952 item->leafname, 0)==0 ||
2953 (item->base_type==TYPE_DIRECTORY &&
2954 !filer_window->filter_directories);
2956 case FILER_SHOW_ALL:
2957 default:
2958 break;
2960 return TRUE;
2963 /* Provided to hide the implementation */
2964 void filer_set_hidden(FilerWindow *filer_window, gboolean hidden)
2966 filer_window->show_hidden=hidden;
2969 /* Provided to hide the implementation */
2970 void filer_set_filter_directories(FilerWindow *filer_window, gboolean filter_directories)
2972 filer_window->filter_directories=filter_directories;
2975 /* Set the filter type. Returns TRUE if the type has changed
2976 * (must call filer_detach_rescan).
2978 gboolean filer_set_filter(FilerWindow *filer_window, FilterType type,
2979 const gchar *filter_string)
2981 /* Is this new filter the same as the old one? */
2982 if (filer_window->filter == type)
2984 switch(filer_window->filter)
2986 case FILER_SHOW_ALL:
2987 return FALSE;
2988 case FILER_SHOW_GLOB:
2989 if (strcmp(filer_window->filter_string,
2990 filter_string) == 0)
2991 return FALSE;
2992 break;
2996 /* Clean up old filter */
2997 if (filer_window->filter_string)
2999 g_free(filer_window->filter_string);
3000 filer_window->filter_string = NULL;
3002 /* Also clean up compiled regexp when implemented */
3004 filer_window->filter = type;
3006 switch(type)
3008 case FILER_SHOW_ALL:
3009 /* No extra work */
3010 break;
3012 case FILER_SHOW_GLOB:
3013 filer_window->filter_string = g_strdup(filter_string);
3014 break;
3016 default:
3017 /* oops */
3018 filer_window->filter = FILER_SHOW_ALL;
3019 g_warning("Impossible: filter type %d", type);
3020 break;
3023 return TRUE;
3026 /* Setting stuff */
3027 static Settings *settings_new(const char *path)
3029 Settings *set;
3031 set=g_new(Settings, 1);
3032 memset(set, 0, sizeof(Settings));
3033 if(path)
3034 set->path=g_strdup(path);
3036 return set;
3039 static void settings_free(Settings *set)
3041 g_free(set->path);
3042 if(set->filter)
3043 g_free(set->filter);
3044 g_free(set);
3047 static void store_settings(Settings *set)
3049 Settings *old;
3051 old=g_hash_table_lookup(settings_table, set->path);
3052 if(old)
3054 g_hash_table_remove(settings_table, set->path);
3055 settings_free(old);
3058 g_hash_table_insert(settings_table, set->path, set);
3061 /* TODO: use symbolic names in the XML file where possible */
3062 static void load_from_node(Settings *set, xmlDocPtr doc, xmlNodePtr node)
3064 xmlChar *str=NULL;
3066 str=xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
3068 if(strcmp(node->name, "X") == 0) {
3069 set->x=atoi(str);
3070 set->flags|=SET_POSITION;
3071 } else if(strcmp(node->name, "Y") == 0) {
3072 set->y=atoi(str);
3073 set->flags|=SET_POSITION;
3074 } else if(strcmp(node->name, "Width") == 0) {
3075 set->width=atoi(str);
3076 set->flags|=SET_SIZE;
3077 } else if(strcmp(node->name, "Height") == 0) {
3078 set->height=atoi(str);
3079 set->flags|=SET_SIZE;
3080 } else if(strcmp(node->name, "ShowHidden") == 0) {
3081 set->show_hidden=atoi(str);
3082 set->flags|=SET_HIDDEN;
3083 } else if(strcmp(node->name, "ViewType") == 0) {
3084 set->view_type=atoi(str);
3085 set->flags|=SET_DETAILS;
3086 } else if(strcmp(node->name, "DetailsType") == 0) {
3087 set->details_type=atoi(str);
3088 set->flags|=SET_DETAILS;
3089 } else if(strcmp(node->name, "SortType") == 0) {
3090 set->sort_type=atoi(str);
3091 set->flags|=SET_SORT;
3092 } else if(strcmp(node->name, "SortOrder") == 0) {
3093 set->sort_order=atoi(str);
3094 set->flags|=SET_SORT;
3095 } else if(strcmp(node->name, "DisplayStyle") == 0) {
3096 set->display_style=atoi(str);
3097 set->flags|=SET_STYLE;
3098 } else if(strcmp(node->name, "ShowThumbs") == 0) {
3099 set->show_thumbs=atoi(str);
3100 set->flags|=SET_THUMBS;
3101 } else if(strcmp(node->name, "FilterType") == 0) {
3102 set->filter_type=atoi(str);
3103 set->flags|=SET_FILTER;
3104 } else if(strcmp(node->name, "Filter") == 0) {
3105 set->filter=g_strdup(str);
3106 set->flags|=SET_FILTER;
3107 } else if(strcmp(node->name, "FilterDirectories") == 0) {
3108 set->filter_directories=atoi(str);
3109 set->flags|=SET_FILTER;
3112 if(str)
3113 xmlFree(str);
3116 static void load_settings(void)
3118 gchar *path;
3119 XMLwrapper *settings_doc=NULL;
3121 path=choices_find_xdg_path_load("Settings.xml", PROJECT, SITE);
3122 if(path) {
3123 settings_doc=xml_new(path);
3124 g_free(path);
3127 if(!settings_table)
3128 settings_table=g_hash_table_new(g_str_hash, g_str_equal);
3130 if(settings_doc) {
3131 xmlNodePtr node, subnode;
3133 node = xmlDocGetRootElement(settings_doc->doc);
3135 for (node = node->xmlChildrenNode; node; node = node->next)
3137 Settings *set;
3138 xmlChar *path;
3140 if (node->type != XML_ELEMENT_NODE)
3141 continue;
3142 if (strcmp(node->name, "FilerWindow") != 0)
3143 continue;
3145 path=xmlGetProp(node, "path");
3146 set=settings_new(path);
3147 xmlFree(path);
3149 for (subnode=node->xmlChildrenNode; subnode;
3150 subnode=subnode->next) {
3152 if (subnode->type != XML_ELEMENT_NODE)
3153 continue;
3154 load_from_node(set, settings_doc->doc,
3155 subnode);
3158 store_settings(set);
3160 g_object_unref(settings_doc);
3164 static void add_nodes(gpointer key, gpointer value, gpointer data)
3166 xmlNodePtr node=(xmlNodePtr) data;
3167 xmlNodePtr sub;
3168 Settings *set=(Settings *) value;
3169 char *tmp;
3171 sub=xmlNewChild(node, NULL, "FilerWindow", NULL);
3173 xmlSetProp(sub, "path", set->path);
3175 if(set->flags & SET_POSITION) {
3176 tmp=g_strdup_printf("%d", set->x);
3177 xmlNewChild(sub, NULL, "X", tmp);
3178 g_free(tmp);
3179 tmp=g_strdup_printf("%d", set->y);
3180 xmlNewChild(sub, NULL, "Y", tmp);
3181 g_free(tmp);
3183 if(set->flags & SET_SIZE) {
3184 tmp=g_strdup_printf("%d", set->width);
3185 xmlNewChild(sub, NULL, "Width", tmp);
3186 g_free(tmp);
3187 tmp=g_strdup_printf("%d", set->height);
3188 xmlNewChild(sub, NULL, "Height", tmp);
3189 g_free(tmp);
3191 if(set->flags & SET_HIDDEN) {
3192 tmp=g_strdup_printf("%d", set->show_hidden);
3193 xmlNewChild(sub, NULL, "ShowHidden", tmp);
3194 g_free(tmp);
3196 if(set->flags & SET_STYLE) {
3197 tmp=g_strdup_printf("%d", set->display_style);
3198 xmlNewChild(sub, NULL, "DisplayStyle", tmp);
3199 g_free(tmp);
3201 if(set->flags & SET_SORT) {
3202 tmp=g_strdup_printf("%d", set->sort_type);
3203 xmlNewChild(sub, NULL, "SortType", tmp);
3204 g_free(tmp);
3205 tmp=g_strdup_printf("%d", set->sort_order);
3206 xmlNewChild(sub, NULL, "SortOrder", tmp);
3207 g_free(tmp);
3209 if(set->flags & SET_DETAILS) {
3210 tmp=g_strdup_printf("%d", set->view_type);
3211 xmlNewChild(sub, NULL, "ViewType", tmp);
3212 g_free(tmp);
3213 tmp=g_strdup_printf("%d", set->details_type);
3214 xmlNewChild(sub, NULL, "DetailsType", tmp);
3215 g_free(tmp);
3217 if(set->flags & SET_STYLE) {
3218 tmp=g_strdup_printf("%d", set->show_thumbs);
3219 xmlNewChild(sub, NULL, "ShowThumbs", tmp);
3220 g_free(tmp);
3222 if(set->flags & SET_FILTER) {
3223 tmp=g_strdup_printf("%d", set->filter_type);
3224 xmlNewChild(sub, NULL, "FilterType", tmp);
3225 g_free(tmp);
3226 if(set->filter && set->filter[0])
3227 xmlNewChild(sub, NULL, "Filter", set->filter);
3228 tmp=g_strdup_printf("%d", set->filter_directories);
3229 xmlNewChild(sub, NULL, "FilterDirectories", tmp);
3233 static void save_settings(void)
3235 gchar *path;
3237 path=choices_find_xdg_path_save("Settings.xml", PROJECT, SITE, TRUE);
3238 if(path) {
3239 xmlDocPtr doc = xmlNewDoc("1.0");
3240 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL,
3241 "Settings", NULL));
3243 g_hash_table_foreach(settings_table, add_nodes,
3244 xmlDocGetRootElement(doc));
3246 save_xml_file(doc, path);
3248 g_free(path);
3249 xmlFreeDoc(doc);
3254 static void check_settings(FilerWindow *filer_window)
3256 Settings *set;
3258 set=(Settings *) g_hash_table_lookup(settings_table,
3259 filer_window->sym_path);
3261 if(set) {
3262 if(set->flags & SET_POSITION)
3263 gtk_window_move(GTK_WINDOW(filer_window->window),
3264 set->x, set->y);
3265 if(set->flags & SET_SIZE)
3266 filer_window_set_size(filer_window, set->width,
3267 set->height);
3268 if(set->flags & SET_HIDDEN)
3269 filer_set_hidden(filer_window, set->show_hidden);
3271 if(set->flags & (SET_STYLE|SET_DETAILS)) {
3272 DisplayStyle style=filer_window->display_style;
3273 DetailsType details=filer_window->details_type;
3275 if(set->flags & SET_STYLE)
3276 style=set->display_style;
3278 if(set->flags & SET_DETAILS) {
3279 details=set->details_type;
3281 filer_set_view_type(filer_window,
3282 set->view_type);
3285 display_set_layout(filer_window, style,
3286 details, FALSE);
3289 if(set->flags & SET_SORT)
3290 display_set_sort_type(filer_window,
3291 set->sort_type,
3292 set->sort_order);
3294 if(set->flags & SET_THUMBS)
3295 display_set_thumbs(filer_window,
3296 set->show_thumbs);
3298 if(set->flags & SET_FILTER)
3300 display_set_filter(filer_window,
3301 set->filter_type,
3302 set->filter);
3303 display_set_filter_directories(filer_window,
3304 set->filter_directories);
3310 typedef struct settings_window {
3311 GtkWidget *window;
3313 GtkWidget *pos, *size, *hidden, *style, *sort, *details,
3314 *thumbs, *filter;
3316 Settings *set;
3317 } SettingsWindow;
3320 static void settings_response(GtkWidget *window, gint response,
3321 SettingsWindow *set_win)
3323 if(response==GTK_RESPONSE_OK) {
3324 gint flags=0;
3326 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->pos)))
3327 flags|=SET_POSITION;
3328 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->size)))
3329 flags|=SET_SIZE;
3330 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->hidden)))
3331 flags|=SET_HIDDEN;
3332 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->style)))
3333 flags|=SET_STYLE;
3334 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->sort)))
3335 flags|=SET_SORT;
3336 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->details)))
3337 flags|=SET_DETAILS;
3338 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->thumbs)))
3339 flags|=SET_THUMBS;
3340 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->filter)))
3341 flags|=SET_FILTER;
3343 set_win->set->flags=flags;
3344 store_settings(set_win->set);
3345 save_settings();
3348 gtk_widget_destroy(window);
3351 void filer_save_settings(FilerWindow *fwin)
3353 SettingsWindow *set_win;
3354 GtkWidget *vbox;
3355 GtkWidget *path;
3356 gint x, y;
3358 Settings *set=settings_new(fwin->sym_path);
3360 gtk_window_get_position(GTK_WINDOW(fwin->window),&x, &y);
3361 set->flags|=SET_POSITION;
3362 set->x=x;
3363 set->y=y;
3365 gtk_window_get_size(GTK_WINDOW(fwin->window),&x, &y);
3366 set->flags|=SET_SIZE;
3367 set->width=x;
3368 set->height=y;
3370 set->flags|=SET_HIDDEN;
3371 set->show_hidden=fwin->show_hidden;
3373 set->flags|=SET_STYLE;
3374 set->display_style=fwin->display_style;
3376 set->flags|=SET_SORT;
3377 set->sort_type=fwin->sort_type;
3378 set->sort_order=fwin->sort_order;
3380 set->flags|=SET_DETAILS;
3381 set->view_type=fwin->view_type;
3382 set->details_type=fwin->details_type;
3384 set->flags|=SET_THUMBS;
3385 set->show_thumbs=fwin->show_thumbs;
3387 set->flags|=SET_FILTER;
3388 set->filter_type=fwin->filter;
3389 if(fwin->filter_string)
3390 set->filter=g_strdup(fwin->filter_string);
3391 set->filter_directories=fwin->filter_directories;
3393 /* Store other parameters
3395 set_win=g_new(SettingsWindow, 1);
3397 set_win->window=gtk_dialog_new();
3398 number_of_windows++;
3400 gtk_dialog_add_button(GTK_DIALOG(set_win->window),
3401 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
3402 gtk_dialog_add_button(GTK_DIALOG(set_win->window),
3403 GTK_STOCK_OK, GTK_RESPONSE_OK);
3405 g_signal_connect(set_win->window, "destroy",
3406 G_CALLBACK(one_less_window), NULL);
3407 g_signal_connect_swapped(set_win->window, "destroy",
3408 G_CALLBACK(g_free), set_win);
3410 gtk_window_set_title(GTK_WINDOW(set_win->window),
3411 _("Select display properties to save"));
3413 vbox=GTK_DIALOG(set_win->window)->vbox;
3415 path=gtk_label_new(set->path);
3416 gtk_box_pack_start(GTK_BOX(vbox), path, FALSE, FALSE, 2);
3418 set_win->pos=gtk_check_button_new_with_label(_("Position"));
3419 gtk_box_pack_start(GTK_BOX(vbox), set_win->pos, FALSE, FALSE, 2);
3420 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->pos),
3421 set->flags & SET_POSITION);
3423 set_win->size=gtk_check_button_new_with_label(_("Size"));
3424 gtk_box_pack_start(GTK_BOX(vbox), set_win->size, FALSE, FALSE, 2);
3425 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->size),
3426 set->flags & SET_SIZE);
3428 set_win->hidden=gtk_check_button_new_with_label(_("Show hidden"));
3429 gtk_box_pack_start(GTK_BOX(vbox), set_win->hidden,
3430 FALSE, FALSE, 2);
3431 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->hidden),
3432 set->flags & SET_HIDDEN);
3434 set_win->style=gtk_check_button_new_with_label(_("Display style"));
3435 gtk_box_pack_start(GTK_BOX(vbox), set_win->style,
3436 FALSE, FALSE, 2);
3437 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->style),
3438 set->flags & SET_STYLE);
3440 set_win->sort=gtk_check_button_new_with_label(_("Sort type and order"));
3441 gtk_box_pack_start(GTK_BOX(vbox), set_win->sort, FALSE, FALSE, 2);
3442 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->sort),
3443 set->flags & SET_SORT);
3445 set_win->details=gtk_check_button_new_with_label(_("Details"));
3446 gtk_box_pack_start(GTK_BOX(vbox), set_win->details, FALSE, FALSE, 2);
3447 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->details),
3448 set->flags & SET_DETAILS);
3450 set_win->thumbs=gtk_check_button_new_with_label(_("Thumbnails"));
3451 gtk_box_pack_start(GTK_BOX(vbox), set_win->thumbs,
3452 FALSE, FALSE, 2);
3453 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->thumbs),
3454 set->flags & SET_THUMBS);
3456 set_win->filter=gtk_check_button_new_with_label(_("Filter"));
3457 gtk_box_pack_start(GTK_BOX(vbox), set_win->filter,
3458 FALSE, FALSE, 2);
3459 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->filter),
3460 set->flags & SET_FILTER);
3462 set_win->set=set;
3463 g_signal_connect(set_win->window, "response",
3464 G_CALLBACK(settings_response), set_win);
3466 gtk_widget_show_all(set_win->window);
3469 static char *tip_from_desktop_file(const char *full_path)
3471 GError *error = NULL;
3472 char *comment = NULL;
3474 comment = get_value_from_desktop_file(full_path,
3475 "Desktop Entry", "Comment", &error);
3476 if (error)
3478 delayed_error("Failed to parse .desktop file '%s':\n%s",
3479 full_path, error->message);
3480 goto err;
3483 err:
3484 if (error != NULL)
3485 g_error_free(error);
3487 return comment;