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