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