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