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