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