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