]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdefault.c
filechooser: Oops, cancel a file operation that was missing
[~andy/gtk] / gtk / gtkfilechooserdefault.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3  * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4  * Copyright (C) 2003, Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include "gtkfilechooserdefault.h"
23
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrendererpixbuf.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcheckmenuitem.h"
29 #include "gtkclipboard.h"
30 #include "gtkcomboboxtext.h"
31 #include "gtkentry.h"
32 #include "gtkexpander.h"
33 #include "gtkfilechooserprivate.h"
34 #include "gtkfilechooserdialog.h"
35 #include "gtkfilechooserembed.h"
36 #include "gtkfilechooserentry.h"
37 #include "gtkfilechooserutils.h"
38 #include "gtkfilechooser.h"
39 #include "gtkfilesystem.h"
40 #include "gtkfilesystemmodel.h"
41 #include "gtkframe.h"
42 #include "gtkgrid.h"
43 #include "gtkiconfactory.h"
44 #include "gtkicontheme.h"
45 #include "gtkimage.h"
46 #include "gtkimagemenuitem.h"
47 #include "gtkinfobar.h"
48 #include "gtklabel.h"
49 #include "gtkmarshalers.h"
50 #include "gtkmessagedialog.h"
51 #include "gtkmountoperation.h"
52 #include "gtkpaned.h"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkradiobutton.h"
56 #include "gtkrecentfilter.h"
57 #include "gtkrecentmanager.h"
58 #include "gtkscrolledwindow.h"
59 #include "gtkseparatormenuitem.h"
60 #include "gtksettings.h"
61 #include "gtksizegroup.h"
62 #include "gtksizerequest.h"
63 #include "gtkstock.h"
64 #include "gtktoolbar.h"
65 #include "gtktoolbutton.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
70 #include "gtkbox.h"
71 #include "gtkorientable.h"
72 #include "gtkintl.h"
73
74 #include <errno.h>
75 #include <string.h>
76 #include <time.h>
77 #include <sys/stat.h>
78 #include <sys/types.h>
79 #include <locale.h>
80
81 #ifdef HAVE_UNISTD_H
82 #include <unistd.h>
83 #endif
84 #ifdef G_OS_WIN32
85 #include <io.h>
86 #endif
87
88 /* Values for GtkSelection-related "info" fields */
89 #define SELECTION_TEXT 0
90 #define SELECTION_URI  1
91
92 /* Profiling stuff */
93 #undef PROFILE_FILE_CHOOSER
94 #ifdef PROFILE_FILE_CHOOSER
95
96
97 #ifndef F_OK 
98 #define F_OK 0
99 #endif
100
101 #define PROFILE_INDENT 4
102
103 static int profile_indent;
104
105 static void
106 profile_add_indent (int indent)
107 {
108   profile_indent += indent;
109   if (profile_indent < 0)
110     g_error ("You screwed up your indentation");
111 }
112
113 void
114 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 {
116   char *str;
117
118   if (indent < 0)
119     profile_add_indent (indent);
120
121   if (profile_indent == 0)
122     str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
123   else
124     str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125
126   access (str, F_OK);
127   g_free (str);
128
129   if (indent > 0)
130     profile_add_indent (indent);
131 }
132
133 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
134 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
135 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
136 #else
137 #define profile_start(x, y)
138 #define profile_end(x, y)
139 #define profile_msg(x, y)
140 #endif
141
142
143
144 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
145
146 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
147 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
148 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
149
150 #define MAX_LOADING_TIME 500
151
152 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
153
154 struct _GtkFileChooserDefaultClass
155 {
156   GtkBoxClass parent_class;
157 };
158
159 /* Signal IDs */
160 enum {
161   LOCATION_POPUP,
162   LOCATION_POPUP_ON_PASTE,
163   UP_FOLDER,
164   DOWN_FOLDER,
165   HOME_FOLDER,
166   DESKTOP_FOLDER,
167   QUICK_BOOKMARK,
168   LOCATION_TOGGLE_POPUP,
169   SHOW_HIDDEN,
170   SEARCH_SHORTCUT,
171   RECENT_SHORTCUT,
172
173   LAST_SIGNAL
174 };
175
176 static guint signals[LAST_SIGNAL] = { 0 };
177
178 /* Column numbers for the shortcuts tree.  Keep these in sync with shortcuts_model_create() */
179 enum {
180   SHORTCUTS_COL_PIXBUF,
181   SHORTCUTS_COL_NAME,
182   SHORTCUTS_COL_DATA,
183   SHORTCUTS_COL_TYPE,
184   SHORTCUTS_COL_REMOVABLE,
185   SHORTCUTS_COL_PIXBUF_VISIBLE,
186   SHORTCUTS_COL_CANCELLABLE,
187   SHORTCUTS_COL_NUM_COLUMNS
188 };
189
190 typedef enum {
191   SHORTCUT_TYPE_FILE,
192   SHORTCUT_TYPE_VOLUME,
193   SHORTCUT_TYPE_SEPARATOR,
194   SHORTCUT_TYPE_SEARCH,
195   SHORTCUT_TYPE_RECENT
196 } ShortcutType;
197
198 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
199                          "standard::is-hidden,standard::is-backup,standard::size," \
200                          "standard::content-type,time::modified"
201 enum {
202   /* the first 3 must be these due to settings caching sort column */
203   MODEL_COL_NAME,
204   MODEL_COL_SIZE,
205   MODEL_COL_MTIME,
206   MODEL_COL_FILE,
207   MODEL_COL_NAME_COLLATED,
208   MODEL_COL_IS_FOLDER,
209   MODEL_COL_IS_SENSITIVE,
210   MODEL_COL_PIXBUF,
211   MODEL_COL_SIZE_TEXT,
212   MODEL_COL_MTIME_TEXT,
213   MODEL_COL_ELLIPSIZE,
214   MODEL_COL_NUM_COLUMNS
215 };
216
217 /* This list of types is passed to _gtk_file_system_model_new*() */
218 #define MODEL_COLUMN_TYPES                                      \
219         MODEL_COL_NUM_COLUMNS,                                  \
220         G_TYPE_STRING,            /* MODEL_COL_NAME */          \
221         G_TYPE_INT64,             /* MODEL_COL_SIZE */          \
222         G_TYPE_LONG,              /* MODEL_COL_MTIME */         \
223         G_TYPE_FILE,              /* MODEL_COL_FILE */          \
224         G_TYPE_STRING,            /* MODEL_COL_NAME_COLLATED */ \
225         G_TYPE_BOOLEAN,           /* MODEL_COL_IS_FOLDER */     \
226         G_TYPE_BOOLEAN,           /* MODEL_COL_IS_SENSITIVE */  \
227         GDK_TYPE_PIXBUF,          /* MODEL_COL_PIXBUF */        \
228         G_TYPE_STRING,            /* MODEL_COL_SIZE_TEXT */     \
229         G_TYPE_STRING,            /* MODEL_COL_MTIME_TEXT */    \
230         PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
231
232 /* Identifiers for target types */
233 enum {
234   GTK_TREE_MODEL_ROW,
235 };
236
237 /* Interesting places in the shortcuts bar */
238 typedef enum {
239   SHORTCUTS_SEARCH,
240   SHORTCUTS_RECENT,
241   SHORTCUTS_RECENT_SEPARATOR,
242   SHORTCUTS_HOME,
243   SHORTCUTS_DESKTOP,
244   SHORTCUTS_VOLUMES,
245   SHORTCUTS_SHORTCUTS,
246   SHORTCUTS_BOOKMARKS_SEPARATOR,
247   SHORTCUTS_BOOKMARKS,
248   SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
249   SHORTCUTS_CURRENT_FOLDER
250 } ShortcutsIndex;
251
252 /* Icon size for if we can't get it from the theme */
253 #define FALLBACK_ICON_SIZE 16
254
255 #define PREVIEW_HBOX_SPACING 12
256 #define NUM_LINES 45
257 #define NUM_CHARS 60
258
259 static void gtk_file_chooser_default_iface_init       (GtkFileChooserIface        *iface);
260 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface   *iface);
261
262 static GObject* gtk_file_chooser_default_constructor  (GType                  type,
263                                                        guint                  n_construct_properties,
264                                                        GObjectConstructParam *construct_params);
265 static void     gtk_file_chooser_default_finalize     (GObject               *object);
266 static void     gtk_file_chooser_default_set_property (GObject               *object,
267                                                        guint                  prop_id,
268                                                        const GValue          *value,
269                                                        GParamSpec            *pspec);
270 static void     gtk_file_chooser_default_get_property (GObject               *object,
271                                                        guint                  prop_id,
272                                                        GValue                *value,
273                                                        GParamSpec            *pspec);
274 static void     gtk_file_chooser_default_dispose      (GObject               *object);
275 static void     gtk_file_chooser_default_show_all       (GtkWidget             *widget);
276 static void     gtk_file_chooser_default_realize        (GtkWidget             *widget);
277 static void     gtk_file_chooser_default_map            (GtkWidget             *widget);
278 static void     gtk_file_chooser_default_unmap          (GtkWidget             *widget);
279 static void     gtk_file_chooser_default_hierarchy_changed (GtkWidget          *widget,
280                                                             GtkWidget          *previous_toplevel);
281 static void     gtk_file_chooser_default_style_updated  (GtkWidget             *widget);
282 static void     gtk_file_chooser_default_screen_changed (GtkWidget             *widget,
283                                                          GdkScreen             *previous_screen);
284
285 static gboolean       gtk_file_chooser_default_set_current_folder          (GtkFileChooser    *chooser,
286                                                                             GFile             *folder,
287                                                                             GError           **error);
288 static gboolean       gtk_file_chooser_default_update_current_folder       (GtkFileChooser    *chooser,
289                                                                             GFile             *folder,
290                                                                             gboolean           keep_trail,
291                                                                             gboolean           clear_entry,
292                                                                             GError           **error);
293 static GFile *        gtk_file_chooser_default_get_current_folder          (GtkFileChooser    *chooser);
294 static void           gtk_file_chooser_default_set_current_name            (GtkFileChooser    *chooser,
295                                                                             const gchar       *name);
296 static gboolean       gtk_file_chooser_default_select_file                 (GtkFileChooser    *chooser,
297                                                                             GFile             *file,
298                                                                             GError           **error);
299 static void           gtk_file_chooser_default_unselect_file               (GtkFileChooser    *chooser,
300                                                                             GFile             *file);
301 static void           gtk_file_chooser_default_select_all                  (GtkFileChooser    *chooser);
302 static void           gtk_file_chooser_default_unselect_all                (GtkFileChooser    *chooser);
303 static GSList *       gtk_file_chooser_default_get_files                   (GtkFileChooser    *chooser);
304 static GFile *        gtk_file_chooser_default_get_preview_file            (GtkFileChooser    *chooser);
305 static GtkFileSystem *gtk_file_chooser_default_get_file_system             (GtkFileChooser    *chooser);
306 static void           gtk_file_chooser_default_add_filter                  (GtkFileChooser    *chooser,
307                                                                             GtkFileFilter     *filter);
308 static void           gtk_file_chooser_default_remove_filter               (GtkFileChooser    *chooser,
309                                                                             GtkFileFilter     *filter);
310 static GSList *       gtk_file_chooser_default_list_filters                (GtkFileChooser    *chooser);
311 static gboolean       gtk_file_chooser_default_add_shortcut_folder    (GtkFileChooser    *chooser,
312                                                                        GFile             *file,
313                                                                        GError           **error);
314 static gboolean       gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser    *chooser,
315                                                                        GFile             *file,
316                                                                        GError           **error);
317 static GSList *       gtk_file_chooser_default_list_shortcut_folders  (GtkFileChooser    *chooser);
318
319 static void           gtk_file_chooser_default_get_default_size       (GtkFileChooserEmbed *chooser_embed,
320                                                                        gint                *default_width,
321                                                                        gint                *default_height);
322 static gboolean       gtk_file_chooser_default_should_respond         (GtkFileChooserEmbed *chooser_embed);
323 static void           gtk_file_chooser_default_initial_focus          (GtkFileChooserEmbed *chooser_embed);
324
325 static void add_selection_to_recent_list (GtkFileChooserDefault *impl);
326
327 static void location_popup_handler  (GtkFileChooserDefault *impl,
328                                      const gchar           *path);
329 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
330 static void location_toggle_popup_handler   (GtkFileChooserDefault *impl);
331 static void up_folder_handler       (GtkFileChooserDefault *impl);
332 static void down_folder_handler     (GtkFileChooserDefault *impl);
333 static void home_folder_handler     (GtkFileChooserDefault *impl);
334 static void desktop_folder_handler  (GtkFileChooserDefault *impl);
335 static void quick_bookmark_handler  (GtkFileChooserDefault *impl,
336                                      gint                   bookmark_index);
337 static void show_hidden_handler     (GtkFileChooserDefault *impl);
338 static void search_shortcut_handler (GtkFileChooserDefault *impl);
339 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
340 static void update_appearance       (GtkFileChooserDefault *impl);
341
342 static void set_current_filter   (GtkFileChooserDefault *impl,
343                                   GtkFileFilter         *filter);
344 static void check_preview_change (GtkFileChooserDefault *impl);
345
346 static void filter_combo_changed       (GtkComboBox           *combo_box,
347                                         GtkFileChooserDefault *impl);
348
349 static gboolean shortcuts_key_press_event_cb (GtkWidget             *widget,
350                                               GdkEventKey           *event,
351                                               GtkFileChooserDefault *impl);
352
353 static gboolean shortcuts_select_func   (GtkTreeSelection      *selection,
354                                          GtkTreeModel          *model,
355                                          GtkTreePath           *path,
356                                          gboolean               path_currently_selected,
357                                          gpointer               data);
358 static gboolean shortcuts_get_selected  (GtkFileChooserDefault *impl,
359                                          GtkTreeIter           *iter);
360 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
361                                      GtkTreeIter           *iter);
362 static int shortcuts_get_index (GtkFileChooserDefault *impl,
363                                 ShortcutsIndex         where);
364 static int shortcut_find_position (GtkFileChooserDefault *impl,
365                                    GFile                 *file);
366
367 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
368
369 static gboolean list_select_func   (GtkTreeSelection      *selection,
370                                     GtkTreeModel          *model,
371                                     GtkTreePath           *path,
372                                     gboolean               path_currently_selected,
373                                     gpointer               data);
374
375 static void list_selection_changed     (GtkTreeSelection      *tree_selection,
376                                         GtkFileChooserDefault *impl);
377 static void list_row_activated         (GtkTreeView           *tree_view,
378                                         GtkTreePath           *path,
379                                         GtkTreeViewColumn     *column,
380                                         GtkFileChooserDefault *impl);
381
382 static void path_bar_clicked (GtkPathBar            *path_bar,
383                               GFile                 *file,
384                               GFile                 *child,
385                               gboolean               child_is_hidden,
386                               GtkFileChooserDefault *impl);
387
388 static void add_bookmark_button_clicked_cb    (GtkButton             *button,
389                                                GtkFileChooserDefault *impl);
390 static void remove_bookmark_button_clicked_cb (GtkButton             *button,
391                                                GtkFileChooserDefault *impl);
392
393 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
394
395 static void load_remove_timer (GtkFileChooserDefault *impl);
396 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
397
398 static void location_button_toggled_cb (GtkToggleButton *toggle,
399                                         GtkFileChooserDefault *impl);
400 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
401
402 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
403                                                gboolean remove_from_treeview);
404
405 static void     search_setup_widgets         (GtkFileChooserDefault *impl);
406 static void     search_stop_searching        (GtkFileChooserDefault *impl,
407                                               gboolean               remove_query);
408 static void     search_clear_model           (GtkFileChooserDefault *impl, 
409                                               gboolean               remove_from_treeview);
410 static gboolean search_should_respond        (GtkFileChooserDefault *impl);
411 static GSList  *search_get_selected_files    (GtkFileChooserDefault *impl);
412 static void     search_entry_activate_cb     (GtkEntry              *entry, 
413                                               gpointer               data);
414 static void     settings_load                (GtkFileChooserDefault *impl);
415
416 static void     recent_start_loading         (GtkFileChooserDefault *impl);
417 static void     recent_stop_loading          (GtkFileChooserDefault *impl);
418 static void     recent_clear_model           (GtkFileChooserDefault *impl,
419                                               gboolean               remove_from_treeview);
420 static gboolean recent_should_respond        (GtkFileChooserDefault *impl);
421 static GSList * recent_get_selected_files    (GtkFileChooserDefault *impl);
422 static void     set_file_system_backend      (GtkFileChooserDefault *impl);
423 static void     unset_file_system_backend    (GtkFileChooserDefault *impl);
424
425
426
427 \f
428
429 /* Drag and drop interface declarations */
430
431 typedef struct {
432   GtkTreeModelFilter parent;
433
434   GtkFileChooserDefault *impl;
435 } ShortcutsPaneModelFilter;
436
437 typedef struct {
438   GtkTreeModelFilterClass parent_class;
439 } ShortcutsPaneModelFilterClass;
440
441 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
442 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
443
444 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
445
446 GType _shortcuts_pane_model_filter_get_type (void);
447
448 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
449                          _shortcuts_pane_model_filter,
450                          GTK_TYPE_TREE_MODEL_FILTER,
451                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
452                                                 shortcuts_pane_model_filter_drag_source_iface_init))
453
454 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
455                                                       GtkTreeModel          *child_model,
456                                                       GtkTreePath           *root);
457
458 \f
459
460 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
461                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
462                                                 gtk_file_chooser_default_iface_init)
463                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
464                                                 gtk_file_chooser_embed_default_iface_init));                                            
465
466
467 static void
468 add_normal_and_shifted_binding (GtkBindingSet  *binding_set,
469                                 guint           keyval,
470                                 GdkModifierType modifiers,
471                                 const gchar    *signal_name)
472 {
473   gtk_binding_entry_add_signal (binding_set,
474                                 keyval, modifiers,
475                                 signal_name, 0);
476
477   gtk_binding_entry_add_signal (binding_set,
478                                 keyval, modifiers | GDK_SHIFT_MASK,
479                                 signal_name, 0);
480 }
481
482 static void
483 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
484 {
485   static const guint quick_bookmark_keyvals[10] = {
486     GDK_KEY_1, GDK_KEY_2, GDK_KEY_3, GDK_KEY_4, GDK_KEY_5, GDK_KEY_6, GDK_KEY_7, GDK_KEY_8, GDK_KEY_9, GDK_KEY_0
487   };
488   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
489   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
490   GtkBindingSet *binding_set;
491   int i;
492
493   gobject_class->finalize = gtk_file_chooser_default_finalize;
494   gobject_class->constructor = gtk_file_chooser_default_constructor;
495   gobject_class->set_property = gtk_file_chooser_default_set_property;
496   gobject_class->get_property = gtk_file_chooser_default_get_property;
497   gobject_class->dispose = gtk_file_chooser_default_dispose;
498
499   widget_class->show_all = gtk_file_chooser_default_show_all;
500   widget_class->realize = gtk_file_chooser_default_realize;
501   widget_class->map = gtk_file_chooser_default_map;
502   widget_class->unmap = gtk_file_chooser_default_unmap;
503   widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
504   widget_class->style_updated = gtk_file_chooser_default_style_updated;
505   widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
506
507   signals[LOCATION_POPUP] =
508     g_signal_new_class_handler (I_("location-popup"),
509                                 G_OBJECT_CLASS_TYPE (class),
510                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
511                                 G_CALLBACK (location_popup_handler),
512                                 NULL, NULL,
513                                 _gtk_marshal_VOID__STRING,
514                                 G_TYPE_NONE, 1, G_TYPE_STRING);
515
516   signals[LOCATION_POPUP_ON_PASTE] =
517     g_signal_new_class_handler (I_("location-popup-on-paste"),
518                                 G_OBJECT_CLASS_TYPE (class),
519                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
520                                 G_CALLBACK (location_popup_on_paste_handler),
521                                 NULL, NULL,
522                                 _gtk_marshal_VOID__VOID,
523                                 G_TYPE_NONE, 0);
524
525   signals[LOCATION_TOGGLE_POPUP] =
526     g_signal_new_class_handler (I_("location-toggle-popup"),
527                                 G_OBJECT_CLASS_TYPE (class),
528                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
529                                 G_CALLBACK (location_toggle_popup_handler),
530                                 NULL, NULL,
531                                 _gtk_marshal_VOID__VOID,
532                                 G_TYPE_NONE, 0);
533
534   signals[UP_FOLDER] =
535     g_signal_new_class_handler (I_("up-folder"),
536                                 G_OBJECT_CLASS_TYPE (class),
537                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
538                                 G_CALLBACK (up_folder_handler),
539                                 NULL, NULL,
540                                 _gtk_marshal_VOID__VOID,
541                                 G_TYPE_NONE, 0);
542
543   signals[DOWN_FOLDER] =
544     g_signal_new_class_handler (I_("down-folder"),
545                                 G_OBJECT_CLASS_TYPE (class),
546                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
547                                 G_CALLBACK (down_folder_handler),
548                                 NULL, NULL,
549                                 _gtk_marshal_VOID__VOID,
550                                 G_TYPE_NONE, 0);
551
552   signals[HOME_FOLDER] =
553     g_signal_new_class_handler (I_("home-folder"),
554                                 G_OBJECT_CLASS_TYPE (class),
555                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
556                                 G_CALLBACK (home_folder_handler),
557                                 NULL, NULL,
558                                 _gtk_marshal_VOID__VOID,
559                                 G_TYPE_NONE, 0);
560
561   signals[DESKTOP_FOLDER] =
562     g_signal_new_class_handler (I_("desktop-folder"),
563                                 G_OBJECT_CLASS_TYPE (class),
564                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
565                                 G_CALLBACK (desktop_folder_handler),
566                                 NULL, NULL,
567                                 _gtk_marshal_VOID__VOID,
568                                 G_TYPE_NONE, 0);
569
570   signals[QUICK_BOOKMARK] =
571     g_signal_new_class_handler (I_("quick-bookmark"),
572                                 G_OBJECT_CLASS_TYPE (class),
573                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
574                                 G_CALLBACK (quick_bookmark_handler),
575                                 NULL, NULL,
576                                 _gtk_marshal_VOID__INT,
577                                 G_TYPE_NONE, 1, G_TYPE_INT);
578
579   signals[SHOW_HIDDEN] =
580     g_signal_new_class_handler (I_("show-hidden"),
581                                 G_OBJECT_CLASS_TYPE (class),
582                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
583                                 G_CALLBACK (show_hidden_handler),
584                                 NULL, NULL,
585                                 _gtk_marshal_VOID__VOID,
586                                 G_TYPE_NONE, 0);
587
588   signals[SEARCH_SHORTCUT] =
589     g_signal_new_class_handler (I_("search-shortcut"),
590                                 G_OBJECT_CLASS_TYPE (class),
591                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
592                                 G_CALLBACK (search_shortcut_handler),
593                                 NULL, NULL,
594                                 _gtk_marshal_VOID__VOID,
595                                 G_TYPE_NONE, 0);
596
597   signals[RECENT_SHORTCUT] =
598     g_signal_new_class_handler (I_("recent-shortcut"),
599                                 G_OBJECT_CLASS_TYPE (class),
600                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
601                                 G_CALLBACK (recent_shortcut_handler),
602                                 NULL, NULL,
603                                 _gtk_marshal_VOID__VOID,
604                                 G_TYPE_NONE, 0);
605
606   binding_set = gtk_binding_set_by_class (class);
607
608   gtk_binding_entry_add_signal (binding_set,
609                                 GDK_KEY_l, GDK_CONTROL_MASK,
610                                 "location-toggle-popup",
611                                 0);
612
613   gtk_binding_entry_add_signal (binding_set,
614                                 GDK_KEY_slash, 0,
615                                 "location-popup",
616                                 1, G_TYPE_STRING, "/");
617   gtk_binding_entry_add_signal (binding_set,
618                                 GDK_KEY_KP_Divide, 0,
619                                 "location-popup",
620                                 1, G_TYPE_STRING, "/");
621
622 #ifdef G_OS_UNIX
623   gtk_binding_entry_add_signal (binding_set,
624                                 GDK_KEY_asciitilde, 0,
625                                 "location-popup",
626                                 1, G_TYPE_STRING, "~");
627 #endif
628
629   gtk_binding_entry_add_signal (binding_set,
630                                 GDK_KEY_v, GDK_CONTROL_MASK,
631                                 "location-popup-on-paste",
632                                 0);
633
634   add_normal_and_shifted_binding (binding_set,
635                                   GDK_KEY_Up, GDK_MOD1_MASK,
636                                   "up-folder");
637
638   add_normal_and_shifted_binding (binding_set,
639                                   GDK_KEY_KP_Up, GDK_MOD1_MASK,
640                                   "up-folder");
641
642   add_normal_and_shifted_binding (binding_set,
643                                   GDK_KEY_Down, GDK_MOD1_MASK,
644                                   "down-folder");
645   add_normal_and_shifted_binding (binding_set,
646                                   GDK_KEY_KP_Down, GDK_MOD1_MASK,
647                                   "down-folder");
648
649   gtk_binding_entry_add_signal (binding_set,
650                                 GDK_KEY_Home, GDK_MOD1_MASK,
651                                 "home-folder",
652                                 0);
653   gtk_binding_entry_add_signal (binding_set,
654                                 GDK_KEY_KP_Home, GDK_MOD1_MASK,
655                                 "home-folder",
656                                 0);
657   gtk_binding_entry_add_signal (binding_set,
658                                 GDK_KEY_d, GDK_MOD1_MASK,
659                                 "desktop-folder",
660                                 0);
661   gtk_binding_entry_add_signal (binding_set,
662                                 GDK_KEY_h, GDK_CONTROL_MASK,
663                                 "show-hidden",
664                                 0);
665   gtk_binding_entry_add_signal (binding_set,
666                                 GDK_KEY_s, GDK_MOD1_MASK,
667                                 "search-shortcut",
668                                 0);
669   gtk_binding_entry_add_signal (binding_set,
670                                 GDK_KEY_r, GDK_MOD1_MASK,
671                                 "recent-shortcut",
672                                 0);
673
674   for (i = 0; i < 10; i++)
675     gtk_binding_entry_add_signal (binding_set,
676                                   quick_bookmark_keyvals[i], GDK_MOD1_MASK,
677                                   "quick-bookmark",
678                                   1, G_TYPE_INT, i);
679
680   _gtk_file_chooser_install_properties (gobject_class);
681 }
682
683 static void
684 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
685 {
686   iface->select_file = gtk_file_chooser_default_select_file;
687   iface->unselect_file = gtk_file_chooser_default_unselect_file;
688   iface->select_all = gtk_file_chooser_default_select_all;
689   iface->unselect_all = gtk_file_chooser_default_unselect_all;
690   iface->get_files = gtk_file_chooser_default_get_files;
691   iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
692   iface->get_file_system = gtk_file_chooser_default_get_file_system;
693   iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
694   iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
695   iface->set_current_name = gtk_file_chooser_default_set_current_name;
696   iface->add_filter = gtk_file_chooser_default_add_filter;
697   iface->remove_filter = gtk_file_chooser_default_remove_filter;
698   iface->list_filters = gtk_file_chooser_default_list_filters;
699   iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
700   iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
701   iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
702 }
703
704 static void
705 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
706 {
707   iface->get_default_size = gtk_file_chooser_default_get_default_size;
708   iface->should_respond = gtk_file_chooser_default_should_respond;
709   iface->initial_focus = gtk_file_chooser_default_initial_focus;
710 }
711
712 static void
713 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
714 {
715   profile_start ("start", NULL);
716 #ifdef PROFILE_FILE_CHOOSER
717   access ("MARK: *** CREATE FILE CHOOSER", F_OK);
718 #endif
719   impl->local_only = TRUE;
720   impl->preview_widget_active = TRUE;
721   impl->use_preview_label = TRUE;
722   impl->select_multiple = FALSE;
723   impl->show_hidden = FALSE;
724   impl->show_size_column = TRUE;
725   impl->icon_size = FALLBACK_ICON_SIZE;
726   impl->load_state = LOAD_EMPTY;
727   impl->reload_state = RELOAD_EMPTY;
728   impl->pending_select_files = NULL;
729   impl->location_mode = LOCATION_MODE_PATH_BAR;
730   impl->operation_mode = OPERATION_MODE_BROWSE;
731   impl->sort_column = MODEL_COL_NAME;
732   impl->sort_order = GTK_SORT_ASCENDING;
733   impl->recent_manager = gtk_recent_manager_get_default ();
734   impl->create_folders = TRUE;
735
736   gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
737                                   GTK_ORIENTATION_VERTICAL);
738   gtk_box_set_spacing (GTK_BOX (impl), 12);
739
740   set_file_system_backend (impl);
741
742   profile_end ("end", NULL);
743 }
744
745 /* Frees the data columns for the specified iter in the shortcuts model*/
746 static void
747 shortcuts_free_row_data (GtkFileChooserDefault *impl,
748                          GtkTreeIter           *iter)
749 {
750   gpointer col_data;
751   ShortcutType shortcut_type;
752   GCancellable *cancellable;
753
754   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
755                       SHORTCUTS_COL_DATA, &col_data,
756                       SHORTCUTS_COL_TYPE, &shortcut_type,
757                       SHORTCUTS_COL_CANCELLABLE, &cancellable,
758                       -1);
759
760   if (cancellable)
761     {
762       g_cancellable_cancel (cancellable);
763       g_object_unref (cancellable);
764     }
765
766   if (!(shortcut_type == SHORTCUT_TYPE_FILE || 
767         shortcut_type == SHORTCUT_TYPE_VOLUME) ||
768       !col_data)
769     return;
770
771   if (shortcut_type == SHORTCUT_TYPE_VOLUME)
772     {
773       GtkFileSystemVolume *volume;
774
775       volume = col_data;
776       _gtk_file_system_volume_unref (volume);
777     }
778   if (shortcut_type == SHORTCUT_TYPE_FILE)
779     {
780       GFile *file;
781
782       file = col_data;
783       g_object_unref (file);
784     }
785 }
786
787 /* Frees all the data columns in the shortcuts model */
788 static void
789 shortcuts_free (GtkFileChooserDefault *impl)
790 {
791   GtkTreeIter iter;
792
793   if (!impl->shortcuts_model)
794     return;
795
796   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
797     do
798       {
799         shortcuts_free_row_data (impl, &iter);
800       }
801     while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
802
803   g_object_unref (impl->shortcuts_model);
804   impl->shortcuts_model = NULL;
805 }
806
807 static void
808 pending_select_files_free (GtkFileChooserDefault *impl)
809 {
810   g_slist_free_full (impl->pending_select_files, g_object_unref);
811   impl->pending_select_files = NULL;
812 }
813
814 static void
815 pending_select_files_add (GtkFileChooserDefault *impl,
816                           GFile                 *file)
817 {
818   impl->pending_select_files =
819     g_slist_prepend (impl->pending_select_files, g_object_ref (file));
820 }
821
822 static void
823 gtk_file_chooser_default_finalize (GObject *object)
824 {
825   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
826   GSList *l;
827
828   unset_file_system_backend (impl);
829
830   if (impl->shortcuts_pane_filter_model)
831     g_object_unref (impl->shortcuts_pane_filter_model);
832
833   shortcuts_free (impl);
834
835   g_free (impl->browse_files_last_selected_name);
836
837   for (l = impl->filters; l; l = l->next)
838     {
839       GtkFileFilter *filter;
840
841       filter = GTK_FILE_FILTER (l->data);
842       g_object_unref (filter);
843     }
844   g_slist_free (impl->filters);
845
846   if (impl->current_filter)
847     g_object_unref (impl->current_filter);
848
849   if (impl->current_volume_file)
850     g_object_unref (impl->current_volume_file);
851
852   if (impl->current_folder)
853     g_object_unref (impl->current_folder);
854
855   if (impl->preview_file)
856     g_object_unref (impl->preview_file);
857
858   if (impl->browse_path_bar_size_group)
859     g_object_unref (impl->browse_path_bar_size_group);
860
861   /* Free all the Models we have */
862   stop_loading_and_clear_list_model (impl, FALSE);
863   search_clear_model (impl, FALSE);
864   recent_clear_model (impl, FALSE);
865
866   /* stopping the load above should have cleared this */
867   g_assert (impl->load_timeout_id == 0);
868
869   g_free (impl->preview_display_name);
870
871   g_free (impl->edited_new_text);
872
873   G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
874 }
875
876 /* Shows an error dialog set as transient for the specified window */
877 static void
878 error_message_with_parent (GtkWindow  *parent,
879                            const char *msg,
880                            const char *detail)
881 {
882   GtkWidget *dialog;
883
884   dialog = gtk_message_dialog_new (parent,
885                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
886                                    GTK_MESSAGE_ERROR,
887                                    GTK_BUTTONS_OK,
888                                    "%s",
889                                    msg);
890   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
891                                             "%s", detail);
892
893   if (parent && gtk_window_has_group (parent))
894     gtk_window_group_add_window (gtk_window_get_group (parent),
895                                  GTK_WINDOW (dialog));
896
897   gtk_dialog_run (GTK_DIALOG (dialog));
898   gtk_widget_destroy (dialog);
899 }
900
901 /* Returns a toplevel GtkWindow, or NULL if none */
902 static GtkWindow *
903 get_toplevel (GtkWidget *widget)
904 {
905   GtkWidget *toplevel;
906
907   toplevel = gtk_widget_get_toplevel (widget);
908   if (!gtk_widget_is_toplevel (toplevel))
909     return NULL;
910   else
911     return GTK_WINDOW (toplevel);
912 }
913
914 /* Shows an error dialog for the file chooser */
915 static void
916 error_message (GtkFileChooserDefault *impl,
917                const char            *msg,
918                const char            *detail)
919 {
920   error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
921 }
922
923 /* Shows a simple error dialog relative to a path.  Frees the GError as well. */
924 static void
925 error_dialog (GtkFileChooserDefault *impl,
926               const char            *msg,
927               GFile                 *file,
928               GError                *error)
929 {
930   if (error)
931     {
932       char *uri = NULL;
933       char *text;
934
935       if (file)
936         uri = g_file_get_uri (file);
937       text = g_strdup_printf (msg, uri);
938       error_message (impl, text, error->message);
939       g_free (text);
940       g_free (uri);
941       g_error_free (error);
942     }
943 }
944
945 /* Displays an error message about not being able to get information for a file.
946  * Frees the GError as well.
947  */
948 static void
949 error_getting_info_dialog (GtkFileChooserDefault *impl,
950                            GFile                 *file,
951                            GError                *error)
952 {
953   error_dialog (impl,
954                 _("Could not retrieve information about the file"),
955                 file, error);
956 }
957
958 /* Shows an error dialog about not being able to add a bookmark */
959 static void
960 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
961                               GFile                 *file,
962                               GError                *error)
963 {
964   error_dialog (impl,
965                 _("Could not add a bookmark"),
966                 file, error);
967 }
968
969 /* Shows an error dialog about not being able to remove a bookmark */
970 static void
971 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
972                                 GFile                 *file,
973                                 GError                *error)
974 {
975   error_dialog (impl,
976                 _("Could not remove bookmark"),
977                 file, error);
978 }
979
980 /* Shows an error dialog about not being able to create a folder */
981 static void
982 error_creating_folder_dialog (GtkFileChooserDefault *impl,
983                               GFile                 *file,
984                               GError                *error)
985 {
986   error_dialog (impl, 
987                 _("The folder could not be created"), 
988                 file, error);
989 }
990
991 /* Shows an error about not being able to create a folder because a file with
992  * the same name is already there.
993  */
994 static void
995 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
996                                                  GFile                 *file,
997                                                  GError                *error)
998 {
999   error_dialog (impl,
1000                 _("The folder could not be created, as a file with the same "
1001                   "name already exists.  Try using a different name for the "
1002                   "folder, or rename the file first."),
1003                 file, error);
1004 }
1005
1006 static void
1007 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1008                                  GFile *parent_file)
1009 {
1010   GError *error;
1011
1012   error = NULL;
1013   g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1014                        _("You need to choose a valid filename."));
1015
1016   error_dialog (impl,
1017                 _("Cannot create a file under %s as it is not a folder"),
1018                 parent_file, error);
1019 }
1020
1021 /* Shows an error about not being able to select a folder because a file with
1022  * the same name is already there.
1023  */
1024 static void
1025 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1026                                                   GFile                 *file)
1027 {
1028   error_dialog (impl,
1029                 _("You may only select folders.  The item that you selected is not a folder; "
1030                   "try using a different item."),
1031                 file, NULL);
1032 }
1033
1034 /* Shows an error dialog about not being able to create a filename */
1035 static void
1036 error_building_filename_dialog (GtkFileChooserDefault *impl,
1037                                 GError                *error)
1038 {
1039   error_dialog (impl, _("Invalid file name"), 
1040                 NULL, error);
1041 }
1042
1043 /* Shows an error dialog when we cannot switch to a folder */
1044 static void
1045 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1046                               GFile                 *file,
1047                               GError                *error)
1048 {
1049   error_dialog (impl, _("The folder contents could not be displayed"),
1050                 file, error);
1051 }
1052
1053 /* Changes folders, displaying an error dialog if this fails */
1054 static gboolean
1055 change_folder_and_display_error (GtkFileChooserDefault *impl,
1056                                  GFile                 *file,
1057                                  gboolean               clear_entry)
1058 {
1059   GError *error;
1060   gboolean result;
1061
1062   g_return_val_if_fail (G_IS_FILE (file), FALSE);
1063
1064   /* We copy the path because of this case:
1065    *
1066    * list_row_activated()
1067    *   fetches path from model; path belongs to the model (*)
1068    *   calls change_folder_and_display_error()
1069    *     calls gtk_file_chooser_set_current_folder_file()
1070    *       changing folders fails, sets model to NULL, thus freeing the path in (*)
1071    */
1072
1073   error = NULL;
1074   result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1075
1076   if (!result)
1077     error_changing_folder_dialog (impl, file, error);
1078
1079   return result;
1080 }
1081
1082 static void
1083 emit_default_size_changed (GtkFileChooserDefault *impl)
1084 {
1085   profile_msg ("    emit default-size-changed start", NULL);
1086   g_signal_emit_by_name (impl, "default-size-changed");
1087   profile_msg ("    emit default-size-changed end", NULL);
1088 }
1089
1090 static void
1091 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1092 {
1093   if (impl->use_preview_label)
1094     {
1095       if (!impl->preview_label)
1096         {
1097           impl->preview_label = gtk_label_new (impl->preview_display_name);
1098           gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1099           gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1100           gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1101           gtk_widget_show (impl->preview_label);
1102         }
1103     }
1104   else
1105     {
1106       if (impl->preview_label)
1107         {
1108           gtk_widget_destroy (impl->preview_label);
1109           impl->preview_label = NULL;
1110         }
1111     }
1112
1113   if (impl->preview_widget_active && impl->preview_widget)
1114     gtk_widget_show (impl->preview_box);
1115   else
1116     gtk_widget_hide (impl->preview_box);
1117
1118   if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1119     emit_default_size_changed (impl);
1120 }
1121
1122 static void
1123 set_preview_widget (GtkFileChooserDefault *impl,
1124                     GtkWidget             *preview_widget)
1125 {
1126   if (preview_widget == impl->preview_widget)
1127     return;
1128
1129   if (impl->preview_widget)
1130     gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1131                           impl->preview_widget);
1132
1133   impl->preview_widget = preview_widget;
1134   if (impl->preview_widget)
1135     {
1136       gtk_widget_show (impl->preview_widget);
1137       gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1138       gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1139                              impl->preview_widget,
1140                              (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1141     }
1142
1143   update_preview_widget_visibility (impl);
1144 }
1145
1146 /* Renders a "Search" icon at an appropriate size for a tree view */
1147 static GdkPixbuf *
1148 render_search_icon (GtkFileChooserDefault *impl)
1149 {
1150   return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1151 }
1152
1153 static GdkPixbuf *
1154 render_recent_icon (GtkFileChooserDefault *impl)
1155 {
1156   GtkIconTheme *theme;
1157   GdkPixbuf *retval;
1158
1159   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1160     theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1161   else
1162     theme = gtk_icon_theme_get_default ();
1163
1164   retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1165                                      impl->icon_size, 0,
1166                                      NULL);
1167
1168   /* fallback */
1169   if (!retval)
1170     retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1171
1172   return retval;
1173 }
1174
1175
1176 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1177 struct ReloadIconsData
1178 {
1179   GtkFileChooserDefault *impl;
1180   GtkTreeRowReference *row_ref;
1181 };
1182
1183 static void
1184 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1185                                     GFileInfo    *info,
1186                                     const GError *error,
1187                                     gpointer      user_data)
1188 {
1189   GdkPixbuf *pixbuf;
1190   GtkTreeIter iter;
1191   GtkTreePath *path;
1192   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1193   struct ReloadIconsData *data = user_data;
1194
1195   if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1196     goto out;
1197
1198   data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1199
1200   if (cancelled || error)
1201     goto out;
1202
1203   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1204
1205   path = gtk_tree_row_reference_get_path (data->row_ref);
1206   if (path)
1207     {
1208       gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1209       gtk_list_store_set (data->impl->shortcuts_model, &iter,
1210                           SHORTCUTS_COL_PIXBUF, pixbuf,
1211                           -1);
1212       gtk_tree_path_free (path);
1213     }
1214
1215   if (pixbuf)
1216     g_object_unref (pixbuf);
1217
1218 out:
1219   gtk_tree_row_reference_free (data->row_ref);
1220   g_object_unref (data->impl);
1221   g_free (data);
1222
1223   g_object_unref (cancellable);
1224 }
1225
1226 static void
1227 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1228 {
1229   GSList *l;
1230   GtkTreeIter iter;
1231
1232   profile_start ("start", NULL);
1233
1234   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1235     goto out;
1236
1237   for (l = impl->reload_icon_cancellables; l; l = l->next)
1238     {
1239       GCancellable *cancellable = G_CANCELLABLE (l->data);
1240       g_cancellable_cancel (cancellable);
1241     }
1242   g_slist_free (impl->reload_icon_cancellables);
1243   impl->reload_icon_cancellables = NULL;
1244
1245   do
1246     {
1247       gpointer data;
1248       ShortcutType shortcut_type;
1249       gboolean pixbuf_visible;
1250       GdkPixbuf *pixbuf;
1251
1252       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1253                           SHORTCUTS_COL_DATA, &data,
1254                           SHORTCUTS_COL_TYPE, &shortcut_type,
1255                           SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1256                           -1);
1257
1258       pixbuf = NULL;
1259       if (pixbuf_visible)
1260         {
1261           if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1262             {
1263               GtkFileSystemVolume *volume;
1264
1265               volume = data;
1266               pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1267                                                             impl->icon_size, NULL);
1268             }
1269           else if (shortcut_type == SHORTCUT_TYPE_FILE)
1270             {
1271               if (g_file_is_native (G_FILE (data)))
1272                 {
1273                   GFile *file;
1274                   struct ReloadIconsData *info;
1275                   GtkTreePath *tree_path;
1276                   GCancellable *cancellable;
1277
1278                   file = data;
1279
1280                   info = g_new0 (struct ReloadIconsData, 1);
1281                   info->impl = g_object_ref (impl);
1282                   tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1283                   info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1284                   gtk_tree_path_free (tree_path);
1285
1286                   cancellable = _gtk_file_system_get_info (impl->file_system, file,
1287                                                            "standard::icon",
1288                                                            shortcuts_reload_icons_get_info_cb,
1289                                                            info);
1290                   impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1291                 }
1292               else
1293                 {
1294                   GtkIconTheme *icon_theme;
1295
1296                   /* Don't call get_info for remote paths to avoid latency and
1297                    * auth dialogs.
1298                    * If we switch to a better bookmarks file format (XBEL), we
1299                    * should use mime info to get a better icon.
1300                    */
1301                   icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1302                   pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote", 
1303                                                      impl->icon_size, 0, NULL);
1304                 }
1305             }
1306           else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1307             {
1308               pixbuf = render_search_icon (impl);
1309             }
1310           else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1311             {
1312               pixbuf = render_recent_icon (impl);
1313             }
1314
1315           gtk_list_store_set (impl->shortcuts_model, &iter,
1316                               SHORTCUTS_COL_PIXBUF, pixbuf,
1317                               -1);
1318
1319           if (pixbuf)
1320             g_object_unref (pixbuf);
1321
1322         }
1323     }
1324   while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1325
1326  out:
1327
1328   profile_end ("end", NULL);
1329 }
1330
1331 static void 
1332 shortcuts_find_folder (GtkFileChooserDefault *impl,
1333                        GFile                 *folder)
1334 {
1335   GtkTreeSelection *selection;
1336   int pos;
1337   GtkTreePath *path;
1338
1339   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1340
1341   g_assert (folder != NULL);
1342   pos = shortcut_find_position (impl, folder);
1343   if (pos == -1)
1344     {
1345       gtk_tree_selection_unselect_all (selection);
1346       return;
1347     }
1348
1349   path = gtk_tree_path_new_from_indices (pos, -1);
1350   gtk_tree_selection_select_path (selection, path);
1351   gtk_tree_path_free (path);
1352 }
1353
1354 /* If a shortcut corresponds to the current folder, selects it */
1355 static void
1356 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1357 {
1358   shortcuts_find_folder (impl, impl->current_folder);
1359 }
1360
1361 /* Removes the specified number of rows from the shortcuts list */
1362 static void
1363 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1364                        int                    start_row,
1365                        int                    n_rows)
1366 {
1367   GtkTreePath *path;
1368
1369   path = gtk_tree_path_new_from_indices (start_row, -1);
1370
1371   for (; n_rows; n_rows--)
1372     {
1373       GtkTreeIter iter;
1374
1375       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1376         g_assert_not_reached ();
1377
1378       shortcuts_free_row_data (impl, &iter);
1379       gtk_list_store_remove (impl->shortcuts_model, &iter);
1380     }
1381
1382   gtk_tree_path_free (path);
1383 }
1384
1385 static void
1386 shortcuts_update_count (GtkFileChooserDefault *impl,
1387                         ShortcutsIndex         type,
1388                         gint                   value)
1389 {
1390   switch (type)
1391     {
1392       case SHORTCUTS_HOME:
1393         if (value < 0)
1394           impl->has_home = FALSE;
1395         else
1396           impl->has_home = TRUE;
1397         break;
1398
1399       case SHORTCUTS_DESKTOP:
1400         if (value < 0)
1401           impl->has_desktop = FALSE;
1402         else
1403           impl->has_desktop = TRUE;
1404         break;
1405
1406       case SHORTCUTS_VOLUMES:
1407         impl->num_volumes += value;
1408         break;
1409
1410       case SHORTCUTS_SHORTCUTS:
1411         impl->num_shortcuts += value;
1412         break;
1413
1414       case SHORTCUTS_BOOKMARKS:
1415         impl->num_bookmarks += value;
1416         break;
1417
1418       case SHORTCUTS_CURRENT_FOLDER:
1419         if (value < 0)
1420           impl->shortcuts_current_folder_active = FALSE;
1421         else
1422           impl->shortcuts_current_folder_active = TRUE;
1423         break;
1424
1425       default:
1426         /* nothing */
1427         break;
1428     }
1429 }
1430
1431 struct ShortcutsInsertRequest
1432 {
1433   GtkFileChooserDefault *impl;
1434   GFile *file;
1435   int pos;
1436   char *label_copy;
1437   GtkTreeRowReference *row_ref;
1438   ShortcutsIndex type;
1439   gboolean name_only;
1440   gboolean removable;
1441 };
1442
1443 static void
1444 get_file_info_finished (GCancellable *cancellable,
1445                         GFileInfo    *info,
1446                         const GError *error,
1447                         gpointer      data)
1448 {
1449   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1450   GdkPixbuf *pixbuf;
1451   GtkTreePath *path;
1452   GtkTreeIter iter;
1453   GCancellable *model_cancellable = NULL;
1454   struct ShortcutsInsertRequest *request = data;
1455
1456   path = gtk_tree_row_reference_get_path (request->row_ref);
1457   if (!path)
1458     /* Handle doesn't exist anymore in the model */
1459     goto out;
1460
1461   gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1462                            &iter, path);
1463   gtk_tree_path_free (path);
1464
1465   /* validate cancellable, else goto out */
1466   gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1467                       SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1468                       -1);
1469   if (cancellable != model_cancellable)
1470     goto out;
1471
1472   /* set the cancellable to NULL in the model (we unref later on) */
1473   gtk_list_store_set (request->impl->shortcuts_model, &iter,
1474                       SHORTCUTS_COL_CANCELLABLE, NULL,
1475                       -1);
1476
1477   if (cancelled)
1478     goto out;
1479
1480   if (!info)
1481     {
1482       gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1483       shortcuts_update_count (request->impl, request->type, -1);
1484
1485       if (request->type == SHORTCUTS_HOME)
1486         {
1487           GFile *home;
1488
1489           home = g_file_new_for_path (g_get_home_dir ());
1490           error_getting_info_dialog (request->impl, home, g_error_copy (error));
1491           g_object_unref (home);
1492         }
1493       else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1494         {
1495           /* Remove the current folder separator */
1496           gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1497           shortcuts_remove_rows (request->impl, separator_pos, 1);
1498         }
1499
1500       goto out;
1501     }
1502   
1503   if (!request->label_copy)
1504     request->label_copy = g_strdup (g_file_info_get_display_name (info));
1505   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1506                                        request->impl->icon_size);
1507
1508   gtk_list_store_set (request->impl->shortcuts_model, &iter,
1509                       SHORTCUTS_COL_PIXBUF, pixbuf,
1510                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1511                       SHORTCUTS_COL_NAME, request->label_copy,
1512                       SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1513                       SHORTCUTS_COL_REMOVABLE, request->removable,
1514                       -1);
1515
1516   if (request->impl->shortcuts_pane_filter_model)
1517     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1518
1519   if (pixbuf)
1520     g_object_unref (pixbuf);
1521
1522 out:
1523   g_object_unref (request->impl);
1524   g_object_unref (request->file);
1525   gtk_tree_row_reference_free (request->row_ref);
1526   g_free (request->label_copy);
1527   g_free (request);
1528
1529   if (model_cancellable)
1530     g_object_unref (model_cancellable);
1531 }
1532
1533 /* FIXME: GtkFileSystem needs a function to split a remote path
1534  * into hostname and path components, or maybe just have a 
1535  * gtk_file_system_path_get_display_name().
1536  *
1537  * This function is also used in gtkfilechooserbutton.c
1538  */
1539 gchar *
1540 _gtk_file_chooser_label_for_file (GFile *file)
1541 {
1542   const gchar *path, *start, *end, *p;
1543   gchar *uri, *host, *label;
1544
1545   uri = g_file_get_uri (file);
1546
1547   start = strstr (uri, "://");
1548   if (start)
1549     {
1550       start += 3;
1551       path = strchr (start, '/');
1552       if (path)
1553         end = path;
1554       else
1555         {
1556           end = uri + strlen (uri);
1557           path = "/";
1558         }
1559
1560       /* strip username */
1561       p = strchr (start, '@');
1562       if (p && p < end)
1563         start = p + 1;
1564   
1565       p = strchr (start, ':');
1566       if (p && p < end)
1567         end = p;
1568   
1569       host = g_strndup (start, end - start);
1570   
1571       /* Translators: the first string is a path and the second string 
1572        * is a hostname. Nautilus and the panel contain the same string 
1573        * to translate. 
1574        */
1575       label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1576   
1577       g_free (host);
1578     }
1579   else
1580     {
1581       label = g_strdup (uri);
1582     }
1583   
1584   g_free (uri);
1585
1586   return label;
1587 }
1588
1589 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1590  * inserts a volume.  A position of -1 indicates the end of the tree.
1591  */
1592 static void
1593 shortcuts_insert_file (GtkFileChooserDefault *impl,
1594                        int                    pos,
1595                        ShortcutType           shortcut_type,
1596                        GtkFileSystemVolume   *volume,
1597                        GFile                 *file,
1598                        const char            *label,
1599                        gboolean               removable,
1600                        ShortcutsIndex         type)
1601 {
1602   char *label_copy;
1603   GdkPixbuf *pixbuf = NULL;
1604   gpointer data = NULL;
1605   GtkTreeIter iter;
1606   GtkIconTheme *icon_theme;
1607
1608   profile_start ("start shortcut", NULL);
1609
1610   if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1611     {
1612       data = volume;
1613       label_copy = _gtk_file_system_volume_get_display_name (volume);
1614       pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1615                                                     impl->icon_size, NULL);
1616     }
1617   else if (shortcut_type == SHORTCUT_TYPE_FILE)
1618     {
1619       if (g_file_is_native (file))
1620         {
1621           struct ShortcutsInsertRequest *request;
1622           GCancellable *cancellable;
1623           GtkTreePath *p;
1624
1625           request = g_new0 (struct ShortcutsInsertRequest, 1);
1626           request->impl = g_object_ref (impl);
1627           request->file = g_object_ref (file);
1628           request->name_only = TRUE;
1629           request->removable = removable;
1630           request->pos = pos;
1631           request->type = type;
1632           if (label)
1633             request->label_copy = g_strdup (label);
1634
1635           if (pos == -1)
1636             gtk_list_store_append (impl->shortcuts_model, &iter);
1637           else
1638             gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1639
1640           p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1641           request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1642           gtk_tree_path_free (p);
1643
1644           cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1645                                                    "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1646                                                    get_file_info_finished, request);
1647
1648           gtk_list_store_set (impl->shortcuts_model, &iter,
1649                               SHORTCUTS_COL_DATA, g_object_ref (file),
1650                               SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1651                               SHORTCUTS_COL_CANCELLABLE, cancellable,
1652                               -1);
1653
1654           shortcuts_update_count (impl, type, 1);
1655
1656           return;
1657         }
1658       else
1659         {
1660           /* Don't call get_info for remote paths to avoid latency and
1661            * auth dialogs.
1662            */
1663           data = g_object_ref (file);
1664           if (label)
1665             label_copy = g_strdup (label);
1666           else
1667             label_copy = _gtk_file_chooser_label_for_file (file);
1668
1669           /* If we switch to a better bookmarks file format (XBEL), we
1670            * should use mime info to get a better icon.
1671            */
1672           icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1673           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote", 
1674                                              impl->icon_size, 0, NULL);
1675         }
1676     }
1677    else
1678     {
1679       g_assert_not_reached ();
1680
1681       return;
1682     }
1683
1684   if (pos == -1)
1685     gtk_list_store_append (impl->shortcuts_model, &iter);
1686   else
1687     gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1688
1689   shortcuts_update_count (impl, type, 1);
1690
1691   gtk_list_store_set (impl->shortcuts_model, &iter,
1692                       SHORTCUTS_COL_PIXBUF, pixbuf,
1693                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1694                       SHORTCUTS_COL_NAME, label_copy,
1695                       SHORTCUTS_COL_DATA, data,
1696                       SHORTCUTS_COL_TYPE, shortcut_type,
1697                       SHORTCUTS_COL_REMOVABLE, removable,
1698                       SHORTCUTS_COL_CANCELLABLE, NULL,
1699                       -1);
1700
1701   if (impl->shortcuts_pane_filter_model)
1702     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1703
1704   g_free (label_copy);
1705
1706   if (pixbuf)
1707     g_object_unref (pixbuf);
1708
1709   profile_end ("end", NULL);
1710 }
1711
1712 static void
1713 shortcuts_append_search (GtkFileChooserDefault *impl)
1714 {
1715   GdkPixbuf *pixbuf;
1716   GtkTreeIter iter;
1717
1718   pixbuf = render_search_icon (impl);
1719
1720   gtk_list_store_append (impl->shortcuts_model, &iter);
1721   gtk_list_store_set (impl->shortcuts_model, &iter,
1722                       SHORTCUTS_COL_PIXBUF, pixbuf,
1723                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1724                       SHORTCUTS_COL_NAME, _("Search"),
1725                       SHORTCUTS_COL_DATA, NULL,
1726                       SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1727                       SHORTCUTS_COL_REMOVABLE, FALSE,
1728                       -1);
1729
1730   if (pixbuf)
1731     g_object_unref (pixbuf);
1732
1733   impl->has_search = TRUE;
1734 }
1735
1736 static gboolean
1737 shortcuts_get_recent_enabled (GtkWidget *widget)
1738 {
1739   GtkSettings *settings;
1740   gboolean enabled;
1741
1742   if (gtk_widget_has_screen (widget))
1743     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1744   else
1745     settings = gtk_settings_get_default ();
1746
1747   g_object_get (settings, "gtk-recent-files-enabled", &enabled, NULL);
1748   return enabled;
1749 }
1750
1751 static void
1752 shortcuts_append_recent (GtkFileChooserDefault *impl)
1753 {
1754   GdkPixbuf *pixbuf;
1755   GtkTreeIter iter;
1756   gboolean enabled;
1757
1758   enabled = shortcuts_get_recent_enabled (GTK_WIDGET (impl));
1759   if (!enabled)
1760     return;
1761
1762   pixbuf = render_recent_icon (impl);
1763
1764   gtk_list_store_append (impl->shortcuts_model, &iter);
1765   gtk_list_store_set (impl->shortcuts_model, &iter,
1766                       SHORTCUTS_COL_PIXBUF, pixbuf,
1767                       SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1768                       SHORTCUTS_COL_NAME, _("Recently Used"),
1769                       SHORTCUTS_COL_DATA, NULL,
1770                       SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1771                       SHORTCUTS_COL_REMOVABLE, FALSE,
1772                       -1);
1773   
1774   if (pixbuf)
1775     g_object_unref (pixbuf);
1776
1777   impl->has_recent = TRUE;
1778 }
1779
1780 /* Appends an item for the user's home directory to the shortcuts model */
1781 static void
1782 shortcuts_append_home (GtkFileChooserDefault *impl)
1783 {
1784   const char *home_path;
1785   GFile *home;
1786
1787   profile_start ("start", NULL);
1788
1789   home_path = g_get_home_dir ();
1790   if (home_path == NULL)
1791     {
1792       profile_end ("end - no home directory!?", NULL);
1793       return;
1794     }
1795
1796   home = g_file_new_for_path (home_path);
1797   shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1798   impl->has_home = TRUE;
1799
1800   g_object_unref (home);
1801
1802   profile_end ("end", NULL);
1803 }
1804
1805 /* Appends the ~/Desktop directory to the shortcuts model */
1806 static void
1807 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1808 {
1809   const char *name;
1810   GFile *file;
1811
1812   profile_start ("start", NULL);
1813
1814   name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1815   /* "To disable a directory, point it to the homedir."
1816    * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1817    **/
1818   if (!g_strcmp0 (name, g_get_home_dir ()))
1819     {
1820       profile_end ("end", NULL);
1821       return;
1822     }
1823
1824   file = g_file_new_for_path (name);
1825   shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1826   impl->has_desktop = TRUE;
1827
1828   /* We do not actually pop up an error dialog if there is no desktop directory
1829    * because some people may really not want to have one.
1830    */
1831
1832   g_object_unref (file);
1833
1834   profile_end ("end", NULL);
1835 }
1836
1837 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1838 static int
1839 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1840                             GSList                *bookmarks)
1841 {
1842   int start_row;
1843   int num_inserted;
1844   gchar *label;
1845
1846   profile_start ("start", NULL);
1847
1848   start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1849   num_inserted = 0;
1850
1851   for (; bookmarks; bookmarks = bookmarks->next)
1852     {
1853       GFile *file;
1854
1855       file = bookmarks->data;
1856
1857       if (impl->local_only && !g_file_is_native (file))
1858         continue;
1859
1860       if (shortcut_find_position (impl, file) != -1)
1861         continue;
1862
1863       label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1864
1865       shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1866       g_free (label);
1867
1868       num_inserted++;
1869     }
1870
1871   profile_end ("end", NULL);
1872
1873   return num_inserted;
1874 }
1875
1876 /* Returns the index for the corresponding item in the shortcuts bar */
1877 static int
1878 shortcuts_get_index (GtkFileChooserDefault *impl,
1879                      ShortcutsIndex         where)
1880 {
1881   int n;
1882
1883   n = 0;
1884   
1885   if (where == SHORTCUTS_SEARCH)
1886     goto out;
1887
1888   n += impl->has_search ? 1 : 0;
1889
1890   if (where == SHORTCUTS_RECENT)
1891     goto out;
1892
1893   n += impl->has_recent ? 1 : 0;
1894
1895   if (where == SHORTCUTS_RECENT_SEPARATOR)
1896     goto out;
1897
1898   n += 1; /* we always have the separator after the recently-used item */
1899
1900   if (where == SHORTCUTS_HOME)
1901     goto out;
1902
1903   n += impl->has_home ? 1 : 0;
1904
1905   if (where == SHORTCUTS_DESKTOP)
1906     goto out;
1907
1908   n += impl->has_desktop ? 1 : 0;
1909
1910   if (where == SHORTCUTS_VOLUMES)
1911     goto out;
1912
1913   n += impl->num_volumes;
1914
1915   if (where == SHORTCUTS_SHORTCUTS)
1916     goto out;
1917
1918   n += impl->num_shortcuts;
1919
1920   if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1921     goto out;
1922
1923   /* If there are no bookmarks there won't be a separator */
1924   n += (impl->num_bookmarks > 0) ? 1 : 0;
1925
1926   if (where == SHORTCUTS_BOOKMARKS)
1927     goto out;
1928
1929   n += impl->num_bookmarks;
1930
1931   if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1932     goto out;
1933
1934   n += 1;
1935
1936   if (where == SHORTCUTS_CURRENT_FOLDER)
1937     goto out;
1938
1939   g_assert_not_reached ();
1940
1941  out:
1942
1943   return n;
1944 }
1945
1946 /* Adds all the file system volumes to the shortcuts model */
1947 static void
1948 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1949 {
1950   int start_row;
1951   GSList *list, *l;
1952   int n;
1953   gboolean old_changing_folders;
1954
1955   profile_start ("start", NULL);
1956
1957   old_changing_folders = impl->changing_folder;
1958   impl->changing_folder = TRUE;
1959
1960   start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1961   shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1962   impl->num_volumes = 0;
1963
1964   list = _gtk_file_system_list_volumes (impl->file_system);
1965
1966   n = 0;
1967
1968   for (l = list; l; l = l->next)
1969     {
1970       GtkFileSystemVolume *volume;
1971
1972       volume = l->data;
1973
1974       if (impl->local_only)
1975         {
1976           if (_gtk_file_system_volume_is_mounted (volume))
1977             {
1978               GFile *base_file;
1979               gboolean base_is_native = TRUE;
1980
1981               base_file = _gtk_file_system_volume_get_root (volume);
1982               if (base_file != NULL)
1983                 {
1984                   base_is_native = g_file_is_native (base_file);
1985                   g_object_unref (base_file);
1986                 }
1987
1988               if (!base_is_native)
1989                 continue;
1990             }
1991         }
1992
1993       shortcuts_insert_file (impl,
1994                              start_row + n,
1995                              SHORTCUT_TYPE_VOLUME,
1996                              _gtk_file_system_volume_ref (volume),
1997                              NULL,
1998                              NULL,
1999                              FALSE,
2000                              SHORTCUTS_VOLUMES);
2001       n++;
2002     }
2003
2004   impl->num_volumes = n;
2005   g_slist_free (list);
2006
2007   if (impl->shortcuts_pane_filter_model)
2008     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2009
2010   impl->changing_folder = old_changing_folders;
2011
2012   profile_end ("end", NULL);
2013 }
2014
2015 /* Inserts a separator node in the shortcuts list */
2016 static void
2017 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2018                             ShortcutsIndex         where)
2019 {
2020   GtkTreeIter iter;
2021
2022   g_assert (where == SHORTCUTS_RECENT_SEPARATOR || 
2023             where == SHORTCUTS_BOOKMARKS_SEPARATOR || 
2024             where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2025
2026   gtk_list_store_insert (impl->shortcuts_model, &iter,
2027                          shortcuts_get_index (impl, where));
2028   gtk_list_store_set (impl->shortcuts_model, &iter,
2029                       SHORTCUTS_COL_PIXBUF, NULL,
2030                       SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2031                       SHORTCUTS_COL_NAME, NULL,
2032                       SHORTCUTS_COL_DATA, NULL,
2033                       SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2034                       -1);
2035 }
2036
2037 /* Updates the list of bookmarks */
2038 static void
2039 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2040 {
2041   GSList *bookmarks;
2042   gboolean old_changing_folders;
2043   GtkTreeIter iter;
2044   GFile *list_selected = NULL;
2045   ShortcutType shortcut_type;
2046   gpointer col_data;
2047
2048   profile_start ("start", NULL);
2049         
2050   old_changing_folders = impl->changing_folder;
2051   impl->changing_folder = TRUE;
2052
2053   if (shortcuts_get_selected (impl, &iter))
2054     {
2055       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), 
2056                           &iter, 
2057                           SHORTCUTS_COL_DATA, &col_data,
2058                           SHORTCUTS_COL_TYPE, &shortcut_type,
2059                           -1);
2060
2061       if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2062         list_selected = g_object_ref (col_data);
2063     }
2064
2065   if (impl->num_bookmarks > 0)
2066     shortcuts_remove_rows (impl,
2067                            shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2068                            impl->num_bookmarks + 1);
2069
2070   impl->num_bookmarks = 0;
2071   shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2072
2073   bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2074   shortcuts_append_bookmarks (impl, bookmarks);
2075   g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2076   g_slist_free (bookmarks);
2077
2078   if (impl->num_bookmarks == 0)
2079     shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2080
2081   if (impl->shortcuts_pane_filter_model)
2082     gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2083
2084   if (list_selected)
2085     {
2086       shortcuts_find_folder (impl, list_selected);
2087       g_object_unref (list_selected);
2088     }
2089
2090   impl->changing_folder = old_changing_folders;
2091
2092   profile_end ("end", NULL);
2093 }
2094
2095 /* Appends a separator and a row to the shortcuts list for the current folder */
2096 static void
2097 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2098 {
2099   int pos;
2100
2101   g_assert (!impl->shortcuts_current_folder_active);
2102
2103   g_assert (impl->current_folder != NULL);
2104
2105   pos = shortcut_find_position (impl, impl->current_folder);
2106   if (pos == -1)
2107     {
2108       GtkFileSystemVolume *volume;
2109       GFile *base_file;
2110
2111       /* Separator */
2112       shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2113
2114       /* Item */
2115       pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2116
2117       volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2118       if (volume)
2119         base_file = _gtk_file_system_volume_get_root (volume);
2120       else
2121         base_file = NULL;
2122
2123       if (base_file && g_file_equal (base_file, impl->current_folder))
2124         shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2125       else
2126         shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2127
2128       if (base_file)
2129         g_object_unref (base_file);
2130     }
2131 }
2132
2133 /* Updates the current folder row in the shortcuts model */
2134 static void
2135 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2136 {
2137   int pos;
2138
2139   pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2140
2141   if (impl->shortcuts_current_folder_active)
2142     {
2143       shortcuts_remove_rows (impl, pos, 2);
2144       impl->shortcuts_current_folder_active = FALSE;
2145     }
2146
2147   shortcuts_add_current_folder (impl);
2148 }
2149
2150 /* Filter function used for the shortcuts filter model */
2151 static gboolean
2152 shortcuts_pane_filter_cb (GtkTreeModel *model,
2153                           GtkTreeIter  *iter,
2154                           gpointer      data)
2155 {
2156   GtkFileChooserDefault *impl;
2157   GtkTreePath *path;
2158   int pos;
2159
2160   impl = GTK_FILE_CHOOSER_DEFAULT (data);
2161
2162   path = gtk_tree_model_get_path (model, iter);
2163   if (!path)
2164     return FALSE;
2165
2166   pos = *gtk_tree_path_get_indices (path);
2167   gtk_tree_path_free (path);
2168
2169   return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2170 }
2171
2172 /* Creates the list model for shortcuts */
2173 static void
2174 shortcuts_model_create (GtkFileChooserDefault *impl)
2175 {
2176   /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2177   impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2178                                               GDK_TYPE_PIXBUF,  /* pixbuf */
2179                                               G_TYPE_STRING,    /* name */
2180                                               G_TYPE_POINTER,   /* path or volume */
2181                                               G_TYPE_INT,       /* ShortcutType */
2182                                               G_TYPE_BOOLEAN,   /* removable */
2183                                               G_TYPE_BOOLEAN,   /* pixbuf cell visibility */
2184                                               G_TYPE_POINTER);  /* GCancellable */
2185
2186   shortcuts_append_search (impl);
2187
2188   if (impl->recent_manager)
2189     {
2190       shortcuts_append_recent (impl);
2191       shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2192     }
2193   
2194   if (impl->file_system)
2195     {
2196       shortcuts_append_home (impl);
2197       shortcuts_append_desktop (impl);
2198       shortcuts_add_volumes (impl);
2199     }
2200
2201   impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2202                                                                        GTK_TREE_MODEL (impl->shortcuts_model),
2203                                                                        NULL);
2204
2205   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2206                                           shortcuts_pane_filter_cb,
2207                                           impl,
2208                                           NULL);
2209 }
2210
2211 /* Callback used when the "New Folder" button is clicked */
2212 static void
2213 new_folder_button_clicked (GtkButton             *button,
2214                            GtkFileChooserDefault *impl)
2215 {
2216   GtkTreeIter iter;
2217   GtkTreePath *path;
2218
2219   if (!impl->browse_files_model)
2220     return; /* FIXME: this sucks.  Disable the New Folder button or something. */
2221
2222   /* Prevent button from being clicked twice */
2223   gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2224
2225   _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2226
2227   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2228   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2229                                 path, impl->list_name_column,
2230                                 FALSE, 0.0, 0.0);
2231
2232   g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2233   gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2234                             path,
2235                             impl->list_name_column,
2236                             TRUE);
2237
2238   gtk_tree_path_free (path);
2239 }
2240
2241 static GSource *
2242 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2243 {
2244   GSource *source;
2245
2246   source = g_idle_source_new ();
2247   g_source_set_closure (source,
2248                         g_cclosure_new_object (callback, G_OBJECT (impl)));
2249   g_source_attach (source, NULL);
2250
2251   return source;
2252 }
2253
2254 /* Idle handler for creating a new folder after editing its name cell, or for
2255  * canceling the editing.
2256  */
2257 static gboolean
2258 edited_idle_cb (GtkFileChooserDefault *impl)
2259 {
2260   gdk_threads_enter ();
2261   
2262   g_source_destroy (impl->edited_idle);
2263   impl->edited_idle = NULL;
2264
2265   _gtk_file_system_model_remove_editable (impl->browse_files_model);
2266   g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2267
2268   gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2269
2270   if (impl->edited_new_text /* not cancelled? */
2271       && (strlen (impl->edited_new_text) != 0)
2272       && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2273     {
2274       GError *error = NULL;
2275       GFile *file;
2276
2277       file = g_file_get_child_for_display_name (impl->current_folder,
2278                                                 impl->edited_new_text,
2279                                                 &error);
2280       if (file)
2281         {
2282           GError *error = NULL;
2283
2284           if (g_file_make_directory (file, NULL, &error))
2285             change_folder_and_display_error (impl, file, FALSE);
2286           else
2287             error_creating_folder_dialog (impl, file, error);
2288
2289           g_object_unref (file);
2290         }
2291       else
2292         error_creating_folder_dialog (impl, file, error);
2293
2294       g_free (impl->edited_new_text);
2295       impl->edited_new_text = NULL;
2296     }
2297
2298   gdk_threads_leave ();
2299
2300   return FALSE;
2301 }
2302
2303 static void
2304 queue_edited_idle (GtkFileChooserDefault *impl,
2305                    const gchar           *new_text)
2306 {
2307   /* We create the folder in an idle handler so that we don't modify the tree
2308    * just now.
2309    */
2310
2311   if (!impl->edited_idle)
2312     impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2313
2314   g_free (impl->edited_new_text);
2315   impl->edited_new_text = g_strdup (new_text);
2316 }
2317
2318 /* Callback used from the text cell renderer when the new folder is named */
2319 static void
2320 renderer_edited_cb (GtkCellRendererText   *cell_renderer_text,
2321                     const gchar           *path,
2322                     const gchar           *new_text,
2323                     GtkFileChooserDefault *impl)
2324 {
2325   /* work around bug #154921 */
2326   g_object_set (cell_renderer_text, 
2327                 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2328   queue_edited_idle (impl, new_text);
2329 }
2330
2331 /* Callback used from the text cell renderer when the new folder edition gets
2332  * canceled.
2333  */
2334 static void
2335 renderer_editing_canceled_cb (GtkCellRendererText   *cell_renderer_text,
2336                               GtkFileChooserDefault *impl)
2337 {
2338   /* work around bug #154921 */
2339   g_object_set (cell_renderer_text, 
2340                 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2341   queue_edited_idle (impl, NULL);
2342 }
2343
2344 /* Creates the widgets for the filter combo box */
2345 static GtkWidget *
2346 filter_create (GtkFileChooserDefault *impl)
2347 {
2348   GtkCellRenderer *cell;
2349   GList           *cells;
2350
2351   impl->filter_combo = gtk_combo_box_text_new ();
2352   gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2353
2354   /* Get the combo's text renderer and set ellipsize parameters */
2355   cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2356   g_assert (cells);
2357   cell = cells->data;
2358
2359   g_object_set (G_OBJECT (cell),
2360                 "ellipsize", PANGO_ELLIPSIZE_END,
2361                 NULL);
2362
2363   g_list_free (cells);
2364
2365   g_signal_connect (impl->filter_combo, "changed",
2366                     G_CALLBACK (filter_combo_changed), impl);
2367
2368   gtk_widget_set_tooltip_text (impl->filter_combo,
2369                                _("Select which types of files are shown"));
2370
2371   return impl->filter_combo;
2372 }
2373
2374 static GtkWidget *
2375 toolbutton_new (GtkFileChooserDefault *impl,
2376                 GIcon                 *icon,
2377                 gboolean               sensitive,
2378                 gboolean               show,
2379                 GCallback              callback)
2380 {
2381   GtkToolItem *item;
2382   GtkWidget *image;
2383
2384   item = gtk_tool_button_new (NULL, NULL);
2385   image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2386   gtk_widget_show (image);
2387   gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2388
2389   gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2390   g_signal_connect (item, "clicked", callback, impl);
2391
2392   if (show)
2393     gtk_widget_show (GTK_WIDGET (item));
2394
2395   return GTK_WIDGET (item);
2396 }
2397
2398 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2399 static int
2400 shortcut_find_position (GtkFileChooserDefault *impl,
2401                         GFile                 *file)
2402 {
2403   GtkTreeIter iter;
2404   int i;
2405   int current_folder_separator_idx;
2406
2407   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2408     return -1;
2409
2410   current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2411
2412 #if 0
2413   /* FIXME: is this still needed? */
2414   if (current_folder_separator_idx >= impl->shortcuts_model->length)
2415     return -1;
2416 #endif
2417
2418   for (i = 0; i < current_folder_separator_idx; i++)
2419     {
2420       gpointer col_data;
2421       ShortcutType shortcut_type;
2422
2423       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2424                           SHORTCUTS_COL_DATA, &col_data,
2425                           SHORTCUTS_COL_TYPE, &shortcut_type,
2426                           -1);
2427
2428       if (col_data)
2429         {
2430           if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2431             {
2432               GtkFileSystemVolume *volume;
2433               GFile *base_file;
2434               gboolean exists;
2435
2436               volume = col_data;
2437               base_file = _gtk_file_system_volume_get_root (volume);
2438
2439               exists = base_file && g_file_equal (file, base_file);
2440
2441               if (base_file)
2442                 g_object_unref (base_file);
2443
2444               if (exists)
2445                 return i;
2446             }
2447           else if (shortcut_type == SHORTCUT_TYPE_FILE)
2448             {
2449               GFile *model_file;
2450
2451               model_file = col_data;
2452
2453               if (model_file && g_file_equal (model_file, file))
2454                 return i;
2455             }
2456         }
2457
2458       if (i < current_folder_separator_idx - 1)
2459         {
2460           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2461             g_assert_not_reached ();
2462         }
2463     }
2464
2465   return -1;
2466 }
2467
2468 /* Tries to add a bookmark from a path name */
2469 static gboolean
2470 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2471                                   GFile                 *file,
2472                                   int                    pos)
2473 {
2474   GError *error;
2475
2476   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2477
2478   if (shortcut_find_position (impl, file) != -1)
2479     return FALSE;
2480
2481   error = NULL;
2482   if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2483     {
2484       error_adding_bookmark_dialog (impl, file, error);
2485       return FALSE;
2486     }
2487
2488   return TRUE;
2489 }
2490
2491 static void
2492 add_bookmark_foreach_cb (GtkTreeModel *model,
2493                          GtkTreePath  *path,
2494                          GtkTreeIter  *iter,
2495                          gpointer      data)
2496 {
2497   GtkFileChooserDefault *impl;
2498   GFile *file;
2499
2500   impl = (GtkFileChooserDefault *) data;
2501
2502   gtk_tree_model_get (model, iter,
2503                       MODEL_COL_FILE, &file,
2504                       -1);
2505
2506   shortcuts_add_bookmark_from_file (impl, file, -1);
2507
2508   g_object_unref (file);
2509 }
2510
2511 /* Adds a bookmark from the currently selected item in the file list */
2512 static void
2513 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2514 {
2515   GtkTreeSelection *selection;
2516
2517   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2518
2519   if (gtk_tree_selection_count_selected_rows (selection) == 0)
2520     shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2521   else
2522     gtk_tree_selection_selected_foreach (selection,
2523                                          add_bookmark_foreach_cb,
2524                                          impl);
2525 }
2526
2527 /* Callback used when the "Add bookmark" button is clicked */
2528 static void
2529 add_bookmark_button_clicked_cb (GtkButton *button,
2530                                 GtkFileChooserDefault *impl)
2531 {
2532   bookmarks_add_selected_folder (impl);
2533 }
2534
2535 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2536  * returns FALSE if no shortcut is selected.
2537  */
2538 static gboolean
2539 shortcuts_get_selected (GtkFileChooserDefault *impl,
2540                         GtkTreeIter           *iter)
2541 {
2542   GtkTreeSelection *selection;
2543   GtkTreeIter parent_iter;
2544
2545   if (!impl->browse_shortcuts_tree_view)
2546     return FALSE;
2547
2548   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2549
2550   if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2551     return FALSE;
2552
2553   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2554                                                     iter,
2555                                                     &parent_iter);
2556   return TRUE;
2557 }
2558
2559 /* Removes the selected bookmarks */
2560 static void
2561 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2562 {
2563   GtkTreeIter iter;
2564   gpointer col_data;
2565   GFile *file;
2566   gboolean removable;
2567   GError *error;
2568
2569   if (!shortcuts_get_selected (impl, &iter))
2570     return;
2571
2572   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2573                       SHORTCUTS_COL_DATA, &col_data,
2574                       SHORTCUTS_COL_REMOVABLE, &removable,
2575                       -1);
2576
2577   if (!removable)
2578     return;
2579
2580   g_assert (col_data != NULL);
2581
2582   file = col_data;
2583
2584   error = NULL;
2585   if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2586     error_removing_bookmark_dialog (impl, file, error);
2587 }
2588
2589 /* Callback used when the "Remove bookmark" button is clicked */
2590 static void
2591 remove_bookmark_button_clicked_cb (GtkButton *button,
2592                                    GtkFileChooserDefault *impl)
2593 {
2594   remove_selected_bookmarks (impl);
2595 }
2596
2597 struct selection_check_closure {
2598   GtkFileChooserDefault *impl;
2599   int num_selected;
2600   gboolean all_files;
2601   gboolean all_folders;
2602 };
2603
2604 /* Used from gtk_tree_selection_selected_foreach() */
2605 static void
2606 selection_check_foreach_cb (GtkTreeModel *model,
2607                             GtkTreePath  *path,
2608                             GtkTreeIter  *iter,
2609                             gpointer      data)
2610 {
2611   struct selection_check_closure *closure;
2612   gboolean is_folder;
2613   GFile *file;
2614
2615   gtk_tree_model_get (model, iter,
2616                       MODEL_COL_FILE, &file,
2617                       MODEL_COL_IS_FOLDER, &is_folder,
2618                       -1);
2619
2620   if (file == NULL)
2621     return;
2622
2623   g_object_unref (file);
2624
2625   closure = data;
2626   closure->num_selected++;
2627
2628   closure->all_folders = closure->all_folders && is_folder;
2629   closure->all_files = closure->all_files && !is_folder;
2630 }
2631
2632 /* Checks whether the selected items in the file list are all files or all folders */
2633 static void
2634 selection_check (GtkFileChooserDefault *impl,
2635                  gint                  *num_selected,
2636                  gboolean              *all_files,
2637                  gboolean              *all_folders)
2638 {
2639   struct selection_check_closure closure;
2640   GtkTreeSelection *selection;
2641
2642   closure.impl = impl;
2643   closure.num_selected = 0;
2644   closure.all_files = TRUE;
2645   closure.all_folders = TRUE;
2646
2647   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2648   gtk_tree_selection_selected_foreach (selection,
2649                                        selection_check_foreach_cb,
2650                                        &closure);
2651
2652   g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2653
2654   if (num_selected)
2655     *num_selected = closure.num_selected;
2656
2657   if (all_files)
2658     *all_files = closure.all_files;
2659
2660   if (all_folders)
2661     *all_folders = closure.all_folders;
2662 }
2663
2664 struct get_selected_file_closure {
2665   GtkFileChooserDefault *impl;
2666   GFile *file;
2667 };
2668
2669 static void
2670 get_selected_file_foreach_cb (GtkTreeModel *model,
2671                               GtkTreePath  *path,
2672                               GtkTreeIter  *iter,
2673                               gpointer      data)
2674 {
2675   struct get_selected_file_closure *closure = data;
2676
2677   if (closure->file)
2678     {
2679       /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2680       g_object_unref (closure->file);
2681       closure->file = NULL;
2682     }
2683
2684   gtk_tree_model_get (model, iter,
2685                       MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2686                       -1);
2687 }
2688
2689 /* Returns a selected path from the file list */
2690 static GFile *
2691 get_selected_file (GtkFileChooserDefault *impl)
2692 {
2693   struct get_selected_file_closure closure;
2694   GtkTreeSelection *selection;
2695
2696   closure.impl = impl;
2697   closure.file = NULL;
2698
2699   selection =  gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2700   gtk_tree_selection_selected_foreach (selection,
2701                                        get_selected_file_foreach_cb,
2702                                        &closure);
2703
2704   return closure.file;
2705 }
2706
2707 typedef struct {
2708   GtkFileChooserDefault *impl;
2709   gchar *tip;
2710 } UpdateTooltipData;
2711
2712 static void 
2713 update_tooltip (GtkTreeModel      *model,
2714                 GtkTreePath       *path,
2715                 GtkTreeIter       *iter,
2716                 gpointer           data)
2717 {
2718   UpdateTooltipData *udata = data;
2719
2720   if (udata->tip == NULL)
2721     {
2722       gchar *display_name;
2723
2724       gtk_tree_model_get (model, iter,
2725                           MODEL_COL_NAME, &display_name,
2726                           -1);
2727
2728       udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2729                                     display_name);
2730       g_free (display_name);
2731     }
2732 }
2733
2734
2735 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2736  * if there are no selected items *and* the current folder is not in the
2737  * bookmarks list.  De-sensitize the button otherwise.
2738  */
2739 static void
2740 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2741 {
2742   gint num_selected;
2743   gboolean all_folders;
2744   gboolean active;
2745   gchar *tip;
2746
2747   selection_check (impl, &num_selected, NULL, &all_folders);
2748
2749   if (num_selected == 0)
2750     active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2751   else if (num_selected == 1)
2752     {
2753       GFile *file;
2754
2755       file = get_selected_file (impl);
2756       active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2757       if (file)
2758         g_object_unref (file);
2759     }
2760   else
2761     active = all_folders;
2762
2763   gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2764
2765   if (impl->browse_files_popup_menu_add_shortcut_item)
2766     gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2767                               (num_selected == 0) ? FALSE : active);
2768
2769   if (active)
2770     {
2771       if (num_selected == 0)
2772         tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2773       else if (num_selected > 1)
2774         tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2775       else
2776         {
2777           GtkTreeSelection *selection;
2778           UpdateTooltipData data;
2779
2780           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2781           data.impl = impl;
2782           data.tip = NULL;
2783           gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2784           tip = data.tip;
2785         }
2786
2787       gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2788       g_free (tip);
2789     }
2790 }
2791
2792 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2793  * bookmark row is selected in the shortcuts tree.
2794  */
2795 static void
2796 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2797 {
2798   GtkTreeIter iter;
2799   gboolean removable = FALSE;
2800   gchar *name = NULL;
2801   gchar *tip;
2802   
2803   if (shortcuts_get_selected (impl, &iter))
2804     {
2805       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2806                           SHORTCUTS_COL_REMOVABLE, &removable,
2807                           SHORTCUTS_COL_NAME, &name,
2808                           -1);
2809       gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2810
2811       if (removable)
2812         tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2813       else
2814         tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2815
2816       gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2817       g_free (tip);
2818     }
2819   else
2820     gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2821                                  _("Remove the selected bookmark"));
2822   g_free (name);
2823 }
2824
2825 static void
2826 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2827 {
2828   GtkTreeIter iter;
2829   gboolean removable = FALSE;
2830
2831   if (impl->browse_shortcuts_popup_menu == NULL)
2832     return;
2833
2834   if (shortcuts_get_selected (impl, &iter))
2835     gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2836                         SHORTCUTS_COL_REMOVABLE, &removable,
2837                         -1);
2838
2839   gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2840   gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2841 }
2842
2843 /* GtkWidget::drag-begin handler for the shortcuts list. */
2844 static void
2845 shortcuts_drag_begin_cb (GtkWidget             *widget,
2846                          GdkDragContext        *context,
2847                          GtkFileChooserDefault *impl)
2848 {
2849 #if 0
2850   impl->shortcuts_drag_context = g_object_ref (context);
2851 #endif
2852 }
2853
2854 #if 0
2855 /* Removes the idle handler for outside drags */
2856 static void
2857 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2858 {
2859   if (!impl->shortcuts_drag_outside_idle)
2860     return;
2861
2862   g_source_destroy (impl->shortcuts_drag_outside_idle);
2863   impl->shortcuts_drag_outside_idle = NULL;
2864 }
2865 #endif
2866
2867 /* GtkWidget::drag-end handler for the shortcuts list. */
2868 static void
2869 shortcuts_drag_end_cb (GtkWidget             *widget,
2870                        GdkDragContext        *context,
2871                        GtkFileChooserDefault *impl)
2872 {
2873 #if 0
2874   g_object_unref (impl->shortcuts_drag_context);
2875
2876   shortcuts_cancel_drag_outside_idle (impl);
2877
2878   if (!impl->shortcuts_drag_outside)
2879     return;
2880
2881   gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2882
2883   impl->shortcuts_drag_outside = FALSE;
2884 #endif
2885 }
2886
2887 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2888 static void
2889 shortcuts_drag_data_delete_cb (GtkWidget             *widget,
2890                                GdkDragContext        *context,
2891                                GtkFileChooserDefault *impl)
2892 {
2893   g_signal_stop_emission_by_name (widget, "drag-data-delete");
2894 }
2895
2896 /* GtkWidget::drag-leave handler for the shortcuts list.  We unhighlight the
2897  * drop position.
2898  */
2899 static void
2900 shortcuts_drag_leave_cb (GtkWidget             *widget,
2901                          GdkDragContext        *context,
2902                          guint                  time_,
2903                          GtkFileChooserDefault *impl)
2904 {
2905 #if 0
2906   if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2907     {
2908       impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2909     }
2910 #endif
2911
2912   gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2913                                    NULL,
2914                                    GTK_TREE_VIEW_DROP_BEFORE);
2915
2916   g_signal_stop_emission_by_name (widget, "drag-leave");
2917 }
2918
2919 /* Computes the appropriate row and position for dropping */
2920 static void
2921 shortcuts_compute_drop_position (GtkFileChooserDefault   *impl,
2922                                  int                      x,
2923                                  int                      y,
2924                                  GtkTreePath            **path,
2925                                  GtkTreeViewDropPosition *pos)
2926 {
2927   GtkTreeView *tree_view;
2928   GtkTreeViewColumn *column;
2929   int cell_y;
2930   GdkRectangle cell;
2931   int row;
2932   int bookmarks_index;
2933   int header_height = 0;
2934
2935   tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2936
2937   if (gtk_tree_view_get_headers_visible (tree_view))
2938     header_height = _gtk_tree_view_get_header_height (tree_view);
2939
2940   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2941
2942   if (!gtk_tree_view_get_path_at_pos (tree_view,
2943                                       x,
2944                                       y - header_height,
2945                                       path,
2946                                       &column,
2947                                       NULL,
2948                                       &cell_y))
2949     {
2950       row = bookmarks_index + impl->num_bookmarks - 1;
2951       *path = gtk_tree_path_new_from_indices (row, -1);
2952       *pos = GTK_TREE_VIEW_DROP_AFTER;
2953       return;
2954     }
2955
2956   row = *gtk_tree_path_get_indices (*path);
2957   gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2958   gtk_tree_path_free (*path);
2959
2960   if (row < bookmarks_index)
2961     {
2962       row = bookmarks_index;
2963       *pos = GTK_TREE_VIEW_DROP_BEFORE;
2964     }
2965   else if (row > bookmarks_index + impl->num_bookmarks - 1)
2966     {
2967       row = bookmarks_index + impl->num_bookmarks - 1;
2968       *pos = GTK_TREE_VIEW_DROP_AFTER;
2969     }
2970   else
2971     {
2972       if (cell_y < cell.height / 2)
2973         *pos = GTK_TREE_VIEW_DROP_BEFORE;
2974       else
2975         *pos = GTK_TREE_VIEW_DROP_AFTER;
2976     }
2977
2978   *path = gtk_tree_path_new_from_indices (row, -1);
2979 }
2980
2981 /* GtkWidget::drag-motion handler for the shortcuts list.  We basically
2982  * implement the destination side of DnD by hand, due to limitations in
2983  * GtkTreeView's DnD API.
2984  */
2985 static gboolean
2986 shortcuts_drag_motion_cb (GtkWidget             *widget,
2987                           GdkDragContext        *context,
2988                           gint                   x,
2989                           gint                   y,
2990                           guint                  time_,
2991                           GtkFileChooserDefault *impl)
2992 {
2993   GtkTreePath *path;
2994   GtkTreeViewDropPosition pos;
2995   GdkDragAction action;
2996
2997 #if 0
2998   if (gtk_drag_get_source_widget (context) == widget)
2999     {
3000       shortcuts_cancel_drag_outside_idle (impl);
3001
3002       if (impl->shortcuts_drag_outside)
3003         {
3004           shortcuts_drag_set_delete_cursor (impl, FALSE);
3005           impl->shortcuts_drag_outside = FALSE;
3006         }
3007     }
3008 #endif
3009
3010   if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3011       (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3012     action = GDK_ACTION_COPY;
3013   else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3014            (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3015     action = GDK_ACTION_MOVE;
3016   else
3017     {
3018       action = 0;
3019       goto out;
3020     }
3021
3022   shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3023   gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3024   gtk_tree_path_free (path);
3025
3026  out:
3027
3028   g_signal_stop_emission_by_name (widget, "drag-motion");
3029
3030   if (action != 0)
3031     {
3032       gdk_drag_status (context, action, time_);
3033       return TRUE;
3034     }
3035   else
3036     return FALSE;
3037 }
3038
3039 /* GtkWidget::drag-drop handler for the shortcuts list. */
3040 static gboolean
3041 shortcuts_drag_drop_cb (GtkWidget             *widget,
3042                         GdkDragContext        *context,
3043                         gint                   x,
3044                         gint                   y,
3045                         guint                  time_,
3046                         GtkFileChooserDefault *impl)
3047 {
3048 #if 0
3049   shortcuts_cancel_drag_outside_idle (impl);
3050 #endif
3051
3052   g_signal_stop_emission_by_name (widget, "drag-drop");
3053   return TRUE;
3054 }
3055
3056 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3057 static void
3058 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3059                      GtkSelectionData      *selection_data,
3060                      int                    position)
3061 {
3062   gchar **uris;
3063   gint i;
3064
3065   uris = gtk_selection_data_get_uris (selection_data);
3066   if (!uris)
3067     return;
3068
3069   for (i = 0; uris[i]; i++)
3070     {
3071       char *uri;
3072       GFile *file;
3073
3074       uri = uris[i];
3075       file = g_file_new_for_uri (uri);
3076
3077       if (shortcuts_add_bookmark_from_file (impl, file, position))
3078         position++;
3079
3080       g_object_unref (file);
3081     }
3082
3083   g_strfreev (uris);
3084 }
3085
3086 /* Reorders the selected bookmark to the specified position */
3087 static void
3088 shortcuts_reorder (GtkFileChooserDefault *impl,
3089                    int                    new_position)
3090 {
3091   GtkTreeIter iter;
3092   gpointer col_data;
3093   ShortcutType shortcut_type;
3094   GtkTreePath *path;
3095   int old_position;
3096   int bookmarks_index;
3097   GFile *file;
3098   GError *error;
3099   gchar *name = NULL;
3100
3101   /* Get the selected path */
3102
3103   if (!shortcuts_get_selected (impl, &iter))
3104     g_assert_not_reached ();
3105
3106   path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3107   old_position = *gtk_tree_path_get_indices (path);
3108   gtk_tree_path_free (path);
3109
3110   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3111   old_position -= bookmarks_index;
3112   g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3113
3114   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3115                       SHORTCUTS_COL_NAME, &name,
3116                       SHORTCUTS_COL_DATA, &col_data,
3117                       SHORTCUTS_COL_TYPE, &shortcut_type,
3118                       -1);
3119   g_assert (col_data != NULL);
3120   g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3121   
3122   file = col_data;
3123   g_object_ref (file); /* removal below will free file, so we need a new ref */
3124
3125   /* Remove the path from the old position and insert it in the new one */
3126
3127   if (new_position > old_position)
3128     new_position--;
3129
3130   if (old_position == new_position)
3131     goto out;
3132
3133   error = NULL;
3134   if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3135     {
3136       shortcuts_add_bookmark_from_file (impl, file, new_position);
3137       _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3138     }
3139   else
3140     error_adding_bookmark_dialog (impl, file, error);
3141
3142  out:
3143
3144   g_object_unref (file);
3145   g_free (name);
3146 }
3147
3148 /* Callback used when we get the drag data for the bookmarks list.  We add the
3149  * received URIs as bookmarks if they are folders.
3150  */
3151 static void
3152 shortcuts_drag_data_received_cb (GtkWidget          *widget,
3153                                  GdkDragContext     *context,
3154                                  gint                x,
3155                                  gint                y,
3156                                  GtkSelectionData   *selection_data,
3157                                  guint               info,
3158                                  guint               time_,
3159                                  gpointer            data)
3160 {
3161   GtkFileChooserDefault *impl;
3162   GtkTreePath *tree_path;
3163   GtkTreeViewDropPosition tree_pos;
3164   GdkAtom target;
3165   int position;
3166   int bookmarks_index;
3167
3168   impl = GTK_FILE_CHOOSER_DEFAULT (data);
3169
3170   /* Compute position */
3171
3172   bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3173
3174   shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3175   position = *gtk_tree_path_get_indices (tree_path);
3176   gtk_tree_path_free (tree_path);
3177
3178   if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3179     position++;
3180
3181   g_assert (position >= bookmarks_index);
3182   position -= bookmarks_index;
3183
3184   target = gtk_selection_data_get_target (selection_data);
3185
3186   if (gtk_targets_include_uri (&target, 1))
3187     shortcuts_drop_uris (impl, selection_data, position);
3188   else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3189     shortcuts_reorder (impl, position);
3190
3191   g_signal_stop_emission_by_name (widget, "drag-data-received");
3192 }
3193
3194 /* Callback used to display a tooltip in the shortcuts tree */
3195 static gboolean
3196 shortcuts_query_tooltip_cb (GtkWidget             *widget,
3197                             gint                   x,
3198                             gint                   y,
3199                             gboolean               keyboard_mode,
3200                             GtkTooltip            *tooltip,
3201                             GtkFileChooserDefault *impl)
3202 {
3203   GtkTreeModel *model;
3204   GtkTreeIter iter;
3205
3206   if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3207                                          &x, &y,
3208                                          keyboard_mode,
3209                                          &model,
3210                                          NULL,
3211                                          &iter))
3212     {
3213       gpointer col_data;
3214       ShortcutType shortcut_type;
3215
3216       gtk_tree_model_get (model, &iter,
3217                           SHORTCUTS_COL_DATA, &col_data,
3218                           SHORTCUTS_COL_TYPE, &shortcut_type,
3219                           -1);
3220
3221       if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3222         return FALSE;
3223       else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3224         return FALSE;
3225       else if (shortcut_type == SHORTCUT_TYPE_FILE)
3226         {
3227           GFile *file;
3228           char *parse_name;
3229
3230           file = G_FILE (col_data);
3231           parse_name = g_file_get_parse_name (file);
3232
3233           gtk_tooltip_set_text (tooltip, parse_name);
3234
3235           g_free (parse_name);
3236
3237           return TRUE;
3238         }
3239       else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3240         {
3241           return FALSE;
3242         }
3243       else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3244         {
3245           return FALSE;
3246         }
3247     }
3248
3249   return FALSE;
3250 }
3251
3252
3253 /* Callback used when the selection in the shortcuts tree changes */
3254 static void
3255 shortcuts_selection_changed_cb (GtkTreeSelection      *selection,
3256                                 GtkFileChooserDefault *impl)
3257 {
3258   GtkTreeIter iter;
3259   GtkTreeIter child_iter;
3260
3261   bookmarks_check_remove_sensitivity (impl);
3262   shortcuts_check_popup_sensitivity (impl);
3263
3264   if (impl->changing_folder)
3265     return;
3266
3267   if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3268     {
3269       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3270                                                         &child_iter,
3271                                                         &iter);
3272       shortcuts_activate_iter (impl, &child_iter);
3273     }
3274 }
3275
3276 static gboolean
3277 shortcuts_row_separator_func (GtkTreeModel *model,
3278                               GtkTreeIter  *iter,
3279                               gpointer      data)
3280 {
3281   ShortcutType shortcut_type;
3282
3283   gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3284   
3285   return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3286 }
3287
3288 static gboolean
3289 shortcuts_key_press_event_after_cb (GtkWidget             *tree_view,
3290                                     GdkEventKey           *event,
3291                                     GtkFileChooserDefault *impl)
3292 {
3293   GtkWidget *entry;
3294
3295   /* don't screw up focus switching with Tab */
3296   if (event->keyval == GDK_KEY_Tab
3297       || event->keyval == GDK_KEY_KP_Tab
3298       || event->keyval == GDK_KEY_ISO_Left_Tab
3299       || event->length < 1)
3300     return FALSE;
3301
3302   if (impl->location_entry)
3303     entry = impl->location_entry;
3304   else if (impl->search_entry)
3305     entry = impl->search_entry;
3306   else
3307     entry = NULL;
3308
3309   if (entry)
3310     {
3311       gtk_widget_grab_focus (entry);
3312       return gtk_widget_event (entry, (GdkEvent *) event);
3313     }
3314   else
3315     return FALSE;
3316 }
3317
3318 /* Callback used when the file list's popup menu is detached */
3319 static void
3320 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3321                                 GtkMenu   *menu)
3322 {
3323   GtkFileChooserDefault *impl;
3324   
3325   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3326   g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3327
3328   impl->browse_shortcuts_popup_menu = NULL;
3329   impl->browse_shortcuts_popup_menu_remove_item = NULL;
3330   impl->browse_shortcuts_popup_menu_rename_item = NULL;
3331 }
3332
3333 static void
3334 remove_shortcut_cb (GtkMenuItem           *item,
3335                     GtkFileChooserDefault *impl)
3336 {
3337   remove_selected_bookmarks (impl);
3338 }
3339
3340 /* Rename the selected bookmark */
3341 static void
3342 rename_selected_bookmark (GtkFileChooserDefault *impl)
3343 {
3344   GtkTreeIter iter;
3345   GtkTreePath *path;
3346   GtkTreeViewColumn *column;
3347   GtkCellRenderer *cell;
3348   GList *renderers;
3349
3350   if (shortcuts_get_selected (impl, &iter))
3351     {
3352       path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3353       column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3354       renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3355       cell = g_list_nth_data (renderers, 1);
3356       g_list_free (renderers);
3357       g_object_set (cell, "editable", TRUE, NULL);
3358       gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3359                                         path, column, cell, TRUE);
3360       gtk_tree_path_free (path);
3361     }
3362 }
3363
3364 static void
3365 rename_shortcut_cb (GtkMenuItem           *item,
3366                     GtkFileChooserDefault *impl)
3367 {
3368   rename_selected_bookmark (impl);
3369 }
3370
3371 /* Constructs the popup menu for the file list if needed */
3372 static void
3373 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3374 {
3375   GtkWidget *item;
3376
3377   if (impl->browse_shortcuts_popup_menu)
3378     return;
3379
3380   impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3381   gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3382                              impl->browse_shortcuts_tree_view,
3383                              shortcuts_popup_menu_detach_cb);
3384
3385   item = gtk_image_menu_item_new_with_label (_("Remove"));
3386   impl->browse_shortcuts_popup_menu_remove_item = item;
3387   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3388                                  gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3389   g_signal_connect (item, "activate",
3390                     G_CALLBACK (remove_shortcut_cb), impl);
3391   gtk_widget_show (item);
3392   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3393
3394   item = gtk_menu_item_new_with_label (_("Rename…"));
3395   impl->browse_shortcuts_popup_menu_rename_item = item;
3396   g_signal_connect (item, "activate",
3397                     G_CALLBACK (rename_shortcut_cb), impl);
3398   gtk_widget_show (item);
3399   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3400 }
3401
3402 static void
3403 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3404 {
3405   shortcuts_build_popup_menu (impl);  
3406   shortcuts_check_popup_sensitivity (impl);
3407 }
3408
3409 static void
3410 popup_position_func (GtkMenu   *menu,
3411                      gint      *x,
3412                      gint      *y,
3413                      gboolean  *push_in,
3414                      gpointer   user_data);
3415
3416 static void
3417 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3418                       GdkEventButton        *event)
3419 {
3420   shortcuts_update_popup_menu (impl);
3421   if (event)
3422     gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3423                     NULL, NULL, NULL, NULL,
3424                     event->button, event->time);
3425   else
3426     {
3427       gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3428                       NULL, NULL,
3429                       popup_position_func, impl->browse_shortcuts_tree_view,
3430                       0, GDK_CURRENT_TIME);
3431       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3432                                    FALSE);
3433     }
3434 }
3435
3436 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3437 static gboolean
3438 shortcuts_popup_menu_cb (GtkWidget *widget,
3439                          GtkFileChooserDefault *impl)
3440 {
3441   shortcuts_popup_menu (impl, NULL);
3442   return TRUE;
3443 }
3444
3445 /* Callback used when a button is pressed on the shortcuts list.  
3446  * We trap button 3 to bring up a popup menu.
3447  */
3448 static gboolean
3449 shortcuts_button_press_event_cb (GtkWidget             *widget,
3450                                  GdkEventButton        *event,
3451                                  GtkFileChooserDefault *impl)
3452 {
3453   static gboolean in_press = FALSE;
3454   gboolean handled;
3455
3456   if (in_press)
3457     return FALSE;
3458
3459   if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
3460     return FALSE;
3461
3462   in_press = TRUE;
3463   handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3464   in_press = FALSE;
3465
3466   if (!handled)
3467     return FALSE;
3468
3469   shortcuts_popup_menu (impl, event);
3470   return TRUE;
3471 }
3472
3473 static void
3474 shortcuts_edited (GtkCellRenderer       *cell,
3475                   gchar                 *path_string,
3476                   gchar                 *new_text,
3477                   GtkFileChooserDefault *impl)
3478 {
3479   GtkTreePath *path;
3480   GtkTreeIter iter;
3481   GFile *shortcut;
3482
3483   g_object_set (cell, "editable", FALSE, NULL);
3484
3485   path = gtk_tree_path_new_from_string (path_string);
3486   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3487     g_assert_not_reached ();
3488
3489   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3490                       SHORTCUTS_COL_DATA, &shortcut,
3491                       -1);
3492   gtk_tree_path_free (path);
3493   
3494   _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3495 }
3496
3497 static void
3498 shortcuts_editing_canceled (GtkCellRenderer       *cell,
3499                             GtkFileChooserDefault *impl)
3500 {
3501   g_object_set (cell, "editable", FALSE, NULL);
3502 }
3503
3504 /* Creates the widgets for the shortcuts and bookmarks tree */
3505 static GtkWidget *
3506 shortcuts_list_create (GtkFileChooserDefault *impl)
3507 {
3508   GtkWidget *swin;
3509   GtkTreeSelection *selection;
3510   GtkTreeViewColumn *column;
3511   GtkCellRenderer *renderer;
3512
3513   /* Target types for dragging a row to/from the shortcuts list */
3514   const GtkTargetEntry tree_model_row_targets[] = {
3515     { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3516   };
3517
3518   /* Scrolled window */
3519
3520   swin = gtk_scrolled_window_new (NULL, NULL);
3521   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3522                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3523   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3524                                        GTK_SHADOW_IN);
3525   gtk_widget_show (swin);
3526
3527   /* Tree */
3528   impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3529   gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3530                                GTK_STYLE_CLASS_SIDEBAR);
3531   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3532 #ifdef PROFILE_FILE_CHOOSER
3533   g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3534 #endif
3535
3536   /* Connect "after" to key-press-event on the shortcuts pane.  We want this action to be possible:
3537    *
3538    *   1. user brings up a SAVE dialog
3539    *   2. user clicks on a shortcut in the shortcuts pane
3540    *   3. user starts typing a filename
3541    *
3542    * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3543    * support interactive search.  However, we'd rather focus the location entry
3544    * so that the user can type *there*.
3545    *
3546    * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3547    * filename entry if one clicks on a shortcut; rather, we focus the entry only
3548    * if the user starts typing while the focus is in the shortcuts pane.
3549    */
3550   g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3551                           G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3552
3553   g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3554                     G_CALLBACK (shortcuts_popup_menu_cb), impl);
3555   g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3556                     G_CALLBACK (shortcuts_button_press_event_cb), impl);
3557   /* Accessible object name for the file chooser's shortcuts pane */
3558   atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3559
3560   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3561
3562   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3563                                           GDK_BUTTON1_MASK,
3564                                           tree_model_row_targets,
3565                                           G_N_ELEMENTS (tree_model_row_targets),
3566                                           GDK_ACTION_MOVE);
3567
3568   gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3569                      GTK_DEST_DEFAULT_ALL,
3570                      tree_model_row_targets,
3571                      G_N_ELEMENTS (tree_model_row_targets),
3572                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
3573   gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3574
3575   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3576   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3577   gtk_tree_selection_set_select_function (selection,
3578                                           shortcuts_select_func,
3579                                           impl, NULL);
3580
3581   g_signal_connect (selection, "changed",
3582                     G_CALLBACK (shortcuts_selection_changed_cb), impl);
3583
3584   g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3585                     G_CALLBACK (shortcuts_key_press_event_cb), impl);
3586
3587   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3588                     G_CALLBACK (shortcuts_drag_begin_cb), impl);
3589   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3590                     G_CALLBACK (shortcuts_drag_end_cb), impl);
3591   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3592                     G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3593
3594   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3595                     G_CALLBACK (shortcuts_drag_leave_cb), impl);
3596   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3597                     G_CALLBACK (shortcuts_drag_motion_cb), impl);
3598   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3599                     G_CALLBACK (shortcuts_drag_drop_cb), impl);
3600   g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3601                     G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3602
3603   /* Support tooltips */
3604   gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3605   g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3606                     G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3607
3608   gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3609   gtk_widget_show (impl->browse_shortcuts_tree_view);
3610
3611   /* Column */
3612
3613   column = gtk_tree_view_column_new ();
3614   /* Column header for the file chooser's shortcuts pane */
3615   gtk_tree_view_column_set_title (column, _("_Places"));
3616
3617   renderer = gtk_cell_renderer_pixbuf_new ();
3618   gtk_tree_view_column_pack_start (column, renderer, FALSE);
3619   gtk_tree_view_column_set_attributes (column, renderer,
3620                                        "pixbuf", SHORTCUTS_COL_PIXBUF,
3621                                        "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3622                                        NULL);
3623
3624   renderer = gtk_cell_renderer_text_new ();
3625   g_object_set (renderer,
3626                 "width-chars", 12,
3627                 "ellipsize", PANGO_ELLIPSIZE_END,
3628                 NULL);
3629   g_signal_connect (renderer, "edited",
3630                     G_CALLBACK (shortcuts_edited), impl);
3631   g_signal_connect (renderer, "editing-canceled",
3632                     G_CALLBACK (shortcuts_editing_canceled), impl);
3633   gtk_tree_view_column_pack_start (column, renderer, TRUE);
3634   gtk_tree_view_column_set_attributes (column, renderer,
3635                                        "text", SHORTCUTS_COL_NAME,
3636                                        NULL);
3637
3638   gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3639                                         shortcuts_row_separator_func,
3640                                         NULL, NULL);
3641
3642   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3643
3644   return swin;
3645 }
3646
3647 /* Creates the widgets for the shortcuts/bookmarks pane */
3648 static GtkWidget *
3649 shortcuts_pane_create (GtkFileChooserDefault *impl,
3650                        GtkSizeGroup          *size_group)
3651 {
3652   GtkWidget *vbox;
3653   GtkWidget *toolbar;
3654   GtkWidget *widget;
3655   GtkStyleContext *context;
3656   GIcon *icon;
3657
3658   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3659   gtk_widget_show (vbox);
3660
3661   /* Shortcuts tree */
3662
3663   widget = shortcuts_list_create (impl);
3664
3665   gtk_size_group_add_widget (size_group, widget);
3666   context = gtk_widget_get_style_context (widget);
3667   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3668
3669   gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3670
3671   /* Box for buttons */
3672
3673   toolbar = gtk_toolbar_new ();
3674   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
3675   gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3676
3677   context = gtk_widget_get_style_context (toolbar);
3678   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3679   gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3680   
3681   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3682   gtk_widget_show (toolbar);
3683
3684   /* Add bookmark button */
3685   icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3686   impl->browse_shortcuts_add_button = toolbutton_new (impl,
3687                                                       icon,
3688                                                       FALSE,
3689                                                       TRUE,
3690                                                       G_CALLBACK (add_bookmark_button_clicked_cb));
3691   g_object_unref (icon);
3692
3693   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3694   gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3695                                _("Add the selected folder to the Bookmarks"));
3696
3697   /* Remove bookmark button */
3698   icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3699   impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3700                                                          icon,
3701                                                          FALSE,
3702                                                          TRUE,
3703                                                          G_CALLBACK (remove_bookmark_button_clicked_cb));
3704   g_object_unref (icon);
3705   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3706   gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3707                                _("Remove the selected bookmark"));
3708
3709   return vbox;
3710 }
3711
3712 static gboolean
3713 key_is_left_or_right (GdkEventKey *event)
3714 {
3715   guint modifiers;
3716
3717   modifiers = gtk_accelerator_get_default_mod_mask ();
3718
3719   return ((event->keyval == GDK_KEY_Right
3720            || event->keyval == GDK_KEY_KP_Right
3721            || event->keyval == GDK_KEY_Left
3722            || event->keyval == GDK_KEY_KP_Left)
3723           && (event->state & modifiers) == 0);
3724 }
3725
3726 /* Handles key press events on the file list, so that we can trap Enter to
3727  * activate the default button on our own.  Also, checks to see if '/' has been
3728  * pressed.
3729  */
3730 static gboolean
3731 browse_files_key_press_event_cb (GtkWidget   *widget,
3732                                  GdkEventKey *event,
3733                                  gpointer     data)
3734 {
3735   GtkFileChooserDefault *impl;
3736   GdkModifierType no_text_input_mask;
3737
3738   impl = (GtkFileChooserDefault *) data;
3739
3740   no_text_input_mask =
3741     gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
3742
3743   if ((event->keyval == GDK_KEY_slash
3744        || event->keyval == GDK_KEY_KP_Divide
3745 #ifdef G_OS_UNIX
3746        || event->keyval == GDK_KEY_asciitilde
3747 #endif
3748        ) && !(event->state & no_text_input_mask))
3749     {
3750       location_popup_handler (impl, event->string);
3751       return TRUE;
3752     }
3753
3754   if (key_is_left_or_right (event))
3755     {
3756       gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3757       return TRUE;
3758     }
3759
3760   if ((event->keyval == GDK_KEY_Return
3761        || event->keyval == GDK_KEY_ISO_Enter
3762        || event->keyval == GDK_KEY_KP_Enter
3763        || event->keyval == GDK_KEY_space
3764        || event->keyval == GDK_KEY_KP_Space)
3765       && !(event->state & gtk_accelerator_get_default_mod_mask ())
3766       && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3767            impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3768     {
3769       GtkWindow *window;
3770
3771       window = get_toplevel (widget);
3772       if (window)
3773         {
3774           GtkWidget *default_widget, *focus_widget;
3775
3776           default_widget = gtk_window_get_default_widget (window);
3777           focus_widget = gtk_window_get_focus (window);
3778
3779           if (widget != default_widget &&
3780               !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3781             {
3782               gtk_window_activate_default (window);
3783
3784               return TRUE;
3785             }
3786         }
3787     }
3788
3789   return FALSE;
3790 }
3791
3792 /* Callback used when the file list's popup menu is detached */
3793 static void
3794 popup_menu_detach_cb (GtkWidget *attach_widget,
3795                       GtkMenu   *menu)
3796 {
3797   GtkFileChooserDefault *impl;
3798
3799   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3800   g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3801
3802   impl->browse_files_popup_menu = NULL;
3803   impl->browse_files_popup_menu_add_shortcut_item = NULL;
3804   impl->browse_files_popup_menu_hidden_files_item = NULL;
3805   impl->browse_files_popup_menu_copy_file_location_item = NULL;
3806 }
3807
3808 /* Callback used when the "Add to Bookmarks" menu item is activated */
3809 static void
3810 add_to_shortcuts_cb (GtkMenuItem           *item,
3811                      GtkFileChooserDefault *impl)
3812 {
3813   bookmarks_add_selected_folder (impl);
3814 }
3815
3816 /* callback used to set data to clipboard */
3817 static void
3818 copy_file_get_cb  (GtkClipboard     *clipboard,
3819                    GtkSelectionData *selection_data,
3820                    guint             info,
3821                    gpointer          data)
3822 {
3823   GSList *selected_files = data;
3824
3825   if (selected_files)
3826     {
3827       gint num_files = g_slist_length (selected_files);
3828       gchar **uris;
3829       gint i;
3830       GSList *l;
3831
3832       uris = g_new (gchar *, num_files + 1);
3833       uris[num_files] = NULL; /* null terminator */
3834
3835       i = 0;
3836
3837       for (l = selected_files; l; l = l->next)
3838         {
3839           GFile *file = (GFile *) l->data;
3840
3841           if (info == SELECTION_URI)
3842             uris[i] = g_file_get_uri (file);
3843           else /* if (info == SELECTION_TEXT) - let this be the fallback */
3844             uris[i] = g_file_get_parse_name (file);
3845
3846           i++;
3847         }
3848
3849       if (info == SELECTION_URI)
3850         gtk_selection_data_set_uris (selection_data, uris);
3851       else /* if (info == SELECTION_TEXT) - let this be the fallback */
3852         {
3853           char *str = g_strjoinv (" ", uris);
3854           gtk_selection_data_set_text (selection_data, str, -1);
3855           g_free (str);
3856         }
3857
3858       g_strfreev (uris);
3859     }
3860 }
3861
3862 /* callback used to clear the clipboard data */
3863 static void
3864 copy_file_clear_cb (GtkClipboard *clipboard,
3865                     gpointer      data)
3866 {
3867   GSList *selected_files = data;
3868
3869   g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3870   g_slist_free (selected_files);
3871 }
3872
3873 /* Callback used when the "Copy file’s location" menu item is activated */
3874 static void
3875 copy_file_location_cb (GtkMenuItem           *item,
3876                        GtkFileChooserDefault *impl)
3877 {
3878   GSList *selected_files = NULL;
3879
3880   selected_files = search_get_selected_files (impl);
3881
3882   if (selected_files)
3883     {
3884       GtkClipboard *clipboard;
3885       GtkTargetList *target_list;
3886       GtkTargetEntry *targets;
3887       int n_targets;
3888
3889       clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3890
3891       target_list = gtk_target_list_new (NULL, 0);
3892       gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3893       gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3894
3895       targets = gtk_target_table_new_from_list (target_list, &n_targets);
3896       gtk_target_list_unref (target_list);
3897
3898       gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3899                                    copy_file_get_cb,
3900                                    copy_file_clear_cb,
3901                                    selected_files);
3902
3903       gtk_target_table_free (targets, n_targets);
3904     }
3905 }
3906
3907 /* Callback used when the "Visit this file" menu item is activated */
3908 static void
3909 visit_file_cb (GtkMenuItem *item,
3910                GtkFileChooserDefault *impl)
3911 {
3912   GSList *files;
3913
3914   files = search_get_selected_files (impl);
3915
3916   /* Sigh, just use the first one */
3917   if (files)
3918     {
3919       GFile *file = files->data;
3920
3921       gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL); /* NULL-GError */
3922     }
3923
3924   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
3925   g_slist_free (files);
3926 }
3927
3928 /* callback used when the "Show Hidden Files" menu item is toggled */
3929 static void
3930 show_hidden_toggled_cb (GtkCheckMenuItem      *item,
3931                         GtkFileChooserDefault *impl)
3932 {
3933   g_object_set (impl,
3934                 "show-hidden", gtk_check_menu_item_get_active (item),
3935                 NULL);
3936 }
3937
3938 /* Callback used when the "Show Size Column" menu item is toggled */
3939 static void
3940 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3941                              GtkFileChooserDefault *impl)
3942 {
3943   impl->show_size_column = gtk_check_menu_item_get_active (item);
3944
3945   gtk_tree_view_column_set_visible (impl->list_size_column,
3946                                     impl->show_size_column);
3947 }
3948
3949 /* Shows an error dialog about not being able to select a dragged file */
3950 static void
3951 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3952                                      GFile                 *file,
3953                                      GError                *error)
3954 {
3955   error_dialog (impl,
3956                 _("Could not select file"),
3957                 file, error);
3958 }
3959
3960 static void
3961 file_list_drag_data_select_uris (GtkFileChooserDefault  *impl,
3962                                  gchar                 **uris)
3963 {
3964   int i;
3965   char *uri;
3966   GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3967
3968   for (i = 1; uris[i]; i++)
3969     {
3970       GFile *file;
3971       GError *error = NULL;
3972
3973       uri = uris[i];
3974       file = g_file_new_for_uri (uri);
3975
3976       gtk_file_chooser_default_select_file (chooser, file, &error);
3977       if (error)
3978         error_selecting_dragged_file_dialog (impl, file, error);
3979
3980       g_object_unref (file);
3981     }
3982 }
3983
3984 struct FileListDragData
3985 {
3986   GtkFileChooserDefault *impl;
3987   gchar **uris;
3988   GFile *file;
3989 };
3990
3991 static void
3992 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3993                                           GFileInfo    *info,
3994                                           const GError *error,
3995                                           gpointer      user_data)
3996 {
3997   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3998   struct FileListDragData *data = user_data;
3999   GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4000
4001   if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4002     goto out;
4003
4004   data->impl->file_list_drag_data_received_cancellable = NULL;
4005
4006   if (cancelled || error)
4007     goto out;
4008
4009   if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4010        data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4011       data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4012     change_folder_and_display_error (data->impl, data->file, FALSE);
4013   else
4014     {
4015       GError *error = NULL;
4016
4017       gtk_file_chooser_default_unselect_all (chooser);
4018       gtk_file_chooser_default_select_file (chooser, data->file, &error);
4019       if (error)
4020         error_selecting_dragged_file_dialog (data->impl, data->file, error);
4021       else
4022         browse_files_center_selected_row (data->impl);
4023     }
4024
4025   if (data->impl->select_multiple)
4026     file_list_drag_data_select_uris (data->impl, data->uris);
4027
4028 out:
4029   g_object_unref (data->impl);
4030   g_strfreev (data->uris);
4031   g_object_unref (data->file);
4032   g_free (data);
4033
4034   g_object_unref (cancellable);
4035 }
4036
4037 static void
4038 file_list_drag_data_received_cb (GtkWidget        *widget,
4039                                  GdkDragContext   *context,
4040                                  gint              x,
4041                                  gint              y,
4042                                  GtkSelectionData *selection_data,
4043                                  guint             info,
4044                                  guint             time_,
4045                                  gpointer          data)
4046 {
4047   GtkFileChooserDefault *impl;
4048   gchar **uris;
4049   char *uri;
4050   GFile *file;
4051
4052   impl = GTK_FILE_CHOOSER_DEFAULT (data);
4053
4054   /* Allow only drags from other widgets; see bug #533891. */
4055   if (gtk_drag_get_source_widget (context) == widget)
4056     {
4057       g_signal_stop_emission_by_name (widget, "drag-data-received");
4058       return;
4059     }
4060
4061   /* Parse the text/uri-list string, navigate to the first one */
4062   uris = gtk_selection_data_get_uris (selection_data);
4063   if (uris && uris[0])
4064     {
4065       struct FileListDragData *data;
4066
4067       uri = uris[0];
4068       file = g_file_new_for_uri (uri);
4069
4070       data = g_new0 (struct FileListDragData, 1);
4071       data->impl = g_object_ref (impl);
4072       data->uris = uris;
4073       data->file = file;
4074
4075       if (impl->file_list_drag_data_received_cancellable)
4076         g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4077
4078       impl->file_list_drag_data_received_cancellable =
4079         _gtk_file_system_get_info (impl->file_system, file,
4080                                    "standard::type",
4081                                    file_list_drag_data_received_get_info_cb,
4082                                    data);
4083     }
4084
4085   g_signal_stop_emission_by_name (widget, "drag-data-received");
4086 }
4087
4088 /* Don't do anything with the drag_drop signal */
4089 static gboolean
4090 file_list_drag_drop_cb (GtkWidget             *widget,
4091                         GdkDragContext        *context,
4092                         gint                   x,
4093                         gint                   y,
4094                         guint                  time_,
4095                         GtkFileChooserDefault *impl)
4096 {
4097   g_signal_stop_emission_by_name (widget, "drag-drop");
4098   return TRUE;
4099 }
4100
4101 /* Disable the normal tree drag motion handler, it makes it look like you're
4102    dropping the dragged item onto a tree item */
4103 static gboolean
4104 file_list_drag_motion_cb (GtkWidget             *widget,
4105                           GdkDragContext        *context,
4106                           gint                   x,
4107                           gint                   y,
4108                           guint                  time_,
4109                           GtkFileChooserDefault *impl)
4110 {
4111   g_signal_stop_emission_by_name (widget, "drag-motion");
4112   return TRUE;
4113 }
4114
4115 /* Sensitizes the "Copy file’s location" context menu item if there is actually
4116  * a selection active.
4117  */
4118 static void
4119 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4120 {
4121   GtkTreeSelection *selection;
4122   gboolean active;
4123
4124   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4125   if (gtk_tree_selection_count_selected_rows (selection) == 0)
4126     active = FALSE;
4127   else
4128     active = TRUE;
4129
4130   if (impl->browse_files_popup_menu_copy_file_location_item)
4131     gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4132 }
4133
4134 static GtkWidget *
4135 file_list_add_image_menu_item (GtkFileChooserDefault *impl,
4136                                const char *stock_name,
4137                                const char *mnemonic_label,
4138                                GCallback callback)
4139 {
4140   GtkWidget *item;
4141
4142   item = gtk_image_menu_item_new_with_mnemonic (mnemonic_label);
4143   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4144                                  gtk_image_new_from_stock (stock_name, GTK_ICON_SIZE_MENU));
4145   g_signal_connect (item, "activate", callback, impl);
4146   gtk_widget_show (item);
4147   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4148   
4149   return item;
4150 }
4151
4152 static GtkWidget *
4153 file_list_add_check_menu_item (GtkFileChooserDefault *impl,
4154                                const char *mnemonic_label,
4155                                GCallback callback)
4156 {
4157   GtkWidget *item;
4158
4159   item = gtk_check_menu_item_new_with_mnemonic (mnemonic_label);
4160   g_signal_connect (item, "toggled", callback, impl);
4161   gtk_widget_show (item);
4162   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4163
4164   return item;
4165 }
4166
4167 /* Constructs the popup menu for the file list if needed */
4168 static void
4169 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4170 {
4171   GtkWidget *item;
4172
4173   if (impl->browse_files_popup_menu)
4174     return;
4175
4176   impl->browse_files_popup_menu = gtk_menu_new ();
4177   gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4178                              impl->browse_files_tree_view,
4179                              popup_menu_detach_cb);
4180
4181   impl->browse_files_popup_menu_visit_file_item         = file_list_add_image_menu_item (impl, GTK_STOCK_DIRECTORY, _("_Visit this file"),
4182                                                                                          G_CALLBACK (visit_file_cb));
4183
4184   impl->browse_files_popup_menu_copy_file_location_item = file_list_add_image_menu_item (impl, GTK_STOCK_COPY, _("_Copy file’s location"),
4185                                                                                          G_CALLBACK (copy_file_location_cb));
4186
4187   impl->browse_files_popup_menu_add_shortcut_item       = file_list_add_image_menu_item (impl, GTK_STOCK_ADD, _("_Add to Bookmarks"),
4188                                                                                          G_CALLBACK (add_to_shortcuts_cb));
4189
4190   item = gtk_separator_menu_item_new ();
4191   gtk_widget_show (item);
4192   gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4193
4194   impl->browse_files_popup_menu_hidden_files_item       = file_list_add_check_menu_item (impl, _("Show _Hidden Files"),
4195                                                                                          G_CALLBACK (show_hidden_toggled_cb));
4196
4197   impl->browse_files_popup_menu_size_column_item        = file_list_add_check_menu_item (impl, _("Show _Size Column"),
4198                                                                                          G_CALLBACK (show_size_column_toggled_cb));
4199
4200   bookmarks_check_add_sensitivity (impl);
4201   check_copy_file_location_sensitivity (impl);
4202 }
4203
4204 /* Updates the popup menu for the file list, creating it if necessary */
4205 static void
4206 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4207 {
4208   file_list_build_popup_menu (impl);
4209
4210   /* The sensitivity of the Add to Bookmarks item is set in
4211    * bookmarks_check_add_sensitivity()
4212    */
4213
4214   /* 'Visit this file' */
4215   gtk_widget_set_visible (impl->browse_files_popup_menu_visit_file_item, (impl->operation_mode != OPERATION_MODE_BROWSE));
4216
4217   /* 'Show Hidden Files' */
4218   g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4219                                    G_CALLBACK (show_hidden_toggled_cb), impl);
4220   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4221                                   impl->show_hidden);
4222   g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4223                                      G_CALLBACK (show_hidden_toggled_cb), impl);
4224
4225   /* 'Show Size Column' */
4226   g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4227                                    G_CALLBACK (show_size_column_toggled_cb), impl);
4228   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4229                                   impl->show_size_column);
4230   g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4231                                      G_CALLBACK (show_size_column_toggled_cb), impl);
4232 }
4233
4234 static void
4235 popup_position_func (GtkMenu   *menu,
4236                      gint      *x,
4237                      gint      *y,
4238                      gboolean  *push_in,
4239                      gpointer   user_data)
4240 {
4241   GtkAllocation allocation;
4242   GtkWidget *widget = GTK_WIDGET (user_data);
4243   GdkScreen *screen = gtk_widget_get_screen (widget);
4244   GtkRequisition req;
4245   gint monitor_num;
4246   GdkRectangle monitor;
4247
4248   g_return_if_fail (gtk_widget_get_realized (widget));
4249
4250   gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4251
4252   gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4253                                  &req, NULL);
4254
4255   gtk_widget_get_allocation (widget, &allocation);
4256   *x += (allocation.width - req.width) / 2;
4257   *y += (allocation.height - req.height) / 2;
4258
4259   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4260   gtk_menu_set_monitor (menu, monitor_num);
4261   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
4262
4263   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4264   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4265
4266   *push_in = FALSE;
4267 }
4268
4269 static void
4270 file_list_popup_menu (GtkFileChooserDefault *impl,
4271                       GdkEventButton        *event)
4272 {
4273   file_list_update_popup_menu (impl);
4274   if (event)
4275     gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4276                     NULL, NULL, NULL, NULL,
4277                     event->button, event->time);
4278   else
4279     {
4280       gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4281                       NULL, NULL,
4282                       popup_position_func, impl->browse_files_tree_view,
4283                       0, GDK_CURRENT_TIME);
4284       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4285                                    FALSE);
4286     }
4287
4288 }
4289
4290 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4291 static gboolean
4292 list_popup_menu_cb (GtkWidget *widget,
4293                     GtkFileChooserDefault *impl)
4294 {
4295   file_list_popup_menu (impl, NULL);
4296   return TRUE;
4297 }
4298
4299 /* Callback used when a button is pressed on the file list.  We trap button 3 to
4300  * bring up a popup menu.
4301  */
4302 static gboolean
4303 list_button_press_event_cb (GtkWidget             *widget,
4304                             GdkEventButton        *event,
4305                             GtkFileChooserDefault *impl)
4306 {
4307   static gboolean in_press = FALSE;
4308
4309   if (in_press)
4310     return FALSE;
4311
4312   if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
4313     return FALSE;
4314
4315   in_press = TRUE;
4316   gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4317   in_press = FALSE;
4318
4319   file_list_popup_menu (impl, event);
4320   return TRUE;
4321 }
4322
4323 typedef struct {
4324   OperationMode operation_mode;
4325   gint general_column;
4326   gint model_column;
4327 } ColumnMap;
4328
4329 /* Sets the sort column IDs for the file list; needs to be done whenever we
4330  * change the model on the treeview.
4331  */
4332 static void
4333 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4334 {
4335   gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4336   gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4337   gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4338 }
4339
4340 static gboolean
4341 file_list_query_tooltip_cb (GtkWidget  *widget,
4342                             gint        x,
4343                             gint        y,
4344                             gboolean    keyboard_tip,
4345                             GtkTooltip *tooltip,
4346                             gpointer    user_data)
4347 {
4348   GtkFileChooserDefault *impl = user_data;
4349   GtkTreeModel *model;
4350   GtkTreePath *path;
4351   GtkTreeIter iter;
4352   GFile *file;
4353   gchar *filename;
4354
4355   if (impl->operation_mode == OPERATION_MODE_BROWSE)
4356     return FALSE;
4357
4358
4359   if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4360                                           &x, &y,
4361                                           keyboard_tip,
4362                                           &model, &path, &iter))
4363     return FALSE;
4364                                        
4365   gtk_tree_model_get (model, &iter,
4366                       MODEL_COL_FILE, &file,
4367                       -1);
4368
4369   if (file == NULL)
4370     {
4371       gtk_tree_path_free (path);
4372       return FALSE;
4373     }
4374
4375   filename = g_file_get_path (file);
4376   gtk_tooltip_set_text (tooltip, filename);
4377   gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4378                                  tooltip,
4379                                  path);
4380
4381   g_free (filename);
4382   g_object_unref (file);
4383   gtk_tree_path_free (path);
4384
4385   return TRUE;
4386 }
4387
4388 static void
4389 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4390 {
4391   gint xpad, ypad;
4392
4393   gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4394   gtk_cell_renderer_set_fixed_size (renderer, 
4395                                     xpad * 2 + impl->icon_size,
4396                                     ypad * 2 + impl->icon_size);
4397 }
4398
4399 /* Creates the widgets for the file list */
4400 static GtkWidget *
4401 create_file_list (GtkFileChooserDefault *impl)
4402 {
4403   GtkWidget *swin;
4404   GtkTreeSelection *selection;
4405   GtkTreeViewColumn *column;
4406   GtkCellRenderer *renderer;
4407
4408   /* Scrolled window */
4409   swin = gtk_scrolled_window_new (NULL, NULL);
4410   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4411                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4412   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4413                                        GTK_SHADOW_IN);
4414
4415   /* Tree/list view */
4416
4417   impl->browse_files_tree_view = gtk_tree_view_new ();
4418 #ifdef PROFILE_FILE_CHOOSER
4419   g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4420 #endif
4421   g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4422   atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4423
4424   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4425   gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4426
4427   gtk_drag_dest_set (impl->browse_files_tree_view,
4428                      GTK_DEST_DEFAULT_ALL,
4429                      NULL, 0,
4430                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
4431   gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4432   
4433   g_signal_connect (impl->browse_files_tree_view, "row-activated",
4434                     G_CALLBACK (list_row_activated), impl);
4435   g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4436                     G_CALLBACK (browse_files_key_press_event_cb), impl);
4437   g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4438                     G_CALLBACK (list_popup_menu_cb), impl);
4439   g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4440                     G_CALLBACK (list_button_press_event_cb), impl);
4441
4442   g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4443                     G_CALLBACK (file_list_drag_data_received_cb), impl);
4444   g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4445                     G_CALLBACK (file_list_drag_drop_cb), impl);
4446   g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4447                     G_CALLBACK (file_list_drag_motion_cb), impl);
4448
4449   g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4450   g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4451                     G_CALLBACK (file_list_query_tooltip_cb), impl);
4452
4453   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4454   gtk_tree_selection_set_select_function (selection,
4455                                           list_select_func,
4456                                           impl, NULL);
4457   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4458                                           GDK_BUTTON1_MASK,
4459                                           NULL, 0,
4460                                           GDK_ACTION_COPY | GDK_ACTION_MOVE);
4461   gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4462
4463   g_signal_connect (selection, "changed",
4464                     G_CALLBACK (list_selection_changed), impl);
4465
4466   /* Keep the column order in sync with update_cell_renderer_attributes() */
4467
4468   /* Filename column */
4469
4470   impl->list_name_column = gtk_tree_view_column_new ();
4471   gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4472   gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4473   gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4474
4475   renderer = gtk_cell_renderer_pixbuf_new ();
4476   /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4477   set_icon_cell_renderer_fixed_size (impl, renderer);
4478   gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4479
4480   impl->list_name_renderer = gtk_cell_renderer_text_new ();
4481   g_object_set (impl->list_name_renderer,
4482                 "ellipsize", PANGO_ELLIPSIZE_END,
4483                 NULL);
4484   g_signal_connect (impl->list_name_renderer, "edited",
4485                     G_CALLBACK (renderer_edited_cb), impl);
4486   g_signal_connect (impl->list_name_renderer, "editing-canceled",
4487                     G_CALLBACK (renderer_editing_canceled_cb), impl);
4488   gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4489
4490   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4491
4492   /* Size column */
4493
4494   column = gtk_tree_view_column_new ();
4495   gtk_tree_view_column_set_resizable (column, TRUE);
4496   gtk_tree_view_column_set_title (column, _("Size"));
4497
4498   renderer = gtk_cell_renderer_text_new ();
4499   g_object_set (renderer, 
4500                 "alignment", PANGO_ALIGN_RIGHT,
4501                 NULL);
4502   gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4503   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4504   impl->list_size_column = column;
4505
4506   /* Modification time column */
4507
4508   column = gtk_tree_view_column_new ();
4509   gtk_tree_view_column_set_resizable (column, TRUE);
4510   gtk_tree_view_column_set_title (column, _("Modified"));
4511
4512   renderer = gtk_cell_renderer_text_new ();
4513   gtk_tree_view_column_pack_start (column, renderer, TRUE);
4514   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4515   impl->list_mtime_column = column;
4516   
4517   file_list_set_sort_column_ids (impl);
4518   update_cell_renderer_attributes (impl);
4519
4520   gtk_widget_show_all (swin);
4521
4522   return swin;
4523 }
4524
4525 /* Creates the widgets for the files/folders pane */
4526 static GtkWidget *
4527 file_pane_create (GtkFileChooserDefault *impl,
4528                   GtkSizeGroup          *size_group)
4529 {
4530   GtkWidget *vbox;
4531   GtkWidget *hbox;
4532   GtkWidget *widget;
4533
4534   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4535   gtk_widget_show (vbox);
4536
4537   /* Box for lists and preview */
4538
4539   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4540   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4541   gtk_widget_show (hbox);
4542
4543   /* File list */
4544
4545   widget = create_file_list (impl);
4546   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4547   gtk_size_group_add_widget (size_group, widget);
4548
4549   /* Preview */
4550
4551   impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4552   gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4553   /* Don't show preview box initially */
4554
4555   /* Filter combo */
4556
4557   impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4558
4559   widget = filter_create (impl);
4560
4561   gtk_widget_show (widget);
4562   gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4563
4564   gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4565
4566   return vbox;
4567 }
4568
4569 static void
4570 location_entry_create (GtkFileChooserDefault *impl)
4571 {
4572   if (!impl->location_entry)
4573     impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4574
4575   _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4576   _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4577   gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4578   gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4579 }
4580
4581 /* Creates the widgets specific to Save mode */
4582 static void
4583 save_widgets_create (GtkFileChooserDefault *impl)
4584 {
4585   GtkWidget *vbox;
4586   GtkWidget *widget;
4587
4588   if (impl->save_widgets != NULL)
4589     return;
4590
4591   location_switch_to_path_bar (impl);
4592
4593   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4594
4595   impl->save_widgets_table = gtk_grid_new ();
4596   gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4597   gtk_widget_show (impl->save_widgets_table);
4598   gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4599   gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4600
4601   /* Label */
4602
4603   widget = gtk_label_new_with_mnemonic (_("_Name:"));
4604   gtk_widget_set_halign (widget, GTK_ALIGN_START);
4605   gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4606   gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4607   gtk_widget_show (widget);
4608
4609   /* Location entry */
4610
4611   location_entry_create (impl);
4612   gtk_widget_set_hexpand (impl->location_entry, TRUE);
4613   gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4614   gtk_widget_show (impl->location_entry);
4615   gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4616
4617   /* Folder combo */
4618   impl->save_folder_label = gtk_label_new (NULL);
4619   gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4620   gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4621   gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4622   gtk_widget_show (impl->save_folder_label);
4623
4624   impl->save_widgets = vbox;
4625   gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4626   gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4627   gtk_widget_show (impl->save_widgets);
4628 }
4629
4630 /* Destroys the widgets specific to Save mode */
4631 static void
4632 save_widgets_destroy (GtkFileChooserDefault *impl)
4633 {
4634   if (impl->save_widgets == NULL)
4635     return;
4636
4637   gtk_widget_destroy (impl->save_widgets);
4638   impl->save_widgets = NULL;
4639   impl->save_widgets_table = NULL;
4640   impl->location_entry = NULL;
4641   impl->save_folder_label = NULL;
4642 }
4643
4644 /* Turns on the path bar widget.  Can be called even if we are already in that
4645  * mode.
4646  */
4647 static void
4648 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4649 {
4650   if (impl->location_entry)
4651     {
4652       gtk_widget_destroy (impl->location_entry);
4653       impl->location_entry = NULL;
4654     }
4655
4656   gtk_widget_hide (impl->location_entry_box);
4657 }
4658
4659 /* Turns on the location entry.  Can be called even if we are already in that
4660  * mode.
4661  */
4662 static void
4663 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4664 {
4665   /* when in search or recent files mode, we are not showing the
4666    * location_entry_box container, so there's no point in switching
4667    * to it.
4668    */
4669   if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4670       impl->operation_mode == OPERATION_MODE_RECENT)
4671     return;
4672
4673   /* Box */
4674
4675   gtk_widget_show (impl->location_entry_box);
4676
4677   /* Entry */
4678
4679   if (!impl->location_entry)
4680     {
4681       location_entry_create (impl);
4682       gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4683       gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4684     }
4685
4686   /* Configure the entry */
4687
4688   _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4689
4690   /* Done */
4691
4692   gtk_widget_show (impl->location_entry);
4693   gtk_widget_grab_focus (impl->location_entry);
4694 }
4695
4696 /* Sets a new location mode.  set_buttons determines whether the toggle button
4697  * for the mode will also be changed.
4698  */
4699 static void
4700 location_mode_set (GtkFileChooserDefault *impl,
4701                    LocationMode new_mode,
4702                    gboolean set_button)
4703 {
4704   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4705       impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4706     {
4707       GtkWindow *toplevel;
4708       GtkWidget *current_focus;
4709       gboolean button_active;
4710       gboolean switch_to_file_list;
4711
4712       switch (new_mode)
4713         {
4714         case LOCATION_MODE_PATH_BAR:
4715           button_active = FALSE;
4716
4717           /* The location_entry will disappear when we switch to path bar mode.  So,
4718            * we'll focus the file list in that case, to avoid having a window with
4719            * no focused widget.
4720            */
4721           toplevel = get_toplevel (GTK_WIDGET (impl));
4722           switch_to_file_list = FALSE;
4723           if (toplevel)
4724             {
4725               current_focus = gtk_window_get_focus (toplevel);
4726               if (!current_focus || current_focus == impl->location_entry)
4727                 switch_to_file_list = TRUE;
4728             }
4729
4730           location_switch_to_path_bar (impl);
4731
4732           if (switch_to_file_list)
4733             gtk_widget_grab_focus (impl->browse_files_tree_view);
4734
4735           break;
4736
4737         case LOCATION_MODE_FILENAME_ENTRY:
4738           button_active = TRUE;
4739           location_switch_to_filename_entry (impl);
4740           break;
4741
4742         default:
4743           g_assert_not_reached ();
4744           return;
4745         }
4746
4747       if (set_button)
4748         {
4749           g_signal_handlers_block_by_func (impl->location_button,
4750                                            G_CALLBACK (location_button_toggled_cb), impl);
4751
4752           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4753
4754           g_signal_handlers_unblock_by_func (impl->location_button,
4755                                              G_CALLBACK (location_button_toggled_cb), impl);
4756         }
4757     }
4758
4759   impl->location_mode = new_mode;
4760 }
4761
4762 static void
4763 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4764 {
4765   /* when in search or recent files mode, we are not showing the
4766    * location_entry_box container, so there's no point in switching
4767    * to it.
4768    */
4769   if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4770       impl->operation_mode == OPERATION_MODE_RECENT)
4771     return;
4772
4773   /* If the file entry is not visible, show it.
4774    * If it is visible, turn it off only if it is focused.  Otherwise, switch to the entry.
4775    */
4776   if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4777     {
4778       location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4779     }
4780   else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4781     {
4782       if (gtk_widget_has_focus (impl->location_entry))
4783         {
4784           location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4785         }
4786       else
4787         {
4788           gtk_widget_grab_focus (impl->location_entry);
4789         }
4790     }
4791 }
4792
4793 /* Callback used when one of the location mode buttons is toggled */
4794 static void
4795 location_button_toggled_cb (GtkToggleButton *toggle,
4796                             GtkFileChooserDefault *impl)
4797 {
4798   gboolean is_active;
4799   LocationMode new_mode;
4800
4801   is_active = gtk_toggle_button_get_active (toggle);
4802
4803   if (is_active)
4804     {
4805       g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4806       new_mode = LOCATION_MODE_FILENAME_ENTRY;
4807     }
4808   else
4809     {
4810       g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4811       new_mode = LOCATION_MODE_PATH_BAR;
4812     }
4813
4814   location_mode_set (impl, new_mode, FALSE);
4815 }
4816
4817 /* Creates a toggle button for the location entry. */
4818 static void
4819 location_button_create (GtkFileChooserDefault *impl)
4820 {
4821   GtkWidget *image;
4822   const char *str;
4823
4824   image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4825   gtk_widget_show (image);
4826
4827   impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4828                                         "image", image,
4829                                         NULL);
4830
4831   g_signal_connect (impl->location_button, "toggled",
4832                     G_CALLBACK (location_button_toggled_cb), impl);
4833
4834   str = _("Type a file name");
4835
4836   gtk_widget_set_tooltip_text (impl->location_button, str);
4837   atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4838 }
4839
4840 typedef enum {
4841   PATH_BAR_FOLDER_PATH,
4842   PATH_BAR_SELECT_A_FOLDER,
4843   PATH_BAR_ERROR_NO_FILENAME,
4844   PATH_BAR_ERROR_NO_FOLDER,
4845   PATH_BAR_RECENTLY_USED,
4846   PATH_BAR_SEARCH
4847 } PathBarMode;
4848
4849 /* Creates the info bar for informational messages or warnings, with its icon and label */
4850 static void
4851 info_bar_create (GtkFileChooserDefault *impl)
4852 {
4853   GtkWidget *content_area;
4854
4855   impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4856   impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4857   impl->browse_select_a_folder_label = gtk_label_new (NULL);
4858
4859   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4860
4861   gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4862   gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4863
4864   gtk_widget_show (impl->browse_select_a_folder_icon);
4865   gtk_widget_show (impl->browse_select_a_folder_label);
4866 }
4867
4868 /* Sets the info bar to show the appropriate informational or warning message */
4869 static void
4870 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4871 {
4872   char *str;
4873   gboolean free_str;
4874   GtkMessageType message_type;
4875
4876   free_str = FALSE;
4877
4878   switch (mode)
4879     {
4880     case PATH_BAR_SELECT_A_FOLDER:
4881       str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4882       free_str = TRUE;
4883       message_type = GTK_MESSAGE_OTHER;
4884       break;
4885
4886     case PATH_BAR_ERROR_NO_FILENAME:
4887       str = _("Please type a file name");
4888       message_type = GTK_MESSAGE_WARNING;
4889       break;
4890
4891     case PATH_BAR_ERROR_NO_FOLDER:
4892       str = _("Please select a folder below");
4893       message_type = GTK_MESSAGE_WARNING;
4894       break;
4895
4896     default:
4897       g_assert_not_reached ();
4898       return;
4899     }
4900
4901   gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4902   gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4903                             (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4904                             GTK_ICON_SIZE_MENU);
4905   gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4906
4907   if (free_str)
4908     g_free (str);
4909 }
4910
4911 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4912 static void
4913 special_mode_widgets_create (GtkFileChooserDefault *impl)
4914 {
4915   impl->browse_special_mode_icon = gtk_image_new ();
4916   gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4917   gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4918
4919   impl->browse_special_mode_label = gtk_label_new (NULL);
4920   gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4921   gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4922 }
4923
4924 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4925 static void
4926 path_bar_widgets_create (GtkFileChooserDefault *impl)
4927 {
4928   /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4929   impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4930   gtk_widget_show (impl->browse_path_bar_hbox);
4931
4932   /* Size group that allows the path bar to be the same size between modes */
4933   impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4934   gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4935
4936   /* Location button */
4937   location_button_create (impl);
4938   gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4939   gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4940
4941   /* Path bar */
4942   impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4943   _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4944   g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4945
4946   gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4947   gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4948
4949   /* Info bar */
4950   info_bar_create (impl);
4951   gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4952   gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4953
4954   /* Widgets for special modes (recently-used in Open mode, Search mode) */
4955   special_mode_widgets_create (impl);
4956
4957   /* Create Folder */
4958   impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4959   g_signal_connect (impl->browse_new_folder_button, "clicked",
4960                     G_CALLBACK (new_folder_button_clicked), impl);
4961   gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4962   gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4963 }
4964
4965 /* Sets the path bar's mode to show a label, the actual folder path, or a
4966  * warning message.  You may call this function with PATH_BAR_ERROR_* directly
4967  * if the pathbar is already showing the widgets you expect; otherwise, call
4968  * path_bar_update() instead to set the appropriate widgets automatically.
4969  */
4970 static void
4971 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4972 {
4973   gboolean path_bar_visible             = FALSE;
4974   gboolean special_mode_widgets_visible = FALSE;
4975   gboolean info_bar_visible             = FALSE;
4976   gboolean create_folder_visible        = FALSE;
4977
4978   char *tmp;
4979
4980   switch (mode)
4981     {
4982     case PATH_BAR_FOLDER_PATH:
4983       path_bar_visible = TRUE;
4984       break;
4985
4986     case PATH_BAR_SELECT_A_FOLDER:
4987     case PATH_BAR_ERROR_NO_FILENAME:
4988     case PATH_BAR_ERROR_NO_FOLDER:
4989       info_bar_set (impl, mode);
4990       info_bar_visible = TRUE;
4991       break;
4992
4993     case PATH_BAR_RECENTLY_USED:
4994       gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4995
4996       tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4997       gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4998       g_free (tmp);
4999
5000       special_mode_widgets_visible = TRUE;
5001       break;
5002
5003     case PATH_BAR_SEARCH:
5004       gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
5005
5006       tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
5007       gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
5008       g_free (tmp);
5009
5010       special_mode_widgets_visible = TRUE;
5011       break;
5012
5013     default:
5014       g_assert_not_reached ();
5015     }
5016
5017   gtk_widget_set_visible (impl->browse_path_bar,                        path_bar_visible);
5018   gtk_widget_set_visible (impl->browse_special_mode_icon,               special_mode_widgets_visible);
5019   gtk_widget_set_visible (impl->browse_special_mode_label,              special_mode_widgets_visible);
5020   gtk_widget_set_visible (impl->browse_select_a_folder_info_bar,        info_bar_visible);
5021
5022   if (path_bar_visible)
5023     {
5024       if (impl->create_folders
5025           && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
5026           && impl->operation_mode != OPERATION_MODE_RECENT)
5027         create_folder_visible = TRUE;
5028     }
5029
5030   gtk_widget_set_visible (impl->browse_new_folder_button,               create_folder_visible);
5031 }
5032
5033 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5034 static void
5035 browse_widgets_create (GtkFileChooserDefault *impl)
5036 {
5037   GtkWidget *hpaned;
5038   GtkWidget *widget;
5039   GtkSizeGroup *size_group;
5040
5041   impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5042   gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5043   gtk_widget_show (impl->browse_widgets_box);
5044
5045   impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5046   gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5047   gtk_widget_show (impl->browse_header_box);
5048
5049   /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5050   path_bar_widgets_create (impl);
5051
5052   /* Box for the location label and entry */
5053
5054   impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5055   gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5056
5057   impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5058   gtk_widget_show (impl->location_label);
5059   gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5060
5061   /* size group is used by the scrolled windows of the panes */
5062   size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5063
5064   /* Paned widget */
5065
5066   hpaned = impl->browse_widgets_hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5067   gtk_widget_show (hpaned);
5068   gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5069
5070   widget = shortcuts_pane_create (impl, size_group);
5071   gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5072   widget = file_pane_create (impl, size_group);
5073   gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5074   g_object_unref (size_group);
5075 }
5076
5077 static GObject*
5078 gtk_file_chooser_default_constructor (GType                  type,
5079                                       guint                  n_construct_properties,
5080                                       GObjectConstructParam *construct_params)
5081 {
5082   GtkFileChooserDefault *impl;
5083   GObject *object;
5084
5085   profile_start ("start", NULL);
5086
5087   object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5088                                                                                 n_construct_properties,
5089                                                                                 construct_params);
5090   impl = GTK_FILE_CHOOSER_DEFAULT (object);
5091
5092   g_assert (impl->file_system);
5093
5094   gtk_widget_push_composite_child ();
5095
5096   /* Shortcuts model */
5097   shortcuts_model_create (impl);
5098
5099   /* The browse widgets */
5100   browse_widgets_create (impl);
5101
5102   /* Alignment to hold extra widget */
5103   impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5104   gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5105   gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5106   gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5107
5108   gtk_widget_pop_composite_child ();
5109   update_appearance (impl);
5110
5111   profile_end ("end", NULL);
5112
5113   return object;
5114 }
5115
5116 /* Sets the extra_widget by packing it in the appropriate place */
5117 static void
5118 set_extra_widget (GtkFileChooserDefault *impl,
5119                   GtkWidget             *extra_widget)
5120 {
5121   if (extra_widget)
5122     {
5123       g_object_ref (extra_widget);
5124       /* FIXME: is this right ? */
5125       gtk_widget_show (extra_widget);
5126     }
5127
5128   if (impl->extra_widget)
5129     {
5130       gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5131       g_object_unref (impl->extra_widget);
5132     }
5133
5134   impl->extra_widget = extra_widget;
5135   if (impl->extra_widget)
5136     {
5137       gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5138       gtk_widget_show (impl->extra_align);
5139     }
5140   else
5141     gtk_widget_hide (impl->extra_align);
5142 }
5143
5144 static void
5145 set_local_only (GtkFileChooserDefault *impl,
5146                 gboolean               local_only)
5147 {
5148   if (local_only != impl->local_only)
5149     {
5150       impl->local_only = local_only;
5151
5152       if (impl->location_entry)
5153         _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5154
5155       if (impl->shortcuts_model && impl->file_system)
5156         {
5157           shortcuts_add_volumes (impl);
5158           shortcuts_add_bookmarks (impl);
5159         }
5160
5161       if (local_only && impl->current_folder &&
5162            !g_file_is_native (impl->current_folder))
5163         {
5164           /* If we are pointing to a non-local folder, make an effort to change
5165            * back to a local folder, but it's really up to the app to not cause
5166            * such a situation, so we ignore errors.
5167            */
5168           const gchar *home = g_get_home_dir ();
5169           GFile *home_file;
5170
5171           if (home == NULL)
5172             return;
5173
5174           home_file = g_file_new_for_path (home);
5175
5176           gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5177
5178           g_object_unref (home_file);
5179         }
5180     }
5181 }
5182
5183 static void
5184 volumes_bookmarks_changed_cb (GtkFileSystem         *file_system,
5185                               GtkFileChooserDefault *impl)
5186 {
5187   shortcuts_add_volumes (impl);
5188   shortcuts_add_bookmarks (impl);
5189
5190   bookmarks_check_add_sensitivity (impl);
5191   bookmarks_check_remove_sensitivity (impl);
5192   shortcuts_check_popup_sensitivity (impl);
5193 }
5194
5195 /* Sets the file chooser to multiple selection mode */
5196 static void
5197 set_select_multiple (GtkFileChooserDefault *impl,
5198                      gboolean               select_multiple,
5199                      gboolean               property_notify)
5200 {
5201   GtkTreeSelection *selection;
5202   GtkSelectionMode mode;
5203
5204   if (select_multiple == impl->select_multiple)
5205     return;
5206
5207   mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
5208
5209   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5210   gtk_tree_selection_set_mode (selection, mode);
5211
5212   gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5213
5214   impl->select_multiple = select_multiple;
5215   g_object_notify (G_OBJECT (impl), "select-multiple");
5216
5217   check_preview_change (impl);
5218 }
5219
5220 static void
5221 set_file_system_backend (GtkFileChooserDefault *impl)
5222 {
5223   profile_start ("start for backend", "default");
5224
5225   impl->file_system = _gtk_file_system_new ();
5226
5227   g_signal_connect (impl->file_system, "volumes-changed",
5228                     G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5229   g_signal_connect (impl->file_system, "bookmarks-changed",
5230                     G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5231
5232   profile_end ("end", NULL);
5233 }
5234
5235 static void
5236 unset_file_system_backend (GtkFileChooserDefault *impl)
5237 {
5238   g_signal_handlers_disconnect_by_func (impl->file_system,
5239                                         G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5240
5241   g_object_unref (impl->file_system);
5242
5243   impl->file_system = NULL;
5244 }
5245
5246 /* Saves the widgets around the pathbar so they can be reparented later
5247  * in the correct place.  This function must be called paired with
5248  * restore_path_bar().
5249  */
5250 static void
5251 save_path_bar (GtkFileChooserDefault *impl)
5252 {
5253   GtkWidget *parent;
5254
5255   g_object_ref (impl->browse_path_bar_hbox);
5256
5257   parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5258   if (parent)
5259     gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5260 }
5261
5262 /* Reparents the path bar and the "Create folder" button to the right place:
5263  * Above the file list in Open mode, or to the right of the "Save in folder:"
5264  * label in Save mode.  The save_path_bar() function must be called before this
5265  * one.
5266  */
5267 static void
5268 restore_path_bar (GtkFileChooserDefault *impl)
5269 {
5270   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5271       || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5272     {
5273       gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5274       gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5275     }
5276   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5277            || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5278     {
5279       gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5280       gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5281       gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5282     }
5283   else
5284     g_assert_not_reached ();
5285
5286   g_object_unref (impl->browse_path_bar_hbox);
5287 }
5288
5289 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5290 static void
5291 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5292 {
5293   GFile *file;
5294
5295   gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5296                       MODEL_COL_FILE, &file,
5297                       -1);
5298   _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE);
5299   g_object_unref (file);
5300 }
5301
5302 /* Sets the pathbar in the appropriate mode according to the current operation mode and action.  This is the central function for
5303  * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5304  * function will update all the pathbar's widgets.
5305  */
5306 static void
5307 path_bar_update (GtkFileChooserDefault *impl)
5308 {
5309   PathBarMode mode;
5310
5311   switch (impl->operation_mode)
5312     {
5313     case OPERATION_MODE_BROWSE:
5314       mode = PATH_BAR_FOLDER_PATH;
5315       break;
5316
5317     case OPERATION_MODE_RECENT:
5318       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5319         {
5320           GtkTreeSelection *selection;
5321           gboolean have_selected;
5322           GtkTreeIter iter;
5323
5324           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5325
5326           /* Save mode means single-selection mode, so the following is valid */
5327           have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5328
5329           if (have_selected)
5330             {
5331               mode = PATH_BAR_FOLDER_PATH;
5332               put_recent_folder_in_pathbar (impl, &iter);
5333             }
5334           else
5335             mode = PATH_BAR_SELECT_A_FOLDER;
5336         }
5337       else
5338         mode = PATH_BAR_RECENTLY_USED;
5339
5340       break;
5341
5342     case OPERATION_MODE_SEARCH:
5343       mode = PATH_BAR_SEARCH;
5344       break;
5345
5346     default:
5347       g_assert_not_reached ();
5348       return;
5349     }
5350
5351   path_bar_set_mode (impl, mode);
5352 }
5353
5354 static void
5355 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5356 {
5357   if (impl->search_hbox)
5358     {
5359       gtk_widget_destroy (impl->search_hbox);
5360
5361       impl->search_hbox = NULL;
5362       impl->search_entry = NULL;
5363     }
5364 }
5365
5366 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5367 static void
5368 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5369 {
5370   switch (mode)
5371     {
5372     case OPERATION_MODE_BROWSE:
5373       stop_loading_and_clear_list_model (impl, TRUE);
5374       break;
5375
5376     case OPERATION_MODE_SEARCH:
5377       search_stop_searching (impl, FALSE);
5378       search_clear_model (impl, TRUE);
5379
5380       operation_mode_discard_search_widgets (impl);
5381       break;
5382
5383     case OPERATION_MODE_RECENT:
5384       recent_stop_loading (impl);
5385       recent_clear_model (impl, TRUE);
5386       break;
5387
5388     default:
5389       g_assert_not_reached ();
5390     }
5391 }
5392
5393 static void
5394 operation_mode_set_browse (GtkFileChooserDefault *impl)
5395 {
5396   path_bar_update (impl);
5397
5398   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5399       impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5400     {
5401       gtk_widget_show (impl->location_button);
5402       location_mode_set (impl, impl->location_mode, TRUE);
5403
5404       if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5405         gtk_widget_show (impl->location_entry_box);
5406     }
5407 }
5408
5409 static void
5410 operation_mode_set_search (GtkFileChooserDefault *impl)
5411 {
5412   g_assert (impl->search_hbox == NULL);
5413   g_assert (impl->search_entry == NULL);
5414   g_assert (impl->search_model == NULL);
5415
5416   search_setup_widgets (impl);
5417 }
5418
5419 static void
5420 operation_mode_set_recent (GtkFileChooserDefault *impl)
5421 {
5422   path_bar_update (impl);
5423
5424   /* Hide the location widgets temporarily */
5425   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5426       impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5427     {
5428       gtk_widget_hide (impl->location_button);
5429       gtk_widget_hide (impl->location_entry_box);
5430     }
5431
5432   recent_start_loading (impl);
5433 }
5434
5435 /* Sometimes we need to frob the selection in the shortcuts list manually */
5436 static void
5437 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5438 {
5439   GtkTreeSelection *selection;
5440   GtkTreePath *path;
5441
5442   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5443
5444   g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5445
5446   path = gtk_tree_path_new_from_indices (pos, -1);
5447   gtk_tree_selection_select_path (selection, path);
5448   gtk_tree_path_free (path);
5449
5450   g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5451 }
5452
5453 static void
5454 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5455 {
5456   ShortcutsIndex shortcut_to_select;
5457
5458   operation_mode_stop (impl, impl->operation_mode);
5459
5460   impl->operation_mode = mode;
5461
5462   switch (impl->operation_mode)
5463     {
5464     case OPERATION_MODE_BROWSE:
5465       operation_mode_set_browse (impl);
5466       shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5467       break;
5468
5469     case OPERATION_MODE_SEARCH:
5470       operation_mode_set_search (impl);
5471       shortcut_to_select = SHORTCUTS_SEARCH;
5472       break;
5473
5474     case OPERATION_MODE_RECENT:
5475       operation_mode_set_recent (impl);
5476       shortcut_to_select = SHORTCUTS_RECENT;
5477       break;
5478
5479     default:
5480       g_assert_not_reached ();
5481       return;
5482     }
5483
5484   if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5485     shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5486 }
5487
5488 /* This function is basically a do_all function.
5489  *
5490  * It sets the visibility on all the widgets based on the current state, and
5491  * moves the custom_widget if needed.
5492  */
5493 static void
5494 update_appearance (GtkFileChooserDefault *impl)
5495 {
5496   save_path_bar (impl);
5497
5498   if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5499       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5500     {
5501       const char *text;
5502
5503       gtk_widget_hide (impl->location_button);
5504       save_widgets_create (impl);
5505
5506       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5507         text = _("Save in _folder:");
5508       else
5509         text = _("Create in _folder:");
5510
5511       gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5512
5513       if (impl->select_multiple)
5514         {
5515           g_warning ("Save mode cannot be set in conjunction with multiple selection mode.  "
5516                      "Re-setting to single selection mode.");
5517           set_select_multiple (impl, FALSE, TRUE);
5518         }
5519     }
5520   else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5521            impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5522     {
5523       gtk_widget_show (impl->location_button);
5524       save_widgets_destroy (impl);
5525       location_mode_set (impl, impl->location_mode, TRUE);
5526     }
5527
5528   if (impl->location_entry)
5529     _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5530
5531   restore_path_bar (impl);
5532   path_bar_update (impl);
5533
5534   /* This *is* needed; we need to redraw the file list because the "sensitivity"
5535    * of files may change depending whether we are in a file or folder-only mode.
5536    */
5537   gtk_widget_queue_draw (impl->browse_files_tree_view);
5538
5539   emit_default_size_changed (impl);
5540 }
5541
5542 static void
5543 gtk_file_chooser_default_set_property (GObject      *object,
5544                                        guint         prop_id,
5545                                        const GValue *value,
5546                                        GParamSpec   *pspec)
5547
5548 {
5549   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5550
5551   switch (prop_id)
5552     {
5553     case GTK_FILE_CHOOSER_PROP_ACTION:
5554       {
5555         GtkFileChooserAction action = g_value_get_enum (value);
5556
5557         if (action != impl->action)
5558           {
5559             gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5560             
5561             if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5562                  action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5563                 && impl->select_multiple)
5564               {
5565                 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5566                            "this is not allowed in multiple selection mode.  Resetting the file chooser "
5567                            "to single selection mode.");
5568                 set_select_multiple (impl, FALSE, TRUE);
5569               }
5570             impl->action = action;
5571             update_cell_renderer_attributes (impl);
5572             update_appearance (impl);
5573             settings_load (impl);
5574           }
5575       }
5576       break;
5577
5578     case GTK_FILE_CHOOSER_PROP_FILTER:
5579       set_current_filter (impl, g_value_get_object (value));
5580       break;
5581
5582     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5583       set_local_only (impl, g_value_get_boolean (value));
5584       break;
5585
5586     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5587       set_preview_widget (impl, g_value_get_object (value));
5588       break;
5589
5590     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5591       impl->preview_widget_active = g_value_get_boolean (value);
5592       update_preview_widget_visibility (impl);
5593       break;
5594
5595     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5596       impl->use_preview_label = g_value_get_boolean (value);
5597       update_preview_widget_visibility (impl);
5598       break;
5599
5600     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5601       set_extra_widget (impl, g_value_get_object (value));
5602       break;
5603
5604     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5605       {
5606         gboolean select_multiple = g_value_get_boolean (value);
5607         if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5608              impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5609             && select_multiple)
5610           {
5611             g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5612                        "not allowed in SAVE or CREATE_FOLDER modes.  Ignoring the change and "
5613                        "leaving the file chooser in single selection mode.");
5614             return;
5615           }
5616
5617         set_select_multiple (impl, select_multiple, FALSE);
5618       }
5619       break;
5620
5621     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5622       {
5623         gboolean show_hidden = g_value_get_boolean (value);
5624         if (show_hidden != impl->show_hidden)
5625           {
5626             impl->show_hidden = show_hidden;
5627
5628             if (impl->browse_files_model)
5629               _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5630           }
5631       }
5632       break;
5633
5634     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5635       {
5636         gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5637         impl->do_overwrite_confirmation = do_overwrite_confirmation;
5638       }
5639       break;
5640
5641     case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5642       {
5643         gboolean create_folders = g_value_get_boolean (value);
5644         impl->create_folders = create_folders;
5645         update_appearance (impl);
5646       }
5647       break;
5648
5649     default:
5650       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5651       break;
5652     }
5653 }
5654
5655 static void
5656 gtk_file_chooser_default_get_property (GObject    *object,
5657                                        guint       prop_id,
5658                                        GValue     *value,
5659                                        GParamSpec *pspec)
5660 {
5661   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5662
5663   switch (prop_id)
5664     {
5665     case GTK_FILE_CHOOSER_PROP_ACTION:
5666       g_value_set_enum (value, impl->action);
5667       break;
5668
5669     case GTK_FILE_CHOOSER_PROP_FILTER:
5670       g_value_set_object (value, impl->current_filter);
5671       break;
5672
5673     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5674       g_value_set_boolean (value, impl->local_only);
5675       break;
5676
5677     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5678       g_value_set_object (value, impl->preview_widget);
5679       break;
5680
5681     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5682       g_value_set_boolean (value, impl->preview_widget_active);
5683       break;
5684
5685     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5686       g_value_set_boolean (value, impl->use_preview_label);
5687       break;
5688
5689     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5690       g_value_set_object (value, impl->extra_widget);
5691       break;
5692
5693     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5694       g_value_set_boolean (value, impl->select_multiple);
5695       break;
5696
5697     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5698       g_value_set_boolean (value, impl->show_hidden);
5699       break;
5700
5701     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5702       g_value_set_boolean (value, impl->do_overwrite_confirmation);
5703       break;
5704
5705     case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5706       g_value_set_boolean (value, impl->create_folders);
5707       break;
5708
5709     default:
5710       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5711       break;
5712     }
5713 }
5714
5715 /* This cancels everything that may be going on in the background. */
5716 static void
5717 cancel_all_operations (GtkFileChooserDefault *impl)
5718 {
5719   GSList *l;
5720
5721   pending_select_files_free (impl);
5722
5723   if (impl->reload_icon_cancellables)
5724     {
5725       for (l = impl->reload_icon_cancellables; l; l = l->next)
5726         {
5727           GCancellable *cancellable = G_CANCELLABLE (l->data);
5728           g_cancellable_cancel (cancellable);
5729         }
5730       g_slist_free (impl->reload_icon_cancellables);
5731       impl->reload_icon_cancellables = NULL;
5732     }
5733
5734   if (impl->loading_shortcuts)
5735     {
5736       for (l = impl->loading_shortcuts; l; l = l->next)
5737         {
5738           GCancellable *cancellable = G_CANCELLABLE (l->data);
5739           g_cancellable_cancel (cancellable);
5740         }
5741       g_slist_free (impl->loading_shortcuts);
5742       impl->loading_shortcuts = NULL;
5743     }
5744
5745   if (impl->file_list_drag_data_received_cancellable)
5746     {
5747       g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5748       impl->file_list_drag_data_received_cancellable = NULL;
5749     }
5750
5751   if (impl->update_current_folder_cancellable)
5752     {
5753       g_cancellable_cancel (impl->update_current_folder_cancellable);
5754       impl->update_current_folder_cancellable = NULL;
5755     }
5756
5757   if (impl->should_respond_get_info_cancellable)
5758     {
5759       g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5760       impl->should_respond_get_info_cancellable = NULL;
5761     }
5762
5763   if (impl->file_exists_get_info_cancellable)
5764     {
5765       g_cancellable_cancel (impl->file_exists_get_info_cancellable);
5766       impl->file_exists_get_info_cancellable = NULL;
5767     }
5768
5769   if (impl->update_from_entry_cancellable)
5770     {
5771       g_cancellable_cancel (impl->update_from_entry_cancellable);
5772       impl->update_from_entry_cancellable = NULL;
5773     }
5774
5775   if (impl->shortcuts_activate_iter_cancellable)
5776     {
5777       g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5778       impl->shortcuts_activate_iter_cancellable = NULL;
5779     }
5780
5781   search_stop_searching (impl, TRUE);
5782   recent_stop_loading (impl);
5783 }
5784
5785 /* Removes the settings signal handler.  It's safe to call multiple times */
5786 static void
5787 remove_settings_signal (GtkFileChooserDefault *impl,
5788                         GdkScreen             *screen)
5789 {
5790   if (impl->settings_signal_id)
5791     {
5792       GtkSettings *settings;
5793
5794       settings = gtk_settings_get_for_screen (screen);
5795       g_signal_handler_disconnect (settings,
5796                                    impl->settings_signal_id);
5797       impl->settings_signal_id = 0;
5798     }
5799 }
5800
5801 static void
5802 gtk_file_chooser_default_dispose (GObject *object)
5803 {
5804   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5805
5806   cancel_all_operations (impl);
5807
5808   if (impl->extra_widget)
5809     {
5810       g_object_unref (impl->extra_widget);
5811       impl->extra_widget = NULL;
5812     }
5813
5814   remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5815
5816   G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5817 }
5818
5819 /* We override show-all since we have internal widgets that
5820  * shouldn't be shown when you call show_all(), like the filter
5821  * combo box.
5822  */
5823 static void
5824 gtk_file_chooser_default_show_all (GtkWidget *widget)
5825 {
5826   GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5827
5828   gtk_widget_show (widget);
5829
5830   if (impl->extra_widget)
5831     gtk_widget_show_all (impl->extra_widget);
5832 }
5833
5834 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5835  * widget on our toplevel.  See gtk_file_chooser_default_hierarchy_changed()
5836  */
5837 static void
5838 toplevel_set_focus_cb (GtkWindow             *window,
5839                        GtkWidget             *focus,
5840                        GtkFileChooserDefault *impl)
5841 {
5842   impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5843 }
5844
5845 /* We monitor the focus widget on our toplevel to be able to know which widget
5846  * was last focused at the time our "should_respond" method gets called.
5847  */
5848 static void
5849 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5850                                             GtkWidget *previous_toplevel)
5851 {
5852   GtkFileChooserDefault *impl;
5853   GtkWidget *toplevel;
5854
5855   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5856   toplevel = gtk_widget_get_toplevel (widget);
5857
5858   if (previous_toplevel && 
5859       impl->toplevel_set_focus_id != 0)
5860     {
5861       g_signal_handler_disconnect (previous_toplevel,
5862                                    impl->toplevel_set_focus_id);
5863       impl->toplevel_set_focus_id = 0;
5864       impl->toplevel_last_focus_widget = NULL;
5865     }
5866
5867   if (gtk_widget_is_toplevel (toplevel))
5868     {
5869       g_assert (impl->toplevel_set_focus_id == 0);
5870       impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5871                                                       G_CALLBACK (toplevel_set_focus_cb), impl);
5872       impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5873     }
5874 }
5875
5876 /* Changes the icons wherever it is needed */
5877 static void
5878 change_icon_theme (GtkFileChooserDefault *impl)
5879 {
5880   GtkSettings *settings;
5881   gint width, height;
5882   GtkCellRenderer *renderer;
5883   GList *cells;
5884
5885   profile_start ("start", NULL);
5886
5887   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5888
5889   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5890     impl->icon_size = MAX (width, height);
5891   else
5892     impl->icon_size = FALLBACK_ICON_SIZE;
5893
5894   shortcuts_reload_icons (impl);
5895   /* the first cell in the first column is the icon column, and we have a fixed size there */
5896   cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5897         gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5898   renderer = GTK_CELL_RENDERER (cells->data);
5899   set_icon_cell_renderer_fixed_size (impl, renderer);
5900   g_list_free (cells);
5901   if (impl->browse_files_model)
5902     _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5903   gtk_widget_queue_resize (impl->browse_files_tree_view);
5904
5905   profile_end ("end", NULL);
5906 }
5907
5908 /* Callback used when a GtkSettings value changes */
5909 static void
5910 settings_notify_cb (GObject               *object,
5911                     GParamSpec            *pspec,
5912                     GtkFileChooserDefault *impl)
5913 {
5914   const char *name;
5915
5916   profile_start ("start", NULL);
5917
5918   name = g_param_spec_get_name (pspec);
5919
5920   if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5921       strcmp (name, "gtk-icon-sizes") == 0)
5922     change_icon_theme (impl);
5923
5924   profile_end ("end", NULL);
5925 }
5926
5927 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5928  * the icon theme.
5929  */
5930 static void
5931 check_icon_theme (GtkFileChooserDefault *impl)
5932 {
5933   GtkSettings *settings;
5934
5935   profile_start ("start", NULL);
5936
5937   if (impl->settings_signal_id)
5938     {
5939       profile_end ("end", NULL);
5940       return;
5941     }
5942
5943   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5944     {
5945       settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5946       impl->settings_signal_id = g_signal_connect (settings, "notify",
5947                                                    G_CALLBACK (settings_notify_cb), impl);
5948
5949       change_icon_theme (impl);
5950     }
5951
5952   profile_end ("end", NULL);
5953 }
5954
5955 static void
5956 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5957 {
5958   GtkFileChooserDefault *impl;
5959
5960   profile_start ("start", NULL);
5961
5962   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5963
5964   profile_msg ("    parent class style_udpated start", NULL);
5965   GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5966   profile_msg ("    parent class style_updated end", NULL);
5967
5968   if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5969     change_icon_theme (impl);
5970
5971   emit_default_size_changed (impl);
5972
5973   profile_end ("end", NULL);
5974 }
5975
5976 static void
5977 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5978                                          GdkScreen *previous_screen)
5979 {
5980   GtkFileChooserDefault *impl;
5981
5982   profile_start ("start", NULL);
5983
5984   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5985
5986   if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5987     GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5988
5989   remove_settings_signal (impl, previous_screen);
5990   check_icon_theme (impl);
5991
5992   emit_default_size_changed (impl);
5993
5994   profile_end ("end", NULL);
5995 }
5996
5997 static void
5998 set_sort_column (GtkFileChooserDefault *impl)
5999 {
6000   GtkTreeSortable *sortable;
6001
6002   sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6003
6004   /* can happen when we're still populating the model */
6005   if (sortable == NULL)
6006     return;
6007
6008   gtk_tree_sortable_set_sort_column_id (sortable,
6009                                         impl->sort_column,
6010                                         impl->sort_order);
6011 }
6012
6013 static void
6014 settings_load (GtkFileChooserDefault *impl)
6015 {
6016   LocationMode location_mode;
6017   gboolean show_hidden;
6018   gboolean show_size_column;
6019   gint sort_column;
6020   GtkSortType sort_order;
6021   gint sidebar_width;
6022   GSettings *settings;
6023
6024   settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
6025
6026   location_mode = g_settings_get_enum (settings, SETTINGS_KEY_LOCATION_MODE);
6027   show_hidden = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN);
6028   show_size_column = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6029   sort_column = g_settings_get_enum (settings, SETTINGS_KEY_SORT_COLUMN);
6030   sort_order = g_settings_get_enum (settings, SETTINGS_KEY_SORT_ORDER);
6031   sidebar_width = g_settings_get_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH);
6032
6033   location_mode_set (impl, location_mode, TRUE);
6034
6035   gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6036
6037   impl->show_size_column = show_size_column;
6038   gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6039
6040   impl->sort_column = sort_column;
6041   impl->sort_order = sort_order;
6042   /* We don't call set_sort_column() here as the models may not have been
6043    * created yet.  The individual functions that create and set the models will
6044    * call set_sort_column() themselves.
6045    */
6046
6047   gtk_paned_set_position (GTK_PANED (impl->browse_widgets_hpaned), sidebar_width);
6048 }
6049
6050 static void
6051 settings_save (GtkFileChooserDefault *impl)
6052 {
6053   GSettings *settings;
6054
6055   settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
6056
6057   /* All the other state */
6058
6059   g_settings_set_enum (settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6060   g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN,
6061                           gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6062   g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6063   g_settings_set_enum (settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6064   g_settings_set_enum (settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6065   g_settings_set_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH,
6066                      gtk_paned_get_position (GTK_PANED (impl->browse_widgets_hpaned)));
6067
6068   /* Now apply the settings */
6069   g_settings_apply (settings);
6070 }
6071
6072 /* GtkWidget::realize method */
6073 static void
6074 gtk_file_chooser_default_realize (GtkWidget *widget)
6075 {
6076   GtkFileChooserDefault *impl;
6077
6078   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6079
6080   GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6081
6082   emit_default_size_changed (impl);
6083 }
6084
6085 /* GtkWidget::map method */
6086 static void
6087 gtk_file_chooser_default_map (GtkWidget *widget)
6088 {
6089   GtkFileChooserDefault *impl;
6090
6091   profile_start ("start", NULL);
6092
6093   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6094
6095   GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6096
6097   if (impl->operation_mode == OPERATION_MODE_BROWSE)
6098     {
6099       switch (impl->reload_state)
6100         {
6101         case RELOAD_EMPTY:
6102           recent_shortcut_handler (impl);
6103           break;
6104         
6105         case RELOAD_HAS_FOLDER:
6106           /* Nothing; we are already loading or loaded, so we
6107            * don't need to reload
6108            */
6109           break;
6110
6111         default:
6112           g_assert_not_reached ();
6113       }
6114     }
6115
6116   volumes_bookmarks_changed_cb (impl->file_system, impl);
6117
6118   settings_load (impl);
6119
6120   profile_end ("end", NULL);
6121 }
6122
6123 /* GtkWidget::unmap method */
6124 static void
6125 gtk_file_chooser_default_unmap (GtkWidget *widget)
6126 {
6127   GtkFileChooserDefault *impl;
6128
6129   impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6130
6131   settings_save (impl);
6132
6133   cancel_all_operations (impl);
6134   impl->reload_state = RELOAD_EMPTY;
6135
6136   GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6137 }
6138
6139 #define COMPARE_DIRECTORIES                                                                                    \
6140   GtkFileChooserDefault *impl = user_data;                                                                     \
6141   GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model);                                                \
6142   gboolean dir_a, dir_b;                                                                                       \
6143                                                                                                                \
6144   dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER));           \
6145   dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER));           \
6146                                                                                                                \
6147   if (dir_a != dir_b)                                                                                          \
6148     return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6149
6150 /* Sort callback for the filename column */
6151 static gint
6152 name_sort_func (GtkTreeModel *model,
6153                 GtkTreeIter  *a,
6154                 GtkTreeIter  *b,
6155                 gpointer      user_data)
6156 {
6157   COMPARE_DIRECTORIES;
6158   else
6159     {
6160       const char *key_a, *key_b;
6161       gint result;
6162
6163       key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6164       key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6165
6166       if (key_a && key_b)
6167         result = strcmp (key_a, key_b);
6168       else if (key_a)
6169         result = 1;
6170       else if (key_b)
6171         result = -1;
6172       else
6173         result = 0;
6174
6175       return result;
6176     }
6177 }
6178
6179 /* Sort callback for the size column */
6180 static gint
6181 size_sort_func (GtkTreeModel *model,
6182                 GtkTreeIter  *a,
6183                 GtkTreeIter  *b,
6184                 gpointer      user_data)
6185 {
6186   COMPARE_DIRECTORIES;
6187   else
6188     {
6189       gint64 size_a, size_b;
6190
6191       size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6192       size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6193
6194       return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6195     }
6196 }
6197
6198 /* Sort callback for the mtime column */
6199 static gint
6200 mtime_sort_func (GtkTreeModel *model,
6201                  GtkTreeIter  *a,
6202                  GtkTreeIter  *b,
6203                  gpointer      user_data)
6204 {
6205   COMPARE_DIRECTORIES;
6206   else
6207     {
6208       glong ta, tb;
6209
6210       ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6211       tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6212
6213       return ta < tb ? -1 : (ta == tb ? 0 : 1);
6214     }
6215 }
6216
6217 /* Callback used when the sort column changes.  We cache the sort order for use
6218  * in name_sort_func().
6219  */
6220 static void
6221 list_sort_column_changed_cb (GtkTreeSortable       *sortable,
6222                              GtkFileChooserDefault *impl)
6223 {
6224   gint sort_column_id;
6225   GtkSortType sort_type;
6226
6227   if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6228     {
6229       impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6230       impl->sort_column = sort_column_id;
6231       impl->sort_order = sort_type;
6232     }
6233 }
6234
6235 static void
6236 set_busy_cursor (GtkFileChooserDefault *impl,
6237                  gboolean               busy)
6238 {
6239   GtkWidget *widget;
6240   GtkWindow *toplevel;
6241   GdkDisplay *display;
6242   GdkCursor *cursor;
6243
6244   toplevel = get_toplevel (GTK_WIDGET (impl));
6245   widget = GTK_WIDGET (toplevel);
6246   if (!toplevel || !gtk_widget_get_realized (widget))
6247     return;
6248
6249   display = gtk_widget_get_display (widget);
6250
6251   if (busy)
6252     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6253   else
6254     cursor = NULL;
6255
6256   gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6257   gdk_display_flush (display);
6258
6259   if (cursor)
6260     g_object_unref (cursor);
6261 }
6262
6263 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6264 static void
6265 load_set_model (GtkFileChooserDefault *impl)
6266 {
6267   profile_start ("start", NULL);
6268
6269   g_assert (impl->browse_files_model != NULL);
6270
6271   profile_msg ("    gtk_tree_view_set_model start", NULL);
6272   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6273                            GTK_TREE_MODEL (impl->browse_files_model));
6274   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6275   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6276                                    MODEL_COL_NAME);
6277   file_list_set_sort_column_ids (impl);
6278   set_sort_column (impl);
6279   profile_msg ("    gtk_tree_view_set_model end", NULL);
6280   impl->list_sort_ascending = TRUE;
6281
6282   profile_end ("end", NULL);
6283 }
6284
6285 /* Timeout callback used when the loading timer expires */
6286 static gboolean
6287 load_timeout_cb (gpointer data)
6288 {
6289   GtkFileChooserDefault *impl;
6290
6291   profile_start ("start", NULL);
6292
6293   impl = GTK_FILE_CHOOSER_DEFAULT (data);
6294   g_assert (impl->load_state == LOAD_PRELOAD);
6295   g_assert (impl->load_timeout_id != 0);
6296   g_assert (impl->browse_files_model != NULL);
6297
6298   impl->load_timeout_id = 0;
6299   impl->load_state = LOAD_LOADING;
6300
6301   load_set_model (impl);
6302
6303   profile_end ("end", NULL);
6304
6305   return FALSE;
6306 }
6307
6308 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6309 static void
6310 load_setup_timer (GtkFileChooserDefault *impl)
6311 {
6312   g_assert (impl->load_timeout_id == 0);
6313   g_assert (impl->load_state != LOAD_PRELOAD);
6314
6315   impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6316   impl->load_state = LOAD_PRELOAD;
6317 }
6318
6319 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6320 static void
6321 load_remove_timer (GtkFileChooserDefault *impl)
6322 {
6323   if (impl->load_timeout_id != 0)
6324     {
6325       g_assert (impl->load_state == LOAD_PRELOAD);
6326
6327       g_source_remove (impl->load_timeout_id);
6328       impl->load_timeout_id = 0;
6329       impl->load_state = LOAD_EMPTY;
6330     }
6331   else
6332     g_assert (impl->load_state == LOAD_EMPTY ||
6333               impl->load_state == LOAD_LOADING ||
6334               impl->load_state == LOAD_FINISHED);
6335 }
6336
6337 /* Selects the first row in the file list */
6338 static void
6339 browse_files_select_first_row (GtkFileChooserDefault *impl)
6340 {
6341   GtkTreePath *path;
6342   GtkTreeIter dummy_iter;
6343   GtkTreeModel *tree_model;
6344
6345   tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6346
6347   if (!tree_model)
6348     return;
6349
6350   path = gtk_tree_path_new_from_indices (0, -1);
6351
6352   /* If the list is empty, do nothing. */
6353   if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6354       gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6355
6356   gtk_tree_path_free (path);
6357 }
6358
6359 struct center_selected_row_closure {
6360   GtkFileChooserDefault *impl;
6361   gboolean already_centered;
6362 };
6363
6364 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6365  * selected row in the tree view.
6366  */
6367 static void
6368 center_selected_row_foreach_cb (GtkTreeModel      *model,
6369                                 GtkTreePath       *path,
6370                                 GtkTreeIter       *iter,
6371                                 gpointer           data)
6372 {
6373   struct center_selected_row_closure *closure;
6374
6375   closure = data;
6376   if (closure->already_centered)
6377     return;
6378
6379   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6380   closure->already_centered = TRUE;
6381 }
6382
6383 /* Centers the selected row in the tree view */
6384 static void
6385 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6386 {
6387   struct center_selected_row_closure closure;
6388   GtkTreeSelection *selection;
6389
6390   closure.impl = impl;
6391   closure.already_centered = FALSE;
6392
6393   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6394   gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6395 }
6396
6397 static gboolean
6398 show_and_select_files (GtkFileChooserDefault *impl,
6399                        GSList                *files)
6400 {
6401   GtkTreeSelection *selection;
6402   GtkFileSystemModel *fsmodel;
6403   gboolean enabled_hidden, removed_filters;
6404   gboolean selected_a_file;
6405   GSList *walk;
6406
6407   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6408   fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6409
6410   enabled_hidden = impl->show_hidden;
6411   removed_filters = (impl->current_filter == NULL);
6412
6413   selected_a_file = FALSE;
6414
6415   for (walk = files; walk; walk = walk->next)
6416     {
6417       GFile *file = walk->data;
6418       GtkTreeIter iter;
6419
6420       /* Is it a hidden file? */
6421
6422       if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6423         continue;
6424
6425       if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6426         {
6427           GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6428
6429           if (!enabled_hidden &&
6430               (g_file_info_get_is_hidden (info) ||
6431                g_file_info_get_is_backup (info)))
6432             {
6433               g_object_set (impl, "show-hidden", TRUE, NULL);
6434               enabled_hidden = TRUE;
6435             }
6436         }
6437
6438       /* Is it a filtered file? */
6439
6440       if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6441         continue; /* re-get the iter as it may change when the model refilters */
6442
6443       if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6444         {
6445           /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6446           if (!removed_filters)
6447             {
6448               set_current_filter (impl, NULL);
6449               removed_filters = TRUE;
6450             }
6451         }
6452
6453       /* Okay, can we select the file now? */
6454           
6455       if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6456         continue;
6457
6458       if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6459         {
6460           GtkTreePath *path;
6461
6462           gtk_tree_selection_select_iter (selection, &iter);
6463
6464           path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6465           gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6466                                     path, NULL, FALSE);
6467           gtk_tree_path_free (path);
6468
6469           selected_a_file = TRUE;
6470         }
6471     }
6472
6473   browse_files_center_selected_row (impl);
6474
6475   return selected_a_file;
6476 }
6477
6478 /* Processes the pending operation when a folder is finished loading */
6479 static void
6480 pending_select_files_process (GtkFileChooserDefault *impl)
6481 {
6482   g_assert (impl->load_state == LOAD_FINISHED);
6483   g_assert (impl->browse_files_model != NULL);
6484
6485   if (impl->pending_select_files)
6486     {
6487       show_and_select_files (impl, impl->pending_select_files);
6488       pending_select_files_free (impl);
6489       browse_files_center_selected_row (impl);
6490     }
6491   else
6492     {
6493       /* We only select the first row if the chooser is actually mapped ---
6494        * selecting the first row is to help the user when he is interacting with
6495        * the chooser, but sometimes a chooser works not on behalf of the user,
6496        * but rather on behalf of something else like GtkFileChooserButton.  In
6497        * that case, the chooser's selection should be what the caller expects,
6498        * as the user can't see that something else got selected.  See bug #165264.
6499        */
6500       if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6501           gtk_widget_get_mapped (GTK_WIDGET (impl)))
6502         browse_files_select_first_row (impl);
6503     }
6504
6505   g_assert (impl->pending_select_files == NULL);
6506 }
6507
6508 static void
6509 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6510 {
6511   GFileInfo *info;
6512   char *msg;
6513
6514   info = g_file_query_info (impl->current_folder,
6515                             G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6516                             G_FILE_QUERY_INFO_NONE,
6517                             NULL,
6518                             NULL);
6519   if (info)
6520     {
6521       msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6522       g_object_unref (info);
6523     }
6524   else
6525     msg = g_strdup (_("Could not read the contents of the folder"));
6526
6527   error_message (impl, msg, error->message);
6528   g_free (msg);
6529 }
6530
6531 /* Callback used when the file system model finishes loading */
6532 static void
6533 browse_files_model_finished_loading_cb (GtkFileSystemModel    *model,
6534                                         GError                *error,
6535                                         GtkFileChooserDefault *impl)
6536 {
6537   profile_start ("start", NULL);
6538
6539   if (error)
6540     show_error_on_reading_current_folder (impl, error);
6541
6542   if (impl->load_state == LOAD_PRELOAD)
6543     {
6544       load_remove_timer (impl);
6545       load_set_model (impl);
6546     }
6547   else if (impl->load_state == LOAD_LOADING)
6548     {
6549       /* Nothing */
6550     }
6551   else
6552     {
6553       /* We can't g_assert_not_reached(), as something other than us may have
6554        *  initiated a folder reload.  See #165556.
6555        */
6556       profile_end ("end", NULL);
6557       return;
6558     }
6559
6560   g_assert (impl->load_timeout_id == 0);
6561
6562   impl->load_state = LOAD_FINISHED;
6563
6564   pending_select_files_process (impl);
6565   set_busy_cursor (impl, FALSE);
6566 #ifdef PROFILE_FILE_CHOOSER
6567   access ("MARK: *** FINISHED LOADING", F_OK);
6568 #endif
6569
6570   profile_end ("end", NULL);
6571 }
6572
6573 static void
6574 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6575                                    gboolean remove_from_treeview)
6576 {
6577   load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6578   
6579   if (impl->browse_files_model)
6580     {
6581       g_object_unref (impl->browse_files_model);
6582       impl->browse_files_model = NULL;
6583     }
6584
6585   if (remove_from_treeview)
6586     gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6587 }
6588
6589 static char *
6590 my_g_format_time_for_display (glong secs)
6591 {
6592   GDate mtime, now;
6593   gint days_diff;
6594   struct tm tm_mtime;
6595   time_t time_mtime, time_now;
6596   const gchar *format;
6597   gchar *locale_format = NULL;
6598   gchar buf[256];
6599   char *date_str = NULL;
6600 #ifdef G_OS_WIN32
6601   const char *locale, *dot = NULL;
6602   gint64 codepage = -1;
6603   char charset[20];
6604 #endif
6605
6606   time_mtime = secs;
6607
6608 #ifdef HAVE_LOCALTIME_R
6609   localtime_r ((time_t *) &time_mtime, &tm_mtime);
6610 #else
6611   {
6612     struct tm *ptm = localtime ((time_t *) &time_mtime);
6613
6614     if (!ptm)
6615       {
6616         g_warning ("ptm != NULL failed");
6617         
6618         return g_strdup (_("Unknown"));
6619       }
6620     else
6621       memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6622   }
6623 #endif /* HAVE_LOCALTIME_R */
6624
6625   g_date_set_time_t (&mtime, time_mtime);
6626   time_now = time (NULL);
6627   g_date_set_time_t (&now, time_now);
6628
6629   days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6630
6631   /* Translators: %H means "hours" and %M means "minutes" */
6632   if (days_diff == 0)
6633     format = _("%H:%M");
6634   else if (days_diff == 1)
6635     format = _("Yesterday at %H:%M");
6636   else
6637     {
6638       if (days_diff > 1 && days_diff < 7)
6639         format = "%A"; /* Days from last week */
6640       else
6641         format = "%x"; /* Any other date */
6642     }
6643
6644 #ifdef G_OS_WIN32
6645   /* g_locale_from_utf8() returns a string in the system
6646    * code-page, which is not always the same as that used by the C
6647    * library. For instance when running a GTK+ program with
6648    * LANG=ko on an English version of Windows, the system
6649    * code-page is 1252, but the code-page used by the C library is
6650    * 949. (It's GTK+ itself that sets the C library locale when it
6651    * notices the LANG environment variable. See gtkmain.c The
6652    * Microsoft C library doesn't look at any locale environment
6653    * variables.) We need to pass strftime() a string in the C
6654    * library's code-page. See bug #509885.
6655    */
6656   locale = setlocale (LC_ALL, NULL);
6657   if (locale != NULL)
6658     dot = strchr (locale, '.');
6659   if (dot != NULL)
6660     {
6661       codepage = g_ascii_strtoll (dot+1, NULL, 10);
6662       
6663       /* All codepages should fit in 16 bits AFAIK */
6664       if (codepage > 0 && codepage < 65536)
6665         {
6666           sprintf (charset, "CP%u", (guint) codepage);
6667           locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6668         }
6669     }
6670 #else
6671   locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6672 #endif
6673   if (locale_format != NULL &&
6674       strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6675     {
6676 #ifdef G_OS_WIN32
6677       /* As above but in opposite direction... */
6678       if (codepage > 0 && codepage < 65536)
6679         date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6680 #else
6681       date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6682 #endif
6683     }
6684
6685   if (date_str == NULL)
6686     date_str = g_strdup (_("Unknown"));
6687
6688   g_free (locale_format);
6689   return date_str;
6690 }
6691
6692 static void
6693 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6694 {
6695   GFileAttributeType type;
6696   gpointer value;
6697
6698   if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6699     g_file_info_set_attribute (to, attribute, type, value);
6700 }
6701
6702 static void
6703 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6704 {
6705   GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6706   GFile *file = G_FILE (object);
6707   GFileInfo *queried, *info;
6708   GtkTreeIter iter;
6709
6710   queried = g_file_query_info_finish (file, res, NULL);
6711   if (queried == NULL)
6712     return;
6713
6714   gdk_threads_enter ();
6715
6716   /* now we know model is valid */
6717
6718   /* file was deleted */
6719   if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6720     {
6721       gdk_threads_leave ();
6722       return;
6723     }
6724
6725   info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6726
6727   copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6728   copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6729   copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6730
6731   _gtk_file_system_model_update_file (model, file, info);
6732
6733   g_object_unref (info);
6734
6735   gdk_threads_leave ();
6736 }
6737
6738 static gboolean
6739 file_system_model_set (GtkFileSystemModel *model,
6740                        GFile              *file,
6741                        GFileInfo          *info,
6742                        int                 column,
6743                        GValue             *value,
6744                        gpointer            data)
6745 {
6746   GtkFileChooserDefault *impl = data;
6747  
6748   switch (column)
6749     {
6750     case MODEL_COL_FILE:
6751       g_value_set_object (value, file);
6752       break;
6753     case MODEL_COL_NAME:
6754       if (info == NULL)
6755         g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6756       else 
6757         g_value_set_string (value, g_file_info_get_display_name (info));
6758       break;
6759     case MODEL_COL_NAME_COLLATED:
6760       if (info == NULL)
6761         g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6762       else 
6763         g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6764       break;
6765     case MODEL_COL_IS_FOLDER:
6766       g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6767       break;
6768     case MODEL_COL_IS_SENSITIVE:
6769       if (info)
6770         {
6771           gboolean sensitive = TRUE;
6772
6773           if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6774                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
6775             {
6776               sensitive = TRUE; /* for file modes... */
6777             }
6778           else if (!_gtk_file_info_consider_as_directory (info))
6779             {
6780               sensitive = FALSE; /* for folder modes, files are not sensitive... */
6781             }
6782           else
6783             {
6784               /* ... and for folder modes, folders are sensitive only if the filter says so */
6785               GtkTreeIter iter;
6786               if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6787                 g_assert_not_reached ();
6788               sensitive = !_gtk_file_system_model_iter_is_filtered_out (model, &iter);
6789             }
6790
6791           g_value_set_boolean (value, sensitive);
6792         }
6793       else
6794         g_value_set_boolean (value, TRUE);
6795       break;
6796     case MODEL_COL_PIXBUF:
6797       if (info)
6798         {
6799           if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6800             {
6801               g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6802             }
6803           else
6804             {
6805               GtkTreeModel *tree_model;
6806               GtkTreePath *path, *start, *end;
6807               GtkTreeIter iter;
6808
6809               if (impl->browse_files_tree_view == NULL ||
6810                   g_file_info_has_attribute (info, "filechooser::queried"))
6811                 return FALSE;
6812
6813               tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6814               if (tree_model != GTK_TREE_MODEL (model))
6815                 return FALSE;
6816
6817               if (!_gtk_file_system_model_get_iter_for_file (model,
6818                                                              &iter,
6819                                                              file))
6820                 g_assert_not_reached ();
6821               if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6822                 return FALSE;
6823               path = gtk_tree_model_get_path (tree_model, &iter);
6824               if (gtk_tree_path_compare (start, path) != 1 &&
6825                   gtk_tree_path_compare (path, end) != 1)
6826                 {
6827                   g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6828                   g_file_query_info_async (file,
6829                                            G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6830                                            G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6831                                            G_FILE_ATTRIBUTE_STANDARD_ICON,
6832                                            G_FILE_QUERY_INFO_NONE,
6833                                            G_PRIORITY_DEFAULT,
6834                                            _gtk_file_system_model_get_cancellable (model),
6835                                            file_system_model_got_thumbnail,
6836                                            model);
6837                 }
6838               gtk_tree_path_free (path);
6839               gtk_tree_path_free (start);
6840               gtk_tree_path_free (end);
6841               return FALSE;
6842             }
6843         }
6844       else
6845         g_value_set_object (value, NULL);
6846       break;
6847     case MODEL_COL_SIZE:
6848       g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6849       break;
6850     case MODEL_COL_SIZE_TEXT:
6851       if (info == NULL || _gtk_file_info_consider_as_directory (info))
6852         g_value_set_string (value, NULL);
6853       else
6854         g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6855       break;
6856     case MODEL_COL_MTIME:
6857     case MODEL_COL_MTIME_TEXT:
6858       {
6859         GTimeVal tv;
6860         if (info == NULL)
6861           break;
6862         g_file_info_get_modification_time (info, &tv);
6863         if (column == MODEL_COL_MTIME)
6864           g_value_set_long (value, tv.tv_sec);
6865         else if (tv.tv_sec == 0)
6866           g_value_set_static_string (value, _("Unknown"));
6867         else
6868           g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6869         break;
6870       }
6871     case MODEL_COL_ELLIPSIZE:
6872       g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6873       break;
6874     default:
6875       g_assert_not_reached ();
6876       break;
6877     }
6878
6879   return TRUE;
6880 }
6881
6882 /* Gets rid of the old list model and creates a new one for the current folder */
6883 static gboolean
6884 set_list_model (GtkFileChooserDefault *impl,
6885                 GError               **error)
6886 {
6887   g_assert (impl->current_folder != NULL);
6888
6889   profile_start ("start", NULL);
6890
6891   stop_loading_and_clear_list_model (impl, TRUE);
6892
6893   set_busy_cursor (impl, TRUE);
6894
6895   impl->browse_files_model = 
6896     _gtk_file_system_model_new_for_directory (impl->current_folder,
6897                                               MODEL_ATTRIBUTES,
6898                                               file_system_model_set,
6899                                               impl,
6900                                               MODEL_COLUMN_TYPES);
6901
6902   _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6903
6904   profile_msg ("    set sort function", NULL);
6905   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6906   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6907   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6908   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6909   set_sort_column (impl);
6910   impl->list_sort_ascending = TRUE;
6911   g_signal_connect (impl->browse_files_model, "sort-column-changed",
6912                     G_CALLBACK (list_sort_column_changed_cb), impl);
6913
6914   load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6915
6916   g_signal_connect (impl->browse_files_model, "finished-loading",
6917                     G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6918
6919   _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
6920
6921   profile_end ("end", NULL);
6922
6923   return TRUE;
6924 }
6925
6926 struct update_chooser_entry_selected_foreach_closure {
6927   int num_selected;
6928   GtkTreeIter first_selected_iter;
6929 };
6930
6931 static gint
6932 compare_utf8_filenames (const gchar *a,
6933                         const gchar *b)
6934 {
6935   gchar *a_folded, *b_folded;
6936   gint retval;
6937
6938   a_folded = g_utf8_strdown (a, -1);
6939   b_folded = g_utf8_strdown (b, -1);
6940
6941   retval = strcmp (a_folded, b_folded);
6942
6943   g_free (a_folded);
6944   g_free (b_folded);
6945
6946   return retval;
6947 }
6948
6949 static void
6950 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6951                                        GtkTreePath *path,
6952                                        GtkTreeIter *iter,
6953                                        gpointer data)
6954 {
6955   struct update_chooser_entry_selected_foreach_closure *closure;
6956
6957   closure = data;
6958   closure->num_selected++;
6959
6960   if (closure->num_selected == 1)
6961     closure->first_selected_iter = *iter;
6962 }
6963
6964 static void
6965 update_chooser_entry (GtkFileChooserDefault *impl)
6966 {
6967   GtkTreeSelection *selection;
6968   struct update_chooser_entry_selected_foreach_closure closure;
6969
6970   /* no need to update the file chooser's entry if there's no entry */
6971   if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6972       !impl->location_entry)
6973     return;
6974
6975   if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6976         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6977         || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6978              || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6979             && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6980     return;
6981
6982   g_assert (impl->location_entry != NULL);
6983
6984   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6985   closure.num_selected = 0;
6986   gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6987
6988   if (closure.num_selected == 0)
6989     {
6990       if (impl->operation_mode == OPERATION_MODE_RECENT)
6991         _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6992       else
6993         goto maybe_clear_entry;
6994     }
6995   else if (closure.num_selected == 1)
6996     {
6997       if (impl->operation_mode == OPERATION_MODE_BROWSE)
6998         {
6999           GFileInfo *info;
7000           gboolean change_entry;
7001
7002           info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7003
7004           /* If the cursor moved to the row of the newly created folder,
7005            * retrieving info will return NULL.
7006            */
7007           if (!info)
7008             return;
7009
7010           g_free (impl->browse_files_last_selected_name);
7011           impl->browse_files_last_selected_name =
7012             g_strdup (g_file_info_get_display_name (info));
7013
7014           if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7015               impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7016               impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7017             {
7018               /* Don't change the name when clicking on a folder... */
7019               change_entry = (! _gtk_file_info_consider_as_directory (info));
7020             }
7021           else
7022             change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7023
7024           if (change_entry)
7025             {
7026               gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7027
7028               if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7029                 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7030             }
7031
7032           return;
7033         }
7034       else if (impl->operation_mode == OPERATION_MODE_RECENT
7035                && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7036         {
7037           GFile *folder;
7038
7039           /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7040
7041           gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7042                               MODEL_COL_FILE, &folder,
7043                               -1);
7044           _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7045           g_object_unref (folder);
7046           return;
7047         }
7048     }
7049   else
7050     {
7051       g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7052                   impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7053
7054       /* Multiple selection, so just clear the entry. */
7055       g_free (impl->browse_files_last_selected_name);
7056       impl->browse_files_last_selected_name = NULL;
7057
7058       gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7059       return;
7060     }
7061
7062  maybe_clear_entry:
7063
7064   if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7065       && impl->browse_files_last_selected_name)
7066     {
7067       const char *entry_text;
7068       int len;
7069       gboolean clear_entry;
7070
7071       entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7072       len = strlen (entry_text);
7073       if (len != 0)
7074         {
7075           /* The file chooser entry may have appended a "/" to its text.
7076            * So take it out, and compare the result to the old selection.
7077            */
7078           if (entry_text[len - 1] == G_DIR_SEPARATOR)
7079             {
7080               gchar *tmp;
7081
7082               tmp = g_strndup (entry_text, len - 1);
7083               clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7084               g_free (tmp);
7085             }
7086           else
7087             clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7088         }
7089       else
7090         clear_entry = FALSE;
7091
7092       if (clear_entry)
7093         gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7094     }
7095 }
7096
7097 static gboolean
7098 gtk_file_chooser_default_set_current_folder (GtkFileChooser  *chooser,
7099                                              GFile           *file,
7100                                              GError         **error)
7101 {
7102   return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7103 }
7104
7105
7106 struct UpdateCurrentFolderData
7107 {
7108   GtkFileChooserDefault *impl;
7109   GFile *file;
7110   gboolean keep_trail;
7111   gboolean clear_entry;
7112   GFile *original_file;
7113   GError *original_error;
7114 };
7115
7116 static void
7117 update_current_folder_mount_enclosing_volume_cb (GCancellable        *cancellable,
7118                                                  GtkFileSystemVolume *volume,
7119                                                  const GError        *error,
7120                                                  gpointer             user_data)
7121 {
7122   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7123   struct UpdateCurrentFolderData *data = user_data;
7124   GtkFileChooserDefault *impl = data->impl;
7125
7126   if (cancellable != impl->update_current_folder_cancellable)
7127     goto out;
7128
7129   impl->update_current_folder_cancellable = NULL;
7130   set_busy_cursor (impl, FALSE);
7131
7132   if (cancelled)
7133     goto out;
7134
7135   if (error)
7136     {
7137       error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7138       impl->reload_state = RELOAD_EMPTY;
7139       goto out;
7140     }
7141
7142   change_folder_and_display_error (impl, data->file, data->clear_entry);
7143
7144 out:
7145   g_object_unref (data->file);
7146   g_free (data);
7147
7148   g_object_unref (cancellable);
7149 }
7150
7151 static void
7152 update_current_folder_get_info_cb (GCancellable *cancellable,
7153                                    GFileInfo    *info,
7154                                    const GError *error,
7155                                    gpointer      user_data)
7156 {
7157   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7158   struct UpdateCurrentFolderData *data = user_data;
7159   GtkFileChooserDefault *impl = data->impl;
7160
7161   if (cancellable != impl->update_current_folder_cancellable)
7162     goto out;
7163
7164   impl->update_current_folder_cancellable = NULL;
7165   impl->reload_state = RELOAD_EMPTY;
7166
7167   set_busy_cursor (impl, FALSE);
7168
7169   if (cancelled)
7170     goto out;
7171
7172   if (error)
7173     {
7174       GFile *parent_file;
7175
7176       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7177         {
7178           GMountOperation *mount_operation;
7179           GtkWidget *toplevel;
7180
7181           g_object_unref (cancellable);
7182           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7183
7184           mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7185
7186           set_busy_cursor (impl, TRUE);
7187
7188           impl->update_current_folder_cancellable =
7189             _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7190                                                      mount_operation,
7191                                                      update_current_folder_mount_enclosing_volume_cb,
7192                                                      data);
7193
7194           return;
7195         }
7196
7197       if (!data->original_file)
7198         {
7199           data->original_file = g_object_ref (data->file);
7200           data->original_error = g_error_copy (error);
7201         }
7202
7203       parent_file = g_file_get_parent (data->file);
7204
7205       /* get parent path and try to change the folder to that */
7206       if (parent_file)
7207         {
7208           g_object_unref (data->file);
7209           data->file = parent_file;
7210
7211           g_object_unref (cancellable);
7212
7213           /* restart the update current folder operation */
7214           impl->reload_state = RELOAD_HAS_FOLDER;
7215
7216           impl->update_current_folder_cancellable =
7217             _gtk_file_system_get_info (impl->file_system, data->file,
7218                                        "standard::type",
7219                                        update_current_folder_get_info_cb,
7220                                        data);
7221
7222           set_busy_cursor (impl, TRUE);
7223
7224           return;
7225         }
7226       else
7227         {
7228           /* Error and bail out, ignoring "not found" errors since they're useless:
7229            * they only happen when a program defaults to a folder that has been (re)moved.
7230            */
7231           if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7232             error_changing_folder_dialog (impl, data->original_file, data->original_error);
7233           else
7234             g_error_free (data->original_error);
7235
7236           g_object_unref (data->original_file);
7237
7238           goto out;
7239         }
7240     }
7241
7242   if (data->original_file)
7243     {
7244       /* Error and bail out, ignoring "not found" errors since they're useless:
7245        * they only happen when a program defaults to a folder that has been (re)moved.
7246        */
7247       if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7248         error_changing_folder_dialog (impl, data->original_file, data->original_error);
7249       else
7250         g_error_free (data->original_error);
7251
7252       g_object_unref (data->original_file);
7253     }
7254
7255   if (! _gtk_file_info_consider_as_directory (info))
7256     goto out;
7257
7258   _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail);
7259
7260   if (impl->current_folder != data->file)
7261     {
7262       if (impl->current_folder)
7263         g_object_unref (impl->current_folder);
7264
7265       impl->current_folder = g_object_ref (data->file);
7266     }
7267
7268   impl->reload_state = RELOAD_HAS_FOLDER;
7269
7270   /* Update the widgets that may trigger a folder change themselves.  */
7271
7272   if (!impl->changing_folder)
7273     {
7274       impl->changing_folder = TRUE;
7275
7276       shortcuts_update_current_folder (impl);
7277
7278       impl->changing_folder = FALSE;
7279     }
7280
7281   /* Set the folder on the save entry */
7282
7283   if (impl->location_entry)
7284     {
7285       _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7286                                                impl->current_folder);
7287
7288       if (data->clear_entry)
7289         gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7290     }
7291
7292   /* Create a new list model.  This is slightly evil; we store the result value
7293    * but perform more actions rather than returning immediately even if it
7294    * generates an error.
7295    */
7296   set_list_model (impl, NULL);
7297
7298   /* Refresh controls */
7299
7300   shortcuts_find_current_folder (impl);
7301
7302   g_signal_emit_by_name (impl, "current-folder-changed", 0);
7303
7304   check_preview_change (impl);
7305   bookmarks_check_add_sensitivity (impl);
7306
7307   g_signal_emit_by_name (impl, "selection-changed", 0);
7308
7309 out:
7310   g_object_unref (data->file);
7311   g_free (data);
7312
7313   g_object_unref (cancellable);
7314 }
7315
7316 static gboolean
7317 gtk_file_chooser_default_update_current_folder (GtkFileChooser    *chooser,
7318                                                 GFile             *file,
7319                                                 gboolean           keep_trail,
7320                                                 gboolean           clear_entry,
7321                                                 GError           **error)
7322 {
7323   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7324   struct UpdateCurrentFolderData *data;
7325
7326   profile_start ("start", NULL);
7327
7328   g_object_ref (file);
7329
7330   operation_mode_set (impl, OPERATION_MODE_BROWSE);
7331
7332   if (impl->local_only && !g_file_is_native (file))
7333     {
7334       g_set_error_literal (error,
7335                            GTK_FILE_CHOOSER_ERROR,
7336                            GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7337                            _("Cannot change to folder because it is not local"));
7338
7339       g_object_unref (file);
7340       profile_end ("end - not local", NULL);
7341       return FALSE;
7342     }
7343
7344   if (impl->update_current_folder_cancellable)
7345     g_cancellable_cancel (impl->update_current_folder_cancellable);
7346
7347   /* Test validity of path here.  */
7348   data = g_new0 (struct UpdateCurrentFolderData, 1);
7349   data->impl = impl;
7350   data->file = g_object_ref (file);
7351   data->keep_trail = keep_trail;
7352   data->clear_entry = clear_entry;
7353
7354   impl->reload_state = RELOAD_HAS_FOLDER;
7355
7356   impl->update_current_folder_cancellable =
7357     _gtk_file_system_get_info (impl->file_system, file,
7358                                "standard::type",
7359                                update_current_folder_get_info_cb,
7360                                data);
7361
7362   set_busy_cursor (impl, TRUE);
7363   g_object_unref (file);
7364
7365   profile_end ("end", NULL);
7366   return TRUE;
7367 }
7368
7369 static GFile *
7370 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7371 {
7372   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7373
7374   if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7375       impl->operation_mode == OPERATION_MODE_RECENT)
7376     return NULL;
7377  
7378   if (impl->current_folder)
7379     return g_object_ref (impl->current_folder);
7380
7381   return NULL;
7382 }
7383
7384 static void
7385 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7386                                            const gchar    *name)
7387 {
7388   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7389
7390   g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7391                     impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7392
7393   pending_select_files_free (impl);
7394   gtk_entry_set_text (GTK_ENTRY (impl->location_entry), name);
7395 }
7396
7397 static gboolean
7398 gtk_file_chooser_default_select_file (GtkFileChooser  *chooser,
7399                                       GFile           *file,
7400                                       GError         **error)
7401 {
7402   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7403   GFile *parent_file;
7404   gboolean same_path;
7405
7406   parent_file = g_file_get_parent (file);
7407
7408   if (!parent_file)
7409     return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7410
7411   if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7412       impl->operation_mode == OPERATION_MODE_RECENT ||
7413       impl->load_state == LOAD_EMPTY)
7414     {
7415       same_path = FALSE;
7416     }
7417   else
7418     {
7419       g_assert (impl->current_folder != NULL);
7420
7421       same_path = g_file_equal (parent_file, impl->current_folder);
7422     }
7423
7424   if (same_path && impl->load_state == LOAD_FINISHED)
7425     {
7426       gboolean result;
7427       GSList files;
7428
7429       files.data = (gpointer) file;
7430       files.next = NULL;
7431
7432       result = show_and_select_files (impl, &files);
7433       g_object_unref (parent_file);
7434       return result;
7435     }
7436
7437   pending_select_files_add (impl, file);
7438
7439   if (!same_path)
7440     {
7441       gboolean result;
7442
7443       result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7444       g_object_unref (parent_file);
7445       return result;
7446     }
7447
7448   g_object_unref (parent_file);
7449   return TRUE;
7450 }
7451
7452 static void
7453 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7454                                         GFile          *file)
7455 {
7456   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7457   GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7458   GtkTreeIter iter;
7459
7460   if (!impl->browse_files_model)
7461     return;
7462
7463   if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7464                                                  &iter,
7465                                                  file))
7466     return;
7467
7468   gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7469                                     &iter);
7470 }
7471
7472 static gboolean
7473 maybe_select (GtkTreeModel *model, 
7474               GtkTreePath  *path, 
7475               GtkTreeIter  *iter, 
7476               gpointer     data)
7477 {
7478   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7479   GtkTreeSelection *selection;
7480   gboolean is_sensitive;
7481   gboolean is_folder;
7482   
7483   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7484   
7485   gtk_tree_model_get (model, iter,
7486                       MODEL_COL_IS_FOLDER, &is_folder,
7487                       MODEL_COL_IS_SENSITIVE, &is_sensitive,
7488                       -1);
7489
7490   if (is_sensitive &&
7491       ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7492        (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)))
7493     gtk_tree_selection_select_iter (selection, iter);
7494   else
7495     gtk_tree_selection_unselect_iter (selection, iter);
7496     
7497   return FALSE;
7498 }
7499
7500 static void
7501 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7502 {
7503   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7504
7505   if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7506       impl->operation_mode == OPERATION_MODE_RECENT)
7507     {
7508       GtkTreeSelection *selection;
7509       
7510       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7511       gtk_tree_selection_select_all (selection);
7512       return;
7513     }
7514
7515   if (impl->select_multiple)
7516     gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model), 
7517                             maybe_select, impl);
7518 }
7519
7520 static void
7521 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7522 {
7523   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7524   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7525
7526   gtk_tree_selection_unselect_all (selection);
7527   pending_select_files_free (impl);
7528 }
7529
7530 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7531  *
7532  * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7533  *
7534  * is_empty_ret - whether the file entry is totally empty
7535  *
7536  * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7537  *                          the path will be "$cwd/foobar")
7538  */
7539 static void
7540 check_save_entry (GtkFileChooserDefault *impl,
7541                   GFile                **file_ret,
7542                   gboolean              *is_well_formed_ret,
7543                   gboolean              *is_empty_ret,
7544                   gboolean              *is_file_part_empty_ret,
7545                   gboolean              *is_folder)
7546 {
7547   GtkFileChooserEntry *chooser_entry;
7548   GFile *current_folder;
7549   const char *file_part;
7550   GFile *file;
7551   GError *error;
7552
7553   g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7554             || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7555             || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7556                  || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7557                 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7558
7559   chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7560
7561   if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7562     {
7563       *file_ret = NULL;
7564       *is_well_formed_ret = TRUE;
7565       *is_empty_ret = TRUE;
7566       *is_file_part_empty_ret = TRUE;
7567       *is_folder = FALSE;
7568
7569       return;
7570     }
7571
7572   *is_empty_ret = FALSE;
7573
7574   current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7575   if (!current_folder)
7576     {
7577       *file_ret = NULL;
7578       *is_well_formed_ret = FALSE;
7579       *is_file_part_empty_ret = FALSE;
7580       *is_folder = FALSE;
7581
7582       return;
7583     }
7584
7585   file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7586
7587   if (!file_part || file_part[0] == '\0')
7588     {
7589       *file_ret = current_folder;
7590       *is_well_formed_ret = TRUE;
7591       *is_file_part_empty_ret = TRUE;
7592       *is_folder = TRUE;
7593
7594       return;
7595     }
7596
7597   *is_file_part_empty_ret = FALSE;
7598
7599   error = NULL;
7600   file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7601   g_object_unref (current_folder);
7602
7603   if (!file)
7604     {
7605       error_building_filename_dialog (impl, error);
7606       *file_ret = NULL;
7607       *is_well_formed_ret = FALSE;
7608       *is_folder = FALSE;
7609
7610       return;
7611     }
7612
7613   *file_ret = file;
7614   *is_well_formed_ret = TRUE;
7615   *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7616 }
7617
7618 struct get_files_closure {
7619   GtkFileChooserDefault *impl;
7620   GSList *result;
7621   GFile *file_from_entry;
7622 };
7623
7624 static void
7625 get_files_foreach (GtkTreeModel *model,
7626                    GtkTreePath  *path,
7627                    GtkTreeIter  *iter,
7628                    gpointer      data)
7629 {
7630   struct get_files_closure *info;
7631   GFile *file;
7632   GtkFileSystemModel *fs_model;
7633
7634   info = data;
7635   fs_model = info->impl->browse_files_model;
7636
7637   file = _gtk_file_system_model_get_file (fs_model, iter);
7638   if (!file)
7639     return; /* We are on the editable row */
7640
7641   if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7642     info->result = g_slist_prepend (info->result, g_object_ref (file));
7643 }
7644
7645 static GSList *
7646 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7647 {
7648   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7649   struct get_files_closure info;
7650   GtkWindow *toplevel;
7651   GtkWidget *current_focus;
7652   gboolean file_list_seen;
7653
7654   info.impl = impl;
7655   info.result = NULL;
7656   info.file_from_entry = NULL;
7657
7658   if (impl->operation_mode == OPERATION_MODE_SEARCH)
7659     return search_get_selected_files (impl);
7660
7661   if (impl->operation_mode == OPERATION_MODE_RECENT)
7662     {
7663       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7664         {
7665           file_list_seen = TRUE;
7666           goto file_entry;
7667         }
7668       else
7669         return recent_get_selected_files (impl);
7670     }
7671
7672   toplevel = get_toplevel (GTK_WIDGET (impl));
7673   if (toplevel)
7674     current_focus = gtk_window_get_focus (toplevel);
7675   else
7676     current_focus = NULL;
7677
7678   file_list_seen = FALSE;
7679   if (current_focus == impl->browse_files_tree_view)
7680     {
7681       GtkTreeSelection *selection;
7682
7683     file_list:
7684
7685       file_list_seen = TRUE;
7686       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7687       gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7688
7689       /* If there is no selection in the file list, we probably have this situation:
7690        *
7691        * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7692        * 2. He then double-clicked on a folder ("bar") in the file list
7693        *
7694        * So we want the selection to be "bar/foo.txt".  Jump to the case for the
7695        * filename entry to see if that is the case.
7696        */
7697       if (info.result == NULL && impl->location_entry)
7698         goto file_entry;
7699     }
7700   else if (impl->location_entry && current_focus == impl->location_entry)
7701     {
7702       gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7703
7704     file_entry:
7705
7706       check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7707
7708       if (is_empty)
7709         goto out;
7710
7711       if (!is_well_formed)
7712         return NULL;
7713
7714       if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7715         {
7716           g_object_unref (info.file_from_entry);
7717           return NULL;
7718         }
7719
7720       if (info.file_from_entry)
7721         info.result = g_slist_prepend (info.result, info.file_from_entry);
7722       else if (!file_list_seen) 
7723         goto file_list;
7724       else
7725         return NULL;
7726     }
7727   else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7728     goto file_list;
7729   else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7730     goto file_entry;
7731   else
7732     {
7733       /* The focus is on a dialog's action area button or something else */
7734       if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7735           impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7736         goto file_entry;
7737       else
7738         goto file_list; 
7739     }
7740
7741  out:
7742
7743   /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7744    * fall back to the current directory */
7745   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7746       info.result == NULL)
7747     {
7748       GFile *current_folder;
7749
7750       current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7751
7752       if (current_folder)
7753         info.result = g_slist_prepend (info.result, current_folder);
7754     }
7755
7756   return g_slist_reverse (info.result);
7757 }
7758
7759 GFile *
7760 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7761 {
7762   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7763
7764   if (impl->preview_file)
7765     return g_object_ref (impl->preview_file);
7766   else
7767     return NULL;
7768 }
7769
7770 static GtkFileSystem *
7771 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7772 {
7773   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7774
7775   return impl->file_system;
7776 }
7777
7778 /* Shows or hides the filter widgets */
7779 static void
7780 show_filters (GtkFileChooserDefault *impl,
7781               gboolean               show)
7782 {
7783   if (show)
7784     gtk_widget_show (impl->filter_combo_hbox);
7785   else
7786     gtk_widget_hide (impl->filter_combo_hbox);
7787 }
7788
7789 static void
7790 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7791                                      GtkFileFilter  *filter)
7792 {
7793   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7794   const gchar *name;
7795
7796   if (g_slist_find (impl->filters, filter))
7797     {
7798       g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7799       return;
7800     }
7801
7802   g_object_ref_sink (filter);
7803   impl->filters = g_slist_append (impl->filters, filter);
7804
7805   name = gtk_file_filter_get_name (filter);
7806   if (!name)
7807     name = "Untitled filter";   /* Place-holder, doesn't need to be marked for translation */
7808
7809   gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7810
7811   if (!g_slist_find (impl->filters, impl->current_filter))
7812     set_current_filter (impl, filter);
7813
7814   show_filters (impl, TRUE);
7815 }
7816
7817 static void
7818 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7819                                         GtkFileFilter  *filter)
7820 {
7821   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7822   GtkTreeModel *model;
7823   GtkTreeIter iter;
7824   gint filter_index;
7825
7826   filter_index = g_slist_index (impl->filters, filter);
7827
7828   if (filter_index < 0)
7829     {
7830       g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7831       return;
7832     }
7833
7834   impl->filters = g_slist_remove (impl->filters, filter);
7835
7836   if (filter == impl->current_filter)
7837     {
7838       if (impl->filters)
7839         set_current_filter (impl, impl->filters->data);
7840       else
7841         set_current_filter (impl, NULL);
7842     }
7843
7844   /* Remove row from the combo box */
7845   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7846   if (!gtk_tree_model_iter_nth_child  (model, &iter, NULL, filter_index))
7847     g_assert_not_reached ();
7848
7849   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7850
7851   g_object_unref (filter);
7852
7853   if (!impl->filters)
7854     show_filters (impl, FALSE);
7855 }
7856
7857 static GSList *
7858 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7859 {
7860   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7861
7862   return g_slist_copy (impl->filters);
7863 }
7864
7865 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7866 static int
7867 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7868                                        int                    pos)
7869 {
7870   return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7871 }
7872
7873 struct AddShortcutData
7874 {
7875   GtkFileChooserDefault *impl;
7876   GFile *file;
7877 };
7878
7879 static void
7880 add_shortcut_get_info_cb (GCancellable *cancellable,
7881                           GFileInfo    *info,
7882                           const GError *error,
7883                           gpointer      user_data)
7884 {
7885   int pos;
7886   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7887   struct AddShortcutData *data = user_data;
7888
7889   if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7890     goto out;
7891
7892   data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7893
7894   if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7895     goto out;
7896
7897   pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7898
7899   shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7900
7901   /* need to call shortcuts_add_bookmarks to flush out any duplicates bug #577806 */
7902   shortcuts_add_bookmarks (data->impl);
7903
7904 out:
7905   g_object_unref (data->impl);
7906   g_object_unref (data->file);
7907   g_free (data);
7908
7909   g_object_unref (cancellable);
7910 }
7911
7912 static gboolean
7913 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser  *chooser,
7914                                               GFile           *file,
7915                                               GError         **error)
7916 {
7917   GCancellable *cancellable;
7918   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7919   struct AddShortcutData *data;
7920   GSList *l;
7921   int pos;
7922
7923   /* Avoid adding duplicates */
7924   pos = shortcut_find_position (impl, file);
7925   if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7926     {
7927       gchar *uri;
7928
7929       uri = g_file_get_uri (file);
7930       /* translators, "Shortcut" means "Bookmark" here */
7931       g_set_error (error,
7932                    GTK_FILE_CHOOSER_ERROR,
7933                    GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7934                    _("Shortcut %s already exists"),
7935                    uri);
7936       g_free (uri);
7937
7938       return FALSE;
7939     }
7940
7941   for (l = impl->loading_shortcuts; l; l = l->next)
7942     {
7943       GCancellable *c = l->data;
7944       GFile *f;
7945
7946       f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7947       if (f && g_file_equal (file, f))
7948         {
7949           gchar *uri;
7950
7951           uri = g_file_get_uri (file);
7952           g_set_error (error,
7953                        GTK_FILE_CHOOSER_ERROR,
7954                        GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7955                        _("Shortcut %s already exists"),
7956                        uri);
7957           g_free (uri);
7958
7959           return FALSE;
7960         }
7961     }
7962
7963   data = g_new0 (struct AddShortcutData, 1);
7964   data->impl = g_object_ref (impl);
7965   data->file = g_object_ref (file);
7966
7967   cancellable = _gtk_file_system_get_info (impl->file_system, file,
7968                                            "standard::type",
7969                                            add_shortcut_get_info_cb, data);
7970
7971   if (!cancellable)
7972     return FALSE;
7973
7974   impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7975   g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7976
7977   return TRUE;
7978 }
7979
7980 static gboolean
7981 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser  *chooser,
7982                                                  GFile           *file,
7983                                                  GError         **error)
7984 {
7985   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7986   int pos;
7987   GtkTreeIter iter;
7988   GSList *l;
7989   char *uri;
7990   int i;
7991
7992   for (l = impl->loading_shortcuts; l; l = l->next)
7993     {
7994       GCancellable *c = l->data;
7995       GFile *f;
7996
7997       f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7998       if (f && g_file_equal (file, f))
7999         {
8000           impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8001           g_cancellable_cancel (c);
8002           return TRUE;
8003         }
8004     }
8005
8006   if (impl->num_shortcuts == 0)
8007     goto out;
8008
8009   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8010   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8011     g_assert_not_reached ();
8012
8013   for (i = 0; i < impl->num_shortcuts; i++)
8014     {
8015       gpointer col_data;
8016       ShortcutType shortcut_type;
8017       GFile *shortcut;
8018
8019       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8020                           SHORTCUTS_COL_DATA, &col_data,
8021                           SHORTCUTS_COL_TYPE, &shortcut_type,
8022                           -1);
8023       g_assert (col_data != NULL);
8024       g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8025
8026       shortcut = col_data;
8027       if (g_file_equal (shortcut, file))
8028         {
8029           shortcuts_remove_rows (impl, pos + i, 1);
8030           impl->num_shortcuts--;
8031           return TRUE;
8032         }
8033
8034       if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8035         g_assert_not_reached ();
8036     }
8037
8038  out:
8039
8040   uri = g_file_get_uri (file);
8041   /* translators, "Shortcut" means "Bookmark" here */
8042   g_set_error (error,
8043                GTK_FILE_CHOOSER_ERROR,
8044                GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8045                _("Shortcut %s does not exist"),
8046                uri);
8047   g_free (uri);
8048
8049   return FALSE;
8050 }
8051
8052 static GSList *
8053 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8054 {
8055   GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8056   int pos;
8057   GtkTreeIter iter;
8058   int i;
8059   GSList *list;
8060
8061   if (impl->num_shortcuts == 0)
8062     return NULL;
8063
8064   pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8065   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8066     g_assert_not_reached ();
8067
8068   list = NULL;
8069
8070   for (i = 0; i < impl->num_shortcuts; i++)
8071     {
8072       gpointer col_data;
8073       ShortcutType shortcut_type;
8074       GFile *shortcut;
8075
8076       gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8077                           SHORTCUTS_COL_DATA, &col_data,
8078                           SHORTCUTS_COL_TYPE, &shortcut_type,
8079                           -1);
8080       g_assert (col_data != NULL);
8081       g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8082
8083       shortcut = col_data;
8084       list = g_slist_prepend (list, g_object_ref (shortcut));
8085
8086       if (i != impl->num_shortcuts - 1)
8087         {
8088           if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8089             g_assert_not_reached ();
8090         }
8091     }
8092
8093   return g_slist_reverse (list);
8094 }
8095
8096 /* Guesses a size based upon font sizes */
8097 static void
8098 find_good_size_from_style (GtkWidget *widget,
8099                            gint      *width,
8100                            gint      *height)
8101 {
8102   GtkStyleContext *context;
8103   GtkStateFlags state;
8104   double font_size;
8105   GdkScreen *screen;
8106   double resolution;
8107
8108   context = gtk_widget_get_style_context (widget);
8109   state = gtk_widget_get_state_flags (widget);
8110
8111   screen = gtk_widget_get_screen (widget);
8112   if (screen)
8113     {
8114       resolution = gdk_screen_get_resolution (screen);
8115       if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8116         resolution = 96.0;
8117     }
8118   else
8119     resolution = 96.0; /* wheeee */
8120
8121   gtk_style_context_get (context, state, "font-size", &font_size, NULL);
8122   font_size = font_size * resolution / 72.0 + 0.5;
8123
8124   *width = font_size * NUM_CHARS;
8125   *height = font_size * NUM_LINES;
8126 }
8127
8128 static void
8129 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8130                                            gint                *default_width,
8131                                            gint                *default_height)
8132 {
8133   GtkFileChooserDefault *impl;
8134   GtkRequisition req;
8135   int x, y, width, height;
8136   GSettings *settings;
8137
8138   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8139
8140   settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
8141
8142   g_settings_get (settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8143   g_settings_get (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8144
8145   if (x >= 0 && y >= 0 && width > 0 && height > 0)
8146     {
8147       *default_width = width;
8148       *default_height = height;
8149       return;
8150     }
8151
8152   find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8153
8154   if (impl->preview_widget_active &&
8155       impl->preview_widget &&
8156       gtk_widget_get_visible (impl->preview_widget))
8157     {
8158       gtk_widget_get_preferred_size (impl->preview_box,
8159                                      &req, NULL);
8160       *default_width += PREVIEW_HBOX_SPACING + req.width;
8161     }
8162
8163   if (impl->extra_widget &&
8164       gtk_widget_get_visible (impl->extra_widget))
8165     {
8166       gtk_widget_get_preferred_size (impl->extra_align,
8167                                      &req, NULL);
8168       *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8169     }
8170 }
8171
8172 struct switch_folder_closure {
8173   GtkFileChooserDefault *impl;
8174   GFile *file;
8175   int num_selected;
8176 };
8177
8178 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8179 static void
8180 switch_folder_foreach_cb (GtkTreeModel      *model,
8181                           GtkTreePath       *path,
8182                           GtkTreeIter       *iter,
8183                           gpointer           data)
8184 {
8185   struct switch_folder_closure *closure;
8186
8187   closure = data;
8188
8189   closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8190   closure->num_selected++;
8191 }
8192
8193 /* Changes to the selected folder in the list view */
8194 static void
8195 switch_to_selected_folder (GtkFileChooserDefault *impl)
8196 {
8197   GtkTreeSelection *selection;
8198   struct switch_folder_closure closure;
8199
8200   /* We do this with foreach() rather than get_selected() as we may be in
8201    * multiple selection mode
8202    */
8203
8204   closure.impl = impl;
8205   closure.file = NULL;
8206   closure.num_selected = 0;
8207
8208   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8209   gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8210
8211   g_assert (closure.file && closure.num_selected == 1);
8212
8213   change_folder_and_display_error (impl, closure.file, FALSE);
8214 }
8215
8216 /* Gets the GFileInfo for the selected row in the file list; assumes single
8217  * selection mode.
8218  */
8219 static GFileInfo *
8220 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8221                                        gboolean              *had_selection)
8222 {
8223   GtkTreeSelection *selection;
8224   GtkTreeIter iter;
8225   GFileInfo *info;
8226
8227   g_assert (!impl->select_multiple);
8228   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8229   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8230     {
8231       *had_selection = FALSE;
8232       return NULL;
8233     }
8234
8235   *had_selection = TRUE;
8236
8237   info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8238   return info;
8239 }
8240
8241 /* Gets the display name of the selected file in the file list; assumes single
8242  * selection mode and that something is selected.
8243  */
8244 static const gchar *
8245 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8246 {
8247   GFileInfo *info;
8248   gboolean had_selection;
8249
8250   info = get_selected_file_info_from_file_list (impl, &had_selection);
8251   g_assert (had_selection);
8252   g_assert (info != NULL);
8253
8254   return g_file_info_get_display_name (info);
8255 }
8256
8257 static void
8258 add_custom_button_to_dialog (GtkDialog   *dialog,
8259                              const gchar *mnemonic_label,
8260                              const gchar *stock_id,
8261                              gint         response_id)
8262 {
8263   GtkWidget *button;
8264
8265   button = gtk_button_new_with_mnemonic (mnemonic_label);
8266   gtk_widget_set_can_default (button, TRUE);
8267   gtk_button_set_image (GTK_BUTTON (button),
8268                         gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8269   gtk_widget_show (button);
8270
8271   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8272 }
8273
8274 /* Presents an overwrite confirmation dialog; returns whether we should accept
8275  * the filename.
8276  */
8277 static gboolean
8278 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8279                                        const gchar           *file_part,
8280                                        const gchar           *folder_display_name)
8281 {
8282   GtkWindow *toplevel;
8283   GtkWidget *dialog;
8284   int response;
8285
8286   toplevel = get_toplevel (GTK_WIDGET (impl));
8287
8288   dialog = gtk_message_dialog_new (toplevel,
8289                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8290                                    GTK_MESSAGE_QUESTION,
8291                                    GTK_BUTTONS_NONE,
8292                                    _("A file named \"%s\" already exists.  Do you want to replace it?"),
8293                                    file_part);
8294   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8295                                             _("The file already exists in \"%s\".  Replacing it will "
8296                                               "overwrite its contents."),
8297                                             folder_display_name);
8298
8299   gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8300   add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8301                                GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8302   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8303                                            GTK_RESPONSE_ACCEPT,
8304                                            GTK_RESPONSE_CANCEL,
8305                                            -1);
8306   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8307
8308   if (gtk_window_has_group (toplevel))
8309     gtk_window_group_add_window (gtk_window_get_group (toplevel),
8310                                  GTK_WINDOW (dialog));
8311
8312   response = gtk_dialog_run (GTK_DIALOG (dialog));
8313
8314   gtk_widget_destroy (dialog);
8315
8316   return (response == GTK_RESPONSE_ACCEPT);
8317 }
8318
8319 struct GetDisplayNameData
8320 {
8321   GtkFileChooserDefault *impl;
8322   gchar *file_part;
8323 };
8324
8325 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8326  * as requesting a response means, "the dialog is confirmed".
8327  */
8328 static void
8329 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8330 {
8331   g_signal_emit_by_name (impl, "response-requested");
8332   add_selection_to_recent_list (impl);
8333 }
8334
8335 static void
8336 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8337                                   GFileInfo    *info,
8338                                   const GError *error,
8339                                   gpointer      user_data)
8340 {
8341   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8342   gboolean should_respond = FALSE;
8343   struct GetDisplayNameData *data = user_data;
8344
8345   if (cancellable != data->impl->should_respond_get_info_cancellable)
8346     goto out;
8347
8348   data->impl->should_respond_get_info_cancellable = NULL;
8349
8350   if (cancelled)
8351     goto out;
8352
8353   if (error)
8354     /* Huh?  Did the folder disappear?  Let the caller deal with it */
8355     should_respond = TRUE;
8356   else
8357     should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8358
8359   set_busy_cursor (data->impl, FALSE);
8360   if (should_respond)
8361     request_response_and_add_to_recent_list (data->impl);
8362
8363 out:
8364   g_object_unref (data->impl);
8365   g_free (data->file_part);
8366   g_free (data);
8367
8368   g_object_unref (cancellable);
8369 }
8370
8371 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8372  * should respond.  Can get the file part from the file list or the save entry.
8373  */
8374 static gboolean
8375 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8376                                         const gchar           *file_part,
8377                                         GFile                 *parent_file)
8378 {
8379   GtkFileChooserConfirmation conf;
8380
8381   if (!impl->do_overwrite_confirmation)
8382     return TRUE;
8383
8384   conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8385
8386   g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8387
8388   switch (conf)
8389     {
8390     case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8391       {
8392         struct GetDisplayNameData *data;
8393
8394         g_assert (file_part != NULL);
8395
8396         data = g_new0 (struct GetDisplayNameData, 1);
8397         data->impl = g_object_ref (impl);
8398         data->file_part = g_strdup (file_part);
8399
8400         if (impl->should_respond_get_info_cancellable)
8401           g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8402
8403         impl->should_respond_get_info_cancellable =
8404           _gtk_file_system_get_info (impl->file_system, parent_file,
8405                                      "standard::display-name",
8406                                      confirmation_confirm_get_info_cb,
8407                                      data);
8408         set_busy_cursor (data->impl, TRUE);
8409         return FALSE;
8410       }
8411
8412     case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8413       return TRUE;
8414
8415     case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8416       return FALSE;
8417
8418     default:
8419       g_assert_not_reached ();
8420       return FALSE;
8421     }
8422 }
8423
8424 struct FileExistsData
8425 {
8426   GtkFileChooserDefault *impl;
8427   gboolean file_exists_and_is_not_folder;
8428   GFile *parent_file;
8429   GFile *file;
8430 };
8431
8432 static void
8433 name_entry_get_parent_info_cb (GCancellable *cancellable,
8434                                GFileInfo    *info,
8435                                const GError *error,
8436                                gpointer      user_data)
8437 {
8438   gboolean parent_is_folder;
8439   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8440   struct FileExistsData *data = user_data;
8441
8442   if (cancellable != data->impl->should_respond_get_info_cancellable)
8443     goto out;
8444
8445   data->impl->should_respond_get_info_cancellable = NULL;
8446
8447   set_busy_cursor (data->impl, FALSE);
8448
8449   if (cancelled)
8450     goto out;
8451
8452   if (!info)
8453     parent_is_folder = FALSE;
8454   else
8455     parent_is_folder = _gtk_file_info_consider_as_directory (info);
8456
8457   if (parent_is_folder)
8458     {
8459       if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8460         {
8461           request_response_and_add_to_recent_list (data->impl); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8462         }
8463       else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8464         {
8465           if (data->file_exists_and_is_not_folder)
8466             {
8467               gboolean retval;
8468               char *file_part;
8469
8470               /* Dup the string because the string may be modified
8471                * depending on what clients do in the confirm-overwrite
8472                * signal and this corrupts the pointer
8473                */
8474               file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8475               retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8476               g_free (file_part);
8477
8478               if (retval)
8479                 request_response_and_add_to_recent_list (data->impl);
8480             }
8481           else
8482             request_response_and_add_to_recent_list (data->impl);
8483         }
8484       else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8485                || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8486         {
8487           GError *mkdir_error = NULL;
8488
8489           /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8490            * "/blah/nonexistent" you *will* want a folder created.
8491            */
8492
8493           set_busy_cursor (data->impl, TRUE);
8494           g_file_make_directory (data->file, NULL, &mkdir_error);
8495           set_busy_cursor (data->impl, FALSE);
8496
8497           if (!mkdir_error)
8498             request_response_and_add_to_recent_list (data->impl);
8499           else
8500             error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8501         }
8502       else
8503         g_assert_not_reached ();
8504     }
8505   else
8506     {
8507       if (info)
8508         {
8509           /* The parent exists, but it's not a folder!  Someone probably typed existing_file.txt/subfile.txt */
8510           error_with_file_under_nonfolder (data->impl, data->parent_file);
8511         }
8512       else
8513         {
8514           GError *error_copy;
8515
8516           /* The parent folder is not readable for some reason */
8517
8518           error_copy = g_error_copy (error);
8519           error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8520         }
8521     }
8522
8523 out:
8524   g_object_unref (data->impl);
8525   g_object_unref (data->file);
8526   g_object_unref (data->parent_file);
8527   g_free (data);
8528
8529   g_object_unref (cancellable);
8530 }
8531
8532 static void
8533 file_exists_get_info_cb (GCancellable *cancellable,
8534                          GFileInfo    *info,
8535                          const GError *error,
8536                          gpointer      user_data)
8537 {
8538   gboolean data_ownership_taken = FALSE;
8539   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8540   gboolean file_exists;
8541   gboolean is_folder;
8542   gboolean needs_parent_check = FALSE;
8543   struct FileExistsData *data = user_data;
8544
8545   if (cancellable != data->impl->file_exists_get_info_cancellable)
8546     goto out;
8547
8548   data->impl->file_exists_get_info_cancellable = NULL;
8549
8550   set_busy_cursor (data->impl, FALSE);
8551
8552   if (cancelled)
8553     goto out;
8554
8555   file_exists = (info != NULL);
8556   is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8557
8558   if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8559     {
8560       if (is_folder)
8561         change_folder_and_display_error (data->impl, data->file, TRUE);
8562       else
8563         {
8564           if (file_exists)
8565             request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8566           else
8567             needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8568         }
8569     }
8570   else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8571     {
8572       if (file_exists && !is_folder)
8573         {
8574           /* Oops, the user typed the name of an existing path which is not
8575            * a folder
8576            */
8577           error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8578                                                            g_error_copy (error));
8579         }
8580       else
8581         {
8582           needs_parent_check = TRUE;
8583         }
8584     }
8585   else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8586     {
8587       if (!file_exists)
8588         {
8589           needs_parent_check = TRUE;
8590         }
8591       else
8592         {
8593           if (is_folder)
8594             {
8595               /* User typed a folder; we are done */
8596               request_response_and_add_to_recent_list (data->impl);
8597             }
8598           else
8599             error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8600         }
8601     }
8602   else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8603     {
8604       if (is_folder)
8605         change_folder_and_display_error (data->impl, data->file, TRUE);
8606       else
8607         needs_parent_check = TRUE;
8608     }
8609   else
8610     {
8611       g_assert_not_reached();
8612     }
8613
8614   if (needs_parent_check) {
8615     /* check that everything up to the last path component exists (i.e. the parent) */
8616
8617     data->file_exists_and_is_not_folder = file_exists && !is_folder;
8618     data_ownership_taken = TRUE;
8619
8620     if (data->impl->should_respond_get_info_cancellable)
8621       g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8622
8623       data->impl->should_respond_get_info_cancellable =
8624         _gtk_file_system_get_info (data->impl->file_system,
8625                                    data->parent_file,
8626                                    "standard::type",
8627                                    name_entry_get_parent_info_cb,
8628                                    data);
8629       set_busy_cursor (data->impl, TRUE);
8630     }
8631
8632 out:
8633   if (!data_ownership_taken)
8634     {
8635       g_object_unref (data->impl);
8636       g_object_unref (data->file);
8637       g_object_unref (data->parent_file);
8638       g_free (data);
8639     }
8640
8641   g_object_unref (cancellable);
8642 }
8643
8644 static void
8645 paste_text_received (GtkClipboard          *clipboard,
8646                      const gchar           *text,
8647                      GtkFileChooserDefault *impl)
8648 {
8649   GFile *file;
8650
8651   if (!text)
8652     return;
8653
8654   file = g_file_new_for_uri (text);
8655
8656   if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8657     location_popup_handler (impl, text);
8658
8659   g_object_unref (file);
8660 }
8661
8662 /* Handler for the "location-popup-on-paste" keybinding signal */
8663 static void
8664 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8665 {
8666   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8667                                                       GDK_SELECTION_CLIPBOARD);
8668   gtk_clipboard_request_text (clipboard,
8669                               (GtkClipboardTextReceivedFunc) paste_text_received,
8670                               impl);
8671 }
8672
8673 /* Implementation for GtkFileChooserEmbed::should_respond() */
8674 static void
8675 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8676 {
8677   GSList *files;
8678   GSList *l;
8679
8680   files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8681
8682   for (l = files; l; l = l->next)
8683     {
8684       GFile *file = l->data;
8685       char *uri;
8686
8687       uri = g_file_get_uri (file);
8688       if (uri)
8689         {
8690           gtk_recent_manager_add_item (impl->recent_manager, uri);
8691           g_free (uri);
8692         }
8693     }
8694
8695   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8696   g_slist_free (files);
8697 }
8698
8699 static gboolean
8700 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8701 {
8702   GtkFileChooserDefault *impl;
8703   GtkWidget *toplevel;
8704   GtkWidget *current_focus;
8705   gboolean retval;
8706
8707   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8708
8709   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8710   g_assert (GTK_IS_WINDOW (toplevel));
8711
8712   retval = FALSE;
8713
8714   current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8715
8716   if (current_focus == impl->browse_files_tree_view)
8717     {
8718       /* The following array encodes what we do based on the impl->action and the
8719        * number of files selected.
8720        */
8721       typedef enum {
8722         NOOP,                   /* Do nothing (don't respond) */
8723         RESPOND,                /* Respond immediately */
8724         RESPOND_OR_SWITCH,      /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8725         ALL_FILES,              /* Respond only if everything selected is a file */
8726         ALL_FOLDERS,            /* Respond only if everything selected is a folder */
8727         SAVE_ENTRY,             /* Go to the code for handling the save entry */
8728         NOT_REACHED             /* Sanity check */
8729       } ActionToTake;
8730       static const ActionToTake what_to_do[4][3] = {
8731         /*                                0 selected            1 selected              many selected */
8732         /* ACTION_OPEN */               { NOOP,                 RESPOND_OR_SWITCH,      ALL_FILES   },
8733         /* ACTION_SAVE */               { SAVE_ENTRY,           RESPOND_OR_SWITCH,      NOT_REACHED },
8734         /* ACTION_SELECT_FOLDER */      { RESPOND,              ALL_FOLDERS,            ALL_FOLDERS },
8735         /* ACTION_CREATE_FOLDER */      { SAVE_ENTRY,           ALL_FOLDERS,            NOT_REACHED }
8736       };
8737
8738       int num_selected;
8739       gboolean all_files, all_folders;
8740       int k;
8741       ActionToTake action;
8742
8743     file_list:
8744
8745       g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8746
8747       if (impl->operation_mode == OPERATION_MODE_SEARCH)
8748         {
8749           retval = search_should_respond (impl);
8750           goto out;
8751         }
8752
8753       if (impl->operation_mode == OPERATION_MODE_RECENT)
8754         {
8755           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8756             goto save_entry;
8757           else
8758             {
8759               retval = recent_should_respond (impl);
8760               goto out;
8761             }
8762         }
8763
8764       selection_check (impl, &num_selected, &all_files, &all_folders);
8765
8766       if (num_selected > 2)
8767         k = 2;
8768       else
8769         k = num_selected;
8770
8771       action = what_to_do [impl->action] [k];
8772
8773       switch (action)
8774         {
8775         case NOOP:
8776           return FALSE;
8777
8778         case RESPOND:
8779           retval = TRUE;
8780           goto out;
8781
8782         case RESPOND_OR_SWITCH:
8783           g_assert (num_selected == 1);
8784
8785           if (all_folders)
8786             {
8787               switch_to_selected_folder (impl);
8788               return FALSE;
8789             }
8790           else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8791             {
8792               retval = should_respond_after_confirm_overwrite (impl,
8793                                                                get_display_name_from_file_list (impl),
8794                                                                impl->current_folder);
8795               goto out;
8796             }
8797           else
8798             {
8799               retval = TRUE;
8800               goto out;
8801             }
8802
8803         case ALL_FILES:
8804           retval = all_files;
8805           goto out;
8806
8807         case ALL_FOLDERS:
8808           retval = all_folders;
8809           goto out;
8810
8811         case SAVE_ENTRY:
8812           goto save_entry;
8813
8814         default:
8815           g_assert_not_reached ();
8816         }
8817     }
8818   else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8819     {
8820       GFile *file;
8821       gboolean is_well_formed, is_empty, is_file_part_empty;
8822       gboolean is_folder;
8823       GtkFileChooserEntry *entry;
8824       GError *error;
8825
8826     save_entry:
8827
8828       g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8829                 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8830                 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8831                      || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8832                     && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8833
8834       entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8835       check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8836
8837       if (!is_well_formed)
8838         {
8839           if (!is_empty
8840               && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8841               && impl->operation_mode == OPERATION_MODE_RECENT)
8842             {
8843               path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8844 #if 0
8845               /* We'll #ifdef this out, as the fucking treeview selects its first row,
8846                * thus changing our assumption that no selection is present - setting
8847                * a selection causes the error message from path_bar_set_mode() to go away,
8848                * but we want the user to see that message!
8849                */
8850               gtk_widget_grab_focus (impl->browse_files_tree_view);
8851 #endif
8852             }
8853           /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8854
8855           return FALSE;
8856         }
8857
8858       if (is_empty)
8859         {
8860           if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8861               || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8862             {
8863               path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8864               gtk_widget_grab_focus (impl->location_entry);
8865               return FALSE;
8866             }
8867
8868           goto file_list;
8869         }
8870
8871       g_assert (file != NULL);
8872
8873       error = NULL;
8874       if (is_folder)
8875         {
8876           if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8877               impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8878             {
8879               change_folder_and_display_error (impl, file, TRUE);
8880             }
8881           else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8882                    impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8883             {
8884               /* The folder already exists, so we do not need to create it.
8885                * Just respond to terminate the dialog.
8886                */
8887               retval = TRUE;
8888             }
8889           else
8890             {
8891               g_assert_not_reached ();
8892             }
8893         }
8894       else
8895         {
8896           struct FileExistsData *data;
8897
8898           /* We need to check whether file exists and whether it is a folder -
8899            * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8900            * negative (it doesn't know yet if your last path component is a
8901            * folder).
8902            */
8903
8904           data = g_new0 (struct FileExistsData, 1);
8905           data->impl = g_object_ref (impl);
8906           data->file = g_object_ref (file);
8907           data->parent_file = _gtk_file_chooser_entry_get_current_folder (entry);
8908
8909           if (impl->file_exists_get_info_cancellable)
8910             g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8911
8912           impl->file_exists_get_info_cancellable =
8913             _gtk_file_system_get_info (impl->file_system, file,
8914                                        "standard::type",
8915                                        file_exists_get_info_cb,
8916                                        data);
8917
8918           set_busy_cursor (impl, TRUE);
8919
8920           if (error != NULL)
8921             g_error_free (error);
8922         }
8923
8924       g_object_unref (file);
8925     }
8926   else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8927     {
8928       /* The focus is on a dialog's action area button, *and* the widget that
8929        * was focused immediately before it is the file list.  
8930        */
8931       goto file_list;
8932     }
8933   else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8934     {
8935       search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8936       return FALSE;
8937     }
8938   else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8939     {
8940       /* The focus is on a dialog's action area button, *and* the widget that
8941        * was focused immediately before it is the location entry.
8942        */
8943       goto save_entry;
8944     }
8945   else
8946     /* The focus is on a dialog's action area button or something else */
8947     if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8948         || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8949       goto save_entry;
8950     else
8951       goto file_list; 
8952
8953  out:
8954
8955   if (retval)
8956     add_selection_to_recent_list (impl);
8957
8958   return retval;
8959 }
8960
8961 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8962 static void
8963 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8964 {
8965   GtkFileChooserDefault *impl;
8966   GtkWidget *widget;
8967
8968   impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8969
8970   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8971       impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8972     {
8973       if (impl->location_mode == LOCATION_MODE_PATH_BAR
8974           || impl->operation_mode == OPERATION_MODE_RECENT)
8975         widget = impl->browse_files_tree_view;
8976       else
8977         widget = impl->location_entry;
8978     }
8979   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8980            impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8981     widget = impl->location_entry;
8982   else
8983     {
8984       g_assert_not_reached ();
8985       widget = NULL;
8986     }
8987
8988   g_assert (widget != NULL);
8989   gtk_widget_grab_focus (widget);
8990 }
8991
8992 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8993 static void
8994 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8995                                      GtkTreePath  *path,
8996                                      GtkTreeIter  *iter,
8997                                      gpointer      data)
8998 {
8999   GSList **list;
9000   GFile *file;
9001
9002   list = data;
9003
9004   gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9005   *list = g_slist_prepend (*list, file); /* The file already has a new ref courtesy of gtk_tree_model_get(); this will be unreffed by the caller */
9006 }
9007
9008 /* Constructs a list of the selected paths in search mode */
9009 static GSList *
9010 search_get_selected_files (GtkFileChooserDefault *impl)
9011 {
9012   GSList *result;
9013   GtkTreeSelection *selection;
9014
9015   result = NULL;
9016
9017   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9018   gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9019   result = g_slist_reverse (result);
9020
9021   return result;
9022 }
9023
9024 /* Called from ::should_respond().  We return whether there are selected files
9025  * in the search list.
9026  */
9027 static gboolean
9028 search_should_respond (GtkFileChooserDefault *impl)
9029 {
9030   GtkTreeSelection *selection;
9031
9032   g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9033
9034   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9035   return (gtk_tree_selection_count_selected_rows (selection) != 0);
9036 }
9037
9038 /* Adds one hit from the search engine to the search_model */
9039 static void
9040 search_add_hit (GtkFileChooserDefault *impl,
9041                 gchar                 *uri)
9042 {
9043   GFile *file;
9044
9045   file = g_file_new_for_uri (uri);
9046   if (!file)
9047     return;
9048
9049   if (!g_file_is_native (file))
9050     {
9051       g_object_unref (file);
9052       return;
9053     }
9054
9055   _gtk_file_system_model_add_and_query_file (impl->search_model,
9056                                              file,
9057                                              MODEL_ATTRIBUTES);
9058
9059   g_object_unref (file);
9060 }
9061
9062 /* Callback used from GtkSearchEngine when we get new hits */
9063 static void
9064 search_engine_hits_added_cb (GtkSearchEngine *engine,
9065                              GList           *hits,
9066                              gpointer         data)
9067 {
9068   GtkFileChooserDefault *impl;
9069   GList *l;
9070   
9071   impl = GTK_FILE_CHOOSER_DEFAULT (data);
9072
9073   for (l = hits; l; l = l->next)
9074     search_add_hit (impl, (gchar*)l->data);
9075 }
9076
9077 /* Callback used from GtkSearchEngine when the query is done running */
9078 static void
9079 search_engine_finished_cb (GtkSearchEngine *engine,
9080                            gpointer         data)
9081 {
9082   GtkFileChooserDefault *impl;
9083   
9084   impl = GTK_FILE_CHOOSER_DEFAULT (data);
9085   
9086 #if 0
9087   /* EB: setting the model here will avoid loads of row events,
9088    * but it'll make the search look like blocked.
9089    */
9090   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9091                            GTK_TREE_MODEL (impl->search_model));
9092   file_list_set_sort_column_ids (impl);
9093 #endif
9094
9095   /* FMQ: if search was empty, say that we got no hits */
9096   set_busy_cursor (impl, FALSE);
9097 }
9098
9099 /* Displays a generic error when we cannot create a GtkSearchEngine.  
9100  * It would be better if _gtk_search_engine_new() gave us a GError 
9101  * with a better message, but it doesn't do that right now.
9102  */
9103 static void
9104 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9105 {
9106   error_message (impl,
9107                  _("Could not start the search process"),
9108                  _("The program was not able to create a connection to the indexer "
9109                    "daemon.  Please make sure it is running."));
9110 }
9111
9112 static void
9113 search_engine_error_cb (GtkSearchEngine *engine,
9114                         const gchar     *message,
9115                         gpointer         data)
9116 {
9117   GtkFileChooserDefault *impl;
9118   
9119   impl = GTK_FILE_CHOOSER_DEFAULT (data);
9120
9121   search_stop_searching (impl, TRUE);
9122   error_message (impl, _("Could not send the search request"), message);
9123
9124   set_busy_cursor (impl, FALSE);
9125 }
9126
9127 /* Frees the data in the search_model */
9128 static void
9129 search_clear_model (GtkFileChooserDefault *impl, 
9130                     gboolean               remove_from_treeview)
9131 {
9132   if (!impl->search_model)
9133     return;
9134
9135   g_object_unref (impl->search_model);
9136   impl->search_model = NULL;
9137   
9138   if (remove_from_treeview)
9139     gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9140 }
9141
9142 /* Stops any ongoing searches; does not touch the search_model */
9143 static void
9144 search_stop_searching (GtkFileChooserDefault *impl,
9145                        gboolean               remove_query)
9146 {
9147   if (remove_query && impl->search_query)
9148     {
9149       g_object_unref (impl->search_query);
9150       impl->search_query = NULL;
9151     }
9152   
9153   if (impl->search_engine)
9154     {
9155       _gtk_search_engine_stop (impl->search_engine);
9156       
9157       g_object_unref (impl->search_engine);
9158       impl->search_engine = NULL;
9159     }
9160 }
9161
9162 /* Creates the search_model and puts it in the tree view */
9163 static void
9164 search_setup_model (GtkFileChooserDefault *impl)
9165 {
9166   g_assert (impl->search_model == NULL);
9167
9168   impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9169                                                    impl,
9170                                                    MODEL_COLUMN_TYPES);
9171
9172   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9173                                    MODEL_COL_NAME,
9174                                    name_sort_func,
9175                                    impl, NULL);
9176   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9177                                    MODEL_COL_MTIME,
9178                                    mtime_sort_func,
9179                                    impl, NULL);
9180   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9181                                    MODEL_COL_SIZE,
9182                                    size_sort_func,
9183                                    impl, NULL);
9184   set_sort_column (impl);
9185
9186   /* EB: setting the model here will make the hits list update feel
9187    * more "alive" than setting the model at the end of the search
9188    * run
9189    */
9190   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9191                            GTK_TREE_MODEL (impl->search_model));
9192   file_list_set_sort_column_ids (impl);
9193 }
9194
9195 /* Creates a new query with the specified text and launches it */
9196 static void
9197 search_start_query (GtkFileChooserDefault *impl,
9198                     const gchar           *query_text)
9199 {
9200   search_stop_searching (impl, FALSE);
9201   search_clear_model (impl, TRUE);
9202   search_setup_model (impl);
9203   set_busy_cursor (impl, TRUE);
9204
9205   if (impl->search_engine == NULL)
9206     impl->search_engine = _gtk_search_engine_new ();
9207
9208   if (!impl->search_engine)
9209     {
9210       set_busy_cursor (impl, FALSE);
9211       search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9212       return;
9213     }
9214
9215   if (!impl->search_query)
9216     {
9217       impl->search_query = _gtk_query_new ();
9218       _gtk_query_set_text (impl->search_query, query_text);
9219     }
9220   
9221   _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9222
9223   g_signal_connect (impl->search_engine, "hits-added",
9224                     G_CALLBACK (search_engine_hits_added_cb), impl);
9225   g_signal_connect (impl->search_engine, "finished",
9226                     G_CALLBACK (search_engine_finished_cb), impl);
9227   g_signal_connect (impl->search_engine, "error",
9228                     G_CALLBACK (search_engine_error_cb), impl);
9229
9230   _gtk_search_engine_start (impl->search_engine);
9231 }
9232
9233 /* Callback used when the user presses Enter while typing on the search
9234  * entry; starts the query
9235  */
9236 static void
9237 search_entry_activate_cb (GtkEntry *entry,
9238                           gpointer data)
9239 {
9240   GtkFileChooserDefault *impl;
9241   const char *text;
9242
9243   impl = GTK_FILE_CHOOSER_DEFAULT (data);
9244
9245   text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9246   if (strlen (text) == 0)
9247     return;
9248
9249   /* reset any existing query object */
9250   if (impl->search_query)
9251     {
9252       g_object_unref (impl->search_query);
9253       impl->search_query = NULL;
9254     }
9255
9256   search_start_query (impl, text);
9257 }
9258
9259 static gboolean
9260 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9261 {
9262   gdk_threads_enter ();
9263   
9264   g_source_destroy (impl->focus_entry_idle);
9265   impl->focus_entry_idle = NULL;
9266
9267   if (impl->search_entry)
9268     gtk_widget_grab_focus (impl->search_entry);
9269
9270   gdk_threads_leave ();
9271
9272   return FALSE;
9273 }
9274
9275 static void
9276 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9277 {
9278   /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9279    * pane, we get a selection-changed signal and we set up the search widgets.
9280    * However, gtk_tree_view_button_press() focuses the treeview *after* making
9281    * the change to the selection.  So, we need to re-focus the search entry
9282    * after the treeview has finished doing its work; we'll do that in an idle
9283    * handler.
9284    */
9285
9286   if (!impl->focus_entry_idle)
9287     impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9288 }
9289
9290 /* Hides the path bar and creates the search entry */
9291 static void
9292 search_setup_widgets (GtkFileChooserDefault *impl)
9293 {
9294   impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9295
9296   path_bar_update (impl);
9297
9298   impl->search_entry = gtk_entry_new ();
9299   g_signal_connect (impl->search_entry, "activate",
9300                     G_CALLBACK (search_entry_activate_cb),
9301                     impl);
9302   gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9303
9304   /* if there already is a query, restart it */
9305   if (impl->search_query)
9306     {
9307       gchar *query = _gtk_query_get_text (impl->search_query);
9308
9309       if (query)
9310         {
9311           gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9312           search_start_query (impl, query);
9313
9314           g_free (query);
9315         }
9316       else
9317         {
9318           g_object_unref (impl->search_query);
9319           impl->search_query = NULL;
9320         }
9321     }
9322
9323   /* Box for search widgets */
9324   gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9325   gtk_widget_show_all (impl->search_hbox);
9326   gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9327
9328   /* Hide the location widgets temporarily */
9329
9330   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9331       impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9332     {
9333       gtk_widget_hide (impl->location_button);
9334       gtk_widget_hide (impl->location_entry_box);
9335     }
9336
9337   focus_search_entry_in_idle (impl);
9338
9339   /* FMQ: hide the filter combo? */
9340 }
9341
9342 /*
9343  * Recent files support
9344  */
9345
9346 /* Frees the data in the recent_model */
9347 static void
9348 recent_clear_model (GtkFileChooserDefault *impl,
9349                     gboolean               remove_from_treeview)
9350 {
9351   if (!impl->recent_model)
9352     return;
9353
9354   if (remove_from_treeview)
9355     gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9356
9357   g_object_unref (impl->recent_model);
9358   impl->recent_model = NULL;
9359 }
9360
9361 /* Stops any ongoing loading of the recent files list; does
9362  * not touch the recent_model
9363  */
9364 static void
9365 recent_stop_loading (GtkFileChooserDefault *impl)
9366 {
9367   if (impl->load_recent_id)
9368     {
9369       g_source_remove (impl->load_recent_id);
9370       impl->load_recent_id = 0;
9371     }
9372 }
9373
9374 static void
9375 recent_setup_model (GtkFileChooserDefault *impl)
9376 {
9377   g_assert (impl->recent_model == NULL);
9378
9379   impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9380                                                    impl,
9381                                                    MODEL_COLUMN_TYPES);
9382
9383   _gtk_file_system_model_set_filter (impl->recent_model,
9384                                      impl->current_filter);
9385   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9386                                    MODEL_COL_NAME,
9387                                    name_sort_func,
9388                                    impl, NULL);
9389   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9390                                    MODEL_COL_SIZE,
9391                                    size_sort_func,
9392                                    impl, NULL);
9393   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9394                                    MODEL_COL_MTIME,
9395                                    mtime_sort_func,
9396                                    impl, NULL);
9397   set_sort_column (impl);
9398 }
9399
9400 typedef struct
9401 {
9402   GtkFileChooserDefault *impl;
9403   GList *items;
9404 } RecentLoadData;
9405
9406 static void
9407 recent_idle_cleanup (gpointer data)
9408 {
9409   RecentLoadData *load_data = data;
9410   GtkFileChooserDefault *impl = load_data->impl;
9411
9412   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9413                            GTK_TREE_MODEL (impl->recent_model));
9414   file_list_set_sort_column_ids (impl);
9415   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9416
9417   set_busy_cursor (impl, FALSE);
9418   
9419   impl->load_recent_id = 0;
9420   
9421   g_free (load_data);
9422 }
9423
9424 static gint
9425 get_recent_files_limit (GtkWidget *widget)
9426 {
9427   GtkSettings *settings;
9428   gint limit;
9429
9430   if (gtk_widget_has_screen (widget))
9431     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9432   else
9433     settings = gtk_settings_get_default ();
9434
9435   g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9436
9437   return limit;
9438 }
9439
9440 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9441 static void
9442 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9443 {
9444   gint limit;
9445   GList *l;
9446   int n;
9447
9448   limit = get_recent_files_limit (GTK_WIDGET (impl));
9449
9450   n = 0;
9451
9452   for (l = items; l; l = l->next)
9453     {
9454       GtkRecentInfo *info = l->data;
9455       GFile *file;
9456
9457       file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9458       _gtk_file_system_model_add_and_query_file (impl->recent_model,
9459                                                  file,
9460                                                  MODEL_ATTRIBUTES);
9461       g_object_unref (file);
9462
9463       n++;
9464       if (limit != -1 && n >= limit)
9465         break;
9466     }
9467 }
9468
9469 static void
9470 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9471 {
9472   GList *folders;
9473   GList *l;
9474
9475   folders = _gtk_file_chooser_extract_recent_folders (items);
9476
9477   for (l = folders; l; l = l->next)
9478     {
9479       GFile *folder = l->data;
9480
9481       _gtk_file_system_model_add_and_query_file (impl->recent_model,
9482                                                  folder,
9483                                                  MODEL_ATTRIBUTES);
9484     }
9485
9486   g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9487   g_list_free (folders);
9488 }
9489
9490 static gboolean
9491 recent_idle_load (gpointer data)
9492 {
9493   RecentLoadData *load_data = data;
9494   GtkFileChooserDefault *impl = load_data->impl;
9495
9496   if (!impl->recent_manager)
9497     return FALSE;
9498
9499   load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9500   if (!load_data->items)
9501     return FALSE;
9502
9503   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9504     populate_model_with_recent_items (impl, load_data->items);
9505   else
9506     populate_model_with_folders (impl, load_data->items);
9507
9508   g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9509   g_list_free (load_data->items);
9510   load_data->items = NULL;
9511
9512   return FALSE;
9513 }
9514
9515 static void
9516 recent_start_loading (GtkFileChooserDefault *impl)
9517 {
9518   RecentLoadData *load_data;
9519
9520   recent_stop_loading (impl);
9521   recent_clear_model (impl, TRUE);
9522   recent_setup_model (impl);
9523   set_busy_cursor (impl, TRUE);
9524
9525   g_assert (impl->load_recent_id == 0);
9526
9527   load_data = g_new (RecentLoadData, 1);
9528   load_data->impl = impl;
9529   load_data->items = NULL;
9530
9531   /* begin lazy loading the recent files into the model */
9532   impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9533                                                     recent_idle_load,
9534                                                     load_data,
9535                                                     recent_idle_cleanup);
9536 }
9537
9538 static void
9539 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9540                                      GtkTreePath  *path,
9541                                      GtkTreeIter  *iter,
9542                                      gpointer      data)
9543 {
9544   GSList **list;
9545   GFile *file;
9546
9547   list = data;
9548
9549   gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9550   *list = g_slist_prepend (*list, file);
9551 }
9552
9553 /* Constructs a list of the selected paths in recent files mode */
9554 static GSList *
9555 recent_get_selected_files (GtkFileChooserDefault *impl)
9556 {
9557   GSList *result;
9558   GtkTreeSelection *selection;
9559
9560   result = NULL;
9561
9562   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9563   gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9564   result = g_slist_reverse (result);
9565
9566   return result;
9567 }
9568
9569 /* Called from ::should_respond().  We return whether there are selected
9570  * files in the recent files list.
9571  */
9572 static gboolean
9573 recent_should_respond (GtkFileChooserDefault *impl)
9574 {
9575   GtkTreeSelection *selection;
9576
9577   g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9578
9579   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9580   return (gtk_tree_selection_count_selected_rows (selection) != 0);
9581 }
9582
9583 static void
9584 set_current_filter (GtkFileChooserDefault *impl,
9585                     GtkFileFilter         *filter)
9586 {
9587   if (impl->current_filter != filter)
9588     {
9589       int filter_index;
9590
9591       /* NULL filters are allowed to reset to non-filtered status
9592        */
9593       filter_index = g_slist_index (impl->filters, filter);
9594       if (impl->filters && filter && filter_index < 0)
9595         return;
9596
9597       if (impl->current_filter)
9598         g_object_unref (impl->current_filter);
9599       impl->current_filter = filter;
9600       if (impl->current_filter)
9601         {
9602           g_object_ref_sink (impl->current_filter);
9603         }
9604
9605       if (impl->filters)
9606         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9607                                   filter_index);
9608
9609       if (impl->browse_files_model)
9610         {
9611           _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
9612           _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_IS_SENSITIVE);
9613         }
9614
9615       if (impl->search_model)
9616         {
9617           _gtk_file_system_model_set_filter (impl->search_model, filter);
9618           _gtk_file_system_model_clear_cache (impl->search_model, MODEL_COL_IS_SENSITIVE);
9619         }
9620
9621       if (impl->recent_model)
9622         {
9623           _gtk_file_system_model_set_filter (impl->recent_model, filter);
9624           _gtk_file_system_model_clear_cache (impl->recent_model, MODEL_COL_IS_SENSITIVE);
9625         }
9626
9627       g_object_notify (G_OBJECT (impl), "filter");
9628     }
9629 }
9630
9631 static void
9632 filter_combo_changed (GtkComboBox           *combo_box,
9633                       GtkFileChooserDefault *impl)
9634 {
9635   gint new_index = gtk_combo_box_get_active (combo_box);
9636   GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9637
9638   set_current_filter (impl, new_filter);
9639 }
9640
9641 static void
9642 check_preview_change (GtkFileChooserDefault *impl)
9643 {
9644   GtkTreePath *cursor_path;
9645   GFile *new_file;
9646   char *new_display_name;
9647   GtkTreeModel *model;
9648
9649   gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9650   model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9651   if (cursor_path)
9652     {
9653       GtkTreeIter iter;
9654
9655       gtk_tree_model_get_iter (model, &iter, cursor_path);
9656       gtk_tree_model_get (model, &iter,
9657                           MODEL_COL_FILE, &new_file,
9658                           MODEL_COL_NAME, &new_display_name,
9659                           -1);
9660       
9661       gtk_tree_path_free (cursor_path);
9662     }
9663   else
9664     {
9665       new_file = NULL;
9666       new_display_name = NULL;
9667     }
9668
9669   if (new_file != impl->preview_file &&
9670       !(new_file && impl->preview_file &&
9671         g_file_equal (new_file, impl->preview_file)))
9672     {
9673       if (impl->preview_file)
9674         {
9675           g_object_unref (impl->preview_file);
9676           g_free (impl->preview_display_name);
9677         }
9678
9679       if (new_file)
9680         {
9681           impl->preview_file = new_file;
9682           impl->preview_display_name = new_display_name;
9683         }
9684       else
9685         {
9686           impl->preview_file = NULL;
9687           impl->preview_display_name = NULL;
9688           g_free (new_display_name);
9689         }
9690
9691       if (impl->use_preview_label && impl->preview_label)
9692         gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9693
9694       g_signal_emit_by_name (impl, "update-preview");
9695     }
9696   else
9697     {
9698       if (new_file)
9699         g_object_unref (new_file);
9700
9701       g_free (new_display_name);
9702     }
9703 }
9704
9705 static void
9706 shortcuts_activate_volume_mount_cb (GCancellable        *cancellable,
9707                                     GtkFileSystemVolume *volume,
9708                                     const GError        *error,
9709                                     gpointer             data)
9710 {
9711   GFile *file;
9712   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9713   GtkFileChooserDefault *impl = data;
9714
9715   if (cancellable != impl->shortcuts_activate_iter_cancellable)
9716     goto out;
9717
9718   impl->shortcuts_activate_iter_cancellable = NULL;
9719
9720   set_busy_cursor (impl, FALSE);
9721
9722   if (cancelled)
9723     goto out;
9724
9725   if (error)
9726     {
9727       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9728         {
9729           char *msg, *name;
9730
9731           name = _gtk_file_system_volume_get_display_name (volume);
9732           msg = g_strdup_printf (_("Could not mount %s"), name);
9733
9734           error_message (impl, msg, error->message);
9735
9736           g_free (msg);
9737           g_free (name);
9738         }
9739
9740       goto out;
9741     }
9742
9743   file = _gtk_file_system_volume_get_root (volume);
9744   if (file != NULL)
9745     {
9746       change_folder_and_display_error (impl, file, FALSE);
9747       g_object_unref (file);
9748     }
9749
9750 out:
9751   g_object_unref (impl);
9752   g_object_unref (cancellable);
9753 }
9754
9755
9756 /* Activates a volume by mounting it if necessary and then switching to its
9757  * base path.
9758  */
9759 static void
9760 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9761                            GtkFileSystemVolume   *volume)
9762 {
9763   GFile *file;
9764
9765   operation_mode_set (impl, OPERATION_MODE_BROWSE);
9766
9767   /* We ref the file chooser since volume_mount() may run a main loop, and the
9768    * user could close the file chooser window in the meantime.
9769    */
9770   g_object_ref (impl);
9771
9772   if (!_gtk_file_system_volume_is_mounted (volume))
9773     {
9774       GMountOperation *mount_op;
9775
9776       set_busy_cursor (impl, TRUE);
9777    
9778       mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9779       impl->shortcuts_activate_iter_cancellable =
9780         _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9781                                        shortcuts_activate_volume_mount_cb,
9782                                        g_object_ref (impl));
9783       g_object_unref (mount_op);
9784     }
9785   else
9786     {
9787       file = _gtk_file_system_volume_get_root (volume);
9788       if (file != NULL)
9789         {
9790           change_folder_and_display_error (impl, file, FALSE);
9791           g_object_unref (file);
9792         }
9793     }
9794
9795   g_object_unref (impl);
9796 }
9797
9798 /* Opens the folder or volume at the specified iter in the shortcuts model */
9799 struct ShortcutsActivateData
9800 {
9801   GtkFileChooserDefault *impl;
9802   GFile *file;
9803 };
9804
9805 static void
9806 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9807                                 GFileInfo    *info,
9808                                 const GError *error,
9809                                 gpointer      user_data)
9810 {
9811   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9812   struct ShortcutsActivateData *data = user_data;
9813
9814   if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9815     goto out;
9816
9817   data->impl->shortcuts_activate_iter_cancellable = NULL;
9818
9819   if (cancelled)
9820     goto out;
9821
9822   if (!error && _gtk_file_info_consider_as_directory (info))
9823     change_folder_and_display_error (data->impl, data->file, FALSE);
9824   else
9825     gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9826                                           data->file,
9827                                           NULL);
9828
9829 out:
9830   g_object_unref (data->impl);
9831   g_object_unref (data->file);
9832   g_free (data);
9833
9834   g_object_unref (cancellable);
9835 }
9836
9837 static void
9838 shortcuts_activate_mount_enclosing_volume (GCancellable        *cancellable,
9839                                            GtkFileSystemVolume *volume,
9840                                            const GError        *error,
9841                                            gpointer             user_data)
9842 {
9843   struct ShortcutsActivateData *data = user_data;
9844
9845   if (error)
9846     {
9847       error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9848
9849       g_object_unref (data->impl);
9850       g_object_unref (data->file);
9851       g_free (data);
9852
9853       return;
9854     }
9855
9856   data->impl->shortcuts_activate_iter_cancellable =
9857     _gtk_file_system_get_info (data->impl->file_system, data->file,
9858                                "standard::type",
9859                                shortcuts_activate_get_info_cb, data);
9860
9861   if (volume)
9862     _gtk_file_system_volume_unref (volume);
9863 }
9864
9865 static void
9866 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9867                          GtkTreeIter           *iter)
9868 {
9869   gpointer col_data;
9870   ShortcutType shortcut_type;
9871
9872   /* In the Save modes, we want to preserve what the uesr typed in the filename
9873    * entry, so that he may choose another folder without erasing his typed name.
9874    */
9875   if (impl->location_entry
9876       && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9877            || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9878     gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
9879
9880   gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9881                       SHORTCUTS_COL_DATA, &col_data,
9882                       SHORTCUTS_COL_TYPE, &shortcut_type,
9883                       -1);
9884
9885   if (impl->shortcuts_activate_iter_cancellable)
9886     {
9887       g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9888       impl->shortcuts_activate_iter_cancellable = NULL;
9889     }
9890
9891   if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9892     return;
9893   else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9894     {
9895       GtkFileSystemVolume *volume;
9896
9897       volume = col_data;
9898
9899       operation_mode_set (impl, OPERATION_MODE_BROWSE);
9900
9901       shortcuts_activate_volume (impl, volume);
9902     }
9903   else if (shortcut_type == SHORTCUT_TYPE_FILE)
9904     {
9905       struct ShortcutsActivateData *data;
9906       GtkFileSystemVolume *volume;
9907
9908       operation_mode_set (impl, OPERATION_MODE_BROWSE);
9909
9910       volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9911
9912       data = g_new0 (struct ShortcutsActivateData, 1);
9913       data->impl = g_object_ref (impl);
9914       data->file = g_object_ref (col_data);
9915
9916       if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9917         {
9918           GMountOperation *mount_operation;
9919           GtkWidget *toplevel;
9920
9921           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9922
9923           mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9924
9925           impl->shortcuts_activate_iter_cancellable =
9926             _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9927                                                      mount_operation,
9928                                                      shortcuts_activate_mount_enclosing_volume,
9929                                                      data);
9930         }
9931       else
9932         {
9933           impl->shortcuts_activate_iter_cancellable =
9934             _gtk_file_system_get_info (impl->file_system, data->file,
9935                                        "standard::type",
9936                                        shortcuts_activate_get_info_cb, data);
9937         }
9938     }
9939   else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9940     {
9941       operation_mode_set (impl, OPERATION_MODE_SEARCH);
9942     }
9943   else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9944     {
9945       operation_mode_set (impl, OPERATION_MODE_RECENT);
9946     }
9947 }
9948
9949 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9950 static gboolean
9951 shortcuts_key_press_event_cb (GtkWidget             *widget,
9952                               GdkEventKey           *event,
9953                               GtkFileChooserDefault *impl)
9954 {
9955   guint modifiers;
9956
9957   modifiers = gtk_accelerator_get_default_mod_mask ();
9958
9959   if (key_is_left_or_right (event))
9960     {
9961       gtk_widget_grab_focus (impl->browse_files_tree_view);
9962       return TRUE;
9963     }
9964
9965   if ((event->keyval == GDK_KEY_BackSpace
9966       || event->keyval == GDK_KEY_Delete
9967       || event->keyval == GDK_KEY_KP_Delete)
9968       && (event->state & modifiers) == 0)
9969     {
9970       remove_selected_bookmarks (impl);
9971       return TRUE;
9972     }
9973
9974   if ((event->keyval == GDK_KEY_F2)
9975       && (event->state & modifiers) == 0)
9976     {
9977       rename_selected_bookmark (impl);
9978       return TRUE;
9979     }
9980
9981   return FALSE;
9982 }
9983
9984 static gboolean
9985 shortcuts_select_func  (GtkTreeSelection  *selection,
9986                         GtkTreeModel      *model,
9987                         GtkTreePath       *path,
9988                         gboolean           path_currently_selected,
9989                         gpointer           data)
9990 {
9991   GtkFileChooserDefault *impl = data;
9992   GtkTreeIter filter_iter;
9993   ShortcutType shortcut_type;
9994
9995   if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9996     g_assert_not_reached ();
9997
9998   gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9999
10000   return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10001 }
10002
10003 static gboolean
10004 list_select_func  (GtkTreeSelection  *selection,
10005                    GtkTreeModel      *model,
10006                    GtkTreePath       *path,
10007                    gboolean           path_currently_selected,
10008                    gpointer           data)
10009 {
10010   GtkFileChooserDefault *impl = data;
10011
10012   if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10013       impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10014     {
10015       GtkTreeIter iter;
10016       gboolean is_sensitive;
10017       gboolean is_folder;
10018
10019       if (!gtk_tree_model_get_iter (model, &iter, path))
10020         return FALSE;
10021       gtk_tree_model_get (model, &iter,
10022                           MODEL_COL_IS_SENSITIVE, &is_sensitive,
10023                           MODEL_COL_IS_FOLDER, &is_folder,
10024                           -1);
10025       if (!is_sensitive || !is_folder)
10026         return FALSE;
10027     }
10028     
10029   return TRUE;
10030 }
10031
10032 static void
10033 list_selection_changed (GtkTreeSelection      *selection,
10034                         GtkFileChooserDefault *impl)
10035 {
10036   /* See if we are in the new folder editable row for Save mode */
10037   if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10038       impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10039     {
10040       GFileInfo *info;
10041       gboolean had_selection;
10042
10043       info = get_selected_file_info_from_file_list (impl, &had_selection);
10044       if (!had_selection)
10045         goto out; /* normal processing */
10046
10047       if (!info)
10048         return; /* We are on the editable row for New Folder */
10049     }
10050
10051  out:
10052
10053   if (impl->location_entry)
10054     update_chooser_entry (impl);
10055
10056   path_bar_update (impl);
10057
10058   check_preview_change (impl);
10059   bookmarks_check_add_sensitivity (impl);
10060   check_copy_file_location_sensitivity (impl);
10061
10062   g_signal_emit_by_name (impl, "selection-changed", 0);
10063 }
10064
10065 /* Callback used when a row in the file list is activated */
10066 static void
10067 list_row_activated (GtkTreeView           *tree_view,
10068                     GtkTreePath           *path,
10069                     GtkTreeViewColumn     *column,
10070                     GtkFileChooserDefault *impl)
10071 {
10072   GFile *file;
10073   GtkTreeIter iter;
10074   GtkTreeModel *model;
10075   gboolean is_folder;
10076   gboolean is_sensitive;
10077
10078   model = gtk_tree_view_get_model (tree_view);
10079
10080   if (!gtk_tree_model_get_iter (model, &iter, path))
10081     return;
10082
10083   gtk_tree_model_get (model, &iter,
10084                       MODEL_COL_FILE, &file,
10085                       MODEL_COL_IS_FOLDER, &is_folder,
10086                       MODEL_COL_IS_SENSITIVE, &is_sensitive,
10087                       -1);
10088         
10089   if (is_sensitive && is_folder && file)
10090     {
10091       change_folder_and_display_error (impl, file, FALSE);
10092       goto out;
10093     }
10094
10095   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10096       impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10097     g_signal_emit_by_name (impl, "file-activated");
10098
10099  out:
10100
10101   if (file)
10102     g_object_unref (file);
10103 }
10104
10105 static void
10106 path_bar_clicked (GtkPathBar            *path_bar,
10107                   GFile                 *file,
10108                   GFile                 *child_file,
10109                   gboolean               child_is_hidden,
10110                   GtkFileChooserDefault *impl)
10111 {
10112   if (child_file)
10113     pending_select_files_add (impl, child_file);
10114
10115   if (!change_folder_and_display_error (impl, file, FALSE))
10116     return;
10117
10118   /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar".  We should then
10119    * show hidden files so that ".baz" appears in the file list, as it will still
10120    * be shown in the path bar: "/foo/[bar]/.baz"
10121    */
10122   if (child_is_hidden)
10123     g_object_set (impl, "show-hidden", TRUE, NULL);
10124 }
10125
10126 static void
10127 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10128 {
10129   GtkTreeViewColumn *column;
10130   GtkCellRenderer *renderer;
10131   GList *walk, *list;
10132
10133   /* Keep the following column numbers in sync with create_file_list() */
10134
10135   /* name */
10136   column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10137   list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10138   for (walk = list; walk; walk = walk->next)
10139     {
10140       renderer = walk->data;
10141       if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10142         {
10143           gtk_tree_view_column_set_attributes (column, renderer, 
10144                                                "pixbuf", MODEL_COL_PIXBUF,
10145                                                NULL);
10146         }
10147       else
10148         {
10149           gtk_tree_view_column_set_attributes (column, renderer, 
10150                                                "text", MODEL_COL_NAME,
10151                                                "ellipsize", MODEL_COL_ELLIPSIZE,
10152                                                NULL);
10153         }
10154
10155       gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10156     }
10157   g_list_free (list);
10158
10159   /* size */
10160   column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10161   list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10162   renderer = list->data;
10163   gtk_tree_view_column_set_attributes (column, renderer, 
10164                                        "text", MODEL_COL_SIZE_TEXT,
10165                                        NULL);
10166
10167   gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10168   g_list_free (list);
10169
10170   /* mtime */
10171   column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10172   list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10173   renderer = list->data;
10174   gtk_tree_view_column_set_attributes (column, renderer, 
10175                                        "text", MODEL_COL_MTIME_TEXT,
10176                                        NULL);
10177   gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10178   g_list_free (list);
10179 }
10180
10181 GtkWidget *
10182 _gtk_file_chooser_default_new (void)
10183 {
10184   return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10185 }
10186
10187 static void
10188 location_set_user_text (GtkFileChooserDefault *impl,
10189                         const gchar           *path)
10190 {
10191   gtk_entry_set_text (GTK_ENTRY (impl->location_entry), path);
10192   gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10193 }
10194
10195 static void
10196 location_popup_handler (GtkFileChooserDefault *impl,
10197                         const gchar           *path)
10198
10199   if (impl->operation_mode != OPERATION_MODE_BROWSE)
10200     {
10201       GtkWidget *widget_to_focus;
10202
10203       operation_mode_set (impl, OPERATION_MODE_BROWSE);
10204       
10205       if (impl->current_folder)
10206         change_folder_and_display_error (impl, impl->current_folder, FALSE);
10207
10208       if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10209         widget_to_focus = impl->browse_files_tree_view;
10210       else
10211         widget_to_focus = impl->location_entry;
10212
10213       gtk_widget_grab_focus (widget_to_focus);
10214       return; 
10215     }
10216   
10217   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10218       impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10219     {
10220       if (!path)
10221         return;
10222
10223       location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10224       location_set_user_text (impl, path);
10225     }
10226   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10227            impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10228     {
10229       gtk_widget_grab_focus (impl->location_entry);
10230       if (path != NULL)
10231         location_set_user_text (impl, path);
10232     }
10233   else
10234     g_assert_not_reached ();
10235 }
10236
10237 /* Handler for the "up-folder" keybinding signal */
10238 static void
10239 up_folder_handler (GtkFileChooserDefault *impl)
10240 {
10241   _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10242 }
10243
10244 /* Handler for the "down-folder" keybinding signal */
10245 static void
10246 down_folder_handler (GtkFileChooserDefault *impl)
10247 {
10248   _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10249 }
10250
10251 /* Switches to the shortcut in the specified index */
10252 static void
10253 switch_to_shortcut (GtkFileChooserDefault *impl,
10254                     int pos)
10255 {
10256   GtkTreeIter iter;
10257
10258   if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10259     g_assert_not_reached ();
10260
10261   shortcuts_activate_iter (impl, &iter);
10262 }
10263
10264 /* Handler for the "home-folder" keybinding signal */
10265 static void
10266 home_folder_handler (GtkFileChooserDefault *impl)
10267 {
10268   if (impl->has_home)
10269     switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10270 }
10271
10272 /* Handler for the "desktop-folder" keybinding signal */
10273 static void
10274 desktop_folder_handler (GtkFileChooserDefault *impl)
10275 {
10276   if (impl->has_desktop)
10277     switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10278 }
10279
10280 /* Handler for the "search-shortcut" keybinding signal */
10281 static void
10282 search_shortcut_handler (GtkFileChooserDefault *impl)
10283 {
10284   if (impl->has_search)
10285     {
10286       switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10287
10288       /* we want the entry widget to grab the focus the first
10289        * time, not the browse_files_tree_view widget.
10290        */
10291       if (impl->search_entry)
10292         gtk_widget_grab_focus (impl->search_entry);
10293     }
10294 }
10295
10296 /* Handler for the "recent-shortcut" keybinding signal */
10297 static void
10298 recent_shortcut_handler (GtkFileChooserDefault *impl)
10299 {
10300   switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10301 }
10302
10303 static void
10304 quick_bookmark_handler (GtkFileChooserDefault *impl,
10305                         gint bookmark_index)
10306 {
10307   int bookmark_pos;
10308   GtkTreePath *path;
10309
10310   if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10311     return;
10312
10313   bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10314
10315   path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10316   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10317                                 path, NULL,
10318                                 FALSE, 0.0, 0.0);
10319   gtk_tree_path_free (path);
10320
10321   switch_to_shortcut (impl, bookmark_pos);
10322 }
10323
10324 static void
10325 show_hidden_handler (GtkFileChooserDefault *impl)
10326 {
10327   g_object_set (impl,
10328                 "show-hidden", !impl->show_hidden,
10329                 NULL);
10330 }
10331
10332
10333 /* Drag and drop interfaces */
10334
10335 static void
10336 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10337 {
10338 }
10339
10340 static void
10341 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10342 {
10343   model->impl = NULL;
10344 }
10345
10346 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10347 static gboolean
10348 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10349                                            GtkTreePath       *path)
10350 {
10351   ShortcutsPaneModelFilter *model;
10352   int pos;
10353   int bookmarks_pos;
10354
10355   model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10356
10357   pos = *gtk_tree_path_get_indices (path);
10358   bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10359
10360   return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10361 }
10362
10363 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10364  * filter model
10365  */
10366 static gboolean
10367 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10368                                            GtkTreePath       *path,
10369                                            GtkSelectionData  *selection_data)
10370 {
10371   /* FIXME */
10372
10373   return FALSE;
10374 }
10375
10376 /* Fill the GtkTreeDragSourceIface vtable */
10377 static void
10378 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10379 {
10380   iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10381   iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10382 }
10383
10384 #if 0
10385 /* Fill the GtkTreeDragDestIface vtable */
10386 static void
10387 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10388 {
10389   iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10390   iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10391 }
10392 #endif
10393
10394 static GtkTreeModel *
10395 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10396                                  GtkTreeModel          *child_model,
10397                                  GtkTreePath           *root)
10398 {
10399   ShortcutsPaneModelFilter *model;
10400
10401   model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10402                         "child-model", child_model,
10403                         "virtual-root", root,
10404                         NULL);
10405
10406   model->impl = impl;
10407
10408   return GTK_TREE_MODEL (model);
10409 }
10410