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, see <http://www.gnu.org/licenses/>.
22 #include "gtkfilechooserdefault.h"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrendererpixbuf.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcheckmenuitem.h"
29 #include "gtkclipboard.h"
30 #include "gtkcomboboxtext.h"
32 #include "gtkexpander.h"
33 #include "gtkfilechooserprivate.h"
34 #include "gtkfilechooserdialog.h"
35 #include "gtkfilechooserembed.h"
36 #include "gtkfilechooserentry.h"
37 #include "gtkfilechooserutils.h"
38 #include "gtkfilechooser.h"
39 #include "gtkfilesystem.h"
40 #include "gtkfilesystemmodel.h"
43 #include "gtkiconfactory.h"
44 #include "gtkicontheme.h"
46 #include "gtkimagemenuitem.h"
47 #include "gtkinfobar.h"
49 #include "gtkmarshalers.h"
50 #include "gtkmessagedialog.h"
51 #include "gtkmountoperation.h"
52 #include "gtkpathbar.h"
53 #include "gtkprivate.h"
54 #include "gtkradiobutton.h"
55 #include "gtkrecentfilter.h"
56 #include "gtkrecentmanager.h"
57 #include "gtkscrolledwindow.h"
58 #include "gtkseparatormenuitem.h"
59 #include "gtksizegroup.h"
60 #include "gtksizerequest.h"
63 #include "gtktoolbar.h"
64 #include "gtktoolbutton.h"
65 #include "gtktooltip.h"
66 #include "gtktreednd.h"
67 #include "gtktreeprivate.h"
68 #include "gtktreeselection.h"
70 #include "gtkorientable.h"
77 #include <sys/types.h>
87 /* Values for GtkSelection-related "info" fields */
88 #define SELECTION_TEXT 0
89 #define SELECTION_URI 1
92 #undef PROFILE_FILE_CHOOSER
93 #ifdef PROFILE_FILE_CHOOSER
100 #define PROFILE_INDENT 4
102 static int profile_indent;
105 profile_add_indent (int indent)
107 profile_indent += indent;
108 if (profile_indent < 0)
109 g_error ("You screwed up your indentation");
113 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
118 profile_add_indent (indent);
120 if (profile_indent == 0)
121 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
123 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
129 profile_add_indent (indent);
132 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
133 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
134 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
136 #define profile_start(x, y)
137 #define profile_end(x, y)
138 #define profile_msg(x, y)
143 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
145 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
147 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
149 #define MAX_LOADING_TIME 500
151 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
153 struct _GtkFileChooserDefaultClass
155 GtkBoxClass parent_class;
161 LOCATION_POPUP_ON_PASTE,
167 LOCATION_TOGGLE_POPUP,
175 static guint signals[LAST_SIGNAL] = { 0 };
177 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
179 SHORTCUTS_COL_PIXBUF,
183 SHORTCUTS_COL_REMOVABLE,
184 SHORTCUTS_COL_PIXBUF_VISIBLE,
185 SHORTCUTS_COL_CANCELLABLE,
186 SHORTCUTS_COL_NUM_COLUMNS
191 SHORTCUT_TYPE_VOLUME,
192 SHORTCUT_TYPE_SEPARATOR,
193 SHORTCUT_TYPE_SEARCH,
197 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
198 "standard::is-hidden,standard::is-backup,standard::size," \
199 "standard::content-type,time::modified"
201 /* the first 3 must be these due to settings caching sort column */
206 MODEL_COL_NAME_COLLATED,
210 MODEL_COL_MTIME_TEXT,
212 MODEL_COL_NUM_COLUMNS
215 /* This list of types is passed to _gtk_file_system_model_new*() */
216 #define MODEL_COLUMN_TYPES \
217 MODEL_COL_NUM_COLUMNS, \
218 G_TYPE_STRING, /* MODEL_COL_NAME */ \
219 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
220 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
221 G_TYPE_FILE, /* MODEL_COL_FILE */ \
222 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
223 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
224 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
225 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
226 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
227 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
229 /* Identifiers for target types */
234 /* Interesting places in the shortcuts bar */
238 SHORTCUTS_RECENT_SEPARATOR,
243 SHORTCUTS_BOOKMARKS_SEPARATOR,
245 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
246 SHORTCUTS_CURRENT_FOLDER
249 /* Icon size for if we can't get it from the theme */
250 #define FALLBACK_ICON_SIZE 16
252 #define PREVIEW_HBOX_SPACING 12
256 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
257 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
258 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
259 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
260 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
261 #define SETTINGS_KEY_SORT_ORDER "sort-order"
262 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
263 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
265 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
266 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
268 static GObject* gtk_file_chooser_default_constructor (GType type,
269 guint n_construct_properties,
270 GObjectConstructParam *construct_params);
271 static void gtk_file_chooser_default_finalize (GObject *object);
272 static void gtk_file_chooser_default_set_property (GObject *object,
276 static void gtk_file_chooser_default_get_property (GObject *object,
280 static void gtk_file_chooser_default_dispose (GObject *object);
281 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
282 static void gtk_file_chooser_default_realize (GtkWidget *widget);
283 static void gtk_file_chooser_default_map (GtkWidget *widget);
284 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
285 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
286 GtkWidget *previous_toplevel);
287 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
288 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
289 GdkScreen *previous_screen);
290 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
291 GtkAllocation *allocation);
293 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
296 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
299 gboolean clear_entry,
301 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
302 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
304 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
307 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
309 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
310 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
311 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
312 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
313 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
314 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
315 GtkFileFilter *filter);
316 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
317 GtkFileFilter *filter);
318 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
319 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
322 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
325 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
327 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
329 gint *default_height);
330 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
331 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
333 static void add_selection_to_recent_list (GtkFileChooserDefault *impl);
335 static void location_popup_handler (GtkFileChooserDefault *impl,
337 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
338 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
339 static void up_folder_handler (GtkFileChooserDefault *impl);
340 static void down_folder_handler (GtkFileChooserDefault *impl);
341 static void home_folder_handler (GtkFileChooserDefault *impl);
342 static void desktop_folder_handler (GtkFileChooserDefault *impl);
343 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
344 gint bookmark_index);
345 static void show_hidden_handler (GtkFileChooserDefault *impl);
346 static void search_shortcut_handler (GtkFileChooserDefault *impl);
347 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
348 static void update_appearance (GtkFileChooserDefault *impl);
350 static void set_current_filter (GtkFileChooserDefault *impl,
351 GtkFileFilter *filter);
352 static void check_preview_change (GtkFileChooserDefault *impl);
354 static void filter_combo_changed (GtkComboBox *combo_box,
355 GtkFileChooserDefault *impl);
357 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
359 GtkFileChooserDefault *impl);
361 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
364 gboolean path_currently_selected,
366 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
368 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
370 static int shortcuts_get_index (GtkFileChooserDefault *impl,
371 ShortcutsIndex where);
372 static int shortcut_find_position (GtkFileChooserDefault *impl,
375 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
377 static gboolean list_select_func (GtkTreeSelection *selection,
380 gboolean path_currently_selected,
383 static void list_selection_changed (GtkTreeSelection *tree_selection,
384 GtkFileChooserDefault *impl);
385 static void list_row_activated (GtkTreeView *tree_view,
387 GtkTreeViewColumn *column,
388 GtkFileChooserDefault *impl);
390 static void path_bar_clicked (GtkPathBar *path_bar,
393 gboolean child_is_hidden,
394 GtkFileChooserDefault *impl);
396 static void add_bookmark_button_clicked_cb (GtkButton *button,
397 GtkFileChooserDefault *impl);
398 static void remove_bookmark_button_clicked_cb (GtkButton *button,
399 GtkFileChooserDefault *impl);
401 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
403 static void load_remove_timer (GtkFileChooserDefault *impl);
404 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
406 static void location_button_toggled_cb (GtkToggleButton *toggle,
407 GtkFileChooserDefault *impl);
408 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
410 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
411 gboolean remove_from_treeview);
413 static void search_setup_widgets (GtkFileChooserDefault *impl);
414 static void search_stop_searching (GtkFileChooserDefault *impl,
415 gboolean remove_query);
416 static void search_clear_model (GtkFileChooserDefault *impl,
417 gboolean remove_from_treeview);
418 static gboolean search_should_respond (GtkFileChooserDefault *impl);
419 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
420 static void search_entry_activate_cb (GtkEntry *entry,
422 static void settings_load (GtkFileChooserDefault *impl);
424 static void recent_start_loading (GtkFileChooserDefault *impl);
425 static void recent_stop_loading (GtkFileChooserDefault *impl);
426 static void recent_clear_model (GtkFileChooserDefault *impl,
427 gboolean remove_from_treeview);
428 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
429 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
430 static void set_file_system_backend (GtkFileChooserDefault *impl);
431 static void unset_file_system_backend (GtkFileChooserDefault *impl);
437 /* Drag and drop interface declarations */
440 GtkTreeModelFilter parent;
442 GtkFileChooserDefault *impl;
443 } ShortcutsPaneModelFilter;
446 GtkTreeModelFilterClass parent_class;
447 } ShortcutsPaneModelFilterClass;
449 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
450 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
452 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
454 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
455 _shortcuts_pane_model_filter,
456 GTK_TYPE_TREE_MODEL_FILTER,
457 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
458 shortcuts_pane_model_filter_drag_source_iface_init))
460 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
461 GtkTreeModel *child_model,
466 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
467 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
468 gtk_file_chooser_default_iface_init)
469 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
470 gtk_file_chooser_embed_default_iface_init));
474 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
476 GdkModifierType modifiers,
477 const gchar *signal_name)
479 gtk_binding_entry_add_signal (binding_set,
483 gtk_binding_entry_add_signal (binding_set,
484 keyval, modifiers | GDK_SHIFT_MASK,
489 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
491 static const guint quick_bookmark_keyvals[10] = {
492 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
494 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
495 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
496 GtkBindingSet *binding_set;
499 gobject_class->finalize = gtk_file_chooser_default_finalize;
500 gobject_class->constructor = gtk_file_chooser_default_constructor;
501 gobject_class->set_property = gtk_file_chooser_default_set_property;
502 gobject_class->get_property = gtk_file_chooser_default_get_property;
503 gobject_class->dispose = gtk_file_chooser_default_dispose;
505 widget_class->show_all = gtk_file_chooser_default_show_all;
506 widget_class->realize = gtk_file_chooser_default_realize;
507 widget_class->map = gtk_file_chooser_default_map;
508 widget_class->unmap = gtk_file_chooser_default_unmap;
509 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
510 widget_class->style_updated = gtk_file_chooser_default_style_updated;
511 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
512 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
514 signals[LOCATION_POPUP] =
515 g_signal_new_class_handler (I_("location-popup"),
516 G_OBJECT_CLASS_TYPE (class),
517 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
518 G_CALLBACK (location_popup_handler),
520 _gtk_marshal_VOID__STRING,
521 G_TYPE_NONE, 1, G_TYPE_STRING);
523 signals[LOCATION_POPUP_ON_PASTE] =
524 g_signal_new_class_handler (I_("location-popup-on-paste"),
525 G_OBJECT_CLASS_TYPE (class),
526 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
527 G_CALLBACK (location_popup_on_paste_handler),
529 _gtk_marshal_VOID__VOID,
532 signals[LOCATION_TOGGLE_POPUP] =
533 g_signal_new_class_handler (I_("location-toggle-popup"),
534 G_OBJECT_CLASS_TYPE (class),
535 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
536 G_CALLBACK (location_toggle_popup_handler),
538 _gtk_marshal_VOID__VOID,
542 g_signal_new_class_handler (I_("up-folder"),
543 G_OBJECT_CLASS_TYPE (class),
544 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
545 G_CALLBACK (up_folder_handler),
547 _gtk_marshal_VOID__VOID,
550 signals[DOWN_FOLDER] =
551 g_signal_new_class_handler (I_("down-folder"),
552 G_OBJECT_CLASS_TYPE (class),
553 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
554 G_CALLBACK (down_folder_handler),
556 _gtk_marshal_VOID__VOID,
559 signals[HOME_FOLDER] =
560 g_signal_new_class_handler (I_("home-folder"),
561 G_OBJECT_CLASS_TYPE (class),
562 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
563 G_CALLBACK (home_folder_handler),
565 _gtk_marshal_VOID__VOID,
568 signals[DESKTOP_FOLDER] =
569 g_signal_new_class_handler (I_("desktop-folder"),
570 G_OBJECT_CLASS_TYPE (class),
571 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
572 G_CALLBACK (desktop_folder_handler),
574 _gtk_marshal_VOID__VOID,
577 signals[QUICK_BOOKMARK] =
578 g_signal_new_class_handler (I_("quick-bookmark"),
579 G_OBJECT_CLASS_TYPE (class),
580 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
581 G_CALLBACK (quick_bookmark_handler),
583 _gtk_marshal_VOID__INT,
584 G_TYPE_NONE, 1, G_TYPE_INT);
586 signals[SHOW_HIDDEN] =
587 g_signal_new_class_handler (I_("show-hidden"),
588 G_OBJECT_CLASS_TYPE (class),
589 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
590 G_CALLBACK (show_hidden_handler),
592 _gtk_marshal_VOID__VOID,
595 signals[SEARCH_SHORTCUT] =
596 g_signal_new_class_handler (I_("search-shortcut"),
597 G_OBJECT_CLASS_TYPE (class),
598 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
599 G_CALLBACK (search_shortcut_handler),
601 _gtk_marshal_VOID__VOID,
604 signals[RECENT_SHORTCUT] =
605 g_signal_new_class_handler (I_("recent-shortcut"),
606 G_OBJECT_CLASS_TYPE (class),
607 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
608 G_CALLBACK (recent_shortcut_handler),
610 _gtk_marshal_VOID__VOID,
613 binding_set = gtk_binding_set_by_class (class);
615 gtk_binding_entry_add_signal (binding_set,
616 GDK_KEY_l, GDK_CONTROL_MASK,
617 "location-toggle-popup",
620 gtk_binding_entry_add_signal (binding_set,
623 1, G_TYPE_STRING, "/");
624 gtk_binding_entry_add_signal (binding_set,
625 GDK_KEY_KP_Divide, 0,
627 1, G_TYPE_STRING, "/");
630 gtk_binding_entry_add_signal (binding_set,
631 GDK_KEY_asciitilde, 0,
633 1, G_TYPE_STRING, "~");
636 gtk_binding_entry_add_signal (binding_set,
637 GDK_KEY_v, GDK_CONTROL_MASK,
638 "location-popup-on-paste",
640 gtk_binding_entry_add_signal (binding_set,
641 GDK_KEY_BackSpace, 0,
645 add_normal_and_shifted_binding (binding_set,
646 GDK_KEY_Up, GDK_MOD1_MASK,
649 add_normal_and_shifted_binding (binding_set,
650 GDK_KEY_KP_Up, GDK_MOD1_MASK,
653 add_normal_and_shifted_binding (binding_set,
654 GDK_KEY_Down, GDK_MOD1_MASK,
656 add_normal_and_shifted_binding (binding_set,
657 GDK_KEY_KP_Down, GDK_MOD1_MASK,
660 gtk_binding_entry_add_signal (binding_set,
661 GDK_KEY_Home, GDK_MOD1_MASK,
664 gtk_binding_entry_add_signal (binding_set,
665 GDK_KEY_KP_Home, GDK_MOD1_MASK,
668 gtk_binding_entry_add_signal (binding_set,
669 GDK_KEY_d, GDK_MOD1_MASK,
672 gtk_binding_entry_add_signal (binding_set,
673 GDK_KEY_h, GDK_CONTROL_MASK,
676 gtk_binding_entry_add_signal (binding_set,
677 GDK_KEY_s, GDK_MOD1_MASK,
680 gtk_binding_entry_add_signal (binding_set,
681 GDK_KEY_r, GDK_MOD1_MASK,
685 for (i = 0; i < 10; i++)
686 gtk_binding_entry_add_signal (binding_set,
687 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
691 _gtk_file_chooser_install_properties (gobject_class);
695 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
697 iface->select_file = gtk_file_chooser_default_select_file;
698 iface->unselect_file = gtk_file_chooser_default_unselect_file;
699 iface->select_all = gtk_file_chooser_default_select_all;
700 iface->unselect_all = gtk_file_chooser_default_unselect_all;
701 iface->get_files = gtk_file_chooser_default_get_files;
702 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
703 iface->get_file_system = gtk_file_chooser_default_get_file_system;
704 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
705 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
706 iface->set_current_name = gtk_file_chooser_default_set_current_name;
707 iface->add_filter = gtk_file_chooser_default_add_filter;
708 iface->remove_filter = gtk_file_chooser_default_remove_filter;
709 iface->list_filters = gtk_file_chooser_default_list_filters;
710 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
711 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
712 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
716 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
718 iface->get_default_size = gtk_file_chooser_default_get_default_size;
719 iface->should_respond = gtk_file_chooser_default_should_respond;
720 iface->initial_focus = gtk_file_chooser_default_initial_focus;
724 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
726 profile_start ("start", NULL);
727 #ifdef PROFILE_FILE_CHOOSER
728 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
730 impl->local_only = TRUE;
731 impl->preview_widget_active = TRUE;
732 impl->use_preview_label = TRUE;
733 impl->select_multiple = FALSE;
734 impl->show_hidden = FALSE;
735 impl->show_size_column = TRUE;
736 impl->icon_size = FALLBACK_ICON_SIZE;
737 impl->load_state = LOAD_EMPTY;
738 impl->reload_state = RELOAD_EMPTY;
739 impl->pending_select_files = NULL;
740 impl->location_mode = LOCATION_MODE_PATH_BAR;
741 impl->operation_mode = OPERATION_MODE_BROWSE;
742 impl->sort_column = MODEL_COL_NAME;
743 impl->sort_order = GTK_SORT_ASCENDING;
744 impl->recent_manager = gtk_recent_manager_get_default ();
745 impl->create_folders = TRUE;
747 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
748 GTK_ORIENTATION_VERTICAL);
749 gtk_box_set_spacing (GTK_BOX (impl), 12);
751 set_file_system_backend (impl);
753 profile_end ("end", NULL);
756 /* Frees the data columns for the specified iter in the shortcuts model*/
758 shortcuts_free_row_data (GtkFileChooserDefault *impl,
762 ShortcutType shortcut_type;
763 GCancellable *cancellable;
765 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
766 SHORTCUTS_COL_DATA, &col_data,
767 SHORTCUTS_COL_TYPE, &shortcut_type,
768 SHORTCUTS_COL_CANCELLABLE, &cancellable,
773 g_cancellable_cancel (cancellable);
774 g_object_unref (cancellable);
777 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
778 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
782 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
784 GtkFileSystemVolume *volume;
787 _gtk_file_system_volume_unref (volume);
789 if (shortcut_type == SHORTCUT_TYPE_FILE)
794 g_object_unref (file);
798 /* Frees all the data columns in the shortcuts model */
800 shortcuts_free (GtkFileChooserDefault *impl)
804 if (!impl->shortcuts_model)
807 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
810 shortcuts_free_row_data (impl, &iter);
812 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
814 g_object_unref (impl->shortcuts_model);
815 impl->shortcuts_model = NULL;
819 pending_select_files_free (GtkFileChooserDefault *impl)
821 g_slist_free_full (impl->pending_select_files, g_object_unref);
822 impl->pending_select_files = NULL;
826 pending_select_files_add (GtkFileChooserDefault *impl,
829 impl->pending_select_files =
830 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
834 gtk_file_chooser_default_finalize (GObject *object)
836 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
839 unset_file_system_backend (impl);
841 if (impl->shortcuts_pane_filter_model)
842 g_object_unref (impl->shortcuts_pane_filter_model);
844 shortcuts_free (impl);
846 g_free (impl->browse_files_last_selected_name);
848 for (l = impl->filters; l; l = l->next)
850 GtkFileFilter *filter;
852 filter = GTK_FILE_FILTER (l->data);
853 g_object_unref (filter);
855 g_slist_free (impl->filters);
857 if (impl->current_filter)
858 g_object_unref (impl->current_filter);
860 if (impl->current_volume_file)
861 g_object_unref (impl->current_volume_file);
863 if (impl->current_folder)
864 g_object_unref (impl->current_folder);
866 if (impl->preview_file)
867 g_object_unref (impl->preview_file);
869 if (impl->browse_path_bar_size_group)
870 g_object_unref (impl->browse_path_bar_size_group);
872 /* Free all the Models we have */
873 stop_loading_and_clear_list_model (impl, FALSE);
874 search_clear_model (impl, FALSE);
875 recent_clear_model (impl, FALSE);
877 /* stopping the load above should have cleared this */
878 g_assert (impl->load_timeout_id == 0);
880 g_free (impl->preview_display_name);
882 g_free (impl->edited_new_text);
884 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
887 /* Shows an error dialog set as transient for the specified window */
889 error_message_with_parent (GtkWindow *parent,
895 dialog = gtk_message_dialog_new (parent,
896 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
901 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
904 if (parent && gtk_window_has_group (parent))
905 gtk_window_group_add_window (gtk_window_get_group (parent),
906 GTK_WINDOW (dialog));
908 gtk_dialog_run (GTK_DIALOG (dialog));
909 gtk_widget_destroy (dialog);
912 /* Returns a toplevel GtkWindow, or NULL if none */
914 get_toplevel (GtkWidget *widget)
918 toplevel = gtk_widget_get_toplevel (widget);
919 if (!gtk_widget_is_toplevel (toplevel))
922 return GTK_WINDOW (toplevel);
925 /* Shows an error dialog for the file chooser */
927 error_message (GtkFileChooserDefault *impl,
931 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
934 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
936 error_dialog (GtkFileChooserDefault *impl,
947 uri = g_file_get_uri (file);
948 text = g_strdup_printf (msg, uri);
949 error_message (impl, text, error->message);
952 g_error_free (error);
956 /* Displays an error message about not being able to get information for a file.
957 * Frees the GError as well.
960 error_getting_info_dialog (GtkFileChooserDefault *impl,
965 _("Could not retrieve information about the file"),
969 /* Shows an error dialog about not being able to add a bookmark */
971 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
976 _("Could not add a bookmark"),
980 /* Shows an error dialog about not being able to remove a bookmark */
982 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
987 _("Could not remove bookmark"),
991 /* Shows an error dialog about not being able to create a folder */
993 error_creating_folder_dialog (GtkFileChooserDefault *impl,
998 _("The folder could not be created"),
1002 /* Shows an error about not being able to create a folder because a file with
1003 * the same name is already there.
1006 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1011 _("The folder could not be created, as a file with the same "
1012 "name already exists. Try using a different name for the "
1013 "folder, or rename the file first."),
1018 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1024 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1025 _("You need to choose a valid filename."));
1028 _("Cannot create a file under %s as it is not a folder"),
1029 parent_file, error);
1032 /* Shows an error about not being able to select a folder because a file with
1033 * the same name is already there.
1036 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1040 _("You may only select folders. The item that you selected is not a folder; "
1041 "try using a different item."),
1045 /* Shows an error dialog about not being able to create a filename */
1047 error_building_filename_dialog (GtkFileChooserDefault *impl,
1050 error_dialog (impl, _("Invalid file name"),
1054 /* Shows an error dialog when we cannot switch to a folder */
1056 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1060 error_dialog (impl, _("The folder contents could not be displayed"),
1064 /* Changes folders, displaying an error dialog if this fails */
1066 change_folder_and_display_error (GtkFileChooserDefault *impl,
1068 gboolean clear_entry)
1073 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1075 /* We copy the path because of this case:
1077 * list_row_activated()
1078 * fetches path from model; path belongs to the model (*)
1079 * calls change_folder_and_display_error()
1080 * calls gtk_file_chooser_set_current_folder_file()
1081 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1085 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1088 error_changing_folder_dialog (impl, file, error);
1094 emit_default_size_changed (GtkFileChooserDefault *impl)
1096 profile_msg (" emit default-size-changed start", NULL);
1097 g_signal_emit_by_name (impl, "default-size-changed");
1098 profile_msg (" emit default-size-changed end", NULL);
1102 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1104 if (impl->use_preview_label)
1106 if (!impl->preview_label)
1108 impl->preview_label = gtk_label_new (impl->preview_display_name);
1109 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1110 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1111 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1112 gtk_widget_show (impl->preview_label);
1117 if (impl->preview_label)
1119 gtk_widget_destroy (impl->preview_label);
1120 impl->preview_label = NULL;
1124 if (impl->preview_widget_active && impl->preview_widget)
1125 gtk_widget_show (impl->preview_box);
1127 gtk_widget_hide (impl->preview_box);
1129 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1130 emit_default_size_changed (impl);
1134 set_preview_widget (GtkFileChooserDefault *impl,
1135 GtkWidget *preview_widget)
1137 if (preview_widget == impl->preview_widget)
1140 if (impl->preview_widget)
1141 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1142 impl->preview_widget);
1144 impl->preview_widget = preview_widget;
1145 if (impl->preview_widget)
1147 gtk_widget_show (impl->preview_widget);
1148 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1149 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1150 impl->preview_widget,
1151 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1154 update_preview_widget_visibility (impl);
1157 /* Renders a "Search" icon at an appropriate size for a tree view */
1159 render_search_icon (GtkFileChooserDefault *impl)
1161 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1165 render_recent_icon (GtkFileChooserDefault *impl)
1167 GtkIconTheme *theme;
1170 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1171 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1173 theme = gtk_icon_theme_get_default ();
1175 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1181 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1187 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1188 struct ReloadIconsData
1190 GtkFileChooserDefault *impl;
1191 GtkTreeRowReference *row_ref;
1195 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1197 const GError *error,
1203 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1204 struct ReloadIconsData *data = user_data;
1206 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1209 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1211 if (cancelled || error)
1214 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1216 path = gtk_tree_row_reference_get_path (data->row_ref);
1219 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1220 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1221 SHORTCUTS_COL_PIXBUF, pixbuf,
1223 gtk_tree_path_free (path);
1227 g_object_unref (pixbuf);
1230 gtk_tree_row_reference_free (data->row_ref);
1231 g_object_unref (data->impl);
1234 g_object_unref (cancellable);
1238 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1243 profile_start ("start", NULL);
1245 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1248 for (l = impl->reload_icon_cancellables; l; l = l->next)
1250 GCancellable *cancellable = G_CANCELLABLE (l->data);
1251 g_cancellable_cancel (cancellable);
1253 g_slist_free (impl->reload_icon_cancellables);
1254 impl->reload_icon_cancellables = NULL;
1259 ShortcutType shortcut_type;
1260 gboolean pixbuf_visible;
1263 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1264 SHORTCUTS_COL_DATA, &data,
1265 SHORTCUTS_COL_TYPE, &shortcut_type,
1266 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1272 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1274 GtkFileSystemVolume *volume;
1277 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1278 impl->icon_size, NULL);
1280 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1282 if (g_file_is_native (G_FILE (data)))
1285 struct ReloadIconsData *info;
1286 GtkTreePath *tree_path;
1287 GCancellable *cancellable;
1291 info = g_new0 (struct ReloadIconsData, 1);
1292 info->impl = g_object_ref (impl);
1293 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1294 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1295 gtk_tree_path_free (tree_path);
1297 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1299 shortcuts_reload_icons_get_info_cb,
1301 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1305 GtkIconTheme *icon_theme;
1307 /* Don't call get_info for remote paths to avoid latency and
1309 * If we switch to a better bookmarks file format (XBEL), we
1310 * should use mime info to get a better icon.
1312 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1313 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1314 impl->icon_size, 0, NULL);
1317 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1319 pixbuf = render_search_icon (impl);
1321 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1323 pixbuf = render_recent_icon (impl);
1326 gtk_list_store_set (impl->shortcuts_model, &iter,
1327 SHORTCUTS_COL_PIXBUF, pixbuf,
1331 g_object_unref (pixbuf);
1335 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1339 profile_end ("end", NULL);
1343 shortcuts_find_folder (GtkFileChooserDefault *impl,
1346 GtkTreeSelection *selection;
1350 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1352 g_assert (folder != NULL);
1353 pos = shortcut_find_position (impl, folder);
1356 gtk_tree_selection_unselect_all (selection);
1360 path = gtk_tree_path_new_from_indices (pos, -1);
1361 gtk_tree_selection_select_path (selection, path);
1362 gtk_tree_path_free (path);
1365 /* If a shortcut corresponds to the current folder, selects it */
1367 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1369 shortcuts_find_folder (impl, impl->current_folder);
1372 /* Removes the specified number of rows from the shortcuts list */
1374 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1380 path = gtk_tree_path_new_from_indices (start_row, -1);
1382 for (; n_rows; n_rows--)
1386 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1387 g_assert_not_reached ();
1389 shortcuts_free_row_data (impl, &iter);
1390 gtk_list_store_remove (impl->shortcuts_model, &iter);
1393 gtk_tree_path_free (path);
1397 shortcuts_update_count (GtkFileChooserDefault *impl,
1398 ShortcutsIndex type,
1403 case SHORTCUTS_HOME:
1405 impl->has_home = FALSE;
1407 impl->has_home = TRUE;
1410 case SHORTCUTS_DESKTOP:
1412 impl->has_desktop = FALSE;
1414 impl->has_desktop = TRUE;
1417 case SHORTCUTS_VOLUMES:
1418 impl->num_volumes += value;
1421 case SHORTCUTS_SHORTCUTS:
1422 impl->num_shortcuts += value;
1425 case SHORTCUTS_BOOKMARKS:
1426 impl->num_bookmarks += value;
1429 case SHORTCUTS_CURRENT_FOLDER:
1431 impl->shortcuts_current_folder_active = FALSE;
1433 impl->shortcuts_current_folder_active = TRUE;
1442 struct ShortcutsInsertRequest
1444 GtkFileChooserDefault *impl;
1448 GtkTreeRowReference *row_ref;
1449 ShortcutsIndex type;
1455 get_file_info_finished (GCancellable *cancellable,
1457 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 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1474 gtk_tree_path_free (path);
1476 /* validate cancellable, else goto out */
1477 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1478 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1480 if (cancellable != model_cancellable)
1483 /* set the cancellable to NULL in the model (we unref later on) */
1484 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1485 SHORTCUTS_COL_CANCELLABLE, NULL,
1493 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1494 shortcuts_update_count (request->impl, request->type, -1);
1496 if (request->type == SHORTCUTS_HOME)
1500 home = g_file_new_for_path (g_get_home_dir ());
1501 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1502 g_object_unref (home);
1504 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1506 /* Remove the current folder separator */
1507 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1508 shortcuts_remove_rows (request->impl, separator_pos, 1);
1514 if (!request->label_copy)
1515 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1516 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1517 request->impl->icon_size);
1519 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1520 SHORTCUTS_COL_PIXBUF, pixbuf,
1521 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1522 SHORTCUTS_COL_NAME, request->label_copy,
1523 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1524 SHORTCUTS_COL_REMOVABLE, request->removable,
1527 if (request->impl->shortcuts_pane_filter_model)
1528 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1531 g_object_unref (pixbuf);
1534 g_object_unref (request->impl);
1535 g_object_unref (request->file);
1536 gtk_tree_row_reference_free (request->row_ref);
1537 g_free (request->label_copy);
1540 if (model_cancellable)
1541 g_object_unref (model_cancellable);
1544 /* FIXME: GtkFileSystem needs a function to split a remote path
1545 * into hostname and path components, or maybe just have a
1546 * gtk_file_system_path_get_display_name().
1548 * This function is also used in gtkfilechooserbutton.c
1551 _gtk_file_chooser_label_for_file (GFile *file)
1553 const gchar *path, *start, *end, *p;
1554 gchar *uri, *host, *label;
1556 uri = g_file_get_uri (file);
1558 start = strstr (uri, "://");
1562 path = strchr (start, '/');
1567 end = uri + strlen (uri);
1571 /* strip username */
1572 p = strchr (start, '@');
1576 p = strchr (start, ':');
1580 host = g_strndup (start, end - start);
1582 /* Translators: the first string is a path and the second string
1583 * is a hostname. Nautilus and the panel contain the same string
1586 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1592 label = g_strdup (uri);
1600 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1601 * inserts a volume. A position of -1 indicates the end of the tree.
1604 shortcuts_insert_file (GtkFileChooserDefault *impl,
1606 ShortcutType shortcut_type,
1607 GtkFileSystemVolume *volume,
1611 ShortcutsIndex type)
1614 GdkPixbuf *pixbuf = NULL;
1615 gpointer data = NULL;
1617 GtkIconTheme *icon_theme;
1619 profile_start ("start shortcut", NULL);
1621 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1624 label_copy = _gtk_file_system_volume_get_display_name (volume);
1625 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1626 impl->icon_size, NULL);
1628 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1630 if (g_file_is_native (file))
1632 struct ShortcutsInsertRequest *request;
1633 GCancellable *cancellable;
1636 request = g_new0 (struct ShortcutsInsertRequest, 1);
1637 request->impl = g_object_ref (impl);
1638 request->file = g_object_ref (file);
1639 request->name_only = TRUE;
1640 request->removable = removable;
1642 request->type = type;
1644 request->label_copy = g_strdup (label);
1647 gtk_list_store_append (impl->shortcuts_model, &iter);
1649 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1651 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1652 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1653 gtk_tree_path_free (p);
1655 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1656 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1657 get_file_info_finished, request);
1659 gtk_list_store_set (impl->shortcuts_model, &iter,
1660 SHORTCUTS_COL_DATA, g_object_ref (file),
1661 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1662 SHORTCUTS_COL_CANCELLABLE, cancellable,
1665 shortcuts_update_count (impl, type, 1);
1671 /* Don't call get_info for remote paths to avoid latency and
1674 data = g_object_ref (file);
1676 label_copy = g_strdup (label);
1678 label_copy = _gtk_file_chooser_label_for_file (file);
1680 /* If we switch to a better bookmarks file format (XBEL), we
1681 * should use mime info to get a better icon.
1683 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1684 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1685 impl->icon_size, 0, NULL);
1690 g_assert_not_reached ();
1696 gtk_list_store_append (impl->shortcuts_model, &iter);
1698 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1700 shortcuts_update_count (impl, type, 1);
1702 gtk_list_store_set (impl->shortcuts_model, &iter,
1703 SHORTCUTS_COL_PIXBUF, pixbuf,
1704 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1705 SHORTCUTS_COL_NAME, label_copy,
1706 SHORTCUTS_COL_DATA, data,
1707 SHORTCUTS_COL_TYPE, shortcut_type,
1708 SHORTCUTS_COL_REMOVABLE, removable,
1709 SHORTCUTS_COL_CANCELLABLE, NULL,
1712 if (impl->shortcuts_pane_filter_model)
1713 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1715 g_free (label_copy);
1718 g_object_unref (pixbuf);
1720 profile_end ("end", NULL);
1724 shortcuts_append_search (GtkFileChooserDefault *impl)
1729 pixbuf = render_search_icon (impl);
1731 gtk_list_store_append (impl->shortcuts_model, &iter);
1732 gtk_list_store_set (impl->shortcuts_model, &iter,
1733 SHORTCUTS_COL_PIXBUF, pixbuf,
1734 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1735 SHORTCUTS_COL_NAME, _("Search"),
1736 SHORTCUTS_COL_DATA, NULL,
1737 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1738 SHORTCUTS_COL_REMOVABLE, FALSE,
1742 g_object_unref (pixbuf);
1744 impl->has_search = TRUE;
1748 shortcuts_append_recent (GtkFileChooserDefault *impl)
1753 pixbuf = render_recent_icon (impl);
1755 gtk_list_store_append (impl->shortcuts_model, &iter);
1756 gtk_list_store_set (impl->shortcuts_model, &iter,
1757 SHORTCUTS_COL_PIXBUF, pixbuf,
1758 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1759 SHORTCUTS_COL_NAME, _("Recently Used"),
1760 SHORTCUTS_COL_DATA, NULL,
1761 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1762 SHORTCUTS_COL_REMOVABLE, FALSE,
1766 g_object_unref (pixbuf);
1769 /* Appends an item for the user's home directory to the shortcuts model */
1771 shortcuts_append_home (GtkFileChooserDefault *impl)
1773 const char *home_path;
1776 profile_start ("start", NULL);
1778 home_path = g_get_home_dir ();
1779 if (home_path == NULL)
1781 profile_end ("end - no home directory!?", NULL);
1785 home = g_file_new_for_path (home_path);
1786 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1787 impl->has_home = TRUE;
1789 g_object_unref (home);
1791 profile_end ("end", NULL);
1794 /* Appends the ~/Desktop directory to the shortcuts model */
1796 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1801 profile_start ("start", NULL);
1803 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1804 /* "To disable a directory, point it to the homedir."
1805 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1807 if (!g_strcmp0 (name, g_get_home_dir ()))
1809 profile_end ("end", NULL);
1813 file = g_file_new_for_path (name);
1814 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1815 impl->has_desktop = TRUE;
1817 /* We do not actually pop up an error dialog if there is no desktop directory
1818 * because some people may really not want to have one.
1821 g_object_unref (file);
1823 profile_end ("end", NULL);
1826 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1828 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1835 profile_start ("start", NULL);
1837 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1840 for (; bookmarks; bookmarks = bookmarks->next)
1844 file = bookmarks->data;
1846 if (impl->local_only && !g_file_is_native (file))
1849 if (shortcut_find_position (impl, file) != -1)
1852 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1854 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1860 profile_end ("end", NULL);
1862 return num_inserted;
1865 /* Returns the index for the corresponding item in the shortcuts bar */
1867 shortcuts_get_index (GtkFileChooserDefault *impl,
1868 ShortcutsIndex where)
1874 if (where == SHORTCUTS_SEARCH)
1877 n += impl->has_search ? 1 : 0;
1879 if (where == SHORTCUTS_RECENT)
1882 n += 1; /* we always have the recently-used item */
1884 if (where == SHORTCUTS_RECENT_SEPARATOR)
1887 n += 1; /* we always have the separator after the recently-used item */
1889 if (where == SHORTCUTS_HOME)
1892 n += impl->has_home ? 1 : 0;
1894 if (where == SHORTCUTS_DESKTOP)
1897 n += impl->has_desktop ? 1 : 0;
1899 if (where == SHORTCUTS_VOLUMES)
1902 n += impl->num_volumes;
1904 if (where == SHORTCUTS_SHORTCUTS)
1907 n += impl->num_shortcuts;
1909 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1912 /* If there are no bookmarks there won't be a separator */
1913 n += (impl->num_bookmarks > 0) ? 1 : 0;
1915 if (where == SHORTCUTS_BOOKMARKS)
1918 n += impl->num_bookmarks;
1920 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1925 if (where == SHORTCUTS_CURRENT_FOLDER)
1928 g_assert_not_reached ();
1935 /* Adds all the file system volumes to the shortcuts model */
1937 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1942 gboolean old_changing_folders;
1944 profile_start ("start", NULL);
1946 old_changing_folders = impl->changing_folder;
1947 impl->changing_folder = TRUE;
1949 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1950 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1951 impl->num_volumes = 0;
1953 list = _gtk_file_system_list_volumes (impl->file_system);
1957 for (l = list; l; l = l->next)
1959 GtkFileSystemVolume *volume;
1963 if (impl->local_only)
1965 if (_gtk_file_system_volume_is_mounted (volume))
1968 gboolean base_is_native = TRUE;
1970 base_file = _gtk_file_system_volume_get_root (volume);
1971 if (base_file != NULL)
1973 base_is_native = g_file_is_native (base_file);
1974 g_object_unref (base_file);
1977 if (!base_is_native)
1982 shortcuts_insert_file (impl,
1984 SHORTCUT_TYPE_VOLUME,
1985 _gtk_file_system_volume_ref (volume),
1993 impl->num_volumes = n;
1994 g_slist_free (list);
1996 if (impl->shortcuts_pane_filter_model)
1997 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1999 impl->changing_folder = old_changing_folders;
2001 profile_end ("end", NULL);
2004 /* Inserts a separator node in the shortcuts list */
2006 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2007 ShortcutsIndex where)
2011 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2012 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2013 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2015 gtk_list_store_insert (impl->shortcuts_model, &iter,
2016 shortcuts_get_index (impl, where));
2017 gtk_list_store_set (impl->shortcuts_model, &iter,
2018 SHORTCUTS_COL_PIXBUF, NULL,
2019 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2020 SHORTCUTS_COL_NAME, NULL,
2021 SHORTCUTS_COL_DATA, NULL,
2022 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2026 /* Updates the list of bookmarks */
2028 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2031 gboolean old_changing_folders;
2033 GFile *list_selected = NULL;
2034 ShortcutType shortcut_type;
2037 profile_start ("start", NULL);
2039 old_changing_folders = impl->changing_folder;
2040 impl->changing_folder = TRUE;
2042 if (shortcuts_get_selected (impl, &iter))
2044 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2046 SHORTCUTS_COL_DATA, &col_data,
2047 SHORTCUTS_COL_TYPE, &shortcut_type,
2050 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2051 list_selected = g_object_ref (col_data);
2054 if (impl->num_bookmarks > 0)
2055 shortcuts_remove_rows (impl,
2056 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2057 impl->num_bookmarks + 1);
2059 impl->num_bookmarks = 0;
2060 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2062 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2063 shortcuts_append_bookmarks (impl, bookmarks);
2064 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2065 g_slist_free (bookmarks);
2067 if (impl->num_bookmarks == 0)
2068 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2070 if (impl->shortcuts_pane_filter_model)
2071 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2075 shortcuts_find_folder (impl, list_selected);
2076 g_object_unref (list_selected);
2079 impl->changing_folder = old_changing_folders;
2081 profile_end ("end", NULL);
2084 /* Appends a separator and a row to the shortcuts list for the current folder */
2086 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2090 g_assert (!impl->shortcuts_current_folder_active);
2092 g_assert (impl->current_folder != NULL);
2094 pos = shortcut_find_position (impl, impl->current_folder);
2097 GtkFileSystemVolume *volume;
2101 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2104 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2106 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2108 base_file = _gtk_file_system_volume_get_root (volume);
2112 if (base_file && g_file_equal (base_file, impl->current_folder))
2113 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2115 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2118 g_object_unref (base_file);
2122 /* Updates the current folder row in the shortcuts model */
2124 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2128 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2130 if (impl->shortcuts_current_folder_active)
2132 shortcuts_remove_rows (impl, pos, 2);
2133 impl->shortcuts_current_folder_active = FALSE;
2136 shortcuts_add_current_folder (impl);
2139 /* Filter function used for the shortcuts filter model */
2141 shortcuts_pane_filter_cb (GtkTreeModel *model,
2145 GtkFileChooserDefault *impl;
2149 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2151 path = gtk_tree_model_get_path (model, iter);
2155 pos = *gtk_tree_path_get_indices (path);
2156 gtk_tree_path_free (path);
2158 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2161 /* Creates the list model for shortcuts */
2163 shortcuts_model_create (GtkFileChooserDefault *impl)
2165 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2166 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2167 GDK_TYPE_PIXBUF, /* pixbuf */
2168 G_TYPE_STRING, /* name */
2169 G_TYPE_POINTER, /* path or volume */
2170 G_TYPE_INT, /* ShortcutType */
2171 G_TYPE_BOOLEAN, /* removable */
2172 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2173 G_TYPE_POINTER); /* GCancellable */
2175 shortcuts_append_search (impl);
2177 if (impl->recent_manager)
2179 shortcuts_append_recent (impl);
2180 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2183 if (impl->file_system)
2185 shortcuts_append_home (impl);
2186 shortcuts_append_desktop (impl);
2187 shortcuts_add_volumes (impl);
2190 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2191 GTK_TREE_MODEL (impl->shortcuts_model),
2194 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2195 shortcuts_pane_filter_cb,
2200 /* Callback used when the "New Folder" button is clicked */
2202 new_folder_button_clicked (GtkButton *button,
2203 GtkFileChooserDefault *impl)
2208 if (!impl->browse_files_model)
2209 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2211 /* Prevent button from being clicked twice */
2212 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2214 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2216 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2217 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2218 path, impl->list_name_column,
2221 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2222 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2224 impl->list_name_column,
2227 gtk_tree_path_free (path);
2231 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2235 source = g_idle_source_new ();
2236 g_source_set_closure (source,
2237 g_cclosure_new_object (callback, G_OBJECT (impl)));
2238 g_source_attach (source, NULL);
2243 /* Idle handler for creating a new folder after editing its name cell, or for
2244 * canceling the editing.
2247 edited_idle_cb (GtkFileChooserDefault *impl)
2249 GDK_THREADS_ENTER ();
2251 g_source_destroy (impl->edited_idle);
2252 impl->edited_idle = NULL;
2254 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2255 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2257 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2259 if (impl->edited_new_text /* not cancelled? */
2260 && (strlen (impl->edited_new_text) != 0)
2261 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2263 GError *error = NULL;
2266 file = g_file_get_child_for_display_name (impl->current_folder,
2267 impl->edited_new_text,
2271 GError *error = NULL;
2273 if (g_file_make_directory (file, NULL, &error))
2274 change_folder_and_display_error (impl, file, FALSE);
2276 error_creating_folder_dialog (impl, file, error);
2278 g_object_unref (file);
2281 error_creating_folder_dialog (impl, file, error);
2283 g_free (impl->edited_new_text);
2284 impl->edited_new_text = NULL;
2287 GDK_THREADS_LEAVE ();
2293 queue_edited_idle (GtkFileChooserDefault *impl,
2294 const gchar *new_text)
2296 /* We create the folder in an idle handler so that we don't modify the tree
2300 if (!impl->edited_idle)
2301 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2303 g_free (impl->edited_new_text);
2304 impl->edited_new_text = g_strdup (new_text);
2307 /* Callback used from the text cell renderer when the new folder is named */
2309 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2311 const gchar *new_text,
2312 GtkFileChooserDefault *impl)
2314 /* work around bug #154921 */
2315 g_object_set (cell_renderer_text,
2316 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2317 queue_edited_idle (impl, new_text);
2320 /* Callback used from the text cell renderer when the new folder edition gets
2324 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2325 GtkFileChooserDefault *impl)
2327 /* work around bug #154921 */
2328 g_object_set (cell_renderer_text,
2329 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2330 queue_edited_idle (impl, NULL);
2333 /* Creates the widgets for the filter combo box */
2335 filter_create (GtkFileChooserDefault *impl)
2337 GtkCellRenderer *cell;
2340 impl->filter_combo = gtk_combo_box_text_new ();
2341 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2343 /* Get the combo's text renderer and set ellipsize parameters */
2344 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2348 g_object_set (G_OBJECT (cell),
2349 "ellipsize", PANGO_ELLIPSIZE_END,
2352 g_list_free (cells);
2354 g_signal_connect (impl->filter_combo, "changed",
2355 G_CALLBACK (filter_combo_changed), impl);
2357 gtk_widget_set_tooltip_text (impl->filter_combo,
2358 _("Select which types of files are shown"));
2360 return impl->filter_combo;
2364 toolbutton_new (GtkFileChooserDefault *impl,
2373 item = gtk_tool_button_new (NULL, NULL);
2374 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2375 gtk_widget_show (image);
2376 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2378 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2379 g_signal_connect (item, "clicked", callback, impl);
2382 gtk_widget_show (GTK_WIDGET (item));
2384 return GTK_WIDGET (item);
2387 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2389 shortcut_find_position (GtkFileChooserDefault *impl,
2394 int current_folder_separator_idx;
2396 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2399 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2402 /* FIXME: is this still needed? */
2403 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2407 for (i = 0; i < current_folder_separator_idx; i++)
2410 ShortcutType shortcut_type;
2412 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2413 SHORTCUTS_COL_DATA, &col_data,
2414 SHORTCUTS_COL_TYPE, &shortcut_type,
2419 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2421 GtkFileSystemVolume *volume;
2426 base_file = _gtk_file_system_volume_get_root (volume);
2428 exists = base_file && g_file_equal (file, base_file);
2431 g_object_unref (base_file);
2436 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2440 model_file = col_data;
2442 if (model_file && g_file_equal (model_file, file))
2447 if (i < current_folder_separator_idx - 1)
2449 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2450 g_assert_not_reached ();
2457 /* Tries to add a bookmark from a path name */
2459 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2465 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2467 if (shortcut_find_position (impl, file) != -1)
2471 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2473 error_adding_bookmark_dialog (impl, file, error);
2481 add_bookmark_foreach_cb (GtkTreeModel *model,
2486 GtkFileChooserDefault *impl;
2489 impl = (GtkFileChooserDefault *) data;
2491 gtk_tree_model_get (model, iter,
2492 MODEL_COL_FILE, &file,
2495 shortcuts_add_bookmark_from_file (impl, file, -1);
2497 g_object_unref (file);
2500 /* Adds a bookmark from the currently selected item in the file list */
2502 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2504 GtkTreeSelection *selection;
2506 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2508 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2509 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2511 gtk_tree_selection_selected_foreach (selection,
2512 add_bookmark_foreach_cb,
2516 /* Callback used when the "Add bookmark" button is clicked */
2518 add_bookmark_button_clicked_cb (GtkButton *button,
2519 GtkFileChooserDefault *impl)
2521 bookmarks_add_selected_folder (impl);
2524 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2525 * returns FALSE if no shortcut is selected.
2528 shortcuts_get_selected (GtkFileChooserDefault *impl,
2531 GtkTreeSelection *selection;
2532 GtkTreeIter parent_iter;
2534 if (!impl->browse_shortcuts_tree_view)
2537 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2539 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2542 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2548 /* Removes the selected bookmarks */
2550 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2558 if (!shortcuts_get_selected (impl, &iter))
2561 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2562 SHORTCUTS_COL_DATA, &col_data,
2563 SHORTCUTS_COL_REMOVABLE, &removable,
2569 g_assert (col_data != NULL);
2574 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2575 error_removing_bookmark_dialog (impl, file, error);
2578 /* Callback used when the "Remove bookmark" button is clicked */
2580 remove_bookmark_button_clicked_cb (GtkButton *button,
2581 GtkFileChooserDefault *impl)
2583 remove_selected_bookmarks (impl);
2586 struct selection_check_closure {
2587 GtkFileChooserDefault *impl;
2590 gboolean all_folders;
2593 /* Used from gtk_tree_selection_selected_foreach() */
2595 selection_check_foreach_cb (GtkTreeModel *model,
2600 struct selection_check_closure *closure;
2604 gtk_tree_model_get (model, iter,
2605 MODEL_COL_FILE, &file,
2606 MODEL_COL_IS_FOLDER, &is_folder,
2612 g_object_unref (file);
2615 closure->num_selected++;
2617 closure->all_folders = closure->all_folders && is_folder;
2618 closure->all_files = closure->all_files && !is_folder;
2621 /* Checks whether the selected items in the file list are all files or all folders */
2623 selection_check (GtkFileChooserDefault *impl,
2625 gboolean *all_files,
2626 gboolean *all_folders)
2628 struct selection_check_closure closure;
2629 GtkTreeSelection *selection;
2631 closure.impl = impl;
2632 closure.num_selected = 0;
2633 closure.all_files = TRUE;
2634 closure.all_folders = TRUE;
2636 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2637 gtk_tree_selection_selected_foreach (selection,
2638 selection_check_foreach_cb,
2641 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2644 *num_selected = closure.num_selected;
2647 *all_files = closure.all_files;
2650 *all_folders = closure.all_folders;
2653 struct get_selected_file_closure {
2654 GtkFileChooserDefault *impl;
2659 get_selected_file_foreach_cb (GtkTreeModel *model,
2664 struct get_selected_file_closure *closure = data;
2668 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2669 g_object_unref (closure->file);
2670 closure->file = NULL;
2673 gtk_tree_model_get (model, iter,
2674 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2678 /* Returns a selected path from the file list */
2680 get_selected_file (GtkFileChooserDefault *impl)
2682 struct get_selected_file_closure closure;
2683 GtkTreeSelection *selection;
2685 closure.impl = impl;
2686 closure.file = NULL;
2688 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2689 gtk_tree_selection_selected_foreach (selection,
2690 get_selected_file_foreach_cb,
2693 return closure.file;
2697 GtkFileChooserDefault *impl;
2699 } UpdateTooltipData;
2702 update_tooltip (GtkTreeModel *model,
2707 UpdateTooltipData *udata = data;
2709 if (udata->tip == NULL)
2711 gchar *display_name;
2713 gtk_tree_model_get (model, iter,
2714 MODEL_COL_NAME, &display_name,
2717 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2719 g_free (display_name);
2724 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2725 * if there are no selected items *and* the current folder is not in the
2726 * bookmarks list. De-sensitize the button otherwise.
2729 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2732 gboolean all_folders;
2736 selection_check (impl, &num_selected, NULL, &all_folders);
2738 if (num_selected == 0)
2739 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2740 else if (num_selected == 1)
2744 file = get_selected_file (impl);
2745 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2747 g_object_unref (file);
2750 active = all_folders;
2752 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2754 if (impl->browse_files_popup_menu_add_shortcut_item)
2755 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2756 (num_selected == 0) ? FALSE : active);
2760 if (num_selected == 0)
2761 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2762 else if (num_selected > 1)
2763 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2766 GtkTreeSelection *selection;
2767 UpdateTooltipData data;
2769 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2772 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2776 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2781 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2782 * bookmark row is selected in the shortcuts tree.
2785 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2788 gboolean removable = FALSE;
2792 if (shortcuts_get_selected (impl, &iter))
2794 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2795 SHORTCUTS_COL_REMOVABLE, &removable,
2796 SHORTCUTS_COL_NAME, &name,
2798 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2801 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2803 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2805 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2809 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2810 _("Remove the selected bookmark"));
2815 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2818 gboolean removable = FALSE;
2820 if (impl->browse_shortcuts_popup_menu == NULL)
2823 if (shortcuts_get_selected (impl, &iter))
2824 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2825 SHORTCUTS_COL_REMOVABLE, &removable,
2828 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2829 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2832 /* GtkWidget::drag-begin handler for the shortcuts list. */
2834 shortcuts_drag_begin_cb (GtkWidget *widget,
2835 GdkDragContext *context,
2836 GtkFileChooserDefault *impl)
2839 impl->shortcuts_drag_context = g_object_ref (context);
2844 /* Removes the idle handler for outside drags */
2846 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2848 if (!impl->shortcuts_drag_outside_idle)
2851 g_source_destroy (impl->shortcuts_drag_outside_idle);
2852 impl->shortcuts_drag_outside_idle = NULL;
2856 /* GtkWidget::drag-end handler for the shortcuts list. */
2858 shortcuts_drag_end_cb (GtkWidget *widget,
2859 GdkDragContext *context,
2860 GtkFileChooserDefault *impl)
2863 g_object_unref (impl->shortcuts_drag_context);
2865 shortcuts_cancel_drag_outside_idle (impl);
2867 if (!impl->shortcuts_drag_outside)
2870 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2872 impl->shortcuts_drag_outside = FALSE;
2876 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2878 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2879 GdkDragContext *context,
2880 GtkFileChooserDefault *impl)
2882 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2885 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2889 shortcuts_drag_leave_cb (GtkWidget *widget,
2890 GdkDragContext *context,
2892 GtkFileChooserDefault *impl)
2895 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2897 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2901 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2903 GTK_TREE_VIEW_DROP_BEFORE);
2905 g_signal_stop_emission_by_name (widget, "drag-leave");
2908 /* Computes the appropriate row and position for dropping */
2910 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2914 GtkTreeViewDropPosition *pos)
2916 GtkTreeView *tree_view;
2917 GtkTreeViewColumn *column;
2921 int bookmarks_index;
2922 int header_height = 0;
2924 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2926 if (gtk_tree_view_get_headers_visible (tree_view))
2927 header_height = _gtk_tree_view_get_header_height (tree_view);
2929 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2931 if (!gtk_tree_view_get_path_at_pos (tree_view,
2939 row = bookmarks_index + impl->num_bookmarks - 1;
2940 *path = gtk_tree_path_new_from_indices (row, -1);
2941 *pos = GTK_TREE_VIEW_DROP_AFTER;
2945 row = *gtk_tree_path_get_indices (*path);
2946 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2947 gtk_tree_path_free (*path);
2949 if (row < bookmarks_index)
2951 row = bookmarks_index;
2952 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2954 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2956 row = bookmarks_index + impl->num_bookmarks - 1;
2957 *pos = GTK_TREE_VIEW_DROP_AFTER;
2961 if (cell_y < cell.height / 2)
2962 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2964 *pos = GTK_TREE_VIEW_DROP_AFTER;
2967 *path = gtk_tree_path_new_from_indices (row, -1);
2970 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2971 * implement the destination side of DnD by hand, due to limitations in
2972 * GtkTreeView's DnD API.
2975 shortcuts_drag_motion_cb (GtkWidget *widget,
2976 GdkDragContext *context,
2980 GtkFileChooserDefault *impl)
2983 GtkTreeViewDropPosition pos;
2984 GdkDragAction action;
2987 if (gtk_drag_get_source_widget (context) == widget)
2989 shortcuts_cancel_drag_outside_idle (impl);
2991 if (impl->shortcuts_drag_outside)
2993 shortcuts_drag_set_delete_cursor (impl, FALSE);
2994 impl->shortcuts_drag_outside = FALSE;
2999 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3000 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3001 action = GDK_ACTION_COPY;
3002 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3003 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3004 action = GDK_ACTION_MOVE;
3011 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3012 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3013 gtk_tree_path_free (path);
3017 g_signal_stop_emission_by_name (widget, "drag-motion");
3021 gdk_drag_status (context, action, time_);
3028 /* GtkWidget::drag-drop handler for the shortcuts list. */
3030 shortcuts_drag_drop_cb (GtkWidget *widget,
3031 GdkDragContext *context,
3035 GtkFileChooserDefault *impl)
3038 shortcuts_cancel_drag_outside_idle (impl);
3041 g_signal_stop_emission_by_name (widget, "drag-drop");
3045 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3047 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3048 GtkSelectionData *selection_data,
3054 uris = gtk_selection_data_get_uris (selection_data);
3058 for (i = 0; uris[i]; i++)
3064 file = g_file_new_for_uri (uri);
3066 if (shortcuts_add_bookmark_from_file (impl, file, position))
3069 g_object_unref (file);
3075 /* Reorders the selected bookmark to the specified position */
3077 shortcuts_reorder (GtkFileChooserDefault *impl,
3082 ShortcutType shortcut_type;
3085 int bookmarks_index;
3090 /* Get the selected path */
3092 if (!shortcuts_get_selected (impl, &iter))
3093 g_assert_not_reached ();
3095 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3096 old_position = *gtk_tree_path_get_indices (path);
3097 gtk_tree_path_free (path);
3099 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3100 old_position -= bookmarks_index;
3101 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3103 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3104 SHORTCUTS_COL_NAME, &name,
3105 SHORTCUTS_COL_DATA, &col_data,
3106 SHORTCUTS_COL_TYPE, &shortcut_type,
3108 g_assert (col_data != NULL);
3109 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3112 g_object_ref (file); /* removal below will free file, so we need a new ref */
3114 /* Remove the path from the old position and insert it in the new one */
3116 if (new_position > old_position)
3119 if (old_position == new_position)
3123 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3125 shortcuts_add_bookmark_from_file (impl, file, new_position);
3126 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3129 error_adding_bookmark_dialog (impl, file, error);
3133 g_object_unref (file);
3137 /* Callback used when we get the drag data for the bookmarks list. We add the
3138 * received URIs as bookmarks if they are folders.
3141 shortcuts_drag_data_received_cb (GtkWidget *widget,
3142 GdkDragContext *context,
3145 GtkSelectionData *selection_data,
3150 GtkFileChooserDefault *impl;
3151 GtkTreePath *tree_path;
3152 GtkTreeViewDropPosition tree_pos;
3155 int bookmarks_index;
3157 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3159 /* Compute position */
3161 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3163 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3164 position = *gtk_tree_path_get_indices (tree_path);
3165 gtk_tree_path_free (tree_path);
3167 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3170 g_assert (position >= bookmarks_index);
3171 position -= bookmarks_index;
3173 target = gtk_selection_data_get_target (selection_data);
3175 if (gtk_targets_include_uri (&target, 1))
3176 shortcuts_drop_uris (impl, selection_data, position);
3177 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3178 shortcuts_reorder (impl, position);
3180 g_signal_stop_emission_by_name (widget, "drag-data-received");
3183 /* Callback used to display a tooltip in the shortcuts tree */
3185 shortcuts_query_tooltip_cb (GtkWidget *widget,
3188 gboolean keyboard_mode,
3189 GtkTooltip *tooltip,
3190 GtkFileChooserDefault *impl)
3192 GtkTreeModel *model;
3195 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3203 ShortcutType shortcut_type;
3205 gtk_tree_model_get (model, &iter,
3206 SHORTCUTS_COL_DATA, &col_data,
3207 SHORTCUTS_COL_TYPE, &shortcut_type,
3210 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3212 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3214 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3219 file = G_FILE (col_data);
3220 parse_name = g_file_get_parse_name (file);
3222 gtk_tooltip_set_text (tooltip, parse_name);
3224 g_free (parse_name);
3228 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3232 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3242 /* Callback used when the selection in the shortcuts tree changes */
3244 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3245 GtkFileChooserDefault *impl)
3248 GtkTreeIter child_iter;
3250 bookmarks_check_remove_sensitivity (impl);
3251 shortcuts_check_popup_sensitivity (impl);
3253 if (impl->changing_folder)
3256 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3258 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3261 shortcuts_activate_iter (impl, &child_iter);
3266 shortcuts_row_separator_func (GtkTreeModel *model,
3270 ShortcutType shortcut_type;
3272 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3274 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3278 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3280 GtkFileChooserDefault *impl)
3284 /* don't screw up focus switching with Tab */
3285 if (event->keyval == GDK_KEY_Tab
3286 || event->keyval == GDK_KEY_KP_Tab
3287 || event->keyval == GDK_KEY_ISO_Left_Tab
3288 || event->length < 1)
3291 if (impl->location_entry)
3292 entry = impl->location_entry;
3293 else if (impl->search_entry)
3294 entry = impl->search_entry;
3300 gtk_widget_grab_focus (entry);
3301 return gtk_widget_event (entry, (GdkEvent *) event);
3307 /* Callback used when the file list's popup menu is detached */
3309 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3312 GtkFileChooserDefault *impl;
3314 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3315 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3317 impl->browse_shortcuts_popup_menu = NULL;
3318 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3319 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3323 remove_shortcut_cb (GtkMenuItem *item,
3324 GtkFileChooserDefault *impl)
3326 remove_selected_bookmarks (impl);
3329 /* Rename the selected bookmark */
3331 rename_selected_bookmark (GtkFileChooserDefault *impl)
3335 GtkTreeViewColumn *column;
3336 GtkCellRenderer *cell;
3339 if (shortcuts_get_selected (impl, &iter))
3341 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3342 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3343 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3344 cell = g_list_nth_data (renderers, 1);
3345 g_list_free (renderers);
3346 g_object_set (cell, "editable", TRUE, NULL);
3347 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3348 path, column, cell, TRUE);
3349 gtk_tree_path_free (path);
3354 rename_shortcut_cb (GtkMenuItem *item,
3355 GtkFileChooserDefault *impl)
3357 rename_selected_bookmark (impl);
3360 /* Constructs the popup menu for the file list if needed */
3362 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3366 if (impl->browse_shortcuts_popup_menu)
3369 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3370 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3371 impl->browse_shortcuts_tree_view,
3372 shortcuts_popup_menu_detach_cb);
3374 item = gtk_image_menu_item_new_with_label (_("Remove"));
3375 impl->browse_shortcuts_popup_menu_remove_item = item;
3376 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3377 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3378 g_signal_connect (item, "activate",
3379 G_CALLBACK (remove_shortcut_cb), impl);
3380 gtk_widget_show (item);
3381 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3383 item = gtk_menu_item_new_with_label (_("Rename..."));
3384 impl->browse_shortcuts_popup_menu_rename_item = item;
3385 g_signal_connect (item, "activate",
3386 G_CALLBACK (rename_shortcut_cb), impl);
3387 gtk_widget_show (item);
3388 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3392 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3394 shortcuts_build_popup_menu (impl);
3395 shortcuts_check_popup_sensitivity (impl);
3399 popup_position_func (GtkMenu *menu,
3403 gpointer user_data);
3406 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3407 GdkEventButton *event)
3409 shortcuts_update_popup_menu (impl);
3411 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3412 NULL, NULL, NULL, NULL,
3413 event->button, event->time);
3416 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3418 popup_position_func, impl->browse_shortcuts_tree_view,
3419 0, GDK_CURRENT_TIME);
3420 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3425 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3427 shortcuts_popup_menu_cb (GtkWidget *widget,
3428 GtkFileChooserDefault *impl)
3430 shortcuts_popup_menu (impl, NULL);
3434 /* Callback used when a button is pressed on the shortcuts list.
3435 * We trap button 3 to bring up a popup menu.
3438 shortcuts_button_press_event_cb (GtkWidget *widget,
3439 GdkEventButton *event,
3440 GtkFileChooserDefault *impl)
3442 static gboolean in_press = FALSE;
3448 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
3452 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3458 shortcuts_popup_menu (impl, event);
3463 shortcuts_edited (GtkCellRenderer *cell,
3466 GtkFileChooserDefault *impl)
3472 g_object_set (cell, "editable", FALSE, NULL);
3474 path = gtk_tree_path_new_from_string (path_string);
3475 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3476 g_assert_not_reached ();
3478 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3479 SHORTCUTS_COL_DATA, &shortcut,
3481 gtk_tree_path_free (path);
3483 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3487 shortcuts_editing_canceled (GtkCellRenderer *cell,
3488 GtkFileChooserDefault *impl)
3490 g_object_set (cell, "editable", FALSE, NULL);
3493 /* Creates the widgets for the shortcuts and bookmarks tree */
3495 shortcuts_list_create (GtkFileChooserDefault *impl)
3498 GtkTreeSelection *selection;
3499 GtkTreeViewColumn *column;
3500 GtkCellRenderer *renderer;
3502 /* Target types for dragging a row to/from the shortcuts list */
3503 const GtkTargetEntry tree_model_row_targets[] = {
3504 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3507 /* Scrolled window */
3509 swin = gtk_scrolled_window_new (NULL, NULL);
3510 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3511 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3512 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3514 gtk_widget_show (swin);
3517 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3518 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3519 GTK_STYLE_CLASS_SIDEBAR);
3520 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3521 #ifdef PROFILE_FILE_CHOOSER
3522 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3525 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3527 * 1. user brings up a SAVE dialog
3528 * 2. user clicks on a shortcut in the shortcuts pane
3529 * 3. user starts typing a filename
3531 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3532 * support interactive search. However, we'd rather focus the location entry
3533 * so that the user can type *there*.
3535 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3536 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3537 * if the user starts typing while the focus is in the shortcuts pane.
3539 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3540 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3542 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3543 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3544 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3545 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3546 /* Accessible object name for the file chooser's shortcuts pane */
3547 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3549 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3551 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3553 tree_model_row_targets,
3554 G_N_ELEMENTS (tree_model_row_targets),
3557 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3558 GTK_DEST_DEFAULT_ALL,
3559 tree_model_row_targets,
3560 G_N_ELEMENTS (tree_model_row_targets),
3561 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3562 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3564 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3565 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3566 gtk_tree_selection_set_select_function (selection,
3567 shortcuts_select_func,
3570 g_signal_connect (selection, "changed",
3571 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3573 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3574 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3576 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3577 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3578 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3579 G_CALLBACK (shortcuts_drag_end_cb), impl);
3580 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3581 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3583 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3584 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3585 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3586 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3587 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3588 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3589 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3590 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3592 /* Support tooltips */
3593 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3594 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3595 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3597 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3598 gtk_widget_show (impl->browse_shortcuts_tree_view);
3602 column = gtk_tree_view_column_new ();
3603 /* Column header for the file chooser's shortcuts pane */
3604 gtk_tree_view_column_set_title (column, _("_Places"));
3606 renderer = gtk_cell_renderer_pixbuf_new ();
3607 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3608 gtk_tree_view_column_set_attributes (column, renderer,
3609 "pixbuf", SHORTCUTS_COL_PIXBUF,
3610 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3613 renderer = gtk_cell_renderer_text_new ();
3614 g_object_set (renderer,
3616 "ellipsize", PANGO_ELLIPSIZE_END,
3618 g_signal_connect (renderer, "edited",
3619 G_CALLBACK (shortcuts_edited), impl);
3620 g_signal_connect (renderer, "editing-canceled",
3621 G_CALLBACK (shortcuts_editing_canceled), impl);
3622 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3623 gtk_tree_view_column_set_attributes (column, renderer,
3624 "text", SHORTCUTS_COL_NAME,
3627 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3628 shortcuts_row_separator_func,
3631 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3636 /* Creates the widgets for the shortcuts/bookmarks pane */
3638 shortcuts_pane_create (GtkFileChooserDefault *impl,
3639 GtkSizeGroup *size_group)
3644 GtkStyleContext *context;
3647 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3648 gtk_widget_show (vbox);
3650 /* Shortcuts tree */
3652 widget = shortcuts_list_create (impl);
3654 gtk_size_group_add_widget (size_group, widget);
3655 context = gtk_widget_get_style_context (widget);
3656 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3658 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3660 /* Box for buttons */
3662 toolbar = gtk_toolbar_new ();
3663 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
3664 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3666 context = gtk_widget_get_style_context (toolbar);
3667 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3668 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3670 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3671 gtk_widget_show (toolbar);
3673 /* Add bookmark button */
3674 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3675 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3679 G_CALLBACK (add_bookmark_button_clicked_cb));
3680 g_object_unref (icon);
3682 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3683 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3684 _("Add the selected folder to the Bookmarks"));
3686 /* Remove bookmark button */
3687 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3688 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3692 G_CALLBACK (remove_bookmark_button_clicked_cb));
3693 g_object_unref (icon);
3694 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3695 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3696 _("Remove the selected bookmark"));
3702 key_is_left_or_right (GdkEventKey *event)
3706 modifiers = gtk_accelerator_get_default_mod_mask ();
3708 return ((event->keyval == GDK_KEY_Right
3709 || event->keyval == GDK_KEY_KP_Right
3710 || event->keyval == GDK_KEY_Left
3711 || event->keyval == GDK_KEY_KP_Left)
3712 && (event->state & modifiers) == 0);
3715 /* Handles key press events on the file list, so that we can trap Enter to
3716 * activate the default button on our own. Also, checks to see if '/' has been
3720 browse_files_key_press_event_cb (GtkWidget *widget,
3724 GtkFileChooserDefault *impl;
3725 GdkModifierType no_text_input_mask;
3727 impl = (GtkFileChooserDefault *) data;
3729 no_text_input_mask =
3730 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
3732 if ((event->keyval == GDK_KEY_slash
3733 || event->keyval == GDK_KEY_KP_Divide
3735 || event->keyval == GDK_KEY_asciitilde
3737 ) && !(event->state & no_text_input_mask))
3739 location_popup_handler (impl, event->string);
3743 if (key_is_left_or_right (event))
3745 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3749 if ((event->keyval == GDK_KEY_Return
3750 || event->keyval == GDK_KEY_ISO_Enter
3751 || event->keyval == GDK_KEY_KP_Enter
3752 || event->keyval == GDK_KEY_space
3753 || event->keyval == GDK_KEY_KP_Space)
3754 && !(event->state & gtk_accelerator_get_default_mod_mask ())
3755 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3756 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3760 window = get_toplevel (widget);
3763 GtkWidget *default_widget, *focus_widget;
3765 default_widget = gtk_window_get_default_widget (window);
3766 focus_widget = gtk_window_get_focus (window);
3768 if (widget != default_widget &&
3769 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3771 gtk_window_activate_default (window);
3781 /* Callback used when the file list's popup menu is detached */
3783 popup_menu_detach_cb (GtkWidget *attach_widget,
3786 GtkFileChooserDefault *impl;
3788 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3789 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3791 impl->browse_files_popup_menu = NULL;
3792 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3793 impl->browse_files_popup_menu_hidden_files_item = NULL;
3794 impl->browse_files_popup_menu_copy_file_location_item = NULL;
3797 /* Callback used when the "Add to Bookmarks" menu item is activated */
3799 add_to_shortcuts_cb (GtkMenuItem *item,
3800 GtkFileChooserDefault *impl)
3802 bookmarks_add_selected_folder (impl);
3805 /* callback used to set data to clipboard */
3807 copy_file_get_cb (GtkClipboard *clipboard,
3808 GtkSelectionData *selection_data,
3812 GSList *selected_files = data;
3816 gint num_files = g_slist_length (selected_files);
3821 uris = g_new (gchar *, num_files + 1);
3822 uris[num_files] = NULL; /* null terminator */
3826 for (l = selected_files; l; l = l->next)
3828 GFile *file = (GFile *) l->data;
3830 if (info == SELECTION_URI)
3831 uris[i] = g_file_get_uri (file);
3832 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3833 uris[i] = g_file_get_parse_name (file);
3838 if (info == SELECTION_URI)
3839 gtk_selection_data_set_uris (selection_data, uris);
3840 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3842 char *str = g_strjoinv (" ", uris);
3843 gtk_selection_data_set_text (selection_data, str, -1);
3851 /* callback used to clear the clipboard data */
3853 copy_file_clear_cb (GtkClipboard *clipboard,
3856 GSList *selected_files = data;
3858 g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3859 g_slist_free (selected_files);
3862 /* Callback used when the "Copy file's location" menu item is activated */
3864 copy_file_location_cb (GtkMenuItem *item,
3865 GtkFileChooserDefault *impl)
3867 GSList *selected_files = NULL;
3869 selected_files = search_get_selected_files (impl);
3873 GtkClipboard *clipboard;
3874 GtkTargetList *target_list;
3875 GtkTargetEntry *targets;
3878 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3880 target_list = gtk_target_list_new (NULL, 0);
3881 gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3882 gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3884 targets = gtk_target_table_new_from_list (target_list, &n_targets);
3885 gtk_target_list_unref (target_list);
3887 gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3892 gtk_target_table_free (targets, n_targets);
3896 /* Callback used when the "Visit this file" menu item is activated */
3898 visit_file_cb (GtkMenuItem *item,
3899 GtkFileChooserDefault *impl)
3903 files = search_get_selected_files (impl);
3905 /* Sigh, just use the first one */
3908 GFile *file = files->data;
3910 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL); /* NULL-GError */
3913 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
3914 g_slist_free (files);
3917 /* callback used when the "Show Hidden Files" menu item is toggled */
3919 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3920 GtkFileChooserDefault *impl)
3923 "show-hidden", gtk_check_menu_item_get_active (item),
3927 /* Callback used when the "Show Size Column" menu item is toggled */
3929 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3930 GtkFileChooserDefault *impl)
3932 impl->show_size_column = gtk_check_menu_item_get_active (item);
3934 gtk_tree_view_column_set_visible (impl->list_size_column,
3935 impl->show_size_column);
3938 /* Shows an error dialog about not being able to select a dragged file */
3940 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3945 _("Could not select file"),
3950 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3955 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3957 for (i = 1; uris[i]; i++)
3960 GError *error = NULL;
3963 file = g_file_new_for_uri (uri);
3965 gtk_file_chooser_default_select_file (chooser, file, &error);
3967 error_selecting_dragged_file_dialog (impl, file, error);
3969 g_object_unref (file);
3973 struct FileListDragData
3975 GtkFileChooserDefault *impl;
3981 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3983 const GError *error,
3986 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3987 struct FileListDragData *data = user_data;
3988 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3990 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3993 data->impl->file_list_drag_data_received_cancellable = NULL;
3995 if (cancelled || error)
3998 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3999 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4000 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4001 change_folder_and_display_error (data->impl, data->file, FALSE);
4004 GError *error = NULL;
4006 gtk_file_chooser_default_unselect_all (chooser);
4007 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4009 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4011 browse_files_center_selected_row (data->impl);
4014 if (data->impl->select_multiple)
4015 file_list_drag_data_select_uris (data->impl, data->uris);
4018 g_object_unref (data->impl);
4019 g_strfreev (data->uris);
4020 g_object_unref (data->file);
4023 g_object_unref (cancellable);
4027 file_list_drag_data_received_cb (GtkWidget *widget,
4028 GdkDragContext *context,
4031 GtkSelectionData *selection_data,
4036 GtkFileChooserDefault *impl;
4041 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4043 /* Allow only drags from other widgets; see bug #533891. */
4044 if (gtk_drag_get_source_widget (context) == widget)
4046 g_signal_stop_emission_by_name (widget, "drag-data-received");
4050 /* Parse the text/uri-list string, navigate to the first one */
4051 uris = gtk_selection_data_get_uris (selection_data);
4052 if (uris && uris[0])
4054 struct FileListDragData *data;
4057 file = g_file_new_for_uri (uri);
4059 data = g_new0 (struct FileListDragData, 1);
4060 data->impl = g_object_ref (impl);
4064 if (impl->file_list_drag_data_received_cancellable)
4065 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4067 impl->file_list_drag_data_received_cancellable =
4068 _gtk_file_system_get_info (impl->file_system, file,
4070 file_list_drag_data_received_get_info_cb,
4074 g_signal_stop_emission_by_name (widget, "drag-data-received");
4077 /* Don't do anything with the drag_drop signal */
4079 file_list_drag_drop_cb (GtkWidget *widget,
4080 GdkDragContext *context,
4084 GtkFileChooserDefault *impl)
4086 g_signal_stop_emission_by_name (widget, "drag-drop");
4090 /* Disable the normal tree drag motion handler, it makes it look like you're
4091 dropping the dragged item onto a tree item */
4093 file_list_drag_motion_cb (GtkWidget *widget,
4094 GdkDragContext *context,
4098 GtkFileChooserDefault *impl)
4100 g_signal_stop_emission_by_name (widget, "drag-motion");
4104 /* Sensitizes the "Copy file's location" context menu item if there is actually
4105 * a selection active.
4108 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4110 GtkTreeSelection *selection;
4113 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4114 if (gtk_tree_selection_count_selected_rows (selection) == 0)
4119 if (impl->browse_files_popup_menu_copy_file_location_item)
4120 gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4124 file_list_add_image_menu_item (GtkFileChooserDefault *impl,
4125 const char *stock_name,
4126 const char *mnemonic_label,
4131 item = gtk_image_menu_item_new_with_mnemonic (mnemonic_label);
4132 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4133 gtk_image_new_from_stock (stock_name, GTK_ICON_SIZE_MENU));
4134 g_signal_connect (item, "activate", callback, impl);
4135 gtk_widget_show (item);
4136 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4142 file_list_add_check_menu_item (GtkFileChooserDefault *impl,
4143 const char *mnemonic_label,
4148 item = gtk_check_menu_item_new_with_mnemonic (mnemonic_label);
4149 g_signal_connect (item, "toggled", callback, impl);
4150 gtk_widget_show (item);
4151 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4156 /* Constructs the popup menu for the file list if needed */
4158 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4162 if (impl->browse_files_popup_menu)
4165 impl->browse_files_popup_menu = gtk_menu_new ();
4166 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4167 impl->browse_files_tree_view,
4168 popup_menu_detach_cb);
4170 impl->browse_files_popup_menu_visit_file_item = file_list_add_image_menu_item (impl, GTK_STOCK_DIRECTORY, _("_Visit this file"),
4171 G_CALLBACK (visit_file_cb));
4173 impl->browse_files_popup_menu_copy_file_location_item = file_list_add_image_menu_item (impl, GTK_STOCK_COPY, _("_Copy file's location"),
4174 G_CALLBACK (copy_file_location_cb));
4176 impl->browse_files_popup_menu_add_shortcut_item = file_list_add_image_menu_item (impl, GTK_STOCK_ADD, _("_Add to Bookmarks"),
4177 G_CALLBACK (add_to_shortcuts_cb));
4179 item = gtk_separator_menu_item_new ();
4180 gtk_widget_show (item);
4181 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4183 impl->browse_files_popup_menu_hidden_files_item = file_list_add_check_menu_item (impl, _("Show _Hidden Files"),
4184 G_CALLBACK (show_hidden_toggled_cb));
4186 impl->browse_files_popup_menu_size_column_item = file_list_add_check_menu_item (impl, _("Show _Size Column"),
4187 G_CALLBACK (show_size_column_toggled_cb));
4189 bookmarks_check_add_sensitivity (impl);
4190 check_copy_file_location_sensitivity (impl);
4193 /* Updates the popup menu for the file list, creating it if necessary */
4195 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4197 file_list_build_popup_menu (impl);
4199 /* The sensitivity of the Add to Bookmarks item is set in
4200 * bookmarks_check_add_sensitivity()
4203 /* 'Visit this file' */
4204 gtk_widget_set_visible (impl->browse_files_popup_menu_visit_file_item, (impl->operation_mode != OPERATION_MODE_BROWSE));
4206 /* 'Show Hidden Files' */
4207 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4208 G_CALLBACK (show_hidden_toggled_cb), impl);
4209 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4211 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4212 G_CALLBACK (show_hidden_toggled_cb), impl);
4214 /* 'Show Size Column' */
4215 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4216 G_CALLBACK (show_size_column_toggled_cb), impl);
4217 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4218 impl->show_size_column);
4219 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4220 G_CALLBACK (show_size_column_toggled_cb), impl);
4224 popup_position_func (GtkMenu *menu,
4230 GtkAllocation allocation;
4231 GtkWidget *widget = GTK_WIDGET (user_data);
4232 GdkScreen *screen = gtk_widget_get_screen (widget);
4235 GdkRectangle monitor;
4237 g_return_if_fail (gtk_widget_get_realized (widget));
4239 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4241 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4244 gtk_widget_get_allocation (widget, &allocation);
4245 *x += (allocation.width - req.width) / 2;
4246 *y += (allocation.height - req.height) / 2;
4248 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4249 gtk_menu_set_monitor (menu, monitor_num);
4250 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
4252 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4253 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4259 file_list_popup_menu (GtkFileChooserDefault *impl,
4260 GdkEventButton *event)
4262 file_list_update_popup_menu (impl);
4264 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4265 NULL, NULL, NULL, NULL,
4266 event->button, event->time);
4269 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4271 popup_position_func, impl->browse_files_tree_view,
4272 0, GDK_CURRENT_TIME);
4273 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4279 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4281 list_popup_menu_cb (GtkWidget *widget,
4282 GtkFileChooserDefault *impl)
4284 file_list_popup_menu (impl, NULL);
4288 /* Callback used when a button is pressed on the file list. We trap button 3 to
4289 * bring up a popup menu.
4292 list_button_press_event_cb (GtkWidget *widget,
4293 GdkEventButton *event,
4294 GtkFileChooserDefault *impl)
4296 static gboolean in_press = FALSE;
4301 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
4305 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4308 file_list_popup_menu (impl, event);
4313 OperationMode operation_mode;
4314 gint general_column;
4318 /* Sets the sort column IDs for the file list; needs to be done whenever we
4319 * change the model on the treeview.
4322 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4324 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4325 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4326 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4330 file_list_query_tooltip_cb (GtkWidget *widget,
4333 gboolean keyboard_tip,
4334 GtkTooltip *tooltip,
4337 GtkFileChooserDefault *impl = user_data;
4338 GtkTreeModel *model;
4344 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4348 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4351 &model, &path, &iter))
4354 gtk_tree_model_get (model, &iter,
4355 MODEL_COL_FILE, &file,
4360 gtk_tree_path_free (path);
4364 filename = g_file_get_path (file);
4365 gtk_tooltip_set_text (tooltip, filename);
4366 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4371 g_object_unref (file);
4372 gtk_tree_path_free (path);
4378 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4382 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4383 gtk_cell_renderer_set_fixed_size (renderer,
4384 xpad * 2 + impl->icon_size,
4385 ypad * 2 + impl->icon_size);
4388 /* Creates the widgets for the file list */
4390 create_file_list (GtkFileChooserDefault *impl)
4393 GtkTreeSelection *selection;
4394 GtkTreeViewColumn *column;
4395 GtkCellRenderer *renderer;
4397 /* Scrolled window */
4398 swin = gtk_scrolled_window_new (NULL, NULL);
4399 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4400 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4401 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4404 /* Tree/list view */
4406 impl->browse_files_tree_view = gtk_tree_view_new ();
4407 #ifdef PROFILE_FILE_CHOOSER
4408 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4410 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4411 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4413 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4414 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4416 gtk_drag_dest_set (impl->browse_files_tree_view,
4417 GTK_DEST_DEFAULT_ALL,
4419 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4420 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4422 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4423 G_CALLBACK (list_row_activated), impl);
4424 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4425 G_CALLBACK (browse_files_key_press_event_cb), impl);
4426 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4427 G_CALLBACK (list_popup_menu_cb), impl);
4428 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4429 G_CALLBACK (list_button_press_event_cb), impl);
4431 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4432 G_CALLBACK (file_list_drag_data_received_cb), impl);
4433 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4434 G_CALLBACK (file_list_drag_drop_cb), impl);
4435 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4436 G_CALLBACK (file_list_drag_motion_cb), impl);
4438 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4439 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4440 G_CALLBACK (file_list_query_tooltip_cb), impl);
4442 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4443 gtk_tree_selection_set_select_function (selection,
4446 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4449 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4450 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4452 g_signal_connect (selection, "changed",
4453 G_CALLBACK (list_selection_changed), impl);
4455 /* Keep the column order in sync with update_cell_renderer_attributes() */
4457 /* Filename column */
4459 impl->list_name_column = gtk_tree_view_column_new ();
4460 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4461 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4462 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4464 renderer = gtk_cell_renderer_pixbuf_new ();
4465 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4466 set_icon_cell_renderer_fixed_size (impl, renderer);
4467 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4469 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4470 g_object_set (impl->list_name_renderer,
4471 "ellipsize", PANGO_ELLIPSIZE_END,
4473 g_signal_connect (impl->list_name_renderer, "edited",
4474 G_CALLBACK (renderer_edited_cb), impl);
4475 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4476 G_CALLBACK (renderer_editing_canceled_cb), impl);
4477 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4479 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4483 column = gtk_tree_view_column_new ();
4484 gtk_tree_view_column_set_resizable (column, TRUE);
4485 gtk_tree_view_column_set_title (column, _("Size"));
4487 renderer = gtk_cell_renderer_text_new ();
4488 g_object_set (renderer,
4489 "alignment", PANGO_ALIGN_RIGHT,
4491 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4492 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4493 impl->list_size_column = column;
4495 /* Modification time column */
4497 column = gtk_tree_view_column_new ();
4498 gtk_tree_view_column_set_resizable (column, TRUE);
4499 gtk_tree_view_column_set_title (column, _("Modified"));
4501 renderer = gtk_cell_renderer_text_new ();
4502 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4503 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4504 impl->list_mtime_column = column;
4506 file_list_set_sort_column_ids (impl);
4507 update_cell_renderer_attributes (impl);
4509 gtk_widget_show_all (swin);
4514 /* Creates the widgets for the files/folders pane */
4516 file_pane_create (GtkFileChooserDefault *impl,
4517 GtkSizeGroup *size_group)
4523 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4524 gtk_widget_show (vbox);
4526 /* Box for lists and preview */
4528 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4529 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4530 gtk_widget_show (hbox);
4534 widget = create_file_list (impl);
4535 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4536 gtk_size_group_add_widget (size_group, widget);
4540 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4541 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4542 /* Don't show preview box initially */
4546 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4548 widget = filter_create (impl);
4550 gtk_widget_show (widget);
4551 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4553 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4559 location_entry_create (GtkFileChooserDefault *impl)
4561 if (!impl->location_entry)
4562 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4564 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4565 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4566 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4567 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4570 /* Creates the widgets specific to Save mode */
4572 save_widgets_create (GtkFileChooserDefault *impl)
4577 if (impl->save_widgets != NULL)
4580 location_switch_to_path_bar (impl);
4582 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4584 impl->save_widgets_table = gtk_grid_new ();
4585 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4586 gtk_widget_show (impl->save_widgets_table);
4587 gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4588 gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4592 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4593 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4594 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4595 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4596 gtk_widget_show (widget);
4598 /* Location entry */
4600 location_entry_create (impl);
4601 gtk_widget_set_hexpand (impl->location_entry, TRUE);
4602 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4603 gtk_widget_show (impl->location_entry);
4604 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4607 impl->save_folder_label = gtk_label_new (NULL);
4608 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4609 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4610 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4611 gtk_widget_show (impl->save_folder_label);
4613 impl->save_widgets = vbox;
4614 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4615 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4616 gtk_widget_show (impl->save_widgets);
4619 /* Destroys the widgets specific to Save mode */
4621 save_widgets_destroy (GtkFileChooserDefault *impl)
4623 if (impl->save_widgets == NULL)
4626 gtk_widget_destroy (impl->save_widgets);
4627 impl->save_widgets = NULL;
4628 impl->save_widgets_table = NULL;
4629 impl->location_entry = NULL;
4630 impl->save_folder_label = NULL;
4633 /* Turns on the path bar widget. Can be called even if we are already in that
4637 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4639 if (impl->location_entry)
4641 gtk_widget_destroy (impl->location_entry);
4642 impl->location_entry = NULL;
4645 gtk_widget_hide (impl->location_entry_box);
4648 /* Turns on the location entry. Can be called even if we are already in that
4652 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4654 /* when in search or recent files mode, we are not showing the
4655 * location_entry_box container, so there's no point in switching
4658 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4659 impl->operation_mode == OPERATION_MODE_RECENT)
4664 gtk_widget_show (impl->location_entry_box);
4668 if (!impl->location_entry)
4670 location_entry_create (impl);
4671 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4672 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4675 /* Configure the entry */
4677 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4681 gtk_widget_show (impl->location_entry);
4682 gtk_widget_grab_focus (impl->location_entry);
4685 /* Sets a new location mode. set_buttons determines whether the toggle button
4686 * for the mode will also be changed.
4689 location_mode_set (GtkFileChooserDefault *impl,
4690 LocationMode new_mode,
4691 gboolean set_button)
4693 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4694 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4696 GtkWindow *toplevel;
4697 GtkWidget *current_focus;
4698 gboolean button_active;
4699 gboolean switch_to_file_list;
4703 case LOCATION_MODE_PATH_BAR:
4704 button_active = FALSE;
4706 /* The location_entry will disappear when we switch to path bar mode. So,
4707 * we'll focus the file list in that case, to avoid having a window with
4708 * no focused widget.
4710 toplevel = get_toplevel (GTK_WIDGET (impl));
4711 switch_to_file_list = FALSE;
4714 current_focus = gtk_window_get_focus (toplevel);
4715 if (!current_focus || current_focus == impl->location_entry)
4716 switch_to_file_list = TRUE;
4719 location_switch_to_path_bar (impl);
4721 if (switch_to_file_list)
4722 gtk_widget_grab_focus (impl->browse_files_tree_view);
4726 case LOCATION_MODE_FILENAME_ENTRY:
4727 button_active = TRUE;
4728 location_switch_to_filename_entry (impl);
4732 g_assert_not_reached ();
4738 g_signal_handlers_block_by_func (impl->location_button,
4739 G_CALLBACK (location_button_toggled_cb), impl);
4741 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4743 g_signal_handlers_unblock_by_func (impl->location_button,
4744 G_CALLBACK (location_button_toggled_cb), impl);
4748 impl->location_mode = new_mode;
4752 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4754 /* when in search or recent files mode, we are not showing the
4755 * location_entry_box container, so there's no point in switching
4758 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4759 impl->operation_mode == OPERATION_MODE_RECENT)
4762 /* If the file entry is not visible, show it.
4763 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4765 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4767 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4769 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4771 if (gtk_widget_has_focus (impl->location_entry))
4773 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4777 gtk_widget_grab_focus (impl->location_entry);
4782 /* Callback used when one of the location mode buttons is toggled */
4784 location_button_toggled_cb (GtkToggleButton *toggle,
4785 GtkFileChooserDefault *impl)
4788 LocationMode new_mode;
4790 is_active = gtk_toggle_button_get_active (toggle);
4794 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4795 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4799 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4800 new_mode = LOCATION_MODE_PATH_BAR;
4803 location_mode_set (impl, new_mode, FALSE);
4806 /* Creates a toggle button for the location entry. */
4808 location_button_create (GtkFileChooserDefault *impl)
4813 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4814 gtk_widget_show (image);
4816 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4820 g_signal_connect (impl->location_button, "toggled",
4821 G_CALLBACK (location_button_toggled_cb), impl);
4823 str = _("Type a file name");
4825 gtk_widget_set_tooltip_text (impl->location_button, str);
4826 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4830 PATH_BAR_FOLDER_PATH,
4831 PATH_BAR_SELECT_A_FOLDER,
4832 PATH_BAR_ERROR_NO_FILENAME,
4833 PATH_BAR_ERROR_NO_FOLDER,
4834 PATH_BAR_RECENTLY_USED,
4838 /* Creates the info bar for informational messages or warnings, with its icon and label */
4840 info_bar_create (GtkFileChooserDefault *impl)
4842 GtkWidget *content_area;
4844 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4845 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4846 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4848 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4850 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4851 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4853 gtk_widget_show (impl->browse_select_a_folder_icon);
4854 gtk_widget_show (impl->browse_select_a_folder_label);
4857 /* Sets the info bar to show the appropriate informational or warning message */
4859 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4863 GtkMessageType message_type;
4869 case PATH_BAR_SELECT_A_FOLDER:
4870 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4872 message_type = GTK_MESSAGE_OTHER;
4875 case PATH_BAR_ERROR_NO_FILENAME:
4876 str = _("Please type a file name");
4877 message_type = GTK_MESSAGE_WARNING;
4880 case PATH_BAR_ERROR_NO_FOLDER:
4881 str = _("Please select a folder below");
4882 message_type = GTK_MESSAGE_WARNING;
4886 g_assert_not_reached ();
4890 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4891 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4892 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4893 GTK_ICON_SIZE_MENU);
4894 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4900 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4902 special_mode_widgets_create (GtkFileChooserDefault *impl)
4904 impl->browse_special_mode_icon = gtk_image_new ();
4905 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4906 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4908 impl->browse_special_mode_label = gtk_label_new (NULL);
4909 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4910 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4913 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4915 path_bar_widgets_create (GtkFileChooserDefault *impl)
4917 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4918 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4919 gtk_widget_show (impl->browse_path_bar_hbox);
4921 /* Size group that allows the path bar to be the same size between modes */
4922 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4923 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4925 /* Location button */
4926 location_button_create (impl);
4927 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4928 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4931 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4932 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4933 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4935 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4936 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4939 info_bar_create (impl);
4940 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4941 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4943 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4944 special_mode_widgets_create (impl);
4947 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4948 g_signal_connect (impl->browse_new_folder_button, "clicked",
4949 G_CALLBACK (new_folder_button_clicked), impl);
4950 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4951 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4954 /* Sets the path bar's mode to show a label, the actual folder path, or a
4955 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4956 * if the pathbar is already showing the widgets you expect; otherwise, call
4957 * path_bar_update() instead to set the appropriate widgets automatically.
4960 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4962 gboolean path_bar_visible = FALSE;
4963 gboolean special_mode_widgets_visible = FALSE;
4964 gboolean info_bar_visible = FALSE;
4965 gboolean create_folder_visible = FALSE;
4971 case PATH_BAR_FOLDER_PATH:
4972 path_bar_visible = TRUE;
4975 case PATH_BAR_SELECT_A_FOLDER:
4976 case PATH_BAR_ERROR_NO_FILENAME:
4977 case PATH_BAR_ERROR_NO_FOLDER:
4978 info_bar_set (impl, mode);
4979 info_bar_visible = TRUE;
4982 case PATH_BAR_RECENTLY_USED:
4983 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4985 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4986 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4989 special_mode_widgets_visible = TRUE;
4992 case PATH_BAR_SEARCH:
4993 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
4995 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
4996 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4999 special_mode_widgets_visible = TRUE;
5003 g_assert_not_reached ();
5006 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
5007 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
5008 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
5009 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
5011 if (path_bar_visible)
5013 if (impl->create_folders
5014 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
5015 && impl->operation_mode != OPERATION_MODE_RECENT)
5016 create_folder_visible = TRUE;
5019 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
5022 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5024 browse_widgets_create (GtkFileChooserDefault *impl)
5028 GtkSizeGroup *size_group;
5030 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5031 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5032 gtk_widget_show (impl->browse_widgets_box);
5034 impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5035 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5036 gtk_widget_show (impl->browse_header_box);
5038 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5039 path_bar_widgets_create (impl);
5041 /* Box for the location label and entry */
5043 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5044 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5046 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5047 gtk_widget_show (impl->location_label);
5048 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5050 /* size group is used by the scrolled windows of the panes */
5051 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5055 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5056 gtk_widget_show (hpaned);
5057 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5059 widget = shortcuts_pane_create (impl, size_group);
5060 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5061 widget = file_pane_create (impl, size_group);
5062 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5063 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5064 g_object_unref (size_group);
5068 gtk_file_chooser_default_constructor (GType type,
5069 guint n_construct_properties,
5070 GObjectConstructParam *construct_params)
5072 GtkFileChooserDefault *impl;
5075 profile_start ("start", NULL);
5077 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5078 n_construct_properties,
5080 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5082 g_assert (impl->file_system);
5084 gtk_widget_push_composite_child ();
5086 /* Shortcuts model */
5087 shortcuts_model_create (impl);
5089 /* The browse widgets */
5090 browse_widgets_create (impl);
5092 /* Alignment to hold extra widget */
5093 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5094 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5095 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5096 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5098 gtk_widget_pop_composite_child ();
5099 update_appearance (impl);
5101 profile_end ("end", NULL);
5106 /* Sets the extra_widget by packing it in the appropriate place */
5108 set_extra_widget (GtkFileChooserDefault *impl,
5109 GtkWidget *extra_widget)
5113 g_object_ref (extra_widget);
5114 /* FIXME: is this right ? */
5115 gtk_widget_show (extra_widget);
5118 if (impl->extra_widget)
5120 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5121 g_object_unref (impl->extra_widget);
5124 impl->extra_widget = extra_widget;
5125 if (impl->extra_widget)
5127 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5128 gtk_widget_show (impl->extra_align);
5131 gtk_widget_hide (impl->extra_align);
5135 set_local_only (GtkFileChooserDefault *impl,
5136 gboolean local_only)
5138 if (local_only != impl->local_only)
5140 impl->local_only = local_only;
5142 if (impl->location_entry)
5143 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5145 if (impl->shortcuts_model && impl->file_system)
5147 shortcuts_add_volumes (impl);
5148 shortcuts_add_bookmarks (impl);
5151 if (local_only && impl->current_folder &&
5152 !g_file_is_native (impl->current_folder))
5154 /* If we are pointing to a non-local folder, make an effort to change
5155 * back to a local folder, but it's really up to the app to not cause
5156 * such a situation, so we ignore errors.
5158 const gchar *home = g_get_home_dir ();
5164 home_file = g_file_new_for_path (home);
5166 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5168 g_object_unref (home_file);
5174 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5175 GtkFileChooserDefault *impl)
5177 shortcuts_add_volumes (impl);
5178 shortcuts_add_bookmarks (impl);
5180 bookmarks_check_add_sensitivity (impl);
5181 bookmarks_check_remove_sensitivity (impl);
5182 shortcuts_check_popup_sensitivity (impl);
5185 /* Sets the file chooser to multiple selection mode */
5187 set_select_multiple (GtkFileChooserDefault *impl,
5188 gboolean select_multiple,
5189 gboolean property_notify)
5191 GtkTreeSelection *selection;
5192 GtkSelectionMode mode;
5194 if (select_multiple == impl->select_multiple)
5197 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
5199 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5200 gtk_tree_selection_set_mode (selection, mode);
5202 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5204 impl->select_multiple = select_multiple;
5205 g_object_notify (G_OBJECT (impl), "select-multiple");
5207 check_preview_change (impl);
5211 set_file_system_backend (GtkFileChooserDefault *impl)
5213 profile_start ("start for backend", "default");
5215 impl->file_system = _gtk_file_system_new ();
5217 g_signal_connect (impl->file_system, "volumes-changed",
5218 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5219 g_signal_connect (impl->file_system, "bookmarks-changed",
5220 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5222 profile_end ("end", NULL);
5226 unset_file_system_backend (GtkFileChooserDefault *impl)
5228 g_signal_handlers_disconnect_by_func (impl->file_system,
5229 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5231 g_object_unref (impl->file_system);
5233 impl->file_system = NULL;
5236 /* Saves the widgets around the pathbar so they can be reparented later
5237 * in the correct place. This function must be called paired with
5238 * restore_path_bar().
5241 save_path_bar (GtkFileChooserDefault *impl)
5245 g_object_ref (impl->browse_path_bar_hbox);
5247 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5249 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5252 /* Reparents the path bar and the "Create folder" button to the right place:
5253 * Above the file list in Open mode, or to the right of the "Save in folder:"
5254 * label in Save mode. The save_path_bar() function must be called before this
5258 restore_path_bar (GtkFileChooserDefault *impl)
5260 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5261 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5263 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5264 gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5266 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5267 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5269 gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5270 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5271 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5274 g_assert_not_reached ();
5276 g_object_unref (impl->browse_path_bar_hbox);
5279 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5281 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5285 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5286 MODEL_COL_FILE, &file,
5288 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE);
5289 g_object_unref (file);
5292 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5293 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5294 * function will update all the pathbar's widgets.
5297 path_bar_update (GtkFileChooserDefault *impl)
5301 switch (impl->operation_mode)
5303 case OPERATION_MODE_BROWSE:
5304 mode = PATH_BAR_FOLDER_PATH;
5307 case OPERATION_MODE_RECENT:
5308 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5310 GtkTreeSelection *selection;
5311 gboolean have_selected;
5314 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5316 /* Save mode means single-selection mode, so the following is valid */
5317 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5321 mode = PATH_BAR_FOLDER_PATH;
5322 put_recent_folder_in_pathbar (impl, &iter);
5325 mode = PATH_BAR_SELECT_A_FOLDER;
5328 mode = PATH_BAR_RECENTLY_USED;
5332 case OPERATION_MODE_SEARCH:
5333 mode = PATH_BAR_SEARCH;
5337 g_assert_not_reached ();
5341 path_bar_set_mode (impl, mode);
5345 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5347 if (impl->search_hbox)
5349 gtk_widget_destroy (impl->search_hbox);
5351 impl->search_hbox = NULL;
5352 impl->search_entry = NULL;
5356 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5358 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5362 case OPERATION_MODE_BROWSE:
5363 stop_loading_and_clear_list_model (impl, TRUE);
5366 case OPERATION_MODE_SEARCH:
5367 search_stop_searching (impl, FALSE);
5368 search_clear_model (impl, TRUE);
5370 operation_mode_discard_search_widgets (impl);
5373 case OPERATION_MODE_RECENT:
5374 recent_stop_loading (impl);
5375 recent_clear_model (impl, TRUE);
5379 g_assert_not_reached ();
5384 operation_mode_set_browse (GtkFileChooserDefault *impl)
5386 path_bar_update (impl);
5388 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5389 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5391 gtk_widget_show (impl->location_button);
5392 location_mode_set (impl, impl->location_mode, TRUE);
5394 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5395 gtk_widget_show (impl->location_entry_box);
5400 operation_mode_set_search (GtkFileChooserDefault *impl)
5402 g_assert (impl->search_hbox == NULL);
5403 g_assert (impl->search_entry == NULL);
5404 g_assert (impl->search_model == NULL);
5406 search_setup_widgets (impl);
5410 operation_mode_set_recent (GtkFileChooserDefault *impl)
5412 path_bar_update (impl);
5414 /* Hide the location widgets temporarily */
5415 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5416 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5418 gtk_widget_hide (impl->location_button);
5419 gtk_widget_hide (impl->location_entry_box);
5422 recent_start_loading (impl);
5425 /* Sometimes we need to frob the selection in the shortcuts list manually */
5427 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5429 GtkTreeSelection *selection;
5432 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5434 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5436 path = gtk_tree_path_new_from_indices (pos, -1);
5437 gtk_tree_selection_select_path (selection, path);
5438 gtk_tree_path_free (path);
5440 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5444 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5446 ShortcutsIndex shortcut_to_select;
5448 operation_mode_stop (impl, impl->operation_mode);
5450 impl->operation_mode = mode;
5452 switch (impl->operation_mode)
5454 case OPERATION_MODE_BROWSE:
5455 operation_mode_set_browse (impl);
5456 shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5459 case OPERATION_MODE_SEARCH:
5460 operation_mode_set_search (impl);
5461 shortcut_to_select = SHORTCUTS_SEARCH;
5464 case OPERATION_MODE_RECENT:
5465 operation_mode_set_recent (impl);
5466 shortcut_to_select = SHORTCUTS_RECENT;
5470 g_assert_not_reached ();
5474 if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5475 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5478 /* This function is basically a do_all function.
5480 * It sets the visibility on all the widgets based on the current state, and
5481 * moves the custom_widget if needed.
5484 update_appearance (GtkFileChooserDefault *impl)
5486 save_path_bar (impl);
5488 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5489 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5493 gtk_widget_hide (impl->location_button);
5494 save_widgets_create (impl);
5496 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5497 text = _("Save in _folder:");
5499 text = _("Create in _folder:");
5501 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5503 if (impl->select_multiple)
5505 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5506 "Re-setting to single selection mode.");
5507 set_select_multiple (impl, FALSE, TRUE);
5510 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5511 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5513 gtk_widget_show (impl->location_button);
5514 save_widgets_destroy (impl);
5515 location_mode_set (impl, impl->location_mode, TRUE);
5518 if (impl->location_entry)
5519 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5521 restore_path_bar (impl);
5522 path_bar_update (impl);
5524 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5525 * of files may change depending whether we are in a file or folder-only mode.
5527 gtk_widget_queue_draw (impl->browse_files_tree_view);
5529 emit_default_size_changed (impl);
5533 gtk_file_chooser_default_set_property (GObject *object,
5535 const GValue *value,
5539 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5543 case GTK_FILE_CHOOSER_PROP_ACTION:
5545 GtkFileChooserAction action = g_value_get_enum (value);
5547 if (action != impl->action)
5549 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5551 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5552 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5553 && impl->select_multiple)
5555 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5556 "this is not allowed in multiple selection mode. Resetting the file chooser "
5557 "to single selection mode.");
5558 set_select_multiple (impl, FALSE, TRUE);
5560 impl->action = action;
5561 update_cell_renderer_attributes (impl);
5562 update_appearance (impl);
5563 settings_load (impl);
5568 case GTK_FILE_CHOOSER_PROP_FILTER:
5569 set_current_filter (impl, g_value_get_object (value));
5572 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5573 set_local_only (impl, g_value_get_boolean (value));
5576 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5577 set_preview_widget (impl, g_value_get_object (value));
5580 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5581 impl->preview_widget_active = g_value_get_boolean (value);
5582 update_preview_widget_visibility (impl);
5585 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5586 impl->use_preview_label = g_value_get_boolean (value);
5587 update_preview_widget_visibility (impl);
5590 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5591 set_extra_widget (impl, g_value_get_object (value));
5594 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5596 gboolean select_multiple = g_value_get_boolean (value);
5597 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5598 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5601 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5602 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5603 "leaving the file chooser in single selection mode.");
5607 set_select_multiple (impl, select_multiple, FALSE);
5611 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5613 gboolean show_hidden = g_value_get_boolean (value);
5614 if (show_hidden != impl->show_hidden)
5616 impl->show_hidden = show_hidden;
5618 if (impl->browse_files_model)
5619 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5624 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5626 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5627 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5631 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5633 gboolean create_folders = g_value_get_boolean (value);
5634 impl->create_folders = create_folders;
5635 update_appearance (impl);
5640 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5646 gtk_file_chooser_default_get_property (GObject *object,
5651 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5655 case GTK_FILE_CHOOSER_PROP_ACTION:
5656 g_value_set_enum (value, impl->action);
5659 case GTK_FILE_CHOOSER_PROP_FILTER:
5660 g_value_set_object (value, impl->current_filter);
5663 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5664 g_value_set_boolean (value, impl->local_only);
5667 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5668 g_value_set_object (value, impl->preview_widget);
5671 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5672 g_value_set_boolean (value, impl->preview_widget_active);
5675 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5676 g_value_set_boolean (value, impl->use_preview_label);
5679 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5680 g_value_set_object (value, impl->extra_widget);
5683 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5684 g_value_set_boolean (value, impl->select_multiple);
5687 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5688 g_value_set_boolean (value, impl->show_hidden);
5691 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5692 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5695 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5696 g_value_set_boolean (value, impl->create_folders);
5700 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5705 /* This cancels everything that may be going on in the background. */
5707 cancel_all_operations (GtkFileChooserDefault *impl)
5711 pending_select_files_free (impl);
5713 /* cancel all pending operations */
5714 if (impl->pending_cancellables)
5716 for (l = impl->pending_cancellables; l; l = l->next)
5718 GCancellable *cancellable = G_CANCELLABLE (l->data);
5719 g_cancellable_cancel (cancellable);
5721 g_slist_free (impl->pending_cancellables);
5722 impl->pending_cancellables = NULL;
5725 if (impl->reload_icon_cancellables)
5727 for (l = impl->reload_icon_cancellables; l; l = l->next)
5729 GCancellable *cancellable = G_CANCELLABLE (l->data);
5730 g_cancellable_cancel (cancellable);
5732 g_slist_free (impl->reload_icon_cancellables);
5733 impl->reload_icon_cancellables = NULL;
5736 if (impl->loading_shortcuts)
5738 for (l = impl->loading_shortcuts; l; l = l->next)
5740 GCancellable *cancellable = G_CANCELLABLE (l->data);
5741 g_cancellable_cancel (cancellable);
5743 g_slist_free (impl->loading_shortcuts);
5744 impl->loading_shortcuts = NULL;
5747 if (impl->file_list_drag_data_received_cancellable)
5749 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5750 impl->file_list_drag_data_received_cancellable = NULL;
5753 if (impl->update_current_folder_cancellable)
5755 g_cancellable_cancel (impl->update_current_folder_cancellable);
5756 impl->update_current_folder_cancellable = NULL;
5759 if (impl->should_respond_get_info_cancellable)
5761 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5762 impl->should_respond_get_info_cancellable = NULL;
5765 if (impl->update_from_entry_cancellable)
5767 g_cancellable_cancel (impl->update_from_entry_cancellable);
5768 impl->update_from_entry_cancellable = NULL;
5771 if (impl->shortcuts_activate_iter_cancellable)
5773 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5774 impl->shortcuts_activate_iter_cancellable = NULL;
5777 search_stop_searching (impl, TRUE);
5778 recent_stop_loading (impl);
5781 /* Removes the settings signal handler. It's safe to call multiple times */
5783 remove_settings_signal (GtkFileChooserDefault *impl,
5786 if (impl->settings_signal_id)
5788 GtkSettings *settings;
5790 settings = gtk_settings_get_for_screen (screen);
5791 g_signal_handler_disconnect (settings,
5792 impl->settings_signal_id);
5793 impl->settings_signal_id = 0;
5798 gtk_file_chooser_default_dispose (GObject *object)
5800 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5802 cancel_all_operations (impl);
5804 if (impl->extra_widget)
5806 g_object_unref (impl->extra_widget);
5807 impl->extra_widget = NULL;
5810 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5812 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5815 /* We override show-all since we have internal widgets that
5816 * shouldn't be shown when you call show_all(), like the filter
5820 gtk_file_chooser_default_show_all (GtkWidget *widget)
5822 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5824 gtk_widget_show (widget);
5826 if (impl->extra_widget)
5827 gtk_widget_show_all (impl->extra_widget);
5830 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5831 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5834 toplevel_set_focus_cb (GtkWindow *window,
5836 GtkFileChooserDefault *impl)
5838 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5841 /* We monitor the focus widget on our toplevel to be able to know which widget
5842 * was last focused at the time our "should_respond" method gets called.
5845 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5846 GtkWidget *previous_toplevel)
5848 GtkFileChooserDefault *impl;
5849 GtkWidget *toplevel;
5851 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5852 toplevel = gtk_widget_get_toplevel (widget);
5854 if (previous_toplevel &&
5855 impl->toplevel_set_focus_id != 0)
5857 g_signal_handler_disconnect (previous_toplevel,
5858 impl->toplevel_set_focus_id);
5859 impl->toplevel_set_focus_id = 0;
5860 impl->toplevel_last_focus_widget = NULL;
5863 if (gtk_widget_is_toplevel (toplevel))
5865 g_assert (impl->toplevel_set_focus_id == 0);
5866 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5867 G_CALLBACK (toplevel_set_focus_cb), impl);
5868 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5872 /* Changes the icons wherever it is needed */
5874 change_icon_theme (GtkFileChooserDefault *impl)
5876 GtkSettings *settings;
5878 GtkCellRenderer *renderer;
5881 profile_start ("start", NULL);
5883 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5885 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5886 impl->icon_size = MAX (width, height);
5888 impl->icon_size = FALLBACK_ICON_SIZE;
5890 shortcuts_reload_icons (impl);
5891 /* the first cell in the first column is the icon column, and we have a fixed size there */
5892 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5893 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5894 renderer = GTK_CELL_RENDERER (cells->data);
5895 set_icon_cell_renderer_fixed_size (impl, renderer);
5896 g_list_free (cells);
5897 if (impl->browse_files_model)
5898 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5899 gtk_widget_queue_resize (impl->browse_files_tree_view);
5901 profile_end ("end", NULL);
5904 /* Callback used when a GtkSettings value changes */
5906 settings_notify_cb (GObject *object,
5908 GtkFileChooserDefault *impl)
5912 profile_start ("start", NULL);
5914 name = g_param_spec_get_name (pspec);
5916 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5917 strcmp (name, "gtk-icon-sizes") == 0)
5918 change_icon_theme (impl);
5920 profile_end ("end", NULL);
5923 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5927 check_icon_theme (GtkFileChooserDefault *impl)
5929 GtkSettings *settings;
5931 profile_start ("start", NULL);
5933 if (impl->settings_signal_id)
5935 profile_end ("end", NULL);
5939 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5941 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5942 impl->settings_signal_id = g_signal_connect (settings, "notify",
5943 G_CALLBACK (settings_notify_cb), impl);
5945 change_icon_theme (impl);
5948 profile_end ("end", NULL);
5952 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5954 GtkFileChooserDefault *impl;
5956 profile_start ("start", NULL);
5958 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5960 profile_msg (" parent class style_udpated start", NULL);
5961 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5962 profile_msg (" parent class style_updated end", NULL);
5964 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5965 change_icon_theme (impl);
5967 emit_default_size_changed (impl);
5969 profile_end ("end", NULL);
5973 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5974 GdkScreen *previous_screen)
5976 GtkFileChooserDefault *impl;
5978 profile_start ("start", NULL);
5980 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5982 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5983 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5985 remove_settings_signal (impl, previous_screen);
5986 check_icon_theme (impl);
5988 emit_default_size_changed (impl);
5990 profile_end ("end", NULL);
5994 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5995 GtkAllocation *allocation)
5997 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6001 set_sort_column (GtkFileChooserDefault *impl)
6003 GtkTreeSortable *sortable;
6005 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6007 /* can happen when we're still populating the model */
6008 if (sortable == NULL)
6011 gtk_tree_sortable_set_sort_column_id (sortable,
6017 settings_ensure (GtkFileChooserDefault *impl)
6019 if (impl->settings != NULL)
6022 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
6023 "/org/gtk/settings/file-chooser/");
6024 g_settings_delay (impl->settings);
6028 settings_load (GtkFileChooserDefault *impl)
6030 LocationMode location_mode;
6031 gboolean show_hidden;
6032 gboolean show_size_column;
6034 GtkSortType sort_order;
6036 settings_ensure (impl);
6038 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
6039 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
6040 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6041 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
6042 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
6044 location_mode_set (impl, location_mode, TRUE);
6046 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6048 impl->show_size_column = show_size_column;
6049 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6051 impl->sort_column = sort_column;
6052 impl->sort_order = sort_order;
6053 /* We don't call set_sort_column() here as the models may not have been
6054 * created yet. The individual functions that create and set the models will
6055 * call set_sort_column() themselves.
6060 save_dialog_geometry (GtkFileChooserDefault *impl)
6062 GtkWindow *toplevel;
6063 int x, y, width, height;
6065 toplevel = get_toplevel (GTK_WIDGET (impl));
6067 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6070 gtk_window_get_position (toplevel, &x, &y);
6071 gtk_window_get_size (toplevel, &width, &height);
6073 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
6074 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
6078 settings_save (GtkFileChooserDefault *impl)
6080 char *current_folder_uri;
6082 settings_ensure (impl);
6084 /* Current folder */
6086 if (impl->current_folder)
6087 current_folder_uri = g_file_get_uri (impl->current_folder);
6089 current_folder_uri = "";
6091 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
6093 if (impl->current_folder)
6094 g_free (current_folder_uri);
6096 /* All the other state */
6098 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6099 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
6100 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6101 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6102 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6103 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6105 save_dialog_geometry (impl);
6107 /* Now apply the settings */
6108 g_settings_apply (impl->settings);
6110 g_object_unref (impl->settings);
6111 impl->settings = NULL;
6114 /* GtkWidget::realize method */
6116 gtk_file_chooser_default_realize (GtkWidget *widget)
6118 GtkFileChooserDefault *impl;
6120 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6122 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6124 emit_default_size_changed (impl);
6128 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
6130 char *last_folder_uri;
6133 settings_ensure (impl);
6135 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
6137 /* If no last folder is set, we use the user's home directory, since
6138 * this is the starting point for most documents.
6140 if (last_folder_uri[0] == '\0')
6141 file = g_file_new_for_path (g_get_home_dir ());
6143 file = g_file_new_for_uri (last_folder_uri);
6145 g_free (last_folder_uri);
6150 /* GtkWidget::map method */
6152 gtk_file_chooser_default_map (GtkWidget *widget)
6154 GtkFileChooserDefault *impl;
6156 profile_start ("start", NULL);
6158 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6160 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6162 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6164 switch (impl->reload_state)
6167 recent_shortcut_handler (impl);
6170 case RELOAD_HAS_FOLDER:
6171 /* Nothing; we are already loading or loaded, so we
6172 * don't need to reload
6177 g_assert_not_reached ();
6181 volumes_bookmarks_changed_cb (impl->file_system, impl);
6183 settings_load (impl);
6185 profile_end ("end", NULL);
6188 /* GtkWidget::unmap method */
6190 gtk_file_chooser_default_unmap (GtkWidget *widget)
6192 GtkFileChooserDefault *impl;
6194 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6196 settings_save (impl);
6198 cancel_all_operations (impl);
6199 impl->reload_state = RELOAD_EMPTY;
6201 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6205 install_list_model_filter (GtkFileChooserDefault *impl)
6207 _gtk_file_system_model_set_filter (impl->browse_files_model,
6208 impl->current_filter);
6211 #define COMPARE_DIRECTORIES \
6212 GtkFileChooserDefault *impl = user_data; \
6213 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6214 gboolean dir_a, dir_b; \
6216 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6217 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6219 if (dir_a != dir_b) \
6220 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6222 /* Sort callback for the filename column */
6224 name_sort_func (GtkTreeModel *model,
6229 COMPARE_DIRECTORIES;
6232 const char *key_a, *key_b;
6235 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6236 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6239 result = strcmp (key_a, key_b);
6251 /* Sort callback for the size column */
6253 size_sort_func (GtkTreeModel *model,
6258 COMPARE_DIRECTORIES;
6261 gint64 size_a, size_b;
6263 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6264 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6266 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6270 /* Sort callback for the mtime column */
6272 mtime_sort_func (GtkTreeModel *model,
6277 COMPARE_DIRECTORIES;
6282 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6283 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6285 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6289 /* Callback used when the sort column changes. We cache the sort order for use
6290 * in name_sort_func().
6293 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6294 GtkFileChooserDefault *impl)
6296 gint sort_column_id;
6297 GtkSortType sort_type;
6299 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6301 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6302 impl->sort_column = sort_column_id;
6303 impl->sort_order = sort_type;
6308 set_busy_cursor (GtkFileChooserDefault *impl,
6312 GtkWindow *toplevel;
6313 GdkDisplay *display;
6316 toplevel = get_toplevel (GTK_WIDGET (impl));
6317 widget = GTK_WIDGET (toplevel);
6318 if (!toplevel || !gtk_widget_get_realized (widget))
6321 display = gtk_widget_get_display (widget);
6324 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6328 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6329 gdk_display_flush (display);
6332 g_object_unref (cursor);
6335 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6337 load_set_model (GtkFileChooserDefault *impl)
6339 profile_start ("start", NULL);
6341 g_assert (impl->browse_files_model != NULL);
6343 profile_msg (" gtk_tree_view_set_model start", NULL);
6344 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6345 GTK_TREE_MODEL (impl->browse_files_model));
6346 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6347 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6349 file_list_set_sort_column_ids (impl);
6350 set_sort_column (impl);
6351 profile_msg (" gtk_tree_view_set_model end", NULL);
6352 impl->list_sort_ascending = TRUE;
6354 profile_end ("end", NULL);
6357 /* Timeout callback used when the loading timer expires */
6359 load_timeout_cb (gpointer data)
6361 GtkFileChooserDefault *impl;
6363 profile_start ("start", NULL);
6365 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6366 g_assert (impl->load_state == LOAD_PRELOAD);
6367 g_assert (impl->load_timeout_id != 0);
6368 g_assert (impl->browse_files_model != NULL);
6370 impl->load_timeout_id = 0;
6371 impl->load_state = LOAD_LOADING;
6373 load_set_model (impl);
6375 profile_end ("end", NULL);
6380 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6382 load_setup_timer (GtkFileChooserDefault *impl)
6384 g_assert (impl->load_timeout_id == 0);
6385 g_assert (impl->load_state != LOAD_PRELOAD);
6387 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6388 impl->load_state = LOAD_PRELOAD;
6391 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6393 load_remove_timer (GtkFileChooserDefault *impl)
6395 if (impl->load_timeout_id != 0)
6397 g_assert (impl->load_state == LOAD_PRELOAD);
6399 g_source_remove (impl->load_timeout_id);
6400 impl->load_timeout_id = 0;
6401 impl->load_state = LOAD_EMPTY;
6404 g_assert (impl->load_state == LOAD_EMPTY ||
6405 impl->load_state == LOAD_LOADING ||
6406 impl->load_state == LOAD_FINISHED);
6409 /* Selects the first row in the file list */
6411 browse_files_select_first_row (GtkFileChooserDefault *impl)
6414 GtkTreeIter dummy_iter;
6415 GtkTreeModel *tree_model;
6417 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6422 path = gtk_tree_path_new_from_indices (0, -1);
6424 /* If the list is empty, do nothing. */
6425 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6426 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6428 gtk_tree_path_free (path);
6431 struct center_selected_row_closure {
6432 GtkFileChooserDefault *impl;
6433 gboolean already_centered;
6436 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6437 * selected row in the tree view.
6440 center_selected_row_foreach_cb (GtkTreeModel *model,
6445 struct center_selected_row_closure *closure;
6448 if (closure->already_centered)
6451 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6452 closure->already_centered = TRUE;
6455 /* Centers the selected row in the tree view */
6457 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6459 struct center_selected_row_closure closure;
6460 GtkTreeSelection *selection;
6462 closure.impl = impl;
6463 closure.already_centered = FALSE;
6465 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6466 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6470 show_and_select_files (GtkFileChooserDefault *impl,
6473 GtkTreeSelection *selection;
6474 GtkFileSystemModel *fsmodel;
6475 gboolean enabled_hidden, removed_filters;
6476 gboolean selected_a_file;
6479 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6480 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6482 enabled_hidden = impl->show_hidden;
6483 removed_filters = (impl->current_filter == NULL);
6485 selected_a_file = FALSE;
6487 for (walk = files; walk; walk = walk->next)
6489 GFile *file = walk->data;
6492 /* Is it a hidden file? */
6494 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6497 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6499 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6501 if (!enabled_hidden &&
6502 (g_file_info_get_is_hidden (info) ||
6503 g_file_info_get_is_backup (info)))
6505 g_object_set (impl, "show-hidden", TRUE, NULL);
6506 enabled_hidden = TRUE;
6510 /* Is it a filtered file? */
6512 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6513 continue; /* re-get the iter as it may change when the model refilters */
6515 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6517 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6518 if (!removed_filters)
6520 set_current_filter (impl, NULL);
6521 removed_filters = TRUE;
6525 /* Okay, can we select the file now? */
6527 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6530 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6534 gtk_tree_selection_select_iter (selection, &iter);
6536 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6537 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6539 gtk_tree_path_free (path);
6541 selected_a_file = TRUE;
6545 browse_files_center_selected_row (impl);
6547 return selected_a_file;
6550 /* Processes the pending operation when a folder is finished loading */
6552 pending_select_files_process (GtkFileChooserDefault *impl)
6554 g_assert (impl->load_state == LOAD_FINISHED);
6555 g_assert (impl->browse_files_model != NULL);
6557 if (impl->pending_select_files)
6559 show_and_select_files (impl, impl->pending_select_files);
6560 pending_select_files_free (impl);
6561 browse_files_center_selected_row (impl);
6565 /* We only select the first row if the chooser is actually mapped ---
6566 * selecting the first row is to help the user when he is interacting with
6567 * the chooser, but sometimes a chooser works not on behalf of the user,
6568 * but rather on behalf of something else like GtkFileChooserButton. In
6569 * that case, the chooser's selection should be what the caller expects,
6570 * as the user can't see that something else got selected. See bug #165264.
6572 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6573 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6574 browse_files_select_first_row (impl);
6577 g_assert (impl->pending_select_files == NULL);
6581 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6586 info = g_file_query_info (impl->current_folder,
6587 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6588 G_FILE_QUERY_INFO_NONE,
6593 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6594 g_object_unref (info);
6597 msg = g_strdup (_("Could not read the contents of the folder"));
6599 error_message (impl, msg, error->message);
6603 /* Callback used when the file system model finishes loading */
6605 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6607 GtkFileChooserDefault *impl)
6609 profile_start ("start", NULL);
6612 show_error_on_reading_current_folder (impl, error);
6614 if (impl->load_state == LOAD_PRELOAD)
6616 load_remove_timer (impl);
6617 load_set_model (impl);
6619 else if (impl->load_state == LOAD_LOADING)
6625 /* We can't g_assert_not_reached(), as something other than us may have
6626 * initiated a folder reload. See #165556.
6628 profile_end ("end", NULL);
6632 g_assert (impl->load_timeout_id == 0);
6634 impl->load_state = LOAD_FINISHED;
6636 pending_select_files_process (impl);
6637 set_busy_cursor (impl, FALSE);
6638 #ifdef PROFILE_FILE_CHOOSER
6639 access ("MARK: *** FINISHED LOADING", F_OK);
6642 profile_end ("end", NULL);
6646 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6647 gboolean remove_from_treeview)
6649 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6651 if (impl->browse_files_model)
6653 g_object_unref (impl->browse_files_model);
6654 impl->browse_files_model = NULL;
6657 if (remove_from_treeview)
6658 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6662 my_g_format_time_for_display (glong secs)
6667 time_t time_mtime, time_now;
6668 const gchar *format;
6669 gchar *locale_format = NULL;
6671 char *date_str = NULL;
6673 const char *locale, *dot = NULL;
6674 gint64 codepage = -1;
6680 #ifdef HAVE_LOCALTIME_R
6681 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6684 struct tm *ptm = localtime ((time_t *) &time_mtime);
6688 g_warning ("ptm != NULL failed");
6690 return g_strdup (_("Unknown"));
6693 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6695 #endif /* HAVE_LOCALTIME_R */
6697 g_date_set_time_t (&mtime, time_mtime);
6698 time_now = time (NULL);
6699 g_date_set_time_t (&now, time_now);
6701 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6703 /* Translators: %H means "hours" and %M means "minutes" */
6705 format = _("%H:%M");
6706 else if (days_diff == 1)
6707 format = _("Yesterday at %H:%M");
6710 if (days_diff > 1 && days_diff < 7)
6711 format = "%A"; /* Days from last week */
6713 format = "%x"; /* Any other date */
6717 /* g_locale_from_utf8() returns a string in the system
6718 * code-page, which is not always the same as that used by the C
6719 * library. For instance when running a GTK+ program with
6720 * LANG=ko on an English version of Windows, the system
6721 * code-page is 1252, but the code-page used by the C library is
6722 * 949. (It's GTK+ itself that sets the C library locale when it
6723 * notices the LANG environment variable. See gtkmain.c The
6724 * Microsoft C library doesn't look at any locale environment
6725 * variables.) We need to pass strftime() a string in the C
6726 * library's code-page. See bug #509885.
6728 locale = setlocale (LC_ALL, NULL);
6730 dot = strchr (locale, '.');
6733 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6735 /* All codepages should fit in 16 bits AFAIK */
6736 if (codepage > 0 && codepage < 65536)
6738 sprintf (charset, "CP%u", (guint) codepage);
6739 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6743 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6745 if (locale_format != NULL &&
6746 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6749 /* As above but in opposite direction... */
6750 if (codepage > 0 && codepage < 65536)
6751 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6753 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6757 if (date_str == NULL)
6758 date_str = g_strdup (_("Unknown"));
6760 g_free (locale_format);
6765 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6767 GFileAttributeType type;
6770 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6771 g_file_info_set_attribute (to, attribute, type, value);
6775 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6777 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6778 GFile *file = G_FILE (object);
6779 GFileInfo *queried, *info;
6782 queried = g_file_query_info_finish (file, res, NULL);
6783 if (queried == NULL)
6786 GDK_THREADS_ENTER ();
6788 /* now we know model is valid */
6790 /* file was deleted */
6791 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6793 GDK_THREADS_LEAVE ();
6797 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6799 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6800 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6801 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6803 _gtk_file_system_model_update_file (model, file, info, FALSE);
6805 g_object_unref (info);
6807 GDK_THREADS_LEAVE ();
6811 file_system_model_set (GtkFileSystemModel *model,
6818 GtkFileChooserDefault *impl = data;
6822 case MODEL_COL_FILE:
6823 g_value_set_object (value, file);
6825 case MODEL_COL_NAME:
6827 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6829 g_value_set_string (value, g_file_info_get_display_name (info));
6831 case MODEL_COL_NAME_COLLATED:
6833 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6835 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6837 case MODEL_COL_IS_FOLDER:
6838 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6840 case MODEL_COL_PIXBUF:
6843 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6845 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6849 GtkTreeModel *tree_model;
6850 GtkTreePath *path, *start, *end;
6853 if (impl->browse_files_tree_view == NULL ||
6854 g_file_info_has_attribute (info, "filechooser::queried"))
6857 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6858 if (tree_model != GTK_TREE_MODEL (model))
6861 if (!_gtk_file_system_model_get_iter_for_file (model,
6864 g_assert_not_reached ();
6865 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6867 path = gtk_tree_model_get_path (tree_model, &iter);
6868 if (gtk_tree_path_compare (start, path) != 1 &&
6869 gtk_tree_path_compare (path, end) != 1)
6871 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6872 g_file_query_info_async (file,
6873 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6874 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6875 G_FILE_ATTRIBUTE_STANDARD_ICON,
6876 G_FILE_QUERY_INFO_NONE,
6878 _gtk_file_system_model_get_cancellable (model),
6879 file_system_model_got_thumbnail,
6882 gtk_tree_path_free (path);
6883 gtk_tree_path_free (start);
6884 gtk_tree_path_free (end);
6889 g_value_set_object (value, NULL);
6891 case MODEL_COL_SIZE:
6892 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6894 case MODEL_COL_SIZE_TEXT:
6895 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6896 g_value_set_string (value, NULL);
6898 g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6900 case MODEL_COL_MTIME:
6901 case MODEL_COL_MTIME_TEXT:
6906 g_file_info_get_modification_time (info, &tv);
6907 if (column == MODEL_COL_MTIME)
6908 g_value_set_long (value, tv.tv_sec);
6909 else if (tv.tv_sec == 0)
6910 g_value_set_static_string (value, _("Unknown"));
6912 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6915 case MODEL_COL_ELLIPSIZE:
6916 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6919 g_assert_not_reached ();
6926 /* Gets rid of the old list model and creates a new one for the current folder */
6928 set_list_model (GtkFileChooserDefault *impl,
6931 g_assert (impl->current_folder != NULL);
6933 profile_start ("start", NULL);
6935 stop_loading_and_clear_list_model (impl, TRUE);
6937 set_busy_cursor (impl, TRUE);
6939 impl->browse_files_model =
6940 _gtk_file_system_model_new_for_directory (impl->current_folder,
6942 file_system_model_set,
6944 MODEL_COLUMN_TYPES);
6946 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6948 profile_msg (" set sort function", NULL);
6949 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6950 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6951 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6952 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6953 set_sort_column (impl);
6954 impl->list_sort_ascending = TRUE;
6955 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6956 G_CALLBACK (list_sort_column_changed_cb), impl);
6958 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6960 g_signal_connect (impl->browse_files_model, "finished-loading",
6961 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6963 install_list_model_filter (impl);
6965 profile_end ("end", NULL);
6970 struct update_chooser_entry_selected_foreach_closure {
6972 GtkTreeIter first_selected_iter;
6976 compare_utf8_filenames (const gchar *a,
6979 gchar *a_folded, *b_folded;
6982 a_folded = g_utf8_strdown (a, -1);
6983 b_folded = g_utf8_strdown (b, -1);
6985 retval = strcmp (a_folded, b_folded);
6994 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6999 struct update_chooser_entry_selected_foreach_closure *closure;
7002 closure->num_selected++;
7004 if (closure->num_selected == 1)
7005 closure->first_selected_iter = *iter;
7009 update_chooser_entry (GtkFileChooserDefault *impl)
7011 GtkTreeSelection *selection;
7012 struct update_chooser_entry_selected_foreach_closure closure;
7014 /* no need to update the file chooser's entry if there's no entry */
7015 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7016 !impl->location_entry)
7019 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7020 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7021 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7022 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7023 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
7026 g_assert (impl->location_entry != NULL);
7028 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7029 closure.num_selected = 0;
7030 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7032 if (closure.num_selected == 0)
7034 if (impl->operation_mode == OPERATION_MODE_RECENT)
7035 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
7037 goto maybe_clear_entry;
7039 else if (closure.num_selected == 1)
7041 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7044 gboolean change_entry;
7046 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7048 /* If the cursor moved to the row of the newly created folder,
7049 * retrieving info will return NULL.
7054 g_free (impl->browse_files_last_selected_name);
7055 impl->browse_files_last_selected_name =
7056 g_strdup (g_file_info_get_display_name (info));
7058 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7059 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7060 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7062 /* Don't change the name when clicking on a folder... */
7063 change_entry = (! _gtk_file_info_consider_as_directory (info));
7066 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7070 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7072 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7073 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7078 else if (impl->operation_mode == OPERATION_MODE_RECENT
7079 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7083 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7085 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7086 MODEL_COL_FILE, &folder,
7088 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7089 g_object_unref (folder);
7095 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7096 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7098 /* Multiple selection, so just clear the entry. */
7099 g_free (impl->browse_files_last_selected_name);
7100 impl->browse_files_last_selected_name = NULL;
7102 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7108 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7109 && impl->browse_files_last_selected_name)
7111 const char *entry_text;
7113 gboolean clear_entry;
7115 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7116 len = strlen (entry_text);
7119 /* The file chooser entry may have appended a "/" to its text.
7120 * So take it out, and compare the result to the old selection.
7122 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7126 tmp = g_strndup (entry_text, len - 1);
7127 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7131 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7134 clear_entry = FALSE;
7137 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7142 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7146 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7150 struct UpdateCurrentFolderData
7152 GtkFileChooserDefault *impl;
7154 gboolean keep_trail;
7155 gboolean clear_entry;
7156 GFile *original_file;
7157 GError *original_error;
7161 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7162 GtkFileSystemVolume *volume,
7163 const GError *error,
7166 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7167 struct UpdateCurrentFolderData *data = user_data;
7168 GtkFileChooserDefault *impl = data->impl;
7170 if (cancellable != impl->update_current_folder_cancellable)
7173 impl->update_current_folder_cancellable = NULL;
7174 set_busy_cursor (impl, FALSE);
7181 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7182 impl->reload_state = RELOAD_EMPTY;
7186 change_folder_and_display_error (impl, data->file, data->clear_entry);
7189 g_object_unref (data->file);
7192 g_object_unref (cancellable);
7196 update_current_folder_get_info_cb (GCancellable *cancellable,
7198 const GError *error,
7201 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7202 struct UpdateCurrentFolderData *data = user_data;
7203 GtkFileChooserDefault *impl = data->impl;
7205 if (cancellable != impl->update_current_folder_cancellable)
7208 impl->update_current_folder_cancellable = NULL;
7209 impl->reload_state = RELOAD_EMPTY;
7211 set_busy_cursor (impl, FALSE);
7220 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7222 GMountOperation *mount_operation;
7223 GtkWidget *toplevel;
7225 g_object_unref (cancellable);
7226 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7228 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7230 set_busy_cursor (impl, TRUE);
7232 impl->update_current_folder_cancellable =
7233 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7235 update_current_folder_mount_enclosing_volume_cb,
7241 if (!data->original_file)
7243 data->original_file = g_object_ref (data->file);
7244 data->original_error = g_error_copy (error);
7247 parent_file = g_file_get_parent (data->file);
7249 /* get parent path and try to change the folder to that */
7252 g_object_unref (data->file);
7253 data->file = parent_file;
7255 g_object_unref (cancellable);
7257 /* restart the update current folder operation */
7258 impl->reload_state = RELOAD_HAS_FOLDER;
7260 impl->update_current_folder_cancellable =
7261 _gtk_file_system_get_info (impl->file_system, data->file,
7263 update_current_folder_get_info_cb,
7266 set_busy_cursor (impl, TRUE);
7272 /* Error and bail out, ignoring "not found" errors since they're useless:
7273 * they only happen when a program defaults to a folder that has been (re)moved.
7275 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7276 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7278 g_error_free (data->original_error);
7280 g_object_unref (data->original_file);
7286 if (data->original_file)
7288 /* Error and bail out, ignoring "not found" errors since they're useless:
7289 * they only happen when a program defaults to a folder that has been (re)moved.
7291 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7292 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7294 g_error_free (data->original_error);
7296 g_object_unref (data->original_file);
7299 if (! _gtk_file_info_consider_as_directory (info))
7302 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail);
7304 if (impl->current_folder != data->file)
7306 if (impl->current_folder)
7307 g_object_unref (impl->current_folder);
7309 impl->current_folder = g_object_ref (data->file);
7312 impl->reload_state = RELOAD_HAS_FOLDER;
7314 /* Update the widgets that may trigger a folder change themselves. */
7316 if (!impl->changing_folder)
7318 impl->changing_folder = TRUE;
7320 shortcuts_update_current_folder (impl);
7322 impl->changing_folder = FALSE;
7325 /* Set the folder on the save entry */
7327 if (impl->location_entry)
7329 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7330 impl->current_folder);
7332 if (data->clear_entry)
7333 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7336 /* Create a new list model. This is slightly evil; we store the result value
7337 * but perform more actions rather than returning immediately even if it
7338 * generates an error.
7340 set_list_model (impl, NULL);
7342 /* Refresh controls */
7344 shortcuts_find_current_folder (impl);
7346 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7348 check_preview_change (impl);
7349 bookmarks_check_add_sensitivity (impl);
7351 g_signal_emit_by_name (impl, "selection-changed", 0);
7354 g_object_unref (data->file);
7357 g_object_unref (cancellable);
7361 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7363 gboolean keep_trail,
7364 gboolean clear_entry,
7367 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7368 struct UpdateCurrentFolderData *data;
7370 profile_start ("start", NULL);
7372 g_object_ref (file);
7374 operation_mode_set (impl, OPERATION_MODE_BROWSE);
7376 if (impl->local_only && !g_file_is_native (file))
7378 g_set_error_literal (error,
7379 GTK_FILE_CHOOSER_ERROR,
7380 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7381 _("Cannot change to folder because it is not local"));
7383 g_object_unref (file);
7384 profile_end ("end - not local", NULL);
7388 if (impl->update_current_folder_cancellable)
7389 g_cancellable_cancel (impl->update_current_folder_cancellable);
7391 /* Test validity of path here. */
7392 data = g_new0 (struct UpdateCurrentFolderData, 1);
7394 data->file = g_object_ref (file);
7395 data->keep_trail = keep_trail;
7396 data->clear_entry = clear_entry;
7398 impl->reload_state = RELOAD_HAS_FOLDER;
7400 impl->update_current_folder_cancellable =
7401 _gtk_file_system_get_info (impl->file_system, file,
7403 update_current_folder_get_info_cb,
7406 set_busy_cursor (impl, TRUE);
7407 g_object_unref (file);
7409 profile_end ("end", NULL);
7414 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7416 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7418 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7419 impl->operation_mode == OPERATION_MODE_RECENT)
7422 if (impl->reload_state == RELOAD_EMPTY)
7424 /* We are unmapped, or we had an error while loading the last folder.
7425 * We'll return the folder used by the last invocation of the file chooser
7426 * since once we get (re)mapped, we'll load *that* folder anyway unless
7427 * the caller explicitly calls set_current_folder() on us.
7429 return get_file_for_last_folder_opened (impl);
7432 if (impl->current_folder)
7433 return g_object_ref (impl->current_folder);
7439 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7442 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7444 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7445 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7447 pending_select_files_free (impl);
7448 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), name);
7452 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7456 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7460 parent_file = g_file_get_parent (file);
7463 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7465 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7466 impl->operation_mode == OPERATION_MODE_RECENT ||
7467 impl->load_state == LOAD_EMPTY)
7473 g_assert (impl->current_folder != NULL);
7475 same_path = g_file_equal (parent_file, impl->current_folder);
7478 if (same_path && impl->load_state == LOAD_FINISHED)
7483 files.data = (gpointer) file;
7486 result = show_and_select_files (impl, &files);
7487 g_object_unref (parent_file);
7491 pending_select_files_add (impl, file);
7497 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7498 g_object_unref (parent_file);
7502 g_object_unref (parent_file);
7507 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7510 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7511 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7514 if (!impl->browse_files_model)
7517 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7522 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7527 maybe_select (GtkTreeModel *model,
7532 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7533 GtkTreeSelection *selection;
7536 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7538 gtk_tree_model_get (model, iter,
7539 MODEL_COL_IS_FOLDER, &is_folder,
7542 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7543 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7544 gtk_tree_selection_select_iter (selection, iter);
7546 gtk_tree_selection_unselect_iter (selection, iter);
7552 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7554 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7556 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7557 impl->operation_mode == OPERATION_MODE_RECENT)
7559 GtkTreeSelection *selection;
7561 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7562 gtk_tree_selection_select_all (selection);
7566 if (impl->select_multiple)
7567 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7568 maybe_select, impl);
7572 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7574 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7575 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7577 gtk_tree_selection_unselect_all (selection);
7578 pending_select_files_free (impl);
7581 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7583 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7585 * is_empty_ret - whether the file entry is totally empty
7587 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7588 * the path will be "$cwd/foobar")
7591 check_save_entry (GtkFileChooserDefault *impl,
7593 gboolean *is_well_formed_ret,
7594 gboolean *is_empty_ret,
7595 gboolean *is_file_part_empty_ret,
7596 gboolean *is_folder)
7598 GtkFileChooserEntry *chooser_entry;
7599 GFile *current_folder;
7600 const char *file_part;
7604 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7605 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7606 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7607 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7608 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7610 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7612 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7615 *is_well_formed_ret = TRUE;
7616 *is_empty_ret = TRUE;
7617 *is_file_part_empty_ret = TRUE;
7623 *is_empty_ret = FALSE;
7625 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7626 if (!current_folder)
7629 *is_well_formed_ret = FALSE;
7630 *is_file_part_empty_ret = FALSE;
7636 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7638 if (!file_part || file_part[0] == '\0')
7640 *file_ret = current_folder;
7641 *is_well_formed_ret = TRUE;
7642 *is_file_part_empty_ret = TRUE;
7648 *is_file_part_empty_ret = FALSE;
7651 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7652 g_object_unref (current_folder);
7656 error_building_filename_dialog (impl, error);
7658 *is_well_formed_ret = FALSE;
7665 *is_well_formed_ret = TRUE;
7666 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7669 struct get_files_closure {
7670 GtkFileChooserDefault *impl;
7672 GFile *file_from_entry;
7676 get_files_foreach (GtkTreeModel *model,
7681 struct get_files_closure *info;
7683 GtkFileSystemModel *fs_model;
7686 fs_model = info->impl->browse_files_model;
7688 file = _gtk_file_system_model_get_file (fs_model, iter);
7690 return; /* We are on the editable row */
7692 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7693 info->result = g_slist_prepend (info->result, g_object_ref (file));
7697 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7699 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7700 struct get_files_closure info;
7701 GtkWindow *toplevel;
7702 GtkWidget *current_focus;
7703 gboolean file_list_seen;
7707 info.file_from_entry = NULL;
7709 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7710 return search_get_selected_files (impl);
7712 if (impl->operation_mode == OPERATION_MODE_RECENT)
7714 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7716 file_list_seen = TRUE;
7720 return recent_get_selected_files (impl);
7723 toplevel = get_toplevel (GTK_WIDGET (impl));
7725 current_focus = gtk_window_get_focus (toplevel);
7727 current_focus = NULL;
7729 file_list_seen = FALSE;
7730 if (current_focus == impl->browse_files_tree_view)
7732 GtkTreeSelection *selection;
7736 file_list_seen = TRUE;
7737 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7738 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7740 /* If there is no selection in the file list, we probably have this situation:
7742 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7743 * 2. He then double-clicked on a folder ("bar") in the file list
7745 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7746 * filename entry to see if that is the case.
7748 if (info.result == NULL && impl->location_entry)
7751 else if (impl->location_entry && current_focus == impl->location_entry)
7753 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7757 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7762 if (!is_well_formed)
7765 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7767 g_object_unref (info.file_from_entry);
7771 if (info.file_from_entry)
7772 info.result = g_slist_prepend (info.result, info.file_from_entry);
7773 else if (!file_list_seen)
7778 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7780 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7784 /* The focus is on a dialog's action area button or something else */
7785 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7786 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7794 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7795 * fall back to the current directory */
7796 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7797 info.result == NULL)
7799 GFile *current_folder;
7801 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7804 info.result = g_slist_prepend (info.result, current_folder);
7807 return g_slist_reverse (info.result);
7811 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7813 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7815 if (impl->preview_file)
7816 return g_object_ref (impl->preview_file);
7821 static GtkFileSystem *
7822 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7824 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7826 return impl->file_system;
7829 /* Shows or hides the filter widgets */
7831 show_filters (GtkFileChooserDefault *impl,
7835 gtk_widget_show (impl->filter_combo_hbox);
7837 gtk_widget_hide (impl->filter_combo_hbox);
7841 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7842 GtkFileFilter *filter)
7844 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7847 if (g_slist_find (impl->filters, filter))
7849 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7853 g_object_ref_sink (filter);
7854 impl->filters = g_slist_append (impl->filters, filter);
7856 name = gtk_file_filter_get_name (filter);
7858 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7860 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7862 if (!g_slist_find (impl->filters, impl->current_filter))
7863 set_current_filter (impl, filter);
7865 show_filters (impl, TRUE);
7869 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7870 GtkFileFilter *filter)
7872 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7873 GtkTreeModel *model;
7877 filter_index = g_slist_index (impl->filters, filter);
7879 if (filter_index < 0)
7881 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7885 impl->filters = g_slist_remove (impl->filters, filter);
7887 if (filter == impl->current_filter)
7890 set_current_filter (impl, impl->filters->data);
7892 set_current_filter (impl, NULL);
7895 /* Remove row from the combo box */
7896 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7897 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7898 g_assert_not_reached ();
7900 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7902 g_object_unref (filter);
7905 show_filters (impl, FALSE);
7909 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7911 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7913 return g_slist_copy (impl->filters);
7916 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7918 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7921 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7924 struct AddShortcutData
7926 GtkFileChooserDefault *impl;
7931 add_shortcut_get_info_cb (GCancellable *cancellable,
7933 const GError *error,
7937 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7938 struct AddShortcutData *data = user_data;
7940 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7943 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7945 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7948 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7950 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7953 g_object_unref (data->impl);
7954 g_object_unref (data->file);
7957 g_object_unref (cancellable);
7961 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7965 GCancellable *cancellable;
7966 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7967 struct AddShortcutData *data;
7971 /* Avoid adding duplicates */
7972 pos = shortcut_find_position (impl, file);
7973 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7977 uri = g_file_get_uri (file);
7978 /* translators, "Shortcut" means "Bookmark" here */
7980 GTK_FILE_CHOOSER_ERROR,
7981 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7982 _("Shortcut %s already exists"),
7989 for (l = impl->loading_shortcuts; l; l = l->next)
7991 GCancellable *c = l->data;
7994 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7995 if (f && g_file_equal (file, f))
7999 uri = g_file_get_uri (file);
8001 GTK_FILE_CHOOSER_ERROR,
8002 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8003 _("Shortcut %s already exists"),
8011 data = g_new0 (struct AddShortcutData, 1);
8012 data->impl = g_object_ref (impl);
8013 data->file = g_object_ref (file);
8015 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8017 add_shortcut_get_info_cb, data);
8022 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8023 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8029 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8033 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8040 for (l = impl->loading_shortcuts; l; l = l->next)
8042 GCancellable *c = l->data;
8045 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8046 if (f && g_file_equal (file, f))
8048 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8049 g_cancellable_cancel (c);
8054 if (impl->num_shortcuts == 0)
8057 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8058 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8059 g_assert_not_reached ();
8061 for (i = 0; i < impl->num_shortcuts; i++)
8064 ShortcutType shortcut_type;
8067 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8068 SHORTCUTS_COL_DATA, &col_data,
8069 SHORTCUTS_COL_TYPE, &shortcut_type,
8071 g_assert (col_data != NULL);
8072 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8074 shortcut = col_data;
8075 if (g_file_equal (shortcut, file))
8077 shortcuts_remove_rows (impl, pos + i, 1);
8078 impl->num_shortcuts--;
8082 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8083 g_assert_not_reached ();
8088 uri = g_file_get_uri (file);
8089 /* translators, "Shortcut" means "Bookmark" here */
8091 GTK_FILE_CHOOSER_ERROR,
8092 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8093 _("Shortcut %s does not exist"),
8101 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8103 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8109 if (impl->num_shortcuts == 0)
8112 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8113 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8114 g_assert_not_reached ();
8118 for (i = 0; i < impl->num_shortcuts; i++)
8121 ShortcutType shortcut_type;
8124 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8125 SHORTCUTS_COL_DATA, &col_data,
8126 SHORTCUTS_COL_TYPE, &shortcut_type,
8128 g_assert (col_data != NULL);
8129 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8131 shortcut = col_data;
8132 list = g_slist_prepend (list, g_object_ref (shortcut));
8134 if (i != impl->num_shortcuts - 1)
8136 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8137 g_assert_not_reached ();
8141 return g_slist_reverse (list);
8144 /* Guesses a size based upon font sizes */
8146 find_good_size_from_style (GtkWidget *widget,
8150 GtkStyleContext *context;
8151 GtkStateFlags state;
8156 context = gtk_widget_get_style_context (widget);
8157 state = gtk_widget_get_state_flags (widget);
8159 screen = gtk_widget_get_screen (widget);
8162 resolution = gdk_screen_get_resolution (screen);
8163 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8167 resolution = 96.0; /* wheeee */
8169 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
8170 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8172 *width = font_size * NUM_CHARS;
8173 *height = font_size * NUM_LINES;
8177 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8178 gint *default_width,
8179 gint *default_height)
8181 GtkFileChooserDefault *impl;
8183 int x, y, width, height;
8185 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8187 settings_ensure (impl);
8189 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8190 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8192 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8194 *default_width = width;
8195 *default_height = height;
8199 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8201 if (impl->preview_widget_active &&
8202 impl->preview_widget &&
8203 gtk_widget_get_visible (impl->preview_widget))
8205 gtk_widget_get_preferred_size (impl->preview_box,
8207 *default_width += PREVIEW_HBOX_SPACING + req.width;
8210 if (impl->extra_widget &&
8211 gtk_widget_get_visible (impl->extra_widget))
8213 gtk_widget_get_preferred_size (impl->extra_align,
8215 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8219 struct switch_folder_closure {
8220 GtkFileChooserDefault *impl;
8225 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8227 switch_folder_foreach_cb (GtkTreeModel *model,
8232 struct switch_folder_closure *closure;
8236 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8237 closure->num_selected++;
8240 /* Changes to the selected folder in the list view */
8242 switch_to_selected_folder (GtkFileChooserDefault *impl)
8244 GtkTreeSelection *selection;
8245 struct switch_folder_closure closure;
8247 /* We do this with foreach() rather than get_selected() as we may be in
8248 * multiple selection mode
8251 closure.impl = impl;
8252 closure.file = NULL;
8253 closure.num_selected = 0;
8255 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8256 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8258 g_assert (closure.file && closure.num_selected == 1);
8260 change_folder_and_display_error (impl, closure.file, FALSE);
8263 /* Gets the GFileInfo for the selected row in the file list; assumes single
8267 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8268 gboolean *had_selection)
8270 GtkTreeSelection *selection;
8274 g_assert (!impl->select_multiple);
8275 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8276 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8278 *had_selection = FALSE;
8282 *had_selection = TRUE;
8284 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8288 /* Gets the display name of the selected file in the file list; assumes single
8289 * selection mode and that something is selected.
8291 static const gchar *
8292 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8295 gboolean had_selection;
8297 info = get_selected_file_info_from_file_list (impl, &had_selection);
8298 g_assert (had_selection);
8299 g_assert (info != NULL);
8301 return g_file_info_get_display_name (info);
8305 add_custom_button_to_dialog (GtkDialog *dialog,
8306 const gchar *mnemonic_label,
8307 const gchar *stock_id,
8312 button = gtk_button_new_with_mnemonic (mnemonic_label);
8313 gtk_widget_set_can_default (button, TRUE);
8314 gtk_button_set_image (GTK_BUTTON (button),
8315 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8316 gtk_widget_show (button);
8318 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8321 /* Presents an overwrite confirmation dialog; returns whether we should accept
8325 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8326 const gchar *file_part,
8327 const gchar *folder_display_name)
8329 GtkWindow *toplevel;
8333 toplevel = get_toplevel (GTK_WIDGET (impl));
8335 dialog = gtk_message_dialog_new (toplevel,
8336 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8337 GTK_MESSAGE_QUESTION,
8339 _("A file named \"%s\" already exists. Do you want to replace it?"),
8341 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8342 _("The file already exists in \"%s\". Replacing it will "
8343 "overwrite its contents."),
8344 folder_display_name);
8346 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8347 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8348 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8349 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8350 GTK_RESPONSE_ACCEPT,
8351 GTK_RESPONSE_CANCEL,
8353 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8355 if (gtk_window_has_group (toplevel))
8356 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8357 GTK_WINDOW (dialog));
8359 response = gtk_dialog_run (GTK_DIALOG (dialog));
8361 gtk_widget_destroy (dialog);
8363 return (response == GTK_RESPONSE_ACCEPT);
8366 struct GetDisplayNameData
8368 GtkFileChooserDefault *impl;
8372 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8373 * as requesting a response means, "the dialog is confirmed".
8376 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8378 g_signal_emit_by_name (impl, "response-requested");
8379 add_selection_to_recent_list (impl);
8383 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8385 const GError *error,
8388 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8389 gboolean should_respond = FALSE;
8390 struct GetDisplayNameData *data = user_data;
8392 if (cancellable != data->impl->should_respond_get_info_cancellable)
8395 data->impl->should_respond_get_info_cancellable = NULL;
8401 /* Huh? Did the folder disappear? Let the caller deal with it */
8402 should_respond = TRUE;
8404 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8406 set_busy_cursor (data->impl, FALSE);
8408 request_response_and_add_to_recent_list (data->impl);
8411 g_object_unref (data->impl);
8412 g_free (data->file_part);
8415 g_object_unref (cancellable);
8418 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8419 * should respond. Can get the file part from the file list or the save entry.
8422 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8423 const gchar *file_part,
8426 GtkFileChooserConfirmation conf;
8428 if (!impl->do_overwrite_confirmation)
8431 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8433 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8437 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8439 struct GetDisplayNameData *data;
8441 g_assert (file_part != NULL);
8443 data = g_new0 (struct GetDisplayNameData, 1);
8444 data->impl = g_object_ref (impl);
8445 data->file_part = g_strdup (file_part);
8447 if (impl->should_respond_get_info_cancellable)
8448 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8450 impl->should_respond_get_info_cancellable =
8451 _gtk_file_system_get_info (impl->file_system, parent_file,
8452 "standard::display-name",
8453 confirmation_confirm_get_info_cb,
8455 set_busy_cursor (data->impl, TRUE);
8459 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8462 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8466 g_assert_not_reached ();
8471 struct FileExistsData
8473 GtkFileChooserDefault *impl;
8474 gboolean file_exists_and_is_not_folder;
8480 name_entry_get_parent_info_cb (GCancellable *cancellable,
8482 const GError *error,
8485 gboolean parent_is_folder;
8486 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8487 struct FileExistsData *data = user_data;
8489 if (cancellable != data->impl->should_respond_get_info_cancellable)
8492 data->impl->should_respond_get_info_cancellable = NULL;
8494 set_busy_cursor (data->impl, FALSE);
8500 parent_is_folder = FALSE;
8502 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8504 if (parent_is_folder)
8506 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8508 request_response_and_add_to_recent_list (data->impl); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8510 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8512 if (data->file_exists_and_is_not_folder)
8517 /* Dup the string because the string may be modified
8518 * depending on what clients do in the confirm-overwrite
8519 * signal and this corrupts the pointer
8521 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8522 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8526 request_response_and_add_to_recent_list (data->impl);
8529 request_response_and_add_to_recent_list (data->impl);
8531 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8532 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8534 GError *mkdir_error = NULL;
8536 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8537 * "/blah/nonexistent" you *will* want a folder created.
8540 set_busy_cursor (data->impl, TRUE);
8541 g_file_make_directory (data->file, NULL, &mkdir_error);
8542 set_busy_cursor (data->impl, FALSE);
8545 request_response_and_add_to_recent_list (data->impl);
8547 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8550 g_assert_not_reached ();
8556 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8557 error_with_file_under_nonfolder (data->impl, data->parent_file);
8563 /* The parent folder is not readable for some reason */
8565 error_copy = g_error_copy (error);
8566 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8571 g_object_unref (data->impl);
8572 g_object_unref (data->file);
8573 g_object_unref (data->parent_file);
8576 g_object_unref (cancellable);
8580 file_exists_get_info_cb (GCancellable *cancellable,
8582 const GError *error,
8585 gboolean data_ownership_taken = FALSE;
8586 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8587 gboolean file_exists;
8589 gboolean needs_parent_check = FALSE;
8590 struct FileExistsData *data = user_data;
8592 if (cancellable != data->impl->file_exists_get_info_cancellable)
8595 data->impl->file_exists_get_info_cancellable = NULL;
8597 set_busy_cursor (data->impl, FALSE);
8602 file_exists = (info != NULL);
8603 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8605 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8608 change_folder_and_display_error (data->impl, data->file, TRUE);
8612 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8614 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8617 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8619 if (file_exists && !is_folder)
8621 /* Oops, the user typed the name of an existing path which is not
8624 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8625 g_error_copy (error));
8629 needs_parent_check = TRUE;
8632 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8636 needs_parent_check = TRUE;
8642 /* User typed a folder; we are done */
8643 request_response_and_add_to_recent_list (data->impl);
8646 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8649 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8652 change_folder_and_display_error (data->impl, data->file, TRUE);
8654 needs_parent_check = TRUE;
8658 g_assert_not_reached();
8661 if (needs_parent_check) {
8662 /* check that everything up to the last path component exists (i.e. the parent) */
8664 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8665 data_ownership_taken = TRUE;
8667 if (data->impl->should_respond_get_info_cancellable)
8668 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8670 data->impl->should_respond_get_info_cancellable =
8671 _gtk_file_system_get_info (data->impl->file_system,
8674 name_entry_get_parent_info_cb,
8676 set_busy_cursor (data->impl, TRUE);
8680 if (!data_ownership_taken)
8682 g_object_unref (data->impl);
8683 g_object_unref (data->file);
8684 g_object_unref (data->parent_file);
8688 g_object_unref (cancellable);
8692 paste_text_received (GtkClipboard *clipboard,
8694 GtkFileChooserDefault *impl)
8701 file = g_file_new_for_uri (text);
8703 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8704 location_popup_handler (impl, text);
8706 g_object_unref (file);
8709 /* Handler for the "location-popup-on-paste" keybinding signal */
8711 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8713 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8714 GDK_SELECTION_CLIPBOARD);
8715 gtk_clipboard_request_text (clipboard,
8716 (GtkClipboardTextReceivedFunc) paste_text_received,
8720 /* Implementation for GtkFileChooserEmbed::should_respond() */
8722 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8727 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8729 for (l = files; l; l = l->next)
8731 GFile *file = l->data;
8734 uri = g_file_get_uri (file);
8737 gtk_recent_manager_add_item (impl->recent_manager, uri);
8742 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8743 g_slist_free (files);
8747 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8749 GtkFileChooserDefault *impl;
8750 GtkWidget *toplevel;
8751 GtkWidget *current_focus;
8754 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8756 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8757 g_assert (GTK_IS_WINDOW (toplevel));
8761 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8763 if (current_focus == impl->browse_files_tree_view)
8765 /* The following array encodes what we do based on the impl->action and the
8766 * number of files selected.
8769 NOOP, /* Do nothing (don't respond) */
8770 RESPOND, /* Respond immediately */
8771 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8772 ALL_FILES, /* Respond only if everything selected is a file */
8773 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8774 SAVE_ENTRY, /* Go to the code for handling the save entry */
8775 NOT_REACHED /* Sanity check */
8777 static const ActionToTake what_to_do[4][3] = {
8778 /* 0 selected 1 selected many selected */
8779 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8780 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8781 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8782 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8786 gboolean all_files, all_folders;
8788 ActionToTake action;
8792 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8794 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8796 retval = search_should_respond (impl);
8800 if (impl->operation_mode == OPERATION_MODE_RECENT)
8802 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8806 retval = recent_should_respond (impl);
8811 selection_check (impl, &num_selected, &all_files, &all_folders);
8813 if (num_selected > 2)
8818 action = what_to_do [impl->action] [k];
8829 case RESPOND_OR_SWITCH:
8830 g_assert (num_selected == 1);
8834 switch_to_selected_folder (impl);
8837 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8839 retval = should_respond_after_confirm_overwrite (impl,
8840 get_display_name_from_file_list (impl),
8841 impl->current_folder);
8855 retval = all_folders;
8862 g_assert_not_reached ();
8865 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8868 gboolean is_well_formed, is_empty, is_file_part_empty;
8870 GtkFileChooserEntry *entry;
8875 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8876 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8877 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8878 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8879 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8881 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8882 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8884 if (!is_well_formed)
8887 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8888 && impl->operation_mode == OPERATION_MODE_RECENT)
8890 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8892 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8893 * thus changing our assumption that no selection is present - setting
8894 * a selection causes the error message from path_bar_set_mode() to go away,
8895 * but we want the user to see that message!
8897 gtk_widget_grab_focus (impl->browse_files_tree_view);
8900 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8907 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8908 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8910 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8911 gtk_widget_grab_focus (impl->location_entry);
8918 g_assert (file != NULL);
8923 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8924 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8926 change_folder_and_display_error (impl, file, TRUE);
8928 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8929 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8931 /* The folder already exists, so we do not need to create it.
8932 * Just respond to terminate the dialog.
8938 g_assert_not_reached ();
8943 struct FileExistsData *data;
8945 /* We need to check whether file exists and whether it is a folder -
8946 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8947 * negative (it doesn't know yet if your last path component is a
8951 data = g_new0 (struct FileExistsData, 1);
8952 data->impl = g_object_ref (impl);
8953 data->file = g_object_ref (file);
8954 data->parent_file = _gtk_file_chooser_entry_get_current_folder (entry);
8956 if (impl->file_exists_get_info_cancellable)
8957 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8959 impl->file_exists_get_info_cancellable =
8960 _gtk_file_system_get_info (impl->file_system, file,
8962 file_exists_get_info_cb,
8965 set_busy_cursor (impl, TRUE);
8968 g_error_free (error);
8971 g_object_unref (file);
8973 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8975 /* The focus is on a dialog's action area button, *and* the widget that
8976 * was focused immediately before it is the file list.
8980 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8982 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8985 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8987 /* The focus is on a dialog's action area button, *and* the widget that
8988 * was focused immediately before it is the location entry.
8993 /* The focus is on a dialog's action area button or something else */
8994 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8995 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9003 add_selection_to_recent_list (impl);
9008 /* Implementation for GtkFileChooserEmbed::initial_focus() */
9010 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
9012 GtkFileChooserDefault *impl;
9015 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
9017 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9018 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9020 if (impl->location_mode == LOCATION_MODE_PATH_BAR
9021 || impl->operation_mode == OPERATION_MODE_RECENT)
9022 widget = impl->browse_files_tree_view;
9024 widget = impl->location_entry;
9026 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
9027 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9028 widget = impl->location_entry;
9031 g_assert_not_reached ();
9035 g_assert (widget != NULL);
9036 gtk_widget_grab_focus (widget);
9039 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
9041 search_selected_foreach_get_file_cb (GtkTreeModel *model,
9051 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9052 *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 */
9055 /* Constructs a list of the selected paths in search mode */
9057 search_get_selected_files (GtkFileChooserDefault *impl)
9060 GtkTreeSelection *selection;
9064 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9065 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9066 result = g_slist_reverse (result);
9071 /* Called from ::should_respond(). We return whether there are selected files
9072 * in the search list.
9075 search_should_respond (GtkFileChooserDefault *impl)
9077 GtkTreeSelection *selection;
9079 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9081 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9082 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9085 /* Adds one hit from the search engine to the search_model */
9087 search_add_hit (GtkFileChooserDefault *impl,
9092 file = g_file_new_for_uri (uri);
9096 if (!g_file_is_native (file))
9098 g_object_unref (file);
9102 _gtk_file_system_model_add_and_query_file (impl->search_model,
9106 g_object_unref (file);
9109 /* Callback used from GtkSearchEngine when we get new hits */
9111 search_engine_hits_added_cb (GtkSearchEngine *engine,
9115 GtkFileChooserDefault *impl;
9118 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9120 for (l = hits; l; l = l->next)
9121 search_add_hit (impl, (gchar*)l->data);
9124 /* Callback used from GtkSearchEngine when the query is done running */
9126 search_engine_finished_cb (GtkSearchEngine *engine,
9129 GtkFileChooserDefault *impl;
9131 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9134 /* EB: setting the model here will avoid loads of row events,
9135 * but it'll make the search look like blocked.
9137 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9138 GTK_TREE_MODEL (impl->search_model));
9139 file_list_set_sort_column_ids (impl);
9142 /* FMQ: if search was empty, say that we got no hits */
9143 set_busy_cursor (impl, FALSE);
9146 /* Displays a generic error when we cannot create a GtkSearchEngine.
9147 * It would be better if _gtk_search_engine_new() gave us a GError
9148 * with a better message, but it doesn't do that right now.
9151 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9153 error_message (impl,
9154 _("Could not start the search process"),
9155 _("The program was not able to create a connection to the indexer "
9156 "daemon. Please make sure it is running."));
9160 search_engine_error_cb (GtkSearchEngine *engine,
9161 const gchar *message,
9164 GtkFileChooserDefault *impl;
9166 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9168 search_stop_searching (impl, TRUE);
9169 error_message (impl, _("Could not send the search request"), message);
9171 set_busy_cursor (impl, FALSE);
9174 /* Frees the data in the search_model */
9176 search_clear_model (GtkFileChooserDefault *impl,
9177 gboolean remove_from_treeview)
9179 if (!impl->search_model)
9182 g_object_unref (impl->search_model);
9183 impl->search_model = NULL;
9185 if (remove_from_treeview)
9186 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9189 /* Stops any ongoing searches; does not touch the search_model */
9191 search_stop_searching (GtkFileChooserDefault *impl,
9192 gboolean remove_query)
9194 if (remove_query && impl->search_query)
9196 g_object_unref (impl->search_query);
9197 impl->search_query = NULL;
9200 if (impl->search_engine)
9202 _gtk_search_engine_stop (impl->search_engine);
9204 g_object_unref (impl->search_engine);
9205 impl->search_engine = NULL;
9209 /* Creates the search_model and puts it in the tree view */
9211 search_setup_model (GtkFileChooserDefault *impl)
9213 g_assert (impl->search_model == NULL);
9215 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9217 MODEL_COLUMN_TYPES);
9219 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9223 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9227 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9231 set_sort_column (impl);
9233 /* EB: setting the model here will make the hits list update feel
9234 * more "alive" than setting the model at the end of the search
9237 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9238 GTK_TREE_MODEL (impl->search_model));
9239 file_list_set_sort_column_ids (impl);
9242 /* Creates a new query with the specified text and launches it */
9244 search_start_query (GtkFileChooserDefault *impl,
9245 const gchar *query_text)
9247 search_stop_searching (impl, FALSE);
9248 search_clear_model (impl, TRUE);
9249 search_setup_model (impl);
9250 set_busy_cursor (impl, TRUE);
9252 if (impl->search_engine == NULL)
9253 impl->search_engine = _gtk_search_engine_new ();
9255 if (!impl->search_engine)
9257 set_busy_cursor (impl, FALSE);
9258 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9262 if (!impl->search_query)
9264 impl->search_query = _gtk_query_new ();
9265 _gtk_query_set_text (impl->search_query, query_text);
9268 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9270 g_signal_connect (impl->search_engine, "hits-added",
9271 G_CALLBACK (search_engine_hits_added_cb), impl);
9272 g_signal_connect (impl->search_engine, "finished",
9273 G_CALLBACK (search_engine_finished_cb), impl);
9274 g_signal_connect (impl->search_engine, "error",
9275 G_CALLBACK (search_engine_error_cb), impl);
9277 _gtk_search_engine_start (impl->search_engine);
9280 /* Callback used when the user presses Enter while typing on the search
9281 * entry; starts the query
9284 search_entry_activate_cb (GtkEntry *entry,
9287 GtkFileChooserDefault *impl;
9290 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9292 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9293 if (strlen (text) == 0)
9296 /* reset any existing query object */
9297 if (impl->search_query)
9299 g_object_unref (impl->search_query);
9300 impl->search_query = NULL;
9303 search_start_query (impl, text);
9307 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9309 GDK_THREADS_ENTER ();
9311 g_source_destroy (impl->focus_entry_idle);
9312 impl->focus_entry_idle = NULL;
9314 if (impl->search_entry)
9315 gtk_widget_grab_focus (impl->search_entry);
9317 GDK_THREADS_LEAVE ();
9323 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9325 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9326 * pane, we get a selection-changed signal and we set up the search widgets.
9327 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9328 * the change to the selection. So, we need to re-focus the search entry
9329 * after the treeview has finished doing its work; we'll do that in an idle
9333 if (!impl->focus_entry_idle)
9334 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9337 /* Hides the path bar and creates the search entry */
9339 search_setup_widgets (GtkFileChooserDefault *impl)
9341 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9343 path_bar_update (impl);
9345 impl->search_entry = gtk_entry_new ();
9346 g_signal_connect (impl->search_entry, "activate",
9347 G_CALLBACK (search_entry_activate_cb),
9349 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9351 /* if there already is a query, restart it */
9352 if (impl->search_query)
9354 gchar *query = _gtk_query_get_text (impl->search_query);
9358 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9359 search_start_query (impl, query);
9365 g_object_unref (impl->search_query);
9366 impl->search_query = NULL;
9370 /* Box for search widgets */
9371 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9372 gtk_widget_show_all (impl->search_hbox);
9373 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9375 /* Hide the location widgets temporarily */
9377 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9378 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9380 gtk_widget_hide (impl->location_button);
9381 gtk_widget_hide (impl->location_entry_box);
9384 focus_search_entry_in_idle (impl);
9386 /* FMQ: hide the filter combo? */
9390 * Recent files support
9393 /* Frees the data in the recent_model */
9395 recent_clear_model (GtkFileChooserDefault *impl,
9396 gboolean remove_from_treeview)
9398 if (!impl->recent_model)
9401 if (remove_from_treeview)
9402 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9404 g_object_unref (impl->recent_model);
9405 impl->recent_model = NULL;
9408 /* Stops any ongoing loading of the recent files list; does
9409 * not touch the recent_model
9412 recent_stop_loading (GtkFileChooserDefault *impl)
9414 if (impl->load_recent_id)
9416 g_source_remove (impl->load_recent_id);
9417 impl->load_recent_id = 0;
9422 recent_setup_model (GtkFileChooserDefault *impl)
9424 g_assert (impl->recent_model == NULL);
9426 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9428 MODEL_COLUMN_TYPES);
9430 _gtk_file_system_model_set_filter (impl->recent_model,
9431 impl->current_filter);
9432 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9436 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9440 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9444 set_sort_column (impl);
9449 GtkFileChooserDefault *impl;
9454 recent_idle_cleanup (gpointer data)
9456 RecentLoadData *load_data = data;
9457 GtkFileChooserDefault *impl = load_data->impl;
9459 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9460 GTK_TREE_MODEL (impl->recent_model));
9461 file_list_set_sort_column_ids (impl);
9462 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9464 set_busy_cursor (impl, FALSE);
9466 impl->load_recent_id = 0;
9472 get_recent_files_limit (GtkWidget *widget)
9474 GtkSettings *settings;
9477 if (gtk_widget_has_screen (widget))
9478 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9480 settings = gtk_settings_get_default ();
9482 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9487 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9489 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9495 limit = get_recent_files_limit (GTK_WIDGET (impl));
9499 for (l = items; l; l = l->next)
9501 GtkRecentInfo *info = l->data;
9504 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9505 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9508 g_object_unref (file);
9511 if (limit != -1 && n >= limit)
9517 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9522 folders = _gtk_file_chooser_extract_recent_folders (items);
9524 for (l = folders; l; l = l->next)
9526 GFile *folder = l->data;
9528 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9533 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9534 g_list_free (folders);
9538 recent_idle_load (gpointer data)
9540 RecentLoadData *load_data = data;
9541 GtkFileChooserDefault *impl = load_data->impl;
9543 if (!impl->recent_manager)
9546 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9547 if (!load_data->items)
9550 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9551 populate_model_with_recent_items (impl, load_data->items);
9553 populate_model_with_folders (impl, load_data->items);
9555 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9556 g_list_free (load_data->items);
9557 load_data->items = NULL;
9563 recent_start_loading (GtkFileChooserDefault *impl)
9565 RecentLoadData *load_data;
9567 recent_stop_loading (impl);
9568 recent_clear_model (impl, TRUE);
9569 recent_setup_model (impl);
9570 set_busy_cursor (impl, TRUE);
9572 g_assert (impl->load_recent_id == 0);
9574 load_data = g_new (RecentLoadData, 1);
9575 load_data->impl = impl;
9576 load_data->items = NULL;
9578 /* begin lazy loading the recent files into the model */
9579 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9582 recent_idle_cleanup);
9586 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9596 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9597 *list = g_slist_prepend (*list, file);
9600 /* Constructs a list of the selected paths in recent files mode */
9602 recent_get_selected_files (GtkFileChooserDefault *impl)
9605 GtkTreeSelection *selection;
9609 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9610 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9611 result = g_slist_reverse (result);
9616 /* Called from ::should_respond(). We return whether there are selected
9617 * files in the recent files list.
9620 recent_should_respond (GtkFileChooserDefault *impl)
9622 GtkTreeSelection *selection;
9624 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9626 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9627 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9631 set_current_filter (GtkFileChooserDefault *impl,
9632 GtkFileFilter *filter)
9634 if (impl->current_filter != filter)
9638 /* NULL filters are allowed to reset to non-filtered status
9640 filter_index = g_slist_index (impl->filters, filter);
9641 if (impl->filters && filter && filter_index < 0)
9644 if (impl->current_filter)
9645 g_object_unref (impl->current_filter);
9646 impl->current_filter = filter;
9647 if (impl->current_filter)
9649 g_object_ref_sink (impl->current_filter);
9653 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9656 if (impl->browse_files_model)
9657 install_list_model_filter (impl);
9659 if (impl->search_model)
9660 _gtk_file_system_model_set_filter (impl->search_model, filter);
9662 if (impl->recent_model)
9663 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9665 g_object_notify (G_OBJECT (impl), "filter");
9670 filter_combo_changed (GtkComboBox *combo_box,
9671 GtkFileChooserDefault *impl)
9673 gint new_index = gtk_combo_box_get_active (combo_box);
9674 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9676 set_current_filter (impl, new_filter);
9680 check_preview_change (GtkFileChooserDefault *impl)
9682 GtkTreePath *cursor_path;
9684 char *new_display_name;
9685 GtkTreeModel *model;
9687 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9688 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9693 gtk_tree_model_get_iter (model, &iter, cursor_path);
9694 gtk_tree_model_get (model, &iter,
9695 MODEL_COL_FILE, &new_file,
9696 MODEL_COL_NAME, &new_display_name,
9699 gtk_tree_path_free (cursor_path);
9704 new_display_name = NULL;
9707 if (new_file != impl->preview_file &&
9708 !(new_file && impl->preview_file &&
9709 g_file_equal (new_file, impl->preview_file)))
9711 if (impl->preview_file)
9713 g_object_unref (impl->preview_file);
9714 g_free (impl->preview_display_name);
9719 impl->preview_file = new_file;
9720 impl->preview_display_name = new_display_name;
9724 impl->preview_file = NULL;
9725 impl->preview_display_name = NULL;
9726 g_free (new_display_name);
9729 if (impl->use_preview_label && impl->preview_label)
9730 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9732 g_signal_emit_by_name (impl, "update-preview");
9737 g_object_unref (new_file);
9739 g_free (new_display_name);
9744 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9745 GtkFileSystemVolume *volume,
9746 const GError *error,
9750 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9751 GtkFileChooserDefault *impl = data;
9753 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9756 impl->shortcuts_activate_iter_cancellable = NULL;
9758 set_busy_cursor (impl, FALSE);
9765 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9769 name = _gtk_file_system_volume_get_display_name (volume);
9770 msg = g_strdup_printf (_("Could not mount %s"), name);
9772 error_message (impl, msg, error->message);
9781 file = _gtk_file_system_volume_get_root (volume);
9784 change_folder_and_display_error (impl, file, FALSE);
9785 g_object_unref (file);
9789 g_object_unref (impl);
9790 g_object_unref (cancellable);
9794 /* Activates a volume by mounting it if necessary and then switching to its
9798 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9799 GtkFileSystemVolume *volume)
9803 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9805 /* We ref the file chooser since volume_mount() may run a main loop, and the
9806 * user could close the file chooser window in the meantime.
9808 g_object_ref (impl);
9810 if (!_gtk_file_system_volume_is_mounted (volume))
9812 GMountOperation *mount_op;
9814 set_busy_cursor (impl, TRUE);
9816 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9817 impl->shortcuts_activate_iter_cancellable =
9818 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9819 shortcuts_activate_volume_mount_cb,
9820 g_object_ref (impl));
9821 g_object_unref (mount_op);
9825 file = _gtk_file_system_volume_get_root (volume);
9828 change_folder_and_display_error (impl, file, FALSE);
9829 g_object_unref (file);
9833 g_object_unref (impl);
9836 /* Opens the folder or volume at the specified iter in the shortcuts model */
9837 struct ShortcutsActivateData
9839 GtkFileChooserDefault *impl;
9844 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9846 const GError *error,
9849 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9850 struct ShortcutsActivateData *data = user_data;
9852 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9855 data->impl->shortcuts_activate_iter_cancellable = NULL;
9860 if (!error && _gtk_file_info_consider_as_directory (info))
9861 change_folder_and_display_error (data->impl, data->file, FALSE);
9863 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9868 g_object_unref (data->impl);
9869 g_object_unref (data->file);
9872 g_object_unref (cancellable);
9876 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9877 GtkFileSystemVolume *volume,
9878 const GError *error,
9881 struct ShortcutsActivateData *data = user_data;
9885 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9887 g_object_unref (data->impl);
9888 g_object_unref (data->file);
9894 data->impl->shortcuts_activate_iter_cancellable =
9895 _gtk_file_system_get_info (data->impl->file_system, data->file,
9897 shortcuts_activate_get_info_cb, data);
9900 _gtk_file_system_volume_unref (volume);
9904 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9908 ShortcutType shortcut_type;
9910 /* In the Save modes, we want to preserve what the uesr typed in the filename
9911 * entry, so that he may choose another folder without erasing his typed name.
9913 if (impl->location_entry
9914 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9915 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9916 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
9918 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9919 SHORTCUTS_COL_DATA, &col_data,
9920 SHORTCUTS_COL_TYPE, &shortcut_type,
9923 if (impl->shortcuts_activate_iter_cancellable)
9925 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9926 impl->shortcuts_activate_iter_cancellable = NULL;
9929 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9931 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9933 GtkFileSystemVolume *volume;
9937 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9939 shortcuts_activate_volume (impl, volume);
9941 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9943 struct ShortcutsActivateData *data;
9944 GtkFileSystemVolume *volume;
9946 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9948 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9950 data = g_new0 (struct ShortcutsActivateData, 1);
9951 data->impl = g_object_ref (impl);
9952 data->file = g_object_ref (col_data);
9954 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9956 GMountOperation *mount_operation;
9957 GtkWidget *toplevel;
9959 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9961 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9963 impl->shortcuts_activate_iter_cancellable =
9964 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9966 shortcuts_activate_mount_enclosing_volume,
9971 impl->shortcuts_activate_iter_cancellable =
9972 _gtk_file_system_get_info (impl->file_system, data->file,
9974 shortcuts_activate_get_info_cb, data);
9977 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9979 operation_mode_set (impl, OPERATION_MODE_SEARCH);
9981 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9983 operation_mode_set (impl, OPERATION_MODE_RECENT);
9987 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9989 shortcuts_key_press_event_cb (GtkWidget *widget,
9991 GtkFileChooserDefault *impl)
9995 modifiers = gtk_accelerator_get_default_mod_mask ();
9997 if (key_is_left_or_right (event))
9999 gtk_widget_grab_focus (impl->browse_files_tree_view);
10003 if ((event->keyval == GDK_KEY_BackSpace
10004 || event->keyval == GDK_KEY_Delete
10005 || event->keyval == GDK_KEY_KP_Delete)
10006 && (event->state & modifiers) == 0)
10008 remove_selected_bookmarks (impl);
10012 if ((event->keyval == GDK_KEY_F2)
10013 && (event->state & modifiers) == 0)
10015 rename_selected_bookmark (impl);
10023 shortcuts_select_func (GtkTreeSelection *selection,
10024 GtkTreeModel *model,
10026 gboolean path_currently_selected,
10029 GtkFileChooserDefault *impl = data;
10030 GtkTreeIter filter_iter;
10031 ShortcutType shortcut_type;
10033 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10034 g_assert_not_reached ();
10036 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10038 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10042 list_select_func (GtkTreeSelection *selection,
10043 GtkTreeModel *model,
10045 gboolean path_currently_selected,
10048 GtkFileChooserDefault *impl = data;
10050 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10051 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10054 gboolean is_folder;
10056 if (!gtk_tree_model_get_iter (model, &iter, path))
10058 gtk_tree_model_get (model, &iter,
10059 MODEL_COL_IS_FOLDER, &is_folder,
10069 list_selection_changed (GtkTreeSelection *selection,
10070 GtkFileChooserDefault *impl)
10072 /* See if we are in the new folder editable row for Save mode */
10073 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10074 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10077 gboolean had_selection;
10079 info = get_selected_file_info_from_file_list (impl, &had_selection);
10080 if (!had_selection)
10081 goto out; /* normal processing */
10084 return; /* We are on the editable row for New Folder */
10089 if (impl->location_entry)
10090 update_chooser_entry (impl);
10092 path_bar_update (impl);
10094 check_preview_change (impl);
10095 bookmarks_check_add_sensitivity (impl);
10096 check_copy_file_location_sensitivity (impl);
10098 g_signal_emit_by_name (impl, "selection-changed", 0);
10101 /* Callback used when a row in the file list is activated */
10103 list_row_activated (GtkTreeView *tree_view,
10105 GtkTreeViewColumn *column,
10106 GtkFileChooserDefault *impl)
10110 GtkTreeModel *model;
10111 gboolean is_folder;
10113 model = gtk_tree_view_get_model (tree_view);
10115 if (!gtk_tree_model_get_iter (model, &iter, path))
10118 gtk_tree_model_get (model, &iter,
10119 MODEL_COL_FILE, &file,
10120 MODEL_COL_IS_FOLDER, &is_folder,
10123 if (is_folder && file)
10125 change_folder_and_display_error (impl, file, FALSE);
10129 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10130 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10131 g_signal_emit_by_name (impl, "file-activated");
10136 g_object_unref (file);
10140 path_bar_clicked (GtkPathBar *path_bar,
10143 gboolean child_is_hidden,
10144 GtkFileChooserDefault *impl)
10147 pending_select_files_add (impl, child_file);
10149 if (!change_folder_and_display_error (impl, file, FALSE))
10152 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10153 * show hidden files so that ".baz" appears in the file list, as it will still
10154 * be shown in the path bar: "/foo/[bar]/.baz"
10156 if (child_is_hidden)
10157 g_object_set (impl, "show-hidden", TRUE, NULL);
10161 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10163 GtkTreeViewColumn *column;
10164 GtkCellRenderer *renderer;
10165 GList *walk, *list;
10166 gboolean always_sensitive;
10168 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10169 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10171 /* Keep the following column numbers in sync with create_file_list() */
10174 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10175 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10176 for (walk = list; walk; walk = walk->next)
10178 renderer = walk->data;
10179 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10181 gtk_tree_view_column_set_attributes (column, renderer,
10182 "pixbuf", MODEL_COL_PIXBUF,
10187 gtk_tree_view_column_set_attributes (column, renderer,
10188 "text", MODEL_COL_NAME,
10189 "ellipsize", MODEL_COL_ELLIPSIZE,
10192 if (always_sensitive)
10193 g_object_set (renderer, "sensitive", TRUE, NULL);
10195 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10197 g_list_free (list);
10200 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10201 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10202 renderer = list->data;
10203 gtk_tree_view_column_set_attributes (column, renderer,
10204 "text", MODEL_COL_SIZE_TEXT,
10206 if (always_sensitive)
10207 g_object_set (renderer, "sensitive", TRUE, NULL);
10209 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10210 g_list_free (list);
10213 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10214 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10215 renderer = list->data;
10216 gtk_tree_view_column_set_attributes (column, renderer,
10217 "text", MODEL_COL_MTIME_TEXT,
10219 if (always_sensitive)
10220 g_object_set (renderer, "sensitive", TRUE, NULL);
10222 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10223 g_list_free (list);
10227 _gtk_file_chooser_default_new (void)
10229 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10233 location_set_user_text (GtkFileChooserDefault *impl,
10236 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), path);
10237 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10241 location_popup_handler (GtkFileChooserDefault *impl,
10244 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10246 GtkWidget *widget_to_focus;
10248 operation_mode_set (impl, OPERATION_MODE_BROWSE);
10250 if (impl->current_folder)
10251 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10253 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10254 widget_to_focus = impl->browse_files_tree_view;
10256 widget_to_focus = impl->location_entry;
10258 gtk_widget_grab_focus (widget_to_focus);
10262 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10263 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10268 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10269 location_set_user_text (impl, path);
10271 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10272 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10274 gtk_widget_grab_focus (impl->location_entry);
10276 location_set_user_text (impl, path);
10279 g_assert_not_reached ();
10282 /* Handler for the "up-folder" keybinding signal */
10284 up_folder_handler (GtkFileChooserDefault *impl)
10286 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10289 /* Handler for the "down-folder" keybinding signal */
10291 down_folder_handler (GtkFileChooserDefault *impl)
10293 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10296 /* Switches to the shortcut in the specified index */
10298 switch_to_shortcut (GtkFileChooserDefault *impl,
10303 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10304 g_assert_not_reached ();
10306 shortcuts_activate_iter (impl, &iter);
10309 /* Handler for the "home-folder" keybinding signal */
10311 home_folder_handler (GtkFileChooserDefault *impl)
10313 if (impl->has_home)
10314 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10317 /* Handler for the "desktop-folder" keybinding signal */
10319 desktop_folder_handler (GtkFileChooserDefault *impl)
10321 if (impl->has_desktop)
10322 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10325 /* Handler for the "search-shortcut" keybinding signal */
10327 search_shortcut_handler (GtkFileChooserDefault *impl)
10329 if (impl->has_search)
10331 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10333 /* we want the entry widget to grab the focus the first
10334 * time, not the browse_files_tree_view widget.
10336 if (impl->search_entry)
10337 gtk_widget_grab_focus (impl->search_entry);
10341 /* Handler for the "recent-shortcut" keybinding signal */
10343 recent_shortcut_handler (GtkFileChooserDefault *impl)
10345 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10349 quick_bookmark_handler (GtkFileChooserDefault *impl,
10350 gint bookmark_index)
10355 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10358 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10360 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10361 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10364 gtk_tree_path_free (path);
10366 switch_to_shortcut (impl, bookmark_pos);
10370 show_hidden_handler (GtkFileChooserDefault *impl)
10372 g_object_set (impl,
10373 "show-hidden", !impl->show_hidden,
10378 /* Drag and drop interfaces */
10381 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10386 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10388 model->impl = NULL;
10391 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10393 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10396 ShortcutsPaneModelFilter *model;
10400 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10402 pos = *gtk_tree_path_get_indices (path);
10403 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10405 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10408 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10412 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10414 GtkSelectionData *selection_data)
10421 /* Fill the GtkTreeDragSourceIface vtable */
10423 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10425 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10426 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10430 /* Fill the GtkTreeDragDestIface vtable */
10432 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10434 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10435 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10439 static GtkTreeModel *
10440 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10441 GtkTreeModel *child_model,
10444 ShortcutsPaneModelFilter *model;
10446 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10447 "child-model", child_model,
10448 "virtual-root", root,
10451 model->impl = impl;
10453 return GTK_TREE_MODEL (model);