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