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