]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdefault.c
Handle errors in setting the path bar's path. Fixes #136000, based on a
[~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() in shortcuts_add_volumes() */
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   gboolean success;
1062
1063   g_assert (!impl->shortcuts_current_folder_active);
1064
1065   success = TRUE;
1066
1067   pos = shortcut_find_position (impl, impl->current_folder);
1068   if (pos == -1)
1069     {
1070       GtkFileSystemVolume *volume;
1071       GtkFilePath *base_path;
1072
1073       /* Separator */
1074
1075       shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1076
1077       /* Item */
1078
1079       pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1080
1081       volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1082       base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1083
1084       if (strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1085         {
1086           success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1087           impl->shortcuts_current_folder_is_volume = TRUE;
1088         }
1089       else
1090         {
1091           gtk_file_system_volume_free (impl->file_system, volume);
1092           success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1093           impl->shortcuts_current_folder_is_volume = FALSE;
1094         }
1095
1096       gtk_file_path_free (base_path);
1097
1098       if (!success)
1099         shortcuts_remove_rows (impl, pos - 1, 1, NULL); /* remove the separator */
1100
1101       impl->shortcuts_current_folder_active = success;
1102     }
1103
1104   if (success)
1105     gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1106 }
1107
1108 /* Used from shortcuts_remove_rows() in shortcuts_update_current_folder() */
1109 static void
1110 remove_current_folder_cb (GtkFileChooserDefault *impl,
1111                           gpointer               data)
1112 {
1113   if (impl->shortcuts_current_folder_is_volume)
1114     gtk_file_system_volume_free (impl->file_system, data);
1115   else
1116     gtk_file_path_free (data);
1117 }
1118
1119 /* Updates the current folder row in the shortcuts model */
1120 static void
1121 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1122 {
1123   int pos;
1124
1125   pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1126
1127   if (impl->shortcuts_current_folder_active)
1128     {
1129       shortcuts_remove_rows (impl, pos, 2, remove_current_folder_cb);
1130       impl->shortcuts_current_folder_active = FALSE;
1131     }
1132
1133   shortcuts_add_current_folder (impl);
1134 }
1135
1136 /* Filter function used for the shortcuts filter model */
1137 static gboolean
1138 shortcuts_filter_cb (GtkTreeModel          *model,
1139                      GtkTreeIter           *iter,
1140                      gpointer               data)
1141 {
1142   GtkFileChooserDefault *impl;
1143   GtkTreePath *path;
1144   int pos;
1145
1146   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1147
1148   path = gtk_tree_model_get_path (model, iter);
1149   if (!path)
1150     return FALSE;
1151
1152   pos = *gtk_tree_path_get_indices (path);
1153   gtk_tree_path_free (path);
1154
1155   return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1156 }
1157
1158 /* Creates the list model for shortcuts */
1159 static void
1160 shortcuts_model_create (GtkFileChooserDefault *impl)
1161 {
1162   /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1163   impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1164                                               GDK_TYPE_PIXBUF,  /* pixbuf */
1165                                               G_TYPE_STRING,    /* name */
1166                                               G_TYPE_POINTER,   /* path or volume */
1167                                               G_TYPE_BOOLEAN,   /* removable */
1168                                               G_TYPE_BOOLEAN);  /* pixbuf cell visibility */
1169
1170   if (impl->file_system)
1171     {
1172       shortcuts_append_home (impl);
1173       shortcuts_append_desktop (impl);
1174       shortcuts_add_volumes (impl);
1175       shortcuts_add_bookmarks (impl);
1176     }
1177
1178   impl->shortcuts_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
1179   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1180                                           shortcuts_filter_cb,
1181                                           impl,
1182                                           NULL);
1183 }
1184
1185 /* Callback used when the "New Folder" toolbar button is clicked */
1186 static void
1187 new_folder_button_clicked (GtkButton             *button,
1188                            GtkFileChooserDefault *impl)
1189 {
1190   GtkTreeIter iter;
1191   GtkTreePath *path;
1192
1193   /* FIXME: this doesn't work for folder mode, just for file mode */
1194
1195   _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1196   g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1197
1198   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1199   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1200                             path,
1201                             impl->list_name_column,
1202                             TRUE);
1203 }
1204
1205 /* Callback used from the text cell renderer when the new folder is named */
1206 static void
1207 renderer_edited_cb (GtkCellRendererText   *cell_renderer_text,
1208                     const gchar           *path,
1209                     const gchar           *new_text,
1210                     GtkFileChooserDefault *impl)
1211 {
1212   GError *error;
1213   GtkFilePath *file_path;
1214
1215   _gtk_file_system_model_remove_editable (impl->browse_files_model);
1216   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1217
1218   error = NULL;
1219   file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
1220   if (!file_path)
1221     {
1222       error_building_filename_dialog (impl, impl->current_folder, new_text, error);
1223       return;
1224     }
1225
1226   error = NULL;
1227   if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
1228     error_dialog (impl,
1229                   _("Could not create folder %s:\n%s"),
1230                   file_path, error);
1231
1232   gtk_file_path_free (file_path);
1233
1234   /* FIXME: scroll to the new folder and select it */
1235 }
1236
1237 /* Callback used from the text cell renderer when the new folder edition gets
1238  * canceled.
1239  */
1240 static void
1241 renderer_editing_canceled_cb (GtkCellRendererText   *cell_renderer_text,
1242                               GtkFileChooserDefault *impl)
1243 {
1244   _gtk_file_system_model_remove_editable (impl->browse_files_model);
1245   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1246 }
1247
1248 /* Creates the widgets for the filter combo box */
1249 static GtkWidget *
1250 filter_create (GtkFileChooserDefault *impl)
1251 {
1252   impl->filter_combo = gtk_combo_box_new_text ();
1253   g_signal_connect (impl->filter_combo, "changed",
1254                     G_CALLBACK (filter_combo_changed), impl);
1255
1256   return impl->filter_combo;
1257 }
1258
1259 static GtkWidget *
1260 button_new (GtkFileChooserDefault *impl,
1261             const char *text,
1262             const char *stock_id,
1263             gboolean    sensitive,
1264             gboolean    show,
1265             GCallback   callback)
1266 {
1267   GtkWidget *button;
1268   GtkWidget *hbox;
1269   GtkWidget *widget;
1270   GtkWidget *align;
1271
1272   button = gtk_button_new ();
1273   hbox = gtk_hbox_new (FALSE, 2);
1274   align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1275
1276   gtk_container_add (GTK_CONTAINER (button), align);
1277   gtk_container_add (GTK_CONTAINER (align), hbox);
1278   widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1279
1280   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1281
1282   widget = gtk_label_new_with_mnemonic (text);
1283   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1284   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1285
1286   gtk_widget_set_sensitive (button, sensitive);
1287   g_signal_connect (button, "clicked", callback, impl);
1288
1289   gtk_widget_show_all (align);
1290
1291   if (show)
1292     gtk_widget_show (button);
1293
1294   return button;
1295 }
1296
1297 /* Creates the widgets for the folder tree */
1298 static GtkWidget *
1299 create_folder_tree (GtkFileChooserDefault *impl)
1300 {
1301   GtkTreeSelection *selection;
1302
1303   /* Scrolled window */
1304
1305   impl->browse_directories_swin = gtk_scrolled_window_new (NULL, NULL);
1306   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1307                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1308   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1309                                        GTK_SHADOW_IN);
1310   /* Tree */
1311
1312   impl->browse_directories_tree_view = gtk_tree_view_new ();
1313   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_directories_tree_view), FALSE);
1314
1315   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
1316   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1317                                           GDK_BUTTON1_MASK,
1318                                           shortcuts_targets,
1319                                           num_shortcuts_targets,
1320                                           GDK_ACTION_COPY);
1321
1322   g_signal_connect (selection, "changed",
1323                     G_CALLBACK (tree_selection_changed), impl);
1324
1325   gtk_container_add (GTK_CONTAINER (impl->browse_directories_swin), impl->browse_directories_tree_view);
1326   gtk_widget_show (impl->browse_directories_tree_view);
1327
1328   /* Column */
1329
1330   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->browse_directories_tree_view), 0,
1331                                               _("Name"),
1332                                               gtk_cell_renderer_text_new (),
1333                                               tree_name_data_func, impl, NULL);
1334   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1335                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1336
1337   return impl->browse_directories_swin;
1338 }
1339
1340 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1341 static int
1342 shortcut_find_position (GtkFileChooserDefault *impl,
1343                         const GtkFilePath     *path)
1344 {
1345   GtkTreeIter iter;
1346   int i;
1347   int bookmarks_separator_idx;
1348   int current_folder_separator_idx;
1349   int volumes_idx;
1350
1351   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1352     return -1;
1353
1354   bookmarks_separator_idx = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1355   current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1356   volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1357
1358   i = 0;
1359
1360   for (i = 0; i < current_folder_separator_idx; i++)
1361     {
1362       gpointer data;
1363
1364       if (i == bookmarks_separator_idx)
1365         goto next_iter;
1366
1367       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1368
1369       if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1370         {
1371           GtkFileSystemVolume *volume;
1372           GtkFilePath *base_path;
1373           gboolean exists;
1374
1375           volume = data;
1376           base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1377
1378           exists = strcmp (gtk_file_path_get_string (path),
1379                            gtk_file_path_get_string (base_path)) == 0;
1380           g_free (base_path);
1381
1382           if (exists)
1383             return i;
1384         }
1385       else
1386         {
1387           GtkFilePath *model_path;
1388
1389           model_path = data;
1390
1391           if (model_path && gtk_file_path_compare (model_path, path) == 0)
1392             return i;
1393         }
1394
1395     next_iter:
1396       gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1397     }
1398
1399   return -1;
1400 }
1401
1402 /* Tries to add a bookmark from a path name */
1403 static void
1404 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1405                                   const GtkFilePath     *path)
1406 {
1407   GtkFileInfo *info;
1408   GError *error;
1409
1410   if (shortcut_find_position (impl, path) != -1)
1411     return;
1412
1413   error = NULL;
1414   info = get_file_info (impl->file_system, path, &error);
1415
1416   if (!info)
1417     error_getting_info_dialog (impl, path, error);
1418   else if (!gtk_file_info_get_is_folder (info))
1419     {
1420       char *msg;
1421
1422       msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1423                              gtk_file_path_get_string (path));
1424       error_message (impl, msg);
1425       g_free (msg);
1426     }
1427   else
1428     {
1429       error = NULL;
1430       if (!gtk_file_system_insert_bookmark (impl->file_system, path, -1, &error))
1431         error_could_not_add_bookmark_dialog (impl, path, error);
1432     }
1433 }
1434
1435 static void
1436 add_bookmark_foreach_cb (GtkTreeModel *model,
1437                          GtkTreePath  *path,
1438                          GtkTreeIter  *iter,
1439                          gpointer      data)
1440 {
1441   GtkFileChooserDefault *impl;
1442   GtkFileSystemModel *fs_model;
1443   GtkTreeIter child_iter;
1444   const GtkFilePath *file_path;
1445
1446   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1447
1448   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1449       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1450     {
1451       fs_model = impl->browse_directories_model;
1452       child_iter = *iter;
1453     }
1454   else
1455     {
1456       fs_model = impl->browse_files_model;
1457       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1458     }
1459
1460   file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &child_iter);
1461   shortcuts_add_bookmark_from_path (impl, file_path);
1462 }
1463
1464 /* Callback used when the "Add bookmark" button is clicked */
1465 static void
1466 add_bookmark_button_clicked_cb (GtkButton *button,
1467                                 GtkFileChooserDefault *impl)
1468 {
1469   GtkWidget *tree_view;
1470   GtkTreeSelection *selection;
1471
1472   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1473       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1474     tree_view = impl->browse_directories_tree_view;
1475   else
1476     tree_view = impl->browse_files_tree_view;
1477
1478   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1479   if (gtk_tree_selection_count_selected_rows (selection) == 0)
1480     shortcuts_add_bookmark_from_path (impl, impl->current_folder);
1481   else
1482     gtk_tree_selection_selected_foreach (selection,
1483                                          add_bookmark_foreach_cb,
1484                                          impl);
1485 }
1486
1487 /* Callback used when the "Remove bookmark" button is clicked */
1488 static void
1489 remove_bookmark_button_clicked_cb (GtkButton *button,
1490                                    GtkFileChooserDefault *impl)
1491 {
1492   GtkTreeSelection *selection;
1493   GtkTreeIter iter;
1494   GtkFilePath *path;
1495   gboolean removable;
1496   GError *error;
1497
1498   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1499
1500   if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1501     {
1502       gtk_tree_model_get (impl->shortcuts_filter_model, &iter,
1503                           SHORTCUTS_COL_PATH, &path,
1504                           SHORTCUTS_COL_REMOVABLE, &removable, -1);
1505       if (!removable)
1506         {
1507           g_assert_not_reached ();
1508           return;
1509         }
1510
1511       error = NULL;
1512       if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1513         error_dialog (impl,
1514                       _("Could not remove bookmark for %s:\n%s"),
1515                       path,
1516                       error);
1517     }
1518 }
1519
1520 struct is_folders_foreach_closure {
1521   GtkFileChooserDefault *impl;
1522   gboolean all_folders;
1523 };
1524
1525 /* Used from gtk_tree_selection_selected_foreach() */
1526 static void
1527 is_folders_foreach_cb (GtkTreeModel *model,
1528                        GtkTreePath  *path,
1529                        GtkTreeIter  *iter,
1530                        gpointer      data)
1531 {
1532   struct is_folders_foreach_closure *closure;
1533   GtkTreeIter child_iter;
1534   const GtkFileInfo *info;
1535
1536   closure = data;
1537
1538   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1539
1540   info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1541   closure->all_folders &= gtk_file_info_get_is_folder (info);
1542 }
1543
1544 /* Returns whether the selected items in the file list are all folders */
1545 static gboolean
1546 selection_is_folders (GtkFileChooserDefault *impl)
1547 {
1548   struct is_folders_foreach_closure closure;
1549   GtkTreeSelection *selection;
1550
1551   closure.impl = impl;
1552   closure.all_folders = TRUE;
1553
1554   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1555   gtk_tree_selection_selected_foreach (selection,
1556                                        is_folders_foreach_cb,
1557                                        &closure);
1558
1559   return closure.all_folders;
1560 }
1561
1562 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1563  * if there are no selected items *and* the current folder is not in the
1564  * bookmarks list.  De-sensitize the button otherwise.
1565  */
1566 static void
1567 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1568 {
1569   GtkWidget *tree_view;
1570   GtkTreeSelection *selection;
1571   gboolean active;
1572
1573   /* Check selection */
1574
1575   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1576       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1577     tree_view = impl->browse_directories_tree_view;
1578   else
1579     tree_view = impl->browse_files_tree_view;
1580
1581   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1582
1583   if (gtk_tree_selection_count_selected_rows (selection) == 0)
1584     active = (shortcut_find_position (impl, impl->current_folder) == -1);
1585   else
1586     active = (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1587               impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER ||
1588               selection_is_folders (impl));
1589
1590   gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
1591 }
1592
1593 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1594  * bookmark row is selected in the shortcuts tree.
1595  */
1596 static void
1597 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1598 {
1599   GtkTreeSelection *selection;
1600   GtkTreeIter iter;
1601   gboolean removable = FALSE;
1602
1603   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1604
1605   if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1606     gtk_tree_model_get (impl->shortcuts_filter_model, &iter,
1607                         SHORTCUTS_COL_REMOVABLE, &removable,
1608                         -1);
1609
1610   gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
1611 }
1612
1613 /* Converts raw selection data from text/uri-list to a list of strings */
1614 static GSList *
1615 split_uris (const char *data)
1616 {
1617   GSList *uris;
1618   const char *p, *start;
1619
1620   uris = NULL;
1621
1622   start = data;
1623
1624   for (p = start; *p != 0; p++)
1625     if (*p == '\r' && *(p + 1) == '\n')
1626       {
1627         char *name;
1628
1629         name = g_strndup (start, p - start);
1630         uris = g_slist_prepend (uris, name);
1631
1632         start = p + 2;
1633         p = start;
1634       }
1635
1636   uris = g_slist_reverse (uris);
1637   return uris;
1638 }
1639
1640 /* Callback used when we get the drag data for the bookmarks list.  We add the
1641  * received URIs as bookmarks if they are folders.
1642  */
1643 static void
1644 shortcuts_drag_data_received_cb (GtkWidget          *widget,
1645                                  GdkDragContext     *context,
1646                                  gint                x,
1647                                  gint                y,
1648                                  GtkSelectionData   *selection_data,
1649                                  guint               info,
1650                                  guint               time_,
1651                                  gpointer            data)
1652 {
1653   GtkFileChooserDefault *impl;
1654   GSList *uris, *l;
1655
1656   impl = GTK_FILE_CHOOSER_DEFAULT (data);
1657
1658   uris = split_uris (selection_data->data);
1659
1660   for (l = uris; l; l = l->next)
1661     {
1662       char *uri;
1663       GtkFilePath *path;
1664
1665       uri = l->data;
1666       path = gtk_file_system_uri_to_path (impl->file_system, uri);
1667
1668       if (path)
1669         {
1670           shortcuts_add_bookmark_from_path (impl, path);
1671           gtk_file_path_free (path);
1672         }
1673       else
1674         {
1675           char *msg;
1676
1677           msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
1678                                  uri);
1679           error_message (impl, msg);
1680           g_free (msg);
1681         }
1682
1683       g_free (uri);
1684     }
1685
1686   g_slist_free (uris);
1687 }
1688
1689 /* Callback used when the selection in the shortcuts tree changes */
1690 static void
1691 shortcuts_selection_changed_cb (GtkTreeSelection      *selection,
1692                                 GtkFileChooserDefault *impl)
1693 {
1694   bookmarks_check_remove_sensitivity (impl);
1695 }
1696
1697 /* Creates the widgets for the shortcuts and bookmarks tree */
1698 static GtkWidget *
1699 shortcuts_list_create (GtkFileChooserDefault *impl)
1700 {
1701   GtkTreeSelection *selection;
1702   GtkTreeViewColumn *column;
1703   GtkCellRenderer *renderer;
1704
1705   /* Scrolled window */
1706
1707   impl->browse_shortcuts_swin = gtk_scrolled_window_new (NULL, NULL);
1708   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1709                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1710   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1711                                        GTK_SHADOW_IN);
1712   gtk_widget_show (impl->browse_shortcuts_swin);
1713
1714   /* Tree */
1715
1716   impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
1717   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
1718
1719   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
1720
1721   gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
1722                      GTK_DEST_DEFAULT_ALL,
1723                      shortcuts_targets,
1724                      num_shortcuts_targets,
1725                      GDK_ACTION_COPY);
1726
1727   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1728   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1729   gtk_tree_selection_set_select_function (selection,
1730                                           shortcuts_select_func,
1731                                           impl, NULL);
1732
1733   g_signal_connect (selection, "changed",
1734                     G_CALLBACK (shortcuts_selection_changed_cb), impl);
1735
1736   g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
1737                     G_CALLBACK (shortcuts_row_activated_cb), impl);
1738
1739   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
1740                     G_CALLBACK (shortcuts_drag_data_received_cb), impl);
1741
1742   gtk_container_add (GTK_CONTAINER (impl->browse_shortcuts_swin), impl->browse_shortcuts_tree_view);
1743   gtk_widget_show (impl->browse_shortcuts_tree_view);
1744
1745   /* Column */
1746
1747   column = gtk_tree_view_column_new ();
1748   gtk_tree_view_column_set_title (column, _("Folder"));
1749
1750   renderer = gtk_cell_renderer_pixbuf_new ();
1751   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1752   gtk_tree_view_column_set_attributes (column, renderer,
1753                                        "pixbuf", SHORTCUTS_COL_PIXBUF,
1754                                        "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
1755                                        NULL);
1756
1757   renderer = _gtk_cell_renderer_sep_text_new ();
1758   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1759   gtk_tree_view_column_set_attributes (column, renderer,
1760                                        "text", SHORTCUTS_COL_NAME,
1761                                        NULL);
1762
1763   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
1764
1765   return impl->browse_shortcuts_swin;
1766 }
1767
1768 /* Creates the widgets for the shortcuts/bookmarks pane */
1769 static GtkWidget *
1770 shortcuts_pane_create (GtkFileChooserDefault *impl,
1771                        GtkSizeGroup          *size_group)
1772 {
1773   GtkWidget *vbox;
1774   GtkWidget *hbox;
1775   GtkWidget *widget;
1776
1777   vbox = gtk_vbox_new (FALSE, 6);
1778   gtk_widget_show (vbox);
1779
1780   /* Shortcuts tree */
1781
1782   widget = shortcuts_list_create (impl);
1783   gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1784
1785   /* Box for buttons */
1786
1787   hbox = gtk_hbox_new (TRUE, 6);
1788   gtk_size_group_add_widget (size_group, hbox);
1789   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1790   gtk_widget_show (hbox);
1791
1792   /* Add bookmark button */
1793
1794   impl->browse_shortcuts_add_button = button_new (impl,
1795                                                   _("_Add"),
1796                                                   GTK_STOCK_ADD,
1797                                                   FALSE,
1798                                                   TRUE,
1799                                                   G_CALLBACK (add_bookmark_button_clicked_cb));
1800   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
1801
1802   /* Remove bookmark button */
1803
1804   impl->browse_shortcuts_remove_button = button_new (impl,
1805                                                      _("_Remove"),
1806                                                      GTK_STOCK_REMOVE,
1807                                                      FALSE,
1808                                                      TRUE,
1809                                                      G_CALLBACK (remove_bookmark_button_clicked_cb));
1810   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
1811
1812   return vbox;
1813 }
1814
1815 /* Creates the widgets for the file list */
1816 static GtkWidget *
1817 create_file_list (GtkFileChooserDefault *impl)
1818 {
1819   GtkTreeSelection *selection;
1820   GtkTreeViewColumn *column;
1821   GtkCellRenderer *renderer;
1822
1823   /* Scrolled window */
1824
1825   impl->browse_files_swin = gtk_scrolled_window_new (NULL, NULL);
1826   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1827                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1828   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1829                                        GTK_SHADOW_IN);
1830
1831   /* Tree/list view */
1832
1833   impl->browse_files_tree_view = gtk_tree_view_new ();
1834   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
1835   gtk_container_add (GTK_CONTAINER (impl->browse_files_swin), impl->browse_files_tree_view);
1836   g_signal_connect (impl->browse_files_tree_view, "row_activated",
1837                     G_CALLBACK (list_row_activated), impl);
1838   gtk_widget_show (impl->browse_files_tree_view);
1839
1840   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1841   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
1842                                           GDK_BUTTON1_MASK,
1843                                           shortcuts_targets,
1844                                           num_shortcuts_targets,
1845                                           GDK_ACTION_COPY);
1846
1847   g_signal_connect (selection, "changed",
1848                     G_CALLBACK (list_selection_changed), impl);
1849
1850   /* Filename column */
1851
1852   impl->list_name_column = gtk_tree_view_column_new ();
1853   gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
1854   gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
1855   gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
1856
1857   renderer = gtk_cell_renderer_pixbuf_new ();
1858   gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
1859   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
1860                                            list_icon_data_func, impl, NULL);
1861
1862   impl->list_name_renderer = gtk_cell_renderer_text_new ();
1863   g_signal_connect (impl->list_name_renderer, "edited",
1864                     G_CALLBACK (renderer_edited_cb), impl);
1865   g_signal_connect (impl->list_name_renderer, "editing-canceled",
1866                     G_CALLBACK (renderer_editing_canceled_cb), impl);
1867   gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
1868   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
1869                                            list_name_data_func, impl, NULL);
1870
1871   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
1872 #if 0
1873   /* Size column */
1874
1875   column = gtk_tree_view_column_new ();
1876   gtk_tree_view_column_set_title (column, _("Size"));
1877
1878   renderer = gtk_cell_renderer_text_new ();
1879   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1880   gtk_tree_view_column_set_cell_data_func (column, renderer,
1881                                            list_size_data_func, impl, NULL);
1882   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
1883   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1884 #endif
1885   /* Modification time column */
1886
1887   column = gtk_tree_view_column_new ();
1888   gtk_tree_view_column_set_title (column, _("Modified"));
1889
1890   renderer = gtk_cell_renderer_text_new ();
1891   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1892   gtk_tree_view_column_set_cell_data_func (column, renderer,
1893                                            list_mtime_data_func, impl, NULL);
1894   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
1895   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1896
1897   return impl->browse_files_swin;
1898 }
1899
1900 static GtkWidget *
1901 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
1902 {
1903   GtkWidget *hbox;
1904   GtkWidget *widget;
1905
1906   hbox = gtk_hbox_new (FALSE, 12);
1907   gtk_widget_show (hbox);
1908
1909   /* Filter combo */
1910
1911   widget = filter_create (impl);
1912   gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1913
1914   return hbox;
1915 }
1916
1917 /* Creates the widgets for the files/folders pane */
1918 static GtkWidget *
1919 file_pane_create (GtkFileChooserDefault *impl,
1920                   GtkSizeGroup          *size_group)
1921 {
1922   GtkWidget *vbox;
1923   GtkWidget *hbox;
1924   GtkWidget *widget;
1925
1926   vbox = gtk_vbox_new (FALSE, 6);
1927   gtk_widget_show (vbox);
1928
1929   /* The path bar and 'Create Folder' button */
1930   hbox = gtk_hbox_new (FALSE, 12);
1931   gtk_widget_show (hbox);
1932   impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
1933   g_signal_connect (impl->browse_path_bar, "path_clicked", G_CALLBACK (path_bar_clicked), impl);
1934   gtk_widget_show_all (impl->browse_path_bar);
1935   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
1936
1937   /* Create Folder */
1938   impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create _Folder"));
1939   g_signal_connect (impl->browse_new_folder_button, "clicked",
1940                     G_CALLBACK (new_folder_button_clicked), impl);
1941   gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
1942   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1943
1944   /* Box for lists and preview */
1945
1946   hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
1947   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
1948   gtk_widget_show (hbox);
1949
1950   /* Folder tree */
1951
1952   widget = create_folder_tree (impl);
1953   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1954
1955   /* File list */
1956
1957   widget = create_file_list (impl);
1958   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1959
1960   /* Preview */
1961
1962   impl->preview_box = gtk_vbox_new (FALSE, 12);
1963   gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
1964   /* Don't show preview box initially */
1965
1966   /* Filename entry and filter combo */
1967   hbox = gtk_hbox_new (FALSE, 0);
1968   gtk_size_group_add_widget (size_group, hbox);
1969   widget = create_filename_entry_and_filter_combo (impl);
1970   gtk_box_pack_end (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1971   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1972   gtk_widget_show (hbox);
1973
1974   return vbox;
1975 }
1976 /* Callback used when the "Browse for more folders" expander is toggled */
1977 static void
1978 expander_changed_cb (GtkExpander           *expander,
1979                      GParamSpec            *pspec,
1980                      GtkFileChooserDefault *impl)
1981 {
1982   update_appearance (impl);
1983 }
1984
1985 /* Callback used when the selection changes in the save folder combo box */
1986 static void
1987 save_folder_combo_changed_cb (GtkComboBox           *combo,
1988                               GtkFileChooserDefault *impl)
1989 {
1990   int active;
1991
1992   active = gtk_combo_box_get_active (combo);
1993   if (active == -1)
1994     return;
1995
1996   shortcuts_activate_item (impl, active);
1997 }
1998
1999 /* Creates the combo box with the save folders */
2000 static GtkWidget *
2001 save_folder_combo_create (GtkFileChooserDefault *impl)
2002 {
2003   GtkWidget *combo;
2004   GtkCellRenderer *cell;
2005
2006   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
2007   gtk_widget_show (combo);
2008
2009   cell = gtk_cell_renderer_pixbuf_new ();
2010   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
2011   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
2012                                   "pixbuf", SHORTCUTS_COL_PIXBUF,
2013                                   "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2014                                   NULL);
2015
2016   cell = _gtk_cell_renderer_sep_text_new ();
2017   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
2018   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
2019                                   "text", SHORTCUTS_COL_NAME,
2020                                   NULL);
2021
2022   g_signal_connect (combo, "changed",
2023                     G_CALLBACK (save_folder_combo_changed_cb), impl);
2024
2025   return combo;
2026 }
2027
2028 /* Creates the widgets specific to Save mode */
2029 static GtkWidget *
2030 save_widgets_create (GtkFileChooserDefault *impl)
2031 {
2032   GtkWidget *vbox;
2033   GtkWidget *table;
2034   GtkWidget *widget;
2035   GtkWidget *alignment;
2036
2037   vbox = gtk_vbox_new (FALSE, 12);
2038
2039   table = gtk_table_new (2, 2, FALSE);
2040   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
2041   gtk_widget_show (table);
2042   gtk_table_set_row_spacings (GTK_TABLE (table), 12);
2043   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2044
2045   /* Name entry */
2046
2047   widget = gtk_label_new_with_mnemonic (_("_Name:"));
2048   gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
2049   gtk_table_attach (GTK_TABLE (table), widget,
2050                     0, 1, 0, 1,
2051                     GTK_FILL, GTK_FILL,
2052                     0, 0);
2053   gtk_widget_show (widget);
2054
2055   impl->save_file_name_entry = gtk_entry_new ();
2056   gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
2057   gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
2058   gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
2059                     1, 2, 0, 1,
2060                     GTK_EXPAND | GTK_FILL, 0,
2061                     0, 0);
2062   gtk_widget_show (impl->save_file_name_entry);
2063   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
2064
2065   /* Folder combo */
2066   impl->save_folder_label = gtk_label_new_with_mnemonic (_("Save in _Folder:"));
2067   gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
2068   gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
2069                     0, 1, 1, 2,
2070                     GTK_FILL, GTK_FILL,
2071                     0, 0);
2072   gtk_widget_show (impl->save_folder_label);
2073
2074   impl->save_folder_combo = save_folder_combo_create (impl);
2075   gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
2076                     1, 2, 1, 2,
2077                     GTK_EXPAND | GTK_FILL, GTK_FILL,
2078                     0, 0);
2079   gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
2080
2081   /* custom widget */
2082   impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
2083   gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
2084
2085   /* Expander */
2086   alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
2087   gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
2088
2089   impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
2090   gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
2091   g_signal_connect (impl->save_expander, "notify::expanded",
2092                     G_CALLBACK (expander_changed_cb),
2093                     impl);
2094   gtk_widget_show_all (alignment);
2095
2096   return vbox;
2097 }
2098
2099 /* Creates the main hpaned with the widgets shared by Open and Save mode */
2100 static GtkWidget *
2101 browse_widgets_create (GtkFileChooserDefault *impl)
2102 {
2103   GtkWidget *vbox;
2104   GtkWidget *hpaned;
2105   GtkWidget *widget;
2106   GtkSizeGroup *size_group;
2107
2108   /* size group is used by the [+][-] buttons and the filter combo */
2109   size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
2110   vbox = gtk_vbox_new (FALSE, 12);
2111
2112   /* Paned widget */
2113   hpaned = gtk_hpaned_new ();
2114   gtk_widget_show (hpaned);
2115   gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
2116   gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
2117
2118   widget = shortcuts_pane_create (impl, size_group);
2119   gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
2120   widget = file_pane_create (impl, size_group);
2121   gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
2122
2123   /* Alignment to hold custom widget */
2124   impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
2125   gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
2126
2127   return vbox;
2128 }
2129
2130 static GObject*
2131 gtk_file_chooser_default_constructor (GType                  type,
2132                                       guint                  n_construct_properties,
2133                                       GObjectConstructParam *construct_params)
2134 {
2135   GtkFileChooserDefault *impl;
2136   GObject *object;
2137
2138   object = parent_class->constructor (type,
2139                                       n_construct_properties,
2140                                       construct_params);
2141   impl = GTK_FILE_CHOOSER_DEFAULT (object);
2142
2143   g_assert (impl->file_system);
2144
2145   gtk_widget_push_composite_child ();
2146
2147   /* Shortcuts model */
2148
2149   shortcuts_model_create (impl);
2150
2151   /* Widgets for Save mode */
2152   impl->save_widgets = save_widgets_create (impl);
2153   gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
2154
2155   /* The browse widgets */
2156   impl->browse_widgets = browse_widgets_create (impl);
2157   gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
2158
2159   gtk_widget_pop_composite_child ();
2160   update_appearance (impl);
2161
2162   return object;
2163 }
2164
2165 /* Sets the extra_widget by packing it in the appropriate place */
2166 static void
2167 set_extra_widget (GtkFileChooserDefault *impl,
2168                   GtkWidget             *extra_widget)
2169 {
2170   if (extra_widget)
2171     {
2172       g_object_ref (extra_widget);
2173       /* FIXME: is this right ? */
2174       gtk_widget_show (extra_widget);
2175     }
2176
2177   if (impl->extra_widget)
2178     g_object_unref (impl->extra_widget);
2179
2180   impl->extra_widget = extra_widget;
2181 }
2182
2183 static void
2184 volumes_changed_cb (GtkFileSystem         *file_system,
2185                     GtkFileChooserDefault *impl)
2186 {
2187   shortcuts_add_volumes (impl);
2188 }
2189
2190 /* Callback used when the set of bookmarks changes in the file system */
2191 static void
2192 bookmarks_changed_cb (GtkFileSystem         *file_system,
2193                       GtkFileChooserDefault *impl)
2194 {
2195   shortcuts_add_bookmarks (impl);
2196
2197   bookmarks_check_add_sensitivity (impl);
2198   bookmarks_check_remove_sensitivity (impl);
2199 }
2200
2201 /* Sets the file chooser to multiple selection mode */
2202 static void
2203 set_select_multiple (GtkFileChooserDefault *impl,
2204                      gboolean               select_multiple,
2205                      gboolean               property_notify)
2206 {
2207   GtkTreeSelection *selection;
2208   GtkSelectionMode mode;
2209
2210   if (select_multiple == impl->select_multiple)
2211     return;
2212
2213   mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
2214
2215   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
2216   gtk_tree_selection_set_mode (selection, mode);
2217
2218   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2219   gtk_tree_selection_set_mode (selection, mode);
2220
2221   impl->select_multiple = select_multiple;
2222   g_object_notify (G_OBJECT (impl), "select-multiple");
2223
2224   /* FIXME #132255: See note in check_preview_change() */
2225   check_preview_change (impl);
2226 }
2227
2228 static void
2229 set_file_system_backend (GtkFileChooserDefault *impl,
2230                          const char *backend)
2231 {
2232   if (impl->file_system)
2233     {
2234       g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
2235       impl->volumes_changed_id = 0;
2236       g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
2237       impl->bookmarks_changed_id = 0;
2238       g_object_unref (impl->file_system);
2239     }
2240
2241   impl->file_system = NULL;
2242   if (backend)
2243     impl->file_system = _gtk_file_system_create (backend);
2244   else
2245     {
2246       GtkSettings *settings = gtk_settings_get_default ();
2247       gchar *default_backend = NULL;
2248
2249       g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
2250       if (default_backend)
2251         {
2252           impl->file_system = _gtk_file_system_create (default_backend);
2253           g_free (default_backend);
2254         }
2255     }
2256
2257   if (!impl->file_system)
2258     {
2259 #if defined (G_OS_UNIX)
2260       impl->file_system = gtk_file_system_unix_new ();
2261 #elif defined (G_OS_WIN32)
2262       impl->file_system = gtk_file_system_win32_new ();
2263 #else
2264 #error "No default filesystem implementation on the platform"
2265 #endif
2266     }
2267
2268   if (impl->file_system)
2269     {
2270       impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
2271                                                    G_CALLBACK (volumes_changed_cb),
2272                                                    impl);
2273       impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
2274                                                      G_CALLBACK (bookmarks_changed_cb),
2275                                                      impl);
2276     }
2277 }
2278
2279 /* This function is basically a do_all function.
2280  *
2281  * It sets the visibility on all the widgets based on the current state, and
2282  * moves the custom_widget if needed.
2283  */
2284 static void
2285 update_appearance (GtkFileChooserDefault *impl)
2286 {
2287   GtkWidget *child;
2288
2289   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
2290       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2291     {
2292       gtk_widget_show (impl->save_widgets);
2293
2294       if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
2295         {
2296           gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
2297           gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
2298           gtk_widget_show (impl->browse_widgets);
2299         }
2300       else
2301         {
2302           gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
2303           gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
2304           gtk_widget_hide (impl->browse_widgets);
2305         }
2306
2307       gtk_widget_show (impl->browse_new_folder_button);
2308
2309       if (impl->select_multiple)
2310         {
2311           g_warning ("Save mode cannot be set in conjunction with multiple selection mode.  "
2312                      "Re-setting to single selection mode.");
2313           set_select_multiple (impl, FALSE, TRUE);
2314         }
2315     }
2316   else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2317            impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
2318     {
2319       gtk_widget_hide (impl->save_widgets);
2320       gtk_widget_show (impl->browse_widgets);
2321     }
2322   /* FIXME: */
2323   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2324       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2325     {
2326       gtk_widget_show (impl->browse_new_folder_button);
2327       gtk_widget_hide (impl->browse_files_swin);
2328       gtk_widget_show (impl->browse_directories_swin);
2329     }
2330   else
2331     {
2332       gtk_widget_hide (impl->browse_new_folder_button);
2333       gtk_widget_hide (impl->browse_directories_swin);
2334       gtk_widget_show (impl->browse_files_swin);
2335     }
2336
2337   if (impl->extra_widget)
2338     {
2339       GtkWidget *align;
2340       GtkWidget *unused_align;
2341
2342       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2343         {
2344           align = impl->save_extra_align;
2345           unused_align = impl->browse_extra_align;
2346         }
2347       else
2348         {
2349           align = impl->browse_extra_align;
2350           unused_align = impl->save_extra_align;
2351         }
2352
2353       /* We own a ref on extra_widget, so it's safe to do this */
2354       child = GTK_BIN (unused_align)->child;
2355       if (child)
2356         gtk_container_remove (GTK_CONTAINER (unused_align), child);
2357
2358       child = GTK_BIN (align)->child;
2359       if (child && child != impl->extra_widget)
2360         {
2361           gtk_container_remove (GTK_CONTAINER (align), child);
2362           gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2363         }
2364       else if (child == NULL)
2365         {
2366           gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2367         }
2368
2369       gtk_widget_show (align);
2370       gtk_widget_hide (unused_align);
2371     }
2372   else
2373     {
2374       child = GTK_BIN (impl->browse_extra_align)->child;
2375       if (child)
2376         gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
2377
2378       child = GTK_BIN (impl->save_extra_align)->child;
2379       if (child)
2380         gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
2381
2382       gtk_widget_hide (impl->save_extra_align);
2383       gtk_widget_hide (impl->browse_extra_align);
2384     }
2385
2386   g_signal_emit_by_name (impl, "default-size-changed");
2387   g_signal_emit_by_name (impl, "resizable-hints-changed");
2388 }
2389
2390 static void
2391 gtk_file_chooser_default_set_property (GObject      *object,
2392                                        guint         prop_id,
2393                                        const GValue *value,
2394                                        GParamSpec   *pspec)
2395
2396 {
2397   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2398
2399   switch (prop_id)
2400     {
2401     case GTK_FILE_CHOOSER_PROP_ACTION:
2402       {
2403         GtkFileChooserAction action = g_value_get_enum (value);
2404
2405         if (action != impl->action)
2406           {
2407             if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
2408               {
2409                 g_warning ("Multiple selection mode is not allowed in Save mode");
2410                 set_select_multiple (impl, FALSE, TRUE);
2411               }
2412             impl->action = action;
2413             update_appearance (impl);
2414           }
2415       }
2416       break;
2417     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
2418       set_file_system_backend (impl, g_value_get_string (value));
2419       break;
2420     case GTK_FILE_CHOOSER_PROP_FILTER:
2421       set_current_filter (impl, g_value_get_object (value));
2422       break;
2423     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2424       impl->local_only = g_value_get_boolean (value);
2425       break;
2426     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2427       set_preview_widget (impl, g_value_get_object (value));
2428       break;
2429     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2430       impl->preview_widget_active = g_value_get_boolean (value);
2431       update_preview_widget_visibility (impl);
2432       break;
2433     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
2434       impl->use_preview_label = g_value_get_boolean (value);
2435       update_preview_widget_visibility (impl);
2436       break;
2437     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2438       set_extra_widget (impl, g_value_get_object (value));
2439       update_appearance (impl);
2440       break;
2441     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2442       {
2443         gboolean select_multiple = g_value_get_boolean (value);
2444         if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
2445           {
2446             g_warning ("Multiple selection mode is not allowed in Save mode");
2447             return;
2448           }
2449
2450         set_select_multiple (impl, select_multiple, FALSE);
2451       }
2452       break;
2453     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2454       {
2455         gboolean show_hidden = g_value_get_boolean (value);
2456         if (show_hidden != impl->show_hidden)
2457           {
2458             impl->show_hidden = show_hidden;
2459             _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2460                                                     show_hidden);
2461             _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_files_model),
2462                                                     show_hidden);
2463           }
2464       }
2465       break;
2466     default:
2467       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2468       break;
2469     }
2470 }
2471
2472 static void
2473 gtk_file_chooser_default_get_property (GObject    *object,
2474                                        guint       prop_id,
2475                                        GValue     *value,
2476                                        GParamSpec *pspec)
2477 {
2478   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2479
2480   switch (prop_id)
2481     {
2482     case GTK_FILE_CHOOSER_PROP_ACTION:
2483       g_value_set_enum (value, impl->action);
2484       break;
2485     case GTK_FILE_CHOOSER_PROP_FILTER:
2486       g_value_set_object (value, impl->current_filter);
2487       break;
2488     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2489       g_value_set_boolean (value, impl->local_only);
2490       break;
2491     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2492       g_value_set_object (value, impl->preview_widget);
2493       break;
2494     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2495       g_value_set_boolean (value, impl->preview_widget_active);
2496       break;
2497     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
2498       g_value_set_boolean (value, impl->use_preview_label);
2499       break;
2500     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2501       g_value_set_object (value, impl->extra_widget);
2502       break;
2503     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2504       g_value_set_boolean (value, impl->select_multiple);
2505       break;
2506     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2507       g_value_set_boolean (value, impl->show_hidden);
2508       break;
2509     default:
2510       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2511       break;
2512     }
2513 }
2514
2515
2516 static void
2517 gtk_file_chooser_default_dispose (GObject *object)
2518 {
2519   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
2520
2521   if (impl->extra_widget)
2522     {
2523       g_object_unref (impl->extra_widget);
2524       impl->extra_widget = NULL;
2525     }
2526   G_OBJECT_CLASS (parent_class)->dispose (object);
2527 }
2528
2529 /* We override show-all since we have internal widgets that
2530  * shouldn't be shown when you call show_all(), like the filter
2531  * combo box.
2532  */
2533 static void
2534 gtk_file_chooser_default_show_all (GtkWidget *widget)
2535 {
2536   gtk_widget_show (widget);
2537 }
2538
2539 static void
2540 gtk_file_chooser_default_style_set      (GtkWidget *widget,
2541                                          GtkStyle  *previous_style)
2542 {
2543   if (GTK_WIDGET_CLASS (parent_class)->style_set)
2544     GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
2545
2546   g_signal_emit_by_name (widget, "default-size-changed");
2547 }
2548
2549 static void
2550 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
2551                                          GdkScreen *previous_screen)
2552 {
2553   if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
2554     GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
2555
2556   g_signal_emit_by_name (widget, "default-size-changed");
2557 }
2558
2559 static void
2560 expand_and_select_func (GtkFileSystemModel *model,
2561                         GtkTreePath        *path,
2562                         GtkTreeIter        *iter,
2563                         gpointer            user_data)
2564 {
2565   GtkFileChooserDefault *impl = user_data;
2566   GtkTreeView *tree_view;
2567
2568   if (model == impl->browse_directories_model)
2569     tree_view = GTK_TREE_VIEW (impl->browse_directories_tree_view);
2570   else
2571     tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2572
2573   gtk_tree_view_expand_to_path (tree_view, path);
2574   gtk_tree_view_expand_row (tree_view, path, FALSE);
2575   gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
2576   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), path, NULL, TRUE, 0.3, 0.5);
2577 }
2578
2579 static gboolean
2580 list_model_filter_func (GtkFileSystemModel *model,
2581                         GtkFilePath        *path,
2582                         const GtkFileInfo  *file_info,
2583                         gpointer            user_data)
2584 {
2585   GtkFileChooserDefault *impl = user_data;
2586   GtkFileFilterInfo filter_info;
2587   GtkFileFilterFlags needed;
2588   gboolean result;
2589
2590   if (!impl->current_filter)
2591     return TRUE;
2592
2593   if (gtk_file_info_get_is_folder (file_info))
2594     return TRUE;
2595
2596   filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
2597
2598   needed = gtk_file_filter_get_needed (impl->current_filter);
2599
2600   filter_info.display_name = gtk_file_info_get_display_name (file_info);
2601   filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
2602
2603   if (needed & GTK_FILE_FILTER_FILENAME)
2604     {
2605       filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
2606       if (filter_info.filename)
2607         filter_info.contains |= GTK_FILE_FILTER_FILENAME;
2608     }
2609   else
2610     filter_info.filename = NULL;
2611
2612   if (needed & GTK_FILE_FILTER_URI)
2613     {
2614       filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
2615       if (filter_info.uri)
2616         filter_info.contains |= GTK_FILE_FILTER_URI;
2617     }
2618   else
2619     filter_info.uri = NULL;
2620
2621   result = gtk_file_filter_filter (impl->current_filter, &filter_info);
2622
2623   if (filter_info.filename)
2624     g_free ((gchar *)filter_info.filename);
2625   if (filter_info.uri)
2626     g_free ((gchar *)filter_info.uri);
2627
2628   return result;
2629 }
2630
2631 static void
2632 install_list_model_filter (GtkFileChooserDefault *impl)
2633 {
2634   if (impl->current_filter)
2635     _gtk_file_system_model_set_filter (impl->browse_files_model,
2636                                        list_model_filter_func,
2637                                        impl);
2638 }
2639
2640 #define COMPARE_DIRECTORIES                                                                                    \
2641   GtkFileChooserDefault *impl = user_data;                                                                     \
2642   const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a);                           \
2643   const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b);                           \
2644   gboolean dir_a, dir_b;                                                                                       \
2645                                                                                                                \
2646   if (info_a)                                                                                                  \
2647     dir_a = gtk_file_info_get_is_folder (info_a);                                                              \
2648   else                                                                                                         \
2649     return impl->list_sort_ascending ? -1 : 1;                                                                 \
2650                                                                                                                \
2651   if (info_b)                                                                                                  \
2652     dir_b = gtk_file_info_get_is_folder (info_b);                                                              \
2653   else                                                                                                         \
2654     return impl->list_sort_ascending ? 1 : -1;                                                                 \
2655                                                                                                                \
2656   if (dir_a != dir_b)                                                                                          \
2657     return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
2658
2659 /* Sort callback for the filename column */
2660 static gint
2661 name_sort_func (GtkTreeModel *model,
2662                 GtkTreeIter  *a,
2663                 GtkTreeIter  *b,
2664                 gpointer      user_data)
2665 {
2666   COMPARE_DIRECTORIES;
2667   else
2668     return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
2669 }
2670
2671 /* Sort callback for the size column */
2672 static gint
2673 size_sort_func (GtkTreeModel *model,
2674                 GtkTreeIter  *a,
2675                 GtkTreeIter  *b,
2676                 gpointer      user_data)
2677 {
2678   COMPARE_DIRECTORIES;
2679   else
2680     {
2681       gint64 size_a = gtk_file_info_get_size (info_a);
2682       gint64 size_b = gtk_file_info_get_size (info_b);
2683
2684       return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
2685     }
2686 }
2687
2688 /* Sort callback for the mtime column */
2689 static gint
2690 mtime_sort_func (GtkTreeModel *model,
2691                  GtkTreeIter  *a,
2692                  GtkTreeIter  *b,
2693                  gpointer      user_data)
2694 {
2695   COMPARE_DIRECTORIES;
2696   else
2697     {
2698       GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
2699       GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
2700
2701       return ta > tb ? -1 : (ta == tb ? 0 : 1);
2702     }
2703 }
2704
2705 /* Callback used when the sort column changes.  We cache the sort order for use
2706  * in name_sort_func().
2707  */
2708 static void
2709 list_sort_column_changed_cb (GtkTreeSortable       *sortable,
2710                              GtkFileChooserDefault *impl)
2711 {
2712   GtkSortType sort_type;
2713
2714   if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
2715     impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
2716 }
2717
2718 /* Gets rid of the old list model and creates a new one for the current folder */
2719 static void
2720 set_list_model (GtkFileChooserDefault *impl)
2721 {
2722   if (impl->browse_files_model)
2723     {
2724       g_object_unref (impl->browse_files_model);
2725       g_object_unref (impl->sort_model);
2726     }
2727
2728   impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
2729                                                  impl->current_folder, 0,
2730                                                  GTK_FILE_INFO_ALL);
2731   _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
2732   install_list_model_filter (impl);
2733
2734   impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
2735   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
2736   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
2737   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
2738   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
2739   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
2740   impl->list_sort_ascending = TRUE;
2741
2742   g_signal_connect (impl->sort_model, "sort_column_changed",
2743                     G_CALLBACK (list_sort_column_changed_cb), impl);
2744
2745   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
2746                            GTK_TREE_MODEL (impl->sort_model));
2747   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
2748   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
2749                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
2750 }
2751
2752 /* Gets rid of the old folder tree model and creates a new one for the volume
2753  * corresponding to the specified path.
2754  */
2755 static void
2756 set_tree_model (GtkFileChooserDefault *impl, const GtkFilePath *path)
2757 {
2758   GtkFileSystemVolume *volume;
2759   GtkFilePath *base_path, *parent_path;
2760
2761   base_path = NULL;
2762
2763   volume = gtk_file_system_get_volume_for_path (impl->file_system, path);
2764
2765   if (volume)
2766     base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2767
2768   if (base_path == NULL)
2769     {
2770       base_path = gtk_file_path_copy (path);
2771       while (gtk_file_system_get_parent (impl->file_system,
2772                                          base_path,
2773                                          &parent_path,
2774                                          NULL) &&
2775              parent_path != NULL)
2776         {
2777           gtk_file_path_free (base_path);
2778           base_path = parent_path;
2779         }
2780     }
2781
2782   if (impl->current_volume_path && gtk_file_path_compare (base_path, impl->current_volume_path) == 0)
2783     goto out;
2784
2785   if (impl->browse_directories_model)
2786     g_object_unref (impl->browse_directories_model);
2787
2788   impl->current_volume_path = gtk_file_path_copy (base_path);
2789
2790   impl->browse_directories_model = _gtk_file_system_model_new (impl->file_system, impl->current_volume_path, -1,
2791                                 GTK_FILE_INFO_DISPLAY_NAME);
2792   _gtk_file_system_model_set_show_files (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2793                                          FALSE);
2794   _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2795                                           impl->show_hidden);
2796
2797   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_directories_tree_view),
2798                            GTK_TREE_MODEL (impl->browse_directories_model));
2799
2800  out:
2801
2802   gtk_file_path_free (base_path);
2803   if (volume)
2804     gtk_file_system_volume_free (impl->file_system, volume);
2805 }
2806
2807 static void
2808 update_chooser_entry (GtkFileChooserDefault *impl)
2809 {
2810   GtkTreeSelection *selection;
2811   const GtkFileInfo *info;
2812   GtkTreeIter iter;
2813   GtkTreeIter child_iter;
2814
2815   if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
2816     return;
2817
2818   g_assert (!impl->select_multiple);
2819   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2820
2821   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2822     return;
2823
2824   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2825                                                   &child_iter,
2826                                                   &iter);
2827
2828   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
2829
2830   if (!gtk_file_info_get_is_folder (info))
2831     gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry),
2832                         gtk_file_info_get_display_name (info));
2833 }
2834
2835 static void
2836 gtk_file_chooser_default_set_current_folder (GtkFileChooser    *chooser,
2837                                              const GtkFilePath *path)
2838 {
2839   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2840   GError *error;
2841
2842   error = NULL;
2843   if (!gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, impl->file_system, &error))
2844     {
2845       error_dialog (impl,
2846                     _("Could not set current folder: %s"),
2847                     path, error);
2848       return;
2849     }
2850
2851   if (impl->current_folder != path)
2852     {
2853       if (impl->current_folder)
2854         gtk_file_path_free (impl->current_folder);
2855
2856       impl->current_folder = gtk_file_path_copy (path);
2857     }
2858
2859   /* Update the widgets that may trigger a folder chnage themselves */
2860
2861   if (!impl->changing_folder)
2862     {
2863       impl->changing_folder = TRUE;
2864
2865       set_tree_model (impl, impl->current_folder);
2866       _gtk_file_system_model_path_do (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2867                                       path, expand_and_select_func, impl);
2868
2869       shortcuts_update_current_folder (impl);
2870
2871       impl->changing_folder = FALSE;
2872     }
2873
2874   /* Create a new list model */
2875   set_list_model (impl);
2876
2877   /* Refresh controls */
2878
2879   shortcuts_unselect_all (impl);
2880
2881   g_signal_emit_by_name (impl, "current-folder-changed", 0);
2882
2883   check_preview_change (impl);
2884   bookmarks_check_add_sensitivity (impl);
2885
2886   g_signal_emit_by_name (impl, "selection-changed", 0);
2887 }
2888
2889 static GtkFilePath *
2890 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
2891 {
2892   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2893
2894   return gtk_file_path_copy (impl->current_folder);
2895 }
2896
2897 static void
2898 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
2899                                            const gchar    *name)
2900 {
2901   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2902
2903   g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
2904
2905   gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry), name);
2906 }
2907
2908 static void
2909 select_func (GtkFileSystemModel *model,
2910              GtkTreePath        *path,
2911              GtkTreeIter        *iter,
2912              gpointer            user_data)
2913 {
2914   GtkFileChooserDefault *impl = user_data;
2915   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2916   GtkTreePath *sorted_path;
2917
2918   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
2919   gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
2920   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), sorted_path, NULL, TRUE, 0.3, 0.0);
2921   gtk_tree_path_free (sorted_path);
2922 }
2923
2924 static void
2925 gtk_file_chooser_default_select_path (GtkFileChooser    *chooser,
2926                                       const GtkFilePath *path)
2927 {
2928   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2929   GtkFilePath *parent_path;
2930   GError *error;
2931
2932   error = NULL;
2933   if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, &error))
2934     {
2935       error_getting_info_dialog (impl, path, error);
2936       return;
2937     }
2938
2939   if (!parent_path)
2940     {
2941       _gtk_file_chooser_set_current_folder_path (chooser, path);
2942     }
2943   else
2944     {
2945       _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
2946       gtk_file_path_free (parent_path);
2947       _gtk_file_system_model_path_do (impl->browse_files_model, path,
2948                                       select_func, impl);
2949     }
2950 }
2951
2952 static void
2953 unselect_func (GtkFileSystemModel *model,
2954                GtkTreePath        *path,
2955                GtkTreeIter        *iter,
2956                gpointer            user_data)
2957 {
2958   GtkFileChooserDefault *impl = user_data;
2959   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2960   GtkTreePath *sorted_path;
2961
2962   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
2963                                                                 path);
2964   gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
2965                                     sorted_path);
2966   gtk_tree_path_free (sorted_path);
2967 }
2968
2969 static void
2970 gtk_file_chooser_default_unselect_path (GtkFileChooser    *chooser,
2971                                         const GtkFilePath *path)
2972 {
2973   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2974
2975   _gtk_file_system_model_path_do (impl->browse_files_model, path,
2976                                  unselect_func, impl);
2977 }
2978
2979 static void
2980 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
2981 {
2982   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2983   if (impl->select_multiple)
2984     {
2985       GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2986       gtk_tree_selection_select_all (selection);
2987     }
2988 }
2989
2990 static void
2991 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
2992 {
2993   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2994   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2995
2996   gtk_tree_selection_unselect_all (selection);
2997 }
2998
2999 struct get_paths_closure {
3000   GtkFileChooserDefault *impl;
3001   GSList *result;
3002   GtkFilePath *path_from_entry;
3003 };
3004
3005 static void
3006 get_paths_foreach (GtkTreeModel *model,
3007                    GtkTreePath  *path,
3008                    GtkTreeIter  *iter,
3009                    gpointer      data)
3010 {
3011   struct get_paths_closure *info;
3012   const GtkFilePath *file_path;
3013   GtkFileSystemModel *fs_model;
3014   GtkTreeIter sel_iter;
3015
3016   info = data;
3017
3018   if (info->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3019       info->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3020     {
3021       fs_model = info->impl->browse_directories_model;
3022       sel_iter = *iter;
3023     }
3024   else
3025     {
3026       fs_model = info->impl->browse_files_model;
3027       gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
3028     }
3029
3030   file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &sel_iter);
3031
3032   if (!info->path_from_entry
3033       || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
3034     info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
3035 }
3036
3037 static GSList *
3038 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
3039 {
3040   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3041   struct get_paths_closure info;
3042
3043   info.impl = impl;
3044   info.result = NULL;
3045   info.path_from_entry = NULL;
3046
3047   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3048     {
3049       const char *filename;
3050
3051       filename = gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry));
3052
3053       if (filename != NULL && filename[0] != '\0')
3054         {
3055           GtkFilePath *selected;
3056           GError *error = NULL;
3057
3058           selected = gtk_file_system_make_path (impl->file_system, impl->current_folder, filename, &error);
3059
3060           if (!selected)
3061             {
3062               error_building_filename_dialog (impl, impl->current_folder, filename, error);
3063               return NULL;
3064             }
3065
3066           info.path_from_entry = selected;
3067         }
3068     }
3069
3070   if (!info.path_from_entry || impl->select_multiple)
3071     {
3072       GtkTreeSelection *selection;
3073
3074       selection = NULL;
3075
3076       if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3077           impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3078         {
3079           if (impl->browse_directories_model)
3080             selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
3081         }
3082       else
3083         {
3084           if (impl->sort_model)
3085             selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3086         }
3087
3088       if (selection)
3089         gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
3090     }
3091
3092   if (info.path_from_entry)
3093     info.result = g_slist_prepend (info.result, info.path_from_entry);
3094
3095   return g_slist_reverse (info.result);
3096 }
3097
3098 static GtkFilePath *
3099 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
3100 {
3101   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3102
3103   if (impl->preview_path)
3104     return gtk_file_path_copy (impl->preview_path);
3105   else
3106     return NULL;
3107 }
3108
3109 static GtkFileSystem *
3110 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
3111 {
3112   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3113
3114   return impl->file_system;
3115 }
3116
3117 /* Shows or hides the filter widgets */
3118 static void
3119 toolbar_show_filters (GtkFileChooserDefault *impl,
3120                       gboolean               show)
3121 {
3122   if (show)
3123     gtk_widget_show (impl->filter_combo);
3124   else
3125     gtk_widget_hide (impl->filter_combo);
3126 }
3127
3128 static void
3129 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
3130                                      GtkFileFilter  *filter)
3131 {
3132   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3133   const gchar *name;
3134
3135   if (g_slist_find (impl->filters, filter))
3136     {
3137       g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
3138       return;
3139     }
3140
3141   g_object_ref (filter);
3142   gtk_object_sink (GTK_OBJECT (filter));
3143   impl->filters = g_slist_append (impl->filters, filter);
3144
3145   name = gtk_file_filter_get_name (filter);
3146   if (!name)
3147     name = "Untitled filter";   /* Place-holder, doesn't need to be marked for translation */
3148
3149   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
3150
3151   if (!g_slist_find (impl->filters, impl->current_filter))
3152     set_current_filter (impl, filter);
3153
3154   toolbar_show_filters (impl, TRUE);
3155 }
3156
3157 static void
3158 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
3159                                         GtkFileFilter  *filter)
3160 {
3161   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3162   GtkTreeModel *model;
3163   GtkTreeIter iter;
3164   gint filter_index;
3165
3166   filter_index = g_slist_index (impl->filters, filter);
3167
3168   if (filter_index < 0)
3169     {
3170       g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
3171       return;
3172     }
3173
3174   impl->filters = g_slist_remove (impl->filters, filter);
3175
3176   if (filter == impl->current_filter)
3177     {
3178       if (impl->filters)
3179         set_current_filter (impl, impl->filters->data);
3180       else
3181         set_current_filter (impl, NULL);
3182     }
3183
3184   /* Remove row from the combo box */
3185   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
3186   gtk_tree_model_iter_nth_child  (model, &iter, NULL, filter_index);
3187   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
3188
3189   g_object_unref (filter);
3190
3191   if (!impl->filters)
3192     toolbar_show_filters (impl, FALSE);
3193 }
3194
3195 static GSList *
3196 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
3197 {
3198   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3199
3200   return g_slist_copy (impl->filters);
3201 }
3202
3203 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
3204 static int
3205 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
3206                                        int                    pos)
3207 {
3208   return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
3209 }
3210
3211 static gboolean
3212 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser    *chooser,
3213                                               const GtkFilePath *path,
3214                                               GError           **error)
3215 {
3216   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3217   gboolean result;
3218   int pos;
3219
3220   pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
3221
3222   result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
3223
3224   if (result)
3225     impl->num_shortcuts++;
3226
3227   return result;
3228 }
3229
3230 static gboolean
3231 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
3232                                                  const GtkFilePath *path,
3233                                                  GError           **error)
3234 {
3235   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3236   int pos;
3237   GtkTreeIter iter;
3238   int i;
3239
3240   if (impl->num_shortcuts == 0)
3241     goto out;
3242
3243   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3244   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3245     g_assert_not_reached ();
3246
3247   for (i = 0; i < impl->num_shortcuts; i++)
3248     {
3249       GtkFilePath *shortcut;
3250
3251       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3252       g_assert (shortcut != NULL);
3253
3254       if (gtk_file_path_compare (shortcut, path) == 0)
3255         {
3256           /* The other columns are freed by the GtkTreeStore */
3257           gtk_file_path_free (shortcut);
3258           gtk_list_store_remove (impl->shortcuts_model, &iter);
3259           impl->num_shortcuts--;
3260           return TRUE;
3261         }
3262
3263       if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3264         g_assert_not_reached ();
3265     }
3266
3267  out:
3268
3269   g_set_error (error,
3270                GTK_FILE_CHOOSER_ERROR,
3271                GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
3272                _("shortcut %s does not exist"),
3273                gtk_file_path_get_string (path));
3274
3275   return FALSE;
3276 }
3277
3278 static GSList *
3279 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
3280 {
3281   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3282   int pos;
3283   GtkTreeIter iter;
3284   int i;
3285   GSList *list;
3286
3287   if (impl->num_shortcuts == 0)
3288     return NULL;
3289
3290   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3291   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3292     g_assert_not_reached ();
3293
3294   list = NULL;
3295
3296   for (i = 0; i < impl->num_shortcuts; i++)
3297     {
3298       GtkFilePath *shortcut;
3299
3300       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3301       g_assert (shortcut != NULL);
3302
3303       list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
3304
3305       if (i != impl->num_shortcuts - 1)
3306         {
3307           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3308             g_assert_not_reached ();
3309         }
3310     }
3311
3312   return g_slist_reverse (list);
3313 }
3314
3315 /* Guesses a size based upon font sizes */
3316 static void
3317 find_good_size_from_style (GtkWidget *widget,
3318                            gint      *width,
3319                            gint      *height)
3320 {
3321   GtkFileChooserDefault *impl;
3322   gint default_width, default_height;
3323   int font_size;
3324   GtkRequisition req;
3325   GtkRequisition preview_req;
3326
3327   g_assert (widget->style != NULL);
3328   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3329
3330   font_size = pango_font_description_get_size (widget->style->font_desc);
3331   font_size = PANGO_PIXELS (font_size);
3332
3333   default_width = font_size * NUM_CHARS;
3334   default_height = font_size * NUM_LINES;
3335
3336   /* Use at least the requisition size not including the preview widget */
3337   gtk_widget_size_request (widget, &req);
3338
3339   if (impl->preview_widget_active && impl->preview_widget)
3340     gtk_widget_size_request (impl->preview_box, &preview_req);
3341   else
3342     preview_req.width = 0;
3343
3344   default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
3345   default_height = MAX (default_height, req.height);
3346
3347   *width = default_width;
3348   *height = default_height;
3349 }
3350
3351 static void
3352 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
3353                                            gint                *default_width,
3354                                            gint                *default_height)
3355 {
3356   GtkFileChooserDefault *impl;
3357
3358   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
3359
3360   find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
3361
3362   if (impl->preview_widget_active && impl->preview_widget)
3363     *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
3364 }
3365
3366 static void
3367 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
3368                                               gboolean            *resize_horizontally,
3369                                               gboolean            *resize_vertically)
3370 {
3371   GtkFileChooserDefault *impl;
3372
3373   g_return_if_fail (resize_horizontally != NULL);
3374   g_return_if_fail (resize_vertically != NULL);
3375
3376   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
3377
3378   *resize_horizontally = TRUE;
3379   *resize_vertically = TRUE;
3380
3381   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3382       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3383     {
3384       if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3385         {
3386           *resize_horizontally = FALSE;
3387           *resize_vertically = FALSE;
3388         }
3389     }
3390 }
3391
3392 static void
3393 set_current_filter (GtkFileChooserDefault *impl,
3394                     GtkFileFilter         *filter)
3395 {
3396   if (impl->current_filter != filter)
3397     {
3398       int filter_index;
3399
3400       /* If we have filters, new filter must be one of them
3401        */
3402       filter_index = g_slist_index (impl->filters, filter);
3403       if (impl->filters && filter_index < 0)
3404         return;
3405
3406       if (impl->current_filter)
3407         g_object_unref (impl->current_filter);
3408       impl->current_filter = filter;
3409       if (impl->current_filter)
3410         {
3411           g_object_ref (impl->current_filter);
3412           gtk_object_sink (GTK_OBJECT (filter));
3413         }
3414
3415       if (impl->filters)
3416         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
3417                                   filter_index);
3418
3419       install_list_model_filter (impl);
3420
3421       g_object_notify (G_OBJECT (impl), "filter");
3422     }
3423 }
3424
3425 static void
3426 open_and_close (GtkTreeView *tree_view,
3427                 GtkTreePath *target_path)
3428 {
3429   GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
3430   GtkTreeIter iter;
3431   GtkTreePath *path;
3432
3433   path = gtk_tree_path_new ();
3434   gtk_tree_path_append_index (path, 0);
3435
3436   gtk_tree_model_get_iter (model, &iter, path);
3437
3438   while (TRUE)
3439     {
3440       if (gtk_tree_path_is_ancestor (path, target_path) ||
3441           gtk_tree_path_compare (path, target_path) == 0)
3442         {
3443           GtkTreeIter child_iter;
3444           gtk_tree_view_expand_row (tree_view, path, FALSE);
3445           if (gtk_tree_model_iter_children (model, &child_iter, &iter))
3446             {
3447               iter = child_iter;
3448               gtk_tree_path_down (path);
3449               goto next;
3450             }
3451         }
3452       else
3453         gtk_tree_view_collapse_row (tree_view, path);
3454
3455       while (TRUE)
3456         {
3457           GtkTreeIter parent_iter;
3458           GtkTreeIter next_iter;
3459
3460           next_iter = iter;
3461           if (gtk_tree_model_iter_next (model, &next_iter))
3462             {
3463               iter = next_iter;
3464               gtk_tree_path_next (path);
3465               goto next;
3466             }
3467
3468           if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
3469             goto out;
3470
3471           iter = parent_iter;
3472           gtk_tree_path_up (path);
3473         }
3474     next:
3475       ;
3476     }
3477
3478  out:
3479   gtk_tree_path_free (path);
3480 }
3481
3482 static void
3483 filter_combo_changed (GtkComboBox           *combo_box,
3484                       GtkFileChooserDefault *impl)
3485 {
3486   gint new_index = gtk_combo_box_get_active (combo_box);
3487   GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
3488
3489   set_current_filter (impl, new_filter);
3490 }
3491
3492 static void
3493 check_preview_change (GtkFileChooserDefault *impl)
3494 {
3495   const GtkFilePath *new_path = NULL;
3496   const GtkFileInfo *new_info = NULL;
3497
3498   /* FIXME #132255: Fixing preview for multiple selection involves getting the
3499    * full selection and diffing to find out what the most recently selected file
3500    * is; there is logic in GtkFileSelection that probably can be
3501    * copied.
3502    */
3503   if (impl->sort_model && !impl->select_multiple)
3504     {
3505       GtkTreeSelection *selection;
3506       GtkTreeIter iter;
3507
3508       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3509       if (gtk_tree_selection_get_selected (selection, NULL, &iter))
3510         {
3511           GtkTreeIter child_iter;
3512
3513           gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3514                                                           &child_iter, &iter);
3515
3516           new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3517           new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3518         }
3519     }
3520
3521   if (new_path != impl->preview_path &&
3522       !(new_path && impl->preview_path &&
3523         gtk_file_path_compare (new_path, impl->preview_path) == 0))
3524     {
3525       if (impl->preview_path)
3526         {
3527           gtk_file_path_free (impl->preview_path);
3528           g_free (impl->preview_display_name);
3529         }
3530
3531       if (new_path)
3532         {
3533           impl->preview_path = gtk_file_path_copy (new_path);
3534           impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
3535         }
3536       else
3537         {
3538           impl->preview_path = NULL;
3539           impl->preview_display_name = NULL;
3540         }
3541
3542       if (impl->use_preview_label && impl->preview_label)
3543         gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
3544
3545       g_signal_emit_by_name (impl, "update-preview");
3546     }
3547 }
3548
3549 static void
3550 tree_selection_changed (GtkTreeSelection      *selection,
3551                         GtkFileChooserDefault *impl)
3552 {
3553   GtkTreeIter iter;
3554   const GtkFilePath *file_path;
3555   GtkTreePath *path;
3556
3557   /* FIXME #132255: Fixing this for multiple selection involves getting the full
3558    * selection and diffing to find out what the most recently selected file is;
3559    * there is logic in GtkFileSelection that probably can be copied;
3560    * check_preview_change() is similar.
3561    */
3562   if (impl->select_multiple
3563       || !gtk_tree_selection_get_selected (selection, NULL, &iter))
3564     return;
3565
3566   file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3567                                                &iter);
3568   if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
3569     return;
3570
3571   /* Close the tree up to only the parents of the newly selected
3572    * node and it's immediate children are visible.
3573    */
3574   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_directories_model), &iter);
3575   open_and_close (GTK_TREE_VIEW (impl->browse_directories_tree_view), path);
3576   gtk_tree_path_free (path);
3577
3578   if (!impl->changing_folder)
3579     _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3580 }
3581
3582 /* Activates a volume by mounting it if necessary and then switching to its
3583  * base path.
3584  */
3585 static void
3586 shortcuts_activate_volume (GtkFileChooserDefault *impl,
3587                            GtkFileSystemVolume   *volume)
3588 {
3589   GtkFilePath *path;
3590
3591   if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
3592     {
3593       GError *error;
3594
3595       error = NULL;
3596       if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
3597         {
3598           char *msg;
3599
3600           msg = g_strdup_printf ("Could not mount %s:\n%s",
3601                                  gtk_file_system_volume_get_display_name (impl->file_system, volume),
3602                                  error->message);
3603           error_message (impl, msg);
3604           g_free (msg);
3605           g_error_free (error);
3606
3607           return;
3608         }
3609     }
3610
3611   path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
3612   _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
3613   gtk_file_path_free (path);
3614 }
3615
3616 /* Opens the folder or volume at the specified index in the shortcuts list */
3617 static void
3618 shortcuts_activate_item (GtkFileChooserDefault *impl,
3619                          int                    item_num)
3620 {
3621   GtkTreePath *path;
3622   gboolean result;
3623   GtkTreeIter iter;
3624   gpointer data;
3625   int start_row;
3626
3627   if (item_num == shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR)
3628       || item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR))
3629     return;
3630
3631   path = gtk_tree_path_new_from_indices (item_num, -1);
3632   result = gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path);
3633   gtk_tree_path_free (path);
3634
3635   if (!result)
3636     return;
3637
3638   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
3639
3640   start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
3641   if ((item_num >= start_row && item_num < start_row + impl->num_volumes)
3642       || (item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER) && impl->shortcuts_current_folder_is_volume))
3643     {
3644       GtkFileSystemVolume *volume;
3645
3646       volume = data;
3647       shortcuts_activate_volume (impl, volume);
3648     }
3649   else
3650     {
3651       GtkFilePath *file_path;
3652
3653       file_path = data;
3654       _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3655     }
3656 }
3657
3658 /* Callback used when a row in the shortcuts list is activated */
3659 static void
3660 shortcuts_row_activated_cb (GtkTreeView           *tree_view,
3661                             GtkTreePath           *path,
3662                             GtkTreeViewColumn     *column,
3663                             GtkFileChooserDefault *impl)
3664 {
3665   int selected;
3666   GtkTreeIter iter;
3667   GtkTreeIter child_iter;
3668   GtkTreePath *child_path;
3669
3670   if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
3671     return;
3672
3673   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
3674                                                     &child_iter,
3675                                                     &iter);
3676   child_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter);
3677   if (!child_path)
3678     return;
3679
3680   selected = *gtk_tree_path_get_indices (child_path);
3681   gtk_tree_path_free (child_path);
3682
3683   shortcuts_activate_item (impl, selected);
3684 }
3685
3686 static gboolean
3687 shortcuts_select_func  (GtkTreeSelection  *selection,
3688                         GtkTreeModel      *model,
3689                         GtkTreePath       *path,
3690                         gboolean           path_currently_selected,
3691                         gpointer           data)
3692 {
3693   GtkFileChooserDefault *impl = data;
3694
3695   return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
3696 }
3697
3698 static void
3699 list_selection_changed (GtkTreeSelection      *selection,
3700                         GtkFileChooserDefault *impl)
3701 {
3702   /* See if we are in the new folder editable row for Save mode */
3703   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3704     {
3705       GtkTreeSelection *selection;
3706       GtkTreeIter iter, child_iter;
3707       const GtkFileInfo *info;
3708
3709       g_assert (!impl->select_multiple);
3710       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3711       if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
3712         return;
3713
3714       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3715                                                       &child_iter,
3716                                                       &iter);
3717
3718       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3719       if (!info)
3720         return; /* We are on the editable row for New Folder */
3721     }
3722
3723   update_chooser_entry (impl);
3724   check_preview_change (impl);
3725   bookmarks_check_add_sensitivity (impl);
3726
3727   g_signal_emit_by_name (impl, "selection-changed", 0);
3728 }
3729
3730 /* Callback used when a row in the file list is activated */
3731 static void
3732 list_row_activated (GtkTreeView           *tree_view,
3733                     GtkTreePath           *path,
3734                     GtkTreeViewColumn     *column,
3735                     GtkFileChooserDefault *impl)
3736 {
3737   GtkTreeIter iter, child_iter;
3738   const GtkFileInfo *info;
3739
3740   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
3741     return;
3742
3743   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
3744
3745   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3746
3747   if (gtk_file_info_get_is_folder (info))
3748     {
3749       const GtkFilePath *file_path;
3750
3751       file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3752       _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3753
3754       return;
3755     }
3756
3757   g_signal_emit_by_name (impl, "file-activated");
3758 }
3759
3760 static void
3761 path_bar_clicked (GtkPathBar            *path_bar,
3762                   GtkFilePath           *file_path,
3763                   GtkFileChooserDefault *impl)
3764 {
3765   _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3766 }
3767
3768 static const GtkFileInfo *
3769 get_list_file_info (GtkFileChooserDefault *impl,
3770                     GtkTreeIter           *iter)
3771 {
3772   GtkTreeIter child_iter;
3773
3774   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3775                                                   &child_iter,
3776                                                   iter);
3777
3778   return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3779 }
3780
3781 static void
3782 tree_name_data_func (GtkTreeViewColumn *tree_column,
3783                      GtkCellRenderer   *cell,
3784                      GtkTreeModel      *tree_model,
3785                      GtkTreeIter       *iter,
3786                      gpointer           data)
3787 {
3788   GtkFileChooserDefault *impl = data;
3789   const GtkFileInfo *info;
3790
3791   info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3792                                           iter);
3793
3794   if (info)
3795     {
3796       g_object_set (cell,
3797                     "text", gtk_file_info_get_display_name (info),
3798                     NULL);
3799     }
3800 }
3801
3802 static void
3803 list_icon_data_func (GtkTreeViewColumn *tree_column,
3804                      GtkCellRenderer   *cell,
3805                      GtkTreeModel      *tree_model,
3806                      GtkTreeIter       *iter,
3807                      gpointer           data)
3808 {
3809   GtkFileChooserDefault *impl = data;
3810   GtkTreeIter child_iter;
3811   const GtkFilePath *path;
3812   GdkPixbuf *pixbuf;
3813
3814   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3815                                                   &child_iter,
3816                                                   iter);
3817   path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3818   if (!path)
3819     return;
3820
3821   /* FIXME: NULL GError */
3822   pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
3823   g_object_set (cell,
3824                 "pixbuf", pixbuf,
3825                 NULL);
3826
3827   if (pixbuf)
3828     g_object_unref (pixbuf);
3829 }
3830
3831 static void
3832 list_name_data_func (GtkTreeViewColumn *tree_column,
3833                      GtkCellRenderer   *cell,
3834                      GtkTreeModel      *tree_model,
3835                      GtkTreeIter       *iter,
3836                      gpointer           data)
3837 {
3838   GtkFileChooserDefault *impl = data;
3839   const GtkFileInfo *info = get_list_file_info (impl, iter);
3840
3841   if (!info)
3842     {
3843       g_object_set (cell,
3844                     "text", _("Type name of new folder"),
3845                     NULL);
3846       return;
3847     }
3848
3849   g_object_set (cell,
3850                 "text", gtk_file_info_get_display_name (info),
3851                 NULL);
3852 }
3853
3854 #if 0
3855 static void
3856 list_size_data_func (GtkTreeViewColumn *tree_column,
3857                      GtkCellRenderer   *cell,
3858                      GtkTreeModel      *tree_model,
3859                      GtkTreeIter       *iter,
3860                      gpointer           data)
3861 {
3862   GtkFileChooserDefault *impl = data;
3863   const GtkFileInfo *info = get_list_file_info (impl, iter);
3864   gint64 size;
3865   gchar *str;
3866
3867   if (!info || gtk_file_info_get_is_folder (info))
3868     return;
3869
3870   size = gtk_file_info_get_size (info);
3871
3872   if (size < (gint64)1024)
3873     str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
3874   else if (size < (gint64)1024*1024)
3875     str = g_strdup_printf (_("%.1f K"), size / (1024.));
3876   else if (size < (gint64)1024*1024*1024)
3877     str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
3878   else
3879     str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
3880
3881   g_object_set (cell,
3882                 "text", str,
3883                 NULL);
3884
3885   g_free (str);
3886 }
3887 #endif
3888
3889 /* Tree column data callback for the file list; fetches the mtime of a file */
3890 static void
3891 list_mtime_data_func (GtkTreeViewColumn *tree_column,
3892                       GtkCellRenderer   *cell,
3893                       GtkTreeModel      *tree_model,
3894                       GtkTreeIter       *iter,
3895                       gpointer           data)
3896 {
3897   GtkFileChooserDefault *impl;
3898   const GtkFileInfo *info;
3899   GtkFileTime time_mtime, time_now;
3900   GDate mtime, now;
3901   int days_diff;
3902   char buf[256];
3903
3904   impl = data;
3905
3906   info = get_list_file_info (impl, iter);
3907   if (!info)
3908     {
3909       g_object_set (cell,
3910                     "text", "",
3911                     NULL);
3912       return;
3913     }
3914
3915   time_mtime = gtk_file_info_get_modification_time (info);
3916   g_date_set_time (&mtime, (GTime) time_mtime);
3917
3918   time_now = (GTime ) time (NULL);
3919   g_date_set_time (&now, (GTime) time_now);
3920
3921   days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
3922
3923   if (days_diff == 0)
3924     strcpy (buf, _("Today"));
3925   else if (days_diff == 1)
3926     strcpy (buf, _("Yesterday"));
3927   else
3928     {
3929       char *format;
3930
3931       if (days_diff > 1 && days_diff < 7)
3932         format = "%A"; /* Days from last week */
3933       else
3934         /* FIXME: Get the right format for the locale */
3935         format = _("%d/%b/%Y"); /* Any other date */
3936
3937       if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
3938         strcpy (buf, _("Unknown"));
3939     }
3940
3941   g_object_set (cell,
3942                 "text", buf,
3943                 NULL);
3944 }
3945
3946 GtkWidget *
3947 _gtk_file_chooser_default_new (const char *file_system)
3948 {
3949   return  g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
3950                         "file-system-backend", file_system,
3951                         NULL);
3952 }
3953
3954 static GtkWidget *
3955 location_entry_create (GtkFileChooserDefault *impl)
3956 {
3957   GtkWidget *entry;
3958
3959   entry = _gtk_file_chooser_entry_new ();
3960   /* Pick a good width for the entry */
3961   gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
3962   gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
3963   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
3964   _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
3965
3966   return GTK_WIDGET (entry);
3967 }
3968
3969 static void
3970 update_from_entry (GtkFileChooserDefault *impl,
3971                    GtkWindow             *parent,
3972                    GtkFileChooserEntry   *chooser_entry)
3973 {
3974   const GtkFilePath *folder_path;
3975   const char *file_part;
3976
3977   folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
3978   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
3979
3980   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
3981     {
3982       error_message_with_parent (parent,
3983                                  _("Cannot change to the folder you specified as it is an invalid path."));
3984       return;
3985     }
3986
3987   if (file_part[0] == '\0')
3988     {
3989       _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), folder_path);
3990       return;
3991     }
3992   else
3993     {
3994       GtkFileFolder *folder = NULL;
3995       GtkFilePath *subfolder_path = NULL;
3996       GtkFileInfo *info = NULL;
3997       GError *error;
3998
3999       /* If the file part is non-empty, we need to figure out if it refers to a
4000        * folder within folder. We could optimize the case here where the folder
4001        * is already loaded for one of our tree models.
4002        */
4003
4004       error = NULL;
4005       folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
4006
4007       if (!folder)
4008         {
4009           error_getting_info_dialog (impl, folder_path, error);
4010           return;
4011         }
4012
4013       error = NULL;
4014       subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
4015
4016       if (!subfolder_path)
4017         {
4018           char *msg;
4019
4020           msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
4021                                  gtk_file_path_get_string (folder_path),
4022                                  file_part,
4023                                  error->message);
4024           error_message (impl, msg);
4025           g_free (msg);
4026           g_object_unref (folder);
4027           return;
4028         }
4029
4030       error = NULL;
4031       info = gtk_file_folder_get_info (folder, subfolder_path, &error);
4032
4033       if (!info)
4034         {
4035 #if 0
4036           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4037             {
4038               g_object_unref (folder);
4039               gtk_file_path_free (subfolder_path);
4040               return;
4041             }
4042 #endif
4043           error_getting_info_dialog (impl, subfolder_path, error);
4044           g_object_unref (folder);
4045           gtk_file_path_free (subfolder_path);
4046           return;
4047         }
4048
4049       if (gtk_file_info_get_is_folder (info))
4050         _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), subfolder_path);
4051       else
4052         _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path);
4053
4054       g_object_unref (folder);
4055       gtk_file_path_free (subfolder_path);
4056       gtk_file_info_free (info);
4057     }
4058 }
4059
4060 static void
4061 location_popup_handler (GtkFileChooserDefault *impl)
4062 {
4063   GtkWidget *dialog;
4064   GtkWidget *toplevel;
4065   GtkWidget *hbox;
4066   GtkWidget *label;
4067   GtkWidget *entry;
4068
4069   /* Create dialog */
4070
4071   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
4072   if (!GTK_WIDGET_TOPLEVEL (toplevel))
4073     toplevel = NULL;
4074
4075   dialog = gtk_dialog_new_with_buttons (_("Open Location"),
4076                                         GTK_WINDOW (toplevel),
4077                                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
4078                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
4079                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
4080                                         NULL);
4081   gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
4082   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
4083   gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
4084   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
4085
4086   hbox = gtk_hbox_new (FALSE, 12);
4087   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
4088   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
4089
4090   label = gtk_label_new_with_mnemonic (_("_Location:"));
4091   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
4092
4093   entry = location_entry_create (impl);
4094   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
4095   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
4096
4097   /* Run */
4098
4099   gtk_widget_show_all (dialog);
4100   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
4101     update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry));
4102
4103   gtk_widget_destroy (dialog);
4104 }
4105
4106 /* Handler for the "up-folder" keybinding signal */
4107 static void
4108 up_folder_handler (GtkFileChooserDefault *impl)
4109 {
4110   GtkFilePath *parent_path;
4111   GError *error;
4112
4113   error = NULL;
4114   if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, &error))
4115     {
4116       if (parent_path) /* If we were on a root, parent_path will be NULL */
4117         {
4118           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), parent_path);
4119           gtk_file_path_free (parent_path);
4120         }
4121     }
4122   else
4123     error_dialog (impl,
4124                   _("Could not go to the parent folder of %s:\n%s"),
4125                   impl->current_folder,
4126                   error);
4127 }
4128
4129 /* Handler for the "home-folder" keybinding signal */
4130 static void
4131 home_folder_handler (GtkFileChooserDefault *impl)
4132 {
4133   const char *home;
4134
4135   /* Should we pull this information from impl->has_home and the shortcuts data
4136    * instead?  Sounds like a bit of overkill...
4137    */
4138
4139   home = g_get_home_dir ();
4140   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), home);
4141 }