]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdefault.c
d1ca6306a80316bc6dc4cef474d8a95bbc7ae228
[~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 <config.h>
22 #include "gdk/gdkkeysyms.h"
23 #include "gtkalignment.h"
24 #include "gtkbindings.h"
25 #include "gtkbutton.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcheckmenuitem.h"
30 #include "gtkclipboard.h"
31 #include "gtkcombobox.h"
32 #include "gtkentry.h"
33 #include "gtkeventbox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechoosersettings.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystemmodel.h"
43 #include "gtkframe.h"
44 #include "gtkhbox.h"
45 #include "gtkhpaned.h"
46 #include "gtkiconfactory.h"
47 #include "gtkicontheme.h"
48 #include "gtkimage.h"
49 #include "gtkimagemenuitem.h"
50 #include "gtkintl.h"
51 #include "gtklabel.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmenuitem.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkscrolledwindow.h"
59 #include "gtkseparatormenuitem.h"
60 #include "gtksizegroup.h"
61 #include "gtkstock.h"
62 #include "gtktable.h"
63 #include "gtktreednd.h"
64 #include "gtktreeprivate.h"
65 #include "gtktreeselection.h"
66 #include "gtktypebuiltins.h"
67 #include "gtkvbox.h"
68
69 #if defined (G_OS_UNIX)
70 #include "gtkfilesystemunix.h"
71 #elif defined (G_OS_WIN32)
72 #include "gtkfilesystemwin32.h"
73 #endif
74
75 #include "gtkalias.h"
76
77 #include <errno.h>
78 #include <string.h>
79 #include <time.h>
80
81 \f
82
83 #ifdef HAVE_UNISTD_H
84 #include <unistd.h>
85 #endif
86 #ifdef G_OS_WIN32
87 #include <io.h>
88 #endif
89
90 /* Profiling stuff */
91 #undef PROFILE_FILE_CHOOSER
92 #ifdef PROFILE_FILE_CHOOSER
93
94
95 #ifndef F_OK 
96 #define F_OK 0
97 #endif
98
99 #define PROFILE_INDENT 4
100 static int profile_indent;
101
102 static void
103 profile_add_indent (int indent)
104 {
105   profile_indent += indent;
106   if (profile_indent < 0)
107     g_error ("You screwed up your indentation");
108 }
109
110 void
111 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
112 {
113   char *str;
114
115   if (indent < 0)
116     profile_add_indent (indent);
117
118   if (profile_indent == 0)
119     str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120   else
121     str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
122
123   access (str, F_OK);
124   g_free (str);
125
126   if (indent > 0)
127     profile_add_indent (indent);
128 }
129
130 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
131 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
132 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #else
134 #define profile_start(x, y)
135 #define profile_end(x, y)
136 #define profile_msg(x, y)
137 #endif
138
139 \f
140
141 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142
143 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
144 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
145 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146
147 #define MAX_LOADING_TIME 500
148
149 struct _GtkFileChooserDefaultClass
150 {
151   GtkVBoxClass parent_class;
152 };
153
154 /* Signal IDs */
155 enum {
156   LOCATION_POPUP,
157   LOCATION_POPUP_ON_PASTE,
158   UP_FOLDER,
159   DOWN_FOLDER,
160   HOME_FOLDER,
161   DESKTOP_FOLDER,
162   QUICK_BOOKMARK,
163   LAST_SIGNAL
164 };
165
166 static guint signals[LAST_SIGNAL] = { 0 };
167
168 /* Column numbers for the shortcuts tree.  Keep these in sync with shortcuts_model_create() */
169 enum {
170   SHORTCUTS_COL_PIXBUF,
171   SHORTCUTS_COL_NAME,
172   SHORTCUTS_COL_DATA,
173   SHORTCUTS_COL_IS_VOLUME,
174   SHORTCUTS_COL_REMOVABLE,
175   SHORTCUTS_COL_PIXBUF_VISIBLE,
176   SHORTCUTS_COL_HANDLE,
177   SHORTCUTS_COL_NUM_COLUMNS
178 };
179
180 /* Column numbers for the file list */
181 enum {
182   FILE_LIST_COL_NAME,
183   FILE_LIST_COL_SIZE,
184   FILE_LIST_COL_MTIME,
185   FILE_LIST_COL_NUM_COLUMNS
186 };
187
188 /* Identifiers for target types */
189 enum {
190   GTK_TREE_MODEL_ROW,
191   TEXT_URI_LIST
192 };
193
194 /* Target types for dragging from the shortcuts list */
195 static const GtkTargetEntry shortcuts_source_targets[] = {
196   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
197 };
198
199 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
200                                                  / sizeof (shortcuts_source_targets[0]));
201
202 /* Target types for dropping into the shortcuts list */
203 static const GtkTargetEntry shortcuts_dest_targets[] = {
204   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
205   { "text/uri-list", 0, TEXT_URI_LIST }
206 };
207
208 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
209                                                / sizeof (shortcuts_dest_targets[0]));
210
211 /* Target types for DnD from the file list */
212 static const GtkTargetEntry file_list_source_targets[] = {
213   { "text/uri-list", 0, TEXT_URI_LIST }
214 };
215
216 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
217                                                  / sizeof (file_list_source_targets[0]));
218
219 /* Target types for dropping into the file list */
220 static const GtkTargetEntry file_list_dest_targets[] = {
221   { "text/uri-list", 0, TEXT_URI_LIST }
222 };
223
224 static const int num_file_list_dest_targets = (sizeof (file_list_dest_targets)
225                                                / sizeof (file_list_dest_targets[0]));
226
227
228 /* Interesting places in the shortcuts bar */
229 typedef enum {
230   SHORTCUTS_HOME,
231   SHORTCUTS_DESKTOP,
232   SHORTCUTS_VOLUMES,
233   SHORTCUTS_SHORTCUTS,
234   SHORTCUTS_BOOKMARKS_SEPARATOR,
235   SHORTCUTS_BOOKMARKS,
236   SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
237   SHORTCUTS_CURRENT_FOLDER
238 } ShortcutsIndex;
239
240 /* Icon size for if we can't get it from the theme */
241 #define FALLBACK_ICON_SIZE 16
242
243 #define PREVIEW_HBOX_SPACING 12
244 #define NUM_LINES 40
245 #define NUM_CHARS 60
246
247 static void gtk_file_chooser_default_iface_init       (GtkFileChooserIface        *iface);
248 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface   *iface);
249
250 static GObject* gtk_file_chooser_default_constructor  (GType                  type,
251                                                        guint                  n_construct_properties,
252                                                        GObjectConstructParam *construct_params);
253 static void     gtk_file_chooser_default_finalize     (GObject               *object);
254 static void     gtk_file_chooser_default_set_property (GObject               *object,
255                                                        guint                  prop_id,
256                                                        const GValue          *value,
257                                                        GParamSpec            *pspec);
258 static void     gtk_file_chooser_default_get_property (GObject               *object,
259                                                        guint                  prop_id,
260                                                        GValue                *value,
261                                                        GParamSpec            *pspec);
262 static void     gtk_file_chooser_default_dispose      (GObject               *object);
263 static void     gtk_file_chooser_default_show_all       (GtkWidget             *widget);
264 static void     gtk_file_chooser_default_map            (GtkWidget             *widget);
265 static void     gtk_file_chooser_default_unmap          (GtkWidget             *widget);
266 static void     gtk_file_chooser_default_hierarchy_changed (GtkWidget          *widget,
267                                                             GtkWidget          *previous_toplevel);
268 static void     gtk_file_chooser_default_style_set      (GtkWidget             *widget,
269                                                          GtkStyle              *previous_style);
270 static void     gtk_file_chooser_default_screen_changed (GtkWidget             *widget,
271                                                          GdkScreen             *previous_screen);
272
273 static gboolean       gtk_file_chooser_default_set_current_folder          (GtkFileChooser    *chooser,
274                                                                             const GtkFilePath *path,
275                                                                             GError           **error);
276 static gboolean       gtk_file_chooser_default_update_current_folder       (GtkFileChooser    *chooser,
277                                                                             const GtkFilePath *path,
278                                                                             gboolean           keep_trail,
279                                                                             GError           **error);
280 static GtkFilePath *  gtk_file_chooser_default_get_current_folder          (GtkFileChooser    *chooser);
281 static void           gtk_file_chooser_default_set_current_name            (GtkFileChooser    *chooser,
282                                                                             const gchar       *name);
283 static gboolean       gtk_file_chooser_default_select_path                 (GtkFileChooser    *chooser,
284                                                                             const GtkFilePath *path,
285                                                                             GError           **error);
286 static void           gtk_file_chooser_default_unselect_path               (GtkFileChooser    *chooser,
287                                                                             const GtkFilePath *path);
288 static void           gtk_file_chooser_default_select_all                  (GtkFileChooser    *chooser);
289 static void           gtk_file_chooser_default_unselect_all                (GtkFileChooser    *chooser);
290 static GSList *       gtk_file_chooser_default_get_paths                   (GtkFileChooser    *chooser);
291 static GtkFilePath *  gtk_file_chooser_default_get_preview_path            (GtkFileChooser    *chooser);
292 static GtkFileSystem *gtk_file_chooser_default_get_file_system             (GtkFileChooser    *chooser);
293 static void           gtk_file_chooser_default_add_filter                  (GtkFileChooser    *chooser,
294                                                                             GtkFileFilter     *filter);
295 static void           gtk_file_chooser_default_remove_filter               (GtkFileChooser    *chooser,
296                                                                             GtkFileFilter     *filter);
297 static GSList *       gtk_file_chooser_default_list_filters                (GtkFileChooser    *chooser);
298 static gboolean       gtk_file_chooser_default_add_shortcut_folder    (GtkFileChooser    *chooser,
299                                                                        const GtkFilePath *path,
300                                                                        GError           **error);
301 static gboolean       gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
302                                                                        const GtkFilePath *path,
303                                                                        GError           **error);
304 static GSList *       gtk_file_chooser_default_list_shortcut_folders  (GtkFileChooser    *chooser);
305
306 static void           gtk_file_chooser_default_get_default_size       (GtkFileChooserEmbed *chooser_embed,
307                                                                        gint                *default_width,
308                                                                        gint                *default_height);
309 static void           gtk_file_chooser_default_get_resizable_hints    (GtkFileChooserEmbed *chooser_embed,
310                                                                        gboolean            *resize_horizontally,
311                                                                        gboolean            *resize_vertically);
312 static gboolean       gtk_file_chooser_default_should_respond         (GtkFileChooserEmbed *chooser_embed);
313 static void           gtk_file_chooser_default_initial_focus          (GtkFileChooserEmbed *chooser_embed);
314
315 static void location_popup_handler (GtkFileChooserDefault *impl,
316                                     const gchar           *path);
317 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
318 static void up_folder_handler      (GtkFileChooserDefault *impl);
319 static void down_folder_handler    (GtkFileChooserDefault *impl);
320 static void home_folder_handler    (GtkFileChooserDefault *impl);
321 static void desktop_folder_handler (GtkFileChooserDefault *impl);
322 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
323                                     gint                   bookmark_index);
324 static void update_appearance      (GtkFileChooserDefault *impl);
325
326 static void set_current_filter   (GtkFileChooserDefault *impl,
327                                   GtkFileFilter         *filter);
328 static void check_preview_change (GtkFileChooserDefault *impl);
329
330 static void filter_combo_changed       (GtkComboBox           *combo_box,
331                                         GtkFileChooserDefault *impl);
332 static void     shortcuts_row_activated_cb (GtkTreeView           *tree_view,
333                                             GtkTreePath           *path,
334                                             GtkTreeViewColumn     *column,
335                                             GtkFileChooserDefault *impl);
336
337 static gboolean shortcuts_key_press_event_cb (GtkWidget             *widget,
338                                               GdkEventKey           *event,
339                                               GtkFileChooserDefault *impl);
340
341 static gboolean shortcuts_select_func   (GtkTreeSelection      *selection,
342                                          GtkTreeModel          *model,
343                                          GtkTreePath           *path,
344                                          gboolean               path_currently_selected,
345                                          gpointer               data);
346 static gboolean shortcuts_get_selected  (GtkFileChooserDefault *impl,
347                                          GtkTreeIter           *iter);
348 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
349                                      GtkTreeIter           *iter);
350 static int shortcuts_get_index (GtkFileChooserDefault *impl,
351                                 ShortcutsIndex         where);
352 static int shortcut_find_position (GtkFileChooserDefault *impl,
353                                    const GtkFilePath     *path);
354
355 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
356
357 static gboolean list_select_func   (GtkTreeSelection      *selection,
358                                     GtkTreeModel          *model,
359                                     GtkTreePath           *path,
360                                     gboolean               path_currently_selected,
361                                     gpointer               data);
362
363 static void list_selection_changed     (GtkTreeSelection      *tree_selection,
364                                         GtkFileChooserDefault *impl);
365 static void list_row_activated         (GtkTreeView           *tree_view,
366                                         GtkTreePath           *path,
367                                         GtkTreeViewColumn     *column,
368                                         GtkFileChooserDefault *impl);
369
370 static void select_func (GtkFileSystemModel *model,
371                          GtkTreePath        *path,
372                          GtkTreeIter        *iter,
373                          gpointer            user_data);
374
375 static void path_bar_clicked           (GtkPathBar            *path_bar,
376                                         GtkFilePath           *file_path,
377                                         GtkFilePath           *child_path,
378                                         gboolean               child_is_hidden,
379                                         GtkFileChooserDefault *impl);
380
381 static void add_bookmark_button_clicked_cb    (GtkButton             *button,
382                                                GtkFileChooserDefault *impl);
383 static void remove_bookmark_button_clicked_cb (GtkButton             *button,
384                                                GtkFileChooserDefault *impl);
385 static void save_folder_combo_changed_cb      (GtkComboBox           *combo,
386                                                GtkFileChooserDefault *impl);
387
388 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
389                                  GtkCellRenderer   *cell,
390                                  GtkTreeModel      *tree_model,
391                                  GtkTreeIter       *iter,
392                                  gpointer           data);
393 static void list_name_data_func (GtkTreeViewColumn *tree_column,
394                                  GtkCellRenderer   *cell,
395                                  GtkTreeModel      *tree_model,
396                                  GtkTreeIter       *iter,
397                                  gpointer           data);
398 #if 0
399 static void list_size_data_func (GtkTreeViewColumn *tree_column,
400                                  GtkCellRenderer   *cell,
401                                  GtkTreeModel      *tree_model,
402                                  GtkTreeIter       *iter,
403                                  gpointer           data);
404 #endif
405 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
406                                   GtkCellRenderer   *cell,
407                                   GtkTreeModel      *tree_model,
408                                   GtkTreeIter       *iter,
409                                   gpointer           data);
410
411 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
412                                               GtkTreeIter           *iter);
413
414 static void load_remove_timer (GtkFileChooserDefault *impl);
415 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
416
417 static void location_button_toggled_cb (GtkToggleButton *toggle,
418                                         GtkFileChooserDefault *impl);
419 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
420
421
422 \f
423
424 /* Drag and drop interface declarations */
425
426 typedef struct {
427   GtkTreeModelFilter parent;
428
429   GtkFileChooserDefault *impl;
430 } ShortcutsModelFilter;
431
432 typedef struct {
433   GtkTreeModelFilterClass parent_class;
434 } ShortcutsModelFilterClass;
435
436 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
437 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
438
439 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
440
441 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
442                          _shortcuts_model_filter,
443                          GTK_TYPE_TREE_MODEL_FILTER,
444                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
445                                                 shortcuts_model_filter_drag_source_iface_init))
446
447 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
448                                                  GtkTreeModel          *child_model,
449                                                  GtkTreePath           *root);
450
451 \f
452
453 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
454                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
455                                                 gtk_file_chooser_default_iface_init)
456                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
457                                                 gtk_file_chooser_embed_default_iface_init));                                            
458
459 static void
460 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
461 {
462   static const guint quick_bookmark_keyvals[10] = {
463     GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
464   };
465   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
466   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
467   GtkBindingSet *binding_set;
468   int i;
469
470   gobject_class->finalize = gtk_file_chooser_default_finalize;
471   gobject_class->constructor = gtk_file_chooser_default_constructor;
472   gobject_class->set_property = gtk_file_chooser_default_set_property;
473   gobject_class->get_property = gtk_file_chooser_default_get_property;
474   gobject_class->dispose = gtk_file_chooser_default_dispose;
475
476   widget_class->show_all = gtk_file_chooser_default_show_all;
477   widget_class->map = gtk_file_chooser_default_map;
478   widget_class->unmap = gtk_file_chooser_default_unmap;
479   widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
480   widget_class->style_set = gtk_file_chooser_default_style_set;
481   widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
482
483   signals[LOCATION_POPUP] =
484     _gtk_binding_signal_new (I_("location-popup"),
485                              G_OBJECT_CLASS_TYPE (class),
486                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
487                              G_CALLBACK (location_popup_handler),
488                              NULL, NULL,
489                              _gtk_marshal_VOID__STRING,
490                              G_TYPE_NONE, 1, G_TYPE_STRING);
491   signals[LOCATION_POPUP_ON_PASTE] =
492     _gtk_binding_signal_new ("location-popup-on-paste",
493                              G_OBJECT_CLASS_TYPE (class),
494                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
495                              G_CALLBACK (location_popup_on_paste_handler),
496                              NULL, NULL,
497                              _gtk_marshal_VOID__VOID,
498                              G_TYPE_NONE, 0);
499   signals[UP_FOLDER] =
500     _gtk_binding_signal_new (I_("up-folder"),
501                              G_OBJECT_CLASS_TYPE (class),
502                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
503                              G_CALLBACK (up_folder_handler),
504                              NULL, NULL,
505                              _gtk_marshal_VOID__VOID,
506                              G_TYPE_NONE, 0);
507   signals[DOWN_FOLDER] =
508     _gtk_binding_signal_new (I_("down-folder"),
509                              G_OBJECT_CLASS_TYPE (class),
510                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
511                              G_CALLBACK (down_folder_handler),
512                              NULL, NULL,
513                              _gtk_marshal_VOID__VOID,
514                              G_TYPE_NONE, 0);
515   signals[HOME_FOLDER] =
516     _gtk_binding_signal_new (I_("home-folder"),
517                              G_OBJECT_CLASS_TYPE (class),
518                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
519                              G_CALLBACK (home_folder_handler),
520                              NULL, NULL,
521                              _gtk_marshal_VOID__VOID,
522                              G_TYPE_NONE, 0);
523   signals[DESKTOP_FOLDER] =
524     _gtk_binding_signal_new (I_("desktop-folder"),
525                              G_OBJECT_CLASS_TYPE (class),
526                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
527                              G_CALLBACK (desktop_folder_handler),
528                              NULL, NULL,
529                              _gtk_marshal_VOID__VOID,
530                              G_TYPE_NONE, 0);
531   signals[QUICK_BOOKMARK] =
532     _gtk_binding_signal_new (I_("quick-bookmark"),
533                              G_OBJECT_CLASS_TYPE (class),
534                              G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
535                              G_CALLBACK (quick_bookmark_handler),
536                              NULL, NULL,
537                              _gtk_marshal_VOID__INT,
538                              G_TYPE_NONE, 1, G_TYPE_INT);
539
540   binding_set = gtk_binding_set_by_class (class);
541
542   gtk_binding_entry_add_signal (binding_set,
543                                 GDK_l, GDK_CONTROL_MASK,
544                                 "location-popup",
545                                 1, G_TYPE_STRING, "");
546
547   gtk_binding_entry_add_signal (binding_set,
548                                 GDK_slash, 0,
549                                 "location-popup",
550                                 1, G_TYPE_STRING, "/");
551   gtk_binding_entry_add_signal (binding_set,
552                                 GDK_KP_Divide, 0,
553                                 "location-popup",
554                                 1, G_TYPE_STRING, "/");
555
556 #ifdef G_OS_UNIX
557   gtk_binding_entry_add_signal (binding_set,
558                                 GDK_asciitilde, 0,
559                                 "location-popup",
560                                 1, G_TYPE_STRING, "~");
561 #endif
562
563   gtk_binding_entry_add_signal (binding_set,
564                                 GDK_v, GDK_CONTROL_MASK,
565                                 "location-popup-on-paste",
566                                 0);
567
568   gtk_binding_entry_add_signal (binding_set,
569                                 GDK_Up, GDK_MOD1_MASK,
570                                 "up-folder",
571                                 0);
572   gtk_binding_entry_add_signal (binding_set,
573                                 GDK_BackSpace, 0,
574                                 "up-folder",
575                                 0);
576   gtk_binding_entry_add_signal (binding_set,
577                                 GDK_KP_Up, GDK_MOD1_MASK,
578                                 "up-folder",
579                                 0);
580
581   gtk_binding_entry_add_signal (binding_set,
582                                 GDK_Down, GDK_MOD1_MASK,
583                                 "down-folder",
584                                 0);
585   gtk_binding_entry_add_signal (binding_set,
586                                 GDK_KP_Down, GDK_MOD1_MASK,
587                                 "down-folder",
588                                 0);
589
590   gtk_binding_entry_add_signal (binding_set,
591                                 GDK_Home, GDK_MOD1_MASK,
592                                 "home-folder",
593                                 0);
594   gtk_binding_entry_add_signal (binding_set,
595                                 GDK_KP_Home, GDK_MOD1_MASK,
596                                 "home-folder",
597                                 0);
598   gtk_binding_entry_add_signal (binding_set,
599                                 GDK_d, GDK_MOD1_MASK,
600                                 "desktop-folder",
601                                 0);
602
603   for (i = 0; i < 10; i++)
604     gtk_binding_entry_add_signal (binding_set,
605                                   quick_bookmark_keyvals[i], GDK_MOD1_MASK,
606                                   "quick-bookmark",
607                                   1, G_TYPE_INT, i);
608
609   _gtk_file_chooser_install_properties (gobject_class);
610
611   gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
612                                                       P_("Default file chooser backend"),
613                                                       P_("Name of the GtkFileChooser backend to use by default"),
614                                                       NULL,
615                                                       GTK_PARAM_READWRITE));
616 }
617
618 static void
619 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
620 {
621   iface->select_path = gtk_file_chooser_default_select_path;
622   iface->unselect_path = gtk_file_chooser_default_unselect_path;
623   iface->select_all = gtk_file_chooser_default_select_all;
624   iface->unselect_all = gtk_file_chooser_default_unselect_all;
625   iface->get_paths = gtk_file_chooser_default_get_paths;
626   iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
627   iface->get_file_system = gtk_file_chooser_default_get_file_system;
628   iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
629   iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
630   iface->set_current_name = gtk_file_chooser_default_set_current_name;
631   iface->add_filter = gtk_file_chooser_default_add_filter;
632   iface->remove_filter = gtk_file_chooser_default_remove_filter;
633   iface->list_filters = gtk_file_chooser_default_list_filters;
634   iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
635   iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
636   iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
637 }
638
639 static void
640 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
641 {
642   iface->get_default_size = gtk_file_chooser_default_get_default_size;
643   iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
644   iface->should_respond = gtk_file_chooser_default_should_respond;
645   iface->initial_focus = gtk_file_chooser_default_initial_focus;
646 }
647
648 static void
649 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
650 {
651   profile_start ("start", NULL);
652 #ifdef PROFILE_FILE_CHOOSER
653   access ("MARK: *** CREATE FILE CHOOSER", F_OK);
654 #endif
655   impl->local_only = TRUE;
656   impl->preview_widget_active = TRUE;
657   impl->use_preview_label = TRUE;
658   impl->select_multiple = FALSE;
659   impl->show_hidden = FALSE;
660   impl->icon_size = FALLBACK_ICON_SIZE;
661   impl->load_state = LOAD_EMPTY;
662   impl->reload_state = RELOAD_EMPTY;
663   impl->pending_select_paths = NULL;
664   impl->location_mode = LOCATION_MODE_PATH_BAR;
665
666   gtk_box_set_spacing (GTK_BOX (impl), 12);
667
668   impl->tooltips = gtk_tooltips_new ();
669   g_object_ref_sink (impl->tooltips);
670
671   profile_end ("end", NULL);
672 }
673
674 /* Frees the data columns for the specified iter in the shortcuts model*/
675 static void
676 shortcuts_free_row_data (GtkFileChooserDefault *impl,
677                          GtkTreeIter           *iter)
678 {
679   gpointer col_data;
680   gboolean is_volume;
681   GtkFileSystemHandle *handle;
682
683   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
684                       SHORTCUTS_COL_DATA, &col_data,
685                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
686                       SHORTCUTS_COL_HANDLE, &handle,
687                       -1);
688
689   if (handle)
690     gtk_file_system_cancel_operation (handle);
691
692   if (!col_data)
693     return;
694
695   if (is_volume)
696     {
697       GtkFileSystemVolume *volume;
698
699       volume = col_data;
700       gtk_file_system_volume_free (impl->file_system, volume);
701     }
702   else
703     {
704       GtkFilePath *path;
705
706       path = col_data;
707       gtk_file_path_free (path);
708     }
709 }
710
711 /* Frees all the data columns in the shortcuts model */
712 static void
713 shortcuts_free (GtkFileChooserDefault *impl)
714 {
715   GtkTreeIter iter;
716
717   if (!impl->shortcuts_model)
718     return;
719
720   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
721     do
722       {
723         shortcuts_free_row_data (impl, &iter);
724       }
725     while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
726
727   g_object_unref (impl->shortcuts_model);
728   impl->shortcuts_model = NULL;
729 }
730
731 static void
732 pending_select_paths_free (GtkFileChooserDefault *impl)
733 {
734   GSList *l;
735
736   for (l = impl->pending_select_paths; l; l = l->next)
737     {
738       GtkFilePath *path;
739
740       path = l->data;
741       gtk_file_path_free (path);
742     }
743
744   g_slist_free (impl->pending_select_paths);
745   impl->pending_select_paths = NULL;
746 }
747
748 static void
749 pending_select_paths_add (GtkFileChooserDefault *impl,
750                           const GtkFilePath     *path)
751 {
752   impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
753 }
754
755 /* Used from gtk_tree_selection_selected_foreach() */
756 static void
757 store_selection_foreach (GtkTreeModel *model,
758                          GtkTreePath  *path,
759                          GtkTreeIter  *iter,
760                          gpointer      data)
761 {
762   GtkFileChooserDefault *impl;
763   GtkTreeIter child_iter;
764   const GtkFilePath *file_path;
765
766   impl = GTK_FILE_CHOOSER_DEFAULT (data);
767
768   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
769
770   file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
771   pending_select_paths_add (impl, file_path);
772 }
773
774 /* Stores the current selection in the list of paths to select; this is used to
775  * preserve the selection when reloading the current folder.
776  */
777 static void
778 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
779 {
780   GtkTreeSelection *selection;
781
782   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
783   gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
784 }
785
786 static void
787 gtk_file_chooser_default_finalize (GObject *object)
788 {
789   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
790   GSList *l;
791
792   if (impl->shortcuts_filter_model)
793     g_object_unref (impl->shortcuts_filter_model);
794
795   shortcuts_free (impl);
796
797   g_object_unref (impl->file_system);
798
799   g_free (impl->browse_files_last_selected_name);
800
801   for (l = impl->filters; l; l = l->next)
802     {
803       GtkFileFilter *filter;
804
805       filter = GTK_FILE_FILTER (l->data);
806       g_object_unref (filter);
807     }
808   g_slist_free (impl->filters);
809
810   if (impl->current_filter)
811     g_object_unref (impl->current_filter);
812
813   if (impl->current_volume_path)
814     gtk_file_path_free (impl->current_volume_path);
815
816   if (impl->current_folder)
817     gtk_file_path_free (impl->current_folder);
818
819   if (impl->preview_path)
820     gtk_file_path_free (impl->preview_path);
821
822   load_remove_timer (impl);
823
824   /* Free all the Models we have */
825   if (impl->browse_files_model)
826     g_object_unref (impl->browse_files_model);
827
828   if (impl->sort_model)
829     g_object_unref (impl->sort_model);
830
831   g_free (impl->preview_display_name);
832
833   g_free (impl->edited_new_text);
834
835   g_object_unref (impl->tooltips);
836
837   G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
838 }
839
840 /* Shows an error dialog set as transient for the specified window */
841 static void
842 error_message_with_parent (GtkWindow  *parent,
843                            const char *msg,
844                            const char *detail)
845 {
846   GtkWidget *dialog;
847
848   dialog = gtk_message_dialog_new (parent,
849                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
850                                    GTK_MESSAGE_ERROR,
851                                    GTK_BUTTONS_OK,
852                                    "%s",
853                                    msg);
854   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
855                                             "%s", detail);
856
857   if (parent->group)
858     gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
859
860   gtk_dialog_run (GTK_DIALOG (dialog));
861   gtk_widget_destroy (dialog);
862 }
863
864 /* Returns a toplevel GtkWindow, or NULL if none */
865 static GtkWindow *
866 get_toplevel (GtkWidget *widget)
867 {
868   GtkWidget *toplevel;
869
870   toplevel = gtk_widget_get_toplevel (widget);
871   if (!GTK_WIDGET_TOPLEVEL (toplevel))
872     return NULL;
873   else
874     return GTK_WINDOW (toplevel);
875 }
876
877 /* Shows an error dialog for the file chooser */
878 static void
879 error_message (GtkFileChooserDefault *impl,
880                const char            *msg,
881                const char            *detail)
882 {
883   error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
884 }
885
886 /* Shows a simple error dialog relative to a path.  Frees the GError as well. */
887 static void
888 error_dialog (GtkFileChooserDefault *impl,
889               const char            *msg,
890               const GtkFilePath     *path,
891               GError                *error)
892 {
893   if (error)
894     {
895       char *uri = NULL;
896       char *text;
897
898       if (path)
899         uri = gtk_file_system_path_to_uri (impl->file_system, path);
900       text = g_strdup_printf (msg, uri);
901       error_message (impl, text, error->message);
902       g_free (text);
903       g_free (uri);
904       g_error_free (error);
905     }
906 }
907
908 /* Displays an error message about not being able to get information for a file.
909  * Frees the GError as well.
910  */
911 static void
912 error_getting_info_dialog (GtkFileChooserDefault *impl,
913                            const GtkFilePath     *path,
914                            GError                *error)
915 {
916   error_dialog (impl,
917                 _("Could not retrieve information about the file"),
918                 path, error);
919 }
920
921 /* Shows an error dialog about not being able to add a bookmark */
922 static void
923 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
924                               const GtkFilePath     *path,
925                               GError                *error)
926 {
927   error_dialog (impl,
928                 _("Could not add a bookmark"),
929                 path, error);
930 }
931
932 /* Shows an error dialog about not being able to remove a bookmark */
933 static void
934 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
935                                 const GtkFilePath     *path,
936                                 GError                *error)
937 {
938   error_dialog (impl,
939                 _("Could not remove bookmark"),
940                 path, error);
941 }
942
943 /* Shows an error dialog about not being able to create a folder */
944 static void
945 error_creating_folder_dialog (GtkFileChooserDefault *impl,
946                               const GtkFilePath     *path,
947                               GError                *error)
948 {
949   error_dialog (impl, 
950                 _("The folder could not be created"), 
951                 path, error);
952 }
953
954 /* Shows an error about not being able to create a folder because a file with
955  * the same name is already there.
956  */
957 static void
958 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
959                                                  const GtkFilePath     *path,
960                                                  GError                *error)
961 {
962   error_dialog (impl,
963                 _("The folder could not be created, as a file with the same name "
964                   "already exists.  Try using a different name for the folder, "
965                   "or rename the file first."),
966                 path, error);
967 }
968
969 /* Shows an error dialog about not being able to create a filename */
970 static void
971 error_building_filename_dialog (GtkFileChooserDefault *impl,
972                                 const GtkFilePath     *folder_part,
973                                 const char            *file_part,
974                                 GError                *error)
975 {
976   error_dialog (impl, _("Invalid file name"), 
977                 NULL, error);
978 }
979
980 /* Shows an error dialog when we cannot switch to a folder */
981 static void
982 error_changing_folder_dialog (GtkFileChooserDefault *impl,
983                               const GtkFilePath     *path,
984                               GError                *error)
985 {
986   error_dialog (impl, _("The folder contents could not be displayed"),
987                 path, error);
988 }
989
990 /* Changes folders, displaying an error dialog if this fails */
991 static gboolean
992 change_folder_and_display_error (GtkFileChooserDefault *impl,
993                                  const GtkFilePath     *path)
994 {
995   GError *error;
996   gboolean result;
997   GtkFilePath *path_copy;
998
999   g_return_val_if_fail (path != NULL, FALSE);
1000
1001   profile_start ("start", (char *) path);
1002
1003   /* We copy the path because of this case:
1004    *
1005    * list_row_activated()
1006    *   fetches path from model; path belongs to the model (*)
1007    *   calls change_folder_and_display_error()
1008    *     calls _gtk_file_chooser_set_current_folder_path()
1009    *       changing folders fails, sets model to NULL, thus freeing the path in (*)
1010    */
1011
1012   path_copy = gtk_file_path_copy (path);
1013
1014   error = NULL;
1015   result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, &error);
1016
1017   if (!result)
1018     error_changing_folder_dialog (impl, path_copy, error);
1019
1020   gtk_file_path_free (path_copy);
1021
1022   profile_end ("end", (char *) path);
1023
1024   return result;
1025 }
1026
1027 static void
1028 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1029 {
1030   if (impl->use_preview_label)
1031     {
1032       if (!impl->preview_label)
1033         {
1034           impl->preview_label = gtk_label_new (impl->preview_display_name);
1035           gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1036           gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1037           gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1038           gtk_widget_show (impl->preview_label);
1039         }
1040     }
1041   else
1042     {
1043       if (impl->preview_label)
1044         {
1045           gtk_widget_destroy (impl->preview_label);
1046           impl->preview_label = NULL;
1047         }
1048     }
1049
1050   if (impl->preview_widget_active && impl->preview_widget)
1051     gtk_widget_show (impl->preview_box);
1052   else
1053     gtk_widget_hide (impl->preview_box);
1054
1055   g_signal_emit_by_name (impl, "default-size-changed");
1056 }
1057
1058 static void
1059 set_preview_widget (GtkFileChooserDefault *impl,
1060                     GtkWidget             *preview_widget)
1061 {
1062   if (preview_widget == impl->preview_widget)
1063     return;
1064
1065   if (impl->preview_widget)
1066     gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1067                           impl->preview_widget);
1068
1069   impl->preview_widget = preview_widget;
1070   if (impl->preview_widget)
1071     {
1072       gtk_widget_show (impl->preview_widget);
1073       gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1074       gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1075                              impl->preview_widget,
1076                              (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1077     }
1078
1079   update_preview_widget_visibility (impl);
1080 }
1081
1082 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1083 struct ReloadIconsData
1084 {
1085   GtkFileChooserDefault *impl;
1086   GtkTreeRowReference *row_ref;
1087 };
1088
1089 static void
1090 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1091                                     const GtkFileInfo   *info,
1092                                     const GError        *error,
1093                                     gpointer             user_data)
1094 {
1095   GdkPixbuf *pixbuf;
1096   GtkTreeIter iter;
1097   GtkTreePath *path;
1098   gboolean cancelled = handle->cancelled;
1099   struct ReloadIconsData *data = user_data;
1100
1101   if (!g_slist_find (data->impl->reload_icon_handles, handle))
1102     goto out;
1103
1104   data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1105
1106   if (cancelled || error)
1107     goto out;
1108
1109   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1110                                       data->impl->icon_size, NULL);
1111
1112   path = gtk_tree_row_reference_get_path (data->row_ref);
1113   gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1114   gtk_list_store_set (data->impl->shortcuts_model, &iter,
1115                       SHORTCUTS_COL_PIXBUF, pixbuf,
1116                       -1);
1117   gtk_tree_path_free (path);
1118
1119   if (pixbuf)
1120     g_object_unref (pixbuf);
1121
1122 out:
1123   gtk_tree_row_reference_free (data->row_ref);
1124   g_object_unref (data->impl);
1125   g_free (data);
1126
1127   g_object_unref (handle);
1128 }
1129
1130 static void
1131 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1132 {
1133   GSList *l;
1134   GtkTreeIter iter;
1135
1136   profile_start ("start", NULL);
1137
1138   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1139     goto out;
1140
1141   for (l = impl->reload_icon_handles; l; l = l->next)
1142     {
1143       GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1144       gtk_file_system_cancel_operation (handle);
1145     }
1146   g_slist_free (impl->reload_icon_handles);
1147   impl->reload_icon_handles = NULL;
1148
1149   do
1150     {
1151       gpointer data;
1152       gboolean is_volume;
1153       gboolean pixbuf_visible;
1154       GdkPixbuf *pixbuf;
1155
1156       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1157                           SHORTCUTS_COL_DATA, &data,
1158                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
1159                           SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1160                           -1);
1161
1162       if (pixbuf_visible && data)
1163         {
1164           if (is_volume)
1165             {
1166               GtkFileSystemVolume *volume;
1167
1168               volume = data;
1169               pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1170                                                            impl->icon_size, NULL);
1171
1172               gtk_list_store_set (impl->shortcuts_model, &iter,
1173                                   SHORTCUTS_COL_PIXBUF, pixbuf,
1174                                   -1);
1175
1176               if (pixbuf)
1177                 g_object_unref (pixbuf);
1178             }
1179           else if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1180             {
1181               const GtkFilePath *path;
1182               struct ReloadIconsData *info;
1183               GtkTreePath *tree_path;
1184               GtkFileSystemHandle *handle;
1185
1186               path = data;
1187
1188               info = g_new0 (struct ReloadIconsData, 1);
1189               info->impl = g_object_ref (impl);
1190               tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1191               info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1192               gtk_tree_path_free (tree_path);
1193
1194               handle = gtk_file_system_get_info (impl->file_system, path,
1195                                                  GTK_FILE_INFO_ICON,
1196                                                  shortcuts_reload_icons_get_info_cb,
1197                                                  info);
1198               impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1199             }
1200           else
1201             {
1202               GtkIconTheme *icon_theme;
1203
1204               /* Don't call get_info for remote paths to avoid latency and
1205                * auth dialogs.
1206                * If we switch to a better bookmarks file format (XBEL), we
1207                * should use mime info to get a better icon.
1208                */
1209               icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1210               pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory", 
1211                                                  impl->icon_size, 0, NULL);
1212
1213               gtk_list_store_set (impl->shortcuts_model, &iter,
1214                                   SHORTCUTS_COL_PIXBUF, pixbuf,
1215                                   -1);
1216
1217               if (pixbuf)
1218                 g_object_unref (pixbuf);
1219             }
1220         }
1221     }
1222   while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1223
1224  out:
1225
1226   profile_end ("end", NULL);
1227 }
1228
1229 static void 
1230 shortcuts_find_folder (GtkFileChooserDefault *impl,
1231                        GtkFilePath           *folder)
1232 {
1233   GtkTreeSelection *selection;
1234   int pos;
1235   GtkTreePath *path;
1236
1237   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1238
1239   g_assert (folder != NULL);
1240   pos = shortcut_find_position (impl, folder);
1241   if (pos == -1)
1242     {
1243       gtk_tree_selection_unselect_all (selection);
1244       return;
1245     }
1246
1247   path = gtk_tree_path_new_from_indices (pos, -1);
1248   gtk_tree_selection_select_path (selection, path);
1249   gtk_tree_path_free (path);
1250 }
1251
1252 /* If a shortcut corresponds to the current folder, selects it */
1253 static void
1254 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1255 {
1256   shortcuts_find_folder (impl, impl->current_folder);
1257 }
1258
1259 /* Removes the specified number of rows from the shortcuts list */
1260 static void
1261 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1262                        int                    start_row,
1263                        int                    n_rows)
1264 {
1265   GtkTreePath *path;
1266
1267   path = gtk_tree_path_new_from_indices (start_row, -1);
1268
1269   for (; n_rows; n_rows--)
1270     {
1271       GtkTreeIter iter;
1272
1273       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1274         g_assert_not_reached ();
1275
1276       shortcuts_free_row_data (impl, &iter);
1277       gtk_list_store_remove (impl->shortcuts_model, &iter);
1278     }
1279
1280   gtk_tree_path_free (path);
1281 }
1282
1283 static void
1284 shortcuts_update_count (GtkFileChooserDefault *impl,
1285                         ShortcutsIndex         type,
1286                         gint                   value)
1287 {
1288   switch (type)
1289     {
1290       case SHORTCUTS_HOME:
1291         if (value < 0)
1292           impl->has_home = FALSE;
1293         else
1294           impl->has_home = TRUE;
1295         break;
1296
1297       case SHORTCUTS_DESKTOP:
1298         if (value < 0)
1299           impl->has_desktop = FALSE;
1300         else
1301           impl->has_desktop = TRUE;
1302         break;
1303
1304       case SHORTCUTS_VOLUMES:
1305         impl->num_volumes += value;
1306         break;
1307
1308       case SHORTCUTS_SHORTCUTS:
1309         impl->num_shortcuts += value;
1310         break;
1311
1312       case SHORTCUTS_BOOKMARKS:
1313         impl->num_bookmarks += value;
1314         break;
1315
1316       case SHORTCUTS_CURRENT_FOLDER:
1317         if (value < 0)
1318           impl->shortcuts_current_folder_active = FALSE;
1319         else
1320           impl->shortcuts_current_folder_active = TRUE;
1321         break;
1322
1323       default:
1324         /* nothing */
1325         break;
1326     }
1327 }
1328
1329 struct ShortcutsInsertRequest
1330 {
1331   GtkFileChooserDefault *impl;
1332   GtkFilePath *parent_path;
1333   GtkFilePath *path;
1334   int pos;
1335   char *label_copy;
1336   GtkTreeRowReference *row_ref;
1337   ShortcutsIndex type;
1338   gboolean name_only;
1339   gboolean removable;
1340 };
1341
1342 static void
1343 get_file_info_finished (GtkFileSystemHandle *handle,
1344                         const GtkFileInfo   *info,
1345                         const GError        *error,
1346                         gpointer             data)
1347 {
1348   gint pos = -1;
1349   gboolean cancelled = handle->cancelled;
1350   gboolean is_volume = FALSE;
1351   GdkPixbuf *pixbuf;
1352   GtkTreePath *path;
1353   GtkTreeIter iter;
1354   GtkFileSystemHandle *model_handle;
1355   struct ShortcutsInsertRequest *request = data;
1356
1357   path = gtk_tree_row_reference_get_path (request->row_ref);
1358   if (!path)
1359     /* Handle doesn't exist anymore in the model */
1360     goto out;
1361
1362   pos = gtk_tree_path_get_indices (path)[0];
1363   gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1364                            &iter, path);
1365   gtk_tree_path_free (path);
1366
1367   /* validate handle, else goto out */
1368   gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1369                       SHORTCUTS_COL_HANDLE, &model_handle,
1370                       -1);
1371   if (handle != model_handle)
1372     goto out;
1373
1374   /* set the handle to NULL in the model (we unref later on) */
1375   gtk_list_store_set (request->impl->shortcuts_model, &iter,
1376                       SHORTCUTS_COL_HANDLE, NULL,
1377                       -1);
1378
1379   if (cancelled)
1380     goto out;
1381
1382   if (!info)
1383     {
1384       gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1385       shortcuts_update_count (request->impl, request->type, -1);
1386
1387       if (request->type == SHORTCUTS_HOME)
1388         {
1389           const char *home = g_get_home_dir ();
1390           GtkFilePath *home_path;
1391
1392           home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1393           error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1394           gtk_file_path_free (home_path);
1395         }
1396       else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1397         {
1398           /* Remove the current folder separator */
1399           gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1400           shortcuts_remove_rows (request->impl, separator_pos, 1);
1401         }
1402
1403       goto out;
1404     }
1405   
1406   if (!request->label_copy)
1407     request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1408   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1409                                       request->impl->icon_size, NULL);
1410
1411   gtk_list_store_set (request->impl->shortcuts_model, &iter,
1412                       SHORTCUTS_COL_PIXBUF, pixbuf,
1413                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1414                       SHORTCUTS_COL_NAME, request->label_copy,
1415                       SHORTCUTS_COL_IS_VOLUME, is_volume,
1416                       SHORTCUTS_COL_REMOVABLE, request->removable,
1417                       -1);
1418
1419   if (request->impl->shortcuts_filter_model)
1420     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_filter_model));
1421
1422   if (request->type == SHORTCUTS_CURRENT_FOLDER
1423       && request->impl->save_folder_combo != NULL)
1424     {
1425       /* The current folder is updated via _activate_iter(), don't
1426        * have save_folder_combo_changed_cb() call _activate_iter()
1427        * again.
1428        */
1429       g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1430                                        G_CALLBACK (save_folder_combo_changed_cb),
1431                                        request->impl);
1432       gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1433       g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1434                                          G_CALLBACK (save_folder_combo_changed_cb),
1435                                          request->impl);
1436     }
1437
1438   if (pixbuf)
1439     g_object_unref (pixbuf);
1440
1441 out:
1442   g_object_unref (request->impl);
1443   gtk_file_path_free (request->parent_path);
1444   gtk_file_path_free (request->path);
1445   gtk_tree_row_reference_free (request->row_ref);
1446   g_free (request->label_copy);
1447   g_free (request);
1448
1449   g_object_unref (handle);
1450 }
1451
1452 /* FIXME: GtkFileSystem needs a function to split a remote path
1453  * into hostname and path components, or maybe just have a 
1454  * gtk_file_system_path_get_display_name().
1455  *
1456  * This function is also used in gtkfilechooserbutton.c
1457  */
1458 gchar *
1459 _gtk_file_chooser_label_for_uri (const gchar *uri)
1460 {
1461   const gchar *path, *start, *end, *p;
1462   gchar *host, *label;
1463   
1464   start = strstr (uri, "://");
1465   start += 3;
1466   path = strchr (start, '/');
1467   
1468   if (path)
1469     end = path;
1470   else
1471     {
1472       end = uri + strlen (uri);
1473       path = "/";
1474     }
1475
1476   /* strip username */
1477   p = strchr (start, '@');
1478   if (p && p < end)
1479     {
1480       start = p + 1;
1481     }
1482   
1483   p = strchr (start, ':');
1484   if (p && p < end)
1485     end = p;
1486   
1487   host = g_strndup (start, end - start);
1488
1489   /* Translators: the first string is a path and the second string 
1490    * is a hostname. Nautilus and the panel contain the same string 
1491    * to translate. 
1492    */
1493   label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1494   
1495   g_free (host);
1496
1497   return label;
1498 }
1499
1500 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1501  * inserts a volume.  A position of -1 indicates the end of the tree.
1502  */
1503 static void
1504 shortcuts_insert_path (GtkFileChooserDefault *impl,
1505                        int                    pos,
1506                        gboolean               is_volume,
1507                        GtkFileSystemVolume   *volume,
1508                        const GtkFilePath     *path,
1509                        const char            *label,
1510                        gboolean               removable,
1511                        ShortcutsIndex         type)
1512 {
1513   char *label_copy;
1514   GdkPixbuf *pixbuf = NULL;
1515   gpointer data = NULL;
1516   GtkTreeIter iter;
1517   GtkIconTheme *icon_theme;      
1518
1519   profile_start ("start", is_volume ? "volume" : (char *) path);
1520
1521   if (is_volume)
1522     {
1523       data = volume;
1524       label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1525       pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1526                                                    impl->icon_size, NULL);
1527     }
1528   else if (gtk_file_system_path_is_local (impl->file_system, path))
1529     {
1530       struct ShortcutsInsertRequest *request;
1531       GtkFileSystemHandle *handle;
1532       GtkTreePath *p;
1533
1534       request = g_new0 (struct ShortcutsInsertRequest, 1);
1535       request->impl = g_object_ref (impl);
1536       request->path = gtk_file_path_copy (path);
1537       request->name_only = TRUE;
1538       request->removable = removable;
1539       request->pos = pos;
1540       request->type = type;
1541       if (label)
1542         request->label_copy = g_strdup (label);
1543
1544       if (pos == -1)
1545         gtk_list_store_append (impl->shortcuts_model, &iter);
1546       else
1547         gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1548
1549       p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1550       request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1551       gtk_tree_path_free (p);
1552
1553       handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1554                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1555                                          get_file_info_finished, request);
1556
1557       gtk_list_store_set (impl->shortcuts_model, &iter,
1558                           SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1559                           SHORTCUTS_COL_IS_VOLUME, is_volume,
1560                           SHORTCUTS_COL_HANDLE, handle,
1561                           -1);
1562
1563       shortcuts_update_count (impl, type, 1);
1564
1565       return;
1566     }
1567   else
1568     {
1569       /* Don't call get_info for remote paths to avoid latency and
1570        * auth dialogs.
1571        */
1572       data = gtk_file_path_copy (path);
1573       if (label)
1574         label_copy = g_strdup (label);
1575       else
1576         {
1577           gchar *uri;
1578
1579           uri = gtk_file_system_path_to_uri (impl->file_system, path);
1580
1581           label_copy = _gtk_file_chooser_label_for_uri (uri);
1582
1583           g_free (uri);
1584         }
1585     
1586       /* If we switch to a better bookmarks file format (XBEL), we
1587        * should use mime info to get a better icon.
1588        */
1589       icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1590       pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory", 
1591                                          impl->icon_size, 0, NULL);
1592     }
1593
1594   if (pos == -1)
1595     gtk_list_store_append (impl->shortcuts_model, &iter);
1596   else
1597     gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1598
1599   shortcuts_update_count (impl, type, 1);
1600
1601   gtk_list_store_set (impl->shortcuts_model, &iter,
1602                       SHORTCUTS_COL_PIXBUF, pixbuf,
1603                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1604                       SHORTCUTS_COL_NAME, label_copy,
1605                       SHORTCUTS_COL_DATA, data,
1606                       SHORTCUTS_COL_IS_VOLUME, is_volume,
1607                       SHORTCUTS_COL_REMOVABLE, removable,
1608                       SHORTCUTS_COL_HANDLE, NULL,
1609                       -1);
1610
1611   if (impl->shortcuts_filter_model)
1612     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1613
1614   if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1615     {
1616       /* The current folder is updated via _activate_iter(), don't
1617        * have save_folder_combo_changed_cb() call _activate_iter()
1618        * again.
1619        */
1620       gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1621       g_signal_handlers_block_by_func (impl->save_folder_combo,
1622                                        G_CALLBACK (save_folder_combo_changed_cb),
1623                                        impl);
1624       gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1625       g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1626                                          G_CALLBACK (save_folder_combo_changed_cb),
1627                                          impl);
1628     }
1629
1630   g_free (label_copy);
1631
1632   if (pixbuf)
1633     g_object_unref (pixbuf);
1634
1635   profile_end ("end", NULL);
1636 }
1637
1638 /* Appends an item for the user's home directory to the shortcuts model */
1639 static void
1640 shortcuts_append_home (GtkFileChooserDefault *impl)
1641 {
1642   const char *home;
1643   GtkFilePath *home_path;
1644
1645   profile_start ("start", NULL);
1646
1647   home = g_get_home_dir ();
1648   if (home == NULL)
1649     {
1650       profile_end ("end - no home directory!?", NULL);
1651       return;
1652     }
1653
1654   home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1655
1656   shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1657
1658   gtk_file_path_free (home_path);
1659
1660   profile_end ("end", NULL);
1661 }
1662
1663 /* Appends the ~/Desktop directory to the shortcuts model */
1664 static void
1665 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1666 {
1667   char *name;
1668   const char *home;
1669   GtkFilePath *path;
1670
1671   profile_start ("start", NULL);
1672
1673 #ifdef G_OS_WIN32
1674   name = _gtk_file_system_win32_get_desktop ();
1675 #else
1676   home = g_get_home_dir ();
1677   if (home == NULL)
1678     {
1679       profile_end ("end - no home directory!?", NULL);
1680       return;
1681     }
1682
1683   name = g_build_filename (home, "Desktop", NULL);
1684 #endif
1685
1686   path = gtk_file_system_filename_to_path (impl->file_system, name);
1687   g_free (name);
1688
1689   shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1690   /* We do not actually pop up an error dialog if there is no desktop directory
1691    * because some people may really not want to have one.
1692    */
1693
1694   gtk_file_path_free (path);
1695
1696   profile_end ("end", NULL);
1697 }
1698
1699 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1700 static int
1701 shortcuts_append_paths (GtkFileChooserDefault *impl,
1702                         GSList                *paths)
1703 {
1704   int start_row;
1705   int num_inserted;
1706   gchar *label;
1707
1708   profile_start ("start", NULL);
1709
1710   /* As there is no separator now, we want to start there.
1711    */
1712   start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1713   num_inserted = 0;
1714
1715   for (; paths; paths = paths->next)
1716     {
1717       GtkFilePath *path;
1718
1719       path = paths->data;
1720
1721       if (impl->local_only &&
1722           !gtk_file_system_path_is_local (impl->file_system, path))
1723         continue;
1724
1725       label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1726
1727       /* NULL GError, but we don't really want to show error boxes here */
1728       shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
1729       num_inserted++;
1730
1731       g_free (label);
1732     }
1733
1734   profile_end ("end", NULL);
1735
1736   return num_inserted;
1737 }
1738
1739 /* Returns the index for the corresponding item in the shortcuts bar */
1740 static int
1741 shortcuts_get_index (GtkFileChooserDefault *impl,
1742                      ShortcutsIndex         where)
1743 {
1744   int n;
1745
1746   n = 0;
1747
1748   if (where == SHORTCUTS_HOME)
1749     goto out;
1750
1751   n += impl->has_home ? 1 : 0;
1752
1753   if (where == SHORTCUTS_DESKTOP)
1754     goto out;
1755
1756   n += impl->has_desktop ? 1 : 0;
1757
1758   if (where == SHORTCUTS_VOLUMES)
1759     goto out;
1760
1761   n += impl->num_volumes;
1762
1763   if (where == SHORTCUTS_SHORTCUTS)
1764     goto out;
1765
1766   n += impl->num_shortcuts;
1767
1768   if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1769     goto out;
1770
1771   /* If there are no bookmarks there won't be a separator */
1772   n += (impl->num_bookmarks > 0) ? 1 : 0;
1773
1774   if (where == SHORTCUTS_BOOKMARKS)
1775     goto out;
1776
1777   n += impl->num_bookmarks;
1778
1779   if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1780     goto out;
1781
1782   n += 1;
1783
1784   if (where == SHORTCUTS_CURRENT_FOLDER)
1785     goto out;
1786
1787   g_assert_not_reached ();
1788
1789  out:
1790
1791   return n;
1792 }
1793
1794 /* Adds all the file system volumes to the shortcuts model */
1795 static void
1796 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1797 {
1798   int start_row;
1799   GSList *list, *l;
1800   int n;
1801   gboolean old_changing_folders;
1802
1803   profile_start ("start", NULL);
1804
1805
1806   old_changing_folders = impl->changing_folder;
1807   impl->changing_folder = TRUE;
1808
1809   start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1810   shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1811   impl->num_volumes = 0;
1812
1813   list = gtk_file_system_list_volumes (impl->file_system);
1814
1815   n = 0;
1816
1817   for (l = list; l; l = l->next)
1818     {
1819       GtkFileSystemVolume *volume;
1820
1821       volume = l->data;
1822
1823       if (impl->local_only)
1824         {
1825           if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
1826             {
1827               GtkFilePath *base_path;
1828
1829               base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1830               if (base_path != NULL)
1831                 {
1832                   gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1833                   gtk_file_path_free (base_path);
1834
1835                   if (!is_local)
1836                     {
1837                       gtk_file_system_volume_free (impl->file_system, volume);
1838                       continue;
1839                     }
1840                 }
1841             }
1842         }
1843
1844       shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
1845       n++;
1846     }
1847
1848   impl->num_volumes = n;
1849   g_slist_free (list);
1850
1851   if (impl->shortcuts_filter_model)
1852     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1853
1854   impl->changing_folder = old_changing_folders;
1855
1856   profile_end ("end", NULL);
1857 }
1858
1859 /* Inserts a separator node in the shortcuts list */
1860 static void
1861 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1862                             ShortcutsIndex where)
1863 {
1864   GtkTreeIter iter;
1865
1866   g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1867
1868   gtk_list_store_insert (impl->shortcuts_model, &iter,
1869                          shortcuts_get_index (impl, where));
1870   gtk_list_store_set (impl->shortcuts_model, &iter,
1871                       SHORTCUTS_COL_PIXBUF, NULL,
1872                       SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1873                       SHORTCUTS_COL_NAME, NULL,
1874                       SHORTCUTS_COL_DATA, NULL,
1875                       -1);
1876 }
1877
1878 /* Updates the list of bookmarks */
1879 static void
1880 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1881 {
1882   GSList *bookmarks;
1883   gboolean old_changing_folders;
1884   GtkTreeIter iter;
1885   GtkFilePath *list_selected = NULL;
1886   GtkFilePath *combo_selected = NULL;
1887   gboolean is_volume;
1888   gpointer col_data;
1889
1890   profile_start ("start", NULL);
1891         
1892   old_changing_folders = impl->changing_folder;
1893   impl->changing_folder = TRUE;
1894
1895   if (shortcuts_get_selected (impl, &iter))
1896     {
1897       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), 
1898                           &iter, 
1899                           SHORTCUTS_COL_DATA, &col_data,
1900                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
1901                           -1);
1902
1903       if (col_data && !is_volume)
1904         list_selected = gtk_file_path_copy (col_data);
1905     }
1906
1907   if (impl->save_folder_combo &&
1908       gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo), 
1909                                      &iter))
1910     {
1911       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), 
1912                           &iter, 
1913                           SHORTCUTS_COL_DATA, &col_data,
1914                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
1915                           -1);
1916       
1917       if (col_data && !is_volume)
1918         combo_selected = gtk_file_path_copy (col_data);
1919     }
1920
1921   if (impl->num_bookmarks > 0)
1922     shortcuts_remove_rows (impl,
1923                            shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1924                            impl->num_bookmarks + 1);
1925
1926   impl->num_bookmarks = 0;
1927
1928   bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1929   shortcuts_append_paths (impl, bookmarks);
1930   gtk_file_paths_free (bookmarks);
1931
1932   if (impl->num_bookmarks > 0)
1933     shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1934
1935   if (impl->shortcuts_filter_model)
1936     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1937
1938   if (list_selected)
1939     {
1940       shortcuts_find_folder (impl, list_selected);
1941       gtk_file_path_free (list_selected);
1942     }
1943
1944   if (combo_selected)
1945     {
1946       gint pos;
1947
1948       pos = shortcut_find_position (impl, combo_selected);
1949       if (pos != -1)
1950         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), 
1951                                   pos);
1952       gtk_file_path_free (combo_selected);
1953     }
1954   
1955   impl->changing_folder = old_changing_folders;
1956
1957   profile_end ("end", NULL);
1958 }
1959
1960 /* Appends a separator and a row to the shortcuts list for the current folder */
1961 static void
1962 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1963 {
1964   int pos;
1965   gboolean success;
1966
1967   g_assert (!impl->shortcuts_current_folder_active);
1968
1969   success = TRUE;
1970
1971   g_assert (impl->current_folder != NULL);
1972
1973   pos = shortcut_find_position (impl, impl->current_folder);
1974   if (pos == -1)
1975     {
1976       GtkFileSystemVolume *volume;
1977       GtkFilePath *base_path;
1978
1979       /* Separator */
1980
1981       shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1982
1983       /* Item */
1984
1985       pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1986
1987       volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1988       if (volume)
1989         base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1990       else
1991         base_path = NULL;
1992
1993       if (base_path &&
1994           strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1995         {
1996           shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
1997         }
1998       else
1999         {
2000           shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2001           if (volume)
2002             gtk_file_system_volume_free (impl->file_system, volume);
2003         }
2004
2005       if (base_path)
2006         gtk_file_path_free (base_path);
2007     }
2008   else if (impl->save_folder_combo != NULL)
2009     gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2010 }
2011
2012 /* Updates the current folder row in the shortcuts model */
2013 static void
2014 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2015 {
2016   int pos;
2017
2018   pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2019
2020   if (impl->shortcuts_current_folder_active)
2021     {
2022       shortcuts_remove_rows (impl, pos, 2);
2023       impl->shortcuts_current_folder_active = FALSE;
2024     }
2025
2026   shortcuts_add_current_folder (impl);
2027 }
2028
2029 /* Filter function used for the shortcuts filter model */
2030 static gboolean
2031 shortcuts_filter_cb (GtkTreeModel          *model,
2032                      GtkTreeIter           *iter,
2033                      gpointer               data)
2034 {
2035   GtkFileChooserDefault *impl;
2036   GtkTreePath *path;
2037   int pos;
2038
2039   impl = GTK_FILE_CHOOSER_DEFAULT (data);
2040
2041   path = gtk_tree_model_get_path (model, iter);
2042   if (!path)
2043     return FALSE;
2044
2045   pos = *gtk_tree_path_get_indices (path);
2046   gtk_tree_path_free (path);
2047
2048   return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2049 }
2050
2051 /* Creates the list model for shortcuts */
2052 static void
2053 shortcuts_model_create (GtkFileChooserDefault *impl)
2054 {
2055   /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2056   impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2057                                               GDK_TYPE_PIXBUF,  /* pixbuf */
2058                                               G_TYPE_STRING,    /* name */
2059                                               G_TYPE_POINTER,   /* path or volume */
2060                                               G_TYPE_BOOLEAN,   /* is the previous column a volume? */
2061                                               G_TYPE_BOOLEAN,   /* removable */
2062                                               G_TYPE_BOOLEAN,   /* pixbuf cell visibility */
2063                                               G_TYPE_POINTER);   /* GtkFileSystemHandle */
2064
2065   if (impl->file_system)
2066     {
2067       shortcuts_append_home (impl);
2068       shortcuts_append_desktop (impl);
2069       shortcuts_add_volumes (impl);
2070     }
2071
2072   impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
2073                                                              GTK_TREE_MODEL (impl->shortcuts_model),
2074                                                              NULL);
2075
2076   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2077                                           shortcuts_filter_cb,
2078                                           impl,
2079                                           NULL);
2080 }
2081
2082 /* Callback used when the "New Folder" button is clicked */
2083 static void
2084 new_folder_button_clicked (GtkButton             *button,
2085                            GtkFileChooserDefault *impl)
2086 {
2087   GtkTreeIter iter;
2088   GtkTreePath *path;
2089
2090   if (!impl->browse_files_model)
2091     return; /* FIXME: this sucks.  Disable the New Folder button or something. */
2092
2093   /* Prevent button from being clicked twice */
2094   gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2095
2096   _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2097
2098   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2099   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2100                                 path, impl->list_name_column,
2101                                 FALSE, 0.0, 0.0);
2102
2103   g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2104   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2105                             path,
2106                             impl->list_name_column,
2107                             TRUE);
2108
2109   gtk_tree_path_free (path);
2110 }
2111
2112 static void
2113 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2114                               const GtkFilePath   *path,
2115                               const GError        *error,
2116                               gpointer             data)
2117 {
2118   gboolean cancelled = handle->cancelled;
2119   GtkFileChooserDefault *impl = data;
2120
2121   if (!g_slist_find (impl->pending_handles, handle))
2122     goto out;
2123
2124   impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2125
2126   if (cancelled)
2127     goto out;
2128
2129   if (!error)
2130     change_folder_and_display_error (impl, path);
2131   else
2132     error_creating_folder_dialog (impl, path, g_error_copy (error));
2133
2134  out:
2135   g_object_unref (impl);
2136   g_object_unref (handle);
2137 }
2138
2139 /* Idle handler for creating a new folder after editing its name cell, or for
2140  * canceling the editing.
2141  */
2142 static gboolean
2143 edited_idle_cb (GtkFileChooserDefault *impl)
2144 {
2145   GDK_THREADS_ENTER ();
2146   
2147   g_source_destroy (impl->edited_idle);
2148   impl->edited_idle = NULL;
2149
2150   _gtk_file_system_model_remove_editable (impl->browse_files_model);
2151   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2152
2153   gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2154
2155   if (impl->edited_new_text) /* not cancelled? */
2156     {
2157       GError *error;
2158       GtkFilePath *file_path;
2159
2160       error = NULL;
2161       file_path = gtk_file_system_make_path (impl->file_system,
2162                                              impl->current_folder,
2163                                              impl->edited_new_text,
2164                                              &error);
2165       if (file_path)
2166         {
2167           GtkFileSystemHandle *handle;
2168
2169           handle = gtk_file_system_create_folder (impl->file_system, file_path,
2170                                                   edited_idle_create_folder_cb,
2171                                                   g_object_ref (impl));
2172           impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2173
2174           gtk_file_path_free (file_path);
2175         }
2176       else
2177         error_creating_folder_dialog (impl, file_path, error);
2178
2179       g_free (impl->edited_new_text);
2180       impl->edited_new_text = NULL;
2181     }
2182
2183   GDK_THREADS_LEAVE ();
2184
2185   return FALSE;
2186 }
2187
2188 static void
2189 queue_edited_idle (GtkFileChooserDefault *impl,
2190                    const gchar           *new_text)
2191 {
2192   /* We create the folder in an idle handler so that we don't modify the tree
2193    * just now.
2194    */
2195
2196   if (!impl->edited_idle)
2197     {
2198       impl->edited_idle = g_idle_source_new ();
2199       g_source_set_closure (impl->edited_idle,
2200                             g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2201                                                    G_OBJECT (impl)));
2202       g_source_attach (impl->edited_idle, NULL);
2203     }
2204
2205   g_free (impl->edited_new_text);
2206   impl->edited_new_text = g_strdup (new_text);
2207 }
2208
2209 /* Callback used from the text cell renderer when the new folder is named */
2210 static void
2211 renderer_edited_cb (GtkCellRendererText   *cell_renderer_text,
2212                     const gchar           *path,
2213                     const gchar           *new_text,
2214                     GtkFileChooserDefault *impl)
2215 {
2216  /* work around bug #154921 */
2217   g_object_set (cell_renderer_text, 
2218                 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2219    queue_edited_idle (impl, new_text);
2220 }
2221
2222 /* Callback used from the text cell renderer when the new folder edition gets
2223  * canceled.
2224  */
2225 static void
2226 renderer_editing_canceled_cb (GtkCellRendererText   *cell_renderer_text,
2227                               GtkFileChooserDefault *impl)
2228 {
2229  /* work around bug #154921 */
2230   g_object_set (cell_renderer_text, 
2231                 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2232    queue_edited_idle (impl, NULL);
2233 }
2234
2235 /* Creates the widgets for the filter combo box */
2236 static GtkWidget *
2237 filter_create (GtkFileChooserDefault *impl)
2238 {
2239   impl->filter_combo = gtk_combo_box_new_text ();
2240   gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2241
2242   g_signal_connect (impl->filter_combo, "changed",
2243                     G_CALLBACK (filter_combo_changed), impl);
2244
2245   return impl->filter_combo;
2246 }
2247
2248 static GtkWidget *
2249 button_new (GtkFileChooserDefault *impl,
2250             const char *text,
2251             const char *stock_id,
2252             gboolean    sensitive,
2253             gboolean    show,
2254             GCallback   callback)
2255 {
2256   GtkWidget *button;
2257   GtkWidget *image;
2258
2259   button = gtk_button_new_with_mnemonic (text);
2260   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2261   gtk_button_set_image (GTK_BUTTON (button), image);
2262
2263   gtk_widget_set_sensitive (button, sensitive);
2264   g_signal_connect (button, "clicked", callback, impl);
2265
2266   if (show)
2267     gtk_widget_show (button);
2268
2269   return button;
2270 }
2271
2272 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2273 static int
2274 shortcut_find_position (GtkFileChooserDefault *impl,
2275                         const GtkFilePath     *path)
2276 {
2277   GtkTreeIter iter;
2278   int i;
2279   int current_folder_separator_idx;
2280
2281   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2282     return -1;
2283
2284   current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2285
2286 #if 0
2287   /* FIXME: is this still needed? */
2288   if (current_folder_separator_idx >= impl->shortcuts_model->length)
2289     return -1;
2290 #endif
2291
2292   for (i = 0; i < current_folder_separator_idx; i++)
2293     {
2294       gpointer col_data;
2295       gboolean is_volume;
2296
2297       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2298                           SHORTCUTS_COL_DATA, &col_data,
2299                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
2300                           -1);
2301
2302       if (col_data)
2303         {
2304           if (is_volume)
2305             {
2306               GtkFileSystemVolume *volume;
2307               GtkFilePath *base_path;
2308               gboolean exists;
2309
2310               volume = col_data;
2311               base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2312
2313               exists = base_path && strcmp (gtk_file_path_get_string (path),
2314                                             gtk_file_path_get_string (base_path)) == 0;
2315               g_free (base_path);
2316
2317               if (exists)
2318                 return i;
2319             }
2320           else
2321             {
2322               GtkFilePath *model_path;
2323
2324               model_path = col_data;
2325
2326               if (model_path && gtk_file_path_compare (model_path, path) == 0)
2327                 return i;
2328             }
2329         }
2330
2331       if (i < current_folder_separator_idx - 1)
2332         {
2333           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2334             g_assert_not_reached ();
2335         }
2336     }
2337
2338   return -1;
2339 }
2340
2341 /* Tries to add a bookmark from a path name */
2342 static gboolean
2343 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2344                                   const GtkFilePath     *path,
2345                                   int                    pos)
2346 {
2347   GError *error;
2348
2349   g_return_val_if_fail (path != NULL, FALSE);
2350  
2351   if (shortcut_find_position (impl, path) != -1)
2352     return FALSE;
2353
2354   error = NULL;
2355   if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2356     {
2357       error_adding_bookmark_dialog (impl, path, error);
2358       return FALSE;
2359     }
2360
2361   return TRUE;
2362 }
2363
2364 static void
2365 add_bookmark_foreach_cb (GtkTreeModel *model,
2366                          GtkTreePath  *path,
2367                          GtkTreeIter  *iter,
2368                          gpointer      data)
2369 {
2370   GtkFileChooserDefault *impl;
2371   GtkFileSystemModel *fs_model;
2372   GtkTreeIter child_iter;
2373   const GtkFilePath *file_path;
2374
2375   impl = (GtkFileChooserDefault *) data;
2376
2377   fs_model = impl->browse_files_model;
2378   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2379
2380   file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2381   shortcuts_add_bookmark_from_path (impl, file_path, -1);
2382 }
2383
2384 /* Adds a bookmark from the currently selected item in the file list */
2385 static void
2386 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2387 {
2388   GtkTreeSelection *selection;
2389
2390   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2391
2392   if (gtk_tree_selection_count_selected_rows (selection) == 0)
2393     shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2394   else
2395     gtk_tree_selection_selected_foreach (selection,
2396                                          add_bookmark_foreach_cb,
2397                                          impl);
2398 }
2399
2400 /* Callback used when the "Add bookmark" button is clicked */
2401 static void
2402 add_bookmark_button_clicked_cb (GtkButton *button,
2403                                 GtkFileChooserDefault *impl)
2404 {
2405   bookmarks_add_selected_folder (impl);
2406 }
2407
2408 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2409  * returns FALSE if no shortcut is selected.
2410  */
2411 static gboolean
2412 shortcuts_get_selected (GtkFileChooserDefault *impl,
2413                         GtkTreeIter           *iter)
2414 {
2415   GtkTreeSelection *selection;
2416   GtkTreeIter parent_iter;
2417
2418   if (!impl->browse_shortcuts_tree_view)
2419     return FALSE;
2420
2421   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2422
2423   if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2424     return FALSE;
2425
2426   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2427                                                     iter,
2428                                                     &parent_iter);
2429   return TRUE;
2430 }
2431
2432 /* Removes the selected bookmarks */
2433 static void
2434 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2435 {
2436   GtkTreeIter iter;
2437   gpointer col_data;
2438   GtkFilePath *path;
2439   gboolean removable;
2440   GError *error;
2441
2442   if (!shortcuts_get_selected (impl, &iter))
2443     return;
2444
2445   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2446                       SHORTCUTS_COL_DATA, &col_data,
2447                       SHORTCUTS_COL_REMOVABLE, &removable,
2448                       -1);
2449   g_assert (col_data != NULL);
2450
2451   if (!removable)
2452     return;
2453
2454   path = col_data;
2455
2456   error = NULL;
2457   if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2458     error_removing_bookmark_dialog (impl, path, error);
2459 }
2460
2461 /* Callback used when the "Remove bookmark" button is clicked */
2462 static void
2463 remove_bookmark_button_clicked_cb (GtkButton *button,
2464                                    GtkFileChooserDefault *impl)
2465 {
2466   remove_selected_bookmarks (impl);
2467 }
2468
2469 struct selection_check_closure {
2470   GtkFileChooserDefault *impl;
2471   int num_selected;
2472   gboolean all_files;
2473   gboolean all_folders;
2474 };
2475
2476 /* Used from gtk_tree_selection_selected_foreach() */
2477 static void
2478 selection_check_foreach_cb (GtkTreeModel *model,
2479                             GtkTreePath  *path,
2480                             GtkTreeIter  *iter,
2481                             gpointer      data)
2482 {
2483   struct selection_check_closure *closure;
2484   GtkTreeIter child_iter;
2485   const GtkFileInfo *info;
2486   gboolean is_folder;
2487
2488   closure = data;
2489   closure->num_selected++;
2490
2491   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2492
2493   info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2494   is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2495
2496   closure->all_folders = closure->all_folders && is_folder;
2497   closure->all_files = closure->all_files && !is_folder;
2498 }
2499
2500 /* Checks whether the selected items in the file list are all files or all folders */
2501 static void
2502 selection_check (GtkFileChooserDefault *impl,
2503                  gint                  *num_selected,
2504                  gboolean              *all_files,
2505                  gboolean              *all_folders)
2506 {
2507   struct selection_check_closure closure;
2508   GtkTreeSelection *selection;
2509
2510   closure.impl = impl;
2511   closure.num_selected = 0;
2512   closure.all_files = TRUE;
2513   closure.all_folders = TRUE;
2514
2515   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2516   gtk_tree_selection_selected_foreach (selection,
2517                                        selection_check_foreach_cb,
2518                                        &closure);
2519
2520   g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2521
2522   if (num_selected)
2523     *num_selected = closure.num_selected;
2524
2525   if (all_files)
2526     *all_files = closure.all_files;
2527
2528   if (all_folders)
2529     *all_folders = closure.all_folders;
2530 }
2531
2532 struct get_selected_path_closure {
2533   GtkFileChooserDefault *impl;
2534   const GtkFilePath *path;
2535 };
2536
2537 static void
2538 get_selected_path_foreach_cb (GtkTreeModel *model,
2539                               GtkTreePath  *path,
2540                               GtkTreeIter  *iter,
2541                               gpointer      data)
2542 {
2543   struct get_selected_path_closure *closure;
2544   GtkTreeIter child_iter;
2545
2546   closure = data;
2547
2548   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2549   closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2550 }
2551
2552 /* Returns a selected path from the file list */
2553 static const GtkFilePath *
2554 get_selected_path (GtkFileChooserDefault *impl)
2555 {
2556   struct get_selected_path_closure closure;
2557   GtkTreeSelection *selection;
2558
2559   closure.impl = impl;
2560   closure.path = NULL;
2561
2562   selection =  gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2563   gtk_tree_selection_selected_foreach (selection,
2564                                        get_selected_path_foreach_cb,
2565                                        &closure);
2566
2567   return closure.path;
2568 }
2569
2570 typedef struct {
2571   GtkFileChooserDefault *impl;
2572   gchar *tip;
2573 } UpdateTooltipData;
2574
2575 static void 
2576 update_tooltip (GtkTreeModel      *model,
2577                 GtkTreePath       *path,
2578                 GtkTreeIter       *iter,
2579                 gpointer           data)
2580 {
2581   UpdateTooltipData *udata = data;
2582   GtkTreeIter child_iter;
2583   const GtkFileInfo *info;
2584
2585   if (udata->tip == NULL)
2586     {
2587       gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2588                                                       &child_iter,
2589                                                       iter);
2590   
2591       info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2592       udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2593                                     gtk_file_info_get_display_name (info));
2594     }
2595 }
2596
2597
2598 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2599  * if there are no selected items *and* the current folder is not in the
2600  * bookmarks list.  De-sensitize the button otherwise.
2601  */
2602 static void
2603 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2604 {
2605   gint num_selected;
2606   gboolean all_folders;
2607   gboolean active;
2608   gchar *tip;
2609
2610   selection_check (impl, &num_selected, NULL, &all_folders);
2611
2612   if (num_selected == 0)
2613     active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2614   else if (num_selected == 1)
2615     {
2616       const GtkFilePath *path;
2617
2618       path = get_selected_path (impl);
2619       active = all_folders && (shortcut_find_position (impl, path) == -1);
2620     }
2621   else
2622     active = all_folders;
2623
2624   gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2625
2626   if (impl->browse_files_popup_menu_add_shortcut_item)
2627     gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2628                               (num_selected == 0) ? FALSE : active);
2629
2630   if (active)
2631     {
2632       if (num_selected == 0)
2633         tip = g_strdup_printf (_("Add the current folder to the bookmarks"));    
2634       else if (num_selected > 1)
2635         tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2636       else
2637         {
2638           GtkTreeSelection *selection;
2639           UpdateTooltipData data;
2640           
2641           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2642           data.impl = impl;
2643           data.tip = NULL;
2644           gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2645           tip = data.tip;
2646           
2647         }
2648       gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2649       g_free (tip);
2650     }
2651 }
2652
2653 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2654  * bookmark row is selected in the shortcuts tree.
2655  */
2656 static void
2657 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2658 {
2659   GtkTreeIter iter;
2660   gboolean removable = FALSE;
2661   gchar *name = NULL;
2662   
2663   if (shortcuts_get_selected (impl, &iter))
2664     gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2665                         SHORTCUTS_COL_REMOVABLE, &removable,
2666                         SHORTCUTS_COL_NAME, &name,
2667                         -1);
2668
2669   gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2670
2671   if (removable)
2672     {
2673       gchar *tip;
2674
2675       tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2676       gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2677                             tip, NULL);
2678       g_free (tip);
2679     }
2680
2681   g_free (name);
2682 }
2683
2684 static void
2685 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2686 {
2687   GtkTreeIter iter;
2688   gboolean removable = FALSE;
2689
2690   if (impl->browse_shortcuts_popup_menu == NULL)
2691     return;
2692
2693   if (shortcuts_get_selected (impl, &iter))
2694     gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2695                         SHORTCUTS_COL_REMOVABLE, &removable,
2696                         -1);
2697
2698   gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2699   gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2700 }
2701
2702 /* GtkWidget::drag-begin handler for the shortcuts list. */
2703 static void
2704 shortcuts_drag_begin_cb (GtkWidget             *widget,
2705                          GdkDragContext        *context,
2706                          GtkFileChooserDefault *impl)
2707 {
2708 #if 0
2709   impl->shortcuts_drag_context = g_object_ref (context);
2710 #endif
2711 }
2712
2713 #if 0
2714 /* Removes the idle handler for outside drags */
2715 static void
2716 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2717 {
2718   if (!impl->shortcuts_drag_outside_idle)
2719     return;
2720
2721   g_source_destroy (impl->shortcuts_drag_outside_idle);
2722   impl->shortcuts_drag_outside_idle = NULL;
2723 }
2724 #endif
2725
2726 /* GtkWidget::drag-end handler for the shortcuts list. */
2727 static void
2728 shortcuts_drag_end_cb (GtkWidget             *widget,
2729                        GdkDragContext        *context,
2730                        GtkFileChooserDefault *impl)
2731 {
2732 #if 0
2733   g_object_unref (impl->shortcuts_drag_context);
2734
2735   shortcuts_cancel_drag_outside_idle (impl);
2736
2737   if (!impl->shortcuts_drag_outside)
2738     return;
2739
2740   gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2741
2742   impl->shortcuts_drag_outside = FALSE;
2743 #endif
2744 }
2745
2746 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2747 static void
2748 shortcuts_drag_data_delete_cb (GtkWidget             *widget,
2749                                GdkDragContext        *context,
2750                                GtkFileChooserDefault *impl)
2751 {
2752   g_signal_stop_emission_by_name (widget, "drag_data_delete");
2753 }
2754
2755 #if 0
2756 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2757  * deleted or not.
2758  */
2759 static void
2760 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2761                                   gboolean               delete)
2762 {
2763   GtkTreeView *tree_view;
2764   GtkTreeIter iter;
2765   GtkTreePath *path;
2766   GdkPixmap *row_pixmap;
2767   GdkBitmap *mask;
2768   int row_pixmap_y;
2769   int cell_y;
2770
2771   tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2772
2773   /* Find the selected path and get its drag pixmap */
2774
2775   if (!shortcuts_get_selected (impl, &iter))
2776     g_assert_not_reached ();
2777
2778   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2779
2780   row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2781   gtk_tree_path_free (path);
2782
2783   mask = NULL;
2784   row_pixmap_y = 0;
2785
2786   if (delete)
2787     {
2788       GdkPixbuf *pixbuf;
2789
2790       pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2791                                        GTK_STOCK_DELETE,
2792                                        GTK_ICON_SIZE_DND,
2793                                        NULL);
2794       if (pixbuf)
2795         {
2796           GdkPixmap *composite;
2797           int row_pixmap_width, row_pixmap_height;
2798           int pixbuf_width, pixbuf_height;
2799           int composite_width, composite_height;
2800           int pixbuf_x, pixbuf_y;
2801           GdkGC *gc, *mask_gc;
2802           GdkColor color;
2803           GdkBitmap *pixbuf_mask;
2804
2805           /* Create pixmap and mask for composite image */
2806
2807           gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2808           pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2809           pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2810
2811           composite_width = MAX (row_pixmap_width, pixbuf_width);
2812           composite_height = MAX (row_pixmap_height, pixbuf_height);
2813
2814           row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2815
2816           if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2817             pixbuf_x = 0;
2818           else
2819             pixbuf_x = composite_width - pixbuf_width;
2820
2821           pixbuf_y = (composite_height - pixbuf_height) / 2;
2822
2823           composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2824           gc = gdk_gc_new (composite);
2825
2826           mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2827           mask_gc = gdk_gc_new (mask);
2828           color.pixel = 0;
2829           gdk_gc_set_foreground (mask_gc, &color);
2830           gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2831
2832           color.red = 0xffff;
2833           color.green = 0xffff;
2834           color.blue = 0xffff;
2835           gdk_gc_set_rgb_fg_color (gc, &color);
2836           gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2837
2838           /* Composite the row pixmap and the pixbuf */
2839
2840           gdk_pixbuf_render_pixmap_and_mask_for_colormap
2841             (pixbuf,
2842              gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2843              NULL, &pixbuf_mask, 128);
2844           gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2845                              0, 0,
2846                              pixbuf_x, pixbuf_y,
2847                              pixbuf_width, pixbuf_height);
2848           g_object_unref (pixbuf_mask);
2849
2850           gdk_draw_drawable (composite, gc, row_pixmap,
2851                              0, 0,
2852                              0, row_pixmap_y,
2853                              row_pixmap_width, row_pixmap_height);
2854           color.pixel = 1;
2855           gdk_gc_set_foreground (mask_gc, &color);
2856           gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2857
2858           gdk_draw_pixbuf (composite, gc, pixbuf,
2859                            0, 0,
2860                            pixbuf_x, pixbuf_y,
2861                            pixbuf_width, pixbuf_height,
2862                            GDK_RGB_DITHER_MAX,
2863                            0, 0);
2864
2865           g_object_unref (pixbuf);
2866           g_object_unref (row_pixmap);
2867
2868           row_pixmap = composite;
2869         }
2870     }
2871
2872   /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2873
2874   gtk_tree_view_get_path_at_pos (tree_view,
2875                                  tree_view->priv->press_start_x,
2876                                  tree_view->priv->press_start_y,
2877                                  NULL,
2878                                  NULL,
2879                                  NULL,
2880                                  &cell_y);
2881
2882   gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2883                             gdk_drawable_get_colormap (row_pixmap),
2884                             row_pixmap,
2885                             mask,
2886                             tree_view->priv->press_start_x + 1,
2887                             row_pixmap_y + cell_y + 1);
2888
2889   g_object_unref (row_pixmap);
2890   if (mask)
2891     g_object_unref (mask);
2892 }
2893
2894 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2895  * handler so that we can tell apart the drag_leave event that comes right
2896  * before a drag_drop, from a normal drag_leave.  We don't want to set the
2897  * cursor nor the flag in the latter case.
2898  */
2899 static gboolean
2900 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2901 {
2902   GDK_THREADS_ENTER ();
2903   
2904   shortcuts_drag_set_delete_cursor (impl, TRUE);
2905   impl->shortcuts_drag_outside = TRUE;
2906
2907   shortcuts_cancel_drag_outside_idle (impl);
2908
2909   GDK_THREADS_LEAVE ();
2910
2911   return FALSE;
2912 }
2913 #endif
2914
2915 /* GtkWidget::drag-leave handler for the shortcuts list.  We unhighlight the
2916  * drop position.
2917  */
2918 static void
2919 shortcuts_drag_leave_cb (GtkWidget             *widget,
2920                          GdkDragContext        *context,
2921                          guint                  time_,
2922                          GtkFileChooserDefault *impl)
2923 {
2924 #if 0
2925   if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2926     {
2927       impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2928       g_source_set_closure (impl->shortcuts_drag_outside_idle,
2929                             g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2930                                                    G_OBJECT (impl)));
2931       g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2932     }
2933 #endif
2934
2935   gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2936                                    NULL,
2937                                    GTK_TREE_VIEW_DROP_BEFORE);
2938
2939   g_signal_stop_emission_by_name (widget, "drag_leave");
2940 }
2941
2942 /* Computes the appropriate row and position for dropping */
2943 static void
2944 shortcuts_compute_drop_position (GtkFileChooserDefault   *impl,
2945                                  int                      x,
2946                                  int                      y,
2947                                  GtkTreePath            **path,
2948                                  GtkTreeViewDropPosition *pos)
2949 {
2950   GtkTreeView *tree_view;
2951   GtkTreeViewColumn *column;
2952   int cell_y;
2953   GdkRectangle cell;
2954   int row;
2955   int bookmarks_index;
2956
2957   tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2958
2959   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2960
2961   if (!gtk_tree_view_get_path_at_pos (tree_view,
2962                                       x,
2963                                       y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2964                                       path,
2965                                       &column,
2966                                       NULL,
2967                                       &cell_y))
2968     {
2969       row = bookmarks_index + impl->num_bookmarks - 1;
2970       *path = gtk_tree_path_new_from_indices (row, -1);
2971       *pos = GTK_TREE_VIEW_DROP_AFTER;
2972       return;
2973     }
2974
2975   row = *gtk_tree_path_get_indices (*path);
2976   gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2977   gtk_tree_path_free (*path);
2978
2979   if (row < bookmarks_index)
2980     {
2981       row = bookmarks_index;
2982       *pos = GTK_TREE_VIEW_DROP_BEFORE;
2983     }
2984   else if (row > bookmarks_index + impl->num_bookmarks - 1)
2985     {
2986       row = bookmarks_index + impl->num_bookmarks - 1;
2987       *pos = GTK_TREE_VIEW_DROP_AFTER;
2988     }
2989   else
2990     {
2991       if (cell_y < cell.height / 2)
2992         *pos = GTK_TREE_VIEW_DROP_BEFORE;
2993       else
2994         *pos = GTK_TREE_VIEW_DROP_AFTER;
2995     }
2996
2997   *path = gtk_tree_path_new_from_indices (row, -1);
2998 }
2999
3000 /* GtkWidget::drag-motion handler for the shortcuts list.  We basically
3001  * implement the destination side of DnD by hand, due to limitations in
3002  * GtkTreeView's DnD API.
3003  */
3004 static gboolean
3005 shortcuts_drag_motion_cb (GtkWidget             *widget,
3006                           GdkDragContext        *context,
3007                           gint                   x,
3008                           gint                   y,
3009                           guint                  time_,
3010                           GtkFileChooserDefault *impl)
3011 {
3012   GtkTreePath *path;
3013   GtkTreeViewDropPosition pos;
3014   GdkDragAction action;
3015
3016 #if 0
3017   if (gtk_drag_get_source_widget (context) == widget)
3018     {
3019       shortcuts_cancel_drag_outside_idle (impl);
3020
3021       if (impl->shortcuts_drag_outside)
3022         {
3023           shortcuts_drag_set_delete_cursor (impl, FALSE);
3024           impl->shortcuts_drag_outside = FALSE;
3025         }
3026     }
3027 #endif
3028
3029   if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
3030     action = GDK_ACTION_COPY;
3031   else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
3032     action = GDK_ACTION_MOVE;
3033   else
3034     {
3035       action = 0;
3036       goto out;
3037     }
3038
3039   shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3040   gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3041   gtk_tree_path_free (path);
3042
3043  out:
3044
3045   g_signal_stop_emission_by_name (widget, "drag_motion");
3046
3047   if (action != 0)
3048     {
3049       gdk_drag_status (context, action, time_);
3050       return TRUE;
3051     }
3052   else
3053     return FALSE;
3054 }
3055
3056 /* GtkWidget::drag-drop handler for the shortcuts list. */
3057 static gboolean
3058 shortcuts_drag_drop_cb (GtkWidget             *widget,
3059                         GdkDragContext        *context,
3060                         gint                   x,
3061                         gint                   y,
3062                         guint                  time_,
3063                         GtkFileChooserDefault *impl)
3064 {
3065 #if 0
3066   shortcuts_cancel_drag_outside_idle (impl);
3067 #endif
3068
3069   g_signal_stop_emission_by_name (widget, "drag_drop");
3070   return TRUE;
3071 }
3072
3073 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3074 static void
3075 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3076                      const char            *data,
3077                      int                    position)
3078 {
3079   gchar **uris;
3080   gint i;
3081
3082   uris = g_uri_list_extract_uris (data);
3083
3084   for (i = 0; uris[i]; i++)
3085     {
3086       char *uri;
3087       GtkFilePath *path;
3088
3089       uri = uris[i];
3090       path = gtk_file_system_uri_to_path (impl->file_system, uri);
3091
3092       if (path)
3093         {
3094           if (shortcuts_add_bookmark_from_path (impl, path, position))
3095             position++;
3096
3097           gtk_file_path_free (path);
3098         }
3099       else
3100         {
3101           GError *error;
3102
3103           g_set_error (&error,
3104                        GTK_FILE_CHOOSER_ERROR,
3105                        GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3106                        _("Could not add a bookmark for '%s' "
3107                          "because it is an invalid path name."),
3108                        uri);
3109           error_adding_bookmark_dialog (impl, path, error);
3110         }
3111     }
3112
3113   g_strfreev (uris);
3114 }
3115
3116 /* Reorders the selected bookmark to the specified position */
3117 static void
3118 shortcuts_reorder (GtkFileChooserDefault *impl,
3119                    int                    new_position)
3120 {
3121   GtkTreeIter iter;
3122   gpointer col_data;
3123   gboolean is_volume;
3124   GtkTreePath *path;
3125   int old_position;
3126   int bookmarks_index;
3127   const GtkFilePath *file_path;
3128   GtkFilePath *file_path_copy;
3129   GError *error;
3130   gchar *name;
3131
3132   /* Get the selected path */
3133
3134   if (!shortcuts_get_selected (impl, &iter))
3135     g_assert_not_reached ();
3136
3137   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3138   old_position = *gtk_tree_path_get_indices (path);
3139   gtk_tree_path_free (path);
3140
3141   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3142   old_position -= bookmarks_index;
3143   g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3144
3145   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3146                       SHORTCUTS_COL_NAME, &name,
3147                       SHORTCUTS_COL_DATA, &col_data,
3148                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
3149                       -1);
3150   g_assert (col_data != NULL);
3151   g_assert (!is_volume);
3152   
3153   file_path = col_data;
3154   file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3155
3156   /* Remove the path from the old position and insert it in the new one */
3157
3158   if (new_position > old_position)
3159     new_position--;
3160
3161   if (old_position == new_position)
3162     goto out;
3163
3164   error = NULL;
3165   if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3166     {
3167       shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3168       gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3169     }
3170   else
3171     error_adding_bookmark_dialog (impl, file_path_copy, error);
3172
3173  out:
3174
3175   gtk_file_path_free (file_path_copy);
3176 }
3177
3178 /* Callback used when we get the drag data for the bookmarks list.  We add the
3179  * received URIs as bookmarks if they are folders.
3180  */
3181 static void
3182 shortcuts_drag_data_received_cb (GtkWidget          *widget,
3183                                  GdkDragContext     *context,
3184                                  gint                x,
3185                                  gint                y,
3186                                  GtkSelectionData   *selection_data,
3187                                  guint               info,
3188                                  guint               time_,
3189                                  gpointer            data)
3190 {
3191   GtkFileChooserDefault *impl;
3192   GtkTreePath *tree_path;
3193   GtkTreeViewDropPosition tree_pos;
3194   int position;
3195   int bookmarks_index;
3196
3197   impl = GTK_FILE_CHOOSER_DEFAULT (data);
3198
3199   /* Compute position */
3200
3201   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3202
3203   shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3204   position = *gtk_tree_path_get_indices (tree_path);
3205   gtk_tree_path_free (tree_path);
3206
3207   if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3208     position++;
3209
3210   g_assert (position >= bookmarks_index);
3211   position -= bookmarks_index;
3212
3213   if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3214     shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3215   else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3216     shortcuts_reorder (impl, position);
3217
3218   g_signal_stop_emission_by_name (widget, "drag_data_received");
3219 }
3220
3221 /* Callback used when the selection in the shortcuts tree changes */
3222 static void
3223 shortcuts_selection_changed_cb (GtkTreeSelection      *selection,
3224                                 GtkFileChooserDefault *impl)
3225 {
3226   bookmarks_check_remove_sensitivity (impl);
3227   shortcuts_check_popup_sensitivity (impl);
3228 }
3229
3230 static gboolean
3231 shortcuts_row_separator_func (GtkTreeModel *model,
3232                               GtkTreeIter  *iter,
3233                               gpointer      data)
3234 {
3235   gint column = GPOINTER_TO_INT (data);
3236   gchar *text;
3237
3238   gtk_tree_model_get (model, iter, column, &text, -1);
3239   
3240   if (!text)
3241     return TRUE;
3242
3243   g_free (text);
3244
3245   return FALSE;
3246 }
3247
3248 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3249  * keypresses before the TreeView gets them.
3250  */
3251 static gboolean
3252 tree_view_keybinding_cb (GtkWidget             *tree_view,
3253                          GdkEventKey           *event,
3254                          GtkFileChooserDefault *impl)
3255 {
3256   if ((event->keyval == GDK_slash
3257        || event->keyval == GDK_KP_Divide
3258 #ifdef G_OS_UNIX
3259        || event->keyval == GDK_asciitilde
3260 #endif
3261        ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3262     {
3263       location_popup_handler (impl, event->string);
3264       return TRUE;
3265     }
3266
3267   return FALSE;
3268 }
3269
3270 /* Callback used when the file list's popup menu is detached */
3271 static void
3272 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3273                                 GtkMenu   *menu)
3274 {
3275   GtkFileChooserDefault *impl;
3276   
3277   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3278   g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3279
3280   impl->browse_shortcuts_popup_menu = NULL;
3281   impl->browse_shortcuts_popup_menu_remove_item = NULL;
3282   impl->browse_shortcuts_popup_menu_rename_item = NULL;
3283 }
3284
3285 static void
3286 remove_shortcut_cb (GtkMenuItem           *item,
3287                     GtkFileChooserDefault *impl)
3288 {
3289   remove_selected_bookmarks (impl);
3290 }
3291
3292 /* Rename the selected bookmark */
3293 static void
3294 rename_selected_bookmark (GtkFileChooserDefault *impl)
3295 {
3296   GtkTreeIter iter;
3297   GtkTreePath *path;
3298   GtkTreeViewColumn *column;
3299   GtkCellRenderer *cell;
3300   GList *renderers;
3301
3302   if (shortcuts_get_selected (impl, &iter))
3303     {
3304       path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3305       column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3306       renderers = gtk_tree_view_column_get_cell_renderers (column);
3307       cell = g_list_nth_data (renderers, 1);
3308       g_list_free (renderers);
3309       g_object_set (cell, "editable", TRUE, NULL);
3310       gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3311                                         path, column, cell, TRUE);
3312       gtk_tree_path_free (path);
3313     }
3314 }
3315
3316 static void
3317 rename_shortcut_cb (GtkMenuItem           *item,
3318                     GtkFileChooserDefault *impl)
3319 {
3320   rename_selected_bookmark (impl);
3321 }
3322
3323 /* Constructs the popup menu for the file list if needed */
3324 static void
3325 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3326 {
3327   GtkWidget *item;
3328
3329   if (impl->browse_shortcuts_popup_menu)
3330     return;
3331
3332   impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3333   gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3334                              impl->browse_shortcuts_tree_view,
3335                              shortcuts_popup_menu_detach_cb);
3336
3337   item = gtk_image_menu_item_new_with_label (_("Remove"));
3338   impl->browse_shortcuts_popup_menu_remove_item = item;
3339   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3340                                  gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3341   g_signal_connect (item, "activate",
3342                     G_CALLBACK (remove_shortcut_cb), impl);
3343   gtk_widget_show (item);
3344   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3345
3346   item = gtk_menu_item_new_with_label (_("Rename..."));
3347   impl->browse_shortcuts_popup_menu_rename_item = item;
3348   g_signal_connect (item, "activate",
3349                     G_CALLBACK (rename_shortcut_cb), impl);
3350   gtk_widget_show (item);
3351   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3352
3353   shortcuts_check_popup_sensitivity (impl);
3354 }
3355
3356 static void
3357 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3358 {
3359   shortcuts_build_popup_menu (impl);  
3360 }
3361
3362 static void
3363 popup_position_func (GtkMenu   *menu,
3364                      gint      *x,
3365                      gint      *y,
3366                      gboolean  *push_in,
3367                      gpointer   user_data);
3368
3369 static void
3370 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3371                       GdkEventButton        *event)
3372 {
3373   shortcuts_update_popup_menu (impl);
3374   if (event)
3375     gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3376                     NULL, NULL, NULL, NULL,
3377                     event->button, event->time);
3378   else
3379     {
3380       gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3381                       NULL, NULL,
3382                       popup_position_func, impl->browse_shortcuts_tree_view,
3383                       0, GDK_CURRENT_TIME);
3384       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3385                                    FALSE);
3386     }
3387 }
3388
3389 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3390 static gboolean
3391 shortcuts_popup_menu_cb (GtkWidget *widget,
3392                          GtkFileChooserDefault *impl)
3393 {
3394   shortcuts_popup_menu (impl, NULL);
3395   return TRUE;
3396 }
3397
3398 /* Callback used when a button is pressed on the shortcuts list.  
3399  * We trap button 3 to bring up a popup menu.
3400  */
3401 static gboolean
3402 shortcuts_button_press_event_cb (GtkWidget             *widget,
3403                                  GdkEventButton        *event,
3404                                  GtkFileChooserDefault *impl)
3405 {
3406   static gboolean in_press = FALSE;
3407   gboolean handled;
3408
3409   if (in_press)
3410     return FALSE;
3411
3412   if (event->button != 3)
3413     return FALSE;
3414
3415   in_press = TRUE;
3416   handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3417   in_press = FALSE;
3418
3419   if (!handled)
3420     return FALSE;
3421
3422   shortcuts_popup_menu (impl, event);
3423   return TRUE;
3424 }
3425
3426 static void
3427 shortcuts_edited (GtkCellRenderer       *cell,
3428                   gchar                 *path_string,
3429                   gchar                 *new_text,
3430                   GtkFileChooserDefault *impl)
3431 {
3432   GtkTreePath *path;
3433   GtkTreeIter iter;
3434   GtkFilePath *shortcut;
3435
3436   g_object_set (cell, "editable", FALSE, NULL);
3437
3438   path = gtk_tree_path_new_from_string (path_string);
3439   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3440     g_assert_not_reached ();
3441
3442   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3443                       SHORTCUTS_COL_DATA, &shortcut,
3444                       -1);
3445   gtk_tree_path_free (path);
3446   
3447   gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3448 }
3449
3450 static void
3451 shortcuts_editing_canceled (GtkCellRenderer       *cell,
3452                             GtkFileChooserDefault *impl)
3453 {
3454   g_object_set (cell, "editable", FALSE, NULL);
3455 }
3456
3457 /* Creates the widgets for the shortcuts and bookmarks tree */
3458 static GtkWidget *
3459 shortcuts_list_create (GtkFileChooserDefault *impl)
3460 {
3461   GtkWidget *swin;
3462   GtkTreeSelection *selection;
3463   GtkTreeViewColumn *column;
3464   GtkCellRenderer *renderer;
3465
3466   /* Scrolled window */
3467
3468   swin = gtk_scrolled_window_new (NULL, NULL);
3469   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3470                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3471   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3472                                        GTK_SHADOW_IN);
3473   gtk_widget_show (swin);
3474
3475   /* Tree */
3476
3477   impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3478 #ifdef PROFILE_FILE_CHOOSER
3479   g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3480 #endif
3481   g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3482                     G_CALLBACK (tree_view_keybinding_cb), impl);
3483   g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3484                     G_CALLBACK (shortcuts_popup_menu_cb), impl);
3485   g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3486                     G_CALLBACK (shortcuts_button_press_event_cb), impl);
3487   /* Accessible object name for the file chooser's shortcuts pane */
3488   atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3489
3490   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3491
3492   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3493                                           GDK_BUTTON1_MASK,
3494                                           shortcuts_source_targets,
3495                                           num_shortcuts_source_targets,
3496                                           GDK_ACTION_MOVE);
3497
3498   gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3499                      GTK_DEST_DEFAULT_ALL,
3500                      shortcuts_dest_targets,
3501                      num_shortcuts_dest_targets,
3502                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
3503
3504   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3505   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3506   gtk_tree_selection_set_select_function (selection,
3507                                           shortcuts_select_func,
3508                                           impl, NULL);
3509
3510   g_signal_connect (selection, "changed",
3511                     G_CALLBACK (shortcuts_selection_changed_cb), impl);
3512
3513   g_signal_connect (impl->browse_shortcuts_tree_view, "row_activated",
3514                     G_CALLBACK (shortcuts_row_activated_cb), impl);
3515
3516   g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3517                     G_CALLBACK (shortcuts_key_press_event_cb), impl);
3518
3519   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3520                     G_CALLBACK (shortcuts_drag_begin_cb), impl);
3521   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3522                     G_CALLBACK (shortcuts_drag_end_cb), impl);
3523   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3524                     G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3525
3526   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3527                     G_CALLBACK (shortcuts_drag_leave_cb), impl);
3528   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3529                     G_CALLBACK (shortcuts_drag_motion_cb), impl);
3530   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3531                     G_CALLBACK (shortcuts_drag_drop_cb), impl);
3532   g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3533                     G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3534
3535   gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3536   gtk_widget_show (impl->browse_shortcuts_tree_view);
3537
3538   /* Column */
3539
3540   column = gtk_tree_view_column_new ();
3541   /* Column header for the file chooser's shortcuts pane */
3542   gtk_tree_view_column_set_title (column, _("_Places"));
3543
3544   renderer = gtk_cell_renderer_pixbuf_new ();
3545   gtk_tree_view_column_pack_start (column, renderer, FALSE);
3546   gtk_tree_view_column_set_attributes (column, renderer,
3547                                        "pixbuf", SHORTCUTS_COL_PIXBUF,
3548                                        "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3549                                        NULL);
3550
3551   renderer = gtk_cell_renderer_text_new ();
3552   g_signal_connect (renderer, "edited", 
3553                     G_CALLBACK (shortcuts_edited), impl);
3554   g_signal_connect (renderer, "editing-canceled", 
3555                     G_CALLBACK (shortcuts_editing_canceled), impl);
3556   gtk_tree_view_column_pack_start (column, renderer, TRUE);
3557   gtk_tree_view_column_set_attributes (column, renderer,
3558                                        "text", SHORTCUTS_COL_NAME,
3559                                        NULL);
3560
3561   gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3562                                         shortcuts_row_separator_func,
3563                                         GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3564                                         NULL);
3565
3566   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3567
3568   return swin;
3569 }
3570
3571 /* Creates the widgets for the shortcuts/bookmarks pane */
3572 static GtkWidget *
3573 shortcuts_pane_create (GtkFileChooserDefault *impl,
3574                        GtkSizeGroup          *size_group)
3575 {
3576   GtkWidget *vbox;
3577   GtkWidget *hbox;
3578   GtkWidget *widget;
3579
3580   vbox = gtk_vbox_new (FALSE, 6);
3581   gtk_widget_show (vbox);
3582
3583   /* Shortcuts tree */
3584
3585   widget = shortcuts_list_create (impl);
3586   gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3587
3588   /* Box for buttons */
3589
3590   hbox = gtk_hbox_new (TRUE, 6);
3591   gtk_size_group_add_widget (size_group, hbox);
3592   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3593   gtk_widget_show (hbox);
3594
3595   /* Add bookmark button */
3596
3597   impl->browse_shortcuts_add_button = button_new (impl,
3598                                                   _("_Add"),
3599                                                   GTK_STOCK_ADD,
3600                                                   FALSE,
3601                                                   TRUE,
3602                                                   G_CALLBACK (add_bookmark_button_clicked_cb));
3603   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3604   gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3605                         _("Add the selected folder to the Bookmarks"), NULL);
3606
3607   /* Remove bookmark button */
3608
3609   impl->browse_shortcuts_remove_button = button_new (impl,
3610                                                      _("_Remove"),
3611                                                      GTK_STOCK_REMOVE,
3612                                                      FALSE,
3613                                                      TRUE,
3614                                                      G_CALLBACK (remove_bookmark_button_clicked_cb));
3615   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3616   gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3617                         _("Remove the selected bookmark"), NULL);
3618
3619   return vbox;
3620 }
3621
3622 /* Handles key press events on the file list, so that we can trap Enter to
3623  * activate the default button on our own.  Also, checks to see if '/' has been
3624  * pressed.  See comment by tree_view_keybinding_cb() for more details.
3625  */
3626 static gboolean
3627 trap_activate_cb (GtkWidget   *widget,
3628                   GdkEventKey *event,
3629                   gpointer     data)
3630 {
3631   GtkFileChooserDefault *impl;
3632   int modifiers;
3633
3634   impl = (GtkFileChooserDefault *) data;
3635
3636   modifiers = gtk_accelerator_get_default_mod_mask ();
3637
3638   if ((event->keyval == GDK_slash
3639        || event->keyval == GDK_KP_Divide
3640 #ifdef G_OS_UNIX
3641        || event->keyval == GDK_asciitilde
3642 #endif
3643        ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3644     {
3645       location_popup_handler (impl, event->string);
3646       return TRUE;
3647     }
3648
3649   if ((event->keyval == GDK_Return
3650        || event->keyval == GDK_ISO_Enter
3651        || event->keyval == GDK_KP_Enter
3652        || event->keyval == GDK_space)
3653       && ((event->state & modifiers) == 0)
3654       && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3655            impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3656     {
3657       GtkWindow *window;
3658
3659       window = get_toplevel (widget);
3660       if (window
3661           && widget != window->default_widget
3662           && !(widget == window->focus_widget &&
3663                (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3664         {
3665           gtk_window_activate_default (window);
3666           return TRUE;
3667         }
3668     }
3669
3670   return FALSE;
3671 }
3672
3673 /* Callback used when the file list's popup menu is detached */
3674 static void
3675 popup_menu_detach_cb (GtkWidget *attach_widget,
3676                       GtkMenu   *menu)
3677 {
3678   GtkFileChooserDefault *impl;
3679
3680   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3681   g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3682
3683   impl->browse_files_popup_menu = NULL;
3684   impl->browse_files_popup_menu_add_shortcut_item = NULL;
3685   impl->browse_files_popup_menu_hidden_files_item = NULL;
3686 }
3687
3688 /* Callback used when the "Add to Bookmarks" menu item is activated */
3689 static void
3690 add_to_shortcuts_cb (GtkMenuItem           *item,
3691                      GtkFileChooserDefault *impl)
3692 {
3693   bookmarks_add_selected_folder (impl);
3694 }
3695
3696 /* Callback used when the "Show Hidden Files" menu item is toggled */
3697 static void
3698 show_hidden_toggled_cb (GtkCheckMenuItem      *item,
3699                         GtkFileChooserDefault *impl)
3700 {
3701   g_object_set (impl,
3702                 "show-hidden", gtk_check_menu_item_get_active (item),
3703                 NULL);
3704 }
3705
3706 /* Shows an error dialog about not being able to select a dragged file */
3707 static void
3708 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3709                                      const GtkFilePath     *path,
3710                                      GError                *error)
3711 {
3712   error_dialog (impl,
3713                 _("Could not select file"),
3714                 path, error);
3715 }
3716
3717 static void
3718 file_list_drag_data_select_uris (GtkFileChooserDefault  *impl,
3719                                  gchar                 **uris)
3720 {
3721   int i;
3722   char *uri;
3723   GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3724
3725   for (i = 1; uris[i]; i++)
3726     {
3727       GtkFilePath *path;
3728
3729       uri = uris[i];
3730       path = gtk_file_system_uri_to_path (impl->file_system, uri);
3731
3732       if (path)
3733         {
3734           GError *error = NULL;
3735
3736           gtk_file_chooser_default_select_path (chooser, path, &error);
3737           if (error)
3738             error_selecting_dragged_file_dialog (impl, path, error);
3739
3740           gtk_file_path_free (path);
3741         }
3742     }
3743 }
3744
3745 struct FileListDragData
3746 {
3747   GtkFileChooserDefault *impl;
3748   gchar **uris;
3749   GtkFilePath *path;
3750 };
3751
3752 static void
3753 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
3754                                           const GtkFileInfo   *info,
3755                                           const GError        *error,
3756                                           gpointer             user_data)
3757 {
3758   gboolean cancelled = handle->cancelled;
3759   struct FileListDragData *data = user_data;
3760   GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3761
3762   if (handle != data->impl->file_list_drag_data_received_handle)
3763     goto out;
3764
3765   data->impl->file_list_drag_data_received_handle = NULL;
3766
3767   if (cancelled || error)
3768     goto out;
3769
3770   if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3771        data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3772       data->uris[1] == 0 && !error &&
3773       gtk_file_info_get_is_folder (info))
3774     change_folder_and_display_error (data->impl, data->path);
3775   else
3776     {
3777       GError *error = NULL;
3778
3779       gtk_file_chooser_default_unselect_all (chooser);
3780       gtk_file_chooser_default_select_path (chooser, data->path, &error);
3781       if (error)
3782         error_selecting_dragged_file_dialog (data->impl, data->path, error);
3783       else
3784         browse_files_center_selected_row (data->impl);
3785     }
3786
3787   if (data->impl->select_multiple)
3788     file_list_drag_data_select_uris (data->impl, data->uris);
3789
3790 out:
3791   g_object_unref (data->impl);
3792   g_strfreev (data->uris);
3793   gtk_file_path_free (data->path);
3794   g_free (data);
3795
3796   g_object_unref (handle);
3797 }
3798
3799 static void
3800 file_list_drag_data_received_cb (GtkWidget          *widget,
3801                                  GdkDragContext     *context,
3802                                  gint                x,
3803                                  gint                y,
3804                                  GtkSelectionData   *selection_data,
3805                                  guint               info,
3806                                  guint               time_,
3807                                  gpointer            data)
3808 {
3809   GtkFileChooserDefault *impl;
3810   GtkFileChooser *chooser;
3811   gchar **uris;
3812   char *uri;
3813   GtkFilePath *path;
3814   GError *error = NULL;
3815  
3816   impl = GTK_FILE_CHOOSER_DEFAULT (data);
3817   chooser = GTK_FILE_CHOOSER (data);
3818   
3819   /* Parse the text/uri-list string, navigate to the first one */
3820   uris = g_uri_list_extract_uris ((const char *) selection_data->data);
3821   if (uris[0]) 
3822     {
3823       uri = uris[0];
3824       path = gtk_file_system_uri_to_path (impl->file_system, uri);
3825       
3826       if (path)
3827         {
3828           struct FileListDragData *data;
3829
3830           data = g_new0 (struct FileListDragData, 1);
3831           data->impl = g_object_ref (impl);
3832           data->uris = uris;
3833           data->path = path;
3834
3835           if (impl->file_list_drag_data_received_handle)
3836             gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
3837
3838           impl->file_list_drag_data_received_handle =
3839             gtk_file_system_get_info (impl->file_system, path,
3840                                       GTK_FILE_INFO_IS_FOLDER,
3841                                       file_list_drag_data_received_get_info_cb,
3842                                       data);
3843           goto out;
3844         }
3845       else
3846         {
3847           g_set_error (&error,
3848                        GTK_FILE_CHOOSER_ERROR,
3849                        GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3850                        _("Could not select file '%s' "
3851                          "because it is an invalid path name."),
3852                        uri);
3853           error_selecting_dragged_file_dialog (impl, NULL, error);
3854         }
3855
3856       if (impl->select_multiple)
3857         file_list_drag_data_select_uris (impl, uris);
3858     }
3859
3860   g_strfreev (uris);
3861
3862 out:
3863   g_signal_stop_emission_by_name (widget, "drag_data_received");
3864 }
3865
3866 /* Don't do anything with the drag_drop signal */
3867 static gboolean
3868 file_list_drag_drop_cb (GtkWidget             *widget,
3869                         GdkDragContext        *context,
3870                         gint                   x,
3871                         gint                   y,
3872                         guint                  time_,
3873                         GtkFileChooserDefault *impl)
3874 {
3875   g_signal_stop_emission_by_name (widget, "drag_drop");
3876   return TRUE;
3877 }
3878
3879 /* Disable the normal tree drag motion handler, it makes it look like you're
3880    dropping the dragged item onto a tree item */
3881 static gboolean
3882 file_list_drag_motion_cb (GtkWidget             *widget,
3883                           GdkDragContext        *context,
3884                           gint                   x,
3885                           gint                   y,
3886                           guint                  time_,
3887                           GtkFileChooserDefault *impl)
3888 {
3889   g_signal_stop_emission_by_name (widget, "drag_motion");
3890   return TRUE;
3891 }
3892
3893 /* Constructs the popup menu for the file list if needed */
3894 static void
3895 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3896 {
3897   GtkWidget *item;
3898
3899   if (impl->browse_files_popup_menu)
3900     return;
3901
3902   impl->browse_files_popup_menu = gtk_menu_new ();
3903   gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3904                              impl->browse_files_tree_view,
3905                              popup_menu_detach_cb);
3906
3907   item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3908   impl->browse_files_popup_menu_add_shortcut_item = item;
3909   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3910                                  gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3911   gtk_widget_set_sensitive (item, FALSE);
3912   g_signal_connect (item, "activate",
3913                     G_CALLBACK (add_to_shortcuts_cb), impl);
3914   gtk_widget_show (item);
3915   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3916
3917   item = gtk_separator_menu_item_new ();
3918   gtk_widget_show (item);
3919   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3920
3921   item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3922   impl->browse_files_popup_menu_hidden_files_item = item;
3923   g_signal_connect (item, "toggled",
3924                     G_CALLBACK (show_hidden_toggled_cb), impl);
3925   gtk_widget_show (item);
3926   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3927 }
3928
3929 /* Updates the popup menu for the file list, creating it if necessary */
3930 static void
3931 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3932 {
3933   file_list_build_popup_menu (impl);
3934
3935   /* The sensitivity of the Add to Bookmarks item is set in
3936    * bookmarks_check_add_sensitivity()
3937    */
3938
3939   g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3940                                    G_CALLBACK (show_hidden_toggled_cb), impl);
3941   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3942                                   impl->show_hidden);
3943   g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3944                                      G_CALLBACK (show_hidden_toggled_cb), impl);
3945 }
3946
3947 static void
3948 popup_position_func (GtkMenu   *menu,
3949                      gint      *x,
3950                      gint      *y,
3951                      gboolean  *push_in,
3952                      gpointer   user_data)
3953 {
3954   GtkWidget *widget = GTK_WIDGET (user_data);
3955   GdkScreen *screen = gtk_widget_get_screen (widget);
3956   GtkRequisition req;
3957   gint monitor_num;
3958   GdkRectangle monitor;
3959
3960   g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3961
3962   gdk_window_get_origin (widget->window, x, y);
3963
3964   gtk_widget_size_request (GTK_WIDGET (menu), &req);
3965
3966   *x += (widget->allocation.width - req.width) / 2;
3967   *y += (widget->allocation.height - req.height) / 2;
3968
3969   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3970   gtk_menu_set_monitor (menu, monitor_num);
3971   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3972
3973   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3974   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3975
3976   *push_in = FALSE;
3977 }
3978
3979 static void
3980 file_list_popup_menu (GtkFileChooserDefault *impl,
3981                       GdkEventButton        *event)
3982 {
3983   file_list_update_popup_menu (impl);
3984   if (event)
3985     gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3986                     NULL, NULL, NULL, NULL,
3987                     event->button, event->time);
3988   else
3989     {
3990       gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3991                       NULL, NULL,
3992                       popup_position_func, impl->browse_files_tree_view,
3993                       0, GDK_CURRENT_TIME);
3994       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3995                                    FALSE);
3996     }
3997
3998 }
3999
4000 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4001 static gboolean
4002 list_popup_menu_cb (GtkWidget *widget,
4003                     GtkFileChooserDefault *impl)
4004 {
4005   file_list_popup_menu (impl, NULL);
4006   return TRUE;
4007 }
4008
4009 /* Callback used when a button is pressed on the file list.  We trap button 3 to
4010  * bring up a popup menu.
4011  */
4012 static gboolean
4013 list_button_press_event_cb (GtkWidget             *widget,
4014                             GdkEventButton        *event,
4015                             GtkFileChooserDefault *impl)
4016 {
4017   static gboolean in_press = FALSE;
4018   gboolean handled;
4019
4020   if (in_press)
4021     return FALSE;
4022
4023   if (event->button != 3)
4024     return FALSE;
4025
4026   in_press = TRUE;
4027   handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4028   in_press = FALSE;
4029
4030   file_list_popup_menu (impl, event);
4031   return TRUE;
4032 }
4033
4034 /* Creates the widgets for the file list */
4035 static GtkWidget *
4036 create_file_list (GtkFileChooserDefault *impl)
4037 {
4038   GtkWidget *swin;
4039   GtkTreeSelection *selection;
4040   GtkTreeViewColumn *column;
4041   GtkCellRenderer *renderer;
4042
4043   /* Scrolled window */
4044
4045   swin = gtk_scrolled_window_new (NULL, NULL);
4046   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4047                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4048   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4049                                        GTK_SHADOW_IN);
4050
4051   /* Tree/list view */
4052
4053   impl->browse_files_tree_view = gtk_tree_view_new ();
4054 #ifdef PROFILE_FILE_CHOOSER
4055   g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4056 #endif
4057   g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4058   atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4059
4060   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4061   gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4062
4063   gtk_drag_dest_set (impl->browse_files_tree_view,
4064                      GTK_DEST_DEFAULT_ALL,
4065                      file_list_dest_targets,
4066                      num_file_list_dest_targets,
4067                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
4068   
4069   g_signal_connect (impl->browse_files_tree_view, "row_activated",
4070                     G_CALLBACK (list_row_activated), impl);
4071   g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4072                     G_CALLBACK (trap_activate_cb), impl);
4073   g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4074                     G_CALLBACK (list_popup_menu_cb), impl);
4075   g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4076                     G_CALLBACK (list_button_press_event_cb), impl);
4077
4078   g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4079                     G_CALLBACK (file_list_drag_data_received_cb), impl);
4080   g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4081                     G_CALLBACK (file_list_drag_drop_cb), impl);
4082   g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4083                     G_CALLBACK (file_list_drag_motion_cb), impl);
4084
4085   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4086   gtk_tree_selection_set_select_function (selection,
4087                                           list_select_func,
4088                                           impl, NULL);
4089   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4090                                           GDK_BUTTON1_MASK,
4091                                           file_list_source_targets,
4092                                           num_file_list_source_targets,
4093                                           GDK_ACTION_COPY);
4094
4095   g_signal_connect (selection, "changed",
4096                     G_CALLBACK (list_selection_changed), impl);
4097
4098   /* Filename column */
4099
4100   impl->list_name_column = gtk_tree_view_column_new ();
4101   gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4102   gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4103   gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4104   gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4105
4106   renderer = gtk_cell_renderer_pixbuf_new ();
4107   gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4108   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4109                                            list_icon_data_func, impl, NULL);
4110
4111   impl->list_name_renderer = gtk_cell_renderer_text_new ();
4112   g_object_set (impl->list_name_renderer,
4113                 "ellipsize", PANGO_ELLIPSIZE_END,
4114                 NULL);
4115   g_signal_connect (impl->list_name_renderer, "edited",
4116                     G_CALLBACK (renderer_edited_cb), impl);
4117   g_signal_connect (impl->list_name_renderer, "editing_canceled",
4118                     G_CALLBACK (renderer_editing_canceled_cb), impl);
4119   gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4120   gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4121                                            list_name_data_func, impl, NULL);
4122
4123   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4124 #if 0
4125   /* Size column */
4126
4127   column = gtk_tree_view_column_new ();
4128   gtk_tree_view_column_set_title (column, _("Size"));
4129
4130   renderer = gtk_cell_renderer_text_new ();
4131   gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4132   gtk_tree_view_column_set_cell_data_func (column, renderer,
4133                                            list_size_data_func, impl, NULL);
4134   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4135   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4136 #endif
4137   /* Modification time column */
4138
4139   column = gtk_tree_view_column_new ();
4140   gtk_tree_view_column_set_resizable (column, TRUE);
4141   gtk_tree_view_column_set_title (column, _("Modified"));
4142
4143   renderer = gtk_cell_renderer_text_new ();
4144   gtk_tree_view_column_pack_start (column, renderer, TRUE);
4145   gtk_tree_view_column_set_cell_data_func (column, renderer,
4146                                            list_mtime_data_func, impl, NULL);
4147   gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
4148   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4149   gtk_widget_show_all (swin);
4150
4151   return swin;
4152 }
4153
4154 static GtkWidget *
4155 create_path_bar (GtkFileChooserDefault *impl)
4156 {
4157   GtkWidget *path_bar;
4158
4159   path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4160   _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4161
4162   return path_bar;
4163 }
4164
4165 static void
4166 set_filter_tooltip (GtkWidget *widget, 
4167                     gpointer   data)
4168 {
4169   GtkTooltips *tooltips = (GtkTooltips *)data;
4170
4171   if (GTK_IS_BUTTON (widget))
4172     gtk_tooltips_set_tip (tooltips, widget,
4173                           _("Select which types of files are shown"), 
4174                           NULL);
4175 }
4176
4177 static void
4178 realize_filter_combo (GtkWidget *combo,
4179                       gpointer   data)
4180 {
4181   GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
4182
4183   gtk_container_forall (GTK_CONTAINER (combo),
4184                         set_filter_tooltip,
4185                         impl->tooltips);
4186 }
4187
4188 /* Creates the widgets for the files/folders pane */
4189 static GtkWidget *
4190 file_pane_create (GtkFileChooserDefault *impl,
4191                   GtkSizeGroup          *size_group)
4192 {
4193   GtkWidget *vbox;
4194   GtkWidget *hbox;
4195   GtkWidget *widget;
4196
4197   vbox = gtk_vbox_new (FALSE, 6);
4198   gtk_widget_show (vbox);
4199
4200   /* Box for lists and preview */
4201
4202   hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4203   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4204   gtk_widget_show (hbox);
4205
4206   /* File list */
4207
4208   widget = create_file_list (impl);
4209   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4210
4211   /* Preview */
4212
4213   impl->preview_box = gtk_vbox_new (FALSE, 12);
4214   gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4215   /* Don't show preview box initially */
4216
4217   /* Filter combo */
4218
4219   impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4220
4221   widget = filter_create (impl);
4222
4223   g_signal_connect (widget, "realize",
4224                     G_CALLBACK (realize_filter_combo), impl);
4225
4226   gtk_widget_show (widget);
4227   gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4228
4229   gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4230   gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4231
4232   return vbox;
4233 }
4234
4235 /* Callback used when the "Browse for more folders" expander is toggled */
4236 static void
4237 expander_changed_cb (GtkExpander           *expander,
4238                      GParamSpec            *pspec,
4239                      GtkFileChooserDefault *impl)
4240 {
4241   update_appearance (impl);
4242 }
4243
4244 /* Callback used when the selection changes in the save folder combo box */
4245 static void
4246 save_folder_combo_changed_cb (GtkComboBox           *combo,
4247                               GtkFileChooserDefault *impl)
4248 {
4249   GtkTreeIter iter;
4250
4251   if (impl->changing_folder)
4252     return;
4253
4254   if (gtk_combo_box_get_active_iter (combo, &iter))
4255     shortcuts_activate_iter (impl, &iter);
4256 }
4257
4258 /* Creates the combo box with the save folders */
4259 static GtkWidget *
4260 save_folder_combo_create (GtkFileChooserDefault *impl)
4261 {
4262   GtkWidget *combo;
4263   GtkCellRenderer *cell;
4264
4265   combo = g_object_new (GTK_TYPE_COMBO_BOX,
4266                         "model", impl->shortcuts_model,
4267                         "focus-on-click", FALSE,
4268                         NULL);
4269   gtk_widget_show (combo);
4270
4271   cell = gtk_cell_renderer_pixbuf_new ();
4272   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4273   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4274                                   "pixbuf", SHORTCUTS_COL_PIXBUF,
4275                                   "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4276                                   "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4277                                   NULL);
4278
4279   cell = gtk_cell_renderer_text_new ();
4280   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4281   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4282                                   "text", SHORTCUTS_COL_NAME,
4283                                   "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4284                                   NULL);
4285
4286   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4287                                         shortcuts_row_separator_func,
4288                                         GINT_TO_POINTER (SHORTCUTS_COL_NAME),
4289                                         NULL);
4290
4291   g_signal_connect (combo, "changed",
4292                     G_CALLBACK (save_folder_combo_changed_cb), impl);
4293
4294   return combo;
4295 }
4296
4297 /* Creates the widgets specific to Save mode */
4298 static void
4299 save_widgets_create (GtkFileChooserDefault *impl)
4300 {
4301   GtkWidget *vbox;
4302   GtkWidget *table;
4303   GtkWidget *widget;
4304   GtkWidget *alignment;
4305
4306   if (impl->save_widgets != NULL)
4307     return;
4308
4309   location_switch_to_path_bar (impl);
4310
4311   vbox = gtk_vbox_new (FALSE, 12);
4312
4313   table = gtk_table_new (2, 2, FALSE);
4314   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4315   gtk_widget_show (table);
4316   gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4317   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4318
4319   /* Label */
4320
4321   widget = gtk_label_new_with_mnemonic (_("_Name:"));
4322   gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4323   gtk_table_attach (GTK_TABLE (table), widget,
4324                     0, 1, 0, 1,
4325                     GTK_FILL, GTK_FILL,
4326                     0, 0);
4327   gtk_widget_show (widget);
4328
4329   /* Location entry */
4330
4331   impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4332   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4333                                            impl->file_system);
4334   gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4335   gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4336   gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4337                     1, 2, 0, 1,
4338                     GTK_EXPAND | GTK_FILL, 0,
4339                     0, 0);
4340   gtk_widget_show (impl->location_entry);
4341   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4342
4343   /* Folder combo */
4344   impl->save_folder_label = gtk_label_new (NULL);
4345   gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4346   gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4347                     0, 1, 1, 2,
4348                     GTK_FILL, GTK_FILL,
4349                     0, 0);
4350   gtk_widget_show (impl->save_folder_label);
4351
4352   impl->save_folder_combo = save_folder_combo_create (impl);
4353   gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4354                     1, 2, 1, 2,
4355                     GTK_EXPAND | GTK_FILL, GTK_FILL,
4356                     0, 0);
4357   gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4358
4359   /* Expander */
4360   alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4361   gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4362
4363   impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4364   gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4365   g_signal_connect (impl->save_expander, "notify::expanded",
4366                     G_CALLBACK (expander_changed_cb),
4367                     impl);
4368   gtk_widget_show_all (alignment);
4369
4370   impl->save_widgets = vbox;
4371   gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4372   gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4373   gtk_widget_show (impl->save_widgets);
4374 }
4375
4376 /* Destroys the widgets specific to Save mode */
4377 static void
4378 save_widgets_destroy (GtkFileChooserDefault *impl)
4379 {
4380   if (impl->save_widgets == NULL)
4381     return;
4382
4383   gtk_widget_destroy (impl->save_widgets);
4384   impl->save_widgets = NULL;
4385   impl->location_entry = NULL;
4386   impl->save_folder_label = NULL;
4387   impl->save_folder_combo = NULL;
4388   impl->save_expander = NULL;
4389 }
4390
4391 /* Turns on the path bar widget.  Can be called even if we are already in that
4392  * mode.
4393  */
4394 static void
4395 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4396 {
4397   if (impl->location_entry)
4398     {
4399       gtk_widget_destroy (impl->location_entry);
4400       impl->location_entry = NULL;
4401     }
4402
4403   gtk_widget_hide (impl->location_entry_box);
4404 }
4405
4406 /* Sets the full path of the current folder as the text in the location entry. */
4407 static void
4408 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4409 {
4410   char *text;
4411
4412   if (!impl->current_folder)
4413     return;
4414
4415   if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4416     {
4417       char *filename;
4418
4419       filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
4420       if (filename)
4421         {
4422           text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4423           g_free (filename);
4424         }
4425       else
4426         text = NULL;
4427     }
4428   else
4429     text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
4430
4431   if (text)
4432     {
4433       gboolean need_slash;
4434       int len;
4435
4436       len = strlen (text);
4437       need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4438
4439       if (need_slash)
4440         {
4441           char *slash_text;
4442
4443           slash_text = g_new (char, len + 2);
4444           strcpy (slash_text, text);
4445           slash_text[len] = G_DIR_SEPARATOR;
4446           slash_text[len + 1] = 0;
4447
4448           g_free (text);
4449           text = slash_text;
4450         }
4451
4452       _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4453       g_free (text);
4454     }
4455 }
4456
4457 /* Turns on the location entry.  Can be called even if we are already in that
4458  * mode.
4459  */
4460 static void
4461 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4462 {
4463   if (impl->location_entry)
4464     gtk_widget_destroy (impl->location_entry);
4465
4466   /* Box */
4467
4468   gtk_widget_show (impl->location_entry_box);
4469
4470   /* Entry */
4471
4472   impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4473   _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4474                                            impl->file_system);
4475   gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4476   _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4477
4478   gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4479   gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4480
4481   /* Configure the entry */
4482
4483   _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4484
4485   /* Done */
4486
4487   gtk_widget_show (impl->location_entry);
4488   gtk_widget_grab_focus (impl->location_entry);
4489 }
4490
4491 /* Sets a new location mode.  set_buttons determines whether the toggle button
4492  * for the mode will also be changed.
4493  */
4494 static void
4495 location_mode_set (GtkFileChooserDefault *impl,
4496                    LocationMode new_mode,
4497                    gboolean set_button)
4498 {
4499   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4500       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4501     {
4502       GtkWindow *toplevel;
4503       GtkWidget *current_focus;
4504       gboolean button_active;
4505       gboolean switch_to_file_list;
4506
4507       switch (new_mode)
4508         {
4509         case LOCATION_MODE_PATH_BAR:
4510           button_active = FALSE;
4511
4512           /* The location_entry will disappear when we switch to path bar mode.  So,
4513            * we'll focus the file list in that case, to avoid having a window with
4514            * no focused widget.
4515            */
4516           toplevel = get_toplevel (GTK_WIDGET (impl));
4517           switch_to_file_list = FALSE;
4518           if (toplevel)
4519             {
4520               current_focus = gtk_window_get_focus (toplevel);
4521               if (!current_focus || current_focus == impl->location_entry)
4522                 switch_to_file_list = TRUE;
4523             }
4524
4525           location_switch_to_path_bar (impl);
4526
4527           if (switch_to_file_list)
4528             gtk_widget_grab_focus (impl->browse_files_tree_view);
4529
4530           break;
4531
4532         case LOCATION_MODE_FILENAME_ENTRY:
4533           button_active = TRUE;
4534           location_switch_to_filename_entry (impl);
4535           break;
4536
4537         default:
4538           g_assert_not_reached ();
4539           return;
4540         }
4541
4542       if (set_button)
4543         {
4544           g_signal_handlers_block_by_func (impl->location_button,
4545                                            G_CALLBACK (location_button_toggled_cb), impl);
4546
4547           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4548
4549           g_signal_handlers_unblock_by_func (impl->location_button,
4550                                              G_CALLBACK (location_button_toggled_cb), impl);
4551         }
4552     }
4553
4554   impl->location_mode = new_mode;
4555 }
4556
4557 /* Callback used when one of the location mode buttons is toggled */
4558 static void
4559 location_button_toggled_cb (GtkToggleButton *toggle,
4560                             GtkFileChooserDefault *impl)
4561 {
4562   gboolean is_active;
4563   LocationMode new_mode;
4564
4565   is_active = gtk_toggle_button_get_active (toggle);
4566
4567   if (is_active)
4568     {
4569       g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4570       new_mode = LOCATION_MODE_FILENAME_ENTRY;
4571     }
4572   else
4573     {
4574       g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4575       new_mode = LOCATION_MODE_PATH_BAR;
4576     }
4577
4578   location_mode_set (impl, new_mode, FALSE);
4579 }
4580
4581 /* Creates a toggle button for the location entry. */
4582 static void
4583 location_button_create (GtkFileChooserDefault *impl)
4584 {
4585   GtkWidget *image;
4586   const char *str;
4587
4588   image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4589   gtk_widget_show (image);
4590
4591   impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4592                                         "image", image,
4593                                         NULL);
4594
4595   g_signal_connect (impl->location_button, "toggled",
4596                     G_CALLBACK (location_button_toggled_cb), impl);
4597
4598   str = _("Type a file name");
4599
4600   gtk_tooltips_set_tip (impl->tooltips, impl->location_button, str, NULL);
4601   atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4602 }
4603
4604 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4605 static GtkWidget *
4606 browse_widgets_create (GtkFileChooserDefault *impl)
4607 {
4608   GtkWidget *vbox;
4609   GtkWidget *hbox;
4610   GtkWidget *hpaned;
4611   GtkWidget *widget;
4612   GtkSizeGroup *size_group;
4613
4614   /* size group is used by the [+][-] buttons and the filter combo */
4615   size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4616   vbox = gtk_vbox_new (FALSE, 12);
4617
4618   /* Location widgets */
4619   hbox = gtk_hbox_new (FALSE, 12);
4620   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4621   gtk_widget_show (hbox);
4622
4623   location_button_create (impl);
4624   gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
4625
4626   /* Path bar */
4627
4628   impl->browse_path_bar = create_path_bar (impl);
4629   g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4630   gtk_widget_show_all (impl->browse_path_bar);
4631   gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4632
4633   /* Create Folder */
4634   impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4635   g_signal_connect (impl->browse_new_folder_button, "clicked",
4636                     G_CALLBACK (new_folder_button_clicked), impl);
4637   gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4638
4639   /* Box for the location label and entry */
4640
4641   impl->location_entry_box = gtk_hbox_new (FALSE, 12);
4642   gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
4643
4644   impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4645   gtk_widget_show (impl->location_label);
4646   gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4647
4648   /* Paned widget */
4649   hpaned = gtk_hpaned_new ();
4650   gtk_widget_show (hpaned);
4651   gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
4652   gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
4653
4654   widget = shortcuts_pane_create (impl, size_group);
4655   gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4656   widget = file_pane_create (impl, size_group);
4657   gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4658
4659   g_object_unref (size_group);
4660
4661   return vbox;
4662 }
4663
4664 static GObject*
4665 gtk_file_chooser_default_constructor (GType                  type,
4666                                       guint                  n_construct_properties,
4667                                       GObjectConstructParam *construct_params)
4668 {
4669   GtkFileChooserDefault *impl;
4670   GObject *object;
4671
4672   profile_start ("start", NULL);
4673
4674   object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4675                                                                                 n_construct_properties,
4676                                                                                 construct_params);
4677   impl = GTK_FILE_CHOOSER_DEFAULT (object);
4678
4679   g_assert (impl->file_system);
4680
4681   gtk_widget_push_composite_child ();
4682
4683   /* Shortcuts model */
4684
4685   shortcuts_model_create (impl);
4686
4687   /* The browse widgets */
4688   impl->browse_widgets = browse_widgets_create (impl);
4689   gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
4690
4691   /* Alignment to hold extra widget */
4692   impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4693   gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4694
4695   gtk_widget_pop_composite_child ();
4696   update_appearance (impl);
4697
4698   profile_end ("end", NULL);
4699
4700   return object;
4701 }
4702
4703 /* Sets the extra_widget by packing it in the appropriate place */
4704 static void
4705 set_extra_widget (GtkFileChooserDefault *impl,
4706                   GtkWidget             *extra_widget)
4707 {
4708   if (extra_widget)
4709     {
4710       g_object_ref (extra_widget);
4711       /* FIXME: is this right ? */
4712       gtk_widget_show (extra_widget);
4713     }
4714
4715   if (impl->extra_widget)
4716     {
4717       gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4718       g_object_unref (impl->extra_widget);
4719     }
4720
4721   impl->extra_widget = extra_widget;
4722   if (impl->extra_widget)
4723     {
4724       gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4725       gtk_widget_show (impl->extra_align);
4726     }
4727   else
4728     gtk_widget_hide (impl->extra_align);
4729 }
4730
4731 static void
4732 set_local_only (GtkFileChooserDefault *impl,
4733                 gboolean               local_only)
4734 {
4735   if (local_only != impl->local_only)
4736     {
4737       impl->local_only = local_only;
4738
4739       if (impl->shortcuts_model && impl->file_system)
4740         {
4741           shortcuts_add_volumes (impl);
4742           shortcuts_add_bookmarks (impl);
4743         }
4744
4745       if (local_only &&
4746           !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4747         {
4748           /* If we are pointing to a non-local folder, make an effort to change
4749            * back to a local folder, but it's really up to the app to not cause
4750            * such a situation, so we ignore errors.
4751            */
4752           const gchar *home = g_get_home_dir ();
4753           GtkFilePath *home_path;
4754
4755           if (home == NULL)
4756             return;
4757
4758           home_path = gtk_file_system_filename_to_path (impl->file_system, home);
4759
4760           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
4761
4762           gtk_file_path_free (home_path);
4763         }
4764     }
4765 }
4766
4767 static void
4768 volumes_changed_cb (GtkFileSystem         *file_system,
4769                     GtkFileChooserDefault *impl)
4770 {
4771   shortcuts_add_volumes (impl);
4772 }
4773
4774 /* Callback used when the set of bookmarks changes in the file system */
4775 static void
4776 bookmarks_changed_cb (GtkFileSystem         *file_system,
4777                       GtkFileChooserDefault *impl)
4778 {
4779   shortcuts_add_bookmarks (impl);
4780
4781   bookmarks_check_add_sensitivity (impl);
4782   bookmarks_check_remove_sensitivity (impl);
4783   shortcuts_check_popup_sensitivity (impl);
4784 }
4785
4786 /* Sets the file chooser to multiple selection mode */
4787 static void
4788 set_select_multiple (GtkFileChooserDefault *impl,
4789                      gboolean               select_multiple,
4790                      gboolean               property_notify)
4791 {
4792   GtkTreeSelection *selection;
4793   GtkSelectionMode mode;
4794
4795   if (select_multiple == impl->select_multiple)
4796     return;
4797
4798   mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4799
4800   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4801   gtk_tree_selection_set_mode (selection, mode);
4802
4803   impl->select_multiple = select_multiple;
4804   g_object_notify (G_OBJECT (impl), "select-multiple");
4805
4806   check_preview_change (impl);
4807 }
4808
4809 static void
4810 set_file_system_backend (GtkFileChooserDefault *impl,
4811                          const char *backend)
4812 {
4813   profile_start ("start for backend", backend ? backend : "default");
4814
4815   if (impl->file_system)
4816     {
4817       g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
4818       impl->volumes_changed_id = 0;
4819       g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
4820       impl->bookmarks_changed_id = 0;
4821       g_object_unref (impl->file_system);
4822     }
4823
4824   impl->file_system = NULL;
4825   if (backend)
4826     impl->file_system = _gtk_file_system_create (backend);
4827   else
4828     {
4829       GtkSettings *settings = gtk_settings_get_default ();
4830       gchar *default_backend = NULL;
4831
4832       g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
4833       if (default_backend)
4834         {
4835           impl->file_system = _gtk_file_system_create (default_backend);
4836           g_free (default_backend);
4837         }
4838     }
4839
4840   if (!impl->file_system)
4841     {
4842 #if defined (G_OS_UNIX)
4843       impl->file_system = gtk_file_system_unix_new ();
4844 #elif defined (G_OS_WIN32)
4845       impl->file_system = gtk_file_system_win32_new ();
4846 #else
4847 #error "No default filesystem implementation on the platform"
4848 #endif
4849     }
4850
4851   if (impl->file_system)
4852     {
4853       impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
4854                                                    G_CALLBACK (volumes_changed_cb),
4855                                                    impl);
4856       impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
4857                                                      G_CALLBACK (bookmarks_changed_cb),
4858                                                      impl);
4859     }
4860
4861   profile_end ("end", NULL);
4862 }
4863
4864 /* This function is basically a do_all function.
4865  *
4866  * It sets the visibility on all the widgets based on the current state, and
4867  * moves the custom_widget if needed.
4868  */
4869 static void
4870 update_appearance (GtkFileChooserDefault *impl)
4871 {
4872   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4873       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4874     {
4875       const char *text;
4876
4877       gtk_widget_hide (impl->location_button);
4878       save_widgets_create (impl);
4879
4880       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4881         text = _("Save in _folder:");
4882       else
4883         text = _("Create in _folder:");
4884
4885       gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4886
4887       if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4888         {
4889           gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4890           gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4891           gtk_widget_show (impl->browse_widgets);
4892         }
4893       else
4894         {
4895           gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4896           gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4897           gtk_widget_hide (impl->browse_widgets);
4898         }
4899
4900       gtk_widget_show (impl->browse_new_folder_button);
4901
4902       if (impl->select_multiple)
4903         {
4904           g_warning ("Save mode cannot be set in conjunction with multiple selection mode.  "
4905                      "Re-setting to single selection mode.");
4906           set_select_multiple (impl, FALSE, TRUE);
4907         }
4908     }
4909   else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4910            impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4911     {
4912       gtk_widget_show (impl->location_button);
4913       save_widgets_destroy (impl);
4914       gtk_widget_show (impl->browse_widgets);
4915       location_mode_set (impl, impl->location_mode, TRUE);
4916     }
4917
4918   if (impl->location_entry)
4919     _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4920
4921   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4922     gtk_widget_hide (impl->browse_new_folder_button);
4923   else
4924     gtk_widget_show (impl->browse_new_folder_button);
4925
4926   /* This *is* needed; we need to redraw the file list because the "sensitivity"
4927    * of files may change depending whether we are in a file or folder-only mode.
4928    */
4929   gtk_widget_queue_draw (impl->browse_files_tree_view);
4930
4931   g_signal_emit_by_name (impl, "default-size-changed");
4932 }
4933
4934 static void
4935 gtk_file_chooser_default_set_property (GObject      *object,
4936                                        guint         prop_id,
4937                                        const GValue *value,
4938                                        GParamSpec   *pspec)
4939
4940 {
4941   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4942
4943   switch (prop_id)
4944     {
4945     case GTK_FILE_CHOOSER_PROP_ACTION:
4946       {
4947         GtkFileChooserAction action = g_value_get_enum (value);
4948
4949         if (action != impl->action)
4950           {
4951             gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4952             
4953             if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4954                 && impl->select_multiple)
4955               {
4956                 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4957                            "this is not allowed in multiple selection mode.  Resetting the file chooser "
4958                            "to single selection mode.");
4959                 set_select_multiple (impl, FALSE, TRUE);
4960               }
4961             impl->action = action;
4962             update_appearance (impl);
4963           }
4964       }
4965       break;
4966
4967     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4968       set_file_system_backend (impl, g_value_get_string (value));
4969       break;
4970
4971     case GTK_FILE_CHOOSER_PROP_FILTER:
4972       set_current_filter (impl, g_value_get_object (value));
4973       break;
4974
4975     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4976       set_local_only (impl, g_value_get_boolean (value));
4977       break;
4978
4979     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4980       set_preview_widget (impl, g_value_get_object (value));
4981       break;
4982
4983     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4984       impl->preview_widget_active = g_value_get_boolean (value);
4985       update_preview_widget_visibility (impl);
4986       break;
4987
4988     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4989       impl->use_preview_label = g_value_get_boolean (value);
4990       update_preview_widget_visibility (impl);
4991       break;
4992
4993     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4994       set_extra_widget (impl, g_value_get_object (value));
4995       break;
4996
4997     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4998       {
4999         gboolean select_multiple = g_value_get_boolean (value);
5000         if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5001             && select_multiple)
5002           {
5003             g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5004                        "not allowed in SAVE or CREATE_FOLDER modes.  Ignoring the change and "
5005                        "leaving the file chooser in single selection mode.");
5006             return;
5007           }
5008
5009         set_select_multiple (impl, select_multiple, FALSE);
5010       }
5011       break;
5012
5013     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5014       {
5015         gboolean show_hidden = g_value_get_boolean (value);
5016         if (show_hidden != impl->show_hidden)
5017           {
5018             impl->show_hidden = show_hidden;
5019
5020             if (impl->browse_files_model)
5021               _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5022           }
5023       }
5024       break;
5025
5026     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5027       {
5028         gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5029         impl->do_overwrite_confirmation = do_overwrite_confirmation;
5030       }
5031       break;
5032
5033     default:
5034       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5035       break;
5036     }
5037 }
5038
5039 static void
5040 gtk_file_chooser_default_get_property (GObject    *object,
5041                                        guint       prop_id,
5042                                        GValue     *value,
5043                                        GParamSpec *pspec)
5044 {
5045   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5046
5047   switch (prop_id)
5048     {
5049     case GTK_FILE_CHOOSER_PROP_ACTION:
5050       g_value_set_enum (value, impl->action);
5051       break;
5052
5053     case GTK_FILE_CHOOSER_PROP_FILTER:
5054       g_value_set_object (value, impl->current_filter);
5055       break;
5056
5057     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5058       g_value_set_boolean (value, impl->local_only);
5059       break;
5060
5061     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5062       g_value_set_object (value, impl->preview_widget);
5063       break;
5064
5065     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5066       g_value_set_boolean (value, impl->preview_widget_active);
5067       break;
5068
5069     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5070       g_value_set_boolean (value, impl->use_preview_label);
5071       break;
5072
5073     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5074       g_value_set_object (value, impl->extra_widget);
5075       break;
5076
5077     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5078       g_value_set_boolean (value, impl->select_multiple);
5079       break;
5080
5081     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5082       g_value_set_boolean (value, impl->show_hidden);
5083       break;
5084
5085     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5086       g_value_set_boolean (value, impl->do_overwrite_confirmation);
5087       break;
5088
5089     default:
5090       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5091       break;
5092     }
5093 }
5094
5095 /* Removes the settings signal handler.  It's safe to call multiple times */
5096 static void
5097 remove_settings_signal (GtkFileChooserDefault *impl,
5098                         GdkScreen             *screen)
5099 {
5100   if (impl->settings_signal_id)
5101     {
5102       GtkSettings *settings;
5103
5104       settings = gtk_settings_get_for_screen (screen);
5105       g_signal_handler_disconnect (settings,
5106                                    impl->settings_signal_id);
5107       impl->settings_signal_id = 0;
5108     }
5109 }
5110
5111 static void
5112 gtk_file_chooser_default_dispose (GObject *object)
5113 {
5114   GSList *l;
5115   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5116
5117   if (impl->extra_widget)
5118     {
5119       g_object_unref (impl->extra_widget);
5120       impl->extra_widget = NULL;
5121     }
5122
5123   if (impl->volumes_changed_id > 0)
5124     {
5125       g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5126       impl->volumes_changed_id = 0;
5127     }
5128
5129   if (impl->bookmarks_changed_id > 0)
5130     {
5131       g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5132       impl->bookmarks_changed_id = 0;
5133     }
5134
5135   pending_select_paths_free (impl);
5136
5137   /* cancel all pending operations */
5138   if (impl->pending_handles)
5139     {
5140       for (l = impl->pending_handles; l; l = l->next)
5141         {
5142           GtkFileSystemHandle *handle =l->data;
5143           gtk_file_system_cancel_operation (handle);
5144         }
5145       g_slist_free (impl->pending_handles);
5146       impl->pending_handles = NULL;
5147     }
5148
5149   if (impl->reload_icon_handles)
5150     {
5151       for (l = impl->reload_icon_handles; l; l = l->next)
5152         {
5153           GtkFileSystemHandle *handle =l->data;
5154           gtk_file_system_cancel_operation (handle);
5155         }
5156       g_slist_free (impl->reload_icon_handles);
5157       impl->reload_icon_handles = NULL;
5158     }
5159
5160   if (impl->loading_shortcuts)
5161     {
5162       for (l = impl->loading_shortcuts; l; l = l->next)
5163         {
5164           GtkFileSystemHandle *handle =l->data;
5165           gtk_file_system_cancel_operation (handle);
5166         }
5167       g_slist_free (impl->loading_shortcuts);
5168       impl->loading_shortcuts = NULL;
5169     }
5170
5171   if (impl->file_list_drag_data_received_handle)
5172     {
5173       gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5174       impl->file_list_drag_data_received_handle = NULL;
5175     }
5176
5177   if (impl->update_current_folder_handle)
5178     {
5179       gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5180       impl->update_current_folder_handle = NULL;
5181     }
5182
5183   if (impl->show_and_select_paths_handle)
5184     {
5185       gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5186       impl->show_and_select_paths_handle = NULL;
5187     }
5188
5189   if (impl->should_respond_get_info_handle)
5190     {
5191       gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5192       impl->should_respond_get_info_handle = NULL;
5193     }
5194
5195   if (impl->update_from_entry_handle)
5196     {
5197       gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5198       impl->update_from_entry_handle = NULL;
5199     }
5200
5201   if (impl->shortcuts_activate_iter_handle)
5202     {
5203       gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5204       impl->shortcuts_activate_iter_handle = NULL;
5205     }
5206
5207   remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5208
5209   G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5210 }
5211
5212 /* We override show-all since we have internal widgets that
5213  * shouldn't be shown when you call show_all(), like the filter
5214  * combo box.
5215  */
5216 static void
5217 gtk_file_chooser_default_show_all (GtkWidget *widget)
5218 {
5219   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5220
5221   gtk_widget_show (widget);
5222
5223   if (impl->extra_widget)
5224     gtk_widget_show_all (impl->extra_widget);
5225 }
5226
5227 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5228  * widget on our toplevel.  See gtk_file_chooser_default_hierarchy_changed()
5229  */
5230 static void
5231 toplevel_set_focus_cb (GtkWindow             *window,
5232                        GtkWidget             *focus,
5233                        GtkFileChooserDefault *impl)
5234 {
5235   impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5236 }
5237
5238 /* We monitor the focus widget on our toplevel to be able to know which widget
5239  * was last focused at the time our "should_respond" method gets called.
5240  */
5241 static void
5242 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5243                                             GtkWidget *previous_toplevel)
5244 {
5245   GtkFileChooserDefault *impl;
5246   GtkWidget *toplevel;
5247
5248   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5249
5250   if (previous_toplevel)
5251     {
5252       g_assert (impl->toplevel_set_focus_id != 0);
5253       g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5254       impl->toplevel_set_focus_id = 0;
5255       impl->toplevel_last_focus_widget = NULL;
5256     }
5257   else
5258     g_assert (impl->toplevel_set_focus_id == 0);
5259
5260   toplevel = gtk_widget_get_toplevel (widget);
5261   if (GTK_IS_WINDOW (toplevel))
5262     {
5263       impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5264                                                       G_CALLBACK (toplevel_set_focus_cb), impl);
5265       impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5266     }
5267 }
5268
5269 /* Changes the icons wherever it is needed */
5270 static void
5271 change_icon_theme (GtkFileChooserDefault *impl)
5272 {
5273   GtkSettings *settings;
5274   gint width, height;
5275
5276   profile_start ("start", NULL);
5277
5278   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5279
5280   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5281     impl->icon_size = MAX (width, height);
5282   else
5283     impl->icon_size = FALLBACK_ICON_SIZE;
5284
5285   shortcuts_reload_icons (impl);
5286   gtk_widget_queue_resize (impl->browse_files_tree_view);
5287
5288   profile_end ("end", NULL);
5289 }
5290
5291 /* Callback used when a GtkSettings value changes */
5292 static void
5293 settings_notify_cb (GObject               *object,
5294                     GParamSpec            *pspec,
5295                     GtkFileChooserDefault *impl)
5296 {
5297   const char *name;
5298
5299   profile_start ("start", NULL);
5300
5301   name = g_param_spec_get_name (pspec);
5302
5303   if (strcmp (name, "gtk-icon-theme-name") == 0
5304       || strcmp (name, "gtk-icon-sizes") == 0)
5305     change_icon_theme (impl);
5306
5307   profile_end ("end", NULL);
5308 }
5309
5310 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5311  * the icon theme.
5312  */
5313 static void
5314 check_icon_theme (GtkFileChooserDefault *impl)
5315 {
5316   GtkSettings *settings;
5317
5318   profile_start ("start", NULL);
5319
5320   if (impl->settings_signal_id)
5321     {
5322       profile_end ("end", NULL);
5323       return;
5324     }
5325
5326   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5327     {
5328       settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5329       impl->settings_signal_id = g_signal_connect (settings, "notify",
5330                                                    G_CALLBACK (settings_notify_cb), impl);
5331
5332       change_icon_theme (impl);
5333     }
5334
5335   profile_end ("end", NULL);
5336 }
5337
5338 static void
5339 gtk_file_chooser_default_style_set (GtkWidget *widget,
5340                                     GtkStyle  *previous_style)
5341 {
5342   GtkFileChooserDefault *impl;
5343
5344   profile_start ("start", NULL);
5345
5346   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5347
5348   profile_msg ("    parent class style_set start", NULL);
5349   if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5350     GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5351   profile_msg ("    parent class style_set end", NULL);
5352
5353   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5354     change_icon_theme (impl);
5355
5356   profile_msg ("    emit default-size-changed start", NULL);
5357   g_signal_emit_by_name (widget, "default-size-changed");
5358   profile_msg ("    emit default-size-changed end", NULL);
5359
5360   profile_end ("end", NULL);
5361 }
5362
5363 static void
5364 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5365                                          GdkScreen *previous_screen)
5366 {
5367   GtkFileChooserDefault *impl;
5368
5369   profile_start ("start", NULL);
5370
5371   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5372
5373   if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5374     GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5375
5376   remove_settings_signal (impl, previous_screen);
5377   check_icon_theme (impl);
5378
5379   g_signal_emit_by_name (widget, "default-size-changed");
5380
5381   profile_end ("end", NULL);
5382 }
5383
5384 static gboolean
5385 get_is_file_filtered (GtkFileChooserDefault *impl,
5386                       const GtkFilePath     *path,
5387                       GtkFileInfo           *file_info)
5388 {
5389   GtkFileFilterInfo filter_info;
5390   GtkFileFilterFlags needed;
5391   gboolean result;
5392
5393   if (!impl->current_filter)
5394     return FALSE;
5395
5396   filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5397
5398   needed = gtk_file_filter_get_needed (impl->current_filter);
5399
5400   filter_info.display_name = gtk_file_info_get_display_name (file_info);
5401   filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
5402
5403   if (needed & GTK_FILE_FILTER_FILENAME)
5404     {
5405       filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
5406       if (filter_info.filename)
5407         filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5408     }
5409   else
5410     filter_info.filename = NULL;
5411
5412   if (needed & GTK_FILE_FILTER_URI)
5413     {
5414       filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
5415       if (filter_info.uri)
5416         filter_info.contains |= GTK_FILE_FILTER_URI;
5417     }
5418   else
5419     filter_info.uri = NULL;
5420
5421   result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5422
5423   if (filter_info.filename)
5424     g_free ((gchar *)filter_info.filename);
5425   if (filter_info.uri)
5426     g_free ((gchar *)filter_info.uri);
5427
5428   return !result;
5429 }
5430
5431 static void
5432 settings_load (GtkFileChooserDefault *impl)
5433 {
5434   GtkFileChooserSettings *settings;
5435   LocationMode location_mode;
5436   gboolean show_hidden;
5437
5438   settings = _gtk_file_chooser_settings_new ();
5439
5440   location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5441   show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5442
5443   g_object_unref (settings);
5444
5445   location_mode_set (impl, location_mode, TRUE);
5446   gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5447 }
5448
5449 static void
5450 settings_save (GtkFileChooserDefault *impl)
5451 {
5452   GtkFileChooserSettings *settings;
5453
5454   settings = _gtk_file_chooser_settings_new ();
5455
5456   _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5457   _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5458
5459   /* NULL GError */
5460   _gtk_file_chooser_settings_save (settings, NULL);
5461
5462   g_object_unref (settings);
5463 }
5464
5465 /* GtkWidget::map method */
5466 static void
5467 gtk_file_chooser_default_map (GtkWidget *widget)
5468 {
5469   GtkFileChooserDefault *impl;
5470   char *current_working_dir;
5471
5472   profile_start ("start", NULL);
5473
5474   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5475
5476   GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5477
5478   switch (impl->reload_state)
5479     {
5480     case RELOAD_EMPTY:
5481       /* The user didn't explicitly give us a folder to display, so we'll use the cwd */
5482       current_working_dir = g_get_current_dir ();
5483       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), current_working_dir);
5484       g_free (current_working_dir);
5485       break;
5486
5487     case RELOAD_HAS_FOLDER:
5488       /* Nothing; we are already loading or loaded, so we don't need to reload */
5489       break;
5490
5491     case RELOAD_WAS_UNMAPPED:
5492       /* Just reload the current folder; else continue the pending load. */
5493       if (impl->current_folder)
5494         {
5495           pending_select_paths_store_selection (impl);
5496           change_folder_and_display_error (impl, impl->current_folder);
5497         }
5498       break;
5499
5500     default:
5501       g_assert_not_reached ();
5502     }
5503
5504   bookmarks_changed_cb (impl->file_system, impl);
5505
5506   settings_load (impl);
5507
5508   profile_end ("end", NULL);
5509 }
5510
5511 /* GtkWidget::unmap method */
5512 static void
5513 gtk_file_chooser_default_unmap (GtkWidget *widget)
5514 {
5515   GtkFileChooserDefault *impl;
5516
5517   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5518
5519   settings_save (impl);
5520
5521   GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5522
5523   impl->reload_state = RELOAD_WAS_UNMAPPED;
5524 }
5525
5526 static gboolean
5527 list_model_filter_func (GtkFileSystemModel *model,
5528                         GtkFilePath        *path,
5529                         const GtkFileInfo  *file_info,
5530                         gpointer            user_data)
5531 {
5532   GtkFileChooserDefault *impl = user_data;
5533
5534   if (!impl->current_filter)
5535     return TRUE;
5536
5537   if (gtk_file_info_get_is_folder (file_info))
5538     return TRUE;
5539
5540   return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
5541 }
5542
5543 static void
5544 install_list_model_filter (GtkFileChooserDefault *impl)
5545 {
5546   GtkFileSystemModelFilter filter;
5547   gpointer data;
5548
5549   g_assert (impl->browse_files_model != NULL);
5550
5551   if (impl->current_filter)
5552     {
5553       filter = list_model_filter_func;
5554       data   = impl;
5555     }
5556   else
5557     {
5558       filter = NULL;
5559       data   = NULL;
5560     }
5561   
5562   _gtk_file_system_model_set_filter (impl->browse_files_model,
5563                                      filter,
5564                                      data);
5565 }
5566
5567 #define COMPARE_DIRECTORIES                                                                                    \
5568   GtkFileChooserDefault *impl = user_data;                                                                     \
5569   const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a);                           \
5570   const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b);                           \
5571   gboolean dir_a, dir_b;                                                                                       \
5572                                                                                                                \
5573   if (info_a)                                                                                                  \
5574     dir_a = gtk_file_info_get_is_folder (info_a);                                                              \
5575   else                                                                                                         \
5576     return impl->list_sort_ascending ? -1 : 1;                                                                 \
5577                                                                                                                \
5578   if (info_b)                                                                                                  \
5579     dir_b = gtk_file_info_get_is_folder (info_b);                                                              \
5580   else                                                                                                         \
5581     return impl->list_sort_ascending ? 1 : -1;                                                                 \
5582                                                                                                                \
5583   if (dir_a != dir_b)                                                                                          \
5584     return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5585
5586 /* Sort callback for the filename column */
5587 static gint
5588 name_sort_func (GtkTreeModel *model,
5589                 GtkTreeIter  *a,
5590                 GtkTreeIter  *b,
5591                 gpointer      user_data)
5592 {
5593   COMPARE_DIRECTORIES;
5594   else
5595     return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
5596 }
5597
5598 /* Sort callback for the size column */
5599 static gint
5600 size_sort_func (GtkTreeModel *model,
5601                 GtkTreeIter  *a,
5602                 GtkTreeIter  *b,
5603                 gpointer      user_data)
5604 {
5605   COMPARE_DIRECTORIES;
5606   else
5607     {
5608       gint64 size_a = gtk_file_info_get_size (info_a);
5609       gint64 size_b = gtk_file_info_get_size (info_b);
5610
5611       return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
5612     }
5613 }
5614
5615 /* Sort callback for the mtime column */
5616 static gint
5617 mtime_sort_func (GtkTreeModel *model,
5618                  GtkTreeIter  *a,
5619                  GtkTreeIter  *b,
5620                  gpointer      user_data)
5621 {
5622   COMPARE_DIRECTORIES;
5623   else
5624     {
5625       GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
5626       GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
5627
5628       return ta > tb ? -1 : (ta == tb ? 0 : 1);
5629     }
5630 }
5631
5632 /* Callback used when the sort column changes.  We cache the sort order for use
5633  * in name_sort_func().
5634  */
5635 static void
5636 list_sort_column_changed_cb (GtkTreeSortable       *sortable,
5637                              GtkFileChooserDefault *impl)
5638 {
5639   GtkSortType sort_type;
5640
5641   if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
5642     impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5643 }
5644
5645 static void
5646 set_busy_cursor (GtkFileChooserDefault *impl,
5647                  gboolean               busy)
5648 {
5649   GtkWindow *toplevel;
5650   GdkDisplay *display;
5651   GdkCursor *cursor;
5652
5653   toplevel = get_toplevel (GTK_WIDGET (impl));
5654   if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
5655     return;
5656
5657   display = gtk_widget_get_display (GTK_WIDGET (toplevel));
5658
5659   if (busy)
5660     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5661   else
5662     cursor = NULL;
5663
5664   gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
5665   gdk_display_flush (display);
5666
5667   if (cursor)
5668     gdk_cursor_unref (cursor);
5669 }
5670
5671 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5672 static void
5673 load_set_model (GtkFileChooserDefault *impl)
5674 {
5675   profile_start ("start", NULL);
5676
5677   g_assert (impl->browse_files_model != NULL);
5678   g_assert (impl->sort_model == NULL);
5679
5680   profile_msg ("    gtk_tree_model_sort_new_with_model start", NULL);
5681   impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
5682   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
5683   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
5684   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
5685   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
5686   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
5687   impl->list_sort_ascending = TRUE;
5688   profile_msg ("    gtk_tree_model_sort_new_with_model end", NULL);
5689
5690   g_signal_connect (impl->sort_model, "sort_column_changed",
5691                     G_CALLBACK (list_sort_column_changed_cb), impl);
5692
5693   profile_msg ("    gtk_tree_view_set_model start", NULL);
5694   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
5695                            GTK_TREE_MODEL (impl->sort_model));
5696   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
5697   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
5698                                    GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
5699   profile_msg ("    gtk_tree_view_set_model end", NULL);
5700
5701   profile_end ("end", NULL);
5702 }
5703
5704 /* Timeout callback used when the loading timer expires */
5705 static gboolean
5706 load_timeout_cb (gpointer data)
5707 {
5708   GtkFileChooserDefault *impl;
5709
5710   profile_start ("start", NULL);
5711
5712   GDK_THREADS_ENTER ();
5713
5714   impl = GTK_FILE_CHOOSER_DEFAULT (data);
5715   g_assert (impl->load_state == LOAD_PRELOAD);
5716   g_assert (impl->load_timeout_id != 0);
5717   g_assert (impl->browse_files_model != NULL);
5718
5719   impl->load_timeout_id = 0;
5720   impl->load_state = LOAD_LOADING;
5721
5722   load_set_model (impl);
5723
5724   GDK_THREADS_LEAVE ();
5725
5726   profile_end ("end", NULL);
5727
5728   return FALSE;
5729 }
5730
5731 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
5732 static void
5733 load_setup_timer (GtkFileChooserDefault *impl)
5734 {
5735   g_assert (impl->load_timeout_id == 0);
5736   g_assert (impl->load_state != LOAD_PRELOAD);
5737
5738   impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
5739   impl->load_state = LOAD_PRELOAD;
5740 }
5741
5742 /* Removes the load timeout and switches to the LOAD_FINISHED state */
5743 static void
5744 load_remove_timer (GtkFileChooserDefault *impl)
5745 {
5746   if (impl->load_timeout_id != 0)
5747     {
5748       g_assert (impl->load_state == LOAD_PRELOAD);
5749
5750       g_source_remove (impl->load_timeout_id);
5751       impl->load_timeout_id = 0;
5752       impl->load_state = LOAD_EMPTY;
5753     }
5754   else
5755     g_assert (impl->load_state == LOAD_EMPTY ||
5756               impl->load_state == LOAD_LOADING ||
5757               impl->load_state == LOAD_FINISHED);
5758 }
5759
5760 /* Selects the first row in the file list */
5761 static void
5762 browse_files_select_first_row (GtkFileChooserDefault *impl)
5763 {
5764   GtkTreePath *path;
5765
5766   if (!impl->sort_model)
5767     return;
5768
5769   path = gtk_tree_path_new_from_indices (0, -1);
5770   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
5771   gtk_tree_path_free (path);
5772 }
5773
5774 struct center_selected_row_closure {
5775   GtkFileChooserDefault *impl;
5776   gboolean already_centered;
5777 };
5778
5779 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
5780  * selected row in the tree view.
5781  */
5782 static void
5783 center_selected_row_foreach_cb (GtkTreeModel      *model,
5784                                 GtkTreePath       *path,
5785                                 GtkTreeIter       *iter,
5786                                 gpointer           data)
5787 {
5788   struct center_selected_row_closure *closure;
5789
5790   closure = data;
5791   if (closure->already_centered)
5792     return;
5793
5794   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
5795   closure->already_centered = TRUE;
5796 }
5797
5798 /* Centers the selected row in the tree view */
5799 static void
5800 browse_files_center_selected_row (GtkFileChooserDefault *impl)
5801 {
5802   struct center_selected_row_closure closure;
5803   GtkTreeSelection *selection;
5804
5805   closure.impl = impl;
5806   closure.already_centered = FALSE;
5807
5808   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5809   gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
5810 }
5811
5812 struct ShowAndSelectPathsData
5813 {
5814   GtkFileChooserDefault *impl;
5815   GSList *paths;
5816 };
5817
5818 static void
5819 show_and_select_paths_finished_loading (GtkFileFolder *folder,
5820                                         gpointer user_data)
5821 {
5822   gboolean have_hidden;
5823   gboolean have_filtered;
5824   GSList *l;
5825   struct ShowAndSelectPathsData *data = user_data;
5826
5827   have_hidden = FALSE;
5828   have_filtered = FALSE;
5829
5830   for (l = data->paths; l; l = l->next)
5831     {
5832       const GtkFilePath *path;
5833       GtkFileInfo *info;
5834
5835       path = l->data;
5836
5837       /* NULL GError */
5838       info = gtk_file_folder_get_info (folder, path, NULL);
5839       if (info)
5840         {
5841           if (!have_hidden)
5842             have_hidden = gtk_file_info_get_is_hidden (info);
5843
5844           if (!have_filtered)
5845             have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
5846
5847           gtk_file_info_free (info);
5848
5849           if (have_hidden && have_filtered)
5850             break; /* we now have all the information we need */
5851         }
5852     }
5853
5854   g_signal_handlers_disconnect_by_func (folder,
5855                                         show_and_select_paths_finished_loading,
5856                                         user_data);
5857
5858   g_object_unref (folder);
5859
5860   if (have_hidden)
5861     g_object_set (data->impl, "show-hidden", TRUE, NULL);
5862
5863   if (have_filtered)
5864     set_current_filter (data->impl, NULL);
5865
5866   for (l = data->paths; l; l = l->next)
5867     {
5868       const GtkFilePath *path;
5869
5870       path = l->data;
5871       _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
5872                                       select_func, data->impl);
5873     }
5874
5875   browse_files_center_selected_row (data->impl);
5876
5877   g_object_unref (data->impl);
5878   gtk_file_paths_free (data->paths);
5879   g_free (data);
5880 }
5881
5882 static void
5883 show_and_select_paths_get_folder_cb (GtkFileSystemHandle   *handle,
5884                                      GtkFileFolder         *folder,
5885                                      const GError          *error,
5886                                      gpointer               user_data)
5887 {
5888   gboolean cancelled = handle->cancelled;
5889   struct ShowAndSelectPathsData *data = user_data;
5890
5891   if (data->impl->show_and_select_paths_handle != handle)
5892     goto out;
5893
5894   data->impl->show_and_select_paths_handle = NULL;
5895
5896   if (cancelled || error)
5897     goto out;
5898
5899   g_object_unref (handle);
5900
5901   if (gtk_file_folder_is_finished_loading (folder))
5902     show_and_select_paths_finished_loading (folder, user_data);
5903   else
5904     g_signal_connect (folder, "finished-loading",
5905                       G_CALLBACK (show_and_select_paths_finished_loading),
5906                       user_data);
5907
5908   return;
5909
5910 out:
5911   g_object_unref (data->impl);
5912   gtk_file_paths_free (data->paths);
5913   g_free (data);
5914
5915   g_object_unref (handle);
5916 }
5917
5918 static gboolean
5919 show_and_select_paths (GtkFileChooserDefault *impl,
5920                        const GtkFilePath     *parent_path,
5921                        GSList                *paths,
5922                        GError                **error)
5923 {
5924   struct ShowAndSelectPathsData *info;
5925
5926   profile_start ("start", NULL);
5927
5928   if (!paths)
5929     {
5930       profile_end ("end", NULL);
5931       return TRUE;
5932     }
5933
5934   info = g_new (struct ShowAndSelectPathsData, 1);
5935   info->impl = g_object_ref (impl);
5936   info->paths = gtk_file_paths_copy (paths);
5937
5938   if (impl->show_and_select_paths_handle)
5939     gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5940
5941   impl->show_and_select_paths_handle =
5942     gtk_file_system_get_folder (impl->file_system, parent_path,
5943                                 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
5944                                 show_and_select_paths_get_folder_cb, info);
5945
5946   profile_end ("end", NULL);
5947   return TRUE;
5948 }
5949
5950 /* Processes the pending operation when a folder is finished loading */
5951 static void
5952 pending_select_paths_process (GtkFileChooserDefault *impl)
5953 {
5954   g_assert (impl->load_state == LOAD_FINISHED);
5955   g_assert (impl->browse_files_model != NULL);
5956   g_assert (impl->sort_model != NULL);
5957
5958   if (impl->pending_select_paths)
5959     {
5960       /* NULL GError */
5961       show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
5962       pending_select_paths_free (impl);
5963       browse_files_center_selected_row (impl);
5964     }
5965   else
5966     {
5967       /* We only select the first row if the chooser is actually mapped ---
5968        * selecting the first row is to help the user when he is interacting with
5969        * the chooser, but sometimes a chooser works not on behalf of the user,
5970        * but rather on behalf of something else like GtkFileChooserButton.  In
5971        * that case, the chooser's selection should be what the caller expects,
5972        * as the user can't see that something else got selected.  See bug #165264.
5973        *
5974        * Also, we don't select the first file if we are not in OPEN mode.  Doing
5975        * so would change the contents of the filename entry for SAVE or
5976        * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
5977        * select a *different* folder from the one into which the user just
5978        * navigated.
5979        */
5980       if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5981         browse_files_select_first_row (impl);
5982     }
5983
5984   g_assert (impl->pending_select_paths == NULL);
5985 }
5986
5987 /* Callback used when the file system model finishes loading */
5988 static void
5989 browse_files_model_finished_loading_cb (GtkFileSystemModel    *model,
5990                                         GtkFileChooserDefault *impl)
5991 {
5992   profile_start ("start", NULL);
5993
5994   if (impl->load_state == LOAD_PRELOAD)
5995     {
5996       load_remove_timer (impl);
5997       load_set_model (impl);
5998     }
5999   else if (impl->load_state == LOAD_LOADING)
6000     {
6001       /* Nothing */
6002     }
6003   else
6004     {
6005       /* We can't g_assert_not_reached(), as something other than us may have
6006        *  initiated a folder reload.  See #165556.
6007        */
6008       profile_end ("end", NULL);
6009       return;
6010     }
6011
6012   g_assert (impl->load_timeout_id == 0);
6013
6014   impl->load_state = LOAD_FINISHED;
6015
6016   pending_select_paths_process (impl);
6017   set_busy_cursor (impl, FALSE);
6018 #ifdef PROFILE_FILE_CHOOSER
6019   access ("MARK: *** FINISHED LOADING", F_OK);
6020 #endif
6021
6022   profile_end ("end", NULL);
6023 }
6024
6025 /* Gets rid of the old list model and creates a new one for the current folder */
6026 static gboolean
6027 set_list_model (GtkFileChooserDefault *impl,
6028                 GError               **error)
6029 {
6030   g_assert (impl->current_folder != NULL);
6031
6032   profile_start ("start", NULL);
6033
6034   load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6035
6036   if (impl->browse_files_model)
6037     {
6038       g_object_unref (impl->browse_files_model);
6039       impl->browse_files_model = NULL;
6040     }
6041
6042   if (impl->sort_model)
6043     {
6044       g_object_unref (impl->sort_model);
6045       impl->sort_model = NULL;
6046     }
6047
6048   set_busy_cursor (impl, TRUE);
6049   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6050
6051   impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6052                                                          impl->current_folder, 0,
6053                                                          GTK_FILE_INFO_ALL,
6054                                                          error);
6055   if (!impl->browse_files_model)
6056     {
6057       set_busy_cursor (impl, FALSE);
6058       profile_end ("end", NULL);
6059       return FALSE;
6060     }
6061
6062   load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6063
6064   g_signal_connect (impl->browse_files_model, "finished-loading",
6065                     G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6066
6067   _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6068
6069   install_list_model_filter (impl);
6070
6071   profile_end ("end", NULL);
6072
6073   return TRUE;
6074 }
6075
6076 struct update_chooser_entry_selected_foreach_closure {
6077   int num_selected;
6078   GtkTreeIter first_selected_iter;
6079 };
6080
6081 static gint
6082 compare_utf8_filenames (const gchar *a,
6083                         const gchar *b)
6084 {
6085   gchar *a_folded, *b_folded;
6086   gint retval;
6087
6088   a_folded = g_utf8_strdown (a, -1);
6089   b_folded = g_utf8_strdown (b, -1);
6090
6091   retval = strcmp (a_folded, b_folded);
6092
6093   g_free (a_folded);
6094   g_free (b_folded);
6095
6096   return retval;
6097 }
6098
6099 static void
6100 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6101                                        GtkTreePath *path,
6102                                        GtkTreeIter *iter,
6103                                        gpointer data)
6104 {
6105   struct update_chooser_entry_selected_foreach_closure *closure;
6106
6107   closure = data;
6108   closure->num_selected++;
6109
6110   if (closure->num_selected == 1)
6111     closure->first_selected_iter = *iter;
6112 }
6113
6114 static void
6115 update_chooser_entry (GtkFileChooserDefault *impl)
6116 {
6117   GtkTreeSelection *selection;
6118   struct update_chooser_entry_selected_foreach_closure closure;
6119   const char *file_part;
6120
6121   if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6122         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6123         || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6124              || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6125             && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6126     return;
6127
6128   g_assert (impl->location_entry != NULL);
6129
6130   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6131   closure.num_selected = 0;
6132   gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6133
6134   file_part = NULL;
6135
6136   if (closure.num_selected == 0)
6137     {
6138       goto maybe_clear_entry;
6139     }
6140   else if (closure.num_selected == 1)
6141     {
6142       GtkTreeIter child_iter;
6143       const GtkFileInfo *info;
6144       gboolean change_entry;
6145
6146       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6147                                                       &child_iter,
6148                                                       &closure.first_selected_iter);
6149
6150       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6151
6152       g_free (impl->browse_files_last_selected_name);
6153       impl->browse_files_last_selected_name = g_strdup (gtk_file_info_get_display_name (info));
6154
6155       if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6156           || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6157         change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6158       else
6159         change_entry = TRUE;                                /* ... unless we are in one of the folder modes */
6160
6161       if (change_entry)
6162         _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6163
6164       return;
6165     }
6166   else
6167     {
6168       g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6169                   || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6170
6171       /* Multiple selection, so just clear the entry. */
6172
6173       g_free (impl->browse_files_last_selected_name);
6174       impl->browse_files_last_selected_name = NULL;
6175
6176       _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6177       return;
6178     }
6179
6180  maybe_clear_entry:
6181
6182   if (impl->browse_files_last_selected_name)
6183     {
6184       const char *entry_text;
6185       int len;
6186       gboolean clear_entry;
6187
6188       entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6189       len = strlen (entry_text);
6190       if (len != 0)
6191         {
6192           /* The file chooser entry may have appended a "/" to its text.  So
6193            * take it out, and compare the result to the old selection.
6194            */
6195           if (entry_text[len - 1] == G_DIR_SEPARATOR)
6196             {
6197               char *tmp;
6198
6199               tmp = g_strndup (entry_text, len - 1);
6200               clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6201               g_free (tmp);
6202             }
6203           else
6204             clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6205         }
6206       else
6207         clear_entry = FALSE;
6208
6209       if (clear_entry)
6210         _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6211     }
6212 }
6213
6214 static gboolean
6215 gtk_file_chooser_default_set_current_folder (GtkFileChooser    *chooser,
6216                                              const GtkFilePath *path,
6217                                              GError           **error)
6218 {
6219   return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, error);
6220 }
6221
6222
6223 struct UpdateCurrentFolderData
6224 {
6225   GtkFileChooserDefault *impl;
6226   GtkFilePath *path;
6227   gboolean keep_trail;
6228   GtkFilePath *original_path;
6229   GError *original_error;
6230 };
6231
6232 static void
6233 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6234                                    const GtkFileInfo   *info,
6235                                    const GError        *error,
6236                                    gpointer             user_data)
6237 {
6238   gboolean cancelled = handle->cancelled;
6239   struct UpdateCurrentFolderData *data = user_data;
6240   GtkFileChooserDefault *impl = data->impl;
6241
6242   if (handle != impl->update_current_folder_handle)
6243     goto out;
6244
6245   impl->update_current_folder_handle = NULL;
6246   impl->reload_state = RELOAD_EMPTY;
6247
6248   set_busy_cursor (impl, FALSE);
6249
6250   if (cancelled)
6251     goto out;
6252
6253   if (error)
6254     {
6255       GtkFilePath *parent_path;
6256
6257       if (!data->original_path)
6258         {
6259           data->original_path = gtk_file_path_copy (data->path);
6260           data->original_error = g_error_copy (error);
6261         }
6262
6263       /* get parent path and try to change the folder to that */
6264       if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6265           parent_path != NULL)
6266         {
6267           gtk_file_path_free (data->path);
6268           data->path = parent_path;
6269
6270           g_object_unref (handle);
6271
6272           /* restart the update current folder operation */
6273           impl->reload_state = RELOAD_HAS_FOLDER;
6274
6275           impl->update_current_folder_handle =
6276             gtk_file_system_get_info (impl->file_system, data->path,
6277                                       GTK_FILE_INFO_IS_FOLDER,
6278                                       update_current_folder_get_info_cb,
6279                                       data);
6280
6281           set_busy_cursor (impl, TRUE);
6282
6283           return;
6284         }
6285       else
6286         {
6287           /* error and bail out */
6288           error_changing_folder_dialog (impl, data->original_path, data->original_error);
6289
6290           gtk_file_path_free (data->original_path);
6291
6292           goto out;
6293         }
6294     }
6295
6296   if (data->original_path)
6297     {
6298       error_changing_folder_dialog (impl, data->original_path, data->original_error);
6299
6300       gtk_file_path_free (data->original_path);
6301     }
6302
6303   if (!gtk_file_info_get_is_folder (info))
6304     goto out;
6305
6306   if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
6307     goto out;
6308
6309   if (impl->current_folder != data->path)
6310     {
6311       if (impl->current_folder)
6312         gtk_file_path_free (impl->current_folder);
6313
6314       impl->current_folder = gtk_file_path_copy (data->path);
6315
6316       impl->reload_state = RELOAD_HAS_FOLDER;
6317     }
6318
6319   /* Update the widgets that may trigger a folder change themselves.  */
6320
6321   if (!impl->changing_folder)
6322     {
6323       impl->changing_folder = TRUE;
6324
6325       shortcuts_update_current_folder (impl);
6326
6327       impl->changing_folder = FALSE;
6328     }
6329
6330   /* Set the folder on the save entry */
6331
6332   if (impl->location_entry)
6333     _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6334                                              impl->current_folder);
6335
6336   /* Create a new list model.  This is slightly evil; we store the result value
6337    * but perform more actions rather than returning immediately even if it
6338    * generates an error.
6339    */
6340   set_list_model (impl, NULL);
6341
6342   /* Refresh controls */
6343
6344   shortcuts_find_current_folder (impl);
6345
6346   g_signal_emit_by_name (impl, "current-folder-changed", 0);
6347
6348   check_preview_change (impl);
6349   bookmarks_check_add_sensitivity (impl);
6350
6351   g_signal_emit_by_name (impl, "selection-changed", 0);
6352
6353 out:
6354   gtk_file_path_free (data->path);
6355   g_free (data);
6356
6357   g_object_unref (handle);
6358 }
6359
6360 static gboolean
6361 gtk_file_chooser_default_update_current_folder (GtkFileChooser    *chooser,
6362                                                 const GtkFilePath *path,
6363                                                 gboolean           keep_trail,
6364                                                 GError           **error)
6365 {
6366   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6367   struct UpdateCurrentFolderData *data;
6368
6369   profile_start ("start", (char *) path);
6370
6371   g_assert (path != NULL);
6372
6373   if (impl->local_only &&
6374       !gtk_file_system_path_is_local (impl->file_system, path))
6375     {
6376       g_set_error (error,
6377                    GTK_FILE_CHOOSER_ERROR,
6378                    GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6379                    _("Cannot change to folder because it is not local"));
6380
6381       profile_end ("end - not local", (char *) path);
6382       return FALSE;
6383     }
6384
6385   if (impl->update_current_folder_handle)
6386     gtk_file_system_cancel_operation (impl->update_current_folder_handle);
6387
6388   /* Test validity of path here.  */
6389   data = g_new0 (struct UpdateCurrentFolderData, 1);
6390   data->impl = impl;
6391   data->path = gtk_file_path_copy (path);
6392   data->keep_trail = keep_trail;
6393
6394   impl->reload_state = RELOAD_HAS_FOLDER;
6395
6396   impl->update_current_folder_handle =
6397     gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
6398                               update_current_folder_get_info_cb,
6399                               data);
6400
6401   set_busy_cursor (impl, TRUE);
6402
6403   profile_end ("end", NULL);
6404   return TRUE;
6405 }
6406
6407 static GtkFilePath *
6408 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6409 {
6410   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6411
6412   if (impl->reload_state == RELOAD_EMPTY)
6413     {
6414       char *current_working_dir;
6415       GtkFilePath *path;
6416
6417       /* We are unmapped, or we had an error while loading the last folder.  We'll return
6418        * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6419        * explicitly calls set_current_folder() on us.
6420        */
6421       current_working_dir = g_get_current_dir ();
6422       path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
6423       g_free (current_working_dir);
6424       return path;
6425     }
6426
6427   return gtk_file_path_copy (impl->current_folder);
6428 }
6429
6430 static void
6431 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
6432                                            const gchar    *name)
6433 {
6434   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6435
6436   g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6437                     || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6438
6439   pending_select_paths_free (impl);
6440   _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
6441 }
6442
6443 static void
6444 select_func (GtkFileSystemModel *model,
6445              GtkTreePath        *path,
6446              GtkTreeIter        *iter,
6447              gpointer            user_data)
6448 {
6449   GtkFileChooserDefault *impl = user_data;
6450   GtkTreeSelection *selection;
6451   GtkTreeIter sorted_iter;
6452
6453   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6454
6455   gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
6456   gtk_tree_selection_select_iter (selection, &sorted_iter);
6457 }
6458
6459 static gboolean
6460 gtk_file_chooser_default_select_path (GtkFileChooser    *chooser,
6461                                       const GtkFilePath *path,
6462                                       GError           **error)
6463 {
6464   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6465   GtkFilePath *parent_path;
6466   gboolean same_path;
6467
6468   if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
6469     return FALSE;
6470
6471   if (!parent_path)
6472     return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
6473
6474   if (impl->load_state == LOAD_EMPTY)
6475     same_path = FALSE;
6476   else
6477     {
6478       g_assert (impl->current_folder != NULL);
6479
6480       same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
6481     }
6482
6483   if (same_path && impl->load_state == LOAD_FINISHED)
6484     {
6485       gboolean result;
6486       GSList paths;
6487
6488       paths.data = (gpointer) path;
6489       paths.next = NULL;
6490
6491       result = show_and_select_paths (impl, parent_path, &paths, error);
6492       gtk_file_path_free (parent_path);
6493       return result;
6494     }
6495
6496   pending_select_paths_add (impl, path);
6497
6498   if (!same_path)
6499     {
6500       gboolean result;
6501
6502       result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
6503       gtk_file_path_free (parent_path);
6504       return result;
6505     }
6506
6507   gtk_file_path_free (parent_path);
6508   return TRUE;
6509 }
6510
6511 static void
6512 unselect_func (GtkFileSystemModel *model,
6513                GtkTreePath        *path,
6514                GtkTreeIter        *iter,
6515                gpointer            user_data)
6516 {
6517   GtkFileChooserDefault *impl = user_data;
6518   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
6519   GtkTreePath *sorted_path;
6520
6521   sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
6522                                                                 path);
6523   gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
6524                                     sorted_path);
6525   gtk_tree_path_free (sorted_path);
6526 }
6527
6528 static void
6529 gtk_file_chooser_default_unselect_path (GtkFileChooser    *chooser,
6530                                         const GtkFilePath *path)
6531 {
6532   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6533
6534   if (!impl->browse_files_model)
6535     return;
6536
6537   _gtk_file_system_model_path_do (impl->browse_files_model, path,
6538                                   unselect_func, impl);
6539 }
6540
6541 static gboolean
6542 maybe_select (GtkTreeModel *model, 
6543               GtkTreePath  *path, 
6544               GtkTreeIter  *iter, 
6545               gpointer     data)
6546 {
6547   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
6548   GtkTreeSelection *selection;
6549   const GtkFileInfo *info;
6550   gboolean is_folder;
6551   
6552   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6553   
6554   info = get_list_file_info (impl, iter);
6555   is_folder = gtk_file_info_get_is_folder (info);
6556
6557   if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
6558       (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
6559     gtk_tree_selection_select_iter (selection, iter);
6560   else
6561     gtk_tree_selection_unselect_iter (selection, iter);
6562     
6563   return FALSE;
6564 }
6565
6566 static void
6567 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
6568 {
6569   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6570   if (impl->select_multiple)
6571     gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model), 
6572                             maybe_select, impl);
6573 }
6574
6575 static void
6576 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
6577 {
6578   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6579   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6580
6581   gtk_tree_selection_unselect_all (selection);
6582   pending_select_paths_free (impl);
6583 }
6584
6585 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
6586  *
6587  * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
6588  *
6589  * is_empty_ret - whether the file entry is totally empty
6590  *
6591  * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
6592  *                          the path will be "$cwd/foobar")
6593  */
6594 static void
6595 check_save_entry (GtkFileChooserDefault *impl,
6596                   GtkFilePath          **path_ret,
6597                   gboolean              *is_well_formed_ret,
6598                   gboolean              *is_empty_ret,
6599                   gboolean              *is_file_part_empty_ret,
6600                   gboolean              *is_folder)
6601 {
6602   GtkFileChooserEntry *chooser_entry;
6603   const GtkFilePath *current_folder;
6604   const char *file_part;
6605   GtkFilePath *path;
6606   GError *error;
6607
6608   g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6609             || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6610             || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6611                  || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6612                 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
6613
6614   chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
6615
6616   if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
6617     {
6618       *path_ret = NULL;
6619       *is_well_formed_ret = TRUE;
6620       *is_empty_ret = TRUE;
6621       *is_file_part_empty_ret = TRUE;
6622       *is_folder = FALSE;
6623
6624       return;
6625     }
6626
6627   *is_empty_ret = FALSE;
6628
6629   current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6630   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6631
6632   if (!file_part || file_part[0] == '\0')
6633     {
6634       *path_ret = gtk_file_path_copy (current_folder);
6635       *is_well_formed_ret = TRUE;
6636       *is_file_part_empty_ret = TRUE;
6637       *is_folder = TRUE;
6638
6639       return;
6640     }
6641
6642   *is_file_part_empty_ret = FALSE;
6643
6644   error = NULL;
6645   path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
6646
6647   if (!path)
6648     {
6649       error_building_filename_dialog (impl, current_folder, file_part, error);
6650       *path_ret = NULL;
6651       *is_well_formed_ret = FALSE;
6652       *is_folder = FALSE;
6653
6654       return;
6655     }
6656
6657   *path_ret = path;
6658   *is_well_formed_ret = TRUE;
6659   *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
6660 }
6661
6662 struct get_paths_closure {
6663   GtkFileChooserDefault *impl;
6664   GSList *result;
6665   GtkFilePath *path_from_entry;
6666 };
6667
6668 static void
6669 get_paths_foreach (GtkTreeModel *model,
6670                    GtkTreePath  *path,
6671                    GtkTreeIter  *iter,
6672                    gpointer      data)
6673 {
6674   struct get_paths_closure *info;
6675   const GtkFilePath *file_path;
6676   GtkFileSystemModel *fs_model;
6677   GtkTreeIter sel_iter;
6678
6679   info = data;
6680   fs_model = info->impl->browse_files_model;
6681   gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
6682
6683   file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
6684   if (!file_path)
6685     return; /* We are on the editable row */
6686
6687   if (!info->path_from_entry
6688       || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
6689     info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
6690 }
6691
6692 static GSList *
6693 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
6694 {
6695   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6696   struct get_paths_closure info;
6697   GtkWindow *toplevel;
6698   GtkWidget *current_focus;
6699
6700   info.impl = impl;
6701   info.result = NULL;
6702   info.path_from_entry = NULL;
6703
6704   toplevel = get_toplevel (GTK_WIDGET (impl));
6705   if (toplevel)
6706     current_focus = gtk_window_get_focus (toplevel);
6707   else
6708     current_focus = NULL;
6709
6710   if (current_focus == impl->browse_files_tree_view)
6711     {
6712       GtkTreeSelection *selection;
6713
6714     file_list:
6715
6716       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6717       gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
6718
6719       /* If there is no selection in the file list, we probably have this situation:
6720        *
6721        * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
6722        * 2. He then double-clicked on a folder ("bar") in the file list
6723        *
6724        * So we want the selection to be "bar/foo.txt".  Jump to the case for the
6725        * filename entry to see if that is the case.
6726        */
6727       if (info.result == NULL && impl->location_entry)
6728         goto file_entry;
6729     }
6730   else if (impl->location_entry && current_focus == impl->location_entry)
6731     {
6732       gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
6733
6734     file_entry:
6735
6736       check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
6737
6738       if (is_empty)
6739         goto out;
6740
6741       if (!is_well_formed)
6742         return NULL;
6743
6744       if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6745         {
6746           gtk_file_path_free (info.path_from_entry);
6747           return NULL;
6748         }
6749
6750       g_assert (info.path_from_entry != NULL);
6751       info.result = g_slist_prepend (info.result, info.path_from_entry);
6752     }
6753   else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
6754     goto file_list;
6755   else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
6756     goto file_entry;
6757   else
6758     {
6759     /* The focus is on a dialog's action area button or something else */
6760       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6761           || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6762         goto file_entry;
6763       else
6764         goto file_list; 
6765     }
6766
6767  out:
6768
6769   /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
6770    * fall back to the current directory */
6771   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
6772       info.result == NULL)
6773     {
6774       info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
6775     }
6776
6777   return g_slist_reverse (info.result);
6778 }
6779
6780 static GtkFilePath *
6781 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
6782 {
6783   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6784
6785   if (impl->preview_path)
6786     return gtk_file_path_copy (impl->preview_path);
6787   else
6788     return NULL;
6789 }
6790
6791 static GtkFileSystem *
6792 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
6793 {
6794   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6795
6796   return impl->file_system;
6797 }
6798
6799 /* Shows or hides the filter widgets */
6800 static void
6801 show_filters (GtkFileChooserDefault *impl,
6802               gboolean               show)
6803 {
6804   if (show)
6805     gtk_widget_show (impl->filter_combo_hbox);
6806   else
6807     gtk_widget_hide (impl->filter_combo_hbox);
6808 }
6809
6810 static void
6811 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
6812                                      GtkFileFilter  *filter)
6813 {
6814   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6815   const gchar *name;
6816
6817   if (g_slist_find (impl->filters, filter))
6818     {
6819       g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
6820       return;
6821     }
6822
6823   g_object_ref_sink (filter);
6824   impl->filters = g_slist_append (impl->filters, filter);
6825
6826   name = gtk_file_filter_get_name (filter);
6827   if (!name)
6828     name = "Untitled filter";   /* Place-holder, doesn't need to be marked for translation */
6829
6830   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
6831
6832   if (!g_slist_find (impl->filters, impl->current_filter))
6833     set_current_filter (impl, filter);
6834
6835   show_filters (impl, TRUE);
6836 }
6837
6838 static void
6839 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
6840                                         GtkFileFilter  *filter)
6841 {
6842   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6843   GtkTreeModel *model;
6844   GtkTreeIter iter;
6845   gint filter_index;
6846
6847   filter_index = g_slist_index (impl->filters, filter);
6848
6849   if (filter_index < 0)
6850     {
6851       g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
6852       return;
6853     }
6854
6855   impl->filters = g_slist_remove (impl->filters, filter);
6856
6857   if (filter == impl->current_filter)
6858     {
6859       if (impl->filters)
6860         set_current_filter (impl, impl->filters->data);
6861       else
6862         set_current_filter (impl, NULL);
6863     }
6864
6865   /* Remove row from the combo box */
6866   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
6867   if (!gtk_tree_model_iter_nth_child  (model, &iter, NULL, filter_index))
6868     g_assert_not_reached ();
6869
6870   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
6871
6872   g_object_unref (filter);
6873
6874   if (!impl->filters)
6875     show_filters (impl, FALSE);
6876 }
6877
6878 static GSList *
6879 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
6880 {
6881   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6882
6883   return g_slist_copy (impl->filters);
6884 }
6885
6886 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
6887 static int
6888 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
6889                                        int                    pos)
6890 {
6891   return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
6892 }
6893
6894 struct AddShortcutData
6895 {
6896   GtkFileChooserDefault *impl;
6897   GtkFilePath *path;
6898 };
6899
6900 static void
6901 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
6902                           const GtkFileInfo   *info,
6903                           const GError        *error,
6904                           gpointer             user_data)
6905 {
6906   int pos;
6907   gboolean cancelled = handle->cancelled;
6908   struct AddShortcutData *data = user_data;
6909
6910   if (!g_slist_find (data->impl->loading_shortcuts, handle))
6911     goto out;
6912
6913   data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
6914
6915   if (cancelled || error || !gtk_file_info_get_is_folder (info))
6916     goto out;
6917
6918   pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
6919
6920   shortcuts_insert_path (data->impl, pos, FALSE, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
6921
6922 out:
6923   g_object_unref (data->impl);
6924   gtk_file_path_free (data->path);
6925   g_free (data);
6926
6927   g_object_unref (handle);
6928 }
6929
6930 static gboolean
6931 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser    *chooser,
6932                                               const GtkFilePath *path,
6933                                               GError           **error)
6934 {
6935   GtkFileSystemHandle *handle;
6936   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6937   struct AddShortcutData *data;
6938   GSList *l;
6939   int pos;
6940
6941   /* Avoid adding duplicates */
6942   pos = shortcut_find_position (impl, path);
6943   if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
6944     {
6945       gchar *uri;
6946
6947       uri = gtk_file_system_path_to_uri (impl->file_system, path);
6948       /* translators, "Shortcut" means "Bookmark" here */
6949       g_set_error (error,
6950                    GTK_FILE_CHOOSER_ERROR,
6951                    GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6952                    _("Shortcut %s already exists"),
6953                    uri);
6954       g_free (uri);
6955
6956       return FALSE;
6957     }
6958
6959   for (l = impl->loading_shortcuts; l; l = l->next)
6960     {
6961       GtkFileSystemHandle *h = l->data;
6962       GtkFilePath *p;
6963
6964       p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
6965       if (p && !gtk_file_path_compare (path, p))
6966         {
6967           gchar *uri;
6968
6969           uri = gtk_file_system_path_to_uri (impl->file_system, path);
6970           g_set_error (error,
6971                        GTK_FILE_CHOOSER_ERROR,
6972                        GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6973                        _("Shortcut %s already exists"),
6974                        uri);
6975           g_free (uri);
6976
6977           return FALSE;
6978         }
6979     }
6980
6981   data = g_new0 (struct AddShortcutData, 1);
6982   data->impl = g_object_ref (impl);
6983   data->path = gtk_file_path_copy (path);
6984
6985   handle = gtk_file_system_get_info (impl->file_system, path,
6986                                      GTK_FILE_INFO_IS_FOLDER,
6987                                      add_shortcut_get_info_cb, data);
6988
6989   if (!handle)
6990     return FALSE;
6991
6992   impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
6993   g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
6994
6995   return TRUE;
6996 }
6997
6998 static gboolean
6999 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
7000                                                  const GtkFilePath *path,
7001                                                  GError           **error)
7002 {
7003   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7004   int pos;
7005   GtkTreeIter iter;
7006   GSList *l;
7007   char *uri;
7008   int i;
7009
7010   for (l = impl->loading_shortcuts; l; l = l->next)
7011     {
7012       GtkFileSystemHandle *h = l->data;
7013       GtkFilePath *p;
7014
7015       p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7016       if (p && !gtk_file_path_compare (path, p))
7017         {
7018           impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7019           gtk_file_system_cancel_operation (h);
7020           return TRUE;
7021         }
7022     }
7023
7024   if (impl->num_shortcuts == 0)
7025     goto out;
7026
7027   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7028   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7029     g_assert_not_reached ();
7030
7031   for (i = 0; i < impl->num_shortcuts; i++)
7032     {
7033       gpointer col_data;
7034       gboolean is_volume;
7035       GtkFilePath *shortcut;
7036
7037       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7038                           SHORTCUTS_COL_DATA, &col_data,
7039                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
7040                           -1);
7041       g_assert (col_data != NULL);
7042       g_assert (!is_volume);
7043
7044       shortcut = col_data;
7045       if (gtk_file_path_compare (shortcut, path) == 0)
7046         {
7047           shortcuts_remove_rows (impl, pos + i, 1);
7048           impl->num_shortcuts--;
7049           return TRUE;
7050         }
7051
7052       if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7053         g_assert_not_reached ();
7054     }
7055
7056  out:
7057
7058   uri = gtk_file_system_path_to_uri (impl->file_system, path);
7059   /* translators, "Shortcut" means "Bookmark" here */
7060   g_set_error (error,
7061                GTK_FILE_CHOOSER_ERROR,
7062                GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7063                _("Shortcut %s does not exist"),
7064                uri);
7065   g_free (uri);
7066
7067   return FALSE;
7068 }
7069
7070 static GSList *
7071 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7072 {
7073   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7074   int pos;
7075   GtkTreeIter iter;
7076   int i;
7077   GSList *list;
7078
7079   if (impl->num_shortcuts == 0)
7080     return NULL;
7081
7082   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7083   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7084     g_assert_not_reached ();
7085
7086   list = NULL;
7087
7088   for (i = 0; i < impl->num_shortcuts; i++)
7089     {
7090       gpointer col_data;
7091       gboolean is_volume;
7092       GtkFilePath *shortcut;
7093
7094       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7095                           SHORTCUTS_COL_DATA, &col_data,
7096                           SHORTCUTS_COL_IS_VOLUME, &is_volume,
7097                           -1);
7098       g_assert (col_data != NULL);
7099       g_assert (!is_volume);
7100
7101       shortcut = col_data;
7102       list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7103
7104       if (i != impl->num_shortcuts - 1)
7105         {
7106           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7107             g_assert_not_reached ();
7108         }
7109     }
7110
7111   return g_slist_reverse (list);
7112 }
7113
7114 /* Guesses a size based upon font sizes */
7115 static void
7116 find_good_size_from_style (GtkWidget *widget,
7117                            gint      *width,
7118                            gint      *height)
7119 {
7120   GtkFileChooserDefault *impl;
7121   gint default_width, default_height;
7122   int font_size;
7123   GtkRequisition req;
7124   GtkRequisition preview_req;
7125
7126   g_assert (widget->style != NULL);
7127   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7128
7129   font_size = pango_font_description_get_size (widget->style->font_desc);
7130   font_size = PANGO_PIXELS (font_size);
7131
7132   default_width = font_size * NUM_CHARS;
7133   default_height = font_size * NUM_LINES;
7134
7135   /* Use at least the requisition size not including the preview widget */
7136   gtk_widget_size_request (widget, &req);
7137
7138   if (impl->preview_widget_active && impl->preview_widget)
7139     gtk_widget_size_request (impl->preview_box, &preview_req);
7140   else
7141     preview_req.width = 0;
7142
7143   default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
7144   default_height = MAX (default_height, req.height);
7145
7146   *width = default_width;
7147   *height = default_height;
7148 }
7149
7150 static void
7151 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7152                                            gint                *default_width,
7153                                            gint                *default_height)
7154 {
7155   GtkFileChooserDefault *impl;
7156
7157   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7158
7159   find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7160
7161   if (impl->preview_widget_active && impl->preview_widget)
7162     *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
7163 }
7164
7165 static void
7166 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
7167                                               gboolean            *resize_horizontally,
7168                                               gboolean            *resize_vertically)
7169 {
7170   GtkFileChooserDefault *impl;
7171
7172   g_return_if_fail (resize_horizontally != NULL);
7173   g_return_if_fail (resize_vertically != NULL);
7174
7175   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7176
7177   *resize_horizontally = TRUE;
7178   *resize_vertically = TRUE;
7179
7180   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7181       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7182     {
7183       if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7184         {
7185           *resize_horizontally = FALSE;
7186           *resize_vertically = FALSE;
7187         }
7188     }
7189 }
7190
7191 struct switch_folder_closure {
7192   GtkFileChooserDefault *impl;
7193   const GtkFilePath *path;
7194   int num_selected;
7195 };
7196
7197 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7198 static void
7199 switch_folder_foreach_cb (GtkTreeModel      *model,
7200                           GtkTreePath       *path,
7201                           GtkTreeIter       *iter,
7202                           gpointer           data)
7203 {
7204   struct switch_folder_closure *closure;
7205   GtkTreeIter child_iter;
7206
7207   closure = data;
7208
7209   gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7210
7211   closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7212   closure->num_selected++;
7213 }
7214
7215 /* Changes to the selected folder in the list view */
7216 static void
7217 switch_to_selected_folder (GtkFileChooserDefault *impl)
7218 {
7219   GtkTreeSelection *selection;
7220   struct switch_folder_closure closure;
7221
7222   /* We do this with foreach() rather than get_selected() as we may be in
7223    * multiple selection mode
7224    */
7225
7226   closure.impl = impl;
7227   closure.path = NULL;
7228   closure.num_selected = 0;
7229
7230   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7231   gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7232
7233   g_assert (closure.path && closure.num_selected == 1);
7234
7235   change_folder_and_display_error (impl, closure.path);
7236 }
7237
7238 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7239  * selection mode.
7240  */
7241 static const GtkFileInfo *
7242 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7243                                        gboolean              *had_selection)
7244 {
7245   GtkTreeSelection *selection;
7246   GtkTreeIter iter, child_iter;
7247   const GtkFileInfo *info;
7248
7249   g_assert (!impl->select_multiple);
7250   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7251   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7252     {
7253       *had_selection = FALSE;
7254       return NULL;
7255     }
7256
7257   *had_selection = TRUE;
7258
7259   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7260                                                   &child_iter,
7261                                                   &iter);
7262
7263   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7264   return info;
7265 }
7266
7267 /* Gets the display name of the selected file in the file list; assumes single
7268  * selection mode and that something is selected.
7269  */
7270 static const gchar *
7271 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7272 {
7273   const GtkFileInfo *info;
7274   gboolean had_selection;
7275
7276   info = get_selected_file_info_from_file_list (impl, &had_selection);
7277   g_assert (had_selection);
7278   g_assert (info != NULL);
7279
7280   return gtk_file_info_get_display_name (info);
7281 }
7282
7283 static void
7284 add_custom_button_to_dialog (GtkDialog   *dialog,
7285                              const gchar *mnemonic_label,
7286                              const gchar *stock_id,
7287                              gint         response_id)
7288 {
7289   GtkWidget *button;
7290
7291   button = gtk_button_new_with_mnemonic (mnemonic_label);
7292   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7293   gtk_button_set_image (GTK_BUTTON (button),
7294                         gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7295   gtk_widget_show (button);
7296
7297   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7298 }
7299
7300 /* Presents an overwrite confirmation dialog; returns whether we should accept
7301  * the filename.
7302  */
7303 static gboolean
7304 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7305                                        const gchar           *file_part,
7306                                        const gchar           *folder_display_name)
7307 {
7308   GtkWindow *toplevel;
7309   GtkWidget *dialog;
7310   int response;
7311
7312   toplevel = get_toplevel (GTK_WIDGET (impl));
7313
7314   dialog = gtk_message_dialog_new (toplevel,
7315                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7316                                    GTK_MESSAGE_QUESTION,
7317                                    GTK_BUTTONS_NONE,
7318                                    _("A file named \"%s\" already exists.  Do you want to replace it?"),
7319                                    file_part);
7320   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7321                                             _("The file already exists in \"%s\".  Replacing it will "
7322                                               "overwrite its contents."),
7323                                             folder_display_name);
7324
7325   gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7326   add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7327   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7328   
7329   if (toplevel->group)
7330     gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7331
7332   response = gtk_dialog_run (GTK_DIALOG (dialog));
7333
7334   gtk_widget_destroy (dialog);
7335
7336   return (response == GTK_RESPONSE_ACCEPT);
7337 }
7338
7339 struct GetDisplayNameData
7340 {
7341   GtkFileChooserDefault *impl;
7342   gchar *file_part;
7343 };
7344
7345 static void
7346 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
7347                                   const GtkFileInfo   *info,
7348                                   const GError        *error,
7349                                   gpointer             user_data)
7350 {
7351   gboolean cancelled = handle->cancelled;
7352   gboolean should_respond = FALSE;
7353   struct GetDisplayNameData *data = user_data;
7354
7355   if (handle != data->impl->should_respond_get_info_handle)
7356     goto out;
7357
7358   data->impl->should_respond_get_info_handle = NULL;
7359
7360   if (cancelled)
7361     goto out;
7362
7363   if (error)
7364     /* Huh?  Did the folder disappear?  Let the caller deal with it */
7365     should_respond = TRUE;
7366   else
7367     should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
7368
7369   set_busy_cursor (data->impl, FALSE);
7370   if (should_respond)
7371     g_signal_emit_by_name (data->impl, "response-requested");
7372
7373 out:
7374   g_object_unref (data->impl);
7375   g_free (data->file_part);
7376   g_free (data);
7377
7378   g_object_unref (handle);
7379 }
7380
7381 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7382  * should respond.  Can get the file part from the file list or the save entry.
7383  */
7384 static gboolean
7385 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7386                                         const gchar           *file_part,
7387                                         const GtkFilePath     *parent_path)
7388 {
7389   GtkFileChooserConfirmation conf;
7390
7391   if (!impl->do_overwrite_confirmation)
7392     return TRUE;
7393
7394   conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
7395
7396   g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
7397
7398   switch (conf)
7399     {
7400     case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
7401       {
7402         struct GetDisplayNameData *data;
7403
7404         g_assert (file_part != NULL);
7405
7406         data = g_new0 (struct GetDisplayNameData, 1);
7407         data->impl = g_object_ref (impl);
7408         data->file_part = g_strdup (file_part);
7409
7410         if (impl->should_respond_get_info_handle)
7411           gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7412
7413         impl->should_respond_get_info_handle =
7414           gtk_file_system_get_info (impl->file_system, parent_path,
7415                                     GTK_FILE_INFO_DISPLAY_NAME,
7416                                     confirmation_confirm_get_info_cb,
7417                                     data);
7418         set_busy_cursor (data->impl, TRUE);
7419         return FALSE;
7420       }
7421
7422     case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
7423       return TRUE;
7424
7425     case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
7426       return FALSE;
7427
7428     default:
7429       g_assert_not_reached ();
7430       return FALSE;
7431     }
7432 }
7433
7434 static void
7435 action_create_folder_cb (GtkFileSystemHandle *handle,
7436                          const GtkFilePath   *path,
7437                          const GError        *error,
7438                          gpointer             user_data)
7439 {
7440   gboolean cancelled = handle->cancelled;
7441   GtkFileChooserDefault *impl = user_data;
7442
7443   if (!g_slist_find (impl->pending_handles, handle))
7444     goto out;
7445
7446   impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
7447
7448   set_busy_cursor (impl, FALSE);
7449
7450   if (cancelled)
7451     goto out;
7452
7453   if (error)
7454     error_creating_folder_dialog (impl, path, g_error_copy (error));
7455   else
7456     g_signal_emit_by_name (impl, "response-requested");
7457
7458 out:
7459   g_object_unref (impl);
7460   g_object_unref (handle);
7461 }
7462
7463 struct FileExistsData
7464 {
7465   GtkFileChooserDefault *impl;
7466   gboolean file_exists_and_is_not_folder;
7467   GtkFilePath *parent_path;
7468   GtkFilePath *path;
7469 };
7470
7471 static void
7472 save_entry_get_info_cb (GtkFileSystemHandle *handle,
7473                         const GtkFileInfo   *info,
7474                         const GError        *error,
7475                         gpointer             user_data)
7476 {
7477   gboolean parent_is_folder;
7478   gboolean cancelled = handle->cancelled;
7479   struct FileExistsData *data = user_data;
7480
7481   if (handle != data->impl->should_respond_get_info_handle)
7482     goto out;
7483
7484   data->impl->should_respond_get_info_handle = NULL;
7485
7486   set_busy_cursor (data->impl, FALSE);
7487
7488   if (cancelled)
7489     goto out;
7490
7491   if (!info)
7492     parent_is_folder = FALSE;
7493   else
7494     parent_is_folder = gtk_file_info_get_is_folder (info);
7495
7496   if (parent_is_folder)
7497     {
7498       if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7499         {
7500           if (data->file_exists_and_is_not_folder)
7501             {
7502               gboolean retval;
7503               const char *file_part;
7504
7505               file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
7506               retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
7507
7508               if (retval)
7509                 g_signal_emit_by_name (data->impl, "response-requested");
7510             }
7511           else
7512             g_signal_emit_by_name (data->impl, "response-requested");
7513         }
7514       else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
7515         {
7516           GtkFileSystemHandle *handle;
7517
7518           g_object_ref (data->impl);
7519           handle = gtk_file_system_create_folder (data->impl->file_system,
7520                                                   data->path,
7521                                                   action_create_folder_cb,
7522                                                   data->impl);
7523           data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
7524           set_busy_cursor (data->impl, TRUE);
7525         }
7526     }
7527   else
7528     {
7529       /* This will display an error, which is what we want */
7530       change_folder_and_display_error (data->impl, data->parent_path);
7531     }
7532
7533 out:
7534   g_object_unref (data->impl);
7535   gtk_file_path_free (data->path);
7536   gtk_file_path_free (data->parent_path);
7537   g_free (data);
7538
7539   g_object_unref (handle);
7540 }
7541
7542 static void
7543 file_exists_get_info_cb (GtkFileSystemHandle *handle,
7544                          const GtkFileInfo   *info,
7545                          const GError        *error,
7546                          gpointer             user_data)
7547 {
7548   gboolean data_ownership_taken = FALSE;
7549   gboolean cancelled = handle->cancelled;
7550   gboolean file_exists_and_is_not_folder;
7551   struct FileExistsData *data = user_data;
7552
7553   if (handle != data->impl->file_exists_get_info_handle)
7554     goto out;
7555
7556   data->impl->file_exists_get_info_handle = NULL;
7557
7558   set_busy_cursor (data->impl, FALSE);
7559
7560   if (cancelled)
7561     goto out;
7562
7563   file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
7564
7565   if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
7566     /* user typed a filename; we are done */
7567     g_signal_emit_by_name (data->impl, "response-requested");
7568   else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7569            && file_exists_and_is_not_folder)
7570     {
7571       /* Oops, the user typed the name of an existing path which is not
7572        * a folder
7573        */
7574       error_creating_folder_over_existing_file_dialog (data->impl, data->path,
7575                                                        g_error_copy (error));
7576     }
7577   else
7578     {
7579       /* check that everything up to the last component exists */
7580
7581       data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
7582       data_ownership_taken = TRUE;
7583
7584       if (data->impl->should_respond_get_info_handle)
7585         gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
7586
7587       data->impl->should_respond_get_info_handle =
7588         gtk_file_system_get_info (data->impl->file_system,
7589                                   data->parent_path,
7590                                   GTK_FILE_INFO_IS_FOLDER,
7591                                   save_entry_get_info_cb,
7592                                   data);
7593       set_busy_cursor (data->impl, TRUE);
7594     }
7595
7596 out:
7597   if (!data_ownership_taken)
7598     {
7599       g_object_unref (data->impl);
7600       gtk_file_path_free (data->path);
7601       gtk_file_path_free (data->parent_path);
7602       g_free (data);
7603     }
7604
7605   g_object_unref (handle);
7606 }
7607
7608 static void
7609 paste_text_received (GtkClipboard          *clipboard,
7610                      const gchar           *text,
7611                      GtkFileChooserDefault *impl)
7612 {
7613   GtkFilePath *path;
7614
7615   if (!text)
7616     return;
7617
7618   path = gtk_file_system_uri_to_path (impl->file_system, text);
7619   if (!path) 
7620     {
7621       if (!g_path_is_absolute (text)) 
7622         {
7623           location_popup_handler (impl, text);
7624           return;
7625         }
7626
7627       path = gtk_file_system_filename_to_path (impl->file_system, text);
7628       if (!path) 
7629         {
7630           location_popup_handler (impl, text);
7631           return;
7632         }
7633     }
7634
7635   if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
7636     location_popup_handler (impl, text);
7637
7638   gtk_file_path_free (path);
7639 }
7640
7641 /* Handler for the "location-popup-on-paste" keybinding signal */
7642 static void
7643 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
7644 {
7645   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
7646                                                       GDK_SELECTION_CLIPBOARD);
7647   gtk_clipboard_request_text (clipboard,
7648                               (GtkClipboardTextReceivedFunc) paste_text_received,
7649                               impl);
7650 }
7651
7652
7653 /* Implementation for GtkFileChooserEmbed::should_respond() */
7654 static gboolean
7655 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
7656 {
7657   GtkFileChooserDefault *impl;
7658   GtkWidget *toplevel;
7659   GtkWidget *current_focus;
7660
7661   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7662
7663   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7664   g_assert (GTK_IS_WINDOW (toplevel));
7665
7666   current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
7667
7668   if (current_focus == impl->browse_files_tree_view)
7669     {
7670       /* The following array encodes what we do based on the impl->action and the
7671        * number of files selected.
7672        */
7673       typedef enum {
7674         NOOP,                   /* Do nothing (don't respond) */
7675         RESPOND,                /* Respond immediately */
7676         RESPOND_OR_SWITCH,      /* Respond immediately if the selected item is a file; switch to it if it is a folder */
7677         ALL_FILES,              /* Respond only if everything selected is a file */
7678         ALL_FOLDERS,            /* Respond only if everything selected is a folder */
7679         SAVE_ENTRY,             /* Go to the code for handling the save entry */
7680         NOT_REACHED             /* Sanity check */
7681       } ActionToTake;
7682       static const ActionToTake what_to_do[4][3] = {
7683         /*                                0 selected            1 selected              many selected */
7684         /* ACTION_OPEN */               { NOOP,                 RESPOND_OR_SWITCH,      ALL_FILES   },
7685         /* ACTION_SAVE */               { SAVE_ENTRY,           RESPOND_OR_SWITCH,      NOT_REACHED },
7686         /* ACTION_SELECT_FOLDER */      { RESPOND,              ALL_FOLDERS,            ALL_FOLDERS },
7687         /* ACTION_CREATE_FOLDER */      { SAVE_ENTRY,           ALL_FOLDERS,            NOT_REACHED }
7688       };
7689
7690       int num_selected;
7691       gboolean all_files, all_folders;
7692       int k;
7693       ActionToTake action;
7694
7695     file_list:
7696
7697       g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7698
7699       selection_check (impl, &num_selected, &all_files, &all_folders);
7700
7701       if (num_selected > 2)
7702         k = 2;
7703       else
7704         k = num_selected;
7705
7706       action = what_to_do [impl->action] [k];
7707
7708       switch (action)
7709         {
7710         case NOOP:
7711           return FALSE;
7712
7713         case RESPOND:
7714           return TRUE;
7715
7716         case RESPOND_OR_SWITCH:
7717           g_assert (num_selected == 1);
7718
7719           if (all_folders)
7720             {
7721               switch_to_selected_folder (impl);
7722               return FALSE;
7723             }
7724           else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7725             return should_respond_after_confirm_overwrite (impl,
7726                                                            get_display_name_from_file_list (impl),
7727                                                            impl->current_folder);
7728           else
7729             return TRUE;
7730
7731         case ALL_FILES:
7732           return all_files;
7733
7734         case ALL_FOLDERS:
7735           return all_folders;
7736
7737         case SAVE_ENTRY:
7738           goto save_entry;
7739
7740         default:
7741           g_assert_not_reached ();
7742         }
7743     }
7744   else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
7745     {
7746       GtkFilePath *path;
7747       gboolean is_well_formed, is_empty, is_file_part_empty;
7748       gboolean is_folder;
7749       gboolean retval;
7750       GtkFileChooserEntry *entry;
7751       GError *error;
7752
7753     save_entry:
7754
7755       g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7756                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7757                 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7758                      || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7759                     && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7760
7761       entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7762       check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7763
7764       if (is_empty || !is_well_formed)
7765         return FALSE;
7766
7767       g_assert (path != NULL);
7768
7769       error = NULL;
7770       if (is_folder)
7771         {
7772           if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7773               || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7774             {
7775               _gtk_file_chooser_entry_set_file_part (entry, "");
7776               change_folder_and_display_error (impl, path);
7777               retval = FALSE;
7778             }
7779           else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7780                    || GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7781             {
7782               /* The folder already exists, so we do not need to create it.
7783                * Just respond to terminate the dialog.
7784                */
7785               retval = TRUE;
7786             }
7787           else
7788             {
7789               g_assert_not_reached ();
7790               retval = FALSE;
7791             }
7792         }
7793       else
7794         {
7795           struct FileExistsData *data;
7796
7797           /* We need to check whether path exists and is not a folder */
7798
7799           data = g_new0 (struct FileExistsData, 1);
7800           data->impl = g_object_ref (impl);
7801           data->path = gtk_file_path_copy (path);
7802           data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
7803
7804           if (impl->file_exists_get_info_handle)
7805             gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
7806
7807           impl->file_exists_get_info_handle =
7808             gtk_file_system_get_info (impl->file_system, path,
7809                                       GTK_FILE_INFO_IS_FOLDER,
7810                                       file_exists_get_info_cb,
7811                                       data);
7812
7813           set_busy_cursor (impl, TRUE);
7814           retval = FALSE;
7815
7816           if (error != NULL)
7817             g_error_free (error);
7818         }
7819
7820       gtk_file_path_free (path);
7821       return retval;
7822     }
7823   else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
7824     {
7825       /* The focus is on a dialog's action area button, *and* the widget that
7826        * was focused immediately before it is the shortcuts list.  Switch to the
7827        * selected shortcut and tell the caller not to respond.
7828        */
7829       GtkTreeIter iter;
7830
7831       if (shortcuts_get_selected (impl, &iter))
7832         {
7833           shortcuts_activate_iter (impl, &iter);
7834           
7835           gtk_widget_grab_focus (impl->browse_files_tree_view);
7836         }
7837       else
7838         goto file_list;
7839
7840       return FALSE;
7841     }
7842   else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7843     {
7844       /* The focus is on a dialog's action area button, *and* the widget that
7845        * was focused immediately before it is the file list.  
7846        */
7847       goto file_list;
7848     }
7849   else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7850     {
7851       /* The focus is on a dialog's action area button, *and* the widget that
7852        * was focused immediately before it is the location entry.
7853        */
7854       goto save_entry;
7855     }
7856   else
7857     /* The focus is on a dialog's action area button or something else */
7858     if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7859         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7860       goto save_entry;
7861     else
7862       goto file_list; 
7863   
7864   g_assert_not_reached ();
7865   return FALSE;
7866 }
7867
7868 /* Implementation for GtkFileChooserEmbed::initial_focus() */
7869 static void
7870 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
7871 {
7872   GtkFileChooserDefault *impl;
7873   GtkWidget *widget;
7874
7875   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7876
7877   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7878       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7879     {
7880       if (impl->location_mode == LOCATION_MODE_PATH_BAR)
7881         widget = impl->browse_files_tree_view;
7882       else
7883         widget = impl->location_entry;
7884     }
7885   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7886            || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7887     widget = impl->location_entry;
7888   else
7889     {
7890       g_assert_not_reached ();
7891       widget = NULL;
7892     }
7893
7894   g_assert (widget != NULL);
7895   gtk_widget_grab_focus (widget);
7896 }
7897
7898 static void
7899 set_current_filter (GtkFileChooserDefault *impl,
7900                     GtkFileFilter         *filter)
7901 {
7902   if (impl->current_filter != filter)
7903     {
7904       int filter_index;
7905
7906       /* NULL filters are allowed to reset to non-filtered status
7907        */
7908       filter_index = g_slist_index (impl->filters, filter);
7909       if (impl->filters && filter && filter_index < 0)
7910         return;
7911
7912       if (impl->current_filter)
7913         g_object_unref (impl->current_filter);
7914       impl->current_filter = filter;
7915       if (impl->current_filter)
7916         {
7917           g_object_ref_sink (impl->current_filter);
7918         }
7919
7920       if (impl->filters)
7921         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
7922                                   filter_index);
7923
7924       if (impl->browse_files_model)
7925         install_list_model_filter (impl);
7926
7927       g_object_notify (G_OBJECT (impl), "filter");
7928     }
7929 }
7930
7931 static void
7932 filter_combo_changed (GtkComboBox           *combo_box,
7933                       GtkFileChooserDefault *impl)
7934 {
7935   gint new_index = gtk_combo_box_get_active (combo_box);
7936   GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
7937
7938   set_current_filter (impl, new_filter);
7939 }
7940
7941 static void
7942 check_preview_change (GtkFileChooserDefault *impl)
7943 {
7944   GtkTreePath *cursor_path;
7945   const GtkFilePath *new_path;
7946   const GtkFileInfo *new_info;
7947
7948   gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
7949   if (cursor_path && impl->sort_model)
7950     {
7951       GtkTreeIter iter;
7952       GtkTreeIter child_iter;
7953
7954       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path))
7955         g_assert_not_reached ();
7956
7957       gtk_tree_path_free (cursor_path);
7958
7959       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
7960
7961       new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
7962       new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7963     }
7964   else
7965     {
7966       new_path = NULL;
7967       new_info = NULL;
7968     }
7969
7970   if (new_path != impl->preview_path &&
7971       !(new_path && impl->preview_path &&
7972         gtk_file_path_compare (new_path, impl->preview_path) == 0))
7973     {
7974       if (impl->preview_path)
7975         {
7976           gtk_file_path_free (impl->preview_path);
7977           g_free (impl->preview_display_name);
7978         }
7979
7980       if (new_path)
7981         {
7982           impl->preview_path = gtk_file_path_copy (new_path);
7983           impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
7984         }
7985       else
7986         {
7987           impl->preview_path = NULL;
7988           impl->preview_display_name = NULL;
7989         }
7990
7991       if (impl->use_preview_label && impl->preview_label)
7992         gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
7993
7994       g_signal_emit_by_name (impl, "update-preview");
7995     }
7996 }
7997
7998 static void
7999 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
8000                                     GtkFileSystemVolume *volume,
8001                                     const GError        *error,
8002                                     gpointer             data)
8003 {
8004   GtkFilePath *path;
8005   gboolean cancelled = handle->cancelled;
8006   GtkFileChooserDefault *impl = data;
8007
8008   if (handle != impl->shortcuts_activate_iter_handle)
8009     goto out;
8010
8011   impl->shortcuts_activate_iter_handle = NULL;
8012
8013   set_busy_cursor (impl, FALSE);
8014
8015   if (cancelled)
8016     goto out;
8017
8018   if (error)
8019     {
8020       char *msg;
8021
8022       msg = g_strdup_printf (_("Could not mount %s"),
8023                              gtk_file_system_volume_get_display_name (impl->file_system, volume));
8024       error_message (impl, msg, error->message);
8025       g_free (msg);
8026
8027       goto out;
8028     }
8029
8030   path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8031   if (path != NULL)
8032     {
8033       change_folder_and_display_error (impl, path);
8034       gtk_file_path_free (path);
8035     }
8036
8037 out:
8038   g_object_unref (impl);
8039   g_object_unref (handle);
8040 }
8041
8042
8043 /* Activates a volume by mounting it if necessary and then switching to its
8044  * base path.
8045  */
8046 static void
8047 shortcuts_activate_volume (GtkFileChooserDefault *impl,
8048                            GtkFileSystemVolume   *volume)
8049 {
8050   GtkFilePath *path;
8051
8052   /* We ref the file chooser since volume_mount() may run a main loop, and the
8053    * user could close the file chooser window in the meantime.
8054    */
8055   g_object_ref (impl);
8056
8057   if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
8058     {
8059       set_busy_cursor (impl, TRUE);
8060
8061       impl->shortcuts_activate_iter_handle =
8062         gtk_file_system_volume_mount (impl->file_system, volume,
8063                                       shortcuts_activate_volume_mount_cb,
8064                                       g_object_ref (impl));
8065     }
8066   else
8067     {
8068       path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8069       if (path != NULL)
8070         {
8071           change_folder_and_display_error (impl, path);
8072           gtk_file_path_free (path);
8073         }
8074     }
8075
8076   g_object_unref (impl);
8077 }
8078
8079 /* Opens the folder or volume at the specified iter in the shortcuts model */
8080 struct ShortcutsActivateData
8081 {
8082   GtkFileChooserDefault *impl;
8083   GtkFilePath *path;
8084 };
8085
8086 static void
8087 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
8088                                 const GtkFileInfo   *info,
8089                                 const GError        *error,
8090                                 gpointer             user_data)
8091 {
8092   gboolean cancelled = handle->cancelled;
8093   struct ShortcutsActivateData *data = user_data;
8094
8095   if (handle != data->impl->shortcuts_activate_iter_handle)
8096     goto out;
8097
8098   data->impl->shortcuts_activate_iter_handle = NULL;
8099
8100   if (cancelled)
8101     goto out;
8102
8103   if (!error && gtk_file_info_get_is_folder (info))
8104     change_folder_and_display_error (data->impl, data->path);
8105   else
8106     gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl), data->path, NULL);
8107
8108 out:
8109   g_object_unref (data->impl);
8110   gtk_file_path_free (data->path);
8111   g_free (data);
8112
8113   g_object_unref (handle);
8114 }
8115
8116 static void
8117 shortcuts_activate_iter (GtkFileChooserDefault *impl,
8118                          GtkTreeIter           *iter)
8119 {
8120   gpointer col_data;
8121   gboolean is_volume;
8122
8123   if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
8124     _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
8125
8126   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
8127                       SHORTCUTS_COL_DATA, &col_data,
8128                       SHORTCUTS_COL_IS_VOLUME, &is_volume,
8129                       -1);
8130
8131   if (!col_data)
8132     return; /* We are on a separator */
8133
8134   if (impl->shortcuts_activate_iter_handle)
8135     {
8136       gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
8137       impl->shortcuts_activate_iter_handle = NULL;
8138     }
8139
8140   if (is_volume)
8141     {
8142       GtkFileSystemVolume *volume;
8143
8144       volume = col_data;
8145
8146       shortcuts_activate_volume (impl, volume);
8147     }
8148   else
8149     {
8150       struct ShortcutsActivateData *data;
8151
8152       data = g_new0 (struct ShortcutsActivateData, 1);
8153       data->impl = g_object_ref (impl);
8154       data->path = gtk_file_path_copy (col_data);
8155
8156       impl->shortcuts_activate_iter_handle =
8157         gtk_file_system_get_info (impl->file_system, data->path,
8158                                   GTK_FILE_INFO_IS_FOLDER,
8159                                   shortcuts_activate_get_info_cb, data);
8160     }
8161 }
8162
8163 /* Callback used when a row in the shortcuts list is activated */
8164 static void
8165 shortcuts_row_activated_cb (GtkTreeView           *tree_view,
8166                             GtkTreePath           *path,
8167                             GtkTreeViewColumn     *column,
8168                             GtkFileChooserDefault *impl)
8169 {
8170   GtkTreeIter iter;
8171   GtkTreeIter child_iter;
8172
8173   if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
8174     return;
8175
8176   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
8177                                                     &child_iter,
8178                                                     &iter);
8179   shortcuts_activate_iter (impl, &child_iter);
8180
8181   gtk_widget_grab_focus (impl->browse_files_tree_view);
8182 }
8183
8184 /* Handler for GtkWidget::key-press-event on the shortcuts list */
8185 static gboolean
8186 shortcuts_key_press_event_cb (GtkWidget             *widget,
8187                               GdkEventKey           *event,
8188                               GtkFileChooserDefault *impl)
8189 {
8190   guint modifiers;
8191
8192   modifiers = gtk_accelerator_get_default_mod_mask ();
8193
8194   if ((event->keyval == GDK_BackSpace
8195       || event->keyval == GDK_Delete
8196       || event->keyval == GDK_KP_Delete)
8197       && (event->state & modifiers) == 0)
8198     {
8199       remove_selected_bookmarks (impl);
8200       return TRUE;
8201     }
8202
8203   if ((event->keyval == GDK_F2)
8204       && (event->state & modifiers) == 0)
8205     {
8206       rename_selected_bookmark (impl);
8207       return TRUE;
8208     }
8209
8210   return FALSE;
8211 }
8212
8213 static gboolean
8214 shortcuts_select_func  (GtkTreeSelection  *selection,
8215                         GtkTreeModel      *model,
8216                         GtkTreePath       *path,
8217                         gboolean           path_currently_selected,
8218                         gpointer           data)
8219 {
8220   GtkFileChooserDefault *impl = data;
8221
8222   return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
8223 }
8224
8225 static gboolean
8226 list_select_func  (GtkTreeSelection  *selection,
8227                    GtkTreeModel      *model,
8228                    GtkTreePath       *path,
8229                    gboolean           path_currently_selected,
8230                    gpointer           data)
8231 {
8232   GtkFileChooserDefault *impl = data;
8233
8234   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8235       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8236     {
8237       GtkTreeIter iter, child_iter;
8238       const GtkFileInfo *info;
8239
8240       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8241         return FALSE;
8242       
8243       gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8244
8245       info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8246
8247       if (info && !gtk_file_info_get_is_folder (info))
8248         return FALSE;
8249     }
8250     
8251   return TRUE;
8252 }
8253
8254 static void
8255 list_selection_changed (GtkTreeSelection      *selection,
8256                         GtkFileChooserDefault *impl)
8257 {
8258   /* See if we are in the new folder editable row for Save mode */
8259   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8260     {
8261       const GtkFileInfo *info;
8262       gboolean had_selection;
8263
8264       info = get_selected_file_info_from_file_list (impl, &had_selection);
8265       if (!had_selection)
8266         goto out; /* normal processing */
8267
8268       if (!info)
8269         return; /* We are on the editable row for New Folder */
8270     }
8271
8272  out:
8273
8274   update_chooser_entry (impl);
8275   check_preview_change (impl);
8276   bookmarks_check_add_sensitivity (impl);
8277
8278   g_signal_emit_by_name (impl, "selection-changed", 0);
8279 }
8280
8281 /* Callback used when a row in the file list is activated */
8282 static void
8283 list_row_activated (GtkTreeView           *tree_view,
8284                     GtkTreePath           *path,
8285                     GtkTreeViewColumn     *column,
8286                     GtkFileChooserDefault *impl)
8287 {
8288   GtkTreeIter iter, child_iter;
8289   const GtkFileInfo *info;
8290
8291   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8292     return;
8293
8294   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8295
8296   info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8297
8298   if (gtk_file_info_get_is_folder (info))
8299     {
8300       const GtkFilePath *file_path;
8301
8302       file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8303       change_folder_and_display_error (impl, file_path);
8304
8305       return;
8306     }
8307
8308   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8309       impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8310     g_signal_emit_by_name (impl, "file-activated");
8311 }
8312
8313 static void
8314 path_bar_clicked (GtkPathBar            *path_bar,
8315                   GtkFilePath           *file_path,
8316                   GtkFilePath           *child_path,
8317                   gboolean               child_is_hidden,
8318                   GtkFileChooserDefault *impl)
8319 {
8320   if (child_path)
8321     pending_select_paths_add (impl, child_path);
8322
8323   if (!change_folder_and_display_error (impl, file_path))
8324     return;
8325
8326   /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar".  We should then
8327    * show hidden files so that ".baz" appears in the file list, as it will still
8328    * be shown in the path bar: "/foo/[bar]/.baz"
8329    */
8330   if (child_is_hidden)
8331     g_object_set (impl, "show-hidden", TRUE, NULL);
8332 }
8333
8334 static const GtkFileInfo *
8335 get_list_file_info (GtkFileChooserDefault *impl,
8336                     GtkTreeIter           *iter)
8337 {
8338   GtkTreeIter child_iter;
8339
8340   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8341                                                   &child_iter,
8342                                                   iter);
8343
8344   return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8345 }
8346
8347 static void
8348 list_icon_data_func (GtkTreeViewColumn *tree_column,
8349                      GtkCellRenderer   *cell,
8350                      GtkTreeModel      *tree_model,
8351                      GtkTreeIter       *iter,
8352                      gpointer           data)
8353 {
8354   GtkFileChooserDefault *impl = data;
8355   GtkTreeIter child_iter;
8356   const GtkFilePath *path;
8357   GdkPixbuf *pixbuf;
8358   const GtkFileInfo *info; 
8359   gboolean sensitive = TRUE;
8360
8361   profile_start ("start", NULL);
8362   
8363   info = get_list_file_info (impl, iter);
8364
8365   gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8366                                                   &child_iter,
8367                                                   iter);
8368   path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8369
8370   if (path)
8371     {
8372       pixbuf = NULL;
8373
8374       if (info)
8375         {
8376           /* FIXME: NULL GError */
8377           pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
8378                                               impl->icon_size, NULL);
8379         }
8380     }
8381   else
8382     {
8383       /* We are on the editable row */
8384       pixbuf = NULL;
8385     }
8386
8387   if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8388                impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
8389     sensitive =  gtk_file_info_get_is_folder (info);    
8390     
8391   g_object_set (cell,
8392                 "pixbuf", pixbuf,
8393                 "sensitive", sensitive,
8394                 NULL);
8395     
8396   if (pixbuf)
8397     g_object_unref (pixbuf);
8398
8399   profile_end ("end", NULL);
8400 }
8401
8402 static void
8403 list_name_data_func (GtkTreeViewColumn *tree_column,
8404                      GtkCellRenderer   *cell,
8405                      GtkTreeModel      *tree_model,
8406                      GtkTreeIter       *iter,
8407                      gpointer           data)
8408 {
8409   GtkFileChooserDefault *impl = data;
8410   const GtkFileInfo *info = get_list_file_info (impl, iter);
8411   gboolean sensitive = TRUE;
8412
8413   if (!info)
8414     {
8415       g_object_set (cell,
8416                     "text", _("Type name of new folder"),
8417                     NULL);
8418
8419       return;
8420     }
8421
8422
8423   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8424          || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8425     {
8426       sensitive = gtk_file_info_get_is_folder (info);
8427     } 
8428     
8429   g_object_set (cell,
8430                 "text", gtk_file_info_get_display_name (info),
8431                 "sensitive", sensitive,
8432                 NULL);
8433 }
8434
8435 #if 0
8436 static void
8437 list_size_data_func (GtkTreeViewColumn *tree_column,
8438                      GtkCellRenderer   *cell,
8439                      GtkTreeModel      *tree_model,
8440                      GtkTreeIter       *iter,
8441                      gpointer           data)
8442 {
8443   GtkFileChooserDefault *impl = data;
8444   const GtkFileInfo *info = get_list_file_info (impl, iter);
8445   gint64 size;
8446   gchar *str;
8447   gboolean sensitive = TRUE;
8448
8449   if (!info || gtk_file_info_get_is_folder (info)) 
8450     {
8451       g_object_set (cell,
8452                     "text", NULL,
8453                     "sensitive", sensitive,
8454                     NULL);
8455       return;
8456     }
8457
8458   size = gtk_file_info_get_size (info);
8459 #if 0
8460   if (size < (gint64)1024)
8461     str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
8462   else if (size < (gint64)1024*1024)
8463     str = g_strdup_printf (_("%.1f KB"), size / (1024.));
8464   else if (size < (gint64)1024*1024*1024)
8465     str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
8466   else
8467     str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
8468 #endif
8469   str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
8470   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8471       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8472     sensitive = FALSE;
8473
8474   g_object_set (cell,
8475                 "text", str,
8476                 "sensitive", sensitive,
8477                 "alignment", PANGO_ALIGN_RIGHT,
8478                 NULL);
8479
8480   g_free (str);
8481 }
8482 #endif
8483
8484 /* Tree column data callback for the file list; fetches the mtime of a file */
8485 static void
8486 list_mtime_data_func (GtkTreeViewColumn *tree_column,
8487                       GtkCellRenderer   *cell,
8488                       GtkTreeModel      *tree_model,
8489                       GtkTreeIter       *iter,
8490                       gpointer           data)
8491 {
8492   GtkFileChooserDefault *impl;
8493   const GtkFileInfo *info;
8494   GtkFileTime time_mtime;
8495   GDate mtime, now;
8496   int days_diff;
8497   char buf[256];
8498   gboolean sensitive = TRUE;
8499
8500   impl = data;
8501
8502   info = get_list_file_info (impl, iter);
8503   if (!info)
8504     {
8505       g_object_set (cell,
8506                     "text", "",
8507                     "sensitive", TRUE,
8508                     NULL);
8509       return;
8510     }
8511
8512   time_mtime = gtk_file_info_get_modification_time (info);
8513
8514   if (time_mtime == 0)
8515     strcpy (buf, _("Unknown"));
8516   else
8517     {
8518       time_t time_now;
8519       g_date_set_time_t (&mtime, time_mtime);
8520       time_now = time (NULL);
8521       g_date_set_time_t (&now, time_now);
8522
8523       days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
8524
8525       if (days_diff == 0)
8526         strcpy (buf, _("Today"));
8527       else if (days_diff == 1)
8528         strcpy (buf, _("Yesterday"));
8529       else
8530         {
8531           char *format;
8532
8533           if (days_diff > 1 && days_diff < 7)
8534             format = "%A"; /* Days from last week */
8535           else
8536             format = "%x"; /* Any other date */
8537
8538           if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
8539             strcpy (buf, _("Unknown"));
8540         }
8541     }
8542
8543   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8544       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8545     sensitive = gtk_file_info_get_is_folder (info);
8546
8547   g_object_set (cell,
8548                 "text", buf,
8549                 "sensitive", sensitive,
8550                 NULL);
8551 }
8552
8553 GtkWidget *
8554 _gtk_file_chooser_default_new (const char *file_system)
8555 {
8556   return  g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
8557                         "file-system-backend", file_system,
8558                         NULL);
8559 }
8560
8561 static void
8562 location_set_user_text (GtkFileChooserDefault *impl,
8563                         const gchar           *path)
8564 {
8565   _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
8566   gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
8567 }
8568
8569 static void
8570 location_popup_handler (GtkFileChooserDefault *impl,
8571                         const gchar           *path)
8572 {
8573   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8574       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8575     {
8576       LocationMode new_mode;
8577
8578       if (path != NULL)
8579         {
8580           /* since the user typed something, we unconditionally want to turn on the entry */
8581           new_mode = LOCATION_MODE_FILENAME_ENTRY;
8582         }
8583       else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8584         new_mode = LOCATION_MODE_FILENAME_ENTRY;
8585       else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8586         new_mode = LOCATION_MODE_PATH_BAR;
8587       else
8588         {
8589           g_assert_not_reached ();
8590           return;
8591         }
8592
8593       location_mode_set (impl, new_mode, TRUE);
8594       if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
8595         {
8596           if (path != NULL)
8597             location_set_user_text (impl, path);
8598           else
8599             {
8600               location_entry_set_initial_text (impl);
8601               gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
8602             }
8603         }
8604     }
8605   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8606            || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8607     {
8608       gtk_widget_grab_focus (impl->location_entry);
8609       if (path != NULL)
8610         location_set_user_text (impl, path);
8611     }
8612   else
8613     g_assert_not_reached ();
8614 }
8615
8616 /* Handler for the "up-folder" keybinding signal */
8617 static void
8618 up_folder_handler (GtkFileChooserDefault *impl)
8619 {
8620   _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
8621 }
8622
8623 /* Handler for the "down-folder" keybinding signal */
8624 static void
8625 down_folder_handler (GtkFileChooserDefault *impl)
8626 {
8627   _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
8628 }
8629
8630 /* Switches to the shortcut in the specified index */
8631 static void
8632 switch_to_shortcut (GtkFileChooserDefault *impl,
8633                     int pos)
8634 {
8635   GtkTreeIter iter;
8636
8637   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8638     g_assert_not_reached ();
8639
8640   shortcuts_activate_iter (impl, &iter);
8641 }
8642
8643 /* Handler for the "home-folder" keybinding signal */
8644 static void
8645 home_folder_handler (GtkFileChooserDefault *impl)
8646 {
8647   if (impl->has_home)
8648     switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
8649 }
8650
8651 /* Handler for the "desktop-folder" keybinding signal */
8652 static void
8653 desktop_folder_handler (GtkFileChooserDefault *impl)
8654 {
8655   if (impl->has_desktop)
8656     switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
8657 }
8658
8659 static void
8660 quick_bookmark_handler (GtkFileChooserDefault *impl,
8661                         gint bookmark_index)
8662 {
8663   int bookmark_pos;
8664   GtkTreePath *path;
8665
8666   if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
8667     return;
8668
8669   bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
8670
8671   path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
8672   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
8673                                 path, NULL,
8674                                 FALSE, 0.0, 0.0);
8675   gtk_tree_path_free (path);
8676
8677   switch_to_shortcut (impl, bookmark_pos);
8678 }
8679
8680 \f
8681
8682 /* Drag and drop interfaces */
8683
8684 static void
8685 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
8686 {
8687 }
8688
8689 static void
8690 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
8691 {
8692   model->impl = NULL;
8693 }
8694
8695 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
8696 static gboolean
8697 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
8698                                       GtkTreePath       *path)
8699 {
8700   ShortcutsModelFilter *model;
8701   int pos;
8702   int bookmarks_pos;
8703
8704   model = SHORTCUTS_MODEL_FILTER (drag_source);
8705
8706   pos = *gtk_tree_path_get_indices (path);
8707   bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
8708
8709   return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
8710 }
8711
8712 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
8713 static gboolean
8714 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
8715                                       GtkTreePath       *path,
8716                                       GtkSelectionData  *selection_data)
8717 {
8718   ShortcutsModelFilter *model;
8719
8720   model = SHORTCUTS_MODEL_FILTER (drag_source);
8721
8722   /* FIXME */
8723
8724   return FALSE;
8725 }
8726
8727 /* Fill the GtkTreeDragSourceIface vtable */
8728 static void
8729 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
8730 {
8731   iface->row_draggable = shortcuts_model_filter_row_draggable;
8732   iface->drag_data_get = shortcuts_model_filter_drag_data_get;
8733 }
8734
8735 #if 0
8736 /* Fill the GtkTreeDragDestIface vtable */
8737 static void
8738 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
8739 {
8740   iface->drag_data_received = shortcuts_model_filter_drag_data_received;
8741   iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
8742 }
8743 #endif
8744
8745 static GtkTreeModel *
8746 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
8747                             GtkTreeModel          *child_model,
8748                             GtkTreePath           *root)
8749 {
8750   ShortcutsModelFilter *model;
8751
8752   model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
8753                         "child-model", child_model,
8754                         "virtual-root", root,
8755                         NULL);
8756
8757   model->impl = impl;
8758
8759   return GTK_TREE_MODEL (model);
8760 }