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