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