]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdefault.c
Unset "focus-on-click" for the save folder combo. (#168688, Sven Neumann)
[~andy/gtk] / gtk / gtkfilechooserdefault.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include "gdk/gdkkeysyms.h"
23 #include "gtkalias.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcheckmenuitem.h"
32 #include "gtkcombobox.h"
33 #include "gtkentry.h"
34 #include "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechooserutils.h"
40 #include "gtkfilechooser.h"
41 #include "gtkfilesystemmodel.h"
42 #include "gtkframe.h"
43 #include "gtkhbox.h"
44 #include "gtkhpaned.h"
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
47 #include "gtkimage.h"
48 #include "gtkimagemenuitem.h"
49 #include "gtkintl.h"
50 #include "gtklabel.h"
51 #include "gtkmarshalers.h"
52 #include "gtkmenuitem.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkpathbar.h"
55 #include "gtkprivate.h"
56 #include "gtkscrolledwindow.h"
57 #include "gtkseparatormenuitem.h"
58 #include "gtksizegroup.h"
59 #include "gtkstock.h"
60 #include "gtktable.h"
61 #include "gtktreednd.h"
62 #include "gtktreeprivate.h"
63 #include "gtktreeview.h"
64 #include "gtktreemodelsort.h"
65 #include "gtktreeselection.h"
66 #include "gtktreestore.h"
67 #include "gtktooltips.h"
68 #include "gtktypebuiltins.h"
69 #include "gtkvbox.h"
70
71 #if defined (G_OS_UNIX)
72 #include "gtkfilesystemunix.h"
73 #elif defined (G_OS_WIN32)
74 #include "gtkfilesystemwin32.h"
75 #endif
76
77 #include <errno.h>
78 #include <string.h>
79 #include <time.h>
80
81 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
82
83 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
84 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
85 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
86
87 typedef enum {
88   LOAD_EMPTY,                   /* There is no model */
89   LOAD_PRELOAD,                 /* Model is loading and a timer is running; model isn't inserted into the tree yet */
90   LOAD_LOADING,                 /* Timeout expired, model is inserted into the tree, but not fully loaded yet */
91   LOAD_FINISHED                 /* Model is fully loaded and inserted into the tree */
92 } LoadState;
93
94 #define MAX_LOADING_TIME 500
95
96 struct _GtkFileChooserDefaultClass
97 {
98   GtkVBoxClass parent_class;
99 };
100
101 struct _GtkFileChooserDefault
102 {
103   GtkVBox parent_instance;
104
105   GtkFileChooserAction action;
106
107   GtkFileSystem *file_system;
108
109   /* Save mode widgets */
110   GtkWidget *save_widgets;
111
112   GtkWidget *save_file_name_entry;
113   GtkWidget *save_folder_label;
114   GtkWidget *save_folder_combo;
115   GtkWidget *save_expander;
116
117   /* The file browsing widgets */
118   GtkWidget *browse_widgets;
119   GtkWidget *browse_shortcuts_tree_view;
120   GtkWidget *browse_shortcuts_add_button;
121   GtkWidget *browse_shortcuts_remove_button;
122   GtkWidget *browse_files_tree_view;
123   GtkWidget *browse_files_popup_menu;
124   GtkWidget *browse_files_popup_menu_add_shortcut_item;
125   GtkWidget *browse_files_popup_menu_hidden_files_item;
126   GtkWidget *browse_new_folder_button;
127   GtkWidget *browse_path_bar;
128
129   GtkFileSystemModel *browse_files_model;
130
131   GtkWidget *filter_combo_hbox;
132   GtkWidget *filter_combo;
133   GtkWidget *preview_box;
134   GtkWidget *preview_label;
135   GtkWidget *preview_widget;
136   GtkWidget *extra_align;
137   GtkWidget *extra_widget;
138
139   GtkListStore *shortcuts_model;
140   GtkTreeModel *shortcuts_filter_model;
141
142   GtkTreeModelSort *sort_model;
143
144   LoadState load_state;
145   guint load_timeout_id;
146
147   GSList *pending_select_paths;
148
149   GtkFileFilter *current_filter;
150   GSList *filters;
151
152   GtkTooltips *tooltips;
153
154   gboolean has_home;
155   gboolean has_desktop;
156
157   int num_volumes;
158   int num_shortcuts;
159   int num_bookmarks;
160
161   gulong volumes_changed_id;
162   gulong bookmarks_changed_id;
163
164   GtkFilePath *current_volume_path;
165   GtkFilePath *current_folder;
166   GtkFilePath *preview_path;
167   char *preview_display_name;
168
169   GtkTreeViewColumn *list_name_column;
170   GtkCellRenderer *list_name_renderer;
171
172   GSource *edited_idle;
173   char *edited_new_text;
174
175   gulong settings_signal_id;
176   int icon_size;
177
178   gulong toplevel_set_focus_id;
179   GtkWidget *toplevel_last_focus_widget;
180
181 #if 0
182   GdkDragContext *shortcuts_drag_context;
183   GSource *shortcuts_drag_outside_idle;
184 #endif
185
186   /* Flags */
187
188   guint local_only : 1;
189   guint preview_widget_active : 1;
190   guint use_preview_label : 1;
191   guint select_multiple : 1;
192   guint show_hidden : 1;
193   guint list_sort_ascending : 1;
194   guint changing_folder : 1;
195   guint shortcuts_current_folder_active : 1;
196
197 #if 0
198   guint shortcuts_drag_outside : 1;
199 #endif
200 };
201
202 /* Signal IDs */
203 enum {
204   LOCATION_POPUP,
205   UP_FOLDER,
206   DOWN_FOLDER,
207   HOME_FOLDER,
208   LAST_SIGNAL
209 };
210
211 static guint signals[LAST_SIGNAL] = { 0 };
212
213 /* Column numbers for the shortcuts tree.  Keep these in sync with shortcuts_model_create() */
214 enum {
215   SHORTCUTS_COL_PIXBUF,
216   SHORTCUTS_COL_NAME,
217   SHORTCUTS_COL_DATA,
218   SHORTCUTS_COL_IS_VOLUME,
219   SHORTCUTS_COL_REMOVABLE,
220   SHORTCUTS_COL_PIXBUF_VISIBLE,
221   SHORTCUTS_COL_NUM_COLUMNS
222 };
223
224 /* Column numbers for the file list */
225 enum {
226   FILE_LIST_COL_NAME,
227   FILE_LIST_COL_SIZE,
228   FILE_LIST_COL_MTIME,
229   FILE_LIST_COL_NUM_COLUMNS
230 };
231
232 /* Identifiers for target types */
233 enum {
234   GTK_TREE_MODEL_ROW,
235   TEXT_URI_LIST
236 };
237
238 /* Target types for dragging from the shortcuts list */
239 static GtkTargetEntry shortcuts_source_targets[] = {
240   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
241 };
242
243 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
244                                                  / sizeof (shortcuts_source_targets[0]));
245
246 /* Target types for dropping into the shortcuts list */
247 static GtkTargetEntry shortcuts_dest_targets[] = {
248   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
249   { "text/uri-list", 0, TEXT_URI_LIST }
250 };
251
252 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
253                                                / sizeof (shortcuts_dest_targets[0]));
254
255 /* Target types for DnD from the file list */
256 static GtkTargetEntry file_list_source_targets[] = {
257   { "text/uri-list", 0, TEXT_URI_LIST }
258 };
259
260 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
261                                                  / sizeof (file_list_source_targets[0]));
262
263 /* Interesting places in the shortcuts bar */
264 typedef enum {
265   SHORTCUTS_HOME,
266   SHORTCUTS_DESKTOP,
267   SHORTCUTS_VOLUMES,
268   SHORTCUTS_SHORTCUTS,
269   SHORTCUTS_BOOKMARKS_SEPARATOR,
270   SHORTCUTS_BOOKMARKS,
271   SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
272   SHORTCUTS_CURRENT_FOLDER
273 } ShortcutsIndex;
274
275 /* Icon size for if we can't get it from the theme */
276 #define FALLBACK_ICON_SIZE 16
277
278 #define PREVIEW_HBOX_SPACING 12
279 #define NUM_LINES 40
280 #define NUM_CHARS 60
281
282 static void gtk_file_chooser_default_class_init       (GtkFileChooserDefaultClass *class);
283 static void gtk_file_chooser_default_iface_init       (GtkFileChooserIface        *iface);
284 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface   *iface);
285 static void gtk_file_chooser_default_init             (GtkFileChooserDefault      *impl);
286
287 static GObject* gtk_file_chooser_default_constructor  (GType                  type,
288                                                        guint                  n_construct_properties,
289                                                        GObjectConstructParam *construct_params);
290 static void     gtk_file_chooser_default_finalize     (GObject               *object);
291 static void     gtk_file_chooser_default_set_property (GObject               *object,
292                                                        guint                  prop_id,
293                                                        const GValue          *value,
294                                                        GParamSpec            *pspec);
295 static void     gtk_file_chooser_default_get_property (GObject               *object,
296                                                        guint                  prop_id,
297                                                        GValue                *value,
298                                                        GParamSpec            *pspec);
299 static void     gtk_file_chooser_default_dispose      (GObject               *object);
300 static void     gtk_file_chooser_default_show_all       (GtkWidget             *widget);
301 static void     gtk_file_chooser_default_map            (GtkWidget             *widget);
302 static void     gtk_file_chooser_default_hierarchy_changed (GtkWidget          *widget,
303                                                             GtkWidget          *previous_toplevel);
304 static void     gtk_file_chooser_default_style_set      (GtkWidget             *widget,
305                                                          GtkStyle              *previous_style);
306 static void     gtk_file_chooser_default_screen_changed (GtkWidget             *widget,
307                                                          GdkScreen             *previous_screen);
308
309 static gboolean       gtk_file_chooser_default_set_current_folder          (GtkFileChooser    *chooser,
310                                                                             const GtkFilePath *path,
311                                                                             GError           **error);
312 static GtkFilePath *  gtk_file_chooser_default_get_current_folder          (GtkFileChooser    *chooser);
313 static void           gtk_file_chooser_default_set_current_name            (GtkFileChooser    *chooser,
314                                                                             const gchar       *name);
315 static gboolean       gtk_file_chooser_default_select_path                 (GtkFileChooser    *chooser,
316                                                                             const GtkFilePath *path,
317                                                                             GError           **error);
318 static void           gtk_file_chooser_default_unselect_path               (GtkFileChooser    *chooser,
319                                                                             const GtkFilePath *path);
320 static void           gtk_file_chooser_default_select_all                  (GtkFileChooser    *chooser);
321 static void           gtk_file_chooser_default_unselect_all                (GtkFileChooser    *chooser);
322 static GSList *       gtk_file_chooser_default_get_paths                   (GtkFileChooser    *chooser);
323 static GtkFilePath *  gtk_file_chooser_default_get_preview_path            (GtkFileChooser    *chooser);
324 static GtkFileSystem *gtk_file_chooser_default_get_file_system             (GtkFileChooser    *chooser);
325 static void           gtk_file_chooser_default_add_filter                  (GtkFileChooser    *chooser,
326                                                                             GtkFileFilter     *filter);
327 static void           gtk_file_chooser_default_remove_filter               (GtkFileChooser    *chooser,
328                                                                             GtkFileFilter     *filter);
329 static GSList *       gtk_file_chooser_default_list_filters                (GtkFileChooser    *chooser);
330 static gboolean       gtk_file_chooser_default_add_shortcut_folder    (GtkFileChooser    *chooser,
331                                                                        const GtkFilePath *path,
332                                                                        GError           **error);
333 static gboolean       gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
334                                                                        const GtkFilePath *path,
335                                                                        GError           **error);
336 static GSList *       gtk_file_chooser_default_list_shortcut_folders  (GtkFileChooser    *chooser);
337
338 static void           gtk_file_chooser_default_get_default_size       (GtkFileChooserEmbed *chooser_embed,
339                                                                        gint                *default_width,
340                                                                        gint                *default_height);
341 static void           gtk_file_chooser_default_get_resizable_hints    (GtkFileChooserEmbed *chooser_embed,
342                                                                        gboolean            *resize_horizontally,
343                                                                        gboolean            *resize_vertically);
344 static gboolean       gtk_file_chooser_default_should_respond         (GtkFileChooserEmbed *chooser_embed);
345 static void           gtk_file_chooser_default_initial_focus          (GtkFileChooserEmbed *chooser_embed);
346
347 static void location_popup_handler (GtkFileChooserDefault *impl,
348                                     const gchar           *path);
349 static void up_folder_handler      (GtkFileChooserDefault *impl);
350 static void down_folder_handler    (GtkFileChooserDefault *impl);
351 static void home_folder_handler    (GtkFileChooserDefault *impl);
352 static void update_appearance      (GtkFileChooserDefault *impl);
353
354 static void set_current_filter   (GtkFileChooserDefault *impl,
355                                   GtkFileFilter         *filter);
356 static void check_preview_change (GtkFileChooserDefault *impl);
357
358 static void filter_combo_changed       (GtkComboBox           *combo_box,
359                                         GtkFileChooserDefault *impl);
360 static void     shortcuts_row_activated_cb (GtkTreeView           *tree_view,
361                                             GtkTreePath           *path,
362                                             GtkTreeViewColumn     *column,
363                                             GtkFileChooserDefault *impl);
364
365 static gboolean shortcuts_key_press_event_cb (GtkWidget             *widget,
366                                               GdkEventKey           *event,
367                                               GtkFileChooserDefault *impl);
368
369 static gboolean shortcuts_select_func   (GtkTreeSelection      *selection,
370                                          GtkTreeModel          *model,
371                                          GtkTreePath           *path,
372                                          gboolean               path_currently_selected,
373                                          gpointer               data);
374 static gboolean shortcuts_get_selected  (GtkFileChooserDefault *impl,
375                                          GtkTreeIter           *iter);
376 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
377                                      GtkTreeIter           *iter);
378 static int shortcuts_get_index (GtkFileChooserDefault *impl,
379                                 ShortcutsIndex         where);
380 static int shortcut_find_position (GtkFileChooserDefault *impl,
381                                    const GtkFilePath     *path);
382
383 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
384
385 static gboolean list_select_func   (GtkTreeSelection      *selection,
386                                     GtkTreeModel          *model,
387                                     GtkTreePath           *path,
388                                     gboolean               path_currently_selected,
389                                     gpointer               data);
390
391 static void list_selection_changed     (GtkTreeSelection      *tree_selection,
392                                         GtkFileChooserDefault *impl);
393 static void list_row_activated         (GtkTreeView           *tree_view,
394                                         GtkTreePath           *path,
395                                         GtkTreeViewColumn     *column,
396                                         GtkFileChooserDefault *impl);
397
398 static void select_func (GtkFileSystemModel *model,
399                          GtkTreePath        *path,
400                          GtkTreeIter        *iter,
401                          gpointer            user_data);
402
403 static void path_bar_clicked           (GtkPathBar            *path_bar,
404                                         GtkFilePath           *file_path,
405                                         gboolean               child_is_hidden,
406                                         GtkFileChooserDefault *impl);
407
408 static void add_bookmark_button_clicked_cb    (GtkButton             *button,
409                                                GtkFileChooserDefault *impl);
410 static void remove_bookmark_button_clicked_cb (GtkButton             *button,
411                                                GtkFileChooserDefault *impl);
412
413 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
414                                  GtkCellRenderer   *cell,
415                                  GtkTreeModel      *tree_model,
416                                  GtkTreeIter       *iter,
417                                  gpointer           data);
418 static void list_name_data_func (GtkTreeViewColumn *tree_column,
419                                  GtkCellRenderer   *cell,
420                                  GtkTreeModel      *tree_model,
421                                  GtkTreeIter       *iter,
422                                  gpointer           data);
423 #if 0
424 static void list_size_data_func (GtkTreeViewColumn *tree_column,
425                                  GtkCellRenderer   *cell,
426                                  GtkTreeModel      *tree_model,
427                                  GtkTreeIter       *iter,
428                                  gpointer           data);
429 #endif
430 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
431                                   GtkCellRenderer   *cell,
432                                   GtkTreeModel      *tree_model,
433                                   GtkTreeIter       *iter,
434                                   gpointer           data);
435
436 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
437                                               GtkTreeIter           *iter);
438
439 static void load_remove_timer (GtkFileChooserDefault *impl);
440
441 static GObjectClass *parent_class;
442
443 \f
444
445 /* Drag and drop interface declarations */
446
447 typedef struct {
448   GtkTreeModelFilter parent;
449
450   GtkFileChooserDefault *impl;
451 } ShortcutsModelFilter;
452
453 typedef struct {
454   GtkTreeModelFilterClass parent_class;
455 } ShortcutsModelFilterClass;
456
457 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
458 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
459
460 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
461
462 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
463                          _shortcuts_model_filter,
464                          GTK_TYPE_TREE_MODEL_FILTER,
465                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
466                                                 shortcuts_model_filter_drag_source_iface_init));
467
468 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
469                                                  GtkTreeModel          *child_model,
470                                                  GtkTreePath           *root);
471
472 \f
473
474 GType
475 _gtk_file_chooser_default_get_type (void)
476 {
477   static GType file_chooser_default_type = 0;
478
479   if (!file_chooser_default_type)
480     {
481       static const GTypeInfo file_chooser_default_info =
482       {
483         sizeof (GtkFileChooserDefaultClass),
484         NULL,           /* base_init */
485         NULL,           /* base_finalize */
486         (GClassInitFunc) gtk_file_chooser_default_class_init,
487         NULL,           /* class_finalize */
488         NULL,           /* class_data */
489         sizeof (GtkFileChooserDefault),
490         0,              /* n_preallocs */
491         (GInstanceInitFunc) gtk_file_chooser_default_init,
492       };
493
494       static const GInterfaceInfo file_chooser_info =
495       {
496         (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
497         NULL,                                                          /* interface_finalize */
498         NULL                                                           /* interface_data */
499       };
500
501       static const GInterfaceInfo file_chooser_embed_info =
502       {
503         (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
504         NULL,                                                          /* interface_finalize */
505         NULL                                                           /* interface_data */
506       };
507
508       file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
509                                                          &file_chooser_default_info, 0);
510
511       g_type_add_interface_static (file_chooser_default_type,
512                                    GTK_TYPE_FILE_CHOOSER,
513                                    &file_chooser_info);
514       g_type_add_interface_static (file_chooser_default_type,
515                                    GTK_TYPE_FILE_CHOOSER_EMBED,
516                                    &file_chooser_embed_info);
517     }
518
519   return file_chooser_default_type;
520 }
521
522 static void
523 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
524 {
525   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
526   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
527   GtkBindingSet *binding_set;
528
529   parent_class = g_type_class_peek_parent (class);
530
531   gobject_class->finalize = gtk_file_chooser_default_finalize;
532   gobject_class->constructor = gtk_file_chooser_default_constructor;
533   gobject_class->set_property = gtk_file_chooser_default_set_property;
534   gobject_class->get_property = gtk_file_chooser_default_get_property;
535   gobject_class->dispose = gtk_file_chooser_default_dispose;
536
537   widget_class->show_all = gtk_file_chooser_default_show_all;
538   widget_class->map = gtk_file_chooser_default_map;
539   widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
540   widget_class->style_set = gtk_file_chooser_default_style_set;
541   widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
542
543   signals[LOCATION_POPUP] =
544     _gtk_binding_signal_new ("location-popup",
545                              G_OBJECT_CLASS_TYPE (class),
546                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
547                              G_CALLBACK (location_popup_handler),
548                              NULL, NULL,
549                              _gtk_marshal_VOID__STRING,
550                              G_TYPE_NONE, 1, G_TYPE_STRING);
551   signals[UP_FOLDER] =
552     _gtk_binding_signal_new ("up-folder",
553                              G_OBJECT_CLASS_TYPE (class),
554                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
555                              G_CALLBACK (up_folder_handler),
556                              NULL, NULL,
557                              _gtk_marshal_VOID__VOID,
558                              G_TYPE_NONE, 0);
559   signals[DOWN_FOLDER] =
560     _gtk_binding_signal_new ("down-folder",
561                              G_OBJECT_CLASS_TYPE (class),
562                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
563                              G_CALLBACK (down_folder_handler),
564                              NULL, NULL,
565                              _gtk_marshal_VOID__VOID,
566                              G_TYPE_NONE, 0);
567   signals[HOME_FOLDER] =
568     _gtk_binding_signal_new ("home-folder",
569                              G_OBJECT_CLASS_TYPE (class),
570                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
571                              G_CALLBACK (home_folder_handler),
572                              NULL, NULL,
573                              _gtk_marshal_VOID__VOID,
574                              G_TYPE_NONE, 0);
575
576   binding_set = gtk_binding_set_by_class (class);
577
578   gtk_binding_entry_add_signal (binding_set,
579                                 GDK_l, GDK_CONTROL_MASK,
580                                 "location-popup",
581                                 1, G_TYPE_STRING, "");
582
583   gtk_binding_entry_add_signal (binding_set,
584                                 GDK_slash, 0,
585                                 "location-popup",
586                                 1, G_TYPE_STRING, "/");
587
588   gtk_binding_entry_add_signal (binding_set,
589                                 GDK_Up, GDK_MOD1_MASK,
590                                 "up-folder",
591                                 0);
592   gtk_binding_entry_add_signal (binding_set,
593                                 GDK_BackSpace, 0,
594                                 "up-folder",
595                                 0);
596   gtk_binding_entry_add_signal (binding_set,
597                                 GDK_KP_Up, GDK_MOD1_MASK,
598                                 "up-folder",
599                                 0);
600
601   gtk_binding_entry_add_signal (binding_set,
602                                 GDK_Down, GDK_MOD1_MASK,
603                                 "down-folder",
604                                 0);
605   gtk_binding_entry_add_signal (binding_set,
606                                 GDK_KP_Down, GDK_MOD1_MASK,
607                                 "down-folder",
608                                 0);
609
610   gtk_binding_entry_add_signal (binding_set,
611                                 GDK_Home, GDK_MOD1_MASK,
612                                 "home-folder",
613                                 0);
614   gtk_binding_entry_add_signal (binding_set,
615                                 GDK_KP_Home, GDK_MOD1_MASK,
616                                 "home-folder",
617                                 0);
618
619   _gtk_file_chooser_install_properties (gobject_class);
620
621   gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
622                                                       P_("Default file chooser backend"),
623                                                       P_("Name of the GtkFileChooser backend to use by default"),
624                                                       NULL,
625                                                       G_PARAM_READWRITE));
626 }
627
628 static void
629 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
630 {
631   iface->select_path = gtk_file_chooser_default_select_path;
632   iface->unselect_path = gtk_file_chooser_default_unselect_path;
633   iface->select_all = gtk_file_chooser_default_select_all;
634   iface->unselect_all = gtk_file_chooser_default_unselect_all;
635   iface->get_paths = gtk_file_chooser_default_get_paths;
636   iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
637   iface->get_file_system = gtk_file_chooser_default_get_file_system;
638   iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
639   iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
640   iface->set_current_name = gtk_file_chooser_default_set_current_name;
641   iface->add_filter = gtk_file_chooser_default_add_filter;
642   iface->remove_filter = gtk_file_chooser_default_remove_filter;
643   iface->list_filters = gtk_file_chooser_default_list_filters;
644   iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
645   iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
646   iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
647 }
648
649 static void
650 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
651 {
652   iface->get_default_size = gtk_file_chooser_default_get_default_size;
653   iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
654   iface->should_respond = gtk_file_chooser_default_should_respond;
655   iface->initial_focus = gtk_file_chooser_default_initial_focus;
656 }
657 static void
658 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
659 {
660   impl->local_only = TRUE;
661   impl->preview_widget_active = TRUE;
662   impl->use_preview_label = TRUE;
663   impl->select_multiple = FALSE;
664   impl->show_hidden = FALSE;
665   impl->icon_size = FALLBACK_ICON_SIZE;
666   impl->load_state = LOAD_EMPTY;
667   impl->pending_select_paths = NULL;
668
669   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
670   gtk_box_set_spacing (GTK_BOX (impl), 12);
671
672   impl->tooltips = gtk_tooltips_new ();
673   g_object_ref (impl->tooltips);
674   gtk_object_sink (GTK_OBJECT (impl->tooltips));
675 }
676
677 /* Frees the data columns for the specified iter in the shortcuts model*/
678 static void
679 shortcuts_free_row_data (GtkFileChooserDefault *impl,
680                          GtkTreeIter           *iter)
681 {
682   gpointer col_data;
683   gboolean is_volume;
684
685   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
686                       SHORTCUTS_COL_DATA, &col_data,
687                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
688                       -1);
689   if (!col_data)
690     return;
691
692   if (is_volume)
693     {
694       GtkFileSystemVolume *volume;
695
696       volume = col_data;
697       gtk_file_system_volume_free (impl->file_system, volume);
698     }
699   else
700     {
701       GtkFilePath *path;
702
703       path = col_data;
704       gtk_file_path_free (path);
705     }
706 }
707
708 /* Frees all the data columns in the shortcuts model */
709 static void
710 shortcuts_free (GtkFileChooserDefault *impl)
711 {
712   GtkTreeIter iter;
713
714   if (!impl->shortcuts_model)
715     return;
716
717   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
718     do
719       {
720         shortcuts_free_row_data (impl, &iter);
721       }
722     while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
723
724   g_object_unref (impl->shortcuts_model);
725   impl->shortcuts_model = NULL;
726 }
727
728 static void
729 pending_select_paths_free (GtkFileChooserDefault *impl)
730 {
731   GSList *l;
732
733   for (l = impl->pending_select_paths; l; l = l->next)
734     {
735       GtkFilePath *path;
736
737       path = l->data;
738       gtk_file_path_free (path);
739     }
740
741   g_slist_free (impl->pending_select_paths);
742   impl->pending_select_paths = NULL;
743 }
744
745 static void
746 pending_select_paths_add (GtkFileChooserDefault *impl,
747                           const GtkFilePath     *path)
748 {
749   impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
750 }
751
752 /* Used from gtk_tree_selection_selected_foreach() */
753 static void
754 store_selection_foreach (GtkTreeModel *model,
755                          GtkTreePath  *path,
756                          GtkTreeIter  *iter,
757                          gpointer      data)
758 {
759   GtkFileChooserDefault *impl;
760   GtkTreeIter child_iter;
761   const GtkFilePath *file_path;
762
763   impl = GTK_FILE_CHOOSER_DEFAULT (data);
764
765   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
766
767   file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
768   pending_select_paths_add (impl, file_path);
769 }
770
771 /* Stores the current selection in the list of paths to select; this is used to
772  * preserve the selection when reloading the current folder.
773  */
774 static void
775 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
776 {
777   GtkTreeSelection *selection;
778
779   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
780   gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
781 }
782
783 static void
784 gtk_file_chooser_default_finalize (GObject *object)
785 {
786   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
787   GSList *l;
788
789   if (impl->shortcuts_filter_model)
790     g_object_unref (impl->shortcuts_filter_model);
791
792   shortcuts_free (impl);
793
794   g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
795   impl->volumes_changed_id = 0;
796   g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
797   impl->bookmarks_changed_id = 0;
798   g_object_unref (impl->file_system);
799
800   for (l = impl->filters; l; l = l->next)
801     {
802       GtkFileFilter *filter;
803
804       filter = GTK_FILE_FILTER (l->data);
805       g_object_unref (filter);
806     }
807   g_slist_free (impl->filters);
808
809   if (impl->current_filter)
810     g_object_unref (impl->current_filter);
811
812   if (impl->current_volume_path)
813     gtk_file_path_free (impl->current_volume_path);
814
815   if (impl->current_folder)
816     gtk_file_path_free (impl->current_folder);
817
818   if (impl->preview_path)
819     gtk_file_path_free (impl->preview_path);
820
821   pending_select_paths_free (impl);
822
823   load_remove_timer (impl);
824
825   /* Free all the Models we have */
826   if (impl->browse_files_model)
827     g_object_unref (impl->browse_files_model);
828
829   if (impl->sort_model)
830     g_object_unref (impl->sort_model);
831
832   g_free (impl->preview_display_name);
833
834   g_free (impl->edited_new_text);
835
836   g_object_unref (impl->tooltips);
837
838   G_OBJECT_CLASS (parent_class)->finalize (object);
839 }
840
841 /* Shows an error dialog set as transient for the specified window */
842 static void
843 error_message_with_parent (GtkWindow  *parent,
844                            const char *msg,
845                            const char *detail)
846 {
847   GtkWidget *dialog;
848
849   dialog = gtk_message_dialog_new (parent,
850                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
851                                    GTK_MESSAGE_ERROR,
852                                    GTK_BUTTONS_OK,
853                                    "%s",
854                                    msg);
855   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
856                                             "%s", detail);
857   gtk_dialog_run (GTK_DIALOG (dialog));
858   gtk_widget_destroy (dialog);
859 }
860
861 /* Returns a toplevel GtkWindow, or NULL if none */
862 static GtkWindow *
863 get_toplevel (GtkWidget *widget)
864 {
865   GtkWidget *toplevel;
866
867   toplevel = gtk_widget_get_toplevel (widget);
868   if (!GTK_WIDGET_TOPLEVEL (toplevel))
869     return NULL;
870   else
871     return GTK_WINDOW (toplevel);
872 }
873
874 /* Shows an error dialog for the file chooser */
875 static void
876 error_message (GtkFileChooserDefault *impl,
877                const char            *msg,
878                const char            *detail)
879 {
880   error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
881 }
882
883 /* Shows a simple error dialog relative to a path.  Frees the GError as well. */
884 static void
885 error_dialog (GtkFileChooserDefault *impl,
886               const char            *msg,
887               const GtkFilePath     *path,
888               GError                *error)
889 {
890   if (error)
891     {
892       char *uri = NULL;
893       char *text;
894
895       if (path)
896         uri = gtk_file_system_path_to_uri (impl->file_system, path);
897       text = g_strdup_printf (msg, uri);
898       error_message (impl, text, error->message);
899       g_free (text);
900       g_free (uri);
901       g_error_free (error);
902     }
903 }
904
905 /* Displays an error message about not being able to get information for a file.
906  * Frees the GError as well.
907  */
908 static void
909 error_getting_info_dialog (GtkFileChooserDefault *impl,
910                            const GtkFilePath     *path,
911                            GError                *error)
912 {
913   error_dialog (impl,
914                 _("Could not retrieve information about the file"),
915                 path, error);
916 }
917
918 /* Shows an error dialog about not being able to add a bookmark */
919 static void
920 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
921                               const GtkFilePath     *path,
922                               GError                *error)
923 {
924   error_dialog (impl,
925                 _("Could not add a bookmark"),
926                 path, error);
927 }
928
929 /* Shows an error dialog about not being able to remove a bookmark */
930 static void
931 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
932                                 const GtkFilePath     *path,
933                                 GError                *error)
934 {
935   error_dialog (impl,
936                 _("Could not remove bookmark"),
937                 path, error);
938 }
939
940 /* Shows an error dialog about not being able to create a folder */
941 static void
942 error_creating_folder_dialog (GtkFileChooserDefault *impl,
943                               const GtkFilePath     *path,
944                               GError                *error)
945 {
946   error_dialog (impl, 
947                 _("The folder could not be created"), 
948                 path, error);
949 }
950
951 /* Shows an error dialog about not being able to create a filename */
952 static void
953 error_building_filename_dialog (GtkFileChooserDefault *impl,
954                                 const GtkFilePath     *folder_part,
955                                 const char            *file_part,
956                                 GError                *error)
957 {
958   error_dialog (impl, _("Invalid file name"), 
959                 NULL, error);
960 }
961
962 /* Shows an error dialog when we cannot switch to a folder */
963 static void
964 error_changing_folder_dialog (GtkFileChooserDefault *impl,
965                               const GtkFilePath     *path,
966                               GError                *error)
967 {
968   error_dialog (impl, _("The folder contents could not be displayed"),
969                 path, error);
970 }
971
972 /* Changes folders, displaying an error dialog if this fails */
973 static gboolean
974 change_folder_and_display_error (GtkFileChooserDefault *impl,
975                                  const GtkFilePath     *path)
976 {
977   GError *error;
978   gboolean result;
979   GtkFilePath *path_copy;
980
981   /* We copy the path because of this case:
982    *
983    * list_row_activated()
984    *   fetches path from model; path belongs to the model (*)
985    *   calls change_folder_and_display_error()
986    *     calls _gtk_file_chooser_set_current_folder_path()
987    *       changing folders fails, sets model to NULL, thus freeing the path in (*)
988    */
989
990   path_copy = gtk_file_path_copy (path);
991
992   error = NULL;
993   result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
994
995   if (!result)
996     error_changing_folder_dialog (impl, path_copy, error);
997
998   gtk_file_path_free (path_copy);
999
1000   return result;
1001 }
1002
1003 static void
1004 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1005 {
1006   if (impl->use_preview_label)
1007     {
1008       if (!impl->preview_label)
1009         {
1010           impl->preview_label = gtk_label_new (impl->preview_display_name);
1011           gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1012           gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1013           gtk_widget_show (impl->preview_label);
1014         }
1015     }
1016   else
1017     {
1018       if (impl->preview_label)
1019         {
1020           gtk_widget_destroy (impl->preview_label);
1021           impl->preview_label = NULL;
1022         }
1023     }
1024
1025   if (impl->preview_widget_active && impl->preview_widget)
1026     gtk_widget_show (impl->preview_box);
1027   else
1028     gtk_widget_hide (impl->preview_box);
1029
1030   g_signal_emit_by_name (impl, "default-size-changed");
1031 }
1032
1033 static void
1034 set_preview_widget (GtkFileChooserDefault *impl,
1035                     GtkWidget             *preview_widget)
1036 {
1037   if (preview_widget == impl->preview_widget)
1038     return;
1039
1040   if (impl->preview_widget)
1041     gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1042                           impl->preview_widget);
1043
1044   impl->preview_widget = preview_widget;
1045   if (impl->preview_widget)
1046     {
1047       gtk_widget_show (impl->preview_widget);
1048       gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1049       gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1050                              impl->preview_widget,
1051                              (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1052     }
1053
1054   update_preview_widget_visibility (impl);
1055 }
1056
1057 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1058 static void
1059 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1060 {
1061   GtkTreeIter iter;
1062
1063   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1064     return;
1065
1066   do {
1067     gpointer data;
1068     gboolean is_volume;
1069     gboolean pixbuf_visible;
1070     GdkPixbuf *pixbuf;
1071
1072     gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1073                         SHORTCUTS_COL_DATA, &data,
1074                         SHORTCUTS_COL_IS_VOLUME, &is_volume,
1075                         SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1076                         -1);
1077
1078     if (pixbuf_visible && data)
1079       {
1080         if (is_volume)
1081           {
1082             GtkFileSystemVolume *volume;
1083
1084             volume = data;
1085             pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1086                                                          impl->icon_size, NULL);
1087           }
1088         else
1089           {
1090             const GtkFilePath *path;
1091
1092             path = data;
1093             pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1094                                                   impl->icon_size, NULL);
1095           }
1096
1097         gtk_list_store_set (impl->shortcuts_model, &iter,
1098                             SHORTCUTS_COL_PIXBUF, pixbuf,
1099                             -1);
1100         if (pixbuf)
1101           g_object_unref (pixbuf);
1102       }
1103   } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1104 }
1105
1106 static void 
1107 shortcuts_find_folder (GtkFileChooserDefault *impl,
1108                        GtkFilePath           *folder)
1109 {
1110   GtkTreeSelection *selection;
1111   int pos;
1112   GtkTreePath *path;
1113
1114   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1115
1116   g_assert (folder != NULL);
1117   pos = shortcut_find_position (impl, folder);
1118   if (pos == -1)
1119     {
1120       gtk_tree_selection_unselect_all (selection);
1121       return;
1122     }
1123
1124   path = gtk_tree_path_new_from_indices (pos, -1);
1125   gtk_tree_selection_select_path (selection, path);
1126   gtk_tree_path_free (path);
1127 }
1128
1129 /* If a shortcut corresponds to the current folder, selects it */
1130 static void
1131 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1132 {
1133   shortcuts_find_folder (impl, impl->current_folder);
1134 }
1135
1136 /* Convenience function to get the display name and icon info for a path */
1137 static GtkFileInfo *
1138 get_file_info (GtkFileSystem      *file_system, 
1139                const GtkFilePath  *path, 
1140                gboolean            name_only, 
1141                GError            **error)
1142 {
1143   GtkFilePath *parent_path;
1144   GtkFileFolder *parent_folder;
1145   GtkFileInfo *info;
1146   GError *tmp = NULL;
1147
1148   parent_path = NULL;
1149   info = NULL;
1150
1151   if (!gtk_file_system_get_parent (file_system, path, &parent_path, &tmp))
1152     goto out;
1153
1154   parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1155                                               GTK_FILE_INFO_DISPLAY_NAME
1156                                               | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1157                                               &tmp);
1158   if (!parent_folder)
1159     goto out;
1160
1161   info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, &tmp);
1162   g_object_unref (parent_folder);
1163
1164  out:
1165   if (parent_path)
1166     gtk_file_path_free (parent_path);
1167
1168   if (tmp)
1169     {
1170       g_set_error (error,
1171                    GTK_FILE_CHOOSER_ERROR,
1172                    GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
1173                    _("Could not get information about '%s': %s"), 
1174                    gtk_file_path_get_string (path),
1175                    tmp->message);
1176       g_error_free (tmp);
1177     }
1178
1179   return info;
1180 }
1181
1182 /* Returns whether a path is a folder */
1183 static gboolean
1184 check_is_folder (GtkFileSystem      *file_system, 
1185                  const GtkFilePath  *path, 
1186                  GError            **error)
1187 {
1188   GtkFileFolder *folder;
1189
1190   folder = gtk_file_system_get_folder (file_system, path, 0, error);
1191   if (!folder)
1192     return FALSE;
1193
1194   g_object_unref (folder);
1195   return TRUE;
1196 }
1197
1198 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1199  * inserts a volume.  A position of -1 indicates the end of the tree.
1200  */
1201 static gboolean
1202 shortcuts_insert_path (GtkFileChooserDefault *impl,
1203                        int                    pos,
1204                        gboolean               is_volume,
1205                        GtkFileSystemVolume   *volume,
1206                        const GtkFilePath     *path,
1207                        const char            *label,
1208                        gboolean               removable,
1209                        GError               **error)
1210 {
1211   char *label_copy;
1212   GdkPixbuf *pixbuf;
1213   gpointer data;
1214   GtkTreeIter iter;
1215
1216   if (is_volume)
1217     {
1218       data = volume;
1219       label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1220       pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1221                                                    impl->icon_size, NULL);
1222     }
1223   else
1224     {
1225       if (!check_is_folder (impl->file_system, path, error))
1226         return FALSE;
1227
1228       if (label)
1229         label_copy = g_strdup (label);
1230       else
1231         {
1232           GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1233
1234           if (!info)
1235             return FALSE;
1236
1237           label_copy = g_strdup (gtk_file_info_get_display_name (info));
1238           gtk_file_info_free (info);
1239         }
1240
1241       data = gtk_file_path_copy (path);
1242       pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1243                                             impl->icon_size, NULL);
1244     }
1245
1246   if (pos == -1)
1247     gtk_list_store_append (impl->shortcuts_model, &iter);
1248   else
1249     gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1250
1251   gtk_list_store_set (impl->shortcuts_model, &iter,
1252                       SHORTCUTS_COL_PIXBUF, pixbuf,
1253                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1254                       SHORTCUTS_COL_NAME, label_copy,
1255                       SHORTCUTS_COL_DATA, data,
1256                       SHORTCUTS_COL_IS_VOLUME, is_volume,
1257                       SHORTCUTS_COL_REMOVABLE, removable,
1258                       -1);
1259
1260   g_free (label_copy);
1261
1262   if (pixbuf)
1263     g_object_unref (pixbuf);
1264
1265   return TRUE;
1266 }
1267
1268 /* Appends an item for the user's home directory to the shortcuts model */
1269 static void
1270 shortcuts_append_home (GtkFileChooserDefault *impl)
1271 {
1272   const char *home;
1273   GtkFilePath *home_path;
1274   GError *error;
1275
1276   home = g_get_home_dir ();
1277   if (home == NULL)
1278     return;
1279
1280   home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1281
1282   error = NULL;
1283   impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1284   if (!impl->has_home)
1285     error_getting_info_dialog (impl, home_path, error);
1286
1287   gtk_file_path_free (home_path);
1288 }
1289
1290 /* Appends the ~/Desktop directory to the shortcuts model */
1291 static void
1292 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1293 {
1294   char *name;
1295   GtkFilePath *path;
1296
1297 #ifdef G_OS_WIN32
1298   name = _gtk_file_system_win32_get_desktop ();
1299 #else
1300   const char *home = g_get_home_dir ();
1301   if (home == NULL)
1302     return;
1303
1304   name = g_build_filename (home, "Desktop", NULL);
1305 #endif
1306
1307   path = gtk_file_system_filename_to_path (impl->file_system, name);
1308   g_free (name);
1309
1310   impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1311   /* We do not actually pop up an error dialog if there is no desktop directory
1312    * because some people may really not want to have one.
1313    */
1314
1315   gtk_file_path_free (path);
1316 }
1317
1318 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1319 static int
1320 shortcuts_append_paths (GtkFileChooserDefault *impl,
1321                         GSList                *paths)
1322 {
1323   int start_row;
1324   int num_inserted;
1325
1326   /* As there is no separator now, we want to start there.
1327    */
1328   start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1329   num_inserted = 0;
1330
1331   for (; paths; paths = paths->next)
1332     {
1333       GtkFilePath *path;
1334       GError *error;
1335
1336       path = paths->data;
1337       error = NULL;
1338
1339       if (impl->local_only &&
1340           !gtk_file_system_path_is_local (impl->file_system, path))
1341         continue;
1342
1343       /* NULL GError, but we don't really want to show error boxes here */
1344       if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1345         num_inserted++;
1346     }
1347
1348   return num_inserted;
1349 }
1350
1351 /* Returns the index for the corresponding item in the shortcuts bar */
1352 static int
1353 shortcuts_get_index (GtkFileChooserDefault *impl,
1354                      ShortcutsIndex         where)
1355 {
1356   int n;
1357
1358   n = 0;
1359
1360   if (where == SHORTCUTS_HOME)
1361     goto out;
1362
1363   n += impl->has_home ? 1 : 0;
1364
1365   if (where == SHORTCUTS_DESKTOP)
1366     goto out;
1367
1368   n += impl->has_desktop ? 1 : 0;
1369
1370   if (where == SHORTCUTS_VOLUMES)
1371     goto out;
1372
1373   n += impl->num_volumes;
1374
1375   if (where == SHORTCUTS_SHORTCUTS)
1376     goto out;
1377
1378   n += impl->num_shortcuts;
1379
1380   if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1381     goto out;
1382
1383   /* If there are no bookmarks there won't be a separator */
1384   n += (impl->num_bookmarks > 0) ? 1 : 0;
1385
1386   if (where == SHORTCUTS_BOOKMARKS)
1387     goto out;
1388
1389   n += impl->num_bookmarks;
1390
1391   if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1392     goto out;
1393
1394   n += 1;
1395
1396   if (where == SHORTCUTS_CURRENT_FOLDER)
1397     goto out;
1398
1399   g_assert_not_reached ();
1400
1401  out:
1402
1403   return n;
1404 }
1405
1406 /* Removes the specified number of rows from the shortcuts list */
1407 static void
1408 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1409                        int                    start_row,
1410                        int                    n_rows)
1411 {
1412   GtkTreePath *path;
1413
1414   path = gtk_tree_path_new_from_indices (start_row, -1);
1415
1416   for (; n_rows; n_rows--)
1417     {
1418       GtkTreeIter iter;
1419
1420       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1421         g_assert_not_reached ();
1422
1423       shortcuts_free_row_data (impl, &iter);
1424       gtk_list_store_remove (impl->shortcuts_model, &iter);
1425     }
1426
1427   gtk_tree_path_free (path);
1428 }
1429
1430 /* Adds all the file system volumes to the shortcuts model */
1431 static void
1432 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1433 {
1434   int start_row;
1435   GSList *list, *l;
1436   int n;
1437   gboolean old_changing_folders;
1438
1439   old_changing_folders = impl->changing_folder;
1440   impl->changing_folder = TRUE;
1441
1442   start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1443   shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1444   impl->num_volumes = 0;
1445
1446   list = gtk_file_system_list_volumes (impl->file_system);
1447
1448   n = 0;
1449
1450   for (l = list; l; l = l->next)
1451     {
1452       GtkFileSystemVolume *volume;
1453
1454       volume = l->data;
1455
1456       if (impl->local_only)
1457         {
1458           GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1459           gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1460           gtk_file_path_free (base_path);
1461
1462           if (!is_local)
1463             {
1464               gtk_file_system_volume_free (impl->file_system, volume);
1465               continue;
1466             }
1467         }
1468
1469       if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1470         n++;
1471       else
1472         gtk_file_system_volume_free (impl->file_system, volume);
1473     }
1474
1475   impl->num_volumes = n;
1476   g_slist_free (list);
1477
1478   if (impl->shortcuts_filter_model)
1479     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1480
1481   impl->changing_folder = old_changing_folders;
1482 }
1483
1484 /* Inserts a separator node in the shortcuts list */
1485 static void
1486 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1487                             ShortcutsIndex where)
1488 {
1489   GtkTreeIter iter;
1490
1491   g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1492
1493   gtk_list_store_insert (impl->shortcuts_model, &iter,
1494                          shortcuts_get_index (impl, where));
1495   gtk_list_store_set (impl->shortcuts_model, &iter,
1496                       SHORTCUTS_COL_PIXBUF, NULL,
1497                       SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1498                       SHORTCUTS_COL_NAME, NULL,
1499                       SHORTCUTS_COL_DATA, NULL,
1500                       -1);
1501 }
1502
1503 /* Updates the list of bookmarks */
1504 static void
1505 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1506 {
1507   GSList *bookmarks;
1508   gboolean old_changing_folders;
1509   GtkTreeIter iter;
1510   GtkFilePath *list_selected = NULL;
1511   GtkFilePath *combo_selected = NULL;
1512   gboolean is_volume;
1513   gpointer col_data;
1514         
1515   old_changing_folders = impl->changing_folder;
1516   impl->changing_folder = TRUE;
1517
1518   if (shortcuts_get_selected (impl, &iter))
1519     {
1520       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), 
1521                           &iter, 
1522                           SHORTCUTS_COL_DATA, &col_data,
1523                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
1524                           -1);
1525
1526       if (col_data && !is_volume)
1527         list_selected = gtk_file_path_copy (col_data);
1528     }
1529
1530   if (impl->save_folder_combo &&
1531       gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo), 
1532                                      &iter))
1533     {
1534       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), 
1535                           &iter, 
1536                           SHORTCUTS_COL_DATA, &col_data,
1537                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
1538                           -1);
1539       
1540       if (col_data && !is_volume)
1541         combo_selected = gtk_file_path_copy (col_data);
1542     }
1543
1544   if (impl->num_bookmarks > 0)
1545     shortcuts_remove_rows (impl,
1546                            shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1547                            impl->num_bookmarks + 1);
1548
1549   bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1550   impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1551   gtk_file_paths_free (bookmarks);
1552
1553   if (impl->num_bookmarks > 0)
1554     shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1555
1556   if (impl->shortcuts_filter_model)
1557     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1558
1559   if (list_selected)
1560     {
1561       shortcuts_find_folder (impl, list_selected);
1562       gtk_file_path_free (list_selected);
1563     }
1564
1565   if (combo_selected)
1566     {
1567       gint pos;
1568
1569       pos = shortcut_find_position (impl, combo_selected);
1570       if (pos != -1)
1571         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), 
1572                                   pos);
1573       gtk_file_path_free (combo_selected);
1574     }
1575   
1576   impl->changing_folder = old_changing_folders;
1577 }
1578
1579 /* Appends a separator and a row to the shortcuts list for the current folder */
1580 static void
1581 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1582 {
1583   int pos;
1584   gboolean success;
1585
1586   g_assert (!impl->shortcuts_current_folder_active);
1587
1588   success = TRUE;
1589
1590   g_assert (impl->current_folder != NULL);
1591
1592   pos = shortcut_find_position (impl, impl->current_folder);
1593   if (pos == -1)
1594     {
1595       GtkFileSystemVolume *volume;
1596       GtkFilePath *base_path;
1597
1598       /* Separator */
1599
1600       shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1601
1602       /* Item */
1603
1604       pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1605
1606       volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1607       if (volume)
1608         base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1609       else
1610         base_path = NULL;
1611
1612       if (base_path &&
1613           strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1614         {
1615           success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1616           if (success)
1617             volume = NULL;
1618         }
1619       else
1620         success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1621
1622       if (volume)
1623         gtk_file_system_volume_free (impl->file_system, volume);
1624
1625       if (base_path)
1626         gtk_file_path_free (base_path);
1627
1628       if (!success)
1629         shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1630
1631       impl->shortcuts_current_folder_active = success;
1632     }
1633
1634   if (success)
1635     gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1636 }
1637
1638 /* Updates the current folder row in the shortcuts model */
1639 static void
1640 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1641 {
1642   int pos;
1643
1644   pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1645
1646   if (impl->shortcuts_current_folder_active)
1647     {
1648       shortcuts_remove_rows (impl, pos, 2);
1649       impl->shortcuts_current_folder_active = FALSE;
1650     }
1651
1652   shortcuts_add_current_folder (impl);
1653 }
1654
1655 /* Filter function used for the shortcuts filter model */
1656 static gboolean
1657 shortcuts_filter_cb (GtkTreeModel          *model,
1658                      GtkTreeIter           *iter,
1659                      gpointer               data)
1660 {
1661   GtkFileChooserDefault *impl;
1662   GtkTreePath *path;
1663   int pos;
1664
1665   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1666
1667   path = gtk_tree_model_get_path (model, iter);
1668   if (!path)
1669     return FALSE;
1670
1671   pos = *gtk_tree_path_get_indices (path);
1672   gtk_tree_path_free (path);
1673
1674   return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1675 }
1676
1677 /* Creates the list model for shortcuts */
1678 static void
1679 shortcuts_model_create (GtkFileChooserDefault *impl)
1680 {
1681   /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1682   impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1683                                               GDK_TYPE_PIXBUF,  /* pixbuf */
1684                                               G_TYPE_STRING,    /* name */
1685                                               G_TYPE_POINTER,   /* path or volume */
1686                                               G_TYPE_BOOLEAN,   /* is the previous column a volume? */
1687                                               G_TYPE_BOOLEAN,   /* removable */
1688                                               G_TYPE_BOOLEAN);  /* pixbuf cell visibility */
1689
1690   if (impl->file_system)
1691     {
1692       shortcuts_append_home (impl);
1693       shortcuts_append_desktop (impl);
1694       shortcuts_add_volumes (impl);
1695       shortcuts_add_bookmarks (impl);
1696     }
1697
1698   impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1699                                                              GTK_TREE_MODEL (impl->shortcuts_model),
1700                                                              NULL);
1701
1702   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1703                                           shortcuts_filter_cb,
1704                                           impl,
1705                                           NULL);
1706 }
1707
1708 /* Callback used when the "New Folder" button is clicked */
1709 static void
1710 new_folder_button_clicked (GtkButton             *button,
1711                            GtkFileChooserDefault *impl)
1712 {
1713   GtkTreeIter iter;
1714   GtkTreePath *path;
1715
1716   if (!impl->browse_files_model)
1717     return; /* FIXME: this sucks.  Disable the New Folder button or something. */
1718
1719   /* Prevent button from being clicked twice */
1720   gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
1721
1722   _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1723
1724   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1725   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1726                                 path, impl->list_name_column,
1727                                 FALSE, 0.0, 0.0);
1728
1729   g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1730   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1731                             path,
1732                             impl->list_name_column,
1733                             TRUE);
1734
1735   gtk_tree_path_free (path);
1736 }
1737
1738 /* Idle handler for creating a new folder after editing its name cell, or for
1739  * canceling the editing.
1740  */
1741 static gboolean
1742 edited_idle_cb (GtkFileChooserDefault *impl)
1743 {
1744   GDK_THREADS_ENTER ();
1745   
1746   g_source_destroy (impl->edited_idle);
1747   impl->edited_idle = NULL;
1748
1749   _gtk_file_system_model_remove_editable (impl->browse_files_model);
1750   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1751
1752   gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
1753
1754   if (impl->edited_new_text) /* not cancelled? */
1755     {
1756       GError *error;
1757       GtkFilePath *file_path;
1758
1759       error = NULL;
1760       file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1761                                              &error);
1762       if (file_path)
1763         {
1764           error = NULL;
1765           if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1766             change_folder_and_display_error (impl, file_path);
1767           else
1768             error_creating_folder_dialog (impl, file_path, error);
1769
1770           gtk_file_path_free (file_path);
1771         }
1772       else
1773         error_creating_folder_dialog (impl, file_path, error);
1774
1775       g_free (impl->edited_new_text);
1776       impl->edited_new_text = NULL;
1777     }
1778
1779   GDK_THREADS_LEAVE ();
1780
1781   return FALSE;
1782 }
1783
1784 static void
1785 queue_edited_idle (GtkFileChooserDefault *impl,
1786                    const gchar           *new_text)
1787 {
1788   /* We create the folder in an idle handler so that we don't modify the tree
1789    * just now.
1790    */
1791
1792   g_assert (!impl->edited_idle);
1793   g_assert (!impl->edited_new_text);
1794
1795   impl->edited_idle = g_idle_source_new ();
1796   g_source_set_closure (impl->edited_idle,
1797                         g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1798                                                G_OBJECT (impl)));
1799   g_source_attach (impl->edited_idle, NULL);
1800
1801   if (new_text)
1802     impl->edited_new_text = g_strdup (new_text);
1803 }
1804
1805 /* Callback used from the text cell renderer when the new folder is named */
1806 static void
1807 renderer_edited_cb (GtkCellRendererText   *cell_renderer_text,
1808                     const gchar           *path,
1809                     const gchar           *new_text,
1810                     GtkFileChooserDefault *impl)
1811 {
1812  /* work around bug #154921 */
1813   g_object_set (cell_renderer_text, 
1814                 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1815    queue_edited_idle (impl, new_text);
1816 }
1817
1818 /* Callback used from the text cell renderer when the new folder edition gets
1819  * canceled.
1820  */
1821 static void
1822 renderer_editing_canceled_cb (GtkCellRendererText   *cell_renderer_text,
1823                               GtkFileChooserDefault *impl)
1824 {
1825  /* work around bug #154921 */
1826   g_object_set (cell_renderer_text, 
1827                 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1828    queue_edited_idle (impl, NULL);
1829 }
1830
1831 /* Creates the widgets for the filter combo box */
1832 static GtkWidget *
1833 filter_create (GtkFileChooserDefault *impl)
1834 {
1835   impl->filter_combo = gtk_combo_box_new_text ();
1836   g_signal_connect (impl->filter_combo, "changed",
1837                     G_CALLBACK (filter_combo_changed), impl);
1838
1839   return impl->filter_combo;
1840 }
1841
1842 static GtkWidget *
1843 button_new (GtkFileChooserDefault *impl,
1844             const char *text,
1845             const char *stock_id,
1846             gboolean    sensitive,
1847             gboolean    show,
1848             GCallback   callback)
1849 {
1850   GtkWidget *button;
1851   GtkWidget *hbox;
1852   GtkWidget *widget;
1853   GtkWidget *align;
1854
1855   button = gtk_button_new ();
1856   hbox = gtk_hbox_new (FALSE, 2);
1857   align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1858
1859   gtk_container_add (GTK_CONTAINER (button), align);
1860   gtk_container_add (GTK_CONTAINER (align), hbox);
1861   widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1862
1863   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1864
1865   widget = gtk_label_new_with_mnemonic (text);
1866   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1867   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1868
1869   gtk_widget_set_sensitive (button, sensitive);
1870   g_signal_connect (button, "clicked", callback, impl);
1871
1872   gtk_widget_show_all (align);
1873
1874   if (show)
1875     gtk_widget_show (button);
1876
1877   return button;
1878 }
1879
1880 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1881 static int
1882 shortcut_find_position (GtkFileChooserDefault *impl,
1883                         const GtkFilePath     *path)
1884 {
1885   GtkTreeIter iter;
1886   int i;
1887   int current_folder_separator_idx;
1888
1889   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1890     return -1;
1891
1892   current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1893
1894   for (i = 0; i < current_folder_separator_idx; i++)
1895     {
1896       gpointer col_data;
1897       gboolean is_volume;
1898
1899       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1900                           SHORTCUTS_COL_DATA, &col_data,
1901                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
1902                           -1);
1903
1904       if (col_data)
1905         {
1906           if (is_volume)
1907             {
1908               GtkFileSystemVolume *volume;
1909               GtkFilePath *base_path;
1910               gboolean exists;
1911
1912               volume = col_data;
1913               base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1914
1915               exists = strcmp (gtk_file_path_get_string (path),
1916                                gtk_file_path_get_string (base_path)) == 0;
1917               g_free (base_path);
1918
1919               if (exists)
1920                 return i;
1921             }
1922           else
1923             {
1924               GtkFilePath *model_path;
1925
1926               model_path = col_data;
1927
1928               if (model_path && gtk_file_path_compare (model_path, path) == 0)
1929                 return i;
1930             }
1931         }
1932
1933       gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1934     }
1935
1936   return -1;
1937 }
1938
1939 /* Tries to add a bookmark from a path name */
1940 static gboolean
1941 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1942                                   const GtkFilePath     *path,
1943                                   int                    pos)
1944 {
1945   GError *error;
1946
1947   if (shortcut_find_position (impl, path) != -1)
1948     return FALSE;
1949
1950   /* FIXME: this check really belongs in gtk_file_system_insert_bookmark.  */
1951   error = NULL;
1952   if (!check_is_folder (impl->file_system, path, &error))
1953     {
1954       error_adding_bookmark_dialog (impl, path, error);
1955       return FALSE;
1956     }
1957
1958   error = NULL;
1959   if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1960     {
1961       error_adding_bookmark_dialog (impl, path, error);
1962       return FALSE;
1963     }
1964
1965   return TRUE;
1966 }
1967
1968 static void
1969 add_bookmark_foreach_cb (GtkTreeModel *model,
1970                          GtkTreePath  *path,
1971                          GtkTreeIter  *iter,
1972                          gpointer      data)
1973 {
1974   GtkFileChooserDefault *impl;
1975   GtkFileSystemModel *fs_model;
1976   GtkTreeIter child_iter;
1977   const GtkFilePath *file_path;
1978
1979   impl = (GtkFileChooserDefault *) data;
1980
1981   fs_model = impl->browse_files_model;
1982   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1983
1984   file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1985   shortcuts_add_bookmark_from_path (impl, file_path, -1);
1986 }
1987
1988 /* Adds a bookmark from the currently selected item in the file list */
1989 static void
1990 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
1991 {
1992   GtkTreeSelection *selection;
1993
1994   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1995
1996   if (gtk_tree_selection_count_selected_rows (selection) == 0)
1997     shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1998   else
1999     gtk_tree_selection_selected_foreach (selection,
2000                                          add_bookmark_foreach_cb,
2001                                          impl);
2002 }
2003
2004 /* Callback used when the "Add bookmark" button is clicked */
2005 static void
2006 add_bookmark_button_clicked_cb (GtkButton *button,
2007                                 GtkFileChooserDefault *impl)
2008 {
2009   bookmarks_add_selected_folder (impl);
2010 }
2011
2012 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2013  * returns FALSE if no shortcut is selected.
2014  */
2015 static gboolean
2016 shortcuts_get_selected (GtkFileChooserDefault *impl,
2017                         GtkTreeIter           *iter)
2018 {
2019   GtkTreeSelection *selection;
2020   GtkTreeIter parent_iter;
2021
2022   if (!impl->browse_shortcuts_tree_view)
2023     return FALSE;
2024
2025   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2026
2027   if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2028     return FALSE;
2029
2030   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2031                                                     iter,
2032                                                     &parent_iter);
2033   return TRUE;
2034 }
2035
2036 /* Removes the selected bookmarks */
2037 static void
2038 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2039 {
2040   GtkTreeIter iter;
2041   gpointer col_data;
2042   gboolean is_volume;
2043   GtkFilePath *path;
2044   gboolean removable;
2045   GError *error;
2046
2047   if (!shortcuts_get_selected (impl, &iter))
2048     return;
2049
2050   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2051                       SHORTCUTS_COL_DATA, &col_data,
2052                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
2053                       SHORTCUTS_COL_REMOVABLE, &removable,
2054                       -1);
2055   g_assert (col_data != NULL);
2056   g_assert (!is_volume);
2057
2058   if (!removable)
2059     return;
2060
2061   path = col_data;
2062
2063   error = NULL;
2064   if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2065     error_removing_bookmark_dialog (impl, path, error);
2066 }
2067
2068 /* Callback used when the "Remove bookmark" button is clicked */
2069 static void
2070 remove_bookmark_button_clicked_cb (GtkButton *button,
2071                                    GtkFileChooserDefault *impl)
2072 {
2073   remove_selected_bookmarks (impl);
2074 }
2075
2076 struct selection_check_closure {
2077   GtkFileChooserDefault *impl;
2078   int num_selected;
2079   gboolean all_files;
2080   gboolean all_folders;
2081 };
2082
2083 /* Used from gtk_tree_selection_selected_foreach() */
2084 static void
2085 selection_check_foreach_cb (GtkTreeModel *model,
2086                             GtkTreePath  *path,
2087                             GtkTreeIter  *iter,
2088                             gpointer      data)
2089 {
2090   struct selection_check_closure *closure;
2091   GtkTreeIter child_iter;
2092   const GtkFileInfo *info;
2093   gboolean is_folder;
2094
2095   closure = data;
2096   closure->num_selected++;
2097
2098   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2099
2100   info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2101   is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2102
2103   closure->all_folders = closure->all_folders && is_folder;
2104   closure->all_files = closure->all_files && !is_folder;
2105 }
2106
2107 /* Checks whether the selected items in the file list are all files or all folders */
2108 static void
2109 selection_check (GtkFileChooserDefault *impl,
2110                  gint                  *num_selected,
2111                  gboolean              *all_files,
2112                  gboolean              *all_folders)
2113 {
2114   struct selection_check_closure closure;
2115   GtkTreeSelection *selection;
2116
2117   closure.impl = impl;
2118   closure.num_selected = 0;
2119   closure.all_files = TRUE;
2120   closure.all_folders = TRUE;
2121
2122   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2123   gtk_tree_selection_selected_foreach (selection,
2124                                        selection_check_foreach_cb,
2125                                        &closure);
2126
2127   g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2128
2129   if (num_selected)
2130     *num_selected = closure.num_selected;
2131
2132   if (all_files)
2133     *all_files = closure.all_files;
2134
2135   if (all_folders)
2136     *all_folders = closure.all_folders;
2137 }
2138
2139 struct get_selected_path_closure {
2140   GtkFileChooserDefault *impl;
2141   const GtkFilePath *path;
2142 };
2143
2144 static void
2145 get_selected_path_foreach_cb (GtkTreeModel *model,
2146                               GtkTreePath  *path,
2147                               GtkTreeIter  *iter,
2148                               gpointer      data)
2149 {
2150   struct get_selected_path_closure *closure;
2151   GtkTreeIter child_iter;
2152
2153   closure = data;
2154
2155   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2156   closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2157 }
2158
2159 /* Returns a selected path from the file list */
2160 static const GtkFilePath *
2161 get_selected_path (GtkFileChooserDefault *impl)
2162 {
2163   struct get_selected_path_closure closure;
2164   GtkTreeSelection *selection;
2165
2166   closure.impl = impl;
2167   closure.path = NULL;
2168
2169   selection =  gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2170   gtk_tree_selection_selected_foreach (selection,
2171                                        get_selected_path_foreach_cb,
2172                                        &closure);
2173
2174   return closure.path;
2175 }
2176
2177 typedef struct {
2178   GtkFileChooserDefault *impl;
2179   gchar *tip;
2180 } UpdateTooltipData;
2181
2182 static void 
2183 update_tooltip (GtkTreeModel      *model,
2184                 GtkTreePath       *path,
2185                 GtkTreeIter       *iter,
2186                 gpointer           data)
2187 {
2188   UpdateTooltipData *udata = data;
2189   GtkTreeIter child_iter;
2190   const GtkFileInfo *info;
2191
2192   if (udata->tip == NULL)
2193     {
2194       gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2195                                                       &child_iter,
2196                                                       iter);
2197   
2198       info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2199       udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2200                                     gtk_file_info_get_display_name (info));
2201     }
2202 }
2203
2204
2205 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2206  * if there are no selected items *and* the current folder is not in the
2207  * bookmarks list.  De-sensitize the button otherwise.
2208  */
2209 static void
2210 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2211 {
2212   gint num_selected;
2213   gboolean all_folders;
2214   gboolean active;
2215   gchar *tip;
2216
2217   selection_check (impl, &num_selected, NULL, &all_folders);
2218
2219   if (num_selected == 0)
2220     active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2221   else if (num_selected == 1)
2222     {
2223       const GtkFilePath *path;
2224
2225       path = get_selected_path (impl);
2226       active = all_folders && (shortcut_find_position (impl, path) == -1);
2227     }
2228   else
2229     active = all_folders;
2230
2231   gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2232
2233   if (impl->browse_files_popup_menu_add_shortcut_item)
2234     gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2235                               (num_selected == 0) ? FALSE : active);
2236
2237   if (active)
2238     {
2239       if (num_selected == 0)
2240         tip = g_strdup_printf (_("Add the current folder to the bookmarks"));    
2241       else if (num_selected > 1)
2242         tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2243       else
2244         {
2245           GtkTreeSelection *selection;
2246           UpdateTooltipData data;
2247           
2248           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2249           data.impl = impl;
2250           data.tip = NULL;
2251           gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2252           tip = data.tip;
2253           
2254         }
2255       gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2256       g_free (tip);
2257     }
2258 }
2259
2260 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2261  * bookmark row is selected in the shortcuts tree.
2262  */
2263 static void
2264 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2265 {
2266   GtkTreeIter iter;
2267   gboolean removable = FALSE;
2268   gchar *name = NULL;
2269   
2270   if (shortcuts_get_selected (impl, &iter))
2271     gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2272                         SHORTCUTS_COL_REMOVABLE, &removable,
2273                         SHORTCUTS_COL_NAME, &name,
2274                         -1);
2275
2276   gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2277
2278   if (removable)
2279     {
2280       gchar *tip;
2281
2282       tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2283       gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2284                             tip, NULL);
2285       g_free (tip);
2286     }
2287
2288   g_free (name);
2289 }
2290
2291 /* GtkWidget::drag-begin handler for the shortcuts list. */
2292 static void
2293 shortcuts_drag_begin_cb (GtkWidget             *widget,
2294                          GdkDragContext        *context,
2295                          GtkFileChooserDefault *impl)
2296 {
2297 #if 0
2298   impl->shortcuts_drag_context = g_object_ref (context);
2299 #endif
2300 }
2301
2302 #if 0
2303 /* Removes the idle handler for outside drags */
2304 static void
2305 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2306 {
2307   if (!impl->shortcuts_drag_outside_idle)
2308     return;
2309
2310   g_source_destroy (impl->shortcuts_drag_outside_idle);
2311   impl->shortcuts_drag_outside_idle = NULL;
2312 }
2313 #endif
2314
2315 /* GtkWidget::drag-end handler for the shortcuts list. */
2316 static void
2317 shortcuts_drag_end_cb (GtkWidget             *widget,
2318                        GdkDragContext        *context,
2319                        GtkFileChooserDefault *impl)
2320 {
2321 #if 0
2322   g_object_unref (impl->shortcuts_drag_context);
2323
2324   shortcuts_cancel_drag_outside_idle (impl);
2325
2326   if (!impl->shortcuts_drag_outside)
2327     return;
2328
2329   gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2330
2331   impl->shortcuts_drag_outside = FALSE;
2332 #endif
2333 }
2334
2335 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2336 static void
2337 shortcuts_drag_data_delete_cb (GtkWidget             *widget,
2338                                GdkDragContext        *context,
2339                                GtkFileChooserDefault *impl)
2340 {
2341   g_signal_stop_emission_by_name (widget, "drag-data-delete");
2342 }
2343
2344 #if 0
2345 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2346  * deleted or not.
2347  */
2348 static void
2349 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2350                                   gboolean               delete)
2351 {
2352   GtkTreeView *tree_view;
2353   GtkTreeIter iter;
2354   GtkTreePath *path;
2355   GdkPixmap *row_pixmap;
2356   GdkBitmap *mask;
2357   int row_pixmap_y;
2358   int cell_y;
2359
2360   tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2361
2362   /* Find the selected path and get its drag pixmap */
2363
2364   if (!shortcuts_get_selected (impl, &iter))
2365     g_assert_not_reached ();
2366
2367   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2368
2369   row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2370   gtk_tree_path_free (path);
2371
2372   mask = NULL;
2373   row_pixmap_y = 0;
2374
2375   if (delete)
2376     {
2377       GdkPixbuf *pixbuf;
2378
2379       pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2380                                        GTK_STOCK_DELETE,
2381                                        GTK_ICON_SIZE_DND,
2382                                        NULL);
2383       if (pixbuf)
2384         {
2385           GdkPixmap *composite;
2386           int row_pixmap_width, row_pixmap_height;
2387           int pixbuf_width, pixbuf_height;
2388           int composite_width, composite_height;
2389           int pixbuf_x, pixbuf_y;
2390           GdkGC *gc, *mask_gc;
2391           GdkColor color;
2392           GdkBitmap *pixbuf_mask;
2393
2394           /* Create pixmap and mask for composite image */
2395
2396           gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2397           pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2398           pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2399
2400           composite_width = MAX (row_pixmap_width, pixbuf_width);
2401           composite_height = MAX (row_pixmap_height, pixbuf_height);
2402
2403           row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2404
2405           if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2406             pixbuf_x = 0;
2407           else
2408             pixbuf_x = composite_width - pixbuf_width;
2409
2410           pixbuf_y = (composite_height - pixbuf_height) / 2;
2411
2412           composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2413           gc = gdk_gc_new (composite);
2414
2415           mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2416           mask_gc = gdk_gc_new (mask);
2417           color.pixel = 0;
2418           gdk_gc_set_foreground (mask_gc, &color);
2419           gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2420
2421           color.red = 0xffff;
2422           color.green = 0xffff;
2423           color.blue = 0xffff;
2424           gdk_gc_set_rgb_fg_color (gc, &color);
2425           gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2426
2427           /* Composite the row pixmap and the pixbuf */
2428
2429           gdk_pixbuf_render_pixmap_and_mask_for_colormap
2430             (pixbuf,
2431              gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2432              NULL, &pixbuf_mask, 128);
2433           gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2434                              0, 0,
2435                              pixbuf_x, pixbuf_y,
2436                              pixbuf_width, pixbuf_height);
2437           g_object_unref (pixbuf_mask);
2438
2439           gdk_draw_drawable (composite, gc, row_pixmap,
2440                              0, 0,
2441                              0, row_pixmap_y,
2442                              row_pixmap_width, row_pixmap_height);
2443           color.pixel = 1;
2444           gdk_gc_set_foreground (mask_gc, &color);
2445           gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2446
2447           gdk_draw_pixbuf (composite, gc, pixbuf,
2448                            0, 0,
2449                            pixbuf_x, pixbuf_y,
2450                            pixbuf_width, pixbuf_height,
2451                            GDK_RGB_DITHER_MAX,
2452                            0, 0);
2453
2454           g_object_unref (pixbuf);
2455           g_object_unref (row_pixmap);
2456
2457           row_pixmap = composite;
2458         }
2459     }
2460
2461   /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2462
2463   gtk_tree_view_get_path_at_pos (tree_view,
2464                                  tree_view->priv->press_start_x,
2465                                  tree_view->priv->press_start_y,
2466                                  NULL,
2467                                  NULL,
2468                                  NULL,
2469                                  &cell_y);
2470
2471   gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2472                             gdk_drawable_get_colormap (row_pixmap),
2473                             row_pixmap,
2474                             mask,
2475                             tree_view->priv->press_start_x + 1,
2476                             row_pixmap_y + cell_y + 1);
2477
2478   g_object_unref (row_pixmap);
2479   if (mask)
2480     g_object_unref (mask);
2481 }
2482
2483 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2484  * handler so that we can tell apart the drag_leave event that comes right
2485  * before a drag_drop, from a normal drag_leave.  We don't want to set the
2486  * cursor nor the flag in the latter case.
2487  */
2488 static gboolean
2489 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2490 {
2491   GDK_THREADS_ENTER ();
2492   
2493   shortcuts_drag_set_delete_cursor (impl, TRUE);
2494   impl->shortcuts_drag_outside = TRUE;
2495
2496   shortcuts_cancel_drag_outside_idle (impl);
2497
2498   GDK_THREADS_LEAVE ();
2499
2500   return FALSE;
2501 }
2502 #endif
2503
2504 /* GtkWidget::drag-leave handler for the shortcuts list.  We unhighlight the
2505  * drop position.
2506  */
2507 static void
2508 shortcuts_drag_leave_cb (GtkWidget             *widget,
2509                          GdkDragContext        *context,
2510                          guint                  time_,
2511                          GtkFileChooserDefault *impl)
2512 {
2513 #if 0
2514   if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2515     {
2516       impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2517       g_source_set_closure (impl->shortcuts_drag_outside_idle,
2518                             g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2519                                                    G_OBJECT (impl)));
2520       g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2521     }
2522 #endif
2523
2524   gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2525                                    NULL,
2526                                    GTK_TREE_VIEW_DROP_BEFORE);
2527
2528   g_signal_stop_emission_by_name (widget, "drag-leave");
2529 }
2530
2531 /* Computes the appropriate row and position for dropping */
2532 static void
2533 shortcuts_compute_drop_position (GtkFileChooserDefault   *impl,
2534                                  int                      x,
2535                                  int                      y,
2536                                  GtkTreePath            **path,
2537                                  GtkTreeViewDropPosition *pos)
2538 {
2539   GtkTreeView *tree_view;
2540   GtkTreeViewColumn *column;
2541   int cell_y;
2542   GdkRectangle cell;
2543   int row;
2544   int bookmarks_index;
2545
2546   tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2547
2548   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2549
2550   if (!gtk_tree_view_get_path_at_pos (tree_view,
2551                                       x,
2552                                       y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2553                                       path,
2554                                       &column,
2555                                       NULL,
2556                                       &cell_y))
2557     {
2558       row = bookmarks_index + impl->num_bookmarks - 1;
2559       *path = gtk_tree_path_new_from_indices (row, -1);
2560       *pos = GTK_TREE_VIEW_DROP_AFTER;
2561       return;
2562     }
2563
2564   row = *gtk_tree_path_get_indices (*path);
2565   gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2566   gtk_tree_path_free (*path);
2567
2568   if (row < bookmarks_index)
2569     {
2570       row = bookmarks_index;
2571       *pos = GTK_TREE_VIEW_DROP_BEFORE;
2572     }
2573   else if (row > bookmarks_index + impl->num_bookmarks - 1)
2574     {
2575       row = bookmarks_index + impl->num_bookmarks - 1;
2576       *pos = GTK_TREE_VIEW_DROP_AFTER;
2577     }
2578   else
2579     {
2580       if (cell_y < cell.height / 2)
2581         *pos = GTK_TREE_VIEW_DROP_BEFORE;
2582       else
2583         *pos = GTK_TREE_VIEW_DROP_AFTER;
2584     }
2585
2586   *path = gtk_tree_path_new_from_indices (row, -1);
2587 }
2588
2589 /* GtkWidget::drag-motion handler for the shortcuts list.  We basically
2590  * implement the destination side of DnD by hand, due to limitations in
2591  * GtkTreeView's DnD API.
2592  */
2593 static gboolean
2594 shortcuts_drag_motion_cb (GtkWidget             *widget,
2595                           GdkDragContext        *context,
2596                           gint                   x,
2597                           gint                   y,
2598                           guint                  time_,
2599                           GtkFileChooserDefault *impl)
2600 {
2601   GtkTreePath *path;
2602   GtkTreeViewDropPosition pos;
2603   GdkDragAction action;
2604
2605 #if 0
2606   if (gtk_drag_get_source_widget (context) == widget)
2607     {
2608       shortcuts_cancel_drag_outside_idle (impl);
2609
2610       if (impl->shortcuts_drag_outside)
2611         {
2612           shortcuts_drag_set_delete_cursor (impl, FALSE);
2613           impl->shortcuts_drag_outside = FALSE;
2614         }
2615     }
2616 #endif
2617
2618   if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2619     action = GDK_ACTION_COPY;
2620   else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2621     action = GDK_ACTION_MOVE;
2622   else
2623     {
2624       action = 0;
2625       goto out;
2626     }
2627
2628   shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2629   gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2630   gtk_tree_path_free (path);
2631
2632  out:
2633
2634   g_signal_stop_emission_by_name (widget, "drag-motion");
2635
2636   if (action != 0)
2637     {
2638       gdk_drag_status (context, action, time_);
2639       return TRUE;
2640     }
2641   else
2642     return FALSE;
2643 }
2644
2645 /* GtkWidget::drag-drop handler for the shortcuts list. */
2646 static gboolean
2647 shortcuts_drag_drop_cb (GtkWidget             *widget,
2648                         GdkDragContext        *context,
2649                         gint                   x,
2650                         gint                   y,
2651                         guint                  time_,
2652                         GtkFileChooserDefault *impl)
2653 {
2654 #if 0
2655   shortcuts_cancel_drag_outside_idle (impl);
2656 #endif
2657
2658   g_signal_stop_emission_by_name (widget, "drag-drop");
2659   return TRUE;
2660 }
2661
2662 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2663 static void
2664 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2665                      const char            *data,
2666                      int                    position)
2667 {
2668   gchar **uris;
2669   gint i;
2670
2671   uris = g_uri_list_extract_uris (data);
2672
2673   for (i = 0; uris[i]; i++)
2674     {
2675       char *uri;
2676       GtkFilePath *path;
2677
2678       uri = uris[i];
2679       path = gtk_file_system_uri_to_path (impl->file_system, uri);
2680
2681       if (path)
2682         {
2683           if (shortcuts_add_bookmark_from_path (impl, path, position))
2684             position++;
2685
2686           gtk_file_path_free (path);
2687         }
2688       else
2689         {
2690           GError *error;
2691
2692           g_set_error (&error,
2693                        GTK_FILE_CHOOSER_ERROR,
2694                        GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
2695                        _("Could not add a bookmark for '%s' "
2696                          "because it is an invalid path name."),
2697                        uri);
2698           error_adding_bookmark_dialog (impl, path, error);
2699         }
2700     }
2701
2702   g_strfreev (uris);
2703 }
2704
2705 /* Reorders the selected bookmark to the specified position */
2706 static void
2707 shortcuts_reorder (GtkFileChooserDefault *impl,
2708                    int                    new_position)
2709 {
2710   GtkTreeIter iter;
2711   gpointer col_data;
2712   gboolean is_volume;
2713   GtkTreePath *path;
2714   int old_position;
2715   int bookmarks_index;
2716   const GtkFilePath *file_path;
2717   GtkFilePath *file_path_copy;
2718   GError *error;
2719
2720   /* Get the selected path */
2721
2722   if (!shortcuts_get_selected (impl, &iter))
2723     g_assert_not_reached ();
2724
2725   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2726   old_position = *gtk_tree_path_get_indices (path);
2727   gtk_tree_path_free (path);
2728
2729   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2730   old_position -= bookmarks_index;
2731   g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2732
2733   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2734                       SHORTCUTS_COL_DATA, &col_data,
2735                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
2736                       -1);
2737   g_assert (col_data != NULL);
2738   g_assert (!is_volume);
2739
2740   file_path = col_data;
2741   file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2742
2743   /* Remove the path from the old position and insert it in the new one */
2744
2745   if (new_position > old_position)
2746     new_position--;
2747
2748   if (old_position == new_position)
2749     goto out;
2750
2751   error = NULL;
2752   if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2753     shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2754   else
2755     error_adding_bookmark_dialog (impl, file_path_copy, error);
2756
2757  out:
2758
2759   gtk_file_path_free (file_path_copy);
2760 }
2761
2762 /* Callback used when we get the drag data for the bookmarks list.  We add the
2763  * received URIs as bookmarks if they are folders.
2764  */
2765 static void
2766 shortcuts_drag_data_received_cb (GtkWidget          *widget,
2767                                  GdkDragContext     *context,
2768                                  gint                x,
2769                                  gint                y,
2770                                  GtkSelectionData   *selection_data,
2771                                  guint               info,
2772                                  guint               time_,
2773                                  gpointer            data)
2774 {
2775   GtkFileChooserDefault *impl;
2776   GtkTreePath *tree_path;
2777   GtkTreeViewDropPosition tree_pos;
2778   int position;
2779   int bookmarks_index;
2780
2781   impl = GTK_FILE_CHOOSER_DEFAULT (data);
2782
2783   /* Compute position */
2784
2785   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2786
2787   shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2788   position = *gtk_tree_path_get_indices (tree_path);
2789   gtk_tree_path_free (tree_path);
2790
2791   if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2792     position++;
2793
2794   g_assert (position >= bookmarks_index);
2795   position -= bookmarks_index;
2796
2797   if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2798     shortcuts_drop_uris (impl, selection_data->data, position);
2799   else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2800     shortcuts_reorder (impl, position);
2801
2802   g_signal_stop_emission_by_name (widget, "drag-data-received");
2803 }
2804
2805 /* Callback used when the selection in the shortcuts tree changes */
2806 static void
2807 shortcuts_selection_changed_cb (GtkTreeSelection      *selection,
2808                                 GtkFileChooserDefault *impl)
2809 {
2810   bookmarks_check_remove_sensitivity (impl);
2811 }
2812
2813 static gboolean
2814 shortcuts_row_separator_func (GtkTreeModel *model,
2815                               GtkTreeIter  *iter,
2816                               gpointer      data)
2817 {
2818   gint column = GPOINTER_TO_INT (data);
2819   gchar *text;
2820
2821   gtk_tree_model_get (model, iter, column, &text, -1);
2822   
2823   if (!text)
2824     return TRUE;
2825
2826   g_free (text);
2827
2828   return FALSE;
2829 }
2830
2831 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
2832  * keypresses before the TreeView gets them.
2833  */
2834 static gboolean
2835 tree_view_keybinding_cb (GtkWidget             *tree_view,
2836                          GdkEventKey           *event,
2837                          GtkFileChooserDefault *impl)
2838 {
2839   if (event->keyval == GDK_slash &&
2840       ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2841     {
2842       location_popup_handler (impl, "/");
2843       return TRUE;
2844     }
2845   
2846   return FALSE;
2847 }
2848
2849
2850 /* Creates the widgets for the shortcuts and bookmarks tree */
2851 static GtkWidget *
2852 shortcuts_list_create (GtkFileChooserDefault *impl)
2853 {
2854   GtkWidget *swin;
2855   GtkTreeSelection *selection;
2856   GtkTreeViewColumn *column;
2857   GtkCellRenderer *renderer;
2858
2859   /* Scrolled window */
2860
2861   swin = gtk_scrolled_window_new (NULL, NULL);
2862   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2863                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2864   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2865                                        GTK_SHADOW_IN);
2866   gtk_widget_show (swin);
2867
2868   /* Tree */
2869
2870   impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2871   g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2872                     G_CALLBACK (tree_view_keybinding_cb), impl);
2873   atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
2874   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2875
2876   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2877
2878   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2879                                           GDK_BUTTON1_MASK,
2880                                           shortcuts_source_targets,
2881                                           num_shortcuts_source_targets,
2882                                           GDK_ACTION_MOVE);
2883
2884   gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2885                      GTK_DEST_DEFAULT_ALL,
2886                      shortcuts_dest_targets,
2887                      num_shortcuts_dest_targets,
2888                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
2889
2890   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2891   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2892   gtk_tree_selection_set_select_function (selection,
2893                                           shortcuts_select_func,
2894                                           impl, NULL);
2895
2896   g_signal_connect (selection, "changed",
2897                     G_CALLBACK (shortcuts_selection_changed_cb), impl);
2898
2899   g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2900                     G_CALLBACK (shortcuts_row_activated_cb), impl);
2901
2902   g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2903                     G_CALLBACK (shortcuts_key_press_event_cb), impl);
2904
2905   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2906                     G_CALLBACK (shortcuts_drag_begin_cb), impl);
2907   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2908                     G_CALLBACK (shortcuts_drag_end_cb), impl);
2909   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2910                     G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2911
2912   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2913                     G_CALLBACK (shortcuts_drag_leave_cb), impl);
2914   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2915                     G_CALLBACK (shortcuts_drag_motion_cb), impl);
2916   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2917                     G_CALLBACK (shortcuts_drag_drop_cb), impl);
2918   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2919                     G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2920
2921   gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2922   gtk_widget_show (impl->browse_shortcuts_tree_view);
2923
2924   /* Column */
2925
2926   column = gtk_tree_view_column_new ();
2927   gtk_tree_view_column_set_title (column, _("Folder"));
2928
2929   renderer = gtk_cell_renderer_pixbuf_new ();
2930   gtk_tree_view_column_pack_start (column, renderer, FALSE);
2931   gtk_tree_view_column_set_attributes (column, renderer,
2932                                        "pixbuf", SHORTCUTS_COL_PIXBUF,
2933                                        "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2934                                        NULL);
2935
2936   renderer = gtk_cell_renderer_text_new ();
2937   gtk_tree_view_column_pack_start (column, renderer, TRUE);
2938   gtk_tree_view_column_set_attributes (column, renderer,
2939                                        "text", SHORTCUTS_COL_NAME,
2940                                        NULL);
2941
2942   gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2943                                         shortcuts_row_separator_func,
2944                                         GINT_TO_POINTER (SHORTCUTS_COL_NAME),
2945                                         NULL);
2946
2947   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2948
2949   return swin;
2950 }
2951
2952 /* Creates the widgets for the shortcuts/bookmarks pane */
2953 static GtkWidget *
2954 shortcuts_pane_create (GtkFileChooserDefault *impl,
2955                        GtkSizeGroup          *size_group)
2956 {
2957   GtkWidget *vbox;
2958   GtkWidget *hbox;
2959   GtkWidget *widget;
2960
2961   vbox = gtk_vbox_new (FALSE, 6);
2962   gtk_widget_show (vbox);
2963
2964   /* Shortcuts tree */
2965
2966   widget = shortcuts_list_create (impl);
2967   gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2968
2969   /* Box for buttons */
2970
2971   hbox = gtk_hbox_new (TRUE, 6);
2972   gtk_size_group_add_widget (size_group, hbox);
2973   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2974   gtk_widget_show (hbox);
2975
2976   /* Add bookmark button */
2977
2978   impl->browse_shortcuts_add_button = button_new (impl,
2979                                                   _("_Add"),
2980                                                   GTK_STOCK_ADD,
2981                                                   FALSE,
2982                                                   TRUE,
2983                                                   G_CALLBACK (add_bookmark_button_clicked_cb));
2984   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2985   gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
2986                         _("Add the selected folder to the bookmarks"), NULL);
2987
2988   /* Remove bookmark button */
2989
2990   impl->browse_shortcuts_remove_button = button_new (impl,
2991                                                      _("_Remove"),
2992                                                      GTK_STOCK_REMOVE,
2993                                                      FALSE,
2994                                                      TRUE,
2995                                                      G_CALLBACK (remove_bookmark_button_clicked_cb));
2996   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2997   gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2998                         _("Remove the selected bookmark"), NULL);
2999
3000   return vbox;
3001 }
3002
3003 /* Handles key press events on the file list, so that we can trap Enter to
3004  * activate the default button on our own.  Also, checks to see if '/' has been
3005  * pressed.  See comment by tree_view_keybinding_cb() for more details.
3006  */
3007 static gboolean
3008 trap_activate_cb (GtkWidget   *widget,
3009                   GdkEventKey *event,
3010                   gpointer     data)
3011 {
3012   GtkFileChooserDefault *impl;
3013
3014   impl = (GtkFileChooserDefault *) data;
3015   
3016   if (event->keyval == GDK_slash &&
3017       ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3018     {
3019       location_popup_handler (impl, "/");
3020       return TRUE;
3021     }
3022
3023   if ((event->keyval == GDK_Return
3024        || event->keyval == GDK_ISO_Enter
3025        || event->keyval == GDK_KP_Enter
3026        || event->keyval == GDK_space)
3027       && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3028            impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3029     {
3030       GtkWindow *window;
3031
3032       window = get_toplevel (widget);
3033       if (window
3034           && widget != window->default_widget
3035           && !(widget == window->focus_widget &&
3036                (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3037         {
3038           gtk_window_activate_default (window);
3039           return TRUE;
3040         }
3041     }
3042
3043   return FALSE;
3044 }
3045
3046 /* Callback used when the file list's popup menu is detached */
3047 static void
3048 popup_menu_detach_cb (GtkWidget *attach_widget,
3049                       GtkMenu   *menu)
3050 {
3051   GtkFileChooserDefault *impl;
3052
3053   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3054   g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3055
3056   impl->browse_files_popup_menu = NULL;
3057   impl->browse_files_popup_menu_add_shortcut_item = NULL;
3058   impl->browse_files_popup_menu_hidden_files_item = NULL;
3059 }
3060
3061 /* Callback used when the "Add to Shortcuts" menu item is activated */
3062 static void
3063 add_to_shortcuts_cb (GtkMenuItem           *item,
3064                      GtkFileChooserDefault *impl)
3065 {
3066   bookmarks_add_selected_folder (impl);
3067 }
3068
3069 /* Callback used when the "Open Location" menu item is activated */
3070 static void
3071 open_location_cb (GtkMenuItem           *item,
3072                   GtkFileChooserDefault *impl)
3073 {
3074   location_popup_handler (impl, "");
3075 }
3076
3077 /* Callback used when the "Show Hidden Files" menu item is toggled */
3078 static void
3079 show_hidden_toggled_cb (GtkCheckMenuItem      *item,
3080                         GtkFileChooserDefault *impl)
3081 {
3082   g_object_set (impl,
3083                 "show-hidden", gtk_check_menu_item_get_active (item),
3084                 NULL);
3085 }
3086
3087 /* Constructs the popup menu for the file list if needed */
3088 static void
3089 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3090 {
3091   GtkWidget *item;
3092
3093   if (impl->browse_files_popup_menu)
3094     return;
3095
3096   impl->browse_files_popup_menu = gtk_menu_new ();
3097   gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3098                              impl->browse_files_tree_view,
3099                              popup_menu_detach_cb);
3100
3101   item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Shortcuts"));
3102   impl->browse_files_popup_menu_add_shortcut_item = item;
3103   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3104                                  gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3105   gtk_widget_set_sensitive (item, FALSE);
3106   g_signal_connect (item, "activate",
3107                     G_CALLBACK (add_to_shortcuts_cb), impl);
3108   gtk_widget_show (item);
3109   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3110
3111   item = gtk_image_menu_item_new_with_mnemonic (_("Open _Location"));
3112   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3113                                  gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU));
3114   g_signal_connect (item, "activate",
3115                     G_CALLBACK (open_location_cb), impl);
3116   gtk_widget_show (item);
3117   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3118
3119   item = gtk_separator_menu_item_new ();
3120   gtk_widget_show (item);
3121   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3122
3123   item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3124   impl->browse_files_popup_menu_hidden_files_item = item;
3125   g_signal_connect (item, "toggled",
3126                     G_CALLBACK (show_hidden_toggled_cb), impl);
3127   gtk_widget_show (item);
3128   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3129 }
3130
3131 /* Updates the popup menu for the file list, creating it if necessary */
3132 static void
3133 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3134 {
3135   file_list_build_popup_menu (impl);
3136
3137   /* The sensitivity of the Add to Shortcuts item is set in
3138    * bookmarks_check_add_sensitivity()
3139    */
3140
3141   g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3142                                    G_CALLBACK (show_hidden_toggled_cb), impl);
3143   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3144                                   impl->show_hidden);
3145   g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3146                                      G_CALLBACK (show_hidden_toggled_cb), impl);
3147 }
3148
3149 static void
3150 popup_position_func (GtkMenu   *menu,
3151                      gint      *x,
3152                      gint      *y,
3153                      gboolean  *push_in,
3154                      gpointer   user_data)
3155 {
3156   GtkWidget *widget = GTK_WIDGET (user_data);
3157   GdkScreen *screen = gtk_widget_get_screen (widget);
3158   GtkRequisition req;
3159   gint monitor_num;
3160   GdkRectangle monitor;
3161
3162   g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3163
3164   gdk_window_get_origin (widget->window, x, y);
3165
3166   gtk_widget_size_request (GTK_WIDGET (menu), &req);
3167
3168   *x += (widget->allocation.width - req.width) / 2;
3169   *y += (widget->allocation.height - req.height) / 2;
3170
3171   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3172   gtk_menu_set_monitor (menu, monitor_num);
3173   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3174
3175   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3176   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3177
3178   *push_in = FALSE;
3179 }
3180
3181 static void
3182 file_list_popup_menu (GtkFileChooserDefault *impl,
3183                       GdkEventButton        *event)
3184 {
3185   file_list_update_popup_menu (impl);
3186   if (event)
3187     gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3188                     NULL, NULL, NULL, NULL,
3189                     event->button, event->time);
3190   else
3191     {
3192       gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3193                       NULL, NULL,
3194                       popup_position_func, impl->browse_files_tree_view,
3195                       0, GDK_CURRENT_TIME);
3196       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3197                                    FALSE);
3198     }
3199
3200 }
3201
3202 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3203 static gboolean
3204 list_popup_menu_cb (GtkWidget *widget,
3205                     GtkFileChooserDefault *impl)
3206 {
3207   file_list_popup_menu (impl, NULL);
3208   return TRUE;
3209 }
3210
3211 /* Callback used when a button is pressed on the file list.  We trap button 3 to
3212  * bring up a popup menu.
3213  */
3214 static gboolean
3215 list_button_press_event_cb (GtkWidget             *widget,
3216                             GdkEventButton        *event,
3217                             GtkFileChooserDefault *impl)
3218 {
3219   if (event->button != 3)
3220     return FALSE;
3221
3222   file_list_popup_menu (impl, event);
3223   return TRUE;
3224 }
3225
3226 /* Creates the widgets for the file list */
3227 static GtkWidget *
3228 create_file_list (GtkFileChooserDefault *impl)
3229 {
3230   GtkWidget *swin;
3231   GtkTreeSelection *selection;
3232   GtkTreeViewColumn *column;
3233   GtkCellRenderer *renderer;
3234
3235   /* Scrolled window */
3236
3237   swin = gtk_scrolled_window_new (NULL, NULL);
3238   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3239                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3240   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3241                                        GTK_SHADOW_IN);
3242
3243   /* Tree/list view */
3244
3245   impl->browse_files_tree_view = gtk_tree_view_new ();
3246   g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3247   atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3248
3249   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3250   gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3251   g_signal_connect (impl->browse_files_tree_view, "row-activated",
3252                     G_CALLBACK (list_row_activated), impl);
3253   g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3254                     G_CALLBACK (trap_activate_cb), impl);
3255   g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3256                     G_CALLBACK (list_popup_menu_cb), impl);
3257   g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3258                     G_CALLBACK (list_button_press_event_cb), impl);
3259
3260   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3261   gtk_tree_selection_set_select_function (selection,
3262                                           list_select_func,
3263                                           impl, NULL);
3264   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3265                                           GDK_BUTTON1_MASK,
3266                                           file_list_source_targets,
3267                                           num_file_list_source_targets,
3268                                           GDK_ACTION_COPY);
3269
3270   g_signal_connect (selection, "changed",
3271                     G_CALLBACK (list_selection_changed), impl);
3272
3273   /* Filename column */
3274
3275   impl->list_name_column = gtk_tree_view_column_new ();
3276   gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3277   gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3278   gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3279   gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3280
3281   renderer = gtk_cell_renderer_pixbuf_new ();
3282   gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3283   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3284                                            list_icon_data_func, impl, NULL);
3285
3286   impl->list_name_renderer = gtk_cell_renderer_text_new ();
3287   g_object_set (impl->list_name_renderer,
3288                 "ellipsize", PANGO_ELLIPSIZE_END,
3289                 NULL);
3290   g_signal_connect (impl->list_name_renderer, "edited",
3291                     G_CALLBACK (renderer_edited_cb), impl);
3292   g_signal_connect (impl->list_name_renderer, "editing-canceled",
3293                     G_CALLBACK (renderer_editing_canceled_cb), impl);
3294   gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3295   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3296                                            list_name_data_func, impl, NULL);
3297
3298   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3299 #if 0
3300   /* Size column */
3301
3302   column = gtk_tree_view_column_new ();
3303   gtk_tree_view_column_set_title (column, _("Size"));
3304
3305   renderer = gtk_cell_renderer_text_new ();
3306   gtk_tree_view_column_pack_start (column, renderer, TRUE);
3307   gtk_tree_view_column_set_cell_data_func (column, renderer,
3308                                            list_size_data_func, impl, NULL);
3309   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3310   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3311 #endif
3312   /* Modification time column */
3313
3314   column = gtk_tree_view_column_new ();
3315   gtk_tree_view_column_set_resizable (column, TRUE);
3316   gtk_tree_view_column_set_title (column, _("Modified"));
3317
3318   renderer = gtk_cell_renderer_text_new ();
3319   gtk_tree_view_column_pack_start (column, renderer, TRUE);
3320   gtk_tree_view_column_set_cell_data_func (column, renderer,
3321                                            list_mtime_data_func, impl, NULL);
3322   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3323   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3324   gtk_widget_show_all (swin);
3325
3326   return swin;
3327 }
3328
3329 static GtkWidget *
3330 create_path_bar (GtkFileChooserDefault *impl)
3331 {
3332   GtkWidget *path_bar;
3333
3334   path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3335   _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3336
3337   return path_bar;
3338 }
3339
3340 static void
3341 set_filter_tooltip (GtkWidget *widget, 
3342                     gpointer   data)
3343 {
3344   GtkTooltips *tooltips = (GtkTooltips *)data;
3345
3346   if (GTK_IS_BUTTON (widget))
3347     gtk_tooltips_set_tip (tooltips, widget,
3348                           _("Select which types of files are shown"), 
3349                           NULL);
3350 }
3351
3352 static void
3353 realize_filter_combo (GtkWidget *combo,
3354                       gpointer   data)
3355 {
3356   GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
3357
3358   gtk_container_forall (GTK_CONTAINER (combo),
3359                         set_filter_tooltip,
3360                         impl->tooltips);
3361 }
3362
3363 /* Creates the widgets for the files/folders pane */
3364 static GtkWidget *
3365 file_pane_create (GtkFileChooserDefault *impl,
3366                   GtkSizeGroup          *size_group)
3367 {
3368   GtkWidget *vbox;
3369   GtkWidget *hbox;
3370   GtkWidget *widget;
3371
3372   vbox = gtk_vbox_new (FALSE, 6);
3373   gtk_widget_show (vbox);
3374
3375   /* The path bar and 'Create Folder' button */
3376   hbox = gtk_hbox_new (FALSE, 12);
3377   gtk_widget_show (hbox);
3378   impl->browse_path_bar = create_path_bar (impl);
3379   g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3380   gtk_widget_show_all (impl->browse_path_bar);
3381   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3382
3383   /* Create Folder */
3384   impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3385   g_signal_connect (impl->browse_new_folder_button, "clicked",
3386                     G_CALLBACK (new_folder_button_clicked), impl);
3387   gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3388   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3389
3390   /* Box for lists and preview */
3391
3392   hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3393   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3394   gtk_widget_show (hbox);
3395
3396   /* File list */
3397
3398   widget = create_file_list (impl);
3399   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3400
3401   /* Preview */
3402
3403   impl->preview_box = gtk_vbox_new (FALSE, 12);
3404   gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3405   /* Don't show preview box initially */
3406
3407   /* Filter combo */
3408
3409   impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3410
3411   widget = filter_create (impl);
3412
3413   g_signal_connect (widget, "realize",
3414                     G_CALLBACK (realize_filter_combo), impl);
3415
3416   gtk_widget_show (widget);
3417   gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3418
3419   gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3420   gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3421
3422   return vbox;
3423 }
3424 /* Callback used when the "Browse for more folders" expander is toggled */
3425 static void
3426 expander_changed_cb (GtkExpander           *expander,
3427                      GParamSpec            *pspec,
3428                      GtkFileChooserDefault *impl)
3429 {
3430   update_appearance (impl);
3431 }
3432
3433 /* Callback used when the selection changes in the save folder combo box */
3434 static void
3435 save_folder_combo_changed_cb (GtkComboBox           *combo,
3436                               GtkFileChooserDefault *impl)
3437 {
3438   GtkTreeIter iter;
3439
3440   if (impl->changing_folder)
3441     return;
3442
3443   if (gtk_combo_box_get_active_iter (combo, &iter))
3444     shortcuts_activate_iter (impl, &iter);
3445 }
3446
3447 /* Creates the combo box with the save folders */
3448 static GtkWidget *
3449 save_folder_combo_create (GtkFileChooserDefault *impl)
3450 {
3451   GtkWidget *combo;
3452   GtkCellRenderer *cell;
3453
3454   combo = g_object_new (GTK_TYPE_COMBO_BOX,
3455                         "model", impl->shortcuts_model,
3456                         "focus-on-click", FALSE,
3457                         NULL);
3458   gtk_widget_show (combo);
3459
3460   cell = gtk_cell_renderer_pixbuf_new ();
3461   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3462   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3463                                   "pixbuf", SHORTCUTS_COL_PIXBUF,
3464                                   "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3465                                   "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3466                                   NULL);
3467
3468   cell = gtk_cell_renderer_text_new ();
3469   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3470   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3471                                   "text", SHORTCUTS_COL_NAME,
3472                                   "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3473                                   NULL);
3474
3475   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3476                                         shortcuts_row_separator_func,
3477                                         GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3478                                         NULL);
3479
3480   g_signal_connect (combo, "changed",
3481                     G_CALLBACK (save_folder_combo_changed_cb), impl);
3482
3483   return combo;
3484 }
3485
3486 /* Creates the widgets specific to Save mode */
3487 static GtkWidget *
3488 save_widgets_create (GtkFileChooserDefault *impl)
3489 {
3490   GtkWidget *vbox;
3491   GtkWidget *table;
3492   GtkWidget *widget;
3493   GtkWidget *alignment;
3494
3495   vbox = gtk_vbox_new (FALSE, 12);
3496
3497   table = gtk_table_new (2, 2, FALSE);
3498   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3499   gtk_widget_show (table);
3500   gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3501   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3502
3503   /* Name entry */
3504
3505   widget = gtk_label_new_with_mnemonic (_("_Name:"));
3506   gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3507   gtk_table_attach (GTK_TABLE (table), widget,
3508                     0, 1, 0, 1,
3509                     GTK_FILL, GTK_FILL,
3510                     0, 0);
3511   gtk_widget_show (widget);
3512
3513   impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3514   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3515                                            impl->file_system);
3516   gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3517   gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3518   gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3519                     1, 2, 0, 1,
3520                     GTK_EXPAND | GTK_FILL, 0,
3521                     0, 0);
3522   gtk_widget_show (impl->save_file_name_entry);
3523   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3524
3525   /* Folder combo */
3526   impl->save_folder_label = gtk_label_new (NULL);
3527   gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3528   gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3529                     0, 1, 1, 2,
3530                     GTK_FILL, GTK_FILL,
3531                     0, 0);
3532   gtk_widget_show (impl->save_folder_label);
3533
3534   impl->save_folder_combo = save_folder_combo_create (impl);
3535   gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3536                     1, 2, 1, 2,
3537                     GTK_EXPAND | GTK_FILL, GTK_FILL,
3538                     0, 0);
3539   gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3540
3541   /* Expander */
3542   alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3543   gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3544
3545   impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3546   gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3547   g_signal_connect (impl->save_expander, "notify::expanded",
3548                     G_CALLBACK (expander_changed_cb),
3549                     impl);
3550   gtk_widget_show_all (alignment);
3551
3552   return vbox;
3553 }
3554
3555 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3556 static GtkWidget *
3557 browse_widgets_create (GtkFileChooserDefault *impl)
3558 {
3559   GtkWidget *vbox;
3560   GtkWidget *hpaned;
3561   GtkWidget *widget;
3562   GtkSizeGroup *size_group;
3563
3564   /* size group is used by the [+][-] buttons and the filter combo */
3565   size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3566   vbox = gtk_vbox_new (FALSE, 12);
3567
3568   /* Paned widget */
3569   hpaned = gtk_hpaned_new ();
3570   gtk_widget_show (hpaned);
3571   gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3572   gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3573
3574   widget = shortcuts_pane_create (impl, size_group);
3575   gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3576   widget = file_pane_create (impl, size_group);
3577   gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3578
3579   g_object_unref (size_group);
3580
3581   return vbox;
3582 }
3583
3584 static GObject*
3585 gtk_file_chooser_default_constructor (GType                  type,
3586                                       guint                  n_construct_properties,
3587                                       GObjectConstructParam *construct_params)
3588 {
3589   GtkFileChooserDefault *impl;
3590   GObject *object;
3591
3592   object = parent_class->constructor (type,
3593                                       n_construct_properties,
3594                                       construct_params);
3595   impl = GTK_FILE_CHOOSER_DEFAULT (object);
3596
3597   g_assert (impl->file_system);
3598
3599   gtk_widget_push_composite_child ();
3600
3601   /* Shortcuts model */
3602
3603   shortcuts_model_create (impl);
3604
3605   /* Widgets for Save mode */
3606   impl->save_widgets = save_widgets_create (impl);
3607   gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3608
3609   /* The browse widgets */
3610   impl->browse_widgets = browse_widgets_create (impl);
3611   gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3612
3613   /* Alignment to hold extra widget */
3614   impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3615   gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
3616
3617   gtk_widget_pop_composite_child ();
3618   update_appearance (impl);
3619
3620   return object;
3621 }
3622
3623 /* Sets the extra_widget by packing it in the appropriate place */
3624 static void
3625 set_extra_widget (GtkFileChooserDefault *impl,
3626                   GtkWidget             *extra_widget)
3627 {
3628   if (extra_widget)
3629     {
3630       g_object_ref (extra_widget);
3631       /* FIXME: is this right ? */
3632       gtk_widget_show (extra_widget);
3633     }
3634
3635   if (impl->extra_widget)
3636     {
3637       gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3638       g_object_unref (impl->extra_widget);
3639     }
3640
3641   impl->extra_widget = extra_widget;
3642   if (impl->extra_widget)
3643     {
3644       gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3645       gtk_widget_show (impl->extra_align);
3646     }
3647   else
3648     gtk_widget_hide (impl->extra_align);
3649 }
3650
3651 static void
3652 set_local_only (GtkFileChooserDefault *impl,
3653                 gboolean               local_only)
3654 {
3655   if (local_only != impl->local_only)
3656     {
3657       impl->local_only = local_only;
3658
3659       if (impl->shortcuts_model && impl->file_system)
3660         {
3661           shortcuts_add_volumes (impl);
3662           shortcuts_add_bookmarks (impl);
3663         }
3664
3665       if (local_only &&
3666           !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3667         {
3668           /* If we are pointing to a non-local folder, make an effort to change
3669            * back to a local folder, but it's really up to the app to not cause
3670            * such a situation, so we ignore errors.
3671            */
3672           const gchar *home = g_get_home_dir ();
3673           GtkFilePath *home_path;
3674
3675           if (home == NULL)
3676             return;
3677
3678           home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3679
3680           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3681
3682           gtk_file_path_free (home_path);
3683         }
3684     }
3685 }
3686
3687 static void
3688 volumes_changed_cb (GtkFileSystem         *file_system,
3689                     GtkFileChooserDefault *impl)
3690 {
3691   shortcuts_add_volumes (impl);
3692 }
3693
3694 /* Callback used when the set of bookmarks changes in the file system */
3695 static void
3696 bookmarks_changed_cb (GtkFileSystem         *file_system,
3697                       GtkFileChooserDefault *impl)
3698 {
3699   shortcuts_add_bookmarks (impl);
3700
3701   bookmarks_check_add_sensitivity (impl);
3702   bookmarks_check_remove_sensitivity (impl);
3703 }
3704
3705 /* Sets the file chooser to multiple selection mode */
3706 static void
3707 set_select_multiple (GtkFileChooserDefault *impl,
3708                      gboolean               select_multiple,
3709                      gboolean               property_notify)
3710 {
3711   GtkTreeSelection *selection;
3712   GtkSelectionMode mode;
3713
3714   if (select_multiple == impl->select_multiple)
3715     return;
3716
3717   mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3718
3719   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3720   gtk_tree_selection_set_mode (selection, mode);
3721
3722   impl->select_multiple = select_multiple;
3723   g_object_notify (G_OBJECT (impl), "select-multiple");
3724
3725   check_preview_change (impl);
3726 }
3727
3728 static void
3729 set_file_system_backend (GtkFileChooserDefault *impl,
3730                          const char *backend)
3731 {
3732   if (impl->file_system)
3733     {
3734       g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3735       impl->volumes_changed_id = 0;
3736       g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3737       impl->bookmarks_changed_id = 0;
3738       g_object_unref (impl->file_system);
3739     }
3740
3741   impl->file_system = NULL;
3742   if (backend)
3743     impl->file_system = _gtk_file_system_create (backend);
3744   else
3745     {
3746       GtkSettings *settings = gtk_settings_get_default ();
3747       gchar *default_backend = NULL;
3748
3749       g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3750       if (default_backend)
3751         {
3752           impl->file_system = _gtk_file_system_create (default_backend);
3753           g_free (default_backend);
3754         }
3755     }
3756
3757   if (!impl->file_system)
3758     {
3759 #if defined (G_OS_UNIX)
3760       impl->file_system = gtk_file_system_unix_new ();
3761 #elif defined (G_OS_WIN32)
3762       impl->file_system = gtk_file_system_win32_new ();
3763 #else
3764 #error "No default filesystem implementation on the platform"
3765 #endif
3766     }
3767
3768   if (impl->file_system)
3769     {
3770       impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3771                                                    G_CALLBACK (volumes_changed_cb),
3772                                                    impl);
3773       impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3774                                                      G_CALLBACK (bookmarks_changed_cb),
3775                                                      impl);
3776     }
3777 }
3778
3779 /* This function is basically a do_all function.
3780  *
3781  * It sets the visibility on all the widgets based on the current state, and
3782  * moves the custom_widget if needed.
3783  */
3784 static void
3785 update_appearance (GtkFileChooserDefault *impl)
3786 {
3787   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3788       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3789     {
3790       const char *text;
3791
3792       gtk_widget_show (impl->save_widgets);
3793
3794       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3795         text = _("Save in _folder:");
3796       else
3797         text = _("Create in _folder:");
3798
3799       gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3800
3801       if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3802         {
3803           gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3804           gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3805           gtk_widget_show (impl->browse_widgets);
3806         }
3807       else
3808         {
3809           gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3810           gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3811           gtk_widget_hide (impl->browse_widgets);
3812         }
3813
3814       gtk_widget_show (impl->browse_new_folder_button);
3815
3816       if (impl->select_multiple)
3817         {
3818           g_warning ("Save mode cannot be set in conjunction with multiple selection mode.  "
3819                      "Re-setting to single selection mode.");
3820           set_select_multiple (impl, FALSE, TRUE);
3821         }
3822     }
3823   else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3824            impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3825     {
3826       gtk_widget_hide (impl->save_widgets);
3827       gtk_widget_show (impl->browse_widgets);
3828     }
3829
3830   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3831     gtk_widget_hide (impl->browse_new_folder_button);
3832   else
3833     gtk_widget_show (impl->browse_new_folder_button);
3834
3835   gtk_widget_queue_draw (impl->browse_files_tree_view);
3836
3837   g_signal_emit_by_name (impl, "default-size-changed");
3838 }
3839
3840 static void
3841 gtk_file_chooser_default_set_property (GObject      *object,
3842                                        guint         prop_id,
3843                                        const GValue *value,
3844                                        GParamSpec   *pspec)
3845
3846 {
3847   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3848
3849   switch (prop_id)
3850     {
3851     case GTK_FILE_CHOOSER_PROP_ACTION:
3852       {
3853         GtkFileChooserAction action = g_value_get_enum (value);
3854
3855         if (action != impl->action)
3856           {
3857             gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
3858             
3859             if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3860               {
3861                 g_warning ("Multiple selection mode is not allowed in Save mode");
3862                 set_select_multiple (impl, FALSE, TRUE);
3863               }
3864             impl->action = action;
3865             update_appearance (impl);
3866           }
3867         
3868         if (impl->save_file_name_entry)
3869           _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3870                                               action);
3871       }
3872       break;
3873     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3874       set_file_system_backend (impl, g_value_get_string (value));
3875       break;
3876     case GTK_FILE_CHOOSER_PROP_FILTER:
3877       set_current_filter (impl, g_value_get_object (value));
3878       break;
3879     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3880       set_local_only (impl, g_value_get_boolean (value));
3881       break;
3882     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3883       set_preview_widget (impl, g_value_get_object (value));
3884       break;
3885     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3886       impl->preview_widget_active = g_value_get_boolean (value);
3887       update_preview_widget_visibility (impl);
3888       break;
3889     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3890       impl->use_preview_label = g_value_get_boolean (value);
3891       update_preview_widget_visibility (impl);
3892       break;
3893     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3894       set_extra_widget (impl, g_value_get_object (value));
3895       break;
3896     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3897       {
3898         gboolean select_multiple = g_value_get_boolean (value);
3899         if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3900           {
3901             g_warning ("Multiple selection mode is not allowed in Save mode");
3902             return;
3903           }
3904
3905         set_select_multiple (impl, select_multiple, FALSE);
3906       }
3907       break;
3908     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3909       {
3910         gboolean show_hidden = g_value_get_boolean (value);
3911         if (show_hidden != impl->show_hidden)
3912           {
3913             impl->show_hidden = show_hidden;
3914
3915             if (impl->browse_files_model)
3916               _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3917           }
3918       }
3919       break;
3920     default:
3921       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3922       break;
3923     }
3924 }
3925
3926 static void
3927 gtk_file_chooser_default_get_property (GObject    *object,
3928                                        guint       prop_id,
3929                                        GValue     *value,
3930                                        GParamSpec *pspec)
3931 {
3932   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3933
3934   switch (prop_id)
3935     {
3936     case GTK_FILE_CHOOSER_PROP_ACTION:
3937       g_value_set_enum (value, impl->action);
3938       break;
3939     case GTK_FILE_CHOOSER_PROP_FILTER:
3940       g_value_set_object (value, impl->current_filter);
3941       break;
3942     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3943       g_value_set_boolean (value, impl->local_only);
3944       break;
3945     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3946       g_value_set_object (value, impl->preview_widget);
3947       break;
3948     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3949       g_value_set_boolean (value, impl->preview_widget_active);
3950       break;
3951     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3952       g_value_set_boolean (value, impl->use_preview_label);
3953       break;
3954     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3955       g_value_set_object (value, impl->extra_widget);
3956       break;
3957     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3958       g_value_set_boolean (value, impl->select_multiple);
3959       break;
3960     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3961       g_value_set_boolean (value, impl->show_hidden);
3962       break;
3963     default:
3964       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3965       break;
3966     }
3967 }
3968
3969 /* Removes the settings signal handler.  It's safe to call multiple times */
3970 static void
3971 remove_settings_signal (GtkFileChooserDefault *impl,
3972                         GdkScreen             *screen)
3973 {
3974   if (impl->settings_signal_id)
3975     {
3976       GtkSettings *settings;
3977
3978       settings = gtk_settings_get_for_screen (screen);
3979       g_signal_handler_disconnect (settings,
3980                                    impl->settings_signal_id);
3981       impl->settings_signal_id = 0;
3982     }
3983 }
3984
3985 static void
3986 gtk_file_chooser_default_dispose (GObject *object)
3987 {
3988   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3989
3990   if (impl->extra_widget)
3991     {
3992       g_object_unref (impl->extra_widget);
3993       impl->extra_widget = NULL;
3994     }
3995
3996   remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3997
3998   G_OBJECT_CLASS (parent_class)->dispose (object);
3999 }
4000
4001 /* We override show-all since we have internal widgets that
4002  * shouldn't be shown when you call show_all(), like the filter
4003  * combo box.
4004  */
4005 static void
4006 gtk_file_chooser_default_show_all (GtkWidget *widget)
4007 {
4008   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
4009
4010   gtk_widget_show (widget);
4011
4012   if (impl->extra_widget)
4013     gtk_widget_show_all (impl->extra_widget);
4014 }
4015
4016 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
4017  * widget on our toplevel.  See gtk_file_chooser_default_hierarchy_changed()
4018  */
4019 static void
4020 toplevel_set_focus_cb (GtkWindow             *window,
4021                        GtkWidget             *focus,
4022                        GtkFileChooserDefault *impl)
4023 {
4024   impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
4025 }
4026
4027 /* We monitor the focus widget on our toplevel to be able to know which widget
4028  * was last focused at the time our "should_respond" method gets called.
4029  */
4030 static void
4031 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
4032                                             GtkWidget *previous_toplevel)
4033 {
4034   GtkFileChooserDefault *impl;
4035   GtkWidget *toplevel;
4036
4037   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4038
4039   if (previous_toplevel)
4040     {
4041       g_assert (impl->toplevel_set_focus_id != 0);
4042       g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
4043       impl->toplevel_set_focus_id = 0;
4044       impl->toplevel_last_focus_widget = NULL;
4045     }
4046   else
4047     g_assert (impl->toplevel_set_focus_id == 0);
4048
4049   toplevel = gtk_widget_get_toplevel (widget);
4050   if (GTK_IS_WINDOW (toplevel))
4051     {
4052       impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
4053                                                       G_CALLBACK (toplevel_set_focus_cb), impl);
4054       impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
4055     }
4056 }
4057
4058 /* Changes the icons wherever it is needed */
4059 static void
4060 change_icon_theme (GtkFileChooserDefault *impl)
4061 {
4062   GtkSettings *settings;
4063   gint width, height;
4064
4065   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4066
4067   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
4068     impl->icon_size = MAX (width, height);
4069   else
4070     impl->icon_size = FALLBACK_ICON_SIZE;
4071
4072   shortcuts_reload_icons (impl);
4073   gtk_widget_queue_resize (impl->browse_files_tree_view);
4074 }
4075
4076 /* Callback used when a GtkSettings value changes */
4077 static void
4078 settings_notify_cb (GObject               *object,
4079                     GParamSpec            *pspec,
4080                     GtkFileChooserDefault *impl)
4081 {
4082   const char *name;
4083
4084   name = g_param_spec_get_name (pspec);
4085
4086   if (strcmp (name, "gtk-icon-theme-name") == 0
4087       || strcmp (name, "gtk-icon-sizes") == 0)
4088     change_icon_theme (impl);
4089 }
4090
4091 /* Installs a signal handler for GtkSettings so that we can monitor changes in
4092  * the icon theme.
4093  */
4094 static void
4095 check_icon_theme (GtkFileChooserDefault *impl)
4096 {
4097   GtkSettings *settings;
4098
4099   if (impl->settings_signal_id)
4100     return;
4101
4102   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4103     {
4104       settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4105       impl->settings_signal_id = g_signal_connect (settings, "notify",
4106                                                    G_CALLBACK (settings_notify_cb), impl);
4107
4108       change_icon_theme (impl);
4109     }
4110 }
4111
4112 static void
4113 gtk_file_chooser_default_style_set (GtkWidget *widget,
4114                                     GtkStyle  *previous_style)
4115 {
4116   GtkFileChooserDefault *impl;
4117
4118   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4119
4120   if (GTK_WIDGET_CLASS (parent_class)->style_set)
4121     GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
4122
4123   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4124     change_icon_theme (impl);
4125
4126   g_signal_emit_by_name (widget, "default-size-changed");
4127 }
4128
4129 static void
4130 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
4131                                          GdkScreen *previous_screen)
4132 {
4133   GtkFileChooserDefault *impl;
4134
4135   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4136
4137   if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
4138     GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
4139
4140   remove_settings_signal (impl, previous_screen);
4141   check_icon_theme (impl);
4142
4143   g_signal_emit_by_name (widget, "default-size-changed");
4144 }
4145
4146 static gboolean
4147 get_is_file_filtered (GtkFileChooserDefault *impl,
4148                       const GtkFilePath     *path,
4149                       GtkFileInfo           *file_info)
4150 {
4151   GtkFileFilterInfo filter_info;
4152   GtkFileFilterFlags needed;
4153   gboolean result;
4154
4155   if (!impl->current_filter)
4156     return FALSE;
4157
4158   filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4159
4160   needed = gtk_file_filter_get_needed (impl->current_filter);
4161
4162   filter_info.display_name = gtk_file_info_get_display_name (file_info);
4163   filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4164
4165   if (needed & GTK_FILE_FILTER_FILENAME)
4166     {
4167       filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4168       if (filter_info.filename)
4169         filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4170     }
4171   else
4172     filter_info.filename = NULL;
4173
4174   if (needed & GTK_FILE_FILTER_URI)
4175     {
4176       filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4177       if (filter_info.uri)
4178         filter_info.contains |= GTK_FILE_FILTER_URI;
4179     }
4180   else
4181     filter_info.uri = NULL;
4182
4183   result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4184
4185   if (filter_info.filename)
4186     g_free ((gchar *)filter_info.filename);
4187   if (filter_info.uri)
4188     g_free ((gchar *)filter_info.uri);
4189
4190   return !result;
4191 }
4192
4193 /* GtkWidget::map method */
4194 static void
4195 gtk_file_chooser_default_map (GtkWidget *widget)
4196 {
4197   GtkFileChooserDefault *impl;
4198
4199   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4200
4201   GTK_WIDGET_CLASS (parent_class)->map (widget);
4202
4203   if (impl->current_folder)
4204     {
4205       pending_select_paths_store_selection (impl);
4206       change_folder_and_display_error (impl, impl->current_folder);
4207     }
4208
4209   bookmarks_changed_cb (impl->file_system, impl);
4210 }
4211
4212 static gboolean
4213 list_model_filter_func (GtkFileSystemModel *model,
4214                         GtkFilePath        *path,
4215                         const GtkFileInfo  *file_info,
4216                         gpointer            user_data)
4217 {
4218   GtkFileChooserDefault *impl = user_data;
4219
4220   if (!impl->current_filter)
4221     return TRUE;
4222
4223   if (gtk_file_info_get_is_folder (file_info))
4224     return TRUE;
4225
4226   return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4227 }
4228
4229 static void
4230 install_list_model_filter (GtkFileChooserDefault *impl)
4231 {
4232   GtkFileSystemModelFilter filter;
4233   gpointer data;
4234
4235   g_assert (impl->browse_files_model != NULL);
4236
4237   if (impl->current_filter)
4238     {
4239       filter = list_model_filter_func;
4240       data   = impl;
4241     }
4242   else
4243     {
4244       filter = NULL;
4245       data   = NULL;
4246     }
4247   
4248   _gtk_file_system_model_set_filter (impl->browse_files_model,
4249                                      filter,
4250                                      data);
4251 }
4252
4253 #define COMPARE_DIRECTORIES                                                                                    \
4254   GtkFileChooserDefault *impl = user_data;                                                                     \
4255   const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a);                           \
4256   const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b);                           \
4257   gboolean dir_a, dir_b;                                                                                       \
4258                                                                                                                \
4259   if (info_a)                                                                                                  \
4260     dir_a = gtk_file_info_get_is_folder (info_a);                                                              \
4261   else                                                                                                         \
4262     return impl->list_sort_ascending ? -1 : 1;                                                                 \
4263                                                                                                                \
4264   if (info_b)                                                                                                  \
4265     dir_b = gtk_file_info_get_is_folder (info_b);                                                              \
4266   else                                                                                                         \
4267     return impl->list_sort_ascending ? 1 : -1;                                                                 \
4268                                                                                                                \
4269   if (dir_a != dir_b)                                                                                          \
4270     return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4271
4272 /* Sort callback for the filename column */
4273 static gint
4274 name_sort_func (GtkTreeModel *model,
4275                 GtkTreeIter  *a,
4276                 GtkTreeIter  *b,
4277                 gpointer      user_data)
4278 {
4279   COMPARE_DIRECTORIES;
4280   else
4281     return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4282 }
4283
4284 /* Sort callback for the size column */
4285 static gint
4286 size_sort_func (GtkTreeModel *model,
4287                 GtkTreeIter  *a,
4288                 GtkTreeIter  *b,
4289                 gpointer      user_data)
4290 {
4291   COMPARE_DIRECTORIES;
4292   else
4293     {
4294       gint64 size_a = gtk_file_info_get_size (info_a);
4295       gint64 size_b = gtk_file_info_get_size (info_b);
4296
4297       return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4298     }
4299 }
4300
4301 /* Sort callback for the mtime column */
4302 static gint
4303 mtime_sort_func (GtkTreeModel *model,
4304                  GtkTreeIter  *a,
4305                  GtkTreeIter  *b,
4306                  gpointer      user_data)
4307 {
4308   COMPARE_DIRECTORIES;
4309   else
4310     {
4311       GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4312       GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4313
4314       return ta > tb ? -1 : (ta == tb ? 0 : 1);
4315     }
4316 }
4317
4318 /* Callback used when the sort column changes.  We cache the sort order for use
4319  * in name_sort_func().
4320  */
4321 static void
4322 list_sort_column_changed_cb (GtkTreeSortable       *sortable,
4323                              GtkFileChooserDefault *impl)
4324 {
4325   GtkSortType sort_type;
4326
4327   if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4328     impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4329 }
4330
4331 static void
4332 set_busy_cursor (GtkFileChooserDefault *impl,
4333                  gboolean               busy)
4334 {
4335   GtkWindow *toplevel;
4336   GdkDisplay *display;
4337   GdkCursor *cursor;
4338
4339   toplevel = get_toplevel (GTK_WIDGET (impl));
4340   if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4341     return;
4342
4343   display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4344
4345   if (busy)
4346     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4347   else
4348     cursor = NULL;
4349
4350   gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4351   gdk_display_flush (display);
4352
4353   if (cursor)
4354     gdk_cursor_unref (cursor);
4355 }
4356
4357 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4358 static void
4359 load_set_model (GtkFileChooserDefault *impl)
4360 {
4361   g_assert (impl->browse_files_model != NULL);
4362   g_assert (impl->sort_model == NULL);
4363
4364   impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4365   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4366   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4367   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4368   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4369   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4370   impl->list_sort_ascending = TRUE;
4371
4372   g_signal_connect (impl->sort_model, "sort-column-changed",
4373                     G_CALLBACK (list_sort_column_changed_cb), impl);
4374
4375   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4376                            GTK_TREE_MODEL (impl->sort_model));
4377   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4378   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4379                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4380 }
4381
4382 /* Timeout callback used when the loading timer expires */
4383 static gboolean
4384 load_timeout_cb (gpointer data)
4385 {
4386   GtkFileChooserDefault *impl;
4387
4388   GDK_THREADS_ENTER ();
4389
4390   impl = GTK_FILE_CHOOSER_DEFAULT (data);
4391   g_assert (impl->load_state == LOAD_PRELOAD);
4392   g_assert (impl->load_timeout_id != 0);
4393   g_assert (impl->browse_files_model != NULL);
4394
4395   impl->load_timeout_id = 0;
4396   impl->load_state = LOAD_LOADING;
4397
4398   load_set_model (impl);
4399
4400   GDK_THREADS_LEAVE ();
4401
4402   return FALSE;
4403 }
4404
4405 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4406 static void
4407 load_setup_timer (GtkFileChooserDefault *impl)
4408 {
4409   g_assert (impl->load_timeout_id == 0);
4410   g_assert (impl->load_state != LOAD_PRELOAD);
4411
4412   impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4413   impl->load_state = LOAD_PRELOAD;
4414 }
4415
4416 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4417 static void
4418 load_remove_timer (GtkFileChooserDefault *impl)
4419 {
4420   if (impl->load_timeout_id != 0)
4421     {
4422       g_assert (impl->load_state == LOAD_PRELOAD);
4423
4424       g_source_remove (impl->load_timeout_id);
4425       impl->load_timeout_id = 0;
4426       impl->load_state = LOAD_EMPTY;
4427     }
4428   else
4429     g_assert (impl->load_state == LOAD_EMPTY ||
4430               impl->load_state == LOAD_LOADING ||
4431               impl->load_state == LOAD_FINISHED);
4432 }
4433
4434 /* Selects the first row in the file list */
4435 static void
4436 browse_files_select_first_row (GtkFileChooserDefault *impl)
4437 {
4438   GtkTreePath *path;
4439
4440   if (!impl->sort_model)
4441     return;
4442
4443   path = gtk_tree_path_new_from_indices (0, -1);
4444   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4445   gtk_tree_path_free (path);
4446 }
4447
4448 struct center_selected_row_closure {
4449   GtkFileChooserDefault *impl;
4450   gboolean already_centered;
4451 };
4452
4453 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4454  * selected row in the tree view.
4455  */
4456 static void
4457 center_selected_row_foreach_cb (GtkTreeModel      *model,
4458                                 GtkTreePath       *path,
4459                                 GtkTreeIter       *iter,
4460                                 gpointer           data)
4461 {
4462   struct center_selected_row_closure *closure;
4463
4464   closure = data;
4465   if (closure->already_centered)
4466     return;
4467
4468   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
4469   closure->already_centered = TRUE;
4470 }
4471
4472 /* Centers the selected row in the tree view */
4473 static void
4474 browse_files_center_selected_row (GtkFileChooserDefault *impl)
4475 {
4476   struct center_selected_row_closure closure;
4477   GtkTreeSelection *selection;
4478
4479   closure.impl = impl;
4480   closure.already_centered = FALSE;
4481
4482   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4483   gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
4484 }
4485
4486 static gboolean
4487 show_and_select_paths (GtkFileChooserDefault *impl,
4488                        const GtkFilePath     *parent_path,
4489                        const GtkFilePath     *only_one_path,
4490                        GSList                *paths,
4491                        GError               **error)
4492 {
4493   GtkFileFolder *folder;
4494   gboolean success;
4495   gboolean have_hidden;
4496   gboolean have_filtered;
4497
4498   if (!only_one_path && !paths)
4499     return TRUE;
4500
4501   folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4502   if (!folder)
4503     return FALSE;
4504
4505   success = FALSE;
4506   have_hidden = FALSE;
4507   have_filtered = FALSE;
4508
4509   if (only_one_path)
4510     {
4511       GtkFileInfo *info;
4512
4513       info = gtk_file_folder_get_info (folder, only_one_path, error);
4514       if (info)
4515         {
4516           success = TRUE;
4517           have_hidden = gtk_file_info_get_is_hidden (info);
4518           have_filtered = get_is_file_filtered (impl, only_one_path, info);
4519           gtk_file_info_free (info);
4520         }
4521     }
4522   else
4523     {
4524       GSList *l;
4525
4526       for (l = paths; l; l = l->next)
4527         {
4528           const GtkFilePath *path;
4529           GtkFileInfo *info;
4530
4531           path = l->data;
4532
4533           /* NULL GError */
4534           info = gtk_file_folder_get_info (folder, path, NULL);
4535           if (info)
4536             {
4537               if (!have_hidden)
4538                 have_hidden = gtk_file_info_get_is_hidden (info);
4539
4540               if (!have_filtered)
4541                 have_filtered = get_is_file_filtered (impl, path, info);
4542
4543               gtk_file_info_free (info);
4544
4545               if (have_hidden && have_filtered)
4546                 break; /* we now have all the information we need */
4547             }
4548         }
4549
4550       success = TRUE;
4551     }
4552
4553   g_object_unref (folder);
4554
4555   if (!success)
4556     return FALSE;
4557
4558   if (have_hidden)
4559     g_object_set (impl, "show-hidden", TRUE, NULL);
4560
4561   if (have_filtered)
4562     set_current_filter (impl, NULL);
4563
4564   if (only_one_path)
4565     _gtk_file_system_model_path_do (impl->browse_files_model, only_one_path, select_func, impl);
4566   else
4567     {
4568       GSList *l;
4569
4570       for (l = paths; l; l = l->next)
4571         {
4572           const GtkFilePath *path;
4573
4574           path = l->data;
4575           _gtk_file_system_model_path_do (impl->browse_files_model, path, select_func, impl);
4576         }
4577     }
4578
4579   return TRUE;
4580 }
4581
4582 /* Processes the pending operation when a folder is finished loading */
4583 static void
4584 pending_select_paths_process (GtkFileChooserDefault *impl)
4585 {
4586   g_assert (impl->load_state == LOAD_FINISHED);
4587   g_assert (impl->browse_files_model != NULL);
4588   g_assert (impl->sort_model != NULL);
4589
4590   if (impl->pending_select_paths)
4591     {
4592       /* NULL GError */
4593       show_and_select_paths (impl, impl->current_folder, NULL, impl->pending_select_paths, NULL);
4594       pending_select_paths_free (impl);
4595       browse_files_center_selected_row (impl);
4596     }
4597   else
4598     {
4599       /* We only select the first row if the chooser is actually mapped ---
4600        * selecting the first row is to help the user when he is interacting with
4601        * the chooser, but sometimes a chooser works not on behalf of the user,
4602        * but rather on behalf of something else like GtkFileChooserButton.  In
4603        * that case, the chooser's selection should be what the caller expects,
4604        * as the user can't see that something else got selected.  See bug #165264.
4605        *
4606        * Also, we don't select the first file if we are in SAVE or CREATE_FOLDER
4607        * modes.  Doing so would change the contents of the filename entry.
4608        */
4609       if (GTK_WIDGET_MAPPED (impl)
4610           && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4611         browse_files_select_first_row (impl);
4612     }
4613
4614   g_assert (impl->pending_select_paths == NULL);
4615 }
4616
4617 /* Callback used when the file system model finishes loading */
4618 static void
4619 browse_files_model_finished_loading_cb (GtkFileSystemModel    *model,
4620                                         GtkFileChooserDefault *impl)
4621 {
4622   if (impl->load_state == LOAD_PRELOAD)
4623     {
4624       load_remove_timer (impl);
4625       load_set_model (impl);
4626     }
4627   else if (impl->load_state == LOAD_LOADING)
4628     {
4629       /* Nothing */
4630     }
4631   else
4632     {
4633       /* We can't g_assert_not_reached(), as something other than us may have
4634        *  initiated a folder reload.  See #165556.
4635        */
4636       return;
4637     }
4638
4639   g_assert (impl->load_timeout_id == 0);
4640
4641   impl->load_state = LOAD_FINISHED;
4642
4643   pending_select_paths_process (impl);
4644   set_busy_cursor (impl, FALSE);
4645 }
4646
4647 /* Gets rid of the old list model and creates a new one for the current folder */
4648 static gboolean
4649 set_list_model (GtkFileChooserDefault *impl,
4650                 GError               **error)
4651 {
4652   g_assert (impl->current_folder != NULL);
4653
4654   load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
4655
4656   if (impl->browse_files_model)
4657     {
4658       g_object_unref (impl->browse_files_model);
4659       impl->browse_files_model = NULL;
4660     }
4661
4662   if (impl->sort_model)
4663     {
4664       g_object_unref (impl->sort_model);
4665       impl->sort_model = NULL;
4666     }
4667
4668   set_busy_cursor (impl, TRUE);
4669   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4670
4671   impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4672                                                          impl->current_folder, 0,
4673                                                          GTK_FILE_INFO_ALL,
4674                                                          error);
4675   if (!impl->browse_files_model)
4676     {
4677       set_busy_cursor (impl, FALSE);
4678       return FALSE;
4679     }
4680
4681   load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
4682
4683   g_signal_connect (impl->browse_files_model, "finished-loading",
4684                     G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4685
4686   _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4687
4688   install_list_model_filter (impl);
4689
4690   return TRUE;
4691 }
4692
4693 static void
4694 update_chooser_entry (GtkFileChooserDefault *impl)
4695 {
4696   GtkTreeSelection *selection;
4697   const GtkFileInfo *info;
4698   GtkTreeIter iter;
4699   GtkTreeIter child_iter;
4700
4701   if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4702     return;
4703
4704   g_assert (!impl->select_multiple);
4705   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4706
4707   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4708     return;
4709
4710   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4711                                                   &child_iter,
4712                                                   &iter);
4713
4714   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4715
4716   if (!gtk_file_info_get_is_folder (info))
4717     _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4718                                            gtk_file_info_get_display_name (info));
4719 }
4720
4721 static gboolean
4722 gtk_file_chooser_default_set_current_folder (GtkFileChooser    *chooser,
4723                                              const GtkFilePath *path,
4724                                              GError           **error)
4725 {
4726   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4727   gboolean result;
4728
4729   g_assert (path != NULL);
4730
4731   if (impl->local_only &&
4732       !gtk_file_system_path_is_local (impl->file_system, path))
4733     {
4734       g_set_error (error,
4735                    GTK_FILE_CHOOSER_ERROR,
4736                    GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4737                    _("Cannot change to folder because it is not local"));
4738
4739       return FALSE;
4740     }
4741
4742   /* Test validity of path here.  */
4743   if (!check_is_folder (impl->file_system, path, error))
4744     return FALSE;
4745
4746   if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4747     return FALSE;
4748
4749   if (impl->current_folder != path)
4750     {
4751       if (impl->current_folder)
4752         gtk_file_path_free (impl->current_folder);
4753
4754       impl->current_folder = gtk_file_path_copy (path);
4755     }
4756
4757   /* Update the widgets that may trigger a folder change themselves.  */
4758
4759   if (!impl->changing_folder)
4760     {
4761       impl->changing_folder = TRUE;
4762
4763       shortcuts_update_current_folder (impl);
4764
4765       impl->changing_folder = FALSE;
4766     }
4767
4768   /* Set the folder on the save entry */
4769
4770   _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4771                                            impl->current_folder);
4772
4773   /* Create a new list model.  This is slightly evil; we store the result value
4774    * but perform more actions rather than returning immediately even if it
4775    * generates an error.
4776    */
4777   result = set_list_model (impl, error);
4778
4779   /* Refresh controls */
4780
4781   shortcuts_find_current_folder (impl);
4782
4783   g_signal_emit_by_name (impl, "current-folder-changed", 0);
4784
4785   check_preview_change (impl);
4786   bookmarks_check_add_sensitivity (impl);
4787
4788   g_signal_emit_by_name (impl, "selection-changed", 0);
4789
4790   return result;
4791 }
4792
4793 static GtkFilePath *
4794 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4795 {
4796   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4797
4798   return gtk_file_path_copy (impl->current_folder);
4799 }
4800
4801 static void
4802 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4803                                            const gchar    *name)
4804 {
4805   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4806
4807   g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4808                     || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4809
4810   _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
4811 }
4812
4813 static void
4814 select_func (GtkFileSystemModel *model,
4815              GtkTreePath        *path,
4816              GtkTreeIter        *iter,
4817              gpointer            user_data)
4818 {
4819   GtkFileChooserDefault *impl = user_data;
4820   GtkTreeSelection *selection;
4821   GtkTreeIter sorted_iter;
4822
4823   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4824
4825   gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
4826   gtk_tree_selection_select_iter (selection, &sorted_iter);
4827 }
4828
4829 static gboolean
4830 gtk_file_chooser_default_select_path (GtkFileChooser    *chooser,
4831                                       const GtkFilePath *path,
4832                                       GError           **error)
4833 {
4834   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4835   GtkFilePath *parent_path;
4836   gboolean same_path;
4837
4838   if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4839     return FALSE;
4840
4841   if (!parent_path)
4842     return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4843
4844   if (impl->load_state == LOAD_EMPTY)
4845     same_path = FALSE;
4846   else
4847     {
4848       g_assert (impl->current_folder != NULL);
4849
4850       same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
4851     }
4852
4853   if (same_path && impl->load_state == LOAD_FINISHED)
4854     {
4855       gboolean result;
4856
4857       result = show_and_select_paths (impl, parent_path, path, NULL, error);
4858       gtk_file_path_free (parent_path);
4859       return result;
4860     }
4861
4862   pending_select_paths_add (impl, path);
4863
4864   if (!same_path)
4865     {
4866       gboolean result;
4867
4868       result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4869       gtk_file_path_free (parent_path);
4870       return result;
4871     }
4872
4873   gtk_file_path_free (parent_path);
4874   return TRUE;
4875 }
4876
4877 static void
4878 unselect_func (GtkFileSystemModel *model,
4879                GtkTreePath        *path,
4880                GtkTreeIter        *iter,
4881                gpointer            user_data)
4882 {
4883   GtkFileChooserDefault *impl = user_data;
4884   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4885   GtkTreePath *sorted_path;
4886
4887   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4888                                                                 path);
4889   gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4890                                     sorted_path);
4891   gtk_tree_path_free (sorted_path);
4892 }
4893
4894 static void
4895 gtk_file_chooser_default_unselect_path (GtkFileChooser    *chooser,
4896                                         const GtkFilePath *path)
4897 {
4898   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4899
4900   if (!impl->browse_files_model)
4901     return;
4902
4903   _gtk_file_system_model_path_do (impl->browse_files_model, path,
4904                                   unselect_func, impl);
4905 }
4906
4907 static gboolean
4908 maybe_select (GtkTreeModel *model, 
4909               GtkTreePath  *path, 
4910               GtkTreeIter  *iter, 
4911               gpointer     data)
4912 {
4913   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
4914   GtkTreeSelection *selection;
4915   const GtkFileInfo *info;
4916   gboolean is_folder;
4917   
4918   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4919   
4920   info = get_list_file_info (impl, iter);
4921   is_folder = gtk_file_info_get_is_folder (info);
4922
4923   if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
4924       (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
4925     gtk_tree_selection_select_iter (selection, iter);
4926   else
4927     gtk_tree_selection_unselect_iter (selection, iter);
4928     
4929   return FALSE;
4930 }
4931
4932 static void
4933 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4934 {
4935   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4936   if (impl->select_multiple)
4937     gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model), 
4938                             maybe_select, impl);
4939 }
4940
4941 static void
4942 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4943 {
4944   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4945   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4946
4947   gtk_tree_selection_unselect_all (selection);
4948 }
4949
4950 /* Checks whether the filename entry for the Save modes contains a valid filename */
4951 static GtkFilePath *
4952 check_save_entry (GtkFileChooserDefault *impl,
4953                   gboolean              *is_valid,
4954                   gboolean              *is_empty)
4955 {
4956   GtkFileChooserEntry *chooser_entry;
4957   const GtkFilePath *current_folder;
4958   const char *file_part;
4959   GtkFilePath *path;
4960   GError *error;
4961
4962   g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4963             || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4964
4965   chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4966
4967   current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4968   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4969
4970   if (!file_part || file_part[0] == '\0')
4971     {
4972       *is_valid = FALSE;
4973       *is_empty = TRUE;
4974       return NULL;
4975     }
4976
4977   *is_empty = FALSE;
4978
4979   error = NULL;
4980   path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
4981
4982   if (!path)
4983     {
4984       error_building_filename_dialog (impl, current_folder, file_part, error);
4985       *is_valid = FALSE;
4986       return NULL;
4987     }
4988
4989   *is_valid = TRUE;
4990   return path;
4991 }
4992
4993 struct get_paths_closure {
4994   GtkFileChooserDefault *impl;
4995   GSList *result;
4996   GtkFilePath *path_from_entry;
4997 };
4998
4999 static void
5000 get_paths_foreach (GtkTreeModel *model,
5001                    GtkTreePath  *path,
5002                    GtkTreeIter  *iter,
5003                    gpointer      data)
5004 {
5005   struct get_paths_closure *info;
5006   const GtkFilePath *file_path;
5007   GtkFileSystemModel *fs_model;
5008   GtkTreeIter sel_iter;
5009
5010   info = data;
5011   fs_model = info->impl->browse_files_model;
5012   gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
5013
5014   file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
5015   if (!file_path)
5016     return; /* We are on the editable row */
5017
5018   if (!info->path_from_entry
5019       || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
5020     info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
5021 }
5022
5023 static GSList *
5024 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
5025 {
5026   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5027   struct get_paths_closure info;
5028
5029   info.impl = impl;
5030   info.result = NULL;
5031   info.path_from_entry = NULL;
5032
5033   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5034       || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5035     {
5036       gboolean is_valid, is_empty;
5037
5038       info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
5039       if (!is_valid && !is_empty)
5040         return NULL;
5041     }
5042
5043   if (!info.path_from_entry || impl->select_multiple)
5044     {
5045       GtkTreeSelection *selection;
5046
5047       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5048       gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
5049     }
5050
5051   if (info.path_from_entry)
5052     info.result = g_slist_prepend (info.result, info.path_from_entry);
5053
5054   /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
5055    * fall back to the current directory */
5056   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
5057       info.result == NULL)
5058     {
5059       info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
5060     }
5061
5062   return g_slist_reverse (info.result);
5063 }
5064
5065 static GtkFilePath *
5066 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
5067 {
5068   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5069
5070   if (impl->preview_path)
5071     return gtk_file_path_copy (impl->preview_path);
5072   else
5073     return NULL;
5074 }
5075
5076 static GtkFileSystem *
5077 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
5078 {
5079   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5080
5081   return impl->file_system;
5082 }
5083
5084 /* Shows or hides the filter widgets */
5085 static void
5086 show_filters (GtkFileChooserDefault *impl,
5087               gboolean               show)
5088 {
5089   if (show)
5090     gtk_widget_show (impl->filter_combo_hbox);
5091   else
5092     gtk_widget_hide (impl->filter_combo_hbox);
5093 }
5094
5095 static void
5096 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
5097                                      GtkFileFilter  *filter)
5098 {
5099   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5100   const gchar *name;
5101
5102   if (g_slist_find (impl->filters, filter))
5103     {
5104       g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
5105       return;
5106     }
5107
5108   g_object_ref (filter);
5109   gtk_object_sink (GTK_OBJECT (filter));
5110   impl->filters = g_slist_append (impl->filters, filter);
5111
5112   name = gtk_file_filter_get_name (filter);
5113   if (!name)
5114     name = "Untitled filter";   /* Place-holder, doesn't need to be marked for translation */
5115
5116   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
5117
5118   if (!g_slist_find (impl->filters, impl->current_filter))
5119     set_current_filter (impl, filter);
5120
5121   show_filters (impl, TRUE);
5122 }
5123
5124 static void
5125 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
5126                                         GtkFileFilter  *filter)
5127 {
5128   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5129   GtkTreeModel *model;
5130   GtkTreeIter iter;
5131   gint filter_index;
5132
5133   filter_index = g_slist_index (impl->filters, filter);
5134
5135   if (filter_index < 0)
5136     {
5137       g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
5138       return;
5139     }
5140
5141   impl->filters = g_slist_remove (impl->filters, filter);
5142
5143   if (filter == impl->current_filter)
5144     {
5145       if (impl->filters)
5146         set_current_filter (impl, impl->filters->data);
5147       else
5148         set_current_filter (impl, NULL);
5149     }
5150
5151   /* Remove row from the combo box */
5152   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
5153   gtk_tree_model_iter_nth_child  (model, &iter, NULL, filter_index);
5154   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
5155
5156   g_object_unref (filter);
5157
5158   if (!impl->filters)
5159     show_filters (impl, FALSE);
5160 }
5161
5162 static GSList *
5163 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
5164 {
5165   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5166
5167   return g_slist_copy (impl->filters);
5168 }
5169
5170 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
5171 static int
5172 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
5173                                        int                    pos)
5174 {
5175   return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
5176 }
5177
5178 static gboolean
5179 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser    *chooser,
5180                                               const GtkFilePath *path,
5181                                               GError           **error)
5182 {
5183   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5184   gboolean result;
5185   int pos;
5186
5187   /* Test validity of path here.  */
5188   if (!check_is_folder (impl->file_system, path, error))
5189     return FALSE;
5190
5191   pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
5192
5193   result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
5194
5195   if (result)
5196     impl->num_shortcuts++;
5197
5198   if (impl->shortcuts_filter_model)
5199     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
5200
5201   return result;
5202 }
5203
5204 static gboolean
5205 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
5206                                                  const GtkFilePath *path,
5207                                                  GError           **error)
5208 {
5209   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5210   int pos;
5211   GtkTreeIter iter;
5212   char *uri;
5213   int i;
5214
5215   if (impl->num_shortcuts == 0)
5216     goto out;
5217
5218   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5219   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5220     g_assert_not_reached ();
5221
5222   for (i = 0; i < impl->num_shortcuts; i++)
5223     {
5224       gpointer col_data;
5225       gboolean is_volume;
5226       GtkFilePath *shortcut;
5227
5228       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5229                           SHORTCUTS_COL_DATA, &col_data,
5230                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
5231                           -1);
5232       g_assert (col_data != NULL);
5233       g_assert (!is_volume);
5234
5235       shortcut = col_data;
5236       if (gtk_file_path_compare (shortcut, path) == 0)
5237         {
5238           shortcuts_remove_rows (impl, pos + i, 1);
5239           impl->num_shortcuts--;
5240           return TRUE;
5241         }
5242
5243       if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5244         g_assert_not_reached ();
5245     }
5246
5247  out:
5248
5249   uri = gtk_file_system_path_to_uri (impl->file_system, path);
5250   g_set_error (error,
5251                GTK_FILE_CHOOSER_ERROR,
5252                GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
5253                _("Shortcut %s does not exist"),
5254                uri);
5255   g_free (uri);
5256
5257   return FALSE;
5258 }
5259
5260 static GSList *
5261 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
5262 {
5263   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5264   int pos;
5265   GtkTreeIter iter;
5266   int i;
5267   GSList *list;
5268
5269   if (impl->num_shortcuts == 0)
5270     return NULL;
5271
5272   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5273   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5274     g_assert_not_reached ();
5275
5276   list = NULL;
5277
5278   for (i = 0; i < impl->num_shortcuts; i++)
5279     {
5280       gpointer col_data;
5281       gboolean is_volume;
5282       GtkFilePath *shortcut;
5283
5284       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5285                           SHORTCUTS_COL_DATA, &col_data,
5286                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
5287                           -1);
5288       g_assert (col_data != NULL);
5289       g_assert (!is_volume);
5290
5291       shortcut = col_data;
5292       list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5293
5294       if (i != impl->num_shortcuts - 1)
5295         {
5296           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5297             g_assert_not_reached ();
5298         }
5299     }
5300
5301   return g_slist_reverse (list);
5302 }
5303
5304 /* Guesses a size based upon font sizes */
5305 static void
5306 find_good_size_from_style (GtkWidget *widget,
5307                            gint      *width,
5308                            gint      *height)
5309 {
5310   GtkFileChooserDefault *impl;
5311   gint default_width, default_height;
5312   int font_size;
5313   GtkRequisition req;
5314   GtkRequisition preview_req;
5315
5316   g_assert (widget->style != NULL);
5317   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5318
5319   font_size = pango_font_description_get_size (widget->style->font_desc);
5320   font_size = PANGO_PIXELS (font_size);
5321
5322   default_width = font_size * NUM_CHARS;
5323   default_height = font_size * NUM_LINES;
5324
5325   /* Use at least the requisition size not including the preview widget */
5326   gtk_widget_size_request (widget, &req);
5327
5328   if (impl->preview_widget_active && impl->preview_widget)
5329     gtk_widget_size_request (impl->preview_box, &preview_req);
5330   else
5331     preview_req.width = 0;
5332
5333   default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5334   default_height = MAX (default_height, req.height);
5335
5336   *width = default_width;
5337   *height = default_height;
5338 }
5339
5340 static void
5341 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5342                                            gint                *default_width,
5343                                            gint                *default_height)
5344 {
5345   GtkFileChooserDefault *impl;
5346
5347   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5348
5349   find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5350
5351   if (impl->preview_widget_active && impl->preview_widget)
5352     *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5353 }
5354
5355 static void
5356 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5357                                               gboolean            *resize_horizontally,
5358                                               gboolean            *resize_vertically)
5359 {
5360   GtkFileChooserDefault *impl;
5361
5362   g_return_if_fail (resize_horizontally != NULL);
5363   g_return_if_fail (resize_vertically != NULL);
5364
5365   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5366
5367   *resize_horizontally = TRUE;
5368   *resize_vertically = TRUE;
5369
5370   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5371       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5372     {
5373       if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5374         {
5375           *resize_horizontally = FALSE;
5376           *resize_vertically = FALSE;
5377         }
5378     }
5379 }
5380
5381 struct switch_folder_closure {
5382   GtkFileChooserDefault *impl;
5383   const GtkFilePath *path;
5384   int num_selected;
5385 };
5386
5387 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
5388 static void
5389 switch_folder_foreach_cb (GtkTreeModel      *model,
5390                           GtkTreePath       *path,
5391                           GtkTreeIter       *iter,
5392                           gpointer           data)
5393 {
5394   struct switch_folder_closure *closure;
5395   GtkTreeIter child_iter;
5396
5397   closure = data;
5398
5399   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
5400
5401   closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
5402   closure->num_selected++;
5403 }
5404
5405 /* Changes to the selected folder in the list view */
5406 static void
5407 switch_to_selected_folder (GtkFileChooserDefault *impl)
5408 {
5409   GtkTreeSelection *selection;
5410   struct switch_folder_closure closure;
5411
5412   /* We do this with foreach() rather than get_selected() as we may be in
5413    * multiple selection mode
5414    */
5415
5416   closure.impl = impl;
5417   closure.path = NULL;
5418   closure.num_selected = 0;
5419
5420   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5421   gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
5422
5423   g_assert (closure.path && closure.num_selected == 1);
5424
5425   change_folder_and_display_error (impl, closure.path);
5426 }
5427
5428 /* Implementation for GtkFileChooserEmbed::should_respond() */
5429 static gboolean
5430 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
5431 {
5432   GtkFileChooserDefault *impl;
5433   GtkWidget *toplevel;
5434   GtkWidget *current_focus;
5435
5436   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5437
5438   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5439   g_assert (GTK_IS_WINDOW (toplevel));
5440
5441   current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
5442
5443   if (current_focus == impl->browse_files_tree_view)
5444     {
5445       int num_selected;
5446       gboolean all_files, all_folders;
5447
5448     file_list:
5449
5450       selection_check (impl, &num_selected, &all_files, &all_folders);
5451
5452       if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5453         {
5454           if (num_selected != 1)         
5455             return TRUE; /* zero means current folder; more than one means use the whole selection */    
5456           else if (current_focus != impl->browse_files_tree_view)        
5457             {
5458               /* a single folder is selected and a button was clicked */
5459               switch_to_selected_folder (impl);          
5460               return TRUE;
5461             }
5462         }
5463
5464       if (num_selected == 0)
5465         {
5466           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5467               || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5468             goto save_entry; /* it makes sense to use the typed name */
5469           else
5470             return FALSE;
5471         }
5472
5473       if (num_selected == 1 && all_folders)
5474         {
5475           switch_to_selected_folder (impl);
5476           return FALSE;
5477         }
5478       else
5479         return all_files;
5480     }
5481   else if (current_focus == impl->save_file_name_entry)
5482     {
5483       GtkFilePath *path;
5484       gboolean is_valid, is_empty;
5485       gboolean is_folder;
5486       gboolean retval;
5487       GtkFileChooserEntry *entry;  
5488
5489     save_entry:
5490
5491       g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5492                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5493
5494       entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5495       path = check_save_entry (impl, &is_valid, &is_empty);
5496
5497       if (!is_empty && !is_valid)
5498         return FALSE;
5499
5500       if (is_empty)
5501         path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5502       
5503       is_folder = check_is_folder (impl->file_system, path, NULL);
5504       if (is_folder)
5505         {
5506           _gtk_file_chooser_entry_set_file_part (entry, "");
5507           change_folder_and_display_error (impl, path);
5508           retval = FALSE;
5509         }
5510       else
5511         {
5512           /* check that everything up to the last component exists */
5513           gtk_file_path_free (path);
5514           path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5515           is_folder = check_is_folder (impl->file_system, path, NULL);
5516           if (!is_folder)
5517             {
5518               change_folder_and_display_error (impl, path);
5519               retval = FALSE;
5520             }
5521           else
5522             retval = TRUE;
5523         }
5524
5525       gtk_file_path_free (path);
5526       return retval;
5527     }
5528   else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5529     {
5530       /* The focus is on a dialog's action area button, *and* the widget that
5531        * was focused immediately before it is the shortcuts list.  Switch to the
5532        * selected shortcut and tell the caller not to respond.
5533        */
5534       GtkTreeIter iter;
5535
5536       if (shortcuts_get_selected (impl, &iter))
5537         {
5538           shortcuts_activate_iter (impl, &iter);
5539           
5540           gtk_widget_grab_focus (impl->browse_files_tree_view);
5541         }
5542       else
5543         goto file_list;
5544
5545       return FALSE;
5546     }
5547   else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5548     {
5549       /* The focus is on a dialog's action area button, *and* the widget that
5550        * was focused immediately before it is the file list.  
5551        */
5552       goto file_list;
5553     }
5554   else
5555     /* The focus is on a dialog's action area button or something else */
5556     if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5557         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5558       goto save_entry;
5559     else
5560       goto file_list; 
5561   
5562   g_assert_not_reached ();
5563   return FALSE;
5564 }
5565
5566 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5567 static void
5568 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5569 {
5570   GtkFileChooserDefault *impl;
5571   GtkWidget *widget;
5572
5573   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5574
5575   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5576       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5577     widget = impl->browse_files_tree_view;
5578   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5579            || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5580     widget = impl->save_file_name_entry;
5581   else
5582     {
5583       g_assert_not_reached ();
5584       widget = NULL;
5585     }
5586
5587   gtk_widget_grab_focus (widget);
5588 }
5589
5590 static void
5591 set_current_filter (GtkFileChooserDefault *impl,
5592                     GtkFileFilter         *filter)
5593 {
5594   if (impl->current_filter != filter)
5595     {
5596       int filter_index;
5597
5598       /* NULL filters are allowed to reset to non-filtered status
5599        */
5600       filter_index = g_slist_index (impl->filters, filter);
5601       if (impl->filters && filter && filter_index < 0)
5602         return;
5603
5604       if (impl->current_filter)
5605         g_object_unref (impl->current_filter);
5606       impl->current_filter = filter;
5607       if (impl->current_filter)
5608         {
5609           g_object_ref (impl->current_filter);
5610           gtk_object_sink (GTK_OBJECT (filter));
5611         }
5612
5613       if (impl->filters)
5614         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5615                                   filter_index);
5616
5617       if (impl->browse_files_model)
5618         install_list_model_filter (impl);
5619
5620       g_object_notify (G_OBJECT (impl), "filter");
5621     }
5622 }
5623
5624 static void
5625 filter_combo_changed (GtkComboBox           *combo_box,
5626                       GtkFileChooserDefault *impl)
5627 {
5628   gint new_index = gtk_combo_box_get_active (combo_box);
5629   GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5630
5631   set_current_filter (impl, new_filter);
5632 }
5633
5634 static void
5635 check_preview_change (GtkFileChooserDefault *impl)
5636 {
5637   GtkTreePath *cursor_path;
5638   const GtkFilePath *new_path;
5639   const GtkFileInfo *new_info;
5640
5641   gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5642   if (cursor_path && impl->sort_model)
5643     {
5644       GtkTreeIter iter;
5645       GtkTreeIter child_iter;
5646
5647       gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5648       gtk_tree_path_free (cursor_path);
5649
5650       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5651
5652       new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5653       new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5654     }
5655   else
5656     {
5657       new_path = NULL;
5658       new_info = NULL;
5659     }
5660
5661   if (new_path != impl->preview_path &&
5662       !(new_path && impl->preview_path &&
5663         gtk_file_path_compare (new_path, impl->preview_path) == 0))
5664     {
5665       if (impl->preview_path)
5666         {
5667           gtk_file_path_free (impl->preview_path);
5668           g_free (impl->preview_display_name);
5669         }
5670
5671       if (new_path)
5672         {
5673           impl->preview_path = gtk_file_path_copy (new_path);
5674           impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
5675         }
5676       else
5677         {
5678           impl->preview_path = NULL;
5679           impl->preview_display_name = NULL;
5680         }
5681
5682       if (impl->use_preview_label && impl->preview_label)
5683         gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
5684
5685       g_signal_emit_by_name (impl, "update-preview");
5686     }
5687 }
5688
5689 /* Activates a volume by mounting it if necessary and then switching to its
5690  * base path.
5691  */
5692 static void
5693 shortcuts_activate_volume (GtkFileChooserDefault *impl,
5694                            GtkFileSystemVolume   *volume)
5695 {
5696   GtkFilePath *path;
5697
5698   /* We ref the file chooser since volume_mount() may run a main loop, and the
5699    * user could close the file chooser window in the meantime.
5700    */
5701   g_object_ref (impl);
5702
5703   if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
5704     {
5705       GError *error;
5706       gboolean result;
5707
5708       set_busy_cursor (impl, TRUE);
5709
5710       error = NULL;
5711       result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5712
5713       if (!result)
5714         {
5715           char *msg;
5716
5717           msg = g_strdup_printf (_("Could not mount %s"),
5718                                  gtk_file_system_volume_get_display_name (impl->file_system, volume));
5719           error_message (impl, msg, error->message);
5720           g_free (msg);
5721           g_error_free (error);
5722         }
5723
5724       set_busy_cursor (impl, FALSE);
5725
5726       if (!result)
5727         goto out;
5728     }
5729
5730   path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5731   change_folder_and_display_error (impl, path);
5732   gtk_file_path_free (path);
5733
5734  out:
5735
5736   g_object_unref (impl);
5737 }
5738
5739 /* Opens the folder or volume at the specified iter in the shortcuts model */
5740 static void
5741 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5742                          GtkTreeIter           *iter)
5743 {
5744   gpointer col_data;
5745   gboolean is_volume;
5746
5747   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
5748                       SHORTCUTS_COL_DATA, &col_data,
5749                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
5750                       -1);
5751
5752   if (!col_data)
5753     return; /* We are on a separator */
5754
5755   if (is_volume)
5756     {
5757       GtkFileSystemVolume *volume;
5758
5759       volume = col_data;
5760
5761       shortcuts_activate_volume (impl, volume);
5762     }
5763   else
5764     {
5765       const GtkFilePath *file_path;
5766
5767       file_path = col_data;
5768       change_folder_and_display_error (impl, file_path);
5769     }
5770 }
5771
5772 /* Callback used when a row in the shortcuts list is activated */
5773 static void
5774 shortcuts_row_activated_cb (GtkTreeView           *tree_view,
5775                             GtkTreePath           *path,
5776                             GtkTreeViewColumn     *column,
5777                             GtkFileChooserDefault *impl)
5778 {
5779   GtkTreeIter iter;
5780   GtkTreeIter child_iter;
5781
5782   if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5783     return;
5784
5785   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5786                                                     &child_iter,
5787                                                     &iter);
5788   shortcuts_activate_iter (impl, &child_iter);
5789
5790   gtk_widget_grab_focus (impl->browse_files_tree_view);
5791 }
5792
5793 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5794 static gboolean
5795 shortcuts_key_press_event_cb (GtkWidget             *widget,
5796                               GdkEventKey           *event,
5797                               GtkFileChooserDefault *impl)
5798 {
5799   guint modifiers;
5800
5801   modifiers = gtk_accelerator_get_default_mod_mask ();
5802
5803   if ((event->keyval == GDK_BackSpace
5804       || event->keyval == GDK_Delete
5805       || event->keyval == GDK_KP_Delete)
5806       && (event->state & modifiers) == 0)
5807     {
5808       remove_selected_bookmarks (impl);
5809       return TRUE;
5810     }
5811
5812   return FALSE;
5813 }
5814
5815 static gboolean
5816 shortcuts_select_func  (GtkTreeSelection  *selection,
5817                         GtkTreeModel      *model,
5818                         GtkTreePath       *path,
5819                         gboolean           path_currently_selected,
5820                         gpointer           data)
5821 {
5822   GtkFileChooserDefault *impl = data;
5823
5824   return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5825 }
5826
5827 static gboolean
5828 list_select_func  (GtkTreeSelection  *selection,
5829                    GtkTreeModel      *model,
5830                    GtkTreePath       *path,
5831                    gboolean           path_currently_selected,
5832                    gpointer           data)
5833 {
5834   GtkFileChooserDefault *impl = data;
5835
5836   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5837       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5838     {
5839       GtkTreeIter iter, child_iter;
5840       const GtkFileInfo *info;
5841
5842       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5843         return FALSE;
5844       
5845       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5846
5847       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5848
5849       if (info && !gtk_file_info_get_is_folder (info))
5850         return FALSE;
5851     }
5852     
5853   return TRUE;
5854 }
5855
5856 static void
5857 list_selection_changed (GtkTreeSelection      *selection,
5858                         GtkFileChooserDefault *impl)
5859 {
5860   /* See if we are in the new folder editable row for Save mode */
5861   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5862     {
5863       GtkTreeSelection *selection;
5864       GtkTreeIter iter, child_iter;
5865       const GtkFileInfo *info;
5866
5867       g_assert (!impl->select_multiple);
5868       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5869       if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5870         return;
5871
5872       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5873                                                       &child_iter,
5874                                                       &iter);
5875
5876       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5877       if (!info)
5878         return; /* We are on the editable row for New Folder */
5879     }
5880
5881   update_chooser_entry (impl);
5882   check_preview_change (impl);
5883   bookmarks_check_add_sensitivity (impl);
5884
5885   g_signal_emit_by_name (impl, "selection-changed", 0);
5886 }
5887
5888 /* Callback used when a row in the file list is activated */
5889 static void
5890 list_row_activated (GtkTreeView           *tree_view,
5891                     GtkTreePath           *path,
5892                     GtkTreeViewColumn     *column,
5893                     GtkFileChooserDefault *impl)
5894 {
5895   GtkTreeIter iter, child_iter;
5896   const GtkFileInfo *info;
5897
5898   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5899     return;
5900
5901   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5902
5903   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5904
5905   if (gtk_file_info_get_is_folder (info))
5906     {
5907       const GtkFilePath *file_path;
5908
5909       file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5910       change_folder_and_display_error (impl, file_path);
5911
5912       return;
5913     }
5914
5915   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5916       impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5917     g_signal_emit_by_name (impl, "file-activated");
5918 }
5919
5920 static void
5921 path_bar_clicked (GtkPathBar            *path_bar,
5922                   GtkFilePath           *file_path,
5923                   gboolean               child_is_hidden,
5924                   GtkFileChooserDefault *impl)
5925 {
5926   if (!change_folder_and_display_error (impl, file_path))
5927     return;
5928
5929   /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar".  We should then
5930    * show hidden files so that ".baz" appears in the file list, as it will still
5931    * be shown in the path bar: "/foo/[bar]/.baz"
5932    */
5933   if (child_is_hidden)
5934     g_object_set (impl, "show-hidden", TRUE, NULL);
5935 }
5936
5937 static const GtkFileInfo *
5938 get_list_file_info (GtkFileChooserDefault *impl,
5939                     GtkTreeIter           *iter)
5940 {
5941   GtkTreeIter child_iter;
5942
5943   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5944                                                   &child_iter,
5945                                                   iter);
5946
5947   return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5948 }
5949
5950 static void
5951 list_icon_data_func (GtkTreeViewColumn *tree_column,
5952                      GtkCellRenderer   *cell,
5953                      GtkTreeModel      *tree_model,
5954                      GtkTreeIter       *iter,
5955                      gpointer           data)
5956 {
5957   GtkFileChooserDefault *impl = data;
5958   GtkTreeIter child_iter;
5959   const GtkFilePath *path;
5960   GdkPixbuf *pixbuf;
5961   const GtkFileInfo *info; 
5962   gboolean sensitive = TRUE;
5963   
5964   info = get_list_file_info (impl, iter);
5965
5966   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5967                                                   &child_iter,
5968                                                   iter);
5969   path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5970
5971   if (path)
5972     {
5973       /* FIXME: NULL GError */
5974       pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5975                                             impl->icon_size, NULL);
5976     }
5977   else
5978     {
5979       /* We are on the editable row */
5980       pixbuf = NULL;
5981     }
5982
5983   if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5984                impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
5985     sensitive =  gtk_file_info_get_is_folder (info);    
5986     
5987   g_object_set (cell,
5988                 "pixbuf", pixbuf,
5989                 "sensitive", sensitive,
5990                 NULL);
5991     
5992   if (pixbuf)
5993     g_object_unref (pixbuf);
5994 }
5995
5996 static void
5997 list_name_data_func (GtkTreeViewColumn *tree_column,
5998                      GtkCellRenderer   *cell,
5999                      GtkTreeModel      *tree_model,
6000                      GtkTreeIter       *iter,
6001                      gpointer           data)
6002 {
6003   GtkFileChooserDefault *impl = data;
6004   const GtkFileInfo *info = get_list_file_info (impl, iter);
6005   gboolean sensitive = TRUE;
6006
6007   if (!info)
6008     {
6009       g_object_set (cell,
6010                     "text", _("Type name of new folder"),
6011                     NULL);
6012
6013       return;
6014     }
6015
6016
6017   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6018          || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6019     {
6020       sensitive = gtk_file_info_get_is_folder (info);
6021     } 
6022     
6023   g_object_set (cell,
6024                 "text", gtk_file_info_get_display_name (info),
6025                 "sensitive", sensitive,
6026                 NULL);
6027 }
6028
6029 #if 0
6030 static void
6031 list_size_data_func (GtkTreeViewColumn *tree_column,
6032                      GtkCellRenderer   *cell,
6033                      GtkTreeModel      *tree_model,
6034                      GtkTreeIter       *iter,
6035                      gpointer           data)
6036 {
6037   GtkFileChooserDefault *impl = data;
6038   const GtkFileInfo *info = get_list_file_info (impl, iter);
6039   gint64 size;
6040   gchar *str;
6041   gboolean sensitive = TRUE;
6042
6043   if (!info || gtk_file_info_get_is_folder (info)) 
6044     {
6045       g_object_set (cell,"sensitive", sensitive, NULL);
6046       return;
6047     }
6048
6049   size = gtk_file_info_get_size (info);
6050
6051   if (size < (gint64)1024)
6052     str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
6053   else if (size < (gint64)1024*1024)
6054     str = g_strdup_printf (_("%.1f K"), size / (1024.));
6055   else if (size < (gint64)1024*1024*1024)
6056     str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
6057   else
6058     str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
6059
6060   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6061       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6062     sensitive = FALSE;
6063
6064   g_object_set (cell,
6065                 "text", str,
6066                 "sensitive", sensitive,
6067                 NULL);
6068
6069   g_free (str);
6070 }
6071 #endif
6072
6073 /* Tree column data callback for the file list; fetches the mtime of a file */
6074 static void
6075 list_mtime_data_func (GtkTreeViewColumn *tree_column,
6076                       GtkCellRenderer   *cell,
6077                       GtkTreeModel      *tree_model,
6078                       GtkTreeIter       *iter,
6079                       gpointer           data)
6080 {
6081   GtkFileChooserDefault *impl;
6082   const GtkFileInfo *info;
6083   GtkFileTime time_mtime, time_now;
6084   GDate mtime, now;
6085   int days_diff;
6086   char buf[256];
6087   gboolean sensitive = TRUE;
6088
6089   impl = data;
6090
6091   info = get_list_file_info (impl, iter);
6092   if (!info)
6093     {
6094       g_object_set (cell,
6095                     "text", "",
6096                     "sensitive", TRUE,
6097                     NULL);
6098       return;
6099     }
6100
6101   time_mtime = gtk_file_info_get_modification_time (info);
6102   g_date_set_time (&mtime, (GTime) time_mtime);
6103
6104   time_now = (GTime ) time (NULL);
6105   g_date_set_time (&now, (GTime) time_now);
6106
6107   days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6108
6109   if (days_diff == 0)
6110     strcpy (buf, _("Today"));
6111   else if (days_diff == 1)
6112     strcpy (buf, _("Yesterday"));
6113   else
6114     {
6115       char *format;
6116
6117       if (days_diff > 1 && days_diff < 7)
6118         format = "%A"; /* Days from last week */
6119       else
6120         format = "%x"; /* Any other date */
6121
6122       if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
6123         strcpy (buf, _("Unknown"));
6124     }
6125
6126   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6127       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6128     sensitive = gtk_file_info_get_is_folder (info);
6129
6130   g_object_set (cell,
6131                 "text", buf,
6132                 "sensitive", sensitive,
6133                 NULL);
6134 }
6135
6136 GtkWidget *
6137 _gtk_file_chooser_default_new (const char *file_system)
6138 {
6139   return  g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
6140                         "file-system-backend", file_system,
6141                         NULL);
6142 }
6143
6144 static GtkWidget *
6145 location_entry_create (GtkFileChooserDefault *impl,
6146                        const gchar           *path)
6147 {
6148   GtkWidget *entry;
6149
6150   entry = _gtk_file_chooser_entry_new (TRUE);
6151   /* Pick a good width for the entry */
6152   gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
6153   gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
6154   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
6155   _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
6156   if (path[0])
6157     {
6158       _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), 
6159                                                gtk_file_path_new_steal ((gchar *)path));
6160       _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
6161     }
6162   else
6163     {
6164       _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
6165       if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6166           || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6167         _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
6168       else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6169                || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6170         _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
6171                                                gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
6172       else
6173         g_assert_not_reached ();
6174     }
6175
6176   return GTK_WIDGET (entry);
6177 }
6178
6179 static gboolean
6180 update_from_entry (GtkFileChooserDefault *impl,
6181                    GtkWindow             *parent,
6182                    GtkFileChooserEntry   *chooser_entry)
6183 {
6184   const GtkFilePath *folder_path;
6185   const char *file_part;
6186
6187   folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6188   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6189
6190   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
6191     {
6192       error_message_with_parent (parent,
6193                                  _("Cannot change folder"),
6194                                  _("The folder you specified is an invalid path."));
6195       return FALSE;
6196     }
6197
6198   if (file_part[0] == '\0')
6199     return change_folder_and_display_error (impl, folder_path);
6200   else
6201     {
6202       GtkFileFolder *folder = NULL;
6203       GtkFilePath *subfolder_path = NULL;
6204       GtkFileInfo *info = NULL;
6205       GError *error;
6206       gboolean result;
6207
6208       result = FALSE;
6209
6210       /* If the file part is non-empty, we need to figure out if it refers to a
6211        * folder within folder. We could optimize the case here where the folder
6212        * is already loaded for one of our tree models.
6213        */
6214
6215       error = NULL;
6216       folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
6217
6218       if (!folder)
6219         {
6220           error_getting_info_dialog (impl, folder_path, error);
6221           goto out;
6222         }
6223
6224       error = NULL;
6225       subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
6226
6227       if (!subfolder_path)
6228         {
6229           char *msg;
6230           char *uri;
6231
6232           uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
6233           msg = g_strdup_printf (_("Could not build file name from '%s' and '%s'"),
6234                                  uri, file_part);
6235           error_message (impl, msg, error->message);
6236           g_free (uri);
6237           g_free (msg);
6238           goto out;
6239         }
6240
6241       error = NULL;
6242       info = gtk_file_folder_get_info (folder, subfolder_path, &error);
6243
6244       if (!info)
6245         {
6246           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6247               || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6248             {
6249               if (!change_folder_and_display_error (impl, folder_path))
6250                 goto out;
6251
6252               gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
6253             }
6254           else
6255             error_getting_info_dialog (impl, subfolder_path, error);
6256
6257           goto out;
6258         }
6259
6260       if (gtk_file_info_get_is_folder (info))
6261         result = change_folder_and_display_error (impl, subfolder_path);
6262       else
6263         {
6264           GError *error;
6265
6266           error = NULL;
6267           result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
6268           if (!result)
6269             error_dialog (impl, _("Could not select item"),
6270                           subfolder_path, error);
6271         }
6272
6273     out:
6274
6275       if (folder)
6276         g_object_unref (folder);
6277
6278       gtk_file_path_free (subfolder_path);
6279
6280       if (info)
6281         gtk_file_info_free (info);
6282
6283       return result;
6284     }
6285
6286   g_assert_not_reached ();
6287 }
6288
6289 static void
6290 location_popup_handler (GtkFileChooserDefault *impl,
6291                         const gchar           *path)
6292 {
6293   GtkWidget *dialog;
6294   GtkWindow *toplevel;
6295   GtkWidget *hbox;
6296   GtkWidget *label;
6297   GtkWidget *entry;
6298   gboolean refocus;
6299   const char *title;
6300   const char *accept_stock;
6301
6302   /* Create dialog */
6303
6304   toplevel = get_toplevel (GTK_WIDGET (impl));
6305
6306   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6307       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6308     {
6309       title = _("Open Location");
6310       accept_stock = GTK_STOCK_OPEN;
6311     }
6312   else
6313     {
6314       g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6315                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6316       title = _("Save in Location");
6317       accept_stock = GTK_STOCK_SAVE;
6318     }
6319
6320   dialog = gtk_dialog_new_with_buttons (title,
6321                                         toplevel,
6322                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
6323                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6324                                         accept_stock, GTK_RESPONSE_ACCEPT,
6325                                         NULL);
6326   gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
6327   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
6328   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
6329   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6330
6331   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
6332                                            GTK_RESPONSE_ACCEPT,
6333                                            GTK_RESPONSE_CANCEL,
6334                                            -1);
6335
6336   hbox = gtk_hbox_new (FALSE, 12);
6337   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
6338   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
6339
6340   label = gtk_label_new_with_mnemonic (_("_Location:"));
6341   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
6342
6343   entry = location_entry_create (impl, path);
6344
6345   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
6346   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
6347
6348   /* Run */
6349
6350   gtk_widget_show_all (dialog);
6351   /* If the dialog is brought up by typing the first characters
6352    * of a path, unselect the text in the entry, so that you can
6353    * just type on without erasing the initial part.
6354    */
6355   if (path[0])
6356     gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
6357
6358   refocus = TRUE;
6359
6360   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
6361     {
6362       if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
6363         {
6364           if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6365               || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6366             {
6367               gtk_widget_grab_focus (impl->browse_files_tree_view);
6368             }
6369           else
6370             {
6371               g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6372                         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6373               gtk_widget_grab_focus (impl->save_file_name_entry);
6374             }
6375           refocus = FALSE;
6376         }
6377     }
6378
6379   if (refocus)
6380     {
6381       GtkWindow *toplevel;
6382
6383       toplevel = get_toplevel (GTK_WIDGET (impl));
6384       if (toplevel && toplevel->focus_widget)
6385         gtk_widget_grab_focus (toplevel->focus_widget);
6386     }
6387
6388   gtk_widget_destroy (dialog);
6389 }
6390
6391 /* Handler for the "up-folder" keybinding signal */
6392 static void
6393 up_folder_handler (GtkFileChooserDefault *impl)
6394 {
6395   pending_select_paths_add (impl, impl->current_folder);
6396   _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
6397 }
6398
6399 /* Handler for the "down-folder" keybinding signal */
6400 static void
6401 down_folder_handler (GtkFileChooserDefault *impl)
6402 {
6403   _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
6404 }
6405
6406 /* Handler for the "home-folder" keybinding signal */
6407 static void
6408 home_folder_handler (GtkFileChooserDefault *impl)
6409 {
6410   int pos;
6411   GtkTreeIter iter;
6412
6413   if (!impl->has_home)
6414     return; /* Should we put up an error dialog? */
6415
6416   pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
6417   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6418     g_assert_not_reached ();
6419
6420   shortcuts_activate_iter (impl, &iter);
6421 }
6422
6423 \f
6424
6425 /* Drag and drop interfaces */
6426
6427 static void
6428 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
6429 {
6430 }
6431
6432 static void
6433 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
6434 {
6435   model->impl = NULL;
6436 }
6437
6438 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
6439 static gboolean
6440 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
6441                                       GtkTreePath       *path)
6442 {
6443   ShortcutsModelFilter *model;
6444   int pos;
6445   int bookmarks_pos;
6446
6447   model = SHORTCUTS_MODEL_FILTER (drag_source);
6448
6449   pos = *gtk_tree_path_get_indices (path);
6450   bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
6451
6452   return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
6453 }
6454
6455 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
6456 static gboolean
6457 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
6458                                       GtkTreePath       *path,
6459                                       GtkSelectionData  *selection_data)
6460 {
6461   ShortcutsModelFilter *model;
6462
6463   model = SHORTCUTS_MODEL_FILTER (drag_source);
6464
6465   /* FIXME */
6466
6467   return FALSE;
6468 }
6469
6470 /* Fill the GtkTreeDragSourceIface vtable */
6471 static void
6472 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
6473 {
6474   iface->row_draggable = shortcuts_model_filter_row_draggable;
6475   iface->drag_data_get = shortcuts_model_filter_drag_data_get;
6476 }
6477
6478 #if 0
6479 /* Fill the GtkTreeDragDestIface vtable */
6480 static void
6481 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
6482 {
6483   iface->drag_data_received = shortcuts_model_filter_drag_data_received;
6484   iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
6485 }
6486 #endif
6487
6488 static GtkTreeModel *
6489 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
6490                             GtkTreeModel          *child_model,
6491                             GtkTreePath           *root)
6492 {
6493   ShortcutsModelFilter *model;
6494
6495   model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
6496                         "child_model", child_model,
6497                         "virtual_root", root,
6498                         NULL);
6499
6500   model->impl = impl;
6501
6502   return GTK_TREE_MODEL (model);
6503 }