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