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