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