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"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkradiobutton.h"
56 #include "gtkrecentfilter.h"
57 #include "gtkrecentmanager.h"
58 #include "gtkscrolledwindow.h"
59 #include "gtkseparatormenuitem.h"
60 #include "gtksettings.h"
61 #include "gtksizegroup.h"
62 #include "gtksizerequest.h"
64 #include "gtktoolbar.h"
65 #include "gtktoolbutton.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
71 #include "gtkorientable.h"
78 #include <sys/types.h>
88 /* Values for GtkSelection-related "info" fields */
89 #define SELECTION_TEXT 0
90 #define SELECTION_URI 1
93 #undef PROFILE_FILE_CHOOSER
94 #ifdef PROFILE_FILE_CHOOSER
101 #define PROFILE_INDENT 4
103 static int profile_indent;
106 profile_add_indent (int indent)
108 profile_indent += indent;
109 if (profile_indent < 0)
110 g_error ("You screwed up your indentation");
114 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
119 profile_add_indent (indent);
121 if (profile_indent == 0)
122 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
124 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
130 profile_add_indent (indent);
133 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
134 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
135 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
137 #define profile_start(x, y)
138 #define profile_end(x, y)
139 #define profile_msg(x, y)
144 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
146 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
147 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
148 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
150 #define MAX_LOADING_TIME 500
152 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
154 struct _GtkFileChooserDefaultClass
156 GtkBoxClass parent_class;
162 LOCATION_POPUP_ON_PASTE,
168 LOCATION_TOGGLE_POPUP,
176 static guint signals[LAST_SIGNAL] = { 0 };
178 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
180 SHORTCUTS_COL_PIXBUF,
184 SHORTCUTS_COL_REMOVABLE,
185 SHORTCUTS_COL_PIXBUF_VISIBLE,
186 SHORTCUTS_COL_CANCELLABLE,
187 SHORTCUTS_COL_NUM_COLUMNS
192 SHORTCUT_TYPE_VOLUME,
193 SHORTCUT_TYPE_SEPARATOR,
194 SHORTCUT_TYPE_SEARCH,
198 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
199 "standard::is-hidden,standard::is-backup,standard::size," \
200 "standard::content-type,time::modified"
202 /* the first 3 must be these due to settings caching sort column */
207 MODEL_COL_NAME_COLLATED,
209 MODEL_COL_IS_SENSITIVE,
212 MODEL_COL_MTIME_TEXT,
214 MODEL_COL_NUM_COLUMNS
217 /* This list of types is passed to _gtk_file_system_model_new*() */
218 #define MODEL_COLUMN_TYPES \
219 MODEL_COL_NUM_COLUMNS, \
220 G_TYPE_STRING, /* MODEL_COL_NAME */ \
221 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
222 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
223 G_TYPE_FILE, /* MODEL_COL_FILE */ \
224 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
225 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
226 G_TYPE_BOOLEAN, /* MODEL_COL_IS_SENSITIVE */ \
227 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
228 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
229 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
230 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
232 /* Identifiers for target types */
237 /* Interesting places in the shortcuts bar */
241 SHORTCUTS_RECENT_SEPARATOR,
246 SHORTCUTS_BOOKMARKS_SEPARATOR,
248 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
249 SHORTCUTS_CURRENT_FOLDER
252 /* Icon size for if we can't get it from the theme */
253 #define FALLBACK_ICON_SIZE 16
255 #define PREVIEW_HBOX_SPACING 12
259 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
260 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
262 static GObject* gtk_file_chooser_default_constructor (GType type,
263 guint n_construct_properties,
264 GObjectConstructParam *construct_params);
265 static void gtk_file_chooser_default_finalize (GObject *object);
266 static void gtk_file_chooser_default_set_property (GObject *object,
270 static void gtk_file_chooser_default_get_property (GObject *object,
274 static void gtk_file_chooser_default_dispose (GObject *object);
275 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
276 static void gtk_file_chooser_default_realize (GtkWidget *widget);
277 static void gtk_file_chooser_default_map (GtkWidget *widget);
278 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
279 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
280 GtkWidget *previous_toplevel);
281 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
282 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
283 GdkScreen *previous_screen);
285 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
288 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
291 gboolean clear_entry,
293 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
294 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
296 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
299 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
301 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
302 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
303 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
304 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
305 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
306 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
307 GtkFileFilter *filter);
308 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
311 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
314 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
317 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
319 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
321 gint *default_height);
322 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
323 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
325 static void add_selection_to_recent_list (GtkFileChooserDefault *impl);
327 static void location_popup_handler (GtkFileChooserDefault *impl,
329 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
330 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
331 static void up_folder_handler (GtkFileChooserDefault *impl);
332 static void down_folder_handler (GtkFileChooserDefault *impl);
333 static void home_folder_handler (GtkFileChooserDefault *impl);
334 static void desktop_folder_handler (GtkFileChooserDefault *impl);
335 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
336 gint bookmark_index);
337 static void show_hidden_handler (GtkFileChooserDefault *impl);
338 static void search_shortcut_handler (GtkFileChooserDefault *impl);
339 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
340 static void update_appearance (GtkFileChooserDefault *impl);
342 static void set_current_filter (GtkFileChooserDefault *impl,
343 GtkFileFilter *filter);
344 static void check_preview_change (GtkFileChooserDefault *impl);
346 static void filter_combo_changed (GtkComboBox *combo_box,
347 GtkFileChooserDefault *impl);
349 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
351 GtkFileChooserDefault *impl);
353 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
356 gboolean path_currently_selected,
358 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
360 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
362 static int shortcuts_get_index (GtkFileChooserDefault *impl,
363 ShortcutsIndex where);
364 static int shortcut_find_position (GtkFileChooserDefault *impl,
367 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
369 static gboolean list_select_func (GtkTreeSelection *selection,
372 gboolean path_currently_selected,
375 static void list_selection_changed (GtkTreeSelection *tree_selection,
376 GtkFileChooserDefault *impl);
377 static void list_row_activated (GtkTreeView *tree_view,
379 GtkTreeViewColumn *column,
380 GtkFileChooserDefault *impl);
382 static void path_bar_clicked (GtkPathBar *path_bar,
385 gboolean child_is_hidden,
386 GtkFileChooserDefault *impl);
388 static void add_bookmark_button_clicked_cb (GtkButton *button,
389 GtkFileChooserDefault *impl);
390 static void remove_bookmark_button_clicked_cb (GtkButton *button,
391 GtkFileChooserDefault *impl);
393 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
395 static void load_remove_timer (GtkFileChooserDefault *impl);
396 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
398 static void location_button_toggled_cb (GtkToggleButton *toggle,
399 GtkFileChooserDefault *impl);
400 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
402 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
403 gboolean remove_from_treeview);
405 static void search_setup_widgets (GtkFileChooserDefault *impl);
406 static void search_stop_searching (GtkFileChooserDefault *impl,
407 gboolean remove_query);
408 static void search_clear_model (GtkFileChooserDefault *impl,
409 gboolean remove_from_treeview);
410 static gboolean search_should_respond (GtkFileChooserDefault *impl);
411 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
412 static void search_entry_activate_cb (GtkEntry *entry,
414 static void settings_load (GtkFileChooserDefault *impl);
416 static void recent_start_loading (GtkFileChooserDefault *impl);
417 static void recent_stop_loading (GtkFileChooserDefault *impl);
418 static void recent_clear_model (GtkFileChooserDefault *impl,
419 gboolean remove_from_treeview);
420 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
421 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
422 static void set_file_system_backend (GtkFileChooserDefault *impl);
423 static void unset_file_system_backend (GtkFileChooserDefault *impl);
429 /* Drag and drop interface declarations */
432 GtkTreeModelFilter parent;
434 GtkFileChooserDefault *impl;
435 } ShortcutsPaneModelFilter;
438 GtkTreeModelFilterClass parent_class;
439 } ShortcutsPaneModelFilterClass;
441 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
442 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
444 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
446 GType _shortcuts_pane_model_filter_get_type (void);
448 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
449 _shortcuts_pane_model_filter,
450 GTK_TYPE_TREE_MODEL_FILTER,
451 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
452 shortcuts_pane_model_filter_drag_source_iface_init))
454 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
455 GtkTreeModel *child_model,
460 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
461 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
462 gtk_file_chooser_default_iface_init)
463 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
464 gtk_file_chooser_embed_default_iface_init));
468 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
470 GdkModifierType modifiers,
471 const gchar *signal_name)
473 gtk_binding_entry_add_signal (binding_set,
477 gtk_binding_entry_add_signal (binding_set,
478 keyval, modifiers | GDK_SHIFT_MASK,
483 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
485 static const guint quick_bookmark_keyvals[10] = {
486 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
488 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
489 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
490 GtkBindingSet *binding_set;
493 gobject_class->finalize = gtk_file_chooser_default_finalize;
494 gobject_class->constructor = gtk_file_chooser_default_constructor;
495 gobject_class->set_property = gtk_file_chooser_default_set_property;
496 gobject_class->get_property = gtk_file_chooser_default_get_property;
497 gobject_class->dispose = gtk_file_chooser_default_dispose;
499 widget_class->show_all = gtk_file_chooser_default_show_all;
500 widget_class->realize = gtk_file_chooser_default_realize;
501 widget_class->map = gtk_file_chooser_default_map;
502 widget_class->unmap = gtk_file_chooser_default_unmap;
503 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
504 widget_class->style_updated = gtk_file_chooser_default_style_updated;
505 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
507 signals[LOCATION_POPUP] =
508 g_signal_new_class_handler (I_("location-popup"),
509 G_OBJECT_CLASS_TYPE (class),
510 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
511 G_CALLBACK (location_popup_handler),
513 _gtk_marshal_VOID__STRING,
514 G_TYPE_NONE, 1, G_TYPE_STRING);
516 signals[LOCATION_POPUP_ON_PASTE] =
517 g_signal_new_class_handler (I_("location-popup-on-paste"),
518 G_OBJECT_CLASS_TYPE (class),
519 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
520 G_CALLBACK (location_popup_on_paste_handler),
522 _gtk_marshal_VOID__VOID,
525 signals[LOCATION_TOGGLE_POPUP] =
526 g_signal_new_class_handler (I_("location-toggle-popup"),
527 G_OBJECT_CLASS_TYPE (class),
528 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
529 G_CALLBACK (location_toggle_popup_handler),
531 _gtk_marshal_VOID__VOID,
535 g_signal_new_class_handler (I_("up-folder"),
536 G_OBJECT_CLASS_TYPE (class),
537 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
538 G_CALLBACK (up_folder_handler),
540 _gtk_marshal_VOID__VOID,
543 signals[DOWN_FOLDER] =
544 g_signal_new_class_handler (I_("down-folder"),
545 G_OBJECT_CLASS_TYPE (class),
546 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
547 G_CALLBACK (down_folder_handler),
549 _gtk_marshal_VOID__VOID,
552 signals[HOME_FOLDER] =
553 g_signal_new_class_handler (I_("home-folder"),
554 G_OBJECT_CLASS_TYPE (class),
555 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
556 G_CALLBACK (home_folder_handler),
558 _gtk_marshal_VOID__VOID,
561 signals[DESKTOP_FOLDER] =
562 g_signal_new_class_handler (I_("desktop-folder"),
563 G_OBJECT_CLASS_TYPE (class),
564 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
565 G_CALLBACK (desktop_folder_handler),
567 _gtk_marshal_VOID__VOID,
570 signals[QUICK_BOOKMARK] =
571 g_signal_new_class_handler (I_("quick-bookmark"),
572 G_OBJECT_CLASS_TYPE (class),
573 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
574 G_CALLBACK (quick_bookmark_handler),
576 _gtk_marshal_VOID__INT,
577 G_TYPE_NONE, 1, G_TYPE_INT);
579 signals[SHOW_HIDDEN] =
580 g_signal_new_class_handler (I_("show-hidden"),
581 G_OBJECT_CLASS_TYPE (class),
582 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
583 G_CALLBACK (show_hidden_handler),
585 _gtk_marshal_VOID__VOID,
588 signals[SEARCH_SHORTCUT] =
589 g_signal_new_class_handler (I_("search-shortcut"),
590 G_OBJECT_CLASS_TYPE (class),
591 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
592 G_CALLBACK (search_shortcut_handler),
594 _gtk_marshal_VOID__VOID,
597 signals[RECENT_SHORTCUT] =
598 g_signal_new_class_handler (I_("recent-shortcut"),
599 G_OBJECT_CLASS_TYPE (class),
600 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
601 G_CALLBACK (recent_shortcut_handler),
603 _gtk_marshal_VOID__VOID,
606 binding_set = gtk_binding_set_by_class (class);
608 gtk_binding_entry_add_signal (binding_set,
609 GDK_KEY_l, GDK_CONTROL_MASK,
610 "location-toggle-popup",
613 gtk_binding_entry_add_signal (binding_set,
616 1, G_TYPE_STRING, "/");
617 gtk_binding_entry_add_signal (binding_set,
618 GDK_KEY_KP_Divide, 0,
620 1, G_TYPE_STRING, "/");
623 gtk_binding_entry_add_signal (binding_set,
624 GDK_KEY_asciitilde, 0,
626 1, G_TYPE_STRING, "~");
629 gtk_binding_entry_add_signal (binding_set,
630 GDK_KEY_v, GDK_CONTROL_MASK,
631 "location-popup-on-paste",
634 add_normal_and_shifted_binding (binding_set,
635 GDK_KEY_Up, GDK_MOD1_MASK,
638 add_normal_and_shifted_binding (binding_set,
639 GDK_KEY_KP_Up, GDK_MOD1_MASK,
642 add_normal_and_shifted_binding (binding_set,
643 GDK_KEY_Down, GDK_MOD1_MASK,
645 add_normal_and_shifted_binding (binding_set,
646 GDK_KEY_KP_Down, GDK_MOD1_MASK,
649 gtk_binding_entry_add_signal (binding_set,
650 GDK_KEY_Home, GDK_MOD1_MASK,
653 gtk_binding_entry_add_signal (binding_set,
654 GDK_KEY_KP_Home, GDK_MOD1_MASK,
657 gtk_binding_entry_add_signal (binding_set,
658 GDK_KEY_d, GDK_MOD1_MASK,
661 gtk_binding_entry_add_signal (binding_set,
662 GDK_KEY_h, GDK_CONTROL_MASK,
665 gtk_binding_entry_add_signal (binding_set,
666 GDK_KEY_s, GDK_MOD1_MASK,
669 gtk_binding_entry_add_signal (binding_set,
670 GDK_KEY_r, GDK_MOD1_MASK,
674 for (i = 0; i < 10; i++)
675 gtk_binding_entry_add_signal (binding_set,
676 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
680 _gtk_file_chooser_install_properties (gobject_class);
684 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
686 iface->select_file = gtk_file_chooser_default_select_file;
687 iface->unselect_file = gtk_file_chooser_default_unselect_file;
688 iface->select_all = gtk_file_chooser_default_select_all;
689 iface->unselect_all = gtk_file_chooser_default_unselect_all;
690 iface->get_files = gtk_file_chooser_default_get_files;
691 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
692 iface->get_file_system = gtk_file_chooser_default_get_file_system;
693 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
694 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
695 iface->set_current_name = gtk_file_chooser_default_set_current_name;
696 iface->add_filter = gtk_file_chooser_default_add_filter;
697 iface->remove_filter = gtk_file_chooser_default_remove_filter;
698 iface->list_filters = gtk_file_chooser_default_list_filters;
699 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
700 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
701 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
705 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
707 iface->get_default_size = gtk_file_chooser_default_get_default_size;
708 iface->should_respond = gtk_file_chooser_default_should_respond;
709 iface->initial_focus = gtk_file_chooser_default_initial_focus;
713 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
715 profile_start ("start", NULL);
716 #ifdef PROFILE_FILE_CHOOSER
717 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
719 impl->local_only = TRUE;
720 impl->preview_widget_active = TRUE;
721 impl->use_preview_label = TRUE;
722 impl->select_multiple = FALSE;
723 impl->show_hidden = FALSE;
724 impl->show_size_column = TRUE;
725 impl->icon_size = FALLBACK_ICON_SIZE;
726 impl->load_state = LOAD_EMPTY;
727 impl->reload_state = RELOAD_EMPTY;
728 impl->pending_select_files = NULL;
729 impl->location_mode = LOCATION_MODE_PATH_BAR;
730 impl->operation_mode = OPERATION_MODE_BROWSE;
731 impl->sort_column = MODEL_COL_NAME;
732 impl->sort_order = GTK_SORT_ASCENDING;
733 impl->recent_manager = gtk_recent_manager_get_default ();
734 impl->create_folders = TRUE;
736 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
737 GTK_ORIENTATION_VERTICAL);
738 gtk_box_set_spacing (GTK_BOX (impl), 12);
740 set_file_system_backend (impl);
742 profile_end ("end", NULL);
745 /* Frees the data columns for the specified iter in the shortcuts model*/
747 shortcuts_free_row_data (GtkFileChooserDefault *impl,
751 ShortcutType shortcut_type;
752 GCancellable *cancellable;
754 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
755 SHORTCUTS_COL_DATA, &col_data,
756 SHORTCUTS_COL_TYPE, &shortcut_type,
757 SHORTCUTS_COL_CANCELLABLE, &cancellable,
762 g_cancellable_cancel (cancellable);
763 g_object_unref (cancellable);
766 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
767 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
771 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
773 GtkFileSystemVolume *volume;
776 _gtk_file_system_volume_unref (volume);
778 if (shortcut_type == SHORTCUT_TYPE_FILE)
783 g_object_unref (file);
787 /* Frees all the data columns in the shortcuts model */
789 shortcuts_free (GtkFileChooserDefault *impl)
793 if (!impl->shortcuts_model)
796 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
799 shortcuts_free_row_data (impl, &iter);
801 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
803 g_object_unref (impl->shortcuts_model);
804 impl->shortcuts_model = NULL;
808 pending_select_files_free (GtkFileChooserDefault *impl)
810 g_slist_free_full (impl->pending_select_files, g_object_unref);
811 impl->pending_select_files = NULL;
815 pending_select_files_add (GtkFileChooserDefault *impl,
818 impl->pending_select_files =
819 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
823 gtk_file_chooser_default_finalize (GObject *object)
825 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
828 unset_file_system_backend (impl);
830 if (impl->shortcuts_pane_filter_model)
831 g_object_unref (impl->shortcuts_pane_filter_model);
833 shortcuts_free (impl);
835 g_free (impl->browse_files_last_selected_name);
837 for (l = impl->filters; l; l = l->next)
839 GtkFileFilter *filter;
841 filter = GTK_FILE_FILTER (l->data);
842 g_object_unref (filter);
844 g_slist_free (impl->filters);
846 if (impl->current_filter)
847 g_object_unref (impl->current_filter);
849 if (impl->current_volume_file)
850 g_object_unref (impl->current_volume_file);
852 if (impl->current_folder)
853 g_object_unref (impl->current_folder);
855 if (impl->preview_file)
856 g_object_unref (impl->preview_file);
858 if (impl->browse_path_bar_size_group)
859 g_object_unref (impl->browse_path_bar_size_group);
861 /* Free all the Models we have */
862 stop_loading_and_clear_list_model (impl, FALSE);
863 search_clear_model (impl, FALSE);
864 recent_clear_model (impl, FALSE);
866 /* stopping the load above should have cleared this */
867 g_assert (impl->load_timeout_id == 0);
869 g_free (impl->preview_display_name);
871 g_free (impl->edited_new_text);
873 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
876 /* Shows an error dialog set as transient for the specified window */
878 error_message_with_parent (GtkWindow *parent,
884 dialog = gtk_message_dialog_new (parent,
885 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
890 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
893 if (parent && gtk_window_has_group (parent))
894 gtk_window_group_add_window (gtk_window_get_group (parent),
895 GTK_WINDOW (dialog));
897 gtk_dialog_run (GTK_DIALOG (dialog));
898 gtk_widget_destroy (dialog);
901 /* Returns a toplevel GtkWindow, or NULL if none */
903 get_toplevel (GtkWidget *widget)
907 toplevel = gtk_widget_get_toplevel (widget);
908 if (!gtk_widget_is_toplevel (toplevel))
911 return GTK_WINDOW (toplevel);
914 /* Shows an error dialog for the file chooser */
916 error_message (GtkFileChooserDefault *impl,
920 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
923 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
925 error_dialog (GtkFileChooserDefault *impl,
936 uri = g_file_get_uri (file);
937 text = g_strdup_printf (msg, uri);
938 error_message (impl, text, error->message);
941 g_error_free (error);
945 /* Displays an error message about not being able to get information for a file.
946 * Frees the GError as well.
949 error_getting_info_dialog (GtkFileChooserDefault *impl,
954 _("Could not retrieve information about the file"),
958 /* Shows an error dialog about not being able to add a bookmark */
960 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
965 _("Could not add a bookmark"),
969 /* Shows an error dialog about not being able to remove a bookmark */
971 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
976 _("Could not remove bookmark"),
980 /* Shows an error dialog about not being able to create a folder */
982 error_creating_folder_dialog (GtkFileChooserDefault *impl,
987 _("The folder could not be created"),
991 /* Shows an error about not being able to create a folder because a file with
992 * the same name is already there.
995 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1000 _("The folder could not be created, as a file with the same "
1001 "name already exists. Try using a different name for the "
1002 "folder, or rename the file first."),
1007 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1013 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1014 _("You need to choose a valid filename."));
1017 _("Cannot create a file under %s as it is not a folder"),
1018 parent_file, error);
1021 /* Shows an error about not being able to select a folder because a file with
1022 * the same name is already there.
1025 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1029 _("You may only select folders. The item that you selected is not a folder; "
1030 "try using a different item."),
1034 /* Shows an error dialog about not being able to create a filename */
1036 error_building_filename_dialog (GtkFileChooserDefault *impl,
1039 error_dialog (impl, _("Invalid file name"),
1043 /* Shows an error dialog when we cannot switch to a folder */
1045 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1049 error_dialog (impl, _("The folder contents could not be displayed"),
1053 /* Changes folders, displaying an error dialog if this fails */
1055 change_folder_and_display_error (GtkFileChooserDefault *impl,
1057 gboolean clear_entry)
1062 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1064 /* We copy the path because of this case:
1066 * list_row_activated()
1067 * fetches path from model; path belongs to the model (*)
1068 * calls change_folder_and_display_error()
1069 * calls gtk_file_chooser_set_current_folder_file()
1070 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1074 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1077 error_changing_folder_dialog (impl, file, error);
1083 emit_default_size_changed (GtkFileChooserDefault *impl)
1085 profile_msg (" emit default-size-changed start", NULL);
1086 g_signal_emit_by_name (impl, "default-size-changed");
1087 profile_msg (" emit default-size-changed end", NULL);
1091 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1093 if (impl->use_preview_label)
1095 if (!impl->preview_label)
1097 impl->preview_label = gtk_label_new (impl->preview_display_name);
1098 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1099 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1100 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1101 gtk_widget_show (impl->preview_label);
1106 if (impl->preview_label)
1108 gtk_widget_destroy (impl->preview_label);
1109 impl->preview_label = NULL;
1113 if (impl->preview_widget_active && impl->preview_widget)
1114 gtk_widget_show (impl->preview_box);
1116 gtk_widget_hide (impl->preview_box);
1118 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1119 emit_default_size_changed (impl);
1123 set_preview_widget (GtkFileChooserDefault *impl,
1124 GtkWidget *preview_widget)
1126 if (preview_widget == impl->preview_widget)
1129 if (impl->preview_widget)
1130 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1131 impl->preview_widget);
1133 impl->preview_widget = preview_widget;
1134 if (impl->preview_widget)
1136 gtk_widget_show (impl->preview_widget);
1137 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1138 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1139 impl->preview_widget,
1140 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1143 update_preview_widget_visibility (impl);
1146 /* Renders a "Search" icon at an appropriate size for a tree view */
1148 render_search_icon (GtkFileChooserDefault *impl)
1150 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1154 render_recent_icon (GtkFileChooserDefault *impl)
1156 GtkIconTheme *theme;
1159 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1160 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1162 theme = gtk_icon_theme_get_default ();
1164 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1170 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1176 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1177 struct ReloadIconsData
1179 GtkFileChooserDefault *impl;
1180 GtkTreeRowReference *row_ref;
1184 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1186 const GError *error,
1192 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1193 struct ReloadIconsData *data = user_data;
1195 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1198 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1200 if (cancelled || error)
1203 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1205 path = gtk_tree_row_reference_get_path (data->row_ref);
1208 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1209 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1210 SHORTCUTS_COL_PIXBUF, pixbuf,
1212 gtk_tree_path_free (path);
1216 g_object_unref (pixbuf);
1219 gtk_tree_row_reference_free (data->row_ref);
1220 g_object_unref (data->impl);
1223 g_object_unref (cancellable);
1227 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1232 profile_start ("start", NULL);
1234 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1237 for (l = impl->reload_icon_cancellables; l; l = l->next)
1239 GCancellable *cancellable = G_CANCELLABLE (l->data);
1240 g_cancellable_cancel (cancellable);
1242 g_slist_free (impl->reload_icon_cancellables);
1243 impl->reload_icon_cancellables = NULL;
1248 ShortcutType shortcut_type;
1249 gboolean pixbuf_visible;
1252 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1253 SHORTCUTS_COL_DATA, &data,
1254 SHORTCUTS_COL_TYPE, &shortcut_type,
1255 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1261 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1263 GtkFileSystemVolume *volume;
1266 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1267 impl->icon_size, NULL);
1269 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1271 if (g_file_is_native (G_FILE (data)))
1274 struct ReloadIconsData *info;
1275 GtkTreePath *tree_path;
1276 GCancellable *cancellable;
1280 info = g_new0 (struct ReloadIconsData, 1);
1281 info->impl = g_object_ref (impl);
1282 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1283 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1284 gtk_tree_path_free (tree_path);
1286 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1288 shortcuts_reload_icons_get_info_cb,
1290 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1294 GtkIconTheme *icon_theme;
1296 /* Don't call get_info for remote paths to avoid latency and
1298 * If we switch to a better bookmarks file format (XBEL), we
1299 * should use mime info to get a better icon.
1301 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1302 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1303 impl->icon_size, 0, NULL);
1306 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1308 pixbuf = render_search_icon (impl);
1310 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1312 pixbuf = render_recent_icon (impl);
1315 gtk_list_store_set (impl->shortcuts_model, &iter,
1316 SHORTCUTS_COL_PIXBUF, pixbuf,
1320 g_object_unref (pixbuf);
1324 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1328 profile_end ("end", NULL);
1332 shortcuts_find_folder (GtkFileChooserDefault *impl,
1335 GtkTreeSelection *selection;
1339 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1341 g_assert (folder != NULL);
1342 pos = shortcut_find_position (impl, folder);
1345 gtk_tree_selection_unselect_all (selection);
1349 path = gtk_tree_path_new_from_indices (pos, -1);
1350 gtk_tree_selection_select_path (selection, path);
1351 gtk_tree_path_free (path);
1354 /* If a shortcut corresponds to the current folder, selects it */
1356 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1358 shortcuts_find_folder (impl, impl->current_folder);
1361 /* Removes the specified number of rows from the shortcuts list */
1363 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1369 path = gtk_tree_path_new_from_indices (start_row, -1);
1371 for (; n_rows; n_rows--)
1375 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1376 g_assert_not_reached ();
1378 shortcuts_free_row_data (impl, &iter);
1379 gtk_list_store_remove (impl->shortcuts_model, &iter);
1382 gtk_tree_path_free (path);
1386 shortcuts_update_count (GtkFileChooserDefault *impl,
1387 ShortcutsIndex type,
1392 case SHORTCUTS_HOME:
1394 impl->has_home = FALSE;
1396 impl->has_home = TRUE;
1399 case SHORTCUTS_DESKTOP:
1401 impl->has_desktop = FALSE;
1403 impl->has_desktop = TRUE;
1406 case SHORTCUTS_VOLUMES:
1407 impl->num_volumes += value;
1410 case SHORTCUTS_SHORTCUTS:
1411 impl->num_shortcuts += value;
1414 case SHORTCUTS_BOOKMARKS:
1415 impl->num_bookmarks += value;
1418 case SHORTCUTS_CURRENT_FOLDER:
1420 impl->shortcuts_current_folder_active = FALSE;
1422 impl->shortcuts_current_folder_active = TRUE;
1431 struct ShortcutsInsertRequest
1433 GtkFileChooserDefault *impl;
1437 GtkTreeRowReference *row_ref;
1438 ShortcutsIndex type;
1444 get_file_info_finished (GCancellable *cancellable,
1446 const GError *error,
1449 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1453 GCancellable *model_cancellable = NULL;
1454 struct ShortcutsInsertRequest *request = data;
1456 path = gtk_tree_row_reference_get_path (request->row_ref);
1458 /* Handle doesn't exist anymore in the model */
1461 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1463 gtk_tree_path_free (path);
1465 /* validate cancellable, else goto out */
1466 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1467 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1469 if (cancellable != model_cancellable)
1472 /* set the cancellable to NULL in the model (we unref later on) */
1473 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1474 SHORTCUTS_COL_CANCELLABLE, NULL,
1482 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1483 shortcuts_update_count (request->impl, request->type, -1);
1485 if (request->type == SHORTCUTS_HOME)
1489 home = g_file_new_for_path (g_get_home_dir ());
1490 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1491 g_object_unref (home);
1493 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1495 /* Remove the current folder separator */
1496 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1497 shortcuts_remove_rows (request->impl, separator_pos, 1);
1503 if (!request->label_copy)
1504 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1505 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1506 request->impl->icon_size);
1508 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1509 SHORTCUTS_COL_PIXBUF, pixbuf,
1510 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1511 SHORTCUTS_COL_NAME, request->label_copy,
1512 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1513 SHORTCUTS_COL_REMOVABLE, request->removable,
1516 if (request->impl->shortcuts_pane_filter_model)
1517 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1520 g_object_unref (pixbuf);
1523 g_object_unref (request->impl);
1524 g_object_unref (request->file);
1525 gtk_tree_row_reference_free (request->row_ref);
1526 g_free (request->label_copy);
1529 if (model_cancellable)
1530 g_object_unref (model_cancellable);
1533 /* FIXME: GtkFileSystem needs a function to split a remote path
1534 * into hostname and path components, or maybe just have a
1535 * gtk_file_system_path_get_display_name().
1537 * This function is also used in gtkfilechooserbutton.c
1540 _gtk_file_chooser_label_for_file (GFile *file)
1542 const gchar *path, *start, *end, *p;
1543 gchar *uri, *host, *label;
1545 uri = g_file_get_uri (file);
1547 start = strstr (uri, "://");
1551 path = strchr (start, '/');
1556 end = uri + strlen (uri);
1560 /* strip username */
1561 p = strchr (start, '@');
1565 p = strchr (start, ':');
1569 host = g_strndup (start, end - start);
1571 /* Translators: the first string is a path and the second string
1572 * is a hostname. Nautilus and the panel contain the same string
1575 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1581 label = g_strdup (uri);
1589 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1590 * inserts a volume. A position of -1 indicates the end of the tree.
1593 shortcuts_insert_file (GtkFileChooserDefault *impl,
1595 ShortcutType shortcut_type,
1596 GtkFileSystemVolume *volume,
1600 ShortcutsIndex type)
1603 GdkPixbuf *pixbuf = NULL;
1604 gpointer data = NULL;
1606 GtkIconTheme *icon_theme;
1608 profile_start ("start shortcut", NULL);
1610 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1613 label_copy = _gtk_file_system_volume_get_display_name (volume);
1614 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1615 impl->icon_size, NULL);
1617 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1619 if (g_file_is_native (file))
1621 struct ShortcutsInsertRequest *request;
1622 GCancellable *cancellable;
1625 request = g_new0 (struct ShortcutsInsertRequest, 1);
1626 request->impl = g_object_ref (impl);
1627 request->file = g_object_ref (file);
1628 request->name_only = TRUE;
1629 request->removable = removable;
1631 request->type = type;
1633 request->label_copy = g_strdup (label);
1636 gtk_list_store_append (impl->shortcuts_model, &iter);
1638 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1640 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1641 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1642 gtk_tree_path_free (p);
1644 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1645 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1646 get_file_info_finished, request);
1648 gtk_list_store_set (impl->shortcuts_model, &iter,
1649 SHORTCUTS_COL_DATA, g_object_ref (file),
1650 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1651 SHORTCUTS_COL_CANCELLABLE, cancellable,
1654 shortcuts_update_count (impl, type, 1);
1660 /* Don't call get_info for remote paths to avoid latency and
1663 data = g_object_ref (file);
1665 label_copy = g_strdup (label);
1667 label_copy = _gtk_file_chooser_label_for_file (file);
1669 /* If we switch to a better bookmarks file format (XBEL), we
1670 * should use mime info to get a better icon.
1672 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1673 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1674 impl->icon_size, 0, NULL);
1679 g_assert_not_reached ();
1685 gtk_list_store_append (impl->shortcuts_model, &iter);
1687 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1689 shortcuts_update_count (impl, type, 1);
1691 gtk_list_store_set (impl->shortcuts_model, &iter,
1692 SHORTCUTS_COL_PIXBUF, pixbuf,
1693 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1694 SHORTCUTS_COL_NAME, label_copy,
1695 SHORTCUTS_COL_DATA, data,
1696 SHORTCUTS_COL_TYPE, shortcut_type,
1697 SHORTCUTS_COL_REMOVABLE, removable,
1698 SHORTCUTS_COL_CANCELLABLE, NULL,
1701 if (impl->shortcuts_pane_filter_model)
1702 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1704 g_free (label_copy);
1707 g_object_unref (pixbuf);
1709 profile_end ("end", NULL);
1713 shortcuts_append_search (GtkFileChooserDefault *impl)
1718 pixbuf = render_search_icon (impl);
1720 gtk_list_store_append (impl->shortcuts_model, &iter);
1721 gtk_list_store_set (impl->shortcuts_model, &iter,
1722 SHORTCUTS_COL_PIXBUF, pixbuf,
1723 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1724 SHORTCUTS_COL_NAME, _("Search"),
1725 SHORTCUTS_COL_DATA, NULL,
1726 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1727 SHORTCUTS_COL_REMOVABLE, FALSE,
1731 g_object_unref (pixbuf);
1733 impl->has_search = TRUE;
1737 shortcuts_get_recent_enabled (GtkWidget *widget)
1739 GtkSettings *settings;
1742 if (gtk_widget_has_screen (widget))
1743 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1745 settings = gtk_settings_get_default ();
1747 g_object_get (settings, "gtk-recent-files-enabled", &enabled, NULL);
1752 shortcuts_append_recent (GtkFileChooserDefault *impl)
1758 enabled = shortcuts_get_recent_enabled (GTK_WIDGET (impl));
1762 pixbuf = render_recent_icon (impl);
1764 gtk_list_store_append (impl->shortcuts_model, &iter);
1765 gtk_list_store_set (impl->shortcuts_model, &iter,
1766 SHORTCUTS_COL_PIXBUF, pixbuf,
1767 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1768 SHORTCUTS_COL_NAME, _("Recently Used"),
1769 SHORTCUTS_COL_DATA, NULL,
1770 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1771 SHORTCUTS_COL_REMOVABLE, FALSE,
1775 g_object_unref (pixbuf);
1777 impl->has_recent = TRUE;
1780 /* Appends an item for the user's home directory to the shortcuts model */
1782 shortcuts_append_home (GtkFileChooserDefault *impl)
1784 const char *home_path;
1787 profile_start ("start", NULL);
1789 home_path = g_get_home_dir ();
1790 if (home_path == NULL)
1792 profile_end ("end - no home directory!?", NULL);
1796 home = g_file_new_for_path (home_path);
1797 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1798 impl->has_home = TRUE;
1800 g_object_unref (home);
1802 profile_end ("end", NULL);
1805 /* Appends the ~/Desktop directory to the shortcuts model */
1807 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1812 profile_start ("start", NULL);
1814 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1815 /* "To disable a directory, point it to the homedir."
1816 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1818 if (!g_strcmp0 (name, g_get_home_dir ()))
1820 profile_end ("end", NULL);
1824 file = g_file_new_for_path (name);
1825 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1826 impl->has_desktop = TRUE;
1828 /* We do not actually pop up an error dialog if there is no desktop directory
1829 * because some people may really not want to have one.
1832 g_object_unref (file);
1834 profile_end ("end", NULL);
1837 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1839 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1846 profile_start ("start", NULL);
1848 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1851 for (; bookmarks; bookmarks = bookmarks->next)
1855 file = bookmarks->data;
1857 if (impl->local_only && !g_file_is_native (file))
1860 if (shortcut_find_position (impl, file) != -1)
1863 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1865 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1871 profile_end ("end", NULL);
1873 return num_inserted;
1876 /* Returns the index for the corresponding item in the shortcuts bar */
1878 shortcuts_get_index (GtkFileChooserDefault *impl,
1879 ShortcutsIndex where)
1885 if (where == SHORTCUTS_SEARCH)
1888 n += impl->has_search ? 1 : 0;
1890 if (where == SHORTCUTS_RECENT)
1893 n += impl->has_recent ? 1 : 0;
1895 if (where == SHORTCUTS_RECENT_SEPARATOR)
1898 n += 1; /* we always have the separator after the recently-used item */
1900 if (where == SHORTCUTS_HOME)
1903 n += impl->has_home ? 1 : 0;
1905 if (where == SHORTCUTS_DESKTOP)
1908 n += impl->has_desktop ? 1 : 0;
1910 if (where == SHORTCUTS_VOLUMES)
1913 n += impl->num_volumes;
1915 if (where == SHORTCUTS_SHORTCUTS)
1918 n += impl->num_shortcuts;
1920 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1923 /* If there are no bookmarks there won't be a separator */
1924 n += (impl->num_bookmarks > 0) ? 1 : 0;
1926 if (where == SHORTCUTS_BOOKMARKS)
1929 n += impl->num_bookmarks;
1931 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1936 if (where == SHORTCUTS_CURRENT_FOLDER)
1939 g_assert_not_reached ();
1946 /* Adds all the file system volumes to the shortcuts model */
1948 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1953 gboolean old_changing_folders;
1955 profile_start ("start", NULL);
1957 old_changing_folders = impl->changing_folder;
1958 impl->changing_folder = TRUE;
1960 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1961 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1962 impl->num_volumes = 0;
1964 list = _gtk_file_system_list_volumes (impl->file_system);
1968 for (l = list; l; l = l->next)
1970 GtkFileSystemVolume *volume;
1974 if (impl->local_only)
1976 if (_gtk_file_system_volume_is_mounted (volume))
1979 gboolean base_is_native = TRUE;
1981 base_file = _gtk_file_system_volume_get_root (volume);
1982 if (base_file != NULL)
1984 base_is_native = g_file_is_native (base_file);
1985 g_object_unref (base_file);
1988 if (!base_is_native)
1993 shortcuts_insert_file (impl,
1995 SHORTCUT_TYPE_VOLUME,
1996 _gtk_file_system_volume_ref (volume),
2004 impl->num_volumes = n;
2005 g_slist_free (list);
2007 if (impl->shortcuts_pane_filter_model)
2008 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2010 impl->changing_folder = old_changing_folders;
2012 profile_end ("end", NULL);
2015 /* Inserts a separator node in the shortcuts list */
2017 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2018 ShortcutsIndex where)
2022 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2023 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2024 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2026 gtk_list_store_insert (impl->shortcuts_model, &iter,
2027 shortcuts_get_index (impl, where));
2028 gtk_list_store_set (impl->shortcuts_model, &iter,
2029 SHORTCUTS_COL_PIXBUF, NULL,
2030 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2031 SHORTCUTS_COL_NAME, NULL,
2032 SHORTCUTS_COL_DATA, NULL,
2033 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2037 /* Updates the list of bookmarks */
2039 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2042 gboolean old_changing_folders;
2044 GFile *list_selected = NULL;
2045 ShortcutType shortcut_type;
2048 profile_start ("start", NULL);
2050 old_changing_folders = impl->changing_folder;
2051 impl->changing_folder = TRUE;
2053 if (shortcuts_get_selected (impl, &iter))
2055 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2057 SHORTCUTS_COL_DATA, &col_data,
2058 SHORTCUTS_COL_TYPE, &shortcut_type,
2061 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2062 list_selected = g_object_ref (col_data);
2065 if (impl->num_bookmarks > 0)
2066 shortcuts_remove_rows (impl,
2067 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2068 impl->num_bookmarks + 1);
2070 impl->num_bookmarks = 0;
2071 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2073 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2074 shortcuts_append_bookmarks (impl, bookmarks);
2075 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2076 g_slist_free (bookmarks);
2078 if (impl->num_bookmarks == 0)
2079 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2081 if (impl->shortcuts_pane_filter_model)
2082 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2086 shortcuts_find_folder (impl, list_selected);
2087 g_object_unref (list_selected);
2090 impl->changing_folder = old_changing_folders;
2092 profile_end ("end", NULL);
2095 /* Appends a separator and a row to the shortcuts list for the current folder */
2097 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2101 g_assert (!impl->shortcuts_current_folder_active);
2103 g_assert (impl->current_folder != NULL);
2105 pos = shortcut_find_position (impl, impl->current_folder);
2108 GtkFileSystemVolume *volume;
2112 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2115 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2117 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2119 base_file = _gtk_file_system_volume_get_root (volume);
2123 if (base_file && g_file_equal (base_file, impl->current_folder))
2124 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2126 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2129 g_object_unref (base_file);
2133 /* Updates the current folder row in the shortcuts model */
2135 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2139 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2141 if (impl->shortcuts_current_folder_active)
2143 shortcuts_remove_rows (impl, pos, 2);
2144 impl->shortcuts_current_folder_active = FALSE;
2147 shortcuts_add_current_folder (impl);
2150 /* Filter function used for the shortcuts filter model */
2152 shortcuts_pane_filter_cb (GtkTreeModel *model,
2156 GtkFileChooserDefault *impl;
2160 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2162 path = gtk_tree_model_get_path (model, iter);
2166 pos = *gtk_tree_path_get_indices (path);
2167 gtk_tree_path_free (path);
2169 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2172 /* Creates the list model for shortcuts */
2174 shortcuts_model_create (GtkFileChooserDefault *impl)
2176 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2177 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2178 GDK_TYPE_PIXBUF, /* pixbuf */
2179 G_TYPE_STRING, /* name */
2180 G_TYPE_POINTER, /* path or volume */
2181 G_TYPE_INT, /* ShortcutType */
2182 G_TYPE_BOOLEAN, /* removable */
2183 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2184 G_TYPE_POINTER); /* GCancellable */
2186 shortcuts_append_search (impl);
2188 if (impl->recent_manager)
2190 shortcuts_append_recent (impl);
2191 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2194 if (impl->file_system)
2196 shortcuts_append_home (impl);
2197 shortcuts_append_desktop (impl);
2198 shortcuts_add_volumes (impl);
2201 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2202 GTK_TREE_MODEL (impl->shortcuts_model),
2205 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2206 shortcuts_pane_filter_cb,
2211 /* Callback used when the "New Folder" button is clicked */
2213 new_folder_button_clicked (GtkButton *button,
2214 GtkFileChooserDefault *impl)
2219 if (!impl->browse_files_model)
2220 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2222 /* Prevent button from being clicked twice */
2223 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2225 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2227 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2228 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2229 path, impl->list_name_column,
2232 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2233 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2235 impl->list_name_column,
2238 gtk_tree_path_free (path);
2242 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2246 source = g_idle_source_new ();
2247 g_source_set_closure (source,
2248 g_cclosure_new_object (callback, G_OBJECT (impl)));
2249 g_source_attach (source, NULL);
2254 /* Idle handler for creating a new folder after editing its name cell, or for
2255 * canceling the editing.
2258 edited_idle_cb (GtkFileChooserDefault *impl)
2260 gdk_threads_enter ();
2262 g_source_destroy (impl->edited_idle);
2263 impl->edited_idle = NULL;
2265 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2266 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2268 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2270 if (impl->edited_new_text /* not cancelled? */
2271 && (strlen (impl->edited_new_text) != 0)
2272 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2274 GError *error = NULL;
2277 file = g_file_get_child_for_display_name (impl->current_folder,
2278 impl->edited_new_text,
2282 GError *error = NULL;
2284 if (g_file_make_directory (file, NULL, &error))
2285 change_folder_and_display_error (impl, file, FALSE);
2287 error_creating_folder_dialog (impl, file, error);
2289 g_object_unref (file);
2292 error_creating_folder_dialog (impl, file, error);
2294 g_free (impl->edited_new_text);
2295 impl->edited_new_text = NULL;
2298 gdk_threads_leave ();
2304 queue_edited_idle (GtkFileChooserDefault *impl,
2305 const gchar *new_text)
2307 /* We create the folder in an idle handler so that we don't modify the tree
2311 if (!impl->edited_idle)
2312 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2314 g_free (impl->edited_new_text);
2315 impl->edited_new_text = g_strdup (new_text);
2318 /* Callback used from the text cell renderer when the new folder is named */
2320 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2322 const gchar *new_text,
2323 GtkFileChooserDefault *impl)
2325 /* work around bug #154921 */
2326 g_object_set (cell_renderer_text,
2327 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2328 queue_edited_idle (impl, new_text);
2331 /* Callback used from the text cell renderer when the new folder edition gets
2335 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2336 GtkFileChooserDefault *impl)
2338 /* work around bug #154921 */
2339 g_object_set (cell_renderer_text,
2340 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2341 queue_edited_idle (impl, NULL);
2344 /* Creates the widgets for the filter combo box */
2346 filter_create (GtkFileChooserDefault *impl)
2348 GtkCellRenderer *cell;
2351 impl->filter_combo = gtk_combo_box_text_new ();
2352 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2354 /* Get the combo's text renderer and set ellipsize parameters */
2355 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2359 g_object_set (G_OBJECT (cell),
2360 "ellipsize", PANGO_ELLIPSIZE_END,
2363 g_list_free (cells);
2365 g_signal_connect (impl->filter_combo, "changed",
2366 G_CALLBACK (filter_combo_changed), impl);
2368 gtk_widget_set_tooltip_text (impl->filter_combo,
2369 _("Select which types of files are shown"));
2371 return impl->filter_combo;
2375 toolbutton_new (GtkFileChooserDefault *impl,
2384 item = gtk_tool_button_new (NULL, NULL);
2385 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2386 gtk_widget_show (image);
2387 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2389 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2390 g_signal_connect (item, "clicked", callback, impl);
2393 gtk_widget_show (GTK_WIDGET (item));
2395 return GTK_WIDGET (item);
2398 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2400 shortcut_find_position (GtkFileChooserDefault *impl,
2405 int current_folder_separator_idx;
2407 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2410 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2413 /* FIXME: is this still needed? */
2414 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2418 for (i = 0; i < current_folder_separator_idx; i++)
2421 ShortcutType shortcut_type;
2423 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2424 SHORTCUTS_COL_DATA, &col_data,
2425 SHORTCUTS_COL_TYPE, &shortcut_type,
2430 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2432 GtkFileSystemVolume *volume;
2437 base_file = _gtk_file_system_volume_get_root (volume);
2439 exists = base_file && g_file_equal (file, base_file);
2442 g_object_unref (base_file);
2447 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2451 model_file = col_data;
2453 if (model_file && g_file_equal (model_file, file))
2458 if (i < current_folder_separator_idx - 1)
2460 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2461 g_assert_not_reached ();
2468 /* Tries to add a bookmark from a path name */
2470 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2476 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2478 if (shortcut_find_position (impl, file) != -1)
2482 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2484 error_adding_bookmark_dialog (impl, file, error);
2492 add_bookmark_foreach_cb (GtkTreeModel *model,
2497 GtkFileChooserDefault *impl;
2500 impl = (GtkFileChooserDefault *) data;
2502 gtk_tree_model_get (model, iter,
2503 MODEL_COL_FILE, &file,
2506 shortcuts_add_bookmark_from_file (impl, file, -1);
2508 g_object_unref (file);
2511 /* Adds a bookmark from the currently selected item in the file list */
2513 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2515 GtkTreeSelection *selection;
2517 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2519 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2520 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2522 gtk_tree_selection_selected_foreach (selection,
2523 add_bookmark_foreach_cb,
2527 /* Callback used when the "Add bookmark" button is clicked */
2529 add_bookmark_button_clicked_cb (GtkButton *button,
2530 GtkFileChooserDefault *impl)
2532 bookmarks_add_selected_folder (impl);
2535 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2536 * returns FALSE if no shortcut is selected.
2539 shortcuts_get_selected (GtkFileChooserDefault *impl,
2542 GtkTreeSelection *selection;
2543 GtkTreeIter parent_iter;
2545 if (!impl->browse_shortcuts_tree_view)
2548 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2550 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2553 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2559 /* Removes the selected bookmarks */
2561 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2569 if (!shortcuts_get_selected (impl, &iter))
2572 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2573 SHORTCUTS_COL_DATA, &col_data,
2574 SHORTCUTS_COL_REMOVABLE, &removable,
2580 g_assert (col_data != NULL);
2585 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2586 error_removing_bookmark_dialog (impl, file, error);
2589 /* Callback used when the "Remove bookmark" button is clicked */
2591 remove_bookmark_button_clicked_cb (GtkButton *button,
2592 GtkFileChooserDefault *impl)
2594 remove_selected_bookmarks (impl);
2597 struct selection_check_closure {
2598 GtkFileChooserDefault *impl;
2601 gboolean all_folders;
2604 /* Used from gtk_tree_selection_selected_foreach() */
2606 selection_check_foreach_cb (GtkTreeModel *model,
2611 struct selection_check_closure *closure;
2615 gtk_tree_model_get (model, iter,
2616 MODEL_COL_FILE, &file,
2617 MODEL_COL_IS_FOLDER, &is_folder,
2623 g_object_unref (file);
2626 closure->num_selected++;
2628 closure->all_folders = closure->all_folders && is_folder;
2629 closure->all_files = closure->all_files && !is_folder;
2632 /* Checks whether the selected items in the file list are all files or all folders */
2634 selection_check (GtkFileChooserDefault *impl,
2636 gboolean *all_files,
2637 gboolean *all_folders)
2639 struct selection_check_closure closure;
2640 GtkTreeSelection *selection;
2642 closure.impl = impl;
2643 closure.num_selected = 0;
2644 closure.all_files = TRUE;
2645 closure.all_folders = TRUE;
2647 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2648 gtk_tree_selection_selected_foreach (selection,
2649 selection_check_foreach_cb,
2652 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2655 *num_selected = closure.num_selected;
2658 *all_files = closure.all_files;
2661 *all_folders = closure.all_folders;
2664 struct get_selected_file_closure {
2665 GtkFileChooserDefault *impl;
2670 get_selected_file_foreach_cb (GtkTreeModel *model,
2675 struct get_selected_file_closure *closure = data;
2679 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2680 g_object_unref (closure->file);
2681 closure->file = NULL;
2684 gtk_tree_model_get (model, iter,
2685 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2689 /* Returns a selected path from the file list */
2691 get_selected_file (GtkFileChooserDefault *impl)
2693 struct get_selected_file_closure closure;
2694 GtkTreeSelection *selection;
2696 closure.impl = impl;
2697 closure.file = NULL;
2699 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2700 gtk_tree_selection_selected_foreach (selection,
2701 get_selected_file_foreach_cb,
2704 return closure.file;
2708 GtkFileChooserDefault *impl;
2710 } UpdateTooltipData;
2713 update_tooltip (GtkTreeModel *model,
2718 UpdateTooltipData *udata = data;
2720 if (udata->tip == NULL)
2722 gchar *display_name;
2724 gtk_tree_model_get (model, iter,
2725 MODEL_COL_NAME, &display_name,
2728 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2730 g_free (display_name);
2735 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2736 * if there are no selected items *and* the current folder is not in the
2737 * bookmarks list. De-sensitize the button otherwise.
2740 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2743 gboolean all_folders;
2747 selection_check (impl, &num_selected, NULL, &all_folders);
2749 if (num_selected == 0)
2750 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2751 else if (num_selected == 1)
2755 file = get_selected_file (impl);
2756 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2758 g_object_unref (file);
2761 active = all_folders;
2763 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2765 if (impl->browse_files_popup_menu_add_shortcut_item)
2766 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2767 (num_selected == 0) ? FALSE : active);
2771 if (num_selected == 0)
2772 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2773 else if (num_selected > 1)
2774 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2777 GtkTreeSelection *selection;
2778 UpdateTooltipData data;
2780 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2783 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2787 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2792 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2793 * bookmark row is selected in the shortcuts tree.
2796 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2799 gboolean removable = FALSE;
2803 if (shortcuts_get_selected (impl, &iter))
2805 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2806 SHORTCUTS_COL_REMOVABLE, &removable,
2807 SHORTCUTS_COL_NAME, &name,
2809 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2812 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2814 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2816 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2820 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2821 _("Remove the selected bookmark"));
2826 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2829 gboolean removable = FALSE;
2831 if (impl->browse_shortcuts_popup_menu == NULL)
2834 if (shortcuts_get_selected (impl, &iter))
2835 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2836 SHORTCUTS_COL_REMOVABLE, &removable,
2839 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2840 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2843 /* GtkWidget::drag-begin handler for the shortcuts list. */
2845 shortcuts_drag_begin_cb (GtkWidget *widget,
2846 GdkDragContext *context,
2847 GtkFileChooserDefault *impl)
2850 impl->shortcuts_drag_context = g_object_ref (context);
2855 /* Removes the idle handler for outside drags */
2857 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2859 if (!impl->shortcuts_drag_outside_idle)
2862 g_source_destroy (impl->shortcuts_drag_outside_idle);
2863 impl->shortcuts_drag_outside_idle = NULL;
2867 /* GtkWidget::drag-end handler for the shortcuts list. */
2869 shortcuts_drag_end_cb (GtkWidget *widget,
2870 GdkDragContext *context,
2871 GtkFileChooserDefault *impl)
2874 g_object_unref (impl->shortcuts_drag_context);
2876 shortcuts_cancel_drag_outside_idle (impl);
2878 if (!impl->shortcuts_drag_outside)
2881 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2883 impl->shortcuts_drag_outside = FALSE;
2887 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2889 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2890 GdkDragContext *context,
2891 GtkFileChooserDefault *impl)
2893 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2896 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2900 shortcuts_drag_leave_cb (GtkWidget *widget,
2901 GdkDragContext *context,
2903 GtkFileChooserDefault *impl)
2906 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2908 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2912 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2914 GTK_TREE_VIEW_DROP_BEFORE);
2916 g_signal_stop_emission_by_name (widget, "drag-leave");
2919 /* Computes the appropriate row and position for dropping */
2921 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2925 GtkTreeViewDropPosition *pos)
2927 GtkTreeView *tree_view;
2928 GtkTreeViewColumn *column;
2932 int bookmarks_index;
2933 int header_height = 0;
2935 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2937 if (gtk_tree_view_get_headers_visible (tree_view))
2938 header_height = _gtk_tree_view_get_header_height (tree_view);
2940 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2942 if (!gtk_tree_view_get_path_at_pos (tree_view,
2950 row = bookmarks_index + impl->num_bookmarks - 1;
2951 *path = gtk_tree_path_new_from_indices (row, -1);
2952 *pos = GTK_TREE_VIEW_DROP_AFTER;
2956 row = *gtk_tree_path_get_indices (*path);
2957 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2958 gtk_tree_path_free (*path);
2960 if (row < bookmarks_index)
2962 row = bookmarks_index;
2963 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2965 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2967 row = bookmarks_index + impl->num_bookmarks - 1;
2968 *pos = GTK_TREE_VIEW_DROP_AFTER;
2972 if (cell_y < cell.height / 2)
2973 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2975 *pos = GTK_TREE_VIEW_DROP_AFTER;
2978 *path = gtk_tree_path_new_from_indices (row, -1);
2981 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2982 * implement the destination side of DnD by hand, due to limitations in
2983 * GtkTreeView's DnD API.
2986 shortcuts_drag_motion_cb (GtkWidget *widget,
2987 GdkDragContext *context,
2991 GtkFileChooserDefault *impl)
2994 GtkTreeViewDropPosition pos;
2995 GdkDragAction action;
2998 if (gtk_drag_get_source_widget (context) == widget)
3000 shortcuts_cancel_drag_outside_idle (impl);
3002 if (impl->shortcuts_drag_outside)
3004 shortcuts_drag_set_delete_cursor (impl, FALSE);
3005 impl->shortcuts_drag_outside = FALSE;
3010 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3011 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3012 action = GDK_ACTION_COPY;
3013 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3014 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3015 action = GDK_ACTION_MOVE;
3022 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3023 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3024 gtk_tree_path_free (path);
3028 g_signal_stop_emission_by_name (widget, "drag-motion");
3032 gdk_drag_status (context, action, time_);
3039 /* GtkWidget::drag-drop handler for the shortcuts list. */
3041 shortcuts_drag_drop_cb (GtkWidget *widget,
3042 GdkDragContext *context,
3046 GtkFileChooserDefault *impl)
3049 shortcuts_cancel_drag_outside_idle (impl);
3052 g_signal_stop_emission_by_name (widget, "drag-drop");
3056 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3058 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3059 GtkSelectionData *selection_data,
3065 uris = gtk_selection_data_get_uris (selection_data);
3069 for (i = 0; uris[i]; i++)
3075 file = g_file_new_for_uri (uri);
3077 if (shortcuts_add_bookmark_from_file (impl, file, position))
3080 g_object_unref (file);
3086 /* Reorders the selected bookmark to the specified position */
3088 shortcuts_reorder (GtkFileChooserDefault *impl,
3093 ShortcutType shortcut_type;
3096 int bookmarks_index;
3101 /* Get the selected path */
3103 if (!shortcuts_get_selected (impl, &iter))
3104 g_assert_not_reached ();
3106 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3107 old_position = *gtk_tree_path_get_indices (path);
3108 gtk_tree_path_free (path);
3110 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3111 old_position -= bookmarks_index;
3112 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3114 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3115 SHORTCUTS_COL_NAME, &name,
3116 SHORTCUTS_COL_DATA, &col_data,
3117 SHORTCUTS_COL_TYPE, &shortcut_type,
3119 g_assert (col_data != NULL);
3120 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3123 g_object_ref (file); /* removal below will free file, so we need a new ref */
3125 /* Remove the path from the old position and insert it in the new one */
3127 if (new_position > old_position)
3130 if (old_position == new_position)
3134 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3136 shortcuts_add_bookmark_from_file (impl, file, new_position);
3137 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3140 error_adding_bookmark_dialog (impl, file, error);
3144 g_object_unref (file);
3148 /* Callback used when we get the drag data for the bookmarks list. We add the
3149 * received URIs as bookmarks if they are folders.
3152 shortcuts_drag_data_received_cb (GtkWidget *widget,
3153 GdkDragContext *context,
3156 GtkSelectionData *selection_data,
3161 GtkFileChooserDefault *impl;
3162 GtkTreePath *tree_path;
3163 GtkTreeViewDropPosition tree_pos;
3166 int bookmarks_index;
3168 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3170 /* Compute position */
3172 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3174 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3175 position = *gtk_tree_path_get_indices (tree_path);
3176 gtk_tree_path_free (tree_path);
3178 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3181 g_assert (position >= bookmarks_index);
3182 position -= bookmarks_index;
3184 target = gtk_selection_data_get_target (selection_data);
3186 if (gtk_targets_include_uri (&target, 1))
3187 shortcuts_drop_uris (impl, selection_data, position);
3188 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3189 shortcuts_reorder (impl, position);
3191 g_signal_stop_emission_by_name (widget, "drag-data-received");
3194 /* Callback used to display a tooltip in the shortcuts tree */
3196 shortcuts_query_tooltip_cb (GtkWidget *widget,
3199 gboolean keyboard_mode,
3200 GtkTooltip *tooltip,
3201 GtkFileChooserDefault *impl)
3203 GtkTreeModel *model;
3206 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3214 ShortcutType shortcut_type;
3216 gtk_tree_model_get (model, &iter,
3217 SHORTCUTS_COL_DATA, &col_data,
3218 SHORTCUTS_COL_TYPE, &shortcut_type,
3221 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3223 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3225 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3230 file = G_FILE (col_data);
3231 parse_name = g_file_get_parse_name (file);
3233 gtk_tooltip_set_text (tooltip, parse_name);
3235 g_free (parse_name);
3239 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3243 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3253 /* Callback used when the selection in the shortcuts tree changes */
3255 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3256 GtkFileChooserDefault *impl)
3259 GtkTreeIter child_iter;
3261 bookmarks_check_remove_sensitivity (impl);
3262 shortcuts_check_popup_sensitivity (impl);
3264 if (impl->changing_folder)
3267 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3269 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3272 shortcuts_activate_iter (impl, &child_iter);
3277 shortcuts_row_separator_func (GtkTreeModel *model,
3281 ShortcutType shortcut_type;
3283 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3285 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3289 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3291 GtkFileChooserDefault *impl)
3295 /* don't screw up focus switching with Tab */
3296 if (event->keyval == GDK_KEY_Tab
3297 || event->keyval == GDK_KEY_KP_Tab
3298 || event->keyval == GDK_KEY_ISO_Left_Tab
3299 || event->length < 1)
3302 if (impl->location_entry)
3303 entry = impl->location_entry;
3304 else if (impl->search_entry)
3305 entry = impl->search_entry;
3311 gtk_widget_grab_focus (entry);
3312 return gtk_widget_event (entry, (GdkEvent *) event);
3318 /* Callback used when the file list's popup menu is detached */
3320 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3323 GtkFileChooserDefault *impl;
3325 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3326 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3328 impl->browse_shortcuts_popup_menu = NULL;
3329 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3330 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3334 remove_shortcut_cb (GtkMenuItem *item,
3335 GtkFileChooserDefault *impl)
3337 remove_selected_bookmarks (impl);
3340 /* Rename the selected bookmark */
3342 rename_selected_bookmark (GtkFileChooserDefault *impl)
3346 GtkTreeViewColumn *column;
3347 GtkCellRenderer *cell;
3350 if (shortcuts_get_selected (impl, &iter))
3352 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3353 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3354 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3355 cell = g_list_nth_data (renderers, 1);
3356 g_list_free (renderers);
3357 g_object_set (cell, "editable", TRUE, NULL);
3358 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3359 path, column, cell, TRUE);
3360 gtk_tree_path_free (path);
3365 rename_shortcut_cb (GtkMenuItem *item,
3366 GtkFileChooserDefault *impl)
3368 rename_selected_bookmark (impl);
3371 /* Constructs the popup menu for the file list if needed */
3373 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3377 if (impl->browse_shortcuts_popup_menu)
3380 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3381 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3382 impl->browse_shortcuts_tree_view,
3383 shortcuts_popup_menu_detach_cb);
3385 item = gtk_image_menu_item_new_with_label (_("Remove"));
3386 impl->browse_shortcuts_popup_menu_remove_item = item;
3387 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3388 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3389 g_signal_connect (item, "activate",
3390 G_CALLBACK (remove_shortcut_cb), impl);
3391 gtk_widget_show (item);
3392 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3394 item = gtk_menu_item_new_with_label (_("Rename…"));
3395 impl->browse_shortcuts_popup_menu_rename_item = item;
3396 g_signal_connect (item, "activate",
3397 G_CALLBACK (rename_shortcut_cb), impl);
3398 gtk_widget_show (item);
3399 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3403 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3405 shortcuts_build_popup_menu (impl);
3406 shortcuts_check_popup_sensitivity (impl);
3410 popup_position_func (GtkMenu *menu,
3414 gpointer user_data);
3417 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3418 GdkEventButton *event)
3420 shortcuts_update_popup_menu (impl);
3422 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3423 NULL, NULL, NULL, NULL,
3424 event->button, event->time);
3427 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3429 popup_position_func, impl->browse_shortcuts_tree_view,
3430 0, GDK_CURRENT_TIME);
3431 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3436 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3438 shortcuts_popup_menu_cb (GtkWidget *widget,
3439 GtkFileChooserDefault *impl)
3441 shortcuts_popup_menu (impl, NULL);
3445 /* Callback used when a button is pressed on the shortcuts list.
3446 * We trap button 3 to bring up a popup menu.
3449 shortcuts_button_press_event_cb (GtkWidget *widget,
3450 GdkEventButton *event,
3451 GtkFileChooserDefault *impl)
3453 static gboolean in_press = FALSE;
3459 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
3463 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3469 shortcuts_popup_menu (impl, event);
3474 shortcuts_edited (GtkCellRenderer *cell,
3477 GtkFileChooserDefault *impl)
3483 g_object_set (cell, "editable", FALSE, NULL);
3485 path = gtk_tree_path_new_from_string (path_string);
3486 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3487 g_assert_not_reached ();
3489 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3490 SHORTCUTS_COL_DATA, &shortcut,
3492 gtk_tree_path_free (path);
3494 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3498 shortcuts_editing_canceled (GtkCellRenderer *cell,
3499 GtkFileChooserDefault *impl)
3501 g_object_set (cell, "editable", FALSE, NULL);
3504 /* Creates the widgets for the shortcuts and bookmarks tree */
3506 shortcuts_list_create (GtkFileChooserDefault *impl)
3509 GtkTreeSelection *selection;
3510 GtkTreeViewColumn *column;
3511 GtkCellRenderer *renderer;
3513 /* Target types for dragging a row to/from the shortcuts list */
3514 const GtkTargetEntry tree_model_row_targets[] = {
3515 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3518 /* Scrolled window */
3520 swin = gtk_scrolled_window_new (NULL, NULL);
3521 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3522 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3523 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3525 gtk_widget_show (swin);
3528 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3529 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3530 GTK_STYLE_CLASS_SIDEBAR);
3531 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3532 #ifdef PROFILE_FILE_CHOOSER
3533 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3536 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3538 * 1. user brings up a SAVE dialog
3539 * 2. user clicks on a shortcut in the shortcuts pane
3540 * 3. user starts typing a filename
3542 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3543 * support interactive search. However, we'd rather focus the location entry
3544 * so that the user can type *there*.
3546 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3547 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3548 * if the user starts typing while the focus is in the shortcuts pane.
3550 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3551 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3553 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3554 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3555 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3556 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3557 /* Accessible object name for the file chooser's shortcuts pane */
3558 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3560 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3562 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3564 tree_model_row_targets,
3565 G_N_ELEMENTS (tree_model_row_targets),
3568 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3569 GTK_DEST_DEFAULT_ALL,
3570 tree_model_row_targets,
3571 G_N_ELEMENTS (tree_model_row_targets),
3572 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3573 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3575 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3576 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3577 gtk_tree_selection_set_select_function (selection,
3578 shortcuts_select_func,
3581 g_signal_connect (selection, "changed",
3582 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3584 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3585 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3587 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3588 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3589 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3590 G_CALLBACK (shortcuts_drag_end_cb), impl);
3591 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3592 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3594 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3595 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3596 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3597 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3598 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3599 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3600 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3601 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3603 /* Support tooltips */
3604 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3605 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3606 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3608 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3609 gtk_widget_show (impl->browse_shortcuts_tree_view);
3613 column = gtk_tree_view_column_new ();
3614 /* Column header for the file chooser's shortcuts pane */
3615 gtk_tree_view_column_set_title (column, _("_Places"));
3617 renderer = gtk_cell_renderer_pixbuf_new ();
3618 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3619 gtk_tree_view_column_set_attributes (column, renderer,
3620 "pixbuf", SHORTCUTS_COL_PIXBUF,
3621 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3624 renderer = gtk_cell_renderer_text_new ();
3625 g_object_set (renderer,
3627 "ellipsize", PANGO_ELLIPSIZE_END,
3629 g_signal_connect (renderer, "edited",
3630 G_CALLBACK (shortcuts_edited), impl);
3631 g_signal_connect (renderer, "editing-canceled",
3632 G_CALLBACK (shortcuts_editing_canceled), impl);
3633 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3634 gtk_tree_view_column_set_attributes (column, renderer,
3635 "text", SHORTCUTS_COL_NAME,
3638 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3639 shortcuts_row_separator_func,
3642 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3647 /* Creates the widgets for the shortcuts/bookmarks pane */
3649 shortcuts_pane_create (GtkFileChooserDefault *impl,
3650 GtkSizeGroup *size_group)
3655 GtkStyleContext *context;
3658 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3659 gtk_widget_show (vbox);
3661 /* Shortcuts tree */
3663 widget = shortcuts_list_create (impl);
3665 gtk_size_group_add_widget (size_group, widget);
3666 context = gtk_widget_get_style_context (widget);
3667 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3669 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3671 /* Box for buttons */
3673 toolbar = gtk_toolbar_new ();
3674 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
3675 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3677 context = gtk_widget_get_style_context (toolbar);
3678 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3679 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3681 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3682 gtk_widget_show (toolbar);
3684 /* Add bookmark button */
3685 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3686 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3690 G_CALLBACK (add_bookmark_button_clicked_cb));
3691 g_object_unref (icon);
3693 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3694 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3695 _("Add the selected folder to the Bookmarks"));
3697 /* Remove bookmark button */
3698 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3699 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3703 G_CALLBACK (remove_bookmark_button_clicked_cb));
3704 g_object_unref (icon);
3705 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3706 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3707 _("Remove the selected bookmark"));
3713 key_is_left_or_right (GdkEventKey *event)
3717 modifiers = gtk_accelerator_get_default_mod_mask ();
3719 return ((event->keyval == GDK_KEY_Right
3720 || event->keyval == GDK_KEY_KP_Right
3721 || event->keyval == GDK_KEY_Left
3722 || event->keyval == GDK_KEY_KP_Left)
3723 && (event->state & modifiers) == 0);
3726 /* Handles key press events on the file list, so that we can trap Enter to
3727 * activate the default button on our own. Also, checks to see if '/' has been
3731 browse_files_key_press_event_cb (GtkWidget *widget,
3735 GtkFileChooserDefault *impl;
3736 GdkModifierType no_text_input_mask;
3738 impl = (GtkFileChooserDefault *) data;
3740 no_text_input_mask =
3741 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
3743 if ((event->keyval == GDK_KEY_slash
3744 || event->keyval == GDK_KEY_KP_Divide
3746 || event->keyval == GDK_KEY_asciitilde
3748 ) && !(event->state & no_text_input_mask))
3750 location_popup_handler (impl, event->string);
3754 if (key_is_left_or_right (event))
3756 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3760 if ((event->keyval == GDK_KEY_Return
3761 || event->keyval == GDK_KEY_ISO_Enter
3762 || event->keyval == GDK_KEY_KP_Enter
3763 || event->keyval == GDK_KEY_space
3764 || event->keyval == GDK_KEY_KP_Space)
3765 && !(event->state & gtk_accelerator_get_default_mod_mask ())
3766 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3767 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3771 window = get_toplevel (widget);
3774 GtkWidget *default_widget, *focus_widget;
3776 default_widget = gtk_window_get_default_widget (window);
3777 focus_widget = gtk_window_get_focus (window);
3779 if (widget != default_widget &&
3780 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3782 gtk_window_activate_default (window);
3792 /* Callback used when the file list's popup menu is detached */
3794 popup_menu_detach_cb (GtkWidget *attach_widget,
3797 GtkFileChooserDefault *impl;
3799 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3800 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3802 impl->browse_files_popup_menu = NULL;
3803 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3804 impl->browse_files_popup_menu_hidden_files_item = NULL;
3805 impl->browse_files_popup_menu_copy_file_location_item = NULL;
3808 /* Callback used when the "Add to Bookmarks" menu item is activated */
3810 add_to_shortcuts_cb (GtkMenuItem *item,
3811 GtkFileChooserDefault *impl)
3813 bookmarks_add_selected_folder (impl);
3816 /* callback used to set data to clipboard */
3818 copy_file_get_cb (GtkClipboard *clipboard,
3819 GtkSelectionData *selection_data,
3823 GSList *selected_files = data;
3827 gint num_files = g_slist_length (selected_files);
3832 uris = g_new (gchar *, num_files + 1);
3833 uris[num_files] = NULL; /* null terminator */
3837 for (l = selected_files; l; l = l->next)
3839 GFile *file = (GFile *) l->data;
3841 if (info == SELECTION_URI)
3842 uris[i] = g_file_get_uri (file);
3843 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3844 uris[i] = g_file_get_parse_name (file);
3849 if (info == SELECTION_URI)
3850 gtk_selection_data_set_uris (selection_data, uris);
3851 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3853 char *str = g_strjoinv (" ", uris);
3854 gtk_selection_data_set_text (selection_data, str, -1);
3862 /* callback used to clear the clipboard data */
3864 copy_file_clear_cb (GtkClipboard *clipboard,
3867 GSList *selected_files = data;
3869 g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3870 g_slist_free (selected_files);
3873 /* Callback used when the "Copy file’s location" menu item is activated */
3875 copy_file_location_cb (GtkMenuItem *item,
3876 GtkFileChooserDefault *impl)
3878 GSList *selected_files = NULL;
3880 selected_files = search_get_selected_files (impl);
3884 GtkClipboard *clipboard;
3885 GtkTargetList *target_list;
3886 GtkTargetEntry *targets;
3889 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3891 target_list = gtk_target_list_new (NULL, 0);
3892 gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3893 gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3895 targets = gtk_target_table_new_from_list (target_list, &n_targets);
3896 gtk_target_list_unref (target_list);
3898 gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3903 gtk_target_table_free (targets, n_targets);
3907 /* Callback used when the "Visit this file" menu item is activated */
3909 visit_file_cb (GtkMenuItem *item,
3910 GtkFileChooserDefault *impl)
3914 files = search_get_selected_files (impl);
3916 /* Sigh, just use the first one */
3919 GFile *file = files->data;
3921 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL); /* NULL-GError */
3924 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
3925 g_slist_free (files);
3928 /* callback used when the "Show Hidden Files" menu item is toggled */
3930 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3931 GtkFileChooserDefault *impl)
3934 "show-hidden", gtk_check_menu_item_get_active (item),
3938 /* Callback used when the "Show Size Column" menu item is toggled */
3940 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3941 GtkFileChooserDefault *impl)
3943 impl->show_size_column = gtk_check_menu_item_get_active (item);
3945 gtk_tree_view_column_set_visible (impl->list_size_column,
3946 impl->show_size_column);
3949 /* Shows an error dialog about not being able to select a dragged file */
3951 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3956 _("Could not select file"),
3961 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3966 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3968 for (i = 1; uris[i]; i++)
3971 GError *error = NULL;
3974 file = g_file_new_for_uri (uri);
3976 gtk_file_chooser_default_select_file (chooser, file, &error);
3978 error_selecting_dragged_file_dialog (impl, file, error);
3980 g_object_unref (file);
3984 struct FileListDragData
3986 GtkFileChooserDefault *impl;
3992 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3994 const GError *error,
3997 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3998 struct FileListDragData *data = user_data;
3999 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4001 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4004 data->impl->file_list_drag_data_received_cancellable = NULL;
4006 if (cancelled || error)
4009 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4010 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4011 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4012 change_folder_and_display_error (data->impl, data->file, FALSE);
4015 GError *error = NULL;
4017 gtk_file_chooser_default_unselect_all (chooser);
4018 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4020 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4022 browse_files_center_selected_row (data->impl);
4025 if (data->impl->select_multiple)
4026 file_list_drag_data_select_uris (data->impl, data->uris);
4029 g_object_unref (data->impl);
4030 g_strfreev (data->uris);
4031 g_object_unref (data->file);
4034 g_object_unref (cancellable);
4038 file_list_drag_data_received_cb (GtkWidget *widget,
4039 GdkDragContext *context,
4042 GtkSelectionData *selection_data,
4047 GtkFileChooserDefault *impl;
4052 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4054 /* Allow only drags from other widgets; see bug #533891. */
4055 if (gtk_drag_get_source_widget (context) == widget)
4057 g_signal_stop_emission_by_name (widget, "drag-data-received");
4061 /* Parse the text/uri-list string, navigate to the first one */
4062 uris = gtk_selection_data_get_uris (selection_data);
4063 if (uris && uris[0])
4065 struct FileListDragData *data;
4068 file = g_file_new_for_uri (uri);
4070 data = g_new0 (struct FileListDragData, 1);
4071 data->impl = g_object_ref (impl);
4075 if (impl->file_list_drag_data_received_cancellable)
4076 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4078 impl->file_list_drag_data_received_cancellable =
4079 _gtk_file_system_get_info (impl->file_system, file,
4081 file_list_drag_data_received_get_info_cb,
4085 g_signal_stop_emission_by_name (widget, "drag-data-received");
4088 /* Don't do anything with the drag_drop signal */
4090 file_list_drag_drop_cb (GtkWidget *widget,
4091 GdkDragContext *context,
4095 GtkFileChooserDefault *impl)
4097 g_signal_stop_emission_by_name (widget, "drag-drop");
4101 /* Disable the normal tree drag motion handler, it makes it look like you're
4102 dropping the dragged item onto a tree item */
4104 file_list_drag_motion_cb (GtkWidget *widget,
4105 GdkDragContext *context,
4109 GtkFileChooserDefault *impl)
4111 g_signal_stop_emission_by_name (widget, "drag-motion");
4115 /* Sensitizes the "Copy file’s location" context menu item if there is actually
4116 * a selection active.
4119 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4121 GtkTreeSelection *selection;
4124 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4125 if (gtk_tree_selection_count_selected_rows (selection) == 0)
4130 if (impl->browse_files_popup_menu_copy_file_location_item)
4131 gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4135 file_list_add_image_menu_item (GtkFileChooserDefault *impl,
4136 const char *stock_name,
4137 const char *mnemonic_label,
4142 item = gtk_image_menu_item_new_with_mnemonic (mnemonic_label);
4143 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4144 gtk_image_new_from_stock (stock_name, GTK_ICON_SIZE_MENU));
4145 g_signal_connect (item, "activate", callback, impl);
4146 gtk_widget_show (item);
4147 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4153 file_list_add_check_menu_item (GtkFileChooserDefault *impl,
4154 const char *mnemonic_label,
4159 item = gtk_check_menu_item_new_with_mnemonic (mnemonic_label);
4160 g_signal_connect (item, "toggled", callback, impl);
4161 gtk_widget_show (item);
4162 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4167 /* Constructs the popup menu for the file list if needed */
4169 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4173 if (impl->browse_files_popup_menu)
4176 impl->browse_files_popup_menu = gtk_menu_new ();
4177 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4178 impl->browse_files_tree_view,
4179 popup_menu_detach_cb);
4181 impl->browse_files_popup_menu_visit_file_item = file_list_add_image_menu_item (impl, GTK_STOCK_DIRECTORY, _("_Visit this file"),
4182 G_CALLBACK (visit_file_cb));
4184 impl->browse_files_popup_menu_copy_file_location_item = file_list_add_image_menu_item (impl, GTK_STOCK_COPY, _("_Copy file’s location"),
4185 G_CALLBACK (copy_file_location_cb));
4187 impl->browse_files_popup_menu_add_shortcut_item = file_list_add_image_menu_item (impl, GTK_STOCK_ADD, _("_Add to Bookmarks"),
4188 G_CALLBACK (add_to_shortcuts_cb));
4190 item = gtk_separator_menu_item_new ();
4191 gtk_widget_show (item);
4192 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4194 impl->browse_files_popup_menu_hidden_files_item = file_list_add_check_menu_item (impl, _("Show _Hidden Files"),
4195 G_CALLBACK (show_hidden_toggled_cb));
4197 impl->browse_files_popup_menu_size_column_item = file_list_add_check_menu_item (impl, _("Show _Size Column"),
4198 G_CALLBACK (show_size_column_toggled_cb));
4200 bookmarks_check_add_sensitivity (impl);
4201 check_copy_file_location_sensitivity (impl);
4204 /* Updates the popup menu for the file list, creating it if necessary */
4206 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4208 file_list_build_popup_menu (impl);
4210 /* The sensitivity of the Add to Bookmarks item is set in
4211 * bookmarks_check_add_sensitivity()
4214 /* 'Visit this file' */
4215 gtk_widget_set_visible (impl->browse_files_popup_menu_visit_file_item, (impl->operation_mode != OPERATION_MODE_BROWSE));
4217 /* 'Show Hidden Files' */
4218 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4219 G_CALLBACK (show_hidden_toggled_cb), impl);
4220 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4222 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4223 G_CALLBACK (show_hidden_toggled_cb), impl);
4225 /* 'Show Size Column' */
4226 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4227 G_CALLBACK (show_size_column_toggled_cb), impl);
4228 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4229 impl->show_size_column);
4230 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4231 G_CALLBACK (show_size_column_toggled_cb), impl);
4235 popup_position_func (GtkMenu *menu,
4241 GtkAllocation allocation;
4242 GtkWidget *widget = GTK_WIDGET (user_data);
4243 GdkScreen *screen = gtk_widget_get_screen (widget);
4246 GdkRectangle monitor;
4248 g_return_if_fail (gtk_widget_get_realized (widget));
4250 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4252 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4255 gtk_widget_get_allocation (widget, &allocation);
4256 *x += (allocation.width - req.width) / 2;
4257 *y += (allocation.height - req.height) / 2;
4259 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4260 gtk_menu_set_monitor (menu, monitor_num);
4261 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
4263 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4264 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4270 file_list_popup_menu (GtkFileChooserDefault *impl,
4271 GdkEventButton *event)
4273 file_list_update_popup_menu (impl);
4275 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4276 NULL, NULL, NULL, NULL,
4277 event->button, event->time);
4280 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4282 popup_position_func, impl->browse_files_tree_view,
4283 0, GDK_CURRENT_TIME);
4284 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4290 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4292 list_popup_menu_cb (GtkWidget *widget,
4293 GtkFileChooserDefault *impl)
4295 file_list_popup_menu (impl, NULL);
4299 /* Callback used when a button is pressed on the file list. We trap button 3 to
4300 * bring up a popup menu.
4303 list_button_press_event_cb (GtkWidget *widget,
4304 GdkEventButton *event,
4305 GtkFileChooserDefault *impl)
4307 static gboolean in_press = FALSE;
4312 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
4316 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4319 file_list_popup_menu (impl, event);
4324 OperationMode operation_mode;
4325 gint general_column;
4329 /* Sets the sort column IDs for the file list; needs to be done whenever we
4330 * change the model on the treeview.
4333 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4335 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4336 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4337 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4341 file_list_query_tooltip_cb (GtkWidget *widget,
4344 gboolean keyboard_tip,
4345 GtkTooltip *tooltip,
4348 GtkFileChooserDefault *impl = user_data;
4349 GtkTreeModel *model;
4355 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4359 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4362 &model, &path, &iter))
4365 gtk_tree_model_get (model, &iter,
4366 MODEL_COL_FILE, &file,
4371 gtk_tree_path_free (path);
4375 filename = g_file_get_path (file);
4376 gtk_tooltip_set_text (tooltip, filename);
4377 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4382 g_object_unref (file);
4383 gtk_tree_path_free (path);
4389 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4393 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4394 gtk_cell_renderer_set_fixed_size (renderer,
4395 xpad * 2 + impl->icon_size,
4396 ypad * 2 + impl->icon_size);
4399 /* Creates the widgets for the file list */
4401 create_file_list (GtkFileChooserDefault *impl)
4404 GtkTreeSelection *selection;
4405 GtkTreeViewColumn *column;
4406 GtkCellRenderer *renderer;
4408 /* Scrolled window */
4409 swin = gtk_scrolled_window_new (NULL, NULL);
4410 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4411 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4412 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4415 /* Tree/list view */
4417 impl->browse_files_tree_view = gtk_tree_view_new ();
4418 #ifdef PROFILE_FILE_CHOOSER
4419 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4421 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4422 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4424 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4425 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4427 gtk_drag_dest_set (impl->browse_files_tree_view,
4428 GTK_DEST_DEFAULT_ALL,
4430 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4431 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4433 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4434 G_CALLBACK (list_row_activated), impl);
4435 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4436 G_CALLBACK (browse_files_key_press_event_cb), impl);
4437 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4438 G_CALLBACK (list_popup_menu_cb), impl);
4439 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4440 G_CALLBACK (list_button_press_event_cb), impl);
4442 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4443 G_CALLBACK (file_list_drag_data_received_cb), impl);
4444 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4445 G_CALLBACK (file_list_drag_drop_cb), impl);
4446 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4447 G_CALLBACK (file_list_drag_motion_cb), impl);
4449 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4450 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4451 G_CALLBACK (file_list_query_tooltip_cb), impl);
4453 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4454 gtk_tree_selection_set_select_function (selection,
4457 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4460 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4461 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4463 g_signal_connect (selection, "changed",
4464 G_CALLBACK (list_selection_changed), impl);
4466 /* Keep the column order in sync with update_cell_renderer_attributes() */
4468 /* Filename column */
4470 impl->list_name_column = gtk_tree_view_column_new ();
4471 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4472 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4473 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4475 renderer = gtk_cell_renderer_pixbuf_new ();
4476 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4477 set_icon_cell_renderer_fixed_size (impl, renderer);
4478 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4480 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4481 g_object_set (impl->list_name_renderer,
4482 "ellipsize", PANGO_ELLIPSIZE_END,
4484 g_signal_connect (impl->list_name_renderer, "edited",
4485 G_CALLBACK (renderer_edited_cb), impl);
4486 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4487 G_CALLBACK (renderer_editing_canceled_cb), impl);
4488 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4490 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4494 column = gtk_tree_view_column_new ();
4495 gtk_tree_view_column_set_resizable (column, TRUE);
4496 gtk_tree_view_column_set_title (column, _("Size"));
4498 renderer = gtk_cell_renderer_text_new ();
4499 g_object_set (renderer,
4500 "alignment", PANGO_ALIGN_RIGHT,
4502 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4503 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4504 impl->list_size_column = column;
4506 /* Modification time column */
4508 column = gtk_tree_view_column_new ();
4509 gtk_tree_view_column_set_resizable (column, TRUE);
4510 gtk_tree_view_column_set_title (column, _("Modified"));
4512 renderer = gtk_cell_renderer_text_new ();
4513 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4514 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4515 impl->list_mtime_column = column;
4517 file_list_set_sort_column_ids (impl);
4518 update_cell_renderer_attributes (impl);
4520 gtk_widget_show_all (swin);
4525 /* Creates the widgets for the files/folders pane */
4527 file_pane_create (GtkFileChooserDefault *impl,
4528 GtkSizeGroup *size_group)
4534 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4535 gtk_widget_show (vbox);
4537 /* Box for lists and preview */
4539 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4540 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4541 gtk_widget_show (hbox);
4545 widget = create_file_list (impl);
4546 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4547 gtk_size_group_add_widget (size_group, widget);
4551 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4552 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4553 /* Don't show preview box initially */
4557 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4559 widget = filter_create (impl);
4561 gtk_widget_show (widget);
4562 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4564 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4570 location_entry_create (GtkFileChooserDefault *impl)
4572 if (!impl->location_entry)
4573 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4575 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4576 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4577 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4578 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4581 /* Creates the widgets specific to Save mode */
4583 save_widgets_create (GtkFileChooserDefault *impl)
4588 if (impl->save_widgets != NULL)
4591 location_switch_to_path_bar (impl);
4593 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4595 impl->save_widgets_table = gtk_grid_new ();
4596 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4597 gtk_widget_show (impl->save_widgets_table);
4598 gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4599 gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4603 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4604 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4605 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4606 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4607 gtk_widget_show (widget);
4609 /* Location entry */
4611 location_entry_create (impl);
4612 gtk_widget_set_hexpand (impl->location_entry, TRUE);
4613 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4614 gtk_widget_show (impl->location_entry);
4615 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4618 impl->save_folder_label = gtk_label_new (NULL);
4619 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4620 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4621 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4622 gtk_widget_show (impl->save_folder_label);
4624 impl->save_widgets = vbox;
4625 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4626 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4627 gtk_widget_show (impl->save_widgets);
4630 /* Destroys the widgets specific to Save mode */
4632 save_widgets_destroy (GtkFileChooserDefault *impl)
4634 if (impl->save_widgets == NULL)
4637 gtk_widget_destroy (impl->save_widgets);
4638 impl->save_widgets = NULL;
4639 impl->save_widgets_table = NULL;
4640 impl->location_entry = NULL;
4641 impl->save_folder_label = NULL;
4644 /* Turns on the path bar widget. Can be called even if we are already in that
4648 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4650 if (impl->location_entry)
4652 gtk_widget_destroy (impl->location_entry);
4653 impl->location_entry = NULL;
4656 gtk_widget_hide (impl->location_entry_box);
4659 /* Turns on the location entry. Can be called even if we are already in that
4663 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4665 /* when in search or recent files mode, we are not showing the
4666 * location_entry_box container, so there's no point in switching
4669 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4670 impl->operation_mode == OPERATION_MODE_RECENT)
4675 gtk_widget_show (impl->location_entry_box);
4679 if (!impl->location_entry)
4681 location_entry_create (impl);
4682 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4683 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4686 /* Configure the entry */
4688 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4692 gtk_widget_show (impl->location_entry);
4693 gtk_widget_grab_focus (impl->location_entry);
4696 /* Sets a new location mode. set_buttons determines whether the toggle button
4697 * for the mode will also be changed.
4700 location_mode_set (GtkFileChooserDefault *impl,
4701 LocationMode new_mode,
4702 gboolean set_button)
4704 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4705 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4707 GtkWindow *toplevel;
4708 GtkWidget *current_focus;
4709 gboolean button_active;
4710 gboolean switch_to_file_list;
4714 case LOCATION_MODE_PATH_BAR:
4715 button_active = FALSE;
4717 /* The location_entry will disappear when we switch to path bar mode. So,
4718 * we'll focus the file list in that case, to avoid having a window with
4719 * no focused widget.
4721 toplevel = get_toplevel (GTK_WIDGET (impl));
4722 switch_to_file_list = FALSE;
4725 current_focus = gtk_window_get_focus (toplevel);
4726 if (!current_focus || current_focus == impl->location_entry)
4727 switch_to_file_list = TRUE;
4730 location_switch_to_path_bar (impl);
4732 if (switch_to_file_list)
4733 gtk_widget_grab_focus (impl->browse_files_tree_view);
4737 case LOCATION_MODE_FILENAME_ENTRY:
4738 button_active = TRUE;
4739 location_switch_to_filename_entry (impl);
4743 g_assert_not_reached ();
4749 g_signal_handlers_block_by_func (impl->location_button,
4750 G_CALLBACK (location_button_toggled_cb), impl);
4752 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4754 g_signal_handlers_unblock_by_func (impl->location_button,
4755 G_CALLBACK (location_button_toggled_cb), impl);
4759 impl->location_mode = new_mode;
4763 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4765 /* when in search or recent files mode, we are not showing the
4766 * location_entry_box container, so there's no point in switching
4769 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4770 impl->operation_mode == OPERATION_MODE_RECENT)
4773 /* If the file entry is not visible, show it.
4774 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4776 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4778 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4780 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4782 if (gtk_widget_has_focus (impl->location_entry))
4784 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4788 gtk_widget_grab_focus (impl->location_entry);
4793 /* Callback used when one of the location mode buttons is toggled */
4795 location_button_toggled_cb (GtkToggleButton *toggle,
4796 GtkFileChooserDefault *impl)
4799 LocationMode new_mode;
4801 is_active = gtk_toggle_button_get_active (toggle);
4805 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4806 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4810 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4811 new_mode = LOCATION_MODE_PATH_BAR;
4814 location_mode_set (impl, new_mode, FALSE);
4817 /* Creates a toggle button for the location entry. */
4819 location_button_create (GtkFileChooserDefault *impl)
4824 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4825 gtk_widget_show (image);
4827 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4831 g_signal_connect (impl->location_button, "toggled",
4832 G_CALLBACK (location_button_toggled_cb), impl);
4834 str = _("Type a file name");
4836 gtk_widget_set_tooltip_text (impl->location_button, str);
4837 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4841 PATH_BAR_FOLDER_PATH,
4842 PATH_BAR_SELECT_A_FOLDER,
4843 PATH_BAR_ERROR_NO_FILENAME,
4844 PATH_BAR_ERROR_NO_FOLDER,
4845 PATH_BAR_RECENTLY_USED,
4849 /* Creates the info bar for informational messages or warnings, with its icon and label */
4851 info_bar_create (GtkFileChooserDefault *impl)
4853 GtkWidget *content_area;
4855 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4856 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4857 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4859 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4861 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4862 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4864 gtk_widget_show (impl->browse_select_a_folder_icon);
4865 gtk_widget_show (impl->browse_select_a_folder_label);
4868 /* Sets the info bar to show the appropriate informational or warning message */
4870 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4874 GtkMessageType message_type;
4880 case PATH_BAR_SELECT_A_FOLDER:
4881 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4883 message_type = GTK_MESSAGE_OTHER;
4886 case PATH_BAR_ERROR_NO_FILENAME:
4887 str = _("Please type a file name");
4888 message_type = GTK_MESSAGE_WARNING;
4891 case PATH_BAR_ERROR_NO_FOLDER:
4892 str = _("Please select a folder below");
4893 message_type = GTK_MESSAGE_WARNING;
4897 g_assert_not_reached ();
4901 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4902 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4903 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4904 GTK_ICON_SIZE_MENU);
4905 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4911 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4913 special_mode_widgets_create (GtkFileChooserDefault *impl)
4915 impl->browse_special_mode_icon = gtk_image_new ();
4916 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4917 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4919 impl->browse_special_mode_label = gtk_label_new (NULL);
4920 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4921 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4924 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4926 path_bar_widgets_create (GtkFileChooserDefault *impl)
4928 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4929 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4930 gtk_widget_show (impl->browse_path_bar_hbox);
4932 /* Size group that allows the path bar to be the same size between modes */
4933 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4934 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4936 /* Location button */
4937 location_button_create (impl);
4938 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4939 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4942 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4943 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4944 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4946 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4947 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4950 info_bar_create (impl);
4951 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4952 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4954 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4955 special_mode_widgets_create (impl);
4958 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4959 g_signal_connect (impl->browse_new_folder_button, "clicked",
4960 G_CALLBACK (new_folder_button_clicked), impl);
4961 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4962 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4965 /* Sets the path bar's mode to show a label, the actual folder path, or a
4966 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4967 * if the pathbar is already showing the widgets you expect; otherwise, call
4968 * path_bar_update() instead to set the appropriate widgets automatically.
4971 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4973 gboolean path_bar_visible = FALSE;
4974 gboolean special_mode_widgets_visible = FALSE;
4975 gboolean info_bar_visible = FALSE;
4976 gboolean create_folder_visible = FALSE;
4982 case PATH_BAR_FOLDER_PATH:
4983 path_bar_visible = TRUE;
4986 case PATH_BAR_SELECT_A_FOLDER:
4987 case PATH_BAR_ERROR_NO_FILENAME:
4988 case PATH_BAR_ERROR_NO_FOLDER:
4989 info_bar_set (impl, mode);
4990 info_bar_visible = TRUE;
4993 case PATH_BAR_RECENTLY_USED:
4994 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4996 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4997 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
5000 special_mode_widgets_visible = TRUE;
5003 case PATH_BAR_SEARCH:
5004 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
5006 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
5007 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
5010 special_mode_widgets_visible = TRUE;
5014 g_assert_not_reached ();
5017 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
5018 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
5019 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
5020 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
5022 if (path_bar_visible)
5024 if (impl->create_folders
5025 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
5026 && impl->operation_mode != OPERATION_MODE_RECENT)
5027 create_folder_visible = TRUE;
5030 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
5033 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5035 browse_widgets_create (GtkFileChooserDefault *impl)
5039 GtkSizeGroup *size_group;
5041 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5042 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5043 gtk_widget_show (impl->browse_widgets_box);
5045 impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5046 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5047 gtk_widget_show (impl->browse_header_box);
5049 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5050 path_bar_widgets_create (impl);
5052 /* Box for the location label and entry */
5054 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5055 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5057 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5058 gtk_widget_show (impl->location_label);
5059 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5061 /* size group is used by the scrolled windows of the panes */
5062 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5066 hpaned = impl->browse_widgets_hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5067 gtk_widget_show (hpaned);
5068 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5070 widget = shortcuts_pane_create (impl, size_group);
5071 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5072 widget = file_pane_create (impl, size_group);
5073 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5074 g_object_unref (size_group);
5078 gtk_file_chooser_default_constructor (GType type,
5079 guint n_construct_properties,
5080 GObjectConstructParam *construct_params)
5082 GtkFileChooserDefault *impl;
5085 profile_start ("start", NULL);
5087 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5088 n_construct_properties,
5090 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5092 g_assert (impl->file_system);
5094 gtk_widget_push_composite_child ();
5096 /* Shortcuts model */
5097 shortcuts_model_create (impl);
5099 /* The browse widgets */
5100 browse_widgets_create (impl);
5102 /* Alignment to hold extra widget */
5103 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5104 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5105 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5106 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5108 gtk_widget_pop_composite_child ();
5109 update_appearance (impl);
5111 profile_end ("end", NULL);
5116 /* Sets the extra_widget by packing it in the appropriate place */
5118 set_extra_widget (GtkFileChooserDefault *impl,
5119 GtkWidget *extra_widget)
5123 g_object_ref (extra_widget);
5124 /* FIXME: is this right ? */
5125 gtk_widget_show (extra_widget);
5128 if (impl->extra_widget)
5130 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5131 g_object_unref (impl->extra_widget);
5134 impl->extra_widget = extra_widget;
5135 if (impl->extra_widget)
5137 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5138 gtk_widget_show (impl->extra_align);
5141 gtk_widget_hide (impl->extra_align);
5145 set_local_only (GtkFileChooserDefault *impl,
5146 gboolean local_only)
5148 if (local_only != impl->local_only)
5150 impl->local_only = local_only;
5152 if (impl->location_entry)
5153 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5155 if (impl->shortcuts_model && impl->file_system)
5157 shortcuts_add_volumes (impl);
5158 shortcuts_add_bookmarks (impl);
5161 if (local_only && impl->current_folder &&
5162 !g_file_is_native (impl->current_folder))
5164 /* If we are pointing to a non-local folder, make an effort to change
5165 * back to a local folder, but it's really up to the app to not cause
5166 * such a situation, so we ignore errors.
5168 const gchar *home = g_get_home_dir ();
5174 home_file = g_file_new_for_path (home);
5176 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5178 g_object_unref (home_file);
5184 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5185 GtkFileChooserDefault *impl)
5187 shortcuts_add_volumes (impl);
5188 shortcuts_add_bookmarks (impl);
5190 bookmarks_check_add_sensitivity (impl);
5191 bookmarks_check_remove_sensitivity (impl);
5192 shortcuts_check_popup_sensitivity (impl);
5195 /* Sets the file chooser to multiple selection mode */
5197 set_select_multiple (GtkFileChooserDefault *impl,
5198 gboolean select_multiple,
5199 gboolean property_notify)
5201 GtkTreeSelection *selection;
5202 GtkSelectionMode mode;
5204 if (select_multiple == impl->select_multiple)
5207 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
5209 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5210 gtk_tree_selection_set_mode (selection, mode);
5212 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5214 impl->select_multiple = select_multiple;
5215 g_object_notify (G_OBJECT (impl), "select-multiple");
5217 check_preview_change (impl);
5221 set_file_system_backend (GtkFileChooserDefault *impl)
5223 profile_start ("start for backend", "default");
5225 impl->file_system = _gtk_file_system_new ();
5227 g_signal_connect (impl->file_system, "volumes-changed",
5228 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5229 g_signal_connect (impl->file_system, "bookmarks-changed",
5230 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5232 profile_end ("end", NULL);
5236 unset_file_system_backend (GtkFileChooserDefault *impl)
5238 g_signal_handlers_disconnect_by_func (impl->file_system,
5239 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5241 g_object_unref (impl->file_system);
5243 impl->file_system = NULL;
5246 /* Saves the widgets around the pathbar so they can be reparented later
5247 * in the correct place. This function must be called paired with
5248 * restore_path_bar().
5251 save_path_bar (GtkFileChooserDefault *impl)
5255 g_object_ref (impl->browse_path_bar_hbox);
5257 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5259 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5262 /* Reparents the path bar and the "Create folder" button to the right place:
5263 * Above the file list in Open mode, or to the right of the "Save in folder:"
5264 * label in Save mode. The save_path_bar() function must be called before this
5268 restore_path_bar (GtkFileChooserDefault *impl)
5270 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5271 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5273 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5274 gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5276 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5277 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5279 gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5280 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5281 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5284 g_assert_not_reached ();
5286 g_object_unref (impl->browse_path_bar_hbox);
5289 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5291 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5295 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5296 MODEL_COL_FILE, &file,
5298 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE);
5299 g_object_unref (file);
5302 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5303 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5304 * function will update all the pathbar's widgets.
5307 path_bar_update (GtkFileChooserDefault *impl)
5311 switch (impl->operation_mode)
5313 case OPERATION_MODE_BROWSE:
5314 mode = PATH_BAR_FOLDER_PATH;
5317 case OPERATION_MODE_RECENT:
5318 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5320 GtkTreeSelection *selection;
5321 gboolean have_selected;
5324 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5326 /* Save mode means single-selection mode, so the following is valid */
5327 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5331 mode = PATH_BAR_FOLDER_PATH;
5332 put_recent_folder_in_pathbar (impl, &iter);
5335 mode = PATH_BAR_SELECT_A_FOLDER;
5338 mode = PATH_BAR_RECENTLY_USED;
5342 case OPERATION_MODE_SEARCH:
5343 mode = PATH_BAR_SEARCH;
5347 g_assert_not_reached ();
5351 path_bar_set_mode (impl, mode);
5355 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5357 if (impl->search_hbox)
5359 gtk_widget_destroy (impl->search_hbox);
5361 impl->search_hbox = NULL;
5362 impl->search_entry = NULL;
5366 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5368 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5372 case OPERATION_MODE_BROWSE:
5373 stop_loading_and_clear_list_model (impl, TRUE);
5376 case OPERATION_MODE_SEARCH:
5377 search_stop_searching (impl, FALSE);
5378 search_clear_model (impl, TRUE);
5380 operation_mode_discard_search_widgets (impl);
5383 case OPERATION_MODE_RECENT:
5384 recent_stop_loading (impl);
5385 recent_clear_model (impl, TRUE);
5389 g_assert_not_reached ();
5394 operation_mode_set_browse (GtkFileChooserDefault *impl)
5396 path_bar_update (impl);
5398 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5399 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5401 gtk_widget_show (impl->location_button);
5402 location_mode_set (impl, impl->location_mode, TRUE);
5404 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5405 gtk_widget_show (impl->location_entry_box);
5410 operation_mode_set_search (GtkFileChooserDefault *impl)
5412 g_assert (impl->search_hbox == NULL);
5413 g_assert (impl->search_entry == NULL);
5414 g_assert (impl->search_model == NULL);
5416 search_setup_widgets (impl);
5420 operation_mode_set_recent (GtkFileChooserDefault *impl)
5422 path_bar_update (impl);
5424 /* Hide the location widgets temporarily */
5425 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5426 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5428 gtk_widget_hide (impl->location_button);
5429 gtk_widget_hide (impl->location_entry_box);
5432 recent_start_loading (impl);
5435 /* Sometimes we need to frob the selection in the shortcuts list manually */
5437 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5439 GtkTreeSelection *selection;
5442 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5444 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5446 path = gtk_tree_path_new_from_indices (pos, -1);
5447 gtk_tree_selection_select_path (selection, path);
5448 gtk_tree_path_free (path);
5450 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5454 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5456 ShortcutsIndex shortcut_to_select;
5458 operation_mode_stop (impl, impl->operation_mode);
5460 impl->operation_mode = mode;
5462 switch (impl->operation_mode)
5464 case OPERATION_MODE_BROWSE:
5465 operation_mode_set_browse (impl);
5466 shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5469 case OPERATION_MODE_SEARCH:
5470 operation_mode_set_search (impl);
5471 shortcut_to_select = SHORTCUTS_SEARCH;
5474 case OPERATION_MODE_RECENT:
5475 operation_mode_set_recent (impl);
5476 shortcut_to_select = SHORTCUTS_RECENT;
5480 g_assert_not_reached ();
5484 if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5485 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5488 /* This function is basically a do_all function.
5490 * It sets the visibility on all the widgets based on the current state, and
5491 * moves the custom_widget if needed.
5494 update_appearance (GtkFileChooserDefault *impl)
5496 save_path_bar (impl);
5498 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5499 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5503 gtk_widget_hide (impl->location_button);
5504 save_widgets_create (impl);
5506 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5507 text = _("Save in _folder:");
5509 text = _("Create in _folder:");
5511 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5513 if (impl->select_multiple)
5515 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5516 "Re-setting to single selection mode.");
5517 set_select_multiple (impl, FALSE, TRUE);
5520 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5521 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5523 gtk_widget_show (impl->location_button);
5524 save_widgets_destroy (impl);
5525 location_mode_set (impl, impl->location_mode, TRUE);
5528 if (impl->location_entry)
5529 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5531 restore_path_bar (impl);
5532 path_bar_update (impl);
5534 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5535 * of files may change depending whether we are in a file or folder-only mode.
5537 gtk_widget_queue_draw (impl->browse_files_tree_view);
5539 emit_default_size_changed (impl);
5543 gtk_file_chooser_default_set_property (GObject *object,
5545 const GValue *value,
5549 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5553 case GTK_FILE_CHOOSER_PROP_ACTION:
5555 GtkFileChooserAction action = g_value_get_enum (value);
5557 if (action != impl->action)
5559 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5561 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5562 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5563 && impl->select_multiple)
5565 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5566 "this is not allowed in multiple selection mode. Resetting the file chooser "
5567 "to single selection mode.");
5568 set_select_multiple (impl, FALSE, TRUE);
5570 impl->action = action;
5571 update_cell_renderer_attributes (impl);
5572 update_appearance (impl);
5573 settings_load (impl);
5578 case GTK_FILE_CHOOSER_PROP_FILTER:
5579 set_current_filter (impl, g_value_get_object (value));
5582 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5583 set_local_only (impl, g_value_get_boolean (value));
5586 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5587 set_preview_widget (impl, g_value_get_object (value));
5590 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5591 impl->preview_widget_active = g_value_get_boolean (value);
5592 update_preview_widget_visibility (impl);
5595 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5596 impl->use_preview_label = g_value_get_boolean (value);
5597 update_preview_widget_visibility (impl);
5600 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5601 set_extra_widget (impl, g_value_get_object (value));
5604 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5606 gboolean select_multiple = g_value_get_boolean (value);
5607 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5608 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5611 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5612 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5613 "leaving the file chooser in single selection mode.");
5617 set_select_multiple (impl, select_multiple, FALSE);
5621 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5623 gboolean show_hidden = g_value_get_boolean (value);
5624 if (show_hidden != impl->show_hidden)
5626 impl->show_hidden = show_hidden;
5628 if (impl->browse_files_model)
5629 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5634 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5636 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5637 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5641 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5643 gboolean create_folders = g_value_get_boolean (value);
5644 impl->create_folders = create_folders;
5645 update_appearance (impl);
5650 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5656 gtk_file_chooser_default_get_property (GObject *object,
5661 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5665 case GTK_FILE_CHOOSER_PROP_ACTION:
5666 g_value_set_enum (value, impl->action);
5669 case GTK_FILE_CHOOSER_PROP_FILTER:
5670 g_value_set_object (value, impl->current_filter);
5673 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5674 g_value_set_boolean (value, impl->local_only);
5677 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5678 g_value_set_object (value, impl->preview_widget);
5681 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5682 g_value_set_boolean (value, impl->preview_widget_active);
5685 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5686 g_value_set_boolean (value, impl->use_preview_label);
5689 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5690 g_value_set_object (value, impl->extra_widget);
5693 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5694 g_value_set_boolean (value, impl->select_multiple);
5697 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5698 g_value_set_boolean (value, impl->show_hidden);
5701 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5702 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5705 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5706 g_value_set_boolean (value, impl->create_folders);
5710 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5715 /* This cancels everything that may be going on in the background. */
5717 cancel_all_operations (GtkFileChooserDefault *impl)
5721 pending_select_files_free (impl);
5723 if (impl->reload_icon_cancellables)
5725 for (l = impl->reload_icon_cancellables; l; l = l->next)
5727 GCancellable *cancellable = G_CANCELLABLE (l->data);
5728 g_cancellable_cancel (cancellable);
5730 g_slist_free (impl->reload_icon_cancellables);
5731 impl->reload_icon_cancellables = NULL;
5734 if (impl->loading_shortcuts)
5736 for (l = impl->loading_shortcuts; l; l = l->next)
5738 GCancellable *cancellable = G_CANCELLABLE (l->data);
5739 g_cancellable_cancel (cancellable);
5741 g_slist_free (impl->loading_shortcuts);
5742 impl->loading_shortcuts = NULL;
5745 if (impl->file_list_drag_data_received_cancellable)
5747 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5748 impl->file_list_drag_data_received_cancellable = NULL;
5751 if (impl->update_current_folder_cancellable)
5753 g_cancellable_cancel (impl->update_current_folder_cancellable);
5754 impl->update_current_folder_cancellable = NULL;
5757 if (impl->should_respond_get_info_cancellable)
5759 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5760 impl->should_respond_get_info_cancellable = NULL;
5763 if (impl->file_exists_get_info_cancellable)
5765 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
5766 impl->file_exists_get_info_cancellable = NULL;
5769 if (impl->update_from_entry_cancellable)
5771 g_cancellable_cancel (impl->update_from_entry_cancellable);
5772 impl->update_from_entry_cancellable = NULL;
5775 if (impl->shortcuts_activate_iter_cancellable)
5777 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5778 impl->shortcuts_activate_iter_cancellable = NULL;
5781 search_stop_searching (impl, TRUE);
5782 recent_stop_loading (impl);
5785 /* Removes the settings signal handler. It's safe to call multiple times */
5787 remove_settings_signal (GtkFileChooserDefault *impl,
5790 if (impl->settings_signal_id)
5792 GtkSettings *settings;
5794 settings = gtk_settings_get_for_screen (screen);
5795 g_signal_handler_disconnect (settings,
5796 impl->settings_signal_id);
5797 impl->settings_signal_id = 0;
5802 gtk_file_chooser_default_dispose (GObject *object)
5804 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5806 cancel_all_operations (impl);
5808 if (impl->extra_widget)
5810 g_object_unref (impl->extra_widget);
5811 impl->extra_widget = NULL;
5814 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5816 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5819 /* We override show-all since we have internal widgets that
5820 * shouldn't be shown when you call show_all(), like the filter
5824 gtk_file_chooser_default_show_all (GtkWidget *widget)
5826 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5828 gtk_widget_show (widget);
5830 if (impl->extra_widget)
5831 gtk_widget_show_all (impl->extra_widget);
5834 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5835 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5838 toplevel_set_focus_cb (GtkWindow *window,
5840 GtkFileChooserDefault *impl)
5842 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5845 /* We monitor the focus widget on our toplevel to be able to know which widget
5846 * was last focused at the time our "should_respond" method gets called.
5849 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5850 GtkWidget *previous_toplevel)
5852 GtkFileChooserDefault *impl;
5853 GtkWidget *toplevel;
5855 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5856 toplevel = gtk_widget_get_toplevel (widget);
5858 if (previous_toplevel &&
5859 impl->toplevel_set_focus_id != 0)
5861 g_signal_handler_disconnect (previous_toplevel,
5862 impl->toplevel_set_focus_id);
5863 impl->toplevel_set_focus_id = 0;
5864 impl->toplevel_last_focus_widget = NULL;
5867 if (gtk_widget_is_toplevel (toplevel))
5869 g_assert (impl->toplevel_set_focus_id == 0);
5870 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5871 G_CALLBACK (toplevel_set_focus_cb), impl);
5872 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5876 /* Changes the icons wherever it is needed */
5878 change_icon_theme (GtkFileChooserDefault *impl)
5880 GtkSettings *settings;
5882 GtkCellRenderer *renderer;
5885 profile_start ("start", NULL);
5887 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5889 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5890 impl->icon_size = MAX (width, height);
5892 impl->icon_size = FALLBACK_ICON_SIZE;
5894 shortcuts_reload_icons (impl);
5895 /* the first cell in the first column is the icon column, and we have a fixed size there */
5896 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5897 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5898 renderer = GTK_CELL_RENDERER (cells->data);
5899 set_icon_cell_renderer_fixed_size (impl, renderer);
5900 g_list_free (cells);
5901 if (impl->browse_files_model)
5902 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5903 gtk_widget_queue_resize (impl->browse_files_tree_view);
5905 profile_end ("end", NULL);
5908 /* Callback used when a GtkSettings value changes */
5910 settings_notify_cb (GObject *object,
5912 GtkFileChooserDefault *impl)
5916 profile_start ("start", NULL);
5918 name = g_param_spec_get_name (pspec);
5920 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5921 strcmp (name, "gtk-icon-sizes") == 0)
5922 change_icon_theme (impl);
5924 profile_end ("end", NULL);
5927 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5931 check_icon_theme (GtkFileChooserDefault *impl)
5933 GtkSettings *settings;
5935 profile_start ("start", NULL);
5937 if (impl->settings_signal_id)
5939 profile_end ("end", NULL);
5943 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5945 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5946 impl->settings_signal_id = g_signal_connect (settings, "notify",
5947 G_CALLBACK (settings_notify_cb), impl);
5949 change_icon_theme (impl);
5952 profile_end ("end", NULL);
5956 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5958 GtkFileChooserDefault *impl;
5960 profile_start ("start", NULL);
5962 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5964 profile_msg (" parent class style_udpated start", NULL);
5965 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5966 profile_msg (" parent class style_updated end", NULL);
5968 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5969 change_icon_theme (impl);
5971 emit_default_size_changed (impl);
5973 profile_end ("end", NULL);
5977 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5978 GdkScreen *previous_screen)
5980 GtkFileChooserDefault *impl;
5982 profile_start ("start", NULL);
5984 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5986 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5987 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5989 remove_settings_signal (impl, previous_screen);
5990 check_icon_theme (impl);
5992 emit_default_size_changed (impl);
5994 profile_end ("end", NULL);
5998 set_sort_column (GtkFileChooserDefault *impl)
6000 GtkTreeSortable *sortable;
6002 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6004 /* can happen when we're still populating the model */
6005 if (sortable == NULL)
6008 gtk_tree_sortable_set_sort_column_id (sortable,
6014 settings_load (GtkFileChooserDefault *impl)
6016 LocationMode location_mode;
6017 gboolean show_hidden;
6018 gboolean show_size_column;
6020 GtkSortType sort_order;
6022 GSettings *settings;
6024 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
6026 location_mode = g_settings_get_enum (settings, SETTINGS_KEY_LOCATION_MODE);
6027 show_hidden = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN);
6028 show_size_column = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6029 sort_column = g_settings_get_enum (settings, SETTINGS_KEY_SORT_COLUMN);
6030 sort_order = g_settings_get_enum (settings, SETTINGS_KEY_SORT_ORDER);
6031 sidebar_width = g_settings_get_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH);
6033 location_mode_set (impl, location_mode, TRUE);
6035 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6037 impl->show_size_column = show_size_column;
6038 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6040 impl->sort_column = sort_column;
6041 impl->sort_order = sort_order;
6042 /* We don't call set_sort_column() here as the models may not have been
6043 * created yet. The individual functions that create and set the models will
6044 * call set_sort_column() themselves.
6047 gtk_paned_set_position (GTK_PANED (impl->browse_widgets_hpaned), sidebar_width);
6051 settings_save (GtkFileChooserDefault *impl)
6053 GSettings *settings;
6055 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
6057 /* All the other state */
6059 g_settings_set_enum (settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6060 g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN,
6061 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6062 g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6063 g_settings_set_enum (settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6064 g_settings_set_enum (settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6065 g_settings_set_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH,
6066 gtk_paned_get_position (GTK_PANED (impl->browse_widgets_hpaned)));
6068 /* Now apply the settings */
6069 g_settings_apply (settings);
6072 /* GtkWidget::realize method */
6074 gtk_file_chooser_default_realize (GtkWidget *widget)
6076 GtkFileChooserDefault *impl;
6078 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6080 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6082 emit_default_size_changed (impl);
6085 /* GtkWidget::map method */
6087 gtk_file_chooser_default_map (GtkWidget *widget)
6089 GtkFileChooserDefault *impl;
6091 profile_start ("start", NULL);
6093 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6095 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6097 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6099 switch (impl->reload_state)
6102 recent_shortcut_handler (impl);
6105 case RELOAD_HAS_FOLDER:
6106 /* Nothing; we are already loading or loaded, so we
6107 * don't need to reload
6112 g_assert_not_reached ();
6116 volumes_bookmarks_changed_cb (impl->file_system, impl);
6118 settings_load (impl);
6120 profile_end ("end", NULL);
6123 /* GtkWidget::unmap method */
6125 gtk_file_chooser_default_unmap (GtkWidget *widget)
6127 GtkFileChooserDefault *impl;
6129 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6131 settings_save (impl);
6133 cancel_all_operations (impl);
6134 impl->reload_state = RELOAD_EMPTY;
6136 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6139 #define COMPARE_DIRECTORIES \
6140 GtkFileChooserDefault *impl = user_data; \
6141 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6142 gboolean dir_a, dir_b; \
6144 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6145 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6147 if (dir_a != dir_b) \
6148 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6150 /* Sort callback for the filename column */
6152 name_sort_func (GtkTreeModel *model,
6157 COMPARE_DIRECTORIES;
6160 const char *key_a, *key_b;
6163 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6164 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6167 result = strcmp (key_a, key_b);
6179 /* Sort callback for the size column */
6181 size_sort_func (GtkTreeModel *model,
6186 COMPARE_DIRECTORIES;
6189 gint64 size_a, size_b;
6191 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6192 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6194 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6198 /* Sort callback for the mtime column */
6200 mtime_sort_func (GtkTreeModel *model,
6205 COMPARE_DIRECTORIES;
6210 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6211 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6213 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6217 /* Callback used when the sort column changes. We cache the sort order for use
6218 * in name_sort_func().
6221 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6222 GtkFileChooserDefault *impl)
6224 gint sort_column_id;
6225 GtkSortType sort_type;
6227 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6229 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6230 impl->sort_column = sort_column_id;
6231 impl->sort_order = sort_type;
6236 set_busy_cursor (GtkFileChooserDefault *impl,
6240 GtkWindow *toplevel;
6241 GdkDisplay *display;
6244 toplevel = get_toplevel (GTK_WIDGET (impl));
6245 widget = GTK_WIDGET (toplevel);
6246 if (!toplevel || !gtk_widget_get_realized (widget))
6249 display = gtk_widget_get_display (widget);
6252 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6256 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6257 gdk_display_flush (display);
6260 g_object_unref (cursor);
6263 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6265 load_set_model (GtkFileChooserDefault *impl)
6267 profile_start ("start", NULL);
6269 g_assert (impl->browse_files_model != NULL);
6271 profile_msg (" gtk_tree_view_set_model start", NULL);
6272 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6273 GTK_TREE_MODEL (impl->browse_files_model));
6274 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6275 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6277 file_list_set_sort_column_ids (impl);
6278 set_sort_column (impl);
6279 profile_msg (" gtk_tree_view_set_model end", NULL);
6280 impl->list_sort_ascending = TRUE;
6282 profile_end ("end", NULL);
6285 /* Timeout callback used when the loading timer expires */
6287 load_timeout_cb (gpointer data)
6289 GtkFileChooserDefault *impl;
6291 profile_start ("start", NULL);
6293 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6294 g_assert (impl->load_state == LOAD_PRELOAD);
6295 g_assert (impl->load_timeout_id != 0);
6296 g_assert (impl->browse_files_model != NULL);
6298 impl->load_timeout_id = 0;
6299 impl->load_state = LOAD_LOADING;
6301 load_set_model (impl);
6303 profile_end ("end", NULL);
6308 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6310 load_setup_timer (GtkFileChooserDefault *impl)
6312 g_assert (impl->load_timeout_id == 0);
6313 g_assert (impl->load_state != LOAD_PRELOAD);
6315 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6316 impl->load_state = LOAD_PRELOAD;
6319 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6321 load_remove_timer (GtkFileChooserDefault *impl)
6323 if (impl->load_timeout_id != 0)
6325 g_assert (impl->load_state == LOAD_PRELOAD);
6327 g_source_remove (impl->load_timeout_id);
6328 impl->load_timeout_id = 0;
6329 impl->load_state = LOAD_EMPTY;
6332 g_assert (impl->load_state == LOAD_EMPTY ||
6333 impl->load_state == LOAD_LOADING ||
6334 impl->load_state == LOAD_FINISHED);
6337 /* Selects the first row in the file list */
6339 browse_files_select_first_row (GtkFileChooserDefault *impl)
6342 GtkTreeIter dummy_iter;
6343 GtkTreeModel *tree_model;
6345 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6350 path = gtk_tree_path_new_from_indices (0, -1);
6352 /* If the list is empty, do nothing. */
6353 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6354 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6356 gtk_tree_path_free (path);
6359 struct center_selected_row_closure {
6360 GtkFileChooserDefault *impl;
6361 gboolean already_centered;
6364 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6365 * selected row in the tree view.
6368 center_selected_row_foreach_cb (GtkTreeModel *model,
6373 struct center_selected_row_closure *closure;
6376 if (closure->already_centered)
6379 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6380 closure->already_centered = TRUE;
6383 /* Centers the selected row in the tree view */
6385 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6387 struct center_selected_row_closure closure;
6388 GtkTreeSelection *selection;
6390 closure.impl = impl;
6391 closure.already_centered = FALSE;
6393 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6394 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6398 show_and_select_files (GtkFileChooserDefault *impl,
6401 GtkTreeSelection *selection;
6402 GtkFileSystemModel *fsmodel;
6403 gboolean enabled_hidden, removed_filters;
6404 gboolean selected_a_file;
6407 g_assert (impl->load_state == LOAD_FINISHED);
6408 g_assert (impl->browse_files_model != NULL);
6410 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6411 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6413 g_assert (fsmodel == impl->browse_files_model);
6415 enabled_hidden = impl->show_hidden;
6416 removed_filters = (impl->current_filter == NULL);
6418 selected_a_file = FALSE;
6420 for (walk = files; walk; walk = walk->next)
6422 GFile *file = walk->data;
6425 /* Is it a hidden file? */
6427 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6430 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6432 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6434 if (!enabled_hidden &&
6435 (g_file_info_get_is_hidden (info) ||
6436 g_file_info_get_is_backup (info)))
6438 g_object_set (impl, "show-hidden", TRUE, NULL);
6439 enabled_hidden = TRUE;
6443 /* Is it a filtered file? */
6445 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6446 continue; /* re-get the iter as it may change when the model refilters */
6448 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6450 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6451 if (!removed_filters)
6453 set_current_filter (impl, NULL);
6454 removed_filters = TRUE;
6458 /* Okay, can we select the file now? */
6460 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6463 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6467 gtk_tree_selection_select_iter (selection, &iter);
6469 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6470 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6472 gtk_tree_path_free (path);
6474 selected_a_file = TRUE;
6478 browse_files_center_selected_row (impl);
6480 return selected_a_file;
6483 /* Processes the pending operation when a folder is finished loading */
6485 pending_select_files_process (GtkFileChooserDefault *impl)
6487 g_assert (impl->load_state == LOAD_FINISHED);
6488 g_assert (impl->browse_files_model != NULL);
6490 if (impl->pending_select_files)
6492 show_and_select_files (impl, impl->pending_select_files);
6493 pending_select_files_free (impl);
6494 browse_files_center_selected_row (impl);
6498 /* We only select the first row if the chooser is actually mapped ---
6499 * selecting the first row is to help the user when he is interacting with
6500 * the chooser, but sometimes a chooser works not on behalf of the user,
6501 * but rather on behalf of something else like GtkFileChooserButton. In
6502 * that case, the chooser's selection should be what the caller expects,
6503 * as the user can't see that something else got selected. See bug #165264.
6505 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6506 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6507 browse_files_select_first_row (impl);
6510 g_assert (impl->pending_select_files == NULL);
6514 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6519 info = g_file_query_info (impl->current_folder,
6520 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6521 G_FILE_QUERY_INFO_NONE,
6526 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6527 g_object_unref (info);
6530 msg = g_strdup (_("Could not read the contents of the folder"));
6532 error_message (impl, msg, error->message);
6536 /* Callback used when the file system model finishes loading */
6538 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6540 GtkFileChooserDefault *impl)
6542 profile_start ("start", NULL);
6545 show_error_on_reading_current_folder (impl, error);
6547 if (impl->load_state == LOAD_PRELOAD)
6549 load_remove_timer (impl);
6550 load_set_model (impl);
6552 else if (impl->load_state == LOAD_LOADING)
6558 /* We can't g_assert_not_reached(), as something other than us may have
6559 * initiated a folder reload. See #165556.
6561 profile_end ("end", NULL);
6565 g_assert (impl->load_timeout_id == 0);
6567 impl->load_state = LOAD_FINISHED;
6569 pending_select_files_process (impl);
6570 set_busy_cursor (impl, FALSE);
6571 #ifdef PROFILE_FILE_CHOOSER
6572 access ("MARK: *** FINISHED LOADING", F_OK);
6575 profile_end ("end", NULL);
6579 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6580 gboolean remove_from_treeview)
6582 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6584 if (impl->browse_files_model)
6586 g_object_unref (impl->browse_files_model);
6587 impl->browse_files_model = NULL;
6590 if (remove_from_treeview)
6591 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6595 my_g_format_time_for_display (glong secs)
6600 time_t time_mtime, time_now;
6601 const gchar *format;
6602 gchar *locale_format = NULL;
6604 char *date_str = NULL;
6606 const char *locale, *dot = NULL;
6607 gint64 codepage = -1;
6613 #ifdef HAVE_LOCALTIME_R
6614 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6617 struct tm *ptm = localtime ((time_t *) &time_mtime);
6621 g_warning ("ptm != NULL failed");
6623 return g_strdup (_("Unknown"));
6626 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6628 #endif /* HAVE_LOCALTIME_R */
6630 g_date_set_time_t (&mtime, time_mtime);
6631 time_now = time (NULL);
6632 g_date_set_time_t (&now, time_now);
6634 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6636 /* Translators: %H means "hours" and %M means "minutes" */
6638 format = _("%H:%M");
6639 else if (days_diff == 1)
6640 format = _("Yesterday at %H:%M");
6643 if (days_diff > 1 && days_diff < 7)
6644 format = "%A"; /* Days from last week */
6646 format = "%x"; /* Any other date */
6650 /* g_locale_from_utf8() returns a string in the system
6651 * code-page, which is not always the same as that used by the C
6652 * library. For instance when running a GTK+ program with
6653 * LANG=ko on an English version of Windows, the system
6654 * code-page is 1252, but the code-page used by the C library is
6655 * 949. (It's GTK+ itself that sets the C library locale when it
6656 * notices the LANG environment variable. See gtkmain.c The
6657 * Microsoft C library doesn't look at any locale environment
6658 * variables.) We need to pass strftime() a string in the C
6659 * library's code-page. See bug #509885.
6661 locale = setlocale (LC_ALL, NULL);
6663 dot = strchr (locale, '.');
6666 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6668 /* All codepages should fit in 16 bits AFAIK */
6669 if (codepage > 0 && codepage < 65536)
6671 sprintf (charset, "CP%u", (guint) codepage);
6672 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6676 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6678 if (locale_format != NULL &&
6679 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6682 /* As above but in opposite direction... */
6683 if (codepage > 0 && codepage < 65536)
6684 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6686 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6690 if (date_str == NULL)
6691 date_str = g_strdup (_("Unknown"));
6693 g_free (locale_format);
6698 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6700 GFileAttributeType type;
6703 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6704 g_file_info_set_attribute (to, attribute, type, value);
6708 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6710 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6711 GFile *file = G_FILE (object);
6712 GFileInfo *queried, *info;
6715 queried = g_file_query_info_finish (file, res, NULL);
6716 if (queried == NULL)
6719 gdk_threads_enter ();
6721 /* now we know model is valid */
6723 /* file was deleted */
6724 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6726 gdk_threads_leave ();
6730 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6732 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6733 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6734 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6736 _gtk_file_system_model_update_file (model, file, info);
6738 g_object_unref (info);
6740 gdk_threads_leave ();
6744 file_system_model_set (GtkFileSystemModel *model,
6751 GtkFileChooserDefault *impl = data;
6755 case MODEL_COL_FILE:
6756 g_value_set_object (value, file);
6758 case MODEL_COL_NAME:
6760 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6762 g_value_set_string (value, g_file_info_get_display_name (info));
6764 case MODEL_COL_NAME_COLLATED:
6766 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6768 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6770 case MODEL_COL_IS_FOLDER:
6771 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6773 case MODEL_COL_IS_SENSITIVE:
6776 gboolean sensitive = TRUE;
6778 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6779 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
6781 sensitive = TRUE; /* for file modes... */
6783 else if (!_gtk_file_info_consider_as_directory (info))
6785 sensitive = FALSE; /* for folder modes, files are not sensitive... */
6789 /* ... and for folder modes, folders are sensitive only if the filter says so */
6791 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6792 g_assert_not_reached ();
6793 sensitive = !_gtk_file_system_model_iter_is_filtered_out (model, &iter);
6796 g_value_set_boolean (value, sensitive);
6799 g_value_set_boolean (value, TRUE);
6801 case MODEL_COL_PIXBUF:
6804 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6806 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6810 GtkTreeModel *tree_model;
6811 GtkTreePath *path, *start, *end;
6814 if (impl->browse_files_tree_view == NULL ||
6815 g_file_info_has_attribute (info, "filechooser::queried"))
6818 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6819 if (tree_model != GTK_TREE_MODEL (model))
6822 if (!_gtk_file_system_model_get_iter_for_file (model,
6825 g_assert_not_reached ();
6826 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6828 path = gtk_tree_model_get_path (tree_model, &iter);
6829 if (gtk_tree_path_compare (start, path) != 1 &&
6830 gtk_tree_path_compare (path, end) != 1)
6832 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6833 g_file_query_info_async (file,
6834 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6835 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6836 G_FILE_ATTRIBUTE_STANDARD_ICON,
6837 G_FILE_QUERY_INFO_NONE,
6839 _gtk_file_system_model_get_cancellable (model),
6840 file_system_model_got_thumbnail,
6843 gtk_tree_path_free (path);
6844 gtk_tree_path_free (start);
6845 gtk_tree_path_free (end);
6850 g_value_set_object (value, NULL);
6852 case MODEL_COL_SIZE:
6853 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6855 case MODEL_COL_SIZE_TEXT:
6856 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6857 g_value_set_string (value, NULL);
6859 g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6861 case MODEL_COL_MTIME:
6862 case MODEL_COL_MTIME_TEXT:
6867 g_file_info_get_modification_time (info, &tv);
6868 if (column == MODEL_COL_MTIME)
6869 g_value_set_long (value, tv.tv_sec);
6870 else if (tv.tv_sec == 0)
6871 g_value_set_static_string (value, _("Unknown"));
6873 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6876 case MODEL_COL_ELLIPSIZE:
6877 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6880 g_assert_not_reached ();
6887 /* Gets rid of the old list model and creates a new one for the current folder */
6889 set_list_model (GtkFileChooserDefault *impl,
6892 g_assert (impl->current_folder != NULL);
6894 profile_start ("start", NULL);
6896 stop_loading_and_clear_list_model (impl, TRUE);
6898 set_busy_cursor (impl, TRUE);
6900 impl->browse_files_model =
6901 _gtk_file_system_model_new_for_directory (impl->current_folder,
6903 file_system_model_set,
6905 MODEL_COLUMN_TYPES);
6907 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6909 profile_msg (" set sort function", NULL);
6910 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6911 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6912 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6913 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6914 set_sort_column (impl);
6915 impl->list_sort_ascending = TRUE;
6916 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6917 G_CALLBACK (list_sort_column_changed_cb), impl);
6919 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6921 g_signal_connect (impl->browse_files_model, "finished-loading",
6922 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6924 _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
6926 profile_end ("end", NULL);
6931 struct update_chooser_entry_selected_foreach_closure {
6933 GtkTreeIter first_selected_iter;
6937 compare_utf8_filenames (const gchar *a,
6940 gchar *a_folded, *b_folded;
6943 a_folded = g_utf8_strdown (a, -1);
6944 b_folded = g_utf8_strdown (b, -1);
6946 retval = strcmp (a_folded, b_folded);
6955 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6960 struct update_chooser_entry_selected_foreach_closure *closure;
6963 closure->num_selected++;
6965 if (closure->num_selected == 1)
6966 closure->first_selected_iter = *iter;
6970 update_chooser_entry (GtkFileChooserDefault *impl)
6972 GtkTreeSelection *selection;
6973 struct update_chooser_entry_selected_foreach_closure closure;
6975 /* no need to update the file chooser's entry if there's no entry */
6976 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6977 !impl->location_entry)
6980 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6981 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6982 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6983 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6984 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6987 g_assert (impl->location_entry != NULL);
6989 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6990 closure.num_selected = 0;
6991 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6993 if (closure.num_selected == 0)
6995 if (impl->operation_mode == OPERATION_MODE_RECENT)
6996 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6998 goto maybe_clear_entry;
7000 else if (closure.num_selected == 1)
7002 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7005 gboolean change_entry;
7007 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7009 /* If the cursor moved to the row of the newly created folder,
7010 * retrieving info will return NULL.
7015 g_free (impl->browse_files_last_selected_name);
7016 impl->browse_files_last_selected_name =
7017 g_strdup (g_file_info_get_display_name (info));
7019 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7020 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7021 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7023 /* Don't change the name when clicking on a folder... */
7024 change_entry = (! _gtk_file_info_consider_as_directory (info));
7027 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7031 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7033 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7034 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7039 else if (impl->operation_mode == OPERATION_MODE_RECENT
7040 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7044 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7046 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7047 MODEL_COL_FILE, &folder,
7049 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7050 g_object_unref (folder);
7056 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7057 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7059 /* Multiple selection, so just clear the entry. */
7060 g_free (impl->browse_files_last_selected_name);
7061 impl->browse_files_last_selected_name = NULL;
7063 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7069 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7070 && impl->browse_files_last_selected_name)
7072 const char *entry_text;
7074 gboolean clear_entry;
7076 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7077 len = strlen (entry_text);
7080 /* The file chooser entry may have appended a "/" to its text.
7081 * So take it out, and compare the result to the old selection.
7083 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7087 tmp = g_strndup (entry_text, len - 1);
7088 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7092 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7095 clear_entry = FALSE;
7098 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7103 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7107 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7111 struct UpdateCurrentFolderData
7113 GtkFileChooserDefault *impl;
7115 gboolean keep_trail;
7116 gboolean clear_entry;
7117 GFile *original_file;
7118 GError *original_error;
7122 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7123 GtkFileSystemVolume *volume,
7124 const GError *error,
7127 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7128 struct UpdateCurrentFolderData *data = user_data;
7129 GtkFileChooserDefault *impl = data->impl;
7131 if (cancellable != impl->update_current_folder_cancellable)
7134 impl->update_current_folder_cancellable = NULL;
7135 set_busy_cursor (impl, FALSE);
7142 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7143 impl->reload_state = RELOAD_EMPTY;
7147 change_folder_and_display_error (impl, data->file, data->clear_entry);
7150 g_object_unref (data->file);
7153 g_object_unref (cancellable);
7157 update_current_folder_get_info_cb (GCancellable *cancellable,
7159 const GError *error,
7162 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7163 struct UpdateCurrentFolderData *data = user_data;
7164 GtkFileChooserDefault *impl = data->impl;
7166 if (cancellable != impl->update_current_folder_cancellable)
7169 impl->update_current_folder_cancellable = NULL;
7170 impl->reload_state = RELOAD_EMPTY;
7172 set_busy_cursor (impl, FALSE);
7181 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7183 GMountOperation *mount_operation;
7184 GtkWidget *toplevel;
7186 g_object_unref (cancellable);
7187 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7189 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7191 set_busy_cursor (impl, TRUE);
7193 impl->update_current_folder_cancellable =
7194 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7196 update_current_folder_mount_enclosing_volume_cb,
7202 if (!data->original_file)
7204 data->original_file = g_object_ref (data->file);
7205 data->original_error = g_error_copy (error);
7208 parent_file = g_file_get_parent (data->file);
7210 /* get parent path and try to change the folder to that */
7213 g_object_unref (data->file);
7214 data->file = parent_file;
7216 g_object_unref (cancellable);
7218 /* restart the update current folder operation */
7219 impl->reload_state = RELOAD_HAS_FOLDER;
7221 impl->update_current_folder_cancellable =
7222 _gtk_file_system_get_info (impl->file_system, data->file,
7224 update_current_folder_get_info_cb,
7227 set_busy_cursor (impl, TRUE);
7233 /* Error and bail out, ignoring "not found" errors since they're useless:
7234 * they only happen when a program defaults to a folder that has been (re)moved.
7236 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7237 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7239 g_error_free (data->original_error);
7241 g_object_unref (data->original_file);
7247 if (data->original_file)
7249 /* Error and bail out, ignoring "not found" errors since they're useless:
7250 * they only happen when a program defaults to a folder that has been (re)moved.
7252 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7253 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7255 g_error_free (data->original_error);
7257 g_object_unref (data->original_file);
7260 if (! _gtk_file_info_consider_as_directory (info))
7263 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail);
7265 if (impl->current_folder != data->file)
7267 if (impl->current_folder)
7268 g_object_unref (impl->current_folder);
7270 impl->current_folder = g_object_ref (data->file);
7273 impl->reload_state = RELOAD_HAS_FOLDER;
7275 /* Update the widgets that may trigger a folder change themselves. */
7277 if (!impl->changing_folder)
7279 impl->changing_folder = TRUE;
7281 shortcuts_update_current_folder (impl);
7283 impl->changing_folder = FALSE;
7286 /* Set the folder on the save entry */
7288 if (impl->location_entry)
7290 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7291 impl->current_folder);
7293 if (data->clear_entry)
7294 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7297 /* Create a new list model. This is slightly evil; we store the result value
7298 * but perform more actions rather than returning immediately even if it
7299 * generates an error.
7301 set_list_model (impl, NULL);
7303 /* Refresh controls */
7305 shortcuts_find_current_folder (impl);
7307 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7309 check_preview_change (impl);
7310 bookmarks_check_add_sensitivity (impl);
7312 g_signal_emit_by_name (impl, "selection-changed", 0);
7315 g_object_unref (data->file);
7318 g_object_unref (cancellable);
7322 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7324 gboolean keep_trail,
7325 gboolean clear_entry,
7328 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7329 struct UpdateCurrentFolderData *data;
7331 profile_start ("start", NULL);
7333 g_object_ref (file);
7335 operation_mode_set (impl, OPERATION_MODE_BROWSE);
7337 if (impl->local_only && !g_file_is_native (file))
7339 g_set_error_literal (error,
7340 GTK_FILE_CHOOSER_ERROR,
7341 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7342 _("Cannot change to folder because it is not local"));
7344 g_object_unref (file);
7345 profile_end ("end - not local", NULL);
7349 if (impl->update_current_folder_cancellable)
7350 g_cancellable_cancel (impl->update_current_folder_cancellable);
7352 /* Test validity of path here. */
7353 data = g_new0 (struct UpdateCurrentFolderData, 1);
7355 data->file = g_object_ref (file);
7356 data->keep_trail = keep_trail;
7357 data->clear_entry = clear_entry;
7359 impl->reload_state = RELOAD_HAS_FOLDER;
7361 impl->update_current_folder_cancellable =
7362 _gtk_file_system_get_info (impl->file_system, file,
7364 update_current_folder_get_info_cb,
7367 set_busy_cursor (impl, TRUE);
7368 g_object_unref (file);
7370 profile_end ("end", NULL);
7375 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7377 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7379 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7380 impl->operation_mode == OPERATION_MODE_RECENT)
7383 if (impl->current_folder)
7384 return g_object_ref (impl->current_folder);
7390 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7393 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7395 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7396 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7398 pending_select_files_free (impl);
7399 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), name);
7403 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7407 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7411 parent_file = g_file_get_parent (file);
7414 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7416 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7417 impl->operation_mode == OPERATION_MODE_RECENT ||
7418 impl->load_state == LOAD_EMPTY)
7424 g_assert (impl->current_folder != NULL);
7426 same_path = g_file_equal (parent_file, impl->current_folder);
7429 if (same_path && impl->load_state == LOAD_FINISHED)
7434 files.data = (gpointer) file;
7437 result = show_and_select_files (impl, &files);
7438 g_object_unref (parent_file);
7442 pending_select_files_add (impl, file);
7448 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7449 g_object_unref (parent_file);
7453 g_object_unref (parent_file);
7458 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7461 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7462 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7465 if (!impl->browse_files_model)
7468 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7473 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7478 maybe_select (GtkTreeModel *model,
7483 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7484 GtkTreeSelection *selection;
7485 gboolean is_sensitive;
7488 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7490 gtk_tree_model_get (model, iter,
7491 MODEL_COL_IS_FOLDER, &is_folder,
7492 MODEL_COL_IS_SENSITIVE, &is_sensitive,
7496 ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7497 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)))
7498 gtk_tree_selection_select_iter (selection, iter);
7500 gtk_tree_selection_unselect_iter (selection, iter);
7506 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7508 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7510 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7511 impl->operation_mode == OPERATION_MODE_RECENT)
7513 GtkTreeSelection *selection;
7515 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7516 gtk_tree_selection_select_all (selection);
7520 if (impl->select_multiple)
7521 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7522 maybe_select, impl);
7526 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7528 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7529 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7531 gtk_tree_selection_unselect_all (selection);
7532 pending_select_files_free (impl);
7535 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7537 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7539 * is_empty_ret - whether the file entry is totally empty
7541 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7542 * the path will be "$cwd/foobar")
7545 check_save_entry (GtkFileChooserDefault *impl,
7547 gboolean *is_well_formed_ret,
7548 gboolean *is_empty_ret,
7549 gboolean *is_file_part_empty_ret,
7550 gboolean *is_folder)
7552 GtkFileChooserEntry *chooser_entry;
7553 GFile *current_folder;
7554 const char *file_part;
7558 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7559 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7560 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7561 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7562 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7564 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7566 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7569 *is_well_formed_ret = TRUE;
7570 *is_empty_ret = TRUE;
7571 *is_file_part_empty_ret = TRUE;
7577 *is_empty_ret = FALSE;
7579 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7580 if (!current_folder)
7583 *is_well_formed_ret = FALSE;
7584 *is_file_part_empty_ret = FALSE;
7590 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7592 if (!file_part || file_part[0] == '\0')
7594 *file_ret = current_folder;
7595 *is_well_formed_ret = TRUE;
7596 *is_file_part_empty_ret = TRUE;
7602 *is_file_part_empty_ret = FALSE;
7605 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7606 g_object_unref (current_folder);
7610 error_building_filename_dialog (impl, error);
7612 *is_well_formed_ret = FALSE;
7619 *is_well_formed_ret = TRUE;
7620 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7623 struct get_files_closure {
7624 GtkFileChooserDefault *impl;
7626 GFile *file_from_entry;
7630 get_files_foreach (GtkTreeModel *model,
7635 struct get_files_closure *info;
7637 GtkFileSystemModel *fs_model;
7640 fs_model = info->impl->browse_files_model;
7642 file = _gtk_file_system_model_get_file (fs_model, iter);
7644 return; /* We are on the editable row */
7646 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7647 info->result = g_slist_prepend (info->result, g_object_ref (file));
7651 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7653 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7654 struct get_files_closure info;
7655 GtkWindow *toplevel;
7656 GtkWidget *current_focus;
7657 gboolean file_list_seen;
7661 info.file_from_entry = NULL;
7663 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7664 return search_get_selected_files (impl);
7666 if (impl->operation_mode == OPERATION_MODE_RECENT)
7668 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7670 file_list_seen = TRUE;
7674 return recent_get_selected_files (impl);
7677 toplevel = get_toplevel (GTK_WIDGET (impl));
7679 current_focus = gtk_window_get_focus (toplevel);
7681 current_focus = NULL;
7683 file_list_seen = FALSE;
7684 if (current_focus == impl->browse_files_tree_view)
7686 GtkTreeSelection *selection;
7690 file_list_seen = TRUE;
7691 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7692 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7694 /* If there is no selection in the file list, we probably have this situation:
7696 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7697 * 2. He then double-clicked on a folder ("bar") in the file list
7699 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7700 * filename entry to see if that is the case.
7702 if (info.result == NULL && impl->location_entry)
7705 else if (impl->location_entry && current_focus == impl->location_entry)
7707 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7711 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7716 if (!is_well_formed)
7719 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7721 g_object_unref (info.file_from_entry);
7725 if (info.file_from_entry)
7726 info.result = g_slist_prepend (info.result, info.file_from_entry);
7727 else if (!file_list_seen)
7732 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7734 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7738 /* The focus is on a dialog's action area button or something else */
7739 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7740 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7748 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7749 * fall back to the current directory */
7750 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7751 info.result == NULL)
7753 GFile *current_folder;
7755 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7758 info.result = g_slist_prepend (info.result, current_folder);
7761 return g_slist_reverse (info.result);
7765 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7767 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7769 if (impl->preview_file)
7770 return g_object_ref (impl->preview_file);
7775 static GtkFileSystem *
7776 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7778 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7780 return impl->file_system;
7783 /* Shows or hides the filter widgets */
7785 show_filters (GtkFileChooserDefault *impl,
7789 gtk_widget_show (impl->filter_combo_hbox);
7791 gtk_widget_hide (impl->filter_combo_hbox);
7795 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7796 GtkFileFilter *filter)
7798 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7801 if (g_slist_find (impl->filters, filter))
7803 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7807 g_object_ref_sink (filter);
7808 impl->filters = g_slist_append (impl->filters, filter);
7810 name = gtk_file_filter_get_name (filter);
7812 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7814 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7816 if (!g_slist_find (impl->filters, impl->current_filter))
7817 set_current_filter (impl, filter);
7819 show_filters (impl, TRUE);
7823 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7824 GtkFileFilter *filter)
7826 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7827 GtkTreeModel *model;
7831 filter_index = g_slist_index (impl->filters, filter);
7833 if (filter_index < 0)
7835 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7839 impl->filters = g_slist_remove (impl->filters, filter);
7841 if (filter == impl->current_filter)
7844 set_current_filter (impl, impl->filters->data);
7846 set_current_filter (impl, NULL);
7849 /* Remove row from the combo box */
7850 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7851 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7852 g_assert_not_reached ();
7854 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7856 g_object_unref (filter);
7859 show_filters (impl, FALSE);
7863 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7865 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7867 return g_slist_copy (impl->filters);
7870 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7872 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7875 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7878 struct AddShortcutData
7880 GtkFileChooserDefault *impl;
7885 add_shortcut_get_info_cb (GCancellable *cancellable,
7887 const GError *error,
7891 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7892 struct AddShortcutData *data = user_data;
7894 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7897 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7899 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7902 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7904 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7906 /* need to call shortcuts_add_bookmarks to flush out any duplicates bug #577806 */
7907 shortcuts_add_bookmarks (data->impl);
7910 g_object_unref (data->impl);
7911 g_object_unref (data->file);
7914 g_object_unref (cancellable);
7918 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7922 GCancellable *cancellable;
7923 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7924 struct AddShortcutData *data;
7928 /* Avoid adding duplicates */
7929 pos = shortcut_find_position (impl, file);
7930 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7934 uri = g_file_get_uri (file);
7935 /* translators, "Shortcut" means "Bookmark" here */
7937 GTK_FILE_CHOOSER_ERROR,
7938 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7939 _("Shortcut %s already exists"),
7946 for (l = impl->loading_shortcuts; l; l = l->next)
7948 GCancellable *c = l->data;
7951 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7952 if (f && g_file_equal (file, f))
7956 uri = g_file_get_uri (file);
7958 GTK_FILE_CHOOSER_ERROR,
7959 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7960 _("Shortcut %s already exists"),
7968 data = g_new0 (struct AddShortcutData, 1);
7969 data->impl = g_object_ref (impl);
7970 data->file = g_object_ref (file);
7972 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7974 add_shortcut_get_info_cb, data);
7979 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7980 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7986 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7990 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7997 for (l = impl->loading_shortcuts; l; l = l->next)
7999 GCancellable *c = l->data;
8002 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8003 if (f && g_file_equal (file, f))
8005 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8006 g_cancellable_cancel (c);
8011 if (impl->num_shortcuts == 0)
8014 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8015 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8016 g_assert_not_reached ();
8018 for (i = 0; i < impl->num_shortcuts; i++)
8021 ShortcutType shortcut_type;
8024 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8025 SHORTCUTS_COL_DATA, &col_data,
8026 SHORTCUTS_COL_TYPE, &shortcut_type,
8028 g_assert (col_data != NULL);
8029 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8031 shortcut = col_data;
8032 if (g_file_equal (shortcut, file))
8034 shortcuts_remove_rows (impl, pos + i, 1);
8035 impl->num_shortcuts--;
8039 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8040 g_assert_not_reached ();
8045 uri = g_file_get_uri (file);
8046 /* translators, "Shortcut" means "Bookmark" here */
8048 GTK_FILE_CHOOSER_ERROR,
8049 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8050 _("Shortcut %s does not exist"),
8058 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8060 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8066 if (impl->num_shortcuts == 0)
8069 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8070 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8071 g_assert_not_reached ();
8075 for (i = 0; i < impl->num_shortcuts; i++)
8078 ShortcutType shortcut_type;
8081 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8082 SHORTCUTS_COL_DATA, &col_data,
8083 SHORTCUTS_COL_TYPE, &shortcut_type,
8085 g_assert (col_data != NULL);
8086 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8088 shortcut = col_data;
8089 list = g_slist_prepend (list, g_object_ref (shortcut));
8091 if (i != impl->num_shortcuts - 1)
8093 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8094 g_assert_not_reached ();
8098 return g_slist_reverse (list);
8101 /* Guesses a size based upon font sizes */
8103 find_good_size_from_style (GtkWidget *widget,
8107 GtkStyleContext *context;
8108 GtkStateFlags state;
8113 context = gtk_widget_get_style_context (widget);
8114 state = gtk_widget_get_state_flags (widget);
8116 screen = gtk_widget_get_screen (widget);
8119 resolution = gdk_screen_get_resolution (screen);
8120 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8124 resolution = 96.0; /* wheeee */
8126 gtk_style_context_get (context, state, "font-size", &font_size, NULL);
8127 font_size = font_size * resolution / 72.0 + 0.5;
8129 *width = font_size * NUM_CHARS;
8130 *height = font_size * NUM_LINES;
8134 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8135 gint *default_width,
8136 gint *default_height)
8138 GtkFileChooserDefault *impl;
8140 int x, y, width, height;
8141 GSettings *settings;
8143 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8145 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
8147 g_settings_get (settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8148 g_settings_get (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8150 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8152 *default_width = width;
8153 *default_height = height;
8157 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8159 if (impl->preview_widget_active &&
8160 impl->preview_widget &&
8161 gtk_widget_get_visible (impl->preview_widget))
8163 gtk_widget_get_preferred_size (impl->preview_box,
8165 *default_width += PREVIEW_HBOX_SPACING + req.width;
8168 if (impl->extra_widget &&
8169 gtk_widget_get_visible (impl->extra_widget))
8171 gtk_widget_get_preferred_size (impl->extra_align,
8173 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8177 struct switch_folder_closure {
8178 GtkFileChooserDefault *impl;
8183 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8185 switch_folder_foreach_cb (GtkTreeModel *model,
8190 struct switch_folder_closure *closure;
8194 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8195 closure->num_selected++;
8198 /* Changes to the selected folder in the list view */
8200 switch_to_selected_folder (GtkFileChooserDefault *impl)
8202 GtkTreeSelection *selection;
8203 struct switch_folder_closure closure;
8205 /* We do this with foreach() rather than get_selected() as we may be in
8206 * multiple selection mode
8209 closure.impl = impl;
8210 closure.file = NULL;
8211 closure.num_selected = 0;
8213 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8214 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8216 g_assert (closure.file && closure.num_selected == 1);
8218 change_folder_and_display_error (impl, closure.file, FALSE);
8221 /* Gets the GFileInfo for the selected row in the file list; assumes single
8225 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8226 gboolean *had_selection)
8228 GtkTreeSelection *selection;
8232 g_assert (!impl->select_multiple);
8233 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8234 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8236 *had_selection = FALSE;
8240 *had_selection = TRUE;
8242 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8246 /* Gets the display name of the selected file in the file list; assumes single
8247 * selection mode and that something is selected.
8249 static const gchar *
8250 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8253 gboolean had_selection;
8255 info = get_selected_file_info_from_file_list (impl, &had_selection);
8256 g_assert (had_selection);
8257 g_assert (info != NULL);
8259 return g_file_info_get_display_name (info);
8263 add_custom_button_to_dialog (GtkDialog *dialog,
8264 const gchar *mnemonic_label,
8265 const gchar *stock_id,
8270 button = gtk_button_new_with_mnemonic (mnemonic_label);
8271 gtk_widget_set_can_default (button, TRUE);
8272 gtk_button_set_image (GTK_BUTTON (button),
8273 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8274 gtk_widget_show (button);
8276 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8279 /* Presents an overwrite confirmation dialog; returns whether we should accept
8283 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8284 const gchar *file_part,
8285 const gchar *folder_display_name)
8287 GtkWindow *toplevel;
8291 toplevel = get_toplevel (GTK_WIDGET (impl));
8293 dialog = gtk_message_dialog_new (toplevel,
8294 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8295 GTK_MESSAGE_QUESTION,
8297 _("A file named \"%s\" already exists. Do you want to replace it?"),
8299 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8300 _("The file already exists in \"%s\". Replacing it will "
8301 "overwrite its contents."),
8302 folder_display_name);
8304 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8305 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8306 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8307 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8308 GTK_RESPONSE_ACCEPT,
8309 GTK_RESPONSE_CANCEL,
8311 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8313 if (gtk_window_has_group (toplevel))
8314 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8315 GTK_WINDOW (dialog));
8317 response = gtk_dialog_run (GTK_DIALOG (dialog));
8319 gtk_widget_destroy (dialog);
8321 return (response == GTK_RESPONSE_ACCEPT);
8324 struct GetDisplayNameData
8326 GtkFileChooserDefault *impl;
8330 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8331 * as requesting a response means, "the dialog is confirmed".
8334 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8336 g_signal_emit_by_name (impl, "response-requested");
8337 add_selection_to_recent_list (impl);
8341 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8343 const GError *error,
8346 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8347 gboolean should_respond = FALSE;
8348 struct GetDisplayNameData *data = user_data;
8350 if (cancellable != data->impl->should_respond_get_info_cancellable)
8353 data->impl->should_respond_get_info_cancellable = NULL;
8359 /* Huh? Did the folder disappear? Let the caller deal with it */
8360 should_respond = TRUE;
8362 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8364 set_busy_cursor (data->impl, FALSE);
8366 request_response_and_add_to_recent_list (data->impl);
8369 g_object_unref (data->impl);
8370 g_free (data->file_part);
8373 g_object_unref (cancellable);
8376 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8377 * should respond. Can get the file part from the file list or the save entry.
8380 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8381 const gchar *file_part,
8384 GtkFileChooserConfirmation conf;
8386 if (!impl->do_overwrite_confirmation)
8389 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8391 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8395 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8397 struct GetDisplayNameData *data;
8399 g_assert (file_part != NULL);
8401 data = g_new0 (struct GetDisplayNameData, 1);
8402 data->impl = g_object_ref (impl);
8403 data->file_part = g_strdup (file_part);
8405 if (impl->should_respond_get_info_cancellable)
8406 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8408 impl->should_respond_get_info_cancellable =
8409 _gtk_file_system_get_info (impl->file_system, parent_file,
8410 "standard::display-name",
8411 confirmation_confirm_get_info_cb,
8413 set_busy_cursor (data->impl, TRUE);
8417 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8420 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8424 g_assert_not_reached ();
8429 struct FileExistsData
8431 GtkFileChooserDefault *impl;
8432 gboolean file_exists_and_is_not_folder;
8438 name_entry_get_parent_info_cb (GCancellable *cancellable,
8440 const GError *error,
8443 gboolean parent_is_folder;
8444 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8445 struct FileExistsData *data = user_data;
8447 if (cancellable != data->impl->should_respond_get_info_cancellable)
8450 data->impl->should_respond_get_info_cancellable = NULL;
8452 set_busy_cursor (data->impl, FALSE);
8458 parent_is_folder = FALSE;
8460 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8462 if (parent_is_folder)
8464 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8466 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) */
8468 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8470 if (data->file_exists_and_is_not_folder)
8475 /* Dup the string because the string may be modified
8476 * depending on what clients do in the confirm-overwrite
8477 * signal and this corrupts the pointer
8479 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8480 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8484 request_response_and_add_to_recent_list (data->impl);
8487 request_response_and_add_to_recent_list (data->impl);
8489 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8490 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8492 GError *mkdir_error = NULL;
8494 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8495 * "/blah/nonexistent" you *will* want a folder created.
8498 set_busy_cursor (data->impl, TRUE);
8499 g_file_make_directory (data->file, NULL, &mkdir_error);
8500 set_busy_cursor (data->impl, FALSE);
8503 request_response_and_add_to_recent_list (data->impl);
8505 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8508 g_assert_not_reached ();
8514 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8515 error_with_file_under_nonfolder (data->impl, data->parent_file);
8521 /* The parent folder is not readable for some reason */
8523 error_copy = g_error_copy (error);
8524 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8529 g_object_unref (data->impl);
8530 g_object_unref (data->file);
8531 g_object_unref (data->parent_file);
8534 g_object_unref (cancellable);
8538 file_exists_get_info_cb (GCancellable *cancellable,
8540 const GError *error,
8543 gboolean data_ownership_taken = FALSE;
8544 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8545 gboolean file_exists;
8547 gboolean needs_parent_check = FALSE;
8548 struct FileExistsData *data = user_data;
8550 if (cancellable != data->impl->file_exists_get_info_cancellable)
8553 data->impl->file_exists_get_info_cancellable = NULL;
8555 set_busy_cursor (data->impl, FALSE);
8560 file_exists = (info != NULL);
8561 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8563 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8566 change_folder_and_display_error (data->impl, data->file, TRUE);
8570 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8572 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8575 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8577 if (file_exists && !is_folder)
8579 /* Oops, the user typed the name of an existing path which is not
8582 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8583 g_error_copy (error));
8587 needs_parent_check = TRUE;
8590 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8594 needs_parent_check = TRUE;
8600 /* User typed a folder; we are done */
8601 request_response_and_add_to_recent_list (data->impl);
8604 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8607 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8610 change_folder_and_display_error (data->impl, data->file, TRUE);
8612 needs_parent_check = TRUE;
8616 g_assert_not_reached();
8619 if (needs_parent_check) {
8620 /* check that everything up to the last path component exists (i.e. the parent) */
8622 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8623 data_ownership_taken = TRUE;
8625 if (data->impl->should_respond_get_info_cancellable)
8626 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8628 data->impl->should_respond_get_info_cancellable =
8629 _gtk_file_system_get_info (data->impl->file_system,
8632 name_entry_get_parent_info_cb,
8634 set_busy_cursor (data->impl, TRUE);
8638 if (!data_ownership_taken)
8640 g_object_unref (data->impl);
8641 g_object_unref (data->file);
8642 g_object_unref (data->parent_file);
8646 g_object_unref (cancellable);
8650 paste_text_received (GtkClipboard *clipboard,
8652 GtkFileChooserDefault *impl)
8659 file = g_file_new_for_uri (text);
8661 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8662 location_popup_handler (impl, text);
8664 g_object_unref (file);
8667 /* Handler for the "location-popup-on-paste" keybinding signal */
8669 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8671 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8672 GDK_SELECTION_CLIPBOARD);
8673 gtk_clipboard_request_text (clipboard,
8674 (GtkClipboardTextReceivedFunc) paste_text_received,
8678 /* Implementation for GtkFileChooserEmbed::should_respond() */
8680 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8685 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8687 for (l = files; l; l = l->next)
8689 GFile *file = l->data;
8692 uri = g_file_get_uri (file);
8695 gtk_recent_manager_add_item (impl->recent_manager, uri);
8700 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8701 g_slist_free (files);
8705 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8707 GtkFileChooserDefault *impl;
8708 GtkWidget *toplevel;
8709 GtkWidget *current_focus;
8712 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8714 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8715 g_assert (GTK_IS_WINDOW (toplevel));
8719 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8721 if (current_focus == impl->browse_files_tree_view)
8723 /* The following array encodes what we do based on the impl->action and the
8724 * number of files selected.
8727 NOOP, /* Do nothing (don't respond) */
8728 RESPOND, /* Respond immediately */
8729 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8730 ALL_FILES, /* Respond only if everything selected is a file */
8731 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8732 SAVE_ENTRY, /* Go to the code for handling the save entry */
8733 NOT_REACHED /* Sanity check */
8735 static const ActionToTake what_to_do[4][3] = {
8736 /* 0 selected 1 selected many selected */
8737 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8738 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8739 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8740 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8744 gboolean all_files, all_folders;
8746 ActionToTake action;
8750 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8752 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8754 retval = search_should_respond (impl);
8758 if (impl->operation_mode == OPERATION_MODE_RECENT)
8760 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8764 retval = recent_should_respond (impl);
8769 selection_check (impl, &num_selected, &all_files, &all_folders);
8771 if (num_selected > 2)
8776 action = what_to_do [impl->action] [k];
8787 case RESPOND_OR_SWITCH:
8788 g_assert (num_selected == 1);
8792 switch_to_selected_folder (impl);
8795 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8797 retval = should_respond_after_confirm_overwrite (impl,
8798 get_display_name_from_file_list (impl),
8799 impl->current_folder);
8813 retval = all_folders;
8820 g_assert_not_reached ();
8823 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8826 gboolean is_well_formed, is_empty, is_file_part_empty;
8828 GtkFileChooserEntry *entry;
8833 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8834 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8835 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8836 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8837 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8839 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8840 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8842 if (!is_well_formed)
8845 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8846 && impl->operation_mode == OPERATION_MODE_RECENT)
8848 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8850 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8851 * thus changing our assumption that no selection is present - setting
8852 * a selection causes the error message from path_bar_set_mode() to go away,
8853 * but we want the user to see that message!
8855 gtk_widget_grab_focus (impl->browse_files_tree_view);
8858 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8865 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8866 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8868 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8869 gtk_widget_grab_focus (impl->location_entry);
8876 g_assert (file != NULL);
8881 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8882 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8884 change_folder_and_display_error (impl, file, TRUE);
8886 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8887 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8889 /* The folder already exists, so we do not need to create it.
8890 * Just respond to terminate the dialog.
8896 g_assert_not_reached ();
8901 struct FileExistsData *data;
8903 /* We need to check whether file exists and whether it is a folder -
8904 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8905 * negative (it doesn't know yet if your last path component is a
8909 data = g_new0 (struct FileExistsData, 1);
8910 data->impl = g_object_ref (impl);
8911 data->file = g_object_ref (file);
8912 data->parent_file = _gtk_file_chooser_entry_get_current_folder (entry);
8914 if (impl->file_exists_get_info_cancellable)
8915 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8917 impl->file_exists_get_info_cancellable =
8918 _gtk_file_system_get_info (impl->file_system, file,
8920 file_exists_get_info_cb,
8923 set_busy_cursor (impl, TRUE);
8926 g_error_free (error);
8929 g_object_unref (file);
8931 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8933 /* The focus is on a dialog's action area button, *and* the widget that
8934 * was focused immediately before it is the file list.
8938 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8940 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8943 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8945 /* The focus is on a dialog's action area button, *and* the widget that
8946 * was focused immediately before it is the location entry.
8951 /* The focus is on a dialog's action area button or something else */
8952 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8953 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8961 add_selection_to_recent_list (impl);
8966 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8968 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8970 GtkFileChooserDefault *impl;
8973 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8975 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8976 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8978 if (impl->location_mode == LOCATION_MODE_PATH_BAR
8979 || impl->operation_mode == OPERATION_MODE_RECENT)
8980 widget = impl->browse_files_tree_view;
8982 widget = impl->location_entry;
8984 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8985 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8986 widget = impl->location_entry;
8989 g_assert_not_reached ();
8993 g_assert (widget != NULL);
8994 gtk_widget_grab_focus (widget);
8997 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8999 search_selected_foreach_get_file_cb (GtkTreeModel *model,
9009 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9010 *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 */
9013 /* Constructs a list of the selected paths in search mode */
9015 search_get_selected_files (GtkFileChooserDefault *impl)
9018 GtkTreeSelection *selection;
9022 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9023 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9024 result = g_slist_reverse (result);
9029 /* Called from ::should_respond(). We return whether there are selected files
9030 * in the search list.
9033 search_should_respond (GtkFileChooserDefault *impl)
9035 GtkTreeSelection *selection;
9037 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9039 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9040 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9043 /* Adds one hit from the search engine to the search_model */
9045 search_add_hit (GtkFileChooserDefault *impl,
9050 file = g_file_new_for_uri (uri);
9054 if (!g_file_is_native (file))
9056 g_object_unref (file);
9060 _gtk_file_system_model_add_and_query_file (impl->search_model,
9064 g_object_unref (file);
9067 /* Callback used from GtkSearchEngine when we get new hits */
9069 search_engine_hits_added_cb (GtkSearchEngine *engine,
9073 GtkFileChooserDefault *impl;
9076 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9078 for (l = hits; l; l = l->next)
9079 search_add_hit (impl, (gchar*)l->data);
9082 /* Callback used from GtkSearchEngine when the query is done running */
9084 search_engine_finished_cb (GtkSearchEngine *engine,
9087 GtkFileChooserDefault *impl;
9089 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9092 /* EB: setting the model here will avoid loads of row events,
9093 * but it'll make the search look like blocked.
9095 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9096 GTK_TREE_MODEL (impl->search_model));
9097 file_list_set_sort_column_ids (impl);
9100 /* FMQ: if search was empty, say that we got no hits */
9101 set_busy_cursor (impl, FALSE);
9104 /* Displays a generic error when we cannot create a GtkSearchEngine.
9105 * It would be better if _gtk_search_engine_new() gave us a GError
9106 * with a better message, but it doesn't do that right now.
9109 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9111 error_message (impl,
9112 _("Could not start the search process"),
9113 _("The program was not able to create a connection to the indexer "
9114 "daemon. Please make sure it is running."));
9118 search_engine_error_cb (GtkSearchEngine *engine,
9119 const gchar *message,
9122 GtkFileChooserDefault *impl;
9124 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9126 search_stop_searching (impl, TRUE);
9127 error_message (impl, _("Could not send the search request"), message);
9129 set_busy_cursor (impl, FALSE);
9132 /* Frees the data in the search_model */
9134 search_clear_model (GtkFileChooserDefault *impl,
9135 gboolean remove_from_treeview)
9137 if (!impl->search_model)
9140 g_object_unref (impl->search_model);
9141 impl->search_model = NULL;
9143 if (remove_from_treeview)
9144 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9147 /* Stops any ongoing searches; does not touch the search_model */
9149 search_stop_searching (GtkFileChooserDefault *impl,
9150 gboolean remove_query)
9152 if (remove_query && impl->search_query)
9154 g_object_unref (impl->search_query);
9155 impl->search_query = NULL;
9158 if (impl->search_engine)
9160 _gtk_search_engine_stop (impl->search_engine);
9162 g_object_unref (impl->search_engine);
9163 impl->search_engine = NULL;
9167 /* Creates the search_model and puts it in the tree view */
9169 search_setup_model (GtkFileChooserDefault *impl)
9171 g_assert (impl->search_model == NULL);
9173 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9175 MODEL_COLUMN_TYPES);
9177 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9181 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9185 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9189 set_sort_column (impl);
9191 /* EB: setting the model here will make the hits list update feel
9192 * more "alive" than setting the model at the end of the search
9195 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9196 GTK_TREE_MODEL (impl->search_model));
9197 file_list_set_sort_column_ids (impl);
9200 /* Creates a new query with the specified text and launches it */
9202 search_start_query (GtkFileChooserDefault *impl,
9203 const gchar *query_text)
9205 search_stop_searching (impl, FALSE);
9206 search_clear_model (impl, TRUE);
9207 search_setup_model (impl);
9208 set_busy_cursor (impl, TRUE);
9210 if (impl->search_engine == NULL)
9211 impl->search_engine = _gtk_search_engine_new ();
9213 if (!impl->search_engine)
9215 set_busy_cursor (impl, FALSE);
9216 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9220 if (!impl->search_query)
9222 impl->search_query = _gtk_query_new ();
9223 _gtk_query_set_text (impl->search_query, query_text);
9226 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9228 g_signal_connect (impl->search_engine, "hits-added",
9229 G_CALLBACK (search_engine_hits_added_cb), impl);
9230 g_signal_connect (impl->search_engine, "finished",
9231 G_CALLBACK (search_engine_finished_cb), impl);
9232 g_signal_connect (impl->search_engine, "error",
9233 G_CALLBACK (search_engine_error_cb), impl);
9235 _gtk_search_engine_start (impl->search_engine);
9238 /* Callback used when the user presses Enter while typing on the search
9239 * entry; starts the query
9242 search_entry_activate_cb (GtkEntry *entry,
9245 GtkFileChooserDefault *impl;
9248 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9250 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9251 if (strlen (text) == 0)
9254 /* reset any existing query object */
9255 if (impl->search_query)
9257 g_object_unref (impl->search_query);
9258 impl->search_query = NULL;
9261 search_start_query (impl, text);
9265 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9267 gdk_threads_enter ();
9269 g_source_destroy (impl->focus_entry_idle);
9270 impl->focus_entry_idle = NULL;
9272 if (impl->search_entry)
9273 gtk_widget_grab_focus (impl->search_entry);
9275 gdk_threads_leave ();
9281 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9283 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9284 * pane, we get a selection-changed signal and we set up the search widgets.
9285 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9286 * the change to the selection. So, we need to re-focus the search entry
9287 * after the treeview has finished doing its work; we'll do that in an idle
9291 if (!impl->focus_entry_idle)
9292 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9295 /* Hides the path bar and creates the search entry */
9297 search_setup_widgets (GtkFileChooserDefault *impl)
9299 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9301 path_bar_update (impl);
9303 impl->search_entry = gtk_entry_new ();
9304 g_signal_connect (impl->search_entry, "activate",
9305 G_CALLBACK (search_entry_activate_cb),
9307 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9309 /* if there already is a query, restart it */
9310 if (impl->search_query)
9312 gchar *query = _gtk_query_get_text (impl->search_query);
9316 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9317 search_start_query (impl, query);
9323 g_object_unref (impl->search_query);
9324 impl->search_query = NULL;
9328 /* Box for search widgets */
9329 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9330 gtk_widget_show_all (impl->search_hbox);
9331 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9333 /* Hide the location widgets temporarily */
9335 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9336 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9338 gtk_widget_hide (impl->location_button);
9339 gtk_widget_hide (impl->location_entry_box);
9342 focus_search_entry_in_idle (impl);
9344 /* FMQ: hide the filter combo? */
9348 * Recent files support
9351 /* Frees the data in the recent_model */
9353 recent_clear_model (GtkFileChooserDefault *impl,
9354 gboolean remove_from_treeview)
9356 if (!impl->recent_model)
9359 if (remove_from_treeview)
9360 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9362 g_object_unref (impl->recent_model);
9363 impl->recent_model = NULL;
9366 /* Stops any ongoing loading of the recent files list; does
9367 * not touch the recent_model
9370 recent_stop_loading (GtkFileChooserDefault *impl)
9372 if (impl->load_recent_id)
9374 g_source_remove (impl->load_recent_id);
9375 impl->load_recent_id = 0;
9380 recent_setup_model (GtkFileChooserDefault *impl)
9382 g_assert (impl->recent_model == NULL);
9384 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9386 MODEL_COLUMN_TYPES);
9388 _gtk_file_system_model_set_filter (impl->recent_model,
9389 impl->current_filter);
9390 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9394 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9398 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9402 set_sort_column (impl);
9407 GtkFileChooserDefault *impl;
9412 recent_idle_cleanup (gpointer data)
9414 RecentLoadData *load_data = data;
9415 GtkFileChooserDefault *impl = load_data->impl;
9417 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9418 GTK_TREE_MODEL (impl->recent_model));
9419 file_list_set_sort_column_ids (impl);
9420 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9422 set_busy_cursor (impl, FALSE);
9424 impl->load_recent_id = 0;
9430 get_recent_files_limit (GtkWidget *widget)
9432 GtkSettings *settings;
9435 if (gtk_widget_has_screen (widget))
9436 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9438 settings = gtk_settings_get_default ();
9440 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9445 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9447 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9453 limit = get_recent_files_limit (GTK_WIDGET (impl));
9457 for (l = items; l; l = l->next)
9459 GtkRecentInfo *info = l->data;
9462 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9463 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9466 g_object_unref (file);
9469 if (limit != -1 && n >= limit)
9475 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9480 folders = _gtk_file_chooser_extract_recent_folders (items);
9482 for (l = folders; l; l = l->next)
9484 GFile *folder = l->data;
9486 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9491 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9492 g_list_free (folders);
9496 recent_idle_load (gpointer data)
9498 RecentLoadData *load_data = data;
9499 GtkFileChooserDefault *impl = load_data->impl;
9501 if (!impl->recent_manager)
9504 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9505 if (!load_data->items)
9508 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9509 populate_model_with_recent_items (impl, load_data->items);
9511 populate_model_with_folders (impl, load_data->items);
9513 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9514 g_list_free (load_data->items);
9515 load_data->items = NULL;
9521 recent_start_loading (GtkFileChooserDefault *impl)
9523 RecentLoadData *load_data;
9525 recent_stop_loading (impl);
9526 recent_clear_model (impl, TRUE);
9527 recent_setup_model (impl);
9528 set_busy_cursor (impl, TRUE);
9530 g_assert (impl->load_recent_id == 0);
9532 load_data = g_new (RecentLoadData, 1);
9533 load_data->impl = impl;
9534 load_data->items = NULL;
9536 /* begin lazy loading the recent files into the model */
9537 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9540 recent_idle_cleanup);
9544 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9554 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9555 *list = g_slist_prepend (*list, file);
9558 /* Constructs a list of the selected paths in recent files mode */
9560 recent_get_selected_files (GtkFileChooserDefault *impl)
9563 GtkTreeSelection *selection;
9567 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9568 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9569 result = g_slist_reverse (result);
9574 /* Called from ::should_respond(). We return whether there are selected
9575 * files in the recent files list.
9578 recent_should_respond (GtkFileChooserDefault *impl)
9580 GtkTreeSelection *selection;
9582 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9584 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9585 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9589 set_current_filter (GtkFileChooserDefault *impl,
9590 GtkFileFilter *filter)
9592 if (impl->current_filter != filter)
9596 /* NULL filters are allowed to reset to non-filtered status
9598 filter_index = g_slist_index (impl->filters, filter);
9599 if (impl->filters && filter && filter_index < 0)
9602 if (impl->current_filter)
9603 g_object_unref (impl->current_filter);
9604 impl->current_filter = filter;
9605 if (impl->current_filter)
9607 g_object_ref_sink (impl->current_filter);
9611 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9614 if (impl->browse_files_model)
9616 _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
9617 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_IS_SENSITIVE);
9620 if (impl->search_model)
9622 _gtk_file_system_model_set_filter (impl->search_model, filter);
9623 _gtk_file_system_model_clear_cache (impl->search_model, MODEL_COL_IS_SENSITIVE);
9626 if (impl->recent_model)
9628 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9629 _gtk_file_system_model_clear_cache (impl->recent_model, MODEL_COL_IS_SENSITIVE);
9632 g_object_notify (G_OBJECT (impl), "filter");
9637 filter_combo_changed (GtkComboBox *combo_box,
9638 GtkFileChooserDefault *impl)
9640 gint new_index = gtk_combo_box_get_active (combo_box);
9641 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9643 set_current_filter (impl, new_filter);
9647 check_preview_change (GtkFileChooserDefault *impl)
9649 GtkTreePath *cursor_path;
9651 char *new_display_name;
9652 GtkTreeModel *model;
9654 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9655 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9660 gtk_tree_model_get_iter (model, &iter, cursor_path);
9661 gtk_tree_model_get (model, &iter,
9662 MODEL_COL_FILE, &new_file,
9663 MODEL_COL_NAME, &new_display_name,
9666 gtk_tree_path_free (cursor_path);
9671 new_display_name = NULL;
9674 if (new_file != impl->preview_file &&
9675 !(new_file && impl->preview_file &&
9676 g_file_equal (new_file, impl->preview_file)))
9678 if (impl->preview_file)
9680 g_object_unref (impl->preview_file);
9681 g_free (impl->preview_display_name);
9686 impl->preview_file = new_file;
9687 impl->preview_display_name = new_display_name;
9691 impl->preview_file = NULL;
9692 impl->preview_display_name = NULL;
9693 g_free (new_display_name);
9696 if (impl->use_preview_label && impl->preview_label)
9697 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9699 g_signal_emit_by_name (impl, "update-preview");
9704 g_object_unref (new_file);
9706 g_free (new_display_name);
9711 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9712 GtkFileSystemVolume *volume,
9713 const GError *error,
9717 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9718 GtkFileChooserDefault *impl = data;
9720 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9723 impl->shortcuts_activate_iter_cancellable = NULL;
9725 set_busy_cursor (impl, FALSE);
9732 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9736 name = _gtk_file_system_volume_get_display_name (volume);
9737 msg = g_strdup_printf (_("Could not mount %s"), name);
9739 error_message (impl, msg, error->message);
9748 file = _gtk_file_system_volume_get_root (volume);
9751 change_folder_and_display_error (impl, file, FALSE);
9752 g_object_unref (file);
9756 g_object_unref (impl);
9757 g_object_unref (cancellable);
9761 /* Activates a volume by mounting it if necessary and then switching to its
9765 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9766 GtkFileSystemVolume *volume)
9770 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9772 /* We ref the file chooser since volume_mount() may run a main loop, and the
9773 * user could close the file chooser window in the meantime.
9775 g_object_ref (impl);
9777 if (!_gtk_file_system_volume_is_mounted (volume))
9779 GMountOperation *mount_op;
9781 set_busy_cursor (impl, TRUE);
9783 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9784 impl->shortcuts_activate_iter_cancellable =
9785 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9786 shortcuts_activate_volume_mount_cb,
9787 g_object_ref (impl));
9788 g_object_unref (mount_op);
9792 file = _gtk_file_system_volume_get_root (volume);
9795 change_folder_and_display_error (impl, file, FALSE);
9796 g_object_unref (file);
9800 g_object_unref (impl);
9803 /* Opens the folder or volume at the specified iter in the shortcuts model */
9804 struct ShortcutsActivateData
9806 GtkFileChooserDefault *impl;
9811 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9813 const GError *error,
9816 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9817 struct ShortcutsActivateData *data = user_data;
9819 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9822 data->impl->shortcuts_activate_iter_cancellable = NULL;
9827 if (!error && _gtk_file_info_consider_as_directory (info))
9828 change_folder_and_display_error (data->impl, data->file, FALSE);
9830 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9835 g_object_unref (data->impl);
9836 g_object_unref (data->file);
9839 g_object_unref (cancellable);
9843 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9844 GtkFileSystemVolume *volume,
9845 const GError *error,
9848 struct ShortcutsActivateData *data = user_data;
9852 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9854 g_object_unref (data->impl);
9855 g_object_unref (data->file);
9861 data->impl->shortcuts_activate_iter_cancellable =
9862 _gtk_file_system_get_info (data->impl->file_system, data->file,
9864 shortcuts_activate_get_info_cb, data);
9867 _gtk_file_system_volume_unref (volume);
9871 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9875 ShortcutType shortcut_type;
9877 /* In the Save modes, we want to preserve what the uesr typed in the filename
9878 * entry, so that he may choose another folder without erasing his typed name.
9880 if (impl->location_entry
9881 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9882 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9883 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
9885 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9886 SHORTCUTS_COL_DATA, &col_data,
9887 SHORTCUTS_COL_TYPE, &shortcut_type,
9890 if (impl->shortcuts_activate_iter_cancellable)
9892 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9893 impl->shortcuts_activate_iter_cancellable = NULL;
9896 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9898 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9900 GtkFileSystemVolume *volume;
9904 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9906 shortcuts_activate_volume (impl, volume);
9908 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9910 struct ShortcutsActivateData *data;
9911 GtkFileSystemVolume *volume;
9913 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9915 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9917 data = g_new0 (struct ShortcutsActivateData, 1);
9918 data->impl = g_object_ref (impl);
9919 data->file = g_object_ref (col_data);
9921 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9923 GMountOperation *mount_operation;
9924 GtkWidget *toplevel;
9926 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9928 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9930 impl->shortcuts_activate_iter_cancellable =
9931 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9933 shortcuts_activate_mount_enclosing_volume,
9938 impl->shortcuts_activate_iter_cancellable =
9939 _gtk_file_system_get_info (impl->file_system, data->file,
9941 shortcuts_activate_get_info_cb, data);
9944 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9946 operation_mode_set (impl, OPERATION_MODE_SEARCH);
9948 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9950 operation_mode_set (impl, OPERATION_MODE_RECENT);
9954 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9956 shortcuts_key_press_event_cb (GtkWidget *widget,
9958 GtkFileChooserDefault *impl)
9962 modifiers = gtk_accelerator_get_default_mod_mask ();
9964 if (key_is_left_or_right (event))
9966 gtk_widget_grab_focus (impl->browse_files_tree_view);
9970 if ((event->keyval == GDK_KEY_BackSpace
9971 || event->keyval == GDK_KEY_Delete
9972 || event->keyval == GDK_KEY_KP_Delete)
9973 && (event->state & modifiers) == 0)
9975 remove_selected_bookmarks (impl);
9979 if ((event->keyval == GDK_KEY_F2)
9980 && (event->state & modifiers) == 0)
9982 rename_selected_bookmark (impl);
9990 shortcuts_select_func (GtkTreeSelection *selection,
9991 GtkTreeModel *model,
9993 gboolean path_currently_selected,
9996 GtkFileChooserDefault *impl = data;
9997 GtkTreeIter filter_iter;
9998 ShortcutType shortcut_type;
10000 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10001 g_assert_not_reached ();
10003 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10005 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10009 list_select_func (GtkTreeSelection *selection,
10010 GtkTreeModel *model,
10012 gboolean path_currently_selected,
10015 GtkFileChooserDefault *impl = data;
10017 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10018 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10021 gboolean is_sensitive;
10022 gboolean is_folder;
10024 if (!gtk_tree_model_get_iter (model, &iter, path))
10026 gtk_tree_model_get (model, &iter,
10027 MODEL_COL_IS_SENSITIVE, &is_sensitive,
10028 MODEL_COL_IS_FOLDER, &is_folder,
10030 if (!is_sensitive || !is_folder)
10038 list_selection_changed (GtkTreeSelection *selection,
10039 GtkFileChooserDefault *impl)
10041 /* See if we are in the new folder editable row for Save mode */
10042 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10043 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10046 gboolean had_selection;
10048 info = get_selected_file_info_from_file_list (impl, &had_selection);
10049 if (!had_selection)
10050 goto out; /* normal processing */
10053 return; /* We are on the editable row for New Folder */
10058 if (impl->location_entry)
10059 update_chooser_entry (impl);
10061 path_bar_update (impl);
10063 check_preview_change (impl);
10064 bookmarks_check_add_sensitivity (impl);
10065 check_copy_file_location_sensitivity (impl);
10067 g_signal_emit_by_name (impl, "selection-changed", 0);
10070 /* Callback used when a row in the file list is activated */
10072 list_row_activated (GtkTreeView *tree_view,
10074 GtkTreeViewColumn *column,
10075 GtkFileChooserDefault *impl)
10079 GtkTreeModel *model;
10080 gboolean is_folder;
10081 gboolean is_sensitive;
10083 model = gtk_tree_view_get_model (tree_view);
10085 if (!gtk_tree_model_get_iter (model, &iter, path))
10088 gtk_tree_model_get (model, &iter,
10089 MODEL_COL_FILE, &file,
10090 MODEL_COL_IS_FOLDER, &is_folder,
10091 MODEL_COL_IS_SENSITIVE, &is_sensitive,
10094 if (is_sensitive && is_folder && file)
10096 change_folder_and_display_error (impl, file, FALSE);
10100 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10101 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10102 g_signal_emit_by_name (impl, "file-activated");
10107 g_object_unref (file);
10111 path_bar_clicked (GtkPathBar *path_bar,
10114 gboolean child_is_hidden,
10115 GtkFileChooserDefault *impl)
10118 pending_select_files_add (impl, child_file);
10120 if (!change_folder_and_display_error (impl, file, FALSE))
10123 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10124 * show hidden files so that ".baz" appears in the file list, as it will still
10125 * be shown in the path bar: "/foo/[bar]/.baz"
10127 if (child_is_hidden)
10128 g_object_set (impl, "show-hidden", TRUE, NULL);
10132 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10134 GtkTreeViewColumn *column;
10135 GtkCellRenderer *renderer;
10136 GList *walk, *list;
10138 /* Keep the following column numbers in sync with create_file_list() */
10141 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10142 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10143 for (walk = list; walk; walk = walk->next)
10145 renderer = walk->data;
10146 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10148 gtk_tree_view_column_set_attributes (column, renderer,
10149 "pixbuf", MODEL_COL_PIXBUF,
10154 gtk_tree_view_column_set_attributes (column, renderer,
10155 "text", MODEL_COL_NAME,
10156 "ellipsize", MODEL_COL_ELLIPSIZE,
10160 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10162 g_list_free (list);
10165 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10166 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10167 renderer = list->data;
10168 gtk_tree_view_column_set_attributes (column, renderer,
10169 "text", MODEL_COL_SIZE_TEXT,
10172 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10173 g_list_free (list);
10176 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10177 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10178 renderer = list->data;
10179 gtk_tree_view_column_set_attributes (column, renderer,
10180 "text", MODEL_COL_MTIME_TEXT,
10182 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10183 g_list_free (list);
10187 _gtk_file_chooser_default_new (void)
10189 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10193 location_set_user_text (GtkFileChooserDefault *impl,
10196 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), path);
10197 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10201 location_popup_handler (GtkFileChooserDefault *impl,
10204 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10206 GtkWidget *widget_to_focus;
10208 operation_mode_set (impl, OPERATION_MODE_BROWSE);
10210 if (impl->current_folder)
10211 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10213 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10214 widget_to_focus = impl->browse_files_tree_view;
10216 widget_to_focus = impl->location_entry;
10218 gtk_widget_grab_focus (widget_to_focus);
10222 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10223 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10228 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10229 location_set_user_text (impl, path);
10231 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10232 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10234 gtk_widget_grab_focus (impl->location_entry);
10236 location_set_user_text (impl, path);
10239 g_assert_not_reached ();
10242 /* Handler for the "up-folder" keybinding signal */
10244 up_folder_handler (GtkFileChooserDefault *impl)
10246 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10249 /* Handler for the "down-folder" keybinding signal */
10251 down_folder_handler (GtkFileChooserDefault *impl)
10253 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10256 /* Switches to the shortcut in the specified index */
10258 switch_to_shortcut (GtkFileChooserDefault *impl,
10263 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10264 g_assert_not_reached ();
10266 shortcuts_activate_iter (impl, &iter);
10269 /* Handler for the "home-folder" keybinding signal */
10271 home_folder_handler (GtkFileChooserDefault *impl)
10273 if (impl->has_home)
10274 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10277 /* Handler for the "desktop-folder" keybinding signal */
10279 desktop_folder_handler (GtkFileChooserDefault *impl)
10281 if (impl->has_desktop)
10282 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10285 /* Handler for the "search-shortcut" keybinding signal */
10287 search_shortcut_handler (GtkFileChooserDefault *impl)
10289 if (impl->has_search)
10291 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10293 /* we want the entry widget to grab the focus the first
10294 * time, not the browse_files_tree_view widget.
10296 if (impl->search_entry)
10297 gtk_widget_grab_focus (impl->search_entry);
10301 /* Handler for the "recent-shortcut" keybinding signal */
10303 recent_shortcut_handler (GtkFileChooserDefault *impl)
10305 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10309 quick_bookmark_handler (GtkFileChooserDefault *impl,
10310 gint bookmark_index)
10315 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10318 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10320 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10321 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10324 gtk_tree_path_free (path);
10326 switch_to_shortcut (impl, bookmark_pos);
10330 show_hidden_handler (GtkFileChooserDefault *impl)
10332 g_object_set (impl,
10333 "show-hidden", !impl->show_hidden,
10338 /* Drag and drop interfaces */
10341 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10346 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10348 model->impl = NULL;
10351 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10353 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10356 ShortcutsPaneModelFilter *model;
10360 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10362 pos = *gtk_tree_path_get_indices (path);
10363 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10365 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10368 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10372 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10374 GtkSelectionData *selection_data)
10381 /* Fill the GtkTreeDragSourceIface vtable */
10383 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10385 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10386 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10390 /* Fill the GtkTreeDragDestIface vtable */
10392 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10394 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10395 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10399 static GtkTreeModel *
10400 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10401 GtkTreeModel *child_model,
10404 ShortcutsPaneModelFilter *model;
10406 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10407 "child-model", child_model,
10408 "virtual-root", root,
10411 model->impl = impl;
10413 return GTK_TREE_MODEL (model);