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