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