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