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 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5232 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5236 case OPERATION_MODE_BROWSE:
5237 stop_loading_and_clear_list_model (impl, TRUE);
5240 case OPERATION_MODE_SEARCH:
5241 search_stop_searching (impl, FALSE);
5242 search_clear_model (impl, TRUE);
5244 gtk_widget_destroy (impl->search_hbox);
5245 impl->search_hbox = NULL;
5246 impl->search_entry = NULL;
5249 case OPERATION_MODE_RECENT:
5250 recent_stop_loading (impl);
5251 recent_clear_model (impl, TRUE);
5255 g_assert_not_reached ();
5259 /* This function is basically a do_all function.
5261 * It sets the visibility on all the widgets based on the current state, and
5262 * moves the custom_widget if needed.
5265 update_appearance (GtkFileChooserDefault *impl)
5267 save_path_bar (impl);
5269 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5270 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5274 gtk_widget_hide (impl->location_button);
5275 save_widgets_create (impl);
5277 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5278 text = _("Save in _folder:");
5280 text = _("Create in _folder:");
5282 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5284 if (impl->select_multiple)
5286 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5287 "Re-setting to single selection mode.");
5288 set_select_multiple (impl, FALSE, TRUE);
5291 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5292 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5294 gtk_widget_show (impl->location_button);
5295 save_widgets_destroy (impl);
5296 gtk_widget_show (impl->browse_widgets_box);
5297 location_mode_set (impl, impl->location_mode, TRUE);
5300 if (impl->location_entry)
5301 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5303 restore_path_bar (impl);
5304 path_bar_update (impl);
5306 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5307 * of files may change depending whether we are in a file or folder-only mode.
5309 gtk_widget_queue_draw (impl->browse_files_tree_view);
5311 emit_default_size_changed (impl);
5315 gtk_file_chooser_default_set_property (GObject *object,
5317 const GValue *value,
5321 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5325 case GTK_FILE_CHOOSER_PROP_ACTION:
5327 GtkFileChooserAction action = g_value_get_enum (value);
5329 if (action != impl->action)
5331 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5333 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5334 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5335 && impl->select_multiple)
5337 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5338 "this is not allowed in multiple selection mode. Resetting the file chooser "
5339 "to single selection mode.");
5340 set_select_multiple (impl, FALSE, TRUE);
5342 impl->action = action;
5343 update_cell_renderer_attributes (impl);
5344 update_appearance (impl);
5345 settings_load (impl);
5350 case GTK_FILE_CHOOSER_PROP_FILTER:
5351 set_current_filter (impl, g_value_get_object (value));
5354 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5355 set_local_only (impl, g_value_get_boolean (value));
5358 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5359 set_preview_widget (impl, g_value_get_object (value));
5362 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5363 impl->preview_widget_active = g_value_get_boolean (value);
5364 update_preview_widget_visibility (impl);
5367 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5368 impl->use_preview_label = g_value_get_boolean (value);
5369 update_preview_widget_visibility (impl);
5372 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5373 set_extra_widget (impl, g_value_get_object (value));
5376 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5378 gboolean select_multiple = g_value_get_boolean (value);
5379 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5380 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5383 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5384 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5385 "leaving the file chooser in single selection mode.");
5389 set_select_multiple (impl, select_multiple, FALSE);
5393 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5395 gboolean show_hidden = g_value_get_boolean (value);
5396 if (show_hidden != impl->show_hidden)
5398 impl->show_hidden = show_hidden;
5400 if (impl->browse_files_model)
5401 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5406 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5408 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5409 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5413 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5415 gboolean create_folders = g_value_get_boolean (value);
5416 impl->create_folders = create_folders;
5417 update_appearance (impl);
5422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5428 gtk_file_chooser_default_get_property (GObject *object,
5433 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5437 case GTK_FILE_CHOOSER_PROP_ACTION:
5438 g_value_set_enum (value, impl->action);
5441 case GTK_FILE_CHOOSER_PROP_FILTER:
5442 g_value_set_object (value, impl->current_filter);
5445 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5446 g_value_set_boolean (value, impl->local_only);
5449 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5450 g_value_set_object (value, impl->preview_widget);
5453 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5454 g_value_set_boolean (value, impl->preview_widget_active);
5457 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5458 g_value_set_boolean (value, impl->use_preview_label);
5461 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5462 g_value_set_object (value, impl->extra_widget);
5465 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5466 g_value_set_boolean (value, impl->select_multiple);
5469 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5470 g_value_set_boolean (value, impl->show_hidden);
5473 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5474 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5477 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5478 g_value_set_boolean (value, impl->create_folders);
5482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5487 /* This cancels everything that may be going on in the background. */
5489 cancel_all_operations (GtkFileChooserDefault *impl)
5493 pending_select_files_free (impl);
5495 /* cancel all pending operations */
5496 if (impl->pending_cancellables)
5498 for (l = impl->pending_cancellables; l; l = l->next)
5500 GCancellable *cancellable = G_CANCELLABLE (l->data);
5501 g_cancellable_cancel (cancellable);
5503 g_slist_free (impl->pending_cancellables);
5504 impl->pending_cancellables = NULL;
5507 if (impl->reload_icon_cancellables)
5509 for (l = impl->reload_icon_cancellables; l; l = l->next)
5511 GCancellable *cancellable = G_CANCELLABLE (l->data);
5512 g_cancellable_cancel (cancellable);
5514 g_slist_free (impl->reload_icon_cancellables);
5515 impl->reload_icon_cancellables = NULL;
5518 if (impl->loading_shortcuts)
5520 for (l = impl->loading_shortcuts; l; l = l->next)
5522 GCancellable *cancellable = G_CANCELLABLE (l->data);
5523 g_cancellable_cancel (cancellable);
5525 g_slist_free (impl->loading_shortcuts);
5526 impl->loading_shortcuts = NULL;
5529 if (impl->file_list_drag_data_received_cancellable)
5531 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5532 impl->file_list_drag_data_received_cancellable = NULL;
5535 if (impl->update_current_folder_cancellable)
5537 g_cancellable_cancel (impl->update_current_folder_cancellable);
5538 impl->update_current_folder_cancellable = NULL;
5541 if (impl->should_respond_get_info_cancellable)
5543 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5544 impl->should_respond_get_info_cancellable = NULL;
5547 if (impl->update_from_entry_cancellable)
5549 g_cancellable_cancel (impl->update_from_entry_cancellable);
5550 impl->update_from_entry_cancellable = NULL;
5553 if (impl->shortcuts_activate_iter_cancellable)
5555 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5556 impl->shortcuts_activate_iter_cancellable = NULL;
5559 search_stop_searching (impl, TRUE);
5560 recent_stop_loading (impl);
5563 /* Removes the settings signal handler. It's safe to call multiple times */
5565 remove_settings_signal (GtkFileChooserDefault *impl,
5568 if (impl->settings_signal_id)
5570 GtkSettings *settings;
5572 settings = gtk_settings_get_for_screen (screen);
5573 g_signal_handler_disconnect (settings,
5574 impl->settings_signal_id);
5575 impl->settings_signal_id = 0;
5580 gtk_file_chooser_default_dispose (GObject *object)
5582 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5584 cancel_all_operations (impl);
5586 if (impl->extra_widget)
5588 g_object_unref (impl->extra_widget);
5589 impl->extra_widget = NULL;
5592 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5594 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5597 /* We override show-all since we have internal widgets that
5598 * shouldn't be shown when you call show_all(), like the filter
5602 gtk_file_chooser_default_show_all (GtkWidget *widget)
5604 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5606 gtk_widget_show (widget);
5608 if (impl->extra_widget)
5609 gtk_widget_show_all (impl->extra_widget);
5612 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5613 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5616 toplevel_set_focus_cb (GtkWindow *window,
5618 GtkFileChooserDefault *impl)
5620 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5623 /* We monitor the focus widget on our toplevel to be able to know which widget
5624 * was last focused at the time our "should_respond" method gets called.
5627 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5628 GtkWidget *previous_toplevel)
5630 GtkFileChooserDefault *impl;
5631 GtkWidget *toplevel;
5633 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5634 toplevel = gtk_widget_get_toplevel (widget);
5636 if (previous_toplevel &&
5637 impl->toplevel_set_focus_id != 0)
5639 g_signal_handler_disconnect (previous_toplevel,
5640 impl->toplevel_set_focus_id);
5641 impl->toplevel_set_focus_id = 0;
5642 impl->toplevel_last_focus_widget = NULL;
5645 if (gtk_widget_is_toplevel (toplevel))
5647 g_assert (impl->toplevel_set_focus_id == 0);
5648 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5649 G_CALLBACK (toplevel_set_focus_cb), impl);
5650 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5654 /* Changes the icons wherever it is needed */
5656 change_icon_theme (GtkFileChooserDefault *impl)
5658 GtkSettings *settings;
5660 GtkCellRenderer *renderer;
5663 profile_start ("start", NULL);
5665 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5667 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5668 impl->icon_size = MAX (width, height);
5670 impl->icon_size = FALLBACK_ICON_SIZE;
5672 shortcuts_reload_icons (impl);
5673 /* the first cell in the first column is the icon column, and we have a fixed size there */
5674 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5675 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5676 renderer = GTK_CELL_RENDERER (cells->data);
5677 set_icon_cell_renderer_fixed_size (impl, renderer);
5678 g_list_free (cells);
5679 if (impl->browse_files_model)
5680 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5681 gtk_widget_queue_resize (impl->browse_files_tree_view);
5683 profile_end ("end", NULL);
5686 /* Callback used when a GtkSettings value changes */
5688 settings_notify_cb (GObject *object,
5690 GtkFileChooserDefault *impl)
5694 profile_start ("start", NULL);
5696 name = g_param_spec_get_name (pspec);
5698 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5699 strcmp (name, "gtk-icon-sizes") == 0)
5700 change_icon_theme (impl);
5702 profile_end ("end", NULL);
5705 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5709 check_icon_theme (GtkFileChooserDefault *impl)
5711 GtkSettings *settings;
5713 profile_start ("start", NULL);
5715 if (impl->settings_signal_id)
5717 profile_end ("end", NULL);
5721 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5723 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5724 impl->settings_signal_id = g_signal_connect (settings, "notify",
5725 G_CALLBACK (settings_notify_cb), impl);
5727 change_icon_theme (impl);
5730 profile_end ("end", NULL);
5734 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5736 GtkFileChooserDefault *impl;
5738 profile_start ("start", NULL);
5740 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5742 profile_msg (" parent class style_udpated start", NULL);
5743 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5744 profile_msg (" parent class style_updated end", NULL);
5746 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5747 change_icon_theme (impl);
5749 emit_default_size_changed (impl);
5751 profile_end ("end", NULL);
5755 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5756 GdkScreen *previous_screen)
5758 GtkFileChooserDefault *impl;
5760 profile_start ("start", NULL);
5762 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5764 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5765 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5767 remove_settings_signal (impl, previous_screen);
5768 check_icon_theme (impl);
5770 emit_default_size_changed (impl);
5772 profile_end ("end", NULL);
5776 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5777 GtkAllocation *allocation)
5779 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5783 set_sort_column (GtkFileChooserDefault *impl)
5785 GtkTreeSortable *sortable;
5787 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5789 /* can happen when we're still populating the model */
5790 if (sortable == NULL)
5793 gtk_tree_sortable_set_sort_column_id (sortable,
5799 settings_ensure (GtkFileChooserDefault *impl)
5801 if (impl->settings != NULL)
5804 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5805 "/org/gtk/settings/file-chooser/");
5806 g_settings_delay (impl->settings);
5810 settings_load (GtkFileChooserDefault *impl)
5812 LocationMode location_mode;
5813 gboolean show_hidden;
5814 gboolean show_size_column;
5816 GtkSortType sort_order;
5818 settings_ensure (impl);
5820 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5821 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5822 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5823 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5824 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5826 location_mode_set (impl, location_mode, TRUE);
5828 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5830 impl->show_size_column = show_size_column;
5831 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5833 impl->sort_column = sort_column;
5834 impl->sort_order = sort_order;
5835 /* We don't call set_sort_column() here as the models may not have been
5836 * created yet. The individual functions that create and set the models will
5837 * call set_sort_column() themselves.
5842 save_dialog_geometry (GtkFileChooserDefault *impl)
5844 GtkWindow *toplevel;
5845 int x, y, width, height;
5847 toplevel = get_toplevel (GTK_WIDGET (impl));
5849 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5852 gtk_window_get_position (toplevel, &x, &y);
5853 gtk_window_get_size (toplevel, &width, &height);
5855 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5856 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5860 settings_save (GtkFileChooserDefault *impl)
5862 char *current_folder_uri;
5864 settings_ensure (impl);
5866 /* Current folder */
5868 if (impl->current_folder)
5869 current_folder_uri = g_file_get_uri (impl->current_folder);
5871 current_folder_uri = "";
5873 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5875 if (impl->current_folder)
5876 g_free (current_folder_uri);
5878 /* All the other state */
5880 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5881 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5882 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5883 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5884 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5885 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5887 save_dialog_geometry (impl);
5889 /* Now apply the settings */
5890 g_settings_apply (impl->settings);
5892 g_object_unref (impl->settings);
5893 impl->settings = NULL;
5896 /* GtkWidget::realize method */
5898 gtk_file_chooser_default_realize (GtkWidget *widget)
5900 GtkFileChooserDefault *impl;
5902 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5904 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5906 emit_default_size_changed (impl);
5910 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5912 char *last_folder_uri;
5915 settings_ensure (impl);
5917 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5919 /* If no last folder is set, we use the user's home directory, since
5920 * this is the starting point for most documents.
5922 if (last_folder_uri[0] == '\0')
5923 file = g_file_new_for_path (g_get_home_dir ());
5925 file = g_file_new_for_uri (last_folder_uri);
5927 g_free (last_folder_uri);
5932 /* GtkWidget::map method */
5934 gtk_file_chooser_default_map (GtkWidget *widget)
5936 GtkFileChooserDefault *impl;
5938 profile_start ("start", NULL);
5940 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5942 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5944 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5946 switch (impl->reload_state)
5949 recent_shortcut_handler (impl);
5952 case RELOAD_HAS_FOLDER:
5953 /* Nothing; we are already loading or loaded, so we
5954 * don't need to reload
5959 g_assert_not_reached ();
5963 volumes_bookmarks_changed_cb (impl->file_system, impl);
5965 settings_load (impl);
5967 profile_end ("end", NULL);
5970 /* GtkWidget::unmap method */
5972 gtk_file_chooser_default_unmap (GtkWidget *widget)
5974 GtkFileChooserDefault *impl;
5976 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5978 settings_save (impl);
5980 cancel_all_operations (impl);
5981 impl->reload_state = RELOAD_EMPTY;
5983 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5987 install_list_model_filter (GtkFileChooserDefault *impl)
5989 _gtk_file_system_model_set_filter (impl->browse_files_model,
5990 impl->current_filter);
5993 #define COMPARE_DIRECTORIES \
5994 GtkFileChooserDefault *impl = user_data; \
5995 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
5996 gboolean dir_a, dir_b; \
5998 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
5999 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6001 if (dir_a != dir_b) \
6002 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6004 /* Sort callback for the filename column */
6006 name_sort_func (GtkTreeModel *model,
6011 COMPARE_DIRECTORIES;
6014 const char *key_a, *key_b;
6017 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6018 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6021 result = strcmp (key_a, key_b);
6033 /* Sort callback for the size column */
6035 size_sort_func (GtkTreeModel *model,
6040 COMPARE_DIRECTORIES;
6043 gint64 size_a, size_b;
6045 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6046 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6048 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6052 /* Sort callback for the mtime column */
6054 mtime_sort_func (GtkTreeModel *model,
6059 COMPARE_DIRECTORIES;
6064 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6065 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6067 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6071 /* Callback used when the sort column changes. We cache the sort order for use
6072 * in name_sort_func().
6075 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6076 GtkFileChooserDefault *impl)
6078 gint sort_column_id;
6079 GtkSortType sort_type;
6081 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6083 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6084 impl->sort_column = sort_column_id;
6085 impl->sort_order = sort_type;
6090 set_busy_cursor (GtkFileChooserDefault *impl,
6094 GtkWindow *toplevel;
6095 GdkDisplay *display;
6098 toplevel = get_toplevel (GTK_WIDGET (impl));
6099 widget = GTK_WIDGET (toplevel);
6100 if (!toplevel || !gtk_widget_get_realized (widget))
6103 display = gtk_widget_get_display (widget);
6106 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6110 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6111 gdk_display_flush (display);
6114 g_object_unref (cursor);
6117 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6119 load_set_model (GtkFileChooserDefault *impl)
6121 profile_start ("start", NULL);
6123 g_assert (impl->browse_files_model != NULL);
6125 profile_msg (" gtk_tree_view_set_model start", NULL);
6126 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6127 GTK_TREE_MODEL (impl->browse_files_model));
6128 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6129 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6131 set_sort_column (impl);
6132 profile_msg (" gtk_tree_view_set_model end", NULL);
6133 impl->list_sort_ascending = TRUE;
6135 profile_end ("end", NULL);
6138 /* Timeout callback used when the loading timer expires */
6140 load_timeout_cb (gpointer data)
6142 GtkFileChooserDefault *impl;
6144 profile_start ("start", NULL);
6146 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6147 g_assert (impl->load_state == LOAD_PRELOAD);
6148 g_assert (impl->load_timeout_id != 0);
6149 g_assert (impl->browse_files_model != NULL);
6151 impl->load_timeout_id = 0;
6152 impl->load_state = LOAD_LOADING;
6154 load_set_model (impl);
6156 profile_end ("end", NULL);
6161 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6163 load_setup_timer (GtkFileChooserDefault *impl)
6165 g_assert (impl->load_timeout_id == 0);
6166 g_assert (impl->load_state != LOAD_PRELOAD);
6168 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6169 impl->load_state = LOAD_PRELOAD;
6172 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6174 load_remove_timer (GtkFileChooserDefault *impl)
6176 if (impl->load_timeout_id != 0)
6178 g_assert (impl->load_state == LOAD_PRELOAD);
6180 g_source_remove (impl->load_timeout_id);
6181 impl->load_timeout_id = 0;
6182 impl->load_state = LOAD_EMPTY;
6185 g_assert (impl->load_state == LOAD_EMPTY ||
6186 impl->load_state == LOAD_LOADING ||
6187 impl->load_state == LOAD_FINISHED);
6190 /* Selects the first row in the file list */
6192 browse_files_select_first_row (GtkFileChooserDefault *impl)
6195 GtkTreeIter dummy_iter;
6196 GtkTreeModel *tree_model;
6198 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6203 path = gtk_tree_path_new_from_indices (0, -1);
6205 /* If the list is empty, do nothing. */
6206 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6207 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6209 gtk_tree_path_free (path);
6212 struct center_selected_row_closure {
6213 GtkFileChooserDefault *impl;
6214 gboolean already_centered;
6217 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6218 * selected row in the tree view.
6221 center_selected_row_foreach_cb (GtkTreeModel *model,
6226 struct center_selected_row_closure *closure;
6229 if (closure->already_centered)
6232 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6233 closure->already_centered = TRUE;
6236 /* Centers the selected row in the tree view */
6238 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6240 struct center_selected_row_closure closure;
6241 GtkTreeSelection *selection;
6243 closure.impl = impl;
6244 closure.already_centered = FALSE;
6246 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6247 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6251 show_and_select_files (GtkFileChooserDefault *impl,
6254 GtkTreeSelection *selection;
6255 GtkFileSystemModel *fsmodel;
6256 gboolean enabled_hidden, removed_filters;
6257 gboolean selected_a_file;
6260 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6261 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6263 enabled_hidden = impl->show_hidden;
6264 removed_filters = (impl->current_filter == NULL);
6266 selected_a_file = FALSE;
6268 for (walk = files; walk; walk = walk->next)
6270 GFile *file = walk->data;
6273 /* Is it a hidden file? */
6275 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6278 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6280 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6282 if (!enabled_hidden &&
6283 (g_file_info_get_is_hidden (info) ||
6284 g_file_info_get_is_backup (info)))
6286 g_object_set (impl, "show-hidden", TRUE, NULL);
6287 enabled_hidden = TRUE;
6291 /* Is it a filtered file? */
6293 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6294 continue; /* re-get the iter as it may change when the model refilters */
6296 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6298 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6299 if (!removed_filters)
6301 set_current_filter (impl, NULL);
6302 removed_filters = TRUE;
6306 /* Okay, can we select the file now? */
6308 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6311 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6315 gtk_tree_selection_select_iter (selection, &iter);
6317 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6318 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6320 gtk_tree_path_free (path);
6322 selected_a_file = TRUE;
6326 browse_files_center_selected_row (impl);
6328 return selected_a_file;
6331 /* Processes the pending operation when a folder is finished loading */
6333 pending_select_files_process (GtkFileChooserDefault *impl)
6335 g_assert (impl->load_state == LOAD_FINISHED);
6336 g_assert (impl->browse_files_model != NULL);
6338 if (impl->pending_select_files)
6340 show_and_select_files (impl, impl->pending_select_files);
6341 pending_select_files_free (impl);
6342 browse_files_center_selected_row (impl);
6346 /* We only select the first row if the chooser is actually mapped ---
6347 * selecting the first row is to help the user when he is interacting with
6348 * the chooser, but sometimes a chooser works not on behalf of the user,
6349 * but rather on behalf of something else like GtkFileChooserButton. In
6350 * that case, the chooser's selection should be what the caller expects,
6351 * as the user can't see that something else got selected. See bug #165264.
6353 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6354 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6355 browse_files_select_first_row (impl);
6358 g_assert (impl->pending_select_files == NULL);
6362 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6367 info = g_file_query_info (impl->current_folder,
6368 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6369 G_FILE_QUERY_INFO_NONE,
6374 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6375 g_object_unref (info);
6378 msg = g_strdup (_("Could not read the contents of the folder"));
6380 error_message (impl, msg, error->message);
6384 /* Callback used when the file system model finishes loading */
6386 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6388 GtkFileChooserDefault *impl)
6390 profile_start ("start", NULL);
6393 show_error_on_reading_current_folder (impl, error);
6395 if (impl->load_state == LOAD_PRELOAD)
6397 load_remove_timer (impl);
6398 load_set_model (impl);
6400 else if (impl->load_state == LOAD_LOADING)
6406 /* We can't g_assert_not_reached(), as something other than us may have
6407 * initiated a folder reload. See #165556.
6409 profile_end ("end", NULL);
6413 g_assert (impl->load_timeout_id == 0);
6415 impl->load_state = LOAD_FINISHED;
6417 pending_select_files_process (impl);
6418 set_busy_cursor (impl, FALSE);
6419 #ifdef PROFILE_FILE_CHOOSER
6420 access ("MARK: *** FINISHED LOADING", F_OK);
6423 profile_end ("end", NULL);
6427 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6428 gboolean remove_from_treeview)
6430 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6432 if (impl->browse_files_model)
6434 g_object_unref (impl->browse_files_model);
6435 impl->browse_files_model = NULL;
6438 if (remove_from_treeview)
6439 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6443 my_g_format_time_for_display (glong secs)
6448 time_t time_mtime, time_now;
6449 const gchar *format;
6450 gchar *locale_format = NULL;
6452 char *date_str = NULL;
6454 const char *locale, *dot = NULL;
6455 gint64 codepage = -1;
6461 #ifdef HAVE_LOCALTIME_R
6462 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6465 struct tm *ptm = localtime ((time_t *) &time_mtime);
6469 g_warning ("ptm != NULL failed");
6471 return g_strdup (_("Unknown"));
6474 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6476 #endif /* HAVE_LOCALTIME_R */
6478 g_date_set_time_t (&mtime, time_mtime);
6479 time_now = time (NULL);
6480 g_date_set_time_t (&now, time_now);
6482 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6484 /* Translators: %H means "hours" and %M means "minutes" */
6486 format = _("%H:%M");
6487 else if (days_diff == 1)
6488 format = _("Yesterday at %H:%M");
6491 if (days_diff > 1 && days_diff < 7)
6492 format = "%A"; /* Days from last week */
6494 format = "%x"; /* Any other date */
6498 /* g_locale_from_utf8() returns a string in the system
6499 * code-page, which is not always the same as that used by the C
6500 * library. For instance when running a GTK+ program with
6501 * LANG=ko on an English version of Windows, the system
6502 * code-page is 1252, but the code-page used by the C library is
6503 * 949. (It's GTK+ itself that sets the C library locale when it
6504 * notices the LANG environment variable. See gtkmain.c The
6505 * Microsoft C library doesn't look at any locale environment
6506 * variables.) We need to pass strftime() a string in the C
6507 * library's code-page. See bug #509885.
6509 locale = setlocale (LC_ALL, NULL);
6511 dot = strchr (locale, '.');
6514 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6516 /* All codepages should fit in 16 bits AFAIK */
6517 if (codepage > 0 && codepage < 65536)
6519 sprintf (charset, "CP%u", (guint) codepage);
6520 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6524 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6526 if (locale_format != NULL &&
6527 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6530 /* As above but in opposite direction... */
6531 if (codepage > 0 && codepage < 65536)
6532 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6534 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6538 if (date_str == NULL)
6539 date_str = g_strdup (_("Unknown"));
6541 g_free (locale_format);
6546 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6548 GFileAttributeType type;
6551 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6552 g_file_info_set_attribute (to, attribute, type, value);
6556 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6558 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6559 GFile *file = G_FILE (object);
6560 GFileInfo *queried, *info;
6563 queried = g_file_query_info_finish (file, res, NULL);
6564 if (queried == NULL)
6567 GDK_THREADS_ENTER ();
6569 /* now we know model is valid */
6571 /* file was deleted */
6572 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6574 GDK_THREADS_LEAVE ();
6578 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6580 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6581 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6582 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6584 _gtk_file_system_model_update_file (model, file, info, FALSE);
6586 g_object_unref (info);
6588 GDK_THREADS_LEAVE ();
6592 file_system_model_set (GtkFileSystemModel *model,
6599 GtkFileChooserDefault *impl = data;
6603 case MODEL_COL_FILE:
6604 g_value_set_object (value, file);
6606 case MODEL_COL_NAME:
6608 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6610 g_value_set_string (value, g_file_info_get_display_name (info));
6612 case MODEL_COL_NAME_COLLATED:
6614 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6616 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6618 case MODEL_COL_IS_FOLDER:
6619 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6621 case MODEL_COL_PIXBUF:
6624 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6626 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6630 GtkTreeModel *tree_model;
6631 GtkTreePath *path, *start, *end;
6634 if (impl->browse_files_tree_view == NULL ||
6635 g_file_info_has_attribute (info, "filechooser::queried"))
6638 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6639 if (tree_model != GTK_TREE_MODEL (model))
6642 if (!_gtk_file_system_model_get_iter_for_file (model,
6645 g_assert_not_reached ();
6646 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6648 path = gtk_tree_model_get_path (tree_model, &iter);
6649 if (gtk_tree_path_compare (start, path) != 1 &&
6650 gtk_tree_path_compare (path, end) != 1)
6652 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6653 g_file_query_info_async (file,
6654 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6655 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6656 G_FILE_ATTRIBUTE_STANDARD_ICON,
6657 G_FILE_QUERY_INFO_NONE,
6659 _gtk_file_system_model_get_cancellable (model),
6660 file_system_model_got_thumbnail,
6663 gtk_tree_path_free (path);
6664 gtk_tree_path_free (start);
6665 gtk_tree_path_free (end);
6670 g_value_set_object (value, NULL);
6672 case MODEL_COL_SIZE:
6673 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6675 case MODEL_COL_SIZE_TEXT:
6676 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6677 g_value_set_string (value, NULL);
6679 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6681 case MODEL_COL_MTIME:
6682 case MODEL_COL_MTIME_TEXT:
6687 g_file_info_get_modification_time (info, &tv);
6688 if (column == MODEL_COL_MTIME)
6689 g_value_set_long (value, tv.tv_sec);
6690 else if (tv.tv_sec == 0)
6691 g_value_set_static_string (value, _("Unknown"));
6693 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6696 case MODEL_COL_ELLIPSIZE:
6697 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6700 g_assert_not_reached ();
6707 /* Gets rid of the old list model and creates a new one for the current folder */
6709 set_list_model (GtkFileChooserDefault *impl,
6712 g_assert (impl->current_folder != NULL);
6714 profile_start ("start", NULL);
6716 stop_loading_and_clear_list_model (impl, TRUE);
6718 set_busy_cursor (impl, TRUE);
6720 impl->browse_files_model =
6721 _gtk_file_system_model_new_for_directory (impl->current_folder,
6723 file_system_model_set,
6725 MODEL_COLUMN_TYPES);
6727 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6729 profile_msg (" set sort function", NULL);
6730 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6731 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6732 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6733 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6734 set_sort_column (impl);
6735 impl->list_sort_ascending = TRUE;
6736 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6737 G_CALLBACK (list_sort_column_changed_cb), impl);
6739 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6741 g_signal_connect (impl->browse_files_model, "finished-loading",
6742 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6744 install_list_model_filter (impl);
6746 profile_end ("end", NULL);
6751 struct update_chooser_entry_selected_foreach_closure {
6753 GtkTreeIter first_selected_iter;
6757 compare_utf8_filenames (const gchar *a,
6760 gchar *a_folded, *b_folded;
6763 a_folded = g_utf8_strdown (a, -1);
6764 b_folded = g_utf8_strdown (b, -1);
6766 retval = strcmp (a_folded, b_folded);
6775 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6780 struct update_chooser_entry_selected_foreach_closure *closure;
6783 closure->num_selected++;
6785 if (closure->num_selected == 1)
6786 closure->first_selected_iter = *iter;
6790 update_chooser_entry (GtkFileChooserDefault *impl)
6792 GtkTreeSelection *selection;
6793 struct update_chooser_entry_selected_foreach_closure closure;
6795 /* no need to update the file chooser's entry if there's no entry */
6796 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6797 !impl->location_entry)
6800 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6801 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6802 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6803 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6804 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6807 g_assert (impl->location_entry != NULL);
6809 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6810 closure.num_selected = 0;
6811 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6813 if (closure.num_selected == 0)
6815 if (impl->operation_mode == OPERATION_MODE_RECENT)
6816 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6818 goto maybe_clear_entry;
6820 else if (closure.num_selected == 1)
6822 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6825 gboolean change_entry;
6827 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6829 /* If the cursor moved to the row of the newly created folder,
6830 * retrieving info will return NULL.
6835 g_free (impl->browse_files_last_selected_name);
6836 impl->browse_files_last_selected_name =
6837 g_strdup (g_file_info_get_display_name (info));
6839 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6840 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6841 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6843 /* Don't change the name when clicking on a folder... */
6844 change_entry = (! _gtk_file_info_consider_as_directory (info));
6847 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6851 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6853 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6854 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6859 else if (impl->operation_mode == OPERATION_MODE_RECENT
6860 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6864 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
6866 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
6867 MODEL_COL_FILE, &folder,
6869 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
6870 g_object_unref (folder);
6876 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6877 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6879 /* Multiple selection, so just clear the entry. */
6880 g_free (impl->browse_files_last_selected_name);
6881 impl->browse_files_last_selected_name = NULL;
6883 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6889 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6890 && impl->browse_files_last_selected_name)
6892 const char *entry_text;
6894 gboolean clear_entry;
6896 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6897 len = strlen (entry_text);
6900 /* The file chooser entry may have appended a "/" to its text.
6901 * So take it out, and compare the result to the old selection.
6903 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6907 tmp = g_strndup (entry_text, len - 1);
6908 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6912 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6915 clear_entry = FALSE;
6918 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6923 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6927 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6931 struct UpdateCurrentFolderData
6933 GtkFileChooserDefault *impl;
6935 gboolean keep_trail;
6936 gboolean clear_entry;
6937 GFile *original_file;
6938 GError *original_error;
6942 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6943 GtkFileSystemVolume *volume,
6944 const GError *error,
6947 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6948 struct UpdateCurrentFolderData *data = user_data;
6949 GtkFileChooserDefault *impl = data->impl;
6951 if (cancellable != impl->update_current_folder_cancellable)
6954 impl->update_current_folder_cancellable = NULL;
6955 set_busy_cursor (impl, FALSE);
6962 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6963 impl->reload_state = RELOAD_EMPTY;
6967 change_folder_and_display_error (impl, data->file, data->clear_entry);
6970 g_object_unref (data->file);
6973 g_object_unref (cancellable);
6977 update_current_folder_get_info_cb (GCancellable *cancellable,
6979 const GError *error,
6982 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6983 struct UpdateCurrentFolderData *data = user_data;
6984 GtkFileChooserDefault *impl = data->impl;
6986 if (cancellable != impl->update_current_folder_cancellable)
6989 impl->update_current_folder_cancellable = NULL;
6990 impl->reload_state = RELOAD_EMPTY;
6992 set_busy_cursor (impl, FALSE);
7001 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7003 GMountOperation *mount_operation;
7004 GtkWidget *toplevel;
7006 g_object_unref (cancellable);
7007 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7009 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7011 set_busy_cursor (impl, TRUE);
7013 impl->update_current_folder_cancellable =
7014 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7016 update_current_folder_mount_enclosing_volume_cb,
7022 if (!data->original_file)
7024 data->original_file = g_object_ref (data->file);
7025 data->original_error = g_error_copy (error);
7028 parent_file = g_file_get_parent (data->file);
7030 /* get parent path and try to change the folder to that */
7033 g_object_unref (data->file);
7034 data->file = parent_file;
7036 g_object_unref (cancellable);
7038 /* restart the update current folder operation */
7039 impl->reload_state = RELOAD_HAS_FOLDER;
7041 impl->update_current_folder_cancellable =
7042 _gtk_file_system_get_info (impl->file_system, data->file,
7044 update_current_folder_get_info_cb,
7047 set_busy_cursor (impl, TRUE);
7053 /* Error and bail out, ignoring "not found" errors since they're useless:
7054 * they only happen when a program defaults to a folder that has been (re)moved.
7056 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7057 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7059 g_error_free (data->original_error);
7061 g_object_unref (data->original_file);
7067 if (data->original_file)
7069 /* Error and bail out, ignoring "not found" errors since they're useless:
7070 * they only happen when a program defaults to a folder that has been (re)moved.
7072 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7073 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7075 g_error_free (data->original_error);
7077 g_object_unref (data->original_file);
7080 if (! _gtk_file_info_consider_as_directory (info))
7083 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7086 if (impl->current_folder != data->file)
7088 if (impl->current_folder)
7089 g_object_unref (impl->current_folder);
7091 impl->current_folder = g_object_ref (data->file);
7094 impl->reload_state = RELOAD_HAS_FOLDER;
7096 /* Update the widgets that may trigger a folder change themselves. */
7098 if (!impl->changing_folder)
7100 impl->changing_folder = TRUE;
7102 shortcuts_update_current_folder (impl);
7104 impl->changing_folder = FALSE;
7107 /* Set the folder on the save entry */
7109 if (impl->location_entry)
7111 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7112 impl->current_folder);
7114 if (data->clear_entry)
7115 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7118 /* Create a new list model. This is slightly evil; we store the result value
7119 * but perform more actions rather than returning immediately even if it
7120 * generates an error.
7122 set_list_model (impl, NULL);
7124 /* Refresh controls */
7126 shortcuts_find_current_folder (impl);
7128 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7130 check_preview_change (impl);
7131 bookmarks_check_add_sensitivity (impl);
7133 g_signal_emit_by_name (impl, "selection-changed", 0);
7136 g_object_unref (data->file);
7139 g_object_unref (cancellable);
7143 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7145 gboolean keep_trail,
7146 gboolean clear_entry,
7149 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7150 struct UpdateCurrentFolderData *data;
7152 profile_start ("start", NULL);
7154 g_object_ref (file);
7156 switch (impl->operation_mode)
7158 case OPERATION_MODE_SEARCH:
7159 search_switch_to_browse_mode (impl);
7161 case OPERATION_MODE_RECENT:
7162 recent_switch_to_browse_mode (impl);
7164 case OPERATION_MODE_BROWSE:
7168 if (impl->local_only && !g_file_is_native (file))
7170 g_set_error_literal (error,
7171 GTK_FILE_CHOOSER_ERROR,
7172 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7173 _("Cannot change to folder because it is not local"));
7175 g_object_unref (file);
7176 profile_end ("end - not local", NULL);
7180 if (impl->update_current_folder_cancellable)
7181 g_cancellable_cancel (impl->update_current_folder_cancellable);
7183 /* Test validity of path here. */
7184 data = g_new0 (struct UpdateCurrentFolderData, 1);
7186 data->file = g_object_ref (file);
7187 data->keep_trail = keep_trail;
7188 data->clear_entry = clear_entry;
7190 impl->reload_state = RELOAD_HAS_FOLDER;
7192 impl->update_current_folder_cancellable =
7193 _gtk_file_system_get_info (impl->file_system, file,
7195 update_current_folder_get_info_cb,
7198 set_busy_cursor (impl, TRUE);
7199 g_object_unref (file);
7201 profile_end ("end", NULL);
7206 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7208 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7210 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7211 impl->operation_mode == OPERATION_MODE_RECENT)
7214 if (impl->reload_state == RELOAD_EMPTY)
7216 /* We are unmapped, or we had an error while loading the last folder.
7217 * We'll return the folder used by the last invocation of the file chooser
7218 * since once we get (re)mapped, we'll load *that* folder anyway unless
7219 * the caller explicitly calls set_current_folder() on us.
7221 return get_file_for_last_folder_opened (impl);
7224 if (impl->current_folder)
7225 return g_object_ref (impl->current_folder);
7231 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7234 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7236 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7237 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7239 pending_select_files_free (impl);
7240 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7244 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7248 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7252 parent_file = g_file_get_parent (file);
7255 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7257 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7258 impl->operation_mode == OPERATION_MODE_RECENT ||
7259 impl->load_state == LOAD_EMPTY)
7265 g_assert (impl->current_folder != NULL);
7267 same_path = g_file_equal (parent_file, impl->current_folder);
7270 if (same_path && impl->load_state == LOAD_FINISHED)
7275 files.data = (gpointer) file;
7278 result = show_and_select_files (impl, &files);
7279 g_object_unref (parent_file);
7283 pending_select_files_add (impl, file);
7289 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7290 g_object_unref (parent_file);
7294 g_object_unref (parent_file);
7299 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7302 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7303 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7306 if (!impl->browse_files_model)
7309 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7314 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7319 maybe_select (GtkTreeModel *model,
7324 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7325 GtkTreeSelection *selection;
7328 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7330 gtk_tree_model_get (model, iter,
7331 MODEL_COL_IS_FOLDER, &is_folder,
7334 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7335 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7336 gtk_tree_selection_select_iter (selection, iter);
7338 gtk_tree_selection_unselect_iter (selection, iter);
7344 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7346 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7348 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7349 impl->operation_mode == OPERATION_MODE_RECENT)
7351 GtkTreeSelection *selection;
7353 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7354 gtk_tree_selection_select_all (selection);
7358 if (impl->select_multiple)
7359 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7360 maybe_select, impl);
7364 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7366 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7367 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7369 gtk_tree_selection_unselect_all (selection);
7370 pending_select_files_free (impl);
7373 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7375 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7377 * is_empty_ret - whether the file entry is totally empty
7379 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7380 * the path will be "$cwd/foobar")
7383 check_save_entry (GtkFileChooserDefault *impl,
7385 gboolean *is_well_formed_ret,
7386 gboolean *is_empty_ret,
7387 gboolean *is_file_part_empty_ret,
7388 gboolean *is_folder)
7390 GtkFileChooserEntry *chooser_entry;
7391 GFile *current_folder;
7392 const char *file_part;
7396 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7397 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7398 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7399 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7400 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7402 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7404 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7407 *is_well_formed_ret = TRUE;
7408 *is_empty_ret = TRUE;
7409 *is_file_part_empty_ret = TRUE;
7415 *is_empty_ret = FALSE;
7417 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7418 if (!current_folder)
7421 *is_well_formed_ret = FALSE;
7422 *is_file_part_empty_ret = FALSE;
7428 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7430 if (!file_part || file_part[0] == '\0')
7432 *file_ret = g_object_ref (current_folder);
7433 *is_well_formed_ret = TRUE;
7434 *is_file_part_empty_ret = TRUE;
7440 *is_file_part_empty_ret = FALSE;
7443 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7447 error_building_filename_dialog (impl, error);
7449 *is_well_formed_ret = FALSE;
7456 *is_well_formed_ret = TRUE;
7457 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7460 struct get_files_closure {
7461 GtkFileChooserDefault *impl;
7463 GFile *file_from_entry;
7467 get_files_foreach (GtkTreeModel *model,
7472 struct get_files_closure *info;
7474 GtkFileSystemModel *fs_model;
7477 fs_model = info->impl->browse_files_model;
7479 file = _gtk_file_system_model_get_file (fs_model, iter);
7481 return; /* We are on the editable row */
7483 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7484 info->result = g_slist_prepend (info->result, g_object_ref (file));
7488 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7490 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7491 struct get_files_closure info;
7492 GtkWindow *toplevel;
7493 GtkWidget *current_focus;
7494 gboolean file_list_seen;
7498 info.file_from_entry = NULL;
7500 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7501 return search_get_selected_files (impl);
7503 if (impl->operation_mode == OPERATION_MODE_RECENT)
7505 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7507 file_list_seen = TRUE;
7511 return recent_get_selected_files (impl);
7514 toplevel = get_toplevel (GTK_WIDGET (impl));
7516 current_focus = gtk_window_get_focus (toplevel);
7518 current_focus = NULL;
7520 file_list_seen = FALSE;
7521 if (current_focus == impl->browse_files_tree_view)
7523 GtkTreeSelection *selection;
7527 file_list_seen = TRUE;
7528 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7529 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7531 /* If there is no selection in the file list, we probably have this situation:
7533 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7534 * 2. He then double-clicked on a folder ("bar") in the file list
7536 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7537 * filename entry to see if that is the case.
7539 if (info.result == NULL && impl->location_entry)
7542 else if (impl->location_entry && current_focus == impl->location_entry)
7544 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7548 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7553 if (!is_well_formed)
7556 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7558 g_object_unref (info.file_from_entry);
7562 if (info.file_from_entry)
7563 info.result = g_slist_prepend (info.result, info.file_from_entry);
7564 else if (!file_list_seen)
7569 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7571 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7575 /* The focus is on a dialog's action area button or something else */
7576 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7577 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7585 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7586 * fall back to the current directory */
7587 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7588 info.result == NULL)
7590 GFile *current_folder;
7592 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7595 info.result = g_slist_prepend (info.result, current_folder);
7598 return g_slist_reverse (info.result);
7602 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7604 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7606 if (impl->preview_file)
7607 return g_object_ref (impl->preview_file);
7612 static GtkFileSystem *
7613 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7615 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7617 return impl->file_system;
7620 /* Shows or hides the filter widgets */
7622 show_filters (GtkFileChooserDefault *impl,
7626 gtk_widget_show (impl->filter_combo_hbox);
7628 gtk_widget_hide (impl->filter_combo_hbox);
7632 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7633 GtkFileFilter *filter)
7635 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7638 if (g_slist_find (impl->filters, filter))
7640 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7644 g_object_ref_sink (filter);
7645 impl->filters = g_slist_append (impl->filters, filter);
7647 name = gtk_file_filter_get_name (filter);
7649 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7651 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7653 if (!g_slist_find (impl->filters, impl->current_filter))
7654 set_current_filter (impl, filter);
7656 show_filters (impl, TRUE);
7660 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7661 GtkFileFilter *filter)
7663 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7664 GtkTreeModel *model;
7668 filter_index = g_slist_index (impl->filters, filter);
7670 if (filter_index < 0)
7672 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7676 impl->filters = g_slist_remove (impl->filters, filter);
7678 if (filter == impl->current_filter)
7681 set_current_filter (impl, impl->filters->data);
7683 set_current_filter (impl, NULL);
7686 /* Remove row from the combo box */
7687 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7688 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7689 g_assert_not_reached ();
7691 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7693 g_object_unref (filter);
7696 show_filters (impl, FALSE);
7700 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7702 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7704 return g_slist_copy (impl->filters);
7707 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7709 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7712 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7715 struct AddShortcutData
7717 GtkFileChooserDefault *impl;
7722 add_shortcut_get_info_cb (GCancellable *cancellable,
7724 const GError *error,
7728 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7729 struct AddShortcutData *data = user_data;
7731 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7734 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7736 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7739 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7741 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7744 g_object_unref (data->impl);
7745 g_object_unref (data->file);
7748 g_object_unref (cancellable);
7752 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7756 GCancellable *cancellable;
7757 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7758 struct AddShortcutData *data;
7762 /* Avoid adding duplicates */
7763 pos = shortcut_find_position (impl, file);
7764 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7768 uri = g_file_get_uri (file);
7769 /* translators, "Shortcut" means "Bookmark" here */
7771 GTK_FILE_CHOOSER_ERROR,
7772 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7773 _("Shortcut %s already exists"),
7780 for (l = impl->loading_shortcuts; l; l = l->next)
7782 GCancellable *c = l->data;
7785 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7786 if (f && g_file_equal (file, f))
7790 uri = g_file_get_uri (file);
7792 GTK_FILE_CHOOSER_ERROR,
7793 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7794 _("Shortcut %s already exists"),
7802 data = g_new0 (struct AddShortcutData, 1);
7803 data->impl = g_object_ref (impl);
7804 data->file = g_object_ref (file);
7806 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7808 add_shortcut_get_info_cb, data);
7813 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7814 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7820 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7824 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7831 for (l = impl->loading_shortcuts; l; l = l->next)
7833 GCancellable *c = l->data;
7836 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7837 if (f && g_file_equal (file, f))
7839 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7840 g_cancellable_cancel (c);
7845 if (impl->num_shortcuts == 0)
7848 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7849 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7850 g_assert_not_reached ();
7852 for (i = 0; i < impl->num_shortcuts; i++)
7855 ShortcutType shortcut_type;
7858 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7859 SHORTCUTS_COL_DATA, &col_data,
7860 SHORTCUTS_COL_TYPE, &shortcut_type,
7862 g_assert (col_data != NULL);
7863 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7865 shortcut = col_data;
7866 if (g_file_equal (shortcut, file))
7868 shortcuts_remove_rows (impl, pos + i, 1);
7869 impl->num_shortcuts--;
7873 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7874 g_assert_not_reached ();
7879 uri = g_file_get_uri (file);
7880 /* translators, "Shortcut" means "Bookmark" here */
7882 GTK_FILE_CHOOSER_ERROR,
7883 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7884 _("Shortcut %s does not exist"),
7892 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7894 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7900 if (impl->num_shortcuts == 0)
7903 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7904 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7905 g_assert_not_reached ();
7909 for (i = 0; i < impl->num_shortcuts; i++)
7912 ShortcutType shortcut_type;
7915 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7916 SHORTCUTS_COL_DATA, &col_data,
7917 SHORTCUTS_COL_TYPE, &shortcut_type,
7919 g_assert (col_data != NULL);
7920 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7922 shortcut = col_data;
7923 list = g_slist_prepend (list, g_object_ref (shortcut));
7925 if (i != impl->num_shortcuts - 1)
7927 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7928 g_assert_not_reached ();
7932 return g_slist_reverse (list);
7935 /* Guesses a size based upon font sizes */
7937 find_good_size_from_style (GtkWidget *widget,
7941 GtkStyleContext *context;
7942 GtkStateFlags state;
7947 context = gtk_widget_get_style_context (widget);
7948 state = gtk_widget_get_state_flags (widget);
7950 screen = gtk_widget_get_screen (widget);
7953 resolution = gdk_screen_get_resolution (screen);
7954 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7958 resolution = 96.0; /* wheeee */
7960 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7961 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7963 *width = font_size * NUM_CHARS;
7964 *height = font_size * NUM_LINES;
7968 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7969 gint *default_width,
7970 gint *default_height)
7972 GtkFileChooserDefault *impl;
7974 int x, y, width, height;
7976 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7978 settings_ensure (impl);
7980 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7981 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7983 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7985 *default_width = width;
7986 *default_height = height;
7990 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7992 if (impl->preview_widget_active &&
7993 impl->preview_widget &&
7994 gtk_widget_get_visible (impl->preview_widget))
7996 gtk_widget_get_preferred_size (impl->preview_box,
7998 *default_width += PREVIEW_HBOX_SPACING + req.width;
8001 if (impl->extra_widget &&
8002 gtk_widget_get_visible (impl->extra_widget))
8004 gtk_widget_get_preferred_size (impl->extra_align,
8006 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8010 struct switch_folder_closure {
8011 GtkFileChooserDefault *impl;
8016 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8018 switch_folder_foreach_cb (GtkTreeModel *model,
8023 struct switch_folder_closure *closure;
8027 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8028 closure->num_selected++;
8031 /* Changes to the selected folder in the list view */
8033 switch_to_selected_folder (GtkFileChooserDefault *impl)
8035 GtkTreeSelection *selection;
8036 struct switch_folder_closure closure;
8038 /* We do this with foreach() rather than get_selected() as we may be in
8039 * multiple selection mode
8042 closure.impl = impl;
8043 closure.file = NULL;
8044 closure.num_selected = 0;
8046 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8047 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8049 g_assert (closure.file && closure.num_selected == 1);
8051 change_folder_and_display_error (impl, closure.file, FALSE);
8054 /* Gets the GFileInfo for the selected row in the file list; assumes single
8058 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8059 gboolean *had_selection)
8061 GtkTreeSelection *selection;
8065 g_assert (!impl->select_multiple);
8066 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8067 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8069 *had_selection = FALSE;
8073 *had_selection = TRUE;
8075 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8079 /* Gets the display name of the selected file in the file list; assumes single
8080 * selection mode and that something is selected.
8082 static const gchar *
8083 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8086 gboolean had_selection;
8088 info = get_selected_file_info_from_file_list (impl, &had_selection);
8089 g_assert (had_selection);
8090 g_assert (info != NULL);
8092 return g_file_info_get_display_name (info);
8096 add_custom_button_to_dialog (GtkDialog *dialog,
8097 const gchar *mnemonic_label,
8098 const gchar *stock_id,
8103 button = gtk_button_new_with_mnemonic (mnemonic_label);
8104 gtk_widget_set_can_default (button, TRUE);
8105 gtk_button_set_image (GTK_BUTTON (button),
8106 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8107 gtk_widget_show (button);
8109 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8112 /* Presents an overwrite confirmation dialog; returns whether we should accept
8116 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8117 const gchar *file_part,
8118 const gchar *folder_display_name)
8120 GtkWindow *toplevel;
8124 toplevel = get_toplevel (GTK_WIDGET (impl));
8126 dialog = gtk_message_dialog_new (toplevel,
8127 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8128 GTK_MESSAGE_QUESTION,
8130 _("A file named \"%s\" already exists. Do you want to replace it?"),
8132 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8133 _("The file already exists in \"%s\". Replacing it will "
8134 "overwrite its contents."),
8135 folder_display_name);
8137 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8138 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8139 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8140 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8141 GTK_RESPONSE_ACCEPT,
8142 GTK_RESPONSE_CANCEL,
8144 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8146 if (gtk_window_has_group (toplevel))
8147 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8148 GTK_WINDOW (dialog));
8150 response = gtk_dialog_run (GTK_DIALOG (dialog));
8152 gtk_widget_destroy (dialog);
8154 return (response == GTK_RESPONSE_ACCEPT);
8157 struct GetDisplayNameData
8159 GtkFileChooserDefault *impl;
8163 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8164 * as requesting a response means, "the dialog is confirmed".
8167 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8169 g_signal_emit_by_name (impl, "response-requested");
8170 add_selection_to_recent_list (impl);
8174 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8176 const GError *error,
8179 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8180 gboolean should_respond = FALSE;
8181 struct GetDisplayNameData *data = user_data;
8183 if (cancellable != data->impl->should_respond_get_info_cancellable)
8186 data->impl->should_respond_get_info_cancellable = NULL;
8192 /* Huh? Did the folder disappear? Let the caller deal with it */
8193 should_respond = TRUE;
8195 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8197 set_busy_cursor (data->impl, FALSE);
8199 request_response_and_add_to_recent_list (data->impl);
8202 g_object_unref (data->impl);
8203 g_free (data->file_part);
8206 g_object_unref (cancellable);
8209 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8210 * should respond. Can get the file part from the file list or the save entry.
8213 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8214 const gchar *file_part,
8217 GtkFileChooserConfirmation conf;
8219 if (!impl->do_overwrite_confirmation)
8222 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8224 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8228 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8230 struct GetDisplayNameData *data;
8232 g_assert (file_part != NULL);
8234 data = g_new0 (struct GetDisplayNameData, 1);
8235 data->impl = g_object_ref (impl);
8236 data->file_part = g_strdup (file_part);
8238 if (impl->should_respond_get_info_cancellable)
8239 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8241 impl->should_respond_get_info_cancellable =
8242 _gtk_file_system_get_info (impl->file_system, parent_file,
8243 "standard::display-name",
8244 confirmation_confirm_get_info_cb,
8246 set_busy_cursor (data->impl, TRUE);
8250 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8253 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8257 g_assert_not_reached ();
8262 struct FileExistsData
8264 GtkFileChooserDefault *impl;
8265 gboolean file_exists_and_is_not_folder;
8271 name_entry_get_parent_info_cb (GCancellable *cancellable,
8273 const GError *error,
8276 gboolean parent_is_folder;
8277 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8278 struct FileExistsData *data = user_data;
8280 if (cancellable != data->impl->should_respond_get_info_cancellable)
8283 data->impl->should_respond_get_info_cancellable = NULL;
8285 set_busy_cursor (data->impl, FALSE);
8291 parent_is_folder = FALSE;
8293 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8295 if (parent_is_folder)
8297 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8299 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) */
8301 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8303 if (data->file_exists_and_is_not_folder)
8308 /* Dup the string because the string may be modified
8309 * depending on what clients do in the confirm-overwrite
8310 * signal and this corrupts the pointer
8312 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8313 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8317 request_response_and_add_to_recent_list (data->impl);
8320 request_response_and_add_to_recent_list (data->impl);
8322 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8323 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8325 GError *mkdir_error = NULL;
8327 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8328 * "/blah/nonexistent" you *will* want a folder created.
8331 set_busy_cursor (data->impl, TRUE);
8332 g_file_make_directory (data->file, NULL, &mkdir_error);
8333 set_busy_cursor (data->impl, FALSE);
8336 request_response_and_add_to_recent_list (data->impl);
8338 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8341 g_assert_not_reached ();
8347 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8348 error_with_file_under_nonfolder (data->impl, data->parent_file);
8354 /* The parent folder is not readable for some reason */
8356 error_copy = g_error_copy (error);
8357 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8362 g_object_unref (data->impl);
8363 g_object_unref (data->file);
8364 g_object_unref (data->parent_file);
8367 g_object_unref (cancellable);
8371 file_exists_get_info_cb (GCancellable *cancellable,
8373 const GError *error,
8376 gboolean data_ownership_taken = FALSE;
8377 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8378 gboolean file_exists;
8380 gboolean needs_parent_check = FALSE;
8381 struct FileExistsData *data = user_data;
8383 if (cancellable != data->impl->file_exists_get_info_cancellable)
8386 data->impl->file_exists_get_info_cancellable = NULL;
8388 set_busy_cursor (data->impl, FALSE);
8393 file_exists = (info != NULL);
8394 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8396 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8399 change_folder_and_display_error (data->impl, data->file, TRUE);
8403 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8405 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8408 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8410 if (file_exists && !is_folder)
8412 /* Oops, the user typed the name of an existing path which is not
8415 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8416 g_error_copy (error));
8420 needs_parent_check = TRUE;
8423 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8427 needs_parent_check = TRUE;
8433 /* User typed a folder; we are done */
8434 request_response_and_add_to_recent_list (data->impl);
8437 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8440 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8443 change_folder_and_display_error (data->impl, data->file, TRUE);
8445 needs_parent_check = TRUE;
8449 g_assert_not_reached();
8452 if (needs_parent_check) {
8453 /* check that everything up to the last path component exists (i.e. the parent) */
8455 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8456 data_ownership_taken = TRUE;
8458 if (data->impl->should_respond_get_info_cancellable)
8459 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8461 data->impl->should_respond_get_info_cancellable =
8462 _gtk_file_system_get_info (data->impl->file_system,
8465 name_entry_get_parent_info_cb,
8467 set_busy_cursor (data->impl, TRUE);
8471 if (!data_ownership_taken)
8473 g_object_unref (data->impl);
8474 g_object_unref (data->file);
8475 g_object_unref (data->parent_file);
8479 g_object_unref (cancellable);
8483 paste_text_received (GtkClipboard *clipboard,
8485 GtkFileChooserDefault *impl)
8492 file = g_file_new_for_uri (text);
8494 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8495 location_popup_handler (impl, text);
8497 g_object_unref (file);
8500 /* Handler for the "location-popup-on-paste" keybinding signal */
8502 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8504 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8505 GDK_SELECTION_CLIPBOARD);
8506 gtk_clipboard_request_text (clipboard,
8507 (GtkClipboardTextReceivedFunc) paste_text_received,
8511 /* Implementation for GtkFileChooserEmbed::should_respond() */
8513 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8518 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8520 for (l = files; l; l = l->next)
8522 GFile *file = l->data;
8525 uri = g_file_get_uri (file);
8528 gtk_recent_manager_add_item (impl->recent_manager, uri);
8533 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8534 g_slist_free (files);
8538 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8540 GtkFileChooserDefault *impl;
8541 GtkWidget *toplevel;
8542 GtkWidget *current_focus;
8545 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8547 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8548 g_assert (GTK_IS_WINDOW (toplevel));
8552 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8554 if (current_focus == impl->browse_files_tree_view)
8556 /* The following array encodes what we do based on the impl->action and the
8557 * number of files selected.
8560 NOOP, /* Do nothing (don't respond) */
8561 RESPOND, /* Respond immediately */
8562 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8563 ALL_FILES, /* Respond only if everything selected is a file */
8564 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8565 SAVE_ENTRY, /* Go to the code for handling the save entry */
8566 NOT_REACHED /* Sanity check */
8568 static const ActionToTake what_to_do[4][3] = {
8569 /* 0 selected 1 selected many selected */
8570 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8571 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8572 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8573 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8577 gboolean all_files, all_folders;
8579 ActionToTake action;
8583 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8585 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8587 retval = search_should_respond (impl);
8591 if (impl->operation_mode == OPERATION_MODE_RECENT)
8593 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8597 retval = recent_should_respond (impl);
8602 selection_check (impl, &num_selected, &all_files, &all_folders);
8604 if (num_selected > 2)
8609 action = what_to_do [impl->action] [k];
8620 case RESPOND_OR_SWITCH:
8621 g_assert (num_selected == 1);
8625 switch_to_selected_folder (impl);
8628 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8630 retval = should_respond_after_confirm_overwrite (impl,
8631 get_display_name_from_file_list (impl),
8632 impl->current_folder);
8646 retval = all_folders;
8653 g_assert_not_reached ();
8656 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8659 gboolean is_well_formed, is_empty, is_file_part_empty;
8661 GtkFileChooserEntry *entry;
8666 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8667 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8668 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8669 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8670 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8672 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8673 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8675 if (!is_well_formed)
8678 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8679 && impl->operation_mode == OPERATION_MODE_RECENT)
8681 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8683 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8684 * thus changing our assumption that no selection is present - setting
8685 * a selection causes the error message from path_bar_set_mode() to go away,
8686 * but we want the user to see that message!
8688 gtk_widget_grab_focus (impl->browse_files_tree_view);
8691 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8698 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8699 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8701 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8702 gtk_widget_grab_focus (impl->location_entry);
8709 g_assert (file != NULL);
8714 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8715 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8717 change_folder_and_display_error (impl, file, TRUE);
8719 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8720 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8722 /* The folder already exists, so we do not need to create it.
8723 * Just respond to terminate the dialog.
8729 g_assert_not_reached ();
8734 struct FileExistsData *data;
8736 /* We need to check whether file exists and whether it is a folder -
8737 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8738 * negative (it doesn't know yet if your last path component is a
8742 data = g_new0 (struct FileExistsData, 1);
8743 data->impl = g_object_ref (impl);
8744 data->file = g_object_ref (file);
8745 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8747 if (impl->file_exists_get_info_cancellable)
8748 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8750 impl->file_exists_get_info_cancellable =
8751 _gtk_file_system_get_info (impl->file_system, file,
8753 file_exists_get_info_cb,
8756 set_busy_cursor (impl, TRUE);
8759 g_error_free (error);
8762 g_object_unref (file);
8764 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8766 /* The focus is on a dialog's action area button, *and* the widget that
8767 * was focused immediately before it is the file list.
8771 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8773 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8776 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8778 /* The focus is on a dialog's action area button, *and* the widget that
8779 * was focused immediately before it is the location entry.
8784 /* The focus is on a dialog's action area button or something else */
8785 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8786 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8794 add_selection_to_recent_list (impl);
8799 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8801 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8803 GtkFileChooserDefault *impl;
8806 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8808 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8809 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8811 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8812 widget = impl->browse_files_tree_view;
8814 widget = impl->location_entry;
8816 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8817 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8818 widget = impl->location_entry;
8821 g_assert_not_reached ();
8825 g_assert (widget != NULL);
8826 gtk_widget_grab_focus (widget);
8829 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8831 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8841 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8842 *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 */
8845 /* Constructs a list of the selected paths in search mode */
8847 search_get_selected_files (GtkFileChooserDefault *impl)
8850 GtkTreeSelection *selection;
8854 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8855 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8856 result = g_slist_reverse (result);
8861 /* Called from ::should_respond(). We return whether there are selected files
8862 * in the search list.
8865 search_should_respond (GtkFileChooserDefault *impl)
8867 GtkTreeSelection *selection;
8869 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8871 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8872 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8875 /* Adds one hit from the search engine to the search_model */
8877 search_add_hit (GtkFileChooserDefault *impl,
8882 file = g_file_new_for_uri (uri);
8886 if (!g_file_is_native (file))
8888 g_object_unref (file);
8892 _gtk_file_system_model_add_and_query_file (impl->search_model,
8896 g_object_unref (file);
8899 /* Callback used from GtkSearchEngine when we get new hits */
8901 search_engine_hits_added_cb (GtkSearchEngine *engine,
8905 GtkFileChooserDefault *impl;
8908 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8910 for (l = hits; l; l = l->next)
8911 search_add_hit (impl, (gchar*)l->data);
8914 /* Callback used from GtkSearchEngine when the query is done running */
8916 search_engine_finished_cb (GtkSearchEngine *engine,
8919 GtkFileChooserDefault *impl;
8921 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8924 /* EB: setting the model here will avoid loads of row events,
8925 * but it'll make the search look like blocked.
8927 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8928 GTK_TREE_MODEL (impl->search_model));
8931 /* FMQ: if search was empty, say that we got no hits */
8932 set_busy_cursor (impl, FALSE);
8935 /* Displays a generic error when we cannot create a GtkSearchEngine.
8936 * It would be better if _gtk_search_engine_new() gave us a GError
8937 * with a better message, but it doesn't do that right now.
8940 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8942 error_message (impl,
8943 _("Could not start the search process"),
8944 _("The program was not able to create a connection to the indexer "
8945 "daemon. Please make sure it is running."));
8949 search_engine_error_cb (GtkSearchEngine *engine,
8950 const gchar *message,
8953 GtkFileChooserDefault *impl;
8955 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8957 search_stop_searching (impl, TRUE);
8958 error_message (impl, _("Could not send the search request"), message);
8960 set_busy_cursor (impl, FALSE);
8963 /* Frees the data in the search_model */
8965 search_clear_model (GtkFileChooserDefault *impl,
8966 gboolean remove_from_treeview)
8968 if (!impl->search_model)
8971 g_object_unref (impl->search_model);
8972 impl->search_model = NULL;
8974 if (remove_from_treeview)
8975 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8978 /* Stops any ongoing searches; does not touch the search_model */
8980 search_stop_searching (GtkFileChooserDefault *impl,
8981 gboolean remove_query)
8983 if (remove_query && impl->search_query)
8985 g_object_unref (impl->search_query);
8986 impl->search_query = NULL;
8989 if (impl->search_engine)
8991 _gtk_search_engine_stop (impl->search_engine);
8993 g_object_unref (impl->search_engine);
8994 impl->search_engine = NULL;
8998 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9000 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9002 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9004 search_stop_searching (impl, FALSE);
9005 search_clear_model (impl, TRUE);
9007 gtk_widget_destroy (impl->search_hbox);
9008 impl->search_hbox = NULL;
9009 impl->search_entry = NULL;
9011 impl->operation_mode = OPERATION_MODE_BROWSE;
9012 path_bar_update (impl);
9014 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9015 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9017 gtk_widget_show (impl->location_button);
9019 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9020 gtk_widget_show (impl->location_entry_box);
9023 file_list_set_sort_column_ids (impl);
9026 /* Creates the search_model and puts it in the tree view */
9028 search_setup_model (GtkFileChooserDefault *impl)
9030 g_assert (impl->search_model == NULL);
9032 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9034 MODEL_COLUMN_TYPES);
9036 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9040 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9044 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9048 set_sort_column (impl);
9050 /* EB: setting the model here will make the hits list update feel
9051 * more "alive" than setting the model at the end of the search
9054 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9055 GTK_TREE_MODEL (impl->search_model));
9058 /* Creates a new query with the specified text and launches it */
9060 search_start_query (GtkFileChooserDefault *impl,
9061 const gchar *query_text)
9063 search_stop_searching (impl, FALSE);
9064 search_clear_model (impl, TRUE);
9065 search_setup_model (impl);
9066 set_busy_cursor (impl, TRUE);
9068 if (impl->search_engine == NULL)
9069 impl->search_engine = _gtk_search_engine_new ();
9071 if (!impl->search_engine)
9073 set_busy_cursor (impl, FALSE);
9074 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9078 if (!impl->search_query)
9080 impl->search_query = _gtk_query_new ();
9081 _gtk_query_set_text (impl->search_query, query_text);
9084 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9086 g_signal_connect (impl->search_engine, "hits-added",
9087 G_CALLBACK (search_engine_hits_added_cb), impl);
9088 g_signal_connect (impl->search_engine, "finished",
9089 G_CALLBACK (search_engine_finished_cb), impl);
9090 g_signal_connect (impl->search_engine, "error",
9091 G_CALLBACK (search_engine_error_cb), impl);
9093 _gtk_search_engine_start (impl->search_engine);
9096 /* Callback used when the user presses Enter while typing on the search
9097 * entry; starts the query
9100 search_entry_activate_cb (GtkEntry *entry,
9103 GtkFileChooserDefault *impl;
9106 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9108 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9109 if (strlen (text) == 0)
9112 /* reset any existing query object */
9113 if (impl->search_query)
9115 g_object_unref (impl->search_query);
9116 impl->search_query = NULL;
9119 search_start_query (impl, text);
9123 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9125 GDK_THREADS_ENTER ();
9127 g_source_destroy (impl->focus_entry_idle);
9128 impl->focus_entry_idle = NULL;
9130 if (impl->search_entry)
9131 gtk_widget_grab_focus (impl->search_entry);
9133 GDK_THREADS_LEAVE ();
9139 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9141 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9142 * pane, we get a selection-changed signal and we set up the search widgets.
9143 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9144 * the change to the selection. So, we need to re-focus the search entry
9145 * after the treeview has finished doing its work; we'll do that in an idle
9149 if (!impl->focus_entry_idle)
9150 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9153 /* Hides the path bar and creates the search entry */
9155 search_setup_widgets (GtkFileChooserDefault *impl)
9157 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9159 path_bar_update (impl);
9161 impl->search_entry = gtk_entry_new ();
9162 g_signal_connect (impl->search_entry, "activate",
9163 G_CALLBACK (search_entry_activate_cb),
9165 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9167 /* if there already is a query, restart it */
9168 if (impl->search_query)
9170 gchar *query = _gtk_query_get_text (impl->search_query);
9174 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9175 search_start_query (impl, query);
9181 g_object_unref (impl->search_query);
9182 impl->search_query = NULL;
9186 /* Box for search widgets */
9187 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9188 gtk_widget_show_all (impl->search_hbox);
9189 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9191 /* Hide the location widgets temporarily */
9193 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9194 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9196 gtk_widget_hide (impl->location_button);
9197 gtk_widget_hide (impl->location_entry_box);
9200 focus_search_entry_in_idle (impl);
9202 /* FMQ: hide the filter combo? */
9205 /* Sometimes we need to frob the selection in the shortcuts list manually */
9207 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
9209 GtkTreeSelection *selection;
9212 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
9214 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
9216 path = gtk_tree_path_new_from_indices (pos, -1);
9217 gtk_tree_selection_select_path (selection, path);
9218 gtk_tree_path_free (path);
9220 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
9223 /* Main entry point to the searching functions; this gets called when the user
9224 * activates the Search shortcut.
9227 search_activate (GtkFileChooserDefault *impl)
9229 OperationMode previous_mode;
9231 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9233 focus_search_entry_in_idle (impl);
9237 previous_mode = impl->operation_mode;
9238 impl->operation_mode = OPERATION_MODE_SEARCH;
9240 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
9242 operation_mode_stop (impl, previous_mode);
9244 g_assert (impl->search_hbox == NULL);
9245 g_assert (impl->search_entry == NULL);
9246 g_assert (impl->search_model == NULL);
9248 search_setup_widgets (impl);
9249 file_list_set_sort_column_ids (impl);
9253 * Recent files support
9256 /* Frees the data in the recent_model */
9258 recent_clear_model (GtkFileChooserDefault *impl,
9259 gboolean remove_from_treeview)
9261 if (!impl->recent_model)
9264 if (remove_from_treeview)
9265 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9267 g_object_unref (impl->recent_model);
9268 impl->recent_model = NULL;
9271 /* Stops any ongoing loading of the recent files list; does
9272 * not touch the recent_model
9275 recent_stop_loading (GtkFileChooserDefault *impl)
9277 if (impl->load_recent_id)
9279 g_source_remove (impl->load_recent_id);
9280 impl->load_recent_id = 0;
9284 /* Stops any pending load, clears the file list, and switches
9285 * back to OPERATION_MODE_BROWSE
9288 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9290 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9292 recent_stop_loading (impl);
9293 recent_clear_model (impl, TRUE);
9295 impl->operation_mode = OPERATION_MODE_BROWSE;
9296 path_bar_update (impl);
9298 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9299 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9301 gtk_widget_show (impl->location_button);
9303 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9304 gtk_widget_show (impl->location_entry_box);
9307 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9309 file_list_set_sort_column_ids (impl);
9313 recent_setup_model (GtkFileChooserDefault *impl)
9315 g_assert (impl->recent_model == NULL);
9317 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9319 MODEL_COLUMN_TYPES);
9321 _gtk_file_system_model_set_filter (impl->recent_model,
9322 impl->current_filter);
9323 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9327 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9331 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9335 set_sort_column (impl);
9340 GtkFileChooserDefault *impl;
9342 guint needs_sorting : 1;
9346 recent_idle_cleanup (gpointer data)
9348 RecentLoadData *load_data = data;
9349 GtkFileChooserDefault *impl = load_data->impl;
9351 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9352 GTK_TREE_MODEL (impl->recent_model));
9354 set_busy_cursor (impl, FALSE);
9356 impl->load_recent_id = 0;
9362 recent_sort_mru (gconstpointer a,
9365 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9366 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9368 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9372 get_recent_files_limit (GtkWidget *widget)
9374 GtkSettings *settings;
9377 if (gtk_widget_has_screen (widget))
9378 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9380 settings = gtk_settings_get_default ();
9382 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9387 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9389 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9395 limit = get_recent_files_limit (GTK_WIDGET (impl));
9399 for (l = items; l; l = l->next)
9401 GtkRecentInfo *info = l->data;
9404 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9405 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9408 g_object_unref (file);
9411 if (limit != -1 && n >= limit)
9417 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9422 folders = _gtk_file_chooser_extract_recent_folders (items);
9424 for (l = folders; l; l = l->next)
9426 GFile *folder = l->data;
9428 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9433 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9434 g_list_free (folders);
9438 recent_idle_load (gpointer data)
9440 RecentLoadData *load_data = data;
9441 GtkFileChooserDefault *impl = load_data->impl;
9443 if (!impl->recent_manager)
9446 /* first iteration: load all the items */
9447 if (!load_data->items)
9449 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9450 if (!load_data->items)
9453 load_data->needs_sorting = TRUE;
9458 /* second iteration: MRU sorting and clamping, and populating the model */
9459 if (load_data->needs_sorting)
9461 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9463 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9464 populate_model_with_recent_items (impl, load_data->items);
9466 populate_model_with_folders (impl, load_data->items);
9468 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9469 g_list_free (load_data->items);
9470 load_data->items = NULL;
9477 recent_start_loading (GtkFileChooserDefault *impl)
9479 RecentLoadData *load_data;
9481 recent_stop_loading (impl);
9482 recent_clear_model (impl, TRUE);
9483 recent_setup_model (impl);
9484 set_busy_cursor (impl, TRUE);
9486 g_assert (impl->load_recent_id == 0);
9488 load_data = g_new (RecentLoadData, 1);
9489 load_data->impl = impl;
9490 load_data->items = NULL;
9491 load_data->needs_sorting = TRUE;
9493 /* begin lazy loading the recent files into the model */
9494 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9497 recent_idle_cleanup);
9501 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9511 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9512 *list = g_slist_prepend (*list, file);
9515 /* Constructs a list of the selected paths in recent files mode */
9517 recent_get_selected_files (GtkFileChooserDefault *impl)
9520 GtkTreeSelection *selection;
9524 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9525 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9526 result = g_slist_reverse (result);
9531 /* Called from ::should_respond(). We return whether there are selected
9532 * files in the recent files list.
9535 recent_should_respond (GtkFileChooserDefault *impl)
9537 GtkTreeSelection *selection;
9539 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9541 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9542 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9545 /* Hide the location widgets temporarily */
9547 recent_hide_entry (GtkFileChooserDefault *impl)
9549 path_bar_update (impl);
9551 /* Hide the location widgets temporarily */
9552 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9553 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9555 gtk_widget_hide (impl->location_button);
9556 gtk_widget_hide (impl->location_entry_box);
9560 /* Main entry point to the recent files functions; this gets called when
9561 * the user activates the Recently Used shortcut.
9564 recent_activate (GtkFileChooserDefault *impl)
9566 OperationMode previous_mode;
9568 if (impl->operation_mode == OPERATION_MODE_RECENT)
9571 previous_mode = impl->operation_mode;
9572 impl->operation_mode = OPERATION_MODE_RECENT;
9574 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
9576 operation_mode_stop (impl, previous_mode);
9578 recent_hide_entry (impl);
9580 file_list_set_sort_column_ids (impl);
9581 recent_start_loading (impl);
9585 set_current_filter (GtkFileChooserDefault *impl,
9586 GtkFileFilter *filter)
9588 if (impl->current_filter != filter)
9592 /* NULL filters are allowed to reset to non-filtered status
9594 filter_index = g_slist_index (impl->filters, filter);
9595 if (impl->filters && filter && filter_index < 0)
9598 if (impl->current_filter)
9599 g_object_unref (impl->current_filter);
9600 impl->current_filter = filter;
9601 if (impl->current_filter)
9603 g_object_ref_sink (impl->current_filter);
9607 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9610 if (impl->browse_files_model)
9611 install_list_model_filter (impl);
9613 if (impl->search_model)
9614 _gtk_file_system_model_set_filter (impl->search_model, filter);
9616 if (impl->recent_model)
9617 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9619 g_object_notify (G_OBJECT (impl), "filter");
9624 filter_combo_changed (GtkComboBox *combo_box,
9625 GtkFileChooserDefault *impl)
9627 gint new_index = gtk_combo_box_get_active (combo_box);
9628 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9630 set_current_filter (impl, new_filter);
9634 check_preview_change (GtkFileChooserDefault *impl)
9636 GtkTreePath *cursor_path;
9638 char *new_display_name;
9639 GtkTreeModel *model;
9641 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9642 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9647 gtk_tree_model_get_iter (model, &iter, cursor_path);
9648 gtk_tree_model_get (model, &iter,
9649 MODEL_COL_FILE, &new_file,
9650 MODEL_COL_NAME, &new_display_name,
9653 gtk_tree_path_free (cursor_path);
9658 new_display_name = NULL;
9661 if (new_file != impl->preview_file &&
9662 !(new_file && impl->preview_file &&
9663 g_file_equal (new_file, impl->preview_file)))
9665 if (impl->preview_file)
9667 g_object_unref (impl->preview_file);
9668 g_free (impl->preview_display_name);
9673 impl->preview_file = new_file;
9674 impl->preview_display_name = new_display_name;
9678 impl->preview_file = NULL;
9679 impl->preview_display_name = NULL;
9680 g_free (new_display_name);
9683 if (impl->use_preview_label && impl->preview_label)
9684 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9686 g_signal_emit_by_name (impl, "update-preview");
9691 g_object_unref (new_file);
9693 g_free (new_display_name);
9698 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9699 GtkFileSystemVolume *volume,
9700 const GError *error,
9704 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9705 GtkFileChooserDefault *impl = data;
9707 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9710 impl->shortcuts_activate_iter_cancellable = NULL;
9712 set_busy_cursor (impl, FALSE);
9719 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9723 name = _gtk_file_system_volume_get_display_name (volume);
9724 msg = g_strdup_printf (_("Could not mount %s"), name);
9726 error_message (impl, msg, error->message);
9735 file = _gtk_file_system_volume_get_root (volume);
9738 change_folder_and_display_error (impl, file, FALSE);
9739 g_object_unref (file);
9743 g_object_unref (impl);
9744 g_object_unref (cancellable);
9748 /* Activates a volume by mounting it if necessary and then switching to its
9752 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9753 GtkFileSystemVolume *volume)
9757 switch (impl->operation_mode)
9759 case OPERATION_MODE_BROWSE:
9761 case OPERATION_MODE_SEARCH:
9762 search_switch_to_browse_mode (impl);
9764 case OPERATION_MODE_RECENT:
9765 recent_switch_to_browse_mode (impl);
9769 /* We ref the file chooser since volume_mount() may run a main loop, and the
9770 * user could close the file chooser window in the meantime.
9772 g_object_ref (impl);
9774 if (!_gtk_file_system_volume_is_mounted (volume))
9776 GMountOperation *mount_op;
9778 set_busy_cursor (impl, TRUE);
9780 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9781 impl->shortcuts_activate_iter_cancellable =
9782 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9783 shortcuts_activate_volume_mount_cb,
9784 g_object_ref (impl));
9785 g_object_unref (mount_op);
9789 file = _gtk_file_system_volume_get_root (volume);
9792 change_folder_and_display_error (impl, file, FALSE);
9793 g_object_unref (file);
9797 g_object_unref (impl);
9800 /* Opens the folder or volume at the specified iter in the shortcuts model */
9801 struct ShortcutsActivateData
9803 GtkFileChooserDefault *impl;
9808 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9810 const GError *error,
9813 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9814 struct ShortcutsActivateData *data = user_data;
9816 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9819 data->impl->shortcuts_activate_iter_cancellable = NULL;
9824 if (!error && _gtk_file_info_consider_as_directory (info))
9825 change_folder_and_display_error (data->impl, data->file, FALSE);
9827 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9832 g_object_unref (data->impl);
9833 g_object_unref (data->file);
9836 g_object_unref (cancellable);
9840 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9841 GtkFileSystemVolume *volume,
9842 const GError *error,
9845 struct ShortcutsActivateData *data = user_data;
9849 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9851 g_object_unref (data->impl);
9852 g_object_unref (data->file);
9858 data->impl->shortcuts_activate_iter_cancellable =
9859 _gtk_file_system_get_info (data->impl->file_system, data->file,
9861 shortcuts_activate_get_info_cb, data);
9864 _gtk_file_system_volume_unref (volume);
9868 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9872 ShortcutType shortcut_type;
9874 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9875 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9876 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9877 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9879 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9880 SHORTCUTS_COL_DATA, &col_data,
9881 SHORTCUTS_COL_TYPE, &shortcut_type,
9884 if (impl->shortcuts_activate_iter_cancellable)
9886 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9887 impl->shortcuts_activate_iter_cancellable = NULL;
9890 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9892 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9894 GtkFileSystemVolume *volume;
9898 shortcuts_activate_volume (impl, volume);
9900 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9902 struct ShortcutsActivateData *data;
9903 GtkFileSystemVolume *volume;
9905 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9907 data = g_new0 (struct ShortcutsActivateData, 1);
9908 data->impl = g_object_ref (impl);
9909 data->file = g_object_ref (col_data);
9911 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9913 GMountOperation *mount_operation;
9914 GtkWidget *toplevel;
9916 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9918 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9920 impl->shortcuts_activate_iter_cancellable =
9921 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9923 shortcuts_activate_mount_enclosing_volume,
9928 impl->shortcuts_activate_iter_cancellable =
9929 _gtk_file_system_get_info (impl->file_system, data->file,
9931 shortcuts_activate_get_info_cb, data);
9934 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9936 search_activate (impl);
9938 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9940 recent_activate (impl);
9944 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9946 shortcuts_key_press_event_cb (GtkWidget *widget,
9948 GtkFileChooserDefault *impl)
9952 modifiers = gtk_accelerator_get_default_mod_mask ();
9954 if (key_is_left_or_right (event))
9956 gtk_widget_grab_focus (impl->browse_files_tree_view);
9960 if ((event->keyval == GDK_KEY_BackSpace
9961 || event->keyval == GDK_KEY_Delete
9962 || event->keyval == GDK_KEY_KP_Delete)
9963 && (event->state & modifiers) == 0)
9965 remove_selected_bookmarks (impl);
9969 if ((event->keyval == GDK_KEY_F2)
9970 && (event->state & modifiers) == 0)
9972 rename_selected_bookmark (impl);
9980 shortcuts_select_func (GtkTreeSelection *selection,
9981 GtkTreeModel *model,
9983 gboolean path_currently_selected,
9986 GtkFileChooserDefault *impl = data;
9987 GtkTreeIter filter_iter;
9988 ShortcutType shortcut_type;
9990 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9991 g_assert_not_reached ();
9993 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9995 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9999 list_select_func (GtkTreeSelection *selection,
10000 GtkTreeModel *model,
10002 gboolean path_currently_selected,
10005 GtkFileChooserDefault *impl = data;
10007 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10008 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10011 gboolean is_folder;
10013 if (!gtk_tree_model_get_iter (model, &iter, path))
10015 gtk_tree_model_get (model, &iter,
10016 MODEL_COL_IS_FOLDER, &is_folder,
10026 list_selection_changed (GtkTreeSelection *selection,
10027 GtkFileChooserDefault *impl)
10029 /* See if we are in the new folder editable row for Save mode */
10030 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10031 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10034 gboolean had_selection;
10036 info = get_selected_file_info_from_file_list (impl, &had_selection);
10037 if (!had_selection)
10038 goto out; /* normal processing */
10041 return; /* We are on the editable row for New Folder */
10046 if (impl->location_entry)
10047 update_chooser_entry (impl);
10049 path_bar_update (impl);
10051 check_preview_change (impl);
10052 bookmarks_check_add_sensitivity (impl);
10054 g_signal_emit_by_name (impl, "selection-changed", 0);
10057 /* Callback used when a row in the file list is activated */
10059 list_row_activated (GtkTreeView *tree_view,
10061 GtkTreeViewColumn *column,
10062 GtkFileChooserDefault *impl)
10066 GtkTreeModel *model;
10067 gboolean is_folder;
10069 model = gtk_tree_view_get_model (tree_view);
10071 if (!gtk_tree_model_get_iter (model, &iter, path))
10074 gtk_tree_model_get (model, &iter,
10075 MODEL_COL_FILE, &file,
10076 MODEL_COL_IS_FOLDER, &is_folder,
10079 if (is_folder && file)
10081 change_folder_and_display_error (impl, file, FALSE);
10082 g_object_unref (file);
10086 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10087 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10088 g_signal_emit_by_name (impl, "file-activated");
10093 g_object_unref (file);
10097 path_bar_clicked (GtkPathBar *path_bar,
10100 gboolean child_is_hidden,
10101 GtkFileChooserDefault *impl)
10104 pending_select_files_add (impl, child_file);
10106 if (!change_folder_and_display_error (impl, file, FALSE))
10109 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10110 * show hidden files so that ".baz" appears in the file list, as it will still
10111 * be shown in the path bar: "/foo/[bar]/.baz"
10113 if (child_is_hidden)
10114 g_object_set (impl, "show-hidden", TRUE, NULL);
10118 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10120 GtkTreeViewColumn *column;
10121 GtkCellRenderer *renderer;
10122 GList *walk, *list;
10123 gboolean always_sensitive;
10125 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10126 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10128 /* Keep the following column numbers in sync with create_file_list() */
10131 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10132 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10133 for (walk = list; walk; walk = walk->next)
10135 renderer = walk->data;
10136 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10138 gtk_tree_view_column_set_attributes (column, renderer,
10139 "pixbuf", MODEL_COL_PIXBUF,
10144 gtk_tree_view_column_set_attributes (column, renderer,
10145 "text", MODEL_COL_NAME,
10146 "ellipsize", MODEL_COL_ELLIPSIZE,
10149 if (always_sensitive)
10150 g_object_set (renderer, "sensitive", TRUE, NULL);
10152 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10154 g_list_free (list);
10157 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10158 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10159 renderer = list->data;
10160 gtk_tree_view_column_set_attributes (column, renderer,
10161 "text", MODEL_COL_SIZE_TEXT,
10163 if (always_sensitive)
10164 g_object_set (renderer, "sensitive", TRUE, NULL);
10166 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10167 g_list_free (list);
10170 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10171 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10172 renderer = list->data;
10173 gtk_tree_view_column_set_attributes (column, renderer,
10174 "text", MODEL_COL_MTIME_TEXT,
10176 if (always_sensitive)
10177 g_object_set (renderer, "sensitive", TRUE, NULL);
10179 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10180 g_list_free (list);
10184 _gtk_file_chooser_default_new (void)
10186 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10190 location_set_user_text (GtkFileChooserDefault *impl,
10193 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10194 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10198 location_popup_handler (GtkFileChooserDefault *impl,
10201 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10203 GtkWidget *widget_to_focus;
10205 /* This will give us the location widgets back */
10206 switch (impl->operation_mode)
10208 case OPERATION_MODE_SEARCH:
10209 search_switch_to_browse_mode (impl);
10211 case OPERATION_MODE_RECENT:
10212 recent_switch_to_browse_mode (impl);
10214 case OPERATION_MODE_BROWSE:
10215 g_assert_not_reached ();
10219 if (impl->current_folder)
10220 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10222 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10223 widget_to_focus = impl->browse_files_tree_view;
10225 widget_to_focus = impl->location_entry;
10227 gtk_widget_grab_focus (widget_to_focus);
10231 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10232 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10234 LocationMode new_mode;
10238 /* since the user typed something, we unconditionally want to turn on the entry */
10239 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10241 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10242 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10243 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10244 new_mode = LOCATION_MODE_PATH_BAR;
10247 g_assert_not_reached ();
10251 location_mode_set (impl, new_mode, TRUE);
10252 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10255 location_set_user_text (impl, path);
10258 location_entry_set_initial_text (impl);
10259 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10263 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10264 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10266 gtk_widget_grab_focus (impl->location_entry);
10268 location_set_user_text (impl, path);
10271 g_assert_not_reached ();
10274 /* Handler for the "up-folder" keybinding signal */
10276 up_folder_handler (GtkFileChooserDefault *impl)
10278 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10281 /* Handler for the "down-folder" keybinding signal */
10283 down_folder_handler (GtkFileChooserDefault *impl)
10285 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10288 /* Switches to the shortcut in the specified index */
10290 switch_to_shortcut (GtkFileChooserDefault *impl,
10295 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10296 g_assert_not_reached ();
10298 shortcuts_activate_iter (impl, &iter);
10301 /* Handler for the "home-folder" keybinding signal */
10303 home_folder_handler (GtkFileChooserDefault *impl)
10305 if (impl->has_home)
10306 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10309 /* Handler for the "desktop-folder" keybinding signal */
10311 desktop_folder_handler (GtkFileChooserDefault *impl)
10313 if (impl->has_desktop)
10314 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10317 /* Handler for the "search-shortcut" keybinding signal */
10319 search_shortcut_handler (GtkFileChooserDefault *impl)
10321 if (impl->has_search)
10323 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10325 /* we want the entry widget to grab the focus the first
10326 * time, not the browse_files_tree_view widget.
10328 if (impl->search_entry)
10329 gtk_widget_grab_focus (impl->search_entry);
10333 /* Handler for the "recent-shortcut" keybinding signal */
10335 recent_shortcut_handler (GtkFileChooserDefault *impl)
10337 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10341 quick_bookmark_handler (GtkFileChooserDefault *impl,
10342 gint bookmark_index)
10347 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10350 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10352 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10353 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10356 gtk_tree_path_free (path);
10358 switch_to_shortcut (impl, bookmark_pos);
10362 show_hidden_handler (GtkFileChooserDefault *impl)
10364 g_object_set (impl,
10365 "show-hidden", !impl->show_hidden,
10370 /* Drag and drop interfaces */
10373 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10378 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10380 model->impl = NULL;
10383 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10385 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10388 ShortcutsPaneModelFilter *model;
10392 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10394 pos = *gtk_tree_path_get_indices (path);
10395 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10397 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10400 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10404 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10406 GtkSelectionData *selection_data)
10413 /* Fill the GtkTreeDragSourceIface vtable */
10415 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10417 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10418 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10422 /* Fill the GtkTreeDragDestIface vtable */
10424 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10426 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10427 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10431 static GtkTreeModel *
10432 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10433 GtkTreeModel *child_model,
10436 ShortcutsPaneModelFilter *model;
10438 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10439 "child-model", child_model,
10440 "virtual-root", root,
10443 model->impl = impl;
10445 return GTK_TREE_MODEL (model);