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