]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
47f5240237cf6a800632ff9766e78d81051292af
[~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       return TRUE;
656     }
657 }
658
659 static void
660 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
661                                        GFile          *file)
662 {
663   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
664   GtkFileChooserButtonPrivate *priv = button->priv;
665   GtkFileChooser *delegate;
666
667   delegate = g_object_get_qdata (G_OBJECT (chooser),
668                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
669
670   if (priv->active)
671     gtk_file_chooser_unselect_file (delegate, file);
672   else
673     {
674       if (g_file_equal (priv->selection_while_inactive, file))
675         {
676           if (priv->selection_while_inactive)
677             {
678               g_object_unref (priv->selection_while_inactive);
679               priv->selection_while_inactive = NULL;
680             }
681         }
682     }
683 }
684
685 static void
686 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
687 {
688   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
689   GtkFileChooserButtonPrivate *priv = button->priv;
690   GtkFileChooser *delegate;
691
692   delegate = g_object_get_qdata (G_OBJECT (chooser),
693                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
694
695   if (priv->active)
696     gtk_file_chooser_unselect_all (delegate);
697   else
698     {
699       if (priv->selection_while_inactive)
700         {
701           g_object_unref (priv->selection_while_inactive);
702           priv->selection_while_inactive = NULL;
703         }
704     }
705 }
706
707 static GSList *
708 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
709 {
710   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
711   GtkFileChooserButtonPrivate *priv = button->priv;
712   GtkFileChooser *delegate;
713
714   delegate = g_object_get_qdata (G_OBJECT (chooser),
715                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
716
717   if (priv->active)
718     return gtk_file_chooser_get_files (delegate);
719   else
720     {
721       GSList *result;
722
723       result = NULL;
724
725       if (priv->selection_while_inactive)
726         result = g_slist_prepend (NULL, g_object_ref (priv->selection_while_inactive));
727       else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
728         {
729           /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
730            * the current folder, since that is what GtkFileChooserDefault would do.
731            */
732           if (priv->current_folder_while_inactive)
733             result = g_slist_prepend (NULL, g_object_ref (priv->current_folder_while_inactive));
734         }
735
736       return result;
737     }
738 }
739
740 static gboolean
741 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser  *chooser,
742                                              GFile           *file,
743                                              GError         **error)
744 {
745   GtkFileChooser *delegate;
746   gboolean retval;
747
748   delegate = g_object_get_qdata (G_OBJECT (chooser),
749                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
750   retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
751
752   if (retval)
753     {
754       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
755       GtkFileChooserButtonPrivate *priv = button->priv;
756       GtkTreeIter iter;
757       gint pos;
758
759       pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
760       pos += priv->n_shortcuts;
761
762       gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
763       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
764                           ICON_COLUMN, NULL,
765                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
766                           TYPE_COLUMN, ROW_TYPE_SHORTCUT,
767                           DATA_COLUMN, g_object_ref (file),
768                           IS_FOLDER_COLUMN, FALSE,
769                           -1);
770       set_info_for_file_at_iter (button, file, &iter);
771       priv->n_shortcuts++;
772
773       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
774     }
775
776   return retval;
777 }
778
779 static gboolean
780 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser  *chooser,
781                                                 GFile           *file,
782                                                 GError         **error)
783 {
784   GtkFileChooser *delegate;
785   gboolean retval;
786
787   delegate = g_object_get_qdata (G_OBJECT (chooser),
788                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
789
790   retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
791
792   if (retval)
793     {
794       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
795       GtkFileChooserButtonPrivate *priv = button->priv;
796       GtkTreeIter iter;
797       gint pos;
798       gchar type;
799
800       pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
801       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
802
803       do
804         {
805           gpointer data;
806
807           gtk_tree_model_get (priv->model, &iter,
808                               TYPE_COLUMN, &type,
809                               DATA_COLUMN, &data,
810                               -1);
811
812           if (type == ROW_TYPE_SHORTCUT &&
813               data && g_file_equal (data, file))
814             {
815               model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
816               gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
817               priv->n_shortcuts--;
818               gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
819               update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
820               break;
821             }
822         }
823       while (type == ROW_TYPE_SHORTCUT &&
824              gtk_tree_model_iter_next (priv->model, &iter));
825     }
826
827   return retval;
828 }
829
830
831 /* ******************* *
832  *  GObject Functions  *
833  * ******************* */
834
835 static GObject *
836 gtk_file_chooser_button_constructor (GType                  type,
837                                      guint                  n_params,
838                                      GObjectConstructParam *params)
839 {
840   GObject *object;
841   GtkFileChooserButton *button;
842   GtkFileChooserButtonPrivate *priv;
843   GSList *list;
844
845   object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
846                                                                                n_params,
847                                                                                params);
848   button = GTK_FILE_CHOOSER_BUTTON (object);
849   priv = button->priv;
850
851   if (!priv->dialog)
852     {
853       priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
854                                                   GTK_FILE_CHOOSER_ACTION_OPEN,
855                                                   GTK_STOCK_CANCEL,
856                                                   GTK_RESPONSE_CANCEL,
857                                                   GTK_STOCK_OPEN,
858                                                   GTK_RESPONSE_ACCEPT,
859                                                   NULL);
860
861       gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
862                                        GTK_RESPONSE_ACCEPT);
863       gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
864                                                GTK_RESPONSE_ACCEPT,
865                                                GTK_RESPONSE_CANCEL,
866                                                -1);
867
868       gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
869     }
870   else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
871     {
872       gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
873     }
874
875   g_signal_connect (priv->dialog, "delete-event",
876                     G_CALLBACK (dialog_delete_event_cb), object);
877   g_signal_connect (priv->dialog, "response",
878                     G_CALLBACK (dialog_response_cb), object);
879
880   /* This is used, instead of the standard delegate, to ensure that signals are only
881    * delegated when the OK button is pressed. */
882   g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
883   priv->dialog_folder_changed_id =
884     g_signal_connect (priv->dialog, "current-folder-changed",
885                       G_CALLBACK (dialog_current_folder_changed_cb), object);
886   priv->dialog_file_activated_id =
887     g_signal_connect (priv->dialog, "file-activated",
888                       G_CALLBACK (dialog_file_activated_cb), object);
889   priv->dialog_selection_changed_id =
890     g_signal_connect (priv->dialog, "selection-changed",
891                       G_CALLBACK (dialog_selection_changed_cb), object);
892   g_signal_connect (priv->dialog, "update-preview",
893                     G_CALLBACK (dialog_update_preview_cb), object);
894   g_signal_connect (priv->dialog, "notify",
895                     G_CALLBACK (dialog_notify_cb), object);
896   g_object_add_weak_pointer (G_OBJECT (priv->dialog),
897                              (gpointer) (&priv->dialog));
898
899   priv->fs =
900     g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
901
902   model_add_special (button);
903
904   list = _gtk_file_system_list_volumes (priv->fs);
905   model_add_volumes (button, list);
906   g_slist_free (list);
907
908   list = _gtk_file_system_list_bookmarks (priv->fs);
909   model_add_bookmarks (button, list);
910   g_slist_foreach (list, (GFunc) g_object_unref, NULL);
911   g_slist_free (list);
912
913   model_add_other (button);
914
915   priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
916   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
917                                           filter_model_visible_func,
918                                           object, NULL);
919
920   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
921   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
922                                         combo_box_row_separator_func,
923                                         NULL, NULL);
924
925   /* set up the action for a user-provided dialog, this also updates
926    * the label, image and combobox
927    */
928   g_object_set (object, 
929                 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
930                 NULL);
931
932   priv->fs_volumes_changed_id =
933     g_signal_connect (priv->fs, "volumes-changed",
934                       G_CALLBACK (fs_volumes_changed_cb), object);
935   priv->fs_bookmarks_changed_id =
936     g_signal_connect (priv->fs, "bookmarks-changed",
937                       G_CALLBACK (fs_bookmarks_changed_cb), object);
938
939   return object;
940 }
941
942 static void
943 gtk_file_chooser_button_set_property (GObject      *object,
944                                       guint         param_id,
945                                       const GValue *value,
946                                       GParamSpec   *pspec)
947 {
948   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
949   GtkFileChooserButtonPrivate *priv = button->priv;
950
951   switch (param_id)
952     {
953     case PROP_DIALOG:
954       /* Construct-only */
955       priv->dialog = g_value_get_object (value);
956       break;
957     case PROP_FOCUS_ON_CLICK:
958       gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
959       break;
960     case PROP_WIDTH_CHARS:
961       gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
962                                                g_value_get_int (value));
963       break;
964     case GTK_FILE_CHOOSER_PROP_ACTION:
965       switch (g_value_get_enum (value))
966         {
967         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
968         case GTK_FILE_CHOOSER_ACTION_SAVE:
969           {
970             GEnumClass *eclass;
971             GEnumValue *eval;
972
973             eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
974             eval = g_enum_get_value (eclass, g_value_get_enum (value));
975             g_warning ("%s: Choosers of type `%s' do not support `%s'.",
976                        G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
977
978             g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
979           }
980           break;
981         }
982
983       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
984       update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
985       update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
986
987       switch (g_value_get_enum (value))
988         {
989         case GTK_FILE_CHOOSER_ACTION_OPEN:
990           gtk_widget_hide (priv->combo_box);
991           gtk_widget_show (priv->button);
992           break;
993         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
994           gtk_widget_hide (priv->button);
995           gtk_widget_show (priv->combo_box);
996           break;
997         default:
998           g_assert_not_reached ();
999           break;
1000         }
1001       break;
1002
1003     case PROP_TITLE:
1004     case GTK_FILE_CHOOSER_PROP_FILTER:
1005     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1006     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1007     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1008     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1009     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1010     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1011     case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1012       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1013       break;
1014
1015     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1016       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1017       fs_volumes_changed_cb (priv->fs, button);
1018       fs_bookmarks_changed_cb (priv->fs, button);
1019       break;
1020
1021     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1022       g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1023                  G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1024       break;
1025     default:
1026       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1027       break;
1028     }
1029 }
1030
1031 static void
1032 gtk_file_chooser_button_get_property (GObject    *object,
1033                                       guint       param_id,
1034                                       GValue     *value,
1035                                       GParamSpec *pspec)
1036 {
1037   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1038   GtkFileChooserButtonPrivate *priv = button->priv;
1039
1040   switch (param_id)
1041     {
1042     case PROP_WIDTH_CHARS:
1043       g_value_set_int (value,
1044                        gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1045       break;
1046     case PROP_FOCUS_ON_CLICK:
1047       g_value_set_boolean (value,
1048                            gtk_file_chooser_button_get_focus_on_click (button));
1049       break;
1050
1051     case PROP_TITLE:
1052     case GTK_FILE_CHOOSER_PROP_ACTION:
1053     case GTK_FILE_CHOOSER_PROP_FILTER:
1054     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1055     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1056     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1057     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1058     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1059     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1060     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1061     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1062     case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1063       g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1064       break;
1065
1066     default:
1067       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1068       break;
1069     }
1070 }
1071
1072 static void
1073 gtk_file_chooser_button_finalize (GObject *object)
1074 {
1075   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1076   GtkFileChooserButtonPrivate *priv = button->priv;
1077
1078   if (priv->selection_while_inactive)
1079     g_object_unref (priv->selection_while_inactive);
1080
1081   if (priv->current_folder_while_inactive)
1082     g_object_unref (priv->current_folder_while_inactive);
1083
1084   G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1085 }
1086
1087 /* ********************* *
1088  *  GtkWidget Functions  *
1089  * ********************* */
1090
1091 static void
1092 gtk_file_chooser_button_destroy (GtkWidget *widget)
1093 {
1094   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1095   GtkFileChooserButtonPrivate *priv = button->priv;
1096   GtkTreeIter iter;
1097   GSList *l;
1098
1099   if (priv->dialog != NULL)
1100     {
1101       gtk_widget_destroy (priv->dialog);
1102       priv->dialog = NULL;
1103     }
1104
1105   if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1106     {
1107       model_free_row_data (button, &iter);
1108     }
1109   while (gtk_tree_model_iter_next (priv->model, &iter));
1110
1111   if (priv->dnd_select_folder_cancellable)
1112     {
1113       g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1114       priv->dnd_select_folder_cancellable = NULL;
1115     }
1116
1117   if (priv->update_button_cancellable)
1118     {
1119       g_cancellable_cancel (priv->update_button_cancellable);
1120       priv->update_button_cancellable = NULL;
1121     }
1122
1123   if (priv->change_icon_theme_cancellables)
1124     {
1125       for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1126         {
1127           GCancellable *cancellable = G_CANCELLABLE (l->data);
1128           g_cancellable_cancel (cancellable);
1129         }
1130       g_slist_free (priv->change_icon_theme_cancellables);
1131       priv->change_icon_theme_cancellables = NULL;
1132     }
1133
1134   if (priv->model)
1135     {
1136       g_object_unref (priv->model);
1137       priv->model = NULL;
1138     }
1139
1140   if (priv->filter_model)
1141     {
1142       g_object_unref (priv->filter_model);
1143       priv->filter_model = NULL;
1144     }
1145
1146   if (priv->fs)
1147     {
1148       g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1149       g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1150       g_object_unref (priv->fs);
1151       priv->fs = NULL;
1152     }
1153
1154   GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1155 }
1156
1157 struct DndSelectFolderData
1158 {
1159   GtkFileSystem *file_system;
1160   GtkFileChooserButton *button;
1161   GtkFileChooserAction action;
1162   GFile *file;
1163   gchar **uris;
1164   guint i;
1165   gboolean selected;
1166 };
1167
1168 static void
1169 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1170                                GFileInfo    *info,
1171                                const GError *error,
1172                                gpointer      user_data)
1173 {
1174   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1175   struct DndSelectFolderData *data = user_data;
1176
1177   if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1178     {
1179       g_object_unref (data->button);
1180       g_object_unref (data->file);
1181       g_strfreev (data->uris);
1182       g_free (data);
1183
1184       g_object_unref (cancellable);
1185       return;
1186     }
1187
1188   data->button->priv->dnd_select_folder_cancellable = NULL;
1189
1190   if (!cancelled && !error && info != NULL)
1191     {
1192       gboolean is_folder;
1193
1194       is_folder = _gtk_file_info_consider_as_directory (info);
1195
1196       data->selected =
1197         (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1198           (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1199          gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1200                                        data->file, NULL));
1201     }
1202   else
1203     data->selected = FALSE;
1204
1205   if (data->selected || data->uris[++data->i] == NULL)
1206     {
1207       g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1208
1209       g_object_unref (data->button);
1210       g_object_unref (data->file);
1211       g_strfreev (data->uris);
1212       g_free (data);
1213
1214       g_object_unref (cancellable);
1215       return;
1216     }
1217
1218   if (data->file)
1219     g_object_unref (data->file);
1220
1221   data->file = g_file_new_for_uri (data->uris[data->i]);
1222
1223   data->button->priv->dnd_select_folder_cancellable =
1224     _gtk_file_system_get_info (data->file_system, data->file,
1225                                "standard::type",
1226                                dnd_select_folder_get_info_cb, user_data);
1227
1228   g_object_unref (cancellable);
1229 }
1230
1231 static void
1232 gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
1233                                             GdkDragContext   *context,
1234                                             gint              x,
1235                                             gint              y,
1236                                             GtkSelectionData *data,
1237                                             guint             type,
1238                                             guint             drag_time)
1239 {
1240   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1241   GtkFileChooserButtonPrivate *priv = button->priv;
1242   GFile *file;
1243   gchar *text;
1244
1245   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1246     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1247                                                                                  context,
1248                                                                                  x, y,
1249                                                                                  data, type,
1250                                                                                  drag_time);
1251
1252   if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1253     return;
1254
1255   switch (type)
1256     {
1257     case TEXT_URI_LIST:
1258       {
1259         gchar **uris;
1260         struct DndSelectFolderData *info;
1261
1262         uris = gtk_selection_data_get_uris (data);
1263         
1264         if (uris == NULL)
1265           break;
1266
1267         info = g_new0 (struct DndSelectFolderData, 1);
1268         info->button = g_object_ref (button);
1269         info->i = 0;
1270         info->uris = uris;
1271         info->selected = FALSE;
1272         info->file_system = priv->fs;
1273         g_object_get (priv->dialog, "action", &info->action, NULL);
1274
1275         info->file = g_file_new_for_uri (info->uris[info->i]);
1276
1277         if (priv->dnd_select_folder_cancellable)
1278           g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1279
1280         priv->dnd_select_folder_cancellable =
1281           _gtk_file_system_get_info (priv->fs, info->file,
1282                                      "standard::type",
1283                                      dnd_select_folder_get_info_cb, info);
1284       }
1285       break;
1286
1287     case TEXT_PLAIN:
1288       text = (char*) gtk_selection_data_get_text (data);
1289       file = g_file_new_for_uri (text);
1290       gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1291                                     NULL);
1292       g_object_unref (file);
1293       g_free (text);
1294       g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1295       break;
1296
1297     default:
1298       break;
1299     }
1300
1301   gtk_drag_finish (context, TRUE, FALSE, drag_time);
1302 }
1303
1304 static void
1305 gtk_file_chooser_button_show_all (GtkWidget *widget)
1306 {
1307   gtk_widget_show (widget);
1308 }
1309
1310 static void
1311 gtk_file_chooser_button_show (GtkWidget *widget)
1312 {
1313   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1314   GtkFileChooserButtonPrivate *priv = button->priv;
1315
1316   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1317     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1318
1319   if (priv->active)
1320     open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1321 }
1322
1323 static void
1324 gtk_file_chooser_button_hide (GtkWidget *widget)
1325 {
1326   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1327   GtkFileChooserButtonPrivate *priv = button->priv;
1328
1329   gtk_widget_hide (priv->dialog);
1330
1331   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1332     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1333 }
1334
1335 static void
1336 gtk_file_chooser_button_map (GtkWidget *widget)
1337 {
1338   GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1339 }
1340
1341 static gboolean
1342 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1343                                            gboolean   group_cycling)
1344 {
1345   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1346   GtkFileChooserButtonPrivate *priv = button->priv;
1347
1348   switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1349     {
1350     case GTK_FILE_CHOOSER_ACTION_OPEN:
1351       gtk_widget_grab_focus (priv->button);
1352       break;
1353     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1354       return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1355       break;
1356     default:
1357       g_assert_not_reached ();
1358       break;
1359     }
1360
1361   return TRUE;
1362 }
1363
1364 /* Changes the icons wherever it is needed */
1365 struct ChangeIconThemeData
1366 {
1367   GtkFileChooserButton *button;
1368   GtkTreeRowReference *row_ref;
1369 };
1370
1371 static void
1372 change_icon_theme_get_info_cb (GCancellable *cancellable,
1373                                GFileInfo    *info,
1374                                const GError *error,
1375                                gpointer      user_data)
1376 {
1377   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1378   GdkPixbuf *pixbuf;
1379   struct ChangeIconThemeData *data = user_data;
1380
1381   if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1382     goto out;
1383
1384   data->button->priv->change_icon_theme_cancellables =
1385     g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1386
1387   if (cancelled || error)
1388     goto out;
1389
1390   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1391
1392   if (pixbuf)
1393     {
1394       gint width = 0;
1395       GtkTreeIter iter;
1396       GtkTreePath *path;
1397
1398       width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1399
1400       path = gtk_tree_row_reference_get_path (data->row_ref);
1401       if (path) 
1402         {
1403           gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1404           gtk_tree_path_free (path);
1405
1406           gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1407                               ICON_COLUMN, pixbuf,
1408                               -1);
1409
1410           g_object_set (data->button->priv->icon_cell,
1411                         "width", width,
1412                         NULL);
1413         }
1414       g_object_unref (pixbuf);
1415     }
1416
1417 out:
1418   g_object_unref (data->button);
1419   gtk_tree_row_reference_free (data->row_ref);
1420   g_free (data);
1421
1422   g_object_unref (cancellable);
1423 }
1424
1425 static void
1426 change_icon_theme (GtkFileChooserButton *button)
1427 {
1428   GtkFileChooserButtonPrivate *priv = button->priv;
1429   GtkSettings *settings;
1430   GtkIconTheme *theme;
1431   GtkTreeIter iter;
1432   GSList *l;
1433   gint width = 0, height = 0;
1434
1435   for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1436     {
1437       GCancellable *cancellable = G_CANCELLABLE (l->data);
1438       g_cancellable_cancel (cancellable);
1439     }
1440   g_slist_free (button->priv->change_icon_theme_cancellables);
1441   button->priv->change_icon_theme_cancellables = NULL;
1442
1443   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1444
1445   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1446                                          &width, &height))
1447     priv->icon_size = MAX (width, height);
1448   else
1449     priv->icon_size = FALLBACK_ICON_SIZE;
1450
1451   update_label_and_image (button);
1452
1453   gtk_tree_model_get_iter_first (priv->model, &iter);
1454
1455   theme = get_icon_theme (GTK_WIDGET (button));
1456
1457   do
1458     {
1459       GdkPixbuf *pixbuf;
1460       gchar type;
1461       gpointer data;
1462
1463       type = ROW_TYPE_INVALID;
1464       gtk_tree_model_get (priv->model, &iter,
1465                           TYPE_COLUMN, &type,
1466                           DATA_COLUMN, &data,
1467                           -1);
1468
1469       switch (type)
1470         {
1471         case ROW_TYPE_SPECIAL:
1472         case ROW_TYPE_SHORTCUT:
1473         case ROW_TYPE_BOOKMARK:
1474         case ROW_TYPE_CURRENT_FOLDER:
1475           if (data)
1476             {
1477               if (g_file_is_native (G_FILE (data)))
1478                 {
1479                   GtkTreePath *path;
1480                   GCancellable *cancellable;
1481                   struct ChangeIconThemeData *info;               
1482                   
1483                   info = g_new0 (struct ChangeIconThemeData, 1);
1484                   info->button = g_object_ref (button);
1485                   path = gtk_tree_model_get_path (priv->model, &iter);
1486                   info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1487                   gtk_tree_path_free (path);
1488
1489                   cancellable =
1490                     _gtk_file_system_get_info (priv->fs, data,
1491                                                "standard::icon",
1492                                                change_icon_theme_get_info_cb,
1493                                                info);
1494                   button->priv->change_icon_theme_cancellables =
1495                     g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1496                   pixbuf = NULL;
1497                 }
1498               else
1499                 /* Don't call get_info for remote paths to avoid latency and
1500                  * auth dialogs.
1501                  * If we switch to a better bookmarks file format (XBEL), we
1502                  * should use mime info to get a better icon.
1503                  */
1504                 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1505                                                    priv->icon_size, 0, NULL);
1506             }
1507           else
1508             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1509                                                priv->icon_size, 0, NULL);
1510           break;
1511         case ROW_TYPE_VOLUME:
1512           if (data)
1513             pixbuf = _gtk_file_system_volume_render_icon (data,
1514                                                           GTK_WIDGET (button),
1515                                                           priv->icon_size,
1516                                                           NULL);
1517           else
1518             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1519                                                priv->icon_size, 0, NULL);
1520           break;
1521         default:
1522           continue;
1523           break;
1524         }
1525
1526       if (pixbuf)
1527         width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1528
1529       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1530                           ICON_COLUMN, pixbuf,
1531                           -1);
1532
1533       if (pixbuf)
1534         g_object_unref (pixbuf);
1535     }
1536   while (gtk_tree_model_iter_next (priv->model, &iter));
1537
1538   g_object_set (button->priv->icon_cell,
1539                 "width", width,
1540                 NULL);
1541 }
1542
1543 static void
1544 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1545 {
1546   GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1547
1548   if (gtk_widget_has_screen (widget))
1549     change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1550 }
1551
1552 static void
1553 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1554                                         GdkScreen *old_screen)
1555 {
1556   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1557     GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1558                                                                              old_screen);
1559
1560   change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1561 }
1562
1563
1564 /* ******************* *
1565  *  Utility Functions  *
1566  * ******************* */
1567
1568 /* General */
1569 static GtkIconTheme *
1570 get_icon_theme (GtkWidget *widget)
1571 {
1572   if (gtk_widget_has_screen (widget))
1573     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1574
1575   return gtk_icon_theme_get_default ();
1576 }
1577
1578
1579 struct SetDisplayNameData
1580 {
1581   GtkFileChooserButton *button;
1582   char *label;
1583   GtkTreeRowReference *row_ref;
1584 };
1585
1586 static void
1587 set_info_get_info_cb (GCancellable *cancellable,
1588                       GFileInfo    *info,
1589                       const GError *error,
1590                       gpointer      callback_data)
1591 {
1592   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1593   GdkPixbuf *pixbuf;
1594   GtkTreePath *path;
1595   GtkTreeIter iter;
1596   GCancellable *model_cancellable = NULL;
1597   struct SetDisplayNameData *data = callback_data;
1598   gboolean is_folder;
1599
1600   if (!data->button->priv->model)
1601     /* button got destroyed */
1602     goto out;
1603
1604   path = gtk_tree_row_reference_get_path (data->row_ref);
1605   if (!path)
1606     /* Cancellable doesn't exist anymore in the model */
1607     goto out;
1608
1609   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1610   gtk_tree_path_free (path);
1611
1612   /* Validate the cancellable */
1613   gtk_tree_model_get (data->button->priv->model, &iter,
1614                       CANCELLABLE_COLUMN, &model_cancellable,
1615                       -1);
1616   if (cancellable != model_cancellable)
1617     goto out;
1618
1619   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1620                       CANCELLABLE_COLUMN, NULL,
1621                       -1);
1622
1623   if (cancelled || error)
1624     /* There was an error, leave the fallback name in there */
1625     goto out;
1626
1627   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1628
1629   if (!data->label)
1630     data->label = g_strdup (g_file_info_get_display_name (info));
1631
1632   is_folder = _gtk_file_info_consider_as_directory (info);
1633
1634   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1635                       ICON_COLUMN, pixbuf,
1636                       DISPLAY_NAME_COLUMN, data->label,
1637                       IS_FOLDER_COLUMN, is_folder,
1638                       -1);
1639
1640   if (pixbuf)
1641     g_object_unref (pixbuf);
1642
1643 out:
1644   g_object_unref (data->button);
1645   g_free (data->label);
1646   gtk_tree_row_reference_free (data->row_ref);
1647   g_free (data);
1648
1649   if (model_cancellable)
1650     g_object_unref (model_cancellable);
1651 }
1652
1653 static void
1654 set_info_for_file_at_iter (GtkFileChooserButton *button,
1655                            GFile                *file,
1656                            GtkTreeIter          *iter)
1657 {
1658   struct SetDisplayNameData *data;
1659   GtkTreePath *tree_path;
1660   GCancellable *cancellable;
1661
1662   data = g_new0 (struct SetDisplayNameData, 1);
1663   data->button = g_object_ref (button);
1664   data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1665
1666   tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1667   data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1668   gtk_tree_path_free (tree_path);
1669
1670   cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1671                                            "standard::type,standard::icon,standard::display-name",
1672                                            set_info_get_info_cb, data);
1673
1674   gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1675                       CANCELLABLE_COLUMN, cancellable,
1676                       -1);
1677 }
1678
1679 /* Shortcuts Model */
1680 static gint
1681 model_get_type_position (GtkFileChooserButton *button,
1682                          RowType               row_type)
1683 {
1684   gint retval = 0;
1685
1686   if (row_type == ROW_TYPE_SPECIAL)
1687     return retval;
1688
1689   retval += button->priv->n_special;
1690
1691   if (row_type == ROW_TYPE_VOLUME)
1692     return retval;
1693
1694   retval += button->priv->n_volumes;
1695
1696   if (row_type == ROW_TYPE_SHORTCUT)
1697     return retval;
1698
1699   retval += button->priv->n_shortcuts;
1700
1701   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1702     return retval;
1703
1704   retval += button->priv->has_bookmark_separator;
1705
1706   if (row_type == ROW_TYPE_BOOKMARK)
1707     return retval;
1708
1709   retval += button->priv->n_bookmarks;
1710
1711   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1712     return retval;
1713
1714   retval += button->priv->has_current_folder_separator;
1715
1716   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1717     return retval;
1718
1719   retval += button->priv->has_current_folder;
1720
1721   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1722     return retval;
1723
1724   retval += button->priv->has_other_separator;
1725
1726   if (row_type == ROW_TYPE_OTHER)
1727     return retval;
1728
1729   g_assert_not_reached ();
1730   return -1;
1731 }
1732
1733 static void
1734 model_free_row_data (GtkFileChooserButton *button,
1735                      GtkTreeIter          *iter)
1736 {
1737   gchar type;
1738   gpointer data;
1739   GCancellable *cancellable;
1740
1741   gtk_tree_model_get (button->priv->model, iter,
1742                       TYPE_COLUMN, &type,
1743                       DATA_COLUMN, &data,
1744                       CANCELLABLE_COLUMN, &cancellable,
1745                       -1);
1746
1747   if (cancellable)
1748     {
1749       g_cancellable_cancel (cancellable);
1750       g_object_unref (cancellable);
1751     }
1752
1753   switch (type)
1754     {
1755     case ROW_TYPE_SPECIAL:
1756     case ROW_TYPE_SHORTCUT:
1757     case ROW_TYPE_BOOKMARK:
1758     case ROW_TYPE_CURRENT_FOLDER:
1759       g_object_unref (data);
1760       break;
1761     case ROW_TYPE_VOLUME:
1762       _gtk_file_system_volume_unref (data);
1763       break;
1764     default:
1765       break;
1766     }
1767 }
1768
1769 static void
1770 model_add_special_get_info_cb (GCancellable *cancellable,
1771                                GFileInfo    *info,
1772                                const GError *error,
1773                                gpointer      user_data)
1774 {
1775   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1776   GtkTreeIter iter;
1777   GtkTreePath *path;
1778   GdkPixbuf *pixbuf;
1779   GCancellable *model_cancellable = NULL;
1780   struct ChangeIconThemeData *data = user_data;
1781   gchar *name;
1782
1783   if (!data->button->priv->model)
1784     /* button got destroyed */
1785     goto out;
1786
1787   path = gtk_tree_row_reference_get_path (data->row_ref);
1788   if (!path)
1789     /* Cancellable doesn't exist anymore in the model */
1790     goto out;
1791
1792   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1793   gtk_tree_path_free (path);
1794
1795   gtk_tree_model_get (data->button->priv->model, &iter,
1796                       CANCELLABLE_COLUMN, &model_cancellable,
1797                       -1);
1798   if (cancellable != model_cancellable)
1799     goto out;
1800
1801   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1802                       CANCELLABLE_COLUMN, NULL,
1803                       -1);
1804
1805   if (cancelled || error)
1806     goto out;
1807
1808   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1809
1810   if (pixbuf)
1811     {
1812       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1813                           ICON_COLUMN, pixbuf,
1814                           -1);
1815       g_object_unref (pixbuf);
1816     }
1817
1818   gtk_tree_model_get (data->button->priv->model, &iter,
1819                       DISPLAY_NAME_COLUMN, &name,
1820                       -1);
1821   if (!name)
1822     gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1823                         DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1824                         -1);
1825   g_free (name);
1826    
1827 out:
1828   g_object_unref (data->button);
1829   gtk_tree_row_reference_free (data->row_ref);
1830   g_free (data);
1831
1832   if (model_cancellable)
1833     g_object_unref (model_cancellable);
1834 }
1835
1836 static inline void
1837 model_add_special (GtkFileChooserButton *button)
1838 {
1839   const gchar *homedir;
1840   const gchar *desktopdir;
1841   GtkListStore *store;
1842   GtkTreeIter iter;
1843   GFile *file;
1844   gint pos;
1845
1846   store = GTK_LIST_STORE (button->priv->model);
1847   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1848
1849   homedir = g_get_home_dir ();
1850
1851   if (homedir)
1852     {
1853       GtkTreePath *tree_path;
1854       GCancellable *cancellable;
1855       struct ChangeIconThemeData *info;
1856
1857       file = g_file_new_for_path (homedir);
1858       gtk_list_store_insert (store, &iter, pos);
1859       pos++;
1860
1861       info = g_new0 (struct ChangeIconThemeData, 1);
1862       info->button = g_object_ref (button);
1863       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1864       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1865                                                   tree_path);
1866       gtk_tree_path_free (tree_path);
1867
1868       cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1869                                                "standard::icon,standard::display-name",
1870                                                model_add_special_get_info_cb, info);
1871
1872       gtk_list_store_set (store, &iter,
1873                           ICON_COLUMN, NULL,
1874                           DISPLAY_NAME_COLUMN, NULL,
1875                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1876                           DATA_COLUMN, file,
1877                           IS_FOLDER_COLUMN, TRUE,
1878                           CANCELLABLE_COLUMN, cancellable,
1879                           -1);
1880
1881       button->priv->n_special++;
1882     }
1883
1884   desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1885
1886   /* "To disable a directory, point it to the homedir."
1887    * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1888    **/
1889   if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1890     {
1891       GtkTreePath *tree_path;
1892       GCancellable *cancellable;
1893       struct ChangeIconThemeData *info;
1894
1895       file = g_file_new_for_path (desktopdir);
1896       gtk_list_store_insert (store, &iter, pos);
1897       pos++;
1898
1899       info = g_new0 (struct ChangeIconThemeData, 1);
1900       info->button = g_object_ref (button);
1901       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1902       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1903                                                   tree_path);
1904       gtk_tree_path_free (tree_path);
1905
1906       cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1907                                                "standard::icon,standard::display-name",
1908                                                model_add_special_get_info_cb, info);
1909
1910       gtk_list_store_set (store, &iter,
1911                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1912                           ICON_COLUMN, NULL,
1913                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1914                           DATA_COLUMN, file,
1915                           IS_FOLDER_COLUMN, TRUE,
1916                           CANCELLABLE_COLUMN, cancellable,
1917                           -1);
1918
1919       button->priv->n_special++;
1920     }
1921 }
1922
1923 static void
1924 model_add_volumes (GtkFileChooserButton *button,
1925                    GSList               *volumes)
1926 {
1927   GtkListStore *store;
1928   gint pos;
1929   gboolean local_only;
1930   GSList *l;
1931
1932   if (!volumes)
1933     return;
1934
1935   store = GTK_LIST_STORE (button->priv->model);
1936   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1937   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1938
1939   for (l = volumes; l; l = l->next)
1940     {
1941       GtkFileSystemVolume *volume;
1942       GtkTreeIter iter;
1943       GdkPixbuf *pixbuf;
1944       gchar *display_name;
1945
1946       volume = l->data;
1947
1948       if (local_only)
1949         {
1950           if (_gtk_file_system_volume_is_mounted (volume))
1951             {
1952               GFile *base_file;
1953
1954               base_file = _gtk_file_system_volume_get_root (volume);
1955               if (base_file != NULL)
1956                 {
1957                   if (!g_file_is_native (base_file))
1958                     {
1959                       g_object_unref (base_file);
1960                       continue;
1961                     }
1962                   else
1963                     g_object_unref (base_file);
1964                 }
1965             }
1966         }
1967
1968       pixbuf = _gtk_file_system_volume_render_icon (volume,
1969                                                     GTK_WIDGET (button),
1970                                                     button->priv->icon_size,
1971                                                     NULL);
1972       display_name = _gtk_file_system_volume_get_display_name (volume);
1973
1974       gtk_list_store_insert (store, &iter, pos);
1975       gtk_list_store_set (store, &iter,
1976                           ICON_COLUMN, pixbuf,
1977                           DISPLAY_NAME_COLUMN, display_name,
1978                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1979                           DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1980                           IS_FOLDER_COLUMN, TRUE,
1981                           -1);
1982
1983       if (pixbuf)
1984         g_object_unref (pixbuf);
1985       g_free (display_name);
1986
1987       button->priv->n_volumes++;
1988       pos++;
1989     }
1990 }
1991
1992 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1993
1994 static void
1995 model_add_bookmarks (GtkFileChooserButton *button,
1996                      GSList               *bookmarks)
1997 {
1998   GtkListStore *store;
1999   GtkTreeIter iter;
2000   gint pos;
2001   gboolean local_only;
2002   GSList *l;
2003
2004   if (!bookmarks)
2005     return;
2006
2007   store = GTK_LIST_STORE (button->priv->model);
2008   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2009   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2010
2011   for (l = bookmarks; l; l = l->next)
2012     {
2013       GFile *file;
2014
2015       file = l->data;
2016
2017       if (g_file_is_native (file))
2018         {
2019           gtk_list_store_insert (store, &iter, pos);
2020           gtk_list_store_set (store, &iter,
2021                               ICON_COLUMN, NULL,
2022                               DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2023                               TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2024                               DATA_COLUMN, g_object_ref (file),
2025                               IS_FOLDER_COLUMN, FALSE,
2026                               -1);
2027           set_info_for_file_at_iter (button, file, &iter);
2028         }
2029       else
2030         {
2031           gchar *label;
2032           GtkIconTheme *icon_theme;
2033           GdkPixbuf *pixbuf;
2034
2035           if (local_only)
2036             continue;
2037
2038           /* Don't call get_info for remote paths to avoid latency and
2039            * auth dialogs.
2040            * If we switch to a better bookmarks file format (XBEL), we
2041            * should use mime info to get a better icon.
2042            */
2043           label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2044           if (!label)
2045             label = _gtk_file_chooser_label_for_file (file);
2046
2047           icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2048           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote", 
2049                                              button->priv->icon_size, 0, NULL);
2050
2051           gtk_list_store_insert (store, &iter, pos);
2052           gtk_list_store_set (store, &iter,
2053                               ICON_COLUMN, pixbuf,
2054                               DISPLAY_NAME_COLUMN, label,
2055                               TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2056                               DATA_COLUMN, g_object_ref (file),
2057                               IS_FOLDER_COLUMN, TRUE,
2058                               -1);
2059
2060           g_free (label);
2061           g_object_unref (pixbuf);
2062         }
2063
2064       button->priv->n_bookmarks++;
2065       pos++;
2066     }
2067
2068   if (button->priv->n_bookmarks > 0 && 
2069       !button->priv->has_bookmark_separator)
2070     {
2071       pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2072
2073       gtk_list_store_insert (store, &iter, pos);
2074       gtk_list_store_set (store, &iter,
2075                           ICON_COLUMN, NULL,
2076                           DISPLAY_NAME_COLUMN, NULL,
2077                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2078                           DATA_COLUMN, NULL,
2079                           IS_FOLDER_COLUMN, FALSE,
2080                           -1);
2081       button->priv->has_bookmark_separator = TRUE;
2082     }
2083 }
2084
2085 static void
2086 model_update_current_folder (GtkFileChooserButton *button,
2087                              GFile                *file)
2088 {
2089   GtkListStore *store;
2090   GtkTreeIter iter;
2091   gint pos;
2092
2093   if (!file)
2094     return;
2095
2096   store = GTK_LIST_STORE (button->priv->model);
2097
2098   if (!button->priv->has_current_folder_separator)
2099     {
2100       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2101       gtk_list_store_insert (store, &iter, pos);
2102       gtk_list_store_set (store, &iter,
2103                           ICON_COLUMN, NULL,
2104                           DISPLAY_NAME_COLUMN, NULL,
2105                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2106                           DATA_COLUMN, NULL,
2107                           IS_FOLDER_COLUMN, FALSE,
2108                           -1);
2109       button->priv->has_current_folder_separator = TRUE;
2110     }
2111
2112   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2113   if (!button->priv->has_current_folder)
2114     {
2115       gtk_list_store_insert (store, &iter, pos);
2116       button->priv->has_current_folder = TRUE;
2117     }
2118   else
2119     {
2120       gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2121       model_free_row_data (button, &iter);
2122     }
2123
2124   if (g_file_is_native (file))
2125     {
2126       gtk_list_store_set (store, &iter,
2127                           ICON_COLUMN, NULL,
2128                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2129                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2130                           DATA_COLUMN, g_object_ref (file),
2131                           IS_FOLDER_COLUMN, FALSE,
2132                           -1);
2133       set_info_for_file_at_iter (button, file, &iter);
2134     }
2135   else
2136     {
2137       gchar *label;
2138       GtkIconTheme *icon_theme;
2139       GdkPixbuf *pixbuf;
2140
2141       /* Don't call get_info for remote paths to avoid latency and
2142        * auth dialogs.
2143        * If we switch to a better bookmarks file format (XBEL), we
2144        * should use mime info to get a better icon.
2145        */
2146       label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2147       if (!label)
2148         label = _gtk_file_chooser_label_for_file (file);
2149
2150       icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2151
2152       if (g_file_is_native (file))
2153           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder", 
2154                                              button->priv->icon_size, 0, NULL);
2155       else
2156           pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote", 
2157                                              button->priv->icon_size, 0, NULL);
2158
2159       gtk_list_store_set (store, &iter,
2160                           ICON_COLUMN, pixbuf,
2161                           DISPLAY_NAME_COLUMN, label,
2162                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2163                           DATA_COLUMN, g_object_ref (file),
2164                           IS_FOLDER_COLUMN, TRUE,
2165                           -1);
2166
2167       g_free (label);
2168       g_object_unref (pixbuf);
2169     }
2170 }
2171
2172 static inline void
2173 model_add_other (GtkFileChooserButton *button)
2174 {
2175   GtkListStore *store;
2176   GtkTreeIter iter;
2177   gint pos;
2178   
2179   store = GTK_LIST_STORE (button->priv->model);
2180   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2181
2182   gtk_list_store_insert (store, &iter, pos);
2183   gtk_list_store_set (store, &iter,
2184                       ICON_COLUMN, NULL,
2185                       DISPLAY_NAME_COLUMN, NULL,
2186                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2187                       DATA_COLUMN, NULL,
2188                       IS_FOLDER_COLUMN, FALSE,
2189                       -1);
2190   button->priv->has_other_separator = TRUE;
2191   pos++;
2192
2193   gtk_list_store_insert (store, &iter, pos);
2194   gtk_list_store_set (store, &iter,
2195                       ICON_COLUMN, NULL,
2196                       DISPLAY_NAME_COLUMN, _("Other…"),
2197                       TYPE_COLUMN, ROW_TYPE_OTHER,
2198                       DATA_COLUMN, NULL,
2199                       IS_FOLDER_COLUMN, FALSE,
2200                       -1);
2201 }
2202
2203 static void
2204 model_remove_rows (GtkFileChooserButton *button,
2205                    gint                  pos,
2206                    gint                  n_rows)
2207 {
2208   GtkListStore *store;
2209
2210   if (!n_rows)
2211     return;
2212
2213   store = GTK_LIST_STORE (button->priv->model);
2214
2215   do
2216     {
2217       GtkTreeIter iter;
2218
2219       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2220         g_assert_not_reached ();
2221
2222       model_free_row_data (button, &iter);
2223       gtk_list_store_remove (store, &iter);
2224       n_rows--;
2225     }
2226   while (n_rows);
2227 }
2228
2229 /* Filter Model */
2230 static inline gboolean
2231 test_if_file_is_visible (GtkFileSystem *fs,
2232                          GFile         *file,
2233                          gboolean       local_only,
2234                          gboolean       is_folder)
2235 {
2236   if (!file)
2237     return FALSE;
2238
2239   if (local_only && !g_file_is_native (file))
2240     return FALSE;
2241
2242   if (!is_folder)
2243     return FALSE;
2244
2245   return TRUE;
2246 }
2247
2248 static gboolean
2249 filter_model_visible_func (GtkTreeModel *model,
2250                            GtkTreeIter  *iter,
2251                            gpointer      user_data)
2252 {
2253   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2254   GtkFileChooserButtonPrivate *priv = button->priv;
2255   gchar type;
2256   gpointer data;
2257   gboolean local_only, retval, is_folder;
2258
2259   type = ROW_TYPE_INVALID;
2260   data = NULL;
2261   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2262
2263   gtk_tree_model_get (model, iter,
2264                       TYPE_COLUMN, &type,
2265                       DATA_COLUMN, &data,
2266                       IS_FOLDER_COLUMN, &is_folder,
2267                       -1);
2268
2269   switch (type)
2270     {
2271     case ROW_TYPE_CURRENT_FOLDER:
2272       retval = TRUE;
2273       break;
2274     case ROW_TYPE_SPECIAL:
2275     case ROW_TYPE_SHORTCUT:
2276     case ROW_TYPE_BOOKMARK:
2277       retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2278       break;
2279     case ROW_TYPE_VOLUME:
2280       {
2281         retval = TRUE;
2282         if (local_only)
2283           {
2284             if (_gtk_file_system_volume_is_mounted (data))
2285               {
2286                 GFile *base_file;
2287
2288                 base_file = _gtk_file_system_volume_get_root (data);
2289
2290                 if (base_file)
2291                   {
2292                     if (!g_file_is_native (base_file))
2293                       retval = FALSE;
2294                     g_object_unref (base_file);
2295                   }
2296                 else
2297                   retval = FALSE;
2298               }
2299           }
2300       }
2301       break;
2302     default:
2303       retval = TRUE;
2304       break;
2305     }
2306
2307   return retval;
2308 }
2309
2310 /* Combo Box */
2311 static void
2312 name_cell_data_func (GtkCellLayout   *layout,
2313                      GtkCellRenderer *cell,
2314                      GtkTreeModel    *model,
2315                      GtkTreeIter     *iter,
2316                      gpointer         user_data)
2317 {
2318   gchar type;
2319
2320   type = 0;
2321   gtk_tree_model_get (model, iter,
2322                       TYPE_COLUMN, &type,
2323                       -1);
2324
2325   if (type == ROW_TYPE_CURRENT_FOLDER)
2326     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2327   else
2328     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2329 }
2330
2331 static gboolean
2332 combo_box_row_separator_func (GtkTreeModel *model,
2333                               GtkTreeIter  *iter,
2334                               gpointer      user_data)
2335 {
2336   gchar type = ROW_TYPE_INVALID;
2337
2338   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2339
2340   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2341           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2342           type == ROW_TYPE_OTHER_SEPARATOR);
2343 }                         
2344
2345 static void
2346 update_combo_box (GtkFileChooserButton *button)
2347 {
2348   GtkFileChooserButtonPrivate *priv = button->priv;
2349   GSList *files;
2350   GtkTreeIter iter;
2351   gboolean row_found;
2352
2353   gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2354
2355   files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2356
2357   row_found = FALSE;
2358
2359   do
2360     {
2361       gchar type;
2362       gpointer data;
2363
2364       type = ROW_TYPE_INVALID;
2365       data = NULL;
2366
2367       gtk_tree_model_get (priv->filter_model, &iter,
2368                           TYPE_COLUMN, &type,
2369                           DATA_COLUMN, &data,
2370                           -1);
2371     
2372       switch (type)
2373         {
2374         case ROW_TYPE_SPECIAL:
2375         case ROW_TYPE_SHORTCUT:
2376         case ROW_TYPE_BOOKMARK:
2377         case ROW_TYPE_CURRENT_FOLDER:
2378           row_found = (files &&
2379                        files->data &&
2380                        g_file_equal (data, files->data));
2381           break;
2382         case ROW_TYPE_VOLUME:
2383           {
2384             GFile *base_file;
2385
2386             base_file = _gtk_file_system_volume_get_root (data);
2387             if (base_file)
2388               {
2389                 row_found = (files &&
2390                              files->data &&
2391                              g_file_equal (base_file, files->data));
2392                 g_object_unref (base_file);
2393               }
2394           }
2395           break;
2396         default:
2397           row_found = FALSE;
2398           break;
2399         }
2400
2401       if (row_found)
2402         {
2403           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2404           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2405                                          &iter);
2406           g_signal_handler_unblock (priv->combo_box,
2407                                     priv->combo_box_changed_id);
2408         }
2409     }
2410   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2411
2412   /* If it hasn't been found already, update & select the current-folder row. */
2413   if (!row_found && files && files->data)
2414     {
2415       GtkTreeIter filter_iter;
2416       gint pos;
2417     
2418       model_update_current_folder (button, files->data);
2419       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2420
2421       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2422       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2423
2424       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2425                                                         &filter_iter, &iter);
2426
2427       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2428       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2429       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2430     }
2431
2432   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2433   g_slist_free (files);
2434 }
2435
2436 /* Button */
2437 static void
2438 update_label_get_info_cb (GCancellable *cancellable,
2439                           GFileInfo    *info,
2440                           const GError *error,
2441                           gpointer      data)
2442 {
2443   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2444   GdkPixbuf *pixbuf;
2445   GtkFileChooserButton *button = data;
2446   GtkFileChooserButtonPrivate *priv = button->priv;
2447
2448   if (cancellable != priv->update_button_cancellable)
2449     goto out;
2450
2451   priv->update_button_cancellable = NULL;
2452
2453   if (cancelled || error)
2454     goto out;
2455
2456   gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2457
2458   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2459
2460   if (!pixbuf)
2461     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2462                                        FALLBACK_ICON_NAME,
2463                                        priv->icon_size, 0, NULL);
2464
2465   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2466   if (pixbuf)
2467     g_object_unref (pixbuf);
2468
2469 out:
2470   g_object_unref (button);
2471   g_object_unref (cancellable);
2472 }
2473
2474 static void
2475 update_label_and_image (GtkFileChooserButton *button)
2476 {
2477   GtkFileChooserButtonPrivate *priv = button->priv;
2478   gchar *label_text;
2479   GSList *files;
2480
2481   files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2482   label_text = NULL;
2483
2484   if (priv->update_button_cancellable)
2485     {
2486       g_cancellable_cancel (priv->update_button_cancellable);
2487       priv->update_button_cancellable = NULL;
2488     }
2489
2490   if (files && files->data)
2491     {
2492       GFile *file;
2493       GtkFileSystemVolume *volume = NULL;
2494
2495       file = files->data;
2496
2497       volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2498       if (volume)
2499         {
2500           GFile *base_file;
2501
2502           base_file = _gtk_file_system_volume_get_root (volume);
2503           if (base_file && g_file_equal (base_file, file))
2504             {
2505               GdkPixbuf *pixbuf;
2506
2507               label_text = _gtk_file_system_volume_get_display_name (volume);
2508               pixbuf = _gtk_file_system_volume_render_icon (volume,
2509                                                             GTK_WIDGET (button),
2510                                                             priv->icon_size,
2511                                                             NULL);
2512               gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2513               if (pixbuf)
2514                 g_object_unref (pixbuf);
2515             }
2516
2517           if (base_file)
2518             g_object_unref (base_file);
2519
2520           _gtk_file_system_volume_unref (volume);
2521
2522           if (label_text)
2523             goto out;
2524         }
2525
2526       if (g_file_is_native (file))
2527         {
2528           priv->update_button_cancellable =
2529             _gtk_file_system_get_info (priv->fs, file,
2530                                        "standard::icon,standard::display-name",
2531                                        update_label_get_info_cb,
2532                                        g_object_ref (button));
2533         }
2534       else
2535         {
2536           GdkPixbuf *pixbuf;
2537
2538           label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2539           pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2540                                              "text-x-generic",
2541                                              priv->icon_size, 0, NULL);
2542           gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2543           if (pixbuf)
2544             g_object_unref (pixbuf);
2545         }
2546     }
2547 out:
2548   g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2549   g_slist_free (files);
2550
2551   if (label_text)
2552     {
2553       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2554       g_free (label_text);
2555     }
2556   else
2557     {
2558       gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2559       gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2560     }
2561 }
2562
2563
2564 /* ************************ *
2565  *  Child Object Callbacks  *
2566  * ************************ */
2567
2568 /* File System */
2569 static void
2570 fs_volumes_changed_cb (GtkFileSystem *fs,
2571                        gpointer       user_data)
2572 {
2573   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2574   GtkFileChooserButtonPrivate *priv = button->priv;
2575   GSList *volumes;
2576
2577   model_remove_rows (user_data,
2578                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
2579                      priv->n_volumes);
2580
2581   priv->n_volumes = 0;
2582
2583   volumes = _gtk_file_system_list_volumes (fs);
2584   model_add_volumes (user_data, volumes);
2585   g_slist_free (volumes);
2586
2587   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2588
2589   update_label_and_image (user_data);
2590   update_combo_box (user_data);
2591 }
2592
2593 static void
2594 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2595                          gpointer       user_data)
2596 {
2597   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2598   GtkFileChooserButtonPrivate *priv = button->priv;
2599   GSList *bookmarks;
2600
2601   bookmarks = _gtk_file_system_list_bookmarks (fs);
2602   model_remove_rows (user_data,
2603                      model_get_type_position (user_data,
2604                                               ROW_TYPE_BOOKMARK_SEPARATOR),
2605                      (priv->n_bookmarks + priv->has_bookmark_separator));
2606   priv->has_bookmark_separator = FALSE;
2607   priv->n_bookmarks = 0;
2608   model_add_bookmarks (user_data, bookmarks);
2609   g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2610   g_slist_free (bookmarks);
2611
2612   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2613
2614   update_label_and_image (user_data);
2615   update_combo_box (user_data);
2616 }
2617
2618 static void
2619 save_inactive_state (GtkFileChooserButton *button)
2620 {
2621   GtkFileChooserButtonPrivate *priv = button->priv;
2622
2623   if (priv->current_folder_while_inactive)
2624     g_object_unref (priv->current_folder_while_inactive);
2625
2626   if (priv->selection_while_inactive)
2627     g_object_unref (priv->selection_while_inactive);
2628
2629   priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2630   priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2631 }
2632
2633 static void
2634 restore_inactive_state (GtkFileChooserButton *button)
2635 {
2636   GtkFileChooserButtonPrivate *priv = button->priv;
2637
2638   if (priv->current_folder_while_inactive)
2639     gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2640
2641   if (priv->selection_while_inactive)
2642     gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2643   else
2644     gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2645 }
2646
2647 /* Dialog */
2648 static void
2649 open_dialog (GtkFileChooserButton *button)
2650 {
2651   GtkFileChooserButtonPrivate *priv = button->priv;
2652
2653   /* Setup the dialog parent to be chooser button's toplevel, and be modal
2654      as needed. */
2655   if (!gtk_widget_get_visible (priv->dialog))
2656     {
2657       GtkWidget *toplevel;
2658
2659       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2660
2661       if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2662         {
2663           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2664             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2665                                           GTK_WINDOW (toplevel));
2666               
2667           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2668                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2669         }
2670     }
2671
2672   if (!priv->active)
2673     {
2674       g_signal_handler_block (priv->dialog,
2675                               priv->dialog_folder_changed_id);
2676       g_signal_handler_block (priv->dialog,
2677                               priv->dialog_file_activated_id);
2678       g_signal_handler_block (priv->dialog,
2679                               priv->dialog_selection_changed_id);
2680
2681       restore_inactive_state (button);
2682
2683       priv->active = TRUE;
2684     }
2685
2686   gtk_widget_set_sensitive (priv->combo_box, FALSE);
2687   gtk_window_present (GTK_WINDOW (priv->dialog));
2688 }
2689
2690 /* Combo Box */
2691 static void
2692 combo_box_changed_cb (GtkComboBox *combo_box,
2693                       gpointer     user_data)
2694 {
2695   GtkTreeIter iter;
2696
2697   if (gtk_combo_box_get_active_iter (combo_box, &iter))
2698     {
2699       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2700       GtkFileChooserButtonPrivate *priv = button->priv;
2701       gchar type;
2702       gpointer data;
2703
2704       type = ROW_TYPE_INVALID;
2705       data = NULL;
2706
2707       gtk_tree_model_get (priv->filter_model, &iter,
2708                           TYPE_COLUMN, &type,
2709                           DATA_COLUMN, &data,
2710                           -1);
2711
2712       switch (type)
2713         {
2714         case ROW_TYPE_SPECIAL:
2715         case ROW_TYPE_SHORTCUT:
2716         case ROW_TYPE_BOOKMARK:
2717         case ROW_TYPE_CURRENT_FOLDER:
2718           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2719           if (data)
2720             gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2721                                                       data, NULL);
2722           break;
2723         case ROW_TYPE_VOLUME:
2724           {
2725             GFile *base_file;
2726
2727             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2728             base_file = _gtk_file_system_volume_get_root (data);
2729             if (base_file)
2730               {
2731                 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2732                                                           base_file, NULL);
2733                 g_object_unref (base_file);
2734               }
2735           }
2736           break;
2737         case ROW_TYPE_OTHER:
2738           open_dialog (user_data);
2739           break;
2740         default:
2741           break;
2742         }
2743     }
2744 }
2745
2746 /* Button */
2747 static void
2748 button_clicked_cb (GtkButton *real_button,
2749                    gpointer   user_data)
2750 {
2751   open_dialog (user_data);
2752 }
2753
2754 /* Dialog */
2755 static void
2756 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2757                                   gpointer        user_data)
2758 {
2759   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2760
2761   g_signal_emit_by_name (button, "current-folder-changed");
2762 }
2763
2764 static void
2765 dialog_file_activated_cb (GtkFileChooser *dialog,
2766                           gpointer        user_data)
2767 {
2768   g_signal_emit_by_name (user_data, "file-activated");
2769 }
2770
2771 static void
2772 dialog_selection_changed_cb (GtkFileChooser *dialog,
2773                              gpointer        user_data)
2774 {
2775   update_label_and_image (user_data);
2776   update_combo_box (user_data);
2777   g_signal_emit_by_name (user_data, "selection-changed");
2778 }
2779
2780 static void
2781 dialog_update_preview_cb (GtkFileChooser *dialog,
2782                           gpointer        user_data)
2783 {
2784   g_signal_emit_by_name (user_data, "update-preview");
2785 }
2786
2787 static void
2788 dialog_notify_cb (GObject    *dialog,
2789                   GParamSpec *pspec,
2790                   gpointer    user_data)
2791 {
2792   gpointer iface;
2793
2794   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2795                                  GTK_TYPE_FILE_CHOOSER);
2796   if (g_object_interface_find_property (iface, pspec->name))
2797     g_object_notify (user_data, pspec->name);
2798
2799   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2800     {
2801       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2802       GtkFileChooserButtonPrivate *priv = button->priv;
2803
2804       if (priv->has_current_folder)
2805         {
2806           GtkTreeIter iter;
2807           gint pos;
2808           gpointer data;
2809
2810           pos = model_get_type_position (user_data,
2811                                          ROW_TYPE_CURRENT_FOLDER);
2812           gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2813
2814           data = NULL;
2815           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2816
2817           /* If the path isn't local but we're in local-only mode now, remove
2818            * the custom-folder row */
2819           if (data && g_file_is_native (G_FILE (data)) &&
2820               gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2821             {
2822               pos--;
2823               model_remove_rows (user_data, pos, 2);
2824             }
2825         }
2826
2827       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2828       update_combo_box (user_data);
2829     }
2830 }
2831
2832 static gboolean
2833 dialog_delete_event_cb (GtkWidget *dialog,
2834                         GdkEvent  *event,
2835                         gpointer   user_data)
2836 {
2837   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2838
2839   return TRUE;
2840 }
2841
2842 static void
2843 dialog_response_cb (GtkDialog *dialog,
2844                     gint       response,
2845                     gpointer   user_data)
2846 {
2847   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2848   GtkFileChooserButtonPrivate *priv = button->priv;
2849
2850   if (response == GTK_RESPONSE_ACCEPT ||
2851       response == GTK_RESPONSE_OK)
2852     {
2853       save_inactive_state (button);
2854
2855       g_signal_emit_by_name (button, "current-folder-changed");
2856       g_signal_emit_by_name (button, "selection-changed");
2857
2858       update_label_and_image (button);
2859       update_combo_box (button);
2860     }
2861   else
2862     {
2863       restore_inactive_state (button);
2864     }
2865
2866   if (priv->active)
2867     {
2868       g_signal_handler_unblock (priv->dialog,
2869                                 priv->dialog_folder_changed_id);
2870       g_signal_handler_unblock (priv->dialog,
2871                                 priv->dialog_file_activated_id);
2872       g_signal_handler_unblock (priv->dialog,
2873                                 priv->dialog_selection_changed_id);
2874       priv->active = FALSE;
2875     }
2876
2877   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2878   gtk_widget_hide (priv->dialog);
2879
2880   if (response == GTK_RESPONSE_ACCEPT ||
2881       response == GTK_RESPONSE_OK)
2882     g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2883 }
2884
2885
2886 /* ************************************************************************** *
2887  *  Public API                                                                *
2888  * ************************************************************************** */
2889
2890 /**
2891  * gtk_file_chooser_button_new:
2892  * @title: the title of the browse dialog.
2893  * @action: the open mode for the widget.
2894  * 
2895  * Creates a new file-selecting button widget.
2896  * 
2897  * Returns: a new button widget.
2898  * 
2899  * Since: 2.6
2900  **/
2901 GtkWidget *
2902 gtk_file_chooser_button_new (const gchar          *title,
2903                              GtkFileChooserAction  action)
2904 {
2905   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2906                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2907
2908   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2909                        "action", action,
2910                        "title", (title ? title : _(DEFAULT_TITLE)),
2911                        NULL);
2912 }
2913
2914 /**
2915  * gtk_file_chooser_button_new_with_dialog:
2916  * @dialog: the widget to use as dialog
2917  *
2918  * Creates a #GtkFileChooserButton widget which uses @dialog as its
2919  * file-picking window.
2920  *
2921  * Note that @dialog must be a #GtkDialog (or subclass) which
2922  * implements the #GtkFileChooser interface and must not have
2923  * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2924  *
2925  * Also note that the dialog needs to have its confirmative button
2926  * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2927  * order for the button to take over the file selected in the dialog.
2928  *
2929  * Returns: a new button widget.
2930  *
2931  * Since: 2.6
2932  **/
2933 GtkWidget *
2934 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2935 {
2936   g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2937
2938   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2939                        "dialog", dialog,
2940                        NULL);
2941 }
2942
2943 /**
2944  * gtk_file_chooser_button_set_title:
2945  * @button: the button widget to modify.
2946  * @title: the new browse dialog title.
2947  * 
2948  * Modifies the @title of the browse dialog used by @button.
2949  * 
2950  * Since: 2.6
2951  **/
2952 void
2953 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2954                                    const gchar          *title)
2955 {
2956   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2957
2958   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2959   g_object_notify (G_OBJECT (button), "title");
2960 }
2961
2962 /**
2963  * gtk_file_chooser_button_get_title:
2964  * @button: the button widget to examine.
2965  * 
2966  * Retrieves the title of the browse dialog used by @button. The returned value
2967  * should not be modified or freed.
2968  * 
2969  * Returns: a pointer to the browse dialog's title.
2970  * 
2971  * Since: 2.6
2972  **/
2973 const gchar *
2974 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2975 {
2976   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2977
2978   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2979 }
2980
2981 /**
2982  * gtk_file_chooser_button_get_width_chars:
2983  * @button: the button widget to examine.
2984  * 
2985  * Retrieves the width in characters of the @button widget's entry and/or label.
2986  * 
2987  * Returns: an integer width (in characters) that the button will use to size itself.
2988  * 
2989  * Since: 2.6
2990  **/
2991 gint
2992 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2993 {
2994   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2995
2996   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2997 }
2998
2999 /**
3000  * gtk_file_chooser_button_set_width_chars:
3001  * @button: the button widget to examine.
3002  * @n_chars: the new width, in characters.
3003  * 
3004  * Sets the width (in characters) that @button will use to @n_chars.
3005  * 
3006  * Since: 2.6
3007  **/
3008 void
3009 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3010                                          gint                  n_chars)
3011 {
3012   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3013
3014   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3015   g_object_notify (G_OBJECT (button), "width-chars");
3016 }
3017
3018 /**
3019  * gtk_file_chooser_button_set_focus_on_click:
3020  * @button: a #GtkFileChooserButton
3021  * @focus_on_click: whether the button grabs focus when clicked with the mouse
3022  * 
3023  * Sets whether the button will grab focus when it is clicked with the mouse.
3024  * Making mouse clicks not grab focus is useful in places like toolbars where
3025  * you don't want the keyboard focus removed from the main area of the
3026  * application.
3027  *
3028  * Since: 2.10
3029  **/
3030 void
3031 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3032                                             gboolean              focus_on_click)
3033 {
3034   GtkFileChooserButtonPrivate *priv;
3035
3036   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3037
3038   priv = button->priv;
3039
3040   focus_on_click = focus_on_click != FALSE;
3041
3042   if (priv->focus_on_click != focus_on_click)
3043     {
3044       priv->focus_on_click = focus_on_click;
3045       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3046       gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3047       
3048       g_object_notify (G_OBJECT (button), "focus-on-click");
3049     }
3050 }
3051
3052 /**
3053  * gtk_file_chooser_button_get_focus_on_click:
3054  * @button: a #GtkFileChooserButton
3055  * 
3056  * Returns whether the button grabs focus when it is clicked with the mouse.
3057  * See gtk_file_chooser_button_set_focus_on_click().
3058  *
3059  * Return value: %TRUE if the button grabs focus when it is clicked with
3060  *               the mouse.
3061  *
3062  * Since: 2.10
3063  **/
3064 gboolean
3065 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3066 {
3067   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3068   
3069   return button->priv->focus_on_click;
3070 }