]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
immodule: Fix context ID lookup on module with multiple contexts
[~andy/gtk] / gtk / gtkfilechooserbutton.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2
3 /* GTK+: gtkfilechooserbutton.c
4  * 
5  * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include <string.h>
30
31 #include "gtkintl.h"
32 #include "gtkbutton.h"
33 #include "gtkcelllayout.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkcellrendererpixbuf.h"
36 #include "gtkcombobox.h"
37 #include "gtkdnd.h"
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
40 #include "gtkimage.h"
41 #include "gtklabel.h"
42 #include "gtkliststore.h"
43 #include "gtkstock.h"
44 #include "gtktreemodelfilter.h"
45 #include "gtkseparator.h"
46 #include "gtkfilechooserdialog.h"
47 #include "gtkfilechooserprivate.h"
48 #include "gtkfilechooserutils.h"
49 #include "gtkmarshalers.h"
50
51 #include "gtkfilechooserbutton.h"
52
53 #include "gtkorientable.h"
54
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
58
59
60 /**
61  * SECTION:gtkfilechooserbutton
62  * @Short_description: A button to launch a file selection dialog
63  * @Title: GtkFileChooserButton
64  * @See_also:#GtkFileChooserDialog
65  *
66  * The #GtkFileChooserButton is a widget that lets the user select a
67  * file.  It implements the #GtkFileChooser interface.  Visually, it is a
68  * file name with a button to bring up a #GtkFileChooserDialog.
69  * The user can then use that dialog to change the file associated with
70  * that button.  This widget does not support setting the
71  * #GtkFileChooser:select-multiple property to %TRUE.
72  *
73  * <example>
74  * <title>Create a button to let the user select a file in /etc</title>
75  * <programlisting>
76  * {
77  *   GtkWidget *button;
78  *
79  *   button = gtk_file_chooser_button_new (_("Select a file"),
80  *                                         GTK_FILE_CHOOSER_ACTION_OPEN);
81  *   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
82  *                                        "/etc");
83  * }
84  * </programlisting>
85  * </example>
86  *
87  * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
88  * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
89  *
90  * <important>
91  * The #GtkFileChooserButton will ellipsize the label,
92  * and thus will thus request little horizontal space.  To give the button
93  * more space, you should call gtk_widget_get_preferred_size(),
94  * gtk_file_chooser_button_set_width_chars(), or pack the button in
95  * such a way that other interface elements give space to the widget.
96  * </important>
97  */
98
99
100 /* **************** *
101  *  Private Macros  *
102  * **************** */
103
104 #define DEFAULT_TITLE           N_("Select a File")
105 #define DESKTOP_DISPLAY_NAME    N_("Desktop")
106 #define FALLBACK_DISPLAY_NAME   N_("(None)")
107 #define FALLBACK_ICON_NAME      "stock_unknown"
108 #define FALLBACK_ICON_SIZE      16
109
110
111 /* ********************** *
112  *  Private Enumerations  *
113  * ********************** */
114
115 /* Property IDs */
116 enum
117 {
118   PROP_0,
119
120   PROP_DIALOG,
121   PROP_FOCUS_ON_CLICK,
122   PROP_TITLE,
123   PROP_WIDTH_CHARS
124 };
125
126 /* Signals */
127 enum
128 {
129   FILE_SET,
130   LAST_SIGNAL
131 };
132
133 /* TreeModel Columns */
134 enum
135 {
136   ICON_COLUMN,
137   DISPLAY_NAME_COLUMN,
138   TYPE_COLUMN,
139   DATA_COLUMN,
140   IS_FOLDER_COLUMN,
141   CANCELLABLE_COLUMN,
142   NUM_COLUMNS
143 };
144
145 /* TreeModel Row Types */
146 typedef enum
147 {
148   ROW_TYPE_SPECIAL,
149   ROW_TYPE_VOLUME,
150   ROW_TYPE_SHORTCUT,
151   ROW_TYPE_BOOKMARK_SEPARATOR,
152   ROW_TYPE_BOOKMARK,
153   ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
154   ROW_TYPE_CURRENT_FOLDER,
155   ROW_TYPE_OTHER_SEPARATOR,
156   ROW_TYPE_OTHER,
157
158   ROW_TYPE_INVALID = -1
159 }
160 RowType;
161
162
163 /* ******************** *
164  *  Private Structures  *
165  * ******************** */
166
167 struct _GtkFileChooserButtonPrivate
168 {
169   GtkWidget *dialog;
170   GtkWidget *button;
171   GtkWidget *image;
172   GtkWidget *label;
173   GtkWidget *combo_box;
174   GtkCellRenderer *icon_cell;
175   GtkCellRenderer *name_cell;
176
177   GtkTreeModel *model;
178   GtkTreeModel *filter_model;
179
180   GtkFileSystem *fs;
181   GFile *selection_while_inactive;
182   GFile *current_folder_while_inactive;
183
184   gulong combo_box_changed_id;
185   gulong dialog_file_activated_id;
186   gulong dialog_folder_changed_id;
187   gulong dialog_selection_changed_id;
188   gulong fs_volumes_changed_id;
189   gulong fs_bookmarks_changed_id;
190
191   GCancellable *dnd_select_folder_cancellable;
192   GCancellable *update_button_cancellable;
193   GSList *change_icon_theme_cancellables;
194
195   gint icon_size;
196
197   guint8 n_special;
198   guint8 n_volumes;
199   guint8 n_shortcuts;
200   guint8 n_bookmarks;
201   guint  has_bookmark_separator       : 1;
202   guint  has_current_folder_separator : 1;
203   guint  has_current_folder           : 1;
204   guint  has_other_separator          : 1;
205
206   /* Used for hiding/showing the dialog when the button is hidden */
207   guint  active                       : 1;
208
209   guint  focus_on_click               : 1;
210 };
211
212
213 /* ************* *
214  *  DnD Support  *
215  * ************* */
216
217 enum
218 {
219   TEXT_PLAIN,
220   TEXT_URI_LIST
221 };
222
223
224 /* ********************* *
225  *  Function Prototypes  *
226  * ********************* */
227
228 /* GtkFileChooserIface Functions */
229 static void     gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
230 static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser    *chooser,
231                                                             GFile             *file,
232                                                             GError           **error);
233 static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser    *chooser);
234 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
235                                                      GFile          *file,
236                                                      GError        **error);
237 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
238                                                    GFile          *file);
239 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
240 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
241 static gboolean gtk_file_chooser_button_add_shortcut_folder     (GtkFileChooser      *chooser,
242                                                                  GFile               *file,
243                                                                  GError             **error);
244 static gboolean gtk_file_chooser_button_remove_shortcut_folder  (GtkFileChooser      *chooser,
245                                                                  GFile               *file,
246                                                                  GError             **error);
247
248 /* GObject Functions */
249 static GObject *gtk_file_chooser_button_constructor        (GType             type,
250                                                             guint             n_params,
251                                                             GObjectConstructParam *params);
252 static void     gtk_file_chooser_button_set_property       (GObject          *object,
253                                                             guint             param_id,
254                                                             const GValue     *value,
255                                                             GParamSpec       *pspec);
256 static void     gtk_file_chooser_button_get_property       (GObject          *object,
257                                                             guint             param_id,
258                                                             GValue           *value,
259                                                             GParamSpec       *pspec);
260 static void     gtk_file_chooser_button_finalize           (GObject          *object);
261
262 /* GtkWidget Functions */
263 static void     gtk_file_chooser_button_destroy            (GtkWidget        *widget);
264 static void     gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
265                                                             GdkDragContext   *context,
266                                                             gint              x,
267                                                             gint              y,
268                                                             GtkSelectionData *data,
269                                                             guint             type,
270                                                             guint             drag_time);
271 static void     gtk_file_chooser_button_show_all           (GtkWidget        *widget);
272 static void     gtk_file_chooser_button_show               (GtkWidget        *widget);
273 static void     gtk_file_chooser_button_hide               (GtkWidget        *widget);
274 static void     gtk_file_chooser_button_map                (GtkWidget        *widget);
275 static gboolean gtk_file_chooser_button_mnemonic_activate  (GtkWidget        *widget,
276                                                             gboolean          group_cycling);
277 static void     gtk_file_chooser_button_style_updated      (GtkWidget        *widget);
278 static void     gtk_file_chooser_button_screen_changed     (GtkWidget        *widget,
279                                                             GdkScreen        *old_screen);
280
281 /* Utility Functions */
282 static GtkIconTheme *get_icon_theme               (GtkWidget            *widget);
283 static void          set_info_for_file_at_iter         (GtkFileChooserButton *fs,
284                                                         GFile                *file,
285                                                         GtkTreeIter          *iter);
286
287 static gint          model_get_type_position      (GtkFileChooserButton *button,
288                                                    RowType               row_type);
289 static void          model_free_row_data          (GtkFileChooserButton *button,
290                                                    GtkTreeIter          *iter);
291 static inline void   model_add_special            (GtkFileChooserButton *button);
292 static inline void   model_add_other              (GtkFileChooserButton *button);
293 static void          model_add_volumes            (GtkFileChooserButton *button,
294                                                    GSList               *volumes);
295 static void          model_add_bookmarks          (GtkFileChooserButton *button,
296                                                    GSList               *bookmarks);
297 static void          model_update_current_folder  (GtkFileChooserButton *button,
298                                                    GFile                *file);
299 static void          model_remove_rows            (GtkFileChooserButton *button,
300                                                    gint                  pos,
301                                                    gint                  n_rows);
302
303 static gboolean      filter_model_visible_func    (GtkTreeModel         *model,
304                                                    GtkTreeIter          *iter,
305                                                    gpointer              user_data);
306
307 static gboolean      combo_box_row_separator_func (GtkTreeModel         *model,
308                                                    GtkTreeIter          *iter,
309                                                    gpointer              user_data);
310 static void          name_cell_data_func          (GtkCellLayout        *layout,
311                                                    GtkCellRenderer      *cell,
312                                                    GtkTreeModel         *model,
313                                                    GtkTreeIter          *iter,
314                                                    gpointer              user_data);
315 static void          open_dialog                  (GtkFileChooserButton *button);
316 static void          update_combo_box             (GtkFileChooserButton *button);
317 static void          update_label_and_image       (GtkFileChooserButton *button);
318
319 /* Child Object Callbacks */
320 static void     fs_volumes_changed_cb            (GtkFileSystem  *fs,
321                                                   gpointer        user_data);
322 static void     fs_bookmarks_changed_cb          (GtkFileSystem  *fs,
323                                                   gpointer        user_data);
324
325 static void     combo_box_changed_cb             (GtkComboBox    *combo_box,
326                                                   gpointer        user_data);
327
328 static void     button_clicked_cb                (GtkButton      *real_button,
329                                                   gpointer        user_data);
330
331 static void     dialog_update_preview_cb         (GtkFileChooser *dialog,
332                                                   gpointer        user_data);
333 static void     dialog_selection_changed_cb      (GtkFileChooser *dialog,
334                                                   gpointer        user_data);
335 static void     dialog_file_activated_cb         (GtkFileChooser *dialog,
336                                                   gpointer        user_data);
337 static void     dialog_current_folder_changed_cb (GtkFileChooser *dialog,
338                                                   gpointer        user_data);
339 static void     dialog_notify_cb                 (GObject        *dialog,
340                                                   GParamSpec     *pspec,
341                                                   gpointer        user_data);
342 static gboolean dialog_delete_event_cb           (GtkWidget      *dialog,
343                                                   GdkEvent       *event,
344                                                   gpointer        user_data);
345 static void     dialog_response_cb               (GtkDialog      *dialog,
346                                                   gint            response,
347                                                   gpointer        user_data);
348
349 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
350
351 /* ******************* *
352  *  GType Declaration  *
353  * ******************* */
354
355 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
356     G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
357 })
358
359
360 /* ***************** *
361  *  GType Functions  *
362  * ***************** */
363
364 static void
365 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
366 {
367   GObjectClass *gobject_class;
368   GtkWidgetClass *widget_class;
369
370   gobject_class = G_OBJECT_CLASS (class);
371   widget_class = GTK_WIDGET_CLASS (class);
372
373   gobject_class->constructor = gtk_file_chooser_button_constructor;
374   gobject_class->set_property = gtk_file_chooser_button_set_property;
375   gobject_class->get_property = gtk_file_chooser_button_get_property;
376   gobject_class->finalize = gtk_file_chooser_button_finalize;
377
378   widget_class->destroy = gtk_file_chooser_button_destroy;
379   widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
380   widget_class->show_all = gtk_file_chooser_button_show_all;
381   widget_class->show = gtk_file_chooser_button_show;
382   widget_class->hide = gtk_file_chooser_button_hide;
383   widget_class->map = gtk_file_chooser_button_map;
384   widget_class->style_updated = gtk_file_chooser_button_style_updated;
385   widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
386   widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
387
388   /**
389    * GtkFileChooserButton::file-set:
390    * @widget: the object which received the signal.
391    *
392    * The ::file-set signal is emitted when the user selects a file.
393    *
394    * Note that this signal is only emitted when the <emphasis>user</emphasis>
395    * changes the file.
396    *
397    * Since: 2.12
398    */
399   file_chooser_button_signals[FILE_SET] =
400     g_signal_new (I_("file-set"),
401                   G_TYPE_FROM_CLASS (gobject_class),
402                   G_SIGNAL_RUN_FIRST,
403                   G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
404                   NULL, NULL,
405                   _gtk_marshal_VOID__VOID,
406                   G_TYPE_NONE, 0);
407
408   /**
409    * GtkFileChooserButton:dialog:
410    * 
411    * Instance of the #GtkFileChooserDialog associated with the button.
412    *
413    * Since: 2.6
414    */
415   g_object_class_install_property (gobject_class, PROP_DIALOG,
416                                    g_param_spec_object ("dialog",
417                                                         P_("Dialog"),
418                                                         P_("The file chooser dialog to use."),
419                                                         GTK_TYPE_FILE_CHOOSER,
420                                                         (GTK_PARAM_WRITABLE |
421                                                          G_PARAM_CONSTRUCT_ONLY)));
422
423   /**
424    * GtkFileChooserButton:focus-on-click:
425    * 
426    * Whether the #GtkFileChooserButton button grabs focus when it is clicked
427    * with the mouse.
428    *
429    * Since: 2.10
430    */
431   g_object_class_install_property (gobject_class,
432                                    PROP_FOCUS_ON_CLICK,
433                                    g_param_spec_boolean ("focus-on-click",
434                                                          P_("Focus on click"),
435                                                          P_("Whether the button grabs focus when it is clicked with the mouse"),
436                                                          TRUE,
437                                                          GTK_PARAM_READWRITE));
438   
439   /**
440    * GtkFileChooserButton:title:
441    * 
442    * Title to put on the #GtkFileChooserDialog associated with the button.
443    *
444    * Since: 2.6
445    */
446   g_object_class_install_property (gobject_class, PROP_TITLE,
447                                    g_param_spec_string ("title",
448                                                         P_("Title"),
449                                                         P_("The title of the file chooser dialog."),
450                                                         _(DEFAULT_TITLE),
451                                                         GTK_PARAM_READWRITE));
452
453   /**
454    * GtkFileChooserButton:width-chars:
455    * 
456    * The width of the entry and label inside the button, in characters.
457    *
458    * Since: 2.6
459    */
460   g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
461                                    g_param_spec_int ("width-chars",
462                                                      P_("Width In Characters"),
463                                                      P_("The desired width of the button widget, in characters."),
464                                                      -1, G_MAXINT, -1,
465                                                      GTK_PARAM_READWRITE));
466
467   _gtk_file_chooser_install_properties (gobject_class);
468
469   g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
470 }
471
472 static void
473 gtk_file_chooser_button_init (GtkFileChooserButton *button)
474 {
475   GtkFileChooserButtonPrivate *priv;
476   GtkWidget *box, *image, *sep;
477   GtkTargetList *target_list;
478
479   priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
480                                                      GTK_TYPE_FILE_CHOOSER_BUTTON,
481                                                      GtkFileChooserButtonPrivate);
482
483   priv->icon_size = FALLBACK_ICON_SIZE;
484   priv->focus_on_click = TRUE;
485
486   gtk_widget_push_composite_child ();
487
488   /* Button */
489   priv->button = gtk_button_new ();
490   g_signal_connect (priv->button, "clicked",
491                     G_CALLBACK (button_clicked_cb), button);
492   gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
493   gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
494   gtk_widget_show (priv->button);
495
496   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
497   gtk_container_add (GTK_CONTAINER (priv->button), box);
498   gtk_widget_show (box);
499
500   priv->image = gtk_image_new ();
501   gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
502   gtk_widget_show (priv->image);
503
504   priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
505   gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
506   gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
507   gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
508   gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
509   //gtk_container_add (GTK_CONTAINER (box), priv->label);
510   gtk_widget_show (priv->label);
511
512   sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
513   gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
514   gtk_widget_show (sep);
515
516   image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
517   gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
518   gtk_widget_show (image);
519
520   /* Combo Box */
521   /* Keep in sync with columns enum, line 88 */
522   priv->model =
523     GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
524                                         GDK_TYPE_PIXBUF, /* Icon */
525                                         G_TYPE_STRING,   /* Display Name */
526                                         G_TYPE_CHAR,     /* Row Type */
527                                         G_TYPE_POINTER   /* Volume || Path */,
528                                         G_TYPE_BOOLEAN   /* Is Folder? */,
529                                         G_TYPE_POINTER   /* cancellable */));
530
531   priv->combo_box = gtk_combo_box_new ();
532   priv->combo_box_changed_id =
533     g_signal_connect (priv->combo_box, "changed",
534                       G_CALLBACK (combo_box_changed_cb), button);
535   gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
536   gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
537
538   priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
539   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
540                               priv->icon_cell, FALSE);
541   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
542                                  priv->icon_cell, "pixbuf", ICON_COLUMN);
543
544   priv->name_cell = gtk_cell_renderer_text_new ();
545   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
546                               priv->name_cell, TRUE);
547   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
548                                  priv->name_cell, "text", DISPLAY_NAME_COLUMN);
549   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
550                                       priv->name_cell, name_cell_data_func,
551                                       NULL, NULL);
552
553   gtk_widget_pop_composite_child ();
554
555   /* DnD */
556   gtk_drag_dest_set (GTK_WIDGET (button),
557                      (GTK_DEST_DEFAULT_ALL),
558                      NULL, 0,
559                      GDK_ACTION_COPY);
560   target_list = gtk_target_list_new (NULL, 0);
561   gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
562   gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
563   gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
564   gtk_target_list_unref (target_list);
565 }
566
567
568 /* ******************************* *
569  *  GtkFileChooserIface Functions  *
570  * ******************************* */
571 static void
572 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
573 {
574   _gtk_file_chooser_delegate_iface_init (iface);
575
576   iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
577   iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
578   iface->select_file = gtk_file_chooser_button_select_file;
579   iface->unselect_file = gtk_file_chooser_button_unselect_file;
580   iface->unselect_all = gtk_file_chooser_button_unselect_all;
581   iface->get_files = gtk_file_chooser_button_get_files;
582   iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
583   iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
584 }
585
586 static gboolean
587 gtk_file_chooser_button_set_current_folder (GtkFileChooser    *chooser,
588                                             GFile             *file,
589                                             GError           **error)
590 {
591   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
592   GtkFileChooserButtonPrivate *priv = button->priv;
593   GtkFileChooser *delegate;
594
595   delegate = g_object_get_qdata (G_OBJECT (chooser),
596                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
597
598   if (priv->active)
599     return gtk_file_chooser_set_current_folder_file (delegate, file, error);
600   else
601     {
602       if (priv->current_folder_while_inactive)
603         g_object_unref (priv->current_folder_while_inactive);
604
605       priv->current_folder_while_inactive = g_object_ref (file);
606
607       g_signal_emit_by_name (button, "current-folder-changed");
608
609       return TRUE;
610     }
611 }
612
613 static GFile *
614 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
615 {
616   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
617   GtkFileChooserButtonPrivate *priv = button->priv;
618   GtkFileChooser *delegate;
619
620   delegate = g_object_get_qdata (G_OBJECT (chooser),
621                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
622
623   if (priv->active)
624     return gtk_file_chooser_get_current_folder_file (delegate);
625   else
626     {
627       if (priv->current_folder_while_inactive)
628         return g_object_ref (priv->current_folder_while_inactive);
629       else
630         return NULL;
631     }
632 }
633
634 static gboolean
635 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
636                                      GFile          *file,
637                                      GError        **error)
638 {
639   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
640   GtkFileChooserButtonPrivate *priv = button->priv;
641   GtkFileChooser *delegate;
642
643   delegate = g_object_get_qdata (G_OBJECT (chooser),
644                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
645
646   if (priv->active)
647     return gtk_file_chooser_select_file (delegate, file, error);
648   else
649     {
650       if (priv->selection_while_inactive)
651         g_object_unref (priv->selection_while_inactive);
652
653       priv->selection_while_inactive = g_object_ref (file);
654
655       g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
656
657       return TRUE;
658     }
659 }
660
661 static void
662 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
663                                        GFile          *file)
664 {
665   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
666   GtkFileChooserButtonPrivate *priv = button->priv;
667   GtkFileChooser *delegate;
668
669   delegate = g_object_get_qdata (G_OBJECT (chooser),
670                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
671
672   if (priv->active)
673     gtk_file_chooser_unselect_file (delegate, file);
674   else
675     {
676       if (g_file_equal (priv->selection_while_inactive, file))
677         {
678           if (priv->selection_while_inactive)
679             {
680               g_object_unref (priv->selection_while_inactive);
681               priv->selection_while_inactive = NULL;
682             }
683
684           g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
685         }
686     }
687 }
688
689 static void
690 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
691 {
692   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
693   GtkFileChooserButtonPrivate *priv = button->priv;
694   GtkFileChooser *delegate;
695
696   delegate = g_object_get_qdata (G_OBJECT (chooser),
697                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
698
699   if (priv->active)
700     gtk_file_chooser_unselect_all (delegate);
701   else
702     {
703       if (priv->selection_while_inactive)
704         {
705           g_object_unref (priv->selection_while_inactive);
706           priv->selection_while_inactive = NULL;
707         }
708
709       g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
710     }
711 }
712
713 static GSList *
714 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
715 {
716   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
717   GtkFileChooserButtonPrivate *priv = button->priv;
718   GtkFileChooser *delegate;
719
720   delegate = g_object_get_qdata (G_OBJECT (chooser),
721                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
722
723   if (priv->active)
724     return gtk_file_chooser_get_files (delegate);
725   else
726     {
727       GSList *result;
728
729       result = NULL;
730
731       if (priv->selection_while_inactive)
732         result = g_slist_prepend (NULL, g_object_ref (priv->selection_while_inactive));
733       else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
734         {
735           /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
736            * the current folder, since that is what GtkFileChooserDefault would do.
737            */
738           if (priv->current_folder_while_inactive)
739             result = g_slist_prepend (NULL, g_object_ref (priv->current_folder_while_inactive));
740         }
741
742       return result;
743     }
744 }
745
746 static gboolean
747 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser  *chooser,
748                                              GFile           *file,
749                                              GError         **error)
750 {
751   GtkFileChooser *delegate;
752   gboolean retval;
753
754   delegate = g_object_get_qdata (G_OBJECT (chooser),
755                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
756   retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
757
758   if (retval)
759     {
760       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
761       GtkFileChooserButtonPrivate *priv = button->priv;
762       GtkTreeIter iter;
763       gint pos;
764
765       pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
766       pos += priv->n_shortcuts;
767
768       gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
769       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
770                           ICON_COLUMN, NULL,
771                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
772                           TYPE_COLUMN, ROW_TYPE_SHORTCUT,
773                           DATA_COLUMN, g_object_ref (file),
774                           IS_FOLDER_COLUMN, FALSE,
775                           -1);
776       set_info_for_file_at_iter (button, file, &iter);
777       priv->n_shortcuts++;
778
779       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
780     }
781
782   return retval;
783 }
784
785 static gboolean
786 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser  *chooser,
787                                                 GFile           *file,
788                                                 GError         **error)
789 {
790   GtkFileChooser *delegate;
791   gboolean retval;
792
793   delegate = g_object_get_qdata (G_OBJECT (chooser),
794                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
795
796   retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
797
798   if (retval)
799     {
800       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
801       GtkFileChooserButtonPrivate *priv = button->priv;
802       GtkTreeIter iter;
803       gint pos;
804       gchar type;
805
806       pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
807       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
808
809       do
810         {
811           gpointer data;
812
813           gtk_tree_model_get (priv->model, &iter,
814                               TYPE_COLUMN, &type,
815                               DATA_COLUMN, &data,
816                               -1);
817
818           if (type == ROW_TYPE_SHORTCUT &&
819               data && g_file_equal (data, file))
820             {
821               model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
822               gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
823               priv->n_shortcuts--;
824               gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
825               update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
826               break;
827             }
828         }
829       while (type == ROW_TYPE_SHORTCUT &&
830              gtk_tree_model_iter_next (priv->model, &iter));
831     }
832
833   return retval;
834 }
835
836
837 /* ******************* *
838  *  GObject Functions  *
839  * ******************* */
840
841 static GObject *
842 gtk_file_chooser_button_constructor (GType                  type,
843                                      guint                  n_params,
844                                      GObjectConstructParam *params)
845 {
846   GObject *object;
847   GtkFileChooserButton *button;
848   GtkFileChooserButtonPrivate *priv;
849   GSList *list;
850
851   object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
852                                                                                n_params,
853                                                                                params);
854   button = GTK_FILE_CHOOSER_BUTTON (object);
855   priv = button->priv;
856
857   if (!priv->dialog)
858     {
859       priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
860                                                   GTK_FILE_CHOOSER_ACTION_OPEN,
861                                                   GTK_STOCK_CANCEL,
862                                                   GTK_RESPONSE_CANCEL,
863                                                   GTK_STOCK_OPEN,
864                                                   GTK_RESPONSE_ACCEPT,
865                                                   NULL);
866
867       gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
868                                        GTK_RESPONSE_ACCEPT);
869       gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
870                                                GTK_RESPONSE_ACCEPT,
871                                                GTK_RESPONSE_CANCEL,
872                                                -1);
873
874       gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
875     }
876   else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
877     {
878       gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
879     }
880
881   g_signal_connect (priv->dialog, "delete-event",
882                     G_CALLBACK (dialog_delete_event_cb), object);
883   g_signal_connect (priv->dialog, "response",
884                     G_CALLBACK (dialog_response_cb), object);
885
886   /* This is used, instead of the standard delegate, to ensure that signals are only
887    * delegated when the OK button is pressed. */
888   g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
889   priv->dialog_folder_changed_id =
890     g_signal_connect (priv->dialog, "current-folder-changed",
891                       G_CALLBACK (dialog_current_folder_changed_cb), object);
892   priv->dialog_file_activated_id =
893     g_signal_connect (priv->dialog, "file-activated",
894                       G_CALLBACK (dialog_file_activated_cb), object);
895   priv->dialog_selection_changed_id =
896     g_signal_connect (priv->dialog, "selection-changed",
897                       G_CALLBACK (dialog_selection_changed_cb), object);
898   g_signal_connect (priv->dialog, "update-preview",
899                     G_CALLBACK (dialog_update_preview_cb), object);
900   g_signal_connect (priv->dialog, "notify",
901                     G_CALLBACK (dialog_notify_cb), object);
902   g_object_add_weak_pointer (G_OBJECT (priv->dialog),
903                              (gpointer) (&priv->dialog));
904
905   priv->fs =
906     g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
907
908   model_add_special (button);
909
910   list = _gtk_file_system_list_volumes (priv->fs);
911   model_add_volumes (button, list);
912   g_slist_free (list);
913
914   list = _gtk_file_system_list_bookmarks (priv->fs);
915   model_add_bookmarks (button, list);
916   g_slist_foreach (list, (GFunc) g_object_unref, NULL);
917   g_slist_free (list);
918
919   model_add_other (button);
920
921   priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
922   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
923                                           filter_model_visible_func,
924                                           object, NULL);
925
926   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
927   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
928                                         combo_box_row_separator_func,
929                                         NULL, NULL);
930
931   /* set up the action for a user-provided dialog, this also updates
932    * the label, image and combobox
933    */
934   g_object_set (object, 
935                 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
936                 NULL);
937
938   priv->fs_volumes_changed_id =
939     g_signal_connect (priv->fs, "volumes-changed",
940                       G_CALLBACK (fs_volumes_changed_cb), object);
941   priv->fs_bookmarks_changed_id =
942     g_signal_connect (priv->fs, "bookmarks-changed",
943                       G_CALLBACK (fs_bookmarks_changed_cb), object);
944
945   return object;
946 }
947
948 static void
949 gtk_file_chooser_button_set_property (GObject      *object,
950                                       guint         param_id,
951                                       const GValue *value,
952                                       GParamSpec   *pspec)
953 {
954   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
955   GtkFileChooserButtonPrivate *priv = button->priv;
956
957   switch (param_id)
958     {
959     case PROP_DIALOG:
960       /* Construct-only */
961       priv->dialog = g_value_get_object (value);
962       break;
963     case PROP_FOCUS_ON_CLICK:
964       gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
965       break;
966     case PROP_WIDTH_CHARS:
967       gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
968                                                g_value_get_int (value));
969       break;
970     case GTK_FILE_CHOOSER_PROP_ACTION:
971       switch (g_value_get_enum (value))
972         {
973         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
974         case GTK_FILE_CHOOSER_ACTION_SAVE:
975           {
976             GEnumClass *eclass;
977             GEnumValue *eval;
978
979             eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
980             eval = g_enum_get_value (eclass, g_value_get_enum (value));
981             g_warning ("%s: Choosers of type `%s' do not support `%s'.",
982                        G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
983
984             g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
985           }
986           break;
987         }
988
989       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
990       update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
991       update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
992
993       switch (g_value_get_enum (value))
994         {
995         case GTK_FILE_CHOOSER_ACTION_OPEN:
996           gtk_widget_hide (priv->combo_box);
997           gtk_widget_show (priv->button);
998           break;
999         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1000           gtk_widget_hide (priv->button);
1001           gtk_widget_show (priv->combo_box);
1002           break;
1003         default:
1004           g_assert_not_reached ();
1005           break;
1006         }
1007       break;
1008
1009     case PROP_TITLE:
1010     case GTK_FILE_CHOOSER_PROP_FILTER:
1011     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1012     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1013     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1014     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1015     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1016     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1017     case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1018       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1019       break;
1020
1021     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1022       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1023       fs_volumes_changed_cb (priv->fs, button);
1024       fs_bookmarks_changed_cb (priv->fs, button);
1025       break;
1026
1027     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1028       g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1029                  G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1030       break;
1031     default:
1032       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1033       break;
1034     }
1035 }
1036
1037 static void
1038 gtk_file_chooser_button_get_property (GObject    *object,
1039                                       guint       param_id,
1040                                       GValue     *value,
1041                                       GParamSpec *pspec)
1042 {
1043   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1044   GtkFileChooserButtonPrivate *priv = button->priv;
1045
1046   switch (param_id)
1047     {
1048     case PROP_WIDTH_CHARS:
1049       g_value_set_int (value,
1050                        gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1051       break;
1052     case PROP_FOCUS_ON_CLICK:
1053       g_value_set_boolean (value,
1054                            gtk_file_chooser_button_get_focus_on_click (button));
1055       break;
1056
1057     case PROP_TITLE:
1058     case GTK_FILE_CHOOSER_PROP_ACTION:
1059     case GTK_FILE_CHOOSER_PROP_FILTER:
1060     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1061     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1062     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1063     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1064     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1065     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1066     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1067     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1068     case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1069       g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1070       break;
1071
1072     default:
1073       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1074       break;
1075     }
1076 }
1077
1078 static void
1079 gtk_file_chooser_button_finalize (GObject *object)
1080 {
1081   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1082   GtkFileChooserButtonPrivate *priv = button->priv;
1083
1084   if (priv->selection_while_inactive)
1085     g_object_unref (priv->selection_while_inactive);
1086
1087   if (priv->current_folder_while_inactive)
1088     g_object_unref (priv->current_folder_while_inactive);
1089
1090   G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1091 }
1092
1093 /* ********************* *
1094  *  GtkWidget Functions  *
1095  * ********************* */
1096
1097 static void
1098 gtk_file_chooser_button_destroy (GtkWidget *widget)
1099 {
1100   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1101   GtkFileChooserButtonPrivate *priv = button->priv;
1102   GtkTreeIter iter;
1103   GSList *l;
1104
1105   if (priv->dialog != NULL)
1106     {
1107       gtk_widget_destroy (priv->dialog);
1108       priv->dialog = NULL;
1109     }
1110
1111   if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1112     {
1113       model_free_row_data (button, &iter);
1114     }
1115   while (gtk_tree_model_iter_next (priv->model, &iter));
1116
1117   if (priv->dnd_select_folder_cancellable)
1118     {
1119       g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1120       priv->dnd_select_folder_cancellable = NULL;
1121     }
1122
1123   if (priv->update_button_cancellable)
1124     {
1125       g_cancellable_cancel (priv->update_button_cancellable);
1126       priv->update_button_cancellable = NULL;
1127     }
1128
1129   if (priv->change_icon_theme_cancellables)
1130     {
1131       for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1132         {
1133           GCancellable *cancellable = G_CANCELLABLE (l->data);
1134           g_cancellable_cancel (cancellable);
1135         }
1136       g_slist_free (priv->change_icon_theme_cancellables);
1137       priv->change_icon_theme_cancellables = NULL;
1138     }
1139
1140   if (priv->model)
1141     {
1142       g_object_unref (priv->model);
1143       priv->model = NULL;
1144     }
1145
1146   if (priv->filter_model)
1147     {
1148       g_object_unref (priv->filter_model);
1149       priv->filter_model = NULL;
1150     }
1151
1152   if (priv->fs)
1153     {
1154       g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1155       g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1156       g_object_unref (priv->fs);
1157       priv->fs = NULL;
1158     }
1159
1160   GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1161 }
1162
1163 struct DndSelectFolderData
1164 {
1165   GtkFileSystem *file_system;
1166   GtkFileChooserButton *button;
1167   GtkFileChooserAction action;
1168   GFile *file;
1169   gchar **uris;
1170   guint i;
1171   gboolean selected;
1172 };
1173
1174 static void
1175 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1176                                GFileInfo    *info,
1177                                const GError *error,
1178                                gpointer      user_data)
1179 {
1180   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1181   struct DndSelectFolderData *data = user_data;
1182
1183   if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1184     {
1185       g_object_unref (data->button);
1186       g_object_unref (data->file);
1187       g_strfreev (data->uris);
1188       g_free (data);
1189
1190       g_object_unref (cancellable);
1191       return;
1192     }
1193
1194   data->button->priv->dnd_select_folder_cancellable = NULL;
1195
1196   if (!cancelled && !error && info != NULL)
1197     {
1198       gboolean is_folder;
1199
1200       is_folder = _gtk_file_info_consider_as_directory (info);
1201
1202       data->selected =
1203         (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1204           (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1205          gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1206                                        data->file, NULL));
1207     }
1208   else
1209     data->selected = FALSE;
1210
1211   if (data->selected || data->uris[++data->i] == NULL)
1212     {
1213       g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1214
1215       g_object_unref (data->button);
1216       g_object_unref (data->file);
1217       g_strfreev (data->uris);
1218       g_free (data);
1219
1220       g_object_unref (cancellable);
1221       return;
1222     }
1223
1224   if (data->file)
1225     g_object_unref (data->file);
1226
1227   data->file = g_file_new_for_uri (data->uris[data->i]);
1228
1229   data->button->priv->dnd_select_folder_cancellable =
1230     _gtk_file_system_get_info (data->file_system, data->file,
1231                                "standard::type",
1232                                dnd_select_folder_get_info_cb, user_data);
1233
1234   g_object_unref (cancellable);
1235 }
1236
1237 static void
1238 gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
1239                                             GdkDragContext   *context,
1240                                             gint              x,
1241                                             gint              y,
1242                                             GtkSelectionData *data,
1243                                             guint             type,
1244                                             guint             drag_time)
1245 {
1246   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1247   GtkFileChooserButtonPrivate *priv = button->priv;
1248   GFile *file;
1249   gchar *text;
1250
1251   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1252     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1253                                                                                  context,
1254                                                                                  x, y,
1255                                                                                  data, type,
1256                                                                                  drag_time);
1257
1258   if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1259     return;
1260
1261   switch (type)
1262     {
1263     case TEXT_URI_LIST:
1264       {
1265         gchar **uris;
1266         struct DndSelectFolderData *info;
1267
1268         uris = gtk_selection_data_get_uris (data);
1269         
1270         if (uris == NULL)
1271           break;
1272
1273         info = g_new0 (struct DndSelectFolderData, 1);
1274         info->button = g_object_ref (button);
1275         info->i = 0;
1276         info->uris = uris;
1277         info->selected = FALSE;
1278         info->file_system = priv->fs;
1279         g_object_get (priv->dialog, "action", &info->action, NULL);
1280
1281         info->file = g_file_new_for_uri (info->uris[info->i]);
1282
1283         if (priv->dnd_select_folder_cancellable)
1284           g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1285
1286         priv->dnd_select_folder_cancellable =
1287           _gtk_file_system_get_info (priv->fs, info->file,
1288                                      "standard::type",
1289                                      dnd_select_folder_get_info_cb, info);
1290       }
1291       break;
1292
1293     case TEXT_PLAIN:
1294       text = (char*) gtk_selection_data_get_text (data);
1295       file = g_file_new_for_uri (text);
1296       gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1297                                     NULL);
1298       g_object_unref (file);
1299       g_free (text);
1300       g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1301       break;
1302
1303     default:
1304       break;
1305     }
1306
1307   gtk_drag_finish (context, TRUE, FALSE, drag_time);
1308 }
1309
1310 static void
1311 gtk_file_chooser_button_show_all (GtkWidget *widget)
1312 {
1313   gtk_widget_show (widget);
1314 }
1315
1316 static void
1317 gtk_file_chooser_button_show (GtkWidget *widget)
1318 {
1319   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1320   GtkFileChooserButtonPrivate *priv = button->priv;
1321
1322   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1323     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1324
1325   if (priv->active)
1326     open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1327 }
1328
1329 static void
1330 gtk_file_chooser_button_hide (GtkWidget *widget)
1331 {
1332   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1333   GtkFileChooserButtonPrivate *priv = button->priv;
1334
1335   gtk_widget_hide (priv->dialog);
1336
1337   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1338     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1339 }
1340
1341 static void
1342 gtk_file_chooser_button_map (GtkWidget *widget)
1343 {
1344   GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1345 }
1346
1347 static gboolean
1348 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1349                                            gboolean   group_cycling)
1350 {
1351   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1352   GtkFileChooserButtonPrivate *priv = button->priv;
1353
1354   switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1355     {
1356     case GTK_FILE_CHOOSER_ACTION_OPEN:
1357       gtk_widget_grab_focus (priv->button);
1358       break;
1359     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1360       return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1361       break;
1362     default:
1363       g_assert_not_reached ();
1364       break;
1365     }
1366
1367   return TRUE;
1368 }
1369
1370 /* Changes the icons wherever it is needed */
1371 struct ChangeIconThemeData
1372 {
1373   GtkFileChooserButton *button;
1374   GtkTreeRowReference *row_ref;
1375 };
1376
1377 static void
1378 change_icon_theme_get_info_cb (GCancellable *cancellable,
1379                                GFileInfo    *info,
1380                                const GError *error,
1381                                gpointer      user_data)
1382 {
1383   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1384   GdkPixbuf *pixbuf;
1385   struct ChangeIconThemeData *data = user_data;
1386
1387   if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1388     goto out;
1389
1390   data->button->priv->change_icon_theme_cancellables =
1391     g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1392
1393   if (cancelled || error)
1394     goto out;
1395
1396   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1397
1398   if (pixbuf)
1399     {
1400       gint width = 0;
1401       GtkTreeIter iter;
1402       GtkTreePath *path;
1403
1404       width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1405
1406       path = gtk_tree_row_reference_get_path (data->row_ref);
1407       if (path) 
1408         {
1409           gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1410           gtk_tree_path_free (path);
1411
1412           gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1413                               ICON_COLUMN, pixbuf,
1414                               -1);
1415
1416           g_object_set (data->button->priv->icon_cell,
1417                         "width", width,
1418                         NULL);
1419         }
1420       g_object_unref (pixbuf);
1421     }
1422
1423 out:
1424   g_object_unref (data->button);
1425   gtk_tree_row_reference_free (data->row_ref);
1426   g_free (data);
1427
1428   g_object_unref (cancellable);
1429 }
1430
1431 static void
1432 change_icon_theme (GtkFileChooserButton *button)
1433 {
1434   GtkFileChooserButtonPrivate *priv = button->priv;
1435   GtkSettings *settings;
1436   GtkIconTheme *theme;
1437   GtkTreeIter iter;
1438   GSList *l;
1439   gint width = 0, height = 0;
1440
1441   for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1442     {
1443       GCancellable *cancellable = G_CANCELLABLE (l->data);
1444       g_cancellable_cancel (cancellable);
1445     }
1446   g_slist_free (button->priv->change_icon_theme_cancellables);
1447   button->priv->change_icon_theme_cancellables = NULL;
1448
1449   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1450
1451   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1452                                          &width, &height))
1453     priv->icon_size = MAX (width, height);
1454   else
1455     priv->icon_size = FALLBACK_ICON_SIZE;
1456
1457   update_label_and_image (button);
1458
1459   gtk_tree_model_get_iter_first (priv->model, &iter);
1460
1461   theme = get_icon_theme (GTK_WIDGET (button));
1462
1463   do
1464     {
1465       GdkPixbuf *pixbuf;
1466       gchar type;
1467       gpointer data;
1468
1469       type = ROW_TYPE_INVALID;
1470       gtk_tree_model_get (priv->model, &iter,
1471                           TYPE_COLUMN, &type,
1472                           DATA_COLUMN, &data,
1473                           -1);
1474
1475       switch (type)
1476         {
1477         case ROW_TYPE_SPECIAL:
1478         case ROW_TYPE_SHORTCUT:
1479         case ROW_TYPE_BOOKMARK:
1480         case ROW_TYPE_CURRENT_FOLDER:
1481           if (data)
1482             {
1483               if (g_file_is_native (G_FILE (data)))
1484                 {
1485                   GtkTreePath *path;
1486                   GCancellable *cancellable;
1487                   struct ChangeIconThemeData *info;               
1488                   
1489                   info = g_new0 (struct ChangeIconThemeData, 1);
1490                   info->button = g_object_ref (button);
1491                   path = gtk_tree_model_get_path (priv->model, &iter);
1492                   info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1493                   gtk_tree_path_free (path);
1494
1495                   cancellable =
1496                     _gtk_file_system_get_info (priv->fs, data,
1497                                                "standard::icon",
1498                                                change_icon_theme_get_info_cb,
1499                                                info);
1500                   button->priv->change_icon_theme_cancellables =
1501                     g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1502                   pixbuf = NULL;
1503                 }
1504               else
1505                 /* Don't call get_info for remote paths to avoid latency and
1506                  * auth dialogs.
1507                  * If we switch to a better bookmarks file format (XBEL), we
1508                  * should use mime info to get a better icon.
1509                  */
1510                 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1511                                                    priv->icon_size, 0, NULL);
1512             }
1513           else
1514             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1515                                                priv->icon_size, 0, NULL);
1516           break;
1517         case ROW_TYPE_VOLUME:
1518           if (data)
1519             pixbuf = _gtk_file_system_volume_render_icon (data,
1520                                                           GTK_WIDGET (button),
1521                                                           priv->icon_size,
1522                                                           NULL);
1523           else
1524             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1525                                                priv->icon_size, 0, NULL);
1526           break;
1527         default:
1528           continue;
1529           break;
1530         }
1531
1532       if (pixbuf)
1533         width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1534
1535       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1536                           ICON_COLUMN, pixbuf,
1537                           -1);
1538
1539       if (pixbuf)
1540         g_object_unref (pixbuf);
1541     }
1542   while (gtk_tree_model_iter_next (priv->model, &iter));
1543
1544   g_object_set (button->priv->icon_cell,
1545                 "width", width,
1546                 NULL);
1547 }
1548
1549 static void
1550 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1551 {
1552   GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1553
1554   if (gtk_widget_has_screen (widget))
1555     change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1556 }
1557
1558 static void
1559 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1560                                         GdkScreen *old_screen)
1561 {
1562   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1563     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1564                                                                              old_screen);
1565
1566   change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1567 }
1568
1569
1570 /* ******************* *
1571  *  Utility Functions  *
1572  * ******************* */
1573
1574 /* General */
1575 static GtkIconTheme *
1576 get_icon_theme (GtkWidget *widget)
1577 {
1578   if (gtk_widget_has_screen (widget))
1579     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1580
1581   return gtk_icon_theme_get_default ();
1582 }
1583
1584
1585 struct SetDisplayNameData
1586 {
1587   GtkFileChooserButton *button;
1588   char *label;
1589   GtkTreeRowReference *row_ref;
1590 };
1591
1592 static void
1593 set_info_get_info_cb (GCancellable *cancellable,
1594                       GFileInfo    *info,
1595                       const GError *error,
1596                       gpointer      callback_data)
1597 {
1598   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1599   GdkPixbuf *pixbuf;
1600   GtkTreePath *path;
1601   GtkTreeIter iter;
1602   GCancellable *model_cancellable = NULL;
1603   struct SetDisplayNameData *data = callback_data;
1604   gboolean is_folder;
1605
1606   if (!data->button->priv->model)
1607     /* button got destroyed */
1608     goto out;
1609
1610   path = gtk_tree_row_reference_get_path (data->row_ref);
1611   if (!path)
1612     /* Cancellable doesn't exist anymore in the model */
1613     goto out;
1614
1615   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1616   gtk_tree_path_free (path);
1617
1618   /* Validate the cancellable */
1619   gtk_tree_model_get (data->button->priv->model, &iter,
1620                       CANCELLABLE_COLUMN, &model_cancellable,
1621                       -1);
1622   if (cancellable != model_cancellable)
1623     goto out;
1624
1625   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1626                       CANCELLABLE_COLUMN, NULL,
1627                       -1);
1628
1629   if (cancelled || error)
1630     /* There was an error, leave the fallback name in there */
1631     goto out;
1632
1633   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1634
1635   if (!data->label)
1636     data->label = g_strdup (g_file_info_get_display_name (info));
1637
1638   is_folder = _gtk_file_info_consider_as_directory (info);
1639
1640   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1641                       ICON_COLUMN, pixbuf,
1642                       DISPLAY_NAME_COLUMN, data->label,
1643                       IS_FOLDER_COLUMN, is_folder,
1644                       -1);
1645
1646   if (pixbuf)
1647     g_object_unref (pixbuf);
1648
1649 out:
1650   g_object_unref (data->button);
1651   g_free (data->label);
1652   gtk_tree_row_reference_free (data->row_ref);
1653   g_free (data);
1654
1655   if (model_cancellable)
1656     g_object_unref (model_cancellable);
1657 }
1658
1659 static void
1660 set_info_for_file_at_iter (GtkFileChooserButton *button,
1661                            GFile                *file,
1662                            GtkTreeIter          *iter)
1663 {
1664   struct SetDisplayNameData *data;
1665   GtkTreePath *tree_path;
1666   GCancellable *cancellable;
1667
1668   data = g_new0 (struct SetDisplayNameData, 1);
1669   data->button = g_object_ref (button);
1670   data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1671
1672   tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1673   data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1674   gtk_tree_path_free (tree_path);
1675
1676   cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1677                                            "standard::type,standard::icon,standard::display-name",
1678                                            set_info_get_info_cb, data);
1679
1680   gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1681                       CANCELLABLE_COLUMN, cancellable,
1682                       -1);
1683 }
1684
1685 /* Shortcuts Model */
1686 static gint
1687 model_get_type_position (GtkFileChooserButton *button,
1688                          RowType               row_type)
1689 {
1690   gint retval = 0;
1691
1692   if (row_type == ROW_TYPE_SPECIAL)
1693     return retval;
1694
1695   retval += button->priv->n_special;
1696
1697   if (row_type == ROW_TYPE_VOLUME)
1698     return retval;
1699
1700   retval += button->priv->n_volumes;
1701
1702   if (row_type == ROW_TYPE_SHORTCUT)
1703     return retval;
1704
1705   retval += button->priv->n_shortcuts;
1706
1707   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1708     return retval;
1709
1710   retval += button->priv->has_bookmark_separator;
1711
1712   if (row_type == ROW_TYPE_BOOKMARK)
1713     return retval;
1714
1715   retval += button->priv->n_bookmarks;
1716
1717   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1718     return retval;
1719
1720   retval += button->priv->has_current_folder_separator;
1721
1722   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1723     return retval;
1724
1725   retval += button->priv->has_current_folder;
1726
1727   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1728     return retval;
1729
1730   retval += button->priv->has_other_separator;
1731
1732   if (row_type == ROW_TYPE_OTHER)
1733     return retval;
1734
1735   g_assert_not_reached ();
1736   return -1;
1737 }
1738
1739 static void
1740 model_free_row_data (GtkFileChooserButton *button,
1741                      GtkTreeIter          *iter)
1742 {
1743   gchar type;
1744   gpointer data;
1745   GCancellable *cancellable;
1746
1747   gtk_tree_model_get (button->priv->model, iter,
1748                       TYPE_COLUMN, &type,
1749                       DATA_COLUMN, &data,
1750                       CANCELLABLE_COLUMN, &cancellable,
1751                       -1);
1752
1753   if (cancellable)
1754     {
1755       g_cancellable_cancel (cancellable);
1756       g_object_unref (cancellable);
1757     }
1758
1759   switch (type)
1760     {
1761     case ROW_TYPE_SPECIAL:
1762     case ROW_TYPE_SHORTCUT:
1763     case ROW_TYPE_BOOKMARK:
1764     case ROW_TYPE_CURRENT_FOLDER:
1765       g_object_unref (data);
1766       break;
1767     case ROW_TYPE_VOLUME:
1768       _gtk_file_system_volume_unref (data);
1769       break;
1770     default:
1771       break;
1772     }
1773 }
1774
1775 static void
1776 model_add_special_get_info_cb (GCancellable *cancellable,
1777                                GFileInfo    *info,
1778                                const GError *error,
1779                                gpointer      user_data)
1780 {
1781   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1782   GtkTreeIter iter;
1783   GtkTreePath *path;
1784   GdkPixbuf *pixbuf;
1785   GCancellable *model_cancellable = NULL;
1786   struct ChangeIconThemeData *data = user_data;
1787   gchar *name;
1788
1789   if (!data->button->priv->model)
1790     /* button got destroyed */
1791     goto out;
1792
1793   path = gtk_tree_row_reference_get_path (data->row_ref);
1794   if (!path)
1795     /* Cancellable doesn't exist anymore in the model */
1796     goto out;
1797
1798   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1799   gtk_tree_path_free (path);
1800
1801   gtk_tree_model_get (data->button->priv->model, &iter,
1802                       CANCELLABLE_COLUMN, &model_cancellable,
1803                       -1);
1804   if (cancellable != model_cancellable)
1805     goto out;
1806
1807   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1808                       CANCELLABLE_COLUMN, NULL,
1809                       -1);
1810
1811   if (cancelled || error)
1812     goto out;
1813
1814   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1815
1816   if (pixbuf)
1817     {
1818       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1819                           ICON_COLUMN, pixbuf,
1820                           -1);
1821       g_object_unref (pixbuf);
1822     }
1823
1824   gtk_tree_model_get (data->button->priv->model, &iter,
1825                       DISPLAY_NAME_COLUMN, &name,
1826                       -1);
1827   if (!name)
1828     gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1829                         DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1830                         -1);
1831   g_free (name);
1832    
1833 out:
1834   g_object_unref (data->button);
1835   gtk_tree_row_reference_free (data->row_ref);
1836   g_free (data);
1837
1838   if (model_cancellable)
1839     g_object_unref (model_cancellable);
1840 }
1841
1842 static inline void
1843 model_add_special (GtkFileChooserButton *button)
1844 {
1845   const gchar *homedir;
1846   const gchar *desktopdir;
1847   GtkListStore *store;
1848   GtkTreeIter iter;
1849   GFile *file;
1850   gint pos;
1851
1852   store = GTK_LIST_STORE (button->priv->model);
1853   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1854
1855   homedir = g_get_home_dir ();
1856
1857   if (homedir)
1858     {
1859       GtkTreePath *tree_path;
1860       GCancellable *cancellable;
1861       struct ChangeIconThemeData *info;
1862
1863       file = g_file_new_for_path (homedir);
1864       gtk_list_store_insert (store, &iter, pos);
1865       pos++;
1866
1867       info = g_new0 (struct ChangeIconThemeData, 1);
1868       info->button = g_object_ref (button);
1869       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1870       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1871                                                   tree_path);
1872       gtk_tree_path_free (tree_path);
1873
1874       cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1875                                                "standard::icon,standard::display-name",
1876                                                model_add_special_get_info_cb, info);
1877
1878       gtk_list_store_set (store, &iter,
1879                           ICON_COLUMN, NULL,
1880                           DISPLAY_NAME_COLUMN, NULL,
1881                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1882                           DATA_COLUMN, file,
1883                           IS_FOLDER_COLUMN, TRUE,
1884                           CANCELLABLE_COLUMN, cancellable,
1885                           -1);
1886
1887       button->priv->n_special++;
1888     }
1889
1890   desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1891
1892   /* "To disable a directory, point it to the homedir."
1893    * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1894    **/
1895   if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1896     {
1897       GtkTreePath *tree_path;
1898       GCancellable *cancellable;
1899       struct ChangeIconThemeData *info;
1900
1901       file = g_file_new_for_path (desktopdir);
1902       gtk_list_store_insert (store, &iter, pos);
1903       pos++;
1904
1905       info = g_new0 (struct ChangeIconThemeData, 1);
1906       info->button = g_object_ref (button);
1907       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1908       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1909                                                   tree_path);
1910       gtk_tree_path_free (tree_path);
1911
1912       cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1913                                                "standard::icon,standard::display-name",
1914                                                model_add_special_get_info_cb, info);
1915
1916       gtk_list_store_set (store, &iter,
1917                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1918                           ICON_COLUMN, NULL,
1919                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1920                           DATA_COLUMN, file,
1921                           IS_FOLDER_COLUMN, TRUE,
1922                           CANCELLABLE_COLUMN, cancellable,
1923                           -1);
1924
1925       button->priv->n_special++;
1926     }
1927 }
1928
1929 static void
1930 model_add_volumes (GtkFileChooserButton *button,
1931                    GSList               *volumes)
1932 {
1933   GtkListStore *store;
1934   gint pos;
1935   gboolean local_only;
1936   GSList *l;
1937
1938   if (!volumes)
1939     return;
1940
1941   store = GTK_LIST_STORE (button->priv->model);
1942   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1943   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1944
1945   for (l = volumes; l; l = l->next)
1946     {
1947       GtkFileSystemVolume *volume;
1948       GtkTreeIter iter;
1949       GdkPixbuf *pixbuf;
1950       gchar *display_name;
1951
1952       volume = l->data;
1953
1954       if (local_only)
1955         {
1956           if (_gtk_file_system_volume_is_mounted (volume))
1957             {
1958               GFile *base_file;
1959
1960               base_file = _gtk_file_system_volume_get_root (volume);
1961               if (base_file != NULL)
1962                 {
1963                   if (!g_file_is_native (base_file))
1964                     {
1965                       g_object_unref (base_file);
1966                       continue;
1967                     }
1968                   else
1969                     g_object_unref (base_file);
1970                 }
1971             }
1972         }
1973
1974       pixbuf = _gtk_file_system_volume_render_icon (volume,
1975                                                     GTK_WIDGET (button),
1976                                                     button->priv->icon_size,
1977                                                     NULL);
1978       display_name = _gtk_file_system_volume_get_display_name (volume);
1979
1980       gtk_list_store_insert (store, &iter, pos);
1981       gtk_list_store_set (store, &iter,
1982                           ICON_COLUMN, pixbuf,
1983                           DISPLAY_NAME_COLUMN, display_name,
1984                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1985                           DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1986                           IS_FOLDER_COLUMN, TRUE,
1987                           -1);
1988
1989       if (pixbuf)
1990         g_object_unref (pixbuf);
1991       g_free (display_name);
1992
1993       button->priv->n_volumes++;
1994       pos++;
1995     }
1996 }
1997
1998 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1999
2000 static void
2001 model_add_bookmarks (GtkFileChooserButton *button,
2002                      GSList               *bookmarks)
2003 {
2004   GtkListStore *store;
2005   GtkTreeIter iter;
2006   gint pos;
2007   gboolean local_only;
2008   GSList *l;
2009
2010   if (!bookmarks)
2011     return;
2012
2013   store = GTK_LIST_STORE (button->priv->model);
2014   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2015   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2016
2017   for (l = bookmarks; l; l = l->next)
2018     {
2019       GFile *file;
2020
2021       file = l->data;
2022
2023       if (g_file_is_native (file))
2024         {
2025           gtk_list_store_insert (store, &iter, pos);
2026           gtk_list_store_set (store, &iter,
2027                               ICON_COLUMN, NULL,
2028                               DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2029                               TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2030                               DATA_COLUMN, g_object_ref (file),
2031                               IS_FOLDER_COLUMN, FALSE,
2032                               -1);
2033           set_info_for_file_at_iter (button, file, &iter);
2034         }
2035       else
2036         {
2037           gchar *label;
2038           GtkIconTheme *icon_theme;
2039           GdkPixbuf *pixbuf;
2040
2041           if (local_only)
2042             continue;
2043
2044           /* Don't call get_info for remote paths to avoid latency and
2045            * auth dialogs.
2046            * If we switch to a better bookmarks file format (XBEL), we
2047            * should use mime info to get a better icon.
2048            */
2049           label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2050           if (!label)
2051             label = _gtk_file_chooser_label_for_file (file);
2052
2053           icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2054           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote", 
2055                                              button->priv->icon_size, 0, NULL);
2056
2057           gtk_list_store_insert (store, &iter, pos);
2058           gtk_list_store_set (store, &iter,
2059                               ICON_COLUMN, pixbuf,
2060                               DISPLAY_NAME_COLUMN, label,
2061                               TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2062                               DATA_COLUMN, g_object_ref (file),
2063                               IS_FOLDER_COLUMN, TRUE,
2064                               -1);
2065
2066           g_free (label);
2067           g_object_unref (pixbuf);
2068         }
2069
2070       button->priv->n_bookmarks++;
2071       pos++;
2072     }
2073
2074   if (button->priv->n_bookmarks > 0 && 
2075       !button->priv->has_bookmark_separator)
2076     {
2077       pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2078
2079       gtk_list_store_insert (store, &iter, pos);
2080       gtk_list_store_set (store, &iter,
2081                           ICON_COLUMN, NULL,
2082                           DISPLAY_NAME_COLUMN, NULL,
2083                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2084                           DATA_COLUMN, NULL,
2085                           IS_FOLDER_COLUMN, FALSE,
2086                           -1);
2087       button->priv->has_bookmark_separator = TRUE;
2088     }
2089 }
2090
2091 static void
2092 model_update_current_folder (GtkFileChooserButton *button,
2093                              GFile                *file)
2094 {
2095   GtkListStore *store;
2096   GtkTreeIter iter;
2097   gint pos;
2098
2099   if (!file)
2100     return;
2101
2102   store = GTK_LIST_STORE (button->priv->model);
2103
2104   if (!button->priv->has_current_folder_separator)
2105     {
2106       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2107       gtk_list_store_insert (store, &iter, pos);
2108       gtk_list_store_set (store, &iter,
2109                           ICON_COLUMN, NULL,
2110                           DISPLAY_NAME_COLUMN, NULL,
2111                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2112                           DATA_COLUMN, NULL,
2113                           IS_FOLDER_COLUMN, FALSE,
2114                           -1);
2115       button->priv->has_current_folder_separator = TRUE;
2116     }
2117
2118   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2119   if (!button->priv->has_current_folder)
2120     {
2121       gtk_list_store_insert (store, &iter, pos);
2122       button->priv->has_current_folder = TRUE;
2123     }
2124   else
2125     {
2126       gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2127       model_free_row_data (button, &iter);
2128     }
2129
2130   if (g_file_is_native (file))
2131     {
2132       gtk_list_store_set (store, &iter,
2133                           ICON_COLUMN, NULL,
2134                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2135                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2136                           DATA_COLUMN, g_object_ref (file),
2137                           IS_FOLDER_COLUMN, FALSE,
2138                           -1);
2139       set_info_for_file_at_iter (button, file, &iter);
2140     }
2141   else
2142     {
2143       gchar *label;
2144       GtkIconTheme *icon_theme;
2145       GdkPixbuf *pixbuf;
2146
2147       /* Don't call get_info for remote paths to avoid latency and
2148        * auth dialogs.
2149        * If we switch to a better bookmarks file format (XBEL), we
2150        * should use mime info to get a better icon.
2151        */
2152       label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2153       if (!label)
2154         label = _gtk_file_chooser_label_for_file (file);
2155
2156       icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2157
2158       if (g_file_is_native (file))
2159           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder", 
2160                                              button->priv->icon_size, 0, NULL);
2161       else
2162           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote", 
2163                                              button->priv->icon_size, 0, NULL);
2164
2165       gtk_list_store_set (store, &iter,
2166                           ICON_COLUMN, pixbuf,
2167                           DISPLAY_NAME_COLUMN, label,
2168                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2169                           DATA_COLUMN, g_object_ref (file),
2170                           IS_FOLDER_COLUMN, TRUE,
2171                           -1);
2172
2173       g_free (label);
2174       g_object_unref (pixbuf);
2175     }
2176 }
2177
2178 static inline void
2179 model_add_other (GtkFileChooserButton *button)
2180 {
2181   GtkListStore *store;
2182   GtkTreeIter iter;
2183   gint pos;
2184   
2185   store = GTK_LIST_STORE (button->priv->model);
2186   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2187
2188   gtk_list_store_insert (store, &iter, pos);
2189   gtk_list_store_set (store, &iter,
2190                       ICON_COLUMN, NULL,
2191                       DISPLAY_NAME_COLUMN, NULL,
2192                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2193                       DATA_COLUMN, NULL,
2194                       IS_FOLDER_COLUMN, FALSE,
2195                       -1);
2196   button->priv->has_other_separator = TRUE;
2197   pos++;
2198
2199   gtk_list_store_insert (store, &iter, pos);
2200   gtk_list_store_set (store, &iter,
2201                       ICON_COLUMN, NULL,
2202                       DISPLAY_NAME_COLUMN, _("Other…"),
2203                       TYPE_COLUMN, ROW_TYPE_OTHER,
2204                       DATA_COLUMN, NULL,
2205                       IS_FOLDER_COLUMN, FALSE,
2206                       -1);
2207 }
2208
2209 static void
2210 model_remove_rows (GtkFileChooserButton *button,
2211                    gint                  pos,
2212                    gint                  n_rows)
2213 {
2214   GtkListStore *store;
2215
2216   if (!n_rows)
2217     return;
2218
2219   store = GTK_LIST_STORE (button->priv->model);
2220
2221   do
2222     {
2223       GtkTreeIter iter;
2224
2225       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2226         g_assert_not_reached ();
2227
2228       model_free_row_data (button, &iter);
2229       gtk_list_store_remove (store, &iter);
2230       n_rows--;
2231     }
2232   while (n_rows);
2233 }
2234
2235 /* Filter Model */
2236 static inline gboolean
2237 test_if_file_is_visible (GtkFileSystem *fs,
2238                          GFile         *file,
2239                          gboolean       local_only,
2240                          gboolean       is_folder)
2241 {
2242   if (!file)
2243     return FALSE;
2244
2245   if (local_only && !g_file_is_native (file))
2246     return FALSE;
2247
2248   if (!is_folder)
2249     return FALSE;
2250
2251   return TRUE;
2252 }
2253
2254 static gboolean
2255 filter_model_visible_func (GtkTreeModel *model,
2256                            GtkTreeIter  *iter,
2257                            gpointer      user_data)
2258 {
2259   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2260   GtkFileChooserButtonPrivate *priv = button->priv;
2261   gchar type;
2262   gpointer data;
2263   gboolean local_only, retval, is_folder;
2264
2265   type = ROW_TYPE_INVALID;
2266   data = NULL;
2267   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2268
2269   gtk_tree_model_get (model, iter,
2270                       TYPE_COLUMN, &type,
2271                       DATA_COLUMN, &data,
2272                       IS_FOLDER_COLUMN, &is_folder,
2273                       -1);
2274
2275   switch (type)
2276     {
2277     case ROW_TYPE_CURRENT_FOLDER:
2278       retval = TRUE;
2279       break;
2280     case ROW_TYPE_SPECIAL:
2281     case ROW_TYPE_SHORTCUT:
2282     case ROW_TYPE_BOOKMARK:
2283       retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2284       break;
2285     case ROW_TYPE_VOLUME:
2286       {
2287         retval = TRUE;
2288         if (local_only)
2289           {
2290             if (_gtk_file_system_volume_is_mounted (data))
2291               {
2292                 GFile *base_file;
2293
2294                 base_file = _gtk_file_system_volume_get_root (data);
2295
2296                 if (base_file)
2297                   {
2298                     if (!g_file_is_native (base_file))
2299                       retval = FALSE;
2300                     g_object_unref (base_file);
2301                   }
2302                 else
2303                   retval = FALSE;
2304               }
2305           }
2306       }
2307       break;
2308     default:
2309       retval = TRUE;
2310       break;
2311     }
2312
2313   return retval;
2314 }
2315
2316 /* Combo Box */
2317 static void
2318 name_cell_data_func (GtkCellLayout   *layout,
2319                      GtkCellRenderer *cell,
2320                      GtkTreeModel    *model,
2321                      GtkTreeIter     *iter,
2322                      gpointer         user_data)
2323 {
2324   gchar type;
2325
2326   type = 0;
2327   gtk_tree_model_get (model, iter,
2328                       TYPE_COLUMN, &type,
2329                       -1);
2330
2331   if (type == ROW_TYPE_CURRENT_FOLDER)
2332     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2333   else
2334     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2335 }
2336
2337 static gboolean
2338 combo_box_row_separator_func (GtkTreeModel *model,
2339                               GtkTreeIter  *iter,
2340                               gpointer      user_data)
2341 {
2342   gchar type = ROW_TYPE_INVALID;
2343
2344   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2345
2346   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2347           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2348           type == ROW_TYPE_OTHER_SEPARATOR);
2349 }                         
2350
2351 static void
2352 update_combo_box (GtkFileChooserButton *button)
2353 {
2354   GtkFileChooserButtonPrivate *priv = button->priv;
2355   GSList *files;
2356   GtkTreeIter iter;
2357   gboolean row_found;
2358
2359   gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2360
2361   files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2362
2363   row_found = FALSE;
2364
2365   do
2366     {
2367       gchar type;
2368       gpointer data;
2369
2370       type = ROW_TYPE_INVALID;
2371       data = NULL;
2372
2373       gtk_tree_model_get (priv->filter_model, &iter,
2374                           TYPE_COLUMN, &type,
2375                           DATA_COLUMN, &data,
2376                           -1);
2377     
2378       switch (type)
2379         {
2380         case ROW_TYPE_SPECIAL:
2381         case ROW_TYPE_SHORTCUT:
2382         case ROW_TYPE_BOOKMARK:
2383         case ROW_TYPE_CURRENT_FOLDER:
2384           row_found = (files &&
2385                        files->data &&
2386                        g_file_equal (data, files->data));
2387           break;
2388         case ROW_TYPE_VOLUME:
2389           {
2390             GFile *base_file;
2391
2392             base_file = _gtk_file_system_volume_get_root (data);
2393             if (base_file)
2394               {
2395                 row_found = (files &&
2396                              files->data &&
2397                              g_file_equal (base_file, files->data));
2398                 g_object_unref (base_file);
2399               }
2400           }
2401           break;
2402         default:
2403           row_found = FALSE;
2404           break;
2405         }
2406
2407       if (row_found)
2408         {
2409           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2410           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2411                                          &iter);
2412           g_signal_handler_unblock (priv->combo_box,
2413                                     priv->combo_box_changed_id);
2414         }
2415     }
2416   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2417
2418   /* If it hasn't been found already, update & select the current-folder row. */
2419   if (!row_found && files && files->data)
2420     {
2421       GtkTreeIter filter_iter;
2422       gint pos;
2423     
2424       model_update_current_folder (button, files->data);
2425       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2426
2427       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2428       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2429
2430       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2431                                                         &filter_iter, &iter);
2432
2433       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2434       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2435       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2436     }
2437
2438   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2439   g_slist_free (files);
2440 }
2441
2442 /* Button */
2443 static void
2444 update_label_get_info_cb (GCancellable *cancellable,
2445                           GFileInfo    *info,
2446                           const GError *error,
2447                           gpointer      data)
2448 {
2449   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2450   GdkPixbuf *pixbuf;
2451   GtkFileChooserButton *button = data;
2452   GtkFileChooserButtonPrivate *priv = button->priv;
2453
2454   if (cancellable != priv->update_button_cancellable)
2455     goto out;
2456
2457   priv->update_button_cancellable = NULL;
2458
2459   if (cancelled || error)
2460     goto out;
2461
2462   gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2463
2464   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2465
2466   if (!pixbuf)
2467     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2468                                        FALLBACK_ICON_NAME,
2469                                        priv->icon_size, 0, NULL);
2470
2471   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2472   if (pixbuf)
2473     g_object_unref (pixbuf);
2474
2475 out:
2476   g_object_unref (button);
2477   g_object_unref (cancellable);
2478 }
2479
2480 static void
2481 update_label_and_image (GtkFileChooserButton *button)
2482 {
2483   GtkFileChooserButtonPrivate *priv = button->priv;
2484   gchar *label_text;
2485   GSList *files;
2486
2487   files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2488   label_text = NULL;
2489
2490   if (priv->update_button_cancellable)
2491     {
2492       g_cancellable_cancel (priv->update_button_cancellable);
2493       priv->update_button_cancellable = NULL;
2494     }
2495
2496   if (files && files->data)
2497     {
2498       GFile *file;
2499       GtkFileSystemVolume *volume = NULL;
2500
2501       file = files->data;
2502
2503       volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2504       if (volume)
2505         {
2506           GFile *base_file;
2507
2508           base_file = _gtk_file_system_volume_get_root (volume);
2509           if (base_file && g_file_equal (base_file, file))
2510             {
2511               GdkPixbuf *pixbuf;
2512
2513               label_text = _gtk_file_system_volume_get_display_name (volume);
2514               pixbuf = _gtk_file_system_volume_render_icon (volume,
2515                                                             GTK_WIDGET (button),
2516                                                             priv->icon_size,
2517                                                             NULL);
2518               gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2519               if (pixbuf)
2520                 g_object_unref (pixbuf);
2521             }
2522
2523           if (base_file)
2524             g_object_unref (base_file);
2525
2526           _gtk_file_system_volume_unref (volume);
2527
2528           if (label_text)
2529             goto out;
2530         }
2531
2532       if (g_file_is_native (file))
2533         {
2534           priv->update_button_cancellable =
2535             _gtk_file_system_get_info (priv->fs, file,
2536                                        "standard::icon,standard::display-name",
2537                                        update_label_get_info_cb,
2538                                        g_object_ref (button));
2539         }
2540       else
2541         {
2542           GdkPixbuf *pixbuf;
2543
2544           label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2545           pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2546                                              "text-x-generic",
2547                                              priv->icon_size, 0, NULL);
2548           gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2549           if (pixbuf)
2550             g_object_unref (pixbuf);
2551         }
2552     }
2553 out:
2554   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2555   g_slist_free (files);
2556
2557   if (label_text)
2558     {
2559       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2560       g_free (label_text);
2561     }
2562   else
2563     {
2564       gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2565       gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2566     }
2567 }
2568
2569
2570 /* ************************ *
2571  *  Child Object Callbacks  *
2572  * ************************ */
2573
2574 /* File System */
2575 static void
2576 fs_volumes_changed_cb (GtkFileSystem *fs,
2577                        gpointer       user_data)
2578 {
2579   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2580   GtkFileChooserButtonPrivate *priv = button->priv;
2581   GSList *volumes;
2582
2583   model_remove_rows (user_data,
2584                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
2585                      priv->n_volumes);
2586
2587   priv->n_volumes = 0;
2588
2589   volumes = _gtk_file_system_list_volumes (fs);
2590   model_add_volumes (user_data, volumes);
2591   g_slist_free (volumes);
2592
2593   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2594
2595   update_label_and_image (user_data);
2596   update_combo_box (user_data);
2597 }
2598
2599 static void
2600 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2601                          gpointer       user_data)
2602 {
2603   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2604   GtkFileChooserButtonPrivate *priv = button->priv;
2605   GSList *bookmarks;
2606
2607   bookmarks = _gtk_file_system_list_bookmarks (fs);
2608   model_remove_rows (user_data,
2609                      model_get_type_position (user_data,
2610                                               ROW_TYPE_BOOKMARK_SEPARATOR),
2611                      (priv->n_bookmarks + priv->has_bookmark_separator));
2612   priv->has_bookmark_separator = FALSE;
2613   priv->n_bookmarks = 0;
2614   model_add_bookmarks (user_data, bookmarks);
2615   g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2616   g_slist_free (bookmarks);
2617
2618   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2619
2620   update_label_and_image (user_data);
2621   update_combo_box (user_data);
2622 }
2623
2624 static void
2625 save_inactive_state (GtkFileChooserButton *button)
2626 {
2627   GtkFileChooserButtonPrivate *priv = button->priv;
2628
2629   if (priv->current_folder_while_inactive)
2630     g_object_unref (priv->current_folder_while_inactive);
2631
2632   if (priv->selection_while_inactive)
2633     g_object_unref (priv->selection_while_inactive);
2634
2635   priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2636   priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2637 }
2638
2639 static void
2640 restore_inactive_state (GtkFileChooserButton *button)
2641 {
2642   GtkFileChooserButtonPrivate *priv = button->priv;
2643
2644   if (priv->current_folder_while_inactive)
2645     gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2646
2647   if (priv->selection_while_inactive)
2648     gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2649   else
2650     gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2651 }
2652
2653 /* Dialog */
2654 static void
2655 open_dialog (GtkFileChooserButton *button)
2656 {
2657   GtkFileChooserButtonPrivate *priv = button->priv;
2658
2659   /* Setup the dialog parent to be chooser button's toplevel, and be modal
2660      as needed. */
2661   if (!gtk_widget_get_visible (priv->dialog))
2662     {
2663       GtkWidget *toplevel;
2664
2665       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2666
2667       if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2668         {
2669           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2670             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2671                                           GTK_WINDOW (toplevel));
2672               
2673           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2674                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2675         }
2676     }
2677
2678   if (!priv->active)
2679     {
2680       g_signal_handler_block (priv->dialog,
2681                               priv->dialog_folder_changed_id);
2682       g_signal_handler_block (priv->dialog,
2683                               priv->dialog_file_activated_id);
2684       g_signal_handler_block (priv->dialog,
2685                               priv->dialog_selection_changed_id);
2686
2687       restore_inactive_state (button);
2688
2689       priv->active = TRUE;
2690     }
2691
2692   gtk_widget_set_sensitive (priv->combo_box, FALSE);
2693   gtk_window_present (GTK_WINDOW (priv->dialog));
2694 }
2695
2696 /* Combo Box */
2697 static void
2698 combo_box_changed_cb (GtkComboBox *combo_box,
2699                       gpointer     user_data)
2700 {
2701   GtkTreeIter iter;
2702
2703   if (gtk_combo_box_get_active_iter (combo_box, &iter))
2704     {
2705       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2706       GtkFileChooserButtonPrivate *priv = button->priv;
2707       gchar type;
2708       gpointer data;
2709
2710       type = ROW_TYPE_INVALID;
2711       data = NULL;
2712
2713       gtk_tree_model_get (priv->filter_model, &iter,
2714                           TYPE_COLUMN, &type,
2715                           DATA_COLUMN, &data,
2716                           -1);
2717
2718       switch (type)
2719         {
2720         case ROW_TYPE_SPECIAL:
2721         case ROW_TYPE_SHORTCUT:
2722         case ROW_TYPE_BOOKMARK:
2723         case ROW_TYPE_CURRENT_FOLDER:
2724           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2725           if (data)
2726             gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2727                                                       data, NULL);
2728           break;
2729         case ROW_TYPE_VOLUME:
2730           {
2731             GFile *base_file;
2732
2733             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2734             base_file = _gtk_file_system_volume_get_root (data);
2735             if (base_file)
2736               {
2737                 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2738                                                           base_file, NULL);
2739                 g_object_unref (base_file);
2740               }
2741           }
2742           break;
2743         case ROW_TYPE_OTHER:
2744           open_dialog (user_data);
2745           break;
2746         default:
2747           break;
2748         }
2749     }
2750 }
2751
2752 /* Button */
2753 static void
2754 button_clicked_cb (GtkButton *real_button,
2755                    gpointer   user_data)
2756 {
2757   open_dialog (user_data);
2758 }
2759
2760 /* Dialog */
2761 static void
2762 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2763                                   gpointer        user_data)
2764 {
2765   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2766
2767   g_signal_emit_by_name (button, "current-folder-changed");
2768 }
2769
2770 static void
2771 dialog_file_activated_cb (GtkFileChooser *dialog,
2772                           gpointer        user_data)
2773 {
2774   g_signal_emit_by_name (user_data, "file-activated");
2775 }
2776
2777 static void
2778 dialog_selection_changed_cb (GtkFileChooser *dialog,
2779                              gpointer        user_data)
2780 {
2781   update_label_and_image (user_data);
2782   update_combo_box (user_data);
2783   g_signal_emit_by_name (user_data, "selection-changed");
2784 }
2785
2786 static void
2787 dialog_update_preview_cb (GtkFileChooser *dialog,
2788                           gpointer        user_data)
2789 {
2790   g_signal_emit_by_name (user_data, "update-preview");
2791 }
2792
2793 static void
2794 dialog_notify_cb (GObject    *dialog,
2795                   GParamSpec *pspec,
2796                   gpointer    user_data)
2797 {
2798   gpointer iface;
2799
2800   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2801                                  GTK_TYPE_FILE_CHOOSER);
2802   if (g_object_interface_find_property (iface, pspec->name))
2803     g_object_notify (user_data, pspec->name);
2804
2805   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2806     {
2807       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2808       GtkFileChooserButtonPrivate *priv = button->priv;
2809
2810       if (priv->has_current_folder)
2811         {
2812           GtkTreeIter iter;
2813           gint pos;
2814           gpointer data;
2815
2816           pos = model_get_type_position (user_data,
2817                                          ROW_TYPE_CURRENT_FOLDER);
2818           gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2819
2820           data = NULL;
2821           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2822
2823           /* If the path isn't local but we're in local-only mode now, remove
2824            * the custom-folder row */
2825           if (data && g_file_is_native (G_FILE (data)) &&
2826               gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2827             {
2828               pos--;
2829               model_remove_rows (user_data, pos, 2);
2830             }
2831         }
2832
2833       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2834       update_combo_box (user_data);
2835     }
2836 }
2837
2838 static gboolean
2839 dialog_delete_event_cb (GtkWidget *dialog,
2840                         GdkEvent  *event,
2841                         gpointer   user_data)
2842 {
2843   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2844
2845   return TRUE;
2846 }
2847
2848 static void
2849 dialog_response_cb (GtkDialog *dialog,
2850                     gint       response,
2851                     gpointer   user_data)
2852 {
2853   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2854   GtkFileChooserButtonPrivate *priv = button->priv;
2855
2856   if (response == GTK_RESPONSE_ACCEPT ||
2857       response == GTK_RESPONSE_OK)
2858     {
2859       save_inactive_state (button);
2860
2861       g_signal_emit_by_name (button, "current-folder-changed");
2862       g_signal_emit_by_name (button, "selection-changed");
2863
2864       update_label_and_image (button);
2865       update_combo_box (button);
2866     }
2867   else
2868     {
2869       restore_inactive_state (button);
2870     }
2871
2872   if (priv->active)
2873     {
2874       g_signal_handler_unblock (priv->dialog,
2875                                 priv->dialog_folder_changed_id);
2876       g_signal_handler_unblock (priv->dialog,
2877                                 priv->dialog_file_activated_id);
2878       g_signal_handler_unblock (priv->dialog,
2879                                 priv->dialog_selection_changed_id);
2880       priv->active = FALSE;
2881     }
2882
2883   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2884   gtk_widget_hide (priv->dialog);
2885
2886   if (response == GTK_RESPONSE_ACCEPT ||
2887       response == GTK_RESPONSE_OK)
2888     g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2889 }
2890
2891
2892 /* ************************************************************************** *
2893  *  Public API                                                                *
2894  * ************************************************************************** */
2895
2896 /**
2897  * gtk_file_chooser_button_new:
2898  * @title: the title of the browse dialog.
2899  * @action: the open mode for the widget.
2900  * 
2901  * Creates a new file-selecting button widget.
2902  * 
2903  * Returns: a new button widget.
2904  * 
2905  * Since: 2.6
2906  **/
2907 GtkWidget *
2908 gtk_file_chooser_button_new (const gchar          *title,
2909                              GtkFileChooserAction  action)
2910 {
2911   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2912                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2913
2914   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2915                        "action", action,
2916                        "title", (title ? title : _(DEFAULT_TITLE)),
2917                        NULL);
2918 }
2919
2920 /**
2921  * gtk_file_chooser_button_new_with_dialog:
2922  * @dialog: the widget to use as dialog
2923  *
2924  * Creates a #GtkFileChooserButton widget which uses @dialog as its
2925  * file-picking window.
2926  *
2927  * Note that @dialog must be a #GtkDialog (or subclass) which
2928  * implements the #GtkFileChooser interface and must not have
2929  * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2930  *
2931  * Also note that the dialog needs to have its confirmative button
2932  * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2933  * order for the button to take over the file selected in the dialog.
2934  *
2935  * Returns: a new button widget.
2936  *
2937  * Since: 2.6
2938  **/
2939 GtkWidget *
2940 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2941 {
2942   g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2943
2944   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2945                        "dialog", dialog,
2946                        NULL);
2947 }
2948
2949 /**
2950  * gtk_file_chooser_button_set_title:
2951  * @button: the button widget to modify.
2952  * @title: the new browse dialog title.
2953  * 
2954  * Modifies the @title of the browse dialog used by @button.
2955  * 
2956  * Since: 2.6
2957  **/
2958 void
2959 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2960                                    const gchar          *title)
2961 {
2962   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2963
2964   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2965   g_object_notify (G_OBJECT (button), "title");
2966 }
2967
2968 /**
2969  * gtk_file_chooser_button_get_title:
2970  * @button: the button widget to examine.
2971  * 
2972  * Retrieves the title of the browse dialog used by @button. The returned value
2973  * should not be modified or freed.
2974  * 
2975  * Returns: a pointer to the browse dialog's title.
2976  * 
2977  * Since: 2.6
2978  **/
2979 const gchar *
2980 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2981 {
2982   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2983
2984   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2985 }
2986
2987 /**
2988  * gtk_file_chooser_button_get_width_chars:
2989  * @button: the button widget to examine.
2990  * 
2991  * Retrieves the width in characters of the @button widget's entry and/or label.
2992  * 
2993  * Returns: an integer width (in characters) that the button will use to size itself.
2994  * 
2995  * Since: 2.6
2996  **/
2997 gint
2998 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2999 {
3000   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
3001
3002   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
3003 }
3004
3005 /**
3006  * gtk_file_chooser_button_set_width_chars:
3007  * @button: the button widget to examine.
3008  * @n_chars: the new width, in characters.
3009  * 
3010  * Sets the width (in characters) that @button will use to @n_chars.
3011  * 
3012  * Since: 2.6
3013  **/
3014 void
3015 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3016                                          gint                  n_chars)
3017 {
3018   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3019
3020   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3021   g_object_notify (G_OBJECT (button), "width-chars");
3022 }
3023
3024 /**
3025  * gtk_file_chooser_button_set_focus_on_click:
3026  * @button: a #GtkFileChooserButton
3027  * @focus_on_click: whether the button grabs focus when clicked with the mouse
3028  * 
3029  * Sets whether the button will grab focus when it is clicked with the mouse.
3030  * Making mouse clicks not grab focus is useful in places like toolbars where
3031  * you don't want the keyboard focus removed from the main area of the
3032  * application.
3033  *
3034  * Since: 2.10
3035  **/
3036 void
3037 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3038                                             gboolean              focus_on_click)
3039 {
3040   GtkFileChooserButtonPrivate *priv;
3041
3042   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3043
3044   priv = button->priv;
3045
3046   focus_on_click = focus_on_click != FALSE;
3047
3048   if (priv->focus_on_click != focus_on_click)
3049     {
3050       priv->focus_on_click = focus_on_click;
3051       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3052       gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3053       
3054       g_object_notify (G_OBJECT (button), "focus-on-click");
3055     }
3056 }
3057
3058 /**
3059  * gtk_file_chooser_button_get_focus_on_click:
3060  * @button: a #GtkFileChooserButton
3061  * 
3062  * Returns whether the button grabs focus when it is clicked with the mouse.
3063  * See gtk_file_chooser_button_set_focus_on_click().
3064  *
3065  * Return value: %TRUE if the button grabs focus when it is clicked with
3066  *               the mouse.
3067  *
3068  * Since: 2.10
3069  **/
3070 gboolean
3071 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3072 {
3073   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3074   
3075   return button->priv->focus_on_click;
3076 }