1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtkfilechooserdefault.h"
26 #include "gtkalignment.h"
27 #include "gtkbindings.h"
28 #include "gtkcelllayout.h"
29 #include "gtkcellrendererpixbuf.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcheckmenuitem.h"
32 #include "gtkclipboard.h"
33 #include "gtkcomboboxtext.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystem.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkmountoperation.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkrecentfilter.h"
59 #include "gtkrecentmanager.h"
60 #include "gtkscrolledwindow.h"
61 #include "gtkseparatormenuitem.h"
62 #include "gtksizegroup.h"
63 #include "gtksizerequest.h"
66 #include "gtktoolbar.h"
67 #include "gtktoolbutton.h"
68 #include "gtktooltip.h"
69 #include "gtktreednd.h"
70 #include "gtktreeprivate.h"
71 #include "gtktreeselection.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
195 "standard::is-hidden,standard::is-backup,standard::size," \
196 "standard::content-type,time::modified"
198 /* the first 3 must be these due to settings caching sort column */
203 MODEL_COL_NAME_COLLATED,
207 MODEL_COL_MTIME_TEXT,
209 MODEL_COL_NUM_COLUMNS
212 /* This list of types is passed to _gtk_file_system_model_new*() */
213 #define MODEL_COLUMN_TYPES \
214 MODEL_COL_NUM_COLUMNS, \
215 G_TYPE_STRING, /* MODEL_COL_NAME */ \
216 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
217 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
218 G_TYPE_FILE, /* MODEL_COL_FILE */ \
219 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
220 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
221 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
222 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
223 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
224 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
226 /* Identifiers for target types */
231 /* Interesting places in the shortcuts bar */
235 SHORTCUTS_RECENT_SEPARATOR,
240 SHORTCUTS_BOOKMARKS_SEPARATOR,
242 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
243 SHORTCUTS_CURRENT_FOLDER
246 /* Icon size for if we can't get it from the theme */
247 #define FALLBACK_ICON_SIZE 16
249 #define PREVIEW_HBOX_SPACING 12
253 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
254 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
255 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
256 #define SETTINGS_KEY_EXPAND_FOLDERS "expand-folders"
257 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
258 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
259 #define SETTINGS_KEY_SORT_ORDER "sort-order"
260 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
261 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
263 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
264 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
266 static GObject* gtk_file_chooser_default_constructor (GType type,
267 guint n_construct_properties,
268 GObjectConstructParam *construct_params);
269 static void gtk_file_chooser_default_finalize (GObject *object);
270 static void gtk_file_chooser_default_set_property (GObject *object,
274 static void gtk_file_chooser_default_get_property (GObject *object,
278 static void gtk_file_chooser_default_dispose (GObject *object);
279 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
280 static void gtk_file_chooser_default_realize (GtkWidget *widget);
281 static void gtk_file_chooser_default_map (GtkWidget *widget);
282 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
283 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
284 GtkWidget *previous_toplevel);
285 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
286 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
287 GdkScreen *previous_screen);
288 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
289 GtkAllocation *allocation);
291 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
294 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
297 gboolean clear_entry,
299 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
300 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
302 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
305 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
307 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
309 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
310 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
311 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
312 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
313 GtkFileFilter *filter);
314 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
315 GtkFileFilter *filter);
316 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
317 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
320 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
323 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
325 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
327 gint *default_height);
328 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
329 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
331 static void location_popup_handler (GtkFileChooserDefault *impl,
333 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
334 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
335 static void up_folder_handler (GtkFileChooserDefault *impl);
336 static void down_folder_handler (GtkFileChooserDefault *impl);
337 static void home_folder_handler (GtkFileChooserDefault *impl);
338 static void desktop_folder_handler (GtkFileChooserDefault *impl);
339 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
340 gint bookmark_index);
341 static void show_hidden_handler (GtkFileChooserDefault *impl);
342 static void search_shortcut_handler (GtkFileChooserDefault *impl);
343 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
344 static void update_appearance (GtkFileChooserDefault *impl);
346 static void set_current_filter (GtkFileChooserDefault *impl,
347 GtkFileFilter *filter);
348 static void check_preview_change (GtkFileChooserDefault *impl);
350 static void filter_combo_changed (GtkComboBox *combo_box,
351 GtkFileChooserDefault *impl);
353 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
355 GtkFileChooserDefault *impl);
357 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
360 gboolean path_currently_selected,
362 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
364 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
366 static int shortcuts_get_index (GtkFileChooserDefault *impl,
367 ShortcutsIndex where);
368 static int shortcut_find_position (GtkFileChooserDefault *impl,
371 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
373 static gboolean list_select_func (GtkTreeSelection *selection,
376 gboolean path_currently_selected,
379 static void list_selection_changed (GtkTreeSelection *tree_selection,
380 GtkFileChooserDefault *impl);
381 static void list_row_activated (GtkTreeView *tree_view,
383 GtkTreeViewColumn *column,
384 GtkFileChooserDefault *impl);
386 static void path_bar_clicked (GtkPathBar *path_bar,
389 gboolean child_is_hidden,
390 GtkFileChooserDefault *impl);
392 static void add_bookmark_button_clicked_cb (GtkButton *button,
393 GtkFileChooserDefault *impl);
394 static void remove_bookmark_button_clicked_cb (GtkButton *button,
395 GtkFileChooserDefault *impl);
396 static void save_folder_combo_changed_cb (GtkComboBox *combo,
397 GtkFileChooserDefault *impl);
399 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
401 static void load_remove_timer (GtkFileChooserDefault *impl);
402 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
404 static void location_button_toggled_cb (GtkToggleButton *toggle,
405 GtkFileChooserDefault *impl);
406 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
408 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
409 gboolean remove_from_treeview);
410 static void search_stop_searching (GtkFileChooserDefault *impl,
411 gboolean remove_query);
412 static void search_clear_model (GtkFileChooserDefault *impl,
413 gboolean remove_from_treeview);
414 static gboolean search_should_respond (GtkFileChooserDefault *impl);
415 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
416 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
417 static void search_entry_activate_cb (GtkEntry *entry,
419 static void settings_load (GtkFileChooserDefault *impl);
421 static void recent_stop_loading (GtkFileChooserDefault *impl);
422 static void recent_clear_model (GtkFileChooserDefault *impl,
423 gboolean remove_from_treeview);
424 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
425 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
426 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
427 static void set_file_system_backend (GtkFileChooserDefault *impl);
428 static void unset_file_system_backend (GtkFileChooserDefault *impl);
434 /* Drag and drop interface declarations */
437 GtkTreeModelFilter parent;
439 GtkFileChooserDefault *impl;
440 } ShortcutsPaneModelFilter;
443 GtkTreeModelFilterClass parent_class;
444 } ShortcutsPaneModelFilterClass;
446 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
447 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
449 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
451 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
452 _shortcuts_pane_model_filter,
453 GTK_TYPE_TREE_MODEL_FILTER,
454 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
455 shortcuts_pane_model_filter_drag_source_iface_init))
457 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
458 GtkTreeModel *child_model,
463 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
464 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
465 gtk_file_chooser_default_iface_init)
466 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
467 gtk_file_chooser_embed_default_iface_init));
471 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
473 GdkModifierType modifiers,
474 const gchar *signal_name)
476 gtk_binding_entry_add_signal (binding_set,
480 gtk_binding_entry_add_signal (binding_set,
481 keyval, modifiers | GDK_SHIFT_MASK,
486 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
488 static const guint quick_bookmark_keyvals[10] = {
489 GDK_KEY_1, GDK_KEY_2, GDK_KEY_3, GDK_KEY_4, GDK_KEY_5, GDK_KEY_6, GDK_KEY_7, GDK_KEY_8, GDK_KEY_9, GDK_KEY_0
491 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
492 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
493 GtkBindingSet *binding_set;
496 gobject_class->finalize = gtk_file_chooser_default_finalize;
497 gobject_class->constructor = gtk_file_chooser_default_constructor;
498 gobject_class->set_property = gtk_file_chooser_default_set_property;
499 gobject_class->get_property = gtk_file_chooser_default_get_property;
500 gobject_class->dispose = gtk_file_chooser_default_dispose;
502 widget_class->show_all = gtk_file_chooser_default_show_all;
503 widget_class->realize = gtk_file_chooser_default_realize;
504 widget_class->map = gtk_file_chooser_default_map;
505 widget_class->unmap = gtk_file_chooser_default_unmap;
506 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
507 widget_class->style_updated = gtk_file_chooser_default_style_updated;
508 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
509 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
511 signals[LOCATION_POPUP] =
512 g_signal_new_class_handler (I_("location-popup"),
513 G_OBJECT_CLASS_TYPE (class),
514 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
515 G_CALLBACK (location_popup_handler),
517 _gtk_marshal_VOID__STRING,
518 G_TYPE_NONE, 1, G_TYPE_STRING);
520 signals[LOCATION_POPUP_ON_PASTE] =
521 g_signal_new_class_handler (I_("location-popup-on-paste"),
522 G_OBJECT_CLASS_TYPE (class),
523 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
524 G_CALLBACK (location_popup_on_paste_handler),
526 _gtk_marshal_VOID__VOID,
529 signals[LOCATION_TOGGLE_POPUP] =
530 g_signal_new_class_handler (I_("location-toggle-popup"),
531 G_OBJECT_CLASS_TYPE (class),
532 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
533 G_CALLBACK (location_toggle_popup_handler),
535 _gtk_marshal_VOID__VOID,
539 g_signal_new_class_handler (I_("up-folder"),
540 G_OBJECT_CLASS_TYPE (class),
541 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
542 G_CALLBACK (up_folder_handler),
544 _gtk_marshal_VOID__VOID,
547 signals[DOWN_FOLDER] =
548 g_signal_new_class_handler (I_("down-folder"),
549 G_OBJECT_CLASS_TYPE (class),
550 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
551 G_CALLBACK (down_folder_handler),
553 _gtk_marshal_VOID__VOID,
556 signals[HOME_FOLDER] =
557 g_signal_new_class_handler (I_("home-folder"),
558 G_OBJECT_CLASS_TYPE (class),
559 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
560 G_CALLBACK (home_folder_handler),
562 _gtk_marshal_VOID__VOID,
565 signals[DESKTOP_FOLDER] =
566 g_signal_new_class_handler (I_("desktop-folder"),
567 G_OBJECT_CLASS_TYPE (class),
568 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
569 G_CALLBACK (desktop_folder_handler),
571 _gtk_marshal_VOID__VOID,
574 signals[QUICK_BOOKMARK] =
575 g_signal_new_class_handler (I_("quick-bookmark"),
576 G_OBJECT_CLASS_TYPE (class),
577 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
578 G_CALLBACK (quick_bookmark_handler),
580 _gtk_marshal_VOID__INT,
581 G_TYPE_NONE, 1, G_TYPE_INT);
583 signals[SHOW_HIDDEN] =
584 g_signal_new_class_handler (I_("show-hidden"),
585 G_OBJECT_CLASS_TYPE (class),
586 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
587 G_CALLBACK (show_hidden_handler),
589 _gtk_marshal_VOID__VOID,
592 signals[SEARCH_SHORTCUT] =
593 g_signal_new_class_handler (I_("search-shortcut"),
594 G_OBJECT_CLASS_TYPE (class),
595 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
596 G_CALLBACK (search_shortcut_handler),
598 _gtk_marshal_VOID__VOID,
601 signals[RECENT_SHORTCUT] =
602 g_signal_new_class_handler (I_("recent-shortcut"),
603 G_OBJECT_CLASS_TYPE (class),
604 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
605 G_CALLBACK (recent_shortcut_handler),
607 _gtk_marshal_VOID__VOID,
610 binding_set = gtk_binding_set_by_class (class);
612 gtk_binding_entry_add_signal (binding_set,
613 GDK_KEY_l, GDK_CONTROL_MASK,
614 "location-toggle-popup",
617 gtk_binding_entry_add_signal (binding_set,
620 1, G_TYPE_STRING, "/");
621 gtk_binding_entry_add_signal (binding_set,
622 GDK_KEY_KP_Divide, 0,
624 1, G_TYPE_STRING, "/");
627 gtk_binding_entry_add_signal (binding_set,
628 GDK_KEY_asciitilde, 0,
630 1, G_TYPE_STRING, "~");
633 gtk_binding_entry_add_signal (binding_set,
634 GDK_KEY_v, GDK_CONTROL_MASK,
635 "location-popup-on-paste",
637 gtk_binding_entry_add_signal (binding_set,
638 GDK_KEY_BackSpace, 0,
642 add_normal_and_shifted_binding (binding_set,
643 GDK_KEY_Up, GDK_MOD1_MASK,
646 add_normal_and_shifted_binding (binding_set,
647 GDK_KEY_KP_Up, GDK_MOD1_MASK,
650 add_normal_and_shifted_binding (binding_set,
651 GDK_KEY_Down, GDK_MOD1_MASK,
653 add_normal_and_shifted_binding (binding_set,
654 GDK_KEY_KP_Down, GDK_MOD1_MASK,
657 gtk_binding_entry_add_signal (binding_set,
658 GDK_KEY_Home, GDK_MOD1_MASK,
661 gtk_binding_entry_add_signal (binding_set,
662 GDK_KEY_KP_Home, GDK_MOD1_MASK,
665 gtk_binding_entry_add_signal (binding_set,
666 GDK_KEY_d, GDK_MOD1_MASK,
669 gtk_binding_entry_add_signal (binding_set,
670 GDK_KEY_h, GDK_CONTROL_MASK,
673 gtk_binding_entry_add_signal (binding_set,
674 GDK_KEY_s, GDK_MOD1_MASK,
677 gtk_binding_entry_add_signal (binding_set,
678 GDK_KEY_r, GDK_MOD1_MASK,
682 for (i = 0; i < 10; i++)
683 gtk_binding_entry_add_signal (binding_set,
684 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
688 _gtk_file_chooser_install_properties (gobject_class);
692 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
694 iface->select_file = gtk_file_chooser_default_select_file;
695 iface->unselect_file = gtk_file_chooser_default_unselect_file;
696 iface->select_all = gtk_file_chooser_default_select_all;
697 iface->unselect_all = gtk_file_chooser_default_unselect_all;
698 iface->get_files = gtk_file_chooser_default_get_files;
699 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
700 iface->get_file_system = gtk_file_chooser_default_get_file_system;
701 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
702 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
703 iface->set_current_name = gtk_file_chooser_default_set_current_name;
704 iface->add_filter = gtk_file_chooser_default_add_filter;
705 iface->remove_filter = gtk_file_chooser_default_remove_filter;
706 iface->list_filters = gtk_file_chooser_default_list_filters;
707 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
708 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
709 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
713 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
715 iface->get_default_size = gtk_file_chooser_default_get_default_size;
716 iface->should_respond = gtk_file_chooser_default_should_respond;
717 iface->initial_focus = gtk_file_chooser_default_initial_focus;
721 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
723 profile_start ("start", NULL);
724 #ifdef PROFILE_FILE_CHOOSER
725 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
727 impl->local_only = TRUE;
728 impl->preview_widget_active = TRUE;
729 impl->use_preview_label = TRUE;
730 impl->select_multiple = FALSE;
731 impl->show_hidden = FALSE;
732 impl->show_size_column = TRUE;
733 impl->icon_size = FALLBACK_ICON_SIZE;
734 impl->load_state = LOAD_EMPTY;
735 impl->reload_state = RELOAD_EMPTY;
736 impl->pending_select_files = NULL;
737 impl->location_mode = LOCATION_MODE_PATH_BAR;
738 impl->operation_mode = OPERATION_MODE_BROWSE;
739 impl->sort_column = MODEL_COL_NAME;
740 impl->sort_order = GTK_SORT_ASCENDING;
741 impl->recent_manager = gtk_recent_manager_get_default ();
742 impl->create_folders = TRUE;
744 gtk_box_set_spacing (GTK_BOX (impl), 12);
746 set_file_system_backend (impl);
748 profile_end ("end", NULL);
751 /* Frees the data columns for the specified iter in the shortcuts model*/
753 shortcuts_free_row_data (GtkFileChooserDefault *impl,
757 ShortcutType shortcut_type;
758 GCancellable *cancellable;
760 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
761 SHORTCUTS_COL_DATA, &col_data,
762 SHORTCUTS_COL_TYPE, &shortcut_type,
763 SHORTCUTS_COL_CANCELLABLE, &cancellable,
768 g_cancellable_cancel (cancellable);
769 g_object_unref (cancellable);
772 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
773 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
777 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
779 GtkFileSystemVolume *volume;
782 _gtk_file_system_volume_unref (volume);
784 if (shortcut_type == SHORTCUT_TYPE_FILE)
789 g_object_unref (file);
793 /* Frees all the data columns in the shortcuts model */
795 shortcuts_free (GtkFileChooserDefault *impl)
799 if (!impl->shortcuts_model)
802 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
805 shortcuts_free_row_data (impl, &iter);
807 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
809 g_object_unref (impl->shortcuts_model);
810 impl->shortcuts_model = NULL;
814 pending_select_files_free (GtkFileChooserDefault *impl)
816 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
817 g_slist_free (impl->pending_select_files);
818 impl->pending_select_files = NULL;
822 pending_select_files_add (GtkFileChooserDefault *impl,
825 impl->pending_select_files =
826 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
830 gtk_file_chooser_default_finalize (GObject *object)
832 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
835 unset_file_system_backend (impl);
837 if (impl->shortcuts_pane_filter_model)
838 g_object_unref (impl->shortcuts_pane_filter_model);
840 if (impl->shortcuts_combo_filter_model)
841 g_object_unref (impl->shortcuts_combo_filter_model);
843 shortcuts_free (impl);
845 g_free (impl->browse_files_last_selected_name);
847 for (l = impl->filters; l; l = l->next)
849 GtkFileFilter *filter;
851 filter = GTK_FILE_FILTER (l->data);
852 g_object_unref (filter);
854 g_slist_free (impl->filters);
856 if (impl->current_filter)
857 g_object_unref (impl->current_filter);
859 if (impl->current_volume_file)
860 g_object_unref (impl->current_volume_file);
862 if (impl->current_folder)
863 g_object_unref (impl->current_folder);
865 if (impl->preview_file)
866 g_object_unref (impl->preview_file);
868 if (impl->browse_path_bar_size_group)
869 g_object_unref (impl->browse_path_bar_size_group);
871 /* Free all the Models we have */
872 stop_loading_and_clear_list_model (impl, FALSE);
873 search_clear_model (impl, FALSE);
874 recent_clear_model (impl, FALSE);
876 /* stopping the load above should have cleared this */
877 g_assert (impl->load_timeout_id == 0);
879 g_free (impl->preview_display_name);
881 g_free (impl->edited_new_text);
883 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
886 /* Shows an error dialog set as transient for the specified window */
888 error_message_with_parent (GtkWindow *parent,
894 dialog = gtk_message_dialog_new (parent,
895 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
900 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
903 if (parent && gtk_window_has_group (parent))
904 gtk_window_group_add_window (gtk_window_get_group (parent),
905 GTK_WINDOW (dialog));
907 gtk_dialog_run (GTK_DIALOG (dialog));
908 gtk_widget_destroy (dialog);
911 /* Returns a toplevel GtkWindow, or NULL if none */
913 get_toplevel (GtkWidget *widget)
917 toplevel = gtk_widget_get_toplevel (widget);
918 if (!gtk_widget_is_toplevel (toplevel))
921 return GTK_WINDOW (toplevel);
924 /* Shows an error dialog for the file chooser */
926 error_message (GtkFileChooserDefault *impl,
930 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
933 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
935 error_dialog (GtkFileChooserDefault *impl,
946 uri = g_file_get_uri (file);
947 text = g_strdup_printf (msg, uri);
948 error_message (impl, text, error->message);
951 g_error_free (error);
955 /* Displays an error message about not being able to get information for a file.
956 * Frees the GError as well.
959 error_getting_info_dialog (GtkFileChooserDefault *impl,
964 _("Could not retrieve information about the file"),
968 /* Shows an error dialog about not being able to add a bookmark */
970 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
975 _("Could not add a bookmark"),
979 /* Shows an error dialog about not being able to remove a bookmark */
981 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
986 _("Could not remove bookmark"),
990 /* Shows an error dialog about not being able to create a folder */
992 error_creating_folder_dialog (GtkFileChooserDefault *impl,
997 _("The folder could not be created"),
1001 /* Shows an error about not being able to create a folder because a file with
1002 * the same name is already there.
1005 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1010 _("The folder could not be created, as a file with the same "
1011 "name already exists. Try using a different name for the "
1012 "folder, or rename the file first."),
1017 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1023 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1024 _("You need to choose a valid filename."));
1027 _("Cannot create a file under %s as it is not a folder"),
1028 parent_file, error);
1031 /* Shows an error about not being able to select a folder because a file with
1032 * the same name is already there.
1035 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1039 _("You may only select folders. The item that you selected is not a folder; "
1040 "try using a different item."),
1044 /* Shows an error dialog about not being able to create a filename */
1046 error_building_filename_dialog (GtkFileChooserDefault *impl,
1049 error_dialog (impl, _("Invalid file name"),
1053 /* Shows an error dialog when we cannot switch to a folder */
1055 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1059 error_dialog (impl, _("The folder contents could not be displayed"),
1063 /* Changes folders, displaying an error dialog if this fails */
1065 change_folder_and_display_error (GtkFileChooserDefault *impl,
1067 gboolean clear_entry)
1072 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1074 /* We copy the path because of this case:
1076 * list_row_activated()
1077 * fetches path from model; path belongs to the model (*)
1078 * calls change_folder_and_display_error()
1079 * calls gtk_file_chooser_set_current_folder_file()
1080 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1084 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1087 error_changing_folder_dialog (impl, file, error);
1093 emit_default_size_changed (GtkFileChooserDefault *impl)
1095 profile_msg (" emit default-size-changed start", NULL);
1096 g_signal_emit_by_name (impl, "default-size-changed");
1097 profile_msg (" emit default-size-changed end", NULL);
1101 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1103 if (impl->use_preview_label)
1105 if (!impl->preview_label)
1107 impl->preview_label = gtk_label_new (impl->preview_display_name);
1108 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1109 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1110 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1111 gtk_widget_show (impl->preview_label);
1116 if (impl->preview_label)
1118 gtk_widget_destroy (impl->preview_label);
1119 impl->preview_label = NULL;
1123 if (impl->preview_widget_active && impl->preview_widget)
1124 gtk_widget_show (impl->preview_box);
1126 gtk_widget_hide (impl->preview_box);
1128 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1129 emit_default_size_changed (impl);
1133 set_preview_widget (GtkFileChooserDefault *impl,
1134 GtkWidget *preview_widget)
1136 if (preview_widget == impl->preview_widget)
1139 if (impl->preview_widget)
1140 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1141 impl->preview_widget);
1143 impl->preview_widget = preview_widget;
1144 if (impl->preview_widget)
1146 gtk_widget_show (impl->preview_widget);
1147 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1148 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1149 impl->preview_widget,
1150 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1153 update_preview_widget_visibility (impl);
1156 /* Renders a "Search" icon at an appropriate size for a tree view */
1158 render_search_icon (GtkFileChooserDefault *impl)
1160 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1164 render_recent_icon (GtkFileChooserDefault *impl)
1166 GtkIconTheme *theme;
1169 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1170 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1172 theme = gtk_icon_theme_get_default ();
1174 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1180 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1186 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1187 struct ReloadIconsData
1189 GtkFileChooserDefault *impl;
1190 GtkTreeRowReference *row_ref;
1194 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1196 const GError *error,
1202 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1203 struct ReloadIconsData *data = user_data;
1205 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1208 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1210 if (cancelled || error)
1213 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1215 path = gtk_tree_row_reference_get_path (data->row_ref);
1218 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1219 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1220 SHORTCUTS_COL_PIXBUF, pixbuf,
1222 gtk_tree_path_free (path);
1226 g_object_unref (pixbuf);
1229 gtk_tree_row_reference_free (data->row_ref);
1230 g_object_unref (data->impl);
1233 g_object_unref (cancellable);
1237 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1242 profile_start ("start", NULL);
1244 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1247 for (l = impl->reload_icon_cancellables; l; l = l->next)
1249 GCancellable *cancellable = G_CANCELLABLE (l->data);
1250 g_cancellable_cancel (cancellable);
1252 g_slist_free (impl->reload_icon_cancellables);
1253 impl->reload_icon_cancellables = NULL;
1258 ShortcutType shortcut_type;
1259 gboolean pixbuf_visible;
1262 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1263 SHORTCUTS_COL_DATA, &data,
1264 SHORTCUTS_COL_TYPE, &shortcut_type,
1265 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1271 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1273 GtkFileSystemVolume *volume;
1276 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1277 impl->icon_size, NULL);
1279 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1281 if (g_file_is_native (G_FILE (data)))
1284 struct ReloadIconsData *info;
1285 GtkTreePath *tree_path;
1286 GCancellable *cancellable;
1290 info = g_new0 (struct ReloadIconsData, 1);
1291 info->impl = g_object_ref (impl);
1292 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1293 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1294 gtk_tree_path_free (tree_path);
1296 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1298 shortcuts_reload_icons_get_info_cb,
1300 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1304 GtkIconTheme *icon_theme;
1306 /* Don't call get_info for remote paths to avoid latency and
1308 * If we switch to a better bookmarks file format (XBEL), we
1309 * should use mime info to get a better icon.
1311 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1312 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1313 impl->icon_size, 0, NULL);
1316 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1318 pixbuf = render_search_icon (impl);
1320 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1322 pixbuf = render_recent_icon (impl);
1325 gtk_list_store_set (impl->shortcuts_model, &iter,
1326 SHORTCUTS_COL_PIXBUF, pixbuf,
1330 g_object_unref (pixbuf);
1334 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1338 profile_end ("end", NULL);
1342 shortcuts_find_folder (GtkFileChooserDefault *impl,
1345 GtkTreeSelection *selection;
1349 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1351 g_assert (folder != NULL);
1352 pos = shortcut_find_position (impl, folder);
1355 gtk_tree_selection_unselect_all (selection);
1359 path = gtk_tree_path_new_from_indices (pos, -1);
1360 gtk_tree_selection_select_path (selection, path);
1361 gtk_tree_path_free (path);
1364 /* If a shortcut corresponds to the current folder, selects it */
1366 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1368 shortcuts_find_folder (impl, impl->current_folder);
1371 /* Removes the specified number of rows from the shortcuts list */
1373 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1379 path = gtk_tree_path_new_from_indices (start_row, -1);
1381 for (; n_rows; n_rows--)
1385 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1386 g_assert_not_reached ();
1388 shortcuts_free_row_data (impl, &iter);
1389 gtk_list_store_remove (impl->shortcuts_model, &iter);
1392 gtk_tree_path_free (path);
1396 shortcuts_update_count (GtkFileChooserDefault *impl,
1397 ShortcutsIndex type,
1402 case SHORTCUTS_HOME:
1404 impl->has_home = FALSE;
1406 impl->has_home = TRUE;
1409 case SHORTCUTS_DESKTOP:
1411 impl->has_desktop = FALSE;
1413 impl->has_desktop = TRUE;
1416 case SHORTCUTS_VOLUMES:
1417 impl->num_volumes += value;
1420 case SHORTCUTS_SHORTCUTS:
1421 impl->num_shortcuts += value;
1424 case SHORTCUTS_BOOKMARKS:
1425 impl->num_bookmarks += value;
1428 case SHORTCUTS_CURRENT_FOLDER:
1430 impl->shortcuts_current_folder_active = FALSE;
1432 impl->shortcuts_current_folder_active = TRUE;
1441 struct ShortcutsInsertRequest
1443 GtkFileChooserDefault *impl;
1447 GtkTreeRowReference *row_ref;
1448 ShortcutsIndex type;
1454 get_file_info_finished (GCancellable *cancellable,
1456 const GError *error,
1460 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1464 GCancellable *model_cancellable = NULL;
1465 struct ShortcutsInsertRequest *request = data;
1467 path = gtk_tree_row_reference_get_path (request->row_ref);
1469 /* Handle doesn't exist anymore in the model */
1472 pos = gtk_tree_path_get_indices (path)[0];
1473 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1475 gtk_tree_path_free (path);
1477 /* validate cancellable, else goto out */
1478 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1479 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1481 if (cancellable != model_cancellable)
1484 /* set the cancellable to NULL in the model (we unref later on) */
1485 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1486 SHORTCUTS_COL_CANCELLABLE, NULL,
1494 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1495 shortcuts_update_count (request->impl, request->type, -1);
1497 if (request->type == SHORTCUTS_HOME)
1501 home = g_file_new_for_path (g_get_home_dir ());
1502 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1503 g_object_unref (home);
1505 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1507 /* Remove the current folder separator */
1508 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1509 shortcuts_remove_rows (request->impl, separator_pos, 1);
1515 if (!request->label_copy)
1516 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1517 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1518 request->impl->icon_size);
1520 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1521 SHORTCUTS_COL_PIXBUF, pixbuf,
1522 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1523 SHORTCUTS_COL_NAME, request->label_copy,
1524 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1525 SHORTCUTS_COL_REMOVABLE, request->removable,
1528 if (request->impl->shortcuts_pane_filter_model)
1529 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1531 if (request->impl->shortcuts_combo_filter_model)
1532 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1534 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1535 request->impl->save_folder_combo != NULL)
1537 /* The current folder is updated via _activate_iter(), don't
1538 * have save_folder_combo_changed_cb() call _activate_iter()
1541 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1542 G_CALLBACK (save_folder_combo_changed_cb),
1545 if (request->impl->has_search)
1548 if (request->impl->has_recent)
1551 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1552 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1553 G_CALLBACK (save_folder_combo_changed_cb),
1558 g_object_unref (pixbuf);
1561 g_object_unref (request->impl);
1562 g_object_unref (request->file);
1563 gtk_tree_row_reference_free (request->row_ref);
1564 g_free (request->label_copy);
1567 if (model_cancellable)
1568 g_object_unref (model_cancellable);
1571 /* FIXME: GtkFileSystem needs a function to split a remote path
1572 * into hostname and path components, or maybe just have a
1573 * gtk_file_system_path_get_display_name().
1575 * This function is also used in gtkfilechooserbutton.c
1578 _gtk_file_chooser_label_for_file (GFile *file)
1580 const gchar *path, *start, *end, *p;
1581 gchar *uri, *host, *label;
1583 uri = g_file_get_uri (file);
1585 start = strstr (uri, "://");
1589 path = strchr (start, '/');
1594 end = uri + strlen (uri);
1598 /* strip username */
1599 p = strchr (start, '@');
1603 p = strchr (start, ':');
1607 host = g_strndup (start, end - start);
1609 /* Translators: the first string is a path and the second string
1610 * is a hostname. Nautilus and the panel contain the same string
1613 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1619 label = g_strdup (uri);
1627 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1628 * inserts a volume. A position of -1 indicates the end of the tree.
1631 shortcuts_insert_file (GtkFileChooserDefault *impl,
1633 ShortcutType shortcut_type,
1634 GtkFileSystemVolume *volume,
1638 ShortcutsIndex type)
1641 GdkPixbuf *pixbuf = NULL;
1642 gpointer data = NULL;
1644 GtkIconTheme *icon_theme;
1646 profile_start ("start shortcut", NULL);
1648 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1651 label_copy = _gtk_file_system_volume_get_display_name (volume);
1652 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1653 impl->icon_size, NULL);
1655 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1657 if (g_file_is_native (file))
1659 struct ShortcutsInsertRequest *request;
1660 GCancellable *cancellable;
1663 request = g_new0 (struct ShortcutsInsertRequest, 1);
1664 request->impl = g_object_ref (impl);
1665 request->file = g_object_ref (file);
1666 request->name_only = TRUE;
1667 request->removable = removable;
1669 request->type = type;
1671 request->label_copy = g_strdup (label);
1674 gtk_list_store_append (impl->shortcuts_model, &iter);
1676 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1678 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1679 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1680 gtk_tree_path_free (p);
1682 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1683 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1684 get_file_info_finished, request);
1686 gtk_list_store_set (impl->shortcuts_model, &iter,
1687 SHORTCUTS_COL_DATA, g_object_ref (file),
1688 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1689 SHORTCUTS_COL_CANCELLABLE, cancellable,
1692 shortcuts_update_count (impl, type, 1);
1698 /* Don't call get_info for remote paths to avoid latency and
1701 data = g_object_ref (file);
1703 label_copy = g_strdup (label);
1705 label_copy = _gtk_file_chooser_label_for_file (file);
1707 /* If we switch to a better bookmarks file format (XBEL), we
1708 * should use mime info to get a better icon.
1710 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1711 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1712 impl->icon_size, 0, NULL);
1717 g_assert_not_reached ();
1723 gtk_list_store_append (impl->shortcuts_model, &iter);
1725 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1727 shortcuts_update_count (impl, type, 1);
1729 gtk_list_store_set (impl->shortcuts_model, &iter,
1730 SHORTCUTS_COL_PIXBUF, pixbuf,
1731 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1732 SHORTCUTS_COL_NAME, label_copy,
1733 SHORTCUTS_COL_DATA, data,
1734 SHORTCUTS_COL_TYPE, shortcut_type,
1735 SHORTCUTS_COL_REMOVABLE, removable,
1736 SHORTCUTS_COL_CANCELLABLE, NULL,
1739 if (impl->shortcuts_pane_filter_model)
1740 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1742 if (impl->shortcuts_combo_filter_model)
1743 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1745 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1747 /* The current folder is updated via _activate_iter(), don't
1748 * have save_folder_combo_changed_cb() call _activate_iter()
1751 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1753 if (impl->has_search)
1756 if (impl->has_recent)
1759 g_signal_handlers_block_by_func (impl->save_folder_combo,
1760 G_CALLBACK (save_folder_combo_changed_cb),
1763 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1764 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1765 G_CALLBACK (save_folder_combo_changed_cb),
1769 g_free (label_copy);
1772 g_object_unref (pixbuf);
1774 profile_end ("end", NULL);
1778 shortcuts_append_search (GtkFileChooserDefault *impl)
1783 pixbuf = render_search_icon (impl);
1785 gtk_list_store_append (impl->shortcuts_model, &iter);
1786 gtk_list_store_set (impl->shortcuts_model, &iter,
1787 SHORTCUTS_COL_PIXBUF, pixbuf,
1788 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1789 SHORTCUTS_COL_NAME, _("Search"),
1790 SHORTCUTS_COL_DATA, NULL,
1791 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1792 SHORTCUTS_COL_REMOVABLE, FALSE,
1796 g_object_unref (pixbuf);
1798 impl->has_search = TRUE;
1802 shortcuts_append_recent (GtkFileChooserDefault *impl)
1807 pixbuf = render_recent_icon (impl);
1809 gtk_list_store_append (impl->shortcuts_model, &iter);
1810 gtk_list_store_set (impl->shortcuts_model, &iter,
1811 SHORTCUTS_COL_PIXBUF, pixbuf,
1812 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1813 SHORTCUTS_COL_NAME, _("Recently Used"),
1814 SHORTCUTS_COL_DATA, NULL,
1815 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1816 SHORTCUTS_COL_REMOVABLE, FALSE,
1820 g_object_unref (pixbuf);
1822 impl->has_recent = TRUE;
1825 /* Appends an item for the user's home directory to the shortcuts model */
1827 shortcuts_append_home (GtkFileChooserDefault *impl)
1829 const char *home_path;
1832 profile_start ("start", NULL);
1834 home_path = g_get_home_dir ();
1835 if (home_path == NULL)
1837 profile_end ("end - no home directory!?", NULL);
1841 home = g_file_new_for_path (home_path);
1842 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1843 impl->has_home = TRUE;
1845 g_object_unref (home);
1847 profile_end ("end", NULL);
1850 /* Appends the ~/Desktop directory to the shortcuts model */
1852 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1857 profile_start ("start", NULL);
1859 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1860 /* "To disable a directory, point it to the homedir."
1861 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1863 if (!g_strcmp0 (name, g_get_home_dir ()))
1865 profile_end ("end", NULL);
1869 file = g_file_new_for_path (name);
1870 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1871 impl->has_desktop = TRUE;
1873 /* We do not actually pop up an error dialog if there is no desktop directory
1874 * because some people may really not want to have one.
1877 g_object_unref (file);
1879 profile_end ("end", NULL);
1882 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1884 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1891 profile_start ("start", NULL);
1893 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1896 for (; bookmarks; bookmarks = bookmarks->next)
1900 file = bookmarks->data;
1902 if (impl->local_only && !g_file_is_native (file))
1905 if (shortcut_find_position (impl, file) != -1)
1908 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1910 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1916 profile_end ("end", NULL);
1918 return num_inserted;
1921 /* Returns the index for the corresponding item in the shortcuts bar */
1923 shortcuts_get_index (GtkFileChooserDefault *impl,
1924 ShortcutsIndex where)
1930 if (where == SHORTCUTS_SEARCH)
1933 n += impl->has_search ? 1 : 0;
1935 if (where == SHORTCUTS_RECENT)
1938 n += impl->has_recent ? 1 : 0;
1940 if (where == SHORTCUTS_RECENT_SEPARATOR)
1943 n += impl->has_recent ? 1 : 0;
1945 if (where == SHORTCUTS_HOME)
1948 n += impl->has_home ? 1 : 0;
1950 if (where == SHORTCUTS_DESKTOP)
1953 n += impl->has_desktop ? 1 : 0;
1955 if (where == SHORTCUTS_VOLUMES)
1958 n += impl->num_volumes;
1960 if (where == SHORTCUTS_SHORTCUTS)
1963 n += impl->num_shortcuts;
1965 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1968 /* If there are no bookmarks there won't be a separator */
1969 n += (impl->num_bookmarks > 0) ? 1 : 0;
1971 if (where == SHORTCUTS_BOOKMARKS)
1974 n += impl->num_bookmarks;
1976 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1981 if (where == SHORTCUTS_CURRENT_FOLDER)
1984 g_assert_not_reached ();
1991 /* Adds all the file system volumes to the shortcuts model */
1993 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1998 gboolean old_changing_folders;
2000 profile_start ("start", NULL);
2002 old_changing_folders = impl->changing_folder;
2003 impl->changing_folder = TRUE;
2005 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2006 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2007 impl->num_volumes = 0;
2009 list = _gtk_file_system_list_volumes (impl->file_system);
2013 for (l = list; l; l = l->next)
2015 GtkFileSystemVolume *volume;
2019 if (impl->local_only)
2021 if (_gtk_file_system_volume_is_mounted (volume))
2024 gboolean base_is_native = TRUE;
2026 base_file = _gtk_file_system_volume_get_root (volume);
2027 if (base_file != NULL)
2029 base_is_native = g_file_is_native (base_file);
2030 g_object_unref (base_file);
2033 if (!base_is_native)
2038 shortcuts_insert_file (impl,
2040 SHORTCUT_TYPE_VOLUME,
2041 _gtk_file_system_volume_ref (volume),
2049 impl->num_volumes = n;
2050 g_slist_free (list);
2052 if (impl->shortcuts_pane_filter_model)
2053 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2055 if (impl->shortcuts_combo_filter_model)
2056 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2058 impl->changing_folder = old_changing_folders;
2060 profile_end ("end", NULL);
2063 /* Inserts a separator node in the shortcuts list */
2065 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2066 ShortcutsIndex where)
2070 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2071 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2072 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2074 gtk_list_store_insert (impl->shortcuts_model, &iter,
2075 shortcuts_get_index (impl, where));
2076 gtk_list_store_set (impl->shortcuts_model, &iter,
2077 SHORTCUTS_COL_PIXBUF, NULL,
2078 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2079 SHORTCUTS_COL_NAME, NULL,
2080 SHORTCUTS_COL_DATA, NULL,
2081 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2085 /* Updates the list of bookmarks */
2087 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2090 gboolean old_changing_folders;
2092 GFile *list_selected = NULL;
2093 GFile *combo_selected = NULL;
2094 ShortcutType shortcut_type;
2097 profile_start ("start", NULL);
2099 old_changing_folders = impl->changing_folder;
2100 impl->changing_folder = TRUE;
2102 if (shortcuts_get_selected (impl, &iter))
2104 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2106 SHORTCUTS_COL_DATA, &col_data,
2107 SHORTCUTS_COL_TYPE, &shortcut_type,
2110 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2111 list_selected = g_object_ref (col_data);
2114 if (impl->save_folder_combo &&
2115 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2118 GtkTreeIter child_iter;
2120 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2123 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2125 SHORTCUTS_COL_DATA, &col_data,
2126 SHORTCUTS_COL_TYPE, &shortcut_type,
2129 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2130 combo_selected = g_object_ref (col_data);
2133 if (impl->num_bookmarks > 0)
2134 shortcuts_remove_rows (impl,
2135 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2136 impl->num_bookmarks + 1);
2138 impl->num_bookmarks = 0;
2139 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2141 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2142 shortcuts_append_bookmarks (impl, bookmarks);
2143 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2144 g_slist_free (bookmarks);
2146 if (impl->num_bookmarks == 0)
2147 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2149 if (impl->shortcuts_pane_filter_model)
2150 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2152 if (impl->shortcuts_combo_filter_model)
2153 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2157 shortcuts_find_folder (impl, list_selected);
2158 g_object_unref (list_selected);
2165 pos = shortcut_find_position (impl, combo_selected);
2168 if (impl->has_search)
2171 if (impl->has_recent)
2174 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2177 g_object_unref (combo_selected);
2180 impl->changing_folder = old_changing_folders;
2182 profile_end ("end", NULL);
2185 /* Appends a separator and a row to the shortcuts list for the current folder */
2187 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2191 g_assert (!impl->shortcuts_current_folder_active);
2193 g_assert (impl->current_folder != NULL);
2195 pos = shortcut_find_position (impl, impl->current_folder);
2198 GtkFileSystemVolume *volume;
2202 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2205 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2207 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2209 base_file = _gtk_file_system_volume_get_root (volume);
2213 if (base_file && g_file_equal (base_file, impl->current_folder))
2214 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2216 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2219 g_object_unref (base_file);
2221 else if (impl->save_folder_combo != NULL)
2223 if (impl->has_search)
2226 if (impl->has_recent)
2227 pos -= 2; /* + separator */
2229 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2233 /* Updates the current folder row in the shortcuts model */
2235 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2239 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2241 if (impl->shortcuts_current_folder_active)
2243 shortcuts_remove_rows (impl, pos, 2);
2244 impl->shortcuts_current_folder_active = FALSE;
2247 shortcuts_add_current_folder (impl);
2250 /* Filter function used for the shortcuts filter model */
2252 shortcuts_pane_filter_cb (GtkTreeModel *model,
2256 GtkFileChooserDefault *impl;
2260 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2262 path = gtk_tree_model_get_path (model, iter);
2266 pos = *gtk_tree_path_get_indices (path);
2267 gtk_tree_path_free (path);
2269 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2272 /* Creates the list model for shortcuts */
2274 shortcuts_model_create (GtkFileChooserDefault *impl)
2276 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2277 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2278 GDK_TYPE_PIXBUF, /* pixbuf */
2279 G_TYPE_STRING, /* name */
2280 G_TYPE_POINTER, /* path or volume */
2281 G_TYPE_INT, /* ShortcutType */
2282 G_TYPE_BOOLEAN, /* removable */
2283 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2284 G_TYPE_POINTER); /* GCancellable */
2286 shortcuts_append_search (impl);
2288 if (impl->recent_manager)
2290 shortcuts_append_recent (impl);
2291 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2294 if (impl->file_system)
2296 shortcuts_append_home (impl);
2297 shortcuts_append_desktop (impl);
2298 shortcuts_add_volumes (impl);
2301 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2302 GTK_TREE_MODEL (impl->shortcuts_model),
2305 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2306 shortcuts_pane_filter_cb,
2311 /* Callback used when the "New Folder" button is clicked */
2313 new_folder_button_clicked (GtkButton *button,
2314 GtkFileChooserDefault *impl)
2319 if (!impl->browse_files_model)
2320 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2322 /* Prevent button from being clicked twice */
2323 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2325 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2327 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2328 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2329 path, impl->list_name_column,
2332 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2333 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2335 impl->list_name_column,
2338 gtk_tree_path_free (path);
2342 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2346 source = g_idle_source_new ();
2347 g_source_set_closure (source,
2348 g_cclosure_new_object (callback, G_OBJECT (impl)));
2349 g_source_attach (source, NULL);
2354 /* Idle handler for creating a new folder after editing its name cell, or for
2355 * canceling the editing.
2358 edited_idle_cb (GtkFileChooserDefault *impl)
2360 GDK_THREADS_ENTER ();
2362 g_source_destroy (impl->edited_idle);
2363 impl->edited_idle = NULL;
2365 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2366 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2368 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2370 if (impl->edited_new_text /* not cancelled? */
2371 && (strlen (impl->edited_new_text) != 0)
2372 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2374 GError *error = NULL;
2377 file = g_file_get_child_for_display_name (impl->current_folder,
2378 impl->edited_new_text,
2382 GError *error = NULL;
2384 if (g_file_make_directory (file, NULL, &error))
2385 change_folder_and_display_error (impl, file, FALSE);
2387 error_creating_folder_dialog (impl, file, error);
2389 g_object_unref (file);
2392 error_creating_folder_dialog (impl, file, error);
2394 g_free (impl->edited_new_text);
2395 impl->edited_new_text = NULL;
2398 GDK_THREADS_LEAVE ();
2404 queue_edited_idle (GtkFileChooserDefault *impl,
2405 const gchar *new_text)
2407 /* We create the folder in an idle handler so that we don't modify the tree
2411 if (!impl->edited_idle)
2412 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2414 g_free (impl->edited_new_text);
2415 impl->edited_new_text = g_strdup (new_text);
2418 /* Callback used from the text cell renderer when the new folder is named */
2420 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2422 const gchar *new_text,
2423 GtkFileChooserDefault *impl)
2425 /* work around bug #154921 */
2426 g_object_set (cell_renderer_text,
2427 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2428 queue_edited_idle (impl, new_text);
2431 /* Callback used from the text cell renderer when the new folder edition gets
2435 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2436 GtkFileChooserDefault *impl)
2438 /* work around bug #154921 */
2439 g_object_set (cell_renderer_text,
2440 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2441 queue_edited_idle (impl, NULL);
2444 /* Creates the widgets for the filter combo box */
2446 filter_create (GtkFileChooserDefault *impl)
2448 GtkCellRenderer *cell;
2451 impl->filter_combo = gtk_combo_box_text_new ();
2452 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2454 /* Get the combo's text renderer and set ellipsize parameters */
2455 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2459 g_object_set (G_OBJECT (cell),
2460 "ellipsize", PANGO_ELLIPSIZE_END,
2463 g_list_free (cells);
2465 g_signal_connect (impl->filter_combo, "changed",
2466 G_CALLBACK (filter_combo_changed), impl);
2468 gtk_widget_set_tooltip_text (impl->filter_combo,
2469 _("Select which types of files are shown"));
2471 return impl->filter_combo;
2475 toolbutton_new (GtkFileChooserDefault *impl,
2484 item = gtk_tool_button_new (NULL, NULL);
2485 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2486 gtk_widget_show (image);
2487 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2489 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2490 g_signal_connect (item, "clicked", callback, impl);
2493 gtk_widget_show (GTK_WIDGET (item));
2495 return GTK_WIDGET (item);
2498 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2500 shortcut_find_position (GtkFileChooserDefault *impl,
2505 int current_folder_separator_idx;
2507 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2510 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2513 /* FIXME: is this still needed? */
2514 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2518 for (i = 0; i < current_folder_separator_idx; i++)
2521 ShortcutType shortcut_type;
2523 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2524 SHORTCUTS_COL_DATA, &col_data,
2525 SHORTCUTS_COL_TYPE, &shortcut_type,
2530 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2532 GtkFileSystemVolume *volume;
2537 base_file = _gtk_file_system_volume_get_root (volume);
2539 exists = base_file && g_file_equal (file, base_file);
2542 g_object_unref (base_file);
2547 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2551 model_file = col_data;
2553 if (model_file && g_file_equal (model_file, file))
2558 if (i < current_folder_separator_idx - 1)
2560 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2561 g_assert_not_reached ();
2568 /* Tries to add a bookmark from a path name */
2570 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2576 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2578 if (shortcut_find_position (impl, file) != -1)
2582 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2584 error_adding_bookmark_dialog (impl, file, error);
2592 add_bookmark_foreach_cb (GtkTreeModel *model,
2597 GtkFileChooserDefault *impl;
2600 impl = (GtkFileChooserDefault *) data;
2602 gtk_tree_model_get (model, iter,
2603 MODEL_COL_FILE, &file,
2606 shortcuts_add_bookmark_from_file (impl, file, -1);
2608 g_object_unref (file);
2611 /* Adds a bookmark from the currently selected item in the file list */
2613 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2615 GtkTreeSelection *selection;
2617 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2619 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2620 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2622 gtk_tree_selection_selected_foreach (selection,
2623 add_bookmark_foreach_cb,
2627 /* Callback used when the "Add bookmark" button is clicked */
2629 add_bookmark_button_clicked_cb (GtkButton *button,
2630 GtkFileChooserDefault *impl)
2632 bookmarks_add_selected_folder (impl);
2635 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2636 * returns FALSE if no shortcut is selected.
2639 shortcuts_get_selected (GtkFileChooserDefault *impl,
2642 GtkTreeSelection *selection;
2643 GtkTreeIter parent_iter;
2645 if (!impl->browse_shortcuts_tree_view)
2648 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2650 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2653 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2659 /* Removes the selected bookmarks */
2661 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2669 if (!shortcuts_get_selected (impl, &iter))
2672 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2673 SHORTCUTS_COL_DATA, &col_data,
2674 SHORTCUTS_COL_REMOVABLE, &removable,
2680 g_assert (col_data != NULL);
2685 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2686 error_removing_bookmark_dialog (impl, file, error);
2689 /* Callback used when the "Remove bookmark" button is clicked */
2691 remove_bookmark_button_clicked_cb (GtkButton *button,
2692 GtkFileChooserDefault *impl)
2694 remove_selected_bookmarks (impl);
2697 struct selection_check_closure {
2698 GtkFileChooserDefault *impl;
2701 gboolean all_folders;
2704 /* Used from gtk_tree_selection_selected_foreach() */
2706 selection_check_foreach_cb (GtkTreeModel *model,
2711 struct selection_check_closure *closure;
2715 gtk_tree_model_get (model, iter,
2716 MODEL_COL_FILE, &file,
2717 MODEL_COL_IS_FOLDER, &is_folder,
2723 g_object_unref (file);
2726 closure->num_selected++;
2728 closure->all_folders = closure->all_folders && is_folder;
2729 closure->all_files = closure->all_files && !is_folder;
2732 /* Checks whether the selected items in the file list are all files or all folders */
2734 selection_check (GtkFileChooserDefault *impl,
2736 gboolean *all_files,
2737 gboolean *all_folders)
2739 struct selection_check_closure closure;
2740 GtkTreeSelection *selection;
2742 closure.impl = impl;
2743 closure.num_selected = 0;
2744 closure.all_files = TRUE;
2745 closure.all_folders = TRUE;
2747 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2748 gtk_tree_selection_selected_foreach (selection,
2749 selection_check_foreach_cb,
2752 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2755 *num_selected = closure.num_selected;
2758 *all_files = closure.all_files;
2761 *all_folders = closure.all_folders;
2764 struct get_selected_file_closure {
2765 GtkFileChooserDefault *impl;
2770 get_selected_file_foreach_cb (GtkTreeModel *model,
2775 struct get_selected_file_closure *closure = data;
2779 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2780 g_object_unref (closure->file);
2781 closure->file = NULL;
2784 gtk_tree_model_get (model, iter,
2785 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2789 /* Returns a selected path from the file list */
2791 get_selected_file (GtkFileChooserDefault *impl)
2793 struct get_selected_file_closure closure;
2794 GtkTreeSelection *selection;
2796 closure.impl = impl;
2797 closure.file = NULL;
2799 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2800 gtk_tree_selection_selected_foreach (selection,
2801 get_selected_file_foreach_cb,
2804 return closure.file;
2808 GtkFileChooserDefault *impl;
2810 } UpdateTooltipData;
2813 update_tooltip (GtkTreeModel *model,
2818 UpdateTooltipData *udata = data;
2820 if (udata->tip == NULL)
2822 gchar *display_name;
2824 gtk_tree_model_get (model, iter,
2825 MODEL_COL_NAME, &display_name,
2828 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2830 g_free (display_name);
2835 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2836 * if there are no selected items *and* the current folder is not in the
2837 * bookmarks list. De-sensitize the button otherwise.
2840 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2843 gboolean all_folders;
2847 selection_check (impl, &num_selected, NULL, &all_folders);
2849 if (num_selected == 0)
2850 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2851 else if (num_selected == 1)
2855 file = get_selected_file (impl);
2856 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2858 g_object_unref (file);
2861 active = all_folders;
2863 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2865 if (impl->browse_files_popup_menu_add_shortcut_item)
2866 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2867 (num_selected == 0) ? FALSE : active);
2871 if (num_selected == 0)
2872 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2873 else if (num_selected > 1)
2874 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2877 GtkTreeSelection *selection;
2878 UpdateTooltipData data;
2880 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2883 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2887 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2892 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2893 * bookmark row is selected in the shortcuts tree.
2896 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2899 gboolean removable = FALSE;
2903 if (shortcuts_get_selected (impl, &iter))
2905 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2906 SHORTCUTS_COL_REMOVABLE, &removable,
2907 SHORTCUTS_COL_NAME, &name,
2909 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2912 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2914 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2916 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2920 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2921 _("Remove the selected bookmark"));
2926 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2929 gboolean removable = FALSE;
2931 if (impl->browse_shortcuts_popup_menu == NULL)
2934 if (shortcuts_get_selected (impl, &iter))
2935 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2936 SHORTCUTS_COL_REMOVABLE, &removable,
2939 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2940 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2943 /* GtkWidget::drag-begin handler for the shortcuts list. */
2945 shortcuts_drag_begin_cb (GtkWidget *widget,
2946 GdkDragContext *context,
2947 GtkFileChooserDefault *impl)
2950 impl->shortcuts_drag_context = g_object_ref (context);
2955 /* Removes the idle handler for outside drags */
2957 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2959 if (!impl->shortcuts_drag_outside_idle)
2962 g_source_destroy (impl->shortcuts_drag_outside_idle);
2963 impl->shortcuts_drag_outside_idle = NULL;
2967 /* GtkWidget::drag-end handler for the shortcuts list. */
2969 shortcuts_drag_end_cb (GtkWidget *widget,
2970 GdkDragContext *context,
2971 GtkFileChooserDefault *impl)
2974 g_object_unref (impl->shortcuts_drag_context);
2976 shortcuts_cancel_drag_outside_idle (impl);
2978 if (!impl->shortcuts_drag_outside)
2981 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2983 impl->shortcuts_drag_outside = FALSE;
2987 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2989 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2990 GdkDragContext *context,
2991 GtkFileChooserDefault *impl)
2993 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2996 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3000 shortcuts_drag_leave_cb (GtkWidget *widget,
3001 GdkDragContext *context,
3003 GtkFileChooserDefault *impl)
3006 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3008 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
3012 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3014 GTK_TREE_VIEW_DROP_BEFORE);
3016 g_signal_stop_emission_by_name (widget, "drag-leave");
3019 /* Computes the appropriate row and position for dropping */
3021 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3025 GtkTreeViewDropPosition *pos)
3027 GtkTreeView *tree_view;
3028 GtkTreeViewColumn *column;
3032 int bookmarks_index;
3033 int header_height = 0;
3035 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3037 if (gtk_tree_view_get_headers_visible (tree_view))
3038 header_height = _gtk_tree_view_get_header_height (tree_view);
3040 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3042 if (!gtk_tree_view_get_path_at_pos (tree_view,
3050 row = bookmarks_index + impl->num_bookmarks - 1;
3051 *path = gtk_tree_path_new_from_indices (row, -1);
3052 *pos = GTK_TREE_VIEW_DROP_AFTER;
3056 row = *gtk_tree_path_get_indices (*path);
3057 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3058 gtk_tree_path_free (*path);
3060 if (row < bookmarks_index)
3062 row = bookmarks_index;
3063 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3065 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3067 row = bookmarks_index + impl->num_bookmarks - 1;
3068 *pos = GTK_TREE_VIEW_DROP_AFTER;
3072 if (cell_y < cell.height / 2)
3073 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3075 *pos = GTK_TREE_VIEW_DROP_AFTER;
3078 *path = gtk_tree_path_new_from_indices (row, -1);
3081 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3082 * implement the destination side of DnD by hand, due to limitations in
3083 * GtkTreeView's DnD API.
3086 shortcuts_drag_motion_cb (GtkWidget *widget,
3087 GdkDragContext *context,
3091 GtkFileChooserDefault *impl)
3094 GtkTreeViewDropPosition pos;
3095 GdkDragAction action;
3098 if (gtk_drag_get_source_widget (context) == widget)
3100 shortcuts_cancel_drag_outside_idle (impl);
3102 if (impl->shortcuts_drag_outside)
3104 shortcuts_drag_set_delete_cursor (impl, FALSE);
3105 impl->shortcuts_drag_outside = FALSE;
3110 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3111 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3112 action = GDK_ACTION_COPY;
3113 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3114 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3115 action = GDK_ACTION_MOVE;
3122 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3123 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3124 gtk_tree_path_free (path);
3128 g_signal_stop_emission_by_name (widget, "drag-motion");
3132 gdk_drag_status (context, action, time_);
3139 /* GtkWidget::drag-drop handler for the shortcuts list. */
3141 shortcuts_drag_drop_cb (GtkWidget *widget,
3142 GdkDragContext *context,
3146 GtkFileChooserDefault *impl)
3149 shortcuts_cancel_drag_outside_idle (impl);
3152 g_signal_stop_emission_by_name (widget, "drag-drop");
3156 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3158 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3159 GtkSelectionData *selection_data,
3165 uris = gtk_selection_data_get_uris (selection_data);
3169 for (i = 0; uris[i]; i++)
3175 file = g_file_new_for_uri (uri);
3177 if (shortcuts_add_bookmark_from_file (impl, file, position))
3180 g_object_unref (file);
3186 /* Reorders the selected bookmark to the specified position */
3188 shortcuts_reorder (GtkFileChooserDefault *impl,
3193 ShortcutType shortcut_type;
3196 int bookmarks_index;
3201 /* Get the selected path */
3203 if (!shortcuts_get_selected (impl, &iter))
3204 g_assert_not_reached ();
3206 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3207 old_position = *gtk_tree_path_get_indices (path);
3208 gtk_tree_path_free (path);
3210 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3211 old_position -= bookmarks_index;
3212 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3214 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3215 SHORTCUTS_COL_NAME, &name,
3216 SHORTCUTS_COL_DATA, &col_data,
3217 SHORTCUTS_COL_TYPE, &shortcut_type,
3219 g_assert (col_data != NULL);
3220 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3223 g_object_ref (file); /* removal below will free file, so we need a new ref */
3225 /* Remove the path from the old position and insert it in the new one */
3227 if (new_position > old_position)
3230 if (old_position == new_position)
3234 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3236 shortcuts_add_bookmark_from_file (impl, file, new_position);
3237 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3240 error_adding_bookmark_dialog (impl, file, error);
3244 g_object_unref (file);
3248 /* Callback used when we get the drag data for the bookmarks list. We add the
3249 * received URIs as bookmarks if they are folders.
3252 shortcuts_drag_data_received_cb (GtkWidget *widget,
3253 GdkDragContext *context,
3256 GtkSelectionData *selection_data,
3261 GtkFileChooserDefault *impl;
3262 GtkTreePath *tree_path;
3263 GtkTreeViewDropPosition tree_pos;
3266 int bookmarks_index;
3268 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3270 /* Compute position */
3272 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3274 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3275 position = *gtk_tree_path_get_indices (tree_path);
3276 gtk_tree_path_free (tree_path);
3278 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3281 g_assert (position >= bookmarks_index);
3282 position -= bookmarks_index;
3284 target = gtk_selection_data_get_target (selection_data);
3286 if (gtk_targets_include_uri (&target, 1))
3287 shortcuts_drop_uris (impl, selection_data, position);
3288 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3289 shortcuts_reorder (impl, position);
3291 g_signal_stop_emission_by_name (widget, "drag-data-received");
3294 /* Callback used to display a tooltip in the shortcuts tree */
3296 shortcuts_query_tooltip_cb (GtkWidget *widget,
3299 gboolean keyboard_mode,
3300 GtkTooltip *tooltip,
3301 GtkFileChooserDefault *impl)
3303 GtkTreeModel *model;
3306 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3314 ShortcutType shortcut_type;
3316 gtk_tree_model_get (model, &iter,
3317 SHORTCUTS_COL_DATA, &col_data,
3318 SHORTCUTS_COL_TYPE, &shortcut_type,
3321 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3323 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3325 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3330 file = G_FILE (col_data);
3331 parse_name = g_file_get_parse_name (file);
3333 gtk_tooltip_set_text (tooltip, parse_name);
3335 g_free (parse_name);
3339 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3343 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3353 /* Callback used when the selection in the shortcuts tree changes */
3355 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3356 GtkFileChooserDefault *impl)
3359 GtkTreeIter child_iter;
3361 bookmarks_check_remove_sensitivity (impl);
3362 shortcuts_check_popup_sensitivity (impl);
3364 if (impl->changing_folder)
3367 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3369 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3372 shortcuts_activate_iter (impl, &child_iter);
3377 shortcuts_row_separator_func (GtkTreeModel *model,
3381 ShortcutType shortcut_type;
3383 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3385 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3389 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3391 GtkFileChooserDefault *impl)
3395 /* don't screw up focus switching with Tab */
3396 if (event->keyval == GDK_KEY_Tab
3397 || event->keyval == GDK_KEY_KP_Tab
3398 || event->keyval == GDK_KEY_ISO_Left_Tab
3399 || event->length < 1)
3402 if (impl->location_entry)
3403 entry = impl->location_entry;
3404 else if (impl->search_entry)
3405 entry = impl->search_entry;
3411 gtk_widget_grab_focus (entry);
3412 return gtk_widget_event (entry, (GdkEvent *) event);
3418 /* Callback used when the file list's popup menu is detached */
3420 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3423 GtkFileChooserDefault *impl;
3425 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3426 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3428 impl->browse_shortcuts_popup_menu = NULL;
3429 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3430 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3434 remove_shortcut_cb (GtkMenuItem *item,
3435 GtkFileChooserDefault *impl)
3437 remove_selected_bookmarks (impl);
3440 /* Rename the selected bookmark */
3442 rename_selected_bookmark (GtkFileChooserDefault *impl)
3446 GtkTreeViewColumn *column;
3447 GtkCellRenderer *cell;
3450 if (shortcuts_get_selected (impl, &iter))
3452 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3453 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3454 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3455 cell = g_list_nth_data (renderers, 1);
3456 g_list_free (renderers);
3457 g_object_set (cell, "editable", TRUE, NULL);
3458 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3459 path, column, cell, TRUE);
3460 gtk_tree_path_free (path);
3465 rename_shortcut_cb (GtkMenuItem *item,
3466 GtkFileChooserDefault *impl)
3468 rename_selected_bookmark (impl);
3471 /* Constructs the popup menu for the file list if needed */
3473 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3477 if (impl->browse_shortcuts_popup_menu)
3480 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3481 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3482 impl->browse_shortcuts_tree_view,
3483 shortcuts_popup_menu_detach_cb);
3485 item = gtk_image_menu_item_new_with_label (_("Remove"));
3486 impl->browse_shortcuts_popup_menu_remove_item = item;
3487 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3488 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3489 g_signal_connect (item, "activate",
3490 G_CALLBACK (remove_shortcut_cb), impl);
3491 gtk_widget_show (item);
3492 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3494 item = gtk_menu_item_new_with_label (_("Rename..."));
3495 impl->browse_shortcuts_popup_menu_rename_item = item;
3496 g_signal_connect (item, "activate",
3497 G_CALLBACK (rename_shortcut_cb), impl);
3498 gtk_widget_show (item);
3499 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3503 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3505 shortcuts_build_popup_menu (impl);
3506 shortcuts_check_popup_sensitivity (impl);
3510 popup_position_func (GtkMenu *menu,
3514 gpointer user_data);
3517 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3518 GdkEventButton *event)
3520 shortcuts_update_popup_menu (impl);
3522 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3523 NULL, NULL, NULL, NULL,
3524 event->button, event->time);
3527 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3529 popup_position_func, impl->browse_shortcuts_tree_view,
3530 0, GDK_CURRENT_TIME);
3531 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3536 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3538 shortcuts_popup_menu_cb (GtkWidget *widget,
3539 GtkFileChooserDefault *impl)
3541 shortcuts_popup_menu (impl, NULL);
3545 /* Callback used when a button is pressed on the shortcuts list.
3546 * We trap button 3 to bring up a popup menu.
3549 shortcuts_button_press_event_cb (GtkWidget *widget,
3550 GdkEventButton *event,
3551 GtkFileChooserDefault *impl)
3553 static gboolean in_press = FALSE;
3559 if (event->button != 3)
3563 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3569 shortcuts_popup_menu (impl, event);
3574 shortcuts_edited (GtkCellRenderer *cell,
3577 GtkFileChooserDefault *impl)
3583 g_object_set (cell, "editable", FALSE, NULL);
3585 path = gtk_tree_path_new_from_string (path_string);
3586 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3587 g_assert_not_reached ();
3589 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3590 SHORTCUTS_COL_DATA, &shortcut,
3592 gtk_tree_path_free (path);
3594 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3598 shortcuts_editing_canceled (GtkCellRenderer *cell,
3599 GtkFileChooserDefault *impl)
3601 g_object_set (cell, "editable", FALSE, NULL);
3604 /* Creates the widgets for the shortcuts and bookmarks tree */
3606 shortcuts_list_create (GtkFileChooserDefault *impl)
3609 GtkTreeSelection *selection;
3610 GtkTreeViewColumn *column;
3611 GtkCellRenderer *renderer;
3613 /* Target types for dragging a row to/from the shortcuts list */
3614 const GtkTargetEntry tree_model_row_targets[] = {
3615 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3618 /* Scrolled window */
3620 swin = gtk_scrolled_window_new (NULL, NULL);
3621 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3622 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3623 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3625 gtk_widget_show (swin);
3628 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3629 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3630 GTK_STYLE_CLASS_SIDEBAR);
3631 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3632 #ifdef PROFILE_FILE_CHOOSER
3633 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3636 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3638 * 1. user brings up a SAVE dialog
3639 * 2. user clicks on a shortcut in the shortcuts pane
3640 * 3. user starts typing a filename
3642 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3643 * support interactive search. However, we'd rather focus the location entry
3644 * so that the user can type *there*.
3646 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3647 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3648 * if the user starts typing while the focus is in the shortcuts pane.
3650 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3651 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3653 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3654 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3655 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3656 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3657 /* Accessible object name for the file chooser's shortcuts pane */
3658 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3660 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3662 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3664 tree_model_row_targets,
3665 G_N_ELEMENTS (tree_model_row_targets),
3668 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3669 GTK_DEST_DEFAULT_ALL,
3670 tree_model_row_targets,
3671 G_N_ELEMENTS (tree_model_row_targets),
3672 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3673 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3675 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3676 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3677 gtk_tree_selection_set_select_function (selection,
3678 shortcuts_select_func,
3681 g_signal_connect (selection, "changed",
3682 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3684 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3685 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3687 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3688 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3689 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3690 G_CALLBACK (shortcuts_drag_end_cb), impl);
3691 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3692 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3694 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3695 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3696 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3697 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3698 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3699 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3700 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3701 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3703 /* Support tooltips */
3704 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3705 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3706 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3708 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3709 gtk_widget_show (impl->browse_shortcuts_tree_view);
3713 column = gtk_tree_view_column_new ();
3714 /* Column header for the file chooser's shortcuts pane */
3715 gtk_tree_view_column_set_title (column, _("_Places"));
3717 renderer = gtk_cell_renderer_pixbuf_new ();
3718 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3719 gtk_tree_view_column_set_attributes (column, renderer,
3720 "pixbuf", SHORTCUTS_COL_PIXBUF,
3721 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3724 renderer = gtk_cell_renderer_text_new ();
3725 g_object_set (renderer,
3727 "ellipsize", PANGO_ELLIPSIZE_END,
3729 g_signal_connect (renderer, "edited",
3730 G_CALLBACK (shortcuts_edited), impl);
3731 g_signal_connect (renderer, "editing-canceled",
3732 G_CALLBACK (shortcuts_editing_canceled), impl);
3733 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3734 gtk_tree_view_column_set_attributes (column, renderer,
3735 "text", SHORTCUTS_COL_NAME,
3738 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3739 shortcuts_row_separator_func,
3742 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3747 /* Creates the widgets for the shortcuts/bookmarks pane */
3749 shortcuts_pane_create (GtkFileChooserDefault *impl,
3750 GtkSizeGroup *size_group)
3755 GtkStyleContext *context;
3758 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3759 gtk_widget_show (vbox);
3761 /* Shortcuts tree */
3763 widget = shortcuts_list_create (impl);
3765 gtk_size_group_add_widget (size_group, widget);
3766 context = gtk_widget_get_style_context (widget);
3767 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3769 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3771 /* Box for buttons */
3773 toolbar = gtk_toolbar_new ();
3774 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3776 context = gtk_widget_get_style_context (toolbar);
3777 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3778 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3780 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3781 gtk_widget_show (toolbar);
3783 /* Add bookmark button */
3784 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3785 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3789 G_CALLBACK (add_bookmark_button_clicked_cb));
3790 g_object_unref (icon);
3792 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3793 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3794 _("Add the selected folder to the Bookmarks"));
3796 /* Remove bookmark button */
3797 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3798 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3802 G_CALLBACK (remove_bookmark_button_clicked_cb));
3803 g_object_unref (icon);
3804 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3805 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3806 _("Remove the selected bookmark"));
3812 key_is_left_or_right (GdkEventKey *event)
3816 modifiers = gtk_accelerator_get_default_mod_mask ();
3818 return ((event->keyval == GDK_KEY_Right
3819 || event->keyval == GDK_KEY_KP_Right
3820 || event->keyval == GDK_KEY_Left
3821 || event->keyval == GDK_KEY_KP_Left)
3822 && (event->state & modifiers) == 0);
3825 /* Handles key press events on the file list, so that we can trap Enter to
3826 * activate the default button on our own. Also, checks to see if '/' has been
3827 * pressed. See comment by tree_view_keybinding_cb() for more details.
3830 browse_files_key_press_event_cb (GtkWidget *widget,
3834 GtkFileChooserDefault *impl;
3837 impl = (GtkFileChooserDefault *) data;
3839 modifiers = gtk_accelerator_get_default_mod_mask ();
3841 if ((event->keyval == GDK_KEY_slash
3842 || event->keyval == GDK_KEY_KP_Divide
3844 || event->keyval == GDK_KEY_asciitilde
3846 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3848 location_popup_handler (impl, event->string);
3852 if (key_is_left_or_right (event))
3854 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3858 if ((event->keyval == GDK_KEY_Return
3859 || event->keyval == GDK_KEY_ISO_Enter
3860 || event->keyval == GDK_KEY_KP_Enter
3861 || event->keyval == GDK_KEY_space
3862 || event->keyval == GDK_KEY_KP_Space)
3863 && ((event->state & modifiers) == 0)
3864 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3865 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3869 window = get_toplevel (widget);
3872 GtkWidget *default_widget, *focus_widget;
3874 default_widget = gtk_window_get_default_widget (window);
3875 focus_widget = gtk_window_get_focus (window);
3877 if (widget != default_widget &&
3878 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3880 gtk_window_activate_default (window);
3890 /* Callback used when the file list's popup menu is detached */
3892 popup_menu_detach_cb (GtkWidget *attach_widget,
3895 GtkFileChooserDefault *impl;
3897 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3898 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3900 impl->browse_files_popup_menu = NULL;
3901 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3902 impl->browse_files_popup_menu_hidden_files_item = NULL;
3905 /* Callback used when the "Add to Bookmarks" menu item is activated */
3907 add_to_shortcuts_cb (GtkMenuItem *item,
3908 GtkFileChooserDefault *impl)
3910 bookmarks_add_selected_folder (impl);
3913 /* Callback used when the "Show Hidden Files" menu item is toggled */
3915 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3916 GtkFileChooserDefault *impl)
3919 "show-hidden", gtk_check_menu_item_get_active (item),
3923 /* Callback used when the "Show Size Column" menu item is toggled */
3925 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3926 GtkFileChooserDefault *impl)
3928 impl->show_size_column = gtk_check_menu_item_get_active (item);
3930 gtk_tree_view_column_set_visible (impl->list_size_column,
3931 impl->show_size_column);
3934 /* Shows an error dialog about not being able to select a dragged file */
3936 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3941 _("Could not select file"),
3946 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3951 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3953 for (i = 1; uris[i]; i++)
3956 GError *error = NULL;
3959 file = g_file_new_for_uri (uri);
3961 gtk_file_chooser_default_select_file (chooser, file, &error);
3963 error_selecting_dragged_file_dialog (impl, file, error);
3965 g_object_unref (file);
3969 struct FileListDragData
3971 GtkFileChooserDefault *impl;
3977 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3979 const GError *error,
3982 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3983 struct FileListDragData *data = user_data;
3984 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3986 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3989 data->impl->file_list_drag_data_received_cancellable = NULL;
3991 if (cancelled || error)
3994 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3995 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3996 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3997 change_folder_and_display_error (data->impl, data->file, FALSE);
4000 GError *error = NULL;
4002 gtk_file_chooser_default_unselect_all (chooser);
4003 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4005 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4007 browse_files_center_selected_row (data->impl);
4010 if (data->impl->select_multiple)
4011 file_list_drag_data_select_uris (data->impl, data->uris);
4014 g_object_unref (data->impl);
4015 g_strfreev (data->uris);
4016 g_object_unref (data->file);
4019 g_object_unref (cancellable);
4023 file_list_drag_data_received_cb (GtkWidget *widget,
4024 GdkDragContext *context,
4027 GtkSelectionData *selection_data,
4032 GtkFileChooserDefault *impl;
4037 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4039 /* Allow only drags from other widgets; see bug #533891. */
4040 if (gtk_drag_get_source_widget (context) == widget)
4042 g_signal_stop_emission_by_name (widget, "drag-data-received");
4046 /* Parse the text/uri-list string, navigate to the first one */
4047 uris = gtk_selection_data_get_uris (selection_data);
4048 if (uris && uris[0])
4050 struct FileListDragData *data;
4053 file = g_file_new_for_uri (uri);
4055 data = g_new0 (struct FileListDragData, 1);
4056 data->impl = g_object_ref (impl);
4060 if (impl->file_list_drag_data_received_cancellable)
4061 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4063 impl->file_list_drag_data_received_cancellable =
4064 _gtk_file_system_get_info (impl->file_system, file,
4066 file_list_drag_data_received_get_info_cb,
4070 g_signal_stop_emission_by_name (widget, "drag-data-received");
4073 /* Don't do anything with the drag_drop signal */
4075 file_list_drag_drop_cb (GtkWidget *widget,
4076 GdkDragContext *context,
4080 GtkFileChooserDefault *impl)
4082 g_signal_stop_emission_by_name (widget, "drag-drop");
4086 /* Disable the normal tree drag motion handler, it makes it look like you're
4087 dropping the dragged item onto a tree item */
4089 file_list_drag_motion_cb (GtkWidget *widget,
4090 GdkDragContext *context,
4094 GtkFileChooserDefault *impl)
4096 g_signal_stop_emission_by_name (widget, "drag-motion");
4100 /* Constructs the popup menu for the file list if needed */
4102 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4106 if (impl->browse_files_popup_menu)
4109 impl->browse_files_popup_menu = gtk_menu_new ();
4110 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4111 impl->browse_files_tree_view,
4112 popup_menu_detach_cb);
4114 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4115 impl->browse_files_popup_menu_add_shortcut_item = item;
4116 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4117 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4118 g_signal_connect (item, "activate",
4119 G_CALLBACK (add_to_shortcuts_cb), impl);
4120 gtk_widget_show (item);
4121 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4123 item = gtk_separator_menu_item_new ();
4124 gtk_widget_show (item);
4125 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4127 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4128 impl->browse_files_popup_menu_hidden_files_item = item;
4129 g_signal_connect (item, "toggled",
4130 G_CALLBACK (show_hidden_toggled_cb), impl);
4131 gtk_widget_show (item);
4132 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4134 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4135 impl->browse_files_popup_menu_size_column_item = item;
4136 g_signal_connect (item, "toggled",
4137 G_CALLBACK (show_size_column_toggled_cb), impl);
4138 gtk_widget_show (item);
4139 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4141 bookmarks_check_add_sensitivity (impl);
4144 /* Updates the popup menu for the file list, creating it if necessary */
4146 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4148 file_list_build_popup_menu (impl);
4150 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4152 /* The sensitivity of the Add to Bookmarks item is set in
4153 * bookmarks_check_add_sensitivity()
4156 /* 'Show Hidden Files' */
4157 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4158 G_CALLBACK (show_hidden_toggled_cb), impl);
4159 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4161 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4162 G_CALLBACK (show_hidden_toggled_cb), impl);
4164 /* 'Show Size Column' */
4165 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4166 G_CALLBACK (show_size_column_toggled_cb), impl);
4167 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4168 impl->show_size_column);
4169 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4170 G_CALLBACK (show_size_column_toggled_cb), impl);
4174 popup_position_func (GtkMenu *menu,
4180 GtkAllocation allocation;
4181 GtkWidget *widget = GTK_WIDGET (user_data);
4182 GdkScreen *screen = gtk_widget_get_screen (widget);
4185 GdkRectangle monitor;
4187 g_return_if_fail (gtk_widget_get_realized (widget));
4189 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4191 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4194 gtk_widget_get_allocation (widget, &allocation);
4195 *x += (allocation.width - req.width) / 2;
4196 *y += (allocation.height - req.height) / 2;
4198 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4199 gtk_menu_set_monitor (menu, monitor_num);
4200 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4202 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4203 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4209 file_list_popup_menu (GtkFileChooserDefault *impl,
4210 GdkEventButton *event)
4212 file_list_update_popup_menu (impl);
4214 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4215 NULL, NULL, NULL, NULL,
4216 event->button, event->time);
4219 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4221 popup_position_func, impl->browse_files_tree_view,
4222 0, GDK_CURRENT_TIME);
4223 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4229 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4231 list_popup_menu_cb (GtkWidget *widget,
4232 GtkFileChooserDefault *impl)
4234 file_list_popup_menu (impl, NULL);
4238 /* Callback used when a button is pressed on the file list. We trap button 3 to
4239 * bring up a popup menu.
4242 list_button_press_event_cb (GtkWidget *widget,
4243 GdkEventButton *event,
4244 GtkFileChooserDefault *impl)
4246 static gboolean in_press = FALSE;
4251 if (event->button != 3)
4255 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4258 file_list_popup_menu (impl, event);
4263 OperationMode operation_mode;
4264 gint general_column;
4268 /* Sets the sort column IDs for the file list based on the operation mode */
4270 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4272 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4273 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4274 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4278 file_list_query_tooltip_cb (GtkWidget *widget,
4281 gboolean keyboard_tip,
4282 GtkTooltip *tooltip,
4285 GtkFileChooserDefault *impl = user_data;
4286 GtkTreeModel *model;
4292 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4296 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4299 &model, &path, &iter))
4302 gtk_tree_model_get (model, &iter,
4303 MODEL_COL_FILE, &file,
4308 gtk_tree_path_free (path);
4312 filename = g_file_get_path (file);
4313 gtk_tooltip_set_text (tooltip, filename);
4314 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4319 g_object_unref (file);
4320 gtk_tree_path_free (path);
4326 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4330 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4331 gtk_cell_renderer_set_fixed_size (renderer,
4332 xpad * 2 + impl->icon_size,
4333 ypad * 2 + impl->icon_size);
4336 /* Creates the widgets for the file list */
4338 create_file_list (GtkFileChooserDefault *impl)
4341 GtkTreeSelection *selection;
4342 GtkTreeViewColumn *column;
4343 GtkCellRenderer *renderer;
4345 /* Scrolled window */
4346 swin = gtk_scrolled_window_new (NULL, NULL);
4347 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4348 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4349 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4352 /* Tree/list view */
4354 impl->browse_files_tree_view = gtk_tree_view_new ();
4355 #ifdef PROFILE_FILE_CHOOSER
4356 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4358 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4359 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4361 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4362 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4364 gtk_drag_dest_set (impl->browse_files_tree_view,
4365 GTK_DEST_DEFAULT_ALL,
4367 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4368 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4370 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4371 G_CALLBACK (list_row_activated), impl);
4372 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4373 G_CALLBACK (browse_files_key_press_event_cb), impl);
4374 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4375 G_CALLBACK (list_popup_menu_cb), impl);
4376 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4377 G_CALLBACK (list_button_press_event_cb), impl);
4379 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4380 G_CALLBACK (file_list_drag_data_received_cb), impl);
4381 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4382 G_CALLBACK (file_list_drag_drop_cb), impl);
4383 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4384 G_CALLBACK (file_list_drag_motion_cb), impl);
4386 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4387 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4388 G_CALLBACK (file_list_query_tooltip_cb), impl);
4390 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4391 gtk_tree_selection_set_select_function (selection,
4394 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4397 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4398 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4400 g_signal_connect (selection, "changed",
4401 G_CALLBACK (list_selection_changed), impl);
4403 /* Keep the column order in sync with update_cell_renderer_attributes() */
4405 /* Filename column */
4407 impl->list_name_column = gtk_tree_view_column_new ();
4408 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4409 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4410 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4412 renderer = gtk_cell_renderer_pixbuf_new ();
4413 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4414 set_icon_cell_renderer_fixed_size (impl, renderer);
4415 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4417 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4418 g_object_set (impl->list_name_renderer,
4419 "ellipsize", PANGO_ELLIPSIZE_END,
4421 g_signal_connect (impl->list_name_renderer, "edited",
4422 G_CALLBACK (renderer_edited_cb), impl);
4423 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4424 G_CALLBACK (renderer_editing_canceled_cb), impl);
4425 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4427 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4431 column = gtk_tree_view_column_new ();
4432 gtk_tree_view_column_set_resizable (column, TRUE);
4433 gtk_tree_view_column_set_title (column, _("Size"));
4435 renderer = gtk_cell_renderer_text_new ();
4436 g_object_set (renderer,
4437 "alignment", PANGO_ALIGN_RIGHT,
4439 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4440 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4441 impl->list_size_column = column;
4443 /* Modification time column */
4445 column = gtk_tree_view_column_new ();
4446 gtk_tree_view_column_set_resizable (column, TRUE);
4447 gtk_tree_view_column_set_title (column, _("Modified"));
4449 renderer = gtk_cell_renderer_text_new ();
4450 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4451 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4452 impl->list_mtime_column = column;
4454 file_list_set_sort_column_ids (impl);
4455 update_cell_renderer_attributes (impl);
4457 gtk_widget_show_all (swin);
4463 create_path_bar (GtkFileChooserDefault *impl)
4465 GtkWidget *path_bar;
4467 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4468 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4473 /* Creates the widgets for the files/folders pane */
4475 file_pane_create (GtkFileChooserDefault *impl,
4476 GtkSizeGroup *size_group)
4482 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4483 gtk_widget_show (vbox);
4485 /* Box for lists and preview */
4487 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4488 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4489 gtk_widget_show (hbox);
4493 widget = create_file_list (impl);
4494 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4495 gtk_size_group_add_widget (size_group, widget);
4499 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4500 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4501 /* Don't show preview box initially */
4505 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4507 widget = filter_create (impl);
4509 gtk_widget_show (widget);
4510 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4512 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4517 /* Callback used when the "Browse for more folders" expander is toggled */
4519 expander_changed_cb (GtkExpander *expander,
4521 GtkFileChooserDefault *impl)
4523 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4524 update_appearance (impl);
4527 /* Callback used when the selection changes in the save folder combo box */
4529 save_folder_combo_changed_cb (GtkComboBox *combo,
4530 GtkFileChooserDefault *impl)
4534 if (impl->changing_folder)
4537 if (gtk_combo_box_get_active_iter (combo, &iter))
4539 GtkTreeIter child_iter;
4541 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4544 shortcuts_activate_iter (impl, &child_iter);
4549 save_folder_update_tooltip (GtkComboBox *combo,
4550 GtkFileChooserDefault *impl)
4557 if (gtk_combo_box_get_active_iter (combo, &iter))
4559 GtkTreeIter child_iter;
4561 ShortcutType shortcut_type;
4563 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4566 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4567 SHORTCUTS_COL_DATA, &col_data,
4568 SHORTCUTS_COL_TYPE, &shortcut_type,
4571 if (shortcut_type == SHORTCUT_TYPE_FILE)
4572 tooltip = g_file_get_parse_name (G_FILE (col_data));
4575 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4576 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4577 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4581 /* Filter function used to filter out the Search item and its separator.
4582 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4585 shortcuts_combo_filter_func (GtkTreeModel *model,
4589 GtkFileChooserDefault *impl;
4590 GtkTreePath *tree_path;
4595 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4597 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4599 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4600 g_assert (tree_path != NULL);
4602 indices = gtk_tree_path_get_indices (tree_path);
4606 if (impl->has_search)
4608 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4609 if (idx == indices[0])
4613 if (impl->has_recent)
4615 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4616 if (idx == indices[0])
4620 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4621 if (idx == indices[0])
4626 gtk_tree_path_free (tree_path);
4631 /* Creates the combo box with the save folders */
4633 save_folder_combo_create (GtkFileChooserDefault *impl)
4636 GtkCellRenderer *cell;
4638 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4639 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4640 shortcuts_combo_filter_func,
4644 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4645 "model", impl->shortcuts_combo_filter_model,
4646 "focus-on-click", FALSE,
4648 gtk_widget_show (combo);
4650 cell = gtk_cell_renderer_pixbuf_new ();
4651 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4652 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4653 "pixbuf", SHORTCUTS_COL_PIXBUF,
4654 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4655 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4658 cell = gtk_cell_renderer_text_new ();
4659 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4660 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4661 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4662 "text", SHORTCUTS_COL_NAME,
4663 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4666 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4667 shortcuts_row_separator_func,
4670 g_signal_connect (combo, "changed",
4671 G_CALLBACK (save_folder_combo_changed_cb), impl);
4672 g_signal_connect (combo, "changed",
4673 G_CALLBACK (save_folder_update_tooltip), impl);
4678 /* Creates the widgets specific to Save mode */
4680 save_widgets_create (GtkFileChooserDefault *impl)
4685 GtkWidget *alignment;
4687 if (impl->save_widgets != NULL)
4690 location_switch_to_path_bar (impl);
4692 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4694 table = gtk_table_new (2, 2, FALSE);
4695 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4696 gtk_widget_show (table);
4697 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4698 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4702 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4703 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4704 gtk_table_attach (GTK_TABLE (table), widget,
4708 gtk_widget_show (widget);
4710 /* Location entry */
4712 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4713 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4715 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4716 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4717 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4718 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4720 GTK_EXPAND | GTK_FILL, 0,
4722 gtk_widget_show (impl->location_entry);
4723 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4726 impl->save_folder_label = gtk_label_new (NULL);
4727 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4728 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4732 gtk_widget_show (impl->save_folder_label);
4734 impl->save_folder_combo = save_folder_combo_create (impl);
4735 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4737 GTK_EXPAND | GTK_FILL, GTK_FILL,
4739 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4742 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4743 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4745 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4746 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4747 g_signal_connect (impl->save_expander, "notify::expanded",
4748 G_CALLBACK (expander_changed_cb),
4750 gtk_widget_show_all (alignment);
4752 impl->save_widgets = vbox;
4753 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4754 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4755 gtk_widget_show (impl->save_widgets);
4758 /* Destroys the widgets specific to Save mode */
4760 save_widgets_destroy (GtkFileChooserDefault *impl)
4762 if (impl->save_widgets == NULL)
4765 gtk_widget_destroy (impl->save_widgets);
4766 impl->save_widgets = NULL;
4767 impl->location_entry = NULL;
4768 impl->save_folder_label = NULL;
4769 impl->save_folder_combo = NULL;
4770 impl->save_expander = NULL;
4773 /* Turns on the path bar widget. Can be called even if we are already in that
4777 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4779 if (impl->location_entry)
4781 gtk_widget_destroy (impl->location_entry);
4782 impl->location_entry = NULL;
4785 gtk_widget_hide (impl->location_entry_box);
4788 /* Sets the full path of the current folder as the text in the location entry. */
4790 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4792 gchar *text, *filename;
4794 if (!impl->current_folder)
4797 filename = g_file_get_path (impl->current_folder);
4801 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4805 text = g_file_get_uri (impl->current_folder);
4809 gboolean need_slash;
4812 len = strlen (text);
4813 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4819 slash_text = g_new (char, len + 2);
4820 strcpy (slash_text, text);
4821 slash_text[len] = G_DIR_SEPARATOR;
4822 slash_text[len + 1] = 0;
4828 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4835 /* Turns on the location entry. Can be called even if we are already in that
4839 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4841 /* when in search or recent files mode, we are not showing the
4842 * location_entry_box container, so there's no point in switching
4845 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4846 impl->operation_mode == OPERATION_MODE_RECENT)
4849 if (impl->location_entry)
4850 gtk_widget_destroy (impl->location_entry);
4854 gtk_widget_show (impl->location_entry_box);
4858 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4859 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4861 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4862 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4864 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4865 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4867 /* Configure the entry */
4869 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4873 gtk_widget_show (impl->location_entry);
4874 gtk_widget_grab_focus (impl->location_entry);
4877 /* Sets a new location mode. set_buttons determines whether the toggle button
4878 * for the mode will also be changed.
4881 location_mode_set (GtkFileChooserDefault *impl,
4882 LocationMode new_mode,
4883 gboolean set_button)
4885 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4886 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4888 GtkWindow *toplevel;
4889 GtkWidget *current_focus;
4890 gboolean button_active;
4891 gboolean switch_to_file_list;
4895 case LOCATION_MODE_PATH_BAR:
4896 button_active = FALSE;
4898 /* The location_entry will disappear when we switch to path bar mode. So,
4899 * we'll focus the file list in that case, to avoid having a window with
4900 * no focused widget.
4902 toplevel = get_toplevel (GTK_WIDGET (impl));
4903 switch_to_file_list = FALSE;
4906 current_focus = gtk_window_get_focus (toplevel);
4907 if (!current_focus || current_focus == impl->location_entry)
4908 switch_to_file_list = TRUE;
4911 location_switch_to_path_bar (impl);
4913 if (switch_to_file_list)
4914 gtk_widget_grab_focus (impl->browse_files_tree_view);
4918 case LOCATION_MODE_FILENAME_ENTRY:
4919 button_active = TRUE;
4920 location_switch_to_filename_entry (impl);
4924 g_assert_not_reached ();
4930 g_signal_handlers_block_by_func (impl->location_button,
4931 G_CALLBACK (location_button_toggled_cb), impl);
4933 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4935 g_signal_handlers_unblock_by_func (impl->location_button,
4936 G_CALLBACK (location_button_toggled_cb), impl);
4940 impl->location_mode = new_mode;
4944 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4946 /* when in search or recent files mode, we are not showing the
4947 * location_entry_box container, so there's no point in switching
4950 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4951 impl->operation_mode == OPERATION_MODE_RECENT)
4954 /* If the file entry is not visible, show it.
4955 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4957 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4959 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4961 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4963 if (gtk_widget_has_focus (impl->location_entry))
4965 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4969 gtk_widget_grab_focus (impl->location_entry);
4974 /* Callback used when one of the location mode buttons is toggled */
4976 location_button_toggled_cb (GtkToggleButton *toggle,
4977 GtkFileChooserDefault *impl)
4980 LocationMode new_mode;
4982 is_active = gtk_toggle_button_get_active (toggle);
4986 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4987 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4991 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4992 new_mode = LOCATION_MODE_PATH_BAR;
4995 location_mode_set (impl, new_mode, FALSE);
4998 /* Creates a toggle button for the location entry. */
5000 location_button_create (GtkFileChooserDefault *impl)
5005 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5006 gtk_widget_show (image);
5008 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5012 g_signal_connect (impl->location_button, "toggled",
5013 G_CALLBACK (location_button_toggled_cb), impl);
5015 str = _("Type a file name");
5017 gtk_widget_set_tooltip_text (impl->location_button, str);
5018 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5021 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5023 browse_widgets_create (GtkFileChooserDefault *impl)
5028 GtkSizeGroup *size_group;
5030 /* size group is used by the scrolled windows of the panes */
5031 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5032 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5034 /* Location widgets */
5035 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5036 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5037 gtk_widget_show (impl->browse_path_bar_hbox);
5039 /* Size group that allows the path bar to be the same size between modes */
5040 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5041 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5043 /* Location button */
5045 location_button_create (impl);
5046 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5047 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5051 impl->browse_path_bar = create_path_bar (impl);
5052 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5053 gtk_widget_show_all (impl->browse_path_bar);
5054 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5055 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
5058 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5059 g_signal_connect (impl->browse_new_folder_button, "clicked",
5060 G_CALLBACK (new_folder_button_clicked), impl);
5061 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5063 /* Box for the location label and entry */
5065 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5066 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5068 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5069 gtk_widget_show (impl->location_label);
5070 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5074 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5075 gtk_widget_show (hpaned);
5076 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5078 widget = shortcuts_pane_create (impl, size_group);
5079 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5080 widget = file_pane_create (impl, size_group);
5081 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5082 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5083 g_object_unref (size_group);
5089 gtk_file_chooser_default_constructor (GType type,
5090 guint n_construct_properties,
5091 GObjectConstructParam *construct_params)
5093 GtkFileChooserDefault *impl;
5096 profile_start ("start", NULL);
5098 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5099 n_construct_properties,
5101 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5103 g_assert (impl->file_system);
5105 gtk_widget_push_composite_child ();
5107 /* Shortcuts model */
5108 shortcuts_model_create (impl);
5110 /* The browse widgets */
5111 impl->browse_widgets = browse_widgets_create (impl);
5112 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5114 /* Alignment to hold extra widget */
5115 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5116 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5118 gtk_widget_pop_composite_child ();
5119 update_appearance (impl);
5121 profile_end ("end", NULL);
5126 /* Sets the extra_widget by packing it in the appropriate place */
5128 set_extra_widget (GtkFileChooserDefault *impl,
5129 GtkWidget *extra_widget)
5133 g_object_ref (extra_widget);
5134 /* FIXME: is this right ? */
5135 gtk_widget_show (extra_widget);
5138 if (impl->extra_widget)
5140 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5141 g_object_unref (impl->extra_widget);
5144 impl->extra_widget = extra_widget;
5145 if (impl->extra_widget)
5147 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5148 gtk_widget_show (impl->extra_align);
5151 gtk_widget_hide (impl->extra_align);
5155 set_local_only (GtkFileChooserDefault *impl,
5156 gboolean local_only)
5158 if (local_only != impl->local_only)
5160 impl->local_only = local_only;
5162 if (impl->location_entry)
5163 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5165 if (impl->shortcuts_model && impl->file_system)
5167 shortcuts_add_volumes (impl);
5168 shortcuts_add_bookmarks (impl);
5171 if (local_only && impl->current_folder &&
5172 !g_file_is_native (impl->current_folder))
5174 /* If we are pointing to a non-local folder, make an effort to change
5175 * back to a local folder, but it's really up to the app to not cause
5176 * such a situation, so we ignore errors.
5178 const gchar *home = g_get_home_dir ();
5184 home_file = g_file_new_for_path (home);
5186 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5188 g_object_unref (home_file);
5194 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5195 GtkFileChooserDefault *impl)
5197 shortcuts_add_volumes (impl);
5198 shortcuts_add_bookmarks (impl);
5200 bookmarks_check_add_sensitivity (impl);
5201 bookmarks_check_remove_sensitivity (impl);
5202 shortcuts_check_popup_sensitivity (impl);
5205 /* Sets the file chooser to multiple selection mode */
5207 set_select_multiple (GtkFileChooserDefault *impl,
5208 gboolean select_multiple,
5209 gboolean property_notify)
5211 GtkTreeSelection *selection;
5212 GtkSelectionMode mode;
5214 if (select_multiple == impl->select_multiple)
5217 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5219 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5220 gtk_tree_selection_set_mode (selection, mode);
5222 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5224 impl->select_multiple = select_multiple;
5225 g_object_notify (G_OBJECT (impl), "select-multiple");
5227 check_preview_change (impl);
5231 set_file_system_backend (GtkFileChooserDefault *impl)
5233 profile_start ("start for backend", "default");
5235 impl->file_system = _gtk_file_system_new ();
5237 g_signal_connect (impl->file_system, "volumes-changed",
5238 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5239 g_signal_connect (impl->file_system, "bookmarks-changed",
5240 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5242 profile_end ("end", NULL);
5246 unset_file_system_backend (GtkFileChooserDefault *impl)
5248 g_signal_handlers_disconnect_by_func (impl->file_system,
5249 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5251 g_object_unref (impl->file_system);
5253 impl->file_system = NULL;
5256 /* This function is basically a do_all function.
5258 * It sets the visibility on all the widgets based on the current state, and
5259 * moves the custom_widget if needed.
5262 update_appearance (GtkFileChooserDefault *impl)
5264 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5265 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5269 gtk_widget_hide (impl->location_button);
5270 save_widgets_create (impl);
5272 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5273 text = _("Save in _folder:");
5275 text = _("Create in _folder:");
5277 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5279 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5281 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5282 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5283 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5284 gtk_widget_show (impl->browse_widgets);
5288 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5289 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5290 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5291 gtk_widget_hide (impl->browse_widgets);
5294 if (impl->select_multiple)
5296 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5297 "Re-setting to single selection mode.");
5298 set_select_multiple (impl, FALSE, TRUE);
5301 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5302 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5304 gtk_widget_show (impl->location_button);
5305 save_widgets_destroy (impl);
5306 gtk_widget_show (impl->browse_widgets);
5307 location_mode_set (impl, impl->location_mode, TRUE);
5310 if (impl->location_entry)
5311 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5313 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5314 gtk_widget_hide (impl->browse_new_folder_button);
5316 gtk_widget_show (impl->browse_new_folder_button);
5318 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5319 * of files may change depending whether we are in a file or folder-only mode.
5321 gtk_widget_queue_draw (impl->browse_files_tree_view);
5323 emit_default_size_changed (impl);
5327 gtk_file_chooser_default_set_property (GObject *object,
5329 const GValue *value,
5333 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5337 case GTK_FILE_CHOOSER_PROP_ACTION:
5339 GtkFileChooserAction action = g_value_get_enum (value);
5341 if (action != impl->action)
5343 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5345 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5346 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5347 && impl->select_multiple)
5349 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5350 "this is not allowed in multiple selection mode. Resetting the file chooser "
5351 "to single selection mode.");
5352 set_select_multiple (impl, FALSE, TRUE);
5354 impl->action = action;
5355 update_cell_renderer_attributes (impl);
5356 update_appearance (impl);
5357 settings_load (impl);
5362 case GTK_FILE_CHOOSER_PROP_FILTER:
5363 set_current_filter (impl, g_value_get_object (value));
5366 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5367 set_local_only (impl, g_value_get_boolean (value));
5370 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5371 set_preview_widget (impl, g_value_get_object (value));
5374 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5375 impl->preview_widget_active = g_value_get_boolean (value);
5376 update_preview_widget_visibility (impl);
5379 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5380 impl->use_preview_label = g_value_get_boolean (value);
5381 update_preview_widget_visibility (impl);
5384 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5385 set_extra_widget (impl, g_value_get_object (value));
5388 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5390 gboolean select_multiple = g_value_get_boolean (value);
5391 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5392 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5395 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5396 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5397 "leaving the file chooser in single selection mode.");
5401 set_select_multiple (impl, select_multiple, FALSE);
5405 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5407 gboolean show_hidden = g_value_get_boolean (value);
5408 if (show_hidden != impl->show_hidden)
5410 impl->show_hidden = show_hidden;
5412 if (impl->browse_files_model)
5413 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5418 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5420 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5421 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5425 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5427 gboolean create_folders = g_value_get_boolean (value);
5428 impl->create_folders = create_folders;
5429 update_appearance (impl);
5434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5440 gtk_file_chooser_default_get_property (GObject *object,
5445 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5449 case GTK_FILE_CHOOSER_PROP_ACTION:
5450 g_value_set_enum (value, impl->action);
5453 case GTK_FILE_CHOOSER_PROP_FILTER:
5454 g_value_set_object (value, impl->current_filter);
5457 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5458 g_value_set_boolean (value, impl->local_only);
5461 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5462 g_value_set_object (value, impl->preview_widget);
5465 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5466 g_value_set_boolean (value, impl->preview_widget_active);
5469 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5470 g_value_set_boolean (value, impl->use_preview_label);
5473 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5474 g_value_set_object (value, impl->extra_widget);
5477 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5478 g_value_set_boolean (value, impl->select_multiple);
5481 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5482 g_value_set_boolean (value, impl->show_hidden);
5485 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5486 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5489 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5490 g_value_set_boolean (value, impl->create_folders);
5494 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5499 /* Removes the settings signal handler. It's safe to call multiple times */
5501 remove_settings_signal (GtkFileChooserDefault *impl,
5504 if (impl->settings_signal_id)
5506 GtkSettings *settings;
5508 settings = gtk_settings_get_for_screen (screen);
5509 g_signal_handler_disconnect (settings,
5510 impl->settings_signal_id);
5511 impl->settings_signal_id = 0;
5516 gtk_file_chooser_default_dispose (GObject *object)
5519 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5521 if (impl->extra_widget)
5523 g_object_unref (impl->extra_widget);
5524 impl->extra_widget = NULL;
5527 pending_select_files_free (impl);
5529 /* cancel all pending operations */
5530 if (impl->pending_cancellables)
5532 for (l = impl->pending_cancellables; l; l = l->next)
5534 GCancellable *cancellable = G_CANCELLABLE (l->data);
5535 g_cancellable_cancel (cancellable);
5537 g_slist_free (impl->pending_cancellables);
5538 impl->pending_cancellables = NULL;
5541 if (impl->reload_icon_cancellables)
5543 for (l = impl->reload_icon_cancellables; l; l = l->next)
5545 GCancellable *cancellable = G_CANCELLABLE (l->data);
5546 g_cancellable_cancel (cancellable);
5548 g_slist_free (impl->reload_icon_cancellables);
5549 impl->reload_icon_cancellables = NULL;
5552 if (impl->loading_shortcuts)
5554 for (l = impl->loading_shortcuts; l; l = l->next)
5556 GCancellable *cancellable = G_CANCELLABLE (l->data);
5557 g_cancellable_cancel (cancellable);
5559 g_slist_free (impl->loading_shortcuts);
5560 impl->loading_shortcuts = NULL;
5563 if (impl->file_list_drag_data_received_cancellable)
5565 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5566 impl->file_list_drag_data_received_cancellable = NULL;
5569 if (impl->update_current_folder_cancellable)
5571 g_cancellable_cancel (impl->update_current_folder_cancellable);
5572 impl->update_current_folder_cancellable = NULL;
5575 if (impl->should_respond_get_info_cancellable)
5577 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5578 impl->should_respond_get_info_cancellable = NULL;
5581 if (impl->update_from_entry_cancellable)
5583 g_cancellable_cancel (impl->update_from_entry_cancellable);
5584 impl->update_from_entry_cancellable = NULL;
5587 if (impl->shortcuts_activate_iter_cancellable)
5589 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5590 impl->shortcuts_activate_iter_cancellable = NULL;
5593 search_stop_searching (impl, TRUE);
5594 recent_stop_loading (impl);
5596 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5598 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5601 /* We override show-all since we have internal widgets that
5602 * shouldn't be shown when you call show_all(), like the filter
5606 gtk_file_chooser_default_show_all (GtkWidget *widget)
5608 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5610 gtk_widget_show (widget);
5612 if (impl->extra_widget)
5613 gtk_widget_show_all (impl->extra_widget);
5616 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5617 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5620 toplevel_set_focus_cb (GtkWindow *window,
5622 GtkFileChooserDefault *impl)
5624 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5627 /* We monitor the focus widget on our toplevel to be able to know which widget
5628 * was last focused at the time our "should_respond" method gets called.
5631 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5632 GtkWidget *previous_toplevel)
5634 GtkFileChooserDefault *impl;
5635 GtkWidget *toplevel;
5637 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5638 toplevel = gtk_widget_get_toplevel (widget);
5640 if (previous_toplevel &&
5641 impl->toplevel_set_focus_id != 0)
5643 g_signal_handler_disconnect (previous_toplevel,
5644 impl->toplevel_set_focus_id);
5645 impl->toplevel_set_focus_id = 0;
5646 impl->toplevel_last_focus_widget = NULL;
5649 if (gtk_widget_is_toplevel (toplevel))
5651 g_assert (impl->toplevel_set_focus_id == 0);
5652 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5653 G_CALLBACK (toplevel_set_focus_cb), impl);
5654 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5658 /* Changes the icons wherever it is needed */
5660 change_icon_theme (GtkFileChooserDefault *impl)
5662 GtkSettings *settings;
5664 GtkCellRenderer *renderer;
5667 profile_start ("start", NULL);
5669 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5671 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5672 impl->icon_size = MAX (width, height);
5674 impl->icon_size = FALLBACK_ICON_SIZE;
5676 shortcuts_reload_icons (impl);
5677 /* the first cell in the first column is the icon column, and we have a fixed size there */
5678 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5679 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5680 renderer = GTK_CELL_RENDERER (cells->data);
5681 set_icon_cell_renderer_fixed_size (impl, renderer);
5682 g_list_free (cells);
5683 if (impl->browse_files_model)
5684 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5685 gtk_widget_queue_resize (impl->browse_files_tree_view);
5687 profile_end ("end", NULL);
5690 /* Callback used when a GtkSettings value changes */
5692 settings_notify_cb (GObject *object,
5694 GtkFileChooserDefault *impl)
5698 profile_start ("start", NULL);
5700 name = g_param_spec_get_name (pspec);
5702 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5703 strcmp (name, "gtk-icon-sizes") == 0)
5704 change_icon_theme (impl);
5706 profile_end ("end", NULL);
5709 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5713 check_icon_theme (GtkFileChooserDefault *impl)
5715 GtkSettings *settings;
5717 profile_start ("start", NULL);
5719 if (impl->settings_signal_id)
5721 profile_end ("end", NULL);
5725 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5727 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5728 impl->settings_signal_id = g_signal_connect (settings, "notify",
5729 G_CALLBACK (settings_notify_cb), impl);
5731 change_icon_theme (impl);
5734 profile_end ("end", NULL);
5738 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5740 GtkFileChooserDefault *impl;
5742 profile_start ("start", NULL);
5744 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5746 profile_msg (" parent class style_udpated start", NULL);
5747 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5748 profile_msg (" parent class style_updated end", NULL);
5750 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5751 change_icon_theme (impl);
5753 emit_default_size_changed (impl);
5755 profile_end ("end", NULL);
5759 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5760 GdkScreen *previous_screen)
5762 GtkFileChooserDefault *impl;
5764 profile_start ("start", NULL);
5766 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5768 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5769 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5771 remove_settings_signal (impl, previous_screen);
5772 check_icon_theme (impl);
5774 emit_default_size_changed (impl);
5776 profile_end ("end", NULL);
5780 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5781 GtkAllocation *allocation)
5783 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5787 set_sort_column (GtkFileChooserDefault *impl)
5789 GtkTreeSortable *sortable;
5791 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5793 /* can happen when we're still populating the model */
5794 if (sortable == NULL)
5797 gtk_tree_sortable_set_sort_column_id (sortable,
5803 settings_ensure (GtkFileChooserDefault *impl)
5805 if (impl->settings != NULL)
5808 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5809 "/org/gtk/settings/file-chooser/");
5810 g_settings_delay (impl->settings);
5814 settings_load (GtkFileChooserDefault *impl)
5816 LocationMode location_mode;
5817 gboolean show_hidden;
5818 gboolean expand_folders;
5819 gboolean show_size_column;
5821 GtkSortType sort_order;
5823 settings_ensure (impl);
5825 expand_folders = g_settings_get_boolean (impl->settings, SETTINGS_KEY_EXPAND_FOLDERS);
5826 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5827 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5828 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5829 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5830 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5832 location_mode_set (impl, location_mode, TRUE);
5834 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5836 impl->expand_folders = expand_folders;
5837 if (impl->save_expander)
5838 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5840 impl->show_size_column = show_size_column;
5841 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5843 impl->sort_column = sort_column;
5844 impl->sort_order = sort_order;
5845 /* We don't call set_sort_column() here as the models may not have been
5846 * created yet. The individual functions that create and set the models will
5847 * call set_sort_column() themselves.
5852 save_dialog_geometry (GtkFileChooserDefault *impl)
5854 GtkWindow *toplevel;
5855 int x, y, width, height;
5857 /* We don't save the geometry in non-expanded "save" mode, so that the "little
5858 * dialog" won't make future Open dialogs too small.
5860 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5861 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5862 || impl->expand_folders))
5865 toplevel = get_toplevel (GTK_WIDGET (impl));
5867 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5870 gtk_window_get_position (toplevel, &x, &y);
5871 gtk_window_get_size (toplevel, &width, &height);
5873 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5874 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5878 settings_save (GtkFileChooserDefault *impl)
5880 char *current_folder_uri;
5882 settings_ensure (impl);
5884 /* Current folder */
5886 if (impl->current_folder)
5887 current_folder_uri = g_file_get_uri (impl->current_folder);
5889 current_folder_uri = "";
5891 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5893 if (impl->current_folder)
5894 g_free (current_folder_uri);
5896 /* All the other state */
5898 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5899 g_settings_set_boolean (impl->settings, SETTINGS_KEY_EXPAND_FOLDERS, impl->expand_folders);
5900 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5901 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5902 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5903 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5904 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5906 save_dialog_geometry (impl);
5908 /* Now apply the settings */
5909 g_settings_apply (impl->settings);
5911 g_object_unref (impl->settings);
5912 impl->settings = NULL;
5915 /* GtkWidget::realize method */
5917 gtk_file_chooser_default_realize (GtkWidget *widget)
5919 GtkFileChooserDefault *impl;
5921 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5923 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5925 emit_default_size_changed (impl);
5929 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5931 char *last_folder_uri;
5934 settings_ensure (impl);
5936 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5938 /* If no last folder is set, we use the user's home directory, since
5939 * this is the starting point for most documents.
5941 if (last_folder_uri[0] == '\0')
5942 file = g_file_new_for_path (g_get_home_dir ());
5944 file = g_file_new_for_uri (last_folder_uri);
5946 g_free (last_folder_uri);
5951 /* GtkWidget::map method */
5953 gtk_file_chooser_default_map (GtkWidget *widget)
5955 GtkFileChooserDefault *impl;
5957 profile_start ("start", NULL);
5959 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5961 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5963 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5967 switch (impl->reload_state)
5970 /* The user didn't explicitly give us a folder to display, so we'll
5971 * use the saved one from the last invocation of the file chooser
5973 folder = get_file_for_last_folder_opened (impl);
5974 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), folder, NULL);
5975 g_object_unref (folder);
5978 case RELOAD_HAS_FOLDER:
5979 /* Nothing; we are already loading or loaded, so we
5980 * don't need to reload
5985 g_assert_not_reached ();
5989 volumes_bookmarks_changed_cb (impl->file_system, impl);
5991 settings_load (impl);
5993 profile_end ("end", NULL);
5996 /* GtkWidget::unmap method */
5998 gtk_file_chooser_default_unmap (GtkWidget *widget)
6000 GtkFileChooserDefault *impl;
6002 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6004 settings_save (impl);
6006 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6010 install_list_model_filter (GtkFileChooserDefault *impl)
6012 _gtk_file_system_model_set_filter (impl->browse_files_model,
6013 impl->current_filter);
6016 #define COMPARE_DIRECTORIES \
6017 GtkFileChooserDefault *impl = user_data; \
6018 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6019 gboolean dir_a, dir_b; \
6021 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6022 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6024 if (dir_a != dir_b) \
6025 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6027 /* Sort callback for the filename column */
6029 name_sort_func (GtkTreeModel *model,
6034 COMPARE_DIRECTORIES;
6037 const char *key_a, *key_b;
6040 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6041 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6044 result = strcmp (key_a, key_b);
6056 /* Sort callback for the size column */
6058 size_sort_func (GtkTreeModel *model,
6063 COMPARE_DIRECTORIES;
6066 gint64 size_a, size_b;
6068 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6069 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6071 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6075 /* Sort callback for the mtime column */
6077 mtime_sort_func (GtkTreeModel *model,
6082 COMPARE_DIRECTORIES;
6087 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6088 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6090 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6094 /* Callback used when the sort column changes. We cache the sort order for use
6095 * in name_sort_func().
6098 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6099 GtkFileChooserDefault *impl)
6101 gint sort_column_id;
6102 GtkSortType sort_type;
6104 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6106 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6107 impl->sort_column = sort_column_id;
6108 impl->sort_order = sort_type;
6113 set_busy_cursor (GtkFileChooserDefault *impl,
6117 GtkWindow *toplevel;
6118 GdkDisplay *display;
6121 toplevel = get_toplevel (GTK_WIDGET (impl));
6122 widget = GTK_WIDGET (toplevel);
6123 if (!toplevel || !gtk_widget_get_realized (widget))
6126 display = gtk_widget_get_display (widget);
6129 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6133 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6134 gdk_display_flush (display);
6137 g_object_unref (cursor);
6140 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6142 load_set_model (GtkFileChooserDefault *impl)
6144 profile_start ("start", NULL);
6146 g_assert (impl->browse_files_model != NULL);
6148 profile_msg (" gtk_tree_view_set_model start", NULL);
6149 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6150 GTK_TREE_MODEL (impl->browse_files_model));
6151 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6152 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6154 set_sort_column (impl);
6155 profile_msg (" gtk_tree_view_set_model end", NULL);
6156 impl->list_sort_ascending = TRUE;
6158 profile_end ("end", NULL);
6161 /* Timeout callback used when the loading timer expires */
6163 load_timeout_cb (gpointer data)
6165 GtkFileChooserDefault *impl;
6167 profile_start ("start", NULL);
6169 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6170 g_assert (impl->load_state == LOAD_PRELOAD);
6171 g_assert (impl->load_timeout_id != 0);
6172 g_assert (impl->browse_files_model != NULL);
6174 impl->load_timeout_id = 0;
6175 impl->load_state = LOAD_LOADING;
6177 load_set_model (impl);
6179 profile_end ("end", NULL);
6184 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6186 load_setup_timer (GtkFileChooserDefault *impl)
6188 g_assert (impl->load_timeout_id == 0);
6189 g_assert (impl->load_state != LOAD_PRELOAD);
6191 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6192 impl->load_state = LOAD_PRELOAD;
6195 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6197 load_remove_timer (GtkFileChooserDefault *impl)
6199 if (impl->load_timeout_id != 0)
6201 g_assert (impl->load_state == LOAD_PRELOAD);
6203 g_source_remove (impl->load_timeout_id);
6204 impl->load_timeout_id = 0;
6205 impl->load_state = LOAD_EMPTY;
6208 g_assert (impl->load_state == LOAD_EMPTY ||
6209 impl->load_state == LOAD_LOADING ||
6210 impl->load_state == LOAD_FINISHED);
6213 /* Selects the first row in the file list */
6215 browse_files_select_first_row (GtkFileChooserDefault *impl)
6218 GtkTreeIter dummy_iter;
6219 GtkTreeModel *tree_model;
6221 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6226 path = gtk_tree_path_new_from_indices (0, -1);
6228 /* If the list is empty, do nothing. */
6229 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6230 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6232 gtk_tree_path_free (path);
6235 struct center_selected_row_closure {
6236 GtkFileChooserDefault *impl;
6237 gboolean already_centered;
6240 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6241 * selected row in the tree view.
6244 center_selected_row_foreach_cb (GtkTreeModel *model,
6249 struct center_selected_row_closure *closure;
6252 if (closure->already_centered)
6255 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6256 closure->already_centered = TRUE;
6259 /* Centers the selected row in the tree view */
6261 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6263 struct center_selected_row_closure closure;
6264 GtkTreeSelection *selection;
6266 closure.impl = impl;
6267 closure.already_centered = FALSE;
6269 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6270 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6274 show_and_select_files (GtkFileChooserDefault *impl,
6277 GtkTreeSelection *selection;
6278 GtkFileSystemModel *fsmodel;
6279 gboolean enabled_hidden, removed_filters;
6280 gboolean selected_a_file;
6283 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6284 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6286 enabled_hidden = impl->show_hidden;
6287 removed_filters = (impl->current_filter == NULL);
6289 selected_a_file = FALSE;
6291 for (walk = files; walk; walk = walk->next)
6293 GFile *file = walk->data;
6296 /* Is it a hidden file? */
6298 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6301 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6303 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6305 if (!enabled_hidden &&
6306 (g_file_info_get_is_hidden (info) ||
6307 g_file_info_get_is_backup (info)))
6309 g_object_set (impl, "show-hidden", TRUE, NULL);
6310 enabled_hidden = TRUE;
6314 /* Is it a filtered file? */
6316 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6317 continue; /* re-get the iter as it may change when the model refilters */
6319 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6321 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6322 if (!removed_filters)
6324 set_current_filter (impl, NULL);
6325 removed_filters = TRUE;
6329 /* Okay, can we select the file now? */
6331 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6334 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6338 gtk_tree_selection_select_iter (selection, &iter);
6340 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6341 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6343 gtk_tree_path_free (path);
6345 selected_a_file = TRUE;
6349 browse_files_center_selected_row (impl);
6351 return selected_a_file;
6354 /* Processes the pending operation when a folder is finished loading */
6356 pending_select_files_process (GtkFileChooserDefault *impl)
6358 g_assert (impl->load_state == LOAD_FINISHED);
6359 g_assert (impl->browse_files_model != NULL);
6361 if (impl->pending_select_files)
6363 show_and_select_files (impl, impl->pending_select_files);
6364 pending_select_files_free (impl);
6365 browse_files_center_selected_row (impl);
6369 /* We only select the first row if the chooser is actually mapped ---
6370 * selecting the first row is to help the user when he is interacting with
6371 * the chooser, but sometimes a chooser works not on behalf of the user,
6372 * but rather on behalf of something else like GtkFileChooserButton. In
6373 * that case, the chooser's selection should be what the caller expects,
6374 * as the user can't see that something else got selected. See bug #165264.
6376 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6377 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6378 browse_files_select_first_row (impl);
6381 g_assert (impl->pending_select_files == NULL);
6385 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6390 info = g_file_query_info (impl->current_folder,
6391 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6392 G_FILE_QUERY_INFO_NONE,
6397 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6398 g_object_unref (info);
6401 msg = g_strdup (_("Could not read the contents of the folder"));
6403 error_message (impl, msg, error->message);
6407 /* Callback used when the file system model finishes loading */
6409 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6411 GtkFileChooserDefault *impl)
6413 profile_start ("start", NULL);
6416 show_error_on_reading_current_folder (impl, error);
6418 if (impl->load_state == LOAD_PRELOAD)
6420 load_remove_timer (impl);
6421 load_set_model (impl);
6423 else if (impl->load_state == LOAD_LOADING)
6429 /* We can't g_assert_not_reached(), as something other than us may have
6430 * initiated a folder reload. See #165556.
6432 profile_end ("end", NULL);
6436 g_assert (impl->load_timeout_id == 0);
6438 impl->load_state = LOAD_FINISHED;
6440 pending_select_files_process (impl);
6441 set_busy_cursor (impl, FALSE);
6442 #ifdef PROFILE_FILE_CHOOSER
6443 access ("MARK: *** FINISHED LOADING", F_OK);
6446 profile_end ("end", NULL);
6450 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6451 gboolean remove_from_treeview)
6453 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6455 if (impl->browse_files_model)
6457 g_object_unref (impl->browse_files_model);
6458 impl->browse_files_model = NULL;
6461 if (remove_from_treeview)
6462 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6466 my_g_format_time_for_display (glong secs)
6471 time_t time_mtime, time_now;
6472 const gchar *format;
6473 gchar *locale_format = NULL;
6475 char *date_str = NULL;
6477 const char *locale, *dot = NULL;
6478 gint64 codepage = -1;
6484 #ifdef HAVE_LOCALTIME_R
6485 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6488 struct tm *ptm = localtime ((time_t *) &time_mtime);
6492 g_warning ("ptm != NULL failed");
6494 return g_strdup (_("Unknown"));
6497 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6499 #endif /* HAVE_LOCALTIME_R */
6501 g_date_set_time_t (&mtime, time_mtime);
6502 time_now = time (NULL);
6503 g_date_set_time_t (&now, time_now);
6505 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6507 /* Translators: %H means "hours" and %M means "minutes" */
6509 format = _("%H:%M");
6510 else if (days_diff == 1)
6511 format = _("Yesterday at %H:%M");
6514 if (days_diff > 1 && days_diff < 7)
6515 format = "%A"; /* Days from last week */
6517 format = "%x"; /* Any other date */
6521 /* g_locale_from_utf8() returns a string in the system
6522 * code-page, which is not always the same as that used by the C
6523 * library. For instance when running a GTK+ program with
6524 * LANG=ko on an English version of Windows, the system
6525 * code-page is 1252, but the code-page used by the C library is
6526 * 949. (It's GTK+ itself that sets the C library locale when it
6527 * notices the LANG environment variable. See gtkmain.c The
6528 * Microsoft C library doesn't look at any locale environment
6529 * variables.) We need to pass strftime() a string in the C
6530 * library's code-page. See bug #509885.
6532 locale = setlocale (LC_ALL, NULL);
6534 dot = strchr (locale, '.');
6537 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6539 /* All codepages should fit in 16 bits AFAIK */
6540 if (codepage > 0 && codepage < 65536)
6542 sprintf (charset, "CP%u", (guint) codepage);
6543 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6547 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6549 if (locale_format != NULL &&
6550 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6553 /* As above but in opposite direction... */
6554 if (codepage > 0 && codepage < 65536)
6555 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6557 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6561 if (date_str == NULL)
6562 date_str = g_strdup (_("Unknown"));
6564 g_free (locale_format);
6569 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6571 GFileAttributeType type;
6574 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6575 g_file_info_set_attribute (to, attribute, type, value);
6579 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6581 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6582 GFile *file = G_FILE (object);
6583 GFileInfo *queried, *info;
6586 queried = g_file_query_info_finish (file, res, NULL);
6587 if (queried == NULL)
6590 GDK_THREADS_ENTER ();
6592 /* now we know model is valid */
6594 /* file was deleted */
6595 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6597 GDK_THREADS_LEAVE ();
6601 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6603 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6604 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6605 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6607 _gtk_file_system_model_update_file (model, file, info, FALSE);
6609 g_object_unref (info);
6611 GDK_THREADS_LEAVE ();
6615 file_system_model_set (GtkFileSystemModel *model,
6622 GtkFileChooserDefault *impl = data;
6626 case MODEL_COL_FILE:
6627 g_value_set_object (value, file);
6629 case MODEL_COL_NAME:
6631 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6633 g_value_set_string (value, g_file_info_get_display_name (info));
6635 case MODEL_COL_NAME_COLLATED:
6637 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6639 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6641 case MODEL_COL_IS_FOLDER:
6642 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6644 case MODEL_COL_PIXBUF:
6647 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6649 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6653 GtkTreeModel *tree_model;
6654 GtkTreePath *path, *start, *end;
6657 if (impl->browse_files_tree_view == NULL ||
6658 g_file_info_has_attribute (info, "filechooser::queried"))
6661 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6662 if (tree_model != GTK_TREE_MODEL (model))
6665 if (!_gtk_file_system_model_get_iter_for_file (model,
6668 g_assert_not_reached ();
6669 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6671 path = gtk_tree_model_get_path (tree_model, &iter);
6672 if (gtk_tree_path_compare (start, path) != 1 &&
6673 gtk_tree_path_compare (path, end) != 1)
6675 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6676 g_file_query_info_async (file,
6677 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6678 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6679 G_FILE_ATTRIBUTE_STANDARD_ICON,
6680 G_FILE_QUERY_INFO_NONE,
6682 _gtk_file_system_model_get_cancellable (model),
6683 file_system_model_got_thumbnail,
6686 gtk_tree_path_free (path);
6687 gtk_tree_path_free (start);
6688 gtk_tree_path_free (end);
6693 g_value_set_object (value, NULL);
6695 case MODEL_COL_SIZE:
6696 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6698 case MODEL_COL_SIZE_TEXT:
6699 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6700 g_value_set_string (value, NULL);
6702 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6704 case MODEL_COL_MTIME:
6705 case MODEL_COL_MTIME_TEXT:
6710 g_file_info_get_modification_time (info, &tv);
6711 if (column == MODEL_COL_MTIME)
6712 g_value_set_long (value, tv.tv_sec);
6713 else if (tv.tv_sec == 0)
6714 g_value_set_static_string (value, _("Unknown"));
6716 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6719 case MODEL_COL_ELLIPSIZE:
6720 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6723 g_assert_not_reached ();
6730 /* Gets rid of the old list model and creates a new one for the current folder */
6732 set_list_model (GtkFileChooserDefault *impl,
6735 g_assert (impl->current_folder != NULL);
6737 profile_start ("start", NULL);
6739 stop_loading_and_clear_list_model (impl, TRUE);
6741 set_busy_cursor (impl, TRUE);
6743 impl->browse_files_model =
6744 _gtk_file_system_model_new_for_directory (impl->current_folder,
6746 file_system_model_set,
6748 MODEL_COLUMN_TYPES);
6750 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6752 profile_msg (" set sort function", NULL);
6753 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6754 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6755 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6756 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6757 set_sort_column (impl);
6758 impl->list_sort_ascending = TRUE;
6759 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6760 G_CALLBACK (list_sort_column_changed_cb), impl);
6762 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6764 g_signal_connect (impl->browse_files_model, "finished-loading",
6765 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6767 install_list_model_filter (impl);
6769 profile_end ("end", NULL);
6774 struct update_chooser_entry_selected_foreach_closure {
6776 GtkTreeIter first_selected_iter;
6780 compare_utf8_filenames (const gchar *a,
6783 gchar *a_folded, *b_folded;
6786 a_folded = g_utf8_strdown (a, -1);
6787 b_folded = g_utf8_strdown (b, -1);
6789 retval = strcmp (a_folded, b_folded);
6798 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6803 struct update_chooser_entry_selected_foreach_closure *closure;
6806 closure->num_selected++;
6808 if (closure->num_selected == 1)
6809 closure->first_selected_iter = *iter;
6813 update_chooser_entry (GtkFileChooserDefault *impl)
6815 GtkTreeSelection *selection;
6816 struct update_chooser_entry_selected_foreach_closure closure;
6818 /* no need to update the file chooser's entry if there's no entry */
6819 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6820 impl->operation_mode == OPERATION_MODE_RECENT ||
6821 !impl->location_entry)
6824 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6825 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6826 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6827 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6828 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6831 g_assert (impl->location_entry != NULL);
6833 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6834 closure.num_selected = 0;
6835 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6837 if (closure.num_selected == 0)
6839 goto maybe_clear_entry;
6841 else if (closure.num_selected == 1)
6843 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6846 gboolean change_entry;
6848 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6850 /* If the cursor moved to the row of the newly created folder,
6851 * retrieving info will return NULL.
6856 g_free (impl->browse_files_last_selected_name);
6857 impl->browse_files_last_selected_name =
6858 g_strdup (g_file_info_get_display_name (info));
6860 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6861 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6862 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6864 /* Don't change the name when clicking on a folder... */
6865 change_entry = (! _gtk_file_info_consider_as_directory (info));
6868 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6872 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6874 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6875 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6883 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6884 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6886 /* Multiple selection, so just clear the entry. */
6887 g_free (impl->browse_files_last_selected_name);
6888 impl->browse_files_last_selected_name = NULL;
6890 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6896 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6897 && impl->browse_files_last_selected_name)
6899 const char *entry_text;
6901 gboolean clear_entry;
6903 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6904 len = strlen (entry_text);
6907 /* The file chooser entry may have appended a "/" to its text.
6908 * So take it out, and compare the result to the old selection.
6910 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6914 tmp = g_strndup (entry_text, len - 1);
6915 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6919 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6922 clear_entry = FALSE;
6925 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6930 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6934 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6938 struct UpdateCurrentFolderData
6940 GtkFileChooserDefault *impl;
6942 gboolean keep_trail;
6943 gboolean clear_entry;
6944 GFile *original_file;
6945 GError *original_error;
6949 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6950 GtkFileSystemVolume *volume,
6951 const GError *error,
6954 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6955 struct UpdateCurrentFolderData *data = user_data;
6956 GtkFileChooserDefault *impl = data->impl;
6958 if (cancellable != impl->update_current_folder_cancellable)
6961 impl->update_current_folder_cancellable = NULL;
6962 set_busy_cursor (impl, FALSE);
6969 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6970 impl->reload_state = RELOAD_EMPTY;
6974 change_folder_and_display_error (impl, data->file, data->clear_entry);
6977 g_object_unref (data->file);
6980 g_object_unref (cancellable);
6984 update_current_folder_get_info_cb (GCancellable *cancellable,
6986 const GError *error,
6989 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6990 struct UpdateCurrentFolderData *data = user_data;
6991 GtkFileChooserDefault *impl = data->impl;
6993 if (cancellable != impl->update_current_folder_cancellable)
6996 impl->update_current_folder_cancellable = NULL;
6997 impl->reload_state = RELOAD_EMPTY;
6999 set_busy_cursor (impl, FALSE);
7008 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7010 GMountOperation *mount_operation;
7011 GtkWidget *toplevel;
7013 g_object_unref (cancellable);
7014 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7016 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7018 set_busy_cursor (impl, TRUE);
7020 impl->update_current_folder_cancellable =
7021 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7023 update_current_folder_mount_enclosing_volume_cb,
7029 if (!data->original_file)
7031 data->original_file = g_object_ref (data->file);
7032 data->original_error = g_error_copy (error);
7035 parent_file = g_file_get_parent (data->file);
7037 /* get parent path and try to change the folder to that */
7040 g_object_unref (data->file);
7041 data->file = parent_file;
7043 g_object_unref (cancellable);
7045 /* restart the update current folder operation */
7046 impl->reload_state = RELOAD_HAS_FOLDER;
7048 impl->update_current_folder_cancellable =
7049 _gtk_file_system_get_info (impl->file_system, data->file,
7051 update_current_folder_get_info_cb,
7054 set_busy_cursor (impl, TRUE);
7060 /* Error and bail out, ignoring "not found" errors since they're useless:
7061 * they only happen when a program defaults to a folder that has been (re)moved.
7063 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7064 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7066 g_error_free (data->original_error);
7068 g_object_unref (data->original_file);
7074 if (data->original_file)
7076 /* Error and bail out, ignoring "not found" errors since they're useless:
7077 * they only happen when a program defaults to a folder that has been (re)moved.
7079 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7080 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7082 g_error_free (data->original_error);
7084 g_object_unref (data->original_file);
7087 if (! _gtk_file_info_consider_as_directory (info))
7090 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7093 if (impl->current_folder != data->file)
7095 if (impl->current_folder)
7096 g_object_unref (impl->current_folder);
7098 impl->current_folder = g_object_ref (data->file);
7101 impl->reload_state = RELOAD_HAS_FOLDER;
7103 /* Update the widgets that may trigger a folder change themselves. */
7105 if (!impl->changing_folder)
7107 impl->changing_folder = TRUE;
7109 shortcuts_update_current_folder (impl);
7111 impl->changing_folder = FALSE;
7114 /* Set the folder on the save entry */
7116 if (impl->location_entry)
7118 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7119 impl->current_folder);
7121 if (data->clear_entry)
7122 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7125 /* Create a new list model. This is slightly evil; we store the result value
7126 * but perform more actions rather than returning immediately even if it
7127 * generates an error.
7129 set_list_model (impl, NULL);
7131 /* Refresh controls */
7133 shortcuts_find_current_folder (impl);
7135 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7137 check_preview_change (impl);
7138 bookmarks_check_add_sensitivity (impl);
7140 g_signal_emit_by_name (impl, "selection-changed", 0);
7143 g_object_unref (data->file);
7146 g_object_unref (cancellable);
7150 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7152 gboolean keep_trail,
7153 gboolean clear_entry,
7156 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7157 struct UpdateCurrentFolderData *data;
7159 profile_start ("start", NULL);
7161 g_object_ref (file);
7163 switch (impl->operation_mode)
7165 case OPERATION_MODE_SEARCH:
7166 search_switch_to_browse_mode (impl);
7168 case OPERATION_MODE_RECENT:
7169 recent_switch_to_browse_mode (impl);
7171 case OPERATION_MODE_BROWSE:
7175 if (impl->local_only && !g_file_is_native (file))
7177 g_set_error_literal (error,
7178 GTK_FILE_CHOOSER_ERROR,
7179 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7180 _("Cannot change to folder because it is not local"));
7182 g_object_unref (file);
7183 profile_end ("end - not local", NULL);
7187 if (impl->update_current_folder_cancellable)
7188 g_cancellable_cancel (impl->update_current_folder_cancellable);
7190 /* Test validity of path here. */
7191 data = g_new0 (struct UpdateCurrentFolderData, 1);
7193 data->file = g_object_ref (file);
7194 data->keep_trail = keep_trail;
7195 data->clear_entry = clear_entry;
7197 impl->reload_state = RELOAD_HAS_FOLDER;
7199 impl->update_current_folder_cancellable =
7200 _gtk_file_system_get_info (impl->file_system, file,
7202 update_current_folder_get_info_cb,
7205 set_busy_cursor (impl, TRUE);
7206 g_object_unref (file);
7208 profile_end ("end", NULL);
7213 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7215 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7217 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7218 impl->operation_mode == OPERATION_MODE_RECENT)
7221 if (impl->reload_state == RELOAD_EMPTY)
7223 /* We are unmapped, or we had an error while loading the last folder.
7224 * We'll return the folder used by the last invocation of the file chooser
7225 * since once we get (re)mapped, we'll load *that* folder anyway unless
7226 * the caller explicitly calls set_current_folder() on us.
7228 return get_file_for_last_folder_opened (impl);
7231 if (impl->current_folder)
7232 return g_object_ref (impl->current_folder);
7238 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7241 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7243 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7244 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7246 pending_select_files_free (impl);
7247 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7251 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7255 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7259 parent_file = g_file_get_parent (file);
7262 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7264 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7265 impl->operation_mode == OPERATION_MODE_RECENT ||
7266 impl->load_state == LOAD_EMPTY)
7272 g_assert (impl->current_folder != NULL);
7274 same_path = g_file_equal (parent_file, impl->current_folder);
7277 if (same_path && impl->load_state == LOAD_FINISHED)
7282 files.data = (gpointer) file;
7285 result = show_and_select_files (impl, &files);
7286 g_object_unref (parent_file);
7290 pending_select_files_add (impl, file);
7296 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7297 g_object_unref (parent_file);
7301 g_object_unref (parent_file);
7306 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7309 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7310 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7313 if (!impl->browse_files_model)
7316 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7321 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7326 maybe_select (GtkTreeModel *model,
7331 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7332 GtkTreeSelection *selection;
7335 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7337 gtk_tree_model_get (model, iter,
7338 MODEL_COL_IS_FOLDER, &is_folder,
7341 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7342 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7343 gtk_tree_selection_select_iter (selection, iter);
7345 gtk_tree_selection_unselect_iter (selection, iter);
7351 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7353 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7355 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7356 impl->operation_mode == OPERATION_MODE_RECENT)
7358 GtkTreeSelection *selection;
7360 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7361 gtk_tree_selection_select_all (selection);
7365 if (impl->select_multiple)
7366 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7367 maybe_select, impl);
7371 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7373 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7374 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7376 gtk_tree_selection_unselect_all (selection);
7377 pending_select_files_free (impl);
7380 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7382 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7384 * is_empty_ret - whether the file entry is totally empty
7386 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7387 * the path will be "$cwd/foobar")
7390 check_save_entry (GtkFileChooserDefault *impl,
7392 gboolean *is_well_formed_ret,
7393 gboolean *is_empty_ret,
7394 gboolean *is_file_part_empty_ret,
7395 gboolean *is_folder)
7397 GtkFileChooserEntry *chooser_entry;
7398 GFile *current_folder;
7399 const char *file_part;
7403 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7404 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7405 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7406 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7407 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7409 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7411 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7414 *is_well_formed_ret = TRUE;
7415 *is_empty_ret = TRUE;
7416 *is_file_part_empty_ret = TRUE;
7422 *is_empty_ret = FALSE;
7424 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7425 if (!current_folder)
7428 *is_well_formed_ret = FALSE;
7429 *is_file_part_empty_ret = FALSE;
7435 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7437 if (!file_part || file_part[0] == '\0')
7439 *file_ret = g_object_ref (current_folder);
7440 *is_well_formed_ret = TRUE;
7441 *is_file_part_empty_ret = TRUE;
7447 *is_file_part_empty_ret = FALSE;
7450 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7454 error_building_filename_dialog (impl, error);
7456 *is_well_formed_ret = FALSE;
7463 *is_well_formed_ret = TRUE;
7464 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7467 struct get_files_closure {
7468 GtkFileChooserDefault *impl;
7470 GFile *file_from_entry;
7474 get_files_foreach (GtkTreeModel *model,
7479 struct get_files_closure *info;
7481 GtkFileSystemModel *fs_model;
7484 fs_model = info->impl->browse_files_model;
7486 file = _gtk_file_system_model_get_file (fs_model, iter);
7488 return; /* We are on the editable row */
7490 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7491 info->result = g_slist_prepend (info->result, g_object_ref (file));
7495 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7497 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7498 struct get_files_closure info;
7499 GtkWindow *toplevel;
7500 GtkWidget *current_focus;
7501 gboolean file_list_seen;
7503 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7504 return search_get_selected_files (impl);
7506 if (impl->operation_mode == OPERATION_MODE_RECENT)
7507 return recent_get_selected_files (impl);
7511 info.file_from_entry = NULL;
7513 toplevel = get_toplevel (GTK_WIDGET (impl));
7515 current_focus = gtk_window_get_focus (toplevel);
7517 current_focus = NULL;
7519 file_list_seen = FALSE;
7520 if (current_focus == impl->browse_files_tree_view)
7522 GtkTreeSelection *selection;
7526 file_list_seen = TRUE;
7527 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7528 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7530 /* If there is no selection in the file list, we probably have this situation:
7532 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7533 * 2. He then double-clicked on a folder ("bar") in the file list
7535 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7536 * filename entry to see if that is the case.
7538 if (info.result == NULL && impl->location_entry)
7541 else if (impl->location_entry && current_focus == impl->location_entry)
7543 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7547 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7552 if (!is_well_formed)
7555 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7557 g_object_unref (info.file_from_entry);
7561 if (info.file_from_entry)
7562 info.result = g_slist_prepend (info.result, info.file_from_entry);
7563 else if (!file_list_seen)
7568 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7570 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7574 /* The focus is on a dialog's action area button or something else */
7575 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7576 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7584 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7585 * fall back to the current directory */
7586 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7587 info.result == NULL)
7589 GFile *current_folder;
7591 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7594 info.result = g_slist_prepend (info.result, current_folder);
7597 return g_slist_reverse (info.result);
7601 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7603 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7605 if (impl->preview_file)
7606 return g_object_ref (impl->preview_file);
7611 static GtkFileSystem *
7612 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7614 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7616 return impl->file_system;
7619 /* Shows or hides the filter widgets */
7621 show_filters (GtkFileChooserDefault *impl,
7625 gtk_widget_show (impl->filter_combo_hbox);
7627 gtk_widget_hide (impl->filter_combo_hbox);
7631 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7632 GtkFileFilter *filter)
7634 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7637 if (g_slist_find (impl->filters, filter))
7639 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7643 g_object_ref_sink (filter);
7644 impl->filters = g_slist_append (impl->filters, filter);
7646 name = gtk_file_filter_get_name (filter);
7648 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7650 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7652 if (!g_slist_find (impl->filters, impl->current_filter))
7653 set_current_filter (impl, filter);
7655 show_filters (impl, TRUE);
7659 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7660 GtkFileFilter *filter)
7662 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7663 GtkTreeModel *model;
7667 filter_index = g_slist_index (impl->filters, filter);
7669 if (filter_index < 0)
7671 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7675 impl->filters = g_slist_remove (impl->filters, filter);
7677 if (filter == impl->current_filter)
7680 set_current_filter (impl, impl->filters->data);
7682 set_current_filter (impl, NULL);
7685 /* Remove row from the combo box */
7686 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7687 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7688 g_assert_not_reached ();
7690 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7692 g_object_unref (filter);
7695 show_filters (impl, FALSE);
7699 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7701 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7703 return g_slist_copy (impl->filters);
7706 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7708 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7711 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7714 struct AddShortcutData
7716 GtkFileChooserDefault *impl;
7721 add_shortcut_get_info_cb (GCancellable *cancellable,
7723 const GError *error,
7727 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7728 struct AddShortcutData *data = user_data;
7730 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7733 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7735 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7738 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7740 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7743 g_object_unref (data->impl);
7744 g_object_unref (data->file);
7747 g_object_unref (cancellable);
7751 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7755 GCancellable *cancellable;
7756 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7757 struct AddShortcutData *data;
7761 /* Avoid adding duplicates */
7762 pos = shortcut_find_position (impl, file);
7763 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7767 uri = g_file_get_uri (file);
7768 /* translators, "Shortcut" means "Bookmark" here */
7770 GTK_FILE_CHOOSER_ERROR,
7771 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7772 _("Shortcut %s already exists"),
7779 for (l = impl->loading_shortcuts; l; l = l->next)
7781 GCancellable *c = l->data;
7784 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7785 if (f && g_file_equal (file, f))
7789 uri = g_file_get_uri (file);
7791 GTK_FILE_CHOOSER_ERROR,
7792 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7793 _("Shortcut %s already exists"),
7801 data = g_new0 (struct AddShortcutData, 1);
7802 data->impl = g_object_ref (impl);
7803 data->file = g_object_ref (file);
7805 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7807 add_shortcut_get_info_cb, data);
7812 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7813 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7819 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7823 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7830 for (l = impl->loading_shortcuts; l; l = l->next)
7832 GCancellable *c = l->data;
7835 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7836 if (f && g_file_equal (file, f))
7838 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7839 g_cancellable_cancel (c);
7844 if (impl->num_shortcuts == 0)
7847 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7848 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7849 g_assert_not_reached ();
7851 for (i = 0; i < impl->num_shortcuts; i++)
7854 ShortcutType shortcut_type;
7857 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7858 SHORTCUTS_COL_DATA, &col_data,
7859 SHORTCUTS_COL_TYPE, &shortcut_type,
7861 g_assert (col_data != NULL);
7862 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7864 shortcut = col_data;
7865 if (g_file_equal (shortcut, file))
7867 shortcuts_remove_rows (impl, pos + i, 1);
7868 impl->num_shortcuts--;
7872 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7873 g_assert_not_reached ();
7878 uri = g_file_get_uri (file);
7879 /* translators, "Shortcut" means "Bookmark" here */
7881 GTK_FILE_CHOOSER_ERROR,
7882 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7883 _("Shortcut %s does not exist"),
7891 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7893 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7899 if (impl->num_shortcuts == 0)
7902 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7903 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7904 g_assert_not_reached ();
7908 for (i = 0; i < impl->num_shortcuts; i++)
7911 ShortcutType shortcut_type;
7914 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7915 SHORTCUTS_COL_DATA, &col_data,
7916 SHORTCUTS_COL_TYPE, &shortcut_type,
7918 g_assert (col_data != NULL);
7919 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7921 shortcut = col_data;
7922 list = g_slist_prepend (list, g_object_ref (shortcut));
7924 if (i != impl->num_shortcuts - 1)
7926 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7927 g_assert_not_reached ();
7931 return g_slist_reverse (list);
7934 /* Guesses a size based upon font sizes */
7936 find_good_size_from_style (GtkWidget *widget,
7940 GtkStyleContext *context;
7941 GtkStateFlags state;
7946 context = gtk_widget_get_style_context (widget);
7947 state = gtk_widget_get_state_flags (widget);
7949 screen = gtk_widget_get_screen (widget);
7952 resolution = gdk_screen_get_resolution (screen);
7953 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7957 resolution = 96.0; /* wheeee */
7959 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7960 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7962 *width = font_size * NUM_CHARS;
7963 *height = font_size * NUM_LINES;
7967 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7968 gint *default_width,
7969 gint *default_height)
7971 GtkFileChooserDefault *impl;
7974 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7976 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7977 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7978 || impl->expand_folders)
7980 int x, y, width, height;
7982 settings_ensure (impl);
7984 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7985 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7987 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7989 *default_width = width;
7990 *default_height = height;
7994 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7996 if (impl->preview_widget_active &&
7997 impl->preview_widget &&
7998 gtk_widget_get_visible (impl->preview_widget))
8000 gtk_widget_get_preferred_size (impl->preview_box,
8002 *default_width += PREVIEW_HBOX_SPACING + req.width;
8005 if (impl->extra_widget &&
8006 gtk_widget_get_visible (impl->extra_widget))
8008 gtk_widget_get_preferred_size (impl->extra_align,
8010 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8015 gtk_widget_get_preferred_size (GTK_WIDGET (impl),
8017 *default_width = req.width;
8018 *default_height = req.height;
8022 struct switch_folder_closure {
8023 GtkFileChooserDefault *impl;
8028 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8030 switch_folder_foreach_cb (GtkTreeModel *model,
8035 struct switch_folder_closure *closure;
8039 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8040 closure->num_selected++;
8043 /* Changes to the selected folder in the list view */
8045 switch_to_selected_folder (GtkFileChooserDefault *impl)
8047 GtkTreeSelection *selection;
8048 struct switch_folder_closure closure;
8050 /* We do this with foreach() rather than get_selected() as we may be in
8051 * multiple selection mode
8054 closure.impl = impl;
8055 closure.file = NULL;
8056 closure.num_selected = 0;
8058 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8059 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8061 g_assert (closure.file && closure.num_selected == 1);
8063 change_folder_and_display_error (impl, closure.file, FALSE);
8066 /* Gets the GFileInfo for the selected row in the file list; assumes single
8070 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8071 gboolean *had_selection)
8073 GtkTreeSelection *selection;
8077 g_assert (!impl->select_multiple);
8078 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8079 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8081 *had_selection = FALSE;
8085 *had_selection = TRUE;
8087 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8091 /* Gets the display name of the selected file in the file list; assumes single
8092 * selection mode and that something is selected.
8094 static const gchar *
8095 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8098 gboolean had_selection;
8100 info = get_selected_file_info_from_file_list (impl, &had_selection);
8101 g_assert (had_selection);
8102 g_assert (info != NULL);
8104 return g_file_info_get_display_name (info);
8108 add_custom_button_to_dialog (GtkDialog *dialog,
8109 const gchar *mnemonic_label,
8110 const gchar *stock_id,
8115 button = gtk_button_new_with_mnemonic (mnemonic_label);
8116 gtk_widget_set_can_default (button, TRUE);
8117 gtk_button_set_image (GTK_BUTTON (button),
8118 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8119 gtk_widget_show (button);
8121 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8124 /* Presents an overwrite confirmation dialog; returns whether we should accept
8128 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8129 const gchar *file_part,
8130 const gchar *folder_display_name)
8132 GtkWindow *toplevel;
8136 toplevel = get_toplevel (GTK_WIDGET (impl));
8138 dialog = gtk_message_dialog_new (toplevel,
8139 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8140 GTK_MESSAGE_QUESTION,
8142 _("A file named \"%s\" already exists. Do you want to replace it?"),
8144 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8145 _("The file already exists in \"%s\". Replacing it will "
8146 "overwrite its contents."),
8147 folder_display_name);
8149 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8150 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8151 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8152 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8153 GTK_RESPONSE_ACCEPT,
8154 GTK_RESPONSE_CANCEL,
8156 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8158 if (gtk_window_has_group (toplevel))
8159 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8160 GTK_WINDOW (dialog));
8162 response = gtk_dialog_run (GTK_DIALOG (dialog));
8164 gtk_widget_destroy (dialog);
8166 return (response == GTK_RESPONSE_ACCEPT);
8169 struct GetDisplayNameData
8171 GtkFileChooserDefault *impl;
8176 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8178 const GError *error,
8181 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8182 gboolean should_respond = FALSE;
8183 struct GetDisplayNameData *data = user_data;
8185 if (cancellable != data->impl->should_respond_get_info_cancellable)
8188 data->impl->should_respond_get_info_cancellable = NULL;
8194 /* Huh? Did the folder disappear? Let the caller deal with it */
8195 should_respond = TRUE;
8197 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8199 set_busy_cursor (data->impl, FALSE);
8201 g_signal_emit_by_name (data->impl, "response-requested");
8204 g_object_unref (data->impl);
8205 g_free (data->file_part);
8208 g_object_unref (cancellable);
8211 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8212 * should respond. Can get the file part from the file list or the save entry.
8215 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8216 const gchar *file_part,
8219 GtkFileChooserConfirmation conf;
8221 if (!impl->do_overwrite_confirmation)
8224 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8226 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8230 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8232 struct GetDisplayNameData *data;
8234 g_assert (file_part != NULL);
8236 data = g_new0 (struct GetDisplayNameData, 1);
8237 data->impl = g_object_ref (impl);
8238 data->file_part = g_strdup (file_part);
8240 if (impl->should_respond_get_info_cancellable)
8241 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8243 impl->should_respond_get_info_cancellable =
8244 _gtk_file_system_get_info (impl->file_system, parent_file,
8245 "standard::display-name",
8246 confirmation_confirm_get_info_cb,
8248 set_busy_cursor (data->impl, TRUE);
8252 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8255 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8259 g_assert_not_reached ();
8264 struct FileExistsData
8266 GtkFileChooserDefault *impl;
8267 gboolean file_exists_and_is_not_folder;
8273 name_entry_get_parent_info_cb (GCancellable *cancellable,
8275 const GError *error,
8278 gboolean parent_is_folder;
8279 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8280 struct FileExistsData *data = user_data;
8282 if (cancellable != data->impl->should_respond_get_info_cancellable)
8285 data->impl->should_respond_get_info_cancellable = NULL;
8287 set_busy_cursor (data->impl, FALSE);
8293 parent_is_folder = FALSE;
8295 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8297 if (parent_is_folder)
8299 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8301 g_signal_emit_by_name (data->impl, "response-requested"); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8303 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8305 if (data->file_exists_and_is_not_folder)
8310 /* Dup the string because the string may be modified
8311 * depending on what clients do in the confirm-overwrite
8312 * signal and this corrupts the pointer
8314 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8315 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8319 g_signal_emit_by_name (data->impl, "response-requested");
8322 g_signal_emit_by_name (data->impl, "response-requested");
8324 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8325 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8327 GError *mkdir_error = NULL;
8329 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8330 * "/blah/nonexistent" you *will* want a folder created.
8333 set_busy_cursor (data->impl, TRUE);
8334 g_file_make_directory (data->file, NULL, &mkdir_error);
8335 set_busy_cursor (data->impl, FALSE);
8338 g_signal_emit_by_name (data->impl, "response-requested");
8340 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8343 g_assert_not_reached ();
8349 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8350 error_with_file_under_nonfolder (data->impl, data->parent_file);
8356 /* The parent folder is not readable for some reason */
8358 error_copy = g_error_copy (error);
8359 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8364 g_object_unref (data->impl);
8365 g_object_unref (data->file);
8366 g_object_unref (data->parent_file);
8369 g_object_unref (cancellable);
8373 file_exists_get_info_cb (GCancellable *cancellable,
8375 const GError *error,
8378 gboolean data_ownership_taken = FALSE;
8379 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8380 gboolean file_exists;
8382 gboolean needs_parent_check = FALSE;
8383 struct FileExistsData *data = user_data;
8385 if (cancellable != data->impl->file_exists_get_info_cancellable)
8388 data->impl->file_exists_get_info_cancellable = NULL;
8390 set_busy_cursor (data->impl, FALSE);
8395 file_exists = (info != NULL);
8396 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8398 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8401 change_folder_and_display_error (data->impl, data->file, TRUE);
8405 g_signal_emit_by_name (data->impl, "response-requested"); /* user typed an existing filename; we are done */
8407 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8410 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8412 if (file_exists && !is_folder)
8414 /* Oops, the user typed the name of an existing path which is not
8417 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8418 g_error_copy (error));
8422 needs_parent_check = TRUE;
8425 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8429 needs_parent_check = TRUE;
8435 /* User typed a folder; we are done */
8436 g_signal_emit_by_name (data->impl, "response-requested");
8439 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8442 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8445 change_folder_and_display_error (data->impl, data->file, TRUE);
8447 needs_parent_check = TRUE;
8451 g_assert_not_reached();
8454 if (needs_parent_check) {
8455 /* check that everything up to the last path component exists (i.e. the parent) */
8457 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8458 data_ownership_taken = TRUE;
8460 if (data->impl->should_respond_get_info_cancellable)
8461 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8463 data->impl->should_respond_get_info_cancellable =
8464 _gtk_file_system_get_info (data->impl->file_system,
8467 name_entry_get_parent_info_cb,
8469 set_busy_cursor (data->impl, TRUE);
8473 if (!data_ownership_taken)
8475 g_object_unref (data->impl);
8476 g_object_unref (data->file);
8477 g_object_unref (data->parent_file);
8481 g_object_unref (cancellable);
8485 paste_text_received (GtkClipboard *clipboard,
8487 GtkFileChooserDefault *impl)
8494 file = g_file_new_for_uri (text);
8496 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8497 location_popup_handler (impl, text);
8499 g_object_unref (file);
8502 /* Handler for the "location-popup-on-paste" keybinding signal */
8504 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8506 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8507 GDK_SELECTION_CLIPBOARD);
8508 gtk_clipboard_request_text (clipboard,
8509 (GtkClipboardTextReceivedFunc) paste_text_received,
8514 /* Implementation for GtkFileChooserEmbed::should_respond() */
8516 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8518 GtkFileChooserDefault *impl;
8519 GtkWidget *toplevel;
8520 GtkWidget *current_focus;
8522 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8524 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8525 g_assert (GTK_IS_WINDOW (toplevel));
8527 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8529 if (current_focus == impl->browse_files_tree_view)
8531 /* The following array encodes what we do based on the impl->action and the
8532 * number of files selected.
8535 NOOP, /* Do nothing (don't respond) */
8536 RESPOND, /* Respond immediately */
8537 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8538 ALL_FILES, /* Respond only if everything selected is a file */
8539 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8540 SAVE_ENTRY, /* Go to the code for handling the save entry */
8541 NOT_REACHED /* Sanity check */
8543 static const ActionToTake what_to_do[4][3] = {
8544 /* 0 selected 1 selected many selected */
8545 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8546 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8547 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8548 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8552 gboolean all_files, all_folders;
8554 ActionToTake action;
8558 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8560 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8561 return search_should_respond (impl);
8563 if (impl->operation_mode == OPERATION_MODE_RECENT)
8564 return recent_should_respond (impl);
8566 selection_check (impl, &num_selected, &all_files, &all_folders);
8568 if (num_selected > 2)
8573 action = what_to_do [impl->action] [k];
8583 case RESPOND_OR_SWITCH:
8584 g_assert (num_selected == 1);
8588 switch_to_selected_folder (impl);
8591 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8592 return should_respond_after_confirm_overwrite (impl,
8593 get_display_name_from_file_list (impl),
8594 impl->current_folder);
8608 g_assert_not_reached ();
8611 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8614 gboolean is_well_formed, is_empty, is_file_part_empty;
8617 GtkFileChooserEntry *entry;
8622 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8623 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8624 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8625 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8626 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8628 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8629 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8631 if (!is_well_formed)
8636 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8637 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8643 g_assert (file != NULL);
8648 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8649 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8651 change_folder_and_display_error (impl, file, TRUE);
8654 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8655 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8657 /* The folder already exists, so we do not need to create it.
8658 * Just respond to terminate the dialog.
8664 g_assert_not_reached ();
8670 struct FileExistsData *data;
8672 /* We need to check whether file exists and whether it is a folder -
8673 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8674 * negative (it doesn't know yet if your last path component is a
8678 data = g_new0 (struct FileExistsData, 1);
8679 data->impl = g_object_ref (impl);
8680 data->file = g_object_ref (file);
8681 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8683 if (impl->file_exists_get_info_cancellable)
8684 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8686 impl->file_exists_get_info_cancellable =
8687 _gtk_file_system_get_info (impl->file_system, file,
8689 file_exists_get_info_cb,
8692 set_busy_cursor (impl, TRUE);
8696 g_error_free (error);
8699 g_object_unref (file);
8702 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8704 /* The focus is on a dialog's action area button, *and* the widget that
8705 * was focused immediately before it is the file list.
8709 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8711 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8714 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8716 /* The focus is on a dialog's action area button, *and* the widget that
8717 * was focused immediately before it is the location entry.
8722 /* The focus is on a dialog's action area button or something else */
8723 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8724 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8729 g_assert_not_reached ();
8733 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8735 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8737 GtkFileChooserDefault *impl;
8740 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8742 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8743 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8745 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8746 widget = impl->browse_files_tree_view;
8748 widget = impl->location_entry;
8750 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8751 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8752 widget = impl->location_entry;
8755 g_assert_not_reached ();
8759 g_assert (widget != NULL);
8760 gtk_widget_grab_focus (widget);
8763 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8765 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8775 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8776 *list = g_slist_prepend (*list, file); /* The file already has a new ref courtesy of gtk_tree_model_get(); this will be unreffed by the caller */
8779 /* Constructs a list of the selected paths in search mode */
8781 search_get_selected_files (GtkFileChooserDefault *impl)
8784 GtkTreeSelection *selection;
8788 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8789 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8790 result = g_slist_reverse (result);
8795 /* Called from ::should_respond(). We return whether there are selected files
8796 * in the search list.
8799 search_should_respond (GtkFileChooserDefault *impl)
8801 GtkTreeSelection *selection;
8803 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8805 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8806 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8809 /* Adds one hit from the search engine to the search_model */
8811 search_add_hit (GtkFileChooserDefault *impl,
8816 file = g_file_new_for_uri (uri);
8820 if (!g_file_is_native (file))
8822 g_object_unref (file);
8826 _gtk_file_system_model_add_and_query_file (impl->search_model,
8830 g_object_unref (file);
8833 /* Callback used from GtkSearchEngine when we get new hits */
8835 search_engine_hits_added_cb (GtkSearchEngine *engine,
8839 GtkFileChooserDefault *impl;
8842 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8844 for (l = hits; l; l = l->next)
8845 search_add_hit (impl, (gchar*)l->data);
8848 /* Callback used from GtkSearchEngine when the query is done running */
8850 search_engine_finished_cb (GtkSearchEngine *engine,
8853 GtkFileChooserDefault *impl;
8855 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8858 /* EB: setting the model here will avoid loads of row events,
8859 * but it'll make the search look like blocked.
8861 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8862 GTK_TREE_MODEL (impl->search_model));
8865 /* FMQ: if search was empty, say that we got no hits */
8866 set_busy_cursor (impl, FALSE);
8869 /* Displays a generic error when we cannot create a GtkSearchEngine.
8870 * It would be better if _gtk_search_engine_new() gave us a GError
8871 * with a better message, but it doesn't do that right now.
8874 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8876 error_message (impl,
8877 _("Could not start the search process"),
8878 _("The program was not able to create a connection to the indexer "
8879 "daemon. Please make sure it is running."));
8883 search_engine_error_cb (GtkSearchEngine *engine,
8884 const gchar *message,
8887 GtkFileChooserDefault *impl;
8889 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8891 search_stop_searching (impl, TRUE);
8892 error_message (impl, _("Could not send the search request"), message);
8894 set_busy_cursor (impl, FALSE);
8897 /* Frees the data in the search_model */
8899 search_clear_model (GtkFileChooserDefault *impl,
8900 gboolean remove_from_treeview)
8902 if (!impl->search_model)
8905 g_object_unref (impl->search_model);
8906 impl->search_model = NULL;
8908 if (remove_from_treeview)
8909 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8912 /* Stops any ongoing searches; does not touch the search_model */
8914 search_stop_searching (GtkFileChooserDefault *impl,
8915 gboolean remove_query)
8917 if (remove_query && impl->search_query)
8919 g_object_unref (impl->search_query);
8920 impl->search_query = NULL;
8923 if (impl->search_engine)
8925 _gtk_search_engine_stop (impl->search_engine);
8927 g_object_unref (impl->search_engine);
8928 impl->search_engine = NULL;
8932 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8934 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8936 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8938 search_stop_searching (impl, FALSE);
8939 search_clear_model (impl, TRUE);
8941 gtk_widget_destroy (impl->search_hbox);
8942 impl->search_hbox = NULL;
8943 impl->search_entry = NULL;
8945 gtk_widget_show (impl->browse_path_bar);
8946 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8947 gtk_widget_hide (impl->browse_new_folder_button);
8949 gtk_widget_show (impl->browse_new_folder_button);
8952 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8953 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8955 gtk_widget_show (impl->location_button);
8957 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8958 gtk_widget_show (impl->location_entry_box);
8961 impl->operation_mode = OPERATION_MODE_BROWSE;
8963 file_list_set_sort_column_ids (impl);
8966 /* Creates the search_model and puts it in the tree view */
8968 search_setup_model (GtkFileChooserDefault *impl)
8970 g_assert (impl->search_model == NULL);
8972 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8974 MODEL_COLUMN_TYPES);
8976 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8980 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8984 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8988 set_sort_column (impl);
8990 /* EB: setting the model here will make the hits list update feel
8991 * more "alive" than setting the model at the end of the search
8994 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8995 GTK_TREE_MODEL (impl->search_model));
8998 /* Creates a new query with the specified text and launches it */
9000 search_start_query (GtkFileChooserDefault *impl,
9001 const gchar *query_text)
9003 search_stop_searching (impl, FALSE);
9004 search_clear_model (impl, TRUE);
9005 search_setup_model (impl);
9006 set_busy_cursor (impl, TRUE);
9008 if (impl->search_engine == NULL)
9009 impl->search_engine = _gtk_search_engine_new ();
9011 if (!impl->search_engine)
9013 set_busy_cursor (impl, FALSE);
9014 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9018 if (!impl->search_query)
9020 impl->search_query = _gtk_query_new ();
9021 _gtk_query_set_text (impl->search_query, query_text);
9024 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9026 g_signal_connect (impl->search_engine, "hits-added",
9027 G_CALLBACK (search_engine_hits_added_cb), impl);
9028 g_signal_connect (impl->search_engine, "finished",
9029 G_CALLBACK (search_engine_finished_cb), impl);
9030 g_signal_connect (impl->search_engine, "error",
9031 G_CALLBACK (search_engine_error_cb), impl);
9033 _gtk_search_engine_start (impl->search_engine);
9036 /* Callback used when the user presses Enter while typing on the search
9037 * entry; starts the query
9040 search_entry_activate_cb (GtkEntry *entry,
9043 GtkFileChooserDefault *impl;
9046 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9048 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9049 if (strlen (text) == 0)
9052 /* reset any existing query object */
9053 if (impl->search_query)
9055 g_object_unref (impl->search_query);
9056 impl->search_query = NULL;
9059 search_start_query (impl, text);
9063 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9065 GDK_THREADS_ENTER ();
9067 g_source_destroy (impl->focus_entry_idle);
9068 impl->focus_entry_idle = NULL;
9070 if (impl->search_entry)
9071 gtk_widget_grab_focus (impl->search_entry);
9073 GDK_THREADS_LEAVE ();
9079 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9081 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9082 * pane, we get a selection-changed signal and we set up the search widgets.
9083 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9084 * the change to the selection. So, we need to re-focus the search entry
9085 * after the treeview has finished doing its work; we'll do that in an idle
9089 if (!impl->focus_entry_idle)
9090 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9093 /* Hides the path bar and creates the search entry */
9095 search_setup_widgets (GtkFileChooserDefault *impl)
9101 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9105 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9106 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9110 label = gtk_label_new (NULL);
9111 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9112 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9113 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9118 impl->search_entry = gtk_entry_new ();
9119 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9120 g_signal_connect (impl->search_entry, "activate",
9121 G_CALLBACK (search_entry_activate_cb),
9123 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9125 /* if there already is a query, restart it */
9126 if (impl->search_query)
9128 gchar *query = _gtk_query_get_text (impl->search_query);
9132 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9133 search_start_query (impl, query);
9139 g_object_unref (impl->search_query);
9140 impl->search_query = NULL;
9144 gtk_widget_hide (impl->browse_path_bar);
9145 gtk_widget_hide (impl->browse_new_folder_button);
9147 /* Box for search widgets */
9148 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9149 gtk_widget_show_all (impl->search_hbox);
9150 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9152 /* Hide the location widgets temporarily */
9154 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9155 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9157 gtk_widget_hide (impl->location_button);
9158 gtk_widget_hide (impl->location_entry_box);
9161 focus_search_entry_in_idle (impl);
9163 /* FMQ: hide the filter combo? */
9166 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9168 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9172 case OPERATION_MODE_BROWSE:
9173 stop_loading_and_clear_list_model (impl, TRUE);
9176 case OPERATION_MODE_SEARCH:
9177 search_stop_searching (impl, FALSE);
9178 search_clear_model (impl, TRUE);
9180 gtk_widget_destroy (impl->search_hbox);
9181 impl->search_hbox = NULL;
9182 impl->search_entry = NULL;
9185 case OPERATION_MODE_RECENT:
9186 recent_stop_loading (impl);
9187 recent_clear_model (impl, TRUE);
9189 gtk_widget_destroy (impl->recent_hbox);
9190 impl->recent_hbox = NULL;
9195 /* Main entry point to the searching functions; this gets called when the user
9196 * activates the Search shortcut.
9199 search_activate (GtkFileChooserDefault *impl)
9201 OperationMode previous_mode;
9203 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9205 focus_search_entry_in_idle (impl);
9209 previous_mode = impl->operation_mode;
9210 impl->operation_mode = OPERATION_MODE_SEARCH;
9212 stop_operation (impl, previous_mode);
9214 g_assert (impl->search_hbox == NULL);
9215 g_assert (impl->search_entry == NULL);
9216 g_assert (impl->search_model == NULL);
9218 search_setup_widgets (impl);
9219 file_list_set_sort_column_ids (impl);
9223 * Recent files support
9226 /* Frees the data in the recent_model */
9228 recent_clear_model (GtkFileChooserDefault *impl,
9229 gboolean remove_from_treeview)
9231 if (!impl->recent_model)
9234 if (remove_from_treeview)
9235 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9237 g_object_unref (impl->recent_model);
9238 impl->recent_model = NULL;
9241 /* Stops any ongoing loading of the recent files list; does
9242 * not touch the recent_model
9245 recent_stop_loading (GtkFileChooserDefault *impl)
9247 if (impl->load_recent_id)
9249 g_source_remove (impl->load_recent_id);
9250 impl->load_recent_id = 0;
9254 /* Stops any pending load, clears the file list, and switches
9255 * back to OPERATION_MODE_BROWSE
9258 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9260 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9262 recent_stop_loading (impl);
9263 recent_clear_model (impl, TRUE);
9265 gtk_widget_destroy (impl->recent_hbox);
9266 impl->recent_hbox = NULL;
9268 gtk_widget_show (impl->browse_path_bar);
9269 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9270 gtk_widget_hide (impl->browse_new_folder_button);
9272 gtk_widget_show (impl->browse_new_folder_button);
9274 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9275 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9277 gtk_widget_show (impl->location_button);
9279 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9280 gtk_widget_show (impl->location_entry_box);
9283 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9285 impl->operation_mode = OPERATION_MODE_BROWSE;
9287 file_list_set_sort_column_ids (impl);
9291 recent_setup_model (GtkFileChooserDefault *impl)
9293 g_assert (impl->recent_model == NULL);
9295 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9297 MODEL_COLUMN_TYPES);
9299 _gtk_file_system_model_set_filter (impl->recent_model,
9300 impl->current_filter);
9301 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9305 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9309 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9313 set_sort_column (impl);
9318 GtkFileChooserDefault *impl;
9321 gint n_loaded_items;
9322 guint needs_sorting : 1;
9326 recent_idle_cleanup (gpointer data)
9328 RecentLoadData *load_data = data;
9329 GtkFileChooserDefault *impl = load_data->impl;
9331 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9332 GTK_TREE_MODEL (impl->recent_model));
9334 set_busy_cursor (impl, FALSE);
9336 impl->load_recent_id = 0;
9338 if (load_data->items)
9340 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9341 g_list_free (load_data->items);
9348 recent_sort_mru (gconstpointer a,
9351 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9352 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9354 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9358 get_recent_files_limit (GtkWidget *widget)
9360 GtkSettings *settings;
9363 if (gtk_widget_has_screen (widget))
9364 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9366 settings = gtk_settings_get_default ();
9368 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9374 recent_idle_load (gpointer data)
9376 RecentLoadData *load_data = data;
9377 GtkFileChooserDefault *impl = load_data->impl;
9381 if (!impl->recent_manager)
9384 /* first iteration: load all the items */
9385 if (!load_data->items)
9387 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9388 if (!load_data->items)
9391 load_data->needs_sorting = TRUE;
9396 /* second iteration: preliminary MRU sorting and clamping */
9397 if (load_data->needs_sorting)
9401 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9402 load_data->n_items = g_list_length (load_data->items);
9404 limit = get_recent_files_limit (GTK_WIDGET (impl));
9406 if (limit != -1 && (load_data->n_items > limit))
9410 clamp = g_list_nth (load_data->items, limit - 1);
9411 if (G_LIKELY (clamp))
9416 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9419 load_data->n_items = limit;
9423 load_data->n_loaded_items = 0;
9424 load_data->needs_sorting = FALSE;
9429 /* finished loading items */
9430 for (walk = load_data->items; walk; walk = walk->next)
9432 GtkRecentInfo *info = walk->data;
9433 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9435 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9438 gtk_recent_info_unref (walk->data);
9439 g_object_unref (file);
9442 g_list_free (load_data->items);
9443 load_data->items = NULL;
9449 recent_start_loading (GtkFileChooserDefault *impl)
9451 RecentLoadData *load_data;
9453 recent_stop_loading (impl);
9454 recent_clear_model (impl, TRUE);
9455 recent_setup_model (impl);
9456 set_busy_cursor (impl, TRUE);
9458 g_assert (impl->load_recent_id == 0);
9460 load_data = g_new (RecentLoadData, 1);
9461 load_data->impl = impl;
9462 load_data->items = NULL;
9463 load_data->n_items = 0;
9464 load_data->n_loaded_items = 0;
9465 load_data->needs_sorting = TRUE;
9467 /* begin lazy loading the recent files into the model */
9468 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9471 recent_idle_cleanup);
9475 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9485 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9486 *list = g_slist_prepend (*list, file);
9489 /* Constructs a list of the selected paths in recent files mode */
9491 recent_get_selected_files (GtkFileChooserDefault *impl)
9494 GtkTreeSelection *selection;
9498 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9499 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9500 result = g_slist_reverse (result);
9505 /* Called from ::should_respond(). We return whether there are selected
9506 * files in the recent files list.
9509 recent_should_respond (GtkFileChooserDefault *impl)
9511 GtkTreeSelection *selection;
9513 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9515 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9516 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9519 /* Hide the location widgets temporarily */
9521 recent_hide_entry (GtkFileChooserDefault *impl)
9527 impl->recent_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9530 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9531 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9534 label = gtk_label_new (NULL);
9535 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9536 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9537 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9540 gtk_widget_hide (impl->browse_path_bar);
9541 gtk_widget_hide (impl->browse_new_folder_button);
9543 /* Box for recent widgets */
9544 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9545 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->recent_hbox);
9546 gtk_widget_show_all (impl->recent_hbox);
9548 /* Hide the location widgets temporarily */
9549 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9550 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9552 gtk_widget_hide (impl->location_button);
9553 gtk_widget_hide (impl->location_entry_box);
9557 /* Main entry point to the recent files functions; this gets called when
9558 * the user activates the Recently Used shortcut.
9561 recent_activate (GtkFileChooserDefault *impl)
9563 OperationMode previous_mode;
9565 if (impl->operation_mode == OPERATION_MODE_RECENT)
9568 previous_mode = impl->operation_mode;
9569 impl->operation_mode = OPERATION_MODE_RECENT;
9571 stop_operation (impl, previous_mode);
9573 recent_hide_entry (impl);
9575 file_list_set_sort_column_ids (impl);
9576 recent_start_loading (impl);
9580 set_current_filter (GtkFileChooserDefault *impl,
9581 GtkFileFilter *filter)
9583 if (impl->current_filter != filter)
9587 /* NULL filters are allowed to reset to non-filtered status
9589 filter_index = g_slist_index (impl->filters, filter);
9590 if (impl->filters && filter && filter_index < 0)
9593 if (impl->current_filter)
9594 g_object_unref (impl->current_filter);
9595 impl->current_filter = filter;
9596 if (impl->current_filter)
9598 g_object_ref_sink (impl->current_filter);
9602 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9605 if (impl->browse_files_model)
9606 install_list_model_filter (impl);
9608 if (impl->search_model)
9609 _gtk_file_system_model_set_filter (impl->search_model, filter);
9611 if (impl->recent_model)
9612 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9614 g_object_notify (G_OBJECT (impl), "filter");
9619 filter_combo_changed (GtkComboBox *combo_box,
9620 GtkFileChooserDefault *impl)
9622 gint new_index = gtk_combo_box_get_active (combo_box);
9623 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9625 set_current_filter (impl, new_filter);
9629 check_preview_change (GtkFileChooserDefault *impl)
9631 GtkTreePath *cursor_path;
9633 char *new_display_name;
9634 GtkTreeModel *model;
9636 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9637 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9642 gtk_tree_model_get_iter (model, &iter, cursor_path);
9643 gtk_tree_model_get (model, &iter,
9644 MODEL_COL_FILE, &new_file,
9645 MODEL_COL_NAME, &new_display_name,
9648 gtk_tree_path_free (cursor_path);
9653 new_display_name = NULL;
9656 if (new_file != impl->preview_file &&
9657 !(new_file && impl->preview_file &&
9658 g_file_equal (new_file, impl->preview_file)))
9660 if (impl->preview_file)
9662 g_object_unref (impl->preview_file);
9663 g_free (impl->preview_display_name);
9668 impl->preview_file = new_file;
9669 impl->preview_display_name = new_display_name;
9673 impl->preview_file = NULL;
9674 impl->preview_display_name = NULL;
9675 g_free (new_display_name);
9678 if (impl->use_preview_label && impl->preview_label)
9679 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9681 g_signal_emit_by_name (impl, "update-preview");
9686 g_object_unref (new_file);
9688 g_free (new_display_name);
9693 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9694 GtkFileSystemVolume *volume,
9695 const GError *error,
9699 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9700 GtkFileChooserDefault *impl = data;
9702 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9705 impl->shortcuts_activate_iter_cancellable = NULL;
9707 set_busy_cursor (impl, FALSE);
9714 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9718 name = _gtk_file_system_volume_get_display_name (volume);
9719 msg = g_strdup_printf (_("Could not mount %s"), name);
9721 error_message (impl, msg, error->message);
9730 file = _gtk_file_system_volume_get_root (volume);
9733 change_folder_and_display_error (impl, file, FALSE);
9734 g_object_unref (file);
9738 g_object_unref (impl);
9739 g_object_unref (cancellable);
9743 /* Activates a volume by mounting it if necessary and then switching to its
9747 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9748 GtkFileSystemVolume *volume)
9752 switch (impl->operation_mode)
9754 case OPERATION_MODE_BROWSE:
9756 case OPERATION_MODE_SEARCH:
9757 search_switch_to_browse_mode (impl);
9759 case OPERATION_MODE_RECENT:
9760 recent_switch_to_browse_mode (impl);
9764 /* We ref the file chooser since volume_mount() may run a main loop, and the
9765 * user could close the file chooser window in the meantime.
9767 g_object_ref (impl);
9769 if (!_gtk_file_system_volume_is_mounted (volume))
9771 GMountOperation *mount_op;
9773 set_busy_cursor (impl, TRUE);
9775 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9776 impl->shortcuts_activate_iter_cancellable =
9777 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9778 shortcuts_activate_volume_mount_cb,
9779 g_object_ref (impl));
9780 g_object_unref (mount_op);
9784 file = _gtk_file_system_volume_get_root (volume);
9787 change_folder_and_display_error (impl, file, FALSE);
9788 g_object_unref (file);
9792 g_object_unref (impl);
9795 /* Opens the folder or volume at the specified iter in the shortcuts model */
9796 struct ShortcutsActivateData
9798 GtkFileChooserDefault *impl;
9803 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9805 const GError *error,
9808 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9809 struct ShortcutsActivateData *data = user_data;
9811 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9814 data->impl->shortcuts_activate_iter_cancellable = NULL;
9819 if (!error && _gtk_file_info_consider_as_directory (info))
9820 change_folder_and_display_error (data->impl, data->file, FALSE);
9822 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9827 g_object_unref (data->impl);
9828 g_object_unref (data->file);
9831 g_object_unref (cancellable);
9835 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9836 GtkFileSystemVolume *volume,
9837 const GError *error,
9840 struct ShortcutsActivateData *data = user_data;
9844 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9846 g_object_unref (data->impl);
9847 g_object_unref (data->file);
9853 data->impl->shortcuts_activate_iter_cancellable =
9854 _gtk_file_system_get_info (data->impl->file_system, data->file,
9856 shortcuts_activate_get_info_cb, data);
9859 _gtk_file_system_volume_unref (volume);
9863 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9867 ShortcutType shortcut_type;
9869 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9870 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9871 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9872 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9874 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9875 SHORTCUTS_COL_DATA, &col_data,
9876 SHORTCUTS_COL_TYPE, &shortcut_type,
9879 if (impl->shortcuts_activate_iter_cancellable)
9881 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9882 impl->shortcuts_activate_iter_cancellable = NULL;
9885 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9887 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9889 GtkFileSystemVolume *volume;
9893 shortcuts_activate_volume (impl, volume);
9895 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9897 struct ShortcutsActivateData *data;
9898 GtkFileSystemVolume *volume;
9900 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9902 data = g_new0 (struct ShortcutsActivateData, 1);
9903 data->impl = g_object_ref (impl);
9904 data->file = g_object_ref (col_data);
9906 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9908 GMountOperation *mount_operation;
9909 GtkWidget *toplevel;
9911 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9913 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9915 impl->shortcuts_activate_iter_cancellable =
9916 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9918 shortcuts_activate_mount_enclosing_volume,
9923 impl->shortcuts_activate_iter_cancellable =
9924 _gtk_file_system_get_info (impl->file_system, data->file,
9926 shortcuts_activate_get_info_cb, data);
9929 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9931 search_activate (impl);
9933 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9935 recent_activate (impl);
9939 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9941 shortcuts_key_press_event_cb (GtkWidget *widget,
9943 GtkFileChooserDefault *impl)
9947 modifiers = gtk_accelerator_get_default_mod_mask ();
9949 if (key_is_left_or_right (event))
9951 gtk_widget_grab_focus (impl->browse_files_tree_view);
9955 if ((event->keyval == GDK_KEY_BackSpace
9956 || event->keyval == GDK_KEY_Delete
9957 || event->keyval == GDK_KEY_KP_Delete)
9958 && (event->state & modifiers) == 0)
9960 remove_selected_bookmarks (impl);
9964 if ((event->keyval == GDK_KEY_F2)
9965 && (event->state & modifiers) == 0)
9967 rename_selected_bookmark (impl);
9975 shortcuts_select_func (GtkTreeSelection *selection,
9976 GtkTreeModel *model,
9978 gboolean path_currently_selected,
9981 GtkFileChooserDefault *impl = data;
9982 GtkTreeIter filter_iter;
9983 ShortcutType shortcut_type;
9985 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9986 g_assert_not_reached ();
9988 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9990 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9994 list_select_func (GtkTreeSelection *selection,
9995 GtkTreeModel *model,
9997 gboolean path_currently_selected,
10000 GtkFileChooserDefault *impl = data;
10002 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10003 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10006 gboolean is_folder;
10008 if (!gtk_tree_model_get_iter (model, &iter, path))
10010 gtk_tree_model_get (model, &iter,
10011 MODEL_COL_IS_FOLDER, &is_folder,
10021 list_selection_changed (GtkTreeSelection *selection,
10022 GtkFileChooserDefault *impl)
10024 /* See if we are in the new folder editable row for Save mode */
10025 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10026 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10029 gboolean had_selection;
10031 info = get_selected_file_info_from_file_list (impl, &had_selection);
10032 if (!had_selection)
10033 goto out; /* normal processing */
10036 return; /* We are on the editable row for New Folder */
10041 if (impl->location_entry)
10042 update_chooser_entry (impl);
10044 check_preview_change (impl);
10045 bookmarks_check_add_sensitivity (impl);
10047 g_signal_emit_by_name (impl, "selection-changed", 0);
10050 /* Callback used when a row in the file list is activated */
10052 list_row_activated (GtkTreeView *tree_view,
10054 GtkTreeViewColumn *column,
10055 GtkFileChooserDefault *impl)
10059 GtkTreeModel *model;
10060 gboolean is_folder;
10062 model = gtk_tree_view_get_model (tree_view);
10064 if (!gtk_tree_model_get_iter (model, &iter, path))
10067 gtk_tree_model_get (model, &iter,
10068 MODEL_COL_FILE, &file,
10069 MODEL_COL_IS_FOLDER, &is_folder,
10072 if (is_folder && file)
10074 change_folder_and_display_error (impl, file, FALSE);
10075 g_object_unref (file);
10079 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10080 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10081 g_signal_emit_by_name (impl, "file-activated");
10086 g_object_unref (file);
10090 path_bar_clicked (GtkPathBar *path_bar,
10093 gboolean child_is_hidden,
10094 GtkFileChooserDefault *impl)
10097 pending_select_files_add (impl, child_file);
10099 if (!change_folder_and_display_error (impl, file, FALSE))
10102 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10103 * show hidden files so that ".baz" appears in the file list, as it will still
10104 * be shown in the path bar: "/foo/[bar]/.baz"
10106 if (child_is_hidden)
10107 g_object_set (impl, "show-hidden", TRUE, NULL);
10111 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10113 GtkTreeViewColumn *column;
10114 GtkCellRenderer *renderer;
10115 GList *walk, *list;
10116 gboolean always_sensitive;
10118 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10119 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10121 /* Keep the following column numbers in sync with create_file_list() */
10124 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10125 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10126 for (walk = list; walk; walk = walk->next)
10128 renderer = walk->data;
10129 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10131 gtk_tree_view_column_set_attributes (column, renderer,
10132 "pixbuf", MODEL_COL_PIXBUF,
10137 gtk_tree_view_column_set_attributes (column, renderer,
10138 "text", MODEL_COL_NAME,
10139 "ellipsize", MODEL_COL_ELLIPSIZE,
10142 if (always_sensitive)
10143 g_object_set (renderer, "sensitive", TRUE, NULL);
10145 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10147 g_list_free (list);
10150 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10151 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10152 renderer = list->data;
10153 gtk_tree_view_column_set_attributes (column, renderer,
10154 "text", MODEL_COL_SIZE_TEXT,
10156 if (always_sensitive)
10157 g_object_set (renderer, "sensitive", TRUE, NULL);
10159 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10160 g_list_free (list);
10163 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10164 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10165 renderer = list->data;
10166 gtk_tree_view_column_set_attributes (column, renderer,
10167 "text", MODEL_COL_MTIME_TEXT,
10169 if (always_sensitive)
10170 g_object_set (renderer, "sensitive", TRUE, NULL);
10172 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10173 g_list_free (list);
10177 _gtk_file_chooser_default_new (void)
10179 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10183 location_set_user_text (GtkFileChooserDefault *impl,
10186 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10187 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10191 location_popup_handler (GtkFileChooserDefault *impl,
10194 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10196 GtkWidget *widget_to_focus;
10198 /* This will give us the location widgets back */
10199 switch (impl->operation_mode)
10201 case OPERATION_MODE_SEARCH:
10202 search_switch_to_browse_mode (impl);
10204 case OPERATION_MODE_RECENT:
10205 recent_switch_to_browse_mode (impl);
10207 case OPERATION_MODE_BROWSE:
10208 g_assert_not_reached ();
10212 if (impl->current_folder)
10213 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10215 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10216 widget_to_focus = impl->browse_files_tree_view;
10218 widget_to_focus = impl->location_entry;
10220 gtk_widget_grab_focus (widget_to_focus);
10224 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10225 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10227 LocationMode new_mode;
10231 /* since the user typed something, we unconditionally want to turn on the entry */
10232 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10234 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10235 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10236 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10237 new_mode = LOCATION_MODE_PATH_BAR;
10240 g_assert_not_reached ();
10244 location_mode_set (impl, new_mode, TRUE);
10245 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10248 location_set_user_text (impl, path);
10251 location_entry_set_initial_text (impl);
10252 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10256 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10257 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10259 gtk_widget_grab_focus (impl->location_entry);
10261 location_set_user_text (impl, path);
10264 g_assert_not_reached ();
10267 /* Handler for the "up-folder" keybinding signal */
10269 up_folder_handler (GtkFileChooserDefault *impl)
10271 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10274 /* Handler for the "down-folder" keybinding signal */
10276 down_folder_handler (GtkFileChooserDefault *impl)
10278 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10281 /* Switches to the shortcut in the specified index */
10283 switch_to_shortcut (GtkFileChooserDefault *impl,
10288 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10289 g_assert_not_reached ();
10291 shortcuts_activate_iter (impl, &iter);
10294 /* Handler for the "home-folder" keybinding signal */
10296 home_folder_handler (GtkFileChooserDefault *impl)
10298 if (impl->has_home)
10299 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10302 /* Handler for the "desktop-folder" keybinding signal */
10304 desktop_folder_handler (GtkFileChooserDefault *impl)
10306 if (impl->has_desktop)
10307 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10310 /* Handler for the "search-shortcut" keybinding signal */
10312 search_shortcut_handler (GtkFileChooserDefault *impl)
10314 if (impl->has_search)
10316 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10318 /* we want the entry widget to grab the focus the first
10319 * time, not the browse_files_tree_view widget.
10321 if (impl->search_entry)
10322 gtk_widget_grab_focus (impl->search_entry);
10326 /* Handler for the "recent-shortcut" keybinding signal */
10328 recent_shortcut_handler (GtkFileChooserDefault *impl)
10330 if (impl->has_recent)
10331 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10335 quick_bookmark_handler (GtkFileChooserDefault *impl,
10336 gint bookmark_index)
10341 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10344 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10346 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10347 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10350 gtk_tree_path_free (path);
10352 switch_to_shortcut (impl, bookmark_pos);
10356 show_hidden_handler (GtkFileChooserDefault *impl)
10358 g_object_set (impl,
10359 "show-hidden", !impl->show_hidden,
10364 /* Drag and drop interfaces */
10367 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10372 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10374 model->impl = NULL;
10377 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10379 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10382 ShortcutsPaneModelFilter *model;
10386 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10388 pos = *gtk_tree_path_get_indices (path);
10389 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10391 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10394 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10398 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10400 GtkSelectionData *selection_data)
10407 /* Fill the GtkTreeDragSourceIface vtable */
10409 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10411 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10412 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10416 /* Fill the GtkTreeDragDestIface vtable */
10418 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10420 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10421 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10425 static GtkTreeModel *
10426 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10427 GtkTreeModel *child_model,
10430 ShortcutsPaneModelFilter *model;
10432 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10433 "child-model", child_model,
10434 "virtual-root", root,
10437 model->impl = impl;
10439 return GTK_TREE_MODEL (model);