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