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"
49 #include "gtkinfobar.h"
51 #include "gtkmarshalers.h"
52 #include "gtkmessagedialog.h"
53 #include "gtkmountoperation.h"
54 #include "gtkpathbar.h"
55 #include "gtkprivate.h"
56 #include "gtkradiobutton.h"
57 #include "gtkrecentfilter.h"
58 #include "gtkrecentmanager.h"
59 #include "gtkscrolledwindow.h"
60 #include "gtkseparatormenuitem.h"
61 #include "gtksizegroup.h"
62 #include "gtksizerequest.h"
65 #include "gtktoolbar.h"
66 #include "gtktoolbutton.h"
67 #include "gtktooltip.h"
68 #include "gtktreednd.h"
69 #include "gtktreeprivate.h"
70 #include "gtktreeselection.h"
72 #include "gtkorientable.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
195 "standard::is-hidden,standard::is-backup,standard::size," \
196 "standard::content-type,time::modified"
198 /* the first 3 must be these due to settings caching sort column */
203 MODEL_COL_NAME_COLLATED,
207 MODEL_COL_MTIME_TEXT,
209 MODEL_COL_NUM_COLUMNS
212 /* This list of types is passed to _gtk_file_system_model_new*() */
213 #define MODEL_COLUMN_TYPES \
214 MODEL_COL_NUM_COLUMNS, \
215 G_TYPE_STRING, /* MODEL_COL_NAME */ \
216 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
217 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
218 G_TYPE_FILE, /* MODEL_COL_FILE */ \
219 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
220 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
221 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
222 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
223 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
224 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
226 /* Identifiers for target types */
231 /* Interesting places in the shortcuts bar */
235 SHORTCUTS_RECENT_SEPARATOR,
240 SHORTCUTS_BOOKMARKS_SEPARATOR,
242 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
243 SHORTCUTS_CURRENT_FOLDER
246 /* Icon size for if we can't get it from the theme */
247 #define FALLBACK_ICON_SIZE 16
249 #define PREVIEW_HBOX_SPACING 12
253 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
254 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
255 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
256 #define SETTINGS_KEY_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 add_selection_to_recent_list (GtkFileChooserDefault *impl);
332 static void location_popup_handler (GtkFileChooserDefault *impl,
334 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
335 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
336 static void up_folder_handler (GtkFileChooserDefault *impl);
337 static void down_folder_handler (GtkFileChooserDefault *impl);
338 static void home_folder_handler (GtkFileChooserDefault *impl);
339 static void desktop_folder_handler (GtkFileChooserDefault *impl);
340 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
341 gint bookmark_index);
342 static void show_hidden_handler (GtkFileChooserDefault *impl);
343 static void search_shortcut_handler (GtkFileChooserDefault *impl);
344 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
345 static void update_appearance (GtkFileChooserDefault *impl);
347 static void set_current_filter (GtkFileChooserDefault *impl,
348 GtkFileFilter *filter);
349 static void check_preview_change (GtkFileChooserDefault *impl);
351 static void filter_combo_changed (GtkComboBox *combo_box,
352 GtkFileChooserDefault *impl);
354 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
356 GtkFileChooserDefault *impl);
358 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
361 gboolean path_currently_selected,
363 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
365 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
367 static int shortcuts_get_index (GtkFileChooserDefault *impl,
368 ShortcutsIndex where);
369 static int shortcut_find_position (GtkFileChooserDefault *impl,
372 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
374 static gboolean list_select_func (GtkTreeSelection *selection,
377 gboolean path_currently_selected,
380 static void list_selection_changed (GtkTreeSelection *tree_selection,
381 GtkFileChooserDefault *impl);
382 static void list_row_activated (GtkTreeView *tree_view,
384 GtkTreeViewColumn *column,
385 GtkFileChooserDefault *impl);
387 static void path_bar_clicked (GtkPathBar *path_bar,
390 gboolean child_is_hidden,
391 GtkFileChooserDefault *impl);
393 static void add_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
395 static void remove_bookmark_button_clicked_cb (GtkButton *button,
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 shortcuts_free (impl);
843 g_free (impl->browse_files_last_selected_name);
845 for (l = impl->filters; l; l = l->next)
847 GtkFileFilter *filter;
849 filter = GTK_FILE_FILTER (l->data);
850 g_object_unref (filter);
852 g_slist_free (impl->filters);
854 if (impl->current_filter)
855 g_object_unref (impl->current_filter);
857 if (impl->current_volume_file)
858 g_object_unref (impl->current_volume_file);
860 if (impl->current_folder)
861 g_object_unref (impl->current_folder);
863 if (impl->preview_file)
864 g_object_unref (impl->preview_file);
866 if (impl->browse_path_bar_size_group)
867 g_object_unref (impl->browse_path_bar_size_group);
869 /* Free all the Models we have */
870 stop_loading_and_clear_list_model (impl, FALSE);
871 search_clear_model (impl, FALSE);
872 recent_clear_model (impl, FALSE);
874 /* stopping the load above should have cleared this */
875 g_assert (impl->load_timeout_id == 0);
877 g_free (impl->preview_display_name);
879 g_free (impl->edited_new_text);
881 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
884 /* Shows an error dialog set as transient for the specified window */
886 error_message_with_parent (GtkWindow *parent,
892 dialog = gtk_message_dialog_new (parent,
893 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
898 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
901 if (parent && gtk_window_has_group (parent))
902 gtk_window_group_add_window (gtk_window_get_group (parent),
903 GTK_WINDOW (dialog));
905 gtk_dialog_run (GTK_DIALOG (dialog));
906 gtk_widget_destroy (dialog);
909 /* Returns a toplevel GtkWindow, or NULL if none */
911 get_toplevel (GtkWidget *widget)
915 toplevel = gtk_widget_get_toplevel (widget);
916 if (!gtk_widget_is_toplevel (toplevel))
919 return GTK_WINDOW (toplevel);
922 /* Shows an error dialog for the file chooser */
924 error_message (GtkFileChooserDefault *impl,
928 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
931 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
933 error_dialog (GtkFileChooserDefault *impl,
944 uri = g_file_get_uri (file);
945 text = g_strdup_printf (msg, uri);
946 error_message (impl, text, error->message);
949 g_error_free (error);
953 /* Displays an error message about not being able to get information for a file.
954 * Frees the GError as well.
957 error_getting_info_dialog (GtkFileChooserDefault *impl,
962 _("Could not retrieve information about the file"),
966 /* Shows an error dialog about not being able to add a bookmark */
968 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
973 _("Could not add a bookmark"),
977 /* Shows an error dialog about not being able to remove a bookmark */
979 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
984 _("Could not remove bookmark"),
988 /* Shows an error dialog about not being able to create a folder */
990 error_creating_folder_dialog (GtkFileChooserDefault *impl,
995 _("The folder could not be created"),
999 /* Shows an error about not being able to create a folder because a file with
1000 * the same name is already there.
1003 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1008 _("The folder could not be created, as a file with the same "
1009 "name already exists. Try using a different name for the "
1010 "folder, or rename the file first."),
1015 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1021 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1022 _("You need to choose a valid filename."));
1025 _("Cannot create a file under %s as it is not a folder"),
1026 parent_file, error);
1029 /* Shows an error about not being able to select a folder because a file with
1030 * the same name is already there.
1033 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1037 _("You may only select folders. The item that you selected is not a folder; "
1038 "try using a different item."),
1042 /* Shows an error dialog about not being able to create a filename */
1044 error_building_filename_dialog (GtkFileChooserDefault *impl,
1047 error_dialog (impl, _("Invalid file name"),
1051 /* Shows an error dialog when we cannot switch to a folder */
1053 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1057 error_dialog (impl, _("The folder contents could not be displayed"),
1061 /* Changes folders, displaying an error dialog if this fails */
1063 change_folder_and_display_error (GtkFileChooserDefault *impl,
1065 gboolean clear_entry)
1070 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1072 /* We copy the path because of this case:
1074 * list_row_activated()
1075 * fetches path from model; path belongs to the model (*)
1076 * calls change_folder_and_display_error()
1077 * calls gtk_file_chooser_set_current_folder_file()
1078 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1082 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1085 error_changing_folder_dialog (impl, file, error);
1091 emit_default_size_changed (GtkFileChooserDefault *impl)
1093 profile_msg (" emit default-size-changed start", NULL);
1094 g_signal_emit_by_name (impl, "default-size-changed");
1095 profile_msg (" emit default-size-changed end", NULL);
1099 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1101 if (impl->use_preview_label)
1103 if (!impl->preview_label)
1105 impl->preview_label = gtk_label_new (impl->preview_display_name);
1106 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1107 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1108 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1109 gtk_widget_show (impl->preview_label);
1114 if (impl->preview_label)
1116 gtk_widget_destroy (impl->preview_label);
1117 impl->preview_label = NULL;
1121 if (impl->preview_widget_active && impl->preview_widget)
1122 gtk_widget_show (impl->preview_box);
1124 gtk_widget_hide (impl->preview_box);
1126 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1127 emit_default_size_changed (impl);
1131 set_preview_widget (GtkFileChooserDefault *impl,
1132 GtkWidget *preview_widget)
1134 if (preview_widget == impl->preview_widget)
1137 if (impl->preview_widget)
1138 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1139 impl->preview_widget);
1141 impl->preview_widget = preview_widget;
1142 if (impl->preview_widget)
1144 gtk_widget_show (impl->preview_widget);
1145 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1146 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1147 impl->preview_widget,
1148 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1151 update_preview_widget_visibility (impl);
1154 /* Renders a "Search" icon at an appropriate size for a tree view */
1156 render_search_icon (GtkFileChooserDefault *impl)
1158 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1162 render_recent_icon (GtkFileChooserDefault *impl)
1164 GtkIconTheme *theme;
1167 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1168 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1170 theme = gtk_icon_theme_get_default ();
1172 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1178 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1184 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1185 struct ReloadIconsData
1187 GtkFileChooserDefault *impl;
1188 GtkTreeRowReference *row_ref;
1192 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1194 const GError *error,
1200 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1201 struct ReloadIconsData *data = user_data;
1203 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1206 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1208 if (cancelled || error)
1211 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1213 path = gtk_tree_row_reference_get_path (data->row_ref);
1216 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1217 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1218 SHORTCUTS_COL_PIXBUF, pixbuf,
1220 gtk_tree_path_free (path);
1224 g_object_unref (pixbuf);
1227 gtk_tree_row_reference_free (data->row_ref);
1228 g_object_unref (data->impl);
1231 g_object_unref (cancellable);
1235 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1240 profile_start ("start", NULL);
1242 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1245 for (l = impl->reload_icon_cancellables; l; l = l->next)
1247 GCancellable *cancellable = G_CANCELLABLE (l->data);
1248 g_cancellable_cancel (cancellable);
1250 g_slist_free (impl->reload_icon_cancellables);
1251 impl->reload_icon_cancellables = NULL;
1256 ShortcutType shortcut_type;
1257 gboolean pixbuf_visible;
1260 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1261 SHORTCUTS_COL_DATA, &data,
1262 SHORTCUTS_COL_TYPE, &shortcut_type,
1263 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1269 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1271 GtkFileSystemVolume *volume;
1274 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1275 impl->icon_size, NULL);
1277 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1279 if (g_file_is_native (G_FILE (data)))
1282 struct ReloadIconsData *info;
1283 GtkTreePath *tree_path;
1284 GCancellable *cancellable;
1288 info = g_new0 (struct ReloadIconsData, 1);
1289 info->impl = g_object_ref (impl);
1290 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1291 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1292 gtk_tree_path_free (tree_path);
1294 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1296 shortcuts_reload_icons_get_info_cb,
1298 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1302 GtkIconTheme *icon_theme;
1304 /* Don't call get_info for remote paths to avoid latency and
1306 * If we switch to a better bookmarks file format (XBEL), we
1307 * should use mime info to get a better icon.
1309 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1310 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1311 impl->icon_size, 0, NULL);
1314 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1316 pixbuf = render_search_icon (impl);
1318 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1320 pixbuf = render_recent_icon (impl);
1323 gtk_list_store_set (impl->shortcuts_model, &iter,
1324 SHORTCUTS_COL_PIXBUF, pixbuf,
1328 g_object_unref (pixbuf);
1332 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1336 profile_end ("end", NULL);
1340 shortcuts_find_folder (GtkFileChooserDefault *impl,
1343 GtkTreeSelection *selection;
1347 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1349 g_assert (folder != NULL);
1350 pos = shortcut_find_position (impl, folder);
1353 gtk_tree_selection_unselect_all (selection);
1357 path = gtk_tree_path_new_from_indices (pos, -1);
1358 gtk_tree_selection_select_path (selection, path);
1359 gtk_tree_path_free (path);
1362 /* If a shortcut corresponds to the current folder, selects it */
1364 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1366 shortcuts_find_folder (impl, impl->current_folder);
1369 /* Removes the specified number of rows from the shortcuts list */
1371 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1377 path = gtk_tree_path_new_from_indices (start_row, -1);
1379 for (; n_rows; n_rows--)
1383 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1384 g_assert_not_reached ();
1386 shortcuts_free_row_data (impl, &iter);
1387 gtk_list_store_remove (impl->shortcuts_model, &iter);
1390 gtk_tree_path_free (path);
1394 shortcuts_update_count (GtkFileChooserDefault *impl,
1395 ShortcutsIndex type,
1400 case SHORTCUTS_HOME:
1402 impl->has_home = FALSE;
1404 impl->has_home = TRUE;
1407 case SHORTCUTS_DESKTOP:
1409 impl->has_desktop = FALSE;
1411 impl->has_desktop = TRUE;
1414 case SHORTCUTS_VOLUMES:
1415 impl->num_volumes += value;
1418 case SHORTCUTS_SHORTCUTS:
1419 impl->num_shortcuts += value;
1422 case SHORTCUTS_BOOKMARKS:
1423 impl->num_bookmarks += value;
1426 case SHORTCUTS_CURRENT_FOLDER:
1428 impl->shortcuts_current_folder_active = FALSE;
1430 impl->shortcuts_current_folder_active = TRUE;
1439 struct ShortcutsInsertRequest
1441 GtkFileChooserDefault *impl;
1445 GtkTreeRowReference *row_ref;
1446 ShortcutsIndex type;
1452 get_file_info_finished (GCancellable *cancellable,
1454 const GError *error,
1458 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1462 GCancellable *model_cancellable = NULL;
1463 struct ShortcutsInsertRequest *request = data;
1465 path = gtk_tree_row_reference_get_path (request->row_ref);
1467 /* Handle doesn't exist anymore in the model */
1470 pos = gtk_tree_path_get_indices (path)[0];
1471 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1473 gtk_tree_path_free (path);
1475 /* validate cancellable, else goto out */
1476 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1477 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1479 if (cancellable != model_cancellable)
1482 /* set the cancellable to NULL in the model (we unref later on) */
1483 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1484 SHORTCUTS_COL_CANCELLABLE, NULL,
1492 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1493 shortcuts_update_count (request->impl, request->type, -1);
1495 if (request->type == SHORTCUTS_HOME)
1499 home = g_file_new_for_path (g_get_home_dir ());
1500 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1501 g_object_unref (home);
1503 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1505 /* Remove the current folder separator */
1506 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1507 shortcuts_remove_rows (request->impl, separator_pos, 1);
1513 if (!request->label_copy)
1514 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1515 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1516 request->impl->icon_size);
1518 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1519 SHORTCUTS_COL_PIXBUF, pixbuf,
1520 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1521 SHORTCUTS_COL_NAME, request->label_copy,
1522 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1523 SHORTCUTS_COL_REMOVABLE, request->removable,
1526 if (request->impl->shortcuts_pane_filter_model)
1527 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1530 g_object_unref (pixbuf);
1533 g_object_unref (request->impl);
1534 g_object_unref (request->file);
1535 gtk_tree_row_reference_free (request->row_ref);
1536 g_free (request->label_copy);
1539 if (model_cancellable)
1540 g_object_unref (model_cancellable);
1543 /* FIXME: GtkFileSystem needs a function to split a remote path
1544 * into hostname and path components, or maybe just have a
1545 * gtk_file_system_path_get_display_name().
1547 * This function is also used in gtkfilechooserbutton.c
1550 _gtk_file_chooser_label_for_file (GFile *file)
1552 const gchar *path, *start, *end, *p;
1553 gchar *uri, *host, *label;
1555 uri = g_file_get_uri (file);
1557 start = strstr (uri, "://");
1561 path = strchr (start, '/');
1566 end = uri + strlen (uri);
1570 /* strip username */
1571 p = strchr (start, '@');
1575 p = strchr (start, ':');
1579 host = g_strndup (start, end - start);
1581 /* Translators: the first string is a path and the second string
1582 * is a hostname. Nautilus and the panel contain the same string
1585 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1591 label = g_strdup (uri);
1599 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1600 * inserts a volume. A position of -1 indicates the end of the tree.
1603 shortcuts_insert_file (GtkFileChooserDefault *impl,
1605 ShortcutType shortcut_type,
1606 GtkFileSystemVolume *volume,
1610 ShortcutsIndex type)
1613 GdkPixbuf *pixbuf = NULL;
1614 gpointer data = NULL;
1616 GtkIconTheme *icon_theme;
1618 profile_start ("start shortcut", NULL);
1620 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1623 label_copy = _gtk_file_system_volume_get_display_name (volume);
1624 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1625 impl->icon_size, NULL);
1627 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1629 if (g_file_is_native (file))
1631 struct ShortcutsInsertRequest *request;
1632 GCancellable *cancellable;
1635 request = g_new0 (struct ShortcutsInsertRequest, 1);
1636 request->impl = g_object_ref (impl);
1637 request->file = g_object_ref (file);
1638 request->name_only = TRUE;
1639 request->removable = removable;
1641 request->type = type;
1643 request->label_copy = g_strdup (label);
1646 gtk_list_store_append (impl->shortcuts_model, &iter);
1648 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1650 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1651 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1652 gtk_tree_path_free (p);
1654 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1655 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1656 get_file_info_finished, request);
1658 gtk_list_store_set (impl->shortcuts_model, &iter,
1659 SHORTCUTS_COL_DATA, g_object_ref (file),
1660 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1661 SHORTCUTS_COL_CANCELLABLE, cancellable,
1664 shortcuts_update_count (impl, type, 1);
1670 /* Don't call get_info for remote paths to avoid latency and
1673 data = g_object_ref (file);
1675 label_copy = g_strdup (label);
1677 label_copy = _gtk_file_chooser_label_for_file (file);
1679 /* If we switch to a better bookmarks file format (XBEL), we
1680 * should use mime info to get a better icon.
1682 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1683 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1684 impl->icon_size, 0, NULL);
1689 g_assert_not_reached ();
1695 gtk_list_store_append (impl->shortcuts_model, &iter);
1697 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1699 shortcuts_update_count (impl, type, 1);
1701 gtk_list_store_set (impl->shortcuts_model, &iter,
1702 SHORTCUTS_COL_PIXBUF, pixbuf,
1703 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1704 SHORTCUTS_COL_NAME, label_copy,
1705 SHORTCUTS_COL_DATA, data,
1706 SHORTCUTS_COL_TYPE, shortcut_type,
1707 SHORTCUTS_COL_REMOVABLE, removable,
1708 SHORTCUTS_COL_CANCELLABLE, NULL,
1711 if (impl->shortcuts_pane_filter_model)
1712 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1714 g_free (label_copy);
1717 g_object_unref (pixbuf);
1719 profile_end ("end", NULL);
1723 shortcuts_append_search (GtkFileChooserDefault *impl)
1728 pixbuf = render_search_icon (impl);
1730 gtk_list_store_append (impl->shortcuts_model, &iter);
1731 gtk_list_store_set (impl->shortcuts_model, &iter,
1732 SHORTCUTS_COL_PIXBUF, pixbuf,
1733 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1734 SHORTCUTS_COL_NAME, _("Search"),
1735 SHORTCUTS_COL_DATA, NULL,
1736 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1737 SHORTCUTS_COL_REMOVABLE, FALSE,
1741 g_object_unref (pixbuf);
1743 impl->has_search = TRUE;
1747 shortcuts_append_recent (GtkFileChooserDefault *impl)
1752 pixbuf = render_recent_icon (impl);
1754 gtk_list_store_append (impl->shortcuts_model, &iter);
1755 gtk_list_store_set (impl->shortcuts_model, &iter,
1756 SHORTCUTS_COL_PIXBUF, pixbuf,
1757 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1758 SHORTCUTS_COL_NAME, _("Recently Used"),
1759 SHORTCUTS_COL_DATA, NULL,
1760 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1761 SHORTCUTS_COL_REMOVABLE, FALSE,
1765 g_object_unref (pixbuf);
1768 /* Appends an item for the user's home directory to the shortcuts model */
1770 shortcuts_append_home (GtkFileChooserDefault *impl)
1772 const char *home_path;
1775 profile_start ("start", NULL);
1777 home_path = g_get_home_dir ();
1778 if (home_path == NULL)
1780 profile_end ("end - no home directory!?", NULL);
1784 home = g_file_new_for_path (home_path);
1785 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1786 impl->has_home = TRUE;
1788 g_object_unref (home);
1790 profile_end ("end", NULL);
1793 /* Appends the ~/Desktop directory to the shortcuts model */
1795 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1800 profile_start ("start", NULL);
1802 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1803 /* "To disable a directory, point it to the homedir."
1804 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1806 if (!g_strcmp0 (name, g_get_home_dir ()))
1808 profile_end ("end", NULL);
1812 file = g_file_new_for_path (name);
1813 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1814 impl->has_desktop = TRUE;
1816 /* We do not actually pop up an error dialog if there is no desktop directory
1817 * because some people may really not want to have one.
1820 g_object_unref (file);
1822 profile_end ("end", NULL);
1825 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1827 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1834 profile_start ("start", NULL);
1836 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1839 for (; bookmarks; bookmarks = bookmarks->next)
1843 file = bookmarks->data;
1845 if (impl->local_only && !g_file_is_native (file))
1848 if (shortcut_find_position (impl, file) != -1)
1851 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1853 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1859 profile_end ("end", NULL);
1861 return num_inserted;
1864 /* Returns the index for the corresponding item in the shortcuts bar */
1866 shortcuts_get_index (GtkFileChooserDefault *impl,
1867 ShortcutsIndex where)
1873 if (where == SHORTCUTS_SEARCH)
1876 n += impl->has_search ? 1 : 0;
1878 if (where == SHORTCUTS_RECENT)
1881 n += 1; /* we always have the recently-used item */
1883 if (where == SHORTCUTS_RECENT_SEPARATOR)
1886 n += 1; /* we always have the separator after the recently-used item */
1888 if (where == SHORTCUTS_HOME)
1891 n += impl->has_home ? 1 : 0;
1893 if (where == SHORTCUTS_DESKTOP)
1896 n += impl->has_desktop ? 1 : 0;
1898 if (where == SHORTCUTS_VOLUMES)
1901 n += impl->num_volumes;
1903 if (where == SHORTCUTS_SHORTCUTS)
1906 n += impl->num_shortcuts;
1908 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1911 /* If there are no bookmarks there won't be a separator */
1912 n += (impl->num_bookmarks > 0) ? 1 : 0;
1914 if (where == SHORTCUTS_BOOKMARKS)
1917 n += impl->num_bookmarks;
1919 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1924 if (where == SHORTCUTS_CURRENT_FOLDER)
1927 g_assert_not_reached ();
1934 /* Adds all the file system volumes to the shortcuts model */
1936 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1941 gboolean old_changing_folders;
1943 profile_start ("start", NULL);
1945 old_changing_folders = impl->changing_folder;
1946 impl->changing_folder = TRUE;
1948 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1949 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1950 impl->num_volumes = 0;
1952 list = _gtk_file_system_list_volumes (impl->file_system);
1956 for (l = list; l; l = l->next)
1958 GtkFileSystemVolume *volume;
1962 if (impl->local_only)
1964 if (_gtk_file_system_volume_is_mounted (volume))
1967 gboolean base_is_native = TRUE;
1969 base_file = _gtk_file_system_volume_get_root (volume);
1970 if (base_file != NULL)
1972 base_is_native = g_file_is_native (base_file);
1973 g_object_unref (base_file);
1976 if (!base_is_native)
1981 shortcuts_insert_file (impl,
1983 SHORTCUT_TYPE_VOLUME,
1984 _gtk_file_system_volume_ref (volume),
1992 impl->num_volumes = n;
1993 g_slist_free (list);
1995 if (impl->shortcuts_pane_filter_model)
1996 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1998 impl->changing_folder = old_changing_folders;
2000 profile_end ("end", NULL);
2003 /* Inserts a separator node in the shortcuts list */
2005 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2006 ShortcutsIndex where)
2010 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2011 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2012 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2014 gtk_list_store_insert (impl->shortcuts_model, &iter,
2015 shortcuts_get_index (impl, where));
2016 gtk_list_store_set (impl->shortcuts_model, &iter,
2017 SHORTCUTS_COL_PIXBUF, NULL,
2018 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2019 SHORTCUTS_COL_NAME, NULL,
2020 SHORTCUTS_COL_DATA, NULL,
2021 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2025 /* Updates the list of bookmarks */
2027 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2030 gboolean old_changing_folders;
2032 GFile *list_selected = NULL;
2033 ShortcutType shortcut_type;
2036 profile_start ("start", NULL);
2038 old_changing_folders = impl->changing_folder;
2039 impl->changing_folder = TRUE;
2041 if (shortcuts_get_selected (impl, &iter))
2043 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2045 SHORTCUTS_COL_DATA, &col_data,
2046 SHORTCUTS_COL_TYPE, &shortcut_type,
2049 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2050 list_selected = g_object_ref (col_data);
2053 if (impl->num_bookmarks > 0)
2054 shortcuts_remove_rows (impl,
2055 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2056 impl->num_bookmarks + 1);
2058 impl->num_bookmarks = 0;
2059 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2061 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2062 shortcuts_append_bookmarks (impl, bookmarks);
2063 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2064 g_slist_free (bookmarks);
2066 if (impl->num_bookmarks == 0)
2067 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2069 if (impl->shortcuts_pane_filter_model)
2070 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2074 shortcuts_find_folder (impl, list_selected);
2075 g_object_unref (list_selected);
2078 impl->changing_folder = old_changing_folders;
2080 profile_end ("end", NULL);
2083 /* Appends a separator and a row to the shortcuts list for the current folder */
2085 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2089 g_assert (!impl->shortcuts_current_folder_active);
2091 g_assert (impl->current_folder != NULL);
2093 pos = shortcut_find_position (impl, impl->current_folder);
2096 GtkFileSystemVolume *volume;
2100 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2103 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2105 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2107 base_file = _gtk_file_system_volume_get_root (volume);
2111 if (base_file && g_file_equal (base_file, impl->current_folder))
2112 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2114 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2117 g_object_unref (base_file);
2121 /* Updates the current folder row in the shortcuts model */
2123 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2127 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2129 if (impl->shortcuts_current_folder_active)
2131 shortcuts_remove_rows (impl, pos, 2);
2132 impl->shortcuts_current_folder_active = FALSE;
2135 shortcuts_add_current_folder (impl);
2138 /* Filter function used for the shortcuts filter model */
2140 shortcuts_pane_filter_cb (GtkTreeModel *model,
2144 GtkFileChooserDefault *impl;
2148 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2150 path = gtk_tree_model_get_path (model, iter);
2154 pos = *gtk_tree_path_get_indices (path);
2155 gtk_tree_path_free (path);
2157 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2160 /* Creates the list model for shortcuts */
2162 shortcuts_model_create (GtkFileChooserDefault *impl)
2164 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2165 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2166 GDK_TYPE_PIXBUF, /* pixbuf */
2167 G_TYPE_STRING, /* name */
2168 G_TYPE_POINTER, /* path or volume */
2169 G_TYPE_INT, /* ShortcutType */
2170 G_TYPE_BOOLEAN, /* removable */
2171 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2172 G_TYPE_POINTER); /* GCancellable */
2174 shortcuts_append_search (impl);
2176 if (impl->recent_manager)
2178 shortcuts_append_recent (impl);
2179 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2182 if (impl->file_system)
2184 shortcuts_append_home (impl);
2185 shortcuts_append_desktop (impl);
2186 shortcuts_add_volumes (impl);
2189 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2190 GTK_TREE_MODEL (impl->shortcuts_model),
2193 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2194 shortcuts_pane_filter_cb,
2199 /* Callback used when the "New Folder" button is clicked */
2201 new_folder_button_clicked (GtkButton *button,
2202 GtkFileChooserDefault *impl)
2207 if (!impl->browse_files_model)
2208 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2210 /* Prevent button from being clicked twice */
2211 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2213 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2215 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2216 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2217 path, impl->list_name_column,
2220 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2221 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2223 impl->list_name_column,
2226 gtk_tree_path_free (path);
2230 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2234 source = g_idle_source_new ();
2235 g_source_set_closure (source,
2236 g_cclosure_new_object (callback, G_OBJECT (impl)));
2237 g_source_attach (source, NULL);
2242 /* Idle handler for creating a new folder after editing its name cell, or for
2243 * canceling the editing.
2246 edited_idle_cb (GtkFileChooserDefault *impl)
2248 GDK_THREADS_ENTER ();
2250 g_source_destroy (impl->edited_idle);
2251 impl->edited_idle = NULL;
2253 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2254 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2256 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2258 if (impl->edited_new_text /* not cancelled? */
2259 && (strlen (impl->edited_new_text) != 0)
2260 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2262 GError *error = NULL;
2265 file = g_file_get_child_for_display_name (impl->current_folder,
2266 impl->edited_new_text,
2270 GError *error = NULL;
2272 if (g_file_make_directory (file, NULL, &error))
2273 change_folder_and_display_error (impl, file, FALSE);
2275 error_creating_folder_dialog (impl, file, error);
2277 g_object_unref (file);
2280 error_creating_folder_dialog (impl, file, error);
2282 g_free (impl->edited_new_text);
2283 impl->edited_new_text = NULL;
2286 GDK_THREADS_LEAVE ();
2292 queue_edited_idle (GtkFileChooserDefault *impl,
2293 const gchar *new_text)
2295 /* We create the folder in an idle handler so that we don't modify the tree
2299 if (!impl->edited_idle)
2300 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2302 g_free (impl->edited_new_text);
2303 impl->edited_new_text = g_strdup (new_text);
2306 /* Callback used from the text cell renderer when the new folder is named */
2308 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2310 const gchar *new_text,
2311 GtkFileChooserDefault *impl)
2313 /* work around bug #154921 */
2314 g_object_set (cell_renderer_text,
2315 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2316 queue_edited_idle (impl, new_text);
2319 /* Callback used from the text cell renderer when the new folder edition gets
2323 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2324 GtkFileChooserDefault *impl)
2326 /* work around bug #154921 */
2327 g_object_set (cell_renderer_text,
2328 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2329 queue_edited_idle (impl, NULL);
2332 /* Creates the widgets for the filter combo box */
2334 filter_create (GtkFileChooserDefault *impl)
2336 GtkCellRenderer *cell;
2339 impl->filter_combo = gtk_combo_box_text_new ();
2340 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2342 /* Get the combo's text renderer and set ellipsize parameters */
2343 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2347 g_object_set (G_OBJECT (cell),
2348 "ellipsize", PANGO_ELLIPSIZE_END,
2351 g_list_free (cells);
2353 g_signal_connect (impl->filter_combo, "changed",
2354 G_CALLBACK (filter_combo_changed), impl);
2356 gtk_widget_set_tooltip_text (impl->filter_combo,
2357 _("Select which types of files are shown"));
2359 return impl->filter_combo;
2363 toolbutton_new (GtkFileChooserDefault *impl,
2372 item = gtk_tool_button_new (NULL, NULL);
2373 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2374 gtk_widget_show (image);
2375 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2377 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2378 g_signal_connect (item, "clicked", callback, impl);
2381 gtk_widget_show (GTK_WIDGET (item));
2383 return GTK_WIDGET (item);
2386 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2388 shortcut_find_position (GtkFileChooserDefault *impl,
2393 int current_folder_separator_idx;
2395 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2398 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2401 /* FIXME: is this still needed? */
2402 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2406 for (i = 0; i < current_folder_separator_idx; i++)
2409 ShortcutType shortcut_type;
2411 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2412 SHORTCUTS_COL_DATA, &col_data,
2413 SHORTCUTS_COL_TYPE, &shortcut_type,
2418 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2420 GtkFileSystemVolume *volume;
2425 base_file = _gtk_file_system_volume_get_root (volume);
2427 exists = base_file && g_file_equal (file, base_file);
2430 g_object_unref (base_file);
2435 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2439 model_file = col_data;
2441 if (model_file && g_file_equal (model_file, file))
2446 if (i < current_folder_separator_idx - 1)
2448 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2449 g_assert_not_reached ();
2456 /* Tries to add a bookmark from a path name */
2458 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2464 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2466 if (shortcut_find_position (impl, file) != -1)
2470 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2472 error_adding_bookmark_dialog (impl, file, error);
2480 add_bookmark_foreach_cb (GtkTreeModel *model,
2485 GtkFileChooserDefault *impl;
2488 impl = (GtkFileChooserDefault *) data;
2490 gtk_tree_model_get (model, iter,
2491 MODEL_COL_FILE, &file,
2494 shortcuts_add_bookmark_from_file (impl, file, -1);
2496 g_object_unref (file);
2499 /* Adds a bookmark from the currently selected item in the file list */
2501 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2503 GtkTreeSelection *selection;
2505 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2507 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2508 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2510 gtk_tree_selection_selected_foreach (selection,
2511 add_bookmark_foreach_cb,
2515 /* Callback used when the "Add bookmark" button is clicked */
2517 add_bookmark_button_clicked_cb (GtkButton *button,
2518 GtkFileChooserDefault *impl)
2520 bookmarks_add_selected_folder (impl);
2523 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2524 * returns FALSE if no shortcut is selected.
2527 shortcuts_get_selected (GtkFileChooserDefault *impl,
2530 GtkTreeSelection *selection;
2531 GtkTreeIter parent_iter;
2533 if (!impl->browse_shortcuts_tree_view)
2536 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2538 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2541 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2547 /* Removes the selected bookmarks */
2549 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2557 if (!shortcuts_get_selected (impl, &iter))
2560 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2561 SHORTCUTS_COL_DATA, &col_data,
2562 SHORTCUTS_COL_REMOVABLE, &removable,
2568 g_assert (col_data != NULL);
2573 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2574 error_removing_bookmark_dialog (impl, file, error);
2577 /* Callback used when the "Remove bookmark" button is clicked */
2579 remove_bookmark_button_clicked_cb (GtkButton *button,
2580 GtkFileChooserDefault *impl)
2582 remove_selected_bookmarks (impl);
2585 struct selection_check_closure {
2586 GtkFileChooserDefault *impl;
2589 gboolean all_folders;
2592 /* Used from gtk_tree_selection_selected_foreach() */
2594 selection_check_foreach_cb (GtkTreeModel *model,
2599 struct selection_check_closure *closure;
2603 gtk_tree_model_get (model, iter,
2604 MODEL_COL_FILE, &file,
2605 MODEL_COL_IS_FOLDER, &is_folder,
2611 g_object_unref (file);
2614 closure->num_selected++;
2616 closure->all_folders = closure->all_folders && is_folder;
2617 closure->all_files = closure->all_files && !is_folder;
2620 /* Checks whether the selected items in the file list are all files or all folders */
2622 selection_check (GtkFileChooserDefault *impl,
2624 gboolean *all_files,
2625 gboolean *all_folders)
2627 struct selection_check_closure closure;
2628 GtkTreeSelection *selection;
2630 closure.impl = impl;
2631 closure.num_selected = 0;
2632 closure.all_files = TRUE;
2633 closure.all_folders = TRUE;
2635 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2636 gtk_tree_selection_selected_foreach (selection,
2637 selection_check_foreach_cb,
2640 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2643 *num_selected = closure.num_selected;
2646 *all_files = closure.all_files;
2649 *all_folders = closure.all_folders;
2652 struct get_selected_file_closure {
2653 GtkFileChooserDefault *impl;
2658 get_selected_file_foreach_cb (GtkTreeModel *model,
2663 struct get_selected_file_closure *closure = data;
2667 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2668 g_object_unref (closure->file);
2669 closure->file = NULL;
2672 gtk_tree_model_get (model, iter,
2673 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2677 /* Returns a selected path from the file list */
2679 get_selected_file (GtkFileChooserDefault *impl)
2681 struct get_selected_file_closure closure;
2682 GtkTreeSelection *selection;
2684 closure.impl = impl;
2685 closure.file = NULL;
2687 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2688 gtk_tree_selection_selected_foreach (selection,
2689 get_selected_file_foreach_cb,
2692 return closure.file;
2696 GtkFileChooserDefault *impl;
2698 } UpdateTooltipData;
2701 update_tooltip (GtkTreeModel *model,
2706 UpdateTooltipData *udata = data;
2708 if (udata->tip == NULL)
2710 gchar *display_name;
2712 gtk_tree_model_get (model, iter,
2713 MODEL_COL_NAME, &display_name,
2716 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2718 g_free (display_name);
2723 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2724 * if there are no selected items *and* the current folder is not in the
2725 * bookmarks list. De-sensitize the button otherwise.
2728 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2731 gboolean all_folders;
2735 selection_check (impl, &num_selected, NULL, &all_folders);
2737 if (num_selected == 0)
2738 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2739 else if (num_selected == 1)
2743 file = get_selected_file (impl);
2744 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2746 g_object_unref (file);
2749 active = all_folders;
2751 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2753 if (impl->browse_files_popup_menu_add_shortcut_item)
2754 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2755 (num_selected == 0) ? FALSE : active);
2759 if (num_selected == 0)
2760 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2761 else if (num_selected > 1)
2762 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2765 GtkTreeSelection *selection;
2766 UpdateTooltipData data;
2768 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2771 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2775 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2780 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2781 * bookmark row is selected in the shortcuts tree.
2784 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2787 gboolean removable = FALSE;
2791 if (shortcuts_get_selected (impl, &iter))
2793 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2794 SHORTCUTS_COL_REMOVABLE, &removable,
2795 SHORTCUTS_COL_NAME, &name,
2797 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2800 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2802 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2804 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2808 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2809 _("Remove the selected bookmark"));
2814 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2817 gboolean removable = FALSE;
2819 if (impl->browse_shortcuts_popup_menu == NULL)
2822 if (shortcuts_get_selected (impl, &iter))
2823 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2824 SHORTCUTS_COL_REMOVABLE, &removable,
2827 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2828 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2831 /* GtkWidget::drag-begin handler for the shortcuts list. */
2833 shortcuts_drag_begin_cb (GtkWidget *widget,
2834 GdkDragContext *context,
2835 GtkFileChooserDefault *impl)
2838 impl->shortcuts_drag_context = g_object_ref (context);
2843 /* Removes the idle handler for outside drags */
2845 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2847 if (!impl->shortcuts_drag_outside_idle)
2850 g_source_destroy (impl->shortcuts_drag_outside_idle);
2851 impl->shortcuts_drag_outside_idle = NULL;
2855 /* GtkWidget::drag-end handler for the shortcuts list. */
2857 shortcuts_drag_end_cb (GtkWidget *widget,
2858 GdkDragContext *context,
2859 GtkFileChooserDefault *impl)
2862 g_object_unref (impl->shortcuts_drag_context);
2864 shortcuts_cancel_drag_outside_idle (impl);
2866 if (!impl->shortcuts_drag_outside)
2869 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2871 impl->shortcuts_drag_outside = FALSE;
2875 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2877 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2878 GdkDragContext *context,
2879 GtkFileChooserDefault *impl)
2881 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2884 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2888 shortcuts_drag_leave_cb (GtkWidget *widget,
2889 GdkDragContext *context,
2891 GtkFileChooserDefault *impl)
2894 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2896 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2900 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2902 GTK_TREE_VIEW_DROP_BEFORE);
2904 g_signal_stop_emission_by_name (widget, "drag-leave");
2907 /* Computes the appropriate row and position for dropping */
2909 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2913 GtkTreeViewDropPosition *pos)
2915 GtkTreeView *tree_view;
2916 GtkTreeViewColumn *column;
2920 int bookmarks_index;
2921 int header_height = 0;
2923 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2925 if (gtk_tree_view_get_headers_visible (tree_view))
2926 header_height = _gtk_tree_view_get_header_height (tree_view);
2928 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2930 if (!gtk_tree_view_get_path_at_pos (tree_view,
2938 row = bookmarks_index + impl->num_bookmarks - 1;
2939 *path = gtk_tree_path_new_from_indices (row, -1);
2940 *pos = GTK_TREE_VIEW_DROP_AFTER;
2944 row = *gtk_tree_path_get_indices (*path);
2945 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2946 gtk_tree_path_free (*path);
2948 if (row < bookmarks_index)
2950 row = bookmarks_index;
2951 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2953 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2955 row = bookmarks_index + impl->num_bookmarks - 1;
2956 *pos = GTK_TREE_VIEW_DROP_AFTER;
2960 if (cell_y < cell.height / 2)
2961 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2963 *pos = GTK_TREE_VIEW_DROP_AFTER;
2966 *path = gtk_tree_path_new_from_indices (row, -1);
2969 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2970 * implement the destination side of DnD by hand, due to limitations in
2971 * GtkTreeView's DnD API.
2974 shortcuts_drag_motion_cb (GtkWidget *widget,
2975 GdkDragContext *context,
2979 GtkFileChooserDefault *impl)
2982 GtkTreeViewDropPosition pos;
2983 GdkDragAction action;
2986 if (gtk_drag_get_source_widget (context) == widget)
2988 shortcuts_cancel_drag_outside_idle (impl);
2990 if (impl->shortcuts_drag_outside)
2992 shortcuts_drag_set_delete_cursor (impl, FALSE);
2993 impl->shortcuts_drag_outside = FALSE;
2998 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
2999 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3000 action = GDK_ACTION_COPY;
3001 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3002 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3003 action = GDK_ACTION_MOVE;
3010 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3011 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3012 gtk_tree_path_free (path);
3016 g_signal_stop_emission_by_name (widget, "drag-motion");
3020 gdk_drag_status (context, action, time_);
3027 /* GtkWidget::drag-drop handler for the shortcuts list. */
3029 shortcuts_drag_drop_cb (GtkWidget *widget,
3030 GdkDragContext *context,
3034 GtkFileChooserDefault *impl)
3037 shortcuts_cancel_drag_outside_idle (impl);
3040 g_signal_stop_emission_by_name (widget, "drag-drop");
3044 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3046 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3047 GtkSelectionData *selection_data,
3053 uris = gtk_selection_data_get_uris (selection_data);
3057 for (i = 0; uris[i]; i++)
3063 file = g_file_new_for_uri (uri);
3065 if (shortcuts_add_bookmark_from_file (impl, file, position))
3068 g_object_unref (file);
3074 /* Reorders the selected bookmark to the specified position */
3076 shortcuts_reorder (GtkFileChooserDefault *impl,
3081 ShortcutType shortcut_type;
3084 int bookmarks_index;
3089 /* Get the selected path */
3091 if (!shortcuts_get_selected (impl, &iter))
3092 g_assert_not_reached ();
3094 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3095 old_position = *gtk_tree_path_get_indices (path);
3096 gtk_tree_path_free (path);
3098 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3099 old_position -= bookmarks_index;
3100 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3102 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3103 SHORTCUTS_COL_NAME, &name,
3104 SHORTCUTS_COL_DATA, &col_data,
3105 SHORTCUTS_COL_TYPE, &shortcut_type,
3107 g_assert (col_data != NULL);
3108 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3111 g_object_ref (file); /* removal below will free file, so we need a new ref */
3113 /* Remove the path from the old position and insert it in the new one */
3115 if (new_position > old_position)
3118 if (old_position == new_position)
3122 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3124 shortcuts_add_bookmark_from_file (impl, file, new_position);
3125 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3128 error_adding_bookmark_dialog (impl, file, error);
3132 g_object_unref (file);
3136 /* Callback used when we get the drag data for the bookmarks list. We add the
3137 * received URIs as bookmarks if they are folders.
3140 shortcuts_drag_data_received_cb (GtkWidget *widget,
3141 GdkDragContext *context,
3144 GtkSelectionData *selection_data,
3149 GtkFileChooserDefault *impl;
3150 GtkTreePath *tree_path;
3151 GtkTreeViewDropPosition tree_pos;
3154 int bookmarks_index;
3156 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3158 /* Compute position */
3160 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3162 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3163 position = *gtk_tree_path_get_indices (tree_path);
3164 gtk_tree_path_free (tree_path);
3166 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3169 g_assert (position >= bookmarks_index);
3170 position -= bookmarks_index;
3172 target = gtk_selection_data_get_target (selection_data);
3174 if (gtk_targets_include_uri (&target, 1))
3175 shortcuts_drop_uris (impl, selection_data, position);
3176 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3177 shortcuts_reorder (impl, position);
3179 g_signal_stop_emission_by_name (widget, "drag-data-received");
3182 /* Callback used to display a tooltip in the shortcuts tree */
3184 shortcuts_query_tooltip_cb (GtkWidget *widget,
3187 gboolean keyboard_mode,
3188 GtkTooltip *tooltip,
3189 GtkFileChooserDefault *impl)
3191 GtkTreeModel *model;
3194 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3202 ShortcutType shortcut_type;
3204 gtk_tree_model_get (model, &iter,
3205 SHORTCUTS_COL_DATA, &col_data,
3206 SHORTCUTS_COL_TYPE, &shortcut_type,
3209 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3211 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3213 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3218 file = G_FILE (col_data);
3219 parse_name = g_file_get_parse_name (file);
3221 gtk_tooltip_set_text (tooltip, parse_name);
3223 g_free (parse_name);
3227 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3231 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3241 /* Callback used when the selection in the shortcuts tree changes */
3243 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3244 GtkFileChooserDefault *impl)
3247 GtkTreeIter child_iter;
3249 bookmarks_check_remove_sensitivity (impl);
3250 shortcuts_check_popup_sensitivity (impl);
3252 if (impl->changing_folder)
3255 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3257 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3260 shortcuts_activate_iter (impl, &child_iter);
3265 shortcuts_row_separator_func (GtkTreeModel *model,
3269 ShortcutType shortcut_type;
3271 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3273 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3277 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3279 GtkFileChooserDefault *impl)
3283 /* don't screw up focus switching with Tab */
3284 if (event->keyval == GDK_KEY_Tab
3285 || event->keyval == GDK_KEY_KP_Tab
3286 || event->keyval == GDK_KEY_ISO_Left_Tab
3287 || event->length < 1)
3290 if (impl->location_entry)
3291 entry = impl->location_entry;
3292 else if (impl->search_entry)
3293 entry = impl->search_entry;
3299 gtk_widget_grab_focus (entry);
3300 return gtk_widget_event (entry, (GdkEvent *) event);
3306 /* Callback used when the file list's popup menu is detached */
3308 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3311 GtkFileChooserDefault *impl;
3313 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3314 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3316 impl->browse_shortcuts_popup_menu = NULL;
3317 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3318 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3322 remove_shortcut_cb (GtkMenuItem *item,
3323 GtkFileChooserDefault *impl)
3325 remove_selected_bookmarks (impl);
3328 /* Rename the selected bookmark */
3330 rename_selected_bookmark (GtkFileChooserDefault *impl)
3334 GtkTreeViewColumn *column;
3335 GtkCellRenderer *cell;
3338 if (shortcuts_get_selected (impl, &iter))
3340 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3341 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3342 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3343 cell = g_list_nth_data (renderers, 1);
3344 g_list_free (renderers);
3345 g_object_set (cell, "editable", TRUE, NULL);
3346 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3347 path, column, cell, TRUE);
3348 gtk_tree_path_free (path);
3353 rename_shortcut_cb (GtkMenuItem *item,
3354 GtkFileChooserDefault *impl)
3356 rename_selected_bookmark (impl);
3359 /* Constructs the popup menu for the file list if needed */
3361 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3365 if (impl->browse_shortcuts_popup_menu)
3368 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3369 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3370 impl->browse_shortcuts_tree_view,
3371 shortcuts_popup_menu_detach_cb);
3373 item = gtk_image_menu_item_new_with_label (_("Remove"));
3374 impl->browse_shortcuts_popup_menu_remove_item = item;
3375 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3376 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3377 g_signal_connect (item, "activate",
3378 G_CALLBACK (remove_shortcut_cb), impl);
3379 gtk_widget_show (item);
3380 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3382 item = gtk_menu_item_new_with_label (_("Rename..."));
3383 impl->browse_shortcuts_popup_menu_rename_item = item;
3384 g_signal_connect (item, "activate",
3385 G_CALLBACK (rename_shortcut_cb), impl);
3386 gtk_widget_show (item);
3387 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3391 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3393 shortcuts_build_popup_menu (impl);
3394 shortcuts_check_popup_sensitivity (impl);
3398 popup_position_func (GtkMenu *menu,
3402 gpointer user_data);
3405 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3406 GdkEventButton *event)
3408 shortcuts_update_popup_menu (impl);
3410 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3411 NULL, NULL, NULL, NULL,
3412 event->button, event->time);
3415 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3417 popup_position_func, impl->browse_shortcuts_tree_view,
3418 0, GDK_CURRENT_TIME);
3419 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3424 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3426 shortcuts_popup_menu_cb (GtkWidget *widget,
3427 GtkFileChooserDefault *impl)
3429 shortcuts_popup_menu (impl, NULL);
3433 /* Callback used when a button is pressed on the shortcuts list.
3434 * We trap button 3 to bring up a popup menu.
3437 shortcuts_button_press_event_cb (GtkWidget *widget,
3438 GdkEventButton *event,
3439 GtkFileChooserDefault *impl)
3441 static gboolean in_press = FALSE;
3447 if (event->button != 3)
3451 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3457 shortcuts_popup_menu (impl, event);
3462 shortcuts_edited (GtkCellRenderer *cell,
3465 GtkFileChooserDefault *impl)
3471 g_object_set (cell, "editable", FALSE, NULL);
3473 path = gtk_tree_path_new_from_string (path_string);
3474 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3475 g_assert_not_reached ();
3477 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3478 SHORTCUTS_COL_DATA, &shortcut,
3480 gtk_tree_path_free (path);
3482 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3486 shortcuts_editing_canceled (GtkCellRenderer *cell,
3487 GtkFileChooserDefault *impl)
3489 g_object_set (cell, "editable", FALSE, NULL);
3492 /* Creates the widgets for the shortcuts and bookmarks tree */
3494 shortcuts_list_create (GtkFileChooserDefault *impl)
3497 GtkTreeSelection *selection;
3498 GtkTreeViewColumn *column;
3499 GtkCellRenderer *renderer;
3501 /* Target types for dragging a row to/from the shortcuts list */
3502 const GtkTargetEntry tree_model_row_targets[] = {
3503 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3506 /* Scrolled window */
3508 swin = gtk_scrolled_window_new (NULL, NULL);
3509 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3510 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3511 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3513 gtk_widget_show (swin);
3516 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3517 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3518 GTK_STYLE_CLASS_SIDEBAR);
3519 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3520 #ifdef PROFILE_FILE_CHOOSER
3521 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3524 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3526 * 1. user brings up a SAVE dialog
3527 * 2. user clicks on a shortcut in the shortcuts pane
3528 * 3. user starts typing a filename
3530 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3531 * support interactive search. However, we'd rather focus the location entry
3532 * so that the user can type *there*.
3534 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3535 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3536 * if the user starts typing while the focus is in the shortcuts pane.
3538 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3539 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3541 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3542 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3543 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3544 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3545 /* Accessible object name for the file chooser's shortcuts pane */
3546 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3548 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3550 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3552 tree_model_row_targets,
3553 G_N_ELEMENTS (tree_model_row_targets),
3556 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3557 GTK_DEST_DEFAULT_ALL,
3558 tree_model_row_targets,
3559 G_N_ELEMENTS (tree_model_row_targets),
3560 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3561 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3563 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3564 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3565 gtk_tree_selection_set_select_function (selection,
3566 shortcuts_select_func,
3569 g_signal_connect (selection, "changed",
3570 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3572 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3573 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3575 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3576 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3577 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3578 G_CALLBACK (shortcuts_drag_end_cb), impl);
3579 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3580 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3582 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3583 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3584 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3585 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3586 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3587 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3588 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3589 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3591 /* Support tooltips */
3592 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3593 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3594 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3596 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3597 gtk_widget_show (impl->browse_shortcuts_tree_view);
3601 column = gtk_tree_view_column_new ();
3602 /* Column header for the file chooser's shortcuts pane */
3603 gtk_tree_view_column_set_title (column, _("_Places"));
3605 renderer = gtk_cell_renderer_pixbuf_new ();
3606 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3607 gtk_tree_view_column_set_attributes (column, renderer,
3608 "pixbuf", SHORTCUTS_COL_PIXBUF,
3609 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3612 renderer = gtk_cell_renderer_text_new ();
3613 g_object_set (renderer,
3615 "ellipsize", PANGO_ELLIPSIZE_END,
3617 g_signal_connect (renderer, "edited",
3618 G_CALLBACK (shortcuts_edited), impl);
3619 g_signal_connect (renderer, "editing-canceled",
3620 G_CALLBACK (shortcuts_editing_canceled), impl);
3621 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3622 gtk_tree_view_column_set_attributes (column, renderer,
3623 "text", SHORTCUTS_COL_NAME,
3626 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3627 shortcuts_row_separator_func,
3630 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3635 /* Creates the widgets for the shortcuts/bookmarks pane */
3637 shortcuts_pane_create (GtkFileChooserDefault *impl,
3638 GtkSizeGroup *size_group)
3643 GtkStyleContext *context;
3646 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3647 gtk_widget_show (vbox);
3649 /* Shortcuts tree */
3651 widget = shortcuts_list_create (impl);
3653 gtk_size_group_add_widget (size_group, widget);
3654 context = gtk_widget_get_style_context (widget);
3655 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3657 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3659 /* Box for buttons */
3661 toolbar = gtk_toolbar_new ();
3662 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3664 context = gtk_widget_get_style_context (toolbar);
3665 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3666 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3668 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3669 gtk_widget_show (toolbar);
3671 /* Add bookmark button */
3672 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3673 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3677 G_CALLBACK (add_bookmark_button_clicked_cb));
3678 g_object_unref (icon);
3680 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3681 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3682 _("Add the selected folder to the Bookmarks"));
3684 /* Remove bookmark button */
3685 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3686 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3690 G_CALLBACK (remove_bookmark_button_clicked_cb));
3691 g_object_unref (icon);
3692 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3693 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3694 _("Remove the selected bookmark"));
3700 key_is_left_or_right (GdkEventKey *event)
3704 modifiers = gtk_accelerator_get_default_mod_mask ();
3706 return ((event->keyval == GDK_KEY_Right
3707 || event->keyval == GDK_KEY_KP_Right
3708 || event->keyval == GDK_KEY_Left
3709 || event->keyval == GDK_KEY_KP_Left)
3710 && (event->state & modifiers) == 0);
3713 /* Handles key press events on the file list, so that we can trap Enter to
3714 * activate the default button on our own. Also, checks to see if '/' has been
3715 * pressed. See comment by tree_view_keybinding_cb() for more details.
3718 browse_files_key_press_event_cb (GtkWidget *widget,
3722 GtkFileChooserDefault *impl;
3725 impl = (GtkFileChooserDefault *) data;
3727 modifiers = gtk_accelerator_get_default_mod_mask ();
3729 if ((event->keyval == GDK_KEY_slash
3730 || event->keyval == GDK_KEY_KP_Divide
3732 || event->keyval == GDK_KEY_asciitilde
3734 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3736 location_popup_handler (impl, event->string);
3740 if (key_is_left_or_right (event))
3742 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3746 if ((event->keyval == GDK_KEY_Return
3747 || event->keyval == GDK_KEY_ISO_Enter
3748 || event->keyval == GDK_KEY_KP_Enter
3749 || event->keyval == GDK_KEY_space
3750 || event->keyval == GDK_KEY_KP_Space)
3751 && ((event->state & modifiers) == 0)
3752 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3753 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3757 window = get_toplevel (widget);
3760 GtkWidget *default_widget, *focus_widget;
3762 default_widget = gtk_window_get_default_widget (window);
3763 focus_widget = gtk_window_get_focus (window);
3765 if (widget != default_widget &&
3766 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3768 gtk_window_activate_default (window);
3778 /* Callback used when the file list's popup menu is detached */
3780 popup_menu_detach_cb (GtkWidget *attach_widget,
3783 GtkFileChooserDefault *impl;
3785 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3786 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3788 impl->browse_files_popup_menu = NULL;
3789 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3790 impl->browse_files_popup_menu_hidden_files_item = NULL;
3793 /* Callback used when the "Add to Bookmarks" menu item is activated */
3795 add_to_shortcuts_cb (GtkMenuItem *item,
3796 GtkFileChooserDefault *impl)
3798 bookmarks_add_selected_folder (impl);
3801 /* Callback used when the "Show Hidden Files" menu item is toggled */
3803 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3804 GtkFileChooserDefault *impl)
3807 "show-hidden", gtk_check_menu_item_get_active (item),
3811 /* Callback used when the "Show Size Column" menu item is toggled */
3813 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3814 GtkFileChooserDefault *impl)
3816 impl->show_size_column = gtk_check_menu_item_get_active (item);
3818 gtk_tree_view_column_set_visible (impl->list_size_column,
3819 impl->show_size_column);
3822 /* Shows an error dialog about not being able to select a dragged file */
3824 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3829 _("Could not select file"),
3834 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3839 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3841 for (i = 1; uris[i]; i++)
3844 GError *error = NULL;
3847 file = g_file_new_for_uri (uri);
3849 gtk_file_chooser_default_select_file (chooser, file, &error);
3851 error_selecting_dragged_file_dialog (impl, file, error);
3853 g_object_unref (file);
3857 struct FileListDragData
3859 GtkFileChooserDefault *impl;
3865 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3867 const GError *error,
3870 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3871 struct FileListDragData *data = user_data;
3872 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3874 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3877 data->impl->file_list_drag_data_received_cancellable = NULL;
3879 if (cancelled || error)
3882 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3883 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3884 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3885 change_folder_and_display_error (data->impl, data->file, FALSE);
3888 GError *error = NULL;
3890 gtk_file_chooser_default_unselect_all (chooser);
3891 gtk_file_chooser_default_select_file (chooser, data->file, &error);
3893 error_selecting_dragged_file_dialog (data->impl, data->file, error);
3895 browse_files_center_selected_row (data->impl);
3898 if (data->impl->select_multiple)
3899 file_list_drag_data_select_uris (data->impl, data->uris);
3902 g_object_unref (data->impl);
3903 g_strfreev (data->uris);
3904 g_object_unref (data->file);
3907 g_object_unref (cancellable);
3911 file_list_drag_data_received_cb (GtkWidget *widget,
3912 GdkDragContext *context,
3915 GtkSelectionData *selection_data,
3920 GtkFileChooserDefault *impl;
3925 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3927 /* Allow only drags from other widgets; see bug #533891. */
3928 if (gtk_drag_get_source_widget (context) == widget)
3930 g_signal_stop_emission_by_name (widget, "drag-data-received");
3934 /* Parse the text/uri-list string, navigate to the first one */
3935 uris = gtk_selection_data_get_uris (selection_data);
3936 if (uris && uris[0])
3938 struct FileListDragData *data;
3941 file = g_file_new_for_uri (uri);
3943 data = g_new0 (struct FileListDragData, 1);
3944 data->impl = g_object_ref (impl);
3948 if (impl->file_list_drag_data_received_cancellable)
3949 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
3951 impl->file_list_drag_data_received_cancellable =
3952 _gtk_file_system_get_info (impl->file_system, file,
3954 file_list_drag_data_received_get_info_cb,
3958 g_signal_stop_emission_by_name (widget, "drag-data-received");
3961 /* Don't do anything with the drag_drop signal */
3963 file_list_drag_drop_cb (GtkWidget *widget,
3964 GdkDragContext *context,
3968 GtkFileChooserDefault *impl)
3970 g_signal_stop_emission_by_name (widget, "drag-drop");
3974 /* Disable the normal tree drag motion handler, it makes it look like you're
3975 dropping the dragged item onto a tree item */
3977 file_list_drag_motion_cb (GtkWidget *widget,
3978 GdkDragContext *context,
3982 GtkFileChooserDefault *impl)
3984 g_signal_stop_emission_by_name (widget, "drag-motion");
3988 /* Constructs the popup menu for the file list if needed */
3990 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3994 if (impl->browse_files_popup_menu)
3997 impl->browse_files_popup_menu = gtk_menu_new ();
3998 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3999 impl->browse_files_tree_view,
4000 popup_menu_detach_cb);
4002 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4003 impl->browse_files_popup_menu_add_shortcut_item = item;
4004 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4005 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4006 g_signal_connect (item, "activate",
4007 G_CALLBACK (add_to_shortcuts_cb), impl);
4008 gtk_widget_show (item);
4009 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4011 item = gtk_separator_menu_item_new ();
4012 gtk_widget_show (item);
4013 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4015 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4016 impl->browse_files_popup_menu_hidden_files_item = item;
4017 g_signal_connect (item, "toggled",
4018 G_CALLBACK (show_hidden_toggled_cb), impl);
4019 gtk_widget_show (item);
4020 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4022 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4023 impl->browse_files_popup_menu_size_column_item = item;
4024 g_signal_connect (item, "toggled",
4025 G_CALLBACK (show_size_column_toggled_cb), impl);
4026 gtk_widget_show (item);
4027 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4029 bookmarks_check_add_sensitivity (impl);
4032 /* Updates the popup menu for the file list, creating it if necessary */
4034 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4036 file_list_build_popup_menu (impl);
4038 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4040 /* The sensitivity of the Add to Bookmarks item is set in
4041 * bookmarks_check_add_sensitivity()
4044 /* 'Show Hidden Files' */
4045 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4046 G_CALLBACK (show_hidden_toggled_cb), impl);
4047 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4049 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4050 G_CALLBACK (show_hidden_toggled_cb), impl);
4052 /* 'Show Size Column' */
4053 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4054 G_CALLBACK (show_size_column_toggled_cb), impl);
4055 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4056 impl->show_size_column);
4057 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4058 G_CALLBACK (show_size_column_toggled_cb), impl);
4062 popup_position_func (GtkMenu *menu,
4068 GtkAllocation allocation;
4069 GtkWidget *widget = GTK_WIDGET (user_data);
4070 GdkScreen *screen = gtk_widget_get_screen (widget);
4073 GdkRectangle monitor;
4075 g_return_if_fail (gtk_widget_get_realized (widget));
4077 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4079 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4082 gtk_widget_get_allocation (widget, &allocation);
4083 *x += (allocation.width - req.width) / 2;
4084 *y += (allocation.height - req.height) / 2;
4086 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4087 gtk_menu_set_monitor (menu, monitor_num);
4088 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4090 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4091 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4097 file_list_popup_menu (GtkFileChooserDefault *impl,
4098 GdkEventButton *event)
4100 file_list_update_popup_menu (impl);
4102 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4103 NULL, NULL, NULL, NULL,
4104 event->button, event->time);
4107 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4109 popup_position_func, impl->browse_files_tree_view,
4110 0, GDK_CURRENT_TIME);
4111 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4117 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4119 list_popup_menu_cb (GtkWidget *widget,
4120 GtkFileChooserDefault *impl)
4122 file_list_popup_menu (impl, NULL);
4126 /* Callback used when a button is pressed on the file list. We trap button 3 to
4127 * bring up a popup menu.
4130 list_button_press_event_cb (GtkWidget *widget,
4131 GdkEventButton *event,
4132 GtkFileChooserDefault *impl)
4134 static gboolean in_press = FALSE;
4139 if (event->button != 3)
4143 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4146 file_list_popup_menu (impl, event);
4151 OperationMode operation_mode;
4152 gint general_column;
4156 /* Sets the sort column IDs for the file list based on the operation mode */
4158 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4160 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4161 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4162 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4166 file_list_query_tooltip_cb (GtkWidget *widget,
4169 gboolean keyboard_tip,
4170 GtkTooltip *tooltip,
4173 GtkFileChooserDefault *impl = user_data;
4174 GtkTreeModel *model;
4180 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4184 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4187 &model, &path, &iter))
4190 gtk_tree_model_get (model, &iter,
4191 MODEL_COL_FILE, &file,
4196 gtk_tree_path_free (path);
4200 filename = g_file_get_path (file);
4201 gtk_tooltip_set_text (tooltip, filename);
4202 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4207 g_object_unref (file);
4208 gtk_tree_path_free (path);
4214 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4218 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4219 gtk_cell_renderer_set_fixed_size (renderer,
4220 xpad * 2 + impl->icon_size,
4221 ypad * 2 + impl->icon_size);
4224 /* Creates the widgets for the file list */
4226 create_file_list (GtkFileChooserDefault *impl)
4229 GtkTreeSelection *selection;
4230 GtkTreeViewColumn *column;
4231 GtkCellRenderer *renderer;
4233 /* Scrolled window */
4234 swin = gtk_scrolled_window_new (NULL, NULL);
4235 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4236 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4237 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4240 /* Tree/list view */
4242 impl->browse_files_tree_view = gtk_tree_view_new ();
4243 #ifdef PROFILE_FILE_CHOOSER
4244 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4246 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4247 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4249 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4250 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4252 gtk_drag_dest_set (impl->browse_files_tree_view,
4253 GTK_DEST_DEFAULT_ALL,
4255 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4256 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4258 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4259 G_CALLBACK (list_row_activated), impl);
4260 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4261 G_CALLBACK (browse_files_key_press_event_cb), impl);
4262 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4263 G_CALLBACK (list_popup_menu_cb), impl);
4264 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4265 G_CALLBACK (list_button_press_event_cb), impl);
4267 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4268 G_CALLBACK (file_list_drag_data_received_cb), impl);
4269 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4270 G_CALLBACK (file_list_drag_drop_cb), impl);
4271 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4272 G_CALLBACK (file_list_drag_motion_cb), impl);
4274 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4275 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4276 G_CALLBACK (file_list_query_tooltip_cb), impl);
4278 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4279 gtk_tree_selection_set_select_function (selection,
4282 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4285 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4286 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4288 g_signal_connect (selection, "changed",
4289 G_CALLBACK (list_selection_changed), impl);
4291 /* Keep the column order in sync with update_cell_renderer_attributes() */
4293 /* Filename column */
4295 impl->list_name_column = gtk_tree_view_column_new ();
4296 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4297 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4298 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4300 renderer = gtk_cell_renderer_pixbuf_new ();
4301 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4302 set_icon_cell_renderer_fixed_size (impl, renderer);
4303 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4305 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4306 g_object_set (impl->list_name_renderer,
4307 "ellipsize", PANGO_ELLIPSIZE_END,
4309 g_signal_connect (impl->list_name_renderer, "edited",
4310 G_CALLBACK (renderer_edited_cb), impl);
4311 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4312 G_CALLBACK (renderer_editing_canceled_cb), impl);
4313 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4315 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4319 column = gtk_tree_view_column_new ();
4320 gtk_tree_view_column_set_resizable (column, TRUE);
4321 gtk_tree_view_column_set_title (column, _("Size"));
4323 renderer = gtk_cell_renderer_text_new ();
4324 g_object_set (renderer,
4325 "alignment", PANGO_ALIGN_RIGHT,
4327 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4328 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4329 impl->list_size_column = column;
4331 /* Modification time column */
4333 column = gtk_tree_view_column_new ();
4334 gtk_tree_view_column_set_resizable (column, TRUE);
4335 gtk_tree_view_column_set_title (column, _("Modified"));
4337 renderer = gtk_cell_renderer_text_new ();
4338 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4339 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4340 impl->list_mtime_column = column;
4342 file_list_set_sort_column_ids (impl);
4343 update_cell_renderer_attributes (impl);
4345 gtk_widget_show_all (swin);
4350 /* Creates the widgets for the files/folders pane */
4352 file_pane_create (GtkFileChooserDefault *impl,
4353 GtkSizeGroup *size_group)
4359 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4360 gtk_widget_show (vbox);
4362 /* Box for lists and preview */
4364 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4365 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4366 gtk_widget_show (hbox);
4370 widget = create_file_list (impl);
4371 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4372 gtk_size_group_add_widget (size_group, widget);
4376 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4377 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4378 /* Don't show preview box initially */
4382 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4384 widget = filter_create (impl);
4386 gtk_widget_show (widget);
4387 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4389 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4394 /* Creates the widgets specific to Save mode */
4396 save_widgets_create (GtkFileChooserDefault *impl)
4401 if (impl->save_widgets != NULL)
4404 location_switch_to_path_bar (impl);
4406 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4408 impl->save_widgets_table = gtk_table_new (2, 2, FALSE);
4409 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4410 gtk_widget_show (impl->save_widgets_table);
4411 gtk_table_set_row_spacings (GTK_TABLE (impl->save_widgets_table), 12);
4412 gtk_table_set_col_spacings (GTK_TABLE (impl->save_widgets_table), 12);
4416 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4417 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4418 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4419 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), widget,
4423 gtk_widget_show (widget);
4425 /* Location entry */
4427 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4428 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4430 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4431 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4432 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4433 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->location_entry,
4435 GTK_EXPAND | GTK_FILL, 0,
4437 gtk_widget_show (impl->location_entry);
4438 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4441 impl->save_folder_label = gtk_label_new (NULL);
4442 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4443 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4444 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->save_folder_label,
4448 gtk_widget_show (impl->save_folder_label);
4450 impl->save_widgets = vbox;
4451 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4452 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4453 gtk_widget_show (impl->save_widgets);
4456 /* Destroys the widgets specific to Save mode */
4458 save_widgets_destroy (GtkFileChooserDefault *impl)
4460 if (impl->save_widgets == NULL)
4463 gtk_widget_destroy (impl->save_widgets);
4464 impl->save_widgets = NULL;
4465 impl->save_widgets_table = NULL;
4466 impl->location_entry = NULL;
4467 impl->save_folder_label = NULL;
4470 /* Turns on the path bar widget. Can be called even if we are already in that
4474 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4476 if (impl->location_entry)
4478 gtk_widget_destroy (impl->location_entry);
4479 impl->location_entry = NULL;
4482 gtk_widget_hide (impl->location_entry_box);
4485 /* Sets the full path of the current folder as the text in the location entry. */
4487 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4489 gchar *text, *filename;
4491 if (!impl->current_folder)
4494 filename = g_file_get_path (impl->current_folder);
4498 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4502 text = g_file_get_uri (impl->current_folder);
4506 gboolean need_slash;
4509 len = strlen (text);
4510 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4516 slash_text = g_new (char, len + 2);
4517 strcpy (slash_text, text);
4518 slash_text[len] = G_DIR_SEPARATOR;
4519 slash_text[len + 1] = 0;
4525 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4532 /* Turns on the location entry. Can be called even if we are already in that
4536 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4538 /* when in search or recent files mode, we are not showing the
4539 * location_entry_box container, so there's no point in switching
4542 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4543 impl->operation_mode == OPERATION_MODE_RECENT)
4546 if (impl->location_entry)
4547 gtk_widget_destroy (impl->location_entry);
4551 gtk_widget_show (impl->location_entry_box);
4555 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4556 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4558 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4559 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4561 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4562 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4564 /* Configure the entry */
4566 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4567 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4571 gtk_widget_show (impl->location_entry);
4572 gtk_widget_grab_focus (impl->location_entry);
4575 /* Sets a new location mode. set_buttons determines whether the toggle button
4576 * for the mode will also be changed.
4579 location_mode_set (GtkFileChooserDefault *impl,
4580 LocationMode new_mode,
4581 gboolean set_button)
4583 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4584 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4586 GtkWindow *toplevel;
4587 GtkWidget *current_focus;
4588 gboolean button_active;
4589 gboolean switch_to_file_list;
4593 case LOCATION_MODE_PATH_BAR:
4594 button_active = FALSE;
4596 /* The location_entry will disappear when we switch to path bar mode. So,
4597 * we'll focus the file list in that case, to avoid having a window with
4598 * no focused widget.
4600 toplevel = get_toplevel (GTK_WIDGET (impl));
4601 switch_to_file_list = FALSE;
4604 current_focus = gtk_window_get_focus (toplevel);
4605 if (!current_focus || current_focus == impl->location_entry)
4606 switch_to_file_list = TRUE;
4609 location_switch_to_path_bar (impl);
4611 if (switch_to_file_list)
4612 gtk_widget_grab_focus (impl->browse_files_tree_view);
4616 case LOCATION_MODE_FILENAME_ENTRY:
4617 button_active = TRUE;
4618 location_switch_to_filename_entry (impl);
4622 g_assert_not_reached ();
4628 g_signal_handlers_block_by_func (impl->location_button,
4629 G_CALLBACK (location_button_toggled_cb), impl);
4631 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4633 g_signal_handlers_unblock_by_func (impl->location_button,
4634 G_CALLBACK (location_button_toggled_cb), impl);
4638 impl->location_mode = new_mode;
4642 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4644 /* when in search or recent files mode, we are not showing the
4645 * location_entry_box container, so there's no point in switching
4648 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4649 impl->operation_mode == OPERATION_MODE_RECENT)
4652 /* If the file entry is not visible, show it.
4653 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4655 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4657 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4659 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4661 if (gtk_widget_has_focus (impl->location_entry))
4663 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4667 gtk_widget_grab_focus (impl->location_entry);
4672 /* Callback used when one of the location mode buttons is toggled */
4674 location_button_toggled_cb (GtkToggleButton *toggle,
4675 GtkFileChooserDefault *impl)
4678 LocationMode new_mode;
4680 is_active = gtk_toggle_button_get_active (toggle);
4684 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4685 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4689 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4690 new_mode = LOCATION_MODE_PATH_BAR;
4693 location_mode_set (impl, new_mode, FALSE);
4696 /* Creates a toggle button for the location entry. */
4698 location_button_create (GtkFileChooserDefault *impl)
4703 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4704 gtk_widget_show (image);
4706 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4710 g_signal_connect (impl->location_button, "toggled",
4711 G_CALLBACK (location_button_toggled_cb), impl);
4713 str = _("Type a file name");
4715 gtk_widget_set_tooltip_text (impl->location_button, str);
4716 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4720 PATH_BAR_FOLDER_PATH,
4721 PATH_BAR_SELECT_A_FOLDER,
4722 PATH_BAR_ERROR_NO_FILENAME,
4723 PATH_BAR_ERROR_NO_FOLDER,
4724 PATH_BAR_RECENTLY_USED,
4728 /* Creates the info bar for informational messages or warnings, with its icon and label */
4730 info_bar_create (GtkFileChooserDefault *impl)
4732 GtkWidget *content_area;
4734 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4735 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4736 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4738 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4740 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4741 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4743 gtk_widget_show (impl->browse_select_a_folder_icon);
4744 gtk_widget_show (impl->browse_select_a_folder_label);
4747 /* Sets the info bar to show the appropriate informational or warning message */
4749 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4753 GtkMessageType message_type;
4759 case PATH_BAR_SELECT_A_FOLDER:
4760 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4762 message_type = GTK_MESSAGE_OTHER;
4765 case PATH_BAR_ERROR_NO_FILENAME:
4766 str = _("Please type a file name");
4767 message_type = GTK_MESSAGE_WARNING;
4770 case PATH_BAR_ERROR_NO_FOLDER:
4771 str = _("Please select a folder below");
4772 message_type = GTK_MESSAGE_WARNING;
4776 g_assert_not_reached ();
4780 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4781 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4782 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4783 GTK_ICON_SIZE_MENU);
4784 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4790 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4792 special_mode_widgets_create (GtkFileChooserDefault *impl)
4794 impl->browse_special_mode_icon = gtk_image_new ();
4795 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4796 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4798 impl->browse_special_mode_label = gtk_label_new (NULL);
4799 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4800 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4803 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4805 path_bar_widgets_create (GtkFileChooserDefault *impl)
4807 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4808 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4809 gtk_widget_show (impl->browse_path_bar_hbox);
4811 /* Size group that allows the path bar to be the same size between modes */
4812 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4813 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4815 /* Location button */
4816 location_button_create (impl);
4817 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4818 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4821 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4822 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4823 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4825 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4826 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4829 info_bar_create (impl);
4830 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4831 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4833 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4834 special_mode_widgets_create (impl);
4837 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4838 g_signal_connect (impl->browse_new_folder_button, "clicked",
4839 G_CALLBACK (new_folder_button_clicked), impl);
4840 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4841 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4844 /* Sets the path bar's mode to show a label, the actual folder path, or a
4845 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4846 * if the pathbar is already showing the widgets you expect; otherwise, call
4847 * path_bar_update() instead to set the appropriate widgets automatically.
4850 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4852 gboolean path_bar_visible = FALSE;
4853 gboolean special_mode_widgets_visible = FALSE;
4854 gboolean info_bar_visible = FALSE;
4855 gboolean create_folder_visible = FALSE;
4861 case PATH_BAR_FOLDER_PATH:
4862 path_bar_visible = TRUE;
4865 case PATH_BAR_SELECT_A_FOLDER:
4866 case PATH_BAR_ERROR_NO_FILENAME:
4867 case PATH_BAR_ERROR_NO_FOLDER:
4868 info_bar_set (impl, mode);
4869 info_bar_visible = TRUE;
4872 case PATH_BAR_RECENTLY_USED:
4873 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4875 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4876 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4879 special_mode_widgets_visible = TRUE;
4882 case PATH_BAR_SEARCH:
4883 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
4885 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
4886 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4889 special_mode_widgets_visible = TRUE;
4893 g_assert_not_reached ();
4896 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
4897 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
4898 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
4899 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
4901 if (path_bar_visible)
4903 if (impl->create_folders
4904 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
4905 && impl->operation_mode != OPERATION_MODE_RECENT)
4906 create_folder_visible = TRUE;
4909 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
4912 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4914 browse_widgets_create (GtkFileChooserDefault *impl)
4918 GtkSizeGroup *size_group;
4920 /* size group is used by the scrolled windows of the panes */
4921 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4922 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4924 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
4925 path_bar_widgets_create (impl);
4927 /* Box for the location label and entry */
4929 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4930 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->location_entry_box, FALSE, FALSE, 0);
4932 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4933 gtk_widget_show (impl->location_label);
4934 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4938 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
4939 gtk_widget_show (hpaned);
4940 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
4942 widget = shortcuts_pane_create (impl, size_group);
4943 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4944 widget = file_pane_create (impl, size_group);
4945 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4946 gtk_paned_set_position (GTK_PANED (hpaned), 148);
4947 g_object_unref (size_group);
4951 gtk_file_chooser_default_constructor (GType type,
4952 guint n_construct_properties,
4953 GObjectConstructParam *construct_params)
4955 GtkFileChooserDefault *impl;
4958 profile_start ("start", NULL);
4960 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4961 n_construct_properties,
4963 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4965 g_assert (impl->file_system);
4967 gtk_widget_push_composite_child ();
4969 /* Shortcuts model */
4970 shortcuts_model_create (impl);
4972 /* The browse widgets */
4973 browse_widgets_create (impl);
4974 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
4976 /* Alignment to hold extra widget */
4977 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
4978 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
4979 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
4980 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4982 gtk_widget_pop_composite_child ();
4983 update_appearance (impl);
4985 profile_end ("end", NULL);
4990 /* Sets the extra_widget by packing it in the appropriate place */
4992 set_extra_widget (GtkFileChooserDefault *impl,
4993 GtkWidget *extra_widget)
4997 g_object_ref (extra_widget);
4998 /* FIXME: is this right ? */
4999 gtk_widget_show (extra_widget);
5002 if (impl->extra_widget)
5004 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5005 g_object_unref (impl->extra_widget);
5008 impl->extra_widget = extra_widget;
5009 if (impl->extra_widget)
5011 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5012 gtk_widget_show (impl->extra_align);
5015 gtk_widget_hide (impl->extra_align);
5019 set_local_only (GtkFileChooserDefault *impl,
5020 gboolean local_only)
5022 if (local_only != impl->local_only)
5024 impl->local_only = local_only;
5026 if (impl->location_entry)
5027 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5029 if (impl->shortcuts_model && impl->file_system)
5031 shortcuts_add_volumes (impl);
5032 shortcuts_add_bookmarks (impl);
5035 if (local_only && impl->current_folder &&
5036 !g_file_is_native (impl->current_folder))
5038 /* If we are pointing to a non-local folder, make an effort to change
5039 * back to a local folder, but it's really up to the app to not cause
5040 * such a situation, so we ignore errors.
5042 const gchar *home = g_get_home_dir ();
5048 home_file = g_file_new_for_path (home);
5050 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5052 g_object_unref (home_file);
5058 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5059 GtkFileChooserDefault *impl)
5061 shortcuts_add_volumes (impl);
5062 shortcuts_add_bookmarks (impl);
5064 bookmarks_check_add_sensitivity (impl);
5065 bookmarks_check_remove_sensitivity (impl);
5066 shortcuts_check_popup_sensitivity (impl);
5069 /* Sets the file chooser to multiple selection mode */
5071 set_select_multiple (GtkFileChooserDefault *impl,
5072 gboolean select_multiple,
5073 gboolean property_notify)
5075 GtkTreeSelection *selection;
5076 GtkSelectionMode mode;
5078 if (select_multiple == impl->select_multiple)
5081 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5083 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5084 gtk_tree_selection_set_mode (selection, mode);
5086 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5088 impl->select_multiple = select_multiple;
5089 g_object_notify (G_OBJECT (impl), "select-multiple");
5091 check_preview_change (impl);
5095 set_file_system_backend (GtkFileChooserDefault *impl)
5097 profile_start ("start for backend", "default");
5099 impl->file_system = _gtk_file_system_new ();
5101 g_signal_connect (impl->file_system, "volumes-changed",
5102 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5103 g_signal_connect (impl->file_system, "bookmarks-changed",
5104 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5106 profile_end ("end", NULL);
5110 unset_file_system_backend (GtkFileChooserDefault *impl)
5112 g_signal_handlers_disconnect_by_func (impl->file_system,
5113 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5115 g_object_unref (impl->file_system);
5117 impl->file_system = NULL;
5120 /* Saves the widgets around the pathbar so they can be reparented later
5121 * in the correct place. This function must be called paired with
5122 * restore_path_bar().
5125 save_path_bar (GtkFileChooserDefault *impl)
5129 g_object_ref (impl->browse_path_bar_hbox);
5131 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5133 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5136 /* Reparents the path bar and the "Create folder" button to the right place:
5137 * Above the file list in Open mode, or to the right of the "Save in folder:"
5138 * label in Save mode. The save_path_bar() function must be called before this
5142 restore_path_bar (GtkFileChooserDefault *impl)
5144 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5145 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5147 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5148 gtk_box_reorder_child (GTK_BOX (impl->browse_widgets_box), impl->browse_path_bar_hbox, 0);
5150 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5151 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5153 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->browse_path_bar_hbox,
5155 GTK_EXPAND | GTK_FILL, GTK_FILL,
5157 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5160 g_assert_not_reached ();
5162 g_object_unref (impl->browse_path_bar_hbox);
5165 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5167 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5171 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5172 MODEL_COL_FILE, &file,
5174 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE, NULL); /* NULL-GError */
5175 g_object_unref (file);
5178 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5179 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5180 * function will update all the pathbar's widgets.
5183 path_bar_update (GtkFileChooserDefault *impl)
5187 switch (impl->operation_mode)
5189 case OPERATION_MODE_BROWSE:
5190 mode = PATH_BAR_FOLDER_PATH;
5193 case OPERATION_MODE_RECENT:
5194 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5196 GtkTreeSelection *selection;
5197 gboolean have_selected;
5200 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5202 /* Save mode means single-selection mode, so the following is valid */
5203 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5207 mode = PATH_BAR_FOLDER_PATH;
5208 put_recent_folder_in_pathbar (impl, &iter);
5211 mode = PATH_BAR_SELECT_A_FOLDER;
5214 mode = PATH_BAR_RECENTLY_USED;
5218 case OPERATION_MODE_SEARCH:
5219 mode = PATH_BAR_SEARCH;
5223 g_assert_not_reached ();
5227 path_bar_set_mode (impl, mode);
5230 /* This function is basically a do_all function.
5232 * It sets the visibility on all the widgets based on the current state, and
5233 * moves the custom_widget if needed.
5236 update_appearance (GtkFileChooserDefault *impl)
5238 save_path_bar (impl);
5240 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5241 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5245 gtk_widget_hide (impl->location_button);
5246 save_widgets_create (impl);
5248 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5249 text = _("Save in _folder:");
5251 text = _("Create in _folder:");
5253 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5255 if (impl->select_multiple)
5257 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5258 "Re-setting to single selection mode.");
5259 set_select_multiple (impl, FALSE, TRUE);
5262 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5263 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5265 gtk_widget_show (impl->location_button);
5266 save_widgets_destroy (impl);
5267 gtk_widget_show (impl->browse_widgets_box);
5268 location_mode_set (impl, impl->location_mode, TRUE);
5271 if (impl->location_entry)
5272 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5274 restore_path_bar (impl);
5275 path_bar_update (impl);
5277 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5278 * of files may change depending whether we are in a file or folder-only mode.
5280 gtk_widget_queue_draw (impl->browse_files_tree_view);
5282 emit_default_size_changed (impl);
5286 gtk_file_chooser_default_set_property (GObject *object,
5288 const GValue *value,
5292 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5296 case GTK_FILE_CHOOSER_PROP_ACTION:
5298 GtkFileChooserAction action = g_value_get_enum (value);
5300 if (action != impl->action)
5302 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5304 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5305 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5306 && impl->select_multiple)
5308 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5309 "this is not allowed in multiple selection mode. Resetting the file chooser "
5310 "to single selection mode.");
5311 set_select_multiple (impl, FALSE, TRUE);
5313 impl->action = action;
5314 update_cell_renderer_attributes (impl);
5315 update_appearance (impl);
5316 settings_load (impl);
5321 case GTK_FILE_CHOOSER_PROP_FILTER:
5322 set_current_filter (impl, g_value_get_object (value));
5325 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5326 set_local_only (impl, g_value_get_boolean (value));
5329 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5330 set_preview_widget (impl, g_value_get_object (value));
5333 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5334 impl->preview_widget_active = g_value_get_boolean (value);
5335 update_preview_widget_visibility (impl);
5338 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5339 impl->use_preview_label = g_value_get_boolean (value);
5340 update_preview_widget_visibility (impl);
5343 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5344 set_extra_widget (impl, g_value_get_object (value));
5347 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5349 gboolean select_multiple = g_value_get_boolean (value);
5350 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5351 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5354 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5355 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5356 "leaving the file chooser in single selection mode.");
5360 set_select_multiple (impl, select_multiple, FALSE);
5364 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5366 gboolean show_hidden = g_value_get_boolean (value);
5367 if (show_hidden != impl->show_hidden)
5369 impl->show_hidden = show_hidden;
5371 if (impl->browse_files_model)
5372 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5377 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5379 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5380 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5384 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5386 gboolean create_folders = g_value_get_boolean (value);
5387 impl->create_folders = create_folders;
5388 update_appearance (impl);
5393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5399 gtk_file_chooser_default_get_property (GObject *object,
5404 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5408 case GTK_FILE_CHOOSER_PROP_ACTION:
5409 g_value_set_enum (value, impl->action);
5412 case GTK_FILE_CHOOSER_PROP_FILTER:
5413 g_value_set_object (value, impl->current_filter);
5416 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5417 g_value_set_boolean (value, impl->local_only);
5420 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5421 g_value_set_object (value, impl->preview_widget);
5424 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5425 g_value_set_boolean (value, impl->preview_widget_active);
5428 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5429 g_value_set_boolean (value, impl->use_preview_label);
5432 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5433 g_value_set_object (value, impl->extra_widget);
5436 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5437 g_value_set_boolean (value, impl->select_multiple);
5440 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5441 g_value_set_boolean (value, impl->show_hidden);
5444 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5445 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5448 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5449 g_value_set_boolean (value, impl->create_folders);
5453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5458 /* This cancels everything that may be going on in the background. */
5460 cancel_all_operations (GtkFileChooserDefault *impl)
5464 pending_select_files_free (impl);
5466 /* cancel all pending operations */
5467 if (impl->pending_cancellables)
5469 for (l = impl->pending_cancellables; l; l = l->next)
5471 GCancellable *cancellable = G_CANCELLABLE (l->data);
5472 g_cancellable_cancel (cancellable);
5474 g_slist_free (impl->pending_cancellables);
5475 impl->pending_cancellables = NULL;
5478 if (impl->reload_icon_cancellables)
5480 for (l = impl->reload_icon_cancellables; l; l = l->next)
5482 GCancellable *cancellable = G_CANCELLABLE (l->data);
5483 g_cancellable_cancel (cancellable);
5485 g_slist_free (impl->reload_icon_cancellables);
5486 impl->reload_icon_cancellables = NULL;
5489 if (impl->loading_shortcuts)
5491 for (l = impl->loading_shortcuts; l; l = l->next)
5493 GCancellable *cancellable = G_CANCELLABLE (l->data);
5494 g_cancellable_cancel (cancellable);
5496 g_slist_free (impl->loading_shortcuts);
5497 impl->loading_shortcuts = NULL;
5500 if (impl->file_list_drag_data_received_cancellable)
5502 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5503 impl->file_list_drag_data_received_cancellable = NULL;
5506 if (impl->update_current_folder_cancellable)
5508 g_cancellable_cancel (impl->update_current_folder_cancellable);
5509 impl->update_current_folder_cancellable = NULL;
5512 if (impl->should_respond_get_info_cancellable)
5514 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5515 impl->should_respond_get_info_cancellable = NULL;
5518 if (impl->update_from_entry_cancellable)
5520 g_cancellable_cancel (impl->update_from_entry_cancellable);
5521 impl->update_from_entry_cancellable = NULL;
5524 if (impl->shortcuts_activate_iter_cancellable)
5526 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5527 impl->shortcuts_activate_iter_cancellable = NULL;
5530 search_stop_searching (impl, TRUE);
5531 recent_stop_loading (impl);
5534 /* Removes the settings signal handler. It's safe to call multiple times */
5536 remove_settings_signal (GtkFileChooserDefault *impl,
5539 if (impl->settings_signal_id)
5541 GtkSettings *settings;
5543 settings = gtk_settings_get_for_screen (screen);
5544 g_signal_handler_disconnect (settings,
5545 impl->settings_signal_id);
5546 impl->settings_signal_id = 0;
5551 gtk_file_chooser_default_dispose (GObject *object)
5553 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5555 cancel_all_operations (impl);
5557 if (impl->extra_widget)
5559 g_object_unref (impl->extra_widget);
5560 impl->extra_widget = NULL;
5563 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5565 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5568 /* We override show-all since we have internal widgets that
5569 * shouldn't be shown when you call show_all(), like the filter
5573 gtk_file_chooser_default_show_all (GtkWidget *widget)
5575 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5577 gtk_widget_show (widget);
5579 if (impl->extra_widget)
5580 gtk_widget_show_all (impl->extra_widget);
5583 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5584 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5587 toplevel_set_focus_cb (GtkWindow *window,
5589 GtkFileChooserDefault *impl)
5591 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5594 /* We monitor the focus widget on our toplevel to be able to know which widget
5595 * was last focused at the time our "should_respond" method gets called.
5598 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5599 GtkWidget *previous_toplevel)
5601 GtkFileChooserDefault *impl;
5602 GtkWidget *toplevel;
5604 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5605 toplevel = gtk_widget_get_toplevel (widget);
5607 if (previous_toplevel &&
5608 impl->toplevel_set_focus_id != 0)
5610 g_signal_handler_disconnect (previous_toplevel,
5611 impl->toplevel_set_focus_id);
5612 impl->toplevel_set_focus_id = 0;
5613 impl->toplevel_last_focus_widget = NULL;
5616 if (gtk_widget_is_toplevel (toplevel))
5618 g_assert (impl->toplevel_set_focus_id == 0);
5619 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5620 G_CALLBACK (toplevel_set_focus_cb), impl);
5621 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5625 /* Changes the icons wherever it is needed */
5627 change_icon_theme (GtkFileChooserDefault *impl)
5629 GtkSettings *settings;
5631 GtkCellRenderer *renderer;
5634 profile_start ("start", NULL);
5636 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5638 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5639 impl->icon_size = MAX (width, height);
5641 impl->icon_size = FALLBACK_ICON_SIZE;
5643 shortcuts_reload_icons (impl);
5644 /* the first cell in the first column is the icon column, and we have a fixed size there */
5645 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5646 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5647 renderer = GTK_CELL_RENDERER (cells->data);
5648 set_icon_cell_renderer_fixed_size (impl, renderer);
5649 g_list_free (cells);
5650 if (impl->browse_files_model)
5651 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5652 gtk_widget_queue_resize (impl->browse_files_tree_view);
5654 profile_end ("end", NULL);
5657 /* Callback used when a GtkSettings value changes */
5659 settings_notify_cb (GObject *object,
5661 GtkFileChooserDefault *impl)
5665 profile_start ("start", NULL);
5667 name = g_param_spec_get_name (pspec);
5669 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5670 strcmp (name, "gtk-icon-sizes") == 0)
5671 change_icon_theme (impl);
5673 profile_end ("end", NULL);
5676 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5680 check_icon_theme (GtkFileChooserDefault *impl)
5682 GtkSettings *settings;
5684 profile_start ("start", NULL);
5686 if (impl->settings_signal_id)
5688 profile_end ("end", NULL);
5692 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5694 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5695 impl->settings_signal_id = g_signal_connect (settings, "notify",
5696 G_CALLBACK (settings_notify_cb), impl);
5698 change_icon_theme (impl);
5701 profile_end ("end", NULL);
5705 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5707 GtkFileChooserDefault *impl;
5709 profile_start ("start", NULL);
5711 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5713 profile_msg (" parent class style_udpated start", NULL);
5714 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5715 profile_msg (" parent class style_updated end", NULL);
5717 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5718 change_icon_theme (impl);
5720 emit_default_size_changed (impl);
5722 profile_end ("end", NULL);
5726 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5727 GdkScreen *previous_screen)
5729 GtkFileChooserDefault *impl;
5731 profile_start ("start", NULL);
5733 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5735 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5736 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5738 remove_settings_signal (impl, previous_screen);
5739 check_icon_theme (impl);
5741 emit_default_size_changed (impl);
5743 profile_end ("end", NULL);
5747 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5748 GtkAllocation *allocation)
5750 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5754 set_sort_column (GtkFileChooserDefault *impl)
5756 GtkTreeSortable *sortable;
5758 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5760 /* can happen when we're still populating the model */
5761 if (sortable == NULL)
5764 gtk_tree_sortable_set_sort_column_id (sortable,
5770 settings_ensure (GtkFileChooserDefault *impl)
5772 if (impl->settings != NULL)
5775 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5776 "/org/gtk/settings/file-chooser/");
5777 g_settings_delay (impl->settings);
5781 settings_load (GtkFileChooserDefault *impl)
5783 LocationMode location_mode;
5784 gboolean show_hidden;
5785 gboolean show_size_column;
5787 GtkSortType sort_order;
5789 settings_ensure (impl);
5791 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5792 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5793 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5794 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5795 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5797 location_mode_set (impl, location_mode, TRUE);
5799 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5801 impl->show_size_column = show_size_column;
5802 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5804 impl->sort_column = sort_column;
5805 impl->sort_order = sort_order;
5806 /* We don't call set_sort_column() here as the models may not have been
5807 * created yet. The individual functions that create and set the models will
5808 * call set_sort_column() themselves.
5813 save_dialog_geometry (GtkFileChooserDefault *impl)
5815 GtkWindow *toplevel;
5816 int x, y, width, height;
5818 toplevel = get_toplevel (GTK_WIDGET (impl));
5820 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5823 gtk_window_get_position (toplevel, &x, &y);
5824 gtk_window_get_size (toplevel, &width, &height);
5826 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5827 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5831 settings_save (GtkFileChooserDefault *impl)
5833 char *current_folder_uri;
5835 settings_ensure (impl);
5837 /* Current folder */
5839 if (impl->current_folder)
5840 current_folder_uri = g_file_get_uri (impl->current_folder);
5842 current_folder_uri = "";
5844 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5846 if (impl->current_folder)
5847 g_free (current_folder_uri);
5849 /* All the other state */
5851 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5852 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5853 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5854 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5855 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5856 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5858 save_dialog_geometry (impl);
5860 /* Now apply the settings */
5861 g_settings_apply (impl->settings);
5863 g_object_unref (impl->settings);
5864 impl->settings = NULL;
5867 /* GtkWidget::realize method */
5869 gtk_file_chooser_default_realize (GtkWidget *widget)
5871 GtkFileChooserDefault *impl;
5873 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5875 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5877 emit_default_size_changed (impl);
5881 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5883 char *last_folder_uri;
5886 settings_ensure (impl);
5888 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5890 /* If no last folder is set, we use the user's home directory, since
5891 * this is the starting point for most documents.
5893 if (last_folder_uri[0] == '\0')
5894 file = g_file_new_for_path (g_get_home_dir ());
5896 file = g_file_new_for_uri (last_folder_uri);
5898 g_free (last_folder_uri);
5903 /* GtkWidget::map method */
5905 gtk_file_chooser_default_map (GtkWidget *widget)
5907 GtkFileChooserDefault *impl;
5909 profile_start ("start", NULL);
5911 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5913 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5915 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5917 switch (impl->reload_state)
5920 recent_shortcut_handler (impl);
5923 case RELOAD_HAS_FOLDER:
5924 /* Nothing; we are already loading or loaded, so we
5925 * don't need to reload
5930 g_assert_not_reached ();
5934 volumes_bookmarks_changed_cb (impl->file_system, impl);
5936 settings_load (impl);
5938 profile_end ("end", NULL);
5941 /* GtkWidget::unmap method */
5943 gtk_file_chooser_default_unmap (GtkWidget *widget)
5945 GtkFileChooserDefault *impl;
5947 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5949 settings_save (impl);
5951 cancel_all_operations (impl);
5952 impl->reload_state = RELOAD_EMPTY;
5954 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5958 install_list_model_filter (GtkFileChooserDefault *impl)
5960 _gtk_file_system_model_set_filter (impl->browse_files_model,
5961 impl->current_filter);
5964 #define COMPARE_DIRECTORIES \
5965 GtkFileChooserDefault *impl = user_data; \
5966 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
5967 gboolean dir_a, dir_b; \
5969 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
5970 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
5972 if (dir_a != dir_b) \
5973 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5975 /* Sort callback for the filename column */
5977 name_sort_func (GtkTreeModel *model,
5982 COMPARE_DIRECTORIES;
5985 const char *key_a, *key_b;
5988 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
5989 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
5992 result = strcmp (key_a, key_b);
6004 /* Sort callback for the size column */
6006 size_sort_func (GtkTreeModel *model,
6011 COMPARE_DIRECTORIES;
6014 gint64 size_a, size_b;
6016 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6017 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6019 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6023 /* Sort callback for the mtime column */
6025 mtime_sort_func (GtkTreeModel *model,
6030 COMPARE_DIRECTORIES;
6035 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6036 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6038 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6042 /* Callback used when the sort column changes. We cache the sort order for use
6043 * in name_sort_func().
6046 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6047 GtkFileChooserDefault *impl)
6049 gint sort_column_id;
6050 GtkSortType sort_type;
6052 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6054 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6055 impl->sort_column = sort_column_id;
6056 impl->sort_order = sort_type;
6061 set_busy_cursor (GtkFileChooserDefault *impl,
6065 GtkWindow *toplevel;
6066 GdkDisplay *display;
6069 toplevel = get_toplevel (GTK_WIDGET (impl));
6070 widget = GTK_WIDGET (toplevel);
6071 if (!toplevel || !gtk_widget_get_realized (widget))
6074 display = gtk_widget_get_display (widget);
6077 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6081 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6082 gdk_display_flush (display);
6085 g_object_unref (cursor);
6088 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6090 load_set_model (GtkFileChooserDefault *impl)
6092 profile_start ("start", NULL);
6094 g_assert (impl->browse_files_model != NULL);
6096 profile_msg (" gtk_tree_view_set_model start", NULL);
6097 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6098 GTK_TREE_MODEL (impl->browse_files_model));
6099 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6100 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6102 set_sort_column (impl);
6103 profile_msg (" gtk_tree_view_set_model end", NULL);
6104 impl->list_sort_ascending = TRUE;
6106 profile_end ("end", NULL);
6109 /* Timeout callback used when the loading timer expires */
6111 load_timeout_cb (gpointer data)
6113 GtkFileChooserDefault *impl;
6115 profile_start ("start", NULL);
6117 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6118 g_assert (impl->load_state == LOAD_PRELOAD);
6119 g_assert (impl->load_timeout_id != 0);
6120 g_assert (impl->browse_files_model != NULL);
6122 impl->load_timeout_id = 0;
6123 impl->load_state = LOAD_LOADING;
6125 load_set_model (impl);
6127 profile_end ("end", NULL);
6132 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6134 load_setup_timer (GtkFileChooserDefault *impl)
6136 g_assert (impl->load_timeout_id == 0);
6137 g_assert (impl->load_state != LOAD_PRELOAD);
6139 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6140 impl->load_state = LOAD_PRELOAD;
6143 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6145 load_remove_timer (GtkFileChooserDefault *impl)
6147 if (impl->load_timeout_id != 0)
6149 g_assert (impl->load_state == LOAD_PRELOAD);
6151 g_source_remove (impl->load_timeout_id);
6152 impl->load_timeout_id = 0;
6153 impl->load_state = LOAD_EMPTY;
6156 g_assert (impl->load_state == LOAD_EMPTY ||
6157 impl->load_state == LOAD_LOADING ||
6158 impl->load_state == LOAD_FINISHED);
6161 /* Selects the first row in the file list */
6163 browse_files_select_first_row (GtkFileChooserDefault *impl)
6166 GtkTreeIter dummy_iter;
6167 GtkTreeModel *tree_model;
6169 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6174 path = gtk_tree_path_new_from_indices (0, -1);
6176 /* If the list is empty, do nothing. */
6177 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6178 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6180 gtk_tree_path_free (path);
6183 struct center_selected_row_closure {
6184 GtkFileChooserDefault *impl;
6185 gboolean already_centered;
6188 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6189 * selected row in the tree view.
6192 center_selected_row_foreach_cb (GtkTreeModel *model,
6197 struct center_selected_row_closure *closure;
6200 if (closure->already_centered)
6203 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6204 closure->already_centered = TRUE;
6207 /* Centers the selected row in the tree view */
6209 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6211 struct center_selected_row_closure closure;
6212 GtkTreeSelection *selection;
6214 closure.impl = impl;
6215 closure.already_centered = FALSE;
6217 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6218 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6222 show_and_select_files (GtkFileChooserDefault *impl,
6225 GtkTreeSelection *selection;
6226 GtkFileSystemModel *fsmodel;
6227 gboolean enabled_hidden, removed_filters;
6228 gboolean selected_a_file;
6231 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6232 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6234 enabled_hidden = impl->show_hidden;
6235 removed_filters = (impl->current_filter == NULL);
6237 selected_a_file = FALSE;
6239 for (walk = files; walk; walk = walk->next)
6241 GFile *file = walk->data;
6244 /* Is it a hidden file? */
6246 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6249 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6251 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6253 if (!enabled_hidden &&
6254 (g_file_info_get_is_hidden (info) ||
6255 g_file_info_get_is_backup (info)))
6257 g_object_set (impl, "show-hidden", TRUE, NULL);
6258 enabled_hidden = TRUE;
6262 /* Is it a filtered file? */
6264 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6265 continue; /* re-get the iter as it may change when the model refilters */
6267 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6269 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6270 if (!removed_filters)
6272 set_current_filter (impl, NULL);
6273 removed_filters = TRUE;
6277 /* Okay, can we select the file now? */
6279 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6282 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6286 gtk_tree_selection_select_iter (selection, &iter);
6288 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6289 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6291 gtk_tree_path_free (path);
6293 selected_a_file = TRUE;
6297 browse_files_center_selected_row (impl);
6299 return selected_a_file;
6302 /* Processes the pending operation when a folder is finished loading */
6304 pending_select_files_process (GtkFileChooserDefault *impl)
6306 g_assert (impl->load_state == LOAD_FINISHED);
6307 g_assert (impl->browse_files_model != NULL);
6309 if (impl->pending_select_files)
6311 show_and_select_files (impl, impl->pending_select_files);
6312 pending_select_files_free (impl);
6313 browse_files_center_selected_row (impl);
6317 /* We only select the first row if the chooser is actually mapped ---
6318 * selecting the first row is to help the user when he is interacting with
6319 * the chooser, but sometimes a chooser works not on behalf of the user,
6320 * but rather on behalf of something else like GtkFileChooserButton. In
6321 * that case, the chooser's selection should be what the caller expects,
6322 * as the user can't see that something else got selected. See bug #165264.
6324 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6325 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6326 browse_files_select_first_row (impl);
6329 g_assert (impl->pending_select_files == NULL);
6333 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6338 info = g_file_query_info (impl->current_folder,
6339 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6340 G_FILE_QUERY_INFO_NONE,
6345 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6346 g_object_unref (info);
6349 msg = g_strdup (_("Could not read the contents of the folder"));
6351 error_message (impl, msg, error->message);
6355 /* Callback used when the file system model finishes loading */
6357 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6359 GtkFileChooserDefault *impl)
6361 profile_start ("start", NULL);
6364 show_error_on_reading_current_folder (impl, error);
6366 if (impl->load_state == LOAD_PRELOAD)
6368 load_remove_timer (impl);
6369 load_set_model (impl);
6371 else if (impl->load_state == LOAD_LOADING)
6377 /* We can't g_assert_not_reached(), as something other than us may have
6378 * initiated a folder reload. See #165556.
6380 profile_end ("end", NULL);
6384 g_assert (impl->load_timeout_id == 0);
6386 impl->load_state = LOAD_FINISHED;
6388 pending_select_files_process (impl);
6389 set_busy_cursor (impl, FALSE);
6390 #ifdef PROFILE_FILE_CHOOSER
6391 access ("MARK: *** FINISHED LOADING", F_OK);
6394 profile_end ("end", NULL);
6398 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6399 gboolean remove_from_treeview)
6401 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6403 if (impl->browse_files_model)
6405 g_object_unref (impl->browse_files_model);
6406 impl->browse_files_model = NULL;
6409 if (remove_from_treeview)
6410 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6414 my_g_format_time_for_display (glong secs)
6419 time_t time_mtime, time_now;
6420 const gchar *format;
6421 gchar *locale_format = NULL;
6423 char *date_str = NULL;
6425 const char *locale, *dot = NULL;
6426 gint64 codepage = -1;
6432 #ifdef HAVE_LOCALTIME_R
6433 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6436 struct tm *ptm = localtime ((time_t *) &time_mtime);
6440 g_warning ("ptm != NULL failed");
6442 return g_strdup (_("Unknown"));
6445 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6447 #endif /* HAVE_LOCALTIME_R */
6449 g_date_set_time_t (&mtime, time_mtime);
6450 time_now = time (NULL);
6451 g_date_set_time_t (&now, time_now);
6453 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6455 /* Translators: %H means "hours" and %M means "minutes" */
6457 format = _("%H:%M");
6458 else if (days_diff == 1)
6459 format = _("Yesterday at %H:%M");
6462 if (days_diff > 1 && days_diff < 7)
6463 format = "%A"; /* Days from last week */
6465 format = "%x"; /* Any other date */
6469 /* g_locale_from_utf8() returns a string in the system
6470 * code-page, which is not always the same as that used by the C
6471 * library. For instance when running a GTK+ program with
6472 * LANG=ko on an English version of Windows, the system
6473 * code-page is 1252, but the code-page used by the C library is
6474 * 949. (It's GTK+ itself that sets the C library locale when it
6475 * notices the LANG environment variable. See gtkmain.c The
6476 * Microsoft C library doesn't look at any locale environment
6477 * variables.) We need to pass strftime() a string in the C
6478 * library's code-page. See bug #509885.
6480 locale = setlocale (LC_ALL, NULL);
6482 dot = strchr (locale, '.');
6485 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6487 /* All codepages should fit in 16 bits AFAIK */
6488 if (codepage > 0 && codepage < 65536)
6490 sprintf (charset, "CP%u", (guint) codepage);
6491 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6495 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6497 if (locale_format != NULL &&
6498 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6501 /* As above but in opposite direction... */
6502 if (codepage > 0 && codepage < 65536)
6503 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6505 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6509 if (date_str == NULL)
6510 date_str = g_strdup (_("Unknown"));
6512 g_free (locale_format);
6517 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6519 GFileAttributeType type;
6522 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6523 g_file_info_set_attribute (to, attribute, type, value);
6527 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6529 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6530 GFile *file = G_FILE (object);
6531 GFileInfo *queried, *info;
6534 queried = g_file_query_info_finish (file, res, NULL);
6535 if (queried == NULL)
6538 GDK_THREADS_ENTER ();
6540 /* now we know model is valid */
6542 /* file was deleted */
6543 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6545 GDK_THREADS_LEAVE ();
6549 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6551 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6552 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6553 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6555 _gtk_file_system_model_update_file (model, file, info, FALSE);
6557 g_object_unref (info);
6559 GDK_THREADS_LEAVE ();
6563 file_system_model_set (GtkFileSystemModel *model,
6570 GtkFileChooserDefault *impl = data;
6574 case MODEL_COL_FILE:
6575 g_value_set_object (value, file);
6577 case MODEL_COL_NAME:
6579 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6581 g_value_set_string (value, g_file_info_get_display_name (info));
6583 case MODEL_COL_NAME_COLLATED:
6585 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6587 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6589 case MODEL_COL_IS_FOLDER:
6590 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6592 case MODEL_COL_PIXBUF:
6595 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6597 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6601 GtkTreeModel *tree_model;
6602 GtkTreePath *path, *start, *end;
6605 if (impl->browse_files_tree_view == NULL ||
6606 g_file_info_has_attribute (info, "filechooser::queried"))
6609 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6610 if (tree_model != GTK_TREE_MODEL (model))
6613 if (!_gtk_file_system_model_get_iter_for_file (model,
6616 g_assert_not_reached ();
6617 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6619 path = gtk_tree_model_get_path (tree_model, &iter);
6620 if (gtk_tree_path_compare (start, path) != 1 &&
6621 gtk_tree_path_compare (path, end) != 1)
6623 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6624 g_file_query_info_async (file,
6625 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6626 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6627 G_FILE_ATTRIBUTE_STANDARD_ICON,
6628 G_FILE_QUERY_INFO_NONE,
6630 _gtk_file_system_model_get_cancellable (model),
6631 file_system_model_got_thumbnail,
6634 gtk_tree_path_free (path);
6635 gtk_tree_path_free (start);
6636 gtk_tree_path_free (end);
6641 g_value_set_object (value, NULL);
6643 case MODEL_COL_SIZE:
6644 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6646 case MODEL_COL_SIZE_TEXT:
6647 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6648 g_value_set_string (value, NULL);
6650 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6652 case MODEL_COL_MTIME:
6653 case MODEL_COL_MTIME_TEXT:
6658 g_file_info_get_modification_time (info, &tv);
6659 if (column == MODEL_COL_MTIME)
6660 g_value_set_long (value, tv.tv_sec);
6661 else if (tv.tv_sec == 0)
6662 g_value_set_static_string (value, _("Unknown"));
6664 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6667 case MODEL_COL_ELLIPSIZE:
6668 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6671 g_assert_not_reached ();
6678 /* Gets rid of the old list model and creates a new one for the current folder */
6680 set_list_model (GtkFileChooserDefault *impl,
6683 g_assert (impl->current_folder != NULL);
6685 profile_start ("start", NULL);
6687 stop_loading_and_clear_list_model (impl, TRUE);
6689 set_busy_cursor (impl, TRUE);
6691 impl->browse_files_model =
6692 _gtk_file_system_model_new_for_directory (impl->current_folder,
6694 file_system_model_set,
6696 MODEL_COLUMN_TYPES);
6698 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6700 profile_msg (" set sort function", NULL);
6701 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6702 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6703 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6704 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6705 set_sort_column (impl);
6706 impl->list_sort_ascending = TRUE;
6707 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6708 G_CALLBACK (list_sort_column_changed_cb), impl);
6710 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6712 g_signal_connect (impl->browse_files_model, "finished-loading",
6713 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6715 install_list_model_filter (impl);
6717 profile_end ("end", NULL);
6722 struct update_chooser_entry_selected_foreach_closure {
6724 GtkTreeIter first_selected_iter;
6728 compare_utf8_filenames (const gchar *a,
6731 gchar *a_folded, *b_folded;
6734 a_folded = g_utf8_strdown (a, -1);
6735 b_folded = g_utf8_strdown (b, -1);
6737 retval = strcmp (a_folded, b_folded);
6746 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6751 struct update_chooser_entry_selected_foreach_closure *closure;
6754 closure->num_selected++;
6756 if (closure->num_selected == 1)
6757 closure->first_selected_iter = *iter;
6761 update_chooser_entry (GtkFileChooserDefault *impl)
6763 GtkTreeSelection *selection;
6764 struct update_chooser_entry_selected_foreach_closure closure;
6766 /* no need to update the file chooser's entry if there's no entry */
6767 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6768 !impl->location_entry)
6771 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6772 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6773 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6774 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6775 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6778 g_assert (impl->location_entry != NULL);
6780 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6781 closure.num_selected = 0;
6782 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6784 if (closure.num_selected == 0)
6786 if (impl->operation_mode == OPERATION_MODE_RECENT)
6787 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6789 goto maybe_clear_entry;
6791 else if (closure.num_selected == 1)
6793 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6796 gboolean change_entry;
6798 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6800 /* If the cursor moved to the row of the newly created folder,
6801 * retrieving info will return NULL.
6806 g_free (impl->browse_files_last_selected_name);
6807 impl->browse_files_last_selected_name =
6808 g_strdup (g_file_info_get_display_name (info));
6810 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6811 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6812 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6814 /* Don't change the name when clicking on a folder... */
6815 change_entry = (! _gtk_file_info_consider_as_directory (info));
6818 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6822 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6824 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6825 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6830 else if (impl->operation_mode == OPERATION_MODE_RECENT
6831 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6835 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
6837 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
6838 MODEL_COL_FILE, &folder,
6840 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
6841 g_object_unref (folder);
6847 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6848 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6850 /* Multiple selection, so just clear the entry. */
6851 g_free (impl->browse_files_last_selected_name);
6852 impl->browse_files_last_selected_name = NULL;
6854 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6860 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6861 && impl->browse_files_last_selected_name)
6863 const char *entry_text;
6865 gboolean clear_entry;
6867 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6868 len = strlen (entry_text);
6871 /* The file chooser entry may have appended a "/" to its text.
6872 * So take it out, and compare the result to the old selection.
6874 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6878 tmp = g_strndup (entry_text, len - 1);
6879 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6883 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6886 clear_entry = FALSE;
6889 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6894 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6898 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6902 struct UpdateCurrentFolderData
6904 GtkFileChooserDefault *impl;
6906 gboolean keep_trail;
6907 gboolean clear_entry;
6908 GFile *original_file;
6909 GError *original_error;
6913 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6914 GtkFileSystemVolume *volume,
6915 const GError *error,
6918 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6919 struct UpdateCurrentFolderData *data = user_data;
6920 GtkFileChooserDefault *impl = data->impl;
6922 if (cancellable != impl->update_current_folder_cancellable)
6925 impl->update_current_folder_cancellable = NULL;
6926 set_busy_cursor (impl, FALSE);
6933 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6934 impl->reload_state = RELOAD_EMPTY;
6938 change_folder_and_display_error (impl, data->file, data->clear_entry);
6941 g_object_unref (data->file);
6944 g_object_unref (cancellable);
6948 update_current_folder_get_info_cb (GCancellable *cancellable,
6950 const GError *error,
6953 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6954 struct UpdateCurrentFolderData *data = user_data;
6955 GtkFileChooserDefault *impl = data->impl;
6957 if (cancellable != impl->update_current_folder_cancellable)
6960 impl->update_current_folder_cancellable = NULL;
6961 impl->reload_state = RELOAD_EMPTY;
6963 set_busy_cursor (impl, FALSE);
6972 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6974 GMountOperation *mount_operation;
6975 GtkWidget *toplevel;
6977 g_object_unref (cancellable);
6978 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6980 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6982 set_busy_cursor (impl, TRUE);
6984 impl->update_current_folder_cancellable =
6985 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
6987 update_current_folder_mount_enclosing_volume_cb,
6993 if (!data->original_file)
6995 data->original_file = g_object_ref (data->file);
6996 data->original_error = g_error_copy (error);
6999 parent_file = g_file_get_parent (data->file);
7001 /* get parent path and try to change the folder to that */
7004 g_object_unref (data->file);
7005 data->file = parent_file;
7007 g_object_unref (cancellable);
7009 /* restart the update current folder operation */
7010 impl->reload_state = RELOAD_HAS_FOLDER;
7012 impl->update_current_folder_cancellable =
7013 _gtk_file_system_get_info (impl->file_system, data->file,
7015 update_current_folder_get_info_cb,
7018 set_busy_cursor (impl, TRUE);
7024 /* Error and bail out, ignoring "not found" errors since they're useless:
7025 * they only happen when a program defaults to a folder that has been (re)moved.
7027 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7028 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7030 g_error_free (data->original_error);
7032 g_object_unref (data->original_file);
7038 if (data->original_file)
7040 /* Error and bail out, ignoring "not found" errors since they're useless:
7041 * they only happen when a program defaults to a folder that has been (re)moved.
7043 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7044 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7046 g_error_free (data->original_error);
7048 g_object_unref (data->original_file);
7051 if (! _gtk_file_info_consider_as_directory (info))
7054 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7057 if (impl->current_folder != data->file)
7059 if (impl->current_folder)
7060 g_object_unref (impl->current_folder);
7062 impl->current_folder = g_object_ref (data->file);
7065 impl->reload_state = RELOAD_HAS_FOLDER;
7067 /* Update the widgets that may trigger a folder change themselves. */
7069 if (!impl->changing_folder)
7071 impl->changing_folder = TRUE;
7073 shortcuts_update_current_folder (impl);
7075 impl->changing_folder = FALSE;
7078 /* Set the folder on the save entry */
7080 if (impl->location_entry)
7082 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7083 impl->current_folder);
7085 if (data->clear_entry)
7086 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7089 /* Create a new list model. This is slightly evil; we store the result value
7090 * but perform more actions rather than returning immediately even if it
7091 * generates an error.
7093 set_list_model (impl, NULL);
7095 /* Refresh controls */
7097 shortcuts_find_current_folder (impl);
7099 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7101 check_preview_change (impl);
7102 bookmarks_check_add_sensitivity (impl);
7104 g_signal_emit_by_name (impl, "selection-changed", 0);
7107 g_object_unref (data->file);
7110 g_object_unref (cancellable);
7114 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7116 gboolean keep_trail,
7117 gboolean clear_entry,
7120 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7121 struct UpdateCurrentFolderData *data;
7123 profile_start ("start", NULL);
7125 g_object_ref (file);
7127 switch (impl->operation_mode)
7129 case OPERATION_MODE_SEARCH:
7130 search_switch_to_browse_mode (impl);
7132 case OPERATION_MODE_RECENT:
7133 recent_switch_to_browse_mode (impl);
7135 case OPERATION_MODE_BROWSE:
7139 if (impl->local_only && !g_file_is_native (file))
7141 g_set_error_literal (error,
7142 GTK_FILE_CHOOSER_ERROR,
7143 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7144 _("Cannot change to folder because it is not local"));
7146 g_object_unref (file);
7147 profile_end ("end - not local", NULL);
7151 if (impl->update_current_folder_cancellable)
7152 g_cancellable_cancel (impl->update_current_folder_cancellable);
7154 /* Test validity of path here. */
7155 data = g_new0 (struct UpdateCurrentFolderData, 1);
7157 data->file = g_object_ref (file);
7158 data->keep_trail = keep_trail;
7159 data->clear_entry = clear_entry;
7161 impl->reload_state = RELOAD_HAS_FOLDER;
7163 impl->update_current_folder_cancellable =
7164 _gtk_file_system_get_info (impl->file_system, file,
7166 update_current_folder_get_info_cb,
7169 set_busy_cursor (impl, TRUE);
7170 g_object_unref (file);
7172 profile_end ("end", NULL);
7177 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7179 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7181 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7182 impl->operation_mode == OPERATION_MODE_RECENT)
7185 if (impl->reload_state == RELOAD_EMPTY)
7187 /* We are unmapped, or we had an error while loading the last folder.
7188 * We'll return the folder used by the last invocation of the file chooser
7189 * since once we get (re)mapped, we'll load *that* folder anyway unless
7190 * the caller explicitly calls set_current_folder() on us.
7192 return get_file_for_last_folder_opened (impl);
7195 if (impl->current_folder)
7196 return g_object_ref (impl->current_folder);
7202 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7205 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7207 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7208 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7210 pending_select_files_free (impl);
7211 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7215 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7219 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7223 parent_file = g_file_get_parent (file);
7226 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7228 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7229 impl->operation_mode == OPERATION_MODE_RECENT ||
7230 impl->load_state == LOAD_EMPTY)
7236 g_assert (impl->current_folder != NULL);
7238 same_path = g_file_equal (parent_file, impl->current_folder);
7241 if (same_path && impl->load_state == LOAD_FINISHED)
7246 files.data = (gpointer) file;
7249 result = show_and_select_files (impl, &files);
7250 g_object_unref (parent_file);
7254 pending_select_files_add (impl, file);
7260 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7261 g_object_unref (parent_file);
7265 g_object_unref (parent_file);
7270 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7273 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7274 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7277 if (!impl->browse_files_model)
7280 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7285 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7290 maybe_select (GtkTreeModel *model,
7295 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7296 GtkTreeSelection *selection;
7299 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7301 gtk_tree_model_get (model, iter,
7302 MODEL_COL_IS_FOLDER, &is_folder,
7305 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7306 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7307 gtk_tree_selection_select_iter (selection, iter);
7309 gtk_tree_selection_unselect_iter (selection, iter);
7315 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7317 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7319 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7320 impl->operation_mode == OPERATION_MODE_RECENT)
7322 GtkTreeSelection *selection;
7324 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7325 gtk_tree_selection_select_all (selection);
7329 if (impl->select_multiple)
7330 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7331 maybe_select, impl);
7335 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7337 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7338 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7340 gtk_tree_selection_unselect_all (selection);
7341 pending_select_files_free (impl);
7344 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7346 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7348 * is_empty_ret - whether the file entry is totally empty
7350 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7351 * the path will be "$cwd/foobar")
7354 check_save_entry (GtkFileChooserDefault *impl,
7356 gboolean *is_well_formed_ret,
7357 gboolean *is_empty_ret,
7358 gboolean *is_file_part_empty_ret,
7359 gboolean *is_folder)
7361 GtkFileChooserEntry *chooser_entry;
7362 GFile *current_folder;
7363 const char *file_part;
7367 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7368 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7369 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7370 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7371 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7373 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7375 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7378 *is_well_formed_ret = TRUE;
7379 *is_empty_ret = TRUE;
7380 *is_file_part_empty_ret = TRUE;
7386 *is_empty_ret = FALSE;
7388 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7389 if (!current_folder)
7392 *is_well_formed_ret = FALSE;
7393 *is_file_part_empty_ret = FALSE;
7399 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7401 if (!file_part || file_part[0] == '\0')
7403 *file_ret = g_object_ref (current_folder);
7404 *is_well_formed_ret = TRUE;
7405 *is_file_part_empty_ret = TRUE;
7411 *is_file_part_empty_ret = FALSE;
7414 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7418 error_building_filename_dialog (impl, error);
7420 *is_well_formed_ret = FALSE;
7427 *is_well_formed_ret = TRUE;
7428 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7431 struct get_files_closure {
7432 GtkFileChooserDefault *impl;
7434 GFile *file_from_entry;
7438 get_files_foreach (GtkTreeModel *model,
7443 struct get_files_closure *info;
7445 GtkFileSystemModel *fs_model;
7448 fs_model = info->impl->browse_files_model;
7450 file = _gtk_file_system_model_get_file (fs_model, iter);
7452 return; /* We are on the editable row */
7454 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7455 info->result = g_slist_prepend (info->result, g_object_ref (file));
7459 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7461 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7462 struct get_files_closure info;
7463 GtkWindow *toplevel;
7464 GtkWidget *current_focus;
7465 gboolean file_list_seen;
7469 info.file_from_entry = NULL;
7471 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7472 return search_get_selected_files (impl);
7474 if (impl->operation_mode == OPERATION_MODE_RECENT)
7476 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7478 file_list_seen = TRUE;
7482 return recent_get_selected_files (impl);
7485 toplevel = get_toplevel (GTK_WIDGET (impl));
7487 current_focus = gtk_window_get_focus (toplevel);
7489 current_focus = NULL;
7491 file_list_seen = FALSE;
7492 if (current_focus == impl->browse_files_tree_view)
7494 GtkTreeSelection *selection;
7498 file_list_seen = TRUE;
7499 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7500 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7502 /* If there is no selection in the file list, we probably have this situation:
7504 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7505 * 2. He then double-clicked on a folder ("bar") in the file list
7507 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7508 * filename entry to see if that is the case.
7510 if (info.result == NULL && impl->location_entry)
7513 else if (impl->location_entry && current_focus == impl->location_entry)
7515 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7519 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7524 if (!is_well_formed)
7527 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7529 g_object_unref (info.file_from_entry);
7533 if (info.file_from_entry)
7534 info.result = g_slist_prepend (info.result, info.file_from_entry);
7535 else if (!file_list_seen)
7540 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7542 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7546 /* The focus is on a dialog's action area button or something else */
7547 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7548 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7556 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7557 * fall back to the current directory */
7558 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7559 info.result == NULL)
7561 GFile *current_folder;
7563 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7566 info.result = g_slist_prepend (info.result, current_folder);
7569 return g_slist_reverse (info.result);
7573 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7575 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7577 if (impl->preview_file)
7578 return g_object_ref (impl->preview_file);
7583 static GtkFileSystem *
7584 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7586 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7588 return impl->file_system;
7591 /* Shows or hides the filter widgets */
7593 show_filters (GtkFileChooserDefault *impl,
7597 gtk_widget_show (impl->filter_combo_hbox);
7599 gtk_widget_hide (impl->filter_combo_hbox);
7603 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7604 GtkFileFilter *filter)
7606 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7609 if (g_slist_find (impl->filters, filter))
7611 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7615 g_object_ref_sink (filter);
7616 impl->filters = g_slist_append (impl->filters, filter);
7618 name = gtk_file_filter_get_name (filter);
7620 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7622 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7624 if (!g_slist_find (impl->filters, impl->current_filter))
7625 set_current_filter (impl, filter);
7627 show_filters (impl, TRUE);
7631 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7632 GtkFileFilter *filter)
7634 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7635 GtkTreeModel *model;
7639 filter_index = g_slist_index (impl->filters, filter);
7641 if (filter_index < 0)
7643 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7647 impl->filters = g_slist_remove (impl->filters, filter);
7649 if (filter == impl->current_filter)
7652 set_current_filter (impl, impl->filters->data);
7654 set_current_filter (impl, NULL);
7657 /* Remove row from the combo box */
7658 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7659 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7660 g_assert_not_reached ();
7662 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7664 g_object_unref (filter);
7667 show_filters (impl, FALSE);
7671 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7673 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7675 return g_slist_copy (impl->filters);
7678 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7680 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7683 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7686 struct AddShortcutData
7688 GtkFileChooserDefault *impl;
7693 add_shortcut_get_info_cb (GCancellable *cancellable,
7695 const GError *error,
7699 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7700 struct AddShortcutData *data = user_data;
7702 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7705 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7707 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7710 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7712 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7715 g_object_unref (data->impl);
7716 g_object_unref (data->file);
7719 g_object_unref (cancellable);
7723 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7727 GCancellable *cancellable;
7728 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7729 struct AddShortcutData *data;
7733 /* Avoid adding duplicates */
7734 pos = shortcut_find_position (impl, file);
7735 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7739 uri = g_file_get_uri (file);
7740 /* translators, "Shortcut" means "Bookmark" here */
7742 GTK_FILE_CHOOSER_ERROR,
7743 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7744 _("Shortcut %s already exists"),
7751 for (l = impl->loading_shortcuts; l; l = l->next)
7753 GCancellable *c = l->data;
7756 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7757 if (f && g_file_equal (file, f))
7761 uri = g_file_get_uri (file);
7763 GTK_FILE_CHOOSER_ERROR,
7764 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7765 _("Shortcut %s already exists"),
7773 data = g_new0 (struct AddShortcutData, 1);
7774 data->impl = g_object_ref (impl);
7775 data->file = g_object_ref (file);
7777 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7779 add_shortcut_get_info_cb, data);
7784 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7785 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7791 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7795 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7802 for (l = impl->loading_shortcuts; l; l = l->next)
7804 GCancellable *c = l->data;
7807 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7808 if (f && g_file_equal (file, f))
7810 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7811 g_cancellable_cancel (c);
7816 if (impl->num_shortcuts == 0)
7819 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7820 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7821 g_assert_not_reached ();
7823 for (i = 0; i < impl->num_shortcuts; i++)
7826 ShortcutType shortcut_type;
7829 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7830 SHORTCUTS_COL_DATA, &col_data,
7831 SHORTCUTS_COL_TYPE, &shortcut_type,
7833 g_assert (col_data != NULL);
7834 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7836 shortcut = col_data;
7837 if (g_file_equal (shortcut, file))
7839 shortcuts_remove_rows (impl, pos + i, 1);
7840 impl->num_shortcuts--;
7844 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7845 g_assert_not_reached ();
7850 uri = g_file_get_uri (file);
7851 /* translators, "Shortcut" means "Bookmark" here */
7853 GTK_FILE_CHOOSER_ERROR,
7854 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7855 _("Shortcut %s does not exist"),
7863 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7865 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7871 if (impl->num_shortcuts == 0)
7874 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7875 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7876 g_assert_not_reached ();
7880 for (i = 0; i < impl->num_shortcuts; i++)
7883 ShortcutType shortcut_type;
7886 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7887 SHORTCUTS_COL_DATA, &col_data,
7888 SHORTCUTS_COL_TYPE, &shortcut_type,
7890 g_assert (col_data != NULL);
7891 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7893 shortcut = col_data;
7894 list = g_slist_prepend (list, g_object_ref (shortcut));
7896 if (i != impl->num_shortcuts - 1)
7898 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7899 g_assert_not_reached ();
7903 return g_slist_reverse (list);
7906 /* Guesses a size based upon font sizes */
7908 find_good_size_from_style (GtkWidget *widget,
7912 GtkStyleContext *context;
7913 GtkStateFlags state;
7918 context = gtk_widget_get_style_context (widget);
7919 state = gtk_widget_get_state_flags (widget);
7921 screen = gtk_widget_get_screen (widget);
7924 resolution = gdk_screen_get_resolution (screen);
7925 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7929 resolution = 96.0; /* wheeee */
7931 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7932 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7934 *width = font_size * NUM_CHARS;
7935 *height = font_size * NUM_LINES;
7939 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7940 gint *default_width,
7941 gint *default_height)
7943 GtkFileChooserDefault *impl;
7945 int x, y, width, height;
7947 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7949 settings_ensure (impl);
7951 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7952 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7954 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7956 *default_width = width;
7957 *default_height = height;
7961 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7963 if (impl->preview_widget_active &&
7964 impl->preview_widget &&
7965 gtk_widget_get_visible (impl->preview_widget))
7967 gtk_widget_get_preferred_size (impl->preview_box,
7969 *default_width += PREVIEW_HBOX_SPACING + req.width;
7972 if (impl->extra_widget &&
7973 gtk_widget_get_visible (impl->extra_widget))
7975 gtk_widget_get_preferred_size (impl->extra_align,
7977 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
7981 struct switch_folder_closure {
7982 GtkFileChooserDefault *impl;
7987 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7989 switch_folder_foreach_cb (GtkTreeModel *model,
7994 struct switch_folder_closure *closure;
7998 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
7999 closure->num_selected++;
8002 /* Changes to the selected folder in the list view */
8004 switch_to_selected_folder (GtkFileChooserDefault *impl)
8006 GtkTreeSelection *selection;
8007 struct switch_folder_closure closure;
8009 /* We do this with foreach() rather than get_selected() as we may be in
8010 * multiple selection mode
8013 closure.impl = impl;
8014 closure.file = NULL;
8015 closure.num_selected = 0;
8017 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8018 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8020 g_assert (closure.file && closure.num_selected == 1);
8022 change_folder_and_display_error (impl, closure.file, FALSE);
8025 /* Gets the GFileInfo for the selected row in the file list; assumes single
8029 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8030 gboolean *had_selection)
8032 GtkTreeSelection *selection;
8036 g_assert (!impl->select_multiple);
8037 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8038 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8040 *had_selection = FALSE;
8044 *had_selection = TRUE;
8046 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8050 /* Gets the display name of the selected file in the file list; assumes single
8051 * selection mode and that something is selected.
8053 static const gchar *
8054 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8057 gboolean had_selection;
8059 info = get_selected_file_info_from_file_list (impl, &had_selection);
8060 g_assert (had_selection);
8061 g_assert (info != NULL);
8063 return g_file_info_get_display_name (info);
8067 add_custom_button_to_dialog (GtkDialog *dialog,
8068 const gchar *mnemonic_label,
8069 const gchar *stock_id,
8074 button = gtk_button_new_with_mnemonic (mnemonic_label);
8075 gtk_widget_set_can_default (button, TRUE);
8076 gtk_button_set_image (GTK_BUTTON (button),
8077 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8078 gtk_widget_show (button);
8080 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8083 /* Presents an overwrite confirmation dialog; returns whether we should accept
8087 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8088 const gchar *file_part,
8089 const gchar *folder_display_name)
8091 GtkWindow *toplevel;
8095 toplevel = get_toplevel (GTK_WIDGET (impl));
8097 dialog = gtk_message_dialog_new (toplevel,
8098 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8099 GTK_MESSAGE_QUESTION,
8101 _("A file named \"%s\" already exists. Do you want to replace it?"),
8103 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8104 _("The file already exists in \"%s\". Replacing it will "
8105 "overwrite its contents."),
8106 folder_display_name);
8108 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8109 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8110 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8111 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8112 GTK_RESPONSE_ACCEPT,
8113 GTK_RESPONSE_CANCEL,
8115 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8117 if (gtk_window_has_group (toplevel))
8118 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8119 GTK_WINDOW (dialog));
8121 response = gtk_dialog_run (GTK_DIALOG (dialog));
8123 gtk_widget_destroy (dialog);
8125 return (response == GTK_RESPONSE_ACCEPT);
8128 struct GetDisplayNameData
8130 GtkFileChooserDefault *impl;
8134 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8135 * as requesting a response means, "the dialog is confirmed".
8138 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8140 g_signal_emit_by_name (impl, "response-requested");
8141 add_selection_to_recent_list (impl);
8145 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8147 const GError *error,
8150 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8151 gboolean should_respond = FALSE;
8152 struct GetDisplayNameData *data = user_data;
8154 if (cancellable != data->impl->should_respond_get_info_cancellable)
8157 data->impl->should_respond_get_info_cancellable = NULL;
8163 /* Huh? Did the folder disappear? Let the caller deal with it */
8164 should_respond = TRUE;
8166 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8168 set_busy_cursor (data->impl, FALSE);
8170 request_response_and_add_to_recent_list (data->impl);
8173 g_object_unref (data->impl);
8174 g_free (data->file_part);
8177 g_object_unref (cancellable);
8180 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8181 * should respond. Can get the file part from the file list or the save entry.
8184 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8185 const gchar *file_part,
8188 GtkFileChooserConfirmation conf;
8190 if (!impl->do_overwrite_confirmation)
8193 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8195 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8199 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8201 struct GetDisplayNameData *data;
8203 g_assert (file_part != NULL);
8205 data = g_new0 (struct GetDisplayNameData, 1);
8206 data->impl = g_object_ref (impl);
8207 data->file_part = g_strdup (file_part);
8209 if (impl->should_respond_get_info_cancellable)
8210 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8212 impl->should_respond_get_info_cancellable =
8213 _gtk_file_system_get_info (impl->file_system, parent_file,
8214 "standard::display-name",
8215 confirmation_confirm_get_info_cb,
8217 set_busy_cursor (data->impl, TRUE);
8221 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8224 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8228 g_assert_not_reached ();
8233 struct FileExistsData
8235 GtkFileChooserDefault *impl;
8236 gboolean file_exists_and_is_not_folder;
8242 name_entry_get_parent_info_cb (GCancellable *cancellable,
8244 const GError *error,
8247 gboolean parent_is_folder;
8248 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8249 struct FileExistsData *data = user_data;
8251 if (cancellable != data->impl->should_respond_get_info_cancellable)
8254 data->impl->should_respond_get_info_cancellable = NULL;
8256 set_busy_cursor (data->impl, FALSE);
8262 parent_is_folder = FALSE;
8264 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8266 if (parent_is_folder)
8268 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8270 request_response_and_add_to_recent_list (data->impl); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8272 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8274 if (data->file_exists_and_is_not_folder)
8279 /* Dup the string because the string may be modified
8280 * depending on what clients do in the confirm-overwrite
8281 * signal and this corrupts the pointer
8283 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8284 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8288 request_response_and_add_to_recent_list (data->impl);
8291 request_response_and_add_to_recent_list (data->impl);
8293 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8294 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8296 GError *mkdir_error = NULL;
8298 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8299 * "/blah/nonexistent" you *will* want a folder created.
8302 set_busy_cursor (data->impl, TRUE);
8303 g_file_make_directory (data->file, NULL, &mkdir_error);
8304 set_busy_cursor (data->impl, FALSE);
8307 request_response_and_add_to_recent_list (data->impl);
8309 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8312 g_assert_not_reached ();
8318 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8319 error_with_file_under_nonfolder (data->impl, data->parent_file);
8325 /* The parent folder is not readable for some reason */
8327 error_copy = g_error_copy (error);
8328 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8333 g_object_unref (data->impl);
8334 g_object_unref (data->file);
8335 g_object_unref (data->parent_file);
8338 g_object_unref (cancellable);
8342 file_exists_get_info_cb (GCancellable *cancellable,
8344 const GError *error,
8347 gboolean data_ownership_taken = FALSE;
8348 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8349 gboolean file_exists;
8351 gboolean needs_parent_check = FALSE;
8352 struct FileExistsData *data = user_data;
8354 if (cancellable != data->impl->file_exists_get_info_cancellable)
8357 data->impl->file_exists_get_info_cancellable = NULL;
8359 set_busy_cursor (data->impl, FALSE);
8364 file_exists = (info != NULL);
8365 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8367 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8370 change_folder_and_display_error (data->impl, data->file, TRUE);
8374 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8376 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8379 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8381 if (file_exists && !is_folder)
8383 /* Oops, the user typed the name of an existing path which is not
8386 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8387 g_error_copy (error));
8391 needs_parent_check = TRUE;
8394 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8398 needs_parent_check = TRUE;
8404 /* User typed a folder; we are done */
8405 request_response_and_add_to_recent_list (data->impl);
8408 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8411 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8414 change_folder_and_display_error (data->impl, data->file, TRUE);
8416 needs_parent_check = TRUE;
8420 g_assert_not_reached();
8423 if (needs_parent_check) {
8424 /* check that everything up to the last path component exists (i.e. the parent) */
8426 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8427 data_ownership_taken = TRUE;
8429 if (data->impl->should_respond_get_info_cancellable)
8430 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8432 data->impl->should_respond_get_info_cancellable =
8433 _gtk_file_system_get_info (data->impl->file_system,
8436 name_entry_get_parent_info_cb,
8438 set_busy_cursor (data->impl, TRUE);
8442 if (!data_ownership_taken)
8444 g_object_unref (data->impl);
8445 g_object_unref (data->file);
8446 g_object_unref (data->parent_file);
8450 g_object_unref (cancellable);
8454 paste_text_received (GtkClipboard *clipboard,
8456 GtkFileChooserDefault *impl)
8463 file = g_file_new_for_uri (text);
8465 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8466 location_popup_handler (impl, text);
8468 g_object_unref (file);
8471 /* Handler for the "location-popup-on-paste" keybinding signal */
8473 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8475 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8476 GDK_SELECTION_CLIPBOARD);
8477 gtk_clipboard_request_text (clipboard,
8478 (GtkClipboardTextReceivedFunc) paste_text_received,
8482 /* Implementation for GtkFileChooserEmbed::should_respond() */
8484 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8489 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8491 for (l = files; l; l = l->next)
8493 GFile *file = l->data;
8496 uri = g_file_get_uri (file);
8499 gtk_recent_manager_add_item (impl->recent_manager, uri);
8504 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8505 g_slist_free (files);
8509 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8511 GtkFileChooserDefault *impl;
8512 GtkWidget *toplevel;
8513 GtkWidget *current_focus;
8516 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8518 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8519 g_assert (GTK_IS_WINDOW (toplevel));
8523 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8525 if (current_focus == impl->browse_files_tree_view)
8527 /* The following array encodes what we do based on the impl->action and the
8528 * number of files selected.
8531 NOOP, /* Do nothing (don't respond) */
8532 RESPOND, /* Respond immediately */
8533 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8534 ALL_FILES, /* Respond only if everything selected is a file */
8535 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8536 SAVE_ENTRY, /* Go to the code for handling the save entry */
8537 NOT_REACHED /* Sanity check */
8539 static const ActionToTake what_to_do[4][3] = {
8540 /* 0 selected 1 selected many selected */
8541 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8542 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8543 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8544 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8548 gboolean all_files, all_folders;
8550 ActionToTake action;
8554 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8556 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8558 retval = search_should_respond (impl);
8562 if (impl->operation_mode == OPERATION_MODE_RECENT)
8564 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8568 retval = recent_should_respond (impl);
8573 selection_check (impl, &num_selected, &all_files, &all_folders);
8575 if (num_selected > 2)
8580 action = what_to_do [impl->action] [k];
8591 case RESPOND_OR_SWITCH:
8592 g_assert (num_selected == 1);
8596 switch_to_selected_folder (impl);
8599 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8601 retval = should_respond_after_confirm_overwrite (impl,
8602 get_display_name_from_file_list (impl),
8603 impl->current_folder);
8617 retval = all_folders;
8624 g_assert_not_reached ();
8627 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8630 gboolean is_well_formed, is_empty, is_file_part_empty;
8632 GtkFileChooserEntry *entry;
8637 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8638 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8639 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8640 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8641 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8643 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8644 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8646 if (!is_well_formed)
8649 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8650 && impl->operation_mode == OPERATION_MODE_RECENT)
8652 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8654 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8655 * thus changing our assumption that no selection is present - setting
8656 * a selection causes the error message from path_bar_set_mode() to go away,
8657 * but we want the user to see that message!
8659 gtk_widget_grab_focus (impl->browse_files_tree_view);
8662 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8669 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8670 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8672 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8673 gtk_widget_grab_focus (impl->location_entry);
8680 g_assert (file != NULL);
8685 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8686 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8688 change_folder_and_display_error (impl, file, TRUE);
8690 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8691 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8693 /* The folder already exists, so we do not need to create it.
8694 * Just respond to terminate the dialog.
8700 g_assert_not_reached ();
8705 struct FileExistsData *data;
8707 /* We need to check whether file exists and whether it is a folder -
8708 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8709 * negative (it doesn't know yet if your last path component is a
8713 data = g_new0 (struct FileExistsData, 1);
8714 data->impl = g_object_ref (impl);
8715 data->file = g_object_ref (file);
8716 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8718 if (impl->file_exists_get_info_cancellable)
8719 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8721 impl->file_exists_get_info_cancellable =
8722 _gtk_file_system_get_info (impl->file_system, file,
8724 file_exists_get_info_cb,
8727 set_busy_cursor (impl, TRUE);
8730 g_error_free (error);
8733 g_object_unref (file);
8735 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8737 /* The focus is on a dialog's action area button, *and* the widget that
8738 * was focused immediately before it is the file list.
8742 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8744 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8747 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8749 /* The focus is on a dialog's action area button, *and* the widget that
8750 * was focused immediately before it is the location entry.
8755 /* The focus is on a dialog's action area button or something else */
8756 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8757 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8765 add_selection_to_recent_list (impl);
8770 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8772 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8774 GtkFileChooserDefault *impl;
8777 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8779 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8780 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8782 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8783 widget = impl->browse_files_tree_view;
8785 widget = impl->location_entry;
8787 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8788 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8789 widget = impl->location_entry;
8792 g_assert_not_reached ();
8796 g_assert (widget != NULL);
8797 gtk_widget_grab_focus (widget);
8800 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8802 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8812 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8813 *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 */
8816 /* Constructs a list of the selected paths in search mode */
8818 search_get_selected_files (GtkFileChooserDefault *impl)
8821 GtkTreeSelection *selection;
8825 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8826 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8827 result = g_slist_reverse (result);
8832 /* Called from ::should_respond(). We return whether there are selected files
8833 * in the search list.
8836 search_should_respond (GtkFileChooserDefault *impl)
8838 GtkTreeSelection *selection;
8840 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8842 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8843 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8846 /* Adds one hit from the search engine to the search_model */
8848 search_add_hit (GtkFileChooserDefault *impl,
8853 file = g_file_new_for_uri (uri);
8857 if (!g_file_is_native (file))
8859 g_object_unref (file);
8863 _gtk_file_system_model_add_and_query_file (impl->search_model,
8867 g_object_unref (file);
8870 /* Callback used from GtkSearchEngine when we get new hits */
8872 search_engine_hits_added_cb (GtkSearchEngine *engine,
8876 GtkFileChooserDefault *impl;
8879 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8881 for (l = hits; l; l = l->next)
8882 search_add_hit (impl, (gchar*)l->data);
8885 /* Callback used from GtkSearchEngine when the query is done running */
8887 search_engine_finished_cb (GtkSearchEngine *engine,
8890 GtkFileChooserDefault *impl;
8892 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8895 /* EB: setting the model here will avoid loads of row events,
8896 * but it'll make the search look like blocked.
8898 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8899 GTK_TREE_MODEL (impl->search_model));
8902 /* FMQ: if search was empty, say that we got no hits */
8903 set_busy_cursor (impl, FALSE);
8906 /* Displays a generic error when we cannot create a GtkSearchEngine.
8907 * It would be better if _gtk_search_engine_new() gave us a GError
8908 * with a better message, but it doesn't do that right now.
8911 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8913 error_message (impl,
8914 _("Could not start the search process"),
8915 _("The program was not able to create a connection to the indexer "
8916 "daemon. Please make sure it is running."));
8920 search_engine_error_cb (GtkSearchEngine *engine,
8921 const gchar *message,
8924 GtkFileChooserDefault *impl;
8926 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8928 search_stop_searching (impl, TRUE);
8929 error_message (impl, _("Could not send the search request"), message);
8931 set_busy_cursor (impl, FALSE);
8934 /* Frees the data in the search_model */
8936 search_clear_model (GtkFileChooserDefault *impl,
8937 gboolean remove_from_treeview)
8939 if (!impl->search_model)
8942 g_object_unref (impl->search_model);
8943 impl->search_model = NULL;
8945 if (remove_from_treeview)
8946 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8949 /* Stops any ongoing searches; does not touch the search_model */
8951 search_stop_searching (GtkFileChooserDefault *impl,
8952 gboolean remove_query)
8954 if (remove_query && impl->search_query)
8956 g_object_unref (impl->search_query);
8957 impl->search_query = NULL;
8960 if (impl->search_engine)
8962 _gtk_search_engine_stop (impl->search_engine);
8964 g_object_unref (impl->search_engine);
8965 impl->search_engine = NULL;
8969 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8971 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8973 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8975 search_stop_searching (impl, FALSE);
8976 search_clear_model (impl, TRUE);
8978 gtk_widget_destroy (impl->search_hbox);
8979 impl->search_hbox = NULL;
8980 impl->search_entry = NULL;
8982 impl->operation_mode = OPERATION_MODE_BROWSE;
8983 path_bar_update (impl);
8985 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8986 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8988 gtk_widget_show (impl->location_button);
8990 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8991 gtk_widget_show (impl->location_entry_box);
8994 file_list_set_sort_column_ids (impl);
8997 /* Creates the search_model and puts it in the tree view */
8999 search_setup_model (GtkFileChooserDefault *impl)
9001 g_assert (impl->search_model == NULL);
9003 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9005 MODEL_COLUMN_TYPES);
9007 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9011 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9015 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9019 set_sort_column (impl);
9021 /* EB: setting the model here will make the hits list update feel
9022 * more "alive" than setting the model at the end of the search
9025 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9026 GTK_TREE_MODEL (impl->search_model));
9029 /* Creates a new query with the specified text and launches it */
9031 search_start_query (GtkFileChooserDefault *impl,
9032 const gchar *query_text)
9034 search_stop_searching (impl, FALSE);
9035 search_clear_model (impl, TRUE);
9036 search_setup_model (impl);
9037 set_busy_cursor (impl, TRUE);
9039 if (impl->search_engine == NULL)
9040 impl->search_engine = _gtk_search_engine_new ();
9042 if (!impl->search_engine)
9044 set_busy_cursor (impl, FALSE);
9045 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9049 if (!impl->search_query)
9051 impl->search_query = _gtk_query_new ();
9052 _gtk_query_set_text (impl->search_query, query_text);
9055 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9057 g_signal_connect (impl->search_engine, "hits-added",
9058 G_CALLBACK (search_engine_hits_added_cb), impl);
9059 g_signal_connect (impl->search_engine, "finished",
9060 G_CALLBACK (search_engine_finished_cb), impl);
9061 g_signal_connect (impl->search_engine, "error",
9062 G_CALLBACK (search_engine_error_cb), impl);
9064 _gtk_search_engine_start (impl->search_engine);
9067 /* Callback used when the user presses Enter while typing on the search
9068 * entry; starts the query
9071 search_entry_activate_cb (GtkEntry *entry,
9074 GtkFileChooserDefault *impl;
9077 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9079 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9080 if (strlen (text) == 0)
9083 /* reset any existing query object */
9084 if (impl->search_query)
9086 g_object_unref (impl->search_query);
9087 impl->search_query = NULL;
9090 search_start_query (impl, text);
9094 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9096 GDK_THREADS_ENTER ();
9098 g_source_destroy (impl->focus_entry_idle);
9099 impl->focus_entry_idle = NULL;
9101 if (impl->search_entry)
9102 gtk_widget_grab_focus (impl->search_entry);
9104 GDK_THREADS_LEAVE ();
9110 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9112 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9113 * pane, we get a selection-changed signal and we set up the search widgets.
9114 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9115 * the change to the selection. So, we need to re-focus the search entry
9116 * after the treeview has finished doing its work; we'll do that in an idle
9120 if (!impl->focus_entry_idle)
9121 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9124 /* Hides the path bar and creates the search entry */
9126 search_setup_widgets (GtkFileChooserDefault *impl)
9128 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9130 path_bar_update (impl);
9132 impl->search_entry = gtk_entry_new ();
9133 g_signal_connect (impl->search_entry, "activate",
9134 G_CALLBACK (search_entry_activate_cb),
9136 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9138 /* if there already is a query, restart it */
9139 if (impl->search_query)
9141 gchar *query = _gtk_query_get_text (impl->search_query);
9145 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9146 search_start_query (impl, query);
9152 g_object_unref (impl->search_query);
9153 impl->search_query = NULL;
9157 /* Box for search widgets */
9158 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9159 gtk_widget_show_all (impl->search_hbox);
9160 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9162 /* Hide the location widgets temporarily */
9164 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9165 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9167 gtk_widget_hide (impl->location_button);
9168 gtk_widget_hide (impl->location_entry_box);
9171 focus_search_entry_in_idle (impl);
9173 /* FMQ: hide the filter combo? */
9176 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9178 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9182 case OPERATION_MODE_BROWSE:
9183 stop_loading_and_clear_list_model (impl, TRUE);
9186 case OPERATION_MODE_SEARCH:
9187 search_stop_searching (impl, FALSE);
9188 search_clear_model (impl, TRUE);
9190 gtk_widget_destroy (impl->search_hbox);
9191 impl->search_hbox = NULL;
9192 impl->search_entry = NULL;
9195 case OPERATION_MODE_RECENT:
9196 recent_stop_loading (impl);
9197 recent_clear_model (impl, TRUE);
9202 /* Sometimes we need to frob the selection in the shortcuts list manually */
9204 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
9206 GtkTreeSelection *selection;
9209 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
9211 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
9213 path = gtk_tree_path_new_from_indices (pos, -1);
9214 gtk_tree_selection_select_path (selection, path);
9215 gtk_tree_path_free (path);
9217 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
9220 /* Main entry point to the searching functions; this gets called when the user
9221 * activates the Search shortcut.
9224 search_activate (GtkFileChooserDefault *impl)
9226 OperationMode previous_mode;
9228 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9230 focus_search_entry_in_idle (impl);
9234 previous_mode = impl->operation_mode;
9235 impl->operation_mode = OPERATION_MODE_SEARCH;
9237 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
9239 stop_operation (impl, previous_mode);
9241 g_assert (impl->search_hbox == NULL);
9242 g_assert (impl->search_entry == NULL);
9243 g_assert (impl->search_model == NULL);
9245 search_setup_widgets (impl);
9246 file_list_set_sort_column_ids (impl);
9250 * Recent files support
9253 /* Frees the data in the recent_model */
9255 recent_clear_model (GtkFileChooserDefault *impl,
9256 gboolean remove_from_treeview)
9258 if (!impl->recent_model)
9261 if (remove_from_treeview)
9262 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9264 g_object_unref (impl->recent_model);
9265 impl->recent_model = NULL;
9268 /* Stops any ongoing loading of the recent files list; does
9269 * not touch the recent_model
9272 recent_stop_loading (GtkFileChooserDefault *impl)
9274 if (impl->load_recent_id)
9276 g_source_remove (impl->load_recent_id);
9277 impl->load_recent_id = 0;
9281 /* Stops any pending load, clears the file list, and switches
9282 * back to OPERATION_MODE_BROWSE
9285 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9287 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9289 recent_stop_loading (impl);
9290 recent_clear_model (impl, TRUE);
9292 impl->operation_mode = OPERATION_MODE_BROWSE;
9293 path_bar_update (impl);
9295 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9296 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9298 gtk_widget_show (impl->location_button);
9300 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9301 gtk_widget_show (impl->location_entry_box);
9304 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9306 file_list_set_sort_column_ids (impl);
9310 recent_setup_model (GtkFileChooserDefault *impl)
9312 g_assert (impl->recent_model == NULL);
9314 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9316 MODEL_COLUMN_TYPES);
9318 _gtk_file_system_model_set_filter (impl->recent_model,
9319 impl->current_filter);
9320 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9324 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9328 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9332 set_sort_column (impl);
9337 GtkFileChooserDefault *impl;
9339 guint needs_sorting : 1;
9343 recent_idle_cleanup (gpointer data)
9345 RecentLoadData *load_data = data;
9346 GtkFileChooserDefault *impl = load_data->impl;
9348 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9349 GTK_TREE_MODEL (impl->recent_model));
9351 set_busy_cursor (impl, FALSE);
9353 impl->load_recent_id = 0;
9359 recent_sort_mru (gconstpointer a,
9362 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9363 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9365 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9369 get_recent_files_limit (GtkWidget *widget)
9371 GtkSettings *settings;
9374 if (gtk_widget_has_screen (widget))
9375 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9377 settings = gtk_settings_get_default ();
9379 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9384 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9386 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9392 limit = get_recent_files_limit (GTK_WIDGET (impl));
9396 for (l = items; l; l = l->next)
9398 GtkRecentInfo *info = l->data;
9401 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9402 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9405 g_object_unref (file);
9408 if (limit != -1 && n >= limit)
9414 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9419 folders = _gtk_file_chooser_extract_recent_folders (items);
9421 for (l = folders; l; l = l->next)
9423 GFile *folder = l->data;
9425 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9430 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9431 g_list_free (folders);
9435 recent_idle_load (gpointer data)
9437 RecentLoadData *load_data = data;
9438 GtkFileChooserDefault *impl = load_data->impl;
9440 if (!impl->recent_manager)
9443 /* first iteration: load all the items */
9444 if (!load_data->items)
9446 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9447 if (!load_data->items)
9450 load_data->needs_sorting = TRUE;
9455 /* second iteration: MRU sorting and clamping, and populating the model */
9456 if (load_data->needs_sorting)
9458 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9460 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9461 populate_model_with_recent_items (impl, load_data->items);
9463 populate_model_with_folders (impl, load_data->items);
9465 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9466 g_list_free (load_data->items);
9467 load_data->items = NULL;
9474 recent_start_loading (GtkFileChooserDefault *impl)
9476 RecentLoadData *load_data;
9478 recent_stop_loading (impl);
9479 recent_clear_model (impl, TRUE);
9480 recent_setup_model (impl);
9481 set_busy_cursor (impl, TRUE);
9483 g_assert (impl->load_recent_id == 0);
9485 load_data = g_new (RecentLoadData, 1);
9486 load_data->impl = impl;
9487 load_data->items = NULL;
9488 load_data->needs_sorting = TRUE;
9490 /* begin lazy loading the recent files into the model */
9491 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9494 recent_idle_cleanup);
9498 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9508 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9509 *list = g_slist_prepend (*list, file);
9512 /* Constructs a list of the selected paths in recent files mode */
9514 recent_get_selected_files (GtkFileChooserDefault *impl)
9517 GtkTreeSelection *selection;
9521 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9522 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9523 result = g_slist_reverse (result);
9528 /* Called from ::should_respond(). We return whether there are selected
9529 * files in the recent files list.
9532 recent_should_respond (GtkFileChooserDefault *impl)
9534 GtkTreeSelection *selection;
9536 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9538 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9539 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9542 /* Hide the location widgets temporarily */
9544 recent_hide_entry (GtkFileChooserDefault *impl)
9546 path_bar_update (impl);
9548 /* Hide the location widgets temporarily */
9549 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9550 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9552 gtk_widget_hide (impl->location_button);
9553 gtk_widget_hide (impl->location_entry_box);
9557 /* Main entry point to the recent files functions; this gets called when
9558 * the user activates the Recently Used shortcut.
9561 recent_activate (GtkFileChooserDefault *impl)
9563 OperationMode previous_mode;
9565 if (impl->operation_mode == OPERATION_MODE_RECENT)
9568 previous_mode = impl->operation_mode;
9569 impl->operation_mode = OPERATION_MODE_RECENT;
9571 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
9573 stop_operation (impl, previous_mode);
9575 recent_hide_entry (impl);
9577 file_list_set_sort_column_ids (impl);
9578 recent_start_loading (impl);
9582 set_current_filter (GtkFileChooserDefault *impl,
9583 GtkFileFilter *filter)
9585 if (impl->current_filter != filter)
9589 /* NULL filters are allowed to reset to non-filtered status
9591 filter_index = g_slist_index (impl->filters, filter);
9592 if (impl->filters && filter && filter_index < 0)
9595 if (impl->current_filter)
9596 g_object_unref (impl->current_filter);
9597 impl->current_filter = filter;
9598 if (impl->current_filter)
9600 g_object_ref_sink (impl->current_filter);
9604 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9607 if (impl->browse_files_model)
9608 install_list_model_filter (impl);
9610 if (impl->search_model)
9611 _gtk_file_system_model_set_filter (impl->search_model, filter);
9613 if (impl->recent_model)
9614 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9616 g_object_notify (G_OBJECT (impl), "filter");
9621 filter_combo_changed (GtkComboBox *combo_box,
9622 GtkFileChooserDefault *impl)
9624 gint new_index = gtk_combo_box_get_active (combo_box);
9625 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9627 set_current_filter (impl, new_filter);
9631 check_preview_change (GtkFileChooserDefault *impl)
9633 GtkTreePath *cursor_path;
9635 char *new_display_name;
9636 GtkTreeModel *model;
9638 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9639 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9644 gtk_tree_model_get_iter (model, &iter, cursor_path);
9645 gtk_tree_model_get (model, &iter,
9646 MODEL_COL_FILE, &new_file,
9647 MODEL_COL_NAME, &new_display_name,
9650 gtk_tree_path_free (cursor_path);
9655 new_display_name = NULL;
9658 if (new_file != impl->preview_file &&
9659 !(new_file && impl->preview_file &&
9660 g_file_equal (new_file, impl->preview_file)))
9662 if (impl->preview_file)
9664 g_object_unref (impl->preview_file);
9665 g_free (impl->preview_display_name);
9670 impl->preview_file = new_file;
9671 impl->preview_display_name = new_display_name;
9675 impl->preview_file = NULL;
9676 impl->preview_display_name = NULL;
9677 g_free (new_display_name);
9680 if (impl->use_preview_label && impl->preview_label)
9681 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9683 g_signal_emit_by_name (impl, "update-preview");
9688 g_object_unref (new_file);
9690 g_free (new_display_name);
9695 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9696 GtkFileSystemVolume *volume,
9697 const GError *error,
9701 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9702 GtkFileChooserDefault *impl = data;
9704 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9707 impl->shortcuts_activate_iter_cancellable = NULL;
9709 set_busy_cursor (impl, FALSE);
9716 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9720 name = _gtk_file_system_volume_get_display_name (volume);
9721 msg = g_strdup_printf (_("Could not mount %s"), name);
9723 error_message (impl, msg, error->message);
9732 file = _gtk_file_system_volume_get_root (volume);
9735 change_folder_and_display_error (impl, file, FALSE);
9736 g_object_unref (file);
9740 g_object_unref (impl);
9741 g_object_unref (cancellable);
9745 /* Activates a volume by mounting it if necessary and then switching to its
9749 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9750 GtkFileSystemVolume *volume)
9754 switch (impl->operation_mode)
9756 case OPERATION_MODE_BROWSE:
9758 case OPERATION_MODE_SEARCH:
9759 search_switch_to_browse_mode (impl);
9761 case OPERATION_MODE_RECENT:
9762 recent_switch_to_browse_mode (impl);
9766 /* We ref the file chooser since volume_mount() may run a main loop, and the
9767 * user could close the file chooser window in the meantime.
9769 g_object_ref (impl);
9771 if (!_gtk_file_system_volume_is_mounted (volume))
9773 GMountOperation *mount_op;
9775 set_busy_cursor (impl, TRUE);
9777 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9778 impl->shortcuts_activate_iter_cancellable =
9779 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9780 shortcuts_activate_volume_mount_cb,
9781 g_object_ref (impl));
9782 g_object_unref (mount_op);
9786 file = _gtk_file_system_volume_get_root (volume);
9789 change_folder_and_display_error (impl, file, FALSE);
9790 g_object_unref (file);
9794 g_object_unref (impl);
9797 /* Opens the folder or volume at the specified iter in the shortcuts model */
9798 struct ShortcutsActivateData
9800 GtkFileChooserDefault *impl;
9805 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9807 const GError *error,
9810 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9811 struct ShortcutsActivateData *data = user_data;
9813 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9816 data->impl->shortcuts_activate_iter_cancellable = NULL;
9821 if (!error && _gtk_file_info_consider_as_directory (info))
9822 change_folder_and_display_error (data->impl, data->file, FALSE);
9824 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9829 g_object_unref (data->impl);
9830 g_object_unref (data->file);
9833 g_object_unref (cancellable);
9837 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9838 GtkFileSystemVolume *volume,
9839 const GError *error,
9842 struct ShortcutsActivateData *data = user_data;
9846 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9848 g_object_unref (data->impl);
9849 g_object_unref (data->file);
9855 data->impl->shortcuts_activate_iter_cancellable =
9856 _gtk_file_system_get_info (data->impl->file_system, data->file,
9858 shortcuts_activate_get_info_cb, data);
9861 _gtk_file_system_volume_unref (volume);
9865 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9869 ShortcutType shortcut_type;
9871 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9872 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9873 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9874 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9876 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9877 SHORTCUTS_COL_DATA, &col_data,
9878 SHORTCUTS_COL_TYPE, &shortcut_type,
9881 if (impl->shortcuts_activate_iter_cancellable)
9883 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9884 impl->shortcuts_activate_iter_cancellable = NULL;
9887 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9889 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9891 GtkFileSystemVolume *volume;
9895 shortcuts_activate_volume (impl, volume);
9897 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9899 struct ShortcutsActivateData *data;
9900 GtkFileSystemVolume *volume;
9902 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9904 data = g_new0 (struct ShortcutsActivateData, 1);
9905 data->impl = g_object_ref (impl);
9906 data->file = g_object_ref (col_data);
9908 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9910 GMountOperation *mount_operation;
9911 GtkWidget *toplevel;
9913 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9915 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9917 impl->shortcuts_activate_iter_cancellable =
9918 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9920 shortcuts_activate_mount_enclosing_volume,
9925 impl->shortcuts_activate_iter_cancellable =
9926 _gtk_file_system_get_info (impl->file_system, data->file,
9928 shortcuts_activate_get_info_cb, data);
9931 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9933 search_activate (impl);
9935 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9937 recent_activate (impl);
9941 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9943 shortcuts_key_press_event_cb (GtkWidget *widget,
9945 GtkFileChooserDefault *impl)
9949 modifiers = gtk_accelerator_get_default_mod_mask ();
9951 if (key_is_left_or_right (event))
9953 gtk_widget_grab_focus (impl->browse_files_tree_view);
9957 if ((event->keyval == GDK_KEY_BackSpace
9958 || event->keyval == GDK_KEY_Delete
9959 || event->keyval == GDK_KEY_KP_Delete)
9960 && (event->state & modifiers) == 0)
9962 remove_selected_bookmarks (impl);
9966 if ((event->keyval == GDK_KEY_F2)
9967 && (event->state & modifiers) == 0)
9969 rename_selected_bookmark (impl);
9977 shortcuts_select_func (GtkTreeSelection *selection,
9978 GtkTreeModel *model,
9980 gboolean path_currently_selected,
9983 GtkFileChooserDefault *impl = data;
9984 GtkTreeIter filter_iter;
9985 ShortcutType shortcut_type;
9987 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9988 g_assert_not_reached ();
9990 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9992 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9996 list_select_func (GtkTreeSelection *selection,
9997 GtkTreeModel *model,
9999 gboolean path_currently_selected,
10002 GtkFileChooserDefault *impl = data;
10004 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10005 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10008 gboolean is_folder;
10010 if (!gtk_tree_model_get_iter (model, &iter, path))
10012 gtk_tree_model_get (model, &iter,
10013 MODEL_COL_IS_FOLDER, &is_folder,
10023 list_selection_changed (GtkTreeSelection *selection,
10024 GtkFileChooserDefault *impl)
10026 /* See if we are in the new folder editable row for Save mode */
10027 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10028 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10031 gboolean had_selection;
10033 info = get_selected_file_info_from_file_list (impl, &had_selection);
10034 if (!had_selection)
10035 goto out; /* normal processing */
10038 return; /* We are on the editable row for New Folder */
10043 if (impl->location_entry)
10044 update_chooser_entry (impl);
10046 path_bar_update (impl);
10048 check_preview_change (impl);
10049 bookmarks_check_add_sensitivity (impl);
10051 g_signal_emit_by_name (impl, "selection-changed", 0);
10054 /* Callback used when a row in the file list is activated */
10056 list_row_activated (GtkTreeView *tree_view,
10058 GtkTreeViewColumn *column,
10059 GtkFileChooserDefault *impl)
10063 GtkTreeModel *model;
10064 gboolean is_folder;
10066 model = gtk_tree_view_get_model (tree_view);
10068 if (!gtk_tree_model_get_iter (model, &iter, path))
10071 gtk_tree_model_get (model, &iter,
10072 MODEL_COL_FILE, &file,
10073 MODEL_COL_IS_FOLDER, &is_folder,
10076 if (is_folder && file)
10078 change_folder_and_display_error (impl, file, FALSE);
10079 g_object_unref (file);
10083 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10084 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10085 g_signal_emit_by_name (impl, "file-activated");
10090 g_object_unref (file);
10094 path_bar_clicked (GtkPathBar *path_bar,
10097 gboolean child_is_hidden,
10098 GtkFileChooserDefault *impl)
10101 pending_select_files_add (impl, child_file);
10103 if (!change_folder_and_display_error (impl, file, FALSE))
10106 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10107 * show hidden files so that ".baz" appears in the file list, as it will still
10108 * be shown in the path bar: "/foo/[bar]/.baz"
10110 if (child_is_hidden)
10111 g_object_set (impl, "show-hidden", TRUE, NULL);
10115 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10117 GtkTreeViewColumn *column;
10118 GtkCellRenderer *renderer;
10119 GList *walk, *list;
10120 gboolean always_sensitive;
10122 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10123 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10125 /* Keep the following column numbers in sync with create_file_list() */
10128 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10129 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10130 for (walk = list; walk; walk = walk->next)
10132 renderer = walk->data;
10133 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10135 gtk_tree_view_column_set_attributes (column, renderer,
10136 "pixbuf", MODEL_COL_PIXBUF,
10141 gtk_tree_view_column_set_attributes (column, renderer,
10142 "text", MODEL_COL_NAME,
10143 "ellipsize", MODEL_COL_ELLIPSIZE,
10146 if (always_sensitive)
10147 g_object_set (renderer, "sensitive", TRUE, NULL);
10149 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10151 g_list_free (list);
10154 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10155 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10156 renderer = list->data;
10157 gtk_tree_view_column_set_attributes (column, renderer,
10158 "text", MODEL_COL_SIZE_TEXT,
10160 if (always_sensitive)
10161 g_object_set (renderer, "sensitive", TRUE, NULL);
10163 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10164 g_list_free (list);
10167 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10168 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10169 renderer = list->data;
10170 gtk_tree_view_column_set_attributes (column, renderer,
10171 "text", MODEL_COL_MTIME_TEXT,
10173 if (always_sensitive)
10174 g_object_set (renderer, "sensitive", TRUE, NULL);
10176 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10177 g_list_free (list);
10181 _gtk_file_chooser_default_new (void)
10183 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10187 location_set_user_text (GtkFileChooserDefault *impl,
10190 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10191 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10195 location_popup_handler (GtkFileChooserDefault *impl,
10198 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10200 GtkWidget *widget_to_focus;
10202 /* This will give us the location widgets back */
10203 switch (impl->operation_mode)
10205 case OPERATION_MODE_SEARCH:
10206 search_switch_to_browse_mode (impl);
10208 case OPERATION_MODE_RECENT:
10209 recent_switch_to_browse_mode (impl);
10211 case OPERATION_MODE_BROWSE:
10212 g_assert_not_reached ();
10216 if (impl->current_folder)
10217 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10219 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10220 widget_to_focus = impl->browse_files_tree_view;
10222 widget_to_focus = impl->location_entry;
10224 gtk_widget_grab_focus (widget_to_focus);
10228 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10229 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10231 LocationMode new_mode;
10235 /* since the user typed something, we unconditionally want to turn on the entry */
10236 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10238 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10239 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10240 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10241 new_mode = LOCATION_MODE_PATH_BAR;
10244 g_assert_not_reached ();
10248 location_mode_set (impl, new_mode, TRUE);
10249 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10252 location_set_user_text (impl, path);
10255 location_entry_set_initial_text (impl);
10256 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10260 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10261 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10263 gtk_widget_grab_focus (impl->location_entry);
10265 location_set_user_text (impl, path);
10268 g_assert_not_reached ();
10271 /* Handler for the "up-folder" keybinding signal */
10273 up_folder_handler (GtkFileChooserDefault *impl)
10275 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10278 /* Handler for the "down-folder" keybinding signal */
10280 down_folder_handler (GtkFileChooserDefault *impl)
10282 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10285 /* Switches to the shortcut in the specified index */
10287 switch_to_shortcut (GtkFileChooserDefault *impl,
10292 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10293 g_assert_not_reached ();
10295 shortcuts_activate_iter (impl, &iter);
10298 /* Handler for the "home-folder" keybinding signal */
10300 home_folder_handler (GtkFileChooserDefault *impl)
10302 if (impl->has_home)
10303 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10306 /* Handler for the "desktop-folder" keybinding signal */
10308 desktop_folder_handler (GtkFileChooserDefault *impl)
10310 if (impl->has_desktop)
10311 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10314 /* Handler for the "search-shortcut" keybinding signal */
10316 search_shortcut_handler (GtkFileChooserDefault *impl)
10318 if (impl->has_search)
10320 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10322 /* we want the entry widget to grab the focus the first
10323 * time, not the browse_files_tree_view widget.
10325 if (impl->search_entry)
10326 gtk_widget_grab_focus (impl->search_entry);
10330 /* Handler for the "recent-shortcut" keybinding signal */
10332 recent_shortcut_handler (GtkFileChooserDefault *impl)
10334 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10338 quick_bookmark_handler (GtkFileChooserDefault *impl,
10339 gint bookmark_index)
10344 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10347 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10349 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10350 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10353 gtk_tree_path_free (path);
10355 switch_to_shortcut (impl, bookmark_pos);
10359 show_hidden_handler (GtkFileChooserDefault *impl)
10361 g_object_set (impl,
10362 "show-hidden", !impl->show_hidden,
10367 /* Drag and drop interfaces */
10370 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10375 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10377 model->impl = NULL;
10380 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10382 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10385 ShortcutsPaneModelFilter *model;
10389 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10391 pos = *gtk_tree_path_get_indices (path);
10392 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10394 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10397 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10401 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10403 GtkSelectionData *selection_data)
10410 /* Fill the GtkTreeDragSourceIface vtable */
10412 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10414 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10415 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10419 /* Fill the GtkTreeDragDestIface vtable */
10421 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10423 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10424 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10428 static GtkTreeModel *
10429 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10430 GtkTreeModel *child_model,
10433 ShortcutsPaneModelFilter *model;
10435 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10436 "child-model", child_model,
10437 "virtual-root", root,
10440 model->impl = impl;
10442 return GTK_TREE_MODEL (model);