1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtkfilechooserdefault.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcomboboxtext.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdialog.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechooserutils.h"
40 #include "gtkfilechooser.h"
41 #include "gtkfilesystem.h"
42 #include "gtkfilesystemmodel.h"
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
48 #include "gtkimagemenuitem.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmessagedialog.h"
52 #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 "gtksizegroup.h"
61 #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>
89 #undef PROFILE_FILE_CHOOSER
90 #ifdef PROFILE_FILE_CHOOSER
97 #define PROFILE_INDENT 4
98 static int profile_indent;
101 profile_add_indent (int indent)
103 profile_indent += indent;
104 if (profile_indent < 0)
105 g_error ("You screwed up your indentation");
109 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
114 profile_add_indent (indent);
116 if (profile_indent == 0)
117 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
119 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 profile_add_indent (indent);
128 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
129 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
130 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
132 #define profile_start(x, y)
133 #define profile_end(x, y)
134 #define profile_msg(x, y)
139 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
141 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
142 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
143 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define MAX_LOADING_TIME 500
147 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
149 struct _GtkFileChooserDefaultClass
151 GtkBoxClass parent_class;
157 LOCATION_POPUP_ON_PASTE,
163 LOCATION_TOGGLE_POPUP,
171 static guint signals[LAST_SIGNAL] = { 0 };
173 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
175 SHORTCUTS_COL_PIXBUF,
179 SHORTCUTS_COL_REMOVABLE,
180 SHORTCUTS_COL_PIXBUF_VISIBLE,
181 SHORTCUTS_COL_CANCELLABLE,
182 SHORTCUTS_COL_NUM_COLUMNS
187 SHORTCUT_TYPE_VOLUME,
188 SHORTCUT_TYPE_SEPARATOR,
189 SHORTCUT_TYPE_SEARCH,
193 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
194 "standard::is-hidden,standard::is-backup,standard::size," \
195 "standard::content-type,time::modified"
197 /* the first 3 must be these due to settings caching sort column */
202 MODEL_COL_NAME_COLLATED,
206 MODEL_COL_MTIME_TEXT,
208 MODEL_COL_NUM_COLUMNS
211 /* This list of types is passed to _gtk_file_system_model_new*() */
212 #define MODEL_COLUMN_TYPES \
213 MODEL_COL_NUM_COLUMNS, \
214 G_TYPE_STRING, /* MODEL_COL_NAME */ \
215 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
216 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
217 G_TYPE_FILE, /* MODEL_COL_FILE */ \
218 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
219 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
220 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
221 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
222 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
223 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
225 /* Identifiers for target types */
230 /* Interesting places in the shortcuts bar */
234 SHORTCUTS_RECENT_SEPARATOR,
239 SHORTCUTS_BOOKMARKS_SEPARATOR,
241 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
242 SHORTCUTS_CURRENT_FOLDER
245 /* Icon size for if we can't get it from the theme */
246 #define FALLBACK_ICON_SIZE 16
248 #define PREVIEW_HBOX_SPACING 12
252 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
253 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
254 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
255 #define SETTINGS_KEY_EXPAND_FOLDERS "expand-folders"
256 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
257 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
258 #define SETTINGS_KEY_SORT_ORDER "sort-order"
259 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
260 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
262 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
263 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
265 static GObject* gtk_file_chooser_default_constructor (GType type,
266 guint n_construct_properties,
267 GObjectConstructParam *construct_params);
268 static void gtk_file_chooser_default_finalize (GObject *object);
269 static void gtk_file_chooser_default_set_property (GObject *object,
273 static void gtk_file_chooser_default_get_property (GObject *object,
277 static void gtk_file_chooser_default_dispose (GObject *object);
278 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
279 static void gtk_file_chooser_default_realize (GtkWidget *widget);
280 static void gtk_file_chooser_default_map (GtkWidget *widget);
281 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
282 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
283 GtkWidget *previous_toplevel);
284 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
285 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
286 GdkScreen *previous_screen);
287 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
296 gboolean clear_entry,
298 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
299 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
301 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
308 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
309 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
310 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
316 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
322 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
324 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
326 gint *default_height);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl,
332 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
333 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
334 static void up_folder_handler (GtkFileChooserDefault *impl);
335 static void down_folder_handler (GtkFileChooserDefault *impl);
336 static void home_folder_handler (GtkFileChooserDefault *impl);
337 static void desktop_folder_handler (GtkFileChooserDefault *impl);
338 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
339 gint bookmark_index);
340 static void show_hidden_handler (GtkFileChooserDefault *impl);
341 static void search_shortcut_handler (GtkFileChooserDefault *impl);
342 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
343 static void update_appearance (GtkFileChooserDefault *impl);
345 static void set_current_filter (GtkFileChooserDefault *impl,
346 GtkFileFilter *filter);
347 static void check_preview_change (GtkFileChooserDefault *impl);
349 static void filter_combo_changed (GtkComboBox *combo_box,
350 GtkFileChooserDefault *impl);
352 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
354 GtkFileChooserDefault *impl);
356 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
359 gboolean path_currently_selected,
361 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
363 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
365 static int shortcuts_get_index (GtkFileChooserDefault *impl,
366 ShortcutsIndex where);
367 static int shortcut_find_position (GtkFileChooserDefault *impl,
370 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
372 static gboolean list_select_func (GtkTreeSelection *selection,
375 gboolean path_currently_selected,
378 static void list_selection_changed (GtkTreeSelection *tree_selection,
379 GtkFileChooserDefault *impl);
380 static void list_row_activated (GtkTreeView *tree_view,
382 GtkTreeViewColumn *column,
383 GtkFileChooserDefault *impl);
385 static void path_bar_clicked (GtkPathBar *path_bar,
388 gboolean child_is_hidden,
389 GtkFileChooserDefault *impl);
391 static void add_bookmark_button_clicked_cb (GtkButton *button,
392 GtkFileChooserDefault *impl);
393 static void remove_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
395 static void save_folder_combo_changed_cb (GtkComboBox *combo,
396 GtkFileChooserDefault *impl);
398 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
400 static void load_remove_timer (GtkFileChooserDefault *impl);
401 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
403 static void location_button_toggled_cb (GtkToggleButton *toggle,
404 GtkFileChooserDefault *impl);
405 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
407 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
408 gboolean remove_from_treeview);
409 static void search_stop_searching (GtkFileChooserDefault *impl,
410 gboolean remove_query);
411 static void search_clear_model (GtkFileChooserDefault *impl,
412 gboolean remove_from_treeview);
413 static gboolean search_should_respond (GtkFileChooserDefault *impl);
414 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
415 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
416 static void search_entry_activate_cb (GtkEntry *entry,
418 static void settings_load (GtkFileChooserDefault *impl);
420 static void recent_stop_loading (GtkFileChooserDefault *impl);
421 static void recent_clear_model (GtkFileChooserDefault *impl,
422 gboolean remove_from_treeview);
423 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
424 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
425 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
426 static void set_file_system_backend (GtkFileChooserDefault *impl);
427 static void unset_file_system_backend (GtkFileChooserDefault *impl);
433 /* Drag and drop interface declarations */
436 GtkTreeModelFilter parent;
438 GtkFileChooserDefault *impl;
439 } ShortcutsPaneModelFilter;
442 GtkTreeModelFilterClass parent_class;
443 } ShortcutsPaneModelFilterClass;
445 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
446 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
448 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
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",
636 gtk_binding_entry_add_signal (binding_set,
637 GDK_KEY_BackSpace, 0,
641 add_normal_and_shifted_binding (binding_set,
642 GDK_KEY_Up, GDK_MOD1_MASK,
645 add_normal_and_shifted_binding (binding_set,
646 GDK_KEY_KP_Up, GDK_MOD1_MASK,
649 add_normal_and_shifted_binding (binding_set,
650 GDK_KEY_Down, GDK_MOD1_MASK,
652 add_normal_and_shifted_binding (binding_set,
653 GDK_KEY_KP_Down, GDK_MOD1_MASK,
656 gtk_binding_entry_add_signal (binding_set,
657 GDK_KEY_Home, GDK_MOD1_MASK,
660 gtk_binding_entry_add_signal (binding_set,
661 GDK_KEY_KP_Home, GDK_MOD1_MASK,
664 gtk_binding_entry_add_signal (binding_set,
665 GDK_KEY_d, GDK_MOD1_MASK,
668 gtk_binding_entry_add_signal (binding_set,
669 GDK_KEY_h, GDK_CONTROL_MASK,
672 gtk_binding_entry_add_signal (binding_set,
673 GDK_KEY_s, GDK_MOD1_MASK,
676 gtk_binding_entry_add_signal (binding_set,
677 GDK_KEY_r, GDK_MOD1_MASK,
681 for (i = 0; i < 10; i++)
682 gtk_binding_entry_add_signal (binding_set,
683 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
687 _gtk_file_chooser_install_properties (gobject_class);
691 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
693 iface->select_file = gtk_file_chooser_default_select_file;
694 iface->unselect_file = gtk_file_chooser_default_unselect_file;
695 iface->select_all = gtk_file_chooser_default_select_all;
696 iface->unselect_all = gtk_file_chooser_default_unselect_all;
697 iface->get_files = gtk_file_chooser_default_get_files;
698 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
699 iface->get_file_system = gtk_file_chooser_default_get_file_system;
700 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
701 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
702 iface->set_current_name = gtk_file_chooser_default_set_current_name;
703 iface->add_filter = gtk_file_chooser_default_add_filter;
704 iface->remove_filter = gtk_file_chooser_default_remove_filter;
705 iface->list_filters = gtk_file_chooser_default_list_filters;
706 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
707 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
708 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
712 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
714 iface->get_default_size = gtk_file_chooser_default_get_default_size;
715 iface->should_respond = gtk_file_chooser_default_should_respond;
716 iface->initial_focus = gtk_file_chooser_default_initial_focus;
720 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
722 profile_start ("start", NULL);
723 #ifdef PROFILE_FILE_CHOOSER
724 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
726 impl->local_only = TRUE;
727 impl->preview_widget_active = TRUE;
728 impl->use_preview_label = TRUE;
729 impl->select_multiple = FALSE;
730 impl->show_hidden = FALSE;
731 impl->show_size_column = TRUE;
732 impl->icon_size = FALLBACK_ICON_SIZE;
733 impl->load_state = LOAD_EMPTY;
734 impl->reload_state = RELOAD_EMPTY;
735 impl->pending_select_files = NULL;
736 impl->location_mode = LOCATION_MODE_PATH_BAR;
737 impl->operation_mode = OPERATION_MODE_BROWSE;
738 impl->sort_column = MODEL_COL_NAME;
739 impl->sort_order = GTK_SORT_ASCENDING;
740 impl->recent_manager = gtk_recent_manager_get_default ();
741 impl->create_folders = TRUE;
743 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
744 GTK_ORIENTATION_VERTICAL);
745 gtk_box_set_spacing (GTK_BOX (impl), 12);
747 set_file_system_backend (impl);
749 profile_end ("end", NULL);
752 /* Frees the data columns for the specified iter in the shortcuts model*/
754 shortcuts_free_row_data (GtkFileChooserDefault *impl,
758 ShortcutType shortcut_type;
759 GCancellable *cancellable;
761 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
762 SHORTCUTS_COL_DATA, &col_data,
763 SHORTCUTS_COL_TYPE, &shortcut_type,
764 SHORTCUTS_COL_CANCELLABLE, &cancellable,
769 g_cancellable_cancel (cancellable);
770 g_object_unref (cancellable);
773 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
774 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
778 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
780 GtkFileSystemVolume *volume;
783 _gtk_file_system_volume_unref (volume);
785 if (shortcut_type == SHORTCUT_TYPE_FILE)
790 g_object_unref (file);
794 /* Frees all the data columns in the shortcuts model */
796 shortcuts_free (GtkFileChooserDefault *impl)
800 if (!impl->shortcuts_model)
803 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
806 shortcuts_free_row_data (impl, &iter);
808 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
810 g_object_unref (impl->shortcuts_model);
811 impl->shortcuts_model = NULL;
815 pending_select_files_free (GtkFileChooserDefault *impl)
817 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
818 g_slist_free (impl->pending_select_files);
819 impl->pending_select_files = NULL;
823 pending_select_files_add (GtkFileChooserDefault *impl,
826 impl->pending_select_files =
827 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
831 gtk_file_chooser_default_finalize (GObject *object)
833 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
836 unset_file_system_backend (impl);
838 if (impl->shortcuts_pane_filter_model)
839 g_object_unref (impl->shortcuts_pane_filter_model);
841 if (impl->shortcuts_combo_filter_model)
842 g_object_unref (impl->shortcuts_combo_filter_model);
844 shortcuts_free (impl);
846 g_free (impl->browse_files_last_selected_name);
848 for (l = impl->filters; l; l = l->next)
850 GtkFileFilter *filter;
852 filter = GTK_FILE_FILTER (l->data);
853 g_object_unref (filter);
855 g_slist_free (impl->filters);
857 if (impl->current_filter)
858 g_object_unref (impl->current_filter);
860 if (impl->current_volume_file)
861 g_object_unref (impl->current_volume_file);
863 if (impl->current_folder)
864 g_object_unref (impl->current_folder);
866 if (impl->preview_file)
867 g_object_unref (impl->preview_file);
869 if (impl->browse_path_bar_size_group)
870 g_object_unref (impl->browse_path_bar_size_group);
872 /* Free all the Models we have */
873 stop_loading_and_clear_list_model (impl, FALSE);
874 search_clear_model (impl, FALSE);
875 recent_clear_model (impl, FALSE);
877 /* stopping the load above should have cleared this */
878 g_assert (impl->load_timeout_id == 0);
880 g_free (impl->preview_display_name);
882 g_free (impl->edited_new_text);
884 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
887 /* Shows an error dialog set as transient for the specified window */
889 error_message_with_parent (GtkWindow *parent,
895 dialog = gtk_message_dialog_new (parent,
896 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
901 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
904 if (parent && gtk_window_has_group (parent))
905 gtk_window_group_add_window (gtk_window_get_group (parent),
906 GTK_WINDOW (dialog));
908 gtk_dialog_run (GTK_DIALOG (dialog));
909 gtk_widget_destroy (dialog);
912 /* Returns a toplevel GtkWindow, or NULL if none */
914 get_toplevel (GtkWidget *widget)
918 toplevel = gtk_widget_get_toplevel (widget);
919 if (!gtk_widget_is_toplevel (toplevel))
922 return GTK_WINDOW (toplevel);
925 /* Shows an error dialog for the file chooser */
927 error_message (GtkFileChooserDefault *impl,
931 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
934 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
936 error_dialog (GtkFileChooserDefault *impl,
947 uri = g_file_get_uri (file);
948 text = g_strdup_printf (msg, uri);
949 error_message (impl, text, error->message);
952 g_error_free (error);
956 /* Displays an error message about not being able to get information for a file.
957 * Frees the GError as well.
960 error_getting_info_dialog (GtkFileChooserDefault *impl,
965 _("Could not retrieve information about the file"),
969 /* Shows an error dialog about not being able to add a bookmark */
971 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
976 _("Could not add a bookmark"),
980 /* Shows an error dialog about not being able to remove a bookmark */
982 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
987 _("Could not remove bookmark"),
991 /* Shows an error dialog about not being able to create a folder */
993 error_creating_folder_dialog (GtkFileChooserDefault *impl,
998 _("The folder could not be created"),
1002 /* Shows an error about not being able to create a folder because a file with
1003 * the same name is already there.
1006 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1011 _("The folder could not be created, as a file with the same "
1012 "name already exists. Try using a different name for the "
1013 "folder, or rename the file first."),
1018 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1024 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1025 _("You need to choose a valid filename."));
1028 _("Cannot create a file under %s as it is not a folder"),
1029 parent_file, error);
1032 /* Shows an error about not being able to select a folder because a file with
1033 * the same name is already there.
1036 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1040 _("You may only select folders. The item that you selected is not a folder; "
1041 "try using a different item."),
1045 /* Shows an error dialog about not being able to create a filename */
1047 error_building_filename_dialog (GtkFileChooserDefault *impl,
1050 error_dialog (impl, _("Invalid file name"),
1054 /* Shows an error dialog when we cannot switch to a folder */
1056 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1060 error_dialog (impl, _("The folder contents could not be displayed"),
1064 /* Changes folders, displaying an error dialog if this fails */
1066 change_folder_and_display_error (GtkFileChooserDefault *impl,
1068 gboolean clear_entry)
1073 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1075 /* We copy the path because of this case:
1077 * list_row_activated()
1078 * fetches path from model; path belongs to the model (*)
1079 * calls change_folder_and_display_error()
1080 * calls gtk_file_chooser_set_current_folder_file()
1081 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1085 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1088 error_changing_folder_dialog (impl, file, error);
1094 emit_default_size_changed (GtkFileChooserDefault *impl)
1096 profile_msg (" emit default-size-changed start", NULL);
1097 g_signal_emit_by_name (impl, "default-size-changed");
1098 profile_msg (" emit default-size-changed end", NULL);
1102 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1104 if (impl->use_preview_label)
1106 if (!impl->preview_label)
1108 impl->preview_label = gtk_label_new (impl->preview_display_name);
1109 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1110 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1111 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1112 gtk_widget_show (impl->preview_label);
1117 if (impl->preview_label)
1119 gtk_widget_destroy (impl->preview_label);
1120 impl->preview_label = NULL;
1124 if (impl->preview_widget_active && impl->preview_widget)
1125 gtk_widget_show (impl->preview_box);
1127 gtk_widget_hide (impl->preview_box);
1129 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1130 emit_default_size_changed (impl);
1134 set_preview_widget (GtkFileChooserDefault *impl,
1135 GtkWidget *preview_widget)
1137 if (preview_widget == impl->preview_widget)
1140 if (impl->preview_widget)
1141 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1142 impl->preview_widget);
1144 impl->preview_widget = preview_widget;
1145 if (impl->preview_widget)
1147 gtk_widget_show (impl->preview_widget);
1148 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1149 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1150 impl->preview_widget,
1151 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1154 update_preview_widget_visibility (impl);
1157 /* Renders a "Search" icon at an appropriate size for a tree view */
1159 render_search_icon (GtkFileChooserDefault *impl)
1161 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1165 render_recent_icon (GtkFileChooserDefault *impl)
1167 GtkIconTheme *theme;
1170 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1171 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1173 theme = gtk_icon_theme_get_default ();
1175 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1181 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1187 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1188 struct ReloadIconsData
1190 GtkFileChooserDefault *impl;
1191 GtkTreeRowReference *row_ref;
1195 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1197 const GError *error,
1203 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1204 struct ReloadIconsData *data = user_data;
1206 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1209 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1211 if (cancelled || error)
1214 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1216 path = gtk_tree_row_reference_get_path (data->row_ref);
1219 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1220 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1221 SHORTCUTS_COL_PIXBUF, pixbuf,
1223 gtk_tree_path_free (path);
1227 g_object_unref (pixbuf);
1230 gtk_tree_row_reference_free (data->row_ref);
1231 g_object_unref (data->impl);
1234 g_object_unref (cancellable);
1238 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1243 profile_start ("start", NULL);
1245 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1248 for (l = impl->reload_icon_cancellables; l; l = l->next)
1250 GCancellable *cancellable = G_CANCELLABLE (l->data);
1251 g_cancellable_cancel (cancellable);
1253 g_slist_free (impl->reload_icon_cancellables);
1254 impl->reload_icon_cancellables = NULL;
1259 ShortcutType shortcut_type;
1260 gboolean pixbuf_visible;
1263 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1264 SHORTCUTS_COL_DATA, &data,
1265 SHORTCUTS_COL_TYPE, &shortcut_type,
1266 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1272 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1274 GtkFileSystemVolume *volume;
1277 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1278 impl->icon_size, NULL);
1280 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1282 if (g_file_is_native (G_FILE (data)))
1285 struct ReloadIconsData *info;
1286 GtkTreePath *tree_path;
1287 GCancellable *cancellable;
1291 info = g_new0 (struct ReloadIconsData, 1);
1292 info->impl = g_object_ref (impl);
1293 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1294 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1295 gtk_tree_path_free (tree_path);
1297 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1299 shortcuts_reload_icons_get_info_cb,
1301 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1305 GtkIconTheme *icon_theme;
1307 /* Don't call get_info for remote paths to avoid latency and
1309 * If we switch to a better bookmarks file format (XBEL), we
1310 * should use mime info to get a better icon.
1312 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1313 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1314 impl->icon_size, 0, NULL);
1317 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1319 pixbuf = render_search_icon (impl);
1321 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1323 pixbuf = render_recent_icon (impl);
1326 gtk_list_store_set (impl->shortcuts_model, &iter,
1327 SHORTCUTS_COL_PIXBUF, pixbuf,
1331 g_object_unref (pixbuf);
1335 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1339 profile_end ("end", NULL);
1343 shortcuts_find_folder (GtkFileChooserDefault *impl,
1346 GtkTreeSelection *selection;
1350 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1352 g_assert (folder != NULL);
1353 pos = shortcut_find_position (impl, folder);
1356 gtk_tree_selection_unselect_all (selection);
1360 path = gtk_tree_path_new_from_indices (pos, -1);
1361 gtk_tree_selection_select_path (selection, path);
1362 gtk_tree_path_free (path);
1365 /* If a shortcut corresponds to the current folder, selects it */
1367 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1369 shortcuts_find_folder (impl, impl->current_folder);
1372 /* Removes the specified number of rows from the shortcuts list */
1374 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1380 path = gtk_tree_path_new_from_indices (start_row, -1);
1382 for (; n_rows; n_rows--)
1386 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1387 g_assert_not_reached ();
1389 shortcuts_free_row_data (impl, &iter);
1390 gtk_list_store_remove (impl->shortcuts_model, &iter);
1393 gtk_tree_path_free (path);
1397 shortcuts_update_count (GtkFileChooserDefault *impl,
1398 ShortcutsIndex type,
1403 case SHORTCUTS_HOME:
1405 impl->has_home = FALSE;
1407 impl->has_home = TRUE;
1410 case SHORTCUTS_DESKTOP:
1412 impl->has_desktop = FALSE;
1414 impl->has_desktop = TRUE;
1417 case SHORTCUTS_VOLUMES:
1418 impl->num_volumes += value;
1421 case SHORTCUTS_SHORTCUTS:
1422 impl->num_shortcuts += value;
1425 case SHORTCUTS_BOOKMARKS:
1426 impl->num_bookmarks += value;
1429 case SHORTCUTS_CURRENT_FOLDER:
1431 impl->shortcuts_current_folder_active = FALSE;
1433 impl->shortcuts_current_folder_active = TRUE;
1442 struct ShortcutsInsertRequest
1444 GtkFileChooserDefault *impl;
1448 GtkTreeRowReference *row_ref;
1449 ShortcutsIndex type;
1455 get_file_info_finished (GCancellable *cancellable,
1457 const GError *error,
1461 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1465 GCancellable *model_cancellable = NULL;
1466 struct ShortcutsInsertRequest *request = data;
1468 path = gtk_tree_row_reference_get_path (request->row_ref);
1470 /* Handle doesn't exist anymore in the model */
1473 pos = gtk_tree_path_get_indices (path)[0];
1474 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1476 gtk_tree_path_free (path);
1478 /* validate cancellable, else goto out */
1479 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1480 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1482 if (cancellable != model_cancellable)
1485 /* set the cancellable to NULL in the model (we unref later on) */
1486 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1487 SHORTCUTS_COL_CANCELLABLE, NULL,
1495 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1496 shortcuts_update_count (request->impl, request->type, -1);
1498 if (request->type == SHORTCUTS_HOME)
1502 home = g_file_new_for_path (g_get_home_dir ());
1503 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1504 g_object_unref (home);
1506 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1508 /* Remove the current folder separator */
1509 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1510 shortcuts_remove_rows (request->impl, separator_pos, 1);
1516 if (!request->label_copy)
1517 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1518 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1519 request->impl->icon_size);
1521 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1522 SHORTCUTS_COL_PIXBUF, pixbuf,
1523 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1524 SHORTCUTS_COL_NAME, request->label_copy,
1525 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1526 SHORTCUTS_COL_REMOVABLE, request->removable,
1529 if (request->impl->shortcuts_pane_filter_model)
1530 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1532 if (request->impl->shortcuts_combo_filter_model)
1533 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1535 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1536 request->impl->save_folder_combo != NULL)
1538 /* The current folder is updated via _activate_iter(), don't
1539 * have save_folder_combo_changed_cb() call _activate_iter()
1542 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1543 G_CALLBACK (save_folder_combo_changed_cb),
1546 if (request->impl->has_search)
1549 if (request->impl->has_recent)
1552 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1553 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1554 G_CALLBACK (save_folder_combo_changed_cb),
1559 g_object_unref (pixbuf);
1562 g_object_unref (request->impl);
1563 g_object_unref (request->file);
1564 gtk_tree_row_reference_free (request->row_ref);
1565 g_free (request->label_copy);
1568 if (model_cancellable)
1569 g_object_unref (model_cancellable);
1572 /* FIXME: GtkFileSystem needs a function to split a remote path
1573 * into hostname and path components, or maybe just have a
1574 * gtk_file_system_path_get_display_name().
1576 * This function is also used in gtkfilechooserbutton.c
1579 _gtk_file_chooser_label_for_file (GFile *file)
1581 const gchar *path, *start, *end, *p;
1582 gchar *uri, *host, *label;
1584 uri = g_file_get_uri (file);
1586 start = strstr (uri, "://");
1590 path = strchr (start, '/');
1595 end = uri + strlen (uri);
1599 /* strip username */
1600 p = strchr (start, '@');
1604 p = strchr (start, ':');
1608 host = g_strndup (start, end - start);
1610 /* Translators: the first string is a path and the second string
1611 * is a hostname. Nautilus and the panel contain the same string
1614 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1620 label = g_strdup (uri);
1628 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1629 * inserts a volume. A position of -1 indicates the end of the tree.
1632 shortcuts_insert_file (GtkFileChooserDefault *impl,
1634 ShortcutType shortcut_type,
1635 GtkFileSystemVolume *volume,
1639 ShortcutsIndex type)
1642 GdkPixbuf *pixbuf = NULL;
1643 gpointer data = NULL;
1645 GtkIconTheme *icon_theme;
1647 profile_start ("start shortcut", NULL);
1649 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1652 label_copy = _gtk_file_system_volume_get_display_name (volume);
1653 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1654 impl->icon_size, NULL);
1656 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1658 if (g_file_is_native (file))
1660 struct ShortcutsInsertRequest *request;
1661 GCancellable *cancellable;
1664 request = g_new0 (struct ShortcutsInsertRequest, 1);
1665 request->impl = g_object_ref (impl);
1666 request->file = g_object_ref (file);
1667 request->name_only = TRUE;
1668 request->removable = removable;
1670 request->type = type;
1672 request->label_copy = g_strdup (label);
1675 gtk_list_store_append (impl->shortcuts_model, &iter);
1677 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1679 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1680 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1681 gtk_tree_path_free (p);
1683 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1684 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1685 get_file_info_finished, request);
1687 gtk_list_store_set (impl->shortcuts_model, &iter,
1688 SHORTCUTS_COL_DATA, g_object_ref (file),
1689 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1690 SHORTCUTS_COL_CANCELLABLE, cancellable,
1693 shortcuts_update_count (impl, type, 1);
1699 /* Don't call get_info for remote paths to avoid latency and
1702 data = g_object_ref (file);
1704 label_copy = g_strdup (label);
1706 label_copy = _gtk_file_chooser_label_for_file (file);
1708 /* If we switch to a better bookmarks file format (XBEL), we
1709 * should use mime info to get a better icon.
1711 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1712 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1713 impl->icon_size, 0, NULL);
1718 g_assert_not_reached ();
1724 gtk_list_store_append (impl->shortcuts_model, &iter);
1726 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1728 shortcuts_update_count (impl, type, 1);
1730 gtk_list_store_set (impl->shortcuts_model, &iter,
1731 SHORTCUTS_COL_PIXBUF, pixbuf,
1732 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1733 SHORTCUTS_COL_NAME, label_copy,
1734 SHORTCUTS_COL_DATA, data,
1735 SHORTCUTS_COL_TYPE, shortcut_type,
1736 SHORTCUTS_COL_REMOVABLE, removable,
1737 SHORTCUTS_COL_CANCELLABLE, NULL,
1740 if (impl->shortcuts_pane_filter_model)
1741 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1743 if (impl->shortcuts_combo_filter_model)
1744 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1746 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1748 /* The current folder is updated via _activate_iter(), don't
1749 * have save_folder_combo_changed_cb() call _activate_iter()
1752 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1754 if (impl->has_search)
1757 if (impl->has_recent)
1760 g_signal_handlers_block_by_func (impl->save_folder_combo,
1761 G_CALLBACK (save_folder_combo_changed_cb),
1764 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1765 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1766 G_CALLBACK (save_folder_combo_changed_cb),
1770 g_free (label_copy);
1773 g_object_unref (pixbuf);
1775 profile_end ("end", NULL);
1779 shortcuts_append_search (GtkFileChooserDefault *impl)
1784 pixbuf = render_search_icon (impl);
1786 gtk_list_store_append (impl->shortcuts_model, &iter);
1787 gtk_list_store_set (impl->shortcuts_model, &iter,
1788 SHORTCUTS_COL_PIXBUF, pixbuf,
1789 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1790 SHORTCUTS_COL_NAME, _("Search"),
1791 SHORTCUTS_COL_DATA, NULL,
1792 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1793 SHORTCUTS_COL_REMOVABLE, FALSE,
1797 g_object_unref (pixbuf);
1799 impl->has_search = TRUE;
1803 shortcuts_append_recent (GtkFileChooserDefault *impl)
1808 pixbuf = render_recent_icon (impl);
1810 gtk_list_store_append (impl->shortcuts_model, &iter);
1811 gtk_list_store_set (impl->shortcuts_model, &iter,
1812 SHORTCUTS_COL_PIXBUF, pixbuf,
1813 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1814 SHORTCUTS_COL_NAME, _("Recently Used"),
1815 SHORTCUTS_COL_DATA, NULL,
1816 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1817 SHORTCUTS_COL_REMOVABLE, FALSE,
1821 g_object_unref (pixbuf);
1823 impl->has_recent = TRUE;
1826 /* Appends an item for the user's home directory to the shortcuts model */
1828 shortcuts_append_home (GtkFileChooserDefault *impl)
1830 const char *home_path;
1833 profile_start ("start", NULL);
1835 home_path = g_get_home_dir ();
1836 if (home_path == NULL)
1838 profile_end ("end - no home directory!?", NULL);
1842 home = g_file_new_for_path (home_path);
1843 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1844 impl->has_home = TRUE;
1846 g_object_unref (home);
1848 profile_end ("end", NULL);
1851 /* Appends the ~/Desktop directory to the shortcuts model */
1853 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1858 profile_start ("start", NULL);
1860 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1861 /* "To disable a directory, point it to the homedir."
1862 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1864 if (!g_strcmp0 (name, g_get_home_dir ()))
1866 profile_end ("end", NULL);
1870 file = g_file_new_for_path (name);
1871 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1872 impl->has_desktop = TRUE;
1874 /* We do not actually pop up an error dialog if there is no desktop directory
1875 * because some people may really not want to have one.
1878 g_object_unref (file);
1880 profile_end ("end", NULL);
1883 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1885 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1892 profile_start ("start", NULL);
1894 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1897 for (; bookmarks; bookmarks = bookmarks->next)
1901 file = bookmarks->data;
1903 if (impl->local_only && !g_file_is_native (file))
1906 if (shortcut_find_position (impl, file) != -1)
1909 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1911 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1917 profile_end ("end", NULL);
1919 return num_inserted;
1922 /* Returns the index for the corresponding item in the shortcuts bar */
1924 shortcuts_get_index (GtkFileChooserDefault *impl,
1925 ShortcutsIndex where)
1931 if (where == SHORTCUTS_SEARCH)
1934 n += impl->has_search ? 1 : 0;
1936 if (where == SHORTCUTS_RECENT)
1939 n += impl->has_recent ? 1 : 0;
1941 if (where == SHORTCUTS_RECENT_SEPARATOR)
1944 n += impl->has_recent ? 1 : 0;
1946 if (where == SHORTCUTS_HOME)
1949 n += impl->has_home ? 1 : 0;
1951 if (where == SHORTCUTS_DESKTOP)
1954 n += impl->has_desktop ? 1 : 0;
1956 if (where == SHORTCUTS_VOLUMES)
1959 n += impl->num_volumes;
1961 if (where == SHORTCUTS_SHORTCUTS)
1964 n += impl->num_shortcuts;
1966 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1969 /* If there are no bookmarks there won't be a separator */
1970 n += (impl->num_bookmarks > 0) ? 1 : 0;
1972 if (where == SHORTCUTS_BOOKMARKS)
1975 n += impl->num_bookmarks;
1977 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1982 if (where == SHORTCUTS_CURRENT_FOLDER)
1985 g_assert_not_reached ();
1992 /* Adds all the file system volumes to the shortcuts model */
1994 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1999 gboolean old_changing_folders;
2001 profile_start ("start", NULL);
2003 old_changing_folders = impl->changing_folder;
2004 impl->changing_folder = TRUE;
2006 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2007 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2008 impl->num_volumes = 0;
2010 list = _gtk_file_system_list_volumes (impl->file_system);
2014 for (l = list; l; l = l->next)
2016 GtkFileSystemVolume *volume;
2020 if (impl->local_only)
2022 if (_gtk_file_system_volume_is_mounted (volume))
2025 gboolean base_is_native = TRUE;
2027 base_file = _gtk_file_system_volume_get_root (volume);
2028 if (base_file != NULL)
2030 base_is_native = g_file_is_native (base_file);
2031 g_object_unref (base_file);
2034 if (!base_is_native)
2039 shortcuts_insert_file (impl,
2041 SHORTCUT_TYPE_VOLUME,
2042 _gtk_file_system_volume_ref (volume),
2050 impl->num_volumes = n;
2051 g_slist_free (list);
2053 if (impl->shortcuts_pane_filter_model)
2054 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2056 if (impl->shortcuts_combo_filter_model)
2057 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2059 impl->changing_folder = old_changing_folders;
2061 profile_end ("end", NULL);
2064 /* Inserts a separator node in the shortcuts list */
2066 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2067 ShortcutsIndex where)
2071 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2072 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2073 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2075 gtk_list_store_insert (impl->shortcuts_model, &iter,
2076 shortcuts_get_index (impl, where));
2077 gtk_list_store_set (impl->shortcuts_model, &iter,
2078 SHORTCUTS_COL_PIXBUF, NULL,
2079 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2080 SHORTCUTS_COL_NAME, NULL,
2081 SHORTCUTS_COL_DATA, NULL,
2082 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2086 /* Updates the list of bookmarks */
2088 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2091 gboolean old_changing_folders;
2093 GFile *list_selected = NULL;
2094 GFile *combo_selected = NULL;
2095 ShortcutType shortcut_type;
2098 profile_start ("start", NULL);
2100 old_changing_folders = impl->changing_folder;
2101 impl->changing_folder = TRUE;
2103 if (shortcuts_get_selected (impl, &iter))
2105 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2107 SHORTCUTS_COL_DATA, &col_data,
2108 SHORTCUTS_COL_TYPE, &shortcut_type,
2111 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2112 list_selected = g_object_ref (col_data);
2115 if (impl->save_folder_combo &&
2116 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2119 GtkTreeIter child_iter;
2121 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2124 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2126 SHORTCUTS_COL_DATA, &col_data,
2127 SHORTCUTS_COL_TYPE, &shortcut_type,
2130 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2131 combo_selected = g_object_ref (col_data);
2134 if (impl->num_bookmarks > 0)
2135 shortcuts_remove_rows (impl,
2136 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2137 impl->num_bookmarks + 1);
2139 impl->num_bookmarks = 0;
2140 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2142 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2143 shortcuts_append_bookmarks (impl, bookmarks);
2144 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2145 g_slist_free (bookmarks);
2147 if (impl->num_bookmarks == 0)
2148 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2150 if (impl->shortcuts_pane_filter_model)
2151 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2153 if (impl->shortcuts_combo_filter_model)
2154 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2158 shortcuts_find_folder (impl, list_selected);
2159 g_object_unref (list_selected);
2166 pos = shortcut_find_position (impl, combo_selected);
2169 if (impl->has_search)
2172 if (impl->has_recent)
2175 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2178 g_object_unref (combo_selected);
2181 impl->changing_folder = old_changing_folders;
2183 profile_end ("end", NULL);
2186 /* Appends a separator and a row to the shortcuts list for the current folder */
2188 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2192 g_assert (!impl->shortcuts_current_folder_active);
2194 g_assert (impl->current_folder != NULL);
2196 pos = shortcut_find_position (impl, impl->current_folder);
2199 GtkFileSystemVolume *volume;
2203 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2206 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2208 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2210 base_file = _gtk_file_system_volume_get_root (volume);
2214 if (base_file && g_file_equal (base_file, impl->current_folder))
2215 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2217 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2220 g_object_unref (base_file);
2222 else if (impl->save_folder_combo != NULL)
2224 if (impl->has_search)
2227 if (impl->has_recent)
2228 pos -= 2; /* + separator */
2230 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2234 /* Updates the current folder row in the shortcuts model */
2236 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2240 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2242 if (impl->shortcuts_current_folder_active)
2244 shortcuts_remove_rows (impl, pos, 2);
2245 impl->shortcuts_current_folder_active = FALSE;
2248 shortcuts_add_current_folder (impl);
2251 /* Filter function used for the shortcuts filter model */
2253 shortcuts_pane_filter_cb (GtkTreeModel *model,
2257 GtkFileChooserDefault *impl;
2261 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2263 path = gtk_tree_model_get_path (model, iter);
2267 pos = *gtk_tree_path_get_indices (path);
2268 gtk_tree_path_free (path);
2270 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2273 /* Creates the list model for shortcuts */
2275 shortcuts_model_create (GtkFileChooserDefault *impl)
2277 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2278 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2279 GDK_TYPE_PIXBUF, /* pixbuf */
2280 G_TYPE_STRING, /* name */
2281 G_TYPE_POINTER, /* path or volume */
2282 G_TYPE_INT, /* ShortcutType */
2283 G_TYPE_BOOLEAN, /* removable */
2284 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2285 G_TYPE_POINTER); /* GCancellable */
2287 shortcuts_append_search (impl);
2289 if (impl->recent_manager)
2291 shortcuts_append_recent (impl);
2292 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2295 if (impl->file_system)
2297 shortcuts_append_home (impl);
2298 shortcuts_append_desktop (impl);
2299 shortcuts_add_volumes (impl);
2302 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2303 GTK_TREE_MODEL (impl->shortcuts_model),
2306 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2307 shortcuts_pane_filter_cb,
2312 /* Callback used when the "New Folder" button is clicked */
2314 new_folder_button_clicked (GtkButton *button,
2315 GtkFileChooserDefault *impl)
2320 if (!impl->browse_files_model)
2321 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2323 /* Prevent button from being clicked twice */
2324 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2326 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2328 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2329 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2330 path, impl->list_name_column,
2333 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2334 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2336 impl->list_name_column,
2339 gtk_tree_path_free (path);
2343 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2347 source = g_idle_source_new ();
2348 g_source_set_closure (source,
2349 g_cclosure_new_object (callback, G_OBJECT (impl)));
2350 g_source_attach (source, NULL);
2355 /* Idle handler for creating a new folder after editing its name cell, or for
2356 * canceling the editing.
2359 edited_idle_cb (GtkFileChooserDefault *impl)
2361 GDK_THREADS_ENTER ();
2363 g_source_destroy (impl->edited_idle);
2364 impl->edited_idle = NULL;
2366 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2367 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2369 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2371 if (impl->edited_new_text /* not cancelled? */
2372 && (strlen (impl->edited_new_text) != 0)
2373 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2375 GError *error = NULL;
2378 file = g_file_get_child_for_display_name (impl->current_folder,
2379 impl->edited_new_text,
2383 GError *error = NULL;
2385 if (g_file_make_directory (file, NULL, &error))
2386 change_folder_and_display_error (impl, file, FALSE);
2388 error_creating_folder_dialog (impl, file, error);
2390 g_object_unref (file);
2393 error_creating_folder_dialog (impl, file, error);
2395 g_free (impl->edited_new_text);
2396 impl->edited_new_text = NULL;
2399 GDK_THREADS_LEAVE ();
2405 queue_edited_idle (GtkFileChooserDefault *impl,
2406 const gchar *new_text)
2408 /* We create the folder in an idle handler so that we don't modify the tree
2412 if (!impl->edited_idle)
2413 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2415 g_free (impl->edited_new_text);
2416 impl->edited_new_text = g_strdup (new_text);
2419 /* Callback used from the text cell renderer when the new folder is named */
2421 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2423 const gchar *new_text,
2424 GtkFileChooserDefault *impl)
2426 /* work around bug #154921 */
2427 g_object_set (cell_renderer_text,
2428 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2429 queue_edited_idle (impl, new_text);
2432 /* Callback used from the text cell renderer when the new folder edition gets
2436 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2437 GtkFileChooserDefault *impl)
2439 /* work around bug #154921 */
2440 g_object_set (cell_renderer_text,
2441 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2442 queue_edited_idle (impl, NULL);
2445 /* Creates the widgets for the filter combo box */
2447 filter_create (GtkFileChooserDefault *impl)
2449 GtkCellRenderer *cell;
2452 impl->filter_combo = gtk_combo_box_text_new ();
2453 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2455 /* Get the combo's text renderer and set ellipsize parameters */
2456 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2460 g_object_set (G_OBJECT (cell),
2461 "ellipsize", PANGO_ELLIPSIZE_END,
2464 g_list_free (cells);
2466 g_signal_connect (impl->filter_combo, "changed",
2467 G_CALLBACK (filter_combo_changed), impl);
2469 gtk_widget_set_tooltip_text (impl->filter_combo,
2470 _("Select which types of files are shown"));
2472 return impl->filter_combo;
2476 toolbutton_new (GtkFileChooserDefault *impl,
2485 item = gtk_tool_button_new (NULL, NULL);
2486 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2487 gtk_widget_show (image);
2488 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2490 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2491 g_signal_connect (item, "clicked", callback, impl);
2494 gtk_widget_show (GTK_WIDGET (item));
2496 return GTK_WIDGET (item);
2499 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2501 shortcut_find_position (GtkFileChooserDefault *impl,
2506 int current_folder_separator_idx;
2508 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2511 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2514 /* FIXME: is this still needed? */
2515 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2519 for (i = 0; i < current_folder_separator_idx; i++)
2522 ShortcutType shortcut_type;
2524 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2525 SHORTCUTS_COL_DATA, &col_data,
2526 SHORTCUTS_COL_TYPE, &shortcut_type,
2531 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2533 GtkFileSystemVolume *volume;
2538 base_file = _gtk_file_system_volume_get_root (volume);
2540 exists = base_file && g_file_equal (file, base_file);
2543 g_object_unref (base_file);
2548 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2552 model_file = col_data;
2554 if (model_file && g_file_equal (model_file, file))
2559 if (i < current_folder_separator_idx - 1)
2561 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2562 g_assert_not_reached ();
2569 /* Tries to add a bookmark from a path name */
2571 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2577 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2579 if (shortcut_find_position (impl, file) != -1)
2583 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2585 error_adding_bookmark_dialog (impl, file, error);
2593 add_bookmark_foreach_cb (GtkTreeModel *model,
2598 GtkFileChooserDefault *impl;
2601 impl = (GtkFileChooserDefault *) data;
2603 gtk_tree_model_get (model, iter,
2604 MODEL_COL_FILE, &file,
2607 shortcuts_add_bookmark_from_file (impl, file, -1);
2609 g_object_unref (file);
2612 /* Adds a bookmark from the currently selected item in the file list */
2614 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2616 GtkTreeSelection *selection;
2618 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2620 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2621 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2623 gtk_tree_selection_selected_foreach (selection,
2624 add_bookmark_foreach_cb,
2628 /* Callback used when the "Add bookmark" button is clicked */
2630 add_bookmark_button_clicked_cb (GtkButton *button,
2631 GtkFileChooserDefault *impl)
2633 bookmarks_add_selected_folder (impl);
2636 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2637 * returns FALSE if no shortcut is selected.
2640 shortcuts_get_selected (GtkFileChooserDefault *impl,
2643 GtkTreeSelection *selection;
2644 GtkTreeIter parent_iter;
2646 if (!impl->browse_shortcuts_tree_view)
2649 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2651 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2654 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2660 /* Removes the selected bookmarks */
2662 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2670 if (!shortcuts_get_selected (impl, &iter))
2673 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2674 SHORTCUTS_COL_DATA, &col_data,
2675 SHORTCUTS_COL_REMOVABLE, &removable,
2681 g_assert (col_data != NULL);
2686 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2687 error_removing_bookmark_dialog (impl, file, error);
2690 /* Callback used when the "Remove bookmark" button is clicked */
2692 remove_bookmark_button_clicked_cb (GtkButton *button,
2693 GtkFileChooserDefault *impl)
2695 remove_selected_bookmarks (impl);
2698 struct selection_check_closure {
2699 GtkFileChooserDefault *impl;
2702 gboolean all_folders;
2705 /* Used from gtk_tree_selection_selected_foreach() */
2707 selection_check_foreach_cb (GtkTreeModel *model,
2712 struct selection_check_closure *closure;
2716 gtk_tree_model_get (model, iter,
2717 MODEL_COL_FILE, &file,
2718 MODEL_COL_IS_FOLDER, &is_folder,
2724 g_object_unref (file);
2727 closure->num_selected++;
2729 closure->all_folders = closure->all_folders && is_folder;
2730 closure->all_files = closure->all_files && !is_folder;
2733 /* Checks whether the selected items in the file list are all files or all folders */
2735 selection_check (GtkFileChooserDefault *impl,
2737 gboolean *all_files,
2738 gboolean *all_folders)
2740 struct selection_check_closure closure;
2741 GtkTreeSelection *selection;
2743 closure.impl = impl;
2744 closure.num_selected = 0;
2745 closure.all_files = TRUE;
2746 closure.all_folders = TRUE;
2748 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2749 gtk_tree_selection_selected_foreach (selection,
2750 selection_check_foreach_cb,
2753 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2756 *num_selected = closure.num_selected;
2759 *all_files = closure.all_files;
2762 *all_folders = closure.all_folders;
2765 struct get_selected_file_closure {
2766 GtkFileChooserDefault *impl;
2771 get_selected_file_foreach_cb (GtkTreeModel *model,
2776 struct get_selected_file_closure *closure = data;
2780 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2781 g_object_unref (closure->file);
2782 closure->file = NULL;
2785 gtk_tree_model_get (model, iter,
2786 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2790 /* Returns a selected path from the file list */
2792 get_selected_file (GtkFileChooserDefault *impl)
2794 struct get_selected_file_closure closure;
2795 GtkTreeSelection *selection;
2797 closure.impl = impl;
2798 closure.file = NULL;
2800 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2801 gtk_tree_selection_selected_foreach (selection,
2802 get_selected_file_foreach_cb,
2805 return closure.file;
2809 GtkFileChooserDefault *impl;
2811 } UpdateTooltipData;
2814 update_tooltip (GtkTreeModel *model,
2819 UpdateTooltipData *udata = data;
2821 if (udata->tip == NULL)
2823 gchar *display_name;
2825 gtk_tree_model_get (model, iter,
2826 MODEL_COL_NAME, &display_name,
2829 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2831 g_free (display_name);
2836 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2837 * if there are no selected items *and* the current folder is not in the
2838 * bookmarks list. De-sensitize the button otherwise.
2841 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2844 gboolean all_folders;
2848 selection_check (impl, &num_selected, NULL, &all_folders);
2850 if (num_selected == 0)
2851 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2852 else if (num_selected == 1)
2856 file = get_selected_file (impl);
2857 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2859 g_object_unref (file);
2862 active = all_folders;
2864 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2866 if (impl->browse_files_popup_menu_add_shortcut_item)
2867 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2868 (num_selected == 0) ? FALSE : active);
2872 if (num_selected == 0)
2873 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2874 else if (num_selected > 1)
2875 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2878 GtkTreeSelection *selection;
2879 UpdateTooltipData data;
2881 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2884 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2888 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2893 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2894 * bookmark row is selected in the shortcuts tree.
2897 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2900 gboolean removable = FALSE;
2904 if (shortcuts_get_selected (impl, &iter))
2906 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2907 SHORTCUTS_COL_REMOVABLE, &removable,
2908 SHORTCUTS_COL_NAME, &name,
2910 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2913 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2915 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2917 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2921 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2922 _("Remove the selected bookmark"));
2927 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2930 gboolean removable = FALSE;
2932 if (impl->browse_shortcuts_popup_menu == NULL)
2935 if (shortcuts_get_selected (impl, &iter))
2936 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2937 SHORTCUTS_COL_REMOVABLE, &removable,
2940 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2941 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2944 /* GtkWidget::drag-begin handler for the shortcuts list. */
2946 shortcuts_drag_begin_cb (GtkWidget *widget,
2947 GdkDragContext *context,
2948 GtkFileChooserDefault *impl)
2951 impl->shortcuts_drag_context = g_object_ref (context);
2956 /* Removes the idle handler for outside drags */
2958 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2960 if (!impl->shortcuts_drag_outside_idle)
2963 g_source_destroy (impl->shortcuts_drag_outside_idle);
2964 impl->shortcuts_drag_outside_idle = NULL;
2968 /* GtkWidget::drag-end handler for the shortcuts list. */
2970 shortcuts_drag_end_cb (GtkWidget *widget,
2971 GdkDragContext *context,
2972 GtkFileChooserDefault *impl)
2975 g_object_unref (impl->shortcuts_drag_context);
2977 shortcuts_cancel_drag_outside_idle (impl);
2979 if (!impl->shortcuts_drag_outside)
2982 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2984 impl->shortcuts_drag_outside = FALSE;
2988 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2990 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2991 GdkDragContext *context,
2992 GtkFileChooserDefault *impl)
2994 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2997 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3001 shortcuts_drag_leave_cb (GtkWidget *widget,
3002 GdkDragContext *context,
3004 GtkFileChooserDefault *impl)
3007 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3009 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
3013 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3015 GTK_TREE_VIEW_DROP_BEFORE);
3017 g_signal_stop_emission_by_name (widget, "drag-leave");
3020 /* Computes the appropriate row and position for dropping */
3022 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3026 GtkTreeViewDropPosition *pos)
3028 GtkTreeView *tree_view;
3029 GtkTreeViewColumn *column;
3033 int bookmarks_index;
3034 int header_height = 0;
3036 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3038 if (gtk_tree_view_get_headers_visible (tree_view))
3039 header_height = _gtk_tree_view_get_header_height (tree_view);
3041 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3043 if (!gtk_tree_view_get_path_at_pos (tree_view,
3051 row = bookmarks_index + impl->num_bookmarks - 1;
3052 *path = gtk_tree_path_new_from_indices (row, -1);
3053 *pos = GTK_TREE_VIEW_DROP_AFTER;
3057 row = *gtk_tree_path_get_indices (*path);
3058 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3059 gtk_tree_path_free (*path);
3061 if (row < bookmarks_index)
3063 row = bookmarks_index;
3064 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3066 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3068 row = bookmarks_index + impl->num_bookmarks - 1;
3069 *pos = GTK_TREE_VIEW_DROP_AFTER;
3073 if (cell_y < cell.height / 2)
3074 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3076 *pos = GTK_TREE_VIEW_DROP_AFTER;
3079 *path = gtk_tree_path_new_from_indices (row, -1);
3082 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3083 * implement the destination side of DnD by hand, due to limitations in
3084 * GtkTreeView's DnD API.
3087 shortcuts_drag_motion_cb (GtkWidget *widget,
3088 GdkDragContext *context,
3092 GtkFileChooserDefault *impl)
3095 GtkTreeViewDropPosition pos;
3096 GdkDragAction action;
3099 if (gtk_drag_get_source_widget (context) == widget)
3101 shortcuts_cancel_drag_outside_idle (impl);
3103 if (impl->shortcuts_drag_outside)
3105 shortcuts_drag_set_delete_cursor (impl, FALSE);
3106 impl->shortcuts_drag_outside = FALSE;
3111 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3112 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3113 action = GDK_ACTION_COPY;
3114 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3115 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3116 action = GDK_ACTION_MOVE;
3123 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3124 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3125 gtk_tree_path_free (path);
3129 g_signal_stop_emission_by_name (widget, "drag-motion");
3133 gdk_drag_status (context, action, time_);
3140 /* GtkWidget::drag-drop handler for the shortcuts list. */
3142 shortcuts_drag_drop_cb (GtkWidget *widget,
3143 GdkDragContext *context,
3147 GtkFileChooserDefault *impl)
3150 shortcuts_cancel_drag_outside_idle (impl);
3153 g_signal_stop_emission_by_name (widget, "drag-drop");
3157 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3159 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3160 GtkSelectionData *selection_data,
3166 uris = gtk_selection_data_get_uris (selection_data);
3170 for (i = 0; uris[i]; i++)
3176 file = g_file_new_for_uri (uri);
3178 if (shortcuts_add_bookmark_from_file (impl, file, position))
3181 g_object_unref (file);
3187 /* Reorders the selected bookmark to the specified position */
3189 shortcuts_reorder (GtkFileChooserDefault *impl,
3194 ShortcutType shortcut_type;
3197 int bookmarks_index;
3202 /* Get the selected path */
3204 if (!shortcuts_get_selected (impl, &iter))
3205 g_assert_not_reached ();
3207 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3208 old_position = *gtk_tree_path_get_indices (path);
3209 gtk_tree_path_free (path);
3211 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3212 old_position -= bookmarks_index;
3213 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3215 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3216 SHORTCUTS_COL_NAME, &name,
3217 SHORTCUTS_COL_DATA, &col_data,
3218 SHORTCUTS_COL_TYPE, &shortcut_type,
3220 g_assert (col_data != NULL);
3221 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3224 g_object_ref (file); /* removal below will free file, so we need a new ref */
3226 /* Remove the path from the old position and insert it in the new one */
3228 if (new_position > old_position)
3231 if (old_position == new_position)
3235 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3237 shortcuts_add_bookmark_from_file (impl, file, new_position);
3238 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3241 error_adding_bookmark_dialog (impl, file, error);
3245 g_object_unref (file);
3249 /* Callback used when we get the drag data for the bookmarks list. We add the
3250 * received URIs as bookmarks if they are folders.
3253 shortcuts_drag_data_received_cb (GtkWidget *widget,
3254 GdkDragContext *context,
3257 GtkSelectionData *selection_data,
3262 GtkFileChooserDefault *impl;
3263 GtkTreePath *tree_path;
3264 GtkTreeViewDropPosition tree_pos;
3267 int bookmarks_index;
3269 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3271 /* Compute position */
3273 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3275 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3276 position = *gtk_tree_path_get_indices (tree_path);
3277 gtk_tree_path_free (tree_path);
3279 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3282 g_assert (position >= bookmarks_index);
3283 position -= bookmarks_index;
3285 target = gtk_selection_data_get_target (selection_data);
3287 if (gtk_targets_include_uri (&target, 1))
3288 shortcuts_drop_uris (impl, selection_data, position);
3289 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3290 shortcuts_reorder (impl, position);
3292 g_signal_stop_emission_by_name (widget, "drag-data-received");
3295 /* Callback used to display a tooltip in the shortcuts tree */
3297 shortcuts_query_tooltip_cb (GtkWidget *widget,
3300 gboolean keyboard_mode,
3301 GtkTooltip *tooltip,
3302 GtkFileChooserDefault *impl)
3304 GtkTreeModel *model;
3307 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3315 ShortcutType shortcut_type;
3317 gtk_tree_model_get (model, &iter,
3318 SHORTCUTS_COL_DATA, &col_data,
3319 SHORTCUTS_COL_TYPE, &shortcut_type,
3322 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3324 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3326 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3331 file = G_FILE (col_data);
3332 parse_name = g_file_get_parse_name (file);
3334 gtk_tooltip_set_text (tooltip, parse_name);
3336 g_free (parse_name);
3340 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3344 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3354 /* Callback used when the selection in the shortcuts tree changes */
3356 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3357 GtkFileChooserDefault *impl)
3360 GtkTreeIter child_iter;
3362 bookmarks_check_remove_sensitivity (impl);
3363 shortcuts_check_popup_sensitivity (impl);
3365 if (impl->changing_folder)
3368 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3370 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3373 shortcuts_activate_iter (impl, &child_iter);
3378 shortcuts_row_separator_func (GtkTreeModel *model,
3382 ShortcutType shortcut_type;
3384 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3386 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3390 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3392 GtkFileChooserDefault *impl)
3396 /* don't screw up focus switching with Tab */
3397 if (event->keyval == GDK_KEY_Tab
3398 || event->keyval == GDK_KEY_KP_Tab
3399 || event->keyval == GDK_KEY_ISO_Left_Tab
3400 || event->length < 1)
3403 if (impl->location_entry)
3404 entry = impl->location_entry;
3405 else if (impl->search_entry)
3406 entry = impl->search_entry;
3412 gtk_widget_grab_focus (entry);
3413 return gtk_widget_event (entry, (GdkEvent *) event);
3419 /* Callback used when the file list's popup menu is detached */
3421 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3424 GtkFileChooserDefault *impl;
3426 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3427 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3429 impl->browse_shortcuts_popup_menu = NULL;
3430 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3431 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3435 remove_shortcut_cb (GtkMenuItem *item,
3436 GtkFileChooserDefault *impl)
3438 remove_selected_bookmarks (impl);
3441 /* Rename the selected bookmark */
3443 rename_selected_bookmark (GtkFileChooserDefault *impl)
3447 GtkTreeViewColumn *column;
3448 GtkCellRenderer *cell;
3451 if (shortcuts_get_selected (impl, &iter))
3453 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3454 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3455 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3456 cell = g_list_nth_data (renderers, 1);
3457 g_list_free (renderers);
3458 g_object_set (cell, "editable", TRUE, NULL);
3459 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3460 path, column, cell, TRUE);
3461 gtk_tree_path_free (path);
3466 rename_shortcut_cb (GtkMenuItem *item,
3467 GtkFileChooserDefault *impl)
3469 rename_selected_bookmark (impl);
3472 /* Constructs the popup menu for the file list if needed */
3474 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3478 if (impl->browse_shortcuts_popup_menu)
3481 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3482 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3483 impl->browse_shortcuts_tree_view,
3484 shortcuts_popup_menu_detach_cb);
3486 item = gtk_image_menu_item_new_with_label (_("Remove"));
3487 impl->browse_shortcuts_popup_menu_remove_item = item;
3488 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3489 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3490 g_signal_connect (item, "activate",
3491 G_CALLBACK (remove_shortcut_cb), impl);
3492 gtk_widget_show (item);
3493 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3495 item = gtk_menu_item_new_with_label (_("Rename..."));
3496 impl->browse_shortcuts_popup_menu_rename_item = item;
3497 g_signal_connect (item, "activate",
3498 G_CALLBACK (rename_shortcut_cb), impl);
3499 gtk_widget_show (item);
3500 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3504 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3506 shortcuts_build_popup_menu (impl);
3507 shortcuts_check_popup_sensitivity (impl);
3511 popup_position_func (GtkMenu *menu,
3515 gpointer user_data);
3518 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3519 GdkEventButton *event)
3521 shortcuts_update_popup_menu (impl);
3523 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3524 NULL, NULL, NULL, NULL,
3525 event->button, event->time);
3528 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3530 popup_position_func, impl->browse_shortcuts_tree_view,
3531 0, GDK_CURRENT_TIME);
3532 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3537 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3539 shortcuts_popup_menu_cb (GtkWidget *widget,
3540 GtkFileChooserDefault *impl)
3542 shortcuts_popup_menu (impl, NULL);
3546 /* Callback used when a button is pressed on the shortcuts list.
3547 * We trap button 3 to bring up a popup menu.
3550 shortcuts_button_press_event_cb (GtkWidget *widget,
3551 GdkEventButton *event,
3552 GtkFileChooserDefault *impl)
3554 static gboolean in_press = FALSE;
3560 if (event->button != 3)
3564 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3570 shortcuts_popup_menu (impl, event);
3575 shortcuts_edited (GtkCellRenderer *cell,
3578 GtkFileChooserDefault *impl)
3584 g_object_set (cell, "editable", FALSE, NULL);
3586 path = gtk_tree_path_new_from_string (path_string);
3587 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3588 g_assert_not_reached ();
3590 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3591 SHORTCUTS_COL_DATA, &shortcut,
3593 gtk_tree_path_free (path);
3595 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3599 shortcuts_editing_canceled (GtkCellRenderer *cell,
3600 GtkFileChooserDefault *impl)
3602 g_object_set (cell, "editable", FALSE, NULL);
3605 /* Creates the widgets for the shortcuts and bookmarks tree */
3607 shortcuts_list_create (GtkFileChooserDefault *impl)
3610 GtkTreeSelection *selection;
3611 GtkTreeViewColumn *column;
3612 GtkCellRenderer *renderer;
3614 /* Target types for dragging a row to/from the shortcuts list */
3615 const GtkTargetEntry tree_model_row_targets[] = {
3616 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3619 /* Scrolled window */
3621 swin = gtk_scrolled_window_new (NULL, NULL);
3622 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3623 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3624 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3626 gtk_widget_show (swin);
3629 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3630 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3631 GTK_STYLE_CLASS_SIDEBAR);
3632 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3633 #ifdef PROFILE_FILE_CHOOSER
3634 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3637 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3639 * 1. user brings up a SAVE dialog
3640 * 2. user clicks on a shortcut in the shortcuts pane
3641 * 3. user starts typing a filename
3643 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3644 * support interactive search. However, we'd rather focus the location entry
3645 * so that the user can type *there*.
3647 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3648 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3649 * if the user starts typing while the focus is in the shortcuts pane.
3651 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3652 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3654 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3655 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3656 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3657 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3658 /* Accessible object name for the file chooser's shortcuts pane */
3659 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3661 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3663 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3665 tree_model_row_targets,
3666 G_N_ELEMENTS (tree_model_row_targets),
3669 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3670 GTK_DEST_DEFAULT_ALL,
3671 tree_model_row_targets,
3672 G_N_ELEMENTS (tree_model_row_targets),
3673 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3674 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3676 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3677 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3678 gtk_tree_selection_set_select_function (selection,
3679 shortcuts_select_func,
3682 g_signal_connect (selection, "changed",
3683 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3685 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3686 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3688 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3689 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3690 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3691 G_CALLBACK (shortcuts_drag_end_cb), impl);
3692 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3693 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3695 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3696 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3697 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3698 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3699 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3700 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3701 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3702 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3704 /* Support tooltips */
3705 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3706 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3707 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3709 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3710 gtk_widget_show (impl->browse_shortcuts_tree_view);
3714 column = gtk_tree_view_column_new ();
3715 /* Column header for the file chooser's shortcuts pane */
3716 gtk_tree_view_column_set_title (column, _("_Places"));
3718 renderer = gtk_cell_renderer_pixbuf_new ();
3719 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3720 gtk_tree_view_column_set_attributes (column, renderer,
3721 "pixbuf", SHORTCUTS_COL_PIXBUF,
3722 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3725 renderer = gtk_cell_renderer_text_new ();
3726 g_object_set (renderer,
3728 "ellipsize", PANGO_ELLIPSIZE_END,
3730 g_signal_connect (renderer, "edited",
3731 G_CALLBACK (shortcuts_edited), impl);
3732 g_signal_connect (renderer, "editing-canceled",
3733 G_CALLBACK (shortcuts_editing_canceled), impl);
3734 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3735 gtk_tree_view_column_set_attributes (column, renderer,
3736 "text", SHORTCUTS_COL_NAME,
3739 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3740 shortcuts_row_separator_func,
3743 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3748 /* Creates the widgets for the shortcuts/bookmarks pane */
3750 shortcuts_pane_create (GtkFileChooserDefault *impl,
3751 GtkSizeGroup *size_group)
3756 GtkStyleContext *context;
3759 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3760 gtk_widget_show (vbox);
3762 /* Shortcuts tree */
3764 widget = shortcuts_list_create (impl);
3766 gtk_size_group_add_widget (size_group, widget);
3767 context = gtk_widget_get_style_context (widget);
3768 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3770 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3772 /* Box for buttons */
3774 toolbar = gtk_toolbar_new ();
3775 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3777 context = gtk_widget_get_style_context (toolbar);
3778 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3779 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3781 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3782 gtk_widget_show (toolbar);
3784 /* Add bookmark button */
3785 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3786 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3790 G_CALLBACK (add_bookmark_button_clicked_cb));
3791 g_object_unref (icon);
3793 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3794 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3795 _("Add the selected folder to the Bookmarks"));
3797 /* Remove bookmark button */
3798 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3799 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3803 G_CALLBACK (remove_bookmark_button_clicked_cb));
3804 g_object_unref (icon);
3805 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3806 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3807 _("Remove the selected bookmark"));
3813 key_is_left_or_right (GdkEventKey *event)
3817 modifiers = gtk_accelerator_get_default_mod_mask ();
3819 return ((event->keyval == GDK_KEY_Right
3820 || event->keyval == GDK_KEY_KP_Right
3821 || event->keyval == GDK_KEY_Left
3822 || event->keyval == GDK_KEY_KP_Left)
3823 && (event->state & modifiers) == 0);
3826 /* Handles key press events on the file list, so that we can trap Enter to
3827 * activate the default button on our own. Also, checks to see if '/' has been
3828 * pressed. See comment by tree_view_keybinding_cb() for more details.
3831 browse_files_key_press_event_cb (GtkWidget *widget,
3835 GtkFileChooserDefault *impl;
3838 impl = (GtkFileChooserDefault *) data;
3840 modifiers = gtk_accelerator_get_default_mod_mask ();
3842 if ((event->keyval == GDK_KEY_slash
3843 || event->keyval == GDK_KEY_KP_Divide
3845 || event->keyval == GDK_KEY_asciitilde
3847 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3849 location_popup_handler (impl, event->string);
3853 if (key_is_left_or_right (event))
3855 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3859 if ((event->keyval == GDK_KEY_Return
3860 || event->keyval == GDK_KEY_ISO_Enter
3861 || event->keyval == GDK_KEY_KP_Enter
3862 || event->keyval == GDK_KEY_space
3863 || event->keyval == GDK_KEY_KP_Space)
3864 && ((event->state & modifiers) == 0)
3865 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3866 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3870 window = get_toplevel (widget);
3873 GtkWidget *default_widget, *focus_widget;
3875 default_widget = gtk_window_get_default_widget (window);
3876 focus_widget = gtk_window_get_focus (window);
3878 if (widget != default_widget &&
3879 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3881 gtk_window_activate_default (window);
3891 /* Callback used when the file list's popup menu is detached */
3893 popup_menu_detach_cb (GtkWidget *attach_widget,
3896 GtkFileChooserDefault *impl;
3898 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3899 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3901 impl->browse_files_popup_menu = NULL;
3902 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3903 impl->browse_files_popup_menu_hidden_files_item = NULL;
3906 /* Callback used when the "Add to Bookmarks" menu item is activated */
3908 add_to_shortcuts_cb (GtkMenuItem *item,
3909 GtkFileChooserDefault *impl)
3911 bookmarks_add_selected_folder (impl);
3914 /* Callback used when the "Show Hidden Files" menu item is toggled */
3916 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3917 GtkFileChooserDefault *impl)
3920 "show-hidden", gtk_check_menu_item_get_active (item),
3924 /* Callback used when the "Show Size Column" menu item is toggled */
3926 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3927 GtkFileChooserDefault *impl)
3929 impl->show_size_column = gtk_check_menu_item_get_active (item);
3931 gtk_tree_view_column_set_visible (impl->list_size_column,
3932 impl->show_size_column);
3935 /* Shows an error dialog about not being able to select a dragged file */
3937 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3942 _("Could not select file"),
3947 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3952 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3954 for (i = 1; uris[i]; i++)
3957 GError *error = NULL;
3960 file = g_file_new_for_uri (uri);
3962 gtk_file_chooser_default_select_file (chooser, file, &error);
3964 error_selecting_dragged_file_dialog (impl, file, error);
3966 g_object_unref (file);
3970 struct FileListDragData
3972 GtkFileChooserDefault *impl;
3978 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3980 const GError *error,
3983 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3984 struct FileListDragData *data = user_data;
3985 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3987 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3990 data->impl->file_list_drag_data_received_cancellable = NULL;
3992 if (cancelled || error)
3995 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3996 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3997 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3998 change_folder_and_display_error (data->impl, data->file, FALSE);
4001 GError *error = NULL;
4003 gtk_file_chooser_default_unselect_all (chooser);
4004 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4006 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4008 browse_files_center_selected_row (data->impl);
4011 if (data->impl->select_multiple)
4012 file_list_drag_data_select_uris (data->impl, data->uris);
4015 g_object_unref (data->impl);
4016 g_strfreev (data->uris);
4017 g_object_unref (data->file);
4020 g_object_unref (cancellable);
4024 file_list_drag_data_received_cb (GtkWidget *widget,
4025 GdkDragContext *context,
4028 GtkSelectionData *selection_data,
4033 GtkFileChooserDefault *impl;
4038 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4040 /* Allow only drags from other widgets; see bug #533891. */
4041 if (gtk_drag_get_source_widget (context) == widget)
4043 g_signal_stop_emission_by_name (widget, "drag-data-received");
4047 /* Parse the text/uri-list string, navigate to the first one */
4048 uris = gtk_selection_data_get_uris (selection_data);
4049 if (uris && uris[0])
4051 struct FileListDragData *data;
4054 file = g_file_new_for_uri (uri);
4056 data = g_new0 (struct FileListDragData, 1);
4057 data->impl = g_object_ref (impl);
4061 if (impl->file_list_drag_data_received_cancellable)
4062 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4064 impl->file_list_drag_data_received_cancellable =
4065 _gtk_file_system_get_info (impl->file_system, file,
4067 file_list_drag_data_received_get_info_cb,
4071 g_signal_stop_emission_by_name (widget, "drag-data-received");
4074 /* Don't do anything with the drag_drop signal */
4076 file_list_drag_drop_cb (GtkWidget *widget,
4077 GdkDragContext *context,
4081 GtkFileChooserDefault *impl)
4083 g_signal_stop_emission_by_name (widget, "drag-drop");
4087 /* Disable the normal tree drag motion handler, it makes it look like you're
4088 dropping the dragged item onto a tree item */
4090 file_list_drag_motion_cb (GtkWidget *widget,
4091 GdkDragContext *context,
4095 GtkFileChooserDefault *impl)
4097 g_signal_stop_emission_by_name (widget, "drag-motion");
4101 /* Constructs the popup menu for the file list if needed */
4103 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4107 if (impl->browse_files_popup_menu)
4110 impl->browse_files_popup_menu = gtk_menu_new ();
4111 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4112 impl->browse_files_tree_view,
4113 popup_menu_detach_cb);
4115 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4116 impl->browse_files_popup_menu_add_shortcut_item = item;
4117 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4118 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4119 g_signal_connect (item, "activate",
4120 G_CALLBACK (add_to_shortcuts_cb), impl);
4121 gtk_widget_show (item);
4122 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4124 item = gtk_separator_menu_item_new ();
4125 gtk_widget_show (item);
4126 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4128 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4129 impl->browse_files_popup_menu_hidden_files_item = item;
4130 g_signal_connect (item, "toggled",
4131 G_CALLBACK (show_hidden_toggled_cb), impl);
4132 gtk_widget_show (item);
4133 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4135 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4136 impl->browse_files_popup_menu_size_column_item = item;
4137 g_signal_connect (item, "toggled",
4138 G_CALLBACK (show_size_column_toggled_cb), impl);
4139 gtk_widget_show (item);
4140 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4142 bookmarks_check_add_sensitivity (impl);
4145 /* Updates the popup menu for the file list, creating it if necessary */
4147 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4149 file_list_build_popup_menu (impl);
4151 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4153 /* The sensitivity of the Add to Bookmarks item is set in
4154 * bookmarks_check_add_sensitivity()
4157 /* 'Show Hidden Files' */
4158 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4159 G_CALLBACK (show_hidden_toggled_cb), impl);
4160 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4162 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4163 G_CALLBACK (show_hidden_toggled_cb), impl);
4165 /* 'Show Size Column' */
4166 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4167 G_CALLBACK (show_size_column_toggled_cb), impl);
4168 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4169 impl->show_size_column);
4170 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4171 G_CALLBACK (show_size_column_toggled_cb), impl);
4175 popup_position_func (GtkMenu *menu,
4181 GtkAllocation allocation;
4182 GtkWidget *widget = GTK_WIDGET (user_data);
4183 GdkScreen *screen = gtk_widget_get_screen (widget);
4186 GdkRectangle monitor;
4188 g_return_if_fail (gtk_widget_get_realized (widget));
4190 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4192 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4195 gtk_widget_get_allocation (widget, &allocation);
4196 *x += (allocation.width - req.width) / 2;
4197 *y += (allocation.height - req.height) / 2;
4199 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4200 gtk_menu_set_monitor (menu, monitor_num);
4201 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4203 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4204 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4210 file_list_popup_menu (GtkFileChooserDefault *impl,
4211 GdkEventButton *event)
4213 file_list_update_popup_menu (impl);
4215 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4216 NULL, NULL, NULL, NULL,
4217 event->button, event->time);
4220 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4222 popup_position_func, impl->browse_files_tree_view,
4223 0, GDK_CURRENT_TIME);
4224 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4230 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4232 list_popup_menu_cb (GtkWidget *widget,
4233 GtkFileChooserDefault *impl)
4235 file_list_popup_menu (impl, NULL);
4239 /* Callback used when a button is pressed on the file list. We trap button 3 to
4240 * bring up a popup menu.
4243 list_button_press_event_cb (GtkWidget *widget,
4244 GdkEventButton *event,
4245 GtkFileChooserDefault *impl)
4247 static gboolean in_press = FALSE;
4252 if (event->button != 3)
4256 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4259 file_list_popup_menu (impl, event);
4264 OperationMode operation_mode;
4265 gint general_column;
4269 /* Sets the sort column IDs for the file list based on the operation mode */
4271 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4273 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4274 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4275 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4279 file_list_query_tooltip_cb (GtkWidget *widget,
4282 gboolean keyboard_tip,
4283 GtkTooltip *tooltip,
4286 GtkFileChooserDefault *impl = user_data;
4287 GtkTreeModel *model;
4293 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4297 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4300 &model, &path, &iter))
4303 gtk_tree_model_get (model, &iter,
4304 MODEL_COL_FILE, &file,
4309 gtk_tree_path_free (path);
4313 filename = g_file_get_path (file);
4314 gtk_tooltip_set_text (tooltip, filename);
4315 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4320 g_object_unref (file);
4321 gtk_tree_path_free (path);
4327 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4331 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4332 gtk_cell_renderer_set_fixed_size (renderer,
4333 xpad * 2 + impl->icon_size,
4334 ypad * 2 + impl->icon_size);
4337 /* Creates the widgets for the file list */
4339 create_file_list (GtkFileChooserDefault *impl)
4342 GtkTreeSelection *selection;
4343 GtkTreeViewColumn *column;
4344 GtkCellRenderer *renderer;
4346 /* Scrolled window */
4347 swin = gtk_scrolled_window_new (NULL, NULL);
4348 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4349 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4350 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4353 /* Tree/list view */
4355 impl->browse_files_tree_view = gtk_tree_view_new ();
4356 #ifdef PROFILE_FILE_CHOOSER
4357 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4359 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4360 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4362 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4363 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4365 gtk_drag_dest_set (impl->browse_files_tree_view,
4366 GTK_DEST_DEFAULT_ALL,
4368 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4369 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4371 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4372 G_CALLBACK (list_row_activated), impl);
4373 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4374 G_CALLBACK (browse_files_key_press_event_cb), impl);
4375 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4376 G_CALLBACK (list_popup_menu_cb), impl);
4377 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4378 G_CALLBACK (list_button_press_event_cb), impl);
4380 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4381 G_CALLBACK (file_list_drag_data_received_cb), impl);
4382 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4383 G_CALLBACK (file_list_drag_drop_cb), impl);
4384 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4385 G_CALLBACK (file_list_drag_motion_cb), impl);
4387 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4388 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4389 G_CALLBACK (file_list_query_tooltip_cb), impl);
4391 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4392 gtk_tree_selection_set_select_function (selection,
4395 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4398 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4399 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4401 g_signal_connect (selection, "changed",
4402 G_CALLBACK (list_selection_changed), impl);
4404 /* Keep the column order in sync with update_cell_renderer_attributes() */
4406 /* Filename column */
4408 impl->list_name_column = gtk_tree_view_column_new ();
4409 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4410 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4411 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4413 renderer = gtk_cell_renderer_pixbuf_new ();
4414 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4415 set_icon_cell_renderer_fixed_size (impl, renderer);
4416 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4418 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4419 g_object_set (impl->list_name_renderer,
4420 "ellipsize", PANGO_ELLIPSIZE_END,
4422 g_signal_connect (impl->list_name_renderer, "edited",
4423 G_CALLBACK (renderer_edited_cb), impl);
4424 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4425 G_CALLBACK (renderer_editing_canceled_cb), impl);
4426 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4428 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4432 column = gtk_tree_view_column_new ();
4433 gtk_tree_view_column_set_resizable (column, TRUE);
4434 gtk_tree_view_column_set_title (column, _("Size"));
4436 renderer = gtk_cell_renderer_text_new ();
4437 g_object_set (renderer,
4438 "alignment", PANGO_ALIGN_RIGHT,
4440 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4441 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4442 impl->list_size_column = column;
4444 /* Modification time column */
4446 column = gtk_tree_view_column_new ();
4447 gtk_tree_view_column_set_resizable (column, TRUE);
4448 gtk_tree_view_column_set_title (column, _("Modified"));
4450 renderer = gtk_cell_renderer_text_new ();
4451 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4452 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4453 impl->list_mtime_column = column;
4455 file_list_set_sort_column_ids (impl);
4456 update_cell_renderer_attributes (impl);
4458 gtk_widget_show_all (swin);
4464 create_path_bar (GtkFileChooserDefault *impl)
4466 GtkWidget *path_bar;
4468 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4469 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4474 /* Creates the widgets for the files/folders pane */
4476 file_pane_create (GtkFileChooserDefault *impl,
4477 GtkSizeGroup *size_group)
4483 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4484 gtk_widget_show (vbox);
4486 /* Box for lists and preview */
4488 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4489 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4490 gtk_widget_show (hbox);
4494 widget = create_file_list (impl);
4495 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4496 gtk_size_group_add_widget (size_group, widget);
4500 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4501 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4502 /* Don't show preview box initially */
4506 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4508 widget = filter_create (impl);
4510 gtk_widget_show (widget);
4511 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4513 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4518 /* Callback used when the "Browse for more folders" expander is toggled */
4520 expander_changed_cb (GtkExpander *expander,
4522 GtkFileChooserDefault *impl)
4524 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4525 update_appearance (impl);
4528 /* Callback used when the selection changes in the save folder combo box */
4530 save_folder_combo_changed_cb (GtkComboBox *combo,
4531 GtkFileChooserDefault *impl)
4535 if (impl->changing_folder)
4538 if (gtk_combo_box_get_active_iter (combo, &iter))
4540 GtkTreeIter child_iter;
4542 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4545 shortcuts_activate_iter (impl, &child_iter);
4550 save_folder_update_tooltip (GtkComboBox *combo,
4551 GtkFileChooserDefault *impl)
4558 if (gtk_combo_box_get_active_iter (combo, &iter))
4560 GtkTreeIter child_iter;
4562 ShortcutType shortcut_type;
4564 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4567 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4568 SHORTCUTS_COL_DATA, &col_data,
4569 SHORTCUTS_COL_TYPE, &shortcut_type,
4572 if (shortcut_type == SHORTCUT_TYPE_FILE)
4573 tooltip = g_file_get_parse_name (G_FILE (col_data));
4576 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4577 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4578 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4582 /* Filter function used to filter out the Search item and its separator.
4583 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4586 shortcuts_combo_filter_func (GtkTreeModel *model,
4590 GtkFileChooserDefault *impl;
4591 GtkTreePath *tree_path;
4596 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4598 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4600 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4601 g_assert (tree_path != NULL);
4603 indices = gtk_tree_path_get_indices (tree_path);
4607 if (impl->has_search)
4609 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4610 if (idx == indices[0])
4614 if (impl->has_recent)
4616 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4617 if (idx == indices[0])
4621 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4622 if (idx == indices[0])
4627 gtk_tree_path_free (tree_path);
4632 /* Creates the combo box with the save folders */
4634 save_folder_combo_create (GtkFileChooserDefault *impl)
4637 GtkCellRenderer *cell;
4639 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4640 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4641 shortcuts_combo_filter_func,
4645 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4646 "model", impl->shortcuts_combo_filter_model,
4647 "focus-on-click", FALSE,
4649 gtk_widget_show (combo);
4651 cell = gtk_cell_renderer_pixbuf_new ();
4652 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4653 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4654 "pixbuf", SHORTCUTS_COL_PIXBUF,
4655 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4656 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4659 cell = gtk_cell_renderer_text_new ();
4660 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4661 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4662 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4663 "text", SHORTCUTS_COL_NAME,
4664 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4667 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4668 shortcuts_row_separator_func,
4671 g_signal_connect (combo, "changed",
4672 G_CALLBACK (save_folder_combo_changed_cb), impl);
4673 g_signal_connect (combo, "changed",
4674 G_CALLBACK (save_folder_update_tooltip), impl);
4679 /* Creates the widgets specific to Save mode */
4681 save_widgets_create (GtkFileChooserDefault *impl)
4687 if (impl->save_widgets != NULL)
4690 location_switch_to_path_bar (impl);
4692 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4694 table = gtk_table_new (2, 2, FALSE);
4695 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4696 gtk_widget_show (table);
4697 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4698 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4702 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4703 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4704 gtk_table_attach (GTK_TABLE (table), widget,
4708 gtk_widget_show (widget);
4710 /* Location entry */
4712 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4713 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4715 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4716 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4717 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4718 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4720 GTK_EXPAND | GTK_FILL, 0,
4722 gtk_widget_show (impl->location_entry);
4723 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4726 impl->save_folder_label = gtk_label_new (NULL);
4727 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4728 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4732 gtk_widget_show (impl->save_folder_label);
4734 impl->save_folder_combo = save_folder_combo_create (impl);
4735 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4737 GTK_EXPAND | GTK_FILL, GTK_FILL,
4739 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4743 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4744 gtk_widget_set_halign (impl->save_expander, GTK_ALIGN_START);
4745 gtk_widget_set_valign (impl->save_expander, GTK_ALIGN_CENTER);
4746 gtk_box_pack_start (GTK_BOX (vbox), impl->save_expander, FALSE, FALSE, 0);
4747 g_signal_connect (impl->save_expander, "notify::expanded",
4748 G_CALLBACK (expander_changed_cb),
4750 gtk_widget_show (impl->save_expander);
4752 impl->save_widgets = vbox;
4753 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4754 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4755 gtk_widget_show (impl->save_widgets);
4758 /* Destroys the widgets specific to Save mode */
4760 save_widgets_destroy (GtkFileChooserDefault *impl)
4762 if (impl->save_widgets == NULL)
4765 gtk_widget_destroy (impl->save_widgets);
4766 impl->save_widgets = NULL;
4767 impl->location_entry = NULL;
4768 impl->save_folder_label = NULL;
4769 impl->save_folder_combo = NULL;
4770 impl->save_expander = NULL;
4773 /* Turns on the path bar widget. Can be called even if we are already in that
4777 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4779 if (impl->location_entry)
4781 gtk_widget_destroy (impl->location_entry);
4782 impl->location_entry = NULL;
4785 gtk_widget_hide (impl->location_entry_box);
4788 /* Sets the full path of the current folder as the text in the location entry. */
4790 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4792 gchar *text, *filename;
4794 if (!impl->current_folder)
4797 filename = g_file_get_path (impl->current_folder);
4801 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4805 text = g_file_get_uri (impl->current_folder);
4809 gboolean need_slash;
4812 len = strlen (text);
4813 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4819 slash_text = g_new (char, len + 2);
4820 strcpy (slash_text, text);
4821 slash_text[len] = G_DIR_SEPARATOR;
4822 slash_text[len + 1] = 0;
4828 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4835 /* Turns on the location entry. Can be called even if we are already in that
4839 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4841 /* when in search or recent files mode, we are not showing the
4842 * location_entry_box container, so there's no point in switching
4845 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4846 impl->operation_mode == OPERATION_MODE_RECENT)
4849 if (impl->location_entry)
4850 gtk_widget_destroy (impl->location_entry);
4854 gtk_widget_show (impl->location_entry_box);
4858 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4859 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4861 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4862 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4864 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4865 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4867 /* Configure the entry */
4869 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4873 gtk_widget_show (impl->location_entry);
4874 gtk_widget_grab_focus (impl->location_entry);
4877 /* Sets a new location mode. set_buttons determines whether the toggle button
4878 * for the mode will also be changed.
4881 location_mode_set (GtkFileChooserDefault *impl,
4882 LocationMode new_mode,
4883 gboolean set_button)
4885 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4886 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4888 GtkWindow *toplevel;
4889 GtkWidget *current_focus;
4890 gboolean button_active;
4891 gboolean switch_to_file_list;
4895 case LOCATION_MODE_PATH_BAR:
4896 button_active = FALSE;
4898 /* The location_entry will disappear when we switch to path bar mode. So,
4899 * we'll focus the file list in that case, to avoid having a window with
4900 * no focused widget.
4902 toplevel = get_toplevel (GTK_WIDGET (impl));
4903 switch_to_file_list = FALSE;
4906 current_focus = gtk_window_get_focus (toplevel);
4907 if (!current_focus || current_focus == impl->location_entry)
4908 switch_to_file_list = TRUE;
4911 location_switch_to_path_bar (impl);
4913 if (switch_to_file_list)
4914 gtk_widget_grab_focus (impl->browse_files_tree_view);
4918 case LOCATION_MODE_FILENAME_ENTRY:
4919 button_active = TRUE;
4920 location_switch_to_filename_entry (impl);
4924 g_assert_not_reached ();
4930 g_signal_handlers_block_by_func (impl->location_button,
4931 G_CALLBACK (location_button_toggled_cb), impl);
4933 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4935 g_signal_handlers_unblock_by_func (impl->location_button,
4936 G_CALLBACK (location_button_toggled_cb), impl);
4940 impl->location_mode = new_mode;
4944 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4946 /* when in search or recent files mode, we are not showing the
4947 * location_entry_box container, so there's no point in switching
4950 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4951 impl->operation_mode == OPERATION_MODE_RECENT)
4954 /* If the file entry is not visible, show it.
4955 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4957 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4959 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4961 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4963 if (gtk_widget_has_focus (impl->location_entry))
4965 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4969 gtk_widget_grab_focus (impl->location_entry);
4974 /* Callback used when one of the location mode buttons is toggled */
4976 location_button_toggled_cb (GtkToggleButton *toggle,
4977 GtkFileChooserDefault *impl)
4980 LocationMode new_mode;
4982 is_active = gtk_toggle_button_get_active (toggle);
4986 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4987 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4991 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4992 new_mode = LOCATION_MODE_PATH_BAR;
4995 location_mode_set (impl, new_mode, FALSE);
4998 /* Creates a toggle button for the location entry. */
5000 location_button_create (GtkFileChooserDefault *impl)
5005 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5006 gtk_widget_show (image);
5008 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5012 g_signal_connect (impl->location_button, "toggled",
5013 G_CALLBACK (location_button_toggled_cb), impl);
5015 str = _("Type a file name");
5017 gtk_widget_set_tooltip_text (impl->location_button, str);
5018 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5021 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5023 browse_widgets_create (GtkFileChooserDefault *impl)
5028 GtkSizeGroup *size_group;
5030 /* size group is used by the scrolled windows of the panes */
5031 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5032 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5034 /* Location widgets */
5035 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5036 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5037 gtk_widget_show (impl->browse_path_bar_hbox);
5039 /* Size group that allows the path bar to be the same size between modes */
5040 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5041 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5043 /* Location button */
5045 location_button_create (impl);
5046 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5047 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5051 impl->browse_path_bar = create_path_bar (impl);
5052 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5053 gtk_widget_show_all (impl->browse_path_bar);
5054 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5055 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
5058 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5059 g_signal_connect (impl->browse_new_folder_button, "clicked",
5060 G_CALLBACK (new_folder_button_clicked), impl);
5061 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5063 /* Box for the location label and entry */
5065 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5066 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5068 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5069 gtk_widget_show (impl->location_label);
5070 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5074 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5075 gtk_widget_show (hpaned);
5076 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5078 widget = shortcuts_pane_create (impl, size_group);
5079 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5080 widget = file_pane_create (impl, size_group);
5081 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5082 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5083 g_object_unref (size_group);
5089 gtk_file_chooser_default_constructor (GType type,
5090 guint n_construct_properties,
5091 GObjectConstructParam *construct_params)
5093 GtkFileChooserDefault *impl;
5096 profile_start ("start", NULL);
5098 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5099 n_construct_properties,
5101 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5103 g_assert (impl->file_system);
5105 gtk_widget_push_composite_child ();
5107 /* Shortcuts model */
5108 shortcuts_model_create (impl);
5110 /* The browse widgets */
5111 impl->browse_widgets = browse_widgets_create (impl);
5112 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5114 /* Alignment to hold extra widget */
5115 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5116 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5117 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5118 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5120 gtk_widget_pop_composite_child ();
5121 update_appearance (impl);
5123 profile_end ("end", NULL);
5128 /* Sets the extra_widget by packing it in the appropriate place */
5130 set_extra_widget (GtkFileChooserDefault *impl,
5131 GtkWidget *extra_widget)
5135 g_object_ref (extra_widget);
5136 /* FIXME: is this right ? */
5137 gtk_widget_show (extra_widget);
5140 if (impl->extra_widget)
5142 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5143 g_object_unref (impl->extra_widget);
5146 impl->extra_widget = extra_widget;
5147 if (impl->extra_widget)
5149 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5150 gtk_widget_show (impl->extra_align);
5153 gtk_widget_hide (impl->extra_align);
5157 set_local_only (GtkFileChooserDefault *impl,
5158 gboolean local_only)
5160 if (local_only != impl->local_only)
5162 impl->local_only = local_only;
5164 if (impl->location_entry)
5165 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5167 if (impl->shortcuts_model && impl->file_system)
5169 shortcuts_add_volumes (impl);
5170 shortcuts_add_bookmarks (impl);
5173 if (local_only && impl->current_folder &&
5174 !g_file_is_native (impl->current_folder))
5176 /* If we are pointing to a non-local folder, make an effort to change
5177 * back to a local folder, but it's really up to the app to not cause
5178 * such a situation, so we ignore errors.
5180 const gchar *home = g_get_home_dir ();
5186 home_file = g_file_new_for_path (home);
5188 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5190 g_object_unref (home_file);
5196 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5197 GtkFileChooserDefault *impl)
5199 shortcuts_add_volumes (impl);
5200 shortcuts_add_bookmarks (impl);
5202 bookmarks_check_add_sensitivity (impl);
5203 bookmarks_check_remove_sensitivity (impl);
5204 shortcuts_check_popup_sensitivity (impl);
5207 /* Sets the file chooser to multiple selection mode */
5209 set_select_multiple (GtkFileChooserDefault *impl,
5210 gboolean select_multiple,
5211 gboolean property_notify)
5213 GtkTreeSelection *selection;
5214 GtkSelectionMode mode;
5216 if (select_multiple == impl->select_multiple)
5219 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5221 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5222 gtk_tree_selection_set_mode (selection, mode);
5224 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5226 impl->select_multiple = select_multiple;
5227 g_object_notify (G_OBJECT (impl), "select-multiple");
5229 check_preview_change (impl);
5233 set_file_system_backend (GtkFileChooserDefault *impl)
5235 profile_start ("start for backend", "default");
5237 impl->file_system = _gtk_file_system_new ();
5239 g_signal_connect (impl->file_system, "volumes-changed",
5240 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5241 g_signal_connect (impl->file_system, "bookmarks-changed",
5242 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5244 profile_end ("end", NULL);
5248 unset_file_system_backend (GtkFileChooserDefault *impl)
5250 g_signal_handlers_disconnect_by_func (impl->file_system,
5251 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5253 g_object_unref (impl->file_system);
5255 impl->file_system = NULL;
5258 /* This function is basically a do_all function.
5260 * It sets the visibility on all the widgets based on the current state, and
5261 * moves the custom_widget if needed.
5264 update_appearance (GtkFileChooserDefault *impl)
5266 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5267 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5271 gtk_widget_hide (impl->location_button);
5272 save_widgets_create (impl);
5274 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5275 text = _("Save in _folder:");
5277 text = _("Create in _folder:");
5279 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5281 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5283 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5284 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5285 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5286 gtk_widget_show (impl->browse_widgets);
5290 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5291 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5292 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5293 gtk_widget_hide (impl->browse_widgets);
5296 if (impl->select_multiple)
5298 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5299 "Re-setting to single selection mode.");
5300 set_select_multiple (impl, FALSE, TRUE);
5303 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5304 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5306 gtk_widget_show (impl->location_button);
5307 save_widgets_destroy (impl);
5308 gtk_widget_show (impl->browse_widgets);
5309 location_mode_set (impl, impl->location_mode, TRUE);
5312 if (impl->location_entry)
5313 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5315 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5316 gtk_widget_hide (impl->browse_new_folder_button);
5318 gtk_widget_show (impl->browse_new_folder_button);
5320 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5321 * of files may change depending whether we are in a file or folder-only mode.
5323 gtk_widget_queue_draw (impl->browse_files_tree_view);
5325 emit_default_size_changed (impl);
5329 gtk_file_chooser_default_set_property (GObject *object,
5331 const GValue *value,
5335 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5339 case GTK_FILE_CHOOSER_PROP_ACTION:
5341 GtkFileChooserAction action = g_value_get_enum (value);
5343 if (action != impl->action)
5345 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5347 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5348 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5349 && impl->select_multiple)
5351 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5352 "this is not allowed in multiple selection mode. Resetting the file chooser "
5353 "to single selection mode.");
5354 set_select_multiple (impl, FALSE, TRUE);
5356 impl->action = action;
5357 update_cell_renderer_attributes (impl);
5358 update_appearance (impl);
5359 settings_load (impl);
5364 case GTK_FILE_CHOOSER_PROP_FILTER:
5365 set_current_filter (impl, g_value_get_object (value));
5368 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5369 set_local_only (impl, g_value_get_boolean (value));
5372 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5373 set_preview_widget (impl, g_value_get_object (value));
5376 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5377 impl->preview_widget_active = g_value_get_boolean (value);
5378 update_preview_widget_visibility (impl);
5381 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5382 impl->use_preview_label = g_value_get_boolean (value);
5383 update_preview_widget_visibility (impl);
5386 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5387 set_extra_widget (impl, g_value_get_object (value));
5390 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5392 gboolean select_multiple = g_value_get_boolean (value);
5393 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5394 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5397 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5398 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5399 "leaving the file chooser in single selection mode.");
5403 set_select_multiple (impl, select_multiple, FALSE);
5407 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5409 gboolean show_hidden = g_value_get_boolean (value);
5410 if (show_hidden != impl->show_hidden)
5412 impl->show_hidden = show_hidden;
5414 if (impl->browse_files_model)
5415 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5420 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5422 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5423 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5427 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5429 gboolean create_folders = g_value_get_boolean (value);
5430 impl->create_folders = create_folders;
5431 update_appearance (impl);
5436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5442 gtk_file_chooser_default_get_property (GObject *object,
5447 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5451 case GTK_FILE_CHOOSER_PROP_ACTION:
5452 g_value_set_enum (value, impl->action);
5455 case GTK_FILE_CHOOSER_PROP_FILTER:
5456 g_value_set_object (value, impl->current_filter);
5459 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5460 g_value_set_boolean (value, impl->local_only);
5463 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5464 g_value_set_object (value, impl->preview_widget);
5467 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5468 g_value_set_boolean (value, impl->preview_widget_active);
5471 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5472 g_value_set_boolean (value, impl->use_preview_label);
5475 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5476 g_value_set_object (value, impl->extra_widget);
5479 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5480 g_value_set_boolean (value, impl->select_multiple);
5483 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5484 g_value_set_boolean (value, impl->show_hidden);
5487 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5488 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5491 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5492 g_value_set_boolean (value, impl->create_folders);
5496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5501 /* Removes the settings signal handler. It's safe to call multiple times */
5503 remove_settings_signal (GtkFileChooserDefault *impl,
5506 if (impl->settings_signal_id)
5508 GtkSettings *settings;
5510 settings = gtk_settings_get_for_screen (screen);
5511 g_signal_handler_disconnect (settings,
5512 impl->settings_signal_id);
5513 impl->settings_signal_id = 0;
5518 gtk_file_chooser_default_dispose (GObject *object)
5521 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5523 if (impl->extra_widget)
5525 g_object_unref (impl->extra_widget);
5526 impl->extra_widget = NULL;
5529 pending_select_files_free (impl);
5531 /* cancel all pending operations */
5532 if (impl->pending_cancellables)
5534 for (l = impl->pending_cancellables; l; l = l->next)
5536 GCancellable *cancellable = G_CANCELLABLE (l->data);
5537 g_cancellable_cancel (cancellable);
5539 g_slist_free (impl->pending_cancellables);
5540 impl->pending_cancellables = NULL;
5543 if (impl->reload_icon_cancellables)
5545 for (l = impl->reload_icon_cancellables; l; l = l->next)
5547 GCancellable *cancellable = G_CANCELLABLE (l->data);
5548 g_cancellable_cancel (cancellable);
5550 g_slist_free (impl->reload_icon_cancellables);
5551 impl->reload_icon_cancellables = NULL;
5554 if (impl->loading_shortcuts)
5556 for (l = impl->loading_shortcuts; l; l = l->next)
5558 GCancellable *cancellable = G_CANCELLABLE (l->data);
5559 g_cancellable_cancel (cancellable);
5561 g_slist_free (impl->loading_shortcuts);
5562 impl->loading_shortcuts = NULL;
5565 if (impl->file_list_drag_data_received_cancellable)
5567 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5568 impl->file_list_drag_data_received_cancellable = NULL;
5571 if (impl->update_current_folder_cancellable)
5573 g_cancellable_cancel (impl->update_current_folder_cancellable);
5574 impl->update_current_folder_cancellable = NULL;
5577 if (impl->should_respond_get_info_cancellable)
5579 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5580 impl->should_respond_get_info_cancellable = NULL;
5583 if (impl->update_from_entry_cancellable)
5585 g_cancellable_cancel (impl->update_from_entry_cancellable);
5586 impl->update_from_entry_cancellable = NULL;
5589 if (impl->shortcuts_activate_iter_cancellable)
5591 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5592 impl->shortcuts_activate_iter_cancellable = NULL;
5595 search_stop_searching (impl, TRUE);
5596 recent_stop_loading (impl);
5598 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5600 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5603 /* We override show-all since we have internal widgets that
5604 * shouldn't be shown when you call show_all(), like the filter
5608 gtk_file_chooser_default_show_all (GtkWidget *widget)
5610 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5612 gtk_widget_show (widget);
5614 if (impl->extra_widget)
5615 gtk_widget_show_all (impl->extra_widget);
5618 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5619 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5622 toplevel_set_focus_cb (GtkWindow *window,
5624 GtkFileChooserDefault *impl)
5626 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5629 /* We monitor the focus widget on our toplevel to be able to know which widget
5630 * was last focused at the time our "should_respond" method gets called.
5633 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5634 GtkWidget *previous_toplevel)
5636 GtkFileChooserDefault *impl;
5637 GtkWidget *toplevel;
5639 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5640 toplevel = gtk_widget_get_toplevel (widget);
5642 if (previous_toplevel &&
5643 impl->toplevel_set_focus_id != 0)
5645 g_signal_handler_disconnect (previous_toplevel,
5646 impl->toplevel_set_focus_id);
5647 impl->toplevel_set_focus_id = 0;
5648 impl->toplevel_last_focus_widget = NULL;
5651 if (gtk_widget_is_toplevel (toplevel))
5653 g_assert (impl->toplevel_set_focus_id == 0);
5654 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5655 G_CALLBACK (toplevel_set_focus_cb), impl);
5656 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5660 /* Changes the icons wherever it is needed */
5662 change_icon_theme (GtkFileChooserDefault *impl)
5664 GtkSettings *settings;
5666 GtkCellRenderer *renderer;
5669 profile_start ("start", NULL);
5671 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5673 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5674 impl->icon_size = MAX (width, height);
5676 impl->icon_size = FALLBACK_ICON_SIZE;
5678 shortcuts_reload_icons (impl);
5679 /* the first cell in the first column is the icon column, and we have a fixed size there */
5680 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5681 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5682 renderer = GTK_CELL_RENDERER (cells->data);
5683 set_icon_cell_renderer_fixed_size (impl, renderer);
5684 g_list_free (cells);
5685 if (impl->browse_files_model)
5686 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5687 gtk_widget_queue_resize (impl->browse_files_tree_view);
5689 profile_end ("end", NULL);
5692 /* Callback used when a GtkSettings value changes */
5694 settings_notify_cb (GObject *object,
5696 GtkFileChooserDefault *impl)
5700 profile_start ("start", NULL);
5702 name = g_param_spec_get_name (pspec);
5704 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5705 strcmp (name, "gtk-icon-sizes") == 0)
5706 change_icon_theme (impl);
5708 profile_end ("end", NULL);
5711 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5715 check_icon_theme (GtkFileChooserDefault *impl)
5717 GtkSettings *settings;
5719 profile_start ("start", NULL);
5721 if (impl->settings_signal_id)
5723 profile_end ("end", NULL);
5727 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5729 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5730 impl->settings_signal_id = g_signal_connect (settings, "notify",
5731 G_CALLBACK (settings_notify_cb), impl);
5733 change_icon_theme (impl);
5736 profile_end ("end", NULL);
5740 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5742 GtkFileChooserDefault *impl;
5744 profile_start ("start", NULL);
5746 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5748 profile_msg (" parent class style_udpated start", NULL);
5749 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5750 profile_msg (" parent class style_updated end", NULL);
5752 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5753 change_icon_theme (impl);
5755 emit_default_size_changed (impl);
5757 profile_end ("end", NULL);
5761 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5762 GdkScreen *previous_screen)
5764 GtkFileChooserDefault *impl;
5766 profile_start ("start", NULL);
5768 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5770 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5771 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5773 remove_settings_signal (impl, previous_screen);
5774 check_icon_theme (impl);
5776 emit_default_size_changed (impl);
5778 profile_end ("end", NULL);
5782 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5783 GtkAllocation *allocation)
5785 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5789 set_sort_column (GtkFileChooserDefault *impl)
5791 GtkTreeSortable *sortable;
5793 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5795 /* can happen when we're still populating the model */
5796 if (sortable == NULL)
5799 gtk_tree_sortable_set_sort_column_id (sortable,
5805 settings_ensure (GtkFileChooserDefault *impl)
5807 if (impl->settings != NULL)
5810 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5811 "/org/gtk/settings/file-chooser/");
5812 g_settings_delay (impl->settings);
5816 settings_load (GtkFileChooserDefault *impl)
5818 LocationMode location_mode;
5819 gboolean show_hidden;
5820 gboolean expand_folders;
5821 gboolean show_size_column;
5823 GtkSortType sort_order;
5825 settings_ensure (impl);
5827 expand_folders = g_settings_get_boolean (impl->settings, SETTINGS_KEY_EXPAND_FOLDERS);
5828 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5829 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5830 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5831 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5832 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5834 location_mode_set (impl, location_mode, TRUE);
5836 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5838 impl->expand_folders = expand_folders;
5839 if (impl->save_expander)
5840 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5842 impl->show_size_column = show_size_column;
5843 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5845 impl->sort_column = sort_column;
5846 impl->sort_order = sort_order;
5847 /* We don't call set_sort_column() here as the models may not have been
5848 * created yet. The individual functions that create and set the models will
5849 * call set_sort_column() themselves.
5854 save_dialog_geometry (GtkFileChooserDefault *impl)
5856 GtkWindow *toplevel;
5857 int x, y, width, height;
5859 /* We don't save the geometry in non-expanded "save" mode, so that the "little
5860 * dialog" won't make future Open dialogs too small.
5862 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5863 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5864 || impl->expand_folders))
5867 toplevel = get_toplevel (GTK_WIDGET (impl));
5869 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5872 gtk_window_get_position (toplevel, &x, &y);
5873 gtk_window_get_size (toplevel, &width, &height);
5875 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5876 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5880 settings_save (GtkFileChooserDefault *impl)
5882 char *current_folder_uri;
5884 settings_ensure (impl);
5886 /* Current folder */
5888 if (impl->current_folder)
5889 current_folder_uri = g_file_get_uri (impl->current_folder);
5891 current_folder_uri = "";
5893 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5895 if (impl->current_folder)
5896 g_free (current_folder_uri);
5898 /* All the other state */
5900 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5901 g_settings_set_boolean (impl->settings, SETTINGS_KEY_EXPAND_FOLDERS, impl->expand_folders);
5902 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5903 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5904 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5905 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5906 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5908 save_dialog_geometry (impl);
5910 /* Now apply the settings */
5911 g_settings_apply (impl->settings);
5913 g_object_unref (impl->settings);
5914 impl->settings = NULL;
5917 /* GtkWidget::realize method */
5919 gtk_file_chooser_default_realize (GtkWidget *widget)
5921 GtkFileChooserDefault *impl;
5923 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5925 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5927 emit_default_size_changed (impl);
5931 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5933 char *last_folder_uri;
5936 settings_ensure (impl);
5938 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5940 /* If no last folder is set, we use the user's home directory, since
5941 * this is the starting point for most documents.
5943 if (last_folder_uri[0] == '\0')
5944 file = g_file_new_for_path (g_get_home_dir ());
5946 file = g_file_new_for_uri (last_folder_uri);
5948 g_free (last_folder_uri);
5953 /* GtkWidget::map method */
5955 gtk_file_chooser_default_map (GtkWidget *widget)
5957 GtkFileChooserDefault *impl;
5959 profile_start ("start", NULL);
5961 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5963 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5965 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5969 switch (impl->reload_state)
5972 /* The user didn't explicitly give us a folder to display, so we'll
5973 * use the saved one from the last invocation of the file chooser
5975 folder = get_file_for_last_folder_opened (impl);
5976 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), folder, NULL);
5977 g_object_unref (folder);
5980 case RELOAD_HAS_FOLDER:
5981 /* Nothing; we are already loading or loaded, so we
5982 * don't need to reload
5987 g_assert_not_reached ();
5991 volumes_bookmarks_changed_cb (impl->file_system, impl);
5993 settings_load (impl);
5995 profile_end ("end", NULL);
5998 /* GtkWidget::unmap method */
6000 gtk_file_chooser_default_unmap (GtkWidget *widget)
6002 GtkFileChooserDefault *impl;
6004 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6006 settings_save (impl);
6008 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6012 install_list_model_filter (GtkFileChooserDefault *impl)
6014 _gtk_file_system_model_set_filter (impl->browse_files_model,
6015 impl->current_filter);
6018 #define COMPARE_DIRECTORIES \
6019 GtkFileChooserDefault *impl = user_data; \
6020 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6021 gboolean dir_a, dir_b; \
6023 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6024 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6026 if (dir_a != dir_b) \
6027 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6029 /* Sort callback for the filename column */
6031 name_sort_func (GtkTreeModel *model,
6036 COMPARE_DIRECTORIES;
6039 const char *key_a, *key_b;
6042 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6043 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6046 result = strcmp (key_a, key_b);
6058 /* Sort callback for the size column */
6060 size_sort_func (GtkTreeModel *model,
6065 COMPARE_DIRECTORIES;
6068 gint64 size_a, size_b;
6070 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6071 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6073 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6077 /* Sort callback for the mtime column */
6079 mtime_sort_func (GtkTreeModel *model,
6084 COMPARE_DIRECTORIES;
6089 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6090 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6092 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6096 /* Callback used when the sort column changes. We cache the sort order for use
6097 * in name_sort_func().
6100 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6101 GtkFileChooserDefault *impl)
6103 gint sort_column_id;
6104 GtkSortType sort_type;
6106 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6108 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6109 impl->sort_column = sort_column_id;
6110 impl->sort_order = sort_type;
6115 set_busy_cursor (GtkFileChooserDefault *impl,
6119 GtkWindow *toplevel;
6120 GdkDisplay *display;
6123 toplevel = get_toplevel (GTK_WIDGET (impl));
6124 widget = GTK_WIDGET (toplevel);
6125 if (!toplevel || !gtk_widget_get_realized (widget))
6128 display = gtk_widget_get_display (widget);
6131 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6135 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6136 gdk_display_flush (display);
6139 g_object_unref (cursor);
6142 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6144 load_set_model (GtkFileChooserDefault *impl)
6146 profile_start ("start", NULL);
6148 g_assert (impl->browse_files_model != NULL);
6150 profile_msg (" gtk_tree_view_set_model start", NULL);
6151 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6152 GTK_TREE_MODEL (impl->browse_files_model));
6153 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6154 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6156 set_sort_column (impl);
6157 profile_msg (" gtk_tree_view_set_model end", NULL);
6158 impl->list_sort_ascending = TRUE;
6160 profile_end ("end", NULL);
6163 /* Timeout callback used when the loading timer expires */
6165 load_timeout_cb (gpointer data)
6167 GtkFileChooserDefault *impl;
6169 profile_start ("start", NULL);
6171 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6172 g_assert (impl->load_state == LOAD_PRELOAD);
6173 g_assert (impl->load_timeout_id != 0);
6174 g_assert (impl->browse_files_model != NULL);
6176 impl->load_timeout_id = 0;
6177 impl->load_state = LOAD_LOADING;
6179 load_set_model (impl);
6181 profile_end ("end", NULL);
6186 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6188 load_setup_timer (GtkFileChooserDefault *impl)
6190 g_assert (impl->load_timeout_id == 0);
6191 g_assert (impl->load_state != LOAD_PRELOAD);
6193 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6194 impl->load_state = LOAD_PRELOAD;
6197 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6199 load_remove_timer (GtkFileChooserDefault *impl)
6201 if (impl->load_timeout_id != 0)
6203 g_assert (impl->load_state == LOAD_PRELOAD);
6205 g_source_remove (impl->load_timeout_id);
6206 impl->load_timeout_id = 0;
6207 impl->load_state = LOAD_EMPTY;
6210 g_assert (impl->load_state == LOAD_EMPTY ||
6211 impl->load_state == LOAD_LOADING ||
6212 impl->load_state == LOAD_FINISHED);
6215 /* Selects the first row in the file list */
6217 browse_files_select_first_row (GtkFileChooserDefault *impl)
6220 GtkTreeIter dummy_iter;
6221 GtkTreeModel *tree_model;
6223 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6228 path = gtk_tree_path_new_from_indices (0, -1);
6230 /* If the list is empty, do nothing. */
6231 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6232 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6234 gtk_tree_path_free (path);
6237 struct center_selected_row_closure {
6238 GtkFileChooserDefault *impl;
6239 gboolean already_centered;
6242 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6243 * selected row in the tree view.
6246 center_selected_row_foreach_cb (GtkTreeModel *model,
6251 struct center_selected_row_closure *closure;
6254 if (closure->already_centered)
6257 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6258 closure->already_centered = TRUE;
6261 /* Centers the selected row in the tree view */
6263 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6265 struct center_selected_row_closure closure;
6266 GtkTreeSelection *selection;
6268 closure.impl = impl;
6269 closure.already_centered = FALSE;
6271 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6272 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6276 show_and_select_files (GtkFileChooserDefault *impl,
6279 GtkTreeSelection *selection;
6280 GtkFileSystemModel *fsmodel;
6281 gboolean enabled_hidden, removed_filters;
6282 gboolean selected_a_file;
6285 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6286 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6288 enabled_hidden = impl->show_hidden;
6289 removed_filters = (impl->current_filter == NULL);
6291 selected_a_file = FALSE;
6293 for (walk = files; walk; walk = walk->next)
6295 GFile *file = walk->data;
6298 /* Is it a hidden file? */
6300 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6303 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6305 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6307 if (!enabled_hidden &&
6308 (g_file_info_get_is_hidden (info) ||
6309 g_file_info_get_is_backup (info)))
6311 g_object_set (impl, "show-hidden", TRUE, NULL);
6312 enabled_hidden = TRUE;
6316 /* Is it a filtered file? */
6318 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6319 continue; /* re-get the iter as it may change when the model refilters */
6321 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6323 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6324 if (!removed_filters)
6326 set_current_filter (impl, NULL);
6327 removed_filters = TRUE;
6331 /* Okay, can we select the file now? */
6333 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6336 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6340 gtk_tree_selection_select_iter (selection, &iter);
6342 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6343 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6345 gtk_tree_path_free (path);
6347 selected_a_file = TRUE;
6351 browse_files_center_selected_row (impl);
6353 return selected_a_file;
6356 /* Processes the pending operation when a folder is finished loading */
6358 pending_select_files_process (GtkFileChooserDefault *impl)
6360 g_assert (impl->load_state == LOAD_FINISHED);
6361 g_assert (impl->browse_files_model != NULL);
6363 if (impl->pending_select_files)
6365 show_and_select_files (impl, impl->pending_select_files);
6366 pending_select_files_free (impl);
6367 browse_files_center_selected_row (impl);
6371 /* We only select the first row if the chooser is actually mapped ---
6372 * selecting the first row is to help the user when he is interacting with
6373 * the chooser, but sometimes a chooser works not on behalf of the user,
6374 * but rather on behalf of something else like GtkFileChooserButton. In
6375 * that case, the chooser's selection should be what the caller expects,
6376 * as the user can't see that something else got selected. See bug #165264.
6378 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6379 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6380 browse_files_select_first_row (impl);
6383 g_assert (impl->pending_select_files == NULL);
6387 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6392 info = g_file_query_info (impl->current_folder,
6393 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6394 G_FILE_QUERY_INFO_NONE,
6399 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6400 g_object_unref (info);
6403 msg = g_strdup (_("Could not read the contents of the folder"));
6405 error_message (impl, msg, error->message);
6409 /* Callback used when the file system model finishes loading */
6411 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6413 GtkFileChooserDefault *impl)
6415 profile_start ("start", NULL);
6418 show_error_on_reading_current_folder (impl, error);
6420 if (impl->load_state == LOAD_PRELOAD)
6422 load_remove_timer (impl);
6423 load_set_model (impl);
6425 else if (impl->load_state == LOAD_LOADING)
6431 /* We can't g_assert_not_reached(), as something other than us may have
6432 * initiated a folder reload. See #165556.
6434 profile_end ("end", NULL);
6438 g_assert (impl->load_timeout_id == 0);
6440 impl->load_state = LOAD_FINISHED;
6442 pending_select_files_process (impl);
6443 set_busy_cursor (impl, FALSE);
6444 #ifdef PROFILE_FILE_CHOOSER
6445 access ("MARK: *** FINISHED LOADING", F_OK);
6448 profile_end ("end", NULL);
6452 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6453 gboolean remove_from_treeview)
6455 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6457 if (impl->browse_files_model)
6459 g_object_unref (impl->browse_files_model);
6460 impl->browse_files_model = NULL;
6463 if (remove_from_treeview)
6464 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6468 my_g_format_time_for_display (glong secs)
6473 time_t time_mtime, time_now;
6474 const gchar *format;
6475 gchar *locale_format = NULL;
6477 char *date_str = NULL;
6479 const char *locale, *dot = NULL;
6480 gint64 codepage = -1;
6486 #ifdef HAVE_LOCALTIME_R
6487 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6490 struct tm *ptm = localtime ((time_t *) &time_mtime);
6494 g_warning ("ptm != NULL failed");
6496 return g_strdup (_("Unknown"));
6499 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6501 #endif /* HAVE_LOCALTIME_R */
6503 g_date_set_time_t (&mtime, time_mtime);
6504 time_now = time (NULL);
6505 g_date_set_time_t (&now, time_now);
6507 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6509 /* Translators: %H means "hours" and %M means "minutes" */
6511 format = _("%H:%M");
6512 else if (days_diff == 1)
6513 format = _("Yesterday at %H:%M");
6516 if (days_diff > 1 && days_diff < 7)
6517 format = "%A"; /* Days from last week */
6519 format = "%x"; /* Any other date */
6523 /* g_locale_from_utf8() returns a string in the system
6524 * code-page, which is not always the same as that used by the C
6525 * library. For instance when running a GTK+ program with
6526 * LANG=ko on an English version of Windows, the system
6527 * code-page is 1252, but the code-page used by the C library is
6528 * 949. (It's GTK+ itself that sets the C library locale when it
6529 * notices the LANG environment variable. See gtkmain.c The
6530 * Microsoft C library doesn't look at any locale environment
6531 * variables.) We need to pass strftime() a string in the C
6532 * library's code-page. See bug #509885.
6534 locale = setlocale (LC_ALL, NULL);
6536 dot = strchr (locale, '.');
6539 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6541 /* All codepages should fit in 16 bits AFAIK */
6542 if (codepage > 0 && codepage < 65536)
6544 sprintf (charset, "CP%u", (guint) codepage);
6545 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6549 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6551 if (locale_format != NULL &&
6552 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6555 /* As above but in opposite direction... */
6556 if (codepage > 0 && codepage < 65536)
6557 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6559 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6563 if (date_str == NULL)
6564 date_str = g_strdup (_("Unknown"));
6566 g_free (locale_format);
6571 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6573 GFileAttributeType type;
6576 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6577 g_file_info_set_attribute (to, attribute, type, value);
6581 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6583 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6584 GFile *file = G_FILE (object);
6585 GFileInfo *queried, *info;
6588 queried = g_file_query_info_finish (file, res, NULL);
6589 if (queried == NULL)
6592 GDK_THREADS_ENTER ();
6594 /* now we know model is valid */
6596 /* file was deleted */
6597 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6599 GDK_THREADS_LEAVE ();
6603 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6605 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6606 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6607 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6609 _gtk_file_system_model_update_file (model, file, info, FALSE);
6611 g_object_unref (info);
6613 GDK_THREADS_LEAVE ();
6617 file_system_model_set (GtkFileSystemModel *model,
6624 GtkFileChooserDefault *impl = data;
6628 case MODEL_COL_FILE:
6629 g_value_set_object (value, file);
6631 case MODEL_COL_NAME:
6633 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6635 g_value_set_string (value, g_file_info_get_display_name (info));
6637 case MODEL_COL_NAME_COLLATED:
6639 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6641 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6643 case MODEL_COL_IS_FOLDER:
6644 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6646 case MODEL_COL_PIXBUF:
6649 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6651 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6655 GtkTreeModel *tree_model;
6656 GtkTreePath *path, *start, *end;
6659 if (impl->browse_files_tree_view == NULL ||
6660 g_file_info_has_attribute (info, "filechooser::queried"))
6663 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6664 if (tree_model != GTK_TREE_MODEL (model))
6667 if (!_gtk_file_system_model_get_iter_for_file (model,
6670 g_assert_not_reached ();
6671 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6673 path = gtk_tree_model_get_path (tree_model, &iter);
6674 if (gtk_tree_path_compare (start, path) != 1 &&
6675 gtk_tree_path_compare (path, end) != 1)
6677 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6678 g_file_query_info_async (file,
6679 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6680 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6681 G_FILE_ATTRIBUTE_STANDARD_ICON,
6682 G_FILE_QUERY_INFO_NONE,
6684 _gtk_file_system_model_get_cancellable (model),
6685 file_system_model_got_thumbnail,
6688 gtk_tree_path_free (path);
6689 gtk_tree_path_free (start);
6690 gtk_tree_path_free (end);
6695 g_value_set_object (value, NULL);
6697 case MODEL_COL_SIZE:
6698 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6700 case MODEL_COL_SIZE_TEXT:
6701 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6702 g_value_set_string (value, NULL);
6704 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6706 case MODEL_COL_MTIME:
6707 case MODEL_COL_MTIME_TEXT:
6712 g_file_info_get_modification_time (info, &tv);
6713 if (column == MODEL_COL_MTIME)
6714 g_value_set_long (value, tv.tv_sec);
6715 else if (tv.tv_sec == 0)
6716 g_value_set_static_string (value, _("Unknown"));
6718 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6721 case MODEL_COL_ELLIPSIZE:
6722 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6725 g_assert_not_reached ();
6732 /* Gets rid of the old list model and creates a new one for the current folder */
6734 set_list_model (GtkFileChooserDefault *impl,
6737 g_assert (impl->current_folder != NULL);
6739 profile_start ("start", NULL);
6741 stop_loading_and_clear_list_model (impl, TRUE);
6743 set_busy_cursor (impl, TRUE);
6745 impl->browse_files_model =
6746 _gtk_file_system_model_new_for_directory (impl->current_folder,
6748 file_system_model_set,
6750 MODEL_COLUMN_TYPES);
6752 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6754 profile_msg (" set sort function", NULL);
6755 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6756 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6757 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6758 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6759 set_sort_column (impl);
6760 impl->list_sort_ascending = TRUE;
6761 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6762 G_CALLBACK (list_sort_column_changed_cb), impl);
6764 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6766 g_signal_connect (impl->browse_files_model, "finished-loading",
6767 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6769 install_list_model_filter (impl);
6771 profile_end ("end", NULL);
6776 struct update_chooser_entry_selected_foreach_closure {
6778 GtkTreeIter first_selected_iter;
6782 compare_utf8_filenames (const gchar *a,
6785 gchar *a_folded, *b_folded;
6788 a_folded = g_utf8_strdown (a, -1);
6789 b_folded = g_utf8_strdown (b, -1);
6791 retval = strcmp (a_folded, b_folded);
6800 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6805 struct update_chooser_entry_selected_foreach_closure *closure;
6808 closure->num_selected++;
6810 if (closure->num_selected == 1)
6811 closure->first_selected_iter = *iter;
6815 update_chooser_entry (GtkFileChooserDefault *impl)
6817 GtkTreeSelection *selection;
6818 struct update_chooser_entry_selected_foreach_closure closure;
6820 /* no need to update the file chooser's entry if there's no entry */
6821 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6822 impl->operation_mode == OPERATION_MODE_RECENT ||
6823 !impl->location_entry)
6826 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6827 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6828 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6829 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6830 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6833 g_assert (impl->location_entry != NULL);
6835 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6836 closure.num_selected = 0;
6837 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6839 if (closure.num_selected == 0)
6841 goto maybe_clear_entry;
6843 else if (closure.num_selected == 1)
6845 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6848 gboolean change_entry;
6850 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6852 /* If the cursor moved to the row of the newly created folder,
6853 * retrieving info will return NULL.
6858 g_free (impl->browse_files_last_selected_name);
6859 impl->browse_files_last_selected_name =
6860 g_strdup (g_file_info_get_display_name (info));
6862 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6863 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6864 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6866 /* Don't change the name when clicking on a folder... */
6867 change_entry = (! _gtk_file_info_consider_as_directory (info));
6870 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6874 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6876 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6877 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6885 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6886 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6888 /* Multiple selection, so just clear the entry. */
6889 g_free (impl->browse_files_last_selected_name);
6890 impl->browse_files_last_selected_name = NULL;
6892 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6898 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6899 && impl->browse_files_last_selected_name)
6901 const char *entry_text;
6903 gboolean clear_entry;
6905 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6906 len = strlen (entry_text);
6909 /* The file chooser entry may have appended a "/" to its text.
6910 * So take it out, and compare the result to the old selection.
6912 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6916 tmp = g_strndup (entry_text, len - 1);
6917 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6921 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6924 clear_entry = FALSE;
6927 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6932 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6936 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6940 struct UpdateCurrentFolderData
6942 GtkFileChooserDefault *impl;
6944 gboolean keep_trail;
6945 gboolean clear_entry;
6946 GFile *original_file;
6947 GError *original_error;
6951 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6952 GtkFileSystemVolume *volume,
6953 const GError *error,
6956 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6957 struct UpdateCurrentFolderData *data = user_data;
6958 GtkFileChooserDefault *impl = data->impl;
6960 if (cancellable != impl->update_current_folder_cancellable)
6963 impl->update_current_folder_cancellable = NULL;
6964 set_busy_cursor (impl, FALSE);
6971 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6972 impl->reload_state = RELOAD_EMPTY;
6976 change_folder_and_display_error (impl, data->file, data->clear_entry);
6979 g_object_unref (data->file);
6982 g_object_unref (cancellable);
6986 update_current_folder_get_info_cb (GCancellable *cancellable,
6988 const GError *error,
6991 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6992 struct UpdateCurrentFolderData *data = user_data;
6993 GtkFileChooserDefault *impl = data->impl;
6995 if (cancellable != impl->update_current_folder_cancellable)
6998 impl->update_current_folder_cancellable = NULL;
6999 impl->reload_state = RELOAD_EMPTY;
7001 set_busy_cursor (impl, FALSE);
7010 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7012 GMountOperation *mount_operation;
7013 GtkWidget *toplevel;
7015 g_object_unref (cancellable);
7016 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7018 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7020 set_busy_cursor (impl, TRUE);
7022 impl->update_current_folder_cancellable =
7023 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7025 update_current_folder_mount_enclosing_volume_cb,
7031 if (!data->original_file)
7033 data->original_file = g_object_ref (data->file);
7034 data->original_error = g_error_copy (error);
7037 parent_file = g_file_get_parent (data->file);
7039 /* get parent path and try to change the folder to that */
7042 g_object_unref (data->file);
7043 data->file = parent_file;
7045 g_object_unref (cancellable);
7047 /* restart the update current folder operation */
7048 impl->reload_state = RELOAD_HAS_FOLDER;
7050 impl->update_current_folder_cancellable =
7051 _gtk_file_system_get_info (impl->file_system, data->file,
7053 update_current_folder_get_info_cb,
7056 set_busy_cursor (impl, TRUE);
7062 /* Error and bail out, ignoring "not found" errors since they're useless:
7063 * they only happen when a program defaults to a folder that has been (re)moved.
7065 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7066 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7068 g_error_free (data->original_error);
7070 g_object_unref (data->original_file);
7076 if (data->original_file)
7078 /* Error and bail out, ignoring "not found" errors since they're useless:
7079 * they only happen when a program defaults to a folder that has been (re)moved.
7081 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7082 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7084 g_error_free (data->original_error);
7086 g_object_unref (data->original_file);
7089 if (! _gtk_file_info_consider_as_directory (info))
7092 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7095 if (impl->current_folder != data->file)
7097 if (impl->current_folder)
7098 g_object_unref (impl->current_folder);
7100 impl->current_folder = g_object_ref (data->file);
7103 impl->reload_state = RELOAD_HAS_FOLDER;
7105 /* Update the widgets that may trigger a folder change themselves. */
7107 if (!impl->changing_folder)
7109 impl->changing_folder = TRUE;
7111 shortcuts_update_current_folder (impl);
7113 impl->changing_folder = FALSE;
7116 /* Set the folder on the save entry */
7118 if (impl->location_entry)
7120 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7121 impl->current_folder);
7123 if (data->clear_entry)
7124 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7127 /* Create a new list model. This is slightly evil; we store the result value
7128 * but perform more actions rather than returning immediately even if it
7129 * generates an error.
7131 set_list_model (impl, NULL);
7133 /* Refresh controls */
7135 shortcuts_find_current_folder (impl);
7137 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7139 check_preview_change (impl);
7140 bookmarks_check_add_sensitivity (impl);
7142 g_signal_emit_by_name (impl, "selection-changed", 0);
7145 g_object_unref (data->file);
7148 g_object_unref (cancellable);
7152 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7154 gboolean keep_trail,
7155 gboolean clear_entry,
7158 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7159 struct UpdateCurrentFolderData *data;
7161 profile_start ("start", NULL);
7163 g_object_ref (file);
7165 switch (impl->operation_mode)
7167 case OPERATION_MODE_SEARCH:
7168 search_switch_to_browse_mode (impl);
7170 case OPERATION_MODE_RECENT:
7171 recent_switch_to_browse_mode (impl);
7173 case OPERATION_MODE_BROWSE:
7177 if (impl->local_only && !g_file_is_native (file))
7179 g_set_error_literal (error,
7180 GTK_FILE_CHOOSER_ERROR,
7181 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7182 _("Cannot change to folder because it is not local"));
7184 g_object_unref (file);
7185 profile_end ("end - not local", NULL);
7189 if (impl->update_current_folder_cancellable)
7190 g_cancellable_cancel (impl->update_current_folder_cancellable);
7192 /* Test validity of path here. */
7193 data = g_new0 (struct UpdateCurrentFolderData, 1);
7195 data->file = g_object_ref (file);
7196 data->keep_trail = keep_trail;
7197 data->clear_entry = clear_entry;
7199 impl->reload_state = RELOAD_HAS_FOLDER;
7201 impl->update_current_folder_cancellable =
7202 _gtk_file_system_get_info (impl->file_system, file,
7204 update_current_folder_get_info_cb,
7207 set_busy_cursor (impl, TRUE);
7208 g_object_unref (file);
7210 profile_end ("end", NULL);
7215 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7217 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7219 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7220 impl->operation_mode == OPERATION_MODE_RECENT)
7223 if (impl->reload_state == RELOAD_EMPTY)
7225 /* We are unmapped, or we had an error while loading the last folder.
7226 * We'll return the folder used by the last invocation of the file chooser
7227 * since once we get (re)mapped, we'll load *that* folder anyway unless
7228 * the caller explicitly calls set_current_folder() on us.
7230 return get_file_for_last_folder_opened (impl);
7233 if (impl->current_folder)
7234 return g_object_ref (impl->current_folder);
7240 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7243 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7245 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7246 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7248 pending_select_files_free (impl);
7249 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7253 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7257 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7261 parent_file = g_file_get_parent (file);
7264 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7266 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7267 impl->operation_mode == OPERATION_MODE_RECENT ||
7268 impl->load_state == LOAD_EMPTY)
7274 g_assert (impl->current_folder != NULL);
7276 same_path = g_file_equal (parent_file, impl->current_folder);
7279 if (same_path && impl->load_state == LOAD_FINISHED)
7284 files.data = (gpointer) file;
7287 result = show_and_select_files (impl, &files);
7288 g_object_unref (parent_file);
7292 pending_select_files_add (impl, file);
7298 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7299 g_object_unref (parent_file);
7303 g_object_unref (parent_file);
7308 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7311 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7312 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7315 if (!impl->browse_files_model)
7318 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7323 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7328 maybe_select (GtkTreeModel *model,
7333 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7334 GtkTreeSelection *selection;
7337 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7339 gtk_tree_model_get (model, iter,
7340 MODEL_COL_IS_FOLDER, &is_folder,
7343 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7344 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7345 gtk_tree_selection_select_iter (selection, iter);
7347 gtk_tree_selection_unselect_iter (selection, iter);
7353 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7355 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7357 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7358 impl->operation_mode == OPERATION_MODE_RECENT)
7360 GtkTreeSelection *selection;
7362 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7363 gtk_tree_selection_select_all (selection);
7367 if (impl->select_multiple)
7368 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7369 maybe_select, impl);
7373 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7375 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7376 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7378 gtk_tree_selection_unselect_all (selection);
7379 pending_select_files_free (impl);
7382 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7384 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7386 * is_empty_ret - whether the file entry is totally empty
7388 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7389 * the path will be "$cwd/foobar")
7392 check_save_entry (GtkFileChooserDefault *impl,
7394 gboolean *is_well_formed_ret,
7395 gboolean *is_empty_ret,
7396 gboolean *is_file_part_empty_ret,
7397 gboolean *is_folder)
7399 GtkFileChooserEntry *chooser_entry;
7400 GFile *current_folder;
7401 const char *file_part;
7405 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7406 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7407 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7408 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7409 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7411 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7413 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7416 *is_well_formed_ret = TRUE;
7417 *is_empty_ret = TRUE;
7418 *is_file_part_empty_ret = TRUE;
7424 *is_empty_ret = FALSE;
7426 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7427 if (!current_folder)
7430 *is_well_formed_ret = FALSE;
7431 *is_file_part_empty_ret = FALSE;
7437 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7439 if (!file_part || file_part[0] == '\0')
7441 *file_ret = g_object_ref (current_folder);
7442 *is_well_formed_ret = TRUE;
7443 *is_file_part_empty_ret = TRUE;
7449 *is_file_part_empty_ret = FALSE;
7452 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7456 error_building_filename_dialog (impl, error);
7458 *is_well_formed_ret = FALSE;
7465 *is_well_formed_ret = TRUE;
7466 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7469 struct get_files_closure {
7470 GtkFileChooserDefault *impl;
7472 GFile *file_from_entry;
7476 get_files_foreach (GtkTreeModel *model,
7481 struct get_files_closure *info;
7483 GtkFileSystemModel *fs_model;
7486 fs_model = info->impl->browse_files_model;
7488 file = _gtk_file_system_model_get_file (fs_model, iter);
7490 return; /* We are on the editable row */
7492 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7493 info->result = g_slist_prepend (info->result, g_object_ref (file));
7497 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7499 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7500 struct get_files_closure info;
7501 GtkWindow *toplevel;
7502 GtkWidget *current_focus;
7503 gboolean file_list_seen;
7505 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7506 return search_get_selected_files (impl);
7508 if (impl->operation_mode == OPERATION_MODE_RECENT)
7509 return recent_get_selected_files (impl);
7513 info.file_from_entry = NULL;
7515 toplevel = get_toplevel (GTK_WIDGET (impl));
7517 current_focus = gtk_window_get_focus (toplevel);
7519 current_focus = NULL;
7521 file_list_seen = FALSE;
7522 if (current_focus == impl->browse_files_tree_view)
7524 GtkTreeSelection *selection;
7528 file_list_seen = TRUE;
7529 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7530 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7532 /* If there is no selection in the file list, we probably have this situation:
7534 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7535 * 2. He then double-clicked on a folder ("bar") in the file list
7537 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7538 * filename entry to see if that is the case.
7540 if (info.result == NULL && impl->location_entry)
7543 else if (impl->location_entry && current_focus == impl->location_entry)
7545 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7549 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7554 if (!is_well_formed)
7557 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7559 g_object_unref (info.file_from_entry);
7563 if (info.file_from_entry)
7564 info.result = g_slist_prepend (info.result, info.file_from_entry);
7565 else if (!file_list_seen)
7570 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7572 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7576 /* The focus is on a dialog's action area button or something else */
7577 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7578 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7586 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7587 * fall back to the current directory */
7588 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7589 info.result == NULL)
7591 GFile *current_folder;
7593 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7596 info.result = g_slist_prepend (info.result, current_folder);
7599 return g_slist_reverse (info.result);
7603 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7605 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7607 if (impl->preview_file)
7608 return g_object_ref (impl->preview_file);
7613 static GtkFileSystem *
7614 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7616 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7618 return impl->file_system;
7621 /* Shows or hides the filter widgets */
7623 show_filters (GtkFileChooserDefault *impl,
7627 gtk_widget_show (impl->filter_combo_hbox);
7629 gtk_widget_hide (impl->filter_combo_hbox);
7633 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7634 GtkFileFilter *filter)
7636 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7639 if (g_slist_find (impl->filters, filter))
7641 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7645 g_object_ref_sink (filter);
7646 impl->filters = g_slist_append (impl->filters, filter);
7648 name = gtk_file_filter_get_name (filter);
7650 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7652 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7654 if (!g_slist_find (impl->filters, impl->current_filter))
7655 set_current_filter (impl, filter);
7657 show_filters (impl, TRUE);
7661 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7662 GtkFileFilter *filter)
7664 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7665 GtkTreeModel *model;
7669 filter_index = g_slist_index (impl->filters, filter);
7671 if (filter_index < 0)
7673 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7677 impl->filters = g_slist_remove (impl->filters, filter);
7679 if (filter == impl->current_filter)
7682 set_current_filter (impl, impl->filters->data);
7684 set_current_filter (impl, NULL);
7687 /* Remove row from the combo box */
7688 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7689 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7690 g_assert_not_reached ();
7692 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7694 g_object_unref (filter);
7697 show_filters (impl, FALSE);
7701 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7703 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7705 return g_slist_copy (impl->filters);
7708 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7710 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7713 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7716 struct AddShortcutData
7718 GtkFileChooserDefault *impl;
7723 add_shortcut_get_info_cb (GCancellable *cancellable,
7725 const GError *error,
7729 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7730 struct AddShortcutData *data = user_data;
7732 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7735 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7737 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7740 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7742 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7745 g_object_unref (data->impl);
7746 g_object_unref (data->file);
7749 g_object_unref (cancellable);
7753 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7757 GCancellable *cancellable;
7758 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7759 struct AddShortcutData *data;
7763 /* Avoid adding duplicates */
7764 pos = shortcut_find_position (impl, file);
7765 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7769 uri = g_file_get_uri (file);
7770 /* translators, "Shortcut" means "Bookmark" here */
7772 GTK_FILE_CHOOSER_ERROR,
7773 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7774 _("Shortcut %s already exists"),
7781 for (l = impl->loading_shortcuts; l; l = l->next)
7783 GCancellable *c = l->data;
7786 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7787 if (f && g_file_equal (file, f))
7791 uri = g_file_get_uri (file);
7793 GTK_FILE_CHOOSER_ERROR,
7794 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7795 _("Shortcut %s already exists"),
7803 data = g_new0 (struct AddShortcutData, 1);
7804 data->impl = g_object_ref (impl);
7805 data->file = g_object_ref (file);
7807 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7809 add_shortcut_get_info_cb, data);
7814 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7815 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7821 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7825 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7832 for (l = impl->loading_shortcuts; l; l = l->next)
7834 GCancellable *c = l->data;
7837 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7838 if (f && g_file_equal (file, f))
7840 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7841 g_cancellable_cancel (c);
7846 if (impl->num_shortcuts == 0)
7849 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7850 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7851 g_assert_not_reached ();
7853 for (i = 0; i < impl->num_shortcuts; i++)
7856 ShortcutType shortcut_type;
7859 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7860 SHORTCUTS_COL_DATA, &col_data,
7861 SHORTCUTS_COL_TYPE, &shortcut_type,
7863 g_assert (col_data != NULL);
7864 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7866 shortcut = col_data;
7867 if (g_file_equal (shortcut, file))
7869 shortcuts_remove_rows (impl, pos + i, 1);
7870 impl->num_shortcuts--;
7874 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7875 g_assert_not_reached ();
7880 uri = g_file_get_uri (file);
7881 /* translators, "Shortcut" means "Bookmark" here */
7883 GTK_FILE_CHOOSER_ERROR,
7884 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7885 _("Shortcut %s does not exist"),
7893 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7895 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7901 if (impl->num_shortcuts == 0)
7904 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7905 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7906 g_assert_not_reached ();
7910 for (i = 0; i < impl->num_shortcuts; i++)
7913 ShortcutType shortcut_type;
7916 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7917 SHORTCUTS_COL_DATA, &col_data,
7918 SHORTCUTS_COL_TYPE, &shortcut_type,
7920 g_assert (col_data != NULL);
7921 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7923 shortcut = col_data;
7924 list = g_slist_prepend (list, g_object_ref (shortcut));
7926 if (i != impl->num_shortcuts - 1)
7928 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7929 g_assert_not_reached ();
7933 return g_slist_reverse (list);
7936 /* Guesses a size based upon font sizes */
7938 find_good_size_from_style (GtkWidget *widget,
7942 GtkStyleContext *context;
7943 GtkStateFlags state;
7948 context = gtk_widget_get_style_context (widget);
7949 state = gtk_widget_get_state_flags (widget);
7951 screen = gtk_widget_get_screen (widget);
7954 resolution = gdk_screen_get_resolution (screen);
7955 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7959 resolution = 96.0; /* wheeee */
7961 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7962 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7964 *width = font_size * NUM_CHARS;
7965 *height = font_size * NUM_LINES;
7969 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7970 gint *default_width,
7971 gint *default_height)
7973 GtkFileChooserDefault *impl;
7976 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7978 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7979 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7980 || impl->expand_folders)
7982 int x, y, width, height;
7984 settings_ensure (impl);
7986 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7987 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7989 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7991 *default_width = width;
7992 *default_height = height;
7996 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7998 if (impl->preview_widget_active &&
7999 impl->preview_widget &&
8000 gtk_widget_get_visible (impl->preview_widget))
8002 gtk_widget_get_preferred_size (impl->preview_box,
8004 *default_width += PREVIEW_HBOX_SPACING + req.width;
8007 if (impl->extra_widget &&
8008 gtk_widget_get_visible (impl->extra_widget))
8010 gtk_widget_get_preferred_size (impl->extra_align, &req, NULL);
8011 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8016 gtk_widget_get_preferred_size (GTK_WIDGET (impl),
8018 *default_width = req.width;
8019 *default_height = req.height;
8023 struct switch_folder_closure {
8024 GtkFileChooserDefault *impl;
8029 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8031 switch_folder_foreach_cb (GtkTreeModel *model,
8036 struct switch_folder_closure *closure;
8040 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8041 closure->num_selected++;
8044 /* Changes to the selected folder in the list view */
8046 switch_to_selected_folder (GtkFileChooserDefault *impl)
8048 GtkTreeSelection *selection;
8049 struct switch_folder_closure closure;
8051 /* We do this with foreach() rather than get_selected() as we may be in
8052 * multiple selection mode
8055 closure.impl = impl;
8056 closure.file = NULL;
8057 closure.num_selected = 0;
8059 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8060 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8062 g_assert (closure.file && closure.num_selected == 1);
8064 change_folder_and_display_error (impl, closure.file, FALSE);
8067 /* Gets the GFileInfo for the selected row in the file list; assumes single
8071 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8072 gboolean *had_selection)
8074 GtkTreeSelection *selection;
8078 g_assert (!impl->select_multiple);
8079 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8080 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8082 *had_selection = FALSE;
8086 *had_selection = TRUE;
8088 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8092 /* Gets the display name of the selected file in the file list; assumes single
8093 * selection mode and that something is selected.
8095 static const gchar *
8096 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8099 gboolean had_selection;
8101 info = get_selected_file_info_from_file_list (impl, &had_selection);
8102 g_assert (had_selection);
8103 g_assert (info != NULL);
8105 return g_file_info_get_display_name (info);
8109 add_custom_button_to_dialog (GtkDialog *dialog,
8110 const gchar *mnemonic_label,
8111 const gchar *stock_id,
8116 button = gtk_button_new_with_mnemonic (mnemonic_label);
8117 gtk_widget_set_can_default (button, TRUE);
8118 gtk_button_set_image (GTK_BUTTON (button),
8119 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8120 gtk_widget_show (button);
8122 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8125 /* Presents an overwrite confirmation dialog; returns whether we should accept
8129 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8130 const gchar *file_part,
8131 const gchar *folder_display_name)
8133 GtkWindow *toplevel;
8137 toplevel = get_toplevel (GTK_WIDGET (impl));
8139 dialog = gtk_message_dialog_new (toplevel,
8140 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8141 GTK_MESSAGE_QUESTION,
8143 _("A file named \"%s\" already exists. Do you want to replace it?"),
8145 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8146 _("The file already exists in \"%s\". Replacing it will "
8147 "overwrite its contents."),
8148 folder_display_name);
8150 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8151 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8152 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8153 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8154 GTK_RESPONSE_ACCEPT,
8155 GTK_RESPONSE_CANCEL,
8157 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8159 if (gtk_window_has_group (toplevel))
8160 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8161 GTK_WINDOW (dialog));
8163 response = gtk_dialog_run (GTK_DIALOG (dialog));
8165 gtk_widget_destroy (dialog);
8167 return (response == GTK_RESPONSE_ACCEPT);
8170 struct GetDisplayNameData
8172 GtkFileChooserDefault *impl;
8177 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8179 const GError *error,
8182 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8183 gboolean should_respond = FALSE;
8184 struct GetDisplayNameData *data = user_data;
8186 if (cancellable != data->impl->should_respond_get_info_cancellable)
8189 data->impl->should_respond_get_info_cancellable = NULL;
8195 /* Huh? Did the folder disappear? Let the caller deal with it */
8196 should_respond = TRUE;
8198 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8200 set_busy_cursor (data->impl, FALSE);
8202 g_signal_emit_by_name (data->impl, "response-requested");
8205 g_object_unref (data->impl);
8206 g_free (data->file_part);
8209 g_object_unref (cancellable);
8212 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8213 * should respond. Can get the file part from the file list or the save entry.
8216 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8217 const gchar *file_part,
8220 GtkFileChooserConfirmation conf;
8222 if (!impl->do_overwrite_confirmation)
8225 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8227 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8231 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8233 struct GetDisplayNameData *data;
8235 g_assert (file_part != NULL);
8237 data = g_new0 (struct GetDisplayNameData, 1);
8238 data->impl = g_object_ref (impl);
8239 data->file_part = g_strdup (file_part);
8241 if (impl->should_respond_get_info_cancellable)
8242 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8244 impl->should_respond_get_info_cancellable =
8245 _gtk_file_system_get_info (impl->file_system, parent_file,
8246 "standard::display-name",
8247 confirmation_confirm_get_info_cb,
8249 set_busy_cursor (data->impl, TRUE);
8253 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8256 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8260 g_assert_not_reached ();
8265 struct FileExistsData
8267 GtkFileChooserDefault *impl;
8268 gboolean file_exists_and_is_not_folder;
8274 name_entry_get_parent_info_cb (GCancellable *cancellable,
8276 const GError *error,
8279 gboolean parent_is_folder;
8280 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8281 struct FileExistsData *data = user_data;
8283 if (cancellable != data->impl->should_respond_get_info_cancellable)
8286 data->impl->should_respond_get_info_cancellable = NULL;
8288 set_busy_cursor (data->impl, FALSE);
8294 parent_is_folder = FALSE;
8296 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8298 if (parent_is_folder)
8300 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8302 g_signal_emit_by_name (data->impl, "response-requested"); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8304 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8306 if (data->file_exists_and_is_not_folder)
8311 /* Dup the string because the string may be modified
8312 * depending on what clients do in the confirm-overwrite
8313 * signal and this corrupts the pointer
8315 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8316 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8320 g_signal_emit_by_name (data->impl, "response-requested");
8323 g_signal_emit_by_name (data->impl, "response-requested");
8325 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8326 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8328 GError *mkdir_error = NULL;
8330 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8331 * "/blah/nonexistent" you *will* want a folder created.
8334 set_busy_cursor (data->impl, TRUE);
8335 g_file_make_directory (data->file, NULL, &mkdir_error);
8336 set_busy_cursor (data->impl, FALSE);
8339 g_signal_emit_by_name (data->impl, "response-requested");
8341 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8344 g_assert_not_reached ();
8350 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8351 error_with_file_under_nonfolder (data->impl, data->parent_file);
8357 /* The parent folder is not readable for some reason */
8359 error_copy = g_error_copy (error);
8360 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8365 g_object_unref (data->impl);
8366 g_object_unref (data->file);
8367 g_object_unref (data->parent_file);
8370 g_object_unref (cancellable);
8374 file_exists_get_info_cb (GCancellable *cancellable,
8376 const GError *error,
8379 gboolean data_ownership_taken = FALSE;
8380 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8381 gboolean file_exists;
8383 gboolean needs_parent_check = FALSE;
8384 struct FileExistsData *data = user_data;
8386 if (cancellable != data->impl->file_exists_get_info_cancellable)
8389 data->impl->file_exists_get_info_cancellable = NULL;
8391 set_busy_cursor (data->impl, FALSE);
8396 file_exists = (info != NULL);
8397 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8399 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8402 change_folder_and_display_error (data->impl, data->file, TRUE);
8406 g_signal_emit_by_name (data->impl, "response-requested"); /* user typed an existing filename; we are done */
8408 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8411 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8413 if (file_exists && !is_folder)
8415 /* Oops, the user typed the name of an existing path which is not
8418 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8419 g_error_copy (error));
8423 needs_parent_check = TRUE;
8426 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8430 needs_parent_check = TRUE;
8436 /* User typed a folder; we are done */
8437 g_signal_emit_by_name (data->impl, "response-requested");
8440 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8443 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8446 change_folder_and_display_error (data->impl, data->file, TRUE);
8448 needs_parent_check = TRUE;
8452 g_assert_not_reached();
8455 if (needs_parent_check) {
8456 /* check that everything up to the last path component exists (i.e. the parent) */
8458 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8459 data_ownership_taken = TRUE;
8461 if (data->impl->should_respond_get_info_cancellable)
8462 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8464 data->impl->should_respond_get_info_cancellable =
8465 _gtk_file_system_get_info (data->impl->file_system,
8468 name_entry_get_parent_info_cb,
8470 set_busy_cursor (data->impl, TRUE);
8474 if (!data_ownership_taken)
8476 g_object_unref (data->impl);
8477 g_object_unref (data->file);
8478 g_object_unref (data->parent_file);
8482 g_object_unref (cancellable);
8486 paste_text_received (GtkClipboard *clipboard,
8488 GtkFileChooserDefault *impl)
8495 file = g_file_new_for_uri (text);
8497 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8498 location_popup_handler (impl, text);
8500 g_object_unref (file);
8503 /* Handler for the "location-popup-on-paste" keybinding signal */
8505 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8507 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8508 GDK_SELECTION_CLIPBOARD);
8509 gtk_clipboard_request_text (clipboard,
8510 (GtkClipboardTextReceivedFunc) paste_text_received,
8515 /* Implementation for GtkFileChooserEmbed::should_respond() */
8517 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8519 GtkFileChooserDefault *impl;
8520 GtkWidget *toplevel;
8521 GtkWidget *current_focus;
8523 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8525 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8526 g_assert (GTK_IS_WINDOW (toplevel));
8528 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8530 if (current_focus == impl->browse_files_tree_view)
8532 /* The following array encodes what we do based on the impl->action and the
8533 * number of files selected.
8536 NOOP, /* Do nothing (don't respond) */
8537 RESPOND, /* Respond immediately */
8538 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8539 ALL_FILES, /* Respond only if everything selected is a file */
8540 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8541 SAVE_ENTRY, /* Go to the code for handling the save entry */
8542 NOT_REACHED /* Sanity check */
8544 static const ActionToTake what_to_do[4][3] = {
8545 /* 0 selected 1 selected many selected */
8546 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8547 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8548 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8549 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8553 gboolean all_files, all_folders;
8555 ActionToTake action;
8559 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8561 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8562 return search_should_respond (impl);
8564 if (impl->operation_mode == OPERATION_MODE_RECENT)
8565 return recent_should_respond (impl);
8567 selection_check (impl, &num_selected, &all_files, &all_folders);
8569 if (num_selected > 2)
8574 action = what_to_do [impl->action] [k];
8584 case RESPOND_OR_SWITCH:
8585 g_assert (num_selected == 1);
8589 switch_to_selected_folder (impl);
8592 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8593 return should_respond_after_confirm_overwrite (impl,
8594 get_display_name_from_file_list (impl),
8595 impl->current_folder);
8609 g_assert_not_reached ();
8612 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8615 gboolean is_well_formed, is_empty, is_file_part_empty;
8618 GtkFileChooserEntry *entry;
8623 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8624 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8625 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8626 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8627 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8629 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8630 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8632 if (!is_well_formed)
8637 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8638 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8644 g_assert (file != NULL);
8649 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8650 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8652 change_folder_and_display_error (impl, file, TRUE);
8655 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8656 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8658 /* The folder already exists, so we do not need to create it.
8659 * Just respond to terminate the dialog.
8665 g_assert_not_reached ();
8671 struct FileExistsData *data;
8673 /* We need to check whether file exists and whether it is a folder -
8674 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8675 * negative (it doesn't know yet if your last path component is a
8679 data = g_new0 (struct FileExistsData, 1);
8680 data->impl = g_object_ref (impl);
8681 data->file = g_object_ref (file);
8682 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8684 if (impl->file_exists_get_info_cancellable)
8685 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8687 impl->file_exists_get_info_cancellable =
8688 _gtk_file_system_get_info (impl->file_system, file,
8690 file_exists_get_info_cb,
8693 set_busy_cursor (impl, TRUE);
8697 g_error_free (error);
8700 g_object_unref (file);
8703 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8705 /* The focus is on a dialog's action area button, *and* the widget that
8706 * was focused immediately before it is the file list.
8710 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8712 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8715 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8717 /* The focus is on a dialog's action area button, *and* the widget that
8718 * was focused immediately before it is the location entry.
8723 /* The focus is on a dialog's action area button or something else */
8724 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8725 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8730 g_assert_not_reached ();
8734 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8736 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8738 GtkFileChooserDefault *impl;
8741 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8743 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8744 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8746 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8747 widget = impl->browse_files_tree_view;
8749 widget = impl->location_entry;
8751 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8752 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8753 widget = impl->location_entry;
8756 g_assert_not_reached ();
8760 g_assert (widget != NULL);
8761 gtk_widget_grab_focus (widget);
8764 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8766 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8776 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8777 *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 */
8780 /* Constructs a list of the selected paths in search mode */
8782 search_get_selected_files (GtkFileChooserDefault *impl)
8785 GtkTreeSelection *selection;
8789 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8790 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8791 result = g_slist_reverse (result);
8796 /* Called from ::should_respond(). We return whether there are selected files
8797 * in the search list.
8800 search_should_respond (GtkFileChooserDefault *impl)
8802 GtkTreeSelection *selection;
8804 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8806 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8807 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8810 /* Adds one hit from the search engine to the search_model */
8812 search_add_hit (GtkFileChooserDefault *impl,
8817 file = g_file_new_for_uri (uri);
8821 if (!g_file_is_native (file))
8823 g_object_unref (file);
8827 _gtk_file_system_model_add_and_query_file (impl->search_model,
8831 g_object_unref (file);
8834 /* Callback used from GtkSearchEngine when we get new hits */
8836 search_engine_hits_added_cb (GtkSearchEngine *engine,
8840 GtkFileChooserDefault *impl;
8843 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8845 for (l = hits; l; l = l->next)
8846 search_add_hit (impl, (gchar*)l->data);
8849 /* Callback used from GtkSearchEngine when the query is done running */
8851 search_engine_finished_cb (GtkSearchEngine *engine,
8854 GtkFileChooserDefault *impl;
8856 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8859 /* EB: setting the model here will avoid loads of row events,
8860 * but it'll make the search look like blocked.
8862 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8863 GTK_TREE_MODEL (impl->search_model));
8866 /* FMQ: if search was empty, say that we got no hits */
8867 set_busy_cursor (impl, FALSE);
8870 /* Displays a generic error when we cannot create a GtkSearchEngine.
8871 * It would be better if _gtk_search_engine_new() gave us a GError
8872 * with a better message, but it doesn't do that right now.
8875 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8877 error_message (impl,
8878 _("Could not start the search process"),
8879 _("The program was not able to create a connection to the indexer "
8880 "daemon. Please make sure it is running."));
8884 search_engine_error_cb (GtkSearchEngine *engine,
8885 const gchar *message,
8888 GtkFileChooserDefault *impl;
8890 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8892 search_stop_searching (impl, TRUE);
8893 error_message (impl, _("Could not send the search request"), message);
8895 set_busy_cursor (impl, FALSE);
8898 /* Frees the data in the search_model */
8900 search_clear_model (GtkFileChooserDefault *impl,
8901 gboolean remove_from_treeview)
8903 if (!impl->search_model)
8906 g_object_unref (impl->search_model);
8907 impl->search_model = NULL;
8909 if (remove_from_treeview)
8910 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8913 /* Stops any ongoing searches; does not touch the search_model */
8915 search_stop_searching (GtkFileChooserDefault *impl,
8916 gboolean remove_query)
8918 if (remove_query && impl->search_query)
8920 g_object_unref (impl->search_query);
8921 impl->search_query = NULL;
8924 if (impl->search_engine)
8926 _gtk_search_engine_stop (impl->search_engine);
8928 g_object_unref (impl->search_engine);
8929 impl->search_engine = NULL;
8933 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8935 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8937 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8939 search_stop_searching (impl, FALSE);
8940 search_clear_model (impl, TRUE);
8942 gtk_widget_destroy (impl->search_hbox);
8943 impl->search_hbox = NULL;
8944 impl->search_entry = NULL;
8946 gtk_widget_show (impl->browse_path_bar);
8947 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8948 gtk_widget_hide (impl->browse_new_folder_button);
8950 gtk_widget_show (impl->browse_new_folder_button);
8953 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8954 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8956 gtk_widget_show (impl->location_button);
8958 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8959 gtk_widget_show (impl->location_entry_box);
8962 impl->operation_mode = OPERATION_MODE_BROWSE;
8964 file_list_set_sort_column_ids (impl);
8967 /* Creates the search_model and puts it in the tree view */
8969 search_setup_model (GtkFileChooserDefault *impl)
8971 g_assert (impl->search_model == NULL);
8973 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8975 MODEL_COLUMN_TYPES);
8977 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8981 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8985 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8989 set_sort_column (impl);
8991 /* EB: setting the model here will make the hits list update feel
8992 * more "alive" than setting the model at the end of the search
8995 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8996 GTK_TREE_MODEL (impl->search_model));
8999 /* Creates a new query with the specified text and launches it */
9001 search_start_query (GtkFileChooserDefault *impl,
9002 const gchar *query_text)
9004 search_stop_searching (impl, FALSE);
9005 search_clear_model (impl, TRUE);
9006 search_setup_model (impl);
9007 set_busy_cursor (impl, TRUE);
9009 if (impl->search_engine == NULL)
9010 impl->search_engine = _gtk_search_engine_new ();
9012 if (!impl->search_engine)
9014 set_busy_cursor (impl, FALSE);
9015 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9019 if (!impl->search_query)
9021 impl->search_query = _gtk_query_new ();
9022 _gtk_query_set_text (impl->search_query, query_text);
9025 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9027 g_signal_connect (impl->search_engine, "hits-added",
9028 G_CALLBACK (search_engine_hits_added_cb), impl);
9029 g_signal_connect (impl->search_engine, "finished",
9030 G_CALLBACK (search_engine_finished_cb), impl);
9031 g_signal_connect (impl->search_engine, "error",
9032 G_CALLBACK (search_engine_error_cb), impl);
9034 _gtk_search_engine_start (impl->search_engine);
9037 /* Callback used when the user presses Enter while typing on the search
9038 * entry; starts the query
9041 search_entry_activate_cb (GtkEntry *entry,
9044 GtkFileChooserDefault *impl;
9047 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9049 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9050 if (strlen (text) == 0)
9053 /* reset any existing query object */
9054 if (impl->search_query)
9056 g_object_unref (impl->search_query);
9057 impl->search_query = NULL;
9060 search_start_query (impl, text);
9064 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9066 GDK_THREADS_ENTER ();
9068 g_source_destroy (impl->focus_entry_idle);
9069 impl->focus_entry_idle = NULL;
9071 if (impl->search_entry)
9072 gtk_widget_grab_focus (impl->search_entry);
9074 GDK_THREADS_LEAVE ();
9080 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9082 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9083 * pane, we get a selection-changed signal and we set up the search widgets.
9084 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9085 * the change to the selection. So, we need to re-focus the search entry
9086 * after the treeview has finished doing its work; we'll do that in an idle
9090 if (!impl->focus_entry_idle)
9091 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9094 /* Hides the path bar and creates the search entry */
9096 search_setup_widgets (GtkFileChooserDefault *impl)
9102 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9106 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9107 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9111 label = gtk_label_new (NULL);
9112 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9113 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9114 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9119 impl->search_entry = gtk_entry_new ();
9120 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9121 g_signal_connect (impl->search_entry, "activate",
9122 G_CALLBACK (search_entry_activate_cb),
9124 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9126 /* if there already is a query, restart it */
9127 if (impl->search_query)
9129 gchar *query = _gtk_query_get_text (impl->search_query);
9133 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9134 search_start_query (impl, query);
9140 g_object_unref (impl->search_query);
9141 impl->search_query = NULL;
9145 gtk_widget_hide (impl->browse_path_bar);
9146 gtk_widget_hide (impl->browse_new_folder_button);
9148 /* Box for search widgets */
9149 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9150 gtk_widget_show_all (impl->search_hbox);
9151 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9153 /* Hide the location widgets temporarily */
9155 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9156 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9158 gtk_widget_hide (impl->location_button);
9159 gtk_widget_hide (impl->location_entry_box);
9162 focus_search_entry_in_idle (impl);
9164 /* FMQ: hide the filter combo? */
9167 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9169 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9173 case OPERATION_MODE_BROWSE:
9174 stop_loading_and_clear_list_model (impl, TRUE);
9177 case OPERATION_MODE_SEARCH:
9178 search_stop_searching (impl, FALSE);
9179 search_clear_model (impl, TRUE);
9181 gtk_widget_destroy (impl->search_hbox);
9182 impl->search_hbox = NULL;
9183 impl->search_entry = NULL;
9186 case OPERATION_MODE_RECENT:
9187 recent_stop_loading (impl);
9188 recent_clear_model (impl, TRUE);
9190 gtk_widget_destroy (impl->recent_hbox);
9191 impl->recent_hbox = NULL;
9196 /* Main entry point to the searching functions; this gets called when the user
9197 * activates the Search shortcut.
9200 search_activate (GtkFileChooserDefault *impl)
9202 OperationMode previous_mode;
9204 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9206 focus_search_entry_in_idle (impl);
9210 previous_mode = impl->operation_mode;
9211 impl->operation_mode = OPERATION_MODE_SEARCH;
9213 stop_operation (impl, previous_mode);
9215 g_assert (impl->search_hbox == NULL);
9216 g_assert (impl->search_entry == NULL);
9217 g_assert (impl->search_model == NULL);
9219 search_setup_widgets (impl);
9220 file_list_set_sort_column_ids (impl);
9224 * Recent files support
9227 /* Frees the data in the recent_model */
9229 recent_clear_model (GtkFileChooserDefault *impl,
9230 gboolean remove_from_treeview)
9232 if (!impl->recent_model)
9235 if (remove_from_treeview)
9236 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9238 g_object_unref (impl->recent_model);
9239 impl->recent_model = NULL;
9242 /* Stops any ongoing loading of the recent files list; does
9243 * not touch the recent_model
9246 recent_stop_loading (GtkFileChooserDefault *impl)
9248 if (impl->load_recent_id)
9250 g_source_remove (impl->load_recent_id);
9251 impl->load_recent_id = 0;
9255 /* Stops any pending load, clears the file list, and switches
9256 * back to OPERATION_MODE_BROWSE
9259 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9261 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9263 recent_stop_loading (impl);
9264 recent_clear_model (impl, TRUE);
9266 gtk_widget_destroy (impl->recent_hbox);
9267 impl->recent_hbox = NULL;
9269 gtk_widget_show (impl->browse_path_bar);
9270 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9271 gtk_widget_hide (impl->browse_new_folder_button);
9273 gtk_widget_show (impl->browse_new_folder_button);
9275 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9276 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9278 gtk_widget_show (impl->location_button);
9280 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9281 gtk_widget_show (impl->location_entry_box);
9284 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9286 impl->operation_mode = OPERATION_MODE_BROWSE;
9288 file_list_set_sort_column_ids (impl);
9292 recent_setup_model (GtkFileChooserDefault *impl)
9294 g_assert (impl->recent_model == NULL);
9296 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9298 MODEL_COLUMN_TYPES);
9300 _gtk_file_system_model_set_filter (impl->recent_model,
9301 impl->current_filter);
9302 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9306 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9310 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9314 set_sort_column (impl);
9319 GtkFileChooserDefault *impl;
9322 gint n_loaded_items;
9323 guint needs_sorting : 1;
9327 recent_idle_cleanup (gpointer data)
9329 RecentLoadData *load_data = data;
9330 GtkFileChooserDefault *impl = load_data->impl;
9332 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9333 GTK_TREE_MODEL (impl->recent_model));
9335 set_busy_cursor (impl, FALSE);
9337 impl->load_recent_id = 0;
9339 if (load_data->items)
9341 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9342 g_list_free (load_data->items);
9349 recent_sort_mru (gconstpointer a,
9352 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9353 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9355 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9359 get_recent_files_limit (GtkWidget *widget)
9361 GtkSettings *settings;
9364 if (gtk_widget_has_screen (widget))
9365 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9367 settings = gtk_settings_get_default ();
9369 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9375 recent_idle_load (gpointer data)
9377 RecentLoadData *load_data = data;
9378 GtkFileChooserDefault *impl = load_data->impl;
9382 if (!impl->recent_manager)
9385 /* first iteration: load all the items */
9386 if (!load_data->items)
9388 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9389 if (!load_data->items)
9392 load_data->needs_sorting = TRUE;
9397 /* second iteration: preliminary MRU sorting and clamping */
9398 if (load_data->needs_sorting)
9402 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9403 load_data->n_items = g_list_length (load_data->items);
9405 limit = get_recent_files_limit (GTK_WIDGET (impl));
9407 if (limit != -1 && (load_data->n_items > limit))
9411 clamp = g_list_nth (load_data->items, limit - 1);
9412 if (G_LIKELY (clamp))
9417 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9420 load_data->n_items = limit;
9424 load_data->n_loaded_items = 0;
9425 load_data->needs_sorting = FALSE;
9430 /* finished loading items */
9431 for (walk = load_data->items; walk; walk = walk->next)
9433 GtkRecentInfo *info = walk->data;
9434 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9436 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9439 gtk_recent_info_unref (walk->data);
9440 g_object_unref (file);
9443 g_list_free (load_data->items);
9444 load_data->items = NULL;
9450 recent_start_loading (GtkFileChooserDefault *impl)
9452 RecentLoadData *load_data;
9454 recent_stop_loading (impl);
9455 recent_clear_model (impl, TRUE);
9456 recent_setup_model (impl);
9457 set_busy_cursor (impl, TRUE);
9459 g_assert (impl->load_recent_id == 0);
9461 load_data = g_new (RecentLoadData, 1);
9462 load_data->impl = impl;
9463 load_data->items = NULL;
9464 load_data->n_items = 0;
9465 load_data->n_loaded_items = 0;
9466 load_data->needs_sorting = TRUE;
9468 /* begin lazy loading the recent files into the model */
9469 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9472 recent_idle_cleanup);
9476 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9486 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9487 *list = g_slist_prepend (*list, file);
9490 /* Constructs a list of the selected paths in recent files mode */
9492 recent_get_selected_files (GtkFileChooserDefault *impl)
9495 GtkTreeSelection *selection;
9499 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9500 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9501 result = g_slist_reverse (result);
9506 /* Called from ::should_respond(). We return whether there are selected
9507 * files in the recent files list.
9510 recent_should_respond (GtkFileChooserDefault *impl)
9512 GtkTreeSelection *selection;
9514 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9516 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9517 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9520 /* Hide the location widgets temporarily */
9522 recent_hide_entry (GtkFileChooserDefault *impl)
9528 impl->recent_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9531 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9532 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9535 label = gtk_label_new (NULL);
9536 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9537 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9538 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9541 gtk_widget_hide (impl->browse_path_bar);
9542 gtk_widget_hide (impl->browse_new_folder_button);
9544 /* Box for recent widgets */
9545 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9546 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->recent_hbox);
9547 gtk_widget_show_all (impl->recent_hbox);
9549 /* Hide the location widgets temporarily */
9550 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9551 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9553 gtk_widget_hide (impl->location_button);
9554 gtk_widget_hide (impl->location_entry_box);
9558 /* Main entry point to the recent files functions; this gets called when
9559 * the user activates the Recently Used shortcut.
9562 recent_activate (GtkFileChooserDefault *impl)
9564 OperationMode previous_mode;
9566 if (impl->operation_mode == OPERATION_MODE_RECENT)
9569 previous_mode = impl->operation_mode;
9570 impl->operation_mode = OPERATION_MODE_RECENT;
9572 stop_operation (impl, previous_mode);
9574 recent_hide_entry (impl);
9576 file_list_set_sort_column_ids (impl);
9577 recent_start_loading (impl);
9581 set_current_filter (GtkFileChooserDefault *impl,
9582 GtkFileFilter *filter)
9584 if (impl->current_filter != filter)
9588 /* NULL filters are allowed to reset to non-filtered status
9590 filter_index = g_slist_index (impl->filters, filter);
9591 if (impl->filters && filter && filter_index < 0)
9594 if (impl->current_filter)
9595 g_object_unref (impl->current_filter);
9596 impl->current_filter = filter;
9597 if (impl->current_filter)
9599 g_object_ref_sink (impl->current_filter);
9603 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9606 if (impl->browse_files_model)
9607 install_list_model_filter (impl);
9609 if (impl->search_model)
9610 _gtk_file_system_model_set_filter (impl->search_model, filter);
9612 if (impl->recent_model)
9613 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9615 g_object_notify (G_OBJECT (impl), "filter");
9620 filter_combo_changed (GtkComboBox *combo_box,
9621 GtkFileChooserDefault *impl)
9623 gint new_index = gtk_combo_box_get_active (combo_box);
9624 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9626 set_current_filter (impl, new_filter);
9630 check_preview_change (GtkFileChooserDefault *impl)
9632 GtkTreePath *cursor_path;
9634 char *new_display_name;
9635 GtkTreeModel *model;
9637 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9638 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9643 gtk_tree_model_get_iter (model, &iter, cursor_path);
9644 gtk_tree_model_get (model, &iter,
9645 MODEL_COL_FILE, &new_file,
9646 MODEL_COL_NAME, &new_display_name,
9649 gtk_tree_path_free (cursor_path);
9654 new_display_name = NULL;
9657 if (new_file != impl->preview_file &&
9658 !(new_file && impl->preview_file &&
9659 g_file_equal (new_file, impl->preview_file)))
9661 if (impl->preview_file)
9663 g_object_unref (impl->preview_file);
9664 g_free (impl->preview_display_name);
9669 impl->preview_file = new_file;
9670 impl->preview_display_name = new_display_name;
9674 impl->preview_file = NULL;
9675 impl->preview_display_name = NULL;
9676 g_free (new_display_name);
9679 if (impl->use_preview_label && impl->preview_label)
9680 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9682 g_signal_emit_by_name (impl, "update-preview");
9687 g_object_unref (new_file);
9689 g_free (new_display_name);
9694 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9695 GtkFileSystemVolume *volume,
9696 const GError *error,
9700 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9701 GtkFileChooserDefault *impl = data;
9703 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9706 impl->shortcuts_activate_iter_cancellable = NULL;
9708 set_busy_cursor (impl, FALSE);
9715 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9719 name = _gtk_file_system_volume_get_display_name (volume);
9720 msg = g_strdup_printf (_("Could not mount %s"), name);
9722 error_message (impl, msg, error->message);
9731 file = _gtk_file_system_volume_get_root (volume);
9734 change_folder_and_display_error (impl, file, FALSE);
9735 g_object_unref (file);
9739 g_object_unref (impl);
9740 g_object_unref (cancellable);
9744 /* Activates a volume by mounting it if necessary and then switching to its
9748 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9749 GtkFileSystemVolume *volume)
9753 switch (impl->operation_mode)
9755 case OPERATION_MODE_BROWSE:
9757 case OPERATION_MODE_SEARCH:
9758 search_switch_to_browse_mode (impl);
9760 case OPERATION_MODE_RECENT:
9761 recent_switch_to_browse_mode (impl);
9765 /* We ref the file chooser since volume_mount() may run a main loop, and the
9766 * user could close the file chooser window in the meantime.
9768 g_object_ref (impl);
9770 if (!_gtk_file_system_volume_is_mounted (volume))
9772 GMountOperation *mount_op;
9774 set_busy_cursor (impl, TRUE);
9776 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9777 impl->shortcuts_activate_iter_cancellable =
9778 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9779 shortcuts_activate_volume_mount_cb,
9780 g_object_ref (impl));
9781 g_object_unref (mount_op);
9785 file = _gtk_file_system_volume_get_root (volume);
9788 change_folder_and_display_error (impl, file, FALSE);
9789 g_object_unref (file);
9793 g_object_unref (impl);
9796 /* Opens the folder or volume at the specified iter in the shortcuts model */
9797 struct ShortcutsActivateData
9799 GtkFileChooserDefault *impl;
9804 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9806 const GError *error,
9809 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9810 struct ShortcutsActivateData *data = user_data;
9812 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9815 data->impl->shortcuts_activate_iter_cancellable = NULL;
9820 if (!error && _gtk_file_info_consider_as_directory (info))
9821 change_folder_and_display_error (data->impl, data->file, FALSE);
9823 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9828 g_object_unref (data->impl);
9829 g_object_unref (data->file);
9832 g_object_unref (cancellable);
9836 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9837 GtkFileSystemVolume *volume,
9838 const GError *error,
9841 struct ShortcutsActivateData *data = user_data;
9845 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9847 g_object_unref (data->impl);
9848 g_object_unref (data->file);
9854 data->impl->shortcuts_activate_iter_cancellable =
9855 _gtk_file_system_get_info (data->impl->file_system, data->file,
9857 shortcuts_activate_get_info_cb, data);
9860 _gtk_file_system_volume_unref (volume);
9864 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9868 ShortcutType shortcut_type;
9870 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9871 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9872 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9873 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9875 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9876 SHORTCUTS_COL_DATA, &col_data,
9877 SHORTCUTS_COL_TYPE, &shortcut_type,
9880 if (impl->shortcuts_activate_iter_cancellable)
9882 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9883 impl->shortcuts_activate_iter_cancellable = NULL;
9886 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9888 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9890 GtkFileSystemVolume *volume;
9894 shortcuts_activate_volume (impl, volume);
9896 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9898 struct ShortcutsActivateData *data;
9899 GtkFileSystemVolume *volume;
9901 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9903 data = g_new0 (struct ShortcutsActivateData, 1);
9904 data->impl = g_object_ref (impl);
9905 data->file = g_object_ref (col_data);
9907 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9909 GMountOperation *mount_operation;
9910 GtkWidget *toplevel;
9912 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9914 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9916 impl->shortcuts_activate_iter_cancellable =
9917 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9919 shortcuts_activate_mount_enclosing_volume,
9924 impl->shortcuts_activate_iter_cancellable =
9925 _gtk_file_system_get_info (impl->file_system, data->file,
9927 shortcuts_activate_get_info_cb, data);
9930 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9932 search_activate (impl);
9934 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9936 recent_activate (impl);
9940 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9942 shortcuts_key_press_event_cb (GtkWidget *widget,
9944 GtkFileChooserDefault *impl)
9948 modifiers = gtk_accelerator_get_default_mod_mask ();
9950 if (key_is_left_or_right (event))
9952 gtk_widget_grab_focus (impl->browse_files_tree_view);
9956 if ((event->keyval == GDK_KEY_BackSpace
9957 || event->keyval == GDK_KEY_Delete
9958 || event->keyval == GDK_KEY_KP_Delete)
9959 && (event->state & modifiers) == 0)
9961 remove_selected_bookmarks (impl);
9965 if ((event->keyval == GDK_KEY_F2)
9966 && (event->state & modifiers) == 0)
9968 rename_selected_bookmark (impl);
9976 shortcuts_select_func (GtkTreeSelection *selection,
9977 GtkTreeModel *model,
9979 gboolean path_currently_selected,
9982 GtkFileChooserDefault *impl = data;
9983 GtkTreeIter filter_iter;
9984 ShortcutType shortcut_type;
9986 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9987 g_assert_not_reached ();
9989 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9991 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9995 list_select_func (GtkTreeSelection *selection,
9996 GtkTreeModel *model,
9998 gboolean path_currently_selected,
10001 GtkFileChooserDefault *impl = data;
10003 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10004 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10007 gboolean is_folder;
10009 if (!gtk_tree_model_get_iter (model, &iter, path))
10011 gtk_tree_model_get (model, &iter,
10012 MODEL_COL_IS_FOLDER, &is_folder,
10022 list_selection_changed (GtkTreeSelection *selection,
10023 GtkFileChooserDefault *impl)
10025 /* See if we are in the new folder editable row for Save mode */
10026 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10027 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10030 gboolean had_selection;
10032 info = get_selected_file_info_from_file_list (impl, &had_selection);
10033 if (!had_selection)
10034 goto out; /* normal processing */
10037 return; /* We are on the editable row for New Folder */
10042 if (impl->location_entry)
10043 update_chooser_entry (impl);
10045 check_preview_change (impl);
10046 bookmarks_check_add_sensitivity (impl);
10048 g_signal_emit_by_name (impl, "selection-changed", 0);
10051 /* Callback used when a row in the file list is activated */
10053 list_row_activated (GtkTreeView *tree_view,
10055 GtkTreeViewColumn *column,
10056 GtkFileChooserDefault *impl)
10060 GtkTreeModel *model;
10061 gboolean is_folder;
10063 model = gtk_tree_view_get_model (tree_view);
10065 if (!gtk_tree_model_get_iter (model, &iter, path))
10068 gtk_tree_model_get (model, &iter,
10069 MODEL_COL_FILE, &file,
10070 MODEL_COL_IS_FOLDER, &is_folder,
10073 if (is_folder && file)
10075 change_folder_and_display_error (impl, file, FALSE);
10076 g_object_unref (file);
10080 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10081 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10082 g_signal_emit_by_name (impl, "file-activated");
10087 g_object_unref (file);
10091 path_bar_clicked (GtkPathBar *path_bar,
10094 gboolean child_is_hidden,
10095 GtkFileChooserDefault *impl)
10098 pending_select_files_add (impl, child_file);
10100 if (!change_folder_and_display_error (impl, file, FALSE))
10103 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10104 * show hidden files so that ".baz" appears in the file list, as it will still
10105 * be shown in the path bar: "/foo/[bar]/.baz"
10107 if (child_is_hidden)
10108 g_object_set (impl, "show-hidden", TRUE, NULL);
10112 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10114 GtkTreeViewColumn *column;
10115 GtkCellRenderer *renderer;
10116 GList *walk, *list;
10117 gboolean always_sensitive;
10119 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10120 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10122 /* Keep the following column numbers in sync with create_file_list() */
10125 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10126 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10127 for (walk = list; walk; walk = walk->next)
10129 renderer = walk->data;
10130 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10132 gtk_tree_view_column_set_attributes (column, renderer,
10133 "pixbuf", MODEL_COL_PIXBUF,
10138 gtk_tree_view_column_set_attributes (column, renderer,
10139 "text", MODEL_COL_NAME,
10140 "ellipsize", MODEL_COL_ELLIPSIZE,
10143 if (always_sensitive)
10144 g_object_set (renderer, "sensitive", TRUE, NULL);
10146 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10148 g_list_free (list);
10151 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10152 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10153 renderer = list->data;
10154 gtk_tree_view_column_set_attributes (column, renderer,
10155 "text", MODEL_COL_SIZE_TEXT,
10157 if (always_sensitive)
10158 g_object_set (renderer, "sensitive", TRUE, NULL);
10160 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10161 g_list_free (list);
10164 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10165 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10166 renderer = list->data;
10167 gtk_tree_view_column_set_attributes (column, renderer,
10168 "text", MODEL_COL_MTIME_TEXT,
10170 if (always_sensitive)
10171 g_object_set (renderer, "sensitive", TRUE, NULL);
10173 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10174 g_list_free (list);
10178 _gtk_file_chooser_default_new (void)
10180 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10184 location_set_user_text (GtkFileChooserDefault *impl,
10187 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10188 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10192 location_popup_handler (GtkFileChooserDefault *impl,
10195 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10197 GtkWidget *widget_to_focus;
10199 /* This will give us the location widgets back */
10200 switch (impl->operation_mode)
10202 case OPERATION_MODE_SEARCH:
10203 search_switch_to_browse_mode (impl);
10205 case OPERATION_MODE_RECENT:
10206 recent_switch_to_browse_mode (impl);
10208 case OPERATION_MODE_BROWSE:
10209 g_assert_not_reached ();
10213 if (impl->current_folder)
10214 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10216 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10217 widget_to_focus = impl->browse_files_tree_view;
10219 widget_to_focus = impl->location_entry;
10221 gtk_widget_grab_focus (widget_to_focus);
10225 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10226 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10228 LocationMode new_mode;
10232 /* since the user typed something, we unconditionally want to turn on the entry */
10233 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10235 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10236 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10237 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10238 new_mode = LOCATION_MODE_PATH_BAR;
10241 g_assert_not_reached ();
10245 location_mode_set (impl, new_mode, TRUE);
10246 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10249 location_set_user_text (impl, path);
10252 location_entry_set_initial_text (impl);
10253 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10257 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10258 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10260 gtk_widget_grab_focus (impl->location_entry);
10262 location_set_user_text (impl, path);
10265 g_assert_not_reached ();
10268 /* Handler for the "up-folder" keybinding signal */
10270 up_folder_handler (GtkFileChooserDefault *impl)
10272 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10275 /* Handler for the "down-folder" keybinding signal */
10277 down_folder_handler (GtkFileChooserDefault *impl)
10279 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10282 /* Switches to the shortcut in the specified index */
10284 switch_to_shortcut (GtkFileChooserDefault *impl,
10289 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10290 g_assert_not_reached ();
10292 shortcuts_activate_iter (impl, &iter);
10295 /* Handler for the "home-folder" keybinding signal */
10297 home_folder_handler (GtkFileChooserDefault *impl)
10299 if (impl->has_home)
10300 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10303 /* Handler for the "desktop-folder" keybinding signal */
10305 desktop_folder_handler (GtkFileChooserDefault *impl)
10307 if (impl->has_desktop)
10308 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10311 /* Handler for the "search-shortcut" keybinding signal */
10313 search_shortcut_handler (GtkFileChooserDefault *impl)
10315 if (impl->has_search)
10317 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10319 /* we want the entry widget to grab the focus the first
10320 * time, not the browse_files_tree_view widget.
10322 if (impl->search_entry)
10323 gtk_widget_grab_focus (impl->search_entry);
10327 /* Handler for the "recent-shortcut" keybinding signal */
10329 recent_shortcut_handler (GtkFileChooserDefault *impl)
10331 if (impl->has_recent)
10332 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10336 quick_bookmark_handler (GtkFileChooserDefault *impl,
10337 gint bookmark_index)
10342 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10345 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10347 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10348 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10351 gtk_tree_path_free (path);
10353 switch_to_shortcut (impl, bookmark_pos);
10357 show_hidden_handler (GtkFileChooserDefault *impl)
10359 g_object_set (impl,
10360 "show-hidden", !impl->show_hidden,
10365 /* Drag and drop interfaces */
10368 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10373 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10375 model->impl = NULL;
10378 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10380 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10383 ShortcutsPaneModelFilter *model;
10387 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10389 pos = *gtk_tree_path_get_indices (path);
10390 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10392 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10395 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10399 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10401 GtkSelectionData *selection_data)
10408 /* Fill the GtkTreeDragSourceIface vtable */
10410 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10412 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10413 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10417 /* Fill the GtkTreeDragDestIface vtable */
10419 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10421 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10422 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10426 static GtkTreeModel *
10427 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10428 GtkTreeModel *child_model,
10431 ShortcutsPaneModelFilter *model;
10433 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10434 "child-model", child_model,
10435 "virtual-root", root,
10438 model->impl = impl;
10440 return GTK_TREE_MODEL (model);