Scan directories using GIO.
[rox-filer.git] / ROX-Filer / src / filer.c
blobc65a9bd8e1e3093ede811c5e7ba8e80a4bb6b726
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."));
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, const gchar *wm_class)
1384 GFile *gfile;
1385 FilerWindow *retval;
1387 gfile = g_file_new_for_path(path);
1388 retval = filer_opendir_gfile(gfile, src_win, wm_class);
1389 g_object_unref(gfile);
1391 return retval;
1394 FilerWindow *filer_opendir_gfile(GFile *gfile, FilerWindow *src_win, const gchar *wm_class)
1396 FilerWindow *filer_window;
1397 char *real_path;
1398 DisplayStyle dstyle;
1399 DetailsType dtype;
1400 SortType s_type;
1401 GtkSortType s_order;
1402 Settings *dir_settings = NULL;
1403 gboolean force_resize = TRUE;
1404 char *path;
1406 path = g_file_get_path(gfile); /// XXX: free
1408 /* Get the real pathname of the directory and copy it */
1409 real_path = pathdup(path);
1411 if (o_unique_filer_windows.int_value && !spring_in_progress)
1413 FilerWindow *same_dir_window;
1415 same_dir_window = find_filer_window(path, NULL);
1417 if (same_dir_window)
1419 gtk_window_present(GTK_WINDOW(same_dir_window->window));
1420 return same_dir_window;
1424 filer_window = g_new(FilerWindow, 1);
1425 filer_window->message = NULL;
1426 filer_window->minibuffer = NULL;
1427 filer_window->minibuffer_label = NULL;
1428 filer_window->minibuffer_area = NULL;
1429 filer_window->temp_show_hidden = FALSE;
1430 filer_window->sym_path = g_strdup(path);
1431 filer_window->real_path = real_path;
1432 filer_window->scanning = FALSE;
1433 filer_window->had_cursor = FALSE;
1434 filer_window->auto_select = NULL;
1435 filer_window->toolbar_text = NULL;
1436 filer_window->target_cb = NULL;
1437 filer_window->mini_type = MINI_NONE;
1438 filer_window->selection_state = GTK_STATE_INSENSITIVE;
1439 filer_window->toolbar = NULL;
1440 filer_window->toplevel_vbox = NULL;
1441 filer_window->view_hbox = NULL;
1442 filer_window->view = NULL;
1443 filer_window->scrollbar = NULL;
1444 filer_window->auto_scroll = -1;
1445 filer_window->window_id = NULL;
1447 tidy_sympath(filer_window->sym_path);
1449 /* Finds the entry for this directory in the dir cache, creating
1450 * a new one if needed. This does not cause a scan to start,
1451 * so if a new entry is created then it will be empty.
1453 filer_window->directory = g_fscache_lookup(dir_cache, real_path);
1454 if (!filer_window->directory)
1456 delayed_error(_("Directory '%s' not found."), path);
1457 g_free(filer_window->real_path);
1458 g_free(filer_window->sym_path);
1459 g_free(filer_window);
1460 return NULL;
1463 filer_window->temp_item_selected = FALSE;
1464 filer_window->flags = (FilerFlags) 0;
1465 filer_window->details_type = DETAILS_TIMES;
1466 filer_window->display_style = UNKNOWN_STYLE;
1467 filer_window->display_style_wanted = UNKNOWN_STYLE;
1468 filer_window->thumb_queue = NULL;
1469 filer_window->max_thumbs = 0;
1470 filer_window->sort_type = -1;
1472 filer_window->filter = FILER_SHOW_ALL;
1473 filer_window->filter_string = NULL;
1474 filer_window->regexp = NULL;
1475 filer_window->filter_directories = FALSE;
1477 if (src_win && o_display_inherit_options.int_value)
1479 s_type = src_win->sort_type;
1480 s_order = src_win->sort_order;
1481 dstyle = src_win->display_style_wanted;
1482 dtype = src_win->details_type;
1483 filer_window->show_hidden = src_win->show_hidden;
1484 filer_window->show_thumbs = src_win->show_thumbs;
1485 filer_window->view_type = src_win->view_type;
1487 filer_window->filter_directories = src_win->filter_directories;
1488 filer_set_filter(filer_window, src_win->filter,
1489 src_win->filter_string);
1491 else
1493 s_type = o_display_sort_by.int_value;
1494 s_order = GTK_SORT_ASCENDING;
1495 dstyle = o_display_size.int_value;
1496 dtype = o_display_details.int_value;
1497 filer_window->show_hidden = o_display_show_hidden.int_value;
1498 filer_window->show_thumbs = o_display_show_thumbs.int_value;
1499 filer_window->view_type = o_filer_view_type.int_value;
1502 dir_settings = (Settings *) g_hash_table_lookup(settings_table,
1503 filer_window->sym_path);
1504 if (dir_settings)
1506 /* Override the current defaults with the per-directory
1507 * user settings.
1509 if (dir_settings->flags & SET_HIDDEN)
1510 filer_window->show_hidden = dir_settings->show_hidden;
1512 if (dir_settings->flags & SET_STYLE)
1513 dstyle = dir_settings->display_style;
1515 if (dir_settings->flags & SET_DETAILS)
1517 dtype = dir_settings->details_type;
1518 filer_window->view_type = dir_settings->view_type;
1521 if (dir_settings->flags & SET_SORT)
1523 s_type = dir_settings->sort_type;
1524 s_order = dir_settings->sort_order;
1527 if (dir_settings->flags & SET_THUMBS)
1528 filer_window->show_thumbs = dir_settings->show_thumbs;
1530 if (dir_settings->flags & SET_FILTER)
1532 filer_set_filter(filer_window,
1533 dir_settings->filter_type,
1534 dir_settings->filter);
1535 filer_set_filter_directories(filer_window,
1536 dir_settings->filter_directories);
1540 /* Add all the user-interface elements & realise */
1541 filer_add_widgets(filer_window, wm_class);
1542 if (src_win)
1543 gtk_window_set_position(GTK_WINDOW(filer_window->window),
1544 GTK_WIN_POS_MOUSE);
1546 if (dir_settings)
1548 if (dir_settings->flags & SET_POSITION)
1550 gtk_window_move(GTK_WINDOW(filer_window->window),
1551 dir_settings->x, dir_settings->y);
1553 if (dir_settings->flags & SET_SIZE)
1555 filer_window_set_size(filer_window,
1556 dir_settings->width,
1557 dir_settings->height);
1558 force_resize = o_filer_auto_resize.int_value != RESIZE_NEVER;
1562 /* Connect to all the signal handlers */
1563 filer_add_signals(filer_window);
1565 display_set_layout(filer_window, dstyle, dtype, force_resize);
1566 display_set_sort_type(filer_window, s_type, s_order);
1568 /* Open the window after a timeout, or when scanning stops.
1569 * Do this before attaching, because attach() might tell us to
1570 * stop scanning (if a scan isn't needed).
1572 filer_window->open_timeout = g_timeout_add(500,
1573 (GSourceFunc) open_filer_window,
1574 filer_window);
1576 /* The view is created empty and then attach() is called, which
1577 * links the filer window to the entry in the directory cache we
1578 * looked up / created above.
1580 * The attach() function will immediately callback to the filer window
1581 * to deliver a list of all known entries in the directory (so,
1582 * the number of items will be known after attach() returns).
1584 * If the directory was not in the cache (because it hadn't been
1585 * opened it before) then the types and icons for the entries are
1586 * not know, but the list of names is.
1589 attach(filer_window);
1591 number_of_windows++;
1592 all_filer_windows = g_list_prepend(all_filer_windows, filer_window);
1594 return filer_window;
1597 void filer_set_view_type(FilerWindow *filer_window, ViewType type)
1599 GtkWidget *view = NULL;
1600 Directory *dir = NULL;
1601 GHashTable *selected = NULL;
1603 g_return_if_fail(filer_window != NULL);
1605 motion_window = NULL;
1607 if (filer_window->view)
1609 /* Save the current selection */
1610 ViewIter iter;
1611 DirItem *item;
1613 selected = g_hash_table_new(g_str_hash, g_str_equal);
1614 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
1615 while ((item = iter.next(&iter)))
1616 g_hash_table_insert(selected, item->leafname, "yes");
1618 /* Destroy the old view */
1619 gtk_widget_destroy(GTK_WIDGET(filer_window->view));
1620 filer_window->view = NULL;
1622 dir = filer_window->directory;
1623 g_object_ref(dir);
1624 detach(filer_window);
1627 switch (type)
1629 case VIEW_TYPE_COLLECTION:
1630 view = view_collection_new(filer_window);
1631 break;
1632 case VIEW_TYPE_DETAILS:
1633 view = view_details_new(filer_window);
1634 break;
1637 g_return_if_fail(view != NULL);
1639 filer_window->view = VIEW(view);
1640 filer_window->view_type = type;
1642 gtk_box_pack_start(filer_window->view_hbox, view, TRUE, TRUE, 0);
1643 gtk_widget_show(view);
1645 /* Drag and drop events */
1646 make_drop_target(view, 0);
1647 g_signal_connect(view, "drag_motion",
1648 G_CALLBACK(drag_motion), filer_window);
1649 g_signal_connect(view, "drag_leave",
1650 G_CALLBACK(drag_leave), filer_window);
1651 g_signal_connect(view, "drag_end",
1652 G_CALLBACK(drag_end), filer_window);
1653 /* Dragging from us... */
1654 g_signal_connect(view, "drag_data_get",
1655 GTK_SIGNAL_FUNC(drag_data_get), NULL);
1657 if (dir)
1659 /* Only when changing type. Otherwise, will attach later. */
1660 filer_window->directory = dir;
1661 attach(filer_window);
1663 if (o_filer_auto_resize.int_value != RESIZE_NEVER)
1664 view_autosize(filer_window->view);
1667 if (selected)
1669 ViewIter iter;
1670 DirItem *item;
1672 view_get_iter(filer_window->view, &iter, 0);
1673 while ((item = iter.next(&iter)))
1675 if (g_hash_table_lookup(selected, item->leafname))
1676 view_set_selected(filer_window->view,
1677 &iter, TRUE);
1679 g_hash_table_destroy(selected);
1683 /* This adds all the widgets to a new filer window. It is in a separate
1684 * function because filer_opendir() was getting too long...
1686 static void filer_add_widgets(FilerWindow *filer_window, const gchar *wm_class)
1688 GtkWidget *hbox, *vbox;
1690 /* Create the top-level window widget */
1691 filer_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1692 filer_set_title(filer_window);
1693 gtk_widget_set_name(filer_window->window, "rox-filer");
1695 if (wm_class)
1696 gtk_window_set_wmclass(GTK_WINDOW(filer_window->window),
1697 wm_class, PROJECT);
1699 /* This property is cleared when the window is destroyed.
1700 * You can thus ref filer_window->window and use this to see
1701 * if the window no longer exists.
1703 g_object_set_data(G_OBJECT(filer_window->window),
1704 "filer_window", filer_window);
1706 /* Create this now to make the Adjustment before the View */
1707 filer_window->scrollbar = gtk_vscrollbar_new(NULL);
1709 vbox = gtk_vbox_new(FALSE, 0);
1710 gtk_container_add(GTK_CONTAINER(filer_window->window), vbox);
1712 filer_window->toplevel_vbox = GTK_BOX(vbox);
1714 /* If there's a message that should be displayed in each window (eg
1715 * 'Running as root'), add it here.
1717 if (show_user_message)
1719 filer_window->message = gtk_label_new(show_user_message);
1720 gtk_box_pack_start(GTK_BOX(vbox), filer_window->message,
1721 FALSE, TRUE, 0);
1722 gtk_widget_show(filer_window->message);
1725 hbox = gtk_hbox_new(FALSE, 0);
1726 filer_window->view_hbox = GTK_BOX(hbox);
1727 gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox);
1728 /* Add the main View widget */
1729 filer_set_view_type(filer_window, filer_window->view_type);
1730 /* Put the scrollbar next to the View */
1731 gtk_box_pack_end(GTK_BOX(hbox),
1732 filer_window->scrollbar, FALSE, TRUE, 0);
1733 gtk_widget_show(hbox);
1735 /* If we want a toolbar, create it now */
1736 toolbar_update_toolbar(filer_window);
1738 /* And the minibuffer (hidden to start with) */
1739 create_minibuffer(filer_window);
1740 gtk_box_pack_end(GTK_BOX(vbox), filer_window->minibuffer_area,
1741 FALSE, TRUE, 0);
1743 /* And the thumbnail progress bar (also hidden) */
1745 GtkWidget *cancel;
1747 filer_window->thumb_bar = gtk_hbox_new(FALSE, 2);
1748 gtk_box_pack_end(GTK_BOX(vbox), filer_window->thumb_bar,
1749 FALSE, TRUE, 0);
1751 filer_window->thumb_progress = gtk_progress_bar_new();
1753 gtk_box_pack_start(GTK_BOX(filer_window->thumb_bar),
1754 filer_window->thumb_progress, TRUE, TRUE, 0);
1756 cancel = gtk_button_new_with_label(_("Cancel"));
1757 GTK_WIDGET_UNSET_FLAGS(cancel, GTK_CAN_FOCUS);
1758 gtk_box_pack_start(GTK_BOX(filer_window->thumb_bar),
1759 cancel, FALSE, TRUE, 0);
1760 g_signal_connect_swapped(cancel, "clicked",
1761 G_CALLBACK(filer_cancel_thumbnails),
1762 filer_window);
1765 gtk_widget_show(vbox);
1766 gtk_widget_show(filer_window->scrollbar);
1768 gtk_widget_realize(filer_window->window);
1770 gdk_window_set_role(filer_window->window->window,
1771 filer_window->sym_path);
1773 filer_window_set_size(filer_window, 4, 4);
1776 static void filer_add_signals(FilerWindow *filer_window)
1778 GtkTargetEntry target_table[] =
1780 {"text/uri-list", 0, TARGET_URI_LIST},
1781 {"UTF8_STRING", 0, TARGET_STRING},
1782 {"STRING", 0, TARGET_STRING},
1783 {"COMPOUND_TEXT", 0, TARGET_STRING},/* XXX: Treats as STRING */
1786 /* Events on the top-level window */
1787 gtk_widget_add_events(filer_window->window, GDK_ENTER_NOTIFY);
1788 g_signal_connect(filer_window->window, "enter-notify-event",
1789 G_CALLBACK(pointer_in), filer_window);
1790 g_signal_connect(filer_window->window, "leave-notify-event",
1791 G_CALLBACK(pointer_out), filer_window);
1792 g_signal_connect(filer_window->window, "destroy",
1793 G_CALLBACK(filer_window_destroyed), filer_window);
1794 g_signal_connect(filer_window->window, "delete-event",
1795 G_CALLBACK(filer_window_delete), filer_window);
1797 g_signal_connect(filer_window->window, "selection_clear_event",
1798 G_CALLBACK(filer_lost_primary), filer_window);
1800 g_signal_connect(filer_window->window, "selection_get",
1801 G_CALLBACK(selection_get), filer_window);
1802 gtk_selection_add_targets(GTK_WIDGET(filer_window->window),
1803 GDK_SELECTION_PRIMARY,
1804 target_table,
1805 sizeof(target_table) / sizeof(*target_table));
1807 g_signal_connect(filer_window->window, "popup-menu",
1808 G_CALLBACK(popup_menu), filer_window);
1809 g_signal_connect(filer_window->window, "key_press_event",
1810 G_CALLBACK(filer_key_press_event), filer_window);
1812 gtk_window_add_accel_group(GTK_WINDOW(filer_window->window),
1813 filer_keys);
1816 static gint clear_scanning_display(FilerWindow *filer_window)
1818 if (filer_exists(filer_window))
1819 filer_set_title(filer_window);
1820 return FALSE;
1823 static void set_scanning_display(FilerWindow *filer_window, gboolean scanning)
1825 if (scanning == filer_window->scanning)
1826 return;
1827 filer_window->scanning = scanning;
1829 if (scanning)
1830 filer_set_title(filer_window);
1831 else
1832 g_timeout_add(300, (GSourceFunc) clear_scanning_display,
1833 filer_window);
1836 /* Note that filer_window may not exist after this call.
1837 * Returns TRUE iff the directory still exists.
1839 gboolean filer_update_dir(FilerWindow *filer_window, gboolean warning)
1841 gboolean still_exists;
1843 still_exists = may_rescan(filer_window, warning);
1845 if (still_exists)
1846 dir_update(filer_window->directory, filer_window->sym_path);
1848 return still_exists;
1851 void filer_update_all(void)
1853 GList *next = all_filer_windows;
1855 while (next)
1857 FilerWindow *filer_window = (FilerWindow *) next->data;
1859 /* Updating directory may remove it from list -- stop sending
1860 * patches to move this line!
1862 next = next->next;
1864 /* Don't trigger a refresh if we're already scanning.
1865 * Otherwise, two views of a single directory will trigger
1866 * two scans.
1868 if (filer_window->directory &&
1869 !filer_window->directory->scanning)
1870 filer_update_dir(filer_window, TRUE);
1874 /* Refresh the various caches even if we don't think we need to */
1875 void full_refresh(void)
1877 mount_update(TRUE);
1878 reread_mime_files(); /* Refreshes all windows */
1881 /* See whether a filer window with a given path already exists
1882 * and is different from diff.
1884 static FilerWindow *find_filer_window(const char *sym_path, FilerWindow *diff)
1886 GList *next;
1888 for (next = all_filer_windows; next; next = next->next)
1890 FilerWindow *filer_window = (FilerWindow *) next->data;
1892 if (filer_window != diff &&
1893 strcmp(sym_path, filer_window->sym_path) == 0)
1894 return filer_window;
1897 return NULL;
1900 /* This path has been mounted/umounted/deleted some files - update all dirs */
1901 void filer_check_mounted(const char *real_path)
1903 GList *next = all_filer_windows;
1904 gchar *parent;
1905 int len;
1906 gboolean resize = o_filer_auto_resize.int_value == RESIZE_ALWAYS;
1908 /* DOS disks, etc, often don't change the mtime of the root directory
1909 * on modification, so force a refresh now.
1911 g_fscache_update(dir_cache, real_path);
1913 len = strlen(real_path);
1915 while (next)
1917 FilerWindow *filer_window = (FilerWindow *) next->data;
1919 next = next->next;
1921 if (strncmp(real_path, filer_window->real_path, len) == 0)
1923 char s = filer_window->real_path[len];
1925 if (s == '/' || s == '\0')
1927 if (filer_update_dir(filer_window, FALSE) &&
1928 resize)
1929 view_autosize(filer_window->view);
1934 parent = g_path_get_dirname(real_path);
1935 refresh_dirs(parent);
1936 g_free(parent);
1938 icons_may_update(real_path);
1941 /* Close all windows displaying 'path' or subdirectories of 'path' */
1942 void filer_close_recursive(const char *path)
1944 GList *next = all_filer_windows;
1945 gchar *real;
1946 int len;
1948 real = pathdup(path);
1949 len = strlen(real);
1951 while (next)
1953 FilerWindow *filer_window = (FilerWindow *) next->data;
1955 next = next->next;
1957 if (strncmp(real, filer_window->real_path, len) == 0)
1959 char s = filer_window->real_path[len];
1961 if (len == 1 || s == '/' || s == '\0')
1962 gtk_widget_destroy(filer_window->window);
1967 /* Like minibuffer_show(), except that:
1968 * - It returns FALSE (to be used from an idle callback)
1969 * - It checks that the filer window still exists.
1971 static gboolean minibuffer_show_cb(FilerWindow *filer_window)
1973 if (filer_exists(filer_window))
1974 minibuffer_show(filer_window, MINI_PATH);
1975 return FALSE;
1978 /* TRUE iff filer_window points to an existing FilerWindow
1979 * structure.
1981 gboolean filer_exists(FilerWindow *filer_window)
1983 GList *next;
1985 for (next = all_filer_windows; next; next = next->next)
1987 FilerWindow *fw = (FilerWindow *) next->data;
1989 if (fw == filer_window)
1990 return TRUE;
1993 return FALSE;
1996 FilerWindow *filer_get_by_id(const char *id)
1998 return g_hash_table_lookup(window_with_id, id);
2001 void filer_set_id(FilerWindow *filer_window, const char *id)
2003 g_return_if_fail(filer_window != NULL);
2005 if (filer_window->window_id)
2007 g_hash_table_remove(window_with_id, filer_window->window_id);
2008 g_free(filer_window->window_id);
2009 filer_window->window_id = NULL;
2012 if (id)
2014 filer_window->window_id = g_strdup(id);
2015 g_hash_table_insert(window_with_id,
2016 filer_window->window_id,
2017 filer_window);
2021 /* Make sure the window title is up-to-date */
2022 void filer_set_title(FilerWindow *filer_window)
2024 gchar *title = NULL;
2025 guchar *flags = "";
2027 if (filer_window->scanning ||
2028 filer_window->filter != FILER_SHOW_ALL ||
2029 filer_window->show_hidden || filer_window->show_thumbs)
2031 if (o_short_flag_names.int_value)
2033 const gchar *hidden = "";
2035 switch(filer_window->filter) {
2036 case FILER_SHOW_ALL:
2037 hidden=filer_window->show_hidden? _("A") : "";
2038 break;
2039 case FILER_SHOW_GLOB: hidden = _("G"); break;
2040 default: break;
2043 flags = g_strconcat(" +",
2044 filer_window->scanning ? _("S") : "",
2045 hidden,
2046 filer_window->show_thumbs ? _("T") : "",
2047 NULL);
2049 else
2051 gchar *hidden = NULL;
2053 switch(filer_window->filter) {
2054 case FILER_SHOW_ALL:
2055 hidden = g_strdup(filer_window->show_hidden
2056 ? _("All, ") : "");
2057 break;
2058 case FILER_SHOW_GLOB:
2059 hidden = g_strdup_printf(_("Glob (%s), "),
2060 filer_window->filter_string);
2061 break;
2062 default:
2063 hidden =g_strdup("");
2064 break;
2066 flags = g_strconcat(" (",
2067 filer_window->scanning ? _("Scanning, ") : "",
2068 hidden,
2069 filer_window->show_thumbs ? _("Thumbs, ") : "",
2070 NULL);
2071 flags[strlen(flags) - 2] = ')';
2072 g_free(hidden);
2076 if (not_local)
2077 title = g_strconcat("//", our_host_name(),
2078 filer_window->sym_path, flags, NULL);
2080 if (!title && home_dir_len > 1 &&
2081 strncmp(filer_window->sym_path, home_dir, home_dir_len) == 0)
2083 guchar sep = filer_window->sym_path[home_dir_len];
2085 if (sep == '\0' || sep == '/')
2086 title = g_strconcat("~",
2087 filer_window->sym_path + home_dir_len,
2088 flags,
2089 NULL);
2092 if (!title)
2093 title = g_strconcat(filer_window->sym_path, flags, NULL);
2095 ensure_utf8(&title);
2097 if (filer_window->directory->error)
2099 gchar *old = title;
2100 title = g_strconcat(old, ": ", filer_window->directory->error,
2101 NULL);
2102 g_free(old);
2105 gtk_window_set_title(GTK_WINDOW(filer_window->window), title);
2107 g_free(title);
2109 if (flags[0] != '\0')
2110 g_free(flags);
2113 /* Reconnect to the same directory (used when the Show Hidden option is
2114 * toggled). This has the side-effect of updating the window title.
2116 void filer_detach_rescan(FilerWindow *filer_window)
2118 Directory *dir = filer_window->directory;
2120 g_object_ref(dir);
2121 detach(filer_window);
2122 filer_window->directory = dir;
2123 attach(filer_window);
2126 /* Puts the filer window into target mode. When an item is chosen,
2127 * fn(filer_window, iter, data) is called. 'reason' will be displayed
2128 * on the toolbar while target mode is active.
2130 * Use fn == NULL to cancel target mode.
2132 void filer_target_mode(FilerWindow *filer_window,
2133 TargetFunc fn,
2134 gpointer data,
2135 const char *reason)
2137 TargetFunc old_fn = filer_window->target_cb;
2139 if (fn != old_fn)
2140 gdk_window_set_cursor(
2141 GTK_WIDGET(filer_window->view)->window,
2142 fn ? crosshair : NULL);
2144 filer_window->target_cb = fn;
2145 filer_window->target_data = data;
2147 if (filer_window->toolbar_text == NULL)
2148 return;
2150 if (fn)
2151 gtk_label_set_text(
2152 GTK_LABEL(filer_window->toolbar_text), reason);
2153 else if (o_toolbar_info.int_value)
2155 if (old_fn)
2156 toolbar_update_info(filer_window);
2158 else
2159 gtk_label_set_text(GTK_LABEL(filer_window->toolbar_text), "");
2162 static void set_selection_state(FilerWindow *filer_window, gboolean normal)
2164 GtkStateType old_state = filer_window->selection_state;
2166 filer_window->selection_state = normal
2167 ? GTK_STATE_SELECTED : GTK_STATE_INSENSITIVE;
2169 if (old_state != filer_window->selection_state
2170 && view_count_selected(filer_window->view))
2171 gtk_widget_queue_draw(GTK_WIDGET(filer_window->view));
2174 void filer_cancel_thumbnails(FilerWindow *filer_window)
2176 gtk_widget_hide(filer_window->thumb_bar);
2178 destroy_glist(&filer_window->thumb_queue);
2179 filer_window->max_thumbs = 0;
2182 /* Generate the next thumb for this window. The window object is
2183 * unref'd when there is nothing more to do.
2184 * If the window no longer has a filer window, nothing is done.
2186 static gboolean filer_next_thumb_real(GObject *window)
2188 FilerWindow *filer_window;
2189 gchar *path;
2190 int done, total;
2192 filer_window = g_object_get_data(window, "filer_window");
2194 if (!filer_window)
2196 g_object_unref(window);
2197 return FALSE;
2200 if (!filer_window->thumb_queue)
2202 filer_cancel_thumbnails(filer_window);
2203 g_object_unref(window);
2204 return FALSE;
2207 total = filer_window->max_thumbs;
2208 done = total - g_list_length(filer_window->thumb_queue);
2210 path = (gchar *) filer_window->thumb_queue->data;
2212 pixmap_background_thumb(path, (GFunc) filer_next_thumb, window);
2214 filer_window->thumb_queue = g_list_remove(filer_window->thumb_queue,
2215 path);
2216 g_free(path);
2218 gtk_progress_bar_set_fraction(
2219 GTK_PROGRESS_BAR(filer_window->thumb_progress),
2220 done / (float) total);
2222 return FALSE;
2225 /* path is the thumb just loaded, if any.
2226 * window is unref'd (eventually).
2228 static void filer_next_thumb(GObject *window, const gchar *path)
2230 if (path)
2231 dir_force_update_path(path);
2233 g_idle_add((GSourceFunc) filer_next_thumb_real, window);
2236 static void start_thumb_scanning(FilerWindow *filer_window)
2238 if (GTK_WIDGET_VISIBLE(filer_window->thumb_bar))
2239 return; /* Already scanning */
2241 gtk_widget_show_all(filer_window->thumb_bar);
2243 g_object_ref(G_OBJECT(filer_window->window));
2244 filer_next_thumb(G_OBJECT(filer_window->window), NULL);
2247 /* Set this image to be loaded some time in the future */
2248 void filer_create_thumb(FilerWindow *filer_window, const gchar *path)
2250 if (g_list_find_custom(filer_window->thumb_queue, path,
2251 (GCompareFunc) strcmp))
2252 return;
2254 if (!filer_window->thumb_queue)
2255 filer_window->max_thumbs=0;
2256 filer_window->max_thumbs++;
2258 filer_window->thumb_queue = g_list_append(filer_window->thumb_queue,
2259 g_strdup(path));
2261 if (filer_window->scanning)
2262 return; /* Will start when scan ends */
2264 start_thumb_scanning(filer_window);
2267 /* If thumbnail display is on, look through all the items in this directory
2268 * and start creating or updating the thumbnails as needed.
2270 void filer_create_thumbs(FilerWindow *filer_window)
2272 DirItem *item;
2273 ViewIter iter;
2275 if (!filer_window->show_thumbs)
2276 return;
2278 view_get_iter(filer_window->view, &iter, 0);
2280 while ((item = iter.next(&iter)))
2282 MaskedPixmap *pixmap;
2283 const guchar *path;
2284 gboolean found;
2286 if (item->base_type != TYPE_FILE)
2287 continue;
2289 /*if (strcmp(item->mime_type->media_type, "image") != 0)
2290 continue;*/
2292 path = make_path(filer_window->real_path, item->leafname);
2294 pixmap = g_fscache_lookup_full(pixmap_cache, path,
2295 FSCACHE_LOOKUP_ONLY_NEW, &found);
2296 if (pixmap)
2297 g_object_unref(pixmap);
2299 /* If we didn't get an image, it could be because:
2301 * - We're loading the image now. found is TRUE,
2302 * and we'll update the item later.
2303 * - We tried to load the image and failed. found
2304 * is TRUE.
2305 * - We haven't tried loading the image. found is
2306 * FALSE, and we start creating the thumb here.
2308 if (!found)
2309 filer_create_thumb(filer_window, path);
2313 static void filer_options_changed(void)
2315 if (o_short_flag_names.has_changed)
2317 GList *next;
2319 for (next = all_filer_windows; next; next = next->next)
2321 FilerWindow *filer_window = (FilerWindow *) next->data;
2323 filer_set_title(filer_window);
2328 /* Append interesting information to this GString */
2329 void filer_add_tip_details(FilerWindow *filer_window,
2330 GString *tip, DirItem *item)
2332 const guchar *fullpath = NULL;
2334 fullpath = make_path(filer_window->real_path, item->leafname);
2336 if (item->flags & ITEM_FLAG_SYMLINK)
2338 char *target;
2340 target = readlink_dup(fullpath);
2341 if (target)
2343 ensure_utf8(&target);
2345 g_string_append(tip, _("Symbolic link to "));
2346 g_string_append(tip, target);
2347 g_string_append_c(tip, '\n');
2348 g_free(target);
2352 if (item->flags & ITEM_FLAG_APPDIR)
2354 XMLwrapper *info;
2355 xmlNode *node;
2357 info = appinfo_get(fullpath, item);
2358 if (info && ((node = xml_get_section(info, NULL, "Summary"))))
2360 guchar *str;
2361 str = xmlNodeListGetString(node->doc,
2362 node->xmlChildrenNode, 1);
2363 if (str)
2365 g_string_append(tip, str);
2366 g_string_append_c(tip, '\n');
2367 g_free(str);
2370 if (info)
2371 g_object_unref(info);
2373 else if (item->mime_type == application_x_desktop)
2375 char *summary;
2376 summary = tip_from_desktop_file(fullpath);
2377 if (summary)
2379 g_string_append(tip, summary);
2380 g_string_append_c(tip, '\n');
2381 g_free(summary);
2385 if (!g_utf8_validate(item->leafname, -1, NULL))
2386 g_string_append(tip,
2387 _("This filename is not valid UTF-8. "
2388 "You should rename it.\n"));
2391 /* Return the selection as a text/uri-list.
2392 * g_free() the result.
2394 static guchar *filer_create_uri_list(FilerWindow *filer_window)
2396 GString *string;
2397 GString *leader;
2398 ViewIter iter;
2399 DirItem *item;
2400 guchar *retval;
2402 g_return_val_if_fail(filer_window != NULL, NULL);
2404 string = g_string_new(NULL);
2406 leader = g_string_new(filer_window->sym_path);
2407 if (leader->str[leader->len - 1] != '/')
2408 g_string_append_c(leader, '/');
2410 view_get_iter(filer_window->view, &iter, VIEW_ITER_SELECTED);
2411 while ((item = iter.next(&iter)))
2413 EscapedPath *uri;
2414 char *path;
2416 path = g_strconcat(leader->str, item->leafname, NULL);
2417 uri = encode_path_as_uri(path);
2418 g_string_append(string, (char *) uri);
2419 g_string_append(string, "\r\n");
2420 g_free(path);
2421 g_free(uri);
2424 g_string_free(leader, TRUE);
2425 retval = string->str;
2426 g_string_free(string, FALSE);
2428 return retval;
2431 void filer_perform_action(FilerWindow *filer_window, GdkEventButton *event)
2433 BindAction action;
2434 ViewIface *view = filer_window->view;
2435 DirItem *item = NULL;
2436 gboolean press = event->type == GDK_BUTTON_PRESS;
2437 ViewIter iter;
2438 OpenFlags flags = 0;
2440 if (event->button > 3)
2441 return;
2443 view_get_iter_at_point(view, &iter, event->window, event->x, event->y);
2444 item = iter.peek(&iter);
2446 if (item && view_cursor_visible(view))
2447 view_cursor_to_iter(view, &iter);
2449 if (item && event->button == 1 &&
2450 view_get_selected(view, &iter) &&
2451 filer_window->selection_state == GTK_STATE_INSENSITIVE)
2453 /* Possibly a really slow DnD operation? */
2454 filer_window->temp_item_selected = FALSE;
2456 filer_selection_changed(filer_window, event->time);
2457 return;
2460 if (filer_window->target_cb)
2462 dnd_motion_ungrab();
2463 if (item && press && event->button == 1)
2464 filer_window->target_cb(filer_window, &iter,
2465 filer_window->target_data);
2467 filer_target_mode(filer_window, NULL, NULL, NULL);
2469 return;
2472 if (!o_single_click.int_value)
2474 /* Make sure both parts of a double-click fall on
2475 * the same file.
2477 static guchar *first_click = NULL;
2478 static guchar *second_click = NULL;
2480 if (event->type == GDK_BUTTON_PRESS)
2482 g_free(first_click);
2483 first_click = second_click;
2485 if (item)
2486 second_click = g_strdup(item->leafname);
2487 else
2488 second_click = NULL;
2491 if (event->type == GDK_2BUTTON_PRESS)
2493 if (first_click && second_click &&
2494 strcmp(first_click, second_click) != 0)
2495 return;
2496 if ((first_click || second_click) &&
2497 !(first_click && second_click))
2498 return;
2502 action = bind_lookup_bev(
2503 item ? BIND_DIRECTORY_ICON : BIND_DIRECTORY,
2504 event);
2506 switch (action)
2508 case ACT_CLEAR_SELECTION:
2509 view_clear_selection(view);
2510 break;
2511 case ACT_TOGGLE_SELECTED:
2512 view_set_selected(view, &iter,
2513 !view_get_selected(view, &iter));
2514 break;
2515 case ACT_SELECT_EXCL:
2516 view_select_only(view, &iter);
2517 break;
2518 case ACT_EDIT_ITEM:
2519 flags |= OPEN_SHIFT;
2520 /* (no break) */
2521 case ACT_OPEN_ITEM:
2522 if (event->button != 1 || event->state & GDK_MOD1_MASK)
2523 flags |= OPEN_CLOSE_WINDOW;
2524 else
2525 flags |= OPEN_SAME_WINDOW;
2526 if (o_new_button_1.int_value)
2527 flags ^= OPEN_SAME_WINDOW;
2528 if (event->type == GDK_2BUTTON_PRESS)
2529 view_set_selected(view, &iter, FALSE);
2530 dnd_motion_ungrab();
2532 filer_openitem(filer_window, &iter, flags);
2533 break;
2534 case ACT_POPUP_MENU:
2535 dnd_motion_ungrab();
2536 tooltip_show(NULL);
2537 show_filer_menu(filer_window,
2538 (GdkEvent *) event, &iter);
2539 break;
2540 case ACT_PRIME_AND_SELECT:
2541 if (item && !view_get_selected(view, &iter))
2542 view_select_only(view, &iter);
2543 dnd_motion_start(MOTION_READY_FOR_DND);
2544 break;
2545 case ACT_PRIME_AND_TOGGLE:
2546 view_set_selected(view, &iter,
2547 !view_get_selected(view, &iter));
2548 dnd_motion_start(MOTION_READY_FOR_DND);
2549 break;
2550 case ACT_PRIME_FOR_DND:
2551 dnd_motion_start(MOTION_READY_FOR_DND);
2552 break;
2553 case ACT_IGNORE:
2554 if (press && event->button < 4)
2556 if (item)
2557 view_wink_item(view, &iter);
2558 dnd_motion_start(MOTION_NONE);
2560 break;
2561 case ACT_LASSO_CLEAR:
2562 view_clear_selection(view);
2563 /* (no break) */
2564 case ACT_LASSO_MODIFY:
2565 view_start_lasso_box(view, event);
2566 break;
2567 case ACT_RESIZE:
2568 view_autosize(filer_window->view);
2569 break;
2570 default:
2571 g_warning("Unsupported action : %d\n", action);
2572 break;
2576 /* It's time to make the tooltip appear. If we're not over the item any
2577 * more, or the item doesn't need a tooltip, do nothing.
2579 static gboolean tooltip_activate(GtkWidget *window)
2581 FilerWindow *filer_window;
2582 ViewIface *view;
2583 ViewIter iter;
2584 gint x, y;
2585 DirItem *item = NULL;
2586 GString *tip = NULL;
2588 g_return_val_if_fail(tip_item != NULL, 0);
2590 filer_window = g_object_get_data(G_OBJECT(window), "filer_window");
2592 if (!motion_window || !filer_window)
2593 return FALSE; /* Window has been destroyed */
2595 view = filer_window->view;
2597 tooltip_show(NULL);
2599 gdk_window_get_pointer(motion_window, &x, &y, NULL);
2600 view_get_iter_at_point(view, &iter, motion_window, x, y);
2602 item = iter.peek(&iter);
2603 if (item != tip_item)
2604 return FALSE; /* Not still under the pointer */
2606 /* OK, the filer window still exists and the pointer is still
2607 * over the same item. Do we need to show a tip?
2610 tip = g_string_new(NULL);
2612 view_extend_tip(filer_window->view, &iter, tip);
2614 filer_add_tip_details(filer_window, tip, tip_item);
2616 if (tip->len > 1)
2618 g_string_truncate(tip, tip->len - 1);
2620 tooltip_show(tip->str);
2623 g_string_free(tip, TRUE);
2625 return FALSE;
2628 /* Motion detected on the View widget */
2629 gint filer_motion_notify(FilerWindow *filer_window, GdkEventMotion *event)
2631 ViewIface *view = filer_window->view;
2632 ViewIter iter;
2633 DirItem *item;
2635 view_get_iter_at_point(view, &iter, event->window, event->x, event->y);
2636 item = iter.peek(&iter);
2638 if (item)
2640 if (item != tip_item)
2642 tooltip_show(NULL);
2644 tip_item = item;
2645 motion_window = event->window;
2646 tooltip_prime((GtkFunction) tooltip_activate,
2647 G_OBJECT(filer_window->window));
2650 else
2652 tooltip_show(NULL);
2653 tip_item = NULL;
2656 if (motion_state != MOTION_READY_FOR_DND)
2657 return FALSE;
2659 if (!dnd_motion_moved(event))
2660 return FALSE;
2662 view_get_iter_at_point(view, &iter,
2663 event->window,
2664 event->x - (event->x_root - drag_start_x),
2665 event->y - (event->y_root - drag_start_y));
2666 item = iter.peek(&iter);
2667 if (!item)
2668 return FALSE;
2670 view_wink_item(view, NULL);
2672 if (!view_get_selected(view, &iter))
2674 if (event->state & GDK_BUTTON1_MASK)
2676 /* Select just this one */
2677 filer_window->temp_item_selected = TRUE;
2678 view_select_only(view, &iter);
2680 else
2682 if (view_count_selected(view) == 0)
2683 filer_window->temp_item_selected = TRUE;
2684 view_set_selected(view, &iter, TRUE);
2688 g_return_val_if_fail(view_count_selected(view) > 0, TRUE);
2690 if (view_count_selected(view) == 1)
2692 if (item->base_type == TYPE_UNKNOWN)
2693 item = dir_update_item(filer_window->directory,
2694 item->leafname);
2696 if (!item)
2698 report_error(_("Item no longer exists!"));
2699 return FALSE;
2702 drag_one_item(GTK_WIDGET(view), event,
2703 make_path(filer_window->sym_path, item->leafname),
2704 item, di_image(item));
2705 #if 0
2706 /* XXX: Use thumbnail */
2707 item, view ? view->image : NULL);
2708 #endif
2710 else
2712 guchar *uris;
2714 uris = filer_create_uri_list(filer_window);
2715 drag_selection(GTK_WIDGET(view), event, uris);
2716 g_free(uris);
2719 return FALSE;
2722 static void drag_end(GtkWidget *widget, GdkDragContext *context,
2723 FilerWindow *filer_window)
2725 filer_set_autoscroll(filer_window, FALSE);
2727 if (filer_window->temp_item_selected)
2729 view_clear_selection(filer_window->view);
2730 filer_window->temp_item_selected = FALSE;
2734 /* Remove highlights */
2735 static void drag_leave(GtkWidget *widget,
2736 GdkDragContext *context,
2737 guint32 time,
2738 FilerWindow *filer_window)
2740 dnd_spring_abort();
2743 /* Called during the drag when the mouse is in a widget registered
2744 * as a drop target. Returns TRUE if we can accept the drop.
2746 static gboolean drag_motion(GtkWidget *widget,
2747 GdkDragContext *context,
2748 gint x,
2749 gint y,
2750 guint time,
2751 FilerWindow *filer_window)
2753 DirItem *item;
2754 ViewIface *view = filer_window->view;
2755 ViewIter iter;
2756 GdkDragAction action = context->suggested_action;
2757 const guchar *new_path = NULL;
2758 const char *type = NULL;
2759 gboolean retval = FALSE;
2760 gboolean same_window;
2762 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value)
2764 guint state;
2765 gdk_window_get_pointer(NULL, NULL, NULL, &state);
2766 if (state & GDK_BUTTON1_MASK)
2767 action = GDK_ACTION_ASK;
2770 same_window = gtk_drag_get_source_widget(context) == widget;
2772 filer_set_autoscroll(filer_window, TRUE);
2774 if (filer_window->view_type == VIEW_TYPE_DETAILS)
2776 GdkWindow *bin;
2777 int bin_y;
2778 /* Correct for position of bin window */
2779 bin = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(view));
2780 gdk_window_get_position(bin, NULL, &bin_y);
2781 y -= bin_y;
2784 if (o_dnd_drag_to_icons.int_value)
2786 view_get_iter_at_point(view, &iter, widget->window, x, y);
2787 item = iter.peek(&iter);
2789 else
2790 item = NULL;
2792 if (item && same_window && view_get_selected(view, &iter))
2793 type = NULL;
2794 else
2795 type = dnd_motion_item(context, &item);
2797 if (!type)
2798 item = NULL;
2800 /* Don't allow drops to non-writeable directories. BUT, still
2801 * allow drops on non-writeable SUBdirectories so that we can
2802 * do the spring-open thing.
2804 if (item && type == drop_dest_dir &&
2805 !(item->flags & ITEM_FLAG_APPDIR))
2807 dnd_spring_load(context, filer_window);
2809 else
2810 dnd_spring_abort();
2812 if (item)
2813 view_cursor_to_iter(view, &iter);
2814 else
2816 view_cursor_to_iter(view, NULL);
2818 /* Disallow background drops within a single window */
2819 if (type && same_window)
2820 type = NULL;
2823 if (type)
2825 if (item)
2826 new_path = make_path(filer_window->sym_path,
2827 item->leafname);
2828 else
2829 new_path = filer_window->sym_path;
2832 /* Don't ask about dragging to an application! */
2833 if (type == drop_dest_prog && action == GDK_ACTION_ASK)
2834 action = GDK_ACTION_COPY;
2836 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
2837 if (type)
2839 gdk_drag_status(context, action, time);
2840 g_dataset_set_data_full(context, "drop_dest_path",
2841 g_strdup(new_path), g_free);
2842 retval = TRUE;
2845 return retval;
2848 static gboolean as_timeout(FilerWindow *filer_window)
2850 gboolean retval;
2852 retval = view_auto_scroll_callback(filer_window->view);
2854 if (!retval)
2855 filer_window->auto_scroll = -1;
2857 return retval;
2860 /* When autoscroll is on, a timer keeps track of the pointer position.
2861 * While it's near the top or bottom of the window, the window scrolls.
2863 * If the mouse buttons are released, the pointer leaves the window, or
2864 * a drag-and-drop operation finishes, auto_scroll is turned off.
2866 void filer_set_autoscroll(FilerWindow *filer_window, gboolean auto_scroll)
2868 g_return_if_fail(filer_window != NULL);
2870 if (auto_scroll)
2872 if (filer_window->auto_scroll != -1)
2873 return; /* Already on! */
2875 filer_window->auto_scroll = g_timeout_add(50,
2876 (GSourceFunc) as_timeout,
2877 filer_window);
2879 else
2881 if (filer_window->auto_scroll == -1)
2882 return; /* Already off! */
2884 g_source_remove(filer_window->auto_scroll);
2885 filer_window->auto_scroll = -1;
2889 #define ZERO_MNT "/uri/0install"
2891 static void refresh_done(FilerWindow *filer_window)
2893 if (filer_exists(filer_window))
2894 filer_update_dir(filer_window, TRUE);
2897 void filer_refresh(FilerWindow *filer_window)
2899 if (!strncmp(ZERO_MNT "/", filer_window->real_path, sizeof(ZERO_MNT)))
2901 /* Try to run 0refresh */
2902 gint pid;
2903 gchar *argv[] = {"0refresh", NULL, NULL};
2904 const char *host = filer_window->real_path + sizeof(ZERO_MNT);
2905 const char *slash;
2907 slash = strchr(host, '/');
2908 if (slash)
2909 argv[1] = g_strndup(host, slash - host);
2910 else
2911 argv[1] = g_strdup(host);
2912 pid = rox_spawn(filer_window->real_path, (const char **) argv);
2913 g_free(argv[1]);
2914 if (pid)
2915 on_child_death(pid, (CallbackFn) refresh_done,
2916 filer_window);
2919 full_refresh();
2922 static inline gboolean is_hidden(const char *dir, DirItem *item)
2924 /* If the leaf name starts with '.' then the item is hidden */
2925 if(item->leafname[0]=='.')
2926 return TRUE;
2928 /*** Test disabled for now. The flags aren't set on the first pass...
2930 #if 0
2931 /* Most files will not have extended attributes, so this should
2932 * be quick. */
2933 if(!o_xattr_ignore.int_value && (item->flags & ITEM_FLAG_HAS_XATTR)) {
2934 gchar *path, *val;
2935 int len;
2936 gboolean hidden=FALSE;
2938 path=g_build_filename(dir, item->leafname, NULL);
2939 val=xattr_get(path, XATTR_HIDDEN, &len);
2940 if(val) {
2941 hidden=atoi(val) || (strcmp(val, "true")==0);
2942 g_free(val);
2944 g_free(path);
2946 if(hidden)
2947 return TRUE;
2949 #endif
2951 /* Otherwise not hidden */
2952 return FALSE;
2955 gboolean filer_match_filter(FilerWindow *filer_window, DirItem *item)
2957 g_return_val_if_fail(item != NULL, FALSE);
2959 if(is_hidden(filer_window->real_path, item) &&
2960 (!filer_window->temp_show_hidden && !filer_window->show_hidden))
2961 return FALSE;
2963 switch(filer_window->filter) {
2964 case FILER_SHOW_GLOB:
2965 return fnmatch(filer_window->filter_string,
2966 item->leafname, 0)==0 ||
2967 (item->base_type==TYPE_DIRECTORY &&
2968 !filer_window->filter_directories);
2970 case FILER_SHOW_ALL:
2971 default:
2972 break;
2974 return TRUE;
2977 /* Provided to hide the implementation */
2978 void filer_set_hidden(FilerWindow *filer_window, gboolean hidden)
2980 filer_window->show_hidden=hidden;
2983 /* Provided to hide the implementation */
2984 void filer_set_filter_directories(FilerWindow *filer_window, gboolean filter_directories)
2986 filer_window->filter_directories=filter_directories;
2989 /* Set the filter type. Returns TRUE if the type has changed
2990 * (must call filer_detach_rescan).
2992 gboolean filer_set_filter(FilerWindow *filer_window, FilterType type,
2993 const gchar *filter_string)
2995 /* Is this new filter the same as the old one? */
2996 if (filer_window->filter == type)
2998 switch(filer_window->filter)
3000 case FILER_SHOW_ALL:
3001 return FALSE;
3002 case FILER_SHOW_GLOB:
3003 if (strcmp(filer_window->filter_string,
3004 filter_string) == 0)
3005 return FALSE;
3006 break;
3010 /* Clean up old filter */
3011 if (filer_window->filter_string)
3013 g_free(filer_window->filter_string);
3014 filer_window->filter_string = NULL;
3016 /* Also clean up compiled regexp when implemented */
3018 filer_window->filter = type;
3020 switch(type)
3022 case FILER_SHOW_ALL:
3023 /* No extra work */
3024 break;
3026 case FILER_SHOW_GLOB:
3027 filer_window->filter_string = g_strdup(filter_string);
3028 break;
3030 default:
3031 /* oops */
3032 filer_window->filter = FILER_SHOW_ALL;
3033 g_warning("Impossible: filter type %d", type);
3034 break;
3037 return TRUE;
3040 /* Setting stuff */
3041 static Settings *settings_new(const char *path)
3043 Settings *set;
3045 set=g_new(Settings, 1);
3046 memset(set, 0, sizeof(Settings));
3047 if(path)
3048 set->path=g_strdup(path);
3050 return set;
3053 static void settings_free(Settings *set)
3055 g_free(set->path);
3056 if(set->filter)
3057 g_free(set->filter);
3058 g_free(set);
3061 static void store_settings(Settings *set)
3063 Settings *old;
3065 old=g_hash_table_lookup(settings_table, set->path);
3066 if(old)
3068 g_hash_table_remove(settings_table, set->path);
3069 settings_free(old);
3072 g_hash_table_insert(settings_table, set->path, set);
3075 /* TODO: use symbolic names in the XML file where possible */
3076 static void load_from_node(Settings *set, xmlDocPtr doc, xmlNodePtr node)
3078 xmlChar *str=NULL;
3080 str=xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
3082 if(strcmp(node->name, "X") == 0) {
3083 set->x=atoi(str);
3084 set->flags|=SET_POSITION;
3085 } else if(strcmp(node->name, "Y") == 0) {
3086 set->y=atoi(str);
3087 set->flags|=SET_POSITION;
3088 } else if(strcmp(node->name, "Width") == 0) {
3089 set->width=atoi(str);
3090 set->flags|=SET_SIZE;
3091 } else if(strcmp(node->name, "Height") == 0) {
3092 set->height=atoi(str);
3093 set->flags|=SET_SIZE;
3094 } else if(strcmp(node->name, "ShowHidden") == 0) {
3095 set->show_hidden=atoi(str);
3096 set->flags|=SET_HIDDEN;
3097 } else if(strcmp(node->name, "ViewType") == 0) {
3098 set->view_type=atoi(str);
3099 set->flags|=SET_DETAILS;
3100 } else if(strcmp(node->name, "DetailsType") == 0) {
3101 set->details_type=atoi(str);
3102 set->flags|=SET_DETAILS;
3103 } else if(strcmp(node->name, "SortType") == 0) {
3104 set->sort_type=atoi(str);
3105 set->flags|=SET_SORT;
3106 } else if(strcmp(node->name, "SortOrder") == 0) {
3107 set->sort_order=atoi(str);
3108 set->flags|=SET_SORT;
3109 } else if(strcmp(node->name, "DisplayStyle") == 0) {
3110 set->display_style=atoi(str);
3111 set->flags|=SET_STYLE;
3112 } else if(strcmp(node->name, "ShowThumbs") == 0) {
3113 set->show_thumbs=atoi(str);
3114 set->flags|=SET_THUMBS;
3115 } else if(strcmp(node->name, "FilterType") == 0) {
3116 set->filter_type=atoi(str);
3117 set->flags|=SET_FILTER;
3118 } else if(strcmp(node->name, "Filter") == 0) {
3119 set->filter=g_strdup(str);
3120 set->flags|=SET_FILTER;
3121 } else if(strcmp(node->name, "FilterDirectories") == 0) {
3122 set->filter_directories=atoi(str);
3123 set->flags|=SET_FILTER;
3126 if(str)
3127 xmlFree(str);
3130 static void load_settings(void)
3132 gchar *path;
3133 XMLwrapper *settings_doc=NULL;
3135 path=choices_find_xdg_path_load("Settings.xml", PROJECT, SITE);
3136 if(path) {
3137 settings_doc=xml_new(path);
3138 g_free(path);
3141 if(!settings_table)
3142 settings_table=g_hash_table_new(g_str_hash, g_str_equal);
3144 if(settings_doc) {
3145 xmlNodePtr node, subnode;
3147 node = xmlDocGetRootElement(settings_doc->doc);
3149 for (node = node->xmlChildrenNode; node; node = node->next)
3151 Settings *set;
3152 xmlChar *path;
3154 if (node->type != XML_ELEMENT_NODE)
3155 continue;
3156 if (strcmp(node->name, "FilerWindow") != 0)
3157 continue;
3159 path=xmlGetProp(node, "path");
3160 set=settings_new(path);
3161 xmlFree(path);
3163 for (subnode=node->xmlChildrenNode; subnode;
3164 subnode=subnode->next) {
3166 if (subnode->type != XML_ELEMENT_NODE)
3167 continue;
3168 load_from_node(set, settings_doc->doc,
3169 subnode);
3172 store_settings(set);
3174 g_object_unref(settings_doc);
3178 static void add_nodes(gpointer key, gpointer value, gpointer data)
3180 xmlNodePtr node=(xmlNodePtr) data;
3181 xmlNodePtr sub;
3182 Settings *set=(Settings *) value;
3183 char *tmp;
3185 sub=xmlNewChild(node, NULL, "FilerWindow", NULL);
3187 xmlSetProp(sub, "path", set->path);
3189 if(set->flags & SET_POSITION) {
3190 tmp=g_strdup_printf("%d", set->x);
3191 xmlNewChild(sub, NULL, "X", tmp);
3192 g_free(tmp);
3193 tmp=g_strdup_printf("%d", set->y);
3194 xmlNewChild(sub, NULL, "Y", tmp);
3195 g_free(tmp);
3197 if(set->flags & SET_SIZE) {
3198 tmp=g_strdup_printf("%d", set->width);
3199 xmlNewChild(sub, NULL, "Width", tmp);
3200 g_free(tmp);
3201 tmp=g_strdup_printf("%d", set->height);
3202 xmlNewChild(sub, NULL, "Height", tmp);
3203 g_free(tmp);
3205 if(set->flags & SET_HIDDEN) {
3206 tmp=g_strdup_printf("%d", set->show_hidden);
3207 xmlNewChild(sub, NULL, "ShowHidden", tmp);
3208 g_free(tmp);
3210 if(set->flags & SET_STYLE) {
3211 tmp=g_strdup_printf("%d", set->display_style);
3212 xmlNewChild(sub, NULL, "DisplayStyle", tmp);
3213 g_free(tmp);
3215 if(set->flags & SET_SORT) {
3216 tmp=g_strdup_printf("%d", set->sort_type);
3217 xmlNewChild(sub, NULL, "SortType", tmp);
3218 g_free(tmp);
3219 tmp=g_strdup_printf("%d", set->sort_order);
3220 xmlNewChild(sub, NULL, "SortOrder", tmp);
3221 g_free(tmp);
3223 if(set->flags & SET_DETAILS) {
3224 tmp=g_strdup_printf("%d", set->view_type);
3225 xmlNewChild(sub, NULL, "ViewType", tmp);
3226 g_free(tmp);
3227 tmp=g_strdup_printf("%d", set->details_type);
3228 xmlNewChild(sub, NULL, "DetailsType", tmp);
3229 g_free(tmp);
3231 if(set->flags & SET_STYLE) {
3232 tmp=g_strdup_printf("%d", set->show_thumbs);
3233 xmlNewChild(sub, NULL, "ShowThumbs", tmp);
3234 g_free(tmp);
3236 if(set->flags & SET_FILTER) {
3237 tmp=g_strdup_printf("%d", set->filter_type);
3238 xmlNewChild(sub, NULL, "FilterType", tmp);
3239 g_free(tmp);
3240 if(set->filter && set->filter[0])
3241 xmlNewChild(sub, NULL, "Filter", set->filter);
3242 tmp=g_strdup_printf("%d", set->filter_directories);
3243 xmlNewChild(sub, NULL, "FilterDirectories", tmp);
3247 static void save_settings(void)
3249 gchar *path;
3251 path=choices_find_xdg_path_save("Settings.xml", PROJECT, SITE, TRUE);
3252 if(path) {
3253 xmlDocPtr doc = xmlNewDoc("1.0");
3254 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL,
3255 "Settings", NULL));
3257 g_hash_table_foreach(settings_table, add_nodes,
3258 xmlDocGetRootElement(doc));
3260 save_xml_file(doc, path);
3262 g_free(path);
3263 xmlFreeDoc(doc);
3268 static void check_settings(FilerWindow *filer_window)
3270 Settings *set;
3272 set=(Settings *) g_hash_table_lookup(settings_table,
3273 filer_window->sym_path);
3275 if(set) {
3276 if(set->flags & SET_POSITION)
3277 gtk_window_move(GTK_WINDOW(filer_window->window),
3278 set->x, set->y);
3279 if(set->flags & SET_SIZE)
3280 filer_window_set_size(filer_window, set->width,
3281 set->height);
3282 if(set->flags & SET_HIDDEN)
3283 filer_set_hidden(filer_window, set->show_hidden);
3285 if(set->flags & (SET_STYLE|SET_DETAILS)) {
3286 DisplayStyle style=filer_window->display_style;
3287 DetailsType details=filer_window->details_type;
3289 if(set->flags & SET_STYLE)
3290 style=set->display_style;
3292 if(set->flags & SET_DETAILS) {
3293 details=set->details_type;
3295 filer_set_view_type(filer_window,
3296 set->view_type);
3299 display_set_layout(filer_window, style,
3300 details, FALSE);
3303 if(set->flags & SET_SORT)
3304 display_set_sort_type(filer_window,
3305 set->sort_type,
3306 set->sort_order);
3308 if(set->flags & SET_THUMBS)
3309 display_set_thumbs(filer_window,
3310 set->show_thumbs);
3312 if(set->flags & SET_FILTER)
3314 display_set_filter(filer_window,
3315 set->filter_type,
3316 set->filter);
3317 display_set_filter_directories(filer_window,
3318 set->filter_directories);
3324 typedef struct settings_window {
3325 GtkWidget *window;
3327 GtkWidget *pos, *size, *hidden, *style, *sort, *details,
3328 *thumbs, *filter;
3330 Settings *set;
3331 } SettingsWindow;
3334 static void settings_response(GtkWidget *window, gint response,
3335 SettingsWindow *set_win)
3337 if(response==GTK_RESPONSE_OK) {
3338 gint flags=0;
3340 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->pos)))
3341 flags|=SET_POSITION;
3342 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->size)))
3343 flags|=SET_SIZE;
3344 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->hidden)))
3345 flags|=SET_HIDDEN;
3346 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->style)))
3347 flags|=SET_STYLE;
3348 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->sort)))
3349 flags|=SET_SORT;
3350 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->details)))
3351 flags|=SET_DETAILS;
3352 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->thumbs)))
3353 flags|=SET_THUMBS;
3354 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(set_win->filter)))
3355 flags|=SET_FILTER;
3357 set_win->set->flags=flags;
3358 store_settings(set_win->set);
3359 save_settings();
3362 gtk_widget_destroy(window);
3365 void filer_save_settings(FilerWindow *fwin)
3367 SettingsWindow *set_win;
3368 GtkWidget *vbox, *frame;
3369 GtkWidget *path, *lbl;
3370 gint x, y;
3372 Settings *set=settings_new(fwin->sym_path);
3374 gtk_window_get_position(GTK_WINDOW(fwin->window),&x, &y);
3375 set->flags|=SET_POSITION;
3376 set->x=x;
3377 set->y=y;
3379 gtk_window_get_size(GTK_WINDOW(fwin->window),&x, &y);
3380 set->flags|=SET_SIZE;
3381 set->width=x;
3382 set->height=y;
3384 set->flags|=SET_HIDDEN;
3385 set->show_hidden=fwin->show_hidden;
3387 set->flags|=SET_STYLE;
3388 set->display_style=fwin->display_style;
3390 set->flags|=SET_SORT;
3391 set->sort_type=fwin->sort_type;
3392 set->sort_order=fwin->sort_order;
3394 set->flags|=SET_DETAILS;
3395 set->view_type=fwin->view_type;
3396 set->details_type=fwin->details_type;
3398 set->flags|=SET_THUMBS;
3399 set->show_thumbs=fwin->show_thumbs;
3401 set->flags|=SET_FILTER;
3402 set->filter_type=fwin->filter;
3403 if(fwin->filter_string)
3404 set->filter=g_strdup(fwin->filter_string);
3405 set->filter_directories=fwin->filter_directories;
3407 /* Store other parameters
3409 set_win=g_new(SettingsWindow, 1);
3411 set_win->window=gtk_dialog_new();
3412 number_of_windows++;
3414 gtk_dialog_add_button(GTK_DIALOG(set_win->window),
3415 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
3416 gtk_dialog_add_button(GTK_DIALOG(set_win->window),
3417 GTK_STOCK_OK, GTK_RESPONSE_OK);
3419 g_signal_connect(set_win->window, "destroy",
3420 G_CALLBACK(one_less_window), NULL);
3421 g_signal_connect_swapped(set_win->window, "destroy",
3422 G_CALLBACK(g_free), set_win);
3424 gtk_window_set_title(GTK_WINDOW(set_win->window),
3425 _("Select display properties to save"));
3427 vbox=GTK_DIALOG(set_win->window)->vbox;
3429 lbl=gtk_label_new(_("<b>Save display settings for directory</b>"));
3430 gtk_label_set_use_markup(GTK_LABEL(lbl), TRUE);
3431 gtk_box_pack_start(GTK_BOX(vbox), lbl, FALSE, FALSE, 2);
3433 path=gtk_label_new(set->path);
3434 gtk_box_pack_start(GTK_BOX(vbox), path, FALSE, FALSE, 2);
3436 frame=gtk_frame_new(_("Select settings to save"));
3437 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 2);
3439 /*Make new vbox to go in the frame */
3440 vbox=gtk_vbox_new(FALSE, 2);
3441 gtk_container_add(GTK_CONTAINER(frame), vbox);
3443 set_win->pos=gtk_check_button_new_with_label(_("Position"));
3444 gtk_box_pack_start(GTK_BOX(vbox), set_win->pos, FALSE, FALSE, 2);
3445 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->pos),
3446 set->flags & SET_POSITION);
3448 set_win->size=gtk_check_button_new_with_label(_("Size"));
3449 gtk_box_pack_start(GTK_BOX(vbox), set_win->size, FALSE, FALSE, 2);
3450 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->size),
3451 set->flags & SET_SIZE);
3453 set_win->hidden=gtk_check_button_new_with_label(_("Show hidden"));
3454 gtk_box_pack_start(GTK_BOX(vbox), set_win->hidden,
3455 FALSE, FALSE, 2);
3456 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->hidden),
3457 set->flags & SET_HIDDEN);
3459 set_win->style=gtk_check_button_new_with_label(_("Display style"));
3460 gtk_box_pack_start(GTK_BOX(vbox), set_win->style,
3461 FALSE, FALSE, 2);
3462 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->style),
3463 set->flags & SET_STYLE);
3465 set_win->sort=gtk_check_button_new_with_label(_("Sort type and order"));
3466 gtk_box_pack_start(GTK_BOX(vbox), set_win->sort, FALSE, FALSE, 2);
3467 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->sort),
3468 set->flags & SET_SORT);
3470 set_win->details=gtk_check_button_new_with_label(_("Details"));
3471 gtk_box_pack_start(GTK_BOX(vbox), set_win->details, FALSE, FALSE, 2);
3472 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->details),
3473 set->flags & SET_DETAILS);
3475 set_win->thumbs=gtk_check_button_new_with_label(_("Thumbnails"));
3476 gtk_box_pack_start(GTK_BOX(vbox), set_win->thumbs,
3477 FALSE, FALSE, 2);
3478 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->thumbs),
3479 set->flags & SET_THUMBS);
3481 set_win->filter=gtk_check_button_new_with_label(_("Filter"));
3482 gtk_box_pack_start(GTK_BOX(vbox), set_win->filter,
3483 FALSE, FALSE, 2);
3484 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(set_win->filter),
3485 set->flags & SET_FILTER);
3487 set_win->set=set;
3488 g_signal_connect(set_win->window, "response",
3489 G_CALLBACK(settings_response), set_win);
3491 gtk_widget_show_all(set_win->window);
3494 static char *tip_from_desktop_file(const char *full_path)
3496 GError *error = NULL;
3497 char *comment = NULL;
3499 comment = get_value_from_desktop_file(full_path,
3500 "Desktop Entry", "Comment", &error);
3501 if (error)
3503 delayed_error("Failed to parse .desktop file '%s':\n%s",
3504 full_path, error->message);
3505 goto err;
3508 err:
3509 if (error != NULL)
3510 g_error_free(error);
3512 return comment;