]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdefault.c
Merged from gtk-2-6:
[~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 = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
3455   gtk_widget_show (combo);
3456
3457   cell = gtk_cell_renderer_pixbuf_new ();
3458   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3459   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3460                                   "pixbuf", SHORTCUTS_COL_PIXBUF,
3461                                   "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3462                                   "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3463                                   NULL);
3464
3465   cell = gtk_cell_renderer_text_new ();
3466   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3467   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3468                                   "text", SHORTCUTS_COL_NAME,
3469                                   "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3470                                   NULL);
3471
3472   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3473                                         shortcuts_row_separator_func,
3474                                         GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3475                                         NULL);
3476
3477   g_signal_connect (combo, "changed",
3478                     G_CALLBACK (save_folder_combo_changed_cb), impl);
3479
3480   return combo;
3481 }
3482
3483 /* Creates the widgets specific to Save mode */
3484 static GtkWidget *
3485 save_widgets_create (GtkFileChooserDefault *impl)
3486 {
3487   GtkWidget *vbox;
3488   GtkWidget *table;
3489   GtkWidget *widget;
3490   GtkWidget *alignment;
3491
3492   vbox = gtk_vbox_new (FALSE, 12);
3493
3494   table = gtk_table_new (2, 2, FALSE);
3495   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3496   gtk_widget_show (table);
3497   gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3498   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3499
3500   /* Name entry */
3501
3502   widget = gtk_label_new_with_mnemonic (_("_Name:"));
3503   gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3504   gtk_table_attach (GTK_TABLE (table), widget,
3505                     0, 1, 0, 1,
3506                     GTK_FILL, GTK_FILL,
3507                     0, 0);
3508   gtk_widget_show (widget);
3509
3510   impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3511   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3512                                            impl->file_system);
3513   gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3514   gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3515   gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3516                     1, 2, 0, 1,
3517                     GTK_EXPAND | GTK_FILL, 0,
3518                     0, 0);
3519   gtk_widget_show (impl->save_file_name_entry);
3520   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3521
3522   /* Folder combo */
3523   impl->save_folder_label = gtk_label_new (NULL);
3524   gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3525   gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3526                     0, 1, 1, 2,
3527                     GTK_FILL, GTK_FILL,
3528                     0, 0);
3529   gtk_widget_show (impl->save_folder_label);
3530
3531   impl->save_folder_combo = save_folder_combo_create (impl);
3532   gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3533                     1, 2, 1, 2,
3534                     GTK_EXPAND | GTK_FILL, GTK_FILL,
3535                     0, 0);
3536   gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3537
3538   /* Expander */
3539   alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3540   gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3541
3542   impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3543   gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3544   g_signal_connect (impl->save_expander, "notify::expanded",
3545                     G_CALLBACK (expander_changed_cb),
3546                     impl);
3547   gtk_widget_show_all (alignment);
3548
3549   return vbox;
3550 }
3551
3552 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3553 static GtkWidget *
3554 browse_widgets_create (GtkFileChooserDefault *impl)
3555 {
3556   GtkWidget *vbox;
3557   GtkWidget *hpaned;
3558   GtkWidget *widget;
3559   GtkSizeGroup *size_group;
3560
3561   /* size group is used by the [+][-] buttons and the filter combo */
3562   size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3563   vbox = gtk_vbox_new (FALSE, 12);
3564
3565   /* Paned widget */
3566   hpaned = gtk_hpaned_new ();
3567   gtk_widget_show (hpaned);
3568   gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3569   gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3570
3571   widget = shortcuts_pane_create (impl, size_group);
3572   gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3573   widget = file_pane_create (impl, size_group);
3574   gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3575
3576   g_object_unref (size_group);
3577
3578   return vbox;
3579 }
3580
3581 static GObject*
3582 gtk_file_chooser_default_constructor (GType                  type,
3583                                       guint                  n_construct_properties,
3584                                       GObjectConstructParam *construct_params)
3585 {
3586   GtkFileChooserDefault *impl;
3587   GObject *object;
3588
3589   object = parent_class->constructor (type,
3590                                       n_construct_properties,
3591                                       construct_params);
3592   impl = GTK_FILE_CHOOSER_DEFAULT (object);
3593
3594   g_assert (impl->file_system);
3595
3596   gtk_widget_push_composite_child ();
3597
3598   /* Shortcuts model */
3599
3600   shortcuts_model_create (impl);
3601
3602   /* Widgets for Save mode */
3603   impl->save_widgets = save_widgets_create (impl);
3604   gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3605
3606   /* The browse widgets */
3607   impl->browse_widgets = browse_widgets_create (impl);
3608   gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3609
3610   /* Alignment to hold extra widget */
3611   impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3612   gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
3613
3614   gtk_widget_pop_composite_child ();
3615   update_appearance (impl);
3616
3617   return object;
3618 }
3619
3620 /* Sets the extra_widget by packing it in the appropriate place */
3621 static void
3622 set_extra_widget (GtkFileChooserDefault *impl,
3623                   GtkWidget             *extra_widget)
3624 {
3625   if (extra_widget)
3626     {
3627       g_object_ref (extra_widget);
3628       /* FIXME: is this right ? */
3629       gtk_widget_show (extra_widget);
3630     }
3631
3632   if (impl->extra_widget)
3633     {
3634       gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3635       g_object_unref (impl->extra_widget);
3636     }
3637
3638   impl->extra_widget = extra_widget;
3639   if (impl->extra_widget)
3640     {
3641       gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3642       gtk_widget_show (impl->extra_align);
3643     }
3644   else
3645     gtk_widget_hide (impl->extra_align);
3646 }
3647
3648 static void
3649 set_local_only (GtkFileChooserDefault *impl,
3650                 gboolean               local_only)
3651 {
3652   if (local_only != impl->local_only)
3653     {
3654       impl->local_only = local_only;
3655
3656       if (impl->shortcuts_model && impl->file_system)
3657         {
3658           shortcuts_add_volumes (impl);
3659           shortcuts_add_bookmarks (impl);
3660         }
3661
3662       if (local_only &&
3663           !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3664         {
3665           /* If we are pointing to a non-local folder, make an effort to change
3666            * back to a local folder, but it's really up to the app to not cause
3667            * such a situation, so we ignore errors.
3668            */
3669           const gchar *home = g_get_home_dir ();
3670           GtkFilePath *home_path;
3671
3672           if (home == NULL)
3673             return;
3674
3675           home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3676
3677           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3678
3679           gtk_file_path_free (home_path);
3680         }
3681     }
3682 }
3683
3684 static void
3685 volumes_changed_cb (GtkFileSystem         *file_system,
3686                     GtkFileChooserDefault *impl)
3687 {
3688   shortcuts_add_volumes (impl);
3689 }
3690
3691 /* Callback used when the set of bookmarks changes in the file system */
3692 static void
3693 bookmarks_changed_cb (GtkFileSystem         *file_system,
3694                       GtkFileChooserDefault *impl)
3695 {
3696   shortcuts_add_bookmarks (impl);
3697
3698   bookmarks_check_add_sensitivity (impl);
3699   bookmarks_check_remove_sensitivity (impl);
3700 }
3701
3702 /* Sets the file chooser to multiple selection mode */
3703 static void
3704 set_select_multiple (GtkFileChooserDefault *impl,
3705                      gboolean               select_multiple,
3706                      gboolean               property_notify)
3707 {
3708   GtkTreeSelection *selection;
3709   GtkSelectionMode mode;
3710
3711   if (select_multiple == impl->select_multiple)
3712     return;
3713
3714   mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3715
3716   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3717   gtk_tree_selection_set_mode (selection, mode);
3718
3719   impl->select_multiple = select_multiple;
3720   g_object_notify (G_OBJECT (impl), "select-multiple");
3721
3722   check_preview_change (impl);
3723 }
3724
3725 static void
3726 set_file_system_backend (GtkFileChooserDefault *impl,
3727                          const char *backend)
3728 {
3729   if (impl->file_system)
3730     {
3731       g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3732       impl->volumes_changed_id = 0;
3733       g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3734       impl->bookmarks_changed_id = 0;
3735       g_object_unref (impl->file_system);
3736     }
3737
3738   impl->file_system = NULL;
3739   if (backend)
3740     impl->file_system = _gtk_file_system_create (backend);
3741   else
3742     {
3743       GtkSettings *settings = gtk_settings_get_default ();
3744       gchar *default_backend = NULL;
3745
3746       g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3747       if (default_backend)
3748         {
3749           impl->file_system = _gtk_file_system_create (default_backend);
3750           g_free (default_backend);
3751         }
3752     }
3753
3754   if (!impl->file_system)
3755     {
3756 #if defined (G_OS_UNIX)
3757       impl->file_system = gtk_file_system_unix_new ();
3758 #elif defined (G_OS_WIN32)
3759       impl->file_system = gtk_file_system_win32_new ();
3760 #else
3761 #error "No default filesystem implementation on the platform"
3762 #endif
3763     }
3764
3765   if (impl->file_system)
3766     {
3767       impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3768                                                    G_CALLBACK (volumes_changed_cb),
3769                                                    impl);
3770       impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3771                                                      G_CALLBACK (bookmarks_changed_cb),
3772                                                      impl);
3773     }
3774 }
3775
3776 /* This function is basically a do_all function.
3777  *
3778  * It sets the visibility on all the widgets based on the current state, and
3779  * moves the custom_widget if needed.
3780  */
3781 static void
3782 update_appearance (GtkFileChooserDefault *impl)
3783 {
3784   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3785       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3786     {
3787       const char *text;
3788
3789       gtk_widget_show (impl->save_widgets);
3790
3791       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3792         text = _("Save in _folder:");
3793       else
3794         text = _("Create in _folder:");
3795
3796       gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3797
3798       if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3799         {
3800           gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3801           gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3802           gtk_widget_show (impl->browse_widgets);
3803         }
3804       else
3805         {
3806           gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3807           gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3808           gtk_widget_hide (impl->browse_widgets);
3809         }
3810
3811       gtk_widget_show (impl->browse_new_folder_button);
3812
3813       if (impl->select_multiple)
3814         {
3815           g_warning ("Save mode cannot be set in conjunction with multiple selection mode.  "
3816                      "Re-setting to single selection mode.");
3817           set_select_multiple (impl, FALSE, TRUE);
3818         }
3819     }
3820   else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3821            impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3822     {
3823       gtk_widget_hide (impl->save_widgets);
3824       gtk_widget_show (impl->browse_widgets);
3825     }
3826
3827   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3828     gtk_widget_hide (impl->browse_new_folder_button);
3829   else
3830     gtk_widget_show (impl->browse_new_folder_button);
3831
3832   gtk_widget_queue_draw (impl->browse_files_tree_view);
3833
3834   g_signal_emit_by_name (impl, "default-size-changed");
3835 }
3836
3837 static void
3838 gtk_file_chooser_default_set_property (GObject      *object,
3839                                        guint         prop_id,
3840                                        const GValue *value,
3841                                        GParamSpec   *pspec)
3842
3843 {
3844   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3845
3846   switch (prop_id)
3847     {
3848     case GTK_FILE_CHOOSER_PROP_ACTION:
3849       {
3850         GtkFileChooserAction action = g_value_get_enum (value);
3851
3852         if (action != impl->action)
3853           {
3854             gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
3855             
3856             if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3857               {
3858                 g_warning ("Multiple selection mode is not allowed in Save mode");
3859                 set_select_multiple (impl, FALSE, TRUE);
3860               }
3861             impl->action = action;
3862             update_appearance (impl);
3863           }
3864         
3865         if (impl->save_file_name_entry)
3866           _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3867                                               action);
3868       }
3869       break;
3870     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3871       set_file_system_backend (impl, g_value_get_string (value));
3872       break;
3873     case GTK_FILE_CHOOSER_PROP_FILTER:
3874       set_current_filter (impl, g_value_get_object (value));
3875       break;
3876     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3877       set_local_only (impl, g_value_get_boolean (value));
3878       break;
3879     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3880       set_preview_widget (impl, g_value_get_object (value));
3881       break;
3882     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3883       impl->preview_widget_active = g_value_get_boolean (value);
3884       update_preview_widget_visibility (impl);
3885       break;
3886     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3887       impl->use_preview_label = g_value_get_boolean (value);
3888       update_preview_widget_visibility (impl);
3889       break;
3890     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3891       set_extra_widget (impl, g_value_get_object (value));
3892       break;
3893     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3894       {
3895         gboolean select_multiple = g_value_get_boolean (value);
3896         if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3897           {
3898             g_warning ("Multiple selection mode is not allowed in Save mode");
3899             return;
3900           }
3901
3902         set_select_multiple (impl, select_multiple, FALSE);
3903       }
3904       break;
3905     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3906       {
3907         gboolean show_hidden = g_value_get_boolean (value);
3908         if (show_hidden != impl->show_hidden)
3909           {
3910             impl->show_hidden = show_hidden;
3911
3912             if (impl->browse_files_model)
3913               _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3914           }
3915       }
3916       break;
3917     default:
3918       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3919       break;
3920     }
3921 }
3922
3923 static void
3924 gtk_file_chooser_default_get_property (GObject    *object,
3925                                        guint       prop_id,
3926                                        GValue     *value,
3927                                        GParamSpec *pspec)
3928 {
3929   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3930
3931   switch (prop_id)
3932     {
3933     case GTK_FILE_CHOOSER_PROP_ACTION:
3934       g_value_set_enum (value, impl->action);
3935       break;
3936     case GTK_FILE_CHOOSER_PROP_FILTER:
3937       g_value_set_object (value, impl->current_filter);
3938       break;
3939     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3940       g_value_set_boolean (value, impl->local_only);
3941       break;
3942     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3943       g_value_set_object (value, impl->preview_widget);
3944       break;
3945     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3946       g_value_set_boolean (value, impl->preview_widget_active);
3947       break;
3948     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3949       g_value_set_boolean (value, impl->use_preview_label);
3950       break;
3951     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3952       g_value_set_object (value, impl->extra_widget);
3953       break;
3954     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3955       g_value_set_boolean (value, impl->select_multiple);
3956       break;
3957     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3958       g_value_set_boolean (value, impl->show_hidden);
3959       break;
3960     default:
3961       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3962       break;
3963     }
3964 }
3965
3966 /* Removes the settings signal handler.  It's safe to call multiple times */
3967 static void
3968 remove_settings_signal (GtkFileChooserDefault *impl,
3969                         GdkScreen             *screen)
3970 {
3971   if (impl->settings_signal_id)
3972     {
3973       GtkSettings *settings;
3974
3975       settings = gtk_settings_get_for_screen (screen);
3976       g_signal_handler_disconnect (settings,
3977                                    impl->settings_signal_id);
3978       impl->settings_signal_id = 0;
3979     }
3980 }
3981
3982 static void
3983 gtk_file_chooser_default_dispose (GObject *object)
3984 {
3985   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3986
3987   if (impl->extra_widget)
3988     {
3989       g_object_unref (impl->extra_widget);
3990       impl->extra_widget = NULL;
3991     }
3992
3993   remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3994
3995   G_OBJECT_CLASS (parent_class)->dispose (object);
3996 }
3997
3998 /* We override show-all since we have internal widgets that
3999  * shouldn't be shown when you call show_all(), like the filter
4000  * combo box.
4001  */
4002 static void
4003 gtk_file_chooser_default_show_all (GtkWidget *widget)
4004 {
4005   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
4006
4007   gtk_widget_show (widget);
4008
4009   if (impl->extra_widget)
4010     gtk_widget_show_all (impl->extra_widget);
4011 }
4012
4013 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
4014  * widget on our toplevel.  See gtk_file_chooser_default_hierarchy_changed()
4015  */
4016 static void
4017 toplevel_set_focus_cb (GtkWindow             *window,
4018                        GtkWidget             *focus,
4019                        GtkFileChooserDefault *impl)
4020 {
4021   impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
4022 }
4023
4024 /* We monitor the focus widget on our toplevel to be able to know which widget
4025  * was last focused at the time our "should_respond" method gets called.
4026  */
4027 static void
4028 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
4029                                             GtkWidget *previous_toplevel)
4030 {
4031   GtkFileChooserDefault *impl;
4032   GtkWidget *toplevel;
4033
4034   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4035
4036   if (previous_toplevel)
4037     {
4038       g_assert (impl->toplevel_set_focus_id != 0);
4039       g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
4040       impl->toplevel_set_focus_id = 0;
4041       impl->toplevel_last_focus_widget = NULL;
4042     }
4043   else
4044     g_assert (impl->toplevel_set_focus_id == 0);
4045
4046   toplevel = gtk_widget_get_toplevel (widget);
4047   if (GTK_IS_WINDOW (toplevel))
4048     {
4049       impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
4050                                                       G_CALLBACK (toplevel_set_focus_cb), impl);
4051       impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
4052     }
4053 }
4054
4055 /* Changes the icons wherever it is needed */
4056 static void
4057 change_icon_theme (GtkFileChooserDefault *impl)
4058 {
4059   GtkSettings *settings;
4060   gint width, height;
4061
4062   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4063
4064   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
4065     impl->icon_size = MAX (width, height);
4066   else
4067     impl->icon_size = FALLBACK_ICON_SIZE;
4068
4069   shortcuts_reload_icons (impl);
4070   gtk_widget_queue_resize (impl->browse_files_tree_view);
4071 }
4072
4073 /* Callback used when a GtkSettings value changes */
4074 static void
4075 settings_notify_cb (GObject               *object,
4076                     GParamSpec            *pspec,
4077                     GtkFileChooserDefault *impl)
4078 {
4079   const char *name;
4080
4081   name = g_param_spec_get_name (pspec);
4082
4083   if (strcmp (name, "gtk-icon-theme-name") == 0
4084       || strcmp (name, "gtk-icon-sizes") == 0)
4085     change_icon_theme (impl);
4086 }
4087
4088 /* Installs a signal handler for GtkSettings so that we can monitor changes in
4089  * the icon theme.
4090  */
4091 static void
4092 check_icon_theme (GtkFileChooserDefault *impl)
4093 {
4094   GtkSettings *settings;
4095
4096   if (impl->settings_signal_id)
4097     return;
4098
4099   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4100     {
4101       settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4102       impl->settings_signal_id = g_signal_connect (settings, "notify",
4103                                                    G_CALLBACK (settings_notify_cb), impl);
4104
4105       change_icon_theme (impl);
4106     }
4107 }
4108
4109 static void
4110 gtk_file_chooser_default_style_set (GtkWidget *widget,
4111                                     GtkStyle  *previous_style)
4112 {
4113   GtkFileChooserDefault *impl;
4114
4115   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4116
4117   if (GTK_WIDGET_CLASS (parent_class)->style_set)
4118     GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
4119
4120   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4121     change_icon_theme (impl);
4122
4123   g_signal_emit_by_name (widget, "default-size-changed");
4124 }
4125
4126 static void
4127 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
4128                                          GdkScreen *previous_screen)
4129 {
4130   GtkFileChooserDefault *impl;
4131
4132   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4133
4134   if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
4135     GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
4136
4137   remove_settings_signal (impl, previous_screen);
4138   check_icon_theme (impl);
4139
4140   g_signal_emit_by_name (widget, "default-size-changed");
4141 }
4142
4143 static gboolean
4144 get_is_file_filtered (GtkFileChooserDefault *impl,
4145                       const GtkFilePath     *path,
4146                       GtkFileInfo           *file_info)
4147 {
4148   GtkFileFilterInfo filter_info;
4149   GtkFileFilterFlags needed;
4150   gboolean result;
4151
4152   if (!impl->current_filter)
4153     return FALSE;
4154
4155   filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4156
4157   needed = gtk_file_filter_get_needed (impl->current_filter);
4158
4159   filter_info.display_name = gtk_file_info_get_display_name (file_info);
4160   filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4161
4162   if (needed & GTK_FILE_FILTER_FILENAME)
4163     {
4164       filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4165       if (filter_info.filename)
4166         filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4167     }
4168   else
4169     filter_info.filename = NULL;
4170
4171   if (needed & GTK_FILE_FILTER_URI)
4172     {
4173       filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4174       if (filter_info.uri)
4175         filter_info.contains |= GTK_FILE_FILTER_URI;
4176     }
4177   else
4178     filter_info.uri = NULL;
4179
4180   result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4181
4182   if (filter_info.filename)
4183     g_free ((gchar *)filter_info.filename);
4184   if (filter_info.uri)
4185     g_free ((gchar *)filter_info.uri);
4186
4187   return !result;
4188 }
4189
4190 /* GtkWidget::map method */
4191 static void
4192 gtk_file_chooser_default_map (GtkWidget *widget)
4193 {
4194   GtkFileChooserDefault *impl;
4195
4196   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4197
4198   GTK_WIDGET_CLASS (parent_class)->map (widget);
4199
4200   if (impl->current_folder)
4201     {
4202       pending_select_paths_store_selection (impl);
4203       change_folder_and_display_error (impl, impl->current_folder);
4204     }
4205
4206   bookmarks_changed_cb (impl->file_system, impl);
4207 }
4208
4209 static gboolean
4210 list_model_filter_func (GtkFileSystemModel *model,
4211                         GtkFilePath        *path,
4212                         const GtkFileInfo  *file_info,
4213                         gpointer            user_data)
4214 {
4215   GtkFileChooserDefault *impl = user_data;
4216
4217   if (!impl->current_filter)
4218     return TRUE;
4219
4220   if (gtk_file_info_get_is_folder (file_info))
4221     return TRUE;
4222
4223   return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4224 }
4225
4226 static void
4227 install_list_model_filter (GtkFileChooserDefault *impl)
4228 {
4229   GtkFileSystemModelFilter filter;
4230   gpointer data;
4231
4232   g_assert (impl->browse_files_model != NULL);
4233
4234   if (impl->current_filter)
4235     {
4236       filter = list_model_filter_func;
4237       data   = impl;
4238     }
4239   else
4240     {
4241       filter = NULL;
4242       data   = NULL;
4243     }
4244   
4245   _gtk_file_system_model_set_filter (impl->browse_files_model,
4246                                      filter,
4247                                      data);
4248 }
4249
4250 #define COMPARE_DIRECTORIES                                                                                    \
4251   GtkFileChooserDefault *impl = user_data;                                                                     \
4252   const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a);                           \
4253   const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b);                           \
4254   gboolean dir_a, dir_b;                                                                                       \
4255                                                                                                                \
4256   if (info_a)                                                                                                  \
4257     dir_a = gtk_file_info_get_is_folder (info_a);                                                              \
4258   else                                                                                                         \
4259     return impl->list_sort_ascending ? -1 : 1;                                                                 \
4260                                                                                                                \
4261   if (info_b)                                                                                                  \
4262     dir_b = gtk_file_info_get_is_folder (info_b);                                                              \
4263   else                                                                                                         \
4264     return impl->list_sort_ascending ? 1 : -1;                                                                 \
4265                                                                                                                \
4266   if (dir_a != dir_b)                                                                                          \
4267     return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4268
4269 /* Sort callback for the filename column */
4270 static gint
4271 name_sort_func (GtkTreeModel *model,
4272                 GtkTreeIter  *a,
4273                 GtkTreeIter  *b,
4274                 gpointer      user_data)
4275 {
4276   COMPARE_DIRECTORIES;
4277   else
4278     return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4279 }
4280
4281 /* Sort callback for the size column */
4282 static gint
4283 size_sort_func (GtkTreeModel *model,
4284                 GtkTreeIter  *a,
4285                 GtkTreeIter  *b,
4286                 gpointer      user_data)
4287 {
4288   COMPARE_DIRECTORIES;
4289   else
4290     {
4291       gint64 size_a = gtk_file_info_get_size (info_a);
4292       gint64 size_b = gtk_file_info_get_size (info_b);
4293
4294       return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4295     }
4296 }
4297
4298 /* Sort callback for the mtime column */
4299 static gint
4300 mtime_sort_func (GtkTreeModel *model,
4301                  GtkTreeIter  *a,
4302                  GtkTreeIter  *b,
4303                  gpointer      user_data)
4304 {
4305   COMPARE_DIRECTORIES;
4306   else
4307     {
4308       GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4309       GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4310
4311       return ta > tb ? -1 : (ta == tb ? 0 : 1);
4312     }
4313 }
4314
4315 /* Callback used when the sort column changes.  We cache the sort order for use
4316  * in name_sort_func().
4317  */
4318 static void
4319 list_sort_column_changed_cb (GtkTreeSortable       *sortable,
4320                              GtkFileChooserDefault *impl)
4321 {
4322   GtkSortType sort_type;
4323
4324   if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4325     impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4326 }
4327
4328 static void
4329 set_busy_cursor (GtkFileChooserDefault *impl,
4330                  gboolean               busy)
4331 {
4332   GtkWindow *toplevel;
4333   GdkDisplay *display;
4334   GdkCursor *cursor;
4335
4336   toplevel = get_toplevel (GTK_WIDGET (impl));
4337   if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4338     return;
4339
4340   display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4341
4342   if (busy)
4343     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4344   else
4345     cursor = NULL;
4346
4347   gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4348   gdk_display_flush (display);
4349
4350   if (cursor)
4351     gdk_cursor_unref (cursor);
4352 }
4353
4354 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4355 static void
4356 load_set_model (GtkFileChooserDefault *impl)
4357 {
4358   g_assert (impl->browse_files_model != NULL);
4359   g_assert (impl->sort_model == NULL);
4360
4361   impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4362   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4363   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4364   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4365   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4366   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4367   impl->list_sort_ascending = TRUE;
4368
4369   g_signal_connect (impl->sort_model, "sort-column-changed",
4370                     G_CALLBACK (list_sort_column_changed_cb), impl);
4371
4372   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4373                            GTK_TREE_MODEL (impl->sort_model));
4374   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4375   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4376                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4377 }
4378
4379 /* Timeout callback used when the loading timer expires */
4380 static gboolean
4381 load_timeout_cb (gpointer data)
4382 {
4383   GtkFileChooserDefault *impl;
4384
4385   GDK_THREADS_ENTER ();
4386
4387   impl = GTK_FILE_CHOOSER_DEFAULT (data);
4388   g_assert (impl->load_state == LOAD_PRELOAD);
4389   g_assert (impl->load_timeout_id != 0);
4390   g_assert (impl->browse_files_model != NULL);
4391
4392   impl->load_timeout_id = 0;
4393   impl->load_state = LOAD_LOADING;
4394
4395   load_set_model (impl);
4396
4397   GDK_THREADS_LEAVE ();
4398
4399   return FALSE;
4400 }
4401
4402 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4403 static void
4404 load_setup_timer (GtkFileChooserDefault *impl)
4405 {
4406   g_assert (impl->load_timeout_id == 0);
4407   g_assert (impl->load_state != LOAD_PRELOAD);
4408
4409   impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4410   impl->load_state = LOAD_PRELOAD;
4411 }
4412
4413 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4414 static void
4415 load_remove_timer (GtkFileChooserDefault *impl)
4416 {
4417   if (impl->load_timeout_id != 0)
4418     {
4419       g_assert (impl->load_state == LOAD_PRELOAD);
4420
4421       g_source_remove (impl->load_timeout_id);
4422       impl->load_timeout_id = 0;
4423       impl->load_state = LOAD_EMPTY;
4424     }
4425   else
4426     g_assert (impl->load_state == LOAD_EMPTY ||
4427               impl->load_state == LOAD_LOADING ||
4428               impl->load_state == LOAD_FINISHED);
4429 }
4430
4431 /* Selects the first row in the file list */
4432 static void
4433 browse_files_select_first_row (GtkFileChooserDefault *impl)
4434 {
4435   GtkTreePath *path;
4436
4437   if (!impl->sort_model)
4438     return;
4439
4440   path = gtk_tree_path_new_from_indices (0, -1);
4441   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4442   gtk_tree_path_free (path);
4443 }
4444
4445 struct center_selected_row_closure {
4446   GtkFileChooserDefault *impl;
4447   gboolean already_centered;
4448 };
4449
4450 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4451  * selected row in the tree view.
4452  */
4453 static void
4454 center_selected_row_foreach_cb (GtkTreeModel      *model,
4455                                 GtkTreePath       *path,
4456                                 GtkTreeIter       *iter,
4457                                 gpointer           data)
4458 {
4459   struct center_selected_row_closure *closure;
4460
4461   closure = data;
4462   if (closure->already_centered)
4463     return;
4464
4465   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
4466   closure->already_centered = TRUE;
4467 }
4468
4469 /* Centers the selected row in the tree view */
4470 static void
4471 browse_files_center_selected_row (GtkFileChooserDefault *impl)
4472 {
4473   struct center_selected_row_closure closure;
4474   GtkTreeSelection *selection;
4475
4476   closure.impl = impl;
4477   closure.already_centered = FALSE;
4478
4479   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4480   gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
4481 }
4482
4483 static gboolean
4484 show_and_select_paths (GtkFileChooserDefault *impl,
4485                        const GtkFilePath     *parent_path,
4486                        const GtkFilePath     *only_one_path,
4487                        GSList                *paths,
4488                        GError               **error)
4489 {
4490   GtkFileFolder *folder;
4491   gboolean success;
4492   gboolean have_hidden;
4493   gboolean have_filtered;
4494
4495   if (!only_one_path && !paths)
4496     return TRUE;
4497
4498   folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4499   if (!folder)
4500     return FALSE;
4501
4502   success = FALSE;
4503   have_hidden = FALSE;
4504   have_filtered = FALSE;
4505
4506   if (only_one_path)
4507     {
4508       GtkFileInfo *info;
4509
4510       info = gtk_file_folder_get_info (folder, only_one_path, error);
4511       if (info)
4512         {
4513           success = TRUE;
4514           have_hidden = gtk_file_info_get_is_hidden (info);
4515           have_filtered = get_is_file_filtered (impl, only_one_path, info);
4516           gtk_file_info_free (info);
4517         }
4518     }
4519   else
4520     {
4521       GSList *l;
4522
4523       for (l = paths; l; l = l->next)
4524         {
4525           const GtkFilePath *path;
4526           GtkFileInfo *info;
4527
4528           path = l->data;
4529
4530           /* NULL GError */
4531           info = gtk_file_folder_get_info (folder, path, NULL);
4532           if (info)
4533             {
4534               if (!have_hidden)
4535                 have_hidden = gtk_file_info_get_is_hidden (info);
4536
4537               if (!have_filtered)
4538                 have_filtered = get_is_file_filtered (impl, path, info);
4539
4540               gtk_file_info_free (info);
4541
4542               if (have_hidden && have_filtered)
4543                 break; /* we now have all the information we need */
4544             }
4545         }
4546
4547       success = TRUE;
4548     }
4549
4550   g_object_unref (folder);
4551
4552   if (!success)
4553     return FALSE;
4554
4555   if (have_hidden)
4556     g_object_set (impl, "show-hidden", TRUE, NULL);
4557
4558   if (have_filtered)
4559     set_current_filter (impl, NULL);
4560
4561   if (only_one_path)
4562     _gtk_file_system_model_path_do (impl->browse_files_model, only_one_path, select_func, impl);
4563   else
4564     {
4565       GSList *l;
4566
4567       for (l = paths; l; l = l->next)
4568         {
4569           const GtkFilePath *path;
4570
4571           path = l->data;
4572           _gtk_file_system_model_path_do (impl->browse_files_model, path, select_func, impl);
4573         }
4574     }
4575
4576   return TRUE;
4577 }
4578
4579 /* Processes the pending operation when a folder is finished loading */
4580 static void
4581 pending_select_paths_process (GtkFileChooserDefault *impl)
4582 {
4583   g_assert (impl->load_state == LOAD_FINISHED);
4584   g_assert (impl->browse_files_model != NULL);
4585   g_assert (impl->sort_model != NULL);
4586
4587   if (impl->pending_select_paths)
4588     {
4589       /* NULL GError */
4590       show_and_select_paths (impl, impl->current_folder, NULL, impl->pending_select_paths, NULL);
4591       pending_select_paths_free (impl);
4592       browse_files_center_selected_row (impl);
4593     }
4594   else
4595     browse_files_select_first_row (impl);
4596
4597   g_assert (impl->pending_select_paths == NULL);
4598 }
4599
4600 /* Callback used when the file system model finishes loading */
4601 static void
4602 browse_files_model_finished_loading_cb (GtkFileSystemModel    *model,
4603                                         GtkFileChooserDefault *impl)
4604 {
4605   if (impl->load_state == LOAD_PRELOAD)
4606     {
4607       load_remove_timer (impl);
4608       load_set_model (impl);
4609     }
4610   else if (impl->load_state == LOAD_LOADING)
4611     {
4612       /* Nothing */
4613     }
4614   else
4615     {
4616       /* We can't g_assert_not_reached(), as something other than us may have
4617        *  initiated a folder reload.  See #165556.
4618        */
4619       return;
4620     }
4621
4622   g_assert (impl->load_timeout_id == 0);
4623
4624   impl->load_state = LOAD_FINISHED;
4625
4626   pending_select_paths_process (impl);
4627   set_busy_cursor (impl, FALSE);
4628 }
4629
4630 /* Gets rid of the old list model and creates a new one for the current folder */
4631 static gboolean
4632 set_list_model (GtkFileChooserDefault *impl,
4633                 GError               **error)
4634 {
4635   g_assert (impl->current_folder != NULL);
4636
4637   load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
4638
4639   if (impl->browse_files_model)
4640     {
4641       g_object_unref (impl->browse_files_model);
4642       impl->browse_files_model = NULL;
4643     }
4644
4645   if (impl->sort_model)
4646     {
4647       g_object_unref (impl->sort_model);
4648       impl->sort_model = NULL;
4649     }
4650
4651   set_busy_cursor (impl, TRUE);
4652   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4653
4654   impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4655                                                          impl->current_folder, 0,
4656                                                          GTK_FILE_INFO_ALL,
4657                                                          error);
4658   if (!impl->browse_files_model)
4659     {
4660       set_busy_cursor (impl, FALSE);
4661       return FALSE;
4662     }
4663
4664   load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
4665
4666   g_signal_connect (impl->browse_files_model, "finished-loading",
4667                     G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4668
4669   _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4670
4671   install_list_model_filter (impl);
4672
4673   return TRUE;
4674 }
4675
4676 static void
4677 update_chooser_entry (GtkFileChooserDefault *impl)
4678 {
4679   GtkTreeSelection *selection;
4680   const GtkFileInfo *info;
4681   GtkTreeIter iter;
4682   GtkTreeIter child_iter;
4683
4684   if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4685     return;
4686
4687   g_assert (!impl->select_multiple);
4688   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4689
4690   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4691     return;
4692
4693   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4694                                                   &child_iter,
4695                                                   &iter);
4696
4697   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4698
4699   if (!gtk_file_info_get_is_folder (info))
4700     _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4701                                            gtk_file_info_get_display_name (info));
4702 }
4703
4704 static gboolean
4705 gtk_file_chooser_default_set_current_folder (GtkFileChooser    *chooser,
4706                                              const GtkFilePath *path,
4707                                              GError           **error)
4708 {
4709   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4710   gboolean result;
4711
4712   g_assert (path != NULL);
4713
4714   if (impl->local_only &&
4715       !gtk_file_system_path_is_local (impl->file_system, path))
4716     {
4717       g_set_error (error,
4718                    GTK_FILE_CHOOSER_ERROR,
4719                    GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4720                    _("Cannot change to folder because it is not local"));
4721
4722       return FALSE;
4723     }
4724
4725   /* Test validity of path here.  */
4726   if (!check_is_folder (impl->file_system, path, error))
4727     return FALSE;
4728
4729   if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4730     return FALSE;
4731
4732   if (impl->current_folder != path)
4733     {
4734       if (impl->current_folder)
4735         gtk_file_path_free (impl->current_folder);
4736
4737       impl->current_folder = gtk_file_path_copy (path);
4738     }
4739
4740   /* Update the widgets that may trigger a folder change themselves.  */
4741
4742   if (!impl->changing_folder)
4743     {
4744       impl->changing_folder = TRUE;
4745
4746       shortcuts_update_current_folder (impl);
4747
4748       impl->changing_folder = FALSE;
4749     }
4750
4751   /* Set the folder on the save entry */
4752
4753   _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4754                                            impl->current_folder);
4755
4756   /* Create a new list model.  This is slightly evil; we store the result value
4757    * but perform more actions rather than returning immediately even if it
4758    * generates an error.
4759    */
4760   result = set_list_model (impl, error);
4761
4762   /* Refresh controls */
4763
4764   shortcuts_find_current_folder (impl);
4765
4766   g_signal_emit_by_name (impl, "current-folder-changed", 0);
4767
4768   check_preview_change (impl);
4769   bookmarks_check_add_sensitivity (impl);
4770
4771   g_signal_emit_by_name (impl, "selection-changed", 0);
4772
4773   return result;
4774 }
4775
4776 static GtkFilePath *
4777 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4778 {
4779   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4780
4781   return gtk_file_path_copy (impl->current_folder);
4782 }
4783
4784 static void
4785 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4786                                            const gchar    *name)
4787 {
4788   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4789
4790   g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4791                     || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4792
4793   _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
4794 }
4795
4796 static void
4797 select_func (GtkFileSystemModel *model,
4798              GtkTreePath        *path,
4799              GtkTreeIter        *iter,
4800              gpointer            user_data)
4801 {
4802   GtkFileChooserDefault *impl = user_data;
4803   GtkTreeSelection *selection;
4804   GtkTreeIter sorted_iter;
4805
4806   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4807
4808   gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
4809   gtk_tree_selection_select_iter (selection, &sorted_iter);
4810 }
4811
4812 static gboolean
4813 gtk_file_chooser_default_select_path (GtkFileChooser    *chooser,
4814                                       const GtkFilePath *path,
4815                                       GError           **error)
4816 {
4817   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4818   GtkFilePath *parent_path;
4819   gboolean same_path;
4820
4821   if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4822     return FALSE;
4823
4824   if (!parent_path)
4825     return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4826
4827   if (impl->load_state == LOAD_EMPTY)
4828     same_path = FALSE;
4829   else
4830     {
4831       g_assert (impl->current_folder != NULL);
4832
4833       same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
4834     }
4835
4836   if (same_path && impl->load_state == LOAD_FINISHED)
4837     {
4838       gboolean result;
4839
4840       result = show_and_select_paths (impl, parent_path, path, NULL, error);
4841       gtk_file_path_free (parent_path);
4842       return result;
4843     }
4844
4845   pending_select_paths_add (impl, path);
4846
4847   if (!same_path)
4848     {
4849       gboolean result;
4850
4851       result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4852       gtk_file_path_free (parent_path);
4853       return result;
4854     }
4855
4856   return TRUE;
4857 }
4858
4859 static void
4860 unselect_func (GtkFileSystemModel *model,
4861                GtkTreePath        *path,
4862                GtkTreeIter        *iter,
4863                gpointer            user_data)
4864 {
4865   GtkFileChooserDefault *impl = user_data;
4866   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4867   GtkTreePath *sorted_path;
4868
4869   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4870                                                                 path);
4871   gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4872                                     sorted_path);
4873   gtk_tree_path_free (sorted_path);
4874 }
4875
4876 static void
4877 gtk_file_chooser_default_unselect_path (GtkFileChooser    *chooser,
4878                                         const GtkFilePath *path)
4879 {
4880   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4881
4882   if (!impl->browse_files_model)
4883     return;
4884
4885   _gtk_file_system_model_path_do (impl->browse_files_model, path,
4886                                   unselect_func, impl);
4887 }
4888
4889 static gboolean
4890 maybe_select (GtkTreeModel *model, 
4891               GtkTreePath  *path, 
4892               GtkTreeIter  *iter, 
4893               gpointer     data)
4894 {
4895   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
4896   GtkTreeSelection *selection;
4897   const GtkFileInfo *info;
4898   gboolean is_folder;
4899   
4900   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4901   
4902   info = get_list_file_info (impl, iter);
4903   is_folder = gtk_file_info_get_is_folder (info);
4904
4905   if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
4906       (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
4907     gtk_tree_selection_select_iter (selection, iter);
4908   else
4909     gtk_tree_selection_unselect_iter (selection, iter);
4910     
4911   return FALSE;
4912 }
4913
4914 static void
4915 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4916 {
4917   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4918   if (impl->select_multiple)
4919     gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model), 
4920                             maybe_select, impl);
4921 }
4922
4923 static void
4924 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4925 {
4926   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4927   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4928
4929   gtk_tree_selection_unselect_all (selection);
4930 }
4931
4932 /* Checks whether the filename entry for the Save modes contains a valid filename */
4933 static GtkFilePath *
4934 check_save_entry (GtkFileChooserDefault *impl,
4935                   gboolean              *is_valid,
4936                   gboolean              *is_empty)
4937 {
4938   GtkFileChooserEntry *chooser_entry;
4939   const GtkFilePath *current_folder;
4940   const char *file_part;
4941   GtkFilePath *path;
4942   GError *error;
4943
4944   g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4945             || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4946
4947   chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4948
4949   current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4950   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4951
4952   if (!file_part || file_part[0] == '\0')
4953     {
4954       *is_valid = FALSE;
4955       *is_empty = TRUE;
4956       return NULL;
4957     }
4958
4959   *is_empty = FALSE;
4960
4961   error = NULL;
4962   path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
4963
4964   if (!path)
4965     {
4966       error_building_filename_dialog (impl, current_folder, file_part, error);
4967       *is_valid = FALSE;
4968       return NULL;
4969     }
4970
4971   *is_valid = TRUE;
4972   return path;
4973 }
4974
4975 struct get_paths_closure {
4976   GtkFileChooserDefault *impl;
4977   GSList *result;
4978   GtkFilePath *path_from_entry;
4979 };
4980
4981 static void
4982 get_paths_foreach (GtkTreeModel *model,
4983                    GtkTreePath  *path,
4984                    GtkTreeIter  *iter,
4985                    gpointer      data)
4986 {
4987   struct get_paths_closure *info;
4988   const GtkFilePath *file_path;
4989   GtkFileSystemModel *fs_model;
4990   GtkTreeIter sel_iter;
4991
4992   info = data;
4993   fs_model = info->impl->browse_files_model;
4994   gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
4995
4996   file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
4997   if (!file_path)
4998     return; /* We are on the editable row */
4999
5000   if (!info->path_from_entry
5001       || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
5002     info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
5003 }
5004
5005 static GSList *
5006 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
5007 {
5008   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5009   struct get_paths_closure info;
5010
5011   info.impl = impl;
5012   info.result = NULL;
5013   info.path_from_entry = NULL;
5014
5015   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5016       || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5017     {
5018       gboolean is_valid, is_empty;
5019
5020       info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
5021       if (!is_valid && !is_empty)
5022         return NULL;
5023     }
5024
5025   if (!info.path_from_entry || impl->select_multiple)
5026     {
5027       GtkTreeSelection *selection;
5028
5029       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5030       gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
5031     }
5032
5033   if (info.path_from_entry)
5034     info.result = g_slist_prepend (info.result, info.path_from_entry);
5035
5036   /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
5037    * fall back to the current directory */
5038   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
5039       info.result == NULL)
5040     {
5041       info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
5042     }
5043
5044   return g_slist_reverse (info.result);
5045 }
5046
5047 static GtkFilePath *
5048 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
5049 {
5050   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5051
5052   if (impl->preview_path)
5053     return gtk_file_path_copy (impl->preview_path);
5054   else
5055     return NULL;
5056 }
5057
5058 static GtkFileSystem *
5059 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
5060 {
5061   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5062
5063   return impl->file_system;
5064 }
5065
5066 /* Shows or hides the filter widgets */
5067 static void
5068 show_filters (GtkFileChooserDefault *impl,
5069               gboolean               show)
5070 {
5071   if (show)
5072     gtk_widget_show (impl->filter_combo_hbox);
5073   else
5074     gtk_widget_hide (impl->filter_combo_hbox);
5075 }
5076
5077 static void
5078 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
5079                                      GtkFileFilter  *filter)
5080 {
5081   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5082   const gchar *name;
5083
5084   if (g_slist_find (impl->filters, filter))
5085     {
5086       g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
5087       return;
5088     }
5089
5090   g_object_ref (filter);
5091   gtk_object_sink (GTK_OBJECT (filter));
5092   impl->filters = g_slist_append (impl->filters, filter);
5093
5094   name = gtk_file_filter_get_name (filter);
5095   if (!name)
5096     name = "Untitled filter";   /* Place-holder, doesn't need to be marked for translation */
5097
5098   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
5099
5100   if (!g_slist_find (impl->filters, impl->current_filter))
5101     set_current_filter (impl, filter);
5102
5103   show_filters (impl, TRUE);
5104 }
5105
5106 static void
5107 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
5108                                         GtkFileFilter  *filter)
5109 {
5110   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5111   GtkTreeModel *model;
5112   GtkTreeIter iter;
5113   gint filter_index;
5114
5115   filter_index = g_slist_index (impl->filters, filter);
5116
5117   if (filter_index < 0)
5118     {
5119       g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
5120       return;
5121     }
5122
5123   impl->filters = g_slist_remove (impl->filters, filter);
5124
5125   if (filter == impl->current_filter)
5126     {
5127       if (impl->filters)
5128         set_current_filter (impl, impl->filters->data);
5129       else
5130         set_current_filter (impl, NULL);
5131     }
5132
5133   /* Remove row from the combo box */
5134   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
5135   gtk_tree_model_iter_nth_child  (model, &iter, NULL, filter_index);
5136   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
5137
5138   g_object_unref (filter);
5139
5140   if (!impl->filters)
5141     show_filters (impl, FALSE);
5142 }
5143
5144 static GSList *
5145 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
5146 {
5147   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5148
5149   return g_slist_copy (impl->filters);
5150 }
5151
5152 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
5153 static int
5154 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
5155                                        int                    pos)
5156 {
5157   return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
5158 }
5159
5160 static gboolean
5161 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser    *chooser,
5162                                               const GtkFilePath *path,
5163                                               GError           **error)
5164 {
5165   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5166   gboolean result;
5167   int pos;
5168
5169   /* Test validity of path here.  */
5170   if (!check_is_folder (impl->file_system, path, error))
5171     return FALSE;
5172
5173   pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
5174
5175   result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
5176
5177   if (result)
5178     impl->num_shortcuts++;
5179
5180   if (impl->shortcuts_filter_model)
5181     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
5182
5183   return result;
5184 }
5185
5186 static gboolean
5187 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
5188                                                  const GtkFilePath *path,
5189                                                  GError           **error)
5190 {
5191   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5192   int pos;
5193   GtkTreeIter iter;
5194   char *uri;
5195   int i;
5196
5197   if (impl->num_shortcuts == 0)
5198     goto out;
5199
5200   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5201   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5202     g_assert_not_reached ();
5203
5204   for (i = 0; i < impl->num_shortcuts; i++)
5205     {
5206       gpointer col_data;
5207       gboolean is_volume;
5208       GtkFilePath *shortcut;
5209
5210       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5211                           SHORTCUTS_COL_DATA, &col_data,
5212                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
5213                           -1);
5214       g_assert (col_data != NULL);
5215       g_assert (!is_volume);
5216
5217       shortcut = col_data;
5218       if (gtk_file_path_compare (shortcut, path) == 0)
5219         {
5220           shortcuts_remove_rows (impl, pos + i, 1);
5221           impl->num_shortcuts--;
5222           return TRUE;
5223         }
5224
5225       if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5226         g_assert_not_reached ();
5227     }
5228
5229  out:
5230
5231   uri = gtk_file_system_path_to_uri (impl->file_system, path);
5232   g_set_error (error,
5233                GTK_FILE_CHOOSER_ERROR,
5234                GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
5235                _("Shortcut %s does not exist"),
5236                uri);
5237   g_free (uri);
5238
5239   return FALSE;
5240 }
5241
5242 static GSList *
5243 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
5244 {
5245   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5246   int pos;
5247   GtkTreeIter iter;
5248   int i;
5249   GSList *list;
5250
5251   if (impl->num_shortcuts == 0)
5252     return NULL;
5253
5254   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5255   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5256     g_assert_not_reached ();
5257
5258   list = NULL;
5259
5260   for (i = 0; i < impl->num_shortcuts; i++)
5261     {
5262       gpointer col_data;
5263       gboolean is_volume;
5264       GtkFilePath *shortcut;
5265
5266       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5267                           SHORTCUTS_COL_DATA, &col_data,
5268                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
5269                           -1);
5270       g_assert (col_data != NULL);
5271       g_assert (!is_volume);
5272
5273       shortcut = col_data;
5274       list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5275
5276       if (i != impl->num_shortcuts - 1)
5277         {
5278           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5279             g_assert_not_reached ();
5280         }
5281     }
5282
5283   return g_slist_reverse (list);
5284 }
5285
5286 /* Guesses a size based upon font sizes */
5287 static void
5288 find_good_size_from_style (GtkWidget *widget,
5289                            gint      *width,
5290                            gint      *height)
5291 {
5292   GtkFileChooserDefault *impl;
5293   gint default_width, default_height;
5294   int font_size;
5295   GtkRequisition req;
5296   GtkRequisition preview_req;
5297
5298   g_assert (widget->style != NULL);
5299   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5300
5301   font_size = pango_font_description_get_size (widget->style->font_desc);
5302   font_size = PANGO_PIXELS (font_size);
5303
5304   default_width = font_size * NUM_CHARS;
5305   default_height = font_size * NUM_LINES;
5306
5307   /* Use at least the requisition size not including the preview widget */
5308   gtk_widget_size_request (widget, &req);
5309
5310   if (impl->preview_widget_active && impl->preview_widget)
5311     gtk_widget_size_request (impl->preview_box, &preview_req);
5312   else
5313     preview_req.width = 0;
5314
5315   default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5316   default_height = MAX (default_height, req.height);
5317
5318   *width = default_width;
5319   *height = default_height;
5320 }
5321
5322 static void
5323 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5324                                            gint                *default_width,
5325                                            gint                *default_height)
5326 {
5327   GtkFileChooserDefault *impl;
5328
5329   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5330
5331   find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5332
5333   if (impl->preview_widget_active && impl->preview_widget)
5334     *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5335 }
5336
5337 static void
5338 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5339                                               gboolean            *resize_horizontally,
5340                                               gboolean            *resize_vertically)
5341 {
5342   GtkFileChooserDefault *impl;
5343
5344   g_return_if_fail (resize_horizontally != NULL);
5345   g_return_if_fail (resize_vertically != NULL);
5346
5347   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5348
5349   *resize_horizontally = TRUE;
5350   *resize_vertically = TRUE;
5351
5352   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5353       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5354     {
5355       if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5356         {
5357           *resize_horizontally = FALSE;
5358           *resize_vertically = FALSE;
5359         }
5360     }
5361 }
5362
5363 struct switch_folder_closure {
5364   GtkFileChooserDefault *impl;
5365   const GtkFilePath *path;
5366   int num_selected;
5367 };
5368
5369 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
5370 static void
5371 switch_folder_foreach_cb (GtkTreeModel      *model,
5372                           GtkTreePath       *path,
5373                           GtkTreeIter       *iter,
5374                           gpointer           data)
5375 {
5376   struct switch_folder_closure *closure;
5377   GtkTreeIter child_iter;
5378
5379   closure = data;
5380
5381   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
5382
5383   closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
5384   closure->num_selected++;
5385 }
5386
5387 /* Changes to the selected folder in the list view */
5388 static void
5389 switch_to_selected_folder (GtkFileChooserDefault *impl)
5390 {
5391   GtkTreeSelection *selection;
5392   struct switch_folder_closure closure;
5393
5394   /* We do this with foreach() rather than get_selected() as we may be in
5395    * multiple selection mode
5396    */
5397
5398   closure.impl = impl;
5399   closure.path = NULL;
5400   closure.num_selected = 0;
5401
5402   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5403   gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
5404
5405   g_assert (closure.path && closure.num_selected == 1);
5406
5407   change_folder_and_display_error (impl, closure.path);
5408 }
5409
5410 /* Implementation for GtkFileChooserEmbed::should_respond() */
5411 static gboolean
5412 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
5413 {
5414   GtkFileChooserDefault *impl;
5415   GtkWidget *toplevel;
5416   GtkWidget *current_focus;
5417
5418   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5419
5420   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5421   g_assert (GTK_IS_WINDOW (toplevel));
5422
5423   current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
5424
5425   if (current_focus == impl->browse_files_tree_view)
5426     {
5427       int num_selected;
5428       gboolean all_files, all_folders;
5429
5430     file_list:
5431
5432       selection_check (impl, &num_selected, &all_files, &all_folders);
5433
5434       if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5435         {
5436           if (num_selected != 1)         
5437             return TRUE; /* zero means current folder; more than one means use the whole selection */    
5438           else if (current_focus != impl->browse_files_tree_view)        
5439             {
5440               /* a single folder is selected and a button was clicked */
5441               switch_to_selected_folder (impl);          
5442               return TRUE;
5443             }
5444         }
5445
5446       if (num_selected == 0)
5447         {
5448           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5449               || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5450             goto save_entry; /* it makes sense to use the typed name */
5451           else
5452             return FALSE;
5453         }
5454
5455       if (num_selected == 1 && all_folders)
5456         {
5457           switch_to_selected_folder (impl);
5458           return FALSE;
5459         }
5460       else
5461         return all_files;
5462     }
5463   else if (current_focus == impl->save_file_name_entry)
5464     {
5465       GtkFilePath *path;
5466       gboolean is_valid, is_empty;
5467       gboolean is_folder;
5468       gboolean retval;
5469       GtkFileChooserEntry *entry;  
5470
5471     save_entry:
5472
5473       g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5474                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5475
5476       entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5477       path = check_save_entry (impl, &is_valid, &is_empty);
5478
5479       if (!is_empty && !is_valid)
5480         return FALSE;
5481
5482       if (is_empty)
5483         path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5484       
5485       is_folder = check_is_folder (impl->file_system, path, NULL);
5486       if (is_folder)
5487         {
5488           _gtk_file_chooser_entry_set_file_part (entry, "");
5489           change_folder_and_display_error (impl, path);
5490           retval = FALSE;
5491         }
5492       else
5493         {
5494           /* check that everything up to the last component exists */
5495           gtk_file_path_free (path);
5496           path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5497           is_folder = check_is_folder (impl->file_system, path, NULL);
5498           if (!is_folder)
5499             {
5500               change_folder_and_display_error (impl, path);
5501               retval = FALSE;
5502             }
5503           else
5504             retval = TRUE;
5505         }
5506
5507       gtk_file_path_free (path);
5508       return retval;
5509     }
5510   else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5511     {
5512       /* The focus is on a dialog's action area button, *and* the widget that
5513        * was focused immediately before it is the shortcuts list.  Switch to the
5514        * selected shortcut and tell the caller not to respond.
5515        */
5516       GtkTreeIter iter;
5517
5518       if (shortcuts_get_selected (impl, &iter))
5519         {
5520           shortcuts_activate_iter (impl, &iter);
5521           
5522           gtk_widget_grab_focus (impl->browse_files_tree_view);
5523         }
5524       else
5525         goto file_list;
5526
5527       return FALSE;
5528     }
5529   else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5530     {
5531       /* The focus is on a dialog's action area button, *and* the widget that
5532        * was focused immediately before it is the file list.  
5533        */
5534       goto file_list;
5535     }
5536   else
5537     /* The focus is on a dialog's action area button or something else */
5538     if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5539         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5540       goto save_entry;
5541     else
5542       goto file_list; 
5543   
5544   g_assert_not_reached ();
5545   return FALSE;
5546 }
5547
5548 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5549 static void
5550 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5551 {
5552   GtkFileChooserDefault *impl;
5553   GtkWidget *widget;
5554
5555   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5556
5557   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5558       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5559     widget = impl->browse_files_tree_view;
5560   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5561            || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5562     widget = impl->save_file_name_entry;
5563   else
5564     {
5565       g_assert_not_reached ();
5566       widget = NULL;
5567     }
5568
5569   gtk_widget_grab_focus (widget);
5570 }
5571
5572 static void
5573 set_current_filter (GtkFileChooserDefault *impl,
5574                     GtkFileFilter         *filter)
5575 {
5576   if (impl->current_filter != filter)
5577     {
5578       int filter_index;
5579
5580       /* NULL filters are allowed to reset to non-filtered status
5581        */
5582       filter_index = g_slist_index (impl->filters, filter);
5583       if (impl->filters && filter && filter_index < 0)
5584         return;
5585
5586       if (impl->current_filter)
5587         g_object_unref (impl->current_filter);
5588       impl->current_filter = filter;
5589       if (impl->current_filter)
5590         {
5591           g_object_ref (impl->current_filter);
5592           gtk_object_sink (GTK_OBJECT (filter));
5593         }
5594
5595       if (impl->filters)
5596         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5597                                   filter_index);
5598
5599       if (impl->browse_files_model)
5600         install_list_model_filter (impl);
5601
5602       g_object_notify (G_OBJECT (impl), "filter");
5603     }
5604 }
5605
5606 static void
5607 filter_combo_changed (GtkComboBox           *combo_box,
5608                       GtkFileChooserDefault *impl)
5609 {
5610   gint new_index = gtk_combo_box_get_active (combo_box);
5611   GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5612
5613   set_current_filter (impl, new_filter);
5614 }
5615
5616 static void
5617 check_preview_change (GtkFileChooserDefault *impl)
5618 {
5619   GtkTreePath *cursor_path;
5620   const GtkFilePath *new_path;
5621   const GtkFileInfo *new_info;
5622
5623   gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5624   if (cursor_path && impl->sort_model)
5625     {
5626       GtkTreeIter iter;
5627       GtkTreeIter child_iter;
5628
5629       gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5630       gtk_tree_path_free (cursor_path);
5631
5632       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5633
5634       new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5635       new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5636     }
5637   else
5638     {
5639       new_path = NULL;
5640       new_info = NULL;
5641     }
5642
5643   if (new_path != impl->preview_path &&
5644       !(new_path && impl->preview_path &&
5645         gtk_file_path_compare (new_path, impl->preview_path) == 0))
5646     {
5647       if (impl->preview_path)
5648         {
5649           gtk_file_path_free (impl->preview_path);
5650           g_free (impl->preview_display_name);
5651         }
5652
5653       if (new_path)
5654         {
5655           impl->preview_path = gtk_file_path_copy (new_path);
5656           impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
5657         }
5658       else
5659         {
5660           impl->preview_path = NULL;
5661           impl->preview_display_name = NULL;
5662         }
5663
5664       if (impl->use_preview_label && impl->preview_label)
5665         gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
5666
5667       g_signal_emit_by_name (impl, "update-preview");
5668     }
5669 }
5670
5671 /* Activates a volume by mounting it if necessary and then switching to its
5672  * base path.
5673  */
5674 static void
5675 shortcuts_activate_volume (GtkFileChooserDefault *impl,
5676                            GtkFileSystemVolume   *volume)
5677 {
5678   GtkFilePath *path;
5679
5680   /* We ref the file chooser since volume_mount() may run a main loop, and the
5681    * user could close the file chooser window in the meantime.
5682    */
5683   g_object_ref (impl);
5684
5685   if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
5686     {
5687       GError *error;
5688       gboolean result;
5689
5690       set_busy_cursor (impl, TRUE);
5691
5692       error = NULL;
5693       result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5694
5695       if (!result)
5696         {
5697           char *msg;
5698
5699           msg = g_strdup_printf (_("Could not mount %s"),
5700                                  gtk_file_system_volume_get_display_name (impl->file_system, volume));
5701           error_message (impl, msg, error->message);
5702           g_free (msg);
5703           g_error_free (error);
5704         }
5705
5706       set_busy_cursor (impl, FALSE);
5707
5708       if (!result)
5709         goto out;
5710     }
5711
5712   path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5713   change_folder_and_display_error (impl, path);
5714   gtk_file_path_free (path);
5715
5716  out:
5717
5718   g_object_unref (impl);
5719 }
5720
5721 /* Opens the folder or volume at the specified iter in the shortcuts model */
5722 static void
5723 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5724                          GtkTreeIter           *iter)
5725 {
5726   gpointer col_data;
5727   gboolean is_volume;
5728
5729   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
5730                       SHORTCUTS_COL_DATA, &col_data,
5731                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
5732                       -1);
5733
5734   if (!col_data)
5735     return; /* We are on a separator */
5736
5737   if (is_volume)
5738     {
5739       GtkFileSystemVolume *volume;
5740
5741       volume = col_data;
5742
5743       shortcuts_activate_volume (impl, volume);
5744     }
5745   else
5746     {
5747       const GtkFilePath *file_path;
5748
5749       file_path = col_data;
5750       change_folder_and_display_error (impl, file_path);
5751     }
5752 }
5753
5754 /* Callback used when a row in the shortcuts list is activated */
5755 static void
5756 shortcuts_row_activated_cb (GtkTreeView           *tree_view,
5757                             GtkTreePath           *path,
5758                             GtkTreeViewColumn     *column,
5759                             GtkFileChooserDefault *impl)
5760 {
5761   GtkTreeIter iter;
5762   GtkTreeIter child_iter;
5763
5764   if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5765     return;
5766
5767   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5768                                                     &child_iter,
5769                                                     &iter);
5770   shortcuts_activate_iter (impl, &child_iter);
5771
5772   gtk_widget_grab_focus (impl->browse_files_tree_view);
5773 }
5774
5775 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5776 static gboolean
5777 shortcuts_key_press_event_cb (GtkWidget             *widget,
5778                               GdkEventKey           *event,
5779                               GtkFileChooserDefault *impl)
5780 {
5781   guint modifiers;
5782
5783   modifiers = gtk_accelerator_get_default_mod_mask ();
5784
5785   if ((event->keyval == GDK_BackSpace
5786       || event->keyval == GDK_Delete
5787       || event->keyval == GDK_KP_Delete)
5788       && (event->state & modifiers) == 0)
5789     {
5790       remove_selected_bookmarks (impl);
5791       return TRUE;
5792     }
5793
5794   return FALSE;
5795 }
5796
5797 static gboolean
5798 shortcuts_select_func  (GtkTreeSelection  *selection,
5799                         GtkTreeModel      *model,
5800                         GtkTreePath       *path,
5801                         gboolean           path_currently_selected,
5802                         gpointer           data)
5803 {
5804   GtkFileChooserDefault *impl = data;
5805
5806   return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5807 }
5808
5809 static gboolean
5810 list_select_func  (GtkTreeSelection  *selection,
5811                    GtkTreeModel      *model,
5812                    GtkTreePath       *path,
5813                    gboolean           path_currently_selected,
5814                    gpointer           data)
5815 {
5816   GtkFileChooserDefault *impl = data;
5817
5818   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5819       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5820     {
5821       GtkTreeIter iter, child_iter;
5822       const GtkFileInfo *info;
5823
5824       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5825         return FALSE;
5826       
5827       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5828
5829       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5830
5831       if (info && !gtk_file_info_get_is_folder (info))
5832         return FALSE;
5833     }
5834     
5835   return TRUE;
5836 }
5837
5838 static void
5839 list_selection_changed (GtkTreeSelection      *selection,
5840                         GtkFileChooserDefault *impl)
5841 {
5842   /* See if we are in the new folder editable row for Save mode */
5843   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5844     {
5845       GtkTreeSelection *selection;
5846       GtkTreeIter iter, child_iter;
5847       const GtkFileInfo *info;
5848
5849       g_assert (!impl->select_multiple);
5850       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5851       if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5852         return;
5853
5854       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5855                                                       &child_iter,
5856                                                       &iter);
5857
5858       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5859       if (!info)
5860         return; /* We are on the editable row for New Folder */
5861     }
5862
5863   update_chooser_entry (impl);
5864   check_preview_change (impl);
5865   bookmarks_check_add_sensitivity (impl);
5866
5867   g_signal_emit_by_name (impl, "selection-changed", 0);
5868 }
5869
5870 /* Callback used when a row in the file list is activated */
5871 static void
5872 list_row_activated (GtkTreeView           *tree_view,
5873                     GtkTreePath           *path,
5874                     GtkTreeViewColumn     *column,
5875                     GtkFileChooserDefault *impl)
5876 {
5877   GtkTreeIter iter, child_iter;
5878   const GtkFileInfo *info;
5879
5880   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5881     return;
5882
5883   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5884
5885   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5886
5887   if (gtk_file_info_get_is_folder (info))
5888     {
5889       const GtkFilePath *file_path;
5890
5891       file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5892       change_folder_and_display_error (impl, file_path);
5893
5894       return;
5895     }
5896
5897   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5898       impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5899     g_signal_emit_by_name (impl, "file-activated");
5900 }
5901
5902 static void
5903 path_bar_clicked (GtkPathBar            *path_bar,
5904                   GtkFilePath           *file_path,
5905                   gboolean               child_is_hidden,
5906                   GtkFileChooserDefault *impl)
5907 {
5908   if (!change_folder_and_display_error (impl, file_path))
5909     return;
5910
5911   /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar".  We should then
5912    * show hidden files so that ".baz" appears in the file list, as it will still
5913    * be shown in the path bar: "/foo/[bar]/.baz"
5914    */
5915   if (child_is_hidden)
5916     g_object_set (impl, "show-hidden", TRUE, NULL);
5917 }
5918
5919 static const GtkFileInfo *
5920 get_list_file_info (GtkFileChooserDefault *impl,
5921                     GtkTreeIter           *iter)
5922 {
5923   GtkTreeIter child_iter;
5924
5925   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5926                                                   &child_iter,
5927                                                   iter);
5928
5929   return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5930 }
5931
5932 static void
5933 list_icon_data_func (GtkTreeViewColumn *tree_column,
5934                      GtkCellRenderer   *cell,
5935                      GtkTreeModel      *tree_model,
5936                      GtkTreeIter       *iter,
5937                      gpointer           data)
5938 {
5939   GtkFileChooserDefault *impl = data;
5940   GtkTreeIter child_iter;
5941   const GtkFilePath *path;
5942   GdkPixbuf *pixbuf;
5943   const GtkFileInfo *info; 
5944   gboolean sensitive = TRUE;
5945   
5946   info = get_list_file_info (impl, iter);
5947
5948   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5949                                                   &child_iter,
5950                                                   iter);
5951   path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5952
5953   if (path)
5954     {
5955       /* FIXME: NULL GError */
5956       pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5957                                             impl->icon_size, NULL);
5958     }
5959   else
5960     {
5961       /* We are on the editable row */
5962       pixbuf = NULL;
5963     }
5964
5965   if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5966                impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
5967     sensitive =  gtk_file_info_get_is_folder (info);    
5968     
5969   g_object_set (cell,
5970                 "pixbuf", pixbuf,
5971                 "sensitive", sensitive,
5972                 NULL);
5973     
5974   if (pixbuf)
5975     g_object_unref (pixbuf);
5976 }
5977
5978 static void
5979 list_name_data_func (GtkTreeViewColumn *tree_column,
5980                      GtkCellRenderer   *cell,
5981                      GtkTreeModel      *tree_model,
5982                      GtkTreeIter       *iter,
5983                      gpointer           data)
5984 {
5985   GtkFileChooserDefault *impl = data;
5986   const GtkFileInfo *info = get_list_file_info (impl, iter);
5987   gboolean sensitive = TRUE;
5988
5989   if (!info)
5990     {
5991       g_object_set (cell,
5992                     "text", _("Type name of new folder"),
5993                     NULL);
5994
5995       return;
5996     }
5997
5998
5999   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6000          || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6001     {
6002       sensitive = gtk_file_info_get_is_folder (info);
6003     } 
6004     
6005   g_object_set (cell,
6006                 "text", gtk_file_info_get_display_name (info),
6007                 "sensitive", sensitive,
6008                 NULL);
6009 }
6010
6011 #if 0
6012 static void
6013 list_size_data_func (GtkTreeViewColumn *tree_column,
6014                      GtkCellRenderer   *cell,
6015                      GtkTreeModel      *tree_model,
6016                      GtkTreeIter       *iter,
6017                      gpointer           data)
6018 {
6019   GtkFileChooserDefault *impl = data;
6020   const GtkFileInfo *info = get_list_file_info (impl, iter);
6021   gint64 size;
6022   gchar *str;
6023   gboolean sensitive = TRUE;
6024
6025   if (!info || gtk_file_info_get_is_folder (info)) 
6026     {
6027       g_object_set (cell,"sensitive", sensitive, NULL);
6028       return;
6029     }
6030
6031   size = gtk_file_info_get_size (info);
6032
6033   if (size < (gint64)1024)
6034     str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
6035   else if (size < (gint64)1024*1024)
6036     str = g_strdup_printf (_("%.1f K"), size / (1024.));
6037   else if (size < (gint64)1024*1024*1024)
6038     str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
6039   else
6040     str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
6041
6042   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6043       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6044     sensitive = FALSE;
6045
6046   g_object_set (cell,
6047                 "text", str,
6048                 "sensitive", sensitive,
6049                 NULL);
6050
6051   g_free (str);
6052 }
6053 #endif
6054
6055 /* Tree column data callback for the file list; fetches the mtime of a file */
6056 static void
6057 list_mtime_data_func (GtkTreeViewColumn *tree_column,
6058                       GtkCellRenderer   *cell,
6059                       GtkTreeModel      *tree_model,
6060                       GtkTreeIter       *iter,
6061                       gpointer           data)
6062 {
6063   GtkFileChooserDefault *impl;
6064   const GtkFileInfo *info;
6065   GtkFileTime time_mtime, time_now;
6066   GDate mtime, now;
6067   int days_diff;
6068   char buf[256];
6069   gboolean sensitive = TRUE;
6070
6071   impl = data;
6072
6073   info = get_list_file_info (impl, iter);
6074   if (!info)
6075     {
6076       g_object_set (cell,
6077                     "text", "",
6078                     "sensitive", TRUE,
6079                     NULL);
6080       return;
6081     }
6082
6083   time_mtime = gtk_file_info_get_modification_time (info);
6084   g_date_set_time (&mtime, (GTime) time_mtime);
6085
6086   time_now = (GTime ) time (NULL);
6087   g_date_set_time (&now, (GTime) time_now);
6088
6089   days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6090
6091   if (days_diff == 0)
6092     strcpy (buf, _("Today"));
6093   else if (days_diff == 1)
6094     strcpy (buf, _("Yesterday"));
6095   else
6096     {
6097       char *format;
6098
6099       if (days_diff > 1 && days_diff < 7)
6100         format = "%A"; /* Days from last week */
6101       else
6102         format = "%x"; /* Any other date */
6103
6104       if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
6105         strcpy (buf, _("Unknown"));
6106     }
6107
6108   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6109       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6110     sensitive = gtk_file_info_get_is_folder (info);
6111
6112   g_object_set (cell,
6113                 "text", buf,
6114                 "sensitive", sensitive,
6115                 NULL);
6116 }
6117
6118 GtkWidget *
6119 _gtk_file_chooser_default_new (const char *file_system)
6120 {
6121   return  g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
6122                         "file-system-backend", file_system,
6123                         NULL);
6124 }
6125
6126 static GtkWidget *
6127 location_entry_create (GtkFileChooserDefault *impl,
6128                        const gchar           *path)
6129 {
6130   GtkWidget *entry;
6131
6132   entry = _gtk_file_chooser_entry_new (TRUE);
6133   /* Pick a good width for the entry */
6134   gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
6135   gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
6136   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
6137   _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
6138   if (path[0])
6139     {
6140       _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), 
6141                                                gtk_file_path_new_steal ((gchar *)path));
6142       _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
6143     }
6144   else
6145     {
6146       _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
6147       if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6148           || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6149         _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
6150       else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6151                || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6152         _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
6153                                                gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
6154       else
6155         g_assert_not_reached ();
6156     }
6157
6158   return GTK_WIDGET (entry);
6159 }
6160
6161 static gboolean
6162 update_from_entry (GtkFileChooserDefault *impl,
6163                    GtkWindow             *parent,
6164                    GtkFileChooserEntry   *chooser_entry)
6165 {
6166   const GtkFilePath *folder_path;
6167   const char *file_part;
6168
6169   folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6170   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6171
6172   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
6173     {
6174       error_message_with_parent (parent,
6175                                  _("Cannot change folder"),
6176                                  _("The folder you specified is an invalid path."));
6177       return FALSE;
6178     }
6179
6180   if (file_part[0] == '\0')
6181     return change_folder_and_display_error (impl, folder_path);
6182   else
6183     {
6184       GtkFileFolder *folder = NULL;
6185       GtkFilePath *subfolder_path = NULL;
6186       GtkFileInfo *info = NULL;
6187       GError *error;
6188       gboolean result;
6189
6190       result = FALSE;
6191
6192       /* If the file part is non-empty, we need to figure out if it refers to a
6193        * folder within folder. We could optimize the case here where the folder
6194        * is already loaded for one of our tree models.
6195        */
6196
6197       error = NULL;
6198       folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
6199
6200       if (!folder)
6201         {
6202           error_getting_info_dialog (impl, folder_path, error);
6203           goto out;
6204         }
6205
6206       error = NULL;
6207       subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
6208
6209       if (!subfolder_path)
6210         {
6211           char *msg;
6212           char *uri;
6213
6214           uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
6215           msg = g_strdup_printf (_("Could not build file name from '%s' and '%s'"),
6216                                  uri, file_part);
6217           error_message (impl, msg, error->message);
6218           g_free (uri);
6219           g_free (msg);
6220           goto out;
6221         }
6222
6223       error = NULL;
6224       info = gtk_file_folder_get_info (folder, subfolder_path, &error);
6225
6226       if (!info)
6227         {
6228           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6229               || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6230             {
6231               if (!change_folder_and_display_error (impl, folder_path))
6232                 goto out;
6233
6234               gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
6235             }
6236           else
6237             error_getting_info_dialog (impl, subfolder_path, error);
6238
6239           goto out;
6240         }
6241
6242       if (gtk_file_info_get_is_folder (info))
6243         result = change_folder_and_display_error (impl, subfolder_path);
6244       else
6245         {
6246           GError *error;
6247
6248           error = NULL;
6249           result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
6250           if (!result)
6251             error_dialog (impl, _("Could not select item"),
6252                           subfolder_path, error);
6253         }
6254
6255     out:
6256
6257       if (folder)
6258         g_object_unref (folder);
6259
6260       gtk_file_path_free (subfolder_path);
6261
6262       if (info)
6263         gtk_file_info_free (info);
6264
6265       return result;
6266     }
6267
6268   g_assert_not_reached ();
6269 }
6270
6271 static void
6272 location_popup_handler (GtkFileChooserDefault *impl,
6273                         const gchar           *path)
6274 {
6275   GtkWidget *dialog;
6276   GtkWindow *toplevel;
6277   GtkWidget *hbox;
6278   GtkWidget *label;
6279   GtkWidget *entry;
6280   gboolean refocus;
6281   const char *title;
6282   const char *accept_stock;
6283
6284   /* Create dialog */
6285
6286   toplevel = get_toplevel (GTK_WIDGET (impl));
6287
6288   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6289       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6290     {
6291       title = _("Open Location");
6292       accept_stock = GTK_STOCK_OPEN;
6293     }
6294   else
6295     {
6296       g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6297                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6298       title = _("Save in Location");
6299       accept_stock = GTK_STOCK_SAVE;
6300     }
6301
6302   dialog = gtk_dialog_new_with_buttons (title,
6303                                         toplevel,
6304                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
6305                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6306                                         accept_stock, GTK_RESPONSE_ACCEPT,
6307                                         NULL);
6308   gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
6309   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
6310   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
6311   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6312
6313   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
6314                                            GTK_RESPONSE_ACCEPT,
6315                                            GTK_RESPONSE_CANCEL,
6316                                            -1);
6317
6318   hbox = gtk_hbox_new (FALSE, 12);
6319   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
6320   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
6321
6322   label = gtk_label_new_with_mnemonic (_("_Location:"));
6323   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
6324
6325   entry = location_entry_create (impl, path);
6326
6327   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
6328   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
6329
6330   /* Run */
6331
6332   gtk_widget_show_all (dialog);
6333   /* If the dialog is brought up by typing the first characters
6334    * of a path, unselect the text in the entry, so that you can
6335    * just type on without erasing the initial part.
6336    */
6337   if (path[0])
6338     gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
6339
6340   refocus = TRUE;
6341
6342   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
6343     {
6344       if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
6345         {
6346           if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6347               || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6348             {
6349               gtk_widget_grab_focus (impl->browse_files_tree_view);
6350             }
6351           else
6352             {
6353               g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6354                         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6355               gtk_widget_grab_focus (impl->save_file_name_entry);
6356             }
6357           refocus = FALSE;
6358         }
6359     }
6360
6361   if (refocus)
6362     {
6363       GtkWindow *toplevel;
6364
6365       toplevel = get_toplevel (GTK_WIDGET (impl));
6366       if (toplevel && toplevel->focus_widget)
6367         gtk_widget_grab_focus (toplevel->focus_widget);
6368     }
6369
6370   gtk_widget_destroy (dialog);
6371 }
6372
6373 /* Handler for the "up-folder" keybinding signal */
6374 static void
6375 up_folder_handler (GtkFileChooserDefault *impl)
6376 {
6377   pending_select_paths_add (impl, impl->current_folder);
6378   _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
6379 }
6380
6381 /* Handler for the "down-folder" keybinding signal */
6382 static void
6383 down_folder_handler (GtkFileChooserDefault *impl)
6384 {
6385   _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
6386 }
6387
6388 /* Handler for the "home-folder" keybinding signal */
6389 static void
6390 home_folder_handler (GtkFileChooserDefault *impl)
6391 {
6392   int pos;
6393   GtkTreeIter iter;
6394
6395   if (!impl->has_home)
6396     return; /* Should we put up an error dialog? */
6397
6398   pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
6399   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6400     g_assert_not_reached ();
6401
6402   shortcuts_activate_iter (impl, &iter);
6403 }
6404
6405 \f
6406
6407 /* Drag and drop interfaces */
6408
6409 static void
6410 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
6411 {
6412 }
6413
6414 static void
6415 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
6416 {
6417   model->impl = NULL;
6418 }
6419
6420 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
6421 static gboolean
6422 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
6423                                       GtkTreePath       *path)
6424 {
6425   ShortcutsModelFilter *model;
6426   int pos;
6427   int bookmarks_pos;
6428
6429   model = SHORTCUTS_MODEL_FILTER (drag_source);
6430
6431   pos = *gtk_tree_path_get_indices (path);
6432   bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
6433
6434   return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
6435 }
6436
6437 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
6438 static gboolean
6439 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
6440                                       GtkTreePath       *path,
6441                                       GtkSelectionData  *selection_data)
6442 {
6443   ShortcutsModelFilter *model;
6444
6445   model = SHORTCUTS_MODEL_FILTER (drag_source);
6446
6447   /* FIXME */
6448
6449   return FALSE;
6450 }
6451
6452 /* Fill the GtkTreeDragSourceIface vtable */
6453 static void
6454 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
6455 {
6456   iface->row_draggable = shortcuts_model_filter_row_draggable;
6457   iface->drag_data_get = shortcuts_model_filter_drag_data_get;
6458 }
6459
6460 #if 0
6461 /* Fill the GtkTreeDragDestIface vtable */
6462 static void
6463 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
6464 {
6465   iface->drag_data_received = shortcuts_model_filter_drag_data_received;
6466   iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
6467 }
6468 #endif
6469
6470 static GtkTreeModel *
6471 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
6472                             GtkTreeModel          *child_model,
6473                             GtkTreePath           *root)
6474 {
6475   ShortcutsModelFilter *model;
6476
6477   model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
6478                         "child_model", child_model,
6479                         "virtual_root", root,
6480                         NULL);
6481
6482   model->impl = impl;
6483
6484   return GTK_TREE_MODEL (model);
6485 }