]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdefault.c
New flag shortcuts_current_folder_is_volume.
[~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 "gdk/gdkkeysyms.h"
22 #include "gtkalignment.h"
23 #include "gtkbindings.h"
24 #include "gtkbutton.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrendererpixbuf.h"
27 #include "gtkcellrendererseptext.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcombobox.h"
30 #include "gtkentry.h"
31 #include "gtkexpander.h"
32 #include "gtkfilechooserdefault.h"
33 #include "gtkfilechooserembed.h"
34 #include "gtkfilechooserentry.h"
35 #include "gtkfilechooserutils.h"
36 #include "gtkfilechooser.h"
37 #include "gtkfilesystemmodel.h"
38 #include "gtkframe.h"
39 #include "gtkhbox.h"
40 #include "gtkhpaned.h"
41 #include "gtkicontheme.h"
42 #include "gtkimage.h"
43 #include "gtkintl.h"
44 #include "gtklabel.h"
45 #include "gtkmarshalers.h"
46 #include "gtkmenuitem.h"
47 #include "gtkmessagedialog.h"
48 #include "gtkpathbar.h"
49 #include "gtkprivate.h"
50 #include "gtkscrolledwindow.h"
51 #include "gtksizegroup.h"
52 #include "gtkstock.h"
53 #include "gtktable.h"
54 #include "gtktreeview.h"
55 #include "gtktreemodelsort.h"
56 #include "gtktreeselection.h"
57 #include "gtktreestore.h"
58 #include "gtktypebuiltins.h"
59 #include "gtkvbox.h"
60
61 #if defined (G_OS_UNIX)
62 #include "gtkfilesystemunix.h"
63 #elif defined (G_OS_WIN32)
64 #include "gtkfilesystemwin32.h"
65 #endif
66
67 #include <string.h>
68 #include <time.h>
69
70 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
71
72 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
73 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
74 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
75
76
77 struct _GtkFileChooserDefaultClass
78 {
79   GtkVBoxClass parent_class;
80 };
81
82 struct _GtkFileChooserDefault
83 {
84   GtkVBox parent_instance;
85
86   GtkFileChooserAction action;
87
88   GtkFileSystem *file_system;
89
90   /* Save mode widgets */
91   GtkWidget *save_widgets;
92
93   GtkWidget *save_file_name_entry;
94   GtkWidget *save_folder_label;
95   GtkWidget *save_folder_combo;
96   GtkWidget *save_extra_align;
97   GtkWidget *save_expander;
98
99   /* The file browsing widgets */
100   GtkWidget *browse_widgets;
101   GtkWidget *browse_shortcuts_tree_view;
102   GtkWidget *browse_shortcuts_swin;
103   GtkWidget *browse_shortcuts_add_button;
104   GtkWidget *browse_shortcuts_remove_button;
105   GtkWidget *browse_files_swin;
106   GtkWidget *browse_files_tree_view;
107   GtkWidget *browse_directories_swin;
108   GtkWidget *browse_directories_tree_view;
109   GtkWidget *browse_new_folder_button;
110   GtkWidget *browse_path_bar;
111   GtkWidget *browse_extra_align;
112
113   GtkFileSystemModel *browse_files_model;
114   GtkFileSystemModel *browse_directories_model;
115
116   GtkWidget *filter_combo;
117   GtkWidget *preview_box;
118   GtkWidget *preview_label;
119   GtkWidget *preview_widget;
120   GtkWidget *extra_widget;
121
122   GtkListStore *shortcuts_model;
123   GtkTreeModel *shortcuts_filter_model;
124
125   GtkTreeModelSort *sort_model;
126
127   GtkFileFilter *current_filter;
128   GSList *filters;
129
130   gboolean has_home;
131   gboolean has_desktop;
132
133   int num_volumes;
134   int num_shortcuts;
135   int num_bookmarks;
136
137   guint volumes_changed_id;
138   guint bookmarks_changed_id;
139
140   GtkFilePath *current_volume_path;
141   GtkFilePath *current_folder;
142   GtkFilePath *preview_path;
143   char *preview_display_name;
144
145   GtkTreeViewColumn *list_name_column;
146   GtkCellRenderer *list_name_renderer;
147
148   /* Flags */
149
150   guint local_only : 1;
151   guint preview_widget_active : 1;
152   guint use_preview_label : 1;
153   guint select_multiple : 1;
154   guint show_hidden : 1;
155   guint list_sort_ascending : 1;
156   guint changing_folder : 1;
157   guint shortcuts_current_folder_active : 1;
158   guint shortcuts_current_folder_is_volume : 1;
159 };
160
161 /* Signal IDs */
162 enum {
163   LOCATION_POPUP,
164   UP_FOLDER,
165   HOME_FOLDER,
166   LAST_SIGNAL
167 };
168
169 static guint signals[LAST_SIGNAL] = { 0 };
170
171 /* Column numbers for the shortcuts tree.  Keep these in sync with shortcuts_model_create() */
172 enum {
173   SHORTCUTS_COL_PIXBUF,
174   SHORTCUTS_COL_NAME,
175   SHORTCUTS_COL_PATH,
176   SHORTCUTS_COL_REMOVABLE,
177   SHORTCUTS_COL_PIXBUF_VISIBLE,
178   SHORTCUTS_COL_NUM_COLUMNS
179 };
180
181 /* Column numbers for the file list */
182 enum {
183   FILE_LIST_COL_NAME,
184   FILE_LIST_COL_SIZE,
185   FILE_LIST_COL_MTIME,
186   FILE_LIST_COL_NUM_COLUMNS
187 };
188
189 /* Identifiers for target types */
190 enum {
191   TEXT_URI_LIST
192 };
193
194 /* Target types for DnD in the shortcuts list */
195 static GtkTargetEntry shortcuts_targets[] = {
196   { "text/uri-list", 0, TEXT_URI_LIST }
197 };
198
199 static const int num_shortcuts_targets = sizeof (shortcuts_targets) / sizeof (shortcuts_targets[0]);
200
201 /* Interesting places in the shortcuts bar */
202 typedef enum {
203   SHORTCUTS_HOME,
204   SHORTCUTS_DESKTOP,
205   SHORTCUTS_VOLUMES,
206   SHORTCUTS_SHORTCUTS,
207   SHORTCUTS_BOOKMARKS_SEPARATOR,
208   SHORTCUTS_BOOKMARKS,
209   SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
210   SHORTCUTS_CURRENT_FOLDER
211 } ShortcutsIndex;
212
213 /* Standard icon size */
214 /* FIXME: maybe this should correspond to the font size in the tree views... */
215 #define ICON_SIZE 20
216 #define PREVIEW_HBOX_SPACING 12
217 #define NUM_LINES 40
218 #define NUM_CHARS 60
219
220 static void gtk_file_chooser_default_class_init       (GtkFileChooserDefaultClass *class);
221 static void gtk_file_chooser_default_iface_init       (GtkFileChooserIface        *iface);
222 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface   *iface);
223 static void gtk_file_chooser_default_init             (GtkFileChooserDefault      *impl);
224
225 static GObject* gtk_file_chooser_default_constructor  (GType                  type,
226                                                        guint                  n_construct_properties,
227                                                        GObjectConstructParam *construct_params);
228 static void     gtk_file_chooser_default_finalize     (GObject               *object);
229 static void     gtk_file_chooser_default_set_property (GObject               *object,
230                                                        guint                  prop_id,
231                                                        const GValue          *value,
232                                                        GParamSpec            *pspec);
233 static void     gtk_file_chooser_default_get_property (GObject               *object,
234                                                        guint                  prop_id,
235                                                        GValue                *value,
236                                                        GParamSpec            *pspec);
237 static void     gtk_file_chooser_default_dispose      (GObject               *object);
238 static void     gtk_file_chooser_default_show_all       (GtkWidget             *widget);
239 static void     gtk_file_chooser_default_style_set      (GtkWidget             *widget,
240                                                          GtkStyle              *previous_style);
241 static void     gtk_file_chooser_default_screen_changed (GtkWidget             *widget,
242                                                          GdkScreen             *previous_screen);
243
244 static void           gtk_file_chooser_default_set_current_folder          (GtkFileChooser    *chooser,
245                                                                             const GtkFilePath *path);
246 static GtkFilePath *  gtk_file_chooser_default_get_current_folder          (GtkFileChooser    *chooser);
247 static void           gtk_file_chooser_default_set_current_name            (GtkFileChooser    *chooser,
248                                                                             const gchar       *name);
249 static void           gtk_file_chooser_default_select_path                 (GtkFileChooser    *chooser,
250                                                                             const GtkFilePath *path);
251 static void           gtk_file_chooser_default_unselect_path               (GtkFileChooser    *chooser,
252                                                                             const GtkFilePath *path);
253 static void           gtk_file_chooser_default_select_all                  (GtkFileChooser    *chooser);
254 static void           gtk_file_chooser_default_unselect_all                (GtkFileChooser    *chooser);
255 static GSList *       gtk_file_chooser_default_get_paths                   (GtkFileChooser    *chooser);
256 static GtkFilePath *  gtk_file_chooser_default_get_preview_path            (GtkFileChooser    *chooser);
257 static GtkFileSystem *gtk_file_chooser_default_get_file_system             (GtkFileChooser    *chooser);
258 static void           gtk_file_chooser_default_add_filter                  (GtkFileChooser    *chooser,
259                                                                             GtkFileFilter     *filter);
260 static void           gtk_file_chooser_default_remove_filter               (GtkFileChooser    *chooser,
261                                                                             GtkFileFilter     *filter);
262 static GSList *       gtk_file_chooser_default_list_filters                (GtkFileChooser    *chooser);
263 static gboolean       gtk_file_chooser_default_add_shortcut_folder    (GtkFileChooser    *chooser,
264                                                                        const GtkFilePath *path,
265                                                                        GError           **error);
266 static gboolean       gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
267                                                                        const GtkFilePath *path,
268                                                                        GError           **error);
269 static GSList *       gtk_file_chooser_default_list_shortcut_folders  (GtkFileChooser    *chooser);
270 static void           gtk_file_chooser_default_get_default_size       (GtkFileChooserEmbed *chooser_embed,
271                                                                        gint                *default_width,
272                                                                        gint                *default_height);
273 static void           gtk_file_chooser_default_get_resizable_hints    (GtkFileChooserEmbed *chooser_embed,
274                                                                        gboolean            *resize_horizontally,
275                                                                        gboolean            *resize_vertically);
276
277 static void location_popup_handler (GtkFileChooserDefault *impl);
278 static void up_folder_handler      (GtkFileChooserDefault *impl);
279 static void home_folder_handler    (GtkFileChooserDefault *impl);
280 static void update_appearance      (GtkFileChooserDefault *impl);
281
282 static void set_current_filter   (GtkFileChooserDefault *impl,
283                                   GtkFileFilter         *filter);
284 static void check_preview_change (GtkFileChooserDefault *impl);
285
286 static void filter_combo_changed       (GtkComboBox           *combo_box,
287                                         GtkFileChooserDefault *impl);
288 static void tree_selection_changed     (GtkTreeSelection      *tree_selection,
289                                         GtkFileChooserDefault *impl);
290
291 static void     shortcuts_row_activated_cb (GtkTreeView           *tree_view,
292                                             GtkTreePath           *path,
293                                             GtkTreeViewColumn     *column,
294                                             GtkFileChooserDefault *impl);
295 static gboolean shortcuts_select_func   (GtkTreeSelection      *selection,
296                                          GtkTreeModel          *model,
297                                          GtkTreePath           *path,
298                                          gboolean               path_currently_selected,
299                                          gpointer               data);
300 static void shortcuts_activate_item (GtkFileChooserDefault *impl,
301                                      int                    item_num);
302 static int shortcuts_get_index (GtkFileChooserDefault *impl,
303                                 ShortcutsIndex         where);
304 static int shortcut_find_position (GtkFileChooserDefault *impl,
305                                    const GtkFilePath     *path);
306
307 static void list_selection_changed     (GtkTreeSelection      *tree_selection,
308                                         GtkFileChooserDefault *impl);
309 static void list_row_activated         (GtkTreeView           *tree_view,
310                                         GtkTreePath           *path,
311                                         GtkTreeViewColumn     *column,
312                                         GtkFileChooserDefault *impl);
313
314 static void path_bar_clicked           (GtkPathBar            *path_bar,
315                                         GtkFilePath           *file_path,
316                                         GtkFileChooserDefault *impl);
317
318 static void add_bookmark_button_clicked_cb    (GtkButton             *button,
319                                                GtkFileChooserDefault *impl);
320 static void remove_bookmark_button_clicked_cb (GtkButton             *button,
321                                                GtkFileChooserDefault *impl);
322
323 static void tree_name_data_func (GtkTreeViewColumn *tree_column,
324                                  GtkCellRenderer   *cell,
325                                  GtkTreeModel      *tree_model,
326                                  GtkTreeIter       *iter,
327                                  gpointer           data);
328 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
329                                  GtkCellRenderer   *cell,
330                                  GtkTreeModel      *tree_model,
331                                  GtkTreeIter       *iter,
332                                  gpointer           data);
333 static void list_name_data_func (GtkTreeViewColumn *tree_column,
334                                  GtkCellRenderer   *cell,
335                                  GtkTreeModel      *tree_model,
336                                  GtkTreeIter       *iter,
337                                  gpointer           data);
338 #if 0
339 static void list_size_data_func (GtkTreeViewColumn *tree_column,
340                                  GtkCellRenderer   *cell,
341                                  GtkTreeModel      *tree_model,
342                                  GtkTreeIter       *iter,
343                                  gpointer           data);
344 #endif
345 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
346                                   GtkCellRenderer   *cell,
347                                   GtkTreeModel      *tree_model,
348                                   GtkTreeIter       *iter,
349                                   gpointer           data);
350
351 static GObjectClass *parent_class;
352
353 GType
354 _gtk_file_chooser_default_get_type (void)
355 {
356   static GType file_chooser_default_type = 0;
357
358   if (!file_chooser_default_type)
359     {
360       static const GTypeInfo file_chooser_default_info =
361       {
362         sizeof (GtkFileChooserDefaultClass),
363         NULL,           /* base_init */
364         NULL,           /* base_finalize */
365         (GClassInitFunc) gtk_file_chooser_default_class_init,
366         NULL,           /* class_finalize */
367         NULL,           /* class_data */
368         sizeof (GtkFileChooserDefault),
369         0,              /* n_preallocs */
370         (GInstanceInitFunc) gtk_file_chooser_default_init,
371       };
372
373       static const GInterfaceInfo file_chooser_info =
374       {
375         (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
376         NULL,                                                          /* interface_finalize */
377         NULL                                                           /* interface_data */
378       };
379
380       static const GInterfaceInfo file_chooser_embed_info =
381       {
382         (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
383         NULL,                                                          /* interface_finalize */
384         NULL                                                           /* interface_data */
385       };
386
387       file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
388                                                          &file_chooser_default_info, 0);
389
390       g_type_add_interface_static (file_chooser_default_type,
391                                    GTK_TYPE_FILE_CHOOSER,
392                                    &file_chooser_info);
393       g_type_add_interface_static (file_chooser_default_type,
394                                    GTK_TYPE_FILE_CHOOSER_EMBED,
395                                    &file_chooser_embed_info);
396     }
397
398   return file_chooser_default_type;
399 }
400
401 static void
402 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
403 {
404   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
405   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
406   GtkBindingSet *binding_set;
407
408   parent_class = g_type_class_peek_parent (class);
409
410   gobject_class->finalize = gtk_file_chooser_default_finalize;
411   gobject_class->constructor = gtk_file_chooser_default_constructor;
412   gobject_class->set_property = gtk_file_chooser_default_set_property;
413   gobject_class->get_property = gtk_file_chooser_default_get_property;
414   gobject_class->dispose = gtk_file_chooser_default_dispose;
415
416   widget_class->show_all = gtk_file_chooser_default_show_all;
417   widget_class->style_set = gtk_file_chooser_default_style_set;
418   widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
419
420   signals[LOCATION_POPUP] =
421     _gtk_binding_signal_new ("location-popup",
422                              G_OBJECT_CLASS_TYPE (class),
423                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
424                              G_CALLBACK (location_popup_handler),
425                              NULL, NULL,
426                              _gtk_marshal_VOID__VOID,
427                              G_TYPE_NONE, 0);
428   signals[UP_FOLDER] =
429     _gtk_binding_signal_new ("up-folder",
430                              G_OBJECT_CLASS_TYPE (class),
431                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
432                              G_CALLBACK (up_folder_handler),
433                              NULL, NULL,
434                              _gtk_marshal_VOID__VOID,
435                              G_TYPE_NONE, 0);
436   signals[HOME_FOLDER] =
437     _gtk_binding_signal_new ("home-folder",
438                              G_OBJECT_CLASS_TYPE (class),
439                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
440                              G_CALLBACK (home_folder_handler),
441                              NULL, NULL,
442                              _gtk_marshal_VOID__VOID,
443                              G_TYPE_NONE, 0);
444
445   binding_set = gtk_binding_set_by_class (class);
446
447   gtk_binding_entry_add_signal (binding_set,
448                                 GDK_l, GDK_CONTROL_MASK,
449                                 "location-popup",
450                                 0);
451
452   gtk_binding_entry_add_signal (binding_set,
453                                 GDK_Up, GDK_MOD1_MASK,
454                                 "up-folder",
455                                 0);
456   gtk_binding_entry_add_signal (binding_set,
457                                 GDK_KP_Up, GDK_MOD1_MASK,
458                                 "up-folder",
459                                 0);
460
461   gtk_binding_entry_add_signal (binding_set,
462                                 GDK_Home, GDK_MOD1_MASK,
463                                 "home-folder",
464                                 0);
465   gtk_binding_entry_add_signal (binding_set,
466                                 GDK_KP_Home, GDK_MOD1_MASK,
467                                 "home-folder",
468                                 0);
469
470   _gtk_file_chooser_install_properties (gobject_class);
471   
472   gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
473                                                       P_("Default file chooser backend"),
474                                                       P_("Name of the GtkFileChooser backend to use by default"),
475                                                       NULL,
476                                                       G_PARAM_READWRITE));
477 }
478
479 static void
480 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
481 {
482   iface->select_path = gtk_file_chooser_default_select_path;
483   iface->unselect_path = gtk_file_chooser_default_unselect_path;
484   iface->select_all = gtk_file_chooser_default_select_all;
485   iface->unselect_all = gtk_file_chooser_default_unselect_all;
486   iface->get_paths = gtk_file_chooser_default_get_paths;
487   iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
488   iface->get_file_system = gtk_file_chooser_default_get_file_system;
489   iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
490   iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
491   iface->set_current_name = gtk_file_chooser_default_set_current_name;
492   iface->add_filter = gtk_file_chooser_default_add_filter;
493   iface->remove_filter = gtk_file_chooser_default_remove_filter;
494   iface->list_filters = gtk_file_chooser_default_list_filters;
495   iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
496   iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
497   iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
498 }
499
500 static void
501 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
502 {
503   iface->get_default_size = gtk_file_chooser_default_get_default_size;
504   iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
505 }
506 static void
507 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
508 {
509   impl->local_only = TRUE;
510   impl->preview_widget_active = TRUE;
511   impl->use_preview_label = TRUE;
512   impl->select_multiple = FALSE;
513   impl->show_hidden = FALSE;
514
515   gtk_box_set_spacing (GTK_BOX (impl), 12);
516 }
517
518 static void
519 gtk_file_chooser_default_finalize (GObject *object)
520 {
521   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
522   GSList *l;
523
524   g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
525   impl->volumes_changed_id = 0;
526   g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
527   impl->bookmarks_changed_id = 0;
528   g_object_unref (impl->file_system);
529
530   for (l = impl->filters; l; l = l->next)
531     {
532       GtkFileFilter *filter;
533
534       filter = GTK_FILE_FILTER (l->data);
535       g_object_unref (filter);
536     }
537   g_slist_free (impl->filters);
538
539   if (impl->current_filter)
540     g_object_unref (impl->current_filter);
541
542   if (impl->current_volume_path)
543     gtk_file_path_free (impl->current_volume_path);
544
545   if (impl->current_folder)
546     gtk_file_path_free (impl->current_folder);
547
548   if (impl->preview_path)
549     gtk_file_path_free (impl->preview_path);
550
551   g_free (impl->preview_display_name);
552
553   G_OBJECT_CLASS (parent_class)->finalize (object);
554 }
555
556 /* Shows an error dialog set as transient for the specified window */
557 static void
558 error_message_with_parent (GtkWindow  *parent,
559                            const char *msg)
560 {
561   GtkWidget *dialog;
562
563   dialog = gtk_message_dialog_new (parent,
564                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
565                                    GTK_MESSAGE_ERROR,
566                                    GTK_BUTTONS_CLOSE,
567                                    "%s",
568                                    msg);
569   gtk_dialog_run (GTK_DIALOG (dialog));
570   gtk_widget_destroy (dialog);
571 }
572
573 /* Shows an error dialog for the file chooser */
574 static void
575 error_message (GtkFileChooserDefault *impl,
576                const char            *msg)
577 {
578   GtkWidget *toplevel;
579
580   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
581   if (!GTK_WIDGET_TOPLEVEL (toplevel))
582     toplevel = NULL;
583
584   error_message_with_parent (toplevel ? GTK_WINDOW (toplevel) : NULL,
585                              msg);
586 }
587
588 /* Shows a simple error dialog relative to a path.  Frees the GError as well. */
589 static void
590 error_dialog (GtkFileChooserDefault *impl,
591               const char            *msg,
592               const GtkFilePath     *path,
593               GError                *error)
594 {
595   char *text;
596
597   text = g_strdup_printf (msg,
598                           gtk_file_path_get_string (path),
599                           error->message);
600   error_message (impl, text);
601   g_free (text);
602   g_error_free (error);
603 }
604
605 /* Displays an error message about not being able to get information for a file.
606  * Frees the GError as well.
607  */
608 static void
609 error_getting_info_dialog (GtkFileChooserDefault *impl,
610                            const GtkFilePath     *path,
611                            GError                *error)
612 {
613   error_dialog (impl,
614                 _("Could not retrieve information about %s:\n%s"),
615                 path, error);
616 }
617
618 /* Shows an error dialog about not being able to add a bookmark */
619 static void
620 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
621                                      const GtkFilePath     *path,
622                                      GError                *error)
623 {
624   error_dialog (impl,
625                 _("Could not add a bookmark for %s:\n%s"),
626                 path, error);
627 }
628
629 /* Shows an error dialog about not being able to compose a filename */
630 static void
631 error_building_filename_dialog (GtkFileChooserDefault *impl,
632                                 const GtkFilePath     *base_path,
633                                 const char            *file_part,
634                                 GError                *error)
635 {
636   char *msg;
637
638   msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
639                          gtk_file_path_get_string (base_path),
640                          file_part,
641                          error->message);
642   error_message (impl, msg);
643   g_free (msg);
644   g_error_free (error);
645 }
646
647 static void
648 update_preview_widget_visibility (GtkFileChooserDefault *impl)
649 {
650   if (impl->use_preview_label)
651     {
652       if (!impl->preview_label)
653         {
654           impl->preview_label = gtk_label_new (impl->preview_display_name);
655           gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
656           gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
657           gtk_widget_show (impl->preview_label);
658         }
659     }
660   else
661     {
662       if (impl->preview_label)
663         {
664           gtk_widget_destroy (impl->preview_label);
665           impl->preview_label = NULL;
666         }
667     }
668
669   if (impl->preview_widget_active && impl->preview_widget)
670     gtk_widget_show (impl->preview_box);
671   else
672     gtk_widget_hide (impl->preview_box);
673
674   g_signal_emit_by_name (impl, "default-size-changed");
675 }
676
677 static void
678 set_preview_widget (GtkFileChooserDefault *impl,
679                     GtkWidget             *preview_widget)
680 {
681   if (preview_widget == impl->preview_widget)
682     return;
683
684   if (impl->preview_widget)
685     gtk_container_remove (GTK_CONTAINER (impl->preview_box),
686                           impl->preview_widget);
687
688   impl->preview_widget = preview_widget;
689   if (impl->preview_widget)
690     {
691       gtk_widget_show_all (impl->preview_widget);
692       gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
693       gtk_box_reorder_child (GTK_BOX (impl->preview_box),
694                              impl->preview_widget,
695                              (impl->use_preview_label && impl->preview_label) ? 1 : 0);
696     }
697
698   update_preview_widget_visibility (impl);
699 }
700
701 /* Clears the selection in the shortcuts tree */
702 static void
703 shortcuts_unselect_all (GtkFileChooserDefault *impl)
704 {
705   GtkTreeSelection *selection;
706
707   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
708   gtk_tree_selection_unselect_all (selection);
709 }
710
711 /* Convenience function to get the display name and icon info for a path */
712 static GtkFileInfo *
713 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
714 {
715   GtkFilePath *parent_path;
716   GtkFileFolder *parent_folder;
717   GtkFileInfo *info;
718
719   if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
720     return NULL;
721
722   parent_folder = gtk_file_system_get_folder (file_system, parent_path,
723                                               GTK_FILE_INFO_DISPLAY_NAME
724 #if 0
725                                               | GTK_FILE_INFO_ICON
726 #endif
727                                               | GTK_FILE_INFO_IS_FOLDER,
728                                               error);
729   gtk_file_path_free (parent_path);
730
731   if (!parent_folder)
732     return NULL;
733
734   info = gtk_file_folder_get_info (parent_folder, path, error);
735   g_object_unref (parent_folder);
736
737   return info;
738 }
739
740 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
741  * inserts a volume.  A position of -1 indicates the end of the tree.
742  */
743 static gboolean
744 shortcuts_insert_path (GtkFileChooserDefault *impl,
745                        int                    pos,
746                        gboolean               is_volume,
747                        GtkFileSystemVolume   *volume,
748                        const GtkFilePath     *path,
749                        const char            *label,
750                        gboolean               removable,
751                        GError               **error)
752 {
753   char *label_copy;
754   GdkPixbuf *pixbuf;
755   gpointer data;
756   GtkTreeIter iter;
757
758   if (is_volume)
759     {
760       data = volume;
761       label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
762       pixbuf = gtk_file_system_volume_render_icon (impl->file_system,
763                                                    volume,
764                                                    GTK_WIDGET (impl),
765                                                    ICON_SIZE,
766                                                    NULL);
767     }
768   else
769     {
770       GtkFileInfo *info;
771
772       info = get_file_info (impl->file_system, path, error);
773       if (!info)
774         return FALSE;
775
776       data = gtk_file_path_copy (path);
777
778       if (label)
779         label_copy = g_strdup (label);
780       else
781         label_copy = g_strdup (gtk_file_info_get_display_name (info));
782
783       pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
784
785       gtk_file_info_free (info);
786     }
787
788   if (pos == -1)
789     gtk_list_store_append (impl->shortcuts_model, &iter);
790   else
791     gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
792
793   gtk_list_store_set (impl->shortcuts_model, &iter,
794                       SHORTCUTS_COL_PIXBUF, pixbuf,
795                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
796                       SHORTCUTS_COL_NAME, label_copy,
797                       SHORTCUTS_COL_PATH, data,
798                       SHORTCUTS_COL_REMOVABLE, removable,
799                       -1);
800
801   g_free (label_copy);
802
803   if (pixbuf)
804     g_object_unref (pixbuf);
805
806   return TRUE;
807 }
808
809 /* Appends an item for the user's home directory to the shortcuts model */
810 static void
811 shortcuts_append_home (GtkFileChooserDefault *impl)
812 {
813   const char *home;
814   GtkFilePath *home_path;
815   GError *error;
816
817   home = g_get_home_dir ();
818   home_path = gtk_file_system_filename_to_path (impl->file_system, home);
819
820   error = NULL;
821   impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
822   if (!impl->has_home)
823     error_getting_info_dialog (impl, home_path, error);
824
825   gtk_file_path_free (home_path);
826 }
827
828 /* Appends the ~/Desktop directory to the shortcuts model */
829 static void
830 shortcuts_append_desktop (GtkFileChooserDefault *impl)
831 {
832   char *name;
833   GtkFilePath *path;
834
835   name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
836   path = gtk_file_system_filename_to_path (impl->file_system, name);
837   g_free (name);
838
839   impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
840   /* We do not actually pop up an error dialog if there is no desktop directory
841    * because some people may really not want to have one.
842    */
843
844   gtk_file_path_free (path);
845 }
846
847 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
848 static int
849 shortcuts_append_paths (GtkFileChooserDefault *impl,
850                         GSList                *paths)
851 {
852   int num_inserted;
853
854   num_inserted = 0;
855
856   for (; paths; paths = paths->next)
857     {
858       GtkFilePath *path;
859       GError *error;
860
861       path = paths->data;
862       error = NULL;
863
864       /* NULL GError, but we don't really want to show error boxes here */
865
866       if (shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, TRUE, NULL))
867         num_inserted++;
868     }
869
870   return num_inserted;
871 }
872
873 /* Returns the index for the corresponding item in the shortcuts bar */
874 static int
875 shortcuts_get_index (GtkFileChooserDefault *impl,
876                      ShortcutsIndex         where)
877 {
878   int n;
879
880   n = 0;
881
882   if (where == SHORTCUTS_HOME)
883     goto out;
884
885   n += impl->has_home ? 1 : 0;
886
887   if (where == SHORTCUTS_DESKTOP)
888     goto out;
889
890   n += impl->has_desktop ? 1 : 0;
891
892   if (where == SHORTCUTS_VOLUMES)
893     goto out;
894
895   n += impl->num_volumes;
896
897   if (where == SHORTCUTS_SHORTCUTS)
898     goto out;
899
900   n += impl->num_shortcuts;
901
902   if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
903     goto out;
904
905   /* If there are no bookmarks there won't be a separator */
906   n += (impl->num_bookmarks > 0) ? 1 : 0;
907
908   if (where == SHORTCUTS_BOOKMARKS)
909     goto out;
910
911   n += impl->num_bookmarks;
912
913   if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
914     goto out;
915
916   n += 1;
917
918   if (where == SHORTCUTS_CURRENT_FOLDER)
919     goto out;
920
921   g_assert_not_reached ();
922
923  out:
924
925   return n;
926 }
927
928 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
929
930 /* Removes the specified number of rows from the shortcuts list */
931 static void
932 shortcuts_remove_rows (GtkFileChooserDefault *impl,
933                        int start_row,
934                        int n_rows,
935                        RemoveFunc remove_fn)
936 {
937   GtkTreePath *path;
938
939   path = gtk_tree_path_new_from_indices (start_row, -1);
940
941   for (; n_rows; n_rows--)
942     {
943       GtkTreeIter iter;
944       gpointer data;
945
946       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
947         g_assert_not_reached ();
948
949       if (remove_fn)
950         {
951           gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
952           (* remove_fn) (impl, data);
953         }
954
955       gtk_list_store_remove (impl->shortcuts_model, &iter);
956     }
957
958   gtk_tree_path_free (path);
959 }
960
961 /* Used from shortcuts_remove_rows() */
962 static void
963 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
964 {
965   GtkFileSystemVolume *volume;
966
967   volume = data;
968   gtk_file_system_volume_free (impl->file_system, volume);
969 }
970
971 /* Adds all the file system volumes to the shortcuts model */
972 static void
973 shortcuts_add_volumes (GtkFileChooserDefault *impl)
974 {
975   int start_row;
976   GSList *list, *l;
977   int n;
978
979   start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
980   shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
981   impl->num_volumes = 0;
982
983   list = gtk_file_system_list_volumes (impl->file_system);
984
985   n = 0;
986
987   for (l = list; l; l = l->next)
988     {
989       GtkFileSystemVolume *volume;
990
991       volume = l->data;
992
993       shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL);
994       n++;
995     }
996
997   impl->num_volumes = n;
998
999   g_slist_free (list);
1000 }
1001
1002 /* Used from shortcuts_remove_rows() */
1003 static void
1004 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
1005 {
1006   GtkFilePath *path;
1007
1008   path = data;
1009   gtk_file_path_free (path);
1010 }
1011
1012 /* Inserts a separator node in the shortcuts list */
1013 static void
1014 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1015                             ShortcutsIndex where)
1016 {
1017   GtkTreeIter iter;
1018
1019   g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1020
1021   gtk_list_store_insert (impl->shortcuts_model, &iter,
1022                          shortcuts_get_index (impl, where));
1023   gtk_list_store_set (impl->shortcuts_model, &iter,
1024                       SHORTCUTS_COL_PIXBUF, NULL,
1025                       SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1026                       SHORTCUTS_COL_NAME, NULL,
1027                       SHORTCUTS_COL_PATH, NULL,
1028                       -1);
1029 }
1030
1031 /* Updates the list of bookmarks */
1032 static void
1033 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1034 {
1035   GSList *bookmarks;
1036
1037   if (impl->num_bookmarks > 0)
1038     {
1039       shortcuts_remove_rows (impl,
1040                              shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1041                              impl->num_bookmarks + 1,
1042                              remove_bookmark_cb);
1043
1044     }
1045
1046   bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1047   impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1048   gtk_file_paths_free (bookmarks);
1049
1050   if (impl->num_bookmarks > 0)
1051     {
1052       shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1053     }
1054 }
1055
1056 /* Appends a separator and a row to the shortcuts list for the current folder */
1057 static void
1058 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1059 {
1060   int pos;
1061
1062   g_assert (!impl->shortcuts_current_folder_active);
1063
1064   pos = shortcut_find_position (impl, impl->current_folder);
1065   if (pos == -1)
1066     {
1067       GtkFileSystemVolume *volume;
1068       GtkFilePath *base_path;
1069
1070       /* Separator */
1071
1072       shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1073
1074       /* Item */
1075
1076       pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1077
1078       volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1079       base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1080
1081       if (strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1082         {
1083           shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1084           impl->shortcuts_current_folder_is_volume = TRUE;
1085         }
1086       else
1087         {
1088           gtk_file_system_volume_free (impl->file_system, volume);
1089           shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1090           impl->shortcuts_current_folder_is_volume = FALSE;
1091         }
1092
1093       gtk_file_path_free (base_path);
1094
1095       impl->shortcuts_current_folder_active = TRUE;
1096     }
1097
1098   gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1099 }
1100
1101 /* Used from shortcuts_remove_rows() in shortcuts_update_current_folder() */
1102 static void
1103 remove_current_folder_cb (GtkFileChooserDefault *impl,
1104                           gpointer               data)
1105 {
1106   if (impl->shortcuts_current_folder_is_volume)
1107     gtk_file_system_volume_free (impl->file_system, data);
1108   else
1109     gtk_file_path_free (data);
1110 }
1111
1112 /* Updates the current folder row in the shortcuts model */
1113 static void
1114 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1115 {
1116   int pos;
1117
1118   pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1119
1120   if (impl->shortcuts_current_folder_active)
1121     {
1122       shortcuts_remove_rows (impl, pos, 2, remove_current_folder_cb);
1123       impl->shortcuts_current_folder_active = FALSE;
1124     }
1125
1126   shortcuts_add_current_folder (impl);
1127 }
1128
1129 /* Filter function used for the shortcuts filter model */
1130 static gboolean
1131 shortcuts_filter_cb (GtkTreeModel          *model,
1132                      GtkTreeIter           *iter,
1133                      gpointer               data)
1134 {
1135   GtkFileChooserDefault *impl;
1136   GtkTreePath *path;
1137   int pos;
1138
1139   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1140
1141   path = gtk_tree_model_get_path (model, iter);
1142   if (!path)
1143     return FALSE;
1144
1145   pos = *gtk_tree_path_get_indices (path);
1146   gtk_tree_path_free (path);
1147
1148   return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1149 }
1150
1151 /* Creates the list model for shortcuts */
1152 static void
1153 shortcuts_model_create (GtkFileChooserDefault *impl)
1154 {
1155   /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1156   impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1157                                               GDK_TYPE_PIXBUF,  /* pixbuf */
1158                                               G_TYPE_STRING,    /* name */
1159                                               G_TYPE_POINTER,   /* path or volume */
1160                                               G_TYPE_BOOLEAN,   /* removable */
1161                                               G_TYPE_BOOLEAN);  /* pixbuf cell visibility */
1162
1163   if (impl->file_system)
1164     {
1165       shortcuts_append_home (impl);
1166       shortcuts_append_desktop (impl);
1167       shortcuts_add_volumes (impl);
1168       shortcuts_add_bookmarks (impl);
1169     }
1170
1171   impl->shortcuts_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
1172   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1173                                           shortcuts_filter_cb,
1174                                           impl,
1175                                           NULL);
1176 }
1177
1178 /* Callback used when the "New Folder" toolbar button is clicked */
1179 static void
1180 new_folder_button_clicked (GtkButton             *button,
1181                            GtkFileChooserDefault *impl)
1182 {
1183   GtkTreeIter iter;
1184   GtkTreePath *path;
1185
1186   /* FIXME: this doesn't work for folder mode, just for file mode */
1187
1188   _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1189   g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1190
1191   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1192   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1193                             path,
1194                             impl->list_name_column,
1195                             TRUE);
1196 }
1197
1198 /* Callback used from the text cell renderer when the new folder is named */
1199 static void
1200 renderer_edited_cb (GtkCellRendererText   *cell_renderer_text,
1201                     const gchar           *path,
1202                     const gchar           *new_text,
1203                     GtkFileChooserDefault *impl)
1204 {
1205   GError *error;
1206   GtkFilePath *file_path;
1207
1208   _gtk_file_system_model_remove_editable (impl->browse_files_model);
1209   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1210
1211   error = NULL;
1212   file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
1213   if (!file_path)
1214     {
1215       error_building_filename_dialog (impl, impl->current_folder, new_text, error);
1216       return;
1217     }
1218
1219   error = NULL;
1220   if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
1221     error_dialog (impl,
1222                   _("Could not create folder %s:\n%s"),
1223                   file_path, error);
1224
1225   gtk_file_path_free (file_path);
1226
1227   /* FIXME: scroll to the new folder and select it */
1228 }
1229
1230 /* Callback used from the text cell renderer when the new folder edition gets
1231  * canceled.
1232  */
1233 static void
1234 renderer_editing_canceled_cb (GtkCellRendererText   *cell_renderer_text,
1235                               GtkFileChooserDefault *impl)
1236 {
1237   _gtk_file_system_model_remove_editable (impl->browse_files_model);
1238   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1239 }
1240
1241 /* Creates the widgets for the filter combo box */
1242 static GtkWidget *
1243 filter_create (GtkFileChooserDefault *impl)
1244 {
1245   impl->filter_combo = gtk_combo_box_new_text ();
1246   g_signal_connect (impl->filter_combo, "changed",
1247                     G_CALLBACK (filter_combo_changed), impl);
1248
1249   return impl->filter_combo;
1250 }
1251
1252 static GtkWidget *
1253 button_new (GtkFileChooserDefault *impl,
1254             const char *text,
1255             const char *stock_id,
1256             gboolean    sensitive,
1257             gboolean    show,
1258             GCallback   callback)
1259 {
1260   GtkWidget *button;
1261   GtkWidget *hbox;
1262   GtkWidget *widget;
1263   GtkWidget *align;
1264
1265   button = gtk_button_new ();
1266   hbox = gtk_hbox_new (FALSE, 2);
1267   align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1268
1269   gtk_container_add (GTK_CONTAINER (button), align);
1270   gtk_container_add (GTK_CONTAINER (align), hbox);
1271   widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1272
1273   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1274
1275   widget = gtk_label_new_with_mnemonic (text);
1276   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1277   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1278
1279   gtk_widget_set_sensitive (button, sensitive);
1280   g_signal_connect (button, "clicked", callback, impl);
1281
1282   gtk_widget_show_all (align);
1283
1284   if (show)
1285     gtk_widget_show (button);
1286
1287   return button;
1288 }
1289
1290 /* Creates the widgets for the folder tree */
1291 static GtkWidget *
1292 create_folder_tree (GtkFileChooserDefault *impl)
1293 {
1294   GtkTreeSelection *selection;
1295
1296   /* Scrolled window */
1297
1298   impl->browse_directories_swin = gtk_scrolled_window_new (NULL, NULL);
1299   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1300                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1301   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1302                                        GTK_SHADOW_IN);
1303   /* Tree */
1304
1305   impl->browse_directories_tree_view = gtk_tree_view_new ();
1306   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_directories_tree_view), FALSE);
1307
1308   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
1309   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1310                                           GDK_BUTTON1_MASK,
1311                                           shortcuts_targets,
1312                                           num_shortcuts_targets,
1313                                           GDK_ACTION_COPY);
1314
1315   g_signal_connect (selection, "changed",
1316                     G_CALLBACK (tree_selection_changed), impl);
1317
1318   gtk_container_add (GTK_CONTAINER (impl->browse_directories_swin), impl->browse_directories_tree_view);
1319   gtk_widget_show (impl->browse_directories_tree_view);
1320
1321   /* Column */
1322
1323   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->browse_directories_tree_view), 0,
1324                                               _("Name"),
1325                                               gtk_cell_renderer_text_new (),
1326                                               tree_name_data_func, impl, NULL);
1327   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1328                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1329
1330   return impl->browse_directories_swin;
1331 }
1332
1333 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1334 static int
1335 shortcut_find_position (GtkFileChooserDefault *impl,
1336                         const GtkFilePath     *path)
1337 {
1338   GtkTreeIter iter;
1339   int i;
1340   int bookmarks_separator_idx;
1341   int current_folder_separator_idx;
1342   int volumes_idx;
1343
1344   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1345     return -1;
1346
1347   bookmarks_separator_idx = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1348   current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1349   volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1350
1351   i = 0;
1352
1353   for (i = 0; i < current_folder_separator_idx; i++)
1354     {
1355       gpointer data;
1356
1357       if (i == bookmarks_separator_idx)
1358         goto next_iter;
1359
1360       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1361
1362       if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1363         {
1364           GtkFileSystemVolume *volume;
1365           GtkFilePath *base_path;
1366           gboolean exists;
1367
1368           volume = data;
1369           base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1370
1371           exists = strcmp (gtk_file_path_get_string (path),
1372                            gtk_file_path_get_string (base_path)) == 0;
1373           g_free (base_path);
1374
1375           if (exists)
1376             return i;
1377         }
1378       else
1379         {
1380           GtkFilePath *model_path;
1381
1382           model_path = data;
1383
1384           if (model_path && gtk_file_path_compare (model_path, path) == 0)
1385             return i;
1386         }
1387
1388     next_iter:
1389       gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1390     }
1391
1392   return -1;
1393 }
1394
1395 /* Tries to add a bookmark from a path name */
1396 static void
1397 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1398                                   const GtkFilePath     *path)
1399 {
1400   GtkFileInfo *info;
1401   GError *error;
1402
1403   if (shortcut_find_position (impl, path) != -1)
1404     return;
1405
1406   error = NULL;
1407   info = get_file_info (impl->file_system, path, &error);
1408
1409   if (!info)
1410     error_getting_info_dialog (impl, path, error);
1411   else if (!gtk_file_info_get_is_folder (info))
1412     {
1413       char *msg;
1414
1415       msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1416                              gtk_file_path_get_string (path));
1417       error_message (impl, msg);
1418       g_free (msg);
1419     }
1420   else
1421     {
1422       error = NULL;
1423       if (!gtk_file_system_insert_bookmark (impl->file_system, path, -1, &error))
1424         error_could_not_add_bookmark_dialog (impl, path, error);
1425     }
1426 }
1427
1428 static void
1429 add_bookmark_foreach_cb (GtkTreeModel *model,
1430                          GtkTreePath  *path,
1431                          GtkTreeIter  *iter,
1432                          gpointer      data)
1433 {
1434   GtkFileChooserDefault *impl;
1435   GtkFileSystemModel *fs_model;
1436   GtkTreeIter child_iter;
1437   const GtkFilePath *file_path;
1438
1439   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1440
1441   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1442       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1443     {
1444       fs_model = impl->browse_directories_model;
1445       child_iter = *iter;
1446     }
1447   else
1448     {
1449       fs_model = impl->browse_files_model;
1450       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1451     }
1452
1453   file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &child_iter);
1454   shortcuts_add_bookmark_from_path (impl, file_path);
1455 }
1456
1457 /* Callback used when the "Add bookmark" button is clicked */
1458 static void
1459 add_bookmark_button_clicked_cb (GtkButton *button,
1460                                 GtkFileChooserDefault *impl)
1461 {
1462   GtkWidget *tree_view;
1463   GtkTreeSelection *selection;
1464
1465   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1466       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1467     tree_view = impl->browse_directories_tree_view;
1468   else
1469     tree_view = impl->browse_files_tree_view;
1470
1471   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1472   if (gtk_tree_selection_count_selected_rows (selection) == 0)
1473     shortcuts_add_bookmark_from_path (impl, impl->current_folder);
1474   else
1475     gtk_tree_selection_selected_foreach (selection,
1476                                          add_bookmark_foreach_cb,
1477                                          impl);
1478 }
1479
1480 /* Callback used when the "Remove bookmark" button is clicked */
1481 static void
1482 remove_bookmark_button_clicked_cb (GtkButton *button,
1483                                    GtkFileChooserDefault *impl)
1484 {
1485   GtkTreeSelection *selection;
1486   GtkTreeIter iter;
1487   GtkFilePath *path;
1488   gboolean removable;
1489   GError *error;
1490
1491   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1492
1493   if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1494     {
1495       gtk_tree_model_get (impl->shortcuts_filter_model, &iter,
1496                           SHORTCUTS_COL_PATH, &path,
1497                           SHORTCUTS_COL_REMOVABLE, &removable, -1);
1498       if (!removable)
1499         {
1500           g_assert_not_reached ();
1501           return;
1502         }
1503
1504       error = NULL;
1505       if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1506         error_dialog (impl,
1507                       _("Could not remove bookmark for %s:\n%s"),
1508                       path,
1509                       error);
1510     }
1511 }
1512
1513 struct is_folders_foreach_closure {
1514   GtkFileChooserDefault *impl;
1515   gboolean all_folders;
1516 };
1517
1518 /* Used from gtk_tree_selection_selected_foreach() */
1519 static void
1520 is_folders_foreach_cb (GtkTreeModel *model,
1521                        GtkTreePath  *path,
1522                        GtkTreeIter  *iter,
1523                        gpointer      data)
1524 {
1525   struct is_folders_foreach_closure *closure;
1526   GtkTreeIter child_iter;
1527   const GtkFileInfo *info;
1528
1529   closure = data;
1530
1531   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1532
1533   info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1534   closure->all_folders &= gtk_file_info_get_is_folder (info);
1535 }
1536
1537 /* Returns whether the selected items in the file list are all folders */
1538 static gboolean
1539 selection_is_folders (GtkFileChooserDefault *impl)
1540 {
1541   struct is_folders_foreach_closure closure;
1542   GtkTreeSelection *selection;
1543
1544   closure.impl = impl;
1545   closure.all_folders = TRUE;
1546
1547   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1548   gtk_tree_selection_selected_foreach (selection,
1549                                        is_folders_foreach_cb,
1550                                        &closure);
1551
1552   return closure.all_folders;
1553 }
1554
1555 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1556  * if there are no selected items *and* the current folder is not in the
1557  * bookmarks list.  De-sensitize the button otherwise.
1558  */
1559 static void
1560 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1561 {
1562   GtkWidget *tree_view;
1563   GtkTreeSelection *selection;
1564   gboolean active;
1565
1566   /* Check selection */
1567
1568   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1569       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1570     tree_view = impl->browse_directories_tree_view;
1571   else
1572     tree_view = impl->browse_files_tree_view;
1573
1574   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1575
1576   if (gtk_tree_selection_count_selected_rows (selection) == 0)
1577     active = (shortcut_find_position (impl, impl->current_folder) == -1);
1578   else
1579     active = (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1580               impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER ||
1581               selection_is_folders (impl));
1582
1583   gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
1584 }
1585
1586 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1587  * bookmark row is selected in the shortcuts tree.
1588  */
1589 static void
1590 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1591 {
1592   GtkTreeSelection *selection;
1593   GtkTreeIter iter;
1594   gboolean removable = FALSE;
1595
1596   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1597
1598   if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1599     gtk_tree_model_get (impl->shortcuts_filter_model, &iter,
1600                         SHORTCUTS_COL_REMOVABLE, &removable,
1601                         -1);
1602
1603   gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
1604 }
1605
1606 /* Converts raw selection data from text/uri-list to a list of strings */
1607 static GSList *
1608 split_uris (const char *data)
1609 {
1610   GSList *uris;
1611   const char *p, *start;
1612
1613   uris = NULL;
1614
1615   start = data;
1616
1617   for (p = start; *p != 0; p++)
1618     if (*p == '\r' && *(p + 1) == '\n')
1619       {
1620         char *name;
1621
1622         name = g_strndup (start, p - start);
1623         uris = g_slist_prepend (uris, name);
1624
1625         start = p + 2;
1626         p = start;
1627       }
1628
1629   uris = g_slist_reverse (uris);
1630   return uris;
1631 }
1632
1633 /* Callback used when we get the drag data for the bookmarks list.  We add the
1634  * received URIs as bookmarks if they are folders.
1635  */
1636 static void
1637 shortcuts_drag_data_received_cb (GtkWidget          *widget,
1638                                  GdkDragContext     *context,
1639                                  gint                x,
1640                                  gint                y,
1641                                  GtkSelectionData   *selection_data,
1642                                  guint               info,
1643                                  guint               time_,
1644                                  gpointer            data)
1645 {
1646   GtkFileChooserDefault *impl;
1647   GSList *uris, *l;
1648
1649   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1650
1651   uris = split_uris (selection_data->data);
1652
1653   for (l = uris; l; l = l->next)
1654     {
1655       char *uri;
1656       GtkFilePath *path;
1657
1658       uri = l->data;
1659       path = gtk_file_system_uri_to_path (impl->file_system, uri);
1660
1661       if (path)
1662         {
1663           shortcuts_add_bookmark_from_path (impl, path);
1664           gtk_file_path_free (path);
1665         }
1666       else
1667         {
1668           char *msg;
1669
1670           msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
1671                                  uri);
1672           error_message (impl, msg);
1673           g_free (msg);
1674         }
1675
1676       g_free (uri);
1677     }
1678
1679   g_slist_free (uris);
1680 }
1681
1682 /* Callback used when the selection in the shortcuts tree changes */
1683 static void
1684 shortcuts_selection_changed_cb (GtkTreeSelection      *selection,
1685                                 GtkFileChooserDefault *impl)
1686 {
1687   bookmarks_check_remove_sensitivity (impl);
1688 }
1689
1690 /* Creates the widgets for the shortcuts and bookmarks tree */
1691 static GtkWidget *
1692 shortcuts_list_create (GtkFileChooserDefault *impl)
1693 {
1694   GtkTreeSelection *selection;
1695   GtkTreeViewColumn *column;
1696   GtkCellRenderer *renderer;
1697
1698   /* Scrolled window */
1699
1700   impl->browse_shortcuts_swin = gtk_scrolled_window_new (NULL, NULL);
1701   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1702                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1703   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1704                                        GTK_SHADOW_IN);
1705   gtk_widget_show (impl->browse_shortcuts_swin);
1706
1707   /* Tree */
1708
1709   impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
1710   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
1711
1712   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
1713
1714   gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
1715                      GTK_DEST_DEFAULT_ALL,
1716                      shortcuts_targets,
1717                      num_shortcuts_targets,
1718                      GDK_ACTION_COPY);
1719
1720   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1721   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1722   gtk_tree_selection_set_select_function (selection,
1723                                           shortcuts_select_func,
1724                                           impl, NULL);
1725
1726   g_signal_connect (selection, "changed",
1727                     G_CALLBACK (shortcuts_selection_changed_cb), impl);
1728
1729   g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
1730                     G_CALLBACK (shortcuts_row_activated_cb), impl);
1731
1732   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
1733                     G_CALLBACK (shortcuts_drag_data_received_cb), impl);
1734
1735   gtk_container_add (GTK_CONTAINER (impl->browse_shortcuts_swin), impl->browse_shortcuts_tree_view);
1736   gtk_widget_show (impl->browse_shortcuts_tree_view);
1737
1738   /* Column */
1739
1740   column = gtk_tree_view_column_new ();
1741   gtk_tree_view_column_set_title (column, _("Folder"));
1742
1743   renderer = gtk_cell_renderer_pixbuf_new ();
1744   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1745   gtk_tree_view_column_set_attributes (column, renderer,
1746                                        "pixbuf", SHORTCUTS_COL_PIXBUF,
1747                                        "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
1748                                        NULL);
1749
1750   renderer = _gtk_cell_renderer_sep_text_new ();
1751   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1752   gtk_tree_view_column_set_attributes (column, renderer,
1753                                        "text", SHORTCUTS_COL_NAME,
1754                                        NULL);
1755
1756   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
1757
1758   return impl->browse_shortcuts_swin;
1759 }
1760
1761 /* Creates the widgets for the shortcuts/bookmarks pane */
1762 static GtkWidget *
1763 shortcuts_pane_create (GtkFileChooserDefault *impl,
1764                        GtkSizeGroup          *size_group)
1765 {
1766   GtkWidget *vbox;
1767   GtkWidget *hbox;
1768   GtkWidget *widget;
1769
1770   vbox = gtk_vbox_new (FALSE, 6);
1771   gtk_widget_show (vbox);
1772
1773   /* Shortcuts tree */
1774
1775   widget = shortcuts_list_create (impl);
1776   gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1777
1778   /* Box for buttons */
1779
1780   hbox = gtk_hbox_new (TRUE, 6);
1781   gtk_size_group_add_widget (size_group, hbox);
1782   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1783   gtk_widget_show (hbox);
1784
1785   /* Add bookmark button */
1786
1787   impl->browse_shortcuts_add_button = button_new (impl,
1788                                                   _("_Add"),
1789                                                   GTK_STOCK_ADD,
1790                                                   FALSE,
1791                                                   TRUE,
1792                                                   G_CALLBACK (add_bookmark_button_clicked_cb));
1793   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
1794
1795   /* Remove bookmark button */
1796
1797   impl->browse_shortcuts_remove_button = button_new (impl,
1798                                                      _("_Remove"),
1799                                                      GTK_STOCK_REMOVE,
1800                                                      FALSE,
1801                                                      TRUE,
1802                                                      G_CALLBACK (remove_bookmark_button_clicked_cb));
1803   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
1804
1805   return vbox;
1806 }
1807
1808 /* Creates the widgets for the file list */
1809 static GtkWidget *
1810 create_file_list (GtkFileChooserDefault *impl)
1811 {
1812   GtkTreeSelection *selection;
1813   GtkTreeViewColumn *column;
1814   GtkCellRenderer *renderer;
1815
1816   /* Scrolled window */
1817
1818   impl->browse_files_swin = gtk_scrolled_window_new (NULL, NULL);
1819   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1820                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1821   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1822                                        GTK_SHADOW_IN);
1823
1824   /* Tree/list view */
1825
1826   impl->browse_files_tree_view = gtk_tree_view_new ();
1827   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
1828   gtk_container_add (GTK_CONTAINER (impl->browse_files_swin), impl->browse_files_tree_view);
1829   g_signal_connect (impl->browse_files_tree_view, "row_activated",
1830                     G_CALLBACK (list_row_activated), impl);
1831   gtk_widget_show (impl->browse_files_tree_view);
1832
1833   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1834   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
1835                                           GDK_BUTTON1_MASK,
1836                                           shortcuts_targets,
1837                                           num_shortcuts_targets,
1838                                           GDK_ACTION_COPY);
1839
1840   g_signal_connect (selection, "changed",
1841                     G_CALLBACK (list_selection_changed), impl);
1842
1843   /* Filename column */
1844
1845   impl->list_name_column = gtk_tree_view_column_new ();
1846   gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
1847   gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
1848   gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
1849
1850   renderer = gtk_cell_renderer_pixbuf_new ();
1851   gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
1852   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
1853                                            list_icon_data_func, impl, NULL);
1854
1855   impl->list_name_renderer = gtk_cell_renderer_text_new ();
1856   g_signal_connect (impl->list_name_renderer, "edited",
1857                     G_CALLBACK (renderer_edited_cb), impl);
1858   g_signal_connect (impl->list_name_renderer, "editing-canceled",
1859                     G_CALLBACK (renderer_editing_canceled_cb), impl);
1860   gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
1861   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
1862                                            list_name_data_func, impl, NULL);
1863
1864   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
1865 #if 0
1866   /* Size column */
1867
1868   column = gtk_tree_view_column_new ();
1869   gtk_tree_view_column_set_title (column, _("Size"));
1870
1871   renderer = gtk_cell_renderer_text_new ();
1872   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1873   gtk_tree_view_column_set_cell_data_func (column, renderer,
1874                                            list_size_data_func, impl, NULL);
1875   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
1876   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1877 #endif
1878   /* Modification time column */
1879
1880   column = gtk_tree_view_column_new ();
1881   gtk_tree_view_column_set_title (column, _("Modified"));
1882
1883   renderer = gtk_cell_renderer_text_new ();
1884   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1885   gtk_tree_view_column_set_cell_data_func (column, renderer,
1886                                            list_mtime_data_func, impl, NULL);
1887   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
1888   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1889
1890   return impl->browse_files_swin;
1891 }
1892
1893 static GtkWidget *
1894 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
1895 {
1896   GtkWidget *hbox;
1897   GtkWidget *widget;
1898
1899   hbox = gtk_hbox_new (FALSE, 12);
1900   gtk_widget_show (hbox);
1901
1902   /* Filter combo */
1903
1904   widget = filter_create (impl);
1905   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1906
1907   return hbox;
1908 }
1909
1910 /* Creates the widgets for the files/folders pane */
1911 static GtkWidget *
1912 file_pane_create (GtkFileChooserDefault *impl,
1913                   GtkSizeGroup          *size_group)
1914 {
1915   GtkWidget *vbox;
1916   GtkWidget *hbox;
1917   GtkWidget *widget;
1918
1919   vbox = gtk_vbox_new (FALSE, 6);
1920   gtk_widget_show (vbox);
1921
1922   /* The path bar and 'Create Folder' button */
1923   hbox = gtk_hbox_new (FALSE, 12);
1924   gtk_widget_show (hbox);
1925   impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
1926   g_signal_connect (impl->browse_path_bar, "path_clicked", G_CALLBACK (path_bar_clicked), impl);
1927   gtk_widget_show_all (impl->browse_path_bar);
1928   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
1929
1930   /* Create Folder */
1931   impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create _Folder"));
1932   g_signal_connect (impl->browse_new_folder_button, "clicked",
1933                     G_CALLBACK (new_folder_button_clicked), impl);
1934   gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
1935   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1936
1937   /* Box for lists and preview */
1938
1939   hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
1940   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
1941   gtk_widget_show (hbox);
1942
1943   /* Folder tree */
1944
1945   widget = create_folder_tree (impl);
1946   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1947
1948   /* File list */
1949
1950   widget = create_file_list (impl);
1951   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1952
1953   /* Preview */
1954
1955   impl->preview_box = gtk_vbox_new (FALSE, 12);
1956   gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
1957   /* Don't show preview box initially */
1958
1959   /* Filename entry and filter combo */
1960   hbox = gtk_hbox_new (FALSE, 0);
1961   gtk_size_group_add_widget (size_group, hbox);
1962   widget = create_filename_entry_and_filter_combo (impl);
1963   gtk_box_pack_end (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1964   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1965   gtk_widget_show (hbox);
1966
1967   return vbox;
1968 }
1969 /* Callback used when the "Browse for more folders" expander is toggled */
1970 static void
1971 expander_changed_cb (GtkExpander           *expander,
1972                      GParamSpec            *pspec,
1973                      GtkFileChooserDefault *impl)
1974 {
1975   update_appearance (impl);
1976 }
1977
1978 /* Callback used when the selection changes in the save folder combo box */
1979 static void
1980 save_folder_combo_changed_cb (GtkComboBox           *combo,
1981                               GtkFileChooserDefault *impl)
1982 {
1983   int active;
1984
1985   active = gtk_combo_box_get_active (combo);
1986   if (active == -1)
1987     return;
1988
1989   shortcuts_activate_item (impl, active);
1990 }
1991
1992 /* Creates the combo box with the save folders */
1993 static GtkWidget *
1994 save_folder_combo_create (GtkFileChooserDefault *impl)
1995 {
1996   GtkWidget *combo;
1997   GtkCellRenderer *cell;
1998
1999   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
2000   gtk_widget_show (combo);
2001
2002   cell = gtk_cell_renderer_pixbuf_new ();
2003   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
2004   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
2005                                   "pixbuf", SHORTCUTS_COL_PIXBUF,
2006                                   "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2007                                   NULL);
2008
2009   cell = _gtk_cell_renderer_sep_text_new ();
2010   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
2011   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
2012                                   "text", SHORTCUTS_COL_NAME,
2013                                   NULL);
2014
2015   g_signal_connect (combo, "changed",
2016                     G_CALLBACK (save_folder_combo_changed_cb), impl);
2017
2018   return combo;
2019 }
2020
2021 /* Creates the widgets specific to Save mode */
2022 static GtkWidget *
2023 save_widgets_create (GtkFileChooserDefault *impl)
2024 {
2025   GtkWidget *vbox;
2026   GtkWidget *table;
2027   GtkWidget *widget;
2028   GtkWidget *alignment;
2029
2030   vbox = gtk_vbox_new (FALSE, 12);
2031
2032   table = gtk_table_new (2, 2, FALSE);
2033   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
2034   gtk_widget_show (table);
2035   gtk_table_set_row_spacings (GTK_TABLE (table), 12);
2036   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2037
2038   /* Name entry */
2039
2040   widget = gtk_label_new_with_mnemonic (_("_Name:"));
2041   gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
2042   gtk_table_attach (GTK_TABLE (table), widget,
2043                     0, 1, 0, 1,
2044                     GTK_FILL, GTK_FILL,
2045                     0, 0);
2046   gtk_widget_show (widget);
2047
2048   impl->save_file_name_entry = gtk_entry_new ();
2049   gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
2050   gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
2051   gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
2052                     1, 2, 0, 1,
2053                     GTK_EXPAND | GTK_FILL, 0,
2054                     0, 0);
2055   gtk_widget_show (impl->save_file_name_entry);
2056   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
2057
2058   /* Folder combo */
2059   impl->save_folder_label = gtk_label_new_with_mnemonic (_("Save in _Folder:"));
2060   gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
2061   gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
2062                     0, 1, 1, 2,
2063                     GTK_FILL, GTK_FILL,
2064                     0, 0);
2065   gtk_widget_show (impl->save_folder_label);
2066
2067   impl->save_folder_combo = save_folder_combo_create (impl);
2068   gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
2069                     1, 2, 1, 2,
2070                     GTK_EXPAND | GTK_FILL, GTK_FILL,
2071                     0, 0);
2072
2073   /* custom widget */
2074   impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
2075   gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
2076
2077   /* Expander */
2078   alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
2079   gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
2080
2081   impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
2082   gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
2083   g_signal_connect (impl->save_expander, "notify::expanded",
2084                     G_CALLBACK (expander_changed_cb),
2085                     impl);
2086   gtk_widget_show_all (alignment);
2087
2088   return vbox;
2089 }
2090
2091 /* Creates the main hpaned with the widgets shared by Open and Save mode */
2092 static GtkWidget *
2093 browse_widgets_create (GtkFileChooserDefault *impl)
2094 {
2095   GtkWidget *vbox;
2096   GtkWidget *hpaned;
2097   GtkWidget *widget;
2098   GtkSizeGroup *size_group;
2099
2100   /* size group is used by the [+][-] buttons and the filter combo */
2101   size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
2102   vbox = gtk_vbox_new (FALSE, 12);
2103
2104   /* Paned widget */
2105   hpaned = gtk_hpaned_new ();
2106   gtk_widget_show (hpaned);
2107   gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
2108   gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
2109
2110   widget = shortcuts_pane_create (impl, size_group);
2111   gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
2112   widget = file_pane_create (impl, size_group);
2113   gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
2114
2115   /* Alignment to hold custom widget */
2116   impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
2117   gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
2118
2119   return vbox;
2120 }
2121
2122 static GObject*
2123 gtk_file_chooser_default_constructor (GType                  type,
2124                                       guint                  n_construct_properties,
2125                                       GObjectConstructParam *construct_params)
2126 {
2127   GtkFileChooserDefault *impl;
2128   GObject *object;
2129
2130   object = parent_class->constructor (type,
2131                                       n_construct_properties,
2132                                       construct_params);
2133   impl = GTK_FILE_CHOOSER_DEFAULT (object);
2134
2135   g_assert (impl->file_system);
2136
2137   gtk_widget_push_composite_child ();
2138
2139   /* Shortcuts model */
2140
2141   shortcuts_model_create (impl);
2142
2143   /* Widgets for Save mode */
2144   impl->save_widgets = save_widgets_create (impl);
2145   gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
2146
2147   /* The browse widgets */
2148   impl->browse_widgets = browse_widgets_create (impl);
2149   gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
2150
2151   gtk_widget_pop_composite_child ();
2152   update_appearance (impl);
2153
2154   return object;
2155 }
2156
2157 /* Sets the extra_widget by packing it in the appropriate place */
2158 static void
2159 set_extra_widget (GtkFileChooserDefault *impl,
2160                   GtkWidget             *extra_widget)
2161 {
2162   if (extra_widget)
2163     {
2164       g_object_ref (extra_widget);
2165       /* FIXME: is this right ? */
2166       gtk_widget_show (extra_widget);
2167     }
2168
2169   if (impl->extra_widget)
2170     g_object_unref (impl->extra_widget);
2171
2172   impl->extra_widget = extra_widget;
2173 }
2174
2175 static void
2176 volumes_changed_cb (GtkFileSystem         *file_system,
2177                     GtkFileChooserDefault *impl)
2178 {
2179   shortcuts_add_volumes (impl);
2180 }
2181
2182 /* Callback used when the set of bookmarks changes in the file system */
2183 static void
2184 bookmarks_changed_cb (GtkFileSystem         *file_system,
2185                       GtkFileChooserDefault *impl)
2186 {
2187   shortcuts_add_bookmarks (impl);
2188
2189   bookmarks_check_add_sensitivity (impl);
2190   bookmarks_check_remove_sensitivity (impl);
2191 }
2192
2193 /* Sets the file chooser to multiple selection mode */
2194 static void
2195 set_select_multiple (GtkFileChooserDefault *impl,
2196                      gboolean               select_multiple,
2197                      gboolean               property_notify)
2198 {
2199   GtkTreeSelection *selection;
2200   GtkSelectionMode mode;
2201
2202   if (select_multiple == impl->select_multiple)
2203     return;
2204
2205   mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
2206
2207   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
2208   gtk_tree_selection_set_mode (selection, mode);
2209
2210   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2211   gtk_tree_selection_set_mode (selection, mode);
2212
2213   impl->select_multiple = select_multiple;
2214   g_object_notify (G_OBJECT (impl), "select-multiple");
2215
2216   /* FIXME #132255: See note in check_preview_change() */
2217   check_preview_change (impl);
2218 }
2219
2220 static void
2221 set_file_system_backend (GtkFileChooserDefault *impl,
2222                          const char *backend)
2223 {
2224   if (impl->file_system)
2225     {
2226       g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
2227       impl->volumes_changed_id = 0;
2228       g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
2229       impl->bookmarks_changed_id = 0;
2230       g_object_unref (impl->file_system);
2231     }
2232
2233   impl->file_system = NULL;
2234   if (backend)
2235     impl->file_system = _gtk_file_system_create (backend);
2236   else
2237     {
2238       GtkSettings *settings = gtk_settings_get_default ();
2239       gchar *default_backend = NULL;
2240       
2241       g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
2242       if (default_backend)
2243         {
2244           impl->file_system = _gtk_file_system_create (default_backend);
2245           g_free (default_backend);
2246         }
2247     }
2248
2249   if (!impl->file_system)
2250     {
2251 #if defined (G_OS_UNIX)
2252       impl->file_system = gtk_file_system_unix_new ();
2253 #elif defined (G_OS_WIN32)
2254       impl->file_system = gtk_file_system_win32_new ();
2255 #else
2256 #error "No default filesystem implementation on the platform"
2257 #endif
2258     }
2259
2260   if (impl->file_system)
2261     {
2262       impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
2263                                                    G_CALLBACK (volumes_changed_cb),
2264                                                    impl);
2265       impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
2266                                                      G_CALLBACK (bookmarks_changed_cb),
2267                                                      impl);
2268     }
2269 }
2270
2271 /* This function is basically a do_all function.
2272  *
2273  * It sets the visibility on all the widgets based on the current state, and
2274  * moves the custom_widget if needed.
2275  */
2276 static void
2277 update_appearance (GtkFileChooserDefault *impl)
2278 {
2279   GtkWidget *child;
2280
2281   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
2282       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2283     {
2284       gtk_widget_show (impl->save_widgets);
2285
2286       if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
2287         {
2288           gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
2289           gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
2290           gtk_widget_show (impl->browse_widgets);
2291         }
2292       else
2293         {
2294           gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
2295           gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
2296           gtk_widget_hide (impl->browse_widgets);
2297         }
2298
2299       gtk_widget_show (impl->browse_new_folder_button);
2300
2301       if (impl->select_multiple)
2302         {
2303           g_warning ("Save mode cannot be set in conjunction with multiple selection mode.  "
2304                      "Re-setting to single selection mode.");
2305           set_select_multiple (impl, FALSE, TRUE);
2306         }
2307     }
2308   else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2309            impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
2310     {
2311       gtk_widget_hide (impl->save_widgets);
2312       gtk_widget_show (impl->browse_widgets);
2313     }
2314   /* FIXME: */
2315   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2316       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2317     {
2318       gtk_widget_show (impl->browse_new_folder_button);
2319       gtk_widget_hide (impl->browse_files_swin);
2320       gtk_widget_show (impl->browse_directories_swin);
2321     }
2322   else
2323     {
2324       gtk_widget_hide (impl->browse_new_folder_button);
2325       gtk_widget_hide (impl->browse_directories_swin);
2326       gtk_widget_show (impl->browse_files_swin);
2327     }
2328
2329   if (impl->extra_widget)
2330     {
2331       GtkWidget *align;
2332       GtkWidget *unused_align;
2333
2334       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2335         {
2336           align = impl->save_extra_align;
2337           unused_align = impl->browse_extra_align;
2338         }
2339       else
2340         {
2341           align = impl->browse_extra_align;
2342           unused_align = impl->save_extra_align;
2343         }
2344
2345       /* We own a ref on extra_widget, so it's safe to do this */
2346       child = GTK_BIN (unused_align)->child;
2347       if (child)
2348         gtk_container_remove (GTK_CONTAINER (unused_align), child);
2349
2350       child = GTK_BIN (align)->child;
2351       if (child && child != impl->extra_widget)
2352         {
2353           gtk_container_remove (GTK_CONTAINER (align), child);
2354           gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2355         }
2356       else if (child == NULL)
2357         {
2358           gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2359         }
2360
2361       gtk_widget_show (align);
2362       gtk_widget_hide (unused_align);
2363     }
2364   else
2365     {
2366       child = GTK_BIN (impl->browse_extra_align)->child;
2367       if (child)
2368         gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
2369
2370       child = GTK_BIN (impl->save_extra_align)->child;
2371       if (child)
2372         gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
2373
2374       gtk_widget_hide (impl->save_extra_align);
2375       gtk_widget_hide (impl->browse_extra_align);
2376     }
2377
2378   g_signal_emit_by_name (impl, "default-size-changed");
2379   g_signal_emit_by_name (impl, "resizable-hints-changed");
2380 }
2381
2382 static void
2383 gtk_file_chooser_default_set_property (GObject      *object,
2384                                        guint         prop_id,
2385                                        const GValue *value,
2386                                        GParamSpec   *pspec)
2387
2388 {
2389   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2390
2391   switch (prop_id)
2392     {
2393     case GTK_FILE_CHOOSER_PROP_ACTION:
2394       {
2395         GtkFileChooserAction action = g_value_get_enum (value);
2396
2397         if (action != impl->action)
2398           {
2399             if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
2400               {
2401                 g_warning ("Multiple selection mode is not allowed in Save mode");
2402                 set_select_multiple (impl, FALSE, TRUE);
2403               }
2404             impl->action = action;
2405             update_appearance (impl);
2406           }
2407       }
2408       break;
2409     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
2410       set_file_system_backend (impl, g_value_get_string (value));
2411       break;
2412     case GTK_FILE_CHOOSER_PROP_FILTER:
2413       set_current_filter (impl, g_value_get_object (value));
2414       break;
2415     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2416       impl->local_only = g_value_get_boolean (value);
2417       break;
2418     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2419       set_preview_widget (impl, g_value_get_object (value));
2420       break;
2421     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2422       impl->preview_widget_active = g_value_get_boolean (value);
2423       update_preview_widget_visibility (impl);
2424       break;
2425     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
2426       impl->use_preview_label = g_value_get_boolean (value);
2427       update_preview_widget_visibility (impl);
2428       break;
2429     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2430       set_extra_widget (impl, g_value_get_object (value));
2431       update_appearance (impl);
2432       break;
2433     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2434       {
2435         gboolean select_multiple = g_value_get_boolean (value);
2436         if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
2437           {
2438             g_warning ("Multiple selection mode is not allowed in Save mode");
2439             return;
2440           }
2441
2442         set_select_multiple (impl, select_multiple, FALSE);
2443       }
2444       break;
2445     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2446       {
2447         gboolean show_hidden = g_value_get_boolean (value);
2448         if (show_hidden != impl->show_hidden)
2449           {
2450             impl->show_hidden = show_hidden;
2451             _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2452                                                     show_hidden);
2453             _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_files_model),
2454                                                     show_hidden);
2455           }
2456       }
2457       break;
2458     default:
2459       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2460       break;
2461     }
2462 }
2463
2464 static void
2465 gtk_file_chooser_default_get_property (GObject    *object,
2466                                        guint       prop_id,
2467                                        GValue     *value,
2468                                        GParamSpec *pspec)
2469 {
2470   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2471
2472   switch (prop_id)
2473     {
2474     case GTK_FILE_CHOOSER_PROP_ACTION:
2475       g_value_set_enum (value, impl->action);
2476       break;
2477     case GTK_FILE_CHOOSER_PROP_FILTER:
2478       g_value_set_object (value, impl->current_filter);
2479       break;
2480     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2481       g_value_set_boolean (value, impl->local_only);
2482       break;
2483     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2484       g_value_set_object (value, impl->preview_widget);
2485       break;
2486     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2487       g_value_set_boolean (value, impl->preview_widget_active);
2488       break;
2489     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
2490       g_value_set_boolean (value, impl->use_preview_label);
2491       break;
2492     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2493       g_value_set_object (value, impl->extra_widget);
2494       break;
2495     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2496       g_value_set_boolean (value, impl->select_multiple);
2497       break;
2498     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2499       g_value_set_boolean (value, impl->show_hidden);
2500       break;
2501     default:
2502       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2503       break;
2504     }
2505 }
2506
2507
2508 static void
2509 gtk_file_chooser_default_dispose (GObject *object)
2510 {
2511   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
2512
2513   if (impl->extra_widget)
2514     {
2515       g_object_unref (impl->extra_widget);
2516       impl->extra_widget = NULL;
2517     }
2518   G_OBJECT_CLASS (parent_class)->dispose (object);
2519 }
2520
2521 /* We override show-all since we have internal widgets that
2522  * shouldn't be shown when you call show_all(), like the filter
2523  * combo box.
2524  */
2525 static void
2526 gtk_file_chooser_default_show_all (GtkWidget *widget)
2527 {
2528   gtk_widget_show (widget);
2529 }
2530
2531 static void
2532 gtk_file_chooser_default_style_set      (GtkWidget *widget,
2533                                          GtkStyle  *previous_style)
2534 {
2535   if (GTK_WIDGET_CLASS (parent_class)->style_set)
2536     GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
2537
2538   g_signal_emit_by_name (widget, "default-size-changed");
2539 }
2540
2541 static void
2542 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
2543                                          GdkScreen *previous_screen)
2544 {
2545   if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
2546     GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
2547
2548   g_signal_emit_by_name (widget, "default-size-changed");
2549 }
2550
2551 static void
2552 expand_and_select_func (GtkFileSystemModel *model,
2553                         GtkTreePath        *path,
2554                         GtkTreeIter        *iter,
2555                         gpointer            user_data)
2556 {
2557   GtkFileChooserDefault *impl = user_data;
2558   GtkTreeView *tree_view;
2559
2560   if (model == impl->browse_directories_model)
2561     tree_view = GTK_TREE_VIEW (impl->browse_directories_tree_view);
2562   else
2563     tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2564
2565   gtk_tree_view_expand_to_path (tree_view, path);
2566   gtk_tree_view_expand_row (tree_view, path, FALSE);
2567   gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
2568   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), path, NULL, TRUE, 0.3, 0.5);
2569 }
2570
2571 static gboolean
2572 list_model_filter_func (GtkFileSystemModel *model,
2573                         GtkFilePath        *path,
2574                         const GtkFileInfo  *file_info,
2575                         gpointer            user_data)
2576 {
2577   GtkFileChooserDefault *impl = user_data;
2578   GtkFileFilterInfo filter_info;
2579   GtkFileFilterFlags needed;
2580   gboolean result;
2581
2582   if (!impl->current_filter)
2583     return TRUE;
2584
2585   if (gtk_file_info_get_is_folder (file_info))
2586     return TRUE;
2587
2588   filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
2589
2590   needed = gtk_file_filter_get_needed (impl->current_filter);
2591
2592   filter_info.display_name = gtk_file_info_get_display_name (file_info);
2593   filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
2594
2595   if (needed & GTK_FILE_FILTER_FILENAME)
2596     {
2597       filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
2598       if (filter_info.filename)
2599         filter_info.contains |= GTK_FILE_FILTER_FILENAME;
2600     }
2601   else
2602     filter_info.filename = NULL;
2603
2604   if (needed & GTK_FILE_FILTER_URI)
2605     {
2606       filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
2607       if (filter_info.uri)
2608         filter_info.contains |= GTK_FILE_FILTER_URI;
2609     }
2610   else
2611     filter_info.uri = NULL;
2612
2613   result = gtk_file_filter_filter (impl->current_filter, &filter_info);
2614
2615   if (filter_info.filename)
2616     g_free ((gchar *)filter_info.filename);
2617   if (filter_info.uri)
2618     g_free ((gchar *)filter_info.uri);
2619
2620   return result;
2621 }
2622
2623 static void
2624 install_list_model_filter (GtkFileChooserDefault *impl)
2625 {
2626   if (impl->current_filter)
2627     _gtk_file_system_model_set_filter (impl->browse_files_model,
2628                                        list_model_filter_func,
2629                                        impl);
2630 }
2631
2632 #define COMPARE_DIRECTORIES                                                                                    \
2633   GtkFileChooserDefault *impl = user_data;                                                                     \
2634   const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a);                           \
2635   const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b);                           \
2636   gboolean dir_a, dir_b;                                                                                       \
2637                                                                                                                \
2638   if (info_a)                                                                                                  \
2639     dir_a = gtk_file_info_get_is_folder (info_a);                                                              \
2640   else                                                                                                         \
2641     return impl->list_sort_ascending ? -1 : 1;                                                                 \
2642                                                                                                                \
2643   if (info_b)                                                                                                  \
2644     dir_b = gtk_file_info_get_is_folder (info_b);                                                              \
2645   else                                                                                                         \
2646     return impl->list_sort_ascending ? 1 : -1;                                                                 \
2647                                                                                                                \
2648   if (dir_a != dir_b)                                                                                          \
2649     return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
2650
2651 /* Sort callback for the filename column */
2652 static gint
2653 name_sort_func (GtkTreeModel *model,
2654                 GtkTreeIter  *a,
2655                 GtkTreeIter  *b,
2656                 gpointer      user_data)
2657 {
2658   COMPARE_DIRECTORIES;
2659   else
2660     return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
2661 }
2662
2663 /* Sort callback for the size column */
2664 static gint
2665 size_sort_func (GtkTreeModel *model,
2666                 GtkTreeIter  *a,
2667                 GtkTreeIter  *b,
2668                 gpointer      user_data)
2669 {
2670   COMPARE_DIRECTORIES;
2671   else
2672     {
2673       gint64 size_a = gtk_file_info_get_size (info_a);
2674       gint64 size_b = gtk_file_info_get_size (info_b);
2675
2676       return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
2677     }
2678 }
2679
2680 /* Sort callback for the mtime column */
2681 static gint
2682 mtime_sort_func (GtkTreeModel *model,
2683                  GtkTreeIter  *a,
2684                  GtkTreeIter  *b,
2685                  gpointer      user_data)
2686 {
2687   COMPARE_DIRECTORIES;
2688   else
2689     {
2690       GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
2691       GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
2692
2693       return ta > tb ? -1 : (ta == tb ? 0 : 1);
2694     }
2695 }
2696
2697 /* Callback used when the sort column changes.  We cache the sort order for use
2698  * in name_sort_func().
2699  */
2700 static void
2701 list_sort_column_changed_cb (GtkTreeSortable       *sortable,
2702                              GtkFileChooserDefault *impl)
2703 {
2704   GtkSortType sort_type;
2705
2706   if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
2707     impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
2708 }
2709
2710 /* Gets rid of the old list model and creates a new one for the current folder */
2711 static void
2712 set_list_model (GtkFileChooserDefault *impl)
2713 {
2714   if (impl->browse_files_model)
2715     {
2716       g_object_unref (impl->browse_files_model);
2717       g_object_unref (impl->sort_model);
2718     }
2719
2720   impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
2721                                                  impl->current_folder, 0,
2722                                                  GTK_FILE_INFO_ALL);
2723   _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
2724   install_list_model_filter (impl);
2725
2726   impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
2727   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
2728   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
2729   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
2730   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
2731   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
2732   impl->list_sort_ascending = TRUE;
2733
2734   g_signal_connect (impl->sort_model, "sort_column_changed",
2735                     G_CALLBACK (list_sort_column_changed_cb), impl);
2736
2737   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
2738                            GTK_TREE_MODEL (impl->sort_model));
2739   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
2740   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
2741                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
2742 }
2743
2744 /* Gets rid of the old folder tree model and creates a new one for the volume
2745  * corresponding to the specified path.
2746  */
2747 static void
2748 set_tree_model (GtkFileChooserDefault *impl, const GtkFilePath *path)
2749 {
2750   GtkFileSystemVolume *volume;
2751   GtkFilePath *base_path, *parent_path;
2752
2753   base_path = NULL;
2754
2755   volume = gtk_file_system_get_volume_for_path (impl->file_system, path);
2756
2757   if (volume)
2758     base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2759
2760   if (base_path == NULL)
2761     {
2762       base_path = gtk_file_path_copy (path);
2763       while (gtk_file_system_get_parent (impl->file_system,
2764                                          base_path,
2765                                          &parent_path,
2766                                          NULL) &&
2767              parent_path != NULL)
2768         {
2769           gtk_file_path_free (base_path);
2770           base_path = parent_path;
2771         }
2772     }
2773
2774   if (impl->current_volume_path && gtk_file_path_compare (base_path, impl->current_volume_path) == 0)
2775     goto out;
2776
2777   if (impl->browse_directories_model)
2778     g_object_unref (impl->browse_directories_model);
2779
2780   impl->current_volume_path = gtk_file_path_copy (base_path);
2781
2782   impl->browse_directories_model = _gtk_file_system_model_new (impl->file_system, impl->current_volume_path, -1,
2783                                 GTK_FILE_INFO_DISPLAY_NAME);
2784   _gtk_file_system_model_set_show_files (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2785                                          FALSE);
2786   _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2787                                           impl->show_hidden);
2788
2789   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_directories_tree_view),
2790                            GTK_TREE_MODEL (impl->browse_directories_model));
2791
2792  out:
2793
2794   gtk_file_path_free (base_path);
2795   if (volume)
2796     gtk_file_system_volume_free (impl->file_system, volume);
2797 }
2798
2799 static void
2800 update_chooser_entry (GtkFileChooserDefault *impl)
2801 {
2802   GtkTreeSelection *selection;
2803   const GtkFileInfo *info;
2804   GtkTreeIter iter;
2805   GtkTreeIter child_iter;
2806
2807   if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
2808     return;
2809
2810   g_assert (!impl->select_multiple);
2811   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2812
2813   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2814     return;
2815
2816   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2817                                                   &child_iter,
2818                                                   &iter);
2819
2820   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
2821
2822   if (!gtk_file_info_get_is_folder (info))
2823     gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry),
2824                         gtk_file_info_get_display_name (info));
2825 }
2826
2827 static void
2828 gtk_file_chooser_default_set_current_folder (GtkFileChooser    *chooser,
2829                                              const GtkFilePath *path)
2830 {
2831   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2832
2833   if (impl->current_folder)
2834     gtk_file_path_free (impl->current_folder);
2835
2836   impl->current_folder = gtk_file_path_copy (path);
2837
2838   /* Change the current folder label */
2839   gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, impl->file_system, NULL);
2840
2841   /* Update the folder tree */
2842
2843   if (!impl->changing_folder)
2844     {
2845       impl->changing_folder = TRUE;
2846       set_tree_model (impl, impl->current_folder);
2847       _gtk_file_system_model_path_do (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2848                                       path, expand_and_select_func, impl);
2849       impl->changing_folder = FALSE;
2850     }
2851
2852   /* Create a new list model */
2853   set_list_model (impl);
2854
2855   /* Refresh controls */
2856
2857   shortcuts_unselect_all (impl);
2858   shortcuts_update_current_folder (impl);
2859
2860   g_signal_emit_by_name (impl, "current-folder-changed", 0);
2861
2862   check_preview_change (impl);
2863   bookmarks_check_add_sensitivity (impl);
2864
2865   g_signal_emit_by_name (impl, "selection-changed", 0);
2866 }
2867
2868 static GtkFilePath *
2869 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
2870 {
2871   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2872
2873   return gtk_file_path_copy (impl->current_folder);
2874 }
2875
2876 static void
2877 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
2878                                            const gchar    *name)
2879 {
2880   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2881
2882   g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
2883
2884   gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry), name);
2885 }
2886
2887 static void
2888 select_func (GtkFileSystemModel *model,
2889              GtkTreePath        *path,
2890              GtkTreeIter        *iter,
2891              gpointer            user_data)
2892 {
2893   GtkFileChooserDefault *impl = user_data;
2894   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2895   GtkTreePath *sorted_path;
2896
2897   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
2898   gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
2899   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), sorted_path, NULL, TRUE, 0.3, 0.0);
2900   gtk_tree_path_free (sorted_path);
2901 }
2902
2903 static void
2904 gtk_file_chooser_default_select_path (GtkFileChooser    *chooser,
2905                                       const GtkFilePath *path)
2906 {
2907   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2908   GtkFilePath *parent_path;
2909   GError *error;
2910
2911   error = NULL;
2912   if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, &error))
2913     {
2914       error_getting_info_dialog (impl, path, error);
2915       return;
2916     }
2917
2918   if (!parent_path)
2919     {
2920       _gtk_file_chooser_set_current_folder_path (chooser, path);
2921     }
2922   else
2923     {
2924       _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
2925       gtk_file_path_free (parent_path);
2926       _gtk_file_system_model_path_do (impl->browse_files_model, path,
2927                                       select_func, impl);
2928     }
2929 }
2930
2931 static void
2932 unselect_func (GtkFileSystemModel *model,
2933                GtkTreePath        *path,
2934                GtkTreeIter        *iter,
2935                gpointer            user_data)
2936 {
2937   GtkFileChooserDefault *impl = user_data;
2938   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2939   GtkTreePath *sorted_path;
2940
2941   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
2942                                                                 path);
2943   gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
2944                                     sorted_path);
2945   gtk_tree_path_free (sorted_path);
2946 }
2947
2948 static void
2949 gtk_file_chooser_default_unselect_path (GtkFileChooser    *chooser,
2950                                         const GtkFilePath *path)
2951 {
2952   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2953
2954   _gtk_file_system_model_path_do (impl->browse_files_model, path,
2955                                  unselect_func, impl);
2956 }
2957
2958 static void
2959 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
2960 {
2961   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2962   if (impl->select_multiple)
2963     {
2964       GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2965       gtk_tree_selection_select_all (selection);
2966     }
2967 }
2968
2969 static void
2970 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
2971 {
2972   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2973   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2974
2975   gtk_tree_selection_unselect_all (selection);
2976 }
2977
2978 struct get_paths_closure {
2979   GtkFileChooserDefault *impl;
2980   GSList *result;
2981   GtkFilePath *path_from_entry;
2982 };
2983
2984 static void
2985 get_paths_foreach (GtkTreeModel *model,
2986                    GtkTreePath  *path,
2987                    GtkTreeIter  *iter,
2988                    gpointer      data)
2989 {
2990   struct get_paths_closure *info;
2991   const GtkFilePath *file_path;
2992   GtkFileSystemModel *fs_model;
2993   GtkTreeIter sel_iter;
2994
2995   info = data;
2996
2997   if (info->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2998       info->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2999     {
3000       fs_model = info->impl->browse_directories_model;
3001       sel_iter = *iter;
3002     }
3003   else
3004     {
3005       fs_model = info->impl->browse_files_model;
3006       gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
3007     }
3008
3009   file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &sel_iter);
3010
3011   if (!info->path_from_entry
3012       || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
3013     info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
3014 }
3015
3016 static GSList *
3017 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
3018 {
3019   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3020   struct get_paths_closure info;
3021
3022   info.impl = impl;
3023   info.result = NULL;
3024   info.path_from_entry = NULL;
3025
3026   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3027     {
3028       const char *filename;
3029
3030       filename = gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry));
3031
3032       if (filename != NULL && filename[0] != '\0')
3033         {
3034           GtkFilePath *selected;
3035           GError *error = NULL;
3036
3037           selected = gtk_file_system_make_path (impl->file_system, impl->current_folder, filename, &error);
3038
3039           if (!selected)
3040             {
3041               error_building_filename_dialog (impl, impl->current_folder, filename, error);
3042               return NULL;
3043             }
3044
3045           info.path_from_entry = selected;
3046         }
3047     }
3048
3049   if (!info.path_from_entry || impl->select_multiple)
3050     {
3051       GtkTreeSelection *selection;
3052
3053       selection = NULL;
3054
3055       if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3056           impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3057         {
3058           if (impl->browse_directories_model)
3059             selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
3060         }
3061       else
3062         {
3063           if (impl->sort_model)
3064             selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3065         }
3066
3067       if (selection)
3068         gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
3069     }
3070
3071   if (info.path_from_entry)
3072     info.result = g_slist_prepend (info.result, info.path_from_entry);
3073
3074   return g_slist_reverse (info.result);
3075 }
3076
3077 static GtkFilePath *
3078 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
3079 {
3080   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3081
3082   if (impl->preview_path)
3083     return gtk_file_path_copy (impl->preview_path);
3084   else
3085     return NULL;
3086 }
3087
3088 static GtkFileSystem *
3089 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
3090 {
3091   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3092
3093   return impl->file_system;
3094 }
3095
3096 /* Shows or hides the filter widgets */
3097 static void
3098 toolbar_show_filters (GtkFileChooserDefault *impl,
3099                       gboolean               show)
3100 {
3101   if (show)
3102     gtk_widget_show (impl->filter_combo);
3103   else
3104     gtk_widget_hide (impl->filter_combo);
3105 }
3106
3107 static void
3108 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
3109                                      GtkFileFilter  *filter)
3110 {
3111   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3112   const gchar *name;
3113
3114   if (g_slist_find (impl->filters, filter))
3115     {
3116       g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
3117       return;
3118     }
3119
3120   g_object_ref (filter);
3121   gtk_object_sink (GTK_OBJECT (filter));
3122   impl->filters = g_slist_append (impl->filters, filter);
3123
3124   name = gtk_file_filter_get_name (filter);
3125   if (!name)
3126     name = "Untitled filter";   /* Place-holder, doesn't need to be marked for translation */
3127
3128   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
3129
3130   if (!g_slist_find (impl->filters, impl->current_filter))
3131     set_current_filter (impl, filter);
3132
3133   toolbar_show_filters (impl, TRUE);
3134 }
3135
3136 static void
3137 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
3138                                         GtkFileFilter  *filter)
3139 {
3140   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3141   GtkTreeModel *model;
3142   GtkTreeIter iter;
3143   gint filter_index;
3144
3145   filter_index = g_slist_index (impl->filters, filter);
3146
3147   if (filter_index < 0)
3148     {
3149       g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
3150       return;
3151     }
3152
3153   impl->filters = g_slist_remove (impl->filters, filter);
3154
3155   if (filter == impl->current_filter)
3156     {
3157       if (impl->filters)
3158         set_current_filter (impl, impl->filters->data);
3159       else
3160         set_current_filter (impl, NULL);
3161     }
3162
3163   /* Remove row from the combo box */
3164   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
3165   gtk_tree_model_iter_nth_child  (model, &iter, NULL, filter_index);
3166   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
3167
3168   g_object_unref (filter);
3169
3170   if (!impl->filters)
3171     toolbar_show_filters (impl, FALSE);
3172 }
3173
3174 static GSList *
3175 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
3176 {
3177   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3178
3179   return g_slist_copy (impl->filters);
3180 }
3181
3182 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
3183 static int
3184 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
3185                                        int                    pos)
3186 {
3187   return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
3188 }
3189
3190 static gboolean
3191 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser    *chooser,
3192                                               const GtkFilePath *path,
3193                                               GError           **error)
3194 {
3195   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3196   gboolean result;
3197   int pos;
3198
3199   pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
3200
3201   result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
3202
3203   if (result)
3204     impl->num_shortcuts++;
3205
3206   return result;
3207 }
3208
3209 static gboolean
3210 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
3211                                                  const GtkFilePath *path,
3212                                                  GError           **error)
3213 {
3214   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3215   int pos;
3216   GtkTreeIter iter;
3217   int i;
3218
3219   if (impl->num_shortcuts == 0)
3220     goto out;
3221
3222   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3223   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3224     g_assert_not_reached ();
3225
3226   for (i = 0; i < impl->num_shortcuts; i++)
3227     {
3228       GtkFilePath *shortcut;
3229
3230       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3231       g_assert (shortcut != NULL);
3232
3233       if (gtk_file_path_compare (shortcut, path) == 0)
3234         {
3235           /* The other columns are freed by the GtkTreeStore */
3236           gtk_file_path_free (shortcut);
3237           gtk_list_store_remove (impl->shortcuts_model, &iter);
3238           impl->num_shortcuts--;
3239           return TRUE;
3240         }
3241
3242       if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3243         g_assert_not_reached ();
3244     }
3245
3246  out:
3247
3248   g_set_error (error,
3249                GTK_FILE_CHOOSER_ERROR,
3250                GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
3251                _("shortcut %s does not exist"),
3252                gtk_file_path_get_string (path));
3253
3254   return FALSE;
3255 }
3256
3257 static GSList *
3258 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
3259 {
3260   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3261   int pos;
3262   GtkTreeIter iter;
3263   int i;
3264   GSList *list;
3265
3266   if (impl->num_shortcuts == 0)
3267     return NULL;
3268
3269   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3270   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3271     g_assert_not_reached ();
3272
3273   list = NULL;
3274
3275   for (i = 0; i < impl->num_shortcuts; i++)
3276     {
3277       GtkFilePath *shortcut;
3278
3279       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3280       g_assert (shortcut != NULL);
3281
3282       list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
3283
3284       if (i != impl->num_shortcuts - 1)
3285         {
3286           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3287             g_assert_not_reached ();
3288         }
3289     }
3290
3291   return g_slist_reverse (list);
3292 }
3293
3294 /* Guesses a size based upon font sizes */
3295 static void
3296 find_good_size_from_style (GtkWidget *widget,
3297                            gint      *width,
3298                            gint      *height)
3299 {
3300   GtkFileChooserDefault *impl;
3301   gint default_width, default_height;
3302   int font_size;
3303   GtkRequisition req;
3304   GtkRequisition preview_req;
3305
3306   g_assert (widget->style != NULL);
3307   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3308
3309   font_size = pango_font_description_get_size (widget->style->font_desc);
3310   font_size = PANGO_PIXELS (font_size);
3311
3312   default_width = font_size * NUM_CHARS;
3313   default_height = font_size * NUM_LINES;
3314
3315   /* Use at least the requisition size not including the preview widget */
3316   gtk_widget_size_request (widget, &req);
3317
3318   if (impl->preview_widget_active && impl->preview_widget)
3319     gtk_widget_size_request (impl->preview_box, &preview_req);
3320   else
3321     preview_req.width = 0;
3322
3323   default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
3324   default_height = MAX (default_height, req.height);
3325
3326   *width = default_width;
3327   *height = default_height;
3328 }
3329
3330 static void
3331 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
3332                                            gint                *default_width,
3333                                            gint                *default_height)
3334 {
3335   GtkFileChooserDefault *impl;
3336
3337   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
3338
3339   find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
3340
3341   if (impl->preview_widget_active && impl->preview_widget)
3342     *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
3343 }
3344
3345 static void
3346 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
3347                                               gboolean            *resize_horizontally,
3348                                               gboolean            *resize_vertically)
3349 {
3350   GtkFileChooserDefault *impl;
3351
3352   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
3353
3354   if (*resize_horizontally)
3355     *resize_horizontally = TRUE;
3356
3357   if (resize_vertically)
3358     {
3359       *resize_vertically = TRUE;
3360
3361       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3362           impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3363         {
3364           if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3365             {
3366               *resize_vertically = FALSE;
3367               *resize_horizontally = FALSE;
3368             }
3369         }
3370     }
3371 }
3372
3373 static void
3374 set_current_filter (GtkFileChooserDefault *impl,
3375                     GtkFileFilter         *filter)
3376 {
3377   if (impl->current_filter != filter)
3378     {
3379       int filter_index;
3380
3381       /* If we have filters, new filter must be one of them
3382        */
3383       filter_index = g_slist_index (impl->filters, filter);
3384       if (impl->filters && filter_index < 0)
3385         return;
3386
3387       if (impl->current_filter)
3388         g_object_unref (impl->current_filter);
3389       impl->current_filter = filter;
3390       if (impl->current_filter)
3391         {
3392           g_object_ref (impl->current_filter);
3393           gtk_object_sink (GTK_OBJECT (filter));
3394         }
3395
3396       if (impl->filters)
3397         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
3398                                   filter_index);
3399
3400       install_list_model_filter (impl);
3401
3402       g_object_notify (G_OBJECT (impl), "filter");
3403     }
3404 }
3405
3406 static void
3407 open_and_close (GtkTreeView *tree_view,
3408                 GtkTreePath *target_path)
3409 {
3410   GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
3411   GtkTreeIter iter;
3412   GtkTreePath *path;
3413
3414   path = gtk_tree_path_new ();
3415   gtk_tree_path_append_index (path, 0);
3416
3417   gtk_tree_model_get_iter (model, &iter, path);
3418
3419   while (TRUE)
3420     {
3421       if (gtk_tree_path_is_ancestor (path, target_path) ||
3422           gtk_tree_path_compare (path, target_path) == 0)
3423         {
3424           GtkTreeIter child_iter;
3425           gtk_tree_view_expand_row (tree_view, path, FALSE);
3426           if (gtk_tree_model_iter_children (model, &child_iter, &iter))
3427             {
3428               iter = child_iter;
3429               gtk_tree_path_down (path);
3430               goto next;
3431             }
3432         }
3433       else
3434         gtk_tree_view_collapse_row (tree_view, path);
3435
3436       while (TRUE)
3437         {
3438           GtkTreeIter parent_iter;
3439           GtkTreeIter next_iter;
3440
3441           next_iter = iter;
3442           if (gtk_tree_model_iter_next (model, &next_iter))
3443             {
3444               iter = next_iter;
3445               gtk_tree_path_next (path);
3446               goto next;
3447             }
3448
3449           if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
3450             goto out;
3451
3452           iter = parent_iter;
3453           gtk_tree_path_up (path);
3454         }
3455     next:
3456       ;
3457     }
3458
3459  out:
3460   gtk_tree_path_free (path);
3461 }
3462
3463 static void
3464 filter_combo_changed (GtkComboBox           *combo_box,
3465                       GtkFileChooserDefault *impl)
3466 {
3467   gint new_index = gtk_combo_box_get_active (combo_box);
3468   GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
3469
3470   set_current_filter (impl, new_filter);
3471 }
3472
3473 static void
3474 check_preview_change (GtkFileChooserDefault *impl)
3475 {
3476   const GtkFilePath *new_path = NULL;
3477   const GtkFileInfo *new_info = NULL;
3478
3479   /* FIXME #132255: Fixing preview for multiple selection involves getting the
3480    * full selection and diffing to find out what the most recently selected file
3481    * is; there is logic in GtkFileSelection that probably can be
3482    * copied.
3483    */
3484   if (impl->sort_model && !impl->select_multiple)
3485     {
3486       GtkTreeSelection *selection;
3487       GtkTreeIter iter;
3488
3489       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3490       if (gtk_tree_selection_get_selected (selection, NULL, &iter))
3491         {
3492           GtkTreeIter child_iter;
3493
3494           gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3495                                                           &child_iter, &iter);
3496
3497           new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3498           new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3499         }
3500     }
3501
3502   if (new_path != impl->preview_path &&
3503       !(new_path && impl->preview_path &&
3504         gtk_file_path_compare (new_path, impl->preview_path) == 0))
3505     {
3506       if (impl->preview_path)
3507         {
3508           gtk_file_path_free (impl->preview_path);
3509           g_free (impl->preview_display_name);
3510         }
3511
3512       if (new_path)
3513         {
3514           impl->preview_path = gtk_file_path_copy (new_path);
3515           impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
3516         }
3517       else
3518         {
3519           impl->preview_path = NULL;
3520           impl->preview_display_name = NULL;
3521         }
3522
3523       if (impl->use_preview_label && impl->preview_label)
3524         gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
3525
3526       g_signal_emit_by_name (impl, "update-preview");
3527     }
3528 }
3529
3530 static void
3531 tree_selection_changed (GtkTreeSelection      *selection,
3532                         GtkFileChooserDefault *impl)
3533 {
3534   GtkTreeIter iter;
3535   const GtkFilePath *file_path;
3536   GtkTreePath *path;
3537
3538   /* FIXME #132255: Fixing this for multiple selection involves getting the full
3539    * selection and diffing to find out what the most recently selected file is;
3540    * there is logic in GtkFileSelection that probably can be copied;
3541    * check_preview_change() is similar.
3542    */
3543   if (impl->select_multiple
3544       || !gtk_tree_selection_get_selected (selection, NULL, &iter))
3545     return;
3546
3547   file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3548                                                &iter);
3549   if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
3550     return;
3551
3552   /* Close the tree up to only the parents of the newly selected
3553    * node and it's immediate children are visible.
3554    */
3555   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_directories_model), &iter);
3556   open_and_close (GTK_TREE_VIEW (impl->browse_directories_tree_view), path);
3557   gtk_tree_path_free (path);
3558
3559   if (!impl->changing_folder)
3560     _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3561 }
3562
3563 /* Activates a volume by mounting it if necessary and then switching to its
3564  * base path.
3565  */
3566 static void
3567 shortcuts_activate_volume (GtkFileChooserDefault *impl,
3568                            GtkFileSystemVolume   *volume)
3569 {
3570   GtkFilePath *path;
3571
3572   if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
3573     {
3574       GError *error;
3575
3576       error = NULL;
3577       if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
3578         {
3579           char *msg;
3580
3581           msg = g_strdup_printf ("Could not mount %s:\n%s",
3582                                  gtk_file_system_volume_get_display_name (impl->file_system, volume),
3583                                  error->message);
3584           error_message (impl, msg);
3585           g_free (msg);
3586           g_error_free (error);
3587
3588           return;
3589         }
3590     }
3591
3592   path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
3593   _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
3594   gtk_file_path_free (path);
3595 }
3596
3597 /* Opens the folder or volume at the specified index in the shortcuts list */
3598 static void
3599 shortcuts_activate_item (GtkFileChooserDefault *impl,
3600                          int                    item_num)
3601 {
3602   GtkTreePath *path;
3603   gboolean result;
3604   GtkTreeIter iter;
3605   gpointer data;
3606   int start_row;
3607
3608   if (item_num == shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR)
3609       || item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR))
3610     return;
3611
3612   path = gtk_tree_path_new_from_indices (item_num, -1);
3613   result = gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path);
3614   gtk_tree_path_free (path);
3615
3616   if (!result)
3617     return;
3618
3619   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
3620
3621   start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
3622   if ((item_num >= start_row && item_num < start_row + impl->num_volumes)
3623       || (item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER) && impl->shortcuts_current_folder_is_volume))
3624     {
3625       GtkFileSystemVolume *volume;
3626
3627       volume = data;
3628       shortcuts_activate_volume (impl, volume);
3629     }
3630   else
3631     {
3632       GtkFilePath *file_path;
3633
3634       file_path = data;
3635       _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3636     }
3637 }
3638
3639 /* Callback used when a row in the shortcuts list is activated */
3640 static void
3641 shortcuts_row_activated_cb (GtkTreeView           *tree_view,
3642                             GtkTreePath           *path,
3643                             GtkTreeViewColumn     *column,
3644                             GtkFileChooserDefault *impl)
3645 {
3646   int selected;
3647   GtkTreeIter iter;
3648   GtkTreeIter child_iter;
3649   GtkTreePath *child_path;
3650
3651   if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
3652     return;
3653
3654   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
3655                                                     &child_iter,
3656                                                     &iter);
3657   child_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter);
3658   if (!child_path)
3659     return;
3660
3661   selected = *gtk_tree_path_get_indices (child_path);
3662   gtk_tree_path_free (child_path);
3663
3664   shortcuts_activate_item (impl, selected);
3665 }
3666
3667 static gboolean
3668 shortcuts_select_func  (GtkTreeSelection  *selection,
3669                         GtkTreeModel      *model,
3670                         GtkTreePath       *path,
3671                         gboolean           path_currently_selected,
3672                         gpointer           data)
3673 {
3674   GtkFileChooserDefault *impl = data;
3675
3676   return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
3677 }
3678
3679 static void
3680 list_selection_changed (GtkTreeSelection      *selection,
3681                         GtkFileChooserDefault *impl)
3682 {
3683   /* See if we are in the new folder editable row for Save mode */
3684   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3685     {
3686       GtkTreeSelection *selection;
3687       GtkTreeIter iter, child_iter;
3688       const GtkFileInfo *info;
3689
3690       g_assert (!impl->select_multiple);
3691       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3692       if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
3693         return;
3694
3695       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3696                                                       &child_iter,
3697                                                       &iter);
3698
3699       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3700       if (!info)
3701         return; /* We are on the editable row for New Folder */
3702     }
3703
3704   update_chooser_entry (impl);
3705   check_preview_change (impl);
3706   bookmarks_check_add_sensitivity (impl);
3707
3708   g_signal_emit_by_name (impl, "selection-changed", 0);
3709 }
3710
3711 /* Callback used when a row in the file list is activated */
3712 static void
3713 list_row_activated (GtkTreeView           *tree_view,
3714                     GtkTreePath           *path,
3715                     GtkTreeViewColumn     *column,
3716                     GtkFileChooserDefault *impl)
3717 {
3718   GtkTreeIter iter, child_iter;
3719   const GtkFileInfo *info;
3720
3721   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
3722     return;
3723
3724   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
3725
3726   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3727
3728   if (gtk_file_info_get_is_folder (info))
3729     {
3730       const GtkFilePath *file_path;
3731
3732       file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3733       _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3734
3735       return;
3736     }
3737
3738   g_signal_emit_by_name (impl, "file-activated");
3739 }
3740
3741 static void
3742 path_bar_clicked (GtkPathBar            *path_bar,
3743                   GtkFilePath           *file_path,
3744                   GtkFileChooserDefault *impl)
3745 {
3746   _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3747 }
3748
3749 static const GtkFileInfo *
3750 get_list_file_info (GtkFileChooserDefault *impl,
3751                     GtkTreeIter           *iter)
3752 {
3753   GtkTreeIter child_iter;
3754
3755   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3756                                                   &child_iter,
3757                                                   iter);
3758
3759   return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3760 }
3761
3762 static void
3763 tree_name_data_func (GtkTreeViewColumn *tree_column,
3764                      GtkCellRenderer   *cell,
3765                      GtkTreeModel      *tree_model,
3766                      GtkTreeIter       *iter,
3767                      gpointer           data)
3768 {
3769   GtkFileChooserDefault *impl = data;
3770   const GtkFileInfo *info;
3771
3772   info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3773                                           iter);
3774
3775   if (info)
3776     {
3777       g_object_set (cell,
3778                     "text", gtk_file_info_get_display_name (info),
3779                     NULL);
3780     }
3781 }
3782
3783 static void
3784 list_icon_data_func (GtkTreeViewColumn *tree_column,
3785                      GtkCellRenderer   *cell,
3786                      GtkTreeModel      *tree_model,
3787                      GtkTreeIter       *iter,
3788                      gpointer           data)
3789 {
3790   GtkFileChooserDefault *impl = data;
3791   GtkTreeIter child_iter;
3792   const GtkFilePath *path;
3793   GdkPixbuf *pixbuf;
3794
3795   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3796                                                   &child_iter,
3797                                                   iter);
3798   path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3799   if (!path)
3800     return;
3801
3802   /* FIXME: NULL GError */
3803   pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
3804   g_object_set (cell,
3805                 "pixbuf", pixbuf,
3806                 NULL);
3807
3808   if (pixbuf)
3809     g_object_unref (pixbuf);
3810 }
3811
3812 static void
3813 list_name_data_func (GtkTreeViewColumn *tree_column,
3814                      GtkCellRenderer   *cell,
3815                      GtkTreeModel      *tree_model,
3816                      GtkTreeIter       *iter,
3817                      gpointer           data)
3818 {
3819   GtkFileChooserDefault *impl = data;
3820   const GtkFileInfo *info = get_list_file_info (impl, iter);
3821
3822   if (!info)
3823     {
3824       g_object_set (cell,
3825                     "text", _("Type name of new folder"),
3826                     NULL);
3827       return;
3828     }
3829
3830   g_object_set (cell,
3831                 "text", gtk_file_info_get_display_name (info),
3832                 NULL);
3833 }
3834
3835 #if 0
3836 static void
3837 list_size_data_func (GtkTreeViewColumn *tree_column,
3838                      GtkCellRenderer   *cell,
3839                      GtkTreeModel      *tree_model,
3840                      GtkTreeIter       *iter,
3841                      gpointer           data)
3842 {
3843   GtkFileChooserDefault *impl = data;
3844   const GtkFileInfo *info = get_list_file_info (impl, iter);
3845   gint64 size;
3846   gchar *str;
3847
3848   if (!info || gtk_file_info_get_is_folder (info))
3849     return;
3850
3851   size = gtk_file_info_get_size (info);
3852
3853   if (size < (gint64)1024)
3854     str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
3855   else if (size < (gint64)1024*1024)
3856     str = g_strdup_printf (_("%.1f K"), size / (1024.));
3857   else if (size < (gint64)1024*1024*1024)
3858     str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
3859   else
3860     str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
3861
3862   g_object_set (cell,
3863                 "text", str,
3864                 NULL);
3865
3866   g_free (str);
3867 }
3868 #endif
3869
3870 /* Tree column data callback for the file list; fetches the mtime of a file */
3871 static void
3872 list_mtime_data_func (GtkTreeViewColumn *tree_column,
3873                       GtkCellRenderer   *cell,
3874                       GtkTreeModel      *tree_model,
3875                       GtkTreeIter       *iter,
3876                       gpointer           data)
3877 {
3878   GtkFileChooserDefault *impl;
3879   const GtkFileInfo *info;
3880   GtkFileTime time_mtime, time_now;
3881   GDate mtime, now;
3882   int days_diff;
3883   char buf[256];
3884
3885   impl = data;
3886
3887   info = get_list_file_info (impl, iter);
3888   if (!info)
3889     {
3890       g_object_set (cell,
3891                     "text", "",
3892                     NULL);
3893       return;
3894     }
3895
3896   time_mtime = gtk_file_info_get_modification_time (info);
3897   g_date_set_time (&mtime, (GTime) time_mtime);
3898
3899   time_now = (GTime ) time (NULL);
3900   g_date_set_time (&now, (GTime) time_now);
3901
3902   days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
3903
3904   if (days_diff == 0)
3905     strcpy (buf, _("Today"));
3906   else if (days_diff == 1)
3907     strcpy (buf, _("Yesterday"));
3908   else
3909     {
3910       char *format;
3911
3912       if (days_diff > 1 && days_diff < 7)
3913         format = "%A"; /* Days from last week */
3914       else
3915         /* FIXME: Get the right format for the locale */
3916         format = _("%d/%b/%Y"); /* Any other date */
3917
3918       if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
3919         strcpy (buf, _("Unknown"));
3920     }
3921
3922   g_object_set (cell,
3923                 "text", buf,
3924                 NULL);
3925 }
3926
3927 GtkWidget *
3928 _gtk_file_chooser_default_new (const char *file_system)
3929 {
3930   return  g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
3931                         "file-system-backend", file_system,
3932                         NULL);
3933 }
3934
3935 static GtkWidget *
3936 location_entry_create (GtkFileChooserDefault *impl)
3937 {
3938   GtkWidget *entry;
3939
3940   entry = _gtk_file_chooser_entry_new ();
3941   /* Pick a good width for the entry */
3942   gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
3943   gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
3944   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
3945   _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
3946
3947   return GTK_WIDGET (entry);
3948 }
3949
3950 static void
3951 update_from_entry (GtkFileChooserDefault *impl,
3952                    GtkWindow             *parent,
3953                    GtkFileChooserEntry   *chooser_entry)
3954 {
3955   const GtkFilePath *folder_path;
3956   const char *file_part;
3957
3958   folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
3959   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
3960
3961   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
3962     {
3963       error_message_with_parent (parent,
3964                                  _("Cannot change to the folder you specified as it is an invalid path."));
3965       return;
3966     }
3967
3968   if (file_part[0] == '\0')
3969     {
3970       _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), folder_path);
3971       return;
3972     }
3973   else
3974     {
3975       GtkFileFolder *folder = NULL;
3976       GtkFilePath *subfolder_path = NULL;
3977       GtkFileInfo *info = NULL;
3978       GError *error;
3979
3980       /* If the file part is non-empty, we need to figure out if it refers to a
3981        * folder within folder. We could optimize the case here where the folder
3982        * is already loaded for one of our tree models.
3983        */
3984
3985       error = NULL;
3986       folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
3987
3988       if (!folder)
3989         {
3990           error_getting_info_dialog (impl, folder_path, error);
3991           return;
3992         }
3993
3994       error = NULL;
3995       subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
3996
3997       if (!subfolder_path)
3998         {
3999           char *msg;
4000
4001           msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
4002                                  gtk_file_path_get_string (folder_path),
4003                                  file_part,
4004                                  error->message);
4005           error_message (impl, msg);
4006           g_free (msg);
4007           g_object_unref (folder);
4008           return;
4009         }
4010
4011       error = NULL;
4012       info = gtk_file_folder_get_info (folder, subfolder_path, &error);
4013
4014       if (!info)
4015         {
4016 #if 0
4017           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4018             {
4019               g_object_unref (folder);
4020               gtk_file_path_free (subfolder_path);
4021               return;
4022             }
4023 #endif
4024           error_getting_info_dialog (impl, subfolder_path, error);
4025           g_object_unref (folder);
4026           gtk_file_path_free (subfolder_path);
4027           return;
4028         }
4029
4030       if (gtk_file_info_get_is_folder (info))
4031         _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), subfolder_path);
4032       else
4033         _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path);
4034
4035       g_object_unref (folder);
4036       gtk_file_path_free (subfolder_path);
4037       gtk_file_info_free (info);
4038     }
4039 }
4040
4041 static void
4042 location_popup_handler (GtkFileChooserDefault *impl)
4043 {
4044   GtkWidget *dialog;
4045   GtkWidget *toplevel;
4046   GtkWidget *hbox;
4047   GtkWidget *label;
4048   GtkWidget *entry;
4049
4050   /* Create dialog */
4051
4052   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
4053   if (!GTK_WIDGET_TOPLEVEL (toplevel))
4054     toplevel = NULL;
4055
4056   dialog = gtk_dialog_new_with_buttons (_("Open Location"),
4057                                         GTK_WINDOW (toplevel),
4058                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
4059                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
4060                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
4061                                         NULL);
4062   gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
4063   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
4064   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
4065   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
4066
4067   hbox = gtk_hbox_new (FALSE, 12);
4068   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
4069   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
4070
4071   label = gtk_label_new_with_mnemonic (_("_Location:"));
4072   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
4073
4074   entry = location_entry_create (impl);
4075   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
4076   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
4077
4078   /* Run */
4079
4080   gtk_widget_show_all (dialog);
4081   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
4082     update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry));
4083
4084   gtk_widget_destroy (dialog);
4085 }
4086
4087 /* Handler for the "up-folder" keybinding signal */
4088 static void
4089 up_folder_handler (GtkFileChooserDefault *impl)
4090 {
4091   GtkFilePath *parent_path;
4092   GError *error;
4093
4094   error = NULL;
4095   if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, &error))
4096     {
4097       if (parent_path) /* If we were on a root, parent_path will be NULL */
4098         {
4099           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), parent_path);
4100           gtk_file_path_free (parent_path);
4101         }
4102     }
4103   else
4104     error_dialog (impl,
4105                   _("Could not go to the parent folder of %s:\n%s"),
4106                   impl->current_folder,
4107                   error);
4108 }
4109
4110 /* Handler for the "home-folder" keybinding signal */
4111 static void
4112 home_folder_handler (GtkFileChooserDefault *impl)
4113 {
4114   const char *home;
4115
4116   /* Should we pull this information from impl->has_home and the shortcuts data
4117    * instead?  Sounds like a bit of overkill...
4118    */
4119
4120   home = g_get_home_dir ();
4121   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), home);
4122 }