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);
284 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
285 GtkAllocation *allocation);
287 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
290 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
293 gboolean clear_entry,
295 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
296 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
298 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
301 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
305 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
306 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
307 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
313 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
316 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
319 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
321 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
323 gint *default_height);
324 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
325 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
327 static void add_selection_to_recent_list (GtkFileChooserDefault *impl);
329 static void location_popup_handler (GtkFileChooserDefault *impl,
331 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
332 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
333 static void up_folder_handler (GtkFileChooserDefault *impl);
334 static void down_folder_handler (GtkFileChooserDefault *impl);
335 static void home_folder_handler (GtkFileChooserDefault *impl);
336 static void desktop_folder_handler (GtkFileChooserDefault *impl);
337 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
338 gint bookmark_index);
339 static void show_hidden_handler (GtkFileChooserDefault *impl);
340 static void search_shortcut_handler (GtkFileChooserDefault *impl);
341 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
342 static void update_appearance (GtkFileChooserDefault *impl);
344 static void set_current_filter (GtkFileChooserDefault *impl,
345 GtkFileFilter *filter);
346 static void check_preview_change (GtkFileChooserDefault *impl);
348 static void filter_combo_changed (GtkComboBox *combo_box,
349 GtkFileChooserDefault *impl);
351 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
353 GtkFileChooserDefault *impl);
355 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
358 gboolean path_currently_selected,
360 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
362 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
364 static int shortcuts_get_index (GtkFileChooserDefault *impl,
365 ShortcutsIndex where);
366 static int shortcut_find_position (GtkFileChooserDefault *impl,
369 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
371 static gboolean list_select_func (GtkTreeSelection *selection,
374 gboolean path_currently_selected,
377 static void list_selection_changed (GtkTreeSelection *tree_selection,
378 GtkFileChooserDefault *impl);
379 static void list_row_activated (GtkTreeView *tree_view,
381 GtkTreeViewColumn *column,
382 GtkFileChooserDefault *impl);
384 static void path_bar_clicked (GtkPathBar *path_bar,
387 gboolean child_is_hidden,
388 GtkFileChooserDefault *impl);
390 static void add_bookmark_button_clicked_cb (GtkButton *button,
391 GtkFileChooserDefault *impl);
392 static void remove_bookmark_button_clicked_cb (GtkButton *button,
393 GtkFileChooserDefault *impl);
395 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
397 static void load_remove_timer (GtkFileChooserDefault *impl);
398 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
400 static void location_button_toggled_cb (GtkToggleButton *toggle,
401 GtkFileChooserDefault *impl);
402 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
404 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
405 gboolean remove_from_treeview);
407 static void search_setup_widgets (GtkFileChooserDefault *impl);
408 static void search_stop_searching (GtkFileChooserDefault *impl,
409 gboolean remove_query);
410 static void search_clear_model (GtkFileChooserDefault *impl,
411 gboolean remove_from_treeview);
412 static gboolean search_should_respond (GtkFileChooserDefault *impl);
413 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
414 static void search_entry_activate_cb (GtkEntry *entry,
416 static void settings_load (GtkFileChooserDefault *impl);
418 static void recent_start_loading (GtkFileChooserDefault *impl);
419 static void recent_stop_loading (GtkFileChooserDefault *impl);
420 static void recent_clear_model (GtkFileChooserDefault *impl,
421 gboolean remove_from_treeview);
422 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
423 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
424 static void set_file_system_backend (GtkFileChooserDefault *impl);
425 static void unset_file_system_backend (GtkFileChooserDefault *impl);
431 /* Drag and drop interface declarations */
434 GtkTreeModelFilter parent;
436 GtkFileChooserDefault *impl;
437 } ShortcutsPaneModelFilter;
440 GtkTreeModelFilterClass parent_class;
441 } ShortcutsPaneModelFilterClass;
443 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
444 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
446 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
448 GType _shortcuts_pane_model_filter_get_type (void);
450 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
451 _shortcuts_pane_model_filter,
452 GTK_TYPE_TREE_MODEL_FILTER,
453 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
454 shortcuts_pane_model_filter_drag_source_iface_init))
456 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
457 GtkTreeModel *child_model,
462 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
463 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
464 gtk_file_chooser_default_iface_init)
465 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
466 gtk_file_chooser_embed_default_iface_init));
470 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
472 GdkModifierType modifiers,
473 const gchar *signal_name)
475 gtk_binding_entry_add_signal (binding_set,
479 gtk_binding_entry_add_signal (binding_set,
480 keyval, modifiers | GDK_SHIFT_MASK,
485 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
487 static const guint quick_bookmark_keyvals[10] = {
488 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
490 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
491 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
492 GtkBindingSet *binding_set;
495 gobject_class->finalize = gtk_file_chooser_default_finalize;
496 gobject_class->constructor = gtk_file_chooser_default_constructor;
497 gobject_class->set_property = gtk_file_chooser_default_set_property;
498 gobject_class->get_property = gtk_file_chooser_default_get_property;
499 gobject_class->dispose = gtk_file_chooser_default_dispose;
501 widget_class->show_all = gtk_file_chooser_default_show_all;
502 widget_class->realize = gtk_file_chooser_default_realize;
503 widget_class->map = gtk_file_chooser_default_map;
504 widget_class->unmap = gtk_file_chooser_default_unmap;
505 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
506 widget_class->style_updated = gtk_file_chooser_default_style_updated;
507 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
508 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
510 signals[LOCATION_POPUP] =
511 g_signal_new_class_handler (I_("location-popup"),
512 G_OBJECT_CLASS_TYPE (class),
513 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
514 G_CALLBACK (location_popup_handler),
516 _gtk_marshal_VOID__STRING,
517 G_TYPE_NONE, 1, G_TYPE_STRING);
519 signals[LOCATION_POPUP_ON_PASTE] =
520 g_signal_new_class_handler (I_("location-popup-on-paste"),
521 G_OBJECT_CLASS_TYPE (class),
522 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
523 G_CALLBACK (location_popup_on_paste_handler),
525 _gtk_marshal_VOID__VOID,
528 signals[LOCATION_TOGGLE_POPUP] =
529 g_signal_new_class_handler (I_("location-toggle-popup"),
530 G_OBJECT_CLASS_TYPE (class),
531 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
532 G_CALLBACK (location_toggle_popup_handler),
534 _gtk_marshal_VOID__VOID,
538 g_signal_new_class_handler (I_("up-folder"),
539 G_OBJECT_CLASS_TYPE (class),
540 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
541 G_CALLBACK (up_folder_handler),
543 _gtk_marshal_VOID__VOID,
546 signals[DOWN_FOLDER] =
547 g_signal_new_class_handler (I_("down-folder"),
548 G_OBJECT_CLASS_TYPE (class),
549 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
550 G_CALLBACK (down_folder_handler),
552 _gtk_marshal_VOID__VOID,
555 signals[HOME_FOLDER] =
556 g_signal_new_class_handler (I_("home-folder"),
557 G_OBJECT_CLASS_TYPE (class),
558 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
559 G_CALLBACK (home_folder_handler),
561 _gtk_marshal_VOID__VOID,
564 signals[DESKTOP_FOLDER] =
565 g_signal_new_class_handler (I_("desktop-folder"),
566 G_OBJECT_CLASS_TYPE (class),
567 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
568 G_CALLBACK (desktop_folder_handler),
570 _gtk_marshal_VOID__VOID,
573 signals[QUICK_BOOKMARK] =
574 g_signal_new_class_handler (I_("quick-bookmark"),
575 G_OBJECT_CLASS_TYPE (class),
576 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
577 G_CALLBACK (quick_bookmark_handler),
579 _gtk_marshal_VOID__INT,
580 G_TYPE_NONE, 1, G_TYPE_INT);
582 signals[SHOW_HIDDEN] =
583 g_signal_new_class_handler (I_("show-hidden"),
584 G_OBJECT_CLASS_TYPE (class),
585 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
586 G_CALLBACK (show_hidden_handler),
588 _gtk_marshal_VOID__VOID,
591 signals[SEARCH_SHORTCUT] =
592 g_signal_new_class_handler (I_("search-shortcut"),
593 G_OBJECT_CLASS_TYPE (class),
594 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
595 G_CALLBACK (search_shortcut_handler),
597 _gtk_marshal_VOID__VOID,
600 signals[RECENT_SHORTCUT] =
601 g_signal_new_class_handler (I_("recent-shortcut"),
602 G_OBJECT_CLASS_TYPE (class),
603 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
604 G_CALLBACK (recent_shortcut_handler),
606 _gtk_marshal_VOID__VOID,
609 binding_set = gtk_binding_set_by_class (class);
611 gtk_binding_entry_add_signal (binding_set,
612 GDK_KEY_l, GDK_CONTROL_MASK,
613 "location-toggle-popup",
616 gtk_binding_entry_add_signal (binding_set,
619 1, G_TYPE_STRING, "/");
620 gtk_binding_entry_add_signal (binding_set,
621 GDK_KEY_KP_Divide, 0,
623 1, G_TYPE_STRING, "/");
626 gtk_binding_entry_add_signal (binding_set,
627 GDK_KEY_asciitilde, 0,
629 1, G_TYPE_STRING, "~");
632 gtk_binding_entry_add_signal (binding_set,
633 GDK_KEY_v, GDK_CONTROL_MASK,
634 "location-popup-on-paste",
637 add_normal_and_shifted_binding (binding_set,
638 GDK_KEY_Up, GDK_MOD1_MASK,
641 add_normal_and_shifted_binding (binding_set,
642 GDK_KEY_KP_Up, GDK_MOD1_MASK,
645 add_normal_and_shifted_binding (binding_set,
646 GDK_KEY_Down, GDK_MOD1_MASK,
648 add_normal_and_shifted_binding (binding_set,
649 GDK_KEY_KP_Down, GDK_MOD1_MASK,
652 gtk_binding_entry_add_signal (binding_set,
653 GDK_KEY_Home, GDK_MOD1_MASK,
656 gtk_binding_entry_add_signal (binding_set,
657 GDK_KEY_KP_Home, GDK_MOD1_MASK,
660 gtk_binding_entry_add_signal (binding_set,
661 GDK_KEY_d, GDK_MOD1_MASK,
664 gtk_binding_entry_add_signal (binding_set,
665 GDK_KEY_h, GDK_CONTROL_MASK,
668 gtk_binding_entry_add_signal (binding_set,
669 GDK_KEY_s, GDK_MOD1_MASK,
672 gtk_binding_entry_add_signal (binding_set,
673 GDK_KEY_r, GDK_MOD1_MASK,
677 for (i = 0; i < 10; i++)
678 gtk_binding_entry_add_signal (binding_set,
679 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
683 _gtk_file_chooser_install_properties (gobject_class);
687 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
689 iface->select_file = gtk_file_chooser_default_select_file;
690 iface->unselect_file = gtk_file_chooser_default_unselect_file;
691 iface->select_all = gtk_file_chooser_default_select_all;
692 iface->unselect_all = gtk_file_chooser_default_unselect_all;
693 iface->get_files = gtk_file_chooser_default_get_files;
694 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
695 iface->get_file_system = gtk_file_chooser_default_get_file_system;
696 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
697 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
698 iface->set_current_name = gtk_file_chooser_default_set_current_name;
699 iface->add_filter = gtk_file_chooser_default_add_filter;
700 iface->remove_filter = gtk_file_chooser_default_remove_filter;
701 iface->list_filters = gtk_file_chooser_default_list_filters;
702 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
703 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
704 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
708 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
710 iface->get_default_size = gtk_file_chooser_default_get_default_size;
711 iface->should_respond = gtk_file_chooser_default_should_respond;
712 iface->initial_focus = gtk_file_chooser_default_initial_focus;
716 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
718 profile_start ("start", NULL);
719 #ifdef PROFILE_FILE_CHOOSER
720 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
722 impl->local_only = TRUE;
723 impl->preview_widget_active = TRUE;
724 impl->use_preview_label = TRUE;
725 impl->select_multiple = FALSE;
726 impl->show_hidden = FALSE;
727 impl->show_size_column = TRUE;
728 impl->icon_size = FALLBACK_ICON_SIZE;
729 impl->load_state = LOAD_EMPTY;
730 impl->reload_state = RELOAD_EMPTY;
731 impl->pending_select_files = NULL;
732 impl->location_mode = LOCATION_MODE_PATH_BAR;
733 impl->operation_mode = OPERATION_MODE_BROWSE;
734 impl->sort_column = MODEL_COL_NAME;
735 impl->sort_order = GTK_SORT_ASCENDING;
736 impl->recent_manager = gtk_recent_manager_get_default ();
737 impl->create_folders = TRUE;
739 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
740 GTK_ORIENTATION_VERTICAL);
741 gtk_box_set_spacing (GTK_BOX (impl), 12);
743 set_file_system_backend (impl);
745 profile_end ("end", NULL);
748 /* Frees the data columns for the specified iter in the shortcuts model*/
750 shortcuts_free_row_data (GtkFileChooserDefault *impl,
754 ShortcutType shortcut_type;
755 GCancellable *cancellable;
757 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
758 SHORTCUTS_COL_DATA, &col_data,
759 SHORTCUTS_COL_TYPE, &shortcut_type,
760 SHORTCUTS_COL_CANCELLABLE, &cancellable,
765 g_cancellable_cancel (cancellable);
766 g_object_unref (cancellable);
769 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
770 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
774 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
776 GtkFileSystemVolume *volume;
779 _gtk_file_system_volume_unref (volume);
781 if (shortcut_type == SHORTCUT_TYPE_FILE)
786 g_object_unref (file);
790 /* Frees all the data columns in the shortcuts model */
792 shortcuts_free (GtkFileChooserDefault *impl)
796 if (!impl->shortcuts_model)
799 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
802 shortcuts_free_row_data (impl, &iter);
804 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
806 g_object_unref (impl->shortcuts_model);
807 impl->shortcuts_model = NULL;
811 pending_select_files_free (GtkFileChooserDefault *impl)
813 g_slist_free_full (impl->pending_select_files, g_object_unref);
814 impl->pending_select_files = NULL;
818 pending_select_files_add (GtkFileChooserDefault *impl,
821 impl->pending_select_files =
822 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
826 gtk_file_chooser_default_finalize (GObject *object)
828 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
831 unset_file_system_backend (impl);
833 if (impl->shortcuts_pane_filter_model)
834 g_object_unref (impl->shortcuts_pane_filter_model);
836 shortcuts_free (impl);
838 g_free (impl->browse_files_last_selected_name);
840 for (l = impl->filters; l; l = l->next)
842 GtkFileFilter *filter;
844 filter = GTK_FILE_FILTER (l->data);
845 g_object_unref (filter);
847 g_slist_free (impl->filters);
849 if (impl->current_filter)
850 g_object_unref (impl->current_filter);
852 if (impl->current_volume_file)
853 g_object_unref (impl->current_volume_file);
855 if (impl->current_folder)
856 g_object_unref (impl->current_folder);
858 if (impl->preview_file)
859 g_object_unref (impl->preview_file);
861 if (impl->browse_path_bar_size_group)
862 g_object_unref (impl->browse_path_bar_size_group);
864 /* Free all the Models we have */
865 stop_loading_and_clear_list_model (impl, FALSE);
866 search_clear_model (impl, FALSE);
867 recent_clear_model (impl, FALSE);
869 /* stopping the load above should have cleared this */
870 g_assert (impl->load_timeout_id == 0);
872 g_free (impl->preview_display_name);
874 g_free (impl->edited_new_text);
876 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
879 /* Shows an error dialog set as transient for the specified window */
881 error_message_with_parent (GtkWindow *parent,
887 dialog = gtk_message_dialog_new (parent,
888 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
893 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
896 if (parent && gtk_window_has_group (parent))
897 gtk_window_group_add_window (gtk_window_get_group (parent),
898 GTK_WINDOW (dialog));
900 gtk_dialog_run (GTK_DIALOG (dialog));
901 gtk_widget_destroy (dialog);
904 /* Returns a toplevel GtkWindow, or NULL if none */
906 get_toplevel (GtkWidget *widget)
910 toplevel = gtk_widget_get_toplevel (widget);
911 if (!gtk_widget_is_toplevel (toplevel))
914 return GTK_WINDOW (toplevel);
917 /* Shows an error dialog for the file chooser */
919 error_message (GtkFileChooserDefault *impl,
923 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
926 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
928 error_dialog (GtkFileChooserDefault *impl,
939 uri = g_file_get_uri (file);
940 text = g_strdup_printf (msg, uri);
941 error_message (impl, text, error->message);
944 g_error_free (error);
948 /* Displays an error message about not being able to get information for a file.
949 * Frees the GError as well.
952 error_getting_info_dialog (GtkFileChooserDefault *impl,
957 _("Could not retrieve information about the file"),
961 /* Shows an error dialog about not being able to add a bookmark */
963 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
968 _("Could not add a bookmark"),
972 /* Shows an error dialog about not being able to remove a bookmark */
974 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
979 _("Could not remove bookmark"),
983 /* Shows an error dialog about not being able to create a folder */
985 error_creating_folder_dialog (GtkFileChooserDefault *impl,
990 _("The folder could not be created"),
994 /* Shows an error about not being able to create a folder because a file with
995 * the same name is already there.
998 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1003 _("The folder could not be created, as a file with the same "
1004 "name already exists. Try using a different name for the "
1005 "folder, or rename the file first."),
1010 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1016 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1017 _("You need to choose a valid filename."));
1020 _("Cannot create a file under %s as it is not a folder"),
1021 parent_file, error);
1024 /* Shows an error about not being able to select a folder because a file with
1025 * the same name is already there.
1028 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1032 _("You may only select folders. The item that you selected is not a folder; "
1033 "try using a different item."),
1037 /* Shows an error dialog about not being able to create a filename */
1039 error_building_filename_dialog (GtkFileChooserDefault *impl,
1042 error_dialog (impl, _("Invalid file name"),
1046 /* Shows an error dialog when we cannot switch to a folder */
1048 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1052 error_dialog (impl, _("The folder contents could not be displayed"),
1056 /* Changes folders, displaying an error dialog if this fails */
1058 change_folder_and_display_error (GtkFileChooserDefault *impl,
1060 gboolean clear_entry)
1065 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1067 /* We copy the path because of this case:
1069 * list_row_activated()
1070 * fetches path from model; path belongs to the model (*)
1071 * calls change_folder_and_display_error()
1072 * calls gtk_file_chooser_set_current_folder_file()
1073 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1077 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1080 error_changing_folder_dialog (impl, file, error);
1086 emit_default_size_changed (GtkFileChooserDefault *impl)
1088 profile_msg (" emit default-size-changed start", NULL);
1089 g_signal_emit_by_name (impl, "default-size-changed");
1090 profile_msg (" emit default-size-changed end", NULL);
1094 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1096 if (impl->use_preview_label)
1098 if (!impl->preview_label)
1100 impl->preview_label = gtk_label_new (impl->preview_display_name);
1101 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1102 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1103 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1104 gtk_widget_show (impl->preview_label);
1109 if (impl->preview_label)
1111 gtk_widget_destroy (impl->preview_label);
1112 impl->preview_label = NULL;
1116 if (impl->preview_widget_active && impl->preview_widget)
1117 gtk_widget_show (impl->preview_box);
1119 gtk_widget_hide (impl->preview_box);
1121 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1122 emit_default_size_changed (impl);
1126 set_preview_widget (GtkFileChooserDefault *impl,
1127 GtkWidget *preview_widget)
1129 if (preview_widget == impl->preview_widget)
1132 if (impl->preview_widget)
1133 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1134 impl->preview_widget);
1136 impl->preview_widget = preview_widget;
1137 if (impl->preview_widget)
1139 gtk_widget_show (impl->preview_widget);
1140 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1141 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1142 impl->preview_widget,
1143 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1146 update_preview_widget_visibility (impl);
1149 /* Renders a "Search" icon at an appropriate size for a tree view */
1151 render_search_icon (GtkFileChooserDefault *impl)
1153 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1157 render_recent_icon (GtkFileChooserDefault *impl)
1159 GtkIconTheme *theme;
1162 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1163 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1165 theme = gtk_icon_theme_get_default ();
1167 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1173 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1179 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1180 struct ReloadIconsData
1182 GtkFileChooserDefault *impl;
1183 GtkTreeRowReference *row_ref;
1187 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1189 const GError *error,
1195 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1196 struct ReloadIconsData *data = user_data;
1198 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1201 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1203 if (cancelled || error)
1206 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1208 path = gtk_tree_row_reference_get_path (data->row_ref);
1211 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1212 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1213 SHORTCUTS_COL_PIXBUF, pixbuf,
1215 gtk_tree_path_free (path);
1219 g_object_unref (pixbuf);
1222 gtk_tree_row_reference_free (data->row_ref);
1223 g_object_unref (data->impl);
1226 g_object_unref (cancellable);
1230 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1235 profile_start ("start", NULL);
1237 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1240 for (l = impl->reload_icon_cancellables; l; l = l->next)
1242 GCancellable *cancellable = G_CANCELLABLE (l->data);
1243 g_cancellable_cancel (cancellable);
1245 g_slist_free (impl->reload_icon_cancellables);
1246 impl->reload_icon_cancellables = NULL;
1251 ShortcutType shortcut_type;
1252 gboolean pixbuf_visible;
1255 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1256 SHORTCUTS_COL_DATA, &data,
1257 SHORTCUTS_COL_TYPE, &shortcut_type,
1258 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1264 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1266 GtkFileSystemVolume *volume;
1269 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1270 impl->icon_size, NULL);
1272 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1274 if (g_file_is_native (G_FILE (data)))
1277 struct ReloadIconsData *info;
1278 GtkTreePath *tree_path;
1279 GCancellable *cancellable;
1283 info = g_new0 (struct ReloadIconsData, 1);
1284 info->impl = g_object_ref (impl);
1285 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1286 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1287 gtk_tree_path_free (tree_path);
1289 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1291 shortcuts_reload_icons_get_info_cb,
1293 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1297 GtkIconTheme *icon_theme;
1299 /* Don't call get_info for remote paths to avoid latency and
1301 * If we switch to a better bookmarks file format (XBEL), we
1302 * should use mime info to get a better icon.
1304 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1305 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1306 impl->icon_size, 0, NULL);
1309 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1311 pixbuf = render_search_icon (impl);
1313 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1315 pixbuf = render_recent_icon (impl);
1318 gtk_list_store_set (impl->shortcuts_model, &iter,
1319 SHORTCUTS_COL_PIXBUF, pixbuf,
1323 g_object_unref (pixbuf);
1327 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1331 profile_end ("end", NULL);
1335 shortcuts_find_folder (GtkFileChooserDefault *impl,
1338 GtkTreeSelection *selection;
1342 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1344 g_assert (folder != NULL);
1345 pos = shortcut_find_position (impl, folder);
1348 gtk_tree_selection_unselect_all (selection);
1352 path = gtk_tree_path_new_from_indices (pos, -1);
1353 gtk_tree_selection_select_path (selection, path);
1354 gtk_tree_path_free (path);
1357 /* If a shortcut corresponds to the current folder, selects it */
1359 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1361 shortcuts_find_folder (impl, impl->current_folder);
1364 /* Removes the specified number of rows from the shortcuts list */
1366 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1372 path = gtk_tree_path_new_from_indices (start_row, -1);
1374 for (; n_rows; n_rows--)
1378 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1379 g_assert_not_reached ();
1381 shortcuts_free_row_data (impl, &iter);
1382 gtk_list_store_remove (impl->shortcuts_model, &iter);
1385 gtk_tree_path_free (path);
1389 shortcuts_update_count (GtkFileChooserDefault *impl,
1390 ShortcutsIndex type,
1395 case SHORTCUTS_HOME:
1397 impl->has_home = FALSE;
1399 impl->has_home = TRUE;
1402 case SHORTCUTS_DESKTOP:
1404 impl->has_desktop = FALSE;
1406 impl->has_desktop = TRUE;
1409 case SHORTCUTS_VOLUMES:
1410 impl->num_volumes += value;
1413 case SHORTCUTS_SHORTCUTS:
1414 impl->num_shortcuts += value;
1417 case SHORTCUTS_BOOKMARKS:
1418 impl->num_bookmarks += value;
1421 case SHORTCUTS_CURRENT_FOLDER:
1423 impl->shortcuts_current_folder_active = FALSE;
1425 impl->shortcuts_current_folder_active = TRUE;
1434 struct ShortcutsInsertRequest
1436 GtkFileChooserDefault *impl;
1440 GtkTreeRowReference *row_ref;
1441 ShortcutsIndex type;
1447 get_file_info_finished (GCancellable *cancellable,
1449 const GError *error,
1452 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1456 GCancellable *model_cancellable = NULL;
1457 struct ShortcutsInsertRequest *request = data;
1459 path = gtk_tree_row_reference_get_path (request->row_ref);
1461 /* Handle doesn't exist anymore in the model */
1464 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1466 gtk_tree_path_free (path);
1468 /* validate cancellable, else goto out */
1469 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1470 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1472 if (cancellable != model_cancellable)
1475 /* set the cancellable to NULL in the model (we unref later on) */
1476 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1477 SHORTCUTS_COL_CANCELLABLE, NULL,
1485 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1486 shortcuts_update_count (request->impl, request->type, -1);
1488 if (request->type == SHORTCUTS_HOME)
1492 home = g_file_new_for_path (g_get_home_dir ());
1493 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1494 g_object_unref (home);
1496 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1498 /* Remove the current folder separator */
1499 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1500 shortcuts_remove_rows (request->impl, separator_pos, 1);
1506 if (!request->label_copy)
1507 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1508 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1509 request->impl->icon_size);
1511 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1512 SHORTCUTS_COL_PIXBUF, pixbuf,
1513 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1514 SHORTCUTS_COL_NAME, request->label_copy,
1515 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1516 SHORTCUTS_COL_REMOVABLE, request->removable,
1519 if (request->impl->shortcuts_pane_filter_model)
1520 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1523 g_object_unref (pixbuf);
1526 g_object_unref (request->impl);
1527 g_object_unref (request->file);
1528 gtk_tree_row_reference_free (request->row_ref);
1529 g_free (request->label_copy);
1532 if (model_cancellable)
1533 g_object_unref (model_cancellable);
1536 /* FIXME: GtkFileSystem needs a function to split a remote path
1537 * into hostname and path components, or maybe just have a
1538 * gtk_file_system_path_get_display_name().
1540 * This function is also used in gtkfilechooserbutton.c
1543 _gtk_file_chooser_label_for_file (GFile *file)
1545 const gchar *path, *start, *end, *p;
1546 gchar *uri, *host, *label;
1548 uri = g_file_get_uri (file);
1550 start = strstr (uri, "://");
1554 path = strchr (start, '/');
1559 end = uri + strlen (uri);
1563 /* strip username */
1564 p = strchr (start, '@');
1568 p = strchr (start, ':');
1572 host = g_strndup (start, end - start);
1574 /* Translators: the first string is a path and the second string
1575 * is a hostname. Nautilus and the panel contain the same string
1578 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1584 label = g_strdup (uri);
1592 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1593 * inserts a volume. A position of -1 indicates the end of the tree.
1596 shortcuts_insert_file (GtkFileChooserDefault *impl,
1598 ShortcutType shortcut_type,
1599 GtkFileSystemVolume *volume,
1603 ShortcutsIndex type)
1606 GdkPixbuf *pixbuf = NULL;
1607 gpointer data = NULL;
1609 GtkIconTheme *icon_theme;
1611 profile_start ("start shortcut", NULL);
1613 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1616 label_copy = _gtk_file_system_volume_get_display_name (volume);
1617 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1618 impl->icon_size, NULL);
1620 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1622 if (g_file_is_native (file))
1624 struct ShortcutsInsertRequest *request;
1625 GCancellable *cancellable;
1628 request = g_new0 (struct ShortcutsInsertRequest, 1);
1629 request->impl = g_object_ref (impl);
1630 request->file = g_object_ref (file);
1631 request->name_only = TRUE;
1632 request->removable = removable;
1634 request->type = type;
1636 request->label_copy = g_strdup (label);
1639 gtk_list_store_append (impl->shortcuts_model, &iter);
1641 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1643 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1644 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1645 gtk_tree_path_free (p);
1647 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1648 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1649 get_file_info_finished, request);
1651 gtk_list_store_set (impl->shortcuts_model, &iter,
1652 SHORTCUTS_COL_DATA, g_object_ref (file),
1653 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1654 SHORTCUTS_COL_CANCELLABLE, cancellable,
1657 shortcuts_update_count (impl, type, 1);
1663 /* Don't call get_info for remote paths to avoid latency and
1666 data = g_object_ref (file);
1668 label_copy = g_strdup (label);
1670 label_copy = _gtk_file_chooser_label_for_file (file);
1672 /* If we switch to a better bookmarks file format (XBEL), we
1673 * should use mime info to get a better icon.
1675 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1676 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1677 impl->icon_size, 0, NULL);
1682 g_assert_not_reached ();
1688 gtk_list_store_append (impl->shortcuts_model, &iter);
1690 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1692 shortcuts_update_count (impl, type, 1);
1694 gtk_list_store_set (impl->shortcuts_model, &iter,
1695 SHORTCUTS_COL_PIXBUF, pixbuf,
1696 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1697 SHORTCUTS_COL_NAME, label_copy,
1698 SHORTCUTS_COL_DATA, data,
1699 SHORTCUTS_COL_TYPE, shortcut_type,
1700 SHORTCUTS_COL_REMOVABLE, removable,
1701 SHORTCUTS_COL_CANCELLABLE, NULL,
1704 if (impl->shortcuts_pane_filter_model)
1705 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1707 g_free (label_copy);
1710 g_object_unref (pixbuf);
1712 profile_end ("end", NULL);
1716 shortcuts_append_search (GtkFileChooserDefault *impl)
1721 pixbuf = render_search_icon (impl);
1723 gtk_list_store_append (impl->shortcuts_model, &iter);
1724 gtk_list_store_set (impl->shortcuts_model, &iter,
1725 SHORTCUTS_COL_PIXBUF, pixbuf,
1726 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1727 SHORTCUTS_COL_NAME, _("Search"),
1728 SHORTCUTS_COL_DATA, NULL,
1729 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1730 SHORTCUTS_COL_REMOVABLE, FALSE,
1734 g_object_unref (pixbuf);
1736 impl->has_search = TRUE;
1740 shortcuts_append_recent (GtkFileChooserDefault *impl)
1745 pixbuf = render_recent_icon (impl);
1747 gtk_list_store_append (impl->shortcuts_model, &iter);
1748 gtk_list_store_set (impl->shortcuts_model, &iter,
1749 SHORTCUTS_COL_PIXBUF, pixbuf,
1750 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1751 SHORTCUTS_COL_NAME, _("Recently Used"),
1752 SHORTCUTS_COL_DATA, NULL,
1753 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1754 SHORTCUTS_COL_REMOVABLE, FALSE,
1758 g_object_unref (pixbuf);
1761 /* Appends an item for the user's home directory to the shortcuts model */
1763 shortcuts_append_home (GtkFileChooserDefault *impl)
1765 const char *home_path;
1768 profile_start ("start", NULL);
1770 home_path = g_get_home_dir ();
1771 if (home_path == NULL)
1773 profile_end ("end - no home directory!?", NULL);
1777 home = g_file_new_for_path (home_path);
1778 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1779 impl->has_home = TRUE;
1781 g_object_unref (home);
1783 profile_end ("end", NULL);
1786 /* Appends the ~/Desktop directory to the shortcuts model */
1788 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1793 profile_start ("start", NULL);
1795 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1796 /* "To disable a directory, point it to the homedir."
1797 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1799 if (!g_strcmp0 (name, g_get_home_dir ()))
1801 profile_end ("end", NULL);
1805 file = g_file_new_for_path (name);
1806 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1807 impl->has_desktop = TRUE;
1809 /* We do not actually pop up an error dialog if there is no desktop directory
1810 * because some people may really not want to have one.
1813 g_object_unref (file);
1815 profile_end ("end", NULL);
1818 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1820 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1827 profile_start ("start", NULL);
1829 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1832 for (; bookmarks; bookmarks = bookmarks->next)
1836 file = bookmarks->data;
1838 if (impl->local_only && !g_file_is_native (file))
1841 if (shortcut_find_position (impl, file) != -1)
1844 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1846 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1852 profile_end ("end", NULL);
1854 return num_inserted;
1857 /* Returns the index for the corresponding item in the shortcuts bar */
1859 shortcuts_get_index (GtkFileChooserDefault *impl,
1860 ShortcutsIndex where)
1866 if (where == SHORTCUTS_SEARCH)
1869 n += impl->has_search ? 1 : 0;
1871 if (where == SHORTCUTS_RECENT)
1874 n += 1; /* we always have the recently-used item */
1876 if (where == SHORTCUTS_RECENT_SEPARATOR)
1879 n += 1; /* we always have the separator after the recently-used item */
1881 if (where == SHORTCUTS_HOME)
1884 n += impl->has_home ? 1 : 0;
1886 if (where == SHORTCUTS_DESKTOP)
1889 n += impl->has_desktop ? 1 : 0;
1891 if (where == SHORTCUTS_VOLUMES)
1894 n += impl->num_volumes;
1896 if (where == SHORTCUTS_SHORTCUTS)
1899 n += impl->num_shortcuts;
1901 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1904 /* If there are no bookmarks there won't be a separator */
1905 n += (impl->num_bookmarks > 0) ? 1 : 0;
1907 if (where == SHORTCUTS_BOOKMARKS)
1910 n += impl->num_bookmarks;
1912 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1917 if (where == SHORTCUTS_CURRENT_FOLDER)
1920 g_assert_not_reached ();
1927 /* Adds all the file system volumes to the shortcuts model */
1929 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1934 gboolean old_changing_folders;
1936 profile_start ("start", NULL);
1938 old_changing_folders = impl->changing_folder;
1939 impl->changing_folder = TRUE;
1941 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1942 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1943 impl->num_volumes = 0;
1945 list = _gtk_file_system_list_volumes (impl->file_system);
1949 for (l = list; l; l = l->next)
1951 GtkFileSystemVolume *volume;
1955 if (impl->local_only)
1957 if (_gtk_file_system_volume_is_mounted (volume))
1960 gboolean base_is_native = TRUE;
1962 base_file = _gtk_file_system_volume_get_root (volume);
1963 if (base_file != NULL)
1965 base_is_native = g_file_is_native (base_file);
1966 g_object_unref (base_file);
1969 if (!base_is_native)
1974 shortcuts_insert_file (impl,
1976 SHORTCUT_TYPE_VOLUME,
1977 _gtk_file_system_volume_ref (volume),
1985 impl->num_volumes = n;
1986 g_slist_free (list);
1988 if (impl->shortcuts_pane_filter_model)
1989 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1991 impl->changing_folder = old_changing_folders;
1993 profile_end ("end", NULL);
1996 /* Inserts a separator node in the shortcuts list */
1998 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1999 ShortcutsIndex where)
2003 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2004 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2005 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2007 gtk_list_store_insert (impl->shortcuts_model, &iter,
2008 shortcuts_get_index (impl, where));
2009 gtk_list_store_set (impl->shortcuts_model, &iter,
2010 SHORTCUTS_COL_PIXBUF, NULL,
2011 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2012 SHORTCUTS_COL_NAME, NULL,
2013 SHORTCUTS_COL_DATA, NULL,
2014 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2018 /* Updates the list of bookmarks */
2020 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2023 gboolean old_changing_folders;
2025 GFile *list_selected = NULL;
2026 ShortcutType shortcut_type;
2029 profile_start ("start", NULL);
2031 old_changing_folders = impl->changing_folder;
2032 impl->changing_folder = TRUE;
2034 if (shortcuts_get_selected (impl, &iter))
2036 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2038 SHORTCUTS_COL_DATA, &col_data,
2039 SHORTCUTS_COL_TYPE, &shortcut_type,
2042 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2043 list_selected = g_object_ref (col_data);
2046 if (impl->num_bookmarks > 0)
2047 shortcuts_remove_rows (impl,
2048 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2049 impl->num_bookmarks + 1);
2051 impl->num_bookmarks = 0;
2052 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2054 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2055 shortcuts_append_bookmarks (impl, bookmarks);
2056 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2057 g_slist_free (bookmarks);
2059 if (impl->num_bookmarks == 0)
2060 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2062 if (impl->shortcuts_pane_filter_model)
2063 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2067 shortcuts_find_folder (impl, list_selected);
2068 g_object_unref (list_selected);
2071 impl->changing_folder = old_changing_folders;
2073 profile_end ("end", NULL);
2076 /* Appends a separator and a row to the shortcuts list for the current folder */
2078 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2082 g_assert (!impl->shortcuts_current_folder_active);
2084 g_assert (impl->current_folder != NULL);
2086 pos = shortcut_find_position (impl, impl->current_folder);
2089 GtkFileSystemVolume *volume;
2093 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2096 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2098 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2100 base_file = _gtk_file_system_volume_get_root (volume);
2104 if (base_file && g_file_equal (base_file, impl->current_folder))
2105 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2107 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2110 g_object_unref (base_file);
2114 /* Updates the current folder row in the shortcuts model */
2116 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2120 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2122 if (impl->shortcuts_current_folder_active)
2124 shortcuts_remove_rows (impl, pos, 2);
2125 impl->shortcuts_current_folder_active = FALSE;
2128 shortcuts_add_current_folder (impl);
2131 /* Filter function used for the shortcuts filter model */
2133 shortcuts_pane_filter_cb (GtkTreeModel *model,
2137 GtkFileChooserDefault *impl;
2141 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2143 path = gtk_tree_model_get_path (model, iter);
2147 pos = *gtk_tree_path_get_indices (path);
2148 gtk_tree_path_free (path);
2150 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2153 /* Creates the list model for shortcuts */
2155 shortcuts_model_create (GtkFileChooserDefault *impl)
2157 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2158 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2159 GDK_TYPE_PIXBUF, /* pixbuf */
2160 G_TYPE_STRING, /* name */
2161 G_TYPE_POINTER, /* path or volume */
2162 G_TYPE_INT, /* ShortcutType */
2163 G_TYPE_BOOLEAN, /* removable */
2164 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2165 G_TYPE_POINTER); /* GCancellable */
2167 shortcuts_append_search (impl);
2169 if (impl->recent_manager)
2171 shortcuts_append_recent (impl);
2172 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2175 if (impl->file_system)
2177 shortcuts_append_home (impl);
2178 shortcuts_append_desktop (impl);
2179 shortcuts_add_volumes (impl);
2182 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2183 GTK_TREE_MODEL (impl->shortcuts_model),
2186 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2187 shortcuts_pane_filter_cb,
2192 /* Callback used when the "New Folder" button is clicked */
2194 new_folder_button_clicked (GtkButton *button,
2195 GtkFileChooserDefault *impl)
2200 if (!impl->browse_files_model)
2201 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2203 /* Prevent button from being clicked twice */
2204 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2206 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2208 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2209 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2210 path, impl->list_name_column,
2213 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2214 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2216 impl->list_name_column,
2219 gtk_tree_path_free (path);
2223 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2227 source = g_idle_source_new ();
2228 g_source_set_closure (source,
2229 g_cclosure_new_object (callback, G_OBJECT (impl)));
2230 g_source_attach (source, NULL);
2235 /* Idle handler for creating a new folder after editing its name cell, or for
2236 * canceling the editing.
2239 edited_idle_cb (GtkFileChooserDefault *impl)
2241 gdk_threads_enter ();
2243 g_source_destroy (impl->edited_idle);
2244 impl->edited_idle = NULL;
2246 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2247 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2249 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2251 if (impl->edited_new_text /* not cancelled? */
2252 && (strlen (impl->edited_new_text) != 0)
2253 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2255 GError *error = NULL;
2258 file = g_file_get_child_for_display_name (impl->current_folder,
2259 impl->edited_new_text,
2263 GError *error = NULL;
2265 if (g_file_make_directory (file, NULL, &error))
2266 change_folder_and_display_error (impl, file, FALSE);
2268 error_creating_folder_dialog (impl, file, error);
2270 g_object_unref (file);
2273 error_creating_folder_dialog (impl, file, error);
2275 g_free (impl->edited_new_text);
2276 impl->edited_new_text = NULL;
2279 gdk_threads_leave ();
2285 queue_edited_idle (GtkFileChooserDefault *impl,
2286 const gchar *new_text)
2288 /* We create the folder in an idle handler so that we don't modify the tree
2292 if (!impl->edited_idle)
2293 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2295 g_free (impl->edited_new_text);
2296 impl->edited_new_text = g_strdup (new_text);
2299 /* Callback used from the text cell renderer when the new folder is named */
2301 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2303 const gchar *new_text,
2304 GtkFileChooserDefault *impl)
2306 /* work around bug #154921 */
2307 g_object_set (cell_renderer_text,
2308 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2309 queue_edited_idle (impl, new_text);
2312 /* Callback used from the text cell renderer when the new folder edition gets
2316 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2317 GtkFileChooserDefault *impl)
2319 /* work around bug #154921 */
2320 g_object_set (cell_renderer_text,
2321 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2322 queue_edited_idle (impl, NULL);
2325 /* Creates the widgets for the filter combo box */
2327 filter_create (GtkFileChooserDefault *impl)
2329 GtkCellRenderer *cell;
2332 impl->filter_combo = gtk_combo_box_text_new ();
2333 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2335 /* Get the combo's text renderer and set ellipsize parameters */
2336 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2340 g_object_set (G_OBJECT (cell),
2341 "ellipsize", PANGO_ELLIPSIZE_END,
2344 g_list_free (cells);
2346 g_signal_connect (impl->filter_combo, "changed",
2347 G_CALLBACK (filter_combo_changed), impl);
2349 gtk_widget_set_tooltip_text (impl->filter_combo,
2350 _("Select which types of files are shown"));
2352 return impl->filter_combo;
2356 toolbutton_new (GtkFileChooserDefault *impl,
2365 item = gtk_tool_button_new (NULL, NULL);
2366 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2367 gtk_widget_show (image);
2368 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2370 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2371 g_signal_connect (item, "clicked", callback, impl);
2374 gtk_widget_show (GTK_WIDGET (item));
2376 return GTK_WIDGET (item);
2379 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2381 shortcut_find_position (GtkFileChooserDefault *impl,
2386 int current_folder_separator_idx;
2388 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2391 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2394 /* FIXME: is this still needed? */
2395 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2399 for (i = 0; i < current_folder_separator_idx; i++)
2402 ShortcutType shortcut_type;
2404 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2405 SHORTCUTS_COL_DATA, &col_data,
2406 SHORTCUTS_COL_TYPE, &shortcut_type,
2411 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2413 GtkFileSystemVolume *volume;
2418 base_file = _gtk_file_system_volume_get_root (volume);
2420 exists = base_file && g_file_equal (file, base_file);
2423 g_object_unref (base_file);
2428 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2432 model_file = col_data;
2434 if (model_file && g_file_equal (model_file, file))
2439 if (i < current_folder_separator_idx - 1)
2441 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2442 g_assert_not_reached ();
2449 /* Tries to add a bookmark from a path name */
2451 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2457 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2459 if (shortcut_find_position (impl, file) != -1)
2463 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2465 error_adding_bookmark_dialog (impl, file, error);
2473 add_bookmark_foreach_cb (GtkTreeModel *model,
2478 GtkFileChooserDefault *impl;
2481 impl = (GtkFileChooserDefault *) data;
2483 gtk_tree_model_get (model, iter,
2484 MODEL_COL_FILE, &file,
2487 shortcuts_add_bookmark_from_file (impl, file, -1);
2489 g_object_unref (file);
2492 /* Adds a bookmark from the currently selected item in the file list */
2494 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2496 GtkTreeSelection *selection;
2498 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2500 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2501 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2503 gtk_tree_selection_selected_foreach (selection,
2504 add_bookmark_foreach_cb,
2508 /* Callback used when the "Add bookmark" button is clicked */
2510 add_bookmark_button_clicked_cb (GtkButton *button,
2511 GtkFileChooserDefault *impl)
2513 bookmarks_add_selected_folder (impl);
2516 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2517 * returns FALSE if no shortcut is selected.
2520 shortcuts_get_selected (GtkFileChooserDefault *impl,
2523 GtkTreeSelection *selection;
2524 GtkTreeIter parent_iter;
2526 if (!impl->browse_shortcuts_tree_view)
2529 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2531 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2534 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2540 /* Removes the selected bookmarks */
2542 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2550 if (!shortcuts_get_selected (impl, &iter))
2553 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2554 SHORTCUTS_COL_DATA, &col_data,
2555 SHORTCUTS_COL_REMOVABLE, &removable,
2561 g_assert (col_data != NULL);
2566 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2567 error_removing_bookmark_dialog (impl, file, error);
2570 /* Callback used when the "Remove bookmark" button is clicked */
2572 remove_bookmark_button_clicked_cb (GtkButton *button,
2573 GtkFileChooserDefault *impl)
2575 remove_selected_bookmarks (impl);
2578 struct selection_check_closure {
2579 GtkFileChooserDefault *impl;
2582 gboolean all_folders;
2585 /* Used from gtk_tree_selection_selected_foreach() */
2587 selection_check_foreach_cb (GtkTreeModel *model,
2592 struct selection_check_closure *closure;
2596 gtk_tree_model_get (model, iter,
2597 MODEL_COL_FILE, &file,
2598 MODEL_COL_IS_FOLDER, &is_folder,
2604 g_object_unref (file);
2607 closure->num_selected++;
2609 closure->all_folders = closure->all_folders && is_folder;
2610 closure->all_files = closure->all_files && !is_folder;
2613 /* Checks whether the selected items in the file list are all files or all folders */
2615 selection_check (GtkFileChooserDefault *impl,
2617 gboolean *all_files,
2618 gboolean *all_folders)
2620 struct selection_check_closure closure;
2621 GtkTreeSelection *selection;
2623 closure.impl = impl;
2624 closure.num_selected = 0;
2625 closure.all_files = TRUE;
2626 closure.all_folders = TRUE;
2628 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2629 gtk_tree_selection_selected_foreach (selection,
2630 selection_check_foreach_cb,
2633 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2636 *num_selected = closure.num_selected;
2639 *all_files = closure.all_files;
2642 *all_folders = closure.all_folders;
2645 struct get_selected_file_closure {
2646 GtkFileChooserDefault *impl;
2651 get_selected_file_foreach_cb (GtkTreeModel *model,
2656 struct get_selected_file_closure *closure = data;
2660 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2661 g_object_unref (closure->file);
2662 closure->file = NULL;
2665 gtk_tree_model_get (model, iter,
2666 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2670 /* Returns a selected path from the file list */
2672 get_selected_file (GtkFileChooserDefault *impl)
2674 struct get_selected_file_closure closure;
2675 GtkTreeSelection *selection;
2677 closure.impl = impl;
2678 closure.file = NULL;
2680 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2681 gtk_tree_selection_selected_foreach (selection,
2682 get_selected_file_foreach_cb,
2685 return closure.file;
2689 GtkFileChooserDefault *impl;
2691 } UpdateTooltipData;
2694 update_tooltip (GtkTreeModel *model,
2699 UpdateTooltipData *udata = data;
2701 if (udata->tip == NULL)
2703 gchar *display_name;
2705 gtk_tree_model_get (model, iter,
2706 MODEL_COL_NAME, &display_name,
2709 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2711 g_free (display_name);
2716 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2717 * if there are no selected items *and* the current folder is not in the
2718 * bookmarks list. De-sensitize the button otherwise.
2721 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2724 gboolean all_folders;
2728 selection_check (impl, &num_selected, NULL, &all_folders);
2730 if (num_selected == 0)
2731 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2732 else if (num_selected == 1)
2736 file = get_selected_file (impl);
2737 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2739 g_object_unref (file);
2742 active = all_folders;
2744 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2746 if (impl->browse_files_popup_menu_add_shortcut_item)
2747 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2748 (num_selected == 0) ? FALSE : active);
2752 if (num_selected == 0)
2753 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2754 else if (num_selected > 1)
2755 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2758 GtkTreeSelection *selection;
2759 UpdateTooltipData data;
2761 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2764 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2768 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2773 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2774 * bookmark row is selected in the shortcuts tree.
2777 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2780 gboolean removable = FALSE;
2784 if (shortcuts_get_selected (impl, &iter))
2786 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2787 SHORTCUTS_COL_REMOVABLE, &removable,
2788 SHORTCUTS_COL_NAME, &name,
2790 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2793 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2795 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2797 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2801 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2802 _("Remove the selected bookmark"));
2807 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2810 gboolean removable = FALSE;
2812 if (impl->browse_shortcuts_popup_menu == NULL)
2815 if (shortcuts_get_selected (impl, &iter))
2816 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2817 SHORTCUTS_COL_REMOVABLE, &removable,
2820 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2821 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2824 /* GtkWidget::drag-begin handler for the shortcuts list. */
2826 shortcuts_drag_begin_cb (GtkWidget *widget,
2827 GdkDragContext *context,
2828 GtkFileChooserDefault *impl)
2831 impl->shortcuts_drag_context = g_object_ref (context);
2836 /* Removes the idle handler for outside drags */
2838 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2840 if (!impl->shortcuts_drag_outside_idle)
2843 g_source_destroy (impl->shortcuts_drag_outside_idle);
2844 impl->shortcuts_drag_outside_idle = NULL;
2848 /* GtkWidget::drag-end handler for the shortcuts list. */
2850 shortcuts_drag_end_cb (GtkWidget *widget,
2851 GdkDragContext *context,
2852 GtkFileChooserDefault *impl)
2855 g_object_unref (impl->shortcuts_drag_context);
2857 shortcuts_cancel_drag_outside_idle (impl);
2859 if (!impl->shortcuts_drag_outside)
2862 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2864 impl->shortcuts_drag_outside = FALSE;
2868 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2870 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2871 GdkDragContext *context,
2872 GtkFileChooserDefault *impl)
2874 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2877 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2881 shortcuts_drag_leave_cb (GtkWidget *widget,
2882 GdkDragContext *context,
2884 GtkFileChooserDefault *impl)
2887 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2889 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2893 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2895 GTK_TREE_VIEW_DROP_BEFORE);
2897 g_signal_stop_emission_by_name (widget, "drag-leave");
2900 /* Computes the appropriate row and position for dropping */
2902 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2906 GtkTreeViewDropPosition *pos)
2908 GtkTreeView *tree_view;
2909 GtkTreeViewColumn *column;
2913 int bookmarks_index;
2914 int header_height = 0;
2916 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2918 if (gtk_tree_view_get_headers_visible (tree_view))
2919 header_height = _gtk_tree_view_get_header_height (tree_view);
2921 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2923 if (!gtk_tree_view_get_path_at_pos (tree_view,
2931 row = bookmarks_index + impl->num_bookmarks - 1;
2932 *path = gtk_tree_path_new_from_indices (row, -1);
2933 *pos = GTK_TREE_VIEW_DROP_AFTER;
2937 row = *gtk_tree_path_get_indices (*path);
2938 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2939 gtk_tree_path_free (*path);
2941 if (row < bookmarks_index)
2943 row = bookmarks_index;
2944 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2946 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2948 row = bookmarks_index + impl->num_bookmarks - 1;
2949 *pos = GTK_TREE_VIEW_DROP_AFTER;
2953 if (cell_y < cell.height / 2)
2954 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2956 *pos = GTK_TREE_VIEW_DROP_AFTER;
2959 *path = gtk_tree_path_new_from_indices (row, -1);
2962 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2963 * implement the destination side of DnD by hand, due to limitations in
2964 * GtkTreeView's DnD API.
2967 shortcuts_drag_motion_cb (GtkWidget *widget,
2968 GdkDragContext *context,
2972 GtkFileChooserDefault *impl)
2975 GtkTreeViewDropPosition pos;
2976 GdkDragAction action;
2979 if (gtk_drag_get_source_widget (context) == widget)
2981 shortcuts_cancel_drag_outside_idle (impl);
2983 if (impl->shortcuts_drag_outside)
2985 shortcuts_drag_set_delete_cursor (impl, FALSE);
2986 impl->shortcuts_drag_outside = FALSE;
2991 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
2992 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
2993 action = GDK_ACTION_COPY;
2994 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
2995 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
2996 action = GDK_ACTION_MOVE;
3003 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3004 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3005 gtk_tree_path_free (path);
3009 g_signal_stop_emission_by_name (widget, "drag-motion");
3013 gdk_drag_status (context, action, time_);
3020 /* GtkWidget::drag-drop handler for the shortcuts list. */
3022 shortcuts_drag_drop_cb (GtkWidget *widget,
3023 GdkDragContext *context,
3027 GtkFileChooserDefault *impl)
3030 shortcuts_cancel_drag_outside_idle (impl);
3033 g_signal_stop_emission_by_name (widget, "drag-drop");
3037 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3039 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3040 GtkSelectionData *selection_data,
3046 uris = gtk_selection_data_get_uris (selection_data);
3050 for (i = 0; uris[i]; i++)
3056 file = g_file_new_for_uri (uri);
3058 if (shortcuts_add_bookmark_from_file (impl, file, position))
3061 g_object_unref (file);
3067 /* Reorders the selected bookmark to the specified position */
3069 shortcuts_reorder (GtkFileChooserDefault *impl,
3074 ShortcutType shortcut_type;
3077 int bookmarks_index;
3082 /* Get the selected path */
3084 if (!shortcuts_get_selected (impl, &iter))
3085 g_assert_not_reached ();
3087 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3088 old_position = *gtk_tree_path_get_indices (path);
3089 gtk_tree_path_free (path);
3091 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3092 old_position -= bookmarks_index;
3093 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3095 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3096 SHORTCUTS_COL_NAME, &name,
3097 SHORTCUTS_COL_DATA, &col_data,
3098 SHORTCUTS_COL_TYPE, &shortcut_type,
3100 g_assert (col_data != NULL);
3101 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3104 g_object_ref (file); /* removal below will free file, so we need a new ref */
3106 /* Remove the path from the old position and insert it in the new one */
3108 if (new_position > old_position)
3111 if (old_position == new_position)
3115 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3117 shortcuts_add_bookmark_from_file (impl, file, new_position);
3118 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3121 error_adding_bookmark_dialog (impl, file, error);
3125 g_object_unref (file);
3129 /* Callback used when we get the drag data for the bookmarks list. We add the
3130 * received URIs as bookmarks if they are folders.
3133 shortcuts_drag_data_received_cb (GtkWidget *widget,
3134 GdkDragContext *context,
3137 GtkSelectionData *selection_data,
3142 GtkFileChooserDefault *impl;
3143 GtkTreePath *tree_path;
3144 GtkTreeViewDropPosition tree_pos;
3147 int bookmarks_index;
3149 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3151 /* Compute position */
3153 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3155 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3156 position = *gtk_tree_path_get_indices (tree_path);
3157 gtk_tree_path_free (tree_path);
3159 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3162 g_assert (position >= bookmarks_index);
3163 position -= bookmarks_index;
3165 target = gtk_selection_data_get_target (selection_data);
3167 if (gtk_targets_include_uri (&target, 1))
3168 shortcuts_drop_uris (impl, selection_data, position);
3169 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3170 shortcuts_reorder (impl, position);
3172 g_signal_stop_emission_by_name (widget, "drag-data-received");
3175 /* Callback used to display a tooltip in the shortcuts tree */
3177 shortcuts_query_tooltip_cb (GtkWidget *widget,
3180 gboolean keyboard_mode,
3181 GtkTooltip *tooltip,
3182 GtkFileChooserDefault *impl)
3184 GtkTreeModel *model;
3187 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3195 ShortcutType shortcut_type;
3197 gtk_tree_model_get (model, &iter,
3198 SHORTCUTS_COL_DATA, &col_data,
3199 SHORTCUTS_COL_TYPE, &shortcut_type,
3202 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3204 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3206 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3211 file = G_FILE (col_data);
3212 parse_name = g_file_get_parse_name (file);
3214 gtk_tooltip_set_text (tooltip, parse_name);
3216 g_free (parse_name);
3220 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3224 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3234 /* Callback used when the selection in the shortcuts tree changes */
3236 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3237 GtkFileChooserDefault *impl)
3240 GtkTreeIter child_iter;
3242 bookmarks_check_remove_sensitivity (impl);
3243 shortcuts_check_popup_sensitivity (impl);
3245 if (impl->changing_folder)
3248 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3250 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3253 shortcuts_activate_iter (impl, &child_iter);
3258 shortcuts_row_separator_func (GtkTreeModel *model,
3262 ShortcutType shortcut_type;
3264 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3266 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3270 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3272 GtkFileChooserDefault *impl)
3276 /* don't screw up focus switching with Tab */
3277 if (event->keyval == GDK_KEY_Tab
3278 || event->keyval == GDK_KEY_KP_Tab
3279 || event->keyval == GDK_KEY_ISO_Left_Tab
3280 || event->length < 1)
3283 if (impl->location_entry)
3284 entry = impl->location_entry;
3285 else if (impl->search_entry)
3286 entry = impl->search_entry;
3292 gtk_widget_grab_focus (entry);
3293 return gtk_widget_event (entry, (GdkEvent *) event);
3299 /* Callback used when the file list's popup menu is detached */
3301 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3304 GtkFileChooserDefault *impl;
3306 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3307 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3309 impl->browse_shortcuts_popup_menu = NULL;
3310 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3311 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3315 remove_shortcut_cb (GtkMenuItem *item,
3316 GtkFileChooserDefault *impl)
3318 remove_selected_bookmarks (impl);
3321 /* Rename the selected bookmark */
3323 rename_selected_bookmark (GtkFileChooserDefault *impl)
3327 GtkTreeViewColumn *column;
3328 GtkCellRenderer *cell;
3331 if (shortcuts_get_selected (impl, &iter))
3333 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3334 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3335 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3336 cell = g_list_nth_data (renderers, 1);
3337 g_list_free (renderers);
3338 g_object_set (cell, "editable", TRUE, NULL);
3339 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3340 path, column, cell, TRUE);
3341 gtk_tree_path_free (path);
3346 rename_shortcut_cb (GtkMenuItem *item,
3347 GtkFileChooserDefault *impl)
3349 rename_selected_bookmark (impl);
3352 /* Constructs the popup menu for the file list if needed */
3354 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3358 if (impl->browse_shortcuts_popup_menu)
3361 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3362 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3363 impl->browse_shortcuts_tree_view,
3364 shortcuts_popup_menu_detach_cb);
3366 item = gtk_image_menu_item_new_with_label (_("Remove"));
3367 impl->browse_shortcuts_popup_menu_remove_item = item;
3368 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3369 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3370 g_signal_connect (item, "activate",
3371 G_CALLBACK (remove_shortcut_cb), impl);
3372 gtk_widget_show (item);
3373 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3375 item = gtk_menu_item_new_with_label (_("Rename…"));
3376 impl->browse_shortcuts_popup_menu_rename_item = item;
3377 g_signal_connect (item, "activate",
3378 G_CALLBACK (rename_shortcut_cb), impl);
3379 gtk_widget_show (item);
3380 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3384 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3386 shortcuts_build_popup_menu (impl);
3387 shortcuts_check_popup_sensitivity (impl);
3391 popup_position_func (GtkMenu *menu,
3395 gpointer user_data);
3398 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3399 GdkEventButton *event)
3401 shortcuts_update_popup_menu (impl);
3403 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3404 NULL, NULL, NULL, NULL,
3405 event->button, event->time);
3408 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3410 popup_position_func, impl->browse_shortcuts_tree_view,
3411 0, GDK_CURRENT_TIME);
3412 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3417 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3419 shortcuts_popup_menu_cb (GtkWidget *widget,
3420 GtkFileChooserDefault *impl)
3422 shortcuts_popup_menu (impl, NULL);
3426 /* Callback used when a button is pressed on the shortcuts list.
3427 * We trap button 3 to bring up a popup menu.
3430 shortcuts_button_press_event_cb (GtkWidget *widget,
3431 GdkEventButton *event,
3432 GtkFileChooserDefault *impl)
3434 static gboolean in_press = FALSE;
3440 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
3444 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3450 shortcuts_popup_menu (impl, event);
3455 shortcuts_edited (GtkCellRenderer *cell,
3458 GtkFileChooserDefault *impl)
3464 g_object_set (cell, "editable", FALSE, NULL);
3466 path = gtk_tree_path_new_from_string (path_string);
3467 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3468 g_assert_not_reached ();
3470 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3471 SHORTCUTS_COL_DATA, &shortcut,
3473 gtk_tree_path_free (path);
3475 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3479 shortcuts_editing_canceled (GtkCellRenderer *cell,
3480 GtkFileChooserDefault *impl)
3482 g_object_set (cell, "editable", FALSE, NULL);
3485 /* Creates the widgets for the shortcuts and bookmarks tree */
3487 shortcuts_list_create (GtkFileChooserDefault *impl)
3490 GtkTreeSelection *selection;
3491 GtkTreeViewColumn *column;
3492 GtkCellRenderer *renderer;
3494 /* Target types for dragging a row to/from the shortcuts list */
3495 const GtkTargetEntry tree_model_row_targets[] = {
3496 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3499 /* Scrolled window */
3501 swin = gtk_scrolled_window_new (NULL, NULL);
3502 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3503 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3504 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3506 gtk_widget_show (swin);
3509 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3510 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3511 GTK_STYLE_CLASS_SIDEBAR);
3512 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3513 #ifdef PROFILE_FILE_CHOOSER
3514 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3517 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3519 * 1. user brings up a SAVE dialog
3520 * 2. user clicks on a shortcut in the shortcuts pane
3521 * 3. user starts typing a filename
3523 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3524 * support interactive search. However, we'd rather focus the location entry
3525 * so that the user can type *there*.
3527 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3528 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3529 * if the user starts typing while the focus is in the shortcuts pane.
3531 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3532 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3534 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3535 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3536 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3537 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3538 /* Accessible object name for the file chooser's shortcuts pane */
3539 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3541 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3543 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3545 tree_model_row_targets,
3546 G_N_ELEMENTS (tree_model_row_targets),
3549 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3550 GTK_DEST_DEFAULT_ALL,
3551 tree_model_row_targets,
3552 G_N_ELEMENTS (tree_model_row_targets),
3553 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3554 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3556 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3557 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3558 gtk_tree_selection_set_select_function (selection,
3559 shortcuts_select_func,
3562 g_signal_connect (selection, "changed",
3563 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3565 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3566 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3568 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3569 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3570 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3571 G_CALLBACK (shortcuts_drag_end_cb), impl);
3572 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3573 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3575 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3576 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3577 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3578 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3579 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3580 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3581 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3582 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3584 /* Support tooltips */
3585 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3586 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3587 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3589 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3590 gtk_widget_show (impl->browse_shortcuts_tree_view);
3594 column = gtk_tree_view_column_new ();
3595 /* Column header for the file chooser's shortcuts pane */
3596 gtk_tree_view_column_set_title (column, _("_Places"));
3598 renderer = gtk_cell_renderer_pixbuf_new ();
3599 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3600 gtk_tree_view_column_set_attributes (column, renderer,
3601 "pixbuf", SHORTCUTS_COL_PIXBUF,
3602 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3605 renderer = gtk_cell_renderer_text_new ();
3606 g_object_set (renderer,
3608 "ellipsize", PANGO_ELLIPSIZE_END,
3610 g_signal_connect (renderer, "edited",
3611 G_CALLBACK (shortcuts_edited), impl);
3612 g_signal_connect (renderer, "editing-canceled",
3613 G_CALLBACK (shortcuts_editing_canceled), impl);
3614 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3615 gtk_tree_view_column_set_attributes (column, renderer,
3616 "text", SHORTCUTS_COL_NAME,
3619 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3620 shortcuts_row_separator_func,
3623 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3628 /* Creates the widgets for the shortcuts/bookmarks pane */
3630 shortcuts_pane_create (GtkFileChooserDefault *impl,
3631 GtkSizeGroup *size_group)
3636 GtkStyleContext *context;
3639 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3640 gtk_widget_show (vbox);
3642 /* Shortcuts tree */
3644 widget = shortcuts_list_create (impl);
3646 gtk_size_group_add_widget (size_group, widget);
3647 context = gtk_widget_get_style_context (widget);
3648 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3650 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3652 /* Box for buttons */
3654 toolbar = gtk_toolbar_new ();
3655 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
3656 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3658 context = gtk_widget_get_style_context (toolbar);
3659 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3660 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3662 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3663 gtk_widget_show (toolbar);
3665 /* Add bookmark button */
3666 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3667 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3671 G_CALLBACK (add_bookmark_button_clicked_cb));
3672 g_object_unref (icon);
3674 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3675 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3676 _("Add the selected folder to the Bookmarks"));
3678 /* Remove bookmark button */
3679 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3680 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3684 G_CALLBACK (remove_bookmark_button_clicked_cb));
3685 g_object_unref (icon);
3686 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3687 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3688 _("Remove the selected bookmark"));
3694 key_is_left_or_right (GdkEventKey *event)
3698 modifiers = gtk_accelerator_get_default_mod_mask ();
3700 return ((event->keyval == GDK_KEY_Right
3701 || event->keyval == GDK_KEY_KP_Right
3702 || event->keyval == GDK_KEY_Left
3703 || event->keyval == GDK_KEY_KP_Left)
3704 && (event->state & modifiers) == 0);
3707 /* Handles key press events on the file list, so that we can trap Enter to
3708 * activate the default button on our own. Also, checks to see if '/' has been
3712 browse_files_key_press_event_cb (GtkWidget *widget,
3716 GtkFileChooserDefault *impl;
3717 GdkModifierType no_text_input_mask;
3719 impl = (GtkFileChooserDefault *) data;
3721 no_text_input_mask =
3722 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
3724 if ((event->keyval == GDK_KEY_slash
3725 || event->keyval == GDK_KEY_KP_Divide
3727 || event->keyval == GDK_KEY_asciitilde
3729 ) && !(event->state & no_text_input_mask))
3731 location_popup_handler (impl, event->string);
3735 if (key_is_left_or_right (event))
3737 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3741 if ((event->keyval == GDK_KEY_Return
3742 || event->keyval == GDK_KEY_ISO_Enter
3743 || event->keyval == GDK_KEY_KP_Enter
3744 || event->keyval == GDK_KEY_space
3745 || event->keyval == GDK_KEY_KP_Space)
3746 && !(event->state & gtk_accelerator_get_default_mod_mask ())
3747 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3748 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3752 window = get_toplevel (widget);
3755 GtkWidget *default_widget, *focus_widget;
3757 default_widget = gtk_window_get_default_widget (window);
3758 focus_widget = gtk_window_get_focus (window);
3760 if (widget != default_widget &&
3761 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3763 gtk_window_activate_default (window);
3773 /* Callback used when the file list's popup menu is detached */
3775 popup_menu_detach_cb (GtkWidget *attach_widget,
3778 GtkFileChooserDefault *impl;
3780 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3781 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3783 impl->browse_files_popup_menu = NULL;
3784 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3785 impl->browse_files_popup_menu_hidden_files_item = NULL;
3786 impl->browse_files_popup_menu_copy_file_location_item = NULL;
3789 /* Callback used when the "Add to Bookmarks" menu item is activated */
3791 add_to_shortcuts_cb (GtkMenuItem *item,
3792 GtkFileChooserDefault *impl)
3794 bookmarks_add_selected_folder (impl);
3797 /* callback used to set data to clipboard */
3799 copy_file_get_cb (GtkClipboard *clipboard,
3800 GtkSelectionData *selection_data,
3804 GSList *selected_files = data;
3808 gint num_files = g_slist_length (selected_files);
3813 uris = g_new (gchar *, num_files + 1);
3814 uris[num_files] = NULL; /* null terminator */
3818 for (l = selected_files; l; l = l->next)
3820 GFile *file = (GFile *) l->data;
3822 if (info == SELECTION_URI)
3823 uris[i] = g_file_get_uri (file);
3824 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3825 uris[i] = g_file_get_parse_name (file);
3830 if (info == SELECTION_URI)
3831 gtk_selection_data_set_uris (selection_data, uris);
3832 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3834 char *str = g_strjoinv (" ", uris);
3835 gtk_selection_data_set_text (selection_data, str, -1);
3843 /* callback used to clear the clipboard data */
3845 copy_file_clear_cb (GtkClipboard *clipboard,
3848 GSList *selected_files = data;
3850 g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3851 g_slist_free (selected_files);
3854 /* Callback used when the "Copy file’s location" menu item is activated */
3856 copy_file_location_cb (GtkMenuItem *item,
3857 GtkFileChooserDefault *impl)
3859 GSList *selected_files = NULL;
3861 selected_files = search_get_selected_files (impl);
3865 GtkClipboard *clipboard;
3866 GtkTargetList *target_list;
3867 GtkTargetEntry *targets;
3870 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3872 target_list = gtk_target_list_new (NULL, 0);
3873 gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3874 gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3876 targets = gtk_target_table_new_from_list (target_list, &n_targets);
3877 gtk_target_list_unref (target_list);
3879 gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3884 gtk_target_table_free (targets, n_targets);
3888 /* Callback used when the "Visit this file" menu item is activated */
3890 visit_file_cb (GtkMenuItem *item,
3891 GtkFileChooserDefault *impl)
3895 files = search_get_selected_files (impl);
3897 /* Sigh, just use the first one */
3900 GFile *file = files->data;
3902 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL); /* NULL-GError */
3905 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
3906 g_slist_free (files);
3909 /* callback used when the "Show Hidden Files" menu item is toggled */
3911 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3912 GtkFileChooserDefault *impl)
3915 "show-hidden", gtk_check_menu_item_get_active (item),
3919 /* Callback used when the "Show Size Column" menu item is toggled */
3921 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3922 GtkFileChooserDefault *impl)
3924 impl->show_size_column = gtk_check_menu_item_get_active (item);
3926 gtk_tree_view_column_set_visible (impl->list_size_column,
3927 impl->show_size_column);
3930 /* Shows an error dialog about not being able to select a dragged file */
3932 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3937 _("Could not select file"),
3942 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3947 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3949 for (i = 1; uris[i]; i++)
3952 GError *error = NULL;
3955 file = g_file_new_for_uri (uri);
3957 gtk_file_chooser_default_select_file (chooser, file, &error);
3959 error_selecting_dragged_file_dialog (impl, file, error);
3961 g_object_unref (file);
3965 struct FileListDragData
3967 GtkFileChooserDefault *impl;
3973 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3975 const GError *error,
3978 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3979 struct FileListDragData *data = user_data;
3980 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3982 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3985 data->impl->file_list_drag_data_received_cancellable = NULL;
3987 if (cancelled || error)
3990 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3991 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3992 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3993 change_folder_and_display_error (data->impl, data->file, FALSE);
3996 GError *error = NULL;
3998 gtk_file_chooser_default_unselect_all (chooser);
3999 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4001 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4003 browse_files_center_selected_row (data->impl);
4006 if (data->impl->select_multiple)
4007 file_list_drag_data_select_uris (data->impl, data->uris);
4010 g_object_unref (data->impl);
4011 g_strfreev (data->uris);
4012 g_object_unref (data->file);
4015 g_object_unref (cancellable);
4019 file_list_drag_data_received_cb (GtkWidget *widget,
4020 GdkDragContext *context,
4023 GtkSelectionData *selection_data,
4028 GtkFileChooserDefault *impl;
4033 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4035 /* Allow only drags from other widgets; see bug #533891. */
4036 if (gtk_drag_get_source_widget (context) == widget)
4038 g_signal_stop_emission_by_name (widget, "drag-data-received");
4042 /* Parse the text/uri-list string, navigate to the first one */
4043 uris = gtk_selection_data_get_uris (selection_data);
4044 if (uris && uris[0])
4046 struct FileListDragData *data;
4049 file = g_file_new_for_uri (uri);
4051 data = g_new0 (struct FileListDragData, 1);
4052 data->impl = g_object_ref (impl);
4056 if (impl->file_list_drag_data_received_cancellable)
4057 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4059 impl->file_list_drag_data_received_cancellable =
4060 _gtk_file_system_get_info (impl->file_system, file,
4062 file_list_drag_data_received_get_info_cb,
4066 g_signal_stop_emission_by_name (widget, "drag-data-received");
4069 /* Don't do anything with the drag_drop signal */
4071 file_list_drag_drop_cb (GtkWidget *widget,
4072 GdkDragContext *context,
4076 GtkFileChooserDefault *impl)
4078 g_signal_stop_emission_by_name (widget, "drag-drop");
4082 /* Disable the normal tree drag motion handler, it makes it look like you're
4083 dropping the dragged item onto a tree item */
4085 file_list_drag_motion_cb (GtkWidget *widget,
4086 GdkDragContext *context,
4090 GtkFileChooserDefault *impl)
4092 g_signal_stop_emission_by_name (widget, "drag-motion");
4096 /* Sensitizes the "Copy file’s location" context menu item if there is actually
4097 * a selection active.
4100 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4102 GtkTreeSelection *selection;
4105 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4106 if (gtk_tree_selection_count_selected_rows (selection) == 0)
4111 if (impl->browse_files_popup_menu_copy_file_location_item)
4112 gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4116 file_list_add_image_menu_item (GtkFileChooserDefault *impl,
4117 const char *stock_name,
4118 const char *mnemonic_label,
4123 item = gtk_image_menu_item_new_with_mnemonic (mnemonic_label);
4124 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4125 gtk_image_new_from_stock (stock_name, GTK_ICON_SIZE_MENU));
4126 g_signal_connect (item, "activate", callback, impl);
4127 gtk_widget_show (item);
4128 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4134 file_list_add_check_menu_item (GtkFileChooserDefault *impl,
4135 const char *mnemonic_label,
4140 item = gtk_check_menu_item_new_with_mnemonic (mnemonic_label);
4141 g_signal_connect (item, "toggled", callback, impl);
4142 gtk_widget_show (item);
4143 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4148 /* Constructs the popup menu for the file list if needed */
4150 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4154 if (impl->browse_files_popup_menu)
4157 impl->browse_files_popup_menu = gtk_menu_new ();
4158 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4159 impl->browse_files_tree_view,
4160 popup_menu_detach_cb);
4162 impl->browse_files_popup_menu_visit_file_item = file_list_add_image_menu_item (impl, GTK_STOCK_DIRECTORY, _("_Visit this file"),
4163 G_CALLBACK (visit_file_cb));
4165 impl->browse_files_popup_menu_copy_file_location_item = file_list_add_image_menu_item (impl, GTK_STOCK_COPY, _("_Copy file’s location"),
4166 G_CALLBACK (copy_file_location_cb));
4168 impl->browse_files_popup_menu_add_shortcut_item = file_list_add_image_menu_item (impl, GTK_STOCK_ADD, _("_Add to Bookmarks"),
4169 G_CALLBACK (add_to_shortcuts_cb));
4171 item = gtk_separator_menu_item_new ();
4172 gtk_widget_show (item);
4173 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4175 impl->browse_files_popup_menu_hidden_files_item = file_list_add_check_menu_item (impl, _("Show _Hidden Files"),
4176 G_CALLBACK (show_hidden_toggled_cb));
4178 impl->browse_files_popup_menu_size_column_item = file_list_add_check_menu_item (impl, _("Show _Size Column"),
4179 G_CALLBACK (show_size_column_toggled_cb));
4181 bookmarks_check_add_sensitivity (impl);
4182 check_copy_file_location_sensitivity (impl);
4185 /* Updates the popup menu for the file list, creating it if necessary */
4187 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4189 file_list_build_popup_menu (impl);
4191 /* The sensitivity of the Add to Bookmarks item is set in
4192 * bookmarks_check_add_sensitivity()
4195 /* 'Visit this file' */
4196 gtk_widget_set_visible (impl->browse_files_popup_menu_visit_file_item, (impl->operation_mode != OPERATION_MODE_BROWSE));
4198 /* 'Show Hidden Files' */
4199 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4200 G_CALLBACK (show_hidden_toggled_cb), impl);
4201 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4203 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4204 G_CALLBACK (show_hidden_toggled_cb), impl);
4206 /* 'Show Size Column' */
4207 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4208 G_CALLBACK (show_size_column_toggled_cb), impl);
4209 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4210 impl->show_size_column);
4211 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4212 G_CALLBACK (show_size_column_toggled_cb), impl);
4216 popup_position_func (GtkMenu *menu,
4222 GtkAllocation allocation;
4223 GtkWidget *widget = GTK_WIDGET (user_data);
4224 GdkScreen *screen = gtk_widget_get_screen (widget);
4227 GdkRectangle monitor;
4229 g_return_if_fail (gtk_widget_get_realized (widget));
4231 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4233 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4236 gtk_widget_get_allocation (widget, &allocation);
4237 *x += (allocation.width - req.width) / 2;
4238 *y += (allocation.height - req.height) / 2;
4240 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4241 gtk_menu_set_monitor (menu, monitor_num);
4242 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
4244 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4245 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4251 file_list_popup_menu (GtkFileChooserDefault *impl,
4252 GdkEventButton *event)
4254 file_list_update_popup_menu (impl);
4256 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4257 NULL, NULL, NULL, NULL,
4258 event->button, event->time);
4261 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4263 popup_position_func, impl->browse_files_tree_view,
4264 0, GDK_CURRENT_TIME);
4265 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4271 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4273 list_popup_menu_cb (GtkWidget *widget,
4274 GtkFileChooserDefault *impl)
4276 file_list_popup_menu (impl, NULL);
4280 /* Callback used when a button is pressed on the file list. We trap button 3 to
4281 * bring up a popup menu.
4284 list_button_press_event_cb (GtkWidget *widget,
4285 GdkEventButton *event,
4286 GtkFileChooserDefault *impl)
4288 static gboolean in_press = FALSE;
4293 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
4297 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4300 file_list_popup_menu (impl, event);
4305 OperationMode operation_mode;
4306 gint general_column;
4310 /* Sets the sort column IDs for the file list; needs to be done whenever we
4311 * change the model on the treeview.
4314 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4316 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4317 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4318 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4322 file_list_query_tooltip_cb (GtkWidget *widget,
4325 gboolean keyboard_tip,
4326 GtkTooltip *tooltip,
4329 GtkFileChooserDefault *impl = user_data;
4330 GtkTreeModel *model;
4336 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4340 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4343 &model, &path, &iter))
4346 gtk_tree_model_get (model, &iter,
4347 MODEL_COL_FILE, &file,
4352 gtk_tree_path_free (path);
4356 filename = g_file_get_path (file);
4357 gtk_tooltip_set_text (tooltip, filename);
4358 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4363 g_object_unref (file);
4364 gtk_tree_path_free (path);
4370 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4374 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4375 gtk_cell_renderer_set_fixed_size (renderer,
4376 xpad * 2 + impl->icon_size,
4377 ypad * 2 + impl->icon_size);
4380 /* Creates the widgets for the file list */
4382 create_file_list (GtkFileChooserDefault *impl)
4385 GtkTreeSelection *selection;
4386 GtkTreeViewColumn *column;
4387 GtkCellRenderer *renderer;
4389 /* Scrolled window */
4390 swin = gtk_scrolled_window_new (NULL, NULL);
4391 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4392 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4393 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4396 /* Tree/list view */
4398 impl->browse_files_tree_view = gtk_tree_view_new ();
4399 #ifdef PROFILE_FILE_CHOOSER
4400 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4402 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4403 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4405 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4406 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4408 gtk_drag_dest_set (impl->browse_files_tree_view,
4409 GTK_DEST_DEFAULT_ALL,
4411 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4412 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4414 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4415 G_CALLBACK (list_row_activated), impl);
4416 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4417 G_CALLBACK (browse_files_key_press_event_cb), impl);
4418 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4419 G_CALLBACK (list_popup_menu_cb), impl);
4420 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4421 G_CALLBACK (list_button_press_event_cb), impl);
4423 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4424 G_CALLBACK (file_list_drag_data_received_cb), impl);
4425 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4426 G_CALLBACK (file_list_drag_drop_cb), impl);
4427 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4428 G_CALLBACK (file_list_drag_motion_cb), impl);
4430 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4431 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4432 G_CALLBACK (file_list_query_tooltip_cb), impl);
4434 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4435 gtk_tree_selection_set_select_function (selection,
4438 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4441 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4442 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4444 g_signal_connect (selection, "changed",
4445 G_CALLBACK (list_selection_changed), impl);
4447 /* Keep the column order in sync with update_cell_renderer_attributes() */
4449 /* Filename column */
4451 impl->list_name_column = gtk_tree_view_column_new ();
4452 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4453 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4454 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4456 renderer = gtk_cell_renderer_pixbuf_new ();
4457 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4458 set_icon_cell_renderer_fixed_size (impl, renderer);
4459 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4461 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4462 g_object_set (impl->list_name_renderer,
4463 "ellipsize", PANGO_ELLIPSIZE_END,
4465 g_signal_connect (impl->list_name_renderer, "edited",
4466 G_CALLBACK (renderer_edited_cb), impl);
4467 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4468 G_CALLBACK (renderer_editing_canceled_cb), impl);
4469 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4471 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4475 column = gtk_tree_view_column_new ();
4476 gtk_tree_view_column_set_resizable (column, TRUE);
4477 gtk_tree_view_column_set_title (column, _("Size"));
4479 renderer = gtk_cell_renderer_text_new ();
4480 g_object_set (renderer,
4481 "alignment", PANGO_ALIGN_RIGHT,
4483 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4484 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4485 impl->list_size_column = column;
4487 /* Modification time column */
4489 column = gtk_tree_view_column_new ();
4490 gtk_tree_view_column_set_resizable (column, TRUE);
4491 gtk_tree_view_column_set_title (column, _("Modified"));
4493 renderer = gtk_cell_renderer_text_new ();
4494 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4495 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4496 impl->list_mtime_column = column;
4498 file_list_set_sort_column_ids (impl);
4499 update_cell_renderer_attributes (impl);
4501 gtk_widget_show_all (swin);
4506 /* Creates the widgets for the files/folders pane */
4508 file_pane_create (GtkFileChooserDefault *impl,
4509 GtkSizeGroup *size_group)
4515 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4516 gtk_widget_show (vbox);
4518 /* Box for lists and preview */
4520 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4521 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4522 gtk_widget_show (hbox);
4526 widget = create_file_list (impl);
4527 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4528 gtk_size_group_add_widget (size_group, widget);
4532 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4533 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4534 /* Don't show preview box initially */
4538 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4540 widget = filter_create (impl);
4542 gtk_widget_show (widget);
4543 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4545 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4551 location_entry_create (GtkFileChooserDefault *impl)
4553 if (!impl->location_entry)
4554 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4556 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4557 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4558 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4559 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4562 /* Creates the widgets specific to Save mode */
4564 save_widgets_create (GtkFileChooserDefault *impl)
4569 if (impl->save_widgets != NULL)
4572 location_switch_to_path_bar (impl);
4574 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4576 impl->save_widgets_table = gtk_grid_new ();
4577 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4578 gtk_widget_show (impl->save_widgets_table);
4579 gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4580 gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4584 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4585 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4586 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4587 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4588 gtk_widget_show (widget);
4590 /* Location entry */
4592 location_entry_create (impl);
4593 gtk_widget_set_hexpand (impl->location_entry, TRUE);
4594 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4595 gtk_widget_show (impl->location_entry);
4596 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4599 impl->save_folder_label = gtk_label_new (NULL);
4600 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4601 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4602 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4603 gtk_widget_show (impl->save_folder_label);
4605 impl->save_widgets = vbox;
4606 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4607 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4608 gtk_widget_show (impl->save_widgets);
4611 /* Destroys the widgets specific to Save mode */
4613 save_widgets_destroy (GtkFileChooserDefault *impl)
4615 if (impl->save_widgets == NULL)
4618 gtk_widget_destroy (impl->save_widgets);
4619 impl->save_widgets = NULL;
4620 impl->save_widgets_table = NULL;
4621 impl->location_entry = NULL;
4622 impl->save_folder_label = NULL;
4625 /* Turns on the path bar widget. Can be called even if we are already in that
4629 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4631 if (impl->location_entry)
4633 gtk_widget_destroy (impl->location_entry);
4634 impl->location_entry = NULL;
4637 gtk_widget_hide (impl->location_entry_box);
4640 /* Turns on the location entry. Can be called even if we are already in that
4644 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4646 /* when in search or recent files mode, we are not showing the
4647 * location_entry_box container, so there's no point in switching
4650 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4651 impl->operation_mode == OPERATION_MODE_RECENT)
4656 gtk_widget_show (impl->location_entry_box);
4660 if (!impl->location_entry)
4662 location_entry_create (impl);
4663 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4664 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4667 /* Configure the entry */
4669 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4673 gtk_widget_show (impl->location_entry);
4674 gtk_widget_grab_focus (impl->location_entry);
4677 /* Sets a new location mode. set_buttons determines whether the toggle button
4678 * for the mode will also be changed.
4681 location_mode_set (GtkFileChooserDefault *impl,
4682 LocationMode new_mode,
4683 gboolean set_button)
4685 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4686 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4688 GtkWindow *toplevel;
4689 GtkWidget *current_focus;
4690 gboolean button_active;
4691 gboolean switch_to_file_list;
4695 case LOCATION_MODE_PATH_BAR:
4696 button_active = FALSE;
4698 /* The location_entry will disappear when we switch to path bar mode. So,
4699 * we'll focus the file list in that case, to avoid having a window with
4700 * no focused widget.
4702 toplevel = get_toplevel (GTK_WIDGET (impl));
4703 switch_to_file_list = FALSE;
4706 current_focus = gtk_window_get_focus (toplevel);
4707 if (!current_focus || current_focus == impl->location_entry)
4708 switch_to_file_list = TRUE;
4711 location_switch_to_path_bar (impl);
4713 if (switch_to_file_list)
4714 gtk_widget_grab_focus (impl->browse_files_tree_view);
4718 case LOCATION_MODE_FILENAME_ENTRY:
4719 button_active = TRUE;
4720 location_switch_to_filename_entry (impl);
4724 g_assert_not_reached ();
4730 g_signal_handlers_block_by_func (impl->location_button,
4731 G_CALLBACK (location_button_toggled_cb), impl);
4733 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4735 g_signal_handlers_unblock_by_func (impl->location_button,
4736 G_CALLBACK (location_button_toggled_cb), impl);
4740 impl->location_mode = new_mode;
4744 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4746 /* when in search or recent files mode, we are not showing the
4747 * location_entry_box container, so there's no point in switching
4750 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4751 impl->operation_mode == OPERATION_MODE_RECENT)
4754 /* If the file entry is not visible, show it.
4755 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4757 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4759 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4761 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4763 if (gtk_widget_has_focus (impl->location_entry))
4765 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4769 gtk_widget_grab_focus (impl->location_entry);
4774 /* Callback used when one of the location mode buttons is toggled */
4776 location_button_toggled_cb (GtkToggleButton *toggle,
4777 GtkFileChooserDefault *impl)
4780 LocationMode new_mode;
4782 is_active = gtk_toggle_button_get_active (toggle);
4786 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4787 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4791 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4792 new_mode = LOCATION_MODE_PATH_BAR;
4795 location_mode_set (impl, new_mode, FALSE);
4798 /* Creates a toggle button for the location entry. */
4800 location_button_create (GtkFileChooserDefault *impl)
4805 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4806 gtk_widget_show (image);
4808 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4812 g_signal_connect (impl->location_button, "toggled",
4813 G_CALLBACK (location_button_toggled_cb), impl);
4815 str = _("Type a file name");
4817 gtk_widget_set_tooltip_text (impl->location_button, str);
4818 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4822 PATH_BAR_FOLDER_PATH,
4823 PATH_BAR_SELECT_A_FOLDER,
4824 PATH_BAR_ERROR_NO_FILENAME,
4825 PATH_BAR_ERROR_NO_FOLDER,
4826 PATH_BAR_RECENTLY_USED,
4830 /* Creates the info bar for informational messages or warnings, with its icon and label */
4832 info_bar_create (GtkFileChooserDefault *impl)
4834 GtkWidget *content_area;
4836 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4837 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4838 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4840 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4842 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4843 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4845 gtk_widget_show (impl->browse_select_a_folder_icon);
4846 gtk_widget_show (impl->browse_select_a_folder_label);
4849 /* Sets the info bar to show the appropriate informational or warning message */
4851 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4855 GtkMessageType message_type;
4861 case PATH_BAR_SELECT_A_FOLDER:
4862 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4864 message_type = GTK_MESSAGE_OTHER;
4867 case PATH_BAR_ERROR_NO_FILENAME:
4868 str = _("Please type a file name");
4869 message_type = GTK_MESSAGE_WARNING;
4872 case PATH_BAR_ERROR_NO_FOLDER:
4873 str = _("Please select a folder below");
4874 message_type = GTK_MESSAGE_WARNING;
4878 g_assert_not_reached ();
4882 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4883 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4884 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4885 GTK_ICON_SIZE_MENU);
4886 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4892 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4894 special_mode_widgets_create (GtkFileChooserDefault *impl)
4896 impl->browse_special_mode_icon = gtk_image_new ();
4897 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4898 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4900 impl->browse_special_mode_label = gtk_label_new (NULL);
4901 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4902 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4905 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4907 path_bar_widgets_create (GtkFileChooserDefault *impl)
4909 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4910 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4911 gtk_widget_show (impl->browse_path_bar_hbox);
4913 /* Size group that allows the path bar to be the same size between modes */
4914 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4915 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4917 /* Location button */
4918 location_button_create (impl);
4919 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4920 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4923 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4924 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4925 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4927 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4928 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4931 info_bar_create (impl);
4932 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4933 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4935 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4936 special_mode_widgets_create (impl);
4939 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4940 g_signal_connect (impl->browse_new_folder_button, "clicked",
4941 G_CALLBACK (new_folder_button_clicked), impl);
4942 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4943 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4946 /* Sets the path bar's mode to show a label, the actual folder path, or a
4947 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4948 * if the pathbar is already showing the widgets you expect; otherwise, call
4949 * path_bar_update() instead to set the appropriate widgets automatically.
4952 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4954 gboolean path_bar_visible = FALSE;
4955 gboolean special_mode_widgets_visible = FALSE;
4956 gboolean info_bar_visible = FALSE;
4957 gboolean create_folder_visible = FALSE;
4963 case PATH_BAR_FOLDER_PATH:
4964 path_bar_visible = TRUE;
4967 case PATH_BAR_SELECT_A_FOLDER:
4968 case PATH_BAR_ERROR_NO_FILENAME:
4969 case PATH_BAR_ERROR_NO_FOLDER:
4970 info_bar_set (impl, mode);
4971 info_bar_visible = TRUE;
4974 case PATH_BAR_RECENTLY_USED:
4975 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4977 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4978 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4981 special_mode_widgets_visible = TRUE;
4984 case PATH_BAR_SEARCH:
4985 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
4987 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
4988 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4991 special_mode_widgets_visible = TRUE;
4995 g_assert_not_reached ();
4998 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
4999 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
5000 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
5001 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
5003 if (path_bar_visible)
5005 if (impl->create_folders
5006 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
5007 && impl->operation_mode != OPERATION_MODE_RECENT)
5008 create_folder_visible = TRUE;
5011 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
5014 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5016 browse_widgets_create (GtkFileChooserDefault *impl)
5020 GtkSizeGroup *size_group;
5022 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5023 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5024 gtk_widget_show (impl->browse_widgets_box);
5026 impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5027 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5028 gtk_widget_show (impl->browse_header_box);
5030 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5031 path_bar_widgets_create (impl);
5033 /* Box for the location label and entry */
5035 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5036 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5038 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5039 gtk_widget_show (impl->location_label);
5040 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5042 /* size group is used by the scrolled windows of the panes */
5043 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5047 hpaned = impl->browse_widgets_hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5048 gtk_widget_show (hpaned);
5049 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5051 widget = shortcuts_pane_create (impl, size_group);
5052 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5053 widget = file_pane_create (impl, size_group);
5054 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5055 g_object_unref (size_group);
5059 gtk_file_chooser_default_constructor (GType type,
5060 guint n_construct_properties,
5061 GObjectConstructParam *construct_params)
5063 GtkFileChooserDefault *impl;
5066 profile_start ("start", NULL);
5068 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5069 n_construct_properties,
5071 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5073 g_assert (impl->file_system);
5075 gtk_widget_push_composite_child ();
5077 /* Shortcuts model */
5078 shortcuts_model_create (impl);
5080 /* The browse widgets */
5081 browse_widgets_create (impl);
5083 /* Alignment to hold extra widget */
5084 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5085 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5086 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5087 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5089 gtk_widget_pop_composite_child ();
5090 update_appearance (impl);
5092 profile_end ("end", NULL);
5097 /* Sets the extra_widget by packing it in the appropriate place */
5099 set_extra_widget (GtkFileChooserDefault *impl,
5100 GtkWidget *extra_widget)
5104 g_object_ref (extra_widget);
5105 /* FIXME: is this right ? */
5106 gtk_widget_show (extra_widget);
5109 if (impl->extra_widget)
5111 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5112 g_object_unref (impl->extra_widget);
5115 impl->extra_widget = extra_widget;
5116 if (impl->extra_widget)
5118 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5119 gtk_widget_show (impl->extra_align);
5122 gtk_widget_hide (impl->extra_align);
5126 set_local_only (GtkFileChooserDefault *impl,
5127 gboolean local_only)
5129 if (local_only != impl->local_only)
5131 impl->local_only = local_only;
5133 if (impl->location_entry)
5134 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5136 if (impl->shortcuts_model && impl->file_system)
5138 shortcuts_add_volumes (impl);
5139 shortcuts_add_bookmarks (impl);
5142 if (local_only && impl->current_folder &&
5143 !g_file_is_native (impl->current_folder))
5145 /* If we are pointing to a non-local folder, make an effort to change
5146 * back to a local folder, but it's really up to the app to not cause
5147 * such a situation, so we ignore errors.
5149 const gchar *home = g_get_home_dir ();
5155 home_file = g_file_new_for_path (home);
5157 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5159 g_object_unref (home_file);
5165 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5166 GtkFileChooserDefault *impl)
5168 shortcuts_add_volumes (impl);
5169 shortcuts_add_bookmarks (impl);
5171 bookmarks_check_add_sensitivity (impl);
5172 bookmarks_check_remove_sensitivity (impl);
5173 shortcuts_check_popup_sensitivity (impl);
5176 /* Sets the file chooser to multiple selection mode */
5178 set_select_multiple (GtkFileChooserDefault *impl,
5179 gboolean select_multiple,
5180 gboolean property_notify)
5182 GtkTreeSelection *selection;
5183 GtkSelectionMode mode;
5185 if (select_multiple == impl->select_multiple)
5188 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
5190 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5191 gtk_tree_selection_set_mode (selection, mode);
5193 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5195 impl->select_multiple = select_multiple;
5196 g_object_notify (G_OBJECT (impl), "select-multiple");
5198 check_preview_change (impl);
5202 set_file_system_backend (GtkFileChooserDefault *impl)
5204 profile_start ("start for backend", "default");
5206 impl->file_system = _gtk_file_system_new ();
5208 g_signal_connect (impl->file_system, "volumes-changed",
5209 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5210 g_signal_connect (impl->file_system, "bookmarks-changed",
5211 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5213 profile_end ("end", NULL);
5217 unset_file_system_backend (GtkFileChooserDefault *impl)
5219 g_signal_handlers_disconnect_by_func (impl->file_system,
5220 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5222 g_object_unref (impl->file_system);
5224 impl->file_system = NULL;
5227 /* Saves the widgets around the pathbar so they can be reparented later
5228 * in the correct place. This function must be called paired with
5229 * restore_path_bar().
5232 save_path_bar (GtkFileChooserDefault *impl)
5236 g_object_ref (impl->browse_path_bar_hbox);
5238 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5240 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5243 /* Reparents the path bar and the "Create folder" button to the right place:
5244 * Above the file list in Open mode, or to the right of the "Save in folder:"
5245 * label in Save mode. The save_path_bar() function must be called before this
5249 restore_path_bar (GtkFileChooserDefault *impl)
5251 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5252 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5254 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5255 gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5257 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5258 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5260 gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5261 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5262 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5265 g_assert_not_reached ();
5267 g_object_unref (impl->browse_path_bar_hbox);
5270 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5272 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5276 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5277 MODEL_COL_FILE, &file,
5279 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE);
5280 g_object_unref (file);
5283 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5284 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5285 * function will update all the pathbar's widgets.
5288 path_bar_update (GtkFileChooserDefault *impl)
5292 switch (impl->operation_mode)
5294 case OPERATION_MODE_BROWSE:
5295 mode = PATH_BAR_FOLDER_PATH;
5298 case OPERATION_MODE_RECENT:
5299 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5301 GtkTreeSelection *selection;
5302 gboolean have_selected;
5305 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5307 /* Save mode means single-selection mode, so the following is valid */
5308 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5312 mode = PATH_BAR_FOLDER_PATH;
5313 put_recent_folder_in_pathbar (impl, &iter);
5316 mode = PATH_BAR_SELECT_A_FOLDER;
5319 mode = PATH_BAR_RECENTLY_USED;
5323 case OPERATION_MODE_SEARCH:
5324 mode = PATH_BAR_SEARCH;
5328 g_assert_not_reached ();
5332 path_bar_set_mode (impl, mode);
5336 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5338 if (impl->search_hbox)
5340 gtk_widget_destroy (impl->search_hbox);
5342 impl->search_hbox = NULL;
5343 impl->search_entry = NULL;
5347 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5349 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5353 case OPERATION_MODE_BROWSE:
5354 stop_loading_and_clear_list_model (impl, TRUE);
5357 case OPERATION_MODE_SEARCH:
5358 search_stop_searching (impl, FALSE);
5359 search_clear_model (impl, TRUE);
5361 operation_mode_discard_search_widgets (impl);
5364 case OPERATION_MODE_RECENT:
5365 recent_stop_loading (impl);
5366 recent_clear_model (impl, TRUE);
5370 g_assert_not_reached ();
5375 operation_mode_set_browse (GtkFileChooserDefault *impl)
5377 path_bar_update (impl);
5379 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5380 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5382 gtk_widget_show (impl->location_button);
5383 location_mode_set (impl, impl->location_mode, TRUE);
5385 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5386 gtk_widget_show (impl->location_entry_box);
5391 operation_mode_set_search (GtkFileChooserDefault *impl)
5393 g_assert (impl->search_hbox == NULL);
5394 g_assert (impl->search_entry == NULL);
5395 g_assert (impl->search_model == NULL);
5397 search_setup_widgets (impl);
5401 operation_mode_set_recent (GtkFileChooserDefault *impl)
5403 path_bar_update (impl);
5405 /* Hide the location widgets temporarily */
5406 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5407 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5409 gtk_widget_hide (impl->location_button);
5410 gtk_widget_hide (impl->location_entry_box);
5413 recent_start_loading (impl);
5416 /* Sometimes we need to frob the selection in the shortcuts list manually */
5418 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5420 GtkTreeSelection *selection;
5423 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5425 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5427 path = gtk_tree_path_new_from_indices (pos, -1);
5428 gtk_tree_selection_select_path (selection, path);
5429 gtk_tree_path_free (path);
5431 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5435 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5437 ShortcutsIndex shortcut_to_select;
5439 operation_mode_stop (impl, impl->operation_mode);
5441 impl->operation_mode = mode;
5443 switch (impl->operation_mode)
5445 case OPERATION_MODE_BROWSE:
5446 operation_mode_set_browse (impl);
5447 shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5450 case OPERATION_MODE_SEARCH:
5451 operation_mode_set_search (impl);
5452 shortcut_to_select = SHORTCUTS_SEARCH;
5455 case OPERATION_MODE_RECENT:
5456 operation_mode_set_recent (impl);
5457 shortcut_to_select = SHORTCUTS_RECENT;
5461 g_assert_not_reached ();
5465 if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5466 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5469 /* This function is basically a do_all function.
5471 * It sets the visibility on all the widgets based on the current state, and
5472 * moves the custom_widget if needed.
5475 update_appearance (GtkFileChooserDefault *impl)
5477 save_path_bar (impl);
5479 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5480 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5484 gtk_widget_hide (impl->location_button);
5485 save_widgets_create (impl);
5487 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5488 text = _("Save in _folder:");
5490 text = _("Create in _folder:");
5492 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5494 if (impl->select_multiple)
5496 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5497 "Re-setting to single selection mode.");
5498 set_select_multiple (impl, FALSE, TRUE);
5501 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5502 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5504 gtk_widget_show (impl->location_button);
5505 save_widgets_destroy (impl);
5506 location_mode_set (impl, impl->location_mode, TRUE);
5509 if (impl->location_entry)
5510 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5512 restore_path_bar (impl);
5513 path_bar_update (impl);
5515 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5516 * of files may change depending whether we are in a file or folder-only mode.
5518 gtk_widget_queue_draw (impl->browse_files_tree_view);
5520 emit_default_size_changed (impl);
5524 gtk_file_chooser_default_set_property (GObject *object,
5526 const GValue *value,
5530 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5534 case GTK_FILE_CHOOSER_PROP_ACTION:
5536 GtkFileChooserAction action = g_value_get_enum (value);
5538 if (action != impl->action)
5540 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5542 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5543 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5544 && impl->select_multiple)
5546 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5547 "this is not allowed in multiple selection mode. Resetting the file chooser "
5548 "to single selection mode.");
5549 set_select_multiple (impl, FALSE, TRUE);
5551 impl->action = action;
5552 update_cell_renderer_attributes (impl);
5553 update_appearance (impl);
5554 settings_load (impl);
5559 case GTK_FILE_CHOOSER_PROP_FILTER:
5560 set_current_filter (impl, g_value_get_object (value));
5563 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5564 set_local_only (impl, g_value_get_boolean (value));
5567 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5568 set_preview_widget (impl, g_value_get_object (value));
5571 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5572 impl->preview_widget_active = g_value_get_boolean (value);
5573 update_preview_widget_visibility (impl);
5576 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5577 impl->use_preview_label = g_value_get_boolean (value);
5578 update_preview_widget_visibility (impl);
5581 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5582 set_extra_widget (impl, g_value_get_object (value));
5585 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5587 gboolean select_multiple = g_value_get_boolean (value);
5588 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5589 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5592 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5593 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5594 "leaving the file chooser in single selection mode.");
5598 set_select_multiple (impl, select_multiple, FALSE);
5602 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5604 gboolean show_hidden = g_value_get_boolean (value);
5605 if (show_hidden != impl->show_hidden)
5607 impl->show_hidden = show_hidden;
5609 if (impl->browse_files_model)
5610 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5615 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5617 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5618 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5622 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5624 gboolean create_folders = g_value_get_boolean (value);
5625 impl->create_folders = create_folders;
5626 update_appearance (impl);
5631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5637 gtk_file_chooser_default_get_property (GObject *object,
5642 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5646 case GTK_FILE_CHOOSER_PROP_ACTION:
5647 g_value_set_enum (value, impl->action);
5650 case GTK_FILE_CHOOSER_PROP_FILTER:
5651 g_value_set_object (value, impl->current_filter);
5654 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5655 g_value_set_boolean (value, impl->local_only);
5658 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5659 g_value_set_object (value, impl->preview_widget);
5662 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5663 g_value_set_boolean (value, impl->preview_widget_active);
5666 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5667 g_value_set_boolean (value, impl->use_preview_label);
5670 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5671 g_value_set_object (value, impl->extra_widget);
5674 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5675 g_value_set_boolean (value, impl->select_multiple);
5678 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5679 g_value_set_boolean (value, impl->show_hidden);
5682 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5683 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5686 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5687 g_value_set_boolean (value, impl->create_folders);
5691 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5696 /* This cancels everything that may be going on in the background. */
5698 cancel_all_operations (GtkFileChooserDefault *impl)
5702 pending_select_files_free (impl);
5704 /* cancel all pending operations */
5705 if (impl->pending_cancellables)
5707 for (l = impl->pending_cancellables; l; l = l->next)
5709 GCancellable *cancellable = G_CANCELLABLE (l->data);
5710 g_cancellable_cancel (cancellable);
5712 g_slist_free (impl->pending_cancellables);
5713 impl->pending_cancellables = NULL;
5716 if (impl->reload_icon_cancellables)
5718 for (l = impl->reload_icon_cancellables; l; l = l->next)
5720 GCancellable *cancellable = G_CANCELLABLE (l->data);
5721 g_cancellable_cancel (cancellable);
5723 g_slist_free (impl->reload_icon_cancellables);
5724 impl->reload_icon_cancellables = NULL;
5727 if (impl->loading_shortcuts)
5729 for (l = impl->loading_shortcuts; l; l = l->next)
5731 GCancellable *cancellable = G_CANCELLABLE (l->data);
5732 g_cancellable_cancel (cancellable);
5734 g_slist_free (impl->loading_shortcuts);
5735 impl->loading_shortcuts = NULL;
5738 if (impl->file_list_drag_data_received_cancellable)
5740 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5741 impl->file_list_drag_data_received_cancellable = NULL;
5744 if (impl->update_current_folder_cancellable)
5746 g_cancellable_cancel (impl->update_current_folder_cancellable);
5747 impl->update_current_folder_cancellable = NULL;
5750 if (impl->should_respond_get_info_cancellable)
5752 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5753 impl->should_respond_get_info_cancellable = NULL;
5756 if (impl->update_from_entry_cancellable)
5758 g_cancellable_cancel (impl->update_from_entry_cancellable);
5759 impl->update_from_entry_cancellable = NULL;
5762 if (impl->shortcuts_activate_iter_cancellable)
5764 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5765 impl->shortcuts_activate_iter_cancellable = NULL;
5768 search_stop_searching (impl, TRUE);
5769 recent_stop_loading (impl);
5772 /* Removes the settings signal handler. It's safe to call multiple times */
5774 remove_settings_signal (GtkFileChooserDefault *impl,
5777 if (impl->settings_signal_id)
5779 GtkSettings *settings;
5781 settings = gtk_settings_get_for_screen (screen);
5782 g_signal_handler_disconnect (settings,
5783 impl->settings_signal_id);
5784 impl->settings_signal_id = 0;
5789 gtk_file_chooser_default_dispose (GObject *object)
5791 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5793 cancel_all_operations (impl);
5795 if (impl->extra_widget)
5797 g_object_unref (impl->extra_widget);
5798 impl->extra_widget = NULL;
5801 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5803 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5806 /* We override show-all since we have internal widgets that
5807 * shouldn't be shown when you call show_all(), like the filter
5811 gtk_file_chooser_default_show_all (GtkWidget *widget)
5813 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5815 gtk_widget_show (widget);
5817 if (impl->extra_widget)
5818 gtk_widget_show_all (impl->extra_widget);
5821 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5822 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5825 toplevel_set_focus_cb (GtkWindow *window,
5827 GtkFileChooserDefault *impl)
5829 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5832 /* We monitor the focus widget on our toplevel to be able to know which widget
5833 * was last focused at the time our "should_respond" method gets called.
5836 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5837 GtkWidget *previous_toplevel)
5839 GtkFileChooserDefault *impl;
5840 GtkWidget *toplevel;
5842 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5843 toplevel = gtk_widget_get_toplevel (widget);
5845 if (previous_toplevel &&
5846 impl->toplevel_set_focus_id != 0)
5848 g_signal_handler_disconnect (previous_toplevel,
5849 impl->toplevel_set_focus_id);
5850 impl->toplevel_set_focus_id = 0;
5851 impl->toplevel_last_focus_widget = NULL;
5854 if (gtk_widget_is_toplevel (toplevel))
5856 g_assert (impl->toplevel_set_focus_id == 0);
5857 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5858 G_CALLBACK (toplevel_set_focus_cb), impl);
5859 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5863 /* Changes the icons wherever it is needed */
5865 change_icon_theme (GtkFileChooserDefault *impl)
5867 GtkSettings *settings;
5869 GtkCellRenderer *renderer;
5872 profile_start ("start", NULL);
5874 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5876 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5877 impl->icon_size = MAX (width, height);
5879 impl->icon_size = FALLBACK_ICON_SIZE;
5881 shortcuts_reload_icons (impl);
5882 /* the first cell in the first column is the icon column, and we have a fixed size there */
5883 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5884 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5885 renderer = GTK_CELL_RENDERER (cells->data);
5886 set_icon_cell_renderer_fixed_size (impl, renderer);
5887 g_list_free (cells);
5888 if (impl->browse_files_model)
5889 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5890 gtk_widget_queue_resize (impl->browse_files_tree_view);
5892 profile_end ("end", NULL);
5895 /* Callback used when a GtkSettings value changes */
5897 settings_notify_cb (GObject *object,
5899 GtkFileChooserDefault *impl)
5903 profile_start ("start", NULL);
5905 name = g_param_spec_get_name (pspec);
5907 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5908 strcmp (name, "gtk-icon-sizes") == 0)
5909 change_icon_theme (impl);
5911 profile_end ("end", NULL);
5914 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5918 check_icon_theme (GtkFileChooserDefault *impl)
5920 GtkSettings *settings;
5922 profile_start ("start", NULL);
5924 if (impl->settings_signal_id)
5926 profile_end ("end", NULL);
5930 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5932 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5933 impl->settings_signal_id = g_signal_connect (settings, "notify",
5934 G_CALLBACK (settings_notify_cb), impl);
5936 change_icon_theme (impl);
5939 profile_end ("end", NULL);
5943 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5945 GtkFileChooserDefault *impl;
5947 profile_start ("start", NULL);
5949 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5951 profile_msg (" parent class style_udpated start", NULL);
5952 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5953 profile_msg (" parent class style_updated end", NULL);
5955 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5956 change_icon_theme (impl);
5958 emit_default_size_changed (impl);
5960 profile_end ("end", NULL);
5964 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5965 GdkScreen *previous_screen)
5967 GtkFileChooserDefault *impl;
5969 profile_start ("start", NULL);
5971 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5973 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5974 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5976 remove_settings_signal (impl, previous_screen);
5977 check_icon_theme (impl);
5979 emit_default_size_changed (impl);
5981 profile_end ("end", NULL);
5985 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5986 GtkAllocation *allocation)
5988 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5992 set_sort_column (GtkFileChooserDefault *impl)
5994 GtkTreeSortable *sortable;
5996 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5998 /* can happen when we're still populating the model */
5999 if (sortable == NULL)
6002 gtk_tree_sortable_set_sort_column_id (sortable,
6008 settings_load (GtkFileChooserDefault *impl)
6010 LocationMode location_mode;
6011 gboolean show_hidden;
6012 gboolean show_size_column;
6014 GtkSortType sort_order;
6016 GSettings *settings;
6018 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
6020 location_mode = g_settings_get_enum (settings, SETTINGS_KEY_LOCATION_MODE);
6021 show_hidden = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN);
6022 show_size_column = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6023 sort_column = g_settings_get_enum (settings, SETTINGS_KEY_SORT_COLUMN);
6024 sort_order = g_settings_get_enum (settings, SETTINGS_KEY_SORT_ORDER);
6025 sidebar_width = g_settings_get_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH);
6027 location_mode_set (impl, location_mode, TRUE);
6029 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6031 impl->show_size_column = show_size_column;
6032 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6034 impl->sort_column = sort_column;
6035 impl->sort_order = sort_order;
6036 /* We don't call set_sort_column() here as the models may not have been
6037 * created yet. The individual functions that create and set the models will
6038 * call set_sort_column() themselves.
6041 gtk_paned_set_position (GTK_PANED (impl->browse_widgets_hpaned), sidebar_width);
6045 settings_save (GtkFileChooserDefault *impl)
6047 GSettings *settings;
6049 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
6051 /* All the other state */
6053 g_settings_set_enum (settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6054 g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN,
6055 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6056 g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6057 g_settings_set_enum (settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6058 g_settings_set_enum (settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6059 g_settings_set_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH,
6060 gtk_paned_get_position (GTK_PANED (impl->browse_widgets_hpaned)));
6062 /* Now apply the settings */
6063 g_settings_apply (settings);
6066 /* GtkWidget::realize method */
6068 gtk_file_chooser_default_realize (GtkWidget *widget)
6070 GtkFileChooserDefault *impl;
6072 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6074 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6076 emit_default_size_changed (impl);
6079 /* GtkWidget::map method */
6081 gtk_file_chooser_default_map (GtkWidget *widget)
6083 GtkFileChooserDefault *impl;
6085 profile_start ("start", NULL);
6087 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6089 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6091 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6093 switch (impl->reload_state)
6096 recent_shortcut_handler (impl);
6099 case RELOAD_HAS_FOLDER:
6100 /* Nothing; we are already loading or loaded, so we
6101 * don't need to reload
6106 g_assert_not_reached ();
6110 volumes_bookmarks_changed_cb (impl->file_system, impl);
6112 settings_load (impl);
6114 profile_end ("end", NULL);
6117 /* GtkWidget::unmap method */
6119 gtk_file_chooser_default_unmap (GtkWidget *widget)
6121 GtkFileChooserDefault *impl;
6123 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6125 settings_save (impl);
6127 cancel_all_operations (impl);
6128 impl->reload_state = RELOAD_EMPTY;
6130 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6133 #define COMPARE_DIRECTORIES \
6134 GtkFileChooserDefault *impl = user_data; \
6135 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6136 gboolean dir_a, dir_b; \
6138 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6139 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6141 if (dir_a != dir_b) \
6142 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6144 /* Sort callback for the filename column */
6146 name_sort_func (GtkTreeModel *model,
6151 COMPARE_DIRECTORIES;
6154 const char *key_a, *key_b;
6157 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6158 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6161 result = strcmp (key_a, key_b);
6173 /* Sort callback for the size column */
6175 size_sort_func (GtkTreeModel *model,
6180 COMPARE_DIRECTORIES;
6183 gint64 size_a, size_b;
6185 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6186 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6188 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6192 /* Sort callback for the mtime column */
6194 mtime_sort_func (GtkTreeModel *model,
6199 COMPARE_DIRECTORIES;
6204 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6205 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6207 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6211 /* Callback used when the sort column changes. We cache the sort order for use
6212 * in name_sort_func().
6215 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6216 GtkFileChooserDefault *impl)
6218 gint sort_column_id;
6219 GtkSortType sort_type;
6221 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6223 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6224 impl->sort_column = sort_column_id;
6225 impl->sort_order = sort_type;
6230 set_busy_cursor (GtkFileChooserDefault *impl,
6234 GtkWindow *toplevel;
6235 GdkDisplay *display;
6238 toplevel = get_toplevel (GTK_WIDGET (impl));
6239 widget = GTK_WIDGET (toplevel);
6240 if (!toplevel || !gtk_widget_get_realized (widget))
6243 display = gtk_widget_get_display (widget);
6246 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6250 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6251 gdk_display_flush (display);
6254 g_object_unref (cursor);
6257 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6259 load_set_model (GtkFileChooserDefault *impl)
6261 profile_start ("start", NULL);
6263 g_assert (impl->browse_files_model != NULL);
6265 profile_msg (" gtk_tree_view_set_model start", NULL);
6266 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6267 GTK_TREE_MODEL (impl->browse_files_model));
6268 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6269 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6271 file_list_set_sort_column_ids (impl);
6272 set_sort_column (impl);
6273 profile_msg (" gtk_tree_view_set_model end", NULL);
6274 impl->list_sort_ascending = TRUE;
6276 profile_end ("end", NULL);
6279 /* Timeout callback used when the loading timer expires */
6281 load_timeout_cb (gpointer data)
6283 GtkFileChooserDefault *impl;
6285 profile_start ("start", NULL);
6287 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6288 g_assert (impl->load_state == LOAD_PRELOAD);
6289 g_assert (impl->load_timeout_id != 0);
6290 g_assert (impl->browse_files_model != NULL);
6292 impl->load_timeout_id = 0;
6293 impl->load_state = LOAD_LOADING;
6295 load_set_model (impl);
6297 profile_end ("end", NULL);
6302 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6304 load_setup_timer (GtkFileChooserDefault *impl)
6306 g_assert (impl->load_timeout_id == 0);
6307 g_assert (impl->load_state != LOAD_PRELOAD);
6309 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6310 impl->load_state = LOAD_PRELOAD;
6313 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6315 load_remove_timer (GtkFileChooserDefault *impl)
6317 if (impl->load_timeout_id != 0)
6319 g_assert (impl->load_state == LOAD_PRELOAD);
6321 g_source_remove (impl->load_timeout_id);
6322 impl->load_timeout_id = 0;
6323 impl->load_state = LOAD_EMPTY;
6326 g_assert (impl->load_state == LOAD_EMPTY ||
6327 impl->load_state == LOAD_LOADING ||
6328 impl->load_state == LOAD_FINISHED);
6331 /* Selects the first row in the file list */
6333 browse_files_select_first_row (GtkFileChooserDefault *impl)
6336 GtkTreeIter dummy_iter;
6337 GtkTreeModel *tree_model;
6339 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6344 path = gtk_tree_path_new_from_indices (0, -1);
6346 /* If the list is empty, do nothing. */
6347 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6348 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6350 gtk_tree_path_free (path);
6353 struct center_selected_row_closure {
6354 GtkFileChooserDefault *impl;
6355 gboolean already_centered;
6358 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6359 * selected row in the tree view.
6362 center_selected_row_foreach_cb (GtkTreeModel *model,
6367 struct center_selected_row_closure *closure;
6370 if (closure->already_centered)
6373 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6374 closure->already_centered = TRUE;
6377 /* Centers the selected row in the tree view */
6379 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6381 struct center_selected_row_closure closure;
6382 GtkTreeSelection *selection;
6384 closure.impl = impl;
6385 closure.already_centered = FALSE;
6387 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6388 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6392 show_and_select_files (GtkFileChooserDefault *impl,
6395 GtkTreeSelection *selection;
6396 GtkFileSystemModel *fsmodel;
6397 gboolean enabled_hidden, removed_filters;
6398 gboolean selected_a_file;
6401 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6402 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6404 enabled_hidden = impl->show_hidden;
6405 removed_filters = (impl->current_filter == NULL);
6407 selected_a_file = FALSE;
6409 for (walk = files; walk; walk = walk->next)
6411 GFile *file = walk->data;
6414 /* Is it a hidden file? */
6416 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6419 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6421 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6423 if (!enabled_hidden &&
6424 (g_file_info_get_is_hidden (info) ||
6425 g_file_info_get_is_backup (info)))
6427 g_object_set (impl, "show-hidden", TRUE, NULL);
6428 enabled_hidden = TRUE;
6432 /* Is it a filtered file? */
6434 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6435 continue; /* re-get the iter as it may change when the model refilters */
6437 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6439 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6440 if (!removed_filters)
6442 set_current_filter (impl, NULL);
6443 removed_filters = TRUE;
6447 /* Okay, can we select the file now? */
6449 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6452 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6456 gtk_tree_selection_select_iter (selection, &iter);
6458 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6459 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6461 gtk_tree_path_free (path);
6463 selected_a_file = TRUE;
6467 browse_files_center_selected_row (impl);
6469 return selected_a_file;
6472 /* Processes the pending operation when a folder is finished loading */
6474 pending_select_files_process (GtkFileChooserDefault *impl)
6476 g_assert (impl->load_state == LOAD_FINISHED);
6477 g_assert (impl->browse_files_model != NULL);
6479 if (impl->pending_select_files)
6481 show_and_select_files (impl, impl->pending_select_files);
6482 pending_select_files_free (impl);
6483 browse_files_center_selected_row (impl);
6487 /* We only select the first row if the chooser is actually mapped ---
6488 * selecting the first row is to help the user when he is interacting with
6489 * the chooser, but sometimes a chooser works not on behalf of the user,
6490 * but rather on behalf of something else like GtkFileChooserButton. In
6491 * that case, the chooser's selection should be what the caller expects,
6492 * as the user can't see that something else got selected. See bug #165264.
6494 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6495 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6496 browse_files_select_first_row (impl);
6499 g_assert (impl->pending_select_files == NULL);
6503 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6508 info = g_file_query_info (impl->current_folder,
6509 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6510 G_FILE_QUERY_INFO_NONE,
6515 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6516 g_object_unref (info);
6519 msg = g_strdup (_("Could not read the contents of the folder"));
6521 error_message (impl, msg, error->message);
6525 /* Callback used when the file system model finishes loading */
6527 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6529 GtkFileChooserDefault *impl)
6531 profile_start ("start", NULL);
6534 show_error_on_reading_current_folder (impl, error);
6536 if (impl->load_state == LOAD_PRELOAD)
6538 load_remove_timer (impl);
6539 load_set_model (impl);
6541 else if (impl->load_state == LOAD_LOADING)
6547 /* We can't g_assert_not_reached(), as something other than us may have
6548 * initiated a folder reload. See #165556.
6550 profile_end ("end", NULL);
6554 g_assert (impl->load_timeout_id == 0);
6556 impl->load_state = LOAD_FINISHED;
6558 pending_select_files_process (impl);
6559 set_busy_cursor (impl, FALSE);
6560 #ifdef PROFILE_FILE_CHOOSER
6561 access ("MARK: *** FINISHED LOADING", F_OK);
6564 profile_end ("end", NULL);
6568 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6569 gboolean remove_from_treeview)
6571 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6573 if (impl->browse_files_model)
6575 g_object_unref (impl->browse_files_model);
6576 impl->browse_files_model = NULL;
6579 if (remove_from_treeview)
6580 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6584 my_g_format_time_for_display (glong secs)
6589 time_t time_mtime, time_now;
6590 const gchar *format;
6591 gchar *locale_format = NULL;
6593 char *date_str = NULL;
6595 const char *locale, *dot = NULL;
6596 gint64 codepage = -1;
6602 #ifdef HAVE_LOCALTIME_R
6603 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6606 struct tm *ptm = localtime ((time_t *) &time_mtime);
6610 g_warning ("ptm != NULL failed");
6612 return g_strdup (_("Unknown"));
6615 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6617 #endif /* HAVE_LOCALTIME_R */
6619 g_date_set_time_t (&mtime, time_mtime);
6620 time_now = time (NULL);
6621 g_date_set_time_t (&now, time_now);
6623 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6625 /* Translators: %H means "hours" and %M means "minutes" */
6627 format = _("%H:%M");
6628 else if (days_diff == 1)
6629 format = _("Yesterday at %H:%M");
6632 if (days_diff > 1 && days_diff < 7)
6633 format = "%A"; /* Days from last week */
6635 format = "%x"; /* Any other date */
6639 /* g_locale_from_utf8() returns a string in the system
6640 * code-page, which is not always the same as that used by the C
6641 * library. For instance when running a GTK+ program with
6642 * LANG=ko on an English version of Windows, the system
6643 * code-page is 1252, but the code-page used by the C library is
6644 * 949. (It's GTK+ itself that sets the C library locale when it
6645 * notices the LANG environment variable. See gtkmain.c The
6646 * Microsoft C library doesn't look at any locale environment
6647 * variables.) We need to pass strftime() a string in the C
6648 * library's code-page. See bug #509885.
6650 locale = setlocale (LC_ALL, NULL);
6652 dot = strchr (locale, '.');
6655 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6657 /* All codepages should fit in 16 bits AFAIK */
6658 if (codepage > 0 && codepage < 65536)
6660 sprintf (charset, "CP%u", (guint) codepage);
6661 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6665 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6667 if (locale_format != NULL &&
6668 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6671 /* As above but in opposite direction... */
6672 if (codepage > 0 && codepage < 65536)
6673 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6675 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6679 if (date_str == NULL)
6680 date_str = g_strdup (_("Unknown"));
6682 g_free (locale_format);
6687 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6689 GFileAttributeType type;
6692 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6693 g_file_info_set_attribute (to, attribute, type, value);
6697 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6699 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6700 GFile *file = G_FILE (object);
6701 GFileInfo *queried, *info;
6704 queried = g_file_query_info_finish (file, res, NULL);
6705 if (queried == NULL)
6708 gdk_threads_enter ();
6710 /* now we know model is valid */
6712 /* file was deleted */
6713 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6715 gdk_threads_leave ();
6719 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6721 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6722 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6723 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6725 _gtk_file_system_model_update_file (model, file, info);
6727 g_object_unref (info);
6729 gdk_threads_leave ();
6733 file_system_model_set (GtkFileSystemModel *model,
6740 GtkFileChooserDefault *impl = data;
6744 case MODEL_COL_FILE:
6745 g_value_set_object (value, file);
6747 case MODEL_COL_NAME:
6749 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6751 g_value_set_string (value, g_file_info_get_display_name (info));
6753 case MODEL_COL_NAME_COLLATED:
6755 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6757 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6759 case MODEL_COL_IS_FOLDER:
6760 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6762 case MODEL_COL_IS_SENSITIVE:
6765 gboolean sensitive = TRUE;
6767 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6768 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
6770 sensitive = TRUE; /* for file modes... */
6772 else if (!_gtk_file_info_consider_as_directory (info))
6774 sensitive = FALSE; /* for folder modes, files are not sensitive... */
6778 /* ... and for folder modes, folders are sensitive only if the filter says so */
6780 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6781 g_assert_not_reached ();
6782 sensitive = !_gtk_file_system_model_iter_is_filtered_out (model, &iter);
6785 g_value_set_boolean (value, sensitive);
6788 g_value_set_boolean (value, TRUE);
6790 case MODEL_COL_PIXBUF:
6793 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6795 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6799 GtkTreeModel *tree_model;
6800 GtkTreePath *path, *start, *end;
6803 if (impl->browse_files_tree_view == NULL ||
6804 g_file_info_has_attribute (info, "filechooser::queried"))
6807 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6808 if (tree_model != GTK_TREE_MODEL (model))
6811 if (!_gtk_file_system_model_get_iter_for_file (model,
6814 g_assert_not_reached ();
6815 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6817 path = gtk_tree_model_get_path (tree_model, &iter);
6818 if (gtk_tree_path_compare (start, path) != 1 &&
6819 gtk_tree_path_compare (path, end) != 1)
6821 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6822 g_file_query_info_async (file,
6823 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6824 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6825 G_FILE_ATTRIBUTE_STANDARD_ICON,
6826 G_FILE_QUERY_INFO_NONE,
6828 _gtk_file_system_model_get_cancellable (model),
6829 file_system_model_got_thumbnail,
6832 gtk_tree_path_free (path);
6833 gtk_tree_path_free (start);
6834 gtk_tree_path_free (end);
6839 g_value_set_object (value, NULL);
6841 case MODEL_COL_SIZE:
6842 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6844 case MODEL_COL_SIZE_TEXT:
6845 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6846 g_value_set_string (value, NULL);
6848 g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6850 case MODEL_COL_MTIME:
6851 case MODEL_COL_MTIME_TEXT:
6856 g_file_info_get_modification_time (info, &tv);
6857 if (column == MODEL_COL_MTIME)
6858 g_value_set_long (value, tv.tv_sec);
6859 else if (tv.tv_sec == 0)
6860 g_value_set_static_string (value, _("Unknown"));
6862 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6865 case MODEL_COL_ELLIPSIZE:
6866 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6869 g_assert_not_reached ();
6876 /* Gets rid of the old list model and creates a new one for the current folder */
6878 set_list_model (GtkFileChooserDefault *impl,
6881 g_assert (impl->current_folder != NULL);
6883 profile_start ("start", NULL);
6885 stop_loading_and_clear_list_model (impl, TRUE);
6887 set_busy_cursor (impl, TRUE);
6889 impl->browse_files_model =
6890 _gtk_file_system_model_new_for_directory (impl->current_folder,
6892 file_system_model_set,
6894 MODEL_COLUMN_TYPES);
6896 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6898 profile_msg (" set sort function", NULL);
6899 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6900 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6901 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6902 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6903 set_sort_column (impl);
6904 impl->list_sort_ascending = TRUE;
6905 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6906 G_CALLBACK (list_sort_column_changed_cb), impl);
6908 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6910 g_signal_connect (impl->browse_files_model, "finished-loading",
6911 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6913 _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
6915 profile_end ("end", NULL);
6920 struct update_chooser_entry_selected_foreach_closure {
6922 GtkTreeIter first_selected_iter;
6926 compare_utf8_filenames (const gchar *a,
6929 gchar *a_folded, *b_folded;
6932 a_folded = g_utf8_strdown (a, -1);
6933 b_folded = g_utf8_strdown (b, -1);
6935 retval = strcmp (a_folded, b_folded);
6944 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6949 struct update_chooser_entry_selected_foreach_closure *closure;
6952 closure->num_selected++;
6954 if (closure->num_selected == 1)
6955 closure->first_selected_iter = *iter;
6959 update_chooser_entry (GtkFileChooserDefault *impl)
6961 GtkTreeSelection *selection;
6962 struct update_chooser_entry_selected_foreach_closure closure;
6964 /* no need to update the file chooser's entry if there's no entry */
6965 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6966 !impl->location_entry)
6969 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6970 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6971 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6972 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6973 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6976 g_assert (impl->location_entry != NULL);
6978 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6979 closure.num_selected = 0;
6980 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6982 if (closure.num_selected == 0)
6984 if (impl->operation_mode == OPERATION_MODE_RECENT)
6985 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6987 goto maybe_clear_entry;
6989 else if (closure.num_selected == 1)
6991 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6994 gboolean change_entry;
6996 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6998 /* If the cursor moved to the row of the newly created folder,
6999 * retrieving info will return NULL.
7004 g_free (impl->browse_files_last_selected_name);
7005 impl->browse_files_last_selected_name =
7006 g_strdup (g_file_info_get_display_name (info));
7008 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7009 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7010 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7012 /* Don't change the name when clicking on a folder... */
7013 change_entry = (! _gtk_file_info_consider_as_directory (info));
7016 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7020 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7022 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7023 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7028 else if (impl->operation_mode == OPERATION_MODE_RECENT
7029 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7033 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7035 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7036 MODEL_COL_FILE, &folder,
7038 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7039 g_object_unref (folder);
7045 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7046 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7048 /* Multiple selection, so just clear the entry. */
7049 g_free (impl->browse_files_last_selected_name);
7050 impl->browse_files_last_selected_name = NULL;
7052 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7058 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7059 && impl->browse_files_last_selected_name)
7061 const char *entry_text;
7063 gboolean clear_entry;
7065 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7066 len = strlen (entry_text);
7069 /* The file chooser entry may have appended a "/" to its text.
7070 * So take it out, and compare the result to the old selection.
7072 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7076 tmp = g_strndup (entry_text, len - 1);
7077 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7081 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7084 clear_entry = FALSE;
7087 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7092 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7096 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7100 struct UpdateCurrentFolderData
7102 GtkFileChooserDefault *impl;
7104 gboolean keep_trail;
7105 gboolean clear_entry;
7106 GFile *original_file;
7107 GError *original_error;
7111 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7112 GtkFileSystemVolume *volume,
7113 const GError *error,
7116 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7117 struct UpdateCurrentFolderData *data = user_data;
7118 GtkFileChooserDefault *impl = data->impl;
7120 if (cancellable != impl->update_current_folder_cancellable)
7123 impl->update_current_folder_cancellable = NULL;
7124 set_busy_cursor (impl, FALSE);
7131 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7132 impl->reload_state = RELOAD_EMPTY;
7136 change_folder_and_display_error (impl, data->file, data->clear_entry);
7139 g_object_unref (data->file);
7142 g_object_unref (cancellable);
7146 update_current_folder_get_info_cb (GCancellable *cancellable,
7148 const GError *error,
7151 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7152 struct UpdateCurrentFolderData *data = user_data;
7153 GtkFileChooserDefault *impl = data->impl;
7155 if (cancellable != impl->update_current_folder_cancellable)
7158 impl->update_current_folder_cancellable = NULL;
7159 impl->reload_state = RELOAD_EMPTY;
7161 set_busy_cursor (impl, FALSE);
7170 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7172 GMountOperation *mount_operation;
7173 GtkWidget *toplevel;
7175 g_object_unref (cancellable);
7176 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7178 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7180 set_busy_cursor (impl, TRUE);
7182 impl->update_current_folder_cancellable =
7183 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7185 update_current_folder_mount_enclosing_volume_cb,
7191 if (!data->original_file)
7193 data->original_file = g_object_ref (data->file);
7194 data->original_error = g_error_copy (error);
7197 parent_file = g_file_get_parent (data->file);
7199 /* get parent path and try to change the folder to that */
7202 g_object_unref (data->file);
7203 data->file = parent_file;
7205 g_object_unref (cancellable);
7207 /* restart the update current folder operation */
7208 impl->reload_state = RELOAD_HAS_FOLDER;
7210 impl->update_current_folder_cancellable =
7211 _gtk_file_system_get_info (impl->file_system, data->file,
7213 update_current_folder_get_info_cb,
7216 set_busy_cursor (impl, TRUE);
7222 /* Error and bail out, ignoring "not found" errors since they're useless:
7223 * they only happen when a program defaults to a folder that has been (re)moved.
7225 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7226 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7228 g_error_free (data->original_error);
7230 g_object_unref (data->original_file);
7236 if (data->original_file)
7238 /* Error and bail out, ignoring "not found" errors since they're useless:
7239 * they only happen when a program defaults to a folder that has been (re)moved.
7241 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7242 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7244 g_error_free (data->original_error);
7246 g_object_unref (data->original_file);
7249 if (! _gtk_file_info_consider_as_directory (info))
7252 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail);
7254 if (impl->current_folder != data->file)
7256 if (impl->current_folder)
7257 g_object_unref (impl->current_folder);
7259 impl->current_folder = g_object_ref (data->file);
7262 impl->reload_state = RELOAD_HAS_FOLDER;
7264 /* Update the widgets that may trigger a folder change themselves. */
7266 if (!impl->changing_folder)
7268 impl->changing_folder = TRUE;
7270 shortcuts_update_current_folder (impl);
7272 impl->changing_folder = FALSE;
7275 /* Set the folder on the save entry */
7277 if (impl->location_entry)
7279 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7280 impl->current_folder);
7282 if (data->clear_entry)
7283 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7286 /* Create a new list model. This is slightly evil; we store the result value
7287 * but perform more actions rather than returning immediately even if it
7288 * generates an error.
7290 set_list_model (impl, NULL);
7292 /* Refresh controls */
7294 shortcuts_find_current_folder (impl);
7296 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7298 check_preview_change (impl);
7299 bookmarks_check_add_sensitivity (impl);
7301 g_signal_emit_by_name (impl, "selection-changed", 0);
7304 g_object_unref (data->file);
7307 g_object_unref (cancellable);
7311 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7313 gboolean keep_trail,
7314 gboolean clear_entry,
7317 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7318 struct UpdateCurrentFolderData *data;
7320 profile_start ("start", NULL);
7322 g_object_ref (file);
7324 operation_mode_set (impl, OPERATION_MODE_BROWSE);
7326 if (impl->local_only && !g_file_is_native (file))
7328 g_set_error_literal (error,
7329 GTK_FILE_CHOOSER_ERROR,
7330 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7331 _("Cannot change to folder because it is not local"));
7333 g_object_unref (file);
7334 profile_end ("end - not local", NULL);
7338 if (impl->update_current_folder_cancellable)
7339 g_cancellable_cancel (impl->update_current_folder_cancellable);
7341 /* Test validity of path here. */
7342 data = g_new0 (struct UpdateCurrentFolderData, 1);
7344 data->file = g_object_ref (file);
7345 data->keep_trail = keep_trail;
7346 data->clear_entry = clear_entry;
7348 impl->reload_state = RELOAD_HAS_FOLDER;
7350 impl->update_current_folder_cancellable =
7351 _gtk_file_system_get_info (impl->file_system, file,
7353 update_current_folder_get_info_cb,
7356 set_busy_cursor (impl, TRUE);
7357 g_object_unref (file);
7359 profile_end ("end", NULL);
7364 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7366 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7368 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7369 impl->operation_mode == OPERATION_MODE_RECENT)
7372 if (impl->current_folder)
7373 return g_object_ref (impl->current_folder);
7379 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7382 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7384 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7385 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7387 pending_select_files_free (impl);
7388 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), name);
7392 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7396 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7400 parent_file = g_file_get_parent (file);
7403 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7405 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7406 impl->operation_mode == OPERATION_MODE_RECENT ||
7407 impl->load_state == LOAD_EMPTY)
7413 g_assert (impl->current_folder != NULL);
7415 same_path = g_file_equal (parent_file, impl->current_folder);
7418 if (same_path && impl->load_state == LOAD_FINISHED)
7423 files.data = (gpointer) file;
7426 result = show_and_select_files (impl, &files);
7427 g_object_unref (parent_file);
7431 pending_select_files_add (impl, file);
7437 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7438 g_object_unref (parent_file);
7442 g_object_unref (parent_file);
7447 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7450 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7451 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7454 if (!impl->browse_files_model)
7457 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7462 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7467 maybe_select (GtkTreeModel *model,
7472 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7473 GtkTreeSelection *selection;
7474 gboolean is_sensitive;
7477 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7479 gtk_tree_model_get (model, iter,
7480 MODEL_COL_IS_FOLDER, &is_folder,
7481 MODEL_COL_IS_SENSITIVE, &is_sensitive,
7485 ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7486 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)))
7487 gtk_tree_selection_select_iter (selection, iter);
7489 gtk_tree_selection_unselect_iter (selection, iter);
7495 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7497 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7499 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7500 impl->operation_mode == OPERATION_MODE_RECENT)
7502 GtkTreeSelection *selection;
7504 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7505 gtk_tree_selection_select_all (selection);
7509 if (impl->select_multiple)
7510 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7511 maybe_select, impl);
7515 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7517 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7518 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7520 gtk_tree_selection_unselect_all (selection);
7521 pending_select_files_free (impl);
7524 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7526 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7528 * is_empty_ret - whether the file entry is totally empty
7530 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7531 * the path will be "$cwd/foobar")
7534 check_save_entry (GtkFileChooserDefault *impl,
7536 gboolean *is_well_formed_ret,
7537 gboolean *is_empty_ret,
7538 gboolean *is_file_part_empty_ret,
7539 gboolean *is_folder)
7541 GtkFileChooserEntry *chooser_entry;
7542 GFile *current_folder;
7543 const char *file_part;
7547 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7548 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7549 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7550 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7551 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7553 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7555 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7558 *is_well_formed_ret = TRUE;
7559 *is_empty_ret = TRUE;
7560 *is_file_part_empty_ret = TRUE;
7566 *is_empty_ret = FALSE;
7568 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7569 if (!current_folder)
7572 *is_well_formed_ret = FALSE;
7573 *is_file_part_empty_ret = FALSE;
7579 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7581 if (!file_part || file_part[0] == '\0')
7583 *file_ret = current_folder;
7584 *is_well_formed_ret = TRUE;
7585 *is_file_part_empty_ret = TRUE;
7591 *is_file_part_empty_ret = FALSE;
7594 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7595 g_object_unref (current_folder);
7599 error_building_filename_dialog (impl, error);
7601 *is_well_formed_ret = FALSE;
7608 *is_well_formed_ret = TRUE;
7609 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7612 struct get_files_closure {
7613 GtkFileChooserDefault *impl;
7615 GFile *file_from_entry;
7619 get_files_foreach (GtkTreeModel *model,
7624 struct get_files_closure *info;
7626 GtkFileSystemModel *fs_model;
7629 fs_model = info->impl->browse_files_model;
7631 file = _gtk_file_system_model_get_file (fs_model, iter);
7633 return; /* We are on the editable row */
7635 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7636 info->result = g_slist_prepend (info->result, g_object_ref (file));
7640 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7642 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7643 struct get_files_closure info;
7644 GtkWindow *toplevel;
7645 GtkWidget *current_focus;
7646 gboolean file_list_seen;
7650 info.file_from_entry = NULL;
7652 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7653 return search_get_selected_files (impl);
7655 if (impl->operation_mode == OPERATION_MODE_RECENT)
7657 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7659 file_list_seen = TRUE;
7663 return recent_get_selected_files (impl);
7666 toplevel = get_toplevel (GTK_WIDGET (impl));
7668 current_focus = gtk_window_get_focus (toplevel);
7670 current_focus = NULL;
7672 file_list_seen = FALSE;
7673 if (current_focus == impl->browse_files_tree_view)
7675 GtkTreeSelection *selection;
7679 file_list_seen = TRUE;
7680 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7681 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7683 /* If there is no selection in the file list, we probably have this situation:
7685 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7686 * 2. He then double-clicked on a folder ("bar") in the file list
7688 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7689 * filename entry to see if that is the case.
7691 if (info.result == NULL && impl->location_entry)
7694 else if (impl->location_entry && current_focus == impl->location_entry)
7696 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7700 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7705 if (!is_well_formed)
7708 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7710 g_object_unref (info.file_from_entry);
7714 if (info.file_from_entry)
7715 info.result = g_slist_prepend (info.result, info.file_from_entry);
7716 else if (!file_list_seen)
7721 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7723 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7727 /* The focus is on a dialog's action area button or something else */
7728 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7729 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7737 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7738 * fall back to the current directory */
7739 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7740 info.result == NULL)
7742 GFile *current_folder;
7744 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7747 info.result = g_slist_prepend (info.result, current_folder);
7750 return g_slist_reverse (info.result);
7754 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7756 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7758 if (impl->preview_file)
7759 return g_object_ref (impl->preview_file);
7764 static GtkFileSystem *
7765 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7767 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7769 return impl->file_system;
7772 /* Shows or hides the filter widgets */
7774 show_filters (GtkFileChooserDefault *impl,
7778 gtk_widget_show (impl->filter_combo_hbox);
7780 gtk_widget_hide (impl->filter_combo_hbox);
7784 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7785 GtkFileFilter *filter)
7787 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7790 if (g_slist_find (impl->filters, filter))
7792 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7796 g_object_ref_sink (filter);
7797 impl->filters = g_slist_append (impl->filters, filter);
7799 name = gtk_file_filter_get_name (filter);
7801 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7803 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7805 if (!g_slist_find (impl->filters, impl->current_filter))
7806 set_current_filter (impl, filter);
7808 show_filters (impl, TRUE);
7812 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7813 GtkFileFilter *filter)
7815 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7816 GtkTreeModel *model;
7820 filter_index = g_slist_index (impl->filters, filter);
7822 if (filter_index < 0)
7824 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7828 impl->filters = g_slist_remove (impl->filters, filter);
7830 if (filter == impl->current_filter)
7833 set_current_filter (impl, impl->filters->data);
7835 set_current_filter (impl, NULL);
7838 /* Remove row from the combo box */
7839 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7840 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7841 g_assert_not_reached ();
7843 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7845 g_object_unref (filter);
7848 show_filters (impl, FALSE);
7852 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7854 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7856 return g_slist_copy (impl->filters);
7859 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7861 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7864 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7867 struct AddShortcutData
7869 GtkFileChooserDefault *impl;
7874 add_shortcut_get_info_cb (GCancellable *cancellable,
7876 const GError *error,
7880 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7881 struct AddShortcutData *data = user_data;
7883 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7886 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7888 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7891 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7893 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7895 /* need to call shortcuts_add_bookmarks to flush out any duplicates bug #577806 */
7896 shortcuts_add_bookmarks (data->impl);
7899 g_object_unref (data->impl);
7900 g_object_unref (data->file);
7903 g_object_unref (cancellable);
7907 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7911 GCancellable *cancellable;
7912 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7913 struct AddShortcutData *data;
7917 /* Avoid adding duplicates */
7918 pos = shortcut_find_position (impl, file);
7919 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7923 uri = g_file_get_uri (file);
7924 /* translators, "Shortcut" means "Bookmark" here */
7926 GTK_FILE_CHOOSER_ERROR,
7927 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7928 _("Shortcut %s already exists"),
7935 for (l = impl->loading_shortcuts; l; l = l->next)
7937 GCancellable *c = l->data;
7940 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7941 if (f && g_file_equal (file, f))
7945 uri = g_file_get_uri (file);
7947 GTK_FILE_CHOOSER_ERROR,
7948 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7949 _("Shortcut %s already exists"),
7957 data = g_new0 (struct AddShortcutData, 1);
7958 data->impl = g_object_ref (impl);
7959 data->file = g_object_ref (file);
7961 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7963 add_shortcut_get_info_cb, data);
7968 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7969 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7975 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7979 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7986 for (l = impl->loading_shortcuts; l; l = l->next)
7988 GCancellable *c = l->data;
7991 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7992 if (f && g_file_equal (file, f))
7994 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7995 g_cancellable_cancel (c);
8000 if (impl->num_shortcuts == 0)
8003 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8004 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8005 g_assert_not_reached ();
8007 for (i = 0; i < impl->num_shortcuts; i++)
8010 ShortcutType shortcut_type;
8013 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8014 SHORTCUTS_COL_DATA, &col_data,
8015 SHORTCUTS_COL_TYPE, &shortcut_type,
8017 g_assert (col_data != NULL);
8018 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8020 shortcut = col_data;
8021 if (g_file_equal (shortcut, file))
8023 shortcuts_remove_rows (impl, pos + i, 1);
8024 impl->num_shortcuts--;
8028 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8029 g_assert_not_reached ();
8034 uri = g_file_get_uri (file);
8035 /* translators, "Shortcut" means "Bookmark" here */
8037 GTK_FILE_CHOOSER_ERROR,
8038 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8039 _("Shortcut %s does not exist"),
8047 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8049 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8055 if (impl->num_shortcuts == 0)
8058 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8059 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8060 g_assert_not_reached ();
8064 for (i = 0; i < impl->num_shortcuts; i++)
8067 ShortcutType shortcut_type;
8070 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8071 SHORTCUTS_COL_DATA, &col_data,
8072 SHORTCUTS_COL_TYPE, &shortcut_type,
8074 g_assert (col_data != NULL);
8075 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8077 shortcut = col_data;
8078 list = g_slist_prepend (list, g_object_ref (shortcut));
8080 if (i != impl->num_shortcuts - 1)
8082 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8083 g_assert_not_reached ();
8087 return g_slist_reverse (list);
8090 /* Guesses a size based upon font sizes */
8092 find_good_size_from_style (GtkWidget *widget,
8096 GtkStyleContext *context;
8097 GtkStateFlags state;
8102 context = gtk_widget_get_style_context (widget);
8103 state = gtk_widget_get_state_flags (widget);
8105 screen = gtk_widget_get_screen (widget);
8108 resolution = gdk_screen_get_resolution (screen);
8109 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8113 resolution = 96.0; /* wheeee */
8115 gtk_style_context_get (context, state, "font-size", &font_size, NULL);
8116 font_size = font_size * resolution / 72.0 + 0.5;
8118 *width = font_size * NUM_CHARS;
8119 *height = font_size * NUM_LINES;
8123 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8124 gint *default_width,
8125 gint *default_height)
8127 GtkFileChooserDefault *impl;
8129 int x, y, width, height;
8130 GSettings *settings;
8132 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8134 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
8136 g_settings_get (settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8137 g_settings_get (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8139 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8141 *default_width = width;
8142 *default_height = height;
8146 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8148 if (impl->preview_widget_active &&
8149 impl->preview_widget &&
8150 gtk_widget_get_visible (impl->preview_widget))
8152 gtk_widget_get_preferred_size (impl->preview_box,
8154 *default_width += PREVIEW_HBOX_SPACING + req.width;
8157 if (impl->extra_widget &&
8158 gtk_widget_get_visible (impl->extra_widget))
8160 gtk_widget_get_preferred_size (impl->extra_align,
8162 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8166 struct switch_folder_closure {
8167 GtkFileChooserDefault *impl;
8172 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8174 switch_folder_foreach_cb (GtkTreeModel *model,
8179 struct switch_folder_closure *closure;
8183 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8184 closure->num_selected++;
8187 /* Changes to the selected folder in the list view */
8189 switch_to_selected_folder (GtkFileChooserDefault *impl)
8191 GtkTreeSelection *selection;
8192 struct switch_folder_closure closure;
8194 /* We do this with foreach() rather than get_selected() as we may be in
8195 * multiple selection mode
8198 closure.impl = impl;
8199 closure.file = NULL;
8200 closure.num_selected = 0;
8202 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8203 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8205 g_assert (closure.file && closure.num_selected == 1);
8207 change_folder_and_display_error (impl, closure.file, FALSE);
8210 /* Gets the GFileInfo for the selected row in the file list; assumes single
8214 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8215 gboolean *had_selection)
8217 GtkTreeSelection *selection;
8221 g_assert (!impl->select_multiple);
8222 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8223 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8225 *had_selection = FALSE;
8229 *had_selection = TRUE;
8231 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8235 /* Gets the display name of the selected file in the file list; assumes single
8236 * selection mode and that something is selected.
8238 static const gchar *
8239 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8242 gboolean had_selection;
8244 info = get_selected_file_info_from_file_list (impl, &had_selection);
8245 g_assert (had_selection);
8246 g_assert (info != NULL);
8248 return g_file_info_get_display_name (info);
8252 add_custom_button_to_dialog (GtkDialog *dialog,
8253 const gchar *mnemonic_label,
8254 const gchar *stock_id,
8259 button = gtk_button_new_with_mnemonic (mnemonic_label);
8260 gtk_widget_set_can_default (button, TRUE);
8261 gtk_button_set_image (GTK_BUTTON (button),
8262 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8263 gtk_widget_show (button);
8265 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8268 /* Presents an overwrite confirmation dialog; returns whether we should accept
8272 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8273 const gchar *file_part,
8274 const gchar *folder_display_name)
8276 GtkWindow *toplevel;
8280 toplevel = get_toplevel (GTK_WIDGET (impl));
8282 dialog = gtk_message_dialog_new (toplevel,
8283 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8284 GTK_MESSAGE_QUESTION,
8286 _("A file named \"%s\" already exists. Do you want to replace it?"),
8288 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8289 _("The file already exists in \"%s\". Replacing it will "
8290 "overwrite its contents."),
8291 folder_display_name);
8293 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8294 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8295 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8296 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8297 GTK_RESPONSE_ACCEPT,
8298 GTK_RESPONSE_CANCEL,
8300 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8302 if (gtk_window_has_group (toplevel))
8303 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8304 GTK_WINDOW (dialog));
8306 response = gtk_dialog_run (GTK_DIALOG (dialog));
8308 gtk_widget_destroy (dialog);
8310 return (response == GTK_RESPONSE_ACCEPT);
8313 struct GetDisplayNameData
8315 GtkFileChooserDefault *impl;
8319 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8320 * as requesting a response means, "the dialog is confirmed".
8323 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8325 g_signal_emit_by_name (impl, "response-requested");
8326 add_selection_to_recent_list (impl);
8330 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8332 const GError *error,
8335 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8336 gboolean should_respond = FALSE;
8337 struct GetDisplayNameData *data = user_data;
8339 if (cancellable != data->impl->should_respond_get_info_cancellable)
8342 data->impl->should_respond_get_info_cancellable = NULL;
8348 /* Huh? Did the folder disappear? Let the caller deal with it */
8349 should_respond = TRUE;
8351 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8353 set_busy_cursor (data->impl, FALSE);
8355 request_response_and_add_to_recent_list (data->impl);
8358 g_object_unref (data->impl);
8359 g_free (data->file_part);
8362 g_object_unref (cancellable);
8365 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8366 * should respond. Can get the file part from the file list or the save entry.
8369 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8370 const gchar *file_part,
8373 GtkFileChooserConfirmation conf;
8375 if (!impl->do_overwrite_confirmation)
8378 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8380 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8384 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8386 struct GetDisplayNameData *data;
8388 g_assert (file_part != NULL);
8390 data = g_new0 (struct GetDisplayNameData, 1);
8391 data->impl = g_object_ref (impl);
8392 data->file_part = g_strdup (file_part);
8394 if (impl->should_respond_get_info_cancellable)
8395 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8397 impl->should_respond_get_info_cancellable =
8398 _gtk_file_system_get_info (impl->file_system, parent_file,
8399 "standard::display-name",
8400 confirmation_confirm_get_info_cb,
8402 set_busy_cursor (data->impl, TRUE);
8406 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8409 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8413 g_assert_not_reached ();
8418 struct FileExistsData
8420 GtkFileChooserDefault *impl;
8421 gboolean file_exists_and_is_not_folder;
8427 name_entry_get_parent_info_cb (GCancellable *cancellable,
8429 const GError *error,
8432 gboolean parent_is_folder;
8433 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8434 struct FileExistsData *data = user_data;
8436 if (cancellable != data->impl->should_respond_get_info_cancellable)
8439 data->impl->should_respond_get_info_cancellable = NULL;
8441 set_busy_cursor (data->impl, FALSE);
8447 parent_is_folder = FALSE;
8449 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8451 if (parent_is_folder)
8453 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8455 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) */
8457 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8459 if (data->file_exists_and_is_not_folder)
8464 /* Dup the string because the string may be modified
8465 * depending on what clients do in the confirm-overwrite
8466 * signal and this corrupts the pointer
8468 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8469 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8473 request_response_and_add_to_recent_list (data->impl);
8476 request_response_and_add_to_recent_list (data->impl);
8478 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8479 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8481 GError *mkdir_error = NULL;
8483 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8484 * "/blah/nonexistent" you *will* want a folder created.
8487 set_busy_cursor (data->impl, TRUE);
8488 g_file_make_directory (data->file, NULL, &mkdir_error);
8489 set_busy_cursor (data->impl, FALSE);
8492 request_response_and_add_to_recent_list (data->impl);
8494 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8497 g_assert_not_reached ();
8503 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8504 error_with_file_under_nonfolder (data->impl, data->parent_file);
8510 /* The parent folder is not readable for some reason */
8512 error_copy = g_error_copy (error);
8513 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8518 g_object_unref (data->impl);
8519 g_object_unref (data->file);
8520 g_object_unref (data->parent_file);
8523 g_object_unref (cancellable);
8527 file_exists_get_info_cb (GCancellable *cancellable,
8529 const GError *error,
8532 gboolean data_ownership_taken = FALSE;
8533 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8534 gboolean file_exists;
8536 gboolean needs_parent_check = FALSE;
8537 struct FileExistsData *data = user_data;
8539 if (cancellable != data->impl->file_exists_get_info_cancellable)
8542 data->impl->file_exists_get_info_cancellable = NULL;
8544 set_busy_cursor (data->impl, FALSE);
8549 file_exists = (info != NULL);
8550 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8552 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8555 change_folder_and_display_error (data->impl, data->file, TRUE);
8559 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8561 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8564 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8566 if (file_exists && !is_folder)
8568 /* Oops, the user typed the name of an existing path which is not
8571 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8572 g_error_copy (error));
8576 needs_parent_check = TRUE;
8579 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8583 needs_parent_check = TRUE;
8589 /* User typed a folder; we are done */
8590 request_response_and_add_to_recent_list (data->impl);
8593 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8596 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8599 change_folder_and_display_error (data->impl, data->file, TRUE);
8601 needs_parent_check = TRUE;
8605 g_assert_not_reached();
8608 if (needs_parent_check) {
8609 /* check that everything up to the last path component exists (i.e. the parent) */
8611 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8612 data_ownership_taken = TRUE;
8614 if (data->impl->should_respond_get_info_cancellable)
8615 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8617 data->impl->should_respond_get_info_cancellable =
8618 _gtk_file_system_get_info (data->impl->file_system,
8621 name_entry_get_parent_info_cb,
8623 set_busy_cursor (data->impl, TRUE);
8627 if (!data_ownership_taken)
8629 g_object_unref (data->impl);
8630 g_object_unref (data->file);
8631 g_object_unref (data->parent_file);
8635 g_object_unref (cancellable);
8639 paste_text_received (GtkClipboard *clipboard,
8641 GtkFileChooserDefault *impl)
8648 file = g_file_new_for_uri (text);
8650 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8651 location_popup_handler (impl, text);
8653 g_object_unref (file);
8656 /* Handler for the "location-popup-on-paste" keybinding signal */
8658 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8660 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8661 GDK_SELECTION_CLIPBOARD);
8662 gtk_clipboard_request_text (clipboard,
8663 (GtkClipboardTextReceivedFunc) paste_text_received,
8667 /* Implementation for GtkFileChooserEmbed::should_respond() */
8669 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8674 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8676 for (l = files; l; l = l->next)
8678 GFile *file = l->data;
8681 uri = g_file_get_uri (file);
8684 gtk_recent_manager_add_item (impl->recent_manager, uri);
8689 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8690 g_slist_free (files);
8694 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8696 GtkFileChooserDefault *impl;
8697 GtkWidget *toplevel;
8698 GtkWidget *current_focus;
8701 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8703 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8704 g_assert (GTK_IS_WINDOW (toplevel));
8708 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8710 if (current_focus == impl->browse_files_tree_view)
8712 /* The following array encodes what we do based on the impl->action and the
8713 * number of files selected.
8716 NOOP, /* Do nothing (don't respond) */
8717 RESPOND, /* Respond immediately */
8718 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8719 ALL_FILES, /* Respond only if everything selected is a file */
8720 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8721 SAVE_ENTRY, /* Go to the code for handling the save entry */
8722 NOT_REACHED /* Sanity check */
8724 static const ActionToTake what_to_do[4][3] = {
8725 /* 0 selected 1 selected many selected */
8726 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8727 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8728 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8729 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8733 gboolean all_files, all_folders;
8735 ActionToTake action;
8739 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8741 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8743 retval = search_should_respond (impl);
8747 if (impl->operation_mode == OPERATION_MODE_RECENT)
8749 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8753 retval = recent_should_respond (impl);
8758 selection_check (impl, &num_selected, &all_files, &all_folders);
8760 if (num_selected > 2)
8765 action = what_to_do [impl->action] [k];
8776 case RESPOND_OR_SWITCH:
8777 g_assert (num_selected == 1);
8781 switch_to_selected_folder (impl);
8784 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8786 retval = should_respond_after_confirm_overwrite (impl,
8787 get_display_name_from_file_list (impl),
8788 impl->current_folder);
8802 retval = all_folders;
8809 g_assert_not_reached ();
8812 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8815 gboolean is_well_formed, is_empty, is_file_part_empty;
8817 GtkFileChooserEntry *entry;
8822 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8823 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8824 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8825 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8826 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8828 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8829 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8831 if (!is_well_formed)
8834 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8835 && impl->operation_mode == OPERATION_MODE_RECENT)
8837 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8839 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8840 * thus changing our assumption that no selection is present - setting
8841 * a selection causes the error message from path_bar_set_mode() to go away,
8842 * but we want the user to see that message!
8844 gtk_widget_grab_focus (impl->browse_files_tree_view);
8847 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8854 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8855 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8857 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8858 gtk_widget_grab_focus (impl->location_entry);
8865 g_assert (file != NULL);
8870 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8871 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8873 change_folder_and_display_error (impl, file, TRUE);
8875 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8876 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8878 /* The folder already exists, so we do not need to create it.
8879 * Just respond to terminate the dialog.
8885 g_assert_not_reached ();
8890 struct FileExistsData *data;
8892 /* We need to check whether file exists and whether it is a folder -
8893 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8894 * negative (it doesn't know yet if your last path component is a
8898 data = g_new0 (struct FileExistsData, 1);
8899 data->impl = g_object_ref (impl);
8900 data->file = g_object_ref (file);
8901 data->parent_file = _gtk_file_chooser_entry_get_current_folder (entry);
8903 if (impl->file_exists_get_info_cancellable)
8904 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8906 impl->file_exists_get_info_cancellable =
8907 _gtk_file_system_get_info (impl->file_system, file,
8909 file_exists_get_info_cb,
8912 set_busy_cursor (impl, TRUE);
8915 g_error_free (error);
8918 g_object_unref (file);
8920 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8922 /* The focus is on a dialog's action area button, *and* the widget that
8923 * was focused immediately before it is the file list.
8927 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8929 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8932 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8934 /* The focus is on a dialog's action area button, *and* the widget that
8935 * was focused immediately before it is the location entry.
8940 /* The focus is on a dialog's action area button or something else */
8941 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8942 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8950 add_selection_to_recent_list (impl);
8955 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8957 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8959 GtkFileChooserDefault *impl;
8962 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8964 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8965 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8967 if (impl->location_mode == LOCATION_MODE_PATH_BAR
8968 || impl->operation_mode == OPERATION_MODE_RECENT)
8969 widget = impl->browse_files_tree_view;
8971 widget = impl->location_entry;
8973 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8974 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8975 widget = impl->location_entry;
8978 g_assert_not_reached ();
8982 g_assert (widget != NULL);
8983 gtk_widget_grab_focus (widget);
8986 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8988 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8998 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8999 *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 */
9002 /* Constructs a list of the selected paths in search mode */
9004 search_get_selected_files (GtkFileChooserDefault *impl)
9007 GtkTreeSelection *selection;
9011 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9012 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9013 result = g_slist_reverse (result);
9018 /* Called from ::should_respond(). We return whether there are selected files
9019 * in the search list.
9022 search_should_respond (GtkFileChooserDefault *impl)
9024 GtkTreeSelection *selection;
9026 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9028 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9029 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9032 /* Adds one hit from the search engine to the search_model */
9034 search_add_hit (GtkFileChooserDefault *impl,
9039 file = g_file_new_for_uri (uri);
9043 if (!g_file_is_native (file))
9045 g_object_unref (file);
9049 _gtk_file_system_model_add_and_query_file (impl->search_model,
9053 g_object_unref (file);
9056 /* Callback used from GtkSearchEngine when we get new hits */
9058 search_engine_hits_added_cb (GtkSearchEngine *engine,
9062 GtkFileChooserDefault *impl;
9065 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9067 for (l = hits; l; l = l->next)
9068 search_add_hit (impl, (gchar*)l->data);
9071 /* Callback used from GtkSearchEngine when the query is done running */
9073 search_engine_finished_cb (GtkSearchEngine *engine,
9076 GtkFileChooserDefault *impl;
9078 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9081 /* EB: setting the model here will avoid loads of row events,
9082 * but it'll make the search look like blocked.
9084 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9085 GTK_TREE_MODEL (impl->search_model));
9086 file_list_set_sort_column_ids (impl);
9089 /* FMQ: if search was empty, say that we got no hits */
9090 set_busy_cursor (impl, FALSE);
9093 /* Displays a generic error when we cannot create a GtkSearchEngine.
9094 * It would be better if _gtk_search_engine_new() gave us a GError
9095 * with a better message, but it doesn't do that right now.
9098 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9100 error_message (impl,
9101 _("Could not start the search process"),
9102 _("The program was not able to create a connection to the indexer "
9103 "daemon. Please make sure it is running."));
9107 search_engine_error_cb (GtkSearchEngine *engine,
9108 const gchar *message,
9111 GtkFileChooserDefault *impl;
9113 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9115 search_stop_searching (impl, TRUE);
9116 error_message (impl, _("Could not send the search request"), message);
9118 set_busy_cursor (impl, FALSE);
9121 /* Frees the data in the search_model */
9123 search_clear_model (GtkFileChooserDefault *impl,
9124 gboolean remove_from_treeview)
9126 if (!impl->search_model)
9129 g_object_unref (impl->search_model);
9130 impl->search_model = NULL;
9132 if (remove_from_treeview)
9133 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9136 /* Stops any ongoing searches; does not touch the search_model */
9138 search_stop_searching (GtkFileChooserDefault *impl,
9139 gboolean remove_query)
9141 if (remove_query && impl->search_query)
9143 g_object_unref (impl->search_query);
9144 impl->search_query = NULL;
9147 if (impl->search_engine)
9149 _gtk_search_engine_stop (impl->search_engine);
9151 g_object_unref (impl->search_engine);
9152 impl->search_engine = NULL;
9156 /* Creates the search_model and puts it in the tree view */
9158 search_setup_model (GtkFileChooserDefault *impl)
9160 g_assert (impl->search_model == NULL);
9162 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9164 MODEL_COLUMN_TYPES);
9166 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9170 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9174 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9178 set_sort_column (impl);
9180 /* EB: setting the model here will make the hits list update feel
9181 * more "alive" than setting the model at the end of the search
9184 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9185 GTK_TREE_MODEL (impl->search_model));
9186 file_list_set_sort_column_ids (impl);
9189 /* Creates a new query with the specified text and launches it */
9191 search_start_query (GtkFileChooserDefault *impl,
9192 const gchar *query_text)
9194 search_stop_searching (impl, FALSE);
9195 search_clear_model (impl, TRUE);
9196 search_setup_model (impl);
9197 set_busy_cursor (impl, TRUE);
9199 if (impl->search_engine == NULL)
9200 impl->search_engine = _gtk_search_engine_new ();
9202 if (!impl->search_engine)
9204 set_busy_cursor (impl, FALSE);
9205 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9209 if (!impl->search_query)
9211 impl->search_query = _gtk_query_new ();
9212 _gtk_query_set_text (impl->search_query, query_text);
9215 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9217 g_signal_connect (impl->search_engine, "hits-added",
9218 G_CALLBACK (search_engine_hits_added_cb), impl);
9219 g_signal_connect (impl->search_engine, "finished",
9220 G_CALLBACK (search_engine_finished_cb), impl);
9221 g_signal_connect (impl->search_engine, "error",
9222 G_CALLBACK (search_engine_error_cb), impl);
9224 _gtk_search_engine_start (impl->search_engine);
9227 /* Callback used when the user presses Enter while typing on the search
9228 * entry; starts the query
9231 search_entry_activate_cb (GtkEntry *entry,
9234 GtkFileChooserDefault *impl;
9237 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9239 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9240 if (strlen (text) == 0)
9243 /* reset any existing query object */
9244 if (impl->search_query)
9246 g_object_unref (impl->search_query);
9247 impl->search_query = NULL;
9250 search_start_query (impl, text);
9254 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9256 gdk_threads_enter ();
9258 g_source_destroy (impl->focus_entry_idle);
9259 impl->focus_entry_idle = NULL;
9261 if (impl->search_entry)
9262 gtk_widget_grab_focus (impl->search_entry);
9264 gdk_threads_leave ();
9270 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9272 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9273 * pane, we get a selection-changed signal and we set up the search widgets.
9274 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9275 * the change to the selection. So, we need to re-focus the search entry
9276 * after the treeview has finished doing its work; we'll do that in an idle
9280 if (!impl->focus_entry_idle)
9281 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9284 /* Hides the path bar and creates the search entry */
9286 search_setup_widgets (GtkFileChooserDefault *impl)
9288 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9290 path_bar_update (impl);
9292 impl->search_entry = gtk_entry_new ();
9293 g_signal_connect (impl->search_entry, "activate",
9294 G_CALLBACK (search_entry_activate_cb),
9296 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9298 /* if there already is a query, restart it */
9299 if (impl->search_query)
9301 gchar *query = _gtk_query_get_text (impl->search_query);
9305 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9306 search_start_query (impl, query);
9312 g_object_unref (impl->search_query);
9313 impl->search_query = NULL;
9317 /* Box for search widgets */
9318 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9319 gtk_widget_show_all (impl->search_hbox);
9320 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9322 /* Hide the location widgets temporarily */
9324 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9325 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9327 gtk_widget_hide (impl->location_button);
9328 gtk_widget_hide (impl->location_entry_box);
9331 focus_search_entry_in_idle (impl);
9333 /* FMQ: hide the filter combo? */
9337 * Recent files support
9340 /* Frees the data in the recent_model */
9342 recent_clear_model (GtkFileChooserDefault *impl,
9343 gboolean remove_from_treeview)
9345 if (!impl->recent_model)
9348 if (remove_from_treeview)
9349 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9351 g_object_unref (impl->recent_model);
9352 impl->recent_model = NULL;
9355 /* Stops any ongoing loading of the recent files list; does
9356 * not touch the recent_model
9359 recent_stop_loading (GtkFileChooserDefault *impl)
9361 if (impl->load_recent_id)
9363 g_source_remove (impl->load_recent_id);
9364 impl->load_recent_id = 0;
9369 recent_setup_model (GtkFileChooserDefault *impl)
9371 g_assert (impl->recent_model == NULL);
9373 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9375 MODEL_COLUMN_TYPES);
9377 _gtk_file_system_model_set_filter (impl->recent_model,
9378 impl->current_filter);
9379 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9383 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9387 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9391 set_sort_column (impl);
9396 GtkFileChooserDefault *impl;
9401 recent_idle_cleanup (gpointer data)
9403 RecentLoadData *load_data = data;
9404 GtkFileChooserDefault *impl = load_data->impl;
9406 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9407 GTK_TREE_MODEL (impl->recent_model));
9408 file_list_set_sort_column_ids (impl);
9409 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9411 set_busy_cursor (impl, FALSE);
9413 impl->load_recent_id = 0;
9419 get_recent_files_limit (GtkWidget *widget)
9421 GtkSettings *settings;
9424 if (gtk_widget_has_screen (widget))
9425 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9427 settings = gtk_settings_get_default ();
9429 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9434 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9436 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9442 limit = get_recent_files_limit (GTK_WIDGET (impl));
9446 for (l = items; l; l = l->next)
9448 GtkRecentInfo *info = l->data;
9451 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9452 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9455 g_object_unref (file);
9458 if (limit != -1 && n >= limit)
9464 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9469 folders = _gtk_file_chooser_extract_recent_folders (items);
9471 for (l = folders; l; l = l->next)
9473 GFile *folder = l->data;
9475 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9480 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9481 g_list_free (folders);
9485 recent_idle_load (gpointer data)
9487 RecentLoadData *load_data = data;
9488 GtkFileChooserDefault *impl = load_data->impl;
9490 if (!impl->recent_manager)
9493 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9494 if (!load_data->items)
9497 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9498 populate_model_with_recent_items (impl, load_data->items);
9500 populate_model_with_folders (impl, load_data->items);
9502 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9503 g_list_free (load_data->items);
9504 load_data->items = NULL;
9510 recent_start_loading (GtkFileChooserDefault *impl)
9512 RecentLoadData *load_data;
9514 recent_stop_loading (impl);
9515 recent_clear_model (impl, TRUE);
9516 recent_setup_model (impl);
9517 set_busy_cursor (impl, TRUE);
9519 g_assert (impl->load_recent_id == 0);
9521 load_data = g_new (RecentLoadData, 1);
9522 load_data->impl = impl;
9523 load_data->items = NULL;
9525 /* begin lazy loading the recent files into the model */
9526 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9529 recent_idle_cleanup);
9533 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9543 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9544 *list = g_slist_prepend (*list, file);
9547 /* Constructs a list of the selected paths in recent files mode */
9549 recent_get_selected_files (GtkFileChooserDefault *impl)
9552 GtkTreeSelection *selection;
9556 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9557 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9558 result = g_slist_reverse (result);
9563 /* Called from ::should_respond(). We return whether there are selected
9564 * files in the recent files list.
9567 recent_should_respond (GtkFileChooserDefault *impl)
9569 GtkTreeSelection *selection;
9571 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9573 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9574 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9578 set_current_filter (GtkFileChooserDefault *impl,
9579 GtkFileFilter *filter)
9581 if (impl->current_filter != filter)
9585 /* NULL filters are allowed to reset to non-filtered status
9587 filter_index = g_slist_index (impl->filters, filter);
9588 if (impl->filters && filter && filter_index < 0)
9591 if (impl->current_filter)
9592 g_object_unref (impl->current_filter);
9593 impl->current_filter = filter;
9594 if (impl->current_filter)
9596 g_object_ref_sink (impl->current_filter);
9600 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9603 if (impl->browse_files_model)
9605 _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
9606 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_IS_SENSITIVE);
9609 if (impl->search_model)
9611 _gtk_file_system_model_set_filter (impl->search_model, filter);
9612 _gtk_file_system_model_clear_cache (impl->search_model, MODEL_COL_IS_SENSITIVE);
9615 if (impl->recent_model)
9617 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9618 _gtk_file_system_model_clear_cache (impl->recent_model, MODEL_COL_IS_SENSITIVE);
9621 g_object_notify (G_OBJECT (impl), "filter");
9626 filter_combo_changed (GtkComboBox *combo_box,
9627 GtkFileChooserDefault *impl)
9629 gint new_index = gtk_combo_box_get_active (combo_box);
9630 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9632 set_current_filter (impl, new_filter);
9636 check_preview_change (GtkFileChooserDefault *impl)
9638 GtkTreePath *cursor_path;
9640 char *new_display_name;
9641 GtkTreeModel *model;
9643 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9644 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9649 gtk_tree_model_get_iter (model, &iter, cursor_path);
9650 gtk_tree_model_get (model, &iter,
9651 MODEL_COL_FILE, &new_file,
9652 MODEL_COL_NAME, &new_display_name,
9655 gtk_tree_path_free (cursor_path);
9660 new_display_name = NULL;
9663 if (new_file != impl->preview_file &&
9664 !(new_file && impl->preview_file &&
9665 g_file_equal (new_file, impl->preview_file)))
9667 if (impl->preview_file)
9669 g_object_unref (impl->preview_file);
9670 g_free (impl->preview_display_name);
9675 impl->preview_file = new_file;
9676 impl->preview_display_name = new_display_name;
9680 impl->preview_file = NULL;
9681 impl->preview_display_name = NULL;
9682 g_free (new_display_name);
9685 if (impl->use_preview_label && impl->preview_label)
9686 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9688 g_signal_emit_by_name (impl, "update-preview");
9693 g_object_unref (new_file);
9695 g_free (new_display_name);
9700 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9701 GtkFileSystemVolume *volume,
9702 const GError *error,
9706 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9707 GtkFileChooserDefault *impl = data;
9709 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9712 impl->shortcuts_activate_iter_cancellable = NULL;
9714 set_busy_cursor (impl, FALSE);
9721 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9725 name = _gtk_file_system_volume_get_display_name (volume);
9726 msg = g_strdup_printf (_("Could not mount %s"), name);
9728 error_message (impl, msg, error->message);
9737 file = _gtk_file_system_volume_get_root (volume);
9740 change_folder_and_display_error (impl, file, FALSE);
9741 g_object_unref (file);
9745 g_object_unref (impl);
9746 g_object_unref (cancellable);
9750 /* Activates a volume by mounting it if necessary and then switching to its
9754 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9755 GtkFileSystemVolume *volume)
9759 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9761 /* We ref the file chooser since volume_mount() may run a main loop, and the
9762 * user could close the file chooser window in the meantime.
9764 g_object_ref (impl);
9766 if (!_gtk_file_system_volume_is_mounted (volume))
9768 GMountOperation *mount_op;
9770 set_busy_cursor (impl, TRUE);
9772 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9773 impl->shortcuts_activate_iter_cancellable =
9774 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9775 shortcuts_activate_volume_mount_cb,
9776 g_object_ref (impl));
9777 g_object_unref (mount_op);
9781 file = _gtk_file_system_volume_get_root (volume);
9784 change_folder_and_display_error (impl, file, FALSE);
9785 g_object_unref (file);
9789 g_object_unref (impl);
9792 /* Opens the folder or volume at the specified iter in the shortcuts model */
9793 struct ShortcutsActivateData
9795 GtkFileChooserDefault *impl;
9800 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9802 const GError *error,
9805 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9806 struct ShortcutsActivateData *data = user_data;
9808 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9811 data->impl->shortcuts_activate_iter_cancellable = NULL;
9816 if (!error && _gtk_file_info_consider_as_directory (info))
9817 change_folder_and_display_error (data->impl, data->file, FALSE);
9819 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9824 g_object_unref (data->impl);
9825 g_object_unref (data->file);
9828 g_object_unref (cancellable);
9832 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9833 GtkFileSystemVolume *volume,
9834 const GError *error,
9837 struct ShortcutsActivateData *data = user_data;
9841 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9843 g_object_unref (data->impl);
9844 g_object_unref (data->file);
9850 data->impl->shortcuts_activate_iter_cancellable =
9851 _gtk_file_system_get_info (data->impl->file_system, data->file,
9853 shortcuts_activate_get_info_cb, data);
9856 _gtk_file_system_volume_unref (volume);
9860 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9864 ShortcutType shortcut_type;
9866 /* In the Save modes, we want to preserve what the uesr typed in the filename
9867 * entry, so that he may choose another folder without erasing his typed name.
9869 if (impl->location_entry
9870 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9871 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9872 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
9874 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9875 SHORTCUTS_COL_DATA, &col_data,
9876 SHORTCUTS_COL_TYPE, &shortcut_type,
9879 if (impl->shortcuts_activate_iter_cancellable)
9881 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9882 impl->shortcuts_activate_iter_cancellable = NULL;
9885 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9887 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9889 GtkFileSystemVolume *volume;
9893 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9895 shortcuts_activate_volume (impl, volume);
9897 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9899 struct ShortcutsActivateData *data;
9900 GtkFileSystemVolume *volume;
9902 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9904 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9906 data = g_new0 (struct ShortcutsActivateData, 1);
9907 data->impl = g_object_ref (impl);
9908 data->file = g_object_ref (col_data);
9910 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9912 GMountOperation *mount_operation;
9913 GtkWidget *toplevel;
9915 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9917 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9919 impl->shortcuts_activate_iter_cancellable =
9920 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9922 shortcuts_activate_mount_enclosing_volume,
9927 impl->shortcuts_activate_iter_cancellable =
9928 _gtk_file_system_get_info (impl->file_system, data->file,
9930 shortcuts_activate_get_info_cb, data);
9933 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9935 operation_mode_set (impl, OPERATION_MODE_SEARCH);
9937 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9939 operation_mode_set (impl, OPERATION_MODE_RECENT);
9943 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9945 shortcuts_key_press_event_cb (GtkWidget *widget,
9947 GtkFileChooserDefault *impl)
9951 modifiers = gtk_accelerator_get_default_mod_mask ();
9953 if (key_is_left_or_right (event))
9955 gtk_widget_grab_focus (impl->browse_files_tree_view);
9959 if ((event->keyval == GDK_KEY_BackSpace
9960 || event->keyval == GDK_KEY_Delete
9961 || event->keyval == GDK_KEY_KP_Delete)
9962 && (event->state & modifiers) == 0)
9964 remove_selected_bookmarks (impl);
9968 if ((event->keyval == GDK_KEY_F2)
9969 && (event->state & modifiers) == 0)
9971 rename_selected_bookmark (impl);
9979 shortcuts_select_func (GtkTreeSelection *selection,
9980 GtkTreeModel *model,
9982 gboolean path_currently_selected,
9985 GtkFileChooserDefault *impl = data;
9986 GtkTreeIter filter_iter;
9987 ShortcutType shortcut_type;
9989 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9990 g_assert_not_reached ();
9992 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9994 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9998 list_select_func (GtkTreeSelection *selection,
9999 GtkTreeModel *model,
10001 gboolean path_currently_selected,
10004 GtkFileChooserDefault *impl = data;
10006 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10007 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10010 gboolean is_sensitive;
10011 gboolean is_folder;
10013 if (!gtk_tree_model_get_iter (model, &iter, path))
10015 gtk_tree_model_get (model, &iter,
10016 MODEL_COL_IS_SENSITIVE, &is_sensitive,
10017 MODEL_COL_IS_FOLDER, &is_folder,
10019 if (!is_sensitive || !is_folder)
10027 list_selection_changed (GtkTreeSelection *selection,
10028 GtkFileChooserDefault *impl)
10030 /* See if we are in the new folder editable row for Save mode */
10031 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10032 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10035 gboolean had_selection;
10037 info = get_selected_file_info_from_file_list (impl, &had_selection);
10038 if (!had_selection)
10039 goto out; /* normal processing */
10042 return; /* We are on the editable row for New Folder */
10047 if (impl->location_entry)
10048 update_chooser_entry (impl);
10050 path_bar_update (impl);
10052 check_preview_change (impl);
10053 bookmarks_check_add_sensitivity (impl);
10054 check_copy_file_location_sensitivity (impl);
10056 g_signal_emit_by_name (impl, "selection-changed", 0);
10059 /* Callback used when a row in the file list is activated */
10061 list_row_activated (GtkTreeView *tree_view,
10063 GtkTreeViewColumn *column,
10064 GtkFileChooserDefault *impl)
10068 GtkTreeModel *model;
10069 gboolean is_folder;
10070 gboolean is_sensitive;
10072 model = gtk_tree_view_get_model (tree_view);
10074 if (!gtk_tree_model_get_iter (model, &iter, path))
10077 gtk_tree_model_get (model, &iter,
10078 MODEL_COL_FILE, &file,
10079 MODEL_COL_IS_FOLDER, &is_folder,
10080 MODEL_COL_IS_SENSITIVE, &is_sensitive,
10083 if (is_sensitive && is_folder && file)
10085 change_folder_and_display_error (impl, file, FALSE);
10089 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10090 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10091 g_signal_emit_by_name (impl, "file-activated");
10096 g_object_unref (file);
10100 path_bar_clicked (GtkPathBar *path_bar,
10103 gboolean child_is_hidden,
10104 GtkFileChooserDefault *impl)
10107 pending_select_files_add (impl, child_file);
10109 if (!change_folder_and_display_error (impl, file, FALSE))
10112 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10113 * show hidden files so that ".baz" appears in the file list, as it will still
10114 * be shown in the path bar: "/foo/[bar]/.baz"
10116 if (child_is_hidden)
10117 g_object_set (impl, "show-hidden", TRUE, NULL);
10121 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10123 GtkTreeViewColumn *column;
10124 GtkCellRenderer *renderer;
10125 GList *walk, *list;
10127 /* Keep the following column numbers in sync with create_file_list() */
10130 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10131 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10132 for (walk = list; walk; walk = walk->next)
10134 renderer = walk->data;
10135 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10137 gtk_tree_view_column_set_attributes (column, renderer,
10138 "pixbuf", MODEL_COL_PIXBUF,
10143 gtk_tree_view_column_set_attributes (column, renderer,
10144 "text", MODEL_COL_NAME,
10145 "ellipsize", MODEL_COL_ELLIPSIZE,
10149 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10151 g_list_free (list);
10154 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10155 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10156 renderer = list->data;
10157 gtk_tree_view_column_set_attributes (column, renderer,
10158 "text", MODEL_COL_SIZE_TEXT,
10161 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), 2);
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_MTIME_TEXT,
10171 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10172 g_list_free (list);
10176 _gtk_file_chooser_default_new (void)
10178 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10182 location_set_user_text (GtkFileChooserDefault *impl,
10185 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), path);
10186 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10190 location_popup_handler (GtkFileChooserDefault *impl,
10193 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10195 GtkWidget *widget_to_focus;
10197 operation_mode_set (impl, OPERATION_MODE_BROWSE);
10199 if (impl->current_folder)
10200 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10202 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10203 widget_to_focus = impl->browse_files_tree_view;
10205 widget_to_focus = impl->location_entry;
10207 gtk_widget_grab_focus (widget_to_focus);
10211 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10212 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10217 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10218 location_set_user_text (impl, path);
10220 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10221 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10223 gtk_widget_grab_focus (impl->location_entry);
10225 location_set_user_text (impl, path);
10228 g_assert_not_reached ();
10231 /* Handler for the "up-folder" keybinding signal */
10233 up_folder_handler (GtkFileChooserDefault *impl)
10235 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10238 /* Handler for the "down-folder" keybinding signal */
10240 down_folder_handler (GtkFileChooserDefault *impl)
10242 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10245 /* Switches to the shortcut in the specified index */
10247 switch_to_shortcut (GtkFileChooserDefault *impl,
10252 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10253 g_assert_not_reached ();
10255 shortcuts_activate_iter (impl, &iter);
10258 /* Handler for the "home-folder" keybinding signal */
10260 home_folder_handler (GtkFileChooserDefault *impl)
10262 if (impl->has_home)
10263 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10266 /* Handler for the "desktop-folder" keybinding signal */
10268 desktop_folder_handler (GtkFileChooserDefault *impl)
10270 if (impl->has_desktop)
10271 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10274 /* Handler for the "search-shortcut" keybinding signal */
10276 search_shortcut_handler (GtkFileChooserDefault *impl)
10278 if (impl->has_search)
10280 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10282 /* we want the entry widget to grab the focus the first
10283 * time, not the browse_files_tree_view widget.
10285 if (impl->search_entry)
10286 gtk_widget_grab_focus (impl->search_entry);
10290 /* Handler for the "recent-shortcut" keybinding signal */
10292 recent_shortcut_handler (GtkFileChooserDefault *impl)
10294 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10298 quick_bookmark_handler (GtkFileChooserDefault *impl,
10299 gint bookmark_index)
10304 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10307 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10309 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10310 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10313 gtk_tree_path_free (path);
10315 switch_to_shortcut (impl, bookmark_pos);
10319 show_hidden_handler (GtkFileChooserDefault *impl)
10321 g_object_set (impl,
10322 "show-hidden", !impl->show_hidden,
10327 /* Drag and drop interfaces */
10330 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10335 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10337 model->impl = NULL;
10340 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10342 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10345 ShortcutsPaneModelFilter *model;
10349 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10351 pos = *gtk_tree_path_get_indices (path);
10352 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10354 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10357 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10361 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10363 GtkSelectionData *selection_data)
10370 /* Fill the GtkTreeDragSourceIface vtable */
10372 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10374 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10375 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10379 /* Fill the GtkTreeDragDestIface vtable */
10381 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10383 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10384 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10388 static GtkTreeModel *
10389 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10390 GtkTreeModel *child_model,
10393 ShortcutsPaneModelFilter *model;
10395 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10396 "child-model", child_model,
10397 "virtual-root", root,
10400 model->impl = impl;
10402 return GTK_TREE_MODEL (model);