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 "gtkalignment.h"
27 #include "gtkbindings.h"
28 #include "gtkcelllayout.h"
29 #include "gtkcellrendererpixbuf.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcheckmenuitem.h"
32 #include "gtkclipboard.h"
33 #include "gtkcomboboxtext.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystem.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkmountoperation.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkrecentfilter.h"
59 #include "gtkrecentmanager.h"
60 #include "gtkscrolledwindow.h"
61 #include "gtkseparatormenuitem.h"
62 #include "gtksizegroup.h"
63 #include "gtksizerequest.h"
66 #include "gtktoolbar.h"
67 #include "gtktoolbutton.h"
68 #include "gtktooltip.h"
69 #include "gtktreednd.h"
70 #include "gtktreeprivate.h"
71 #include "gtktreeselection.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 GtkVBoxClass 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_LOCATION_MODE "location-mode"
254 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
255 #define SETTINGS_KEY_EXPAND_FOLDERS "expand-folders"
256 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
257 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
258 #define SETTINGS_KEY_SORT_ORDER "sort-order"
259 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
260 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
262 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
263 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
265 static GObject* gtk_file_chooser_default_constructor (GType type,
266 guint n_construct_properties,
267 GObjectConstructParam *construct_params);
268 static void gtk_file_chooser_default_finalize (GObject *object);
269 static void gtk_file_chooser_default_set_property (GObject *object,
273 static void gtk_file_chooser_default_get_property (GObject *object,
277 static void gtk_file_chooser_default_dispose (GObject *object);
278 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
279 static void gtk_file_chooser_default_realize (GtkWidget *widget);
280 static void gtk_file_chooser_default_map (GtkWidget *widget);
281 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
282 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
283 GtkWidget *previous_toplevel);
284 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
285 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
286 GdkScreen *previous_screen);
287 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
296 gboolean clear_entry,
298 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
299 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
301 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
308 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
309 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
310 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
316 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
322 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
324 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
326 gint *default_height);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl,
332 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
333 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
334 static void up_folder_handler (GtkFileChooserDefault *impl);
335 static void down_folder_handler (GtkFileChooserDefault *impl);
336 static void home_folder_handler (GtkFileChooserDefault *impl);
337 static void desktop_folder_handler (GtkFileChooserDefault *impl);
338 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
339 gint bookmark_index);
340 static void show_hidden_handler (GtkFileChooserDefault *impl);
341 static void search_shortcut_handler (GtkFileChooserDefault *impl);
342 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
343 static void update_appearance (GtkFileChooserDefault *impl);
345 static void set_current_filter (GtkFileChooserDefault *impl,
346 GtkFileFilter *filter);
347 static void check_preview_change (GtkFileChooserDefault *impl);
349 static void filter_combo_changed (GtkComboBox *combo_box,
350 GtkFileChooserDefault *impl);
352 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
354 GtkFileChooserDefault *impl);
356 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
359 gboolean path_currently_selected,
361 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
363 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
365 static int shortcuts_get_index (GtkFileChooserDefault *impl,
366 ShortcutsIndex where);
367 static int shortcut_find_position (GtkFileChooserDefault *impl,
370 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
372 static gboolean list_select_func (GtkTreeSelection *selection,
375 gboolean path_currently_selected,
378 static void list_selection_changed (GtkTreeSelection *tree_selection,
379 GtkFileChooserDefault *impl);
380 static void list_row_activated (GtkTreeView *tree_view,
382 GtkTreeViewColumn *column,
383 GtkFileChooserDefault *impl);
385 static void path_bar_clicked (GtkPathBar *path_bar,
388 gboolean child_is_hidden,
389 GtkFileChooserDefault *impl);
391 static void add_bookmark_button_clicked_cb (GtkButton *button,
392 GtkFileChooserDefault *impl);
393 static void remove_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
395 static void save_folder_combo_changed_cb (GtkComboBox *combo,
396 GtkFileChooserDefault *impl);
398 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
400 static void load_remove_timer (GtkFileChooserDefault *impl);
401 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
403 static void location_button_toggled_cb (GtkToggleButton *toggle,
404 GtkFileChooserDefault *impl);
405 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
407 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
408 gboolean remove_from_treeview);
409 static void search_stop_searching (GtkFileChooserDefault *impl,
410 gboolean remove_query);
411 static void search_clear_model (GtkFileChooserDefault *impl,
412 gboolean remove_from_treeview);
413 static gboolean search_should_respond (GtkFileChooserDefault *impl);
414 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
415 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
416 static void search_entry_activate_cb (GtkEntry *entry,
418 static void settings_load (GtkFileChooserDefault *impl);
420 static void recent_stop_loading (GtkFileChooserDefault *impl);
421 static void recent_clear_model (GtkFileChooserDefault *impl,
422 gboolean remove_from_treeview);
423 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
424 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
425 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
426 static void set_file_system_backend (GtkFileChooserDefault *impl);
427 static void unset_file_system_backend (GtkFileChooserDefault *impl);
433 /* Drag and drop interface declarations */
436 GtkTreeModelFilter parent;
438 GtkFileChooserDefault *impl;
439 } ShortcutsPaneModelFilter;
442 GtkTreeModelFilterClass parent_class;
443 } ShortcutsPaneModelFilterClass;
445 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
446 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
448 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
450 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
451 _shortcuts_pane_model_filter,
452 GTK_TYPE_TREE_MODEL_FILTER,
453 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
454 shortcuts_pane_model_filter_drag_source_iface_init))
456 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
457 GtkTreeModel *child_model,
462 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
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_box_set_spacing (GTK_BOX (impl), 12);
745 set_file_system_backend (impl);
747 profile_end ("end", NULL);
750 /* Frees the data columns for the specified iter in the shortcuts model*/
752 shortcuts_free_row_data (GtkFileChooserDefault *impl,
756 ShortcutType shortcut_type;
757 GCancellable *cancellable;
759 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
760 SHORTCUTS_COL_DATA, &col_data,
761 SHORTCUTS_COL_TYPE, &shortcut_type,
762 SHORTCUTS_COL_CANCELLABLE, &cancellable,
766 g_cancellable_cancel (cancellable);
768 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
769 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
773 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
775 GtkFileSystemVolume *volume;
778 _gtk_file_system_volume_unref (volume);
780 if (shortcut_type == SHORTCUT_TYPE_FILE)
785 g_object_unref (file);
789 /* Frees all the data columns in the shortcuts model */
791 shortcuts_free (GtkFileChooserDefault *impl)
795 if (!impl->shortcuts_model)
798 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
801 shortcuts_free_row_data (impl, &iter);
803 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
805 g_object_unref (impl->shortcuts_model);
806 impl->shortcuts_model = NULL;
810 pending_select_files_free (GtkFileChooserDefault *impl)
812 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
813 g_slist_free (impl->pending_select_files);
814 impl->pending_select_files = NULL;
818 pending_select_files_add (GtkFileChooserDefault *impl,
821 impl->pending_select_files =
822 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
826 gtk_file_chooser_default_finalize (GObject *object)
828 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
831 unset_file_system_backend (impl);
833 if (impl->shortcuts_pane_filter_model)
834 g_object_unref (impl->shortcuts_pane_filter_model);
836 if (impl->shortcuts_combo_filter_model)
837 g_object_unref (impl->shortcuts_combo_filter_model);
839 shortcuts_free (impl);
841 g_free (impl->browse_files_last_selected_name);
843 for (l = impl->filters; l; l = l->next)
845 GtkFileFilter *filter;
847 filter = GTK_FILE_FILTER (l->data);
848 g_object_unref (filter);
850 g_slist_free (impl->filters);
852 if (impl->current_filter)
853 g_object_unref (impl->current_filter);
855 if (impl->current_volume_file)
856 g_object_unref (impl->current_volume_file);
858 if (impl->current_folder)
859 g_object_unref (impl->current_folder);
861 if (impl->preview_file)
862 g_object_unref (impl->preview_file);
864 if (impl->browse_path_bar_size_group)
865 g_object_unref (impl->browse_path_bar_size_group);
867 /* Free all the Models we have */
868 stop_loading_and_clear_list_model (impl, FALSE);
869 search_clear_model (impl, FALSE);
870 recent_clear_model (impl, FALSE);
872 /* stopping the load above should have cleared this */
873 g_assert (impl->load_timeout_id == 0);
875 g_free (impl->preview_display_name);
877 g_free (impl->edited_new_text);
879 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
882 /* Shows an error dialog set as transient for the specified window */
884 error_message_with_parent (GtkWindow *parent,
890 dialog = gtk_message_dialog_new (parent,
891 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
896 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
899 if (parent && gtk_window_has_group (parent))
900 gtk_window_group_add_window (gtk_window_get_group (parent),
901 GTK_WINDOW (dialog));
903 gtk_dialog_run (GTK_DIALOG (dialog));
904 gtk_widget_destroy (dialog);
907 /* Returns a toplevel GtkWindow, or NULL if none */
909 get_toplevel (GtkWidget *widget)
913 toplevel = gtk_widget_get_toplevel (widget);
914 if (!gtk_widget_is_toplevel (toplevel))
917 return GTK_WINDOW (toplevel);
920 /* Shows an error dialog for the file chooser */
922 error_message (GtkFileChooserDefault *impl,
926 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
929 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
931 error_dialog (GtkFileChooserDefault *impl,
942 uri = g_file_get_uri (file);
943 text = g_strdup_printf (msg, uri);
944 error_message (impl, text, error->message);
947 g_error_free (error);
951 /* Displays an error message about not being able to get information for a file.
952 * Frees the GError as well.
955 error_getting_info_dialog (GtkFileChooserDefault *impl,
960 _("Could not retrieve information about the file"),
964 /* Shows an error dialog about not being able to add a bookmark */
966 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
971 _("Could not add a bookmark"),
975 /* Shows an error dialog about not being able to remove a bookmark */
977 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
982 _("Could not remove bookmark"),
986 /* Shows an error dialog about not being able to create a folder */
988 error_creating_folder_dialog (GtkFileChooserDefault *impl,
993 _("The folder could not be created"),
997 /* Shows an error about not being able to create a folder because a file with
998 * the same name is already there.
1001 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1006 _("The folder could not be created, as a file with the same "
1007 "name already exists. Try using a different name for the "
1008 "folder, or rename the file first."),
1013 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1019 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1020 _("You need to choose a valid filename."));
1023 _("Cannot create a file under %s as it is not a folder"),
1024 parent_file, error);
1027 /* Shows an error about not being able to select a folder because a file with
1028 * the same name is already there.
1031 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1035 _("You may only select folders. The item that you selected is not a folder; "
1036 "try using a different item."),
1040 /* Shows an error dialog about not being able to create a filename */
1042 error_building_filename_dialog (GtkFileChooserDefault *impl,
1045 error_dialog (impl, _("Invalid file name"),
1049 /* Shows an error dialog when we cannot switch to a folder */
1051 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1055 error_dialog (impl, _("The folder contents could not be displayed"),
1059 /* Changes folders, displaying an error dialog if this fails */
1061 change_folder_and_display_error (GtkFileChooserDefault *impl,
1063 gboolean clear_entry)
1068 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1070 /* We copy the path because of this case:
1072 * list_row_activated()
1073 * fetches path from model; path belongs to the model (*)
1074 * calls change_folder_and_display_error()
1075 * calls gtk_file_chooser_set_current_folder_file()
1076 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1080 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1083 error_changing_folder_dialog (impl, file, error);
1089 emit_default_size_changed (GtkFileChooserDefault *impl)
1091 profile_msg (" emit default-size-changed start", NULL);
1092 g_signal_emit_by_name (impl, "default-size-changed");
1093 profile_msg (" emit default-size-changed end", NULL);
1097 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1099 if (impl->use_preview_label)
1101 if (!impl->preview_label)
1103 impl->preview_label = gtk_label_new (impl->preview_display_name);
1104 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1105 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1106 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1107 gtk_widget_show (impl->preview_label);
1112 if (impl->preview_label)
1114 gtk_widget_destroy (impl->preview_label);
1115 impl->preview_label = NULL;
1119 if (impl->preview_widget_active && impl->preview_widget)
1120 gtk_widget_show (impl->preview_box);
1122 gtk_widget_hide (impl->preview_box);
1124 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1125 emit_default_size_changed (impl);
1129 set_preview_widget (GtkFileChooserDefault *impl,
1130 GtkWidget *preview_widget)
1132 if (preview_widget == impl->preview_widget)
1135 if (impl->preview_widget)
1136 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1137 impl->preview_widget);
1139 impl->preview_widget = preview_widget;
1140 if (impl->preview_widget)
1142 gtk_widget_show (impl->preview_widget);
1143 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1144 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1145 impl->preview_widget,
1146 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1149 update_preview_widget_visibility (impl);
1152 /* Renders a "Search" icon at an appropriate size for a tree view */
1154 render_search_icon (GtkFileChooserDefault *impl)
1156 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1160 render_recent_icon (GtkFileChooserDefault *impl)
1162 GtkIconTheme *theme;
1165 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1166 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1168 theme = gtk_icon_theme_get_default ();
1170 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1176 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1182 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1183 struct ReloadIconsData
1185 GtkFileChooserDefault *impl;
1186 GtkTreeRowReference *row_ref;
1190 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1192 const GError *error,
1198 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1199 struct ReloadIconsData *data = user_data;
1201 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1204 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1206 if (cancelled || error)
1209 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1211 path = gtk_tree_row_reference_get_path (data->row_ref);
1214 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1215 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1216 SHORTCUTS_COL_PIXBUF, pixbuf,
1218 gtk_tree_path_free (path);
1222 g_object_unref (pixbuf);
1225 gtk_tree_row_reference_free (data->row_ref);
1226 g_object_unref (data->impl);
1229 g_object_unref (cancellable);
1233 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1238 profile_start ("start", NULL);
1240 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1243 for (l = impl->reload_icon_cancellables; l; l = l->next)
1245 GCancellable *cancellable = G_CANCELLABLE (l->data);
1246 g_cancellable_cancel (cancellable);
1248 g_slist_free (impl->reload_icon_cancellables);
1249 impl->reload_icon_cancellables = NULL;
1254 ShortcutType shortcut_type;
1255 gboolean pixbuf_visible;
1258 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1259 SHORTCUTS_COL_DATA, &data,
1260 SHORTCUTS_COL_TYPE, &shortcut_type,
1261 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1267 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1269 GtkFileSystemVolume *volume;
1272 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1273 impl->icon_size, NULL);
1275 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1277 if (g_file_is_native (G_FILE (data)))
1280 struct ReloadIconsData *info;
1281 GtkTreePath *tree_path;
1282 GCancellable *cancellable;
1286 info = g_new0 (struct ReloadIconsData, 1);
1287 info->impl = g_object_ref (impl);
1288 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1289 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1290 gtk_tree_path_free (tree_path);
1292 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1294 shortcuts_reload_icons_get_info_cb,
1296 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1300 GtkIconTheme *icon_theme;
1302 /* Don't call get_info for remote paths to avoid latency and
1304 * If we switch to a better bookmarks file format (XBEL), we
1305 * should use mime info to get a better icon.
1307 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1308 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1309 impl->icon_size, 0, NULL);
1312 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1314 pixbuf = render_search_icon (impl);
1316 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1318 pixbuf = render_recent_icon (impl);
1321 gtk_list_store_set (impl->shortcuts_model, &iter,
1322 SHORTCUTS_COL_PIXBUF, pixbuf,
1326 g_object_unref (pixbuf);
1330 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1334 profile_end ("end", NULL);
1338 shortcuts_find_folder (GtkFileChooserDefault *impl,
1341 GtkTreeSelection *selection;
1345 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1347 g_assert (folder != NULL);
1348 pos = shortcut_find_position (impl, folder);
1351 gtk_tree_selection_unselect_all (selection);
1355 path = gtk_tree_path_new_from_indices (pos, -1);
1356 gtk_tree_selection_select_path (selection, path);
1357 gtk_tree_path_free (path);
1360 /* If a shortcut corresponds to the current folder, selects it */
1362 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1364 shortcuts_find_folder (impl, impl->current_folder);
1367 /* Removes the specified number of rows from the shortcuts list */
1369 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1375 path = gtk_tree_path_new_from_indices (start_row, -1);
1377 for (; n_rows; n_rows--)
1381 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1382 g_assert_not_reached ();
1384 shortcuts_free_row_data (impl, &iter);
1385 gtk_list_store_remove (impl->shortcuts_model, &iter);
1388 gtk_tree_path_free (path);
1392 shortcuts_update_count (GtkFileChooserDefault *impl,
1393 ShortcutsIndex type,
1398 case SHORTCUTS_HOME:
1400 impl->has_home = FALSE;
1402 impl->has_home = TRUE;
1405 case SHORTCUTS_DESKTOP:
1407 impl->has_desktop = FALSE;
1409 impl->has_desktop = TRUE;
1412 case SHORTCUTS_VOLUMES:
1413 impl->num_volumes += value;
1416 case SHORTCUTS_SHORTCUTS:
1417 impl->num_shortcuts += value;
1420 case SHORTCUTS_BOOKMARKS:
1421 impl->num_bookmarks += value;
1424 case SHORTCUTS_CURRENT_FOLDER:
1426 impl->shortcuts_current_folder_active = FALSE;
1428 impl->shortcuts_current_folder_active = TRUE;
1437 struct ShortcutsInsertRequest
1439 GtkFileChooserDefault *impl;
1443 GtkTreeRowReference *row_ref;
1444 ShortcutsIndex type;
1450 get_file_info_finished (GCancellable *cancellable,
1452 const GError *error,
1456 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1460 GCancellable *model_cancellable;
1461 struct ShortcutsInsertRequest *request = data;
1463 path = gtk_tree_row_reference_get_path (request->row_ref);
1465 /* Handle doesn't exist anymore in the model */
1468 pos = gtk_tree_path_get_indices (path)[0];
1469 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1471 gtk_tree_path_free (path);
1473 /* validate cancellable, else goto out */
1474 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1475 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1477 if (cancellable != model_cancellable)
1480 /* set the cancellable to NULL in the model (we unref later on) */
1481 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1482 SHORTCUTS_COL_CANCELLABLE, NULL,
1490 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1491 shortcuts_update_count (request->impl, request->type, -1);
1493 if (request->type == SHORTCUTS_HOME)
1497 home = g_file_new_for_path (g_get_home_dir ());
1498 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1499 g_object_unref (home);
1501 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1503 /* Remove the current folder separator */
1504 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1505 shortcuts_remove_rows (request->impl, separator_pos, 1);
1511 if (!request->label_copy)
1512 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1513 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1514 request->impl->icon_size);
1516 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1517 SHORTCUTS_COL_PIXBUF, pixbuf,
1518 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1519 SHORTCUTS_COL_NAME, request->label_copy,
1520 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1521 SHORTCUTS_COL_REMOVABLE, request->removable,
1524 if (request->impl->shortcuts_pane_filter_model)
1525 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1527 if (request->impl->shortcuts_combo_filter_model)
1528 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1530 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1531 request->impl->save_folder_combo != NULL)
1533 /* The current folder is updated via _activate_iter(), don't
1534 * have save_folder_combo_changed_cb() call _activate_iter()
1537 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1538 G_CALLBACK (save_folder_combo_changed_cb),
1541 if (request->impl->has_search)
1544 if (request->impl->has_recent)
1547 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1548 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1549 G_CALLBACK (save_folder_combo_changed_cb),
1554 g_object_unref (pixbuf);
1557 g_object_unref (request->impl);
1558 g_object_unref (request->file);
1559 gtk_tree_row_reference_free (request->row_ref);
1560 g_free (request->label_copy);
1563 g_object_unref (cancellable);
1566 /* FIXME: GtkFileSystem needs a function to split a remote path
1567 * into hostname and path components, or maybe just have a
1568 * gtk_file_system_path_get_display_name().
1570 * This function is also used in gtkfilechooserbutton.c
1573 _gtk_file_chooser_label_for_file (GFile *file)
1575 const gchar *path, *start, *end, *p;
1576 gchar *uri, *host, *label;
1578 uri = g_file_get_uri (file);
1580 start = strstr (uri, "://");
1584 path = strchr (start, '/');
1589 end = uri + strlen (uri);
1593 /* strip username */
1594 p = strchr (start, '@');
1598 p = strchr (start, ':');
1602 host = g_strndup (start, end - start);
1604 /* Translators: the first string is a path and the second string
1605 * is a hostname. Nautilus and the panel contain the same string
1608 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1614 label = g_strdup (uri);
1622 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1623 * inserts a volume. A position of -1 indicates the end of the tree.
1626 shortcuts_insert_file (GtkFileChooserDefault *impl,
1628 ShortcutType shortcut_type,
1629 GtkFileSystemVolume *volume,
1633 ShortcutsIndex type)
1636 GdkPixbuf *pixbuf = NULL;
1637 gpointer data = NULL;
1639 GtkIconTheme *icon_theme;
1641 profile_start ("start shortcut", NULL);
1643 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1646 label_copy = _gtk_file_system_volume_get_display_name (volume);
1647 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1648 impl->icon_size, NULL);
1650 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1652 if (g_file_is_native (file))
1654 struct ShortcutsInsertRequest *request;
1655 GCancellable *cancellable;
1658 request = g_new0 (struct ShortcutsInsertRequest, 1);
1659 request->impl = g_object_ref (impl);
1660 request->file = g_object_ref (file);
1661 request->name_only = TRUE;
1662 request->removable = removable;
1664 request->type = type;
1666 request->label_copy = g_strdup (label);
1669 gtk_list_store_append (impl->shortcuts_model, &iter);
1671 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1673 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1674 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1675 gtk_tree_path_free (p);
1677 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1678 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1679 get_file_info_finished, request);
1681 gtk_list_store_set (impl->shortcuts_model, &iter,
1682 SHORTCUTS_COL_DATA, g_object_ref (file),
1683 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1684 SHORTCUTS_COL_CANCELLABLE, cancellable,
1687 shortcuts_update_count (impl, type, 1);
1693 /* Don't call get_info for remote paths to avoid latency and
1696 data = g_object_ref (file);
1698 label_copy = g_strdup (label);
1700 label_copy = _gtk_file_chooser_label_for_file (file);
1702 /* If we switch to a better bookmarks file format (XBEL), we
1703 * should use mime info to get a better icon.
1705 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1706 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1707 impl->icon_size, 0, NULL);
1712 g_assert_not_reached ();
1718 gtk_list_store_append (impl->shortcuts_model, &iter);
1720 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1722 shortcuts_update_count (impl, type, 1);
1724 gtk_list_store_set (impl->shortcuts_model, &iter,
1725 SHORTCUTS_COL_PIXBUF, pixbuf,
1726 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1727 SHORTCUTS_COL_NAME, label_copy,
1728 SHORTCUTS_COL_DATA, data,
1729 SHORTCUTS_COL_TYPE, shortcut_type,
1730 SHORTCUTS_COL_REMOVABLE, removable,
1731 SHORTCUTS_COL_CANCELLABLE, NULL,
1734 if (impl->shortcuts_pane_filter_model)
1735 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1737 if (impl->shortcuts_combo_filter_model)
1738 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1740 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1742 /* The current folder is updated via _activate_iter(), don't
1743 * have save_folder_combo_changed_cb() call _activate_iter()
1746 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1748 if (impl->has_search)
1751 if (impl->has_recent)
1754 g_signal_handlers_block_by_func (impl->save_folder_combo,
1755 G_CALLBACK (save_folder_combo_changed_cb),
1758 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1759 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1760 G_CALLBACK (save_folder_combo_changed_cb),
1764 g_free (label_copy);
1767 g_object_unref (pixbuf);
1769 profile_end ("end", NULL);
1773 shortcuts_append_search (GtkFileChooserDefault *impl)
1778 pixbuf = render_search_icon (impl);
1780 gtk_list_store_append (impl->shortcuts_model, &iter);
1781 gtk_list_store_set (impl->shortcuts_model, &iter,
1782 SHORTCUTS_COL_PIXBUF, pixbuf,
1783 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1784 SHORTCUTS_COL_NAME, _("Search"),
1785 SHORTCUTS_COL_DATA, NULL,
1786 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1787 SHORTCUTS_COL_REMOVABLE, FALSE,
1791 g_object_unref (pixbuf);
1793 impl->has_search = TRUE;
1797 shortcuts_append_recent (GtkFileChooserDefault *impl)
1802 pixbuf = render_recent_icon (impl);
1804 gtk_list_store_append (impl->shortcuts_model, &iter);
1805 gtk_list_store_set (impl->shortcuts_model, &iter,
1806 SHORTCUTS_COL_PIXBUF, pixbuf,
1807 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1808 SHORTCUTS_COL_NAME, _("Recently Used"),
1809 SHORTCUTS_COL_DATA, NULL,
1810 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1811 SHORTCUTS_COL_REMOVABLE, FALSE,
1815 g_object_unref (pixbuf);
1817 impl->has_recent = TRUE;
1820 /* Appends an item for the user's home directory to the shortcuts model */
1822 shortcuts_append_home (GtkFileChooserDefault *impl)
1824 const char *home_path;
1827 profile_start ("start", NULL);
1829 home_path = g_get_home_dir ();
1830 if (home_path == NULL)
1832 profile_end ("end - no home directory!?", NULL);
1836 home = g_file_new_for_path (home_path);
1837 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1838 impl->has_home = TRUE;
1840 g_object_unref (home);
1842 profile_end ("end", NULL);
1845 /* Appends the ~/Desktop directory to the shortcuts model */
1847 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1852 profile_start ("start", NULL);
1854 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1855 /* "To disable a directory, point it to the homedir."
1856 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1858 if (!g_strcmp0 (name, g_get_home_dir ()))
1860 profile_end ("end", NULL);
1864 file = g_file_new_for_path (name);
1865 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1866 impl->has_desktop = TRUE;
1868 /* We do not actually pop up an error dialog if there is no desktop directory
1869 * because some people may really not want to have one.
1872 g_object_unref (file);
1874 profile_end ("end", NULL);
1877 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1879 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1886 profile_start ("start", NULL);
1888 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1891 for (; bookmarks; bookmarks = bookmarks->next)
1895 file = bookmarks->data;
1897 if (impl->local_only && !g_file_is_native (file))
1900 if (shortcut_find_position (impl, file) != -1)
1903 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1905 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1911 profile_end ("end", NULL);
1913 return num_inserted;
1916 /* Returns the index for the corresponding item in the shortcuts bar */
1918 shortcuts_get_index (GtkFileChooserDefault *impl,
1919 ShortcutsIndex where)
1925 if (where == SHORTCUTS_SEARCH)
1928 n += impl->has_search ? 1 : 0;
1930 if (where == SHORTCUTS_RECENT)
1933 n += impl->has_recent ? 1 : 0;
1935 if (where == SHORTCUTS_RECENT_SEPARATOR)
1938 n += impl->has_recent ? 1 : 0;
1940 if (where == SHORTCUTS_HOME)
1943 n += impl->has_home ? 1 : 0;
1945 if (where == SHORTCUTS_DESKTOP)
1948 n += impl->has_desktop ? 1 : 0;
1950 if (where == SHORTCUTS_VOLUMES)
1953 n += impl->num_volumes;
1955 if (where == SHORTCUTS_SHORTCUTS)
1958 n += impl->num_shortcuts;
1960 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1963 /* If there are no bookmarks there won't be a separator */
1964 n += (impl->num_bookmarks > 0) ? 1 : 0;
1966 if (where == SHORTCUTS_BOOKMARKS)
1969 n += impl->num_bookmarks;
1971 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1976 if (where == SHORTCUTS_CURRENT_FOLDER)
1979 g_assert_not_reached ();
1986 /* Adds all the file system volumes to the shortcuts model */
1988 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1993 gboolean old_changing_folders;
1995 profile_start ("start", NULL);
1997 old_changing_folders = impl->changing_folder;
1998 impl->changing_folder = TRUE;
2000 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2001 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2002 impl->num_volumes = 0;
2004 list = _gtk_file_system_list_volumes (impl->file_system);
2008 for (l = list; l; l = l->next)
2010 GtkFileSystemVolume *volume;
2014 if (impl->local_only)
2016 if (_gtk_file_system_volume_is_mounted (volume))
2019 gboolean base_is_native = TRUE;
2021 base_file = _gtk_file_system_volume_get_root (volume);
2022 if (base_file != NULL)
2024 base_is_native = g_file_is_native (base_file);
2025 g_object_unref (base_file);
2028 if (!base_is_native)
2033 shortcuts_insert_file (impl,
2035 SHORTCUT_TYPE_VOLUME,
2036 _gtk_file_system_volume_ref (volume),
2044 impl->num_volumes = n;
2045 g_slist_free (list);
2047 if (impl->shortcuts_pane_filter_model)
2048 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2050 if (impl->shortcuts_combo_filter_model)
2051 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2053 impl->changing_folder = old_changing_folders;
2055 profile_end ("end", NULL);
2058 /* Inserts a separator node in the shortcuts list */
2060 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2061 ShortcutsIndex where)
2065 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2066 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2067 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2069 gtk_list_store_insert (impl->shortcuts_model, &iter,
2070 shortcuts_get_index (impl, where));
2071 gtk_list_store_set (impl->shortcuts_model, &iter,
2072 SHORTCUTS_COL_PIXBUF, NULL,
2073 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2074 SHORTCUTS_COL_NAME, NULL,
2075 SHORTCUTS_COL_DATA, NULL,
2076 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2080 /* Updates the list of bookmarks */
2082 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2085 gboolean old_changing_folders;
2087 GFile *list_selected = NULL;
2088 GFile *combo_selected = NULL;
2089 ShortcutType shortcut_type;
2092 profile_start ("start", NULL);
2094 old_changing_folders = impl->changing_folder;
2095 impl->changing_folder = TRUE;
2097 if (shortcuts_get_selected (impl, &iter))
2099 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2101 SHORTCUTS_COL_DATA, &col_data,
2102 SHORTCUTS_COL_TYPE, &shortcut_type,
2105 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2106 list_selected = g_object_ref (col_data);
2109 if (impl->save_folder_combo &&
2110 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2113 GtkTreeIter child_iter;
2115 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2118 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2120 SHORTCUTS_COL_DATA, &col_data,
2121 SHORTCUTS_COL_TYPE, &shortcut_type,
2124 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2125 combo_selected = g_object_ref (col_data);
2128 if (impl->num_bookmarks > 0)
2129 shortcuts_remove_rows (impl,
2130 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2131 impl->num_bookmarks + 1);
2133 impl->num_bookmarks = 0;
2134 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2136 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2137 shortcuts_append_bookmarks (impl, bookmarks);
2138 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2139 g_slist_free (bookmarks);
2141 if (impl->num_bookmarks == 0)
2142 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2144 if (impl->shortcuts_pane_filter_model)
2145 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2147 if (impl->shortcuts_combo_filter_model)
2148 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2152 shortcuts_find_folder (impl, list_selected);
2153 g_object_unref (list_selected);
2160 pos = shortcut_find_position (impl, combo_selected);
2163 if (impl->has_search)
2166 if (impl->has_recent)
2169 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2172 g_object_unref (combo_selected);
2175 impl->changing_folder = old_changing_folders;
2177 profile_end ("end", NULL);
2180 /* Appends a separator and a row to the shortcuts list for the current folder */
2182 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2186 g_assert (!impl->shortcuts_current_folder_active);
2188 g_assert (impl->current_folder != NULL);
2190 pos = shortcut_find_position (impl, impl->current_folder);
2193 GtkFileSystemVolume *volume;
2197 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2200 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2202 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2204 base_file = _gtk_file_system_volume_get_root (volume);
2208 if (base_file && g_file_equal (base_file, impl->current_folder))
2209 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2211 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2214 g_object_unref (base_file);
2216 else if (impl->save_folder_combo != NULL)
2218 if (impl->has_search)
2221 if (impl->has_recent)
2222 pos -= 2; /* + separator */
2224 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2228 /* Updates the current folder row in the shortcuts model */
2230 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2234 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2236 if (impl->shortcuts_current_folder_active)
2238 shortcuts_remove_rows (impl, pos, 2);
2239 impl->shortcuts_current_folder_active = FALSE;
2242 shortcuts_add_current_folder (impl);
2245 /* Filter function used for the shortcuts filter model */
2247 shortcuts_pane_filter_cb (GtkTreeModel *model,
2251 GtkFileChooserDefault *impl;
2255 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2257 path = gtk_tree_model_get_path (model, iter);
2261 pos = *gtk_tree_path_get_indices (path);
2262 gtk_tree_path_free (path);
2264 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2267 /* Creates the list model for shortcuts */
2269 shortcuts_model_create (GtkFileChooserDefault *impl)
2271 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2272 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2273 GDK_TYPE_PIXBUF, /* pixbuf */
2274 G_TYPE_STRING, /* name */
2275 G_TYPE_POINTER, /* path or volume */
2276 G_TYPE_INT, /* ShortcutType */
2277 G_TYPE_BOOLEAN, /* removable */
2278 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2279 G_TYPE_POINTER); /* GCancellable */
2281 shortcuts_append_search (impl);
2283 if (impl->recent_manager)
2285 shortcuts_append_recent (impl);
2286 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2289 if (impl->file_system)
2291 shortcuts_append_home (impl);
2292 shortcuts_append_desktop (impl);
2293 shortcuts_add_volumes (impl);
2296 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2297 GTK_TREE_MODEL (impl->shortcuts_model),
2300 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2301 shortcuts_pane_filter_cb,
2306 /* Callback used when the "New Folder" button is clicked */
2308 new_folder_button_clicked (GtkButton *button,
2309 GtkFileChooserDefault *impl)
2314 if (!impl->browse_files_model)
2315 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2317 /* Prevent button from being clicked twice */
2318 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2320 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2322 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2323 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2324 path, impl->list_name_column,
2327 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2328 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2330 impl->list_name_column,
2333 gtk_tree_path_free (path);
2337 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2341 source = g_idle_source_new ();
2342 g_source_set_closure (source,
2343 g_cclosure_new_object (callback, G_OBJECT (impl)));
2344 g_source_attach (source, NULL);
2349 /* Idle handler for creating a new folder after editing its name cell, or for
2350 * canceling the editing.
2353 edited_idle_cb (GtkFileChooserDefault *impl)
2355 GDK_THREADS_ENTER ();
2357 g_source_destroy (impl->edited_idle);
2358 impl->edited_idle = NULL;
2360 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2361 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2363 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2365 if (impl->edited_new_text /* not cancelled? */
2366 && (strlen (impl->edited_new_text) != 0)
2367 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2369 GError *error = NULL;
2372 file = g_file_get_child_for_display_name (impl->current_folder,
2373 impl->edited_new_text,
2377 GError *error = NULL;
2379 if (g_file_make_directory (file, NULL, &error))
2380 change_folder_and_display_error (impl, file, FALSE);
2382 error_creating_folder_dialog (impl, file, error);
2384 g_object_unref (file);
2387 error_creating_folder_dialog (impl, file, error);
2389 g_free (impl->edited_new_text);
2390 impl->edited_new_text = NULL;
2393 GDK_THREADS_LEAVE ();
2399 queue_edited_idle (GtkFileChooserDefault *impl,
2400 const gchar *new_text)
2402 /* We create the folder in an idle handler so that we don't modify the tree
2406 if (!impl->edited_idle)
2407 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2409 g_free (impl->edited_new_text);
2410 impl->edited_new_text = g_strdup (new_text);
2413 /* Callback used from the text cell renderer when the new folder is named */
2415 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2417 const gchar *new_text,
2418 GtkFileChooserDefault *impl)
2420 /* work around bug #154921 */
2421 g_object_set (cell_renderer_text,
2422 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2423 queue_edited_idle (impl, new_text);
2426 /* Callback used from the text cell renderer when the new folder edition gets
2430 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2431 GtkFileChooserDefault *impl)
2433 /* work around bug #154921 */
2434 g_object_set (cell_renderer_text,
2435 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2436 queue_edited_idle (impl, NULL);
2439 /* Creates the widgets for the filter combo box */
2441 filter_create (GtkFileChooserDefault *impl)
2443 GtkCellRenderer *cell;
2446 impl->filter_combo = gtk_combo_box_text_new ();
2447 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2449 /* Get the combo's text renderer and set ellipsize parameters */
2450 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2454 g_object_set (G_OBJECT (cell),
2455 "ellipsize", PANGO_ELLIPSIZE_END,
2458 g_list_free (cells);
2460 g_signal_connect (impl->filter_combo, "changed",
2461 G_CALLBACK (filter_combo_changed), impl);
2463 gtk_widget_set_tooltip_text (impl->filter_combo,
2464 _("Select which types of files are shown"));
2466 return impl->filter_combo;
2470 toolbutton_new (GtkFileChooserDefault *impl,
2479 item = gtk_tool_button_new (NULL, NULL);
2480 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2481 gtk_widget_show (image);
2482 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2484 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2485 g_signal_connect (item, "clicked", callback, impl);
2488 gtk_widget_show (GTK_WIDGET (item));
2490 return GTK_WIDGET (item);
2493 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2495 shortcut_find_position (GtkFileChooserDefault *impl,
2500 int current_folder_separator_idx;
2502 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2505 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2508 /* FIXME: is this still needed? */
2509 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2513 for (i = 0; i < current_folder_separator_idx; i++)
2516 ShortcutType shortcut_type;
2518 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2519 SHORTCUTS_COL_DATA, &col_data,
2520 SHORTCUTS_COL_TYPE, &shortcut_type,
2525 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2527 GtkFileSystemVolume *volume;
2532 base_file = _gtk_file_system_volume_get_root (volume);
2534 exists = base_file && g_file_equal (file, base_file);
2537 g_object_unref (base_file);
2542 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2546 model_file = col_data;
2548 if (model_file && g_file_equal (model_file, file))
2553 if (i < current_folder_separator_idx - 1)
2555 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2556 g_assert_not_reached ();
2563 /* Tries to add a bookmark from a path name */
2565 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2571 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2573 if (shortcut_find_position (impl, file) != -1)
2577 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2579 error_adding_bookmark_dialog (impl, file, error);
2587 add_bookmark_foreach_cb (GtkTreeModel *model,
2592 GtkFileChooserDefault *impl;
2595 impl = (GtkFileChooserDefault *) data;
2597 gtk_tree_model_get (model, iter,
2598 MODEL_COL_FILE, &file,
2601 shortcuts_add_bookmark_from_file (impl, file, -1);
2603 g_object_unref (file);
2606 /* Adds a bookmark from the currently selected item in the file list */
2608 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2610 GtkTreeSelection *selection;
2612 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2614 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2615 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2617 gtk_tree_selection_selected_foreach (selection,
2618 add_bookmark_foreach_cb,
2622 /* Callback used when the "Add bookmark" button is clicked */
2624 add_bookmark_button_clicked_cb (GtkButton *button,
2625 GtkFileChooserDefault *impl)
2627 bookmarks_add_selected_folder (impl);
2630 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2631 * returns FALSE if no shortcut is selected.
2634 shortcuts_get_selected (GtkFileChooserDefault *impl,
2637 GtkTreeSelection *selection;
2638 GtkTreeIter parent_iter;
2640 if (!impl->browse_shortcuts_tree_view)
2643 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2645 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2648 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2654 /* Removes the selected bookmarks */
2656 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2664 if (!shortcuts_get_selected (impl, &iter))
2667 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2668 SHORTCUTS_COL_DATA, &col_data,
2669 SHORTCUTS_COL_REMOVABLE, &removable,
2675 g_assert (col_data != NULL);
2680 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2681 error_removing_bookmark_dialog (impl, file, error);
2684 /* Callback used when the "Remove bookmark" button is clicked */
2686 remove_bookmark_button_clicked_cb (GtkButton *button,
2687 GtkFileChooserDefault *impl)
2689 remove_selected_bookmarks (impl);
2692 struct selection_check_closure {
2693 GtkFileChooserDefault *impl;
2696 gboolean all_folders;
2699 /* Used from gtk_tree_selection_selected_foreach() */
2701 selection_check_foreach_cb (GtkTreeModel *model,
2706 struct selection_check_closure *closure;
2710 gtk_tree_model_get (model, iter,
2711 MODEL_COL_FILE, &file,
2712 MODEL_COL_IS_FOLDER, &is_folder,
2718 g_object_unref (file);
2721 closure->num_selected++;
2723 closure->all_folders = closure->all_folders && is_folder;
2724 closure->all_files = closure->all_files && !is_folder;
2727 /* Checks whether the selected items in the file list are all files or all folders */
2729 selection_check (GtkFileChooserDefault *impl,
2731 gboolean *all_files,
2732 gboolean *all_folders)
2734 struct selection_check_closure closure;
2735 GtkTreeSelection *selection;
2737 closure.impl = impl;
2738 closure.num_selected = 0;
2739 closure.all_files = TRUE;
2740 closure.all_folders = TRUE;
2742 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2743 gtk_tree_selection_selected_foreach (selection,
2744 selection_check_foreach_cb,
2747 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2750 *num_selected = closure.num_selected;
2753 *all_files = closure.all_files;
2756 *all_folders = closure.all_folders;
2759 struct get_selected_file_closure {
2760 GtkFileChooserDefault *impl;
2765 get_selected_file_foreach_cb (GtkTreeModel *model,
2770 struct get_selected_file_closure *closure = data;
2774 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2775 g_object_unref (closure->file);
2776 closure->file = NULL;
2779 gtk_tree_model_get (model, iter,
2780 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2784 /* Returns a selected path from the file list */
2786 get_selected_file (GtkFileChooserDefault *impl)
2788 struct get_selected_file_closure closure;
2789 GtkTreeSelection *selection;
2791 closure.impl = impl;
2792 closure.file = NULL;
2794 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2795 gtk_tree_selection_selected_foreach (selection,
2796 get_selected_file_foreach_cb,
2799 return closure.file;
2803 GtkFileChooserDefault *impl;
2805 } UpdateTooltipData;
2808 update_tooltip (GtkTreeModel *model,
2813 UpdateTooltipData *udata = data;
2815 if (udata->tip == NULL)
2817 gchar *display_name;
2819 gtk_tree_model_get (model, iter,
2820 MODEL_COL_NAME, &display_name,
2823 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2825 g_free (display_name);
2830 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2831 * if there are no selected items *and* the current folder is not in the
2832 * bookmarks list. De-sensitize the button otherwise.
2835 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2838 gboolean all_folders;
2842 selection_check (impl, &num_selected, NULL, &all_folders);
2844 if (num_selected == 0)
2845 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2846 else if (num_selected == 1)
2850 file = get_selected_file (impl);
2851 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2853 g_object_unref (file);
2856 active = all_folders;
2858 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2860 if (impl->browse_files_popup_menu_add_shortcut_item)
2861 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2862 (num_selected == 0) ? FALSE : active);
2866 if (num_selected == 0)
2867 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2868 else if (num_selected > 1)
2869 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2872 GtkTreeSelection *selection;
2873 UpdateTooltipData data;
2875 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2878 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2882 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2887 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2888 * bookmark row is selected in the shortcuts tree.
2891 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2894 gboolean removable = FALSE;
2898 if (shortcuts_get_selected (impl, &iter))
2900 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2901 SHORTCUTS_COL_REMOVABLE, &removable,
2902 SHORTCUTS_COL_NAME, &name,
2904 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2907 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2909 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2911 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2915 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2916 _("Remove the selected bookmark"));
2921 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2924 gboolean removable = FALSE;
2926 if (impl->browse_shortcuts_popup_menu == NULL)
2929 if (shortcuts_get_selected (impl, &iter))
2930 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2931 SHORTCUTS_COL_REMOVABLE, &removable,
2934 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2935 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2938 /* GtkWidget::drag-begin handler for the shortcuts list. */
2940 shortcuts_drag_begin_cb (GtkWidget *widget,
2941 GdkDragContext *context,
2942 GtkFileChooserDefault *impl)
2945 impl->shortcuts_drag_context = g_object_ref (context);
2950 /* Removes the idle handler for outside drags */
2952 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2954 if (!impl->shortcuts_drag_outside_idle)
2957 g_source_destroy (impl->shortcuts_drag_outside_idle);
2958 impl->shortcuts_drag_outside_idle = NULL;
2962 /* GtkWidget::drag-end handler for the shortcuts list. */
2964 shortcuts_drag_end_cb (GtkWidget *widget,
2965 GdkDragContext *context,
2966 GtkFileChooserDefault *impl)
2969 g_object_unref (impl->shortcuts_drag_context);
2971 shortcuts_cancel_drag_outside_idle (impl);
2973 if (!impl->shortcuts_drag_outside)
2976 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2978 impl->shortcuts_drag_outside = FALSE;
2982 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2984 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2985 GdkDragContext *context,
2986 GtkFileChooserDefault *impl)
2988 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2991 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2995 shortcuts_drag_leave_cb (GtkWidget *widget,
2996 GdkDragContext *context,
2998 GtkFileChooserDefault *impl)
3001 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3003 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
3007 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3009 GTK_TREE_VIEW_DROP_BEFORE);
3011 g_signal_stop_emission_by_name (widget, "drag-leave");
3014 /* Computes the appropriate row and position for dropping */
3016 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3020 GtkTreeViewDropPosition *pos)
3022 GtkTreeView *tree_view;
3023 GtkTreeViewColumn *column;
3027 int bookmarks_index;
3028 int header_height = 0;
3030 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3032 if (gtk_tree_view_get_headers_visible (tree_view))
3033 header_height = _gtk_tree_view_get_header_height (tree_view);
3035 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3037 if (!gtk_tree_view_get_path_at_pos (tree_view,
3045 row = bookmarks_index + impl->num_bookmarks - 1;
3046 *path = gtk_tree_path_new_from_indices (row, -1);
3047 *pos = GTK_TREE_VIEW_DROP_AFTER;
3051 row = *gtk_tree_path_get_indices (*path);
3052 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3053 gtk_tree_path_free (*path);
3055 if (row < bookmarks_index)
3057 row = bookmarks_index;
3058 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3060 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3062 row = bookmarks_index + impl->num_bookmarks - 1;
3063 *pos = GTK_TREE_VIEW_DROP_AFTER;
3067 if (cell_y < cell.height / 2)
3068 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3070 *pos = GTK_TREE_VIEW_DROP_AFTER;
3073 *path = gtk_tree_path_new_from_indices (row, -1);
3076 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3077 * implement the destination side of DnD by hand, due to limitations in
3078 * GtkTreeView's DnD API.
3081 shortcuts_drag_motion_cb (GtkWidget *widget,
3082 GdkDragContext *context,
3086 GtkFileChooserDefault *impl)
3089 GtkTreeViewDropPosition pos;
3090 GdkDragAction action;
3093 if (gtk_drag_get_source_widget (context) == widget)
3095 shortcuts_cancel_drag_outside_idle (impl);
3097 if (impl->shortcuts_drag_outside)
3099 shortcuts_drag_set_delete_cursor (impl, FALSE);
3100 impl->shortcuts_drag_outside = FALSE;
3105 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3106 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3107 action = GDK_ACTION_COPY;
3108 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3109 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3110 action = GDK_ACTION_MOVE;
3117 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3118 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3119 gtk_tree_path_free (path);
3123 g_signal_stop_emission_by_name (widget, "drag-motion");
3127 gdk_drag_status (context, action, time_);
3134 /* GtkWidget::drag-drop handler for the shortcuts list. */
3136 shortcuts_drag_drop_cb (GtkWidget *widget,
3137 GdkDragContext *context,
3141 GtkFileChooserDefault *impl)
3144 shortcuts_cancel_drag_outside_idle (impl);
3147 g_signal_stop_emission_by_name (widget, "drag-drop");
3151 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3153 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3154 GtkSelectionData *selection_data,
3160 uris = gtk_selection_data_get_uris (selection_data);
3164 for (i = 0; uris[i]; i++)
3170 file = g_file_new_for_uri (uri);
3172 if (shortcuts_add_bookmark_from_file (impl, file, position))
3175 g_object_unref (file);
3181 /* Reorders the selected bookmark to the specified position */
3183 shortcuts_reorder (GtkFileChooserDefault *impl,
3188 ShortcutType shortcut_type;
3191 int bookmarks_index;
3196 /* Get the selected path */
3198 if (!shortcuts_get_selected (impl, &iter))
3199 g_assert_not_reached ();
3201 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3202 old_position = *gtk_tree_path_get_indices (path);
3203 gtk_tree_path_free (path);
3205 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3206 old_position -= bookmarks_index;
3207 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3209 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3210 SHORTCUTS_COL_NAME, &name,
3211 SHORTCUTS_COL_DATA, &col_data,
3212 SHORTCUTS_COL_TYPE, &shortcut_type,
3214 g_assert (col_data != NULL);
3215 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3218 g_object_ref (file); /* removal below will free file, so we need a new ref */
3220 /* Remove the path from the old position and insert it in the new one */
3222 if (new_position > old_position)
3225 if (old_position == new_position)
3229 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3231 shortcuts_add_bookmark_from_file (impl, file, new_position);
3232 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3235 error_adding_bookmark_dialog (impl, file, error);
3239 g_object_unref (file);
3242 /* Callback used when we get the drag data for the bookmarks list. We add the
3243 * received URIs as bookmarks if they are folders.
3246 shortcuts_drag_data_received_cb (GtkWidget *widget,
3247 GdkDragContext *context,
3250 GtkSelectionData *selection_data,
3255 GtkFileChooserDefault *impl;
3256 GtkTreePath *tree_path;
3257 GtkTreeViewDropPosition tree_pos;
3260 int bookmarks_index;
3262 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3264 /* Compute position */
3266 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3268 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3269 position = *gtk_tree_path_get_indices (tree_path);
3270 gtk_tree_path_free (tree_path);
3272 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3275 g_assert (position >= bookmarks_index);
3276 position -= bookmarks_index;
3278 target = gtk_selection_data_get_target (selection_data);
3280 if (gtk_targets_include_uri (&target, 1))
3281 shortcuts_drop_uris (impl, selection_data, position);
3282 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3283 shortcuts_reorder (impl, position);
3285 g_signal_stop_emission_by_name (widget, "drag-data-received");
3288 /* Callback used to display a tooltip in the shortcuts tree */
3290 shortcuts_query_tooltip_cb (GtkWidget *widget,
3293 gboolean keyboard_mode,
3294 GtkTooltip *tooltip,
3295 GtkFileChooserDefault *impl)
3297 GtkTreeModel *model;
3300 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3308 ShortcutType shortcut_type;
3310 gtk_tree_model_get (model, &iter,
3311 SHORTCUTS_COL_DATA, &col_data,
3312 SHORTCUTS_COL_TYPE, &shortcut_type,
3315 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3317 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3319 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3324 file = G_FILE (col_data);
3325 parse_name = g_file_get_parse_name (file);
3327 gtk_tooltip_set_text (tooltip, parse_name);
3329 g_free (parse_name);
3333 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3337 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3347 /* Callback used when the selection in the shortcuts tree changes */
3349 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3350 GtkFileChooserDefault *impl)
3353 GtkTreeIter child_iter;
3355 bookmarks_check_remove_sensitivity (impl);
3356 shortcuts_check_popup_sensitivity (impl);
3358 if (impl->changing_folder)
3361 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3363 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3366 shortcuts_activate_iter (impl, &child_iter);
3371 shortcuts_row_separator_func (GtkTreeModel *model,
3375 ShortcutType shortcut_type;
3377 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3379 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3383 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3385 GtkFileChooserDefault *impl)
3389 /* don't screw up focus switching with Tab */
3390 if (event->keyval == GDK_KEY_Tab
3391 || event->keyval == GDK_KEY_KP_Tab
3392 || event->keyval == GDK_KEY_ISO_Left_Tab
3393 || event->length < 1)
3396 if (impl->location_entry)
3397 entry = impl->location_entry;
3398 else if (impl->search_entry)
3399 entry = impl->search_entry;
3405 gtk_widget_grab_focus (entry);
3406 return gtk_widget_event (entry, (GdkEvent *) event);
3412 /* Callback used when the file list's popup menu is detached */
3414 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3417 GtkFileChooserDefault *impl;
3419 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3420 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3422 impl->browse_shortcuts_popup_menu = NULL;
3423 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3424 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3428 remove_shortcut_cb (GtkMenuItem *item,
3429 GtkFileChooserDefault *impl)
3431 remove_selected_bookmarks (impl);
3434 /* Rename the selected bookmark */
3436 rename_selected_bookmark (GtkFileChooserDefault *impl)
3440 GtkTreeViewColumn *column;
3441 GtkCellRenderer *cell;
3444 if (shortcuts_get_selected (impl, &iter))
3446 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3447 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3448 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3449 cell = g_list_nth_data (renderers, 1);
3450 g_list_free (renderers);
3451 g_object_set (cell, "editable", TRUE, NULL);
3452 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3453 path, column, cell, TRUE);
3454 gtk_tree_path_free (path);
3459 rename_shortcut_cb (GtkMenuItem *item,
3460 GtkFileChooserDefault *impl)
3462 rename_selected_bookmark (impl);
3465 /* Constructs the popup menu for the file list if needed */
3467 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3471 if (impl->browse_shortcuts_popup_menu)
3474 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3475 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3476 impl->browse_shortcuts_tree_view,
3477 shortcuts_popup_menu_detach_cb);
3479 item = gtk_image_menu_item_new_with_label (_("Remove"));
3480 impl->browse_shortcuts_popup_menu_remove_item = item;
3481 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3482 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3483 g_signal_connect (item, "activate",
3484 G_CALLBACK (remove_shortcut_cb), impl);
3485 gtk_widget_show (item);
3486 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3488 item = gtk_menu_item_new_with_label (_("Rename..."));
3489 impl->browse_shortcuts_popup_menu_rename_item = item;
3490 g_signal_connect (item, "activate",
3491 G_CALLBACK (rename_shortcut_cb), impl);
3492 gtk_widget_show (item);
3493 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3497 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3499 shortcuts_build_popup_menu (impl);
3500 shortcuts_check_popup_sensitivity (impl);
3504 popup_position_func (GtkMenu *menu,
3508 gpointer user_data);
3511 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3512 GdkEventButton *event)
3514 shortcuts_update_popup_menu (impl);
3516 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3517 NULL, NULL, NULL, NULL,
3518 event->button, event->time);
3521 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3523 popup_position_func, impl->browse_shortcuts_tree_view,
3524 0, GDK_CURRENT_TIME);
3525 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3530 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3532 shortcuts_popup_menu_cb (GtkWidget *widget,
3533 GtkFileChooserDefault *impl)
3535 shortcuts_popup_menu (impl, NULL);
3539 /* Callback used when a button is pressed on the shortcuts list.
3540 * We trap button 3 to bring up a popup menu.
3543 shortcuts_button_press_event_cb (GtkWidget *widget,
3544 GdkEventButton *event,
3545 GtkFileChooserDefault *impl)
3547 static gboolean in_press = FALSE;
3553 if (event->button != 3)
3557 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3563 shortcuts_popup_menu (impl, event);
3568 shortcuts_edited (GtkCellRenderer *cell,
3571 GtkFileChooserDefault *impl)
3577 g_object_set (cell, "editable", FALSE, NULL);
3579 path = gtk_tree_path_new_from_string (path_string);
3580 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3581 g_assert_not_reached ();
3583 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3584 SHORTCUTS_COL_DATA, &shortcut,
3586 gtk_tree_path_free (path);
3588 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3592 shortcuts_editing_canceled (GtkCellRenderer *cell,
3593 GtkFileChooserDefault *impl)
3595 g_object_set (cell, "editable", FALSE, NULL);
3598 /* Creates the widgets for the shortcuts and bookmarks tree */
3600 shortcuts_list_create (GtkFileChooserDefault *impl)
3603 GtkTreeSelection *selection;
3604 GtkTreeViewColumn *column;
3605 GtkCellRenderer *renderer;
3607 /* Target types for dragging a row to/from the shortcuts list */
3608 const GtkTargetEntry tree_model_row_targets[] = {
3609 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3612 /* Scrolled window */
3614 swin = gtk_scrolled_window_new (NULL, NULL);
3615 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3616 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3617 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3619 gtk_widget_show (swin);
3623 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3624 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3625 #ifdef PROFILE_FILE_CHOOSER
3626 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3629 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3631 * 1. user brings up a SAVE dialog
3632 * 2. user clicks on a shortcut in the shortcuts pane
3633 * 3. user starts typing a filename
3635 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3636 * support interactive search. However, we'd rather focus the location entry
3637 * so that the user can type *there*.
3639 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3640 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3641 * if the user starts typing while the focus is in the shortcuts pane.
3643 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3644 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3646 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3647 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3648 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3649 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3650 /* Accessible object name for the file chooser's shortcuts pane */
3651 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3653 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3655 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3657 tree_model_row_targets,
3658 G_N_ELEMENTS (tree_model_row_targets),
3661 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3662 GTK_DEST_DEFAULT_ALL,
3663 tree_model_row_targets,
3664 G_N_ELEMENTS (tree_model_row_targets),
3665 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3666 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3668 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3669 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3670 gtk_tree_selection_set_select_function (selection,
3671 shortcuts_select_func,
3674 g_signal_connect (selection, "changed",
3675 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3677 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3678 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3680 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3681 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3682 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3683 G_CALLBACK (shortcuts_drag_end_cb), impl);
3684 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3685 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3687 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3688 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3689 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3690 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3691 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3692 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3693 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3694 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3696 /* Support tooltips */
3697 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3698 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3699 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3701 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3702 gtk_widget_show (impl->browse_shortcuts_tree_view);
3706 column = gtk_tree_view_column_new ();
3707 /* Column header for the file chooser's shortcuts pane */
3708 gtk_tree_view_column_set_title (column, _("_Places"));
3710 renderer = gtk_cell_renderer_pixbuf_new ();
3711 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3712 gtk_tree_view_column_set_attributes (column, renderer,
3713 "pixbuf", SHORTCUTS_COL_PIXBUF,
3714 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3717 renderer = gtk_cell_renderer_text_new ();
3718 g_object_set (renderer,
3720 "ellipsize", PANGO_ELLIPSIZE_END,
3722 g_signal_connect (renderer, "edited",
3723 G_CALLBACK (shortcuts_edited), impl);
3724 g_signal_connect (renderer, "editing-canceled",
3725 G_CALLBACK (shortcuts_editing_canceled), impl);
3726 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3727 gtk_tree_view_column_set_attributes (column, renderer,
3728 "text", SHORTCUTS_COL_NAME,
3731 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3732 shortcuts_row_separator_func,
3735 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3740 /* Creates the widgets for the shortcuts/bookmarks pane */
3742 shortcuts_pane_create (GtkFileChooserDefault *impl,
3743 GtkSizeGroup *size_group)
3748 GtkStyleContext *context;
3751 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3752 gtk_widget_show (vbox);
3754 /* Shortcuts tree */
3756 widget = shortcuts_list_create (impl);
3758 gtk_size_group_add_widget (size_group, widget);
3759 context = gtk_widget_get_style_context (widget);
3760 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3762 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3764 /* Box for buttons */
3766 toolbar = gtk_toolbar_new ();
3767 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3769 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3770 gtk_widget_show (toolbar);
3772 context = gtk_widget_get_style_context (toolbar);
3773 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3775 /* Add bookmark button */
3776 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3777 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3781 G_CALLBACK (add_bookmark_button_clicked_cb));
3782 g_object_unref (icon);
3784 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3785 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3786 _("Add the selected folder to the Bookmarks"));
3788 /* Remove bookmark button */
3789 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3790 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3794 G_CALLBACK (remove_bookmark_button_clicked_cb));
3795 g_object_unref (icon);
3796 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3797 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3798 _("Remove the selected bookmark"));
3804 key_is_left_or_right (GdkEventKey *event)
3808 modifiers = gtk_accelerator_get_default_mod_mask ();
3810 return ((event->keyval == GDK_KEY_Right
3811 || event->keyval == GDK_KEY_KP_Right
3812 || event->keyval == GDK_KEY_Left
3813 || event->keyval == GDK_KEY_KP_Left)
3814 && (event->state & modifiers) == 0);
3817 /* Handles key press events on the file list, so that we can trap Enter to
3818 * activate the default button on our own. Also, checks to see if '/' has been
3819 * pressed. See comment by tree_view_keybinding_cb() for more details.
3822 browse_files_key_press_event_cb (GtkWidget *widget,
3826 GtkFileChooserDefault *impl;
3829 impl = (GtkFileChooserDefault *) data;
3831 modifiers = gtk_accelerator_get_default_mod_mask ();
3833 if ((event->keyval == GDK_KEY_slash
3834 || event->keyval == GDK_KEY_KP_Divide
3836 || event->keyval == GDK_KEY_asciitilde
3838 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3840 location_popup_handler (impl, event->string);
3844 if (key_is_left_or_right (event))
3846 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3850 if ((event->keyval == GDK_KEY_Return
3851 || event->keyval == GDK_KEY_ISO_Enter
3852 || event->keyval == GDK_KEY_KP_Enter
3853 || event->keyval == GDK_KEY_space
3854 || event->keyval == GDK_KEY_KP_Space)
3855 && ((event->state & modifiers) == 0)
3856 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3857 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3861 window = get_toplevel (widget);
3864 GtkWidget *default_widget, *focus_widget;
3866 default_widget = gtk_window_get_default_widget (window);
3867 focus_widget = gtk_window_get_focus (window);
3869 if (widget != default_widget &&
3870 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3872 gtk_window_activate_default (window);
3882 /* Callback used when the file list's popup menu is detached */
3884 popup_menu_detach_cb (GtkWidget *attach_widget,
3887 GtkFileChooserDefault *impl;
3889 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3890 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3892 impl->browse_files_popup_menu = NULL;
3893 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3894 impl->browse_files_popup_menu_hidden_files_item = NULL;
3897 /* Callback used when the "Add to Bookmarks" menu item is activated */
3899 add_to_shortcuts_cb (GtkMenuItem *item,
3900 GtkFileChooserDefault *impl)
3902 bookmarks_add_selected_folder (impl);
3905 /* Callback used when the "Show Hidden Files" menu item is toggled */
3907 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3908 GtkFileChooserDefault *impl)
3911 "show-hidden", gtk_check_menu_item_get_active (item),
3915 /* Callback used when the "Show Size Column" menu item is toggled */
3917 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3918 GtkFileChooserDefault *impl)
3920 impl->show_size_column = gtk_check_menu_item_get_active (item);
3922 gtk_tree_view_column_set_visible (impl->list_size_column,
3923 impl->show_size_column);
3926 /* Shows an error dialog about not being able to select a dragged file */
3928 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3933 _("Could not select file"),
3938 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3943 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3945 for (i = 1; uris[i]; i++)
3948 GError *error = NULL;
3951 file = g_file_new_for_uri (uri);
3953 gtk_file_chooser_default_select_file (chooser, file, &error);
3955 error_selecting_dragged_file_dialog (impl, file, error);
3957 g_object_unref (file);
3961 struct FileListDragData
3963 GtkFileChooserDefault *impl;
3969 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3971 const GError *error,
3974 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3975 struct FileListDragData *data = user_data;
3976 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3978 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3981 data->impl->file_list_drag_data_received_cancellable = NULL;
3983 if (cancelled || error)
3986 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3987 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3988 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3989 change_folder_and_display_error (data->impl, data->file, FALSE);
3992 GError *error = NULL;
3994 gtk_file_chooser_default_unselect_all (chooser);
3995 gtk_file_chooser_default_select_file (chooser, data->file, &error);
3997 error_selecting_dragged_file_dialog (data->impl, data->file, error);
3999 browse_files_center_selected_row (data->impl);
4002 if (data->impl->select_multiple)
4003 file_list_drag_data_select_uris (data->impl, data->uris);
4006 g_object_unref (data->impl);
4007 g_strfreev (data->uris);
4008 g_object_unref (data->file);
4011 g_object_unref (cancellable);
4015 file_list_drag_data_received_cb (GtkWidget *widget,
4016 GdkDragContext *context,
4019 GtkSelectionData *selection_data,
4024 GtkFileChooserDefault *impl;
4029 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4031 /* Allow only drags from other widgets; see bug #533891. */
4032 if (gtk_drag_get_source_widget (context) == widget)
4034 g_signal_stop_emission_by_name (widget, "drag-data-received");
4038 /* Parse the text/uri-list string, navigate to the first one */
4039 uris = gtk_selection_data_get_uris (selection_data);
4040 if (uris && uris[0])
4042 struct FileListDragData *data;
4045 file = g_file_new_for_uri (uri);
4047 data = g_new0 (struct FileListDragData, 1);
4048 data->impl = g_object_ref (impl);
4052 if (impl->file_list_drag_data_received_cancellable)
4053 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4055 impl->file_list_drag_data_received_cancellable =
4056 _gtk_file_system_get_info (impl->file_system, file,
4058 file_list_drag_data_received_get_info_cb,
4062 g_signal_stop_emission_by_name (widget, "drag-data-received");
4065 /* Don't do anything with the drag_drop signal */
4067 file_list_drag_drop_cb (GtkWidget *widget,
4068 GdkDragContext *context,
4072 GtkFileChooserDefault *impl)
4074 g_signal_stop_emission_by_name (widget, "drag-drop");
4078 /* Disable the normal tree drag motion handler, it makes it look like you're
4079 dropping the dragged item onto a tree item */
4081 file_list_drag_motion_cb (GtkWidget *widget,
4082 GdkDragContext *context,
4086 GtkFileChooserDefault *impl)
4088 g_signal_stop_emission_by_name (widget, "drag-motion");
4092 /* Constructs the popup menu for the file list if needed */
4094 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4098 if (impl->browse_files_popup_menu)
4101 impl->browse_files_popup_menu = gtk_menu_new ();
4102 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4103 impl->browse_files_tree_view,
4104 popup_menu_detach_cb);
4106 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4107 impl->browse_files_popup_menu_add_shortcut_item = item;
4108 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4109 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4110 g_signal_connect (item, "activate",
4111 G_CALLBACK (add_to_shortcuts_cb), impl);
4112 gtk_widget_show (item);
4113 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4115 item = gtk_separator_menu_item_new ();
4116 gtk_widget_show (item);
4117 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4119 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4120 impl->browse_files_popup_menu_hidden_files_item = item;
4121 g_signal_connect (item, "toggled",
4122 G_CALLBACK (show_hidden_toggled_cb), impl);
4123 gtk_widget_show (item);
4124 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4126 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4127 impl->browse_files_popup_menu_size_column_item = item;
4128 g_signal_connect (item, "toggled",
4129 G_CALLBACK (show_size_column_toggled_cb), impl);
4130 gtk_widget_show (item);
4131 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4133 bookmarks_check_add_sensitivity (impl);
4136 /* Updates the popup menu for the file list, creating it if necessary */
4138 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4140 file_list_build_popup_menu (impl);
4142 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4144 /* The sensitivity of the Add to Bookmarks item is set in
4145 * bookmarks_check_add_sensitivity()
4148 /* 'Show Hidden Files' */
4149 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4150 G_CALLBACK (show_hidden_toggled_cb), impl);
4151 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4153 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4154 G_CALLBACK (show_hidden_toggled_cb), impl);
4156 /* 'Show Size Column' */
4157 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4158 G_CALLBACK (show_size_column_toggled_cb), impl);
4159 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4160 impl->show_size_column);
4161 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4162 G_CALLBACK (show_size_column_toggled_cb), impl);
4166 popup_position_func (GtkMenu *menu,
4172 GtkAllocation allocation;
4173 GtkWidget *widget = GTK_WIDGET (user_data);
4174 GdkScreen *screen = gtk_widget_get_screen (widget);
4177 GdkRectangle monitor;
4179 g_return_if_fail (gtk_widget_get_realized (widget));
4181 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4183 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4186 gtk_widget_get_allocation (widget, &allocation);
4187 *x += (allocation.width - req.width) / 2;
4188 *y += (allocation.height - req.height) / 2;
4190 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4191 gtk_menu_set_monitor (menu, monitor_num);
4192 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4194 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4195 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4201 file_list_popup_menu (GtkFileChooserDefault *impl,
4202 GdkEventButton *event)
4204 file_list_update_popup_menu (impl);
4206 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4207 NULL, NULL, NULL, NULL,
4208 event->button, event->time);
4211 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4213 popup_position_func, impl->browse_files_tree_view,
4214 0, GDK_CURRENT_TIME);
4215 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4221 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4223 list_popup_menu_cb (GtkWidget *widget,
4224 GtkFileChooserDefault *impl)
4226 file_list_popup_menu (impl, NULL);
4230 /* Callback used when a button is pressed on the file list. We trap button 3 to
4231 * bring up a popup menu.
4234 list_button_press_event_cb (GtkWidget *widget,
4235 GdkEventButton *event,
4236 GtkFileChooserDefault *impl)
4238 static gboolean in_press = FALSE;
4243 if (event->button != 3)
4247 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4250 file_list_popup_menu (impl, event);
4255 OperationMode operation_mode;
4256 gint general_column;
4260 /* Sets the sort column IDs for the file list based on the operation mode */
4262 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4264 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4265 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4266 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4270 file_list_query_tooltip_cb (GtkWidget *widget,
4273 gboolean keyboard_tip,
4274 GtkTooltip *tooltip,
4277 GtkFileChooserDefault *impl = user_data;
4278 GtkTreeModel *model;
4284 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4288 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4291 &model, &path, &iter))
4294 gtk_tree_model_get (model, &iter,
4295 MODEL_COL_FILE, &file,
4300 gtk_tree_path_free (path);
4304 filename = g_file_get_path (file);
4305 gtk_tooltip_set_text (tooltip, filename);
4306 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4311 g_object_unref (file);
4312 gtk_tree_path_free (path);
4318 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4322 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4323 gtk_cell_renderer_set_fixed_size (renderer,
4324 xpad * 2 + impl->icon_size,
4325 ypad * 2 + impl->icon_size);
4328 /* Creates the widgets for the file list */
4330 create_file_list (GtkFileChooserDefault *impl)
4333 GtkTreeSelection *selection;
4334 GtkTreeViewColumn *column;
4335 GtkCellRenderer *renderer;
4337 /* Scrolled window */
4338 swin = gtk_scrolled_window_new (NULL, NULL);
4339 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4340 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4341 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4344 /* Tree/list view */
4346 impl->browse_files_tree_view = gtk_tree_view_new ();
4347 #ifdef PROFILE_FILE_CHOOSER
4348 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4350 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4351 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4353 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4354 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4356 gtk_drag_dest_set (impl->browse_files_tree_view,
4357 GTK_DEST_DEFAULT_ALL,
4359 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4360 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4362 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4363 G_CALLBACK (list_row_activated), impl);
4364 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4365 G_CALLBACK (browse_files_key_press_event_cb), impl);
4366 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4367 G_CALLBACK (list_popup_menu_cb), impl);
4368 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4369 G_CALLBACK (list_button_press_event_cb), impl);
4371 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4372 G_CALLBACK (file_list_drag_data_received_cb), impl);
4373 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4374 G_CALLBACK (file_list_drag_drop_cb), impl);
4375 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4376 G_CALLBACK (file_list_drag_motion_cb), impl);
4378 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4379 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4380 G_CALLBACK (file_list_query_tooltip_cb), impl);
4382 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4383 gtk_tree_selection_set_select_function (selection,
4386 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4389 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4390 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4392 g_signal_connect (selection, "changed",
4393 G_CALLBACK (list_selection_changed), impl);
4395 /* Keep the column order in sync with update_cell_renderer_attributes() */
4397 /* Filename column */
4399 impl->list_name_column = gtk_tree_view_column_new ();
4400 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4401 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4402 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4404 renderer = gtk_cell_renderer_pixbuf_new ();
4405 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4406 set_icon_cell_renderer_fixed_size (impl, renderer);
4407 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4409 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4410 g_object_set (impl->list_name_renderer,
4411 "ellipsize", PANGO_ELLIPSIZE_END,
4413 g_signal_connect (impl->list_name_renderer, "edited",
4414 G_CALLBACK (renderer_edited_cb), impl);
4415 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4416 G_CALLBACK (renderer_editing_canceled_cb), impl);
4417 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4419 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4423 column = gtk_tree_view_column_new ();
4424 gtk_tree_view_column_set_resizable (column, TRUE);
4425 gtk_tree_view_column_set_title (column, _("Size"));
4427 renderer = gtk_cell_renderer_text_new ();
4428 g_object_set (renderer,
4429 "alignment", PANGO_ALIGN_RIGHT,
4431 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4432 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4433 impl->list_size_column = column;
4435 /* Modification time column */
4437 column = gtk_tree_view_column_new ();
4438 gtk_tree_view_column_set_resizable (column, TRUE);
4439 gtk_tree_view_column_set_title (column, _("Modified"));
4441 renderer = gtk_cell_renderer_text_new ();
4442 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4443 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4444 impl->list_mtime_column = column;
4446 file_list_set_sort_column_ids (impl);
4447 update_cell_renderer_attributes (impl);
4449 gtk_widget_show_all (swin);
4455 create_path_bar (GtkFileChooserDefault *impl)
4457 GtkWidget *path_bar;
4459 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4460 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4465 /* Creates the widgets for the files/folders pane */
4467 file_pane_create (GtkFileChooserDefault *impl,
4468 GtkSizeGroup *size_group)
4474 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4475 gtk_widget_show (vbox);
4477 /* Box for lists and preview */
4479 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4480 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4481 gtk_widget_show (hbox);
4485 widget = create_file_list (impl);
4486 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4487 gtk_size_group_add_widget (size_group, widget);
4491 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4492 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4493 /* Don't show preview box initially */
4497 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4499 widget = filter_create (impl);
4501 gtk_widget_show (widget);
4502 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4504 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4509 /* Callback used when the "Browse for more folders" expander is toggled */
4511 expander_changed_cb (GtkExpander *expander,
4513 GtkFileChooserDefault *impl)
4515 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4516 update_appearance (impl);
4519 /* Callback used when the selection changes in the save folder combo box */
4521 save_folder_combo_changed_cb (GtkComboBox *combo,
4522 GtkFileChooserDefault *impl)
4526 if (impl->changing_folder)
4529 if (gtk_combo_box_get_active_iter (combo, &iter))
4531 GtkTreeIter child_iter;
4533 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4536 shortcuts_activate_iter (impl, &child_iter);
4541 save_folder_update_tooltip (GtkComboBox *combo,
4542 GtkFileChooserDefault *impl)
4549 if (gtk_combo_box_get_active_iter (combo, &iter))
4551 GtkTreeIter child_iter;
4553 ShortcutType shortcut_type;
4555 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4558 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4559 SHORTCUTS_COL_DATA, &col_data,
4560 SHORTCUTS_COL_TYPE, &shortcut_type,
4563 if (shortcut_type == SHORTCUT_TYPE_FILE)
4564 tooltip = g_file_get_parse_name (G_FILE (col_data));
4567 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4568 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4569 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4573 /* Filter function used to filter out the Search item and its separator.
4574 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4577 shortcuts_combo_filter_func (GtkTreeModel *model,
4581 GtkFileChooserDefault *impl;
4582 GtkTreePath *tree_path;
4587 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4589 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4591 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4592 g_assert (tree_path != NULL);
4594 indices = gtk_tree_path_get_indices (tree_path);
4598 if (impl->has_search)
4600 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4601 if (idx == indices[0])
4605 if (impl->has_recent)
4607 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4608 if (idx == indices[0])
4612 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4613 if (idx == indices[0])
4618 gtk_tree_path_free (tree_path);
4623 /* Creates the combo box with the save folders */
4625 save_folder_combo_create (GtkFileChooserDefault *impl)
4628 GtkCellRenderer *cell;
4630 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4631 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4632 shortcuts_combo_filter_func,
4636 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4637 "model", impl->shortcuts_combo_filter_model,
4638 "focus-on-click", FALSE,
4640 gtk_widget_show (combo);
4642 cell = gtk_cell_renderer_pixbuf_new ();
4643 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4644 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4645 "pixbuf", SHORTCUTS_COL_PIXBUF,
4646 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4647 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4650 cell = gtk_cell_renderer_text_new ();
4651 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4652 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4653 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4654 "text", SHORTCUTS_COL_NAME,
4655 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4658 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4659 shortcuts_row_separator_func,
4662 g_signal_connect (combo, "changed",
4663 G_CALLBACK (save_folder_combo_changed_cb), impl);
4664 g_signal_connect (combo, "changed",
4665 G_CALLBACK (save_folder_update_tooltip), impl);
4670 /* Creates the widgets specific to Save mode */
4672 save_widgets_create (GtkFileChooserDefault *impl)
4677 GtkWidget *alignment;
4679 if (impl->save_widgets != NULL)
4682 location_switch_to_path_bar (impl);
4684 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4686 table = gtk_table_new (2, 2, FALSE);
4687 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4688 gtk_widget_show (table);
4689 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4690 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4694 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4695 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4696 gtk_table_attach (GTK_TABLE (table), widget,
4700 gtk_widget_show (widget);
4702 /* Location entry */
4704 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4705 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4707 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4708 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4709 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4710 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4712 GTK_EXPAND | GTK_FILL, 0,
4714 gtk_widget_show (impl->location_entry);
4715 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4718 impl->save_folder_label = gtk_label_new (NULL);
4719 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4720 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4724 gtk_widget_show (impl->save_folder_label);
4726 impl->save_folder_combo = save_folder_combo_create (impl);
4727 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4729 GTK_EXPAND | GTK_FILL, GTK_FILL,
4731 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4734 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4735 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4737 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4738 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4739 g_signal_connect (impl->save_expander, "notify::expanded",
4740 G_CALLBACK (expander_changed_cb),
4742 gtk_widget_show_all (alignment);
4744 impl->save_widgets = vbox;
4745 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4746 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4747 gtk_widget_show (impl->save_widgets);
4750 /* Destroys the widgets specific to Save mode */
4752 save_widgets_destroy (GtkFileChooserDefault *impl)
4754 if (impl->save_widgets == NULL)
4757 gtk_widget_destroy (impl->save_widgets);
4758 impl->save_widgets = NULL;
4759 impl->location_entry = NULL;
4760 impl->save_folder_label = NULL;
4761 impl->save_folder_combo = NULL;
4762 impl->save_expander = NULL;
4765 /* Turns on the path bar widget. Can be called even if we are already in that
4769 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4771 if (impl->location_entry)
4773 gtk_widget_destroy (impl->location_entry);
4774 impl->location_entry = NULL;
4777 gtk_widget_hide (impl->location_entry_box);
4780 /* Sets the full path of the current folder as the text in the location entry. */
4782 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4784 gchar *text, *filename;
4786 if (!impl->current_folder)
4789 filename = g_file_get_path (impl->current_folder);
4793 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4797 text = g_file_get_uri (impl->current_folder);
4801 gboolean need_slash;
4804 len = strlen (text);
4805 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4811 slash_text = g_new (char, len + 2);
4812 strcpy (slash_text, text);
4813 slash_text[len] = G_DIR_SEPARATOR;
4814 slash_text[len + 1] = 0;
4820 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4827 /* Turns on the location entry. Can be called even if we are already in that
4831 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4833 /* when in search or recent files mode, we are not showing the
4834 * location_entry_box container, so there's no point in switching
4837 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4838 impl->operation_mode == OPERATION_MODE_RECENT)
4841 if (impl->location_entry)
4842 gtk_widget_destroy (impl->location_entry);
4846 gtk_widget_show (impl->location_entry_box);
4850 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4851 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4853 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4854 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4856 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4857 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4859 /* Configure the entry */
4861 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4865 gtk_widget_show (impl->location_entry);
4866 gtk_widget_grab_focus (impl->location_entry);
4869 /* Sets a new location mode. set_buttons determines whether the toggle button
4870 * for the mode will also be changed.
4873 location_mode_set (GtkFileChooserDefault *impl,
4874 LocationMode new_mode,
4875 gboolean set_button)
4877 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4878 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4880 GtkWindow *toplevel;
4881 GtkWidget *current_focus;
4882 gboolean button_active;
4883 gboolean switch_to_file_list;
4887 case LOCATION_MODE_PATH_BAR:
4888 button_active = FALSE;
4890 /* The location_entry will disappear when we switch to path bar mode. So,
4891 * we'll focus the file list in that case, to avoid having a window with
4892 * no focused widget.
4894 toplevel = get_toplevel (GTK_WIDGET (impl));
4895 switch_to_file_list = FALSE;
4898 current_focus = gtk_window_get_focus (toplevel);
4899 if (!current_focus || current_focus == impl->location_entry)
4900 switch_to_file_list = TRUE;
4903 location_switch_to_path_bar (impl);
4905 if (switch_to_file_list)
4906 gtk_widget_grab_focus (impl->browse_files_tree_view);
4910 case LOCATION_MODE_FILENAME_ENTRY:
4911 button_active = TRUE;
4912 location_switch_to_filename_entry (impl);
4916 g_assert_not_reached ();
4922 g_signal_handlers_block_by_func (impl->location_button,
4923 G_CALLBACK (location_button_toggled_cb), impl);
4925 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4927 g_signal_handlers_unblock_by_func (impl->location_button,
4928 G_CALLBACK (location_button_toggled_cb), impl);
4932 impl->location_mode = new_mode;
4936 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4938 /* when in search or recent files mode, we are not showing the
4939 * location_entry_box container, so there's no point in switching
4942 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4943 impl->operation_mode == OPERATION_MODE_RECENT)
4946 /* If the file entry is not visible, show it.
4947 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4949 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4951 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4953 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4955 if (gtk_widget_has_focus (impl->location_entry))
4957 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4961 gtk_widget_grab_focus (impl->location_entry);
4966 /* Callback used when one of the location mode buttons is toggled */
4968 location_button_toggled_cb (GtkToggleButton *toggle,
4969 GtkFileChooserDefault *impl)
4972 LocationMode new_mode;
4974 is_active = gtk_toggle_button_get_active (toggle);
4978 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4979 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4983 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4984 new_mode = LOCATION_MODE_PATH_BAR;
4987 location_mode_set (impl, new_mode, FALSE);
4990 /* Creates a toggle button for the location entry. */
4992 location_button_create (GtkFileChooserDefault *impl)
4997 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4998 gtk_widget_show (image);
5000 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5004 g_signal_connect (impl->location_button, "toggled",
5005 G_CALLBACK (location_button_toggled_cb), impl);
5007 str = _("Type a file name");
5009 gtk_widget_set_tooltip_text (impl->location_button, str);
5010 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5013 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5015 browse_widgets_create (GtkFileChooserDefault *impl)
5020 GtkSizeGroup *size_group;
5022 /* size group is used by the scrolled windows of the panes */
5023 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5024 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5026 /* Location widgets */
5027 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5028 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5029 gtk_widget_show (impl->browse_path_bar_hbox);
5031 /* Size group that allows the path bar to be the same size between modes */
5032 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5033 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5035 /* Location button */
5037 location_button_create (impl);
5038 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5039 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5043 impl->browse_path_bar = create_path_bar (impl);
5044 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5045 gtk_widget_show_all (impl->browse_path_bar);
5046 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5047 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
5050 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5051 g_signal_connect (impl->browse_new_folder_button, "clicked",
5052 G_CALLBACK (new_folder_button_clicked), impl);
5053 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5055 /* Box for the location label and entry */
5057 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5058 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5060 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5061 gtk_widget_show (impl->location_label);
5062 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5066 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5067 gtk_widget_show (hpaned);
5068 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5070 widget = shortcuts_pane_create (impl, size_group);
5071 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5072 widget = file_pane_create (impl, size_group);
5073 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5075 g_object_unref (size_group);
5081 gtk_file_chooser_default_constructor (GType type,
5082 guint n_construct_properties,
5083 GObjectConstructParam *construct_params)
5085 GtkFileChooserDefault *impl;
5088 profile_start ("start", NULL);
5090 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5091 n_construct_properties,
5093 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5095 g_assert (impl->file_system);
5097 gtk_widget_push_composite_child ();
5099 /* Shortcuts model */
5100 shortcuts_model_create (impl);
5102 /* The browse widgets */
5103 impl->browse_widgets = browse_widgets_create (impl);
5104 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5106 /* Alignment to hold extra widget */
5107 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5108 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5110 gtk_widget_pop_composite_child ();
5111 update_appearance (impl);
5113 profile_end ("end", NULL);
5118 /* Sets the extra_widget by packing it in the appropriate place */
5120 set_extra_widget (GtkFileChooserDefault *impl,
5121 GtkWidget *extra_widget)
5125 g_object_ref (extra_widget);
5126 /* FIXME: is this right ? */
5127 gtk_widget_show (extra_widget);
5130 if (impl->extra_widget)
5132 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5133 g_object_unref (impl->extra_widget);
5136 impl->extra_widget = extra_widget;
5137 if (impl->extra_widget)
5139 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5140 gtk_widget_show (impl->extra_align);
5143 gtk_widget_hide (impl->extra_align);
5147 set_local_only (GtkFileChooserDefault *impl,
5148 gboolean local_only)
5150 if (local_only != impl->local_only)
5152 impl->local_only = local_only;
5154 if (impl->location_entry)
5155 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5157 if (impl->shortcuts_model && impl->file_system)
5159 shortcuts_add_volumes (impl);
5160 shortcuts_add_bookmarks (impl);
5163 if (local_only && impl->current_folder &&
5164 !g_file_is_native (impl->current_folder))
5166 /* If we are pointing to a non-local folder, make an effort to change
5167 * back to a local folder, but it's really up to the app to not cause
5168 * such a situation, so we ignore errors.
5170 const gchar *home = g_get_home_dir ();
5176 home_file = g_file_new_for_path (home);
5178 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5180 g_object_unref (home_file);
5186 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5187 GtkFileChooserDefault *impl)
5189 shortcuts_add_volumes (impl);
5190 shortcuts_add_bookmarks (impl);
5192 bookmarks_check_add_sensitivity (impl);
5193 bookmarks_check_remove_sensitivity (impl);
5194 shortcuts_check_popup_sensitivity (impl);
5197 /* Sets the file chooser to multiple selection mode */
5199 set_select_multiple (GtkFileChooserDefault *impl,
5200 gboolean select_multiple,
5201 gboolean property_notify)
5203 GtkTreeSelection *selection;
5204 GtkSelectionMode mode;
5206 if (select_multiple == impl->select_multiple)
5209 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5211 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5212 gtk_tree_selection_set_mode (selection, mode);
5214 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5216 impl->select_multiple = select_multiple;
5217 g_object_notify (G_OBJECT (impl), "select-multiple");
5219 check_preview_change (impl);
5223 set_file_system_backend (GtkFileChooserDefault *impl)
5225 profile_start ("start for backend", "default");
5227 impl->file_system = _gtk_file_system_new ();
5229 g_signal_connect (impl->file_system, "volumes-changed",
5230 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5231 g_signal_connect (impl->file_system, "bookmarks-changed",
5232 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5234 profile_end ("end", NULL);
5238 unset_file_system_backend (GtkFileChooserDefault *impl)
5240 g_signal_handlers_disconnect_by_func (impl->file_system,
5241 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5243 g_object_unref (impl->file_system);
5245 impl->file_system = NULL;
5248 /* This function is basically a do_all function.
5250 * It sets the visibility on all the widgets based on the current state, and
5251 * moves the custom_widget if needed.
5254 update_appearance (GtkFileChooserDefault *impl)
5256 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5257 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5261 gtk_widget_hide (impl->location_button);
5262 save_widgets_create (impl);
5264 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5265 text = _("Save in _folder:");
5267 text = _("Create in _folder:");
5269 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5271 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5273 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5274 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5275 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5276 gtk_widget_show (impl->browse_widgets);
5280 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5281 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5282 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5283 gtk_widget_hide (impl->browse_widgets);
5286 if (impl->select_multiple)
5288 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5289 "Re-setting to single selection mode.");
5290 set_select_multiple (impl, FALSE, TRUE);
5293 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5294 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5296 gtk_widget_show (impl->location_button);
5297 save_widgets_destroy (impl);
5298 gtk_widget_show (impl->browse_widgets);
5299 location_mode_set (impl, impl->location_mode, TRUE);
5302 if (impl->location_entry)
5303 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5305 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5306 gtk_widget_hide (impl->browse_new_folder_button);
5308 gtk_widget_show (impl->browse_new_folder_button);
5310 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5311 * of files may change depending whether we are in a file or folder-only mode.
5313 gtk_widget_queue_draw (impl->browse_files_tree_view);
5315 emit_default_size_changed (impl);
5319 gtk_file_chooser_default_set_property (GObject *object,
5321 const GValue *value,
5325 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5329 case GTK_FILE_CHOOSER_PROP_ACTION:
5331 GtkFileChooserAction action = g_value_get_enum (value);
5333 if (action != impl->action)
5335 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5337 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5338 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5339 && impl->select_multiple)
5341 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5342 "this is not allowed in multiple selection mode. Resetting the file chooser "
5343 "to single selection mode.");
5344 set_select_multiple (impl, FALSE, TRUE);
5346 impl->action = action;
5347 update_cell_renderer_attributes (impl);
5348 update_appearance (impl);
5349 settings_load (impl);
5354 case GTK_FILE_CHOOSER_PROP_FILTER:
5355 set_current_filter (impl, g_value_get_object (value));
5358 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5359 set_local_only (impl, g_value_get_boolean (value));
5362 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5363 set_preview_widget (impl, g_value_get_object (value));
5366 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5367 impl->preview_widget_active = g_value_get_boolean (value);
5368 update_preview_widget_visibility (impl);
5371 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5372 impl->use_preview_label = g_value_get_boolean (value);
5373 update_preview_widget_visibility (impl);
5376 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5377 set_extra_widget (impl, g_value_get_object (value));
5380 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5382 gboolean select_multiple = g_value_get_boolean (value);
5383 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5384 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5387 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5388 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5389 "leaving the file chooser in single selection mode.");
5393 set_select_multiple (impl, select_multiple, FALSE);
5397 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5399 gboolean show_hidden = g_value_get_boolean (value);
5400 if (show_hidden != impl->show_hidden)
5402 impl->show_hidden = show_hidden;
5404 if (impl->browse_files_model)
5405 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5410 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5412 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5413 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5417 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5419 gboolean create_folders = g_value_get_boolean (value);
5420 impl->create_folders = create_folders;
5421 update_appearance (impl);
5426 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5432 gtk_file_chooser_default_get_property (GObject *object,
5437 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5441 case GTK_FILE_CHOOSER_PROP_ACTION:
5442 g_value_set_enum (value, impl->action);
5445 case GTK_FILE_CHOOSER_PROP_FILTER:
5446 g_value_set_object (value, impl->current_filter);
5449 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5450 g_value_set_boolean (value, impl->local_only);
5453 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5454 g_value_set_object (value, impl->preview_widget);
5457 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5458 g_value_set_boolean (value, impl->preview_widget_active);
5461 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5462 g_value_set_boolean (value, impl->use_preview_label);
5465 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5466 g_value_set_object (value, impl->extra_widget);
5469 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5470 g_value_set_boolean (value, impl->select_multiple);
5473 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5474 g_value_set_boolean (value, impl->show_hidden);
5477 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5478 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5481 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5482 g_value_set_boolean (value, impl->create_folders);
5486 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5491 /* Removes the settings signal handler. It's safe to call multiple times */
5493 remove_settings_signal (GtkFileChooserDefault *impl,
5496 if (impl->settings_signal_id)
5498 GtkSettings *settings;
5500 settings = gtk_settings_get_for_screen (screen);
5501 g_signal_handler_disconnect (settings,
5502 impl->settings_signal_id);
5503 impl->settings_signal_id = 0;
5508 gtk_file_chooser_default_dispose (GObject *object)
5511 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5513 if (impl->extra_widget)
5515 g_object_unref (impl->extra_widget);
5516 impl->extra_widget = NULL;
5519 pending_select_files_free (impl);
5521 /* cancel all pending operations */
5522 if (impl->pending_cancellables)
5524 for (l = impl->pending_cancellables; l; l = l->next)
5526 GCancellable *cancellable = G_CANCELLABLE (l->data);
5527 g_cancellable_cancel (cancellable);
5529 g_slist_free (impl->pending_cancellables);
5530 impl->pending_cancellables = NULL;
5533 if (impl->reload_icon_cancellables)
5535 for (l = impl->reload_icon_cancellables; l; l = l->next)
5537 GCancellable *cancellable = G_CANCELLABLE (l->data);
5538 g_cancellable_cancel (cancellable);
5540 g_slist_free (impl->reload_icon_cancellables);
5541 impl->reload_icon_cancellables = NULL;
5544 if (impl->loading_shortcuts)
5546 for (l = impl->loading_shortcuts; l; l = l->next)
5548 GCancellable *cancellable = G_CANCELLABLE (l->data);
5549 g_cancellable_cancel (cancellable);
5551 g_slist_free (impl->loading_shortcuts);
5552 impl->loading_shortcuts = NULL;
5555 if (impl->file_list_drag_data_received_cancellable)
5557 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5558 impl->file_list_drag_data_received_cancellable = NULL;
5561 if (impl->update_current_folder_cancellable)
5563 g_cancellable_cancel (impl->update_current_folder_cancellable);
5564 impl->update_current_folder_cancellable = NULL;
5567 if (impl->should_respond_get_info_cancellable)
5569 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5570 impl->should_respond_get_info_cancellable = NULL;
5573 if (impl->update_from_entry_cancellable)
5575 g_cancellable_cancel (impl->update_from_entry_cancellable);
5576 impl->update_from_entry_cancellable = NULL;
5579 if (impl->shortcuts_activate_iter_cancellable)
5581 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5582 impl->shortcuts_activate_iter_cancellable = NULL;
5585 search_stop_searching (impl, TRUE);
5586 recent_stop_loading (impl);
5588 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5590 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5593 /* We override show-all since we have internal widgets that
5594 * shouldn't be shown when you call show_all(), like the filter
5598 gtk_file_chooser_default_show_all (GtkWidget *widget)
5600 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5602 gtk_widget_show (widget);
5604 if (impl->extra_widget)
5605 gtk_widget_show_all (impl->extra_widget);
5608 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5609 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5612 toplevel_set_focus_cb (GtkWindow *window,
5614 GtkFileChooserDefault *impl)
5616 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5619 /* We monitor the focus widget on our toplevel to be able to know which widget
5620 * was last focused at the time our "should_respond" method gets called.
5623 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5624 GtkWidget *previous_toplevel)
5626 GtkFileChooserDefault *impl;
5627 GtkWidget *toplevel;
5629 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5630 toplevel = gtk_widget_get_toplevel (widget);
5632 if (previous_toplevel &&
5633 impl->toplevel_set_focus_id != 0)
5635 g_signal_handler_disconnect (previous_toplevel,
5636 impl->toplevel_set_focus_id);
5637 impl->toplevel_set_focus_id = 0;
5638 impl->toplevel_last_focus_widget = NULL;
5641 if (gtk_widget_is_toplevel (toplevel))
5643 g_assert (impl->toplevel_set_focus_id == 0);
5644 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5645 G_CALLBACK (toplevel_set_focus_cb), impl);
5646 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5650 /* Changes the icons wherever it is needed */
5652 change_icon_theme (GtkFileChooserDefault *impl)
5654 GtkSettings *settings;
5656 GtkCellRenderer *renderer;
5659 profile_start ("start", NULL);
5661 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5663 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5664 impl->icon_size = MAX (width, height);
5666 impl->icon_size = FALLBACK_ICON_SIZE;
5668 shortcuts_reload_icons (impl);
5669 /* the first cell in the first column is the icon column, and we have a fixed size there */
5670 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5671 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5672 renderer = GTK_CELL_RENDERER (cells->data);
5673 set_icon_cell_renderer_fixed_size (impl, renderer);
5674 g_list_free (cells);
5675 if (impl->browse_files_model)
5676 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5677 gtk_widget_queue_resize (impl->browse_files_tree_view);
5679 profile_end ("end", NULL);
5682 /* Callback used when a GtkSettings value changes */
5684 settings_notify_cb (GObject *object,
5686 GtkFileChooserDefault *impl)
5690 profile_start ("start", NULL);
5692 name = g_param_spec_get_name (pspec);
5694 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5695 strcmp (name, "gtk-icon-sizes") == 0)
5696 change_icon_theme (impl);
5698 profile_end ("end", NULL);
5701 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5705 check_icon_theme (GtkFileChooserDefault *impl)
5707 GtkSettings *settings;
5709 profile_start ("start", NULL);
5711 if (impl->settings_signal_id)
5713 profile_end ("end", NULL);
5717 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5719 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5720 impl->settings_signal_id = g_signal_connect (settings, "notify",
5721 G_CALLBACK (settings_notify_cb), impl);
5723 change_icon_theme (impl);
5726 profile_end ("end", NULL);
5730 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5732 GtkFileChooserDefault *impl;
5734 profile_start ("start", NULL);
5736 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5738 profile_msg (" parent class style_udpated start", NULL);
5739 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5740 profile_msg (" parent class style_updated end", NULL);
5742 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5743 change_icon_theme (impl);
5745 emit_default_size_changed (impl);
5747 profile_end ("end", NULL);
5751 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5752 GdkScreen *previous_screen)
5754 GtkFileChooserDefault *impl;
5756 profile_start ("start", NULL);
5758 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5760 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5761 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5763 remove_settings_signal (impl, previous_screen);
5764 check_icon_theme (impl);
5766 emit_default_size_changed (impl);
5768 profile_end ("end", NULL);
5772 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5773 GtkAllocation *allocation)
5775 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5779 set_sort_column (GtkFileChooserDefault *impl)
5781 GtkTreeSortable *sortable;
5783 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5785 /* can happen when we're still populating the model */
5786 if (sortable == NULL)
5789 gtk_tree_sortable_set_sort_column_id (sortable,
5795 settings_ensure (GtkFileChooserDefault *impl)
5797 if (impl->settings != NULL)
5800 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5801 "/org/gtk/settings/file-chooser/");
5802 g_settings_delay (impl->settings);
5806 settings_load (GtkFileChooserDefault *impl)
5808 LocationMode location_mode;
5809 gboolean show_hidden;
5810 gboolean expand_folders;
5811 gboolean show_size_column;
5813 GtkSortType sort_order;
5815 settings_ensure (impl);
5817 expand_folders = g_settings_get_boolean (impl->settings, SETTINGS_KEY_EXPAND_FOLDERS);
5818 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5819 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5820 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5821 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5822 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5824 location_mode_set (impl, location_mode, TRUE);
5826 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5828 impl->expand_folders = expand_folders;
5829 if (impl->save_expander)
5830 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5832 impl->show_size_column = show_size_column;
5833 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5835 impl->sort_column = sort_column;
5836 impl->sort_order = sort_order;
5837 /* We don't call set_sort_column() here as the models may not have been
5838 * created yet. The individual functions that create and set the models will
5839 * call set_sort_column() themselves.
5844 save_dialog_geometry (GtkFileChooserDefault *impl)
5846 GtkWindow *toplevel;
5847 int x, y, width, height;
5849 /* We don't save the geometry in non-expanded "save" mode, so that the "little
5850 * dialog" won't make future Open dialogs too small.
5852 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5853 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5854 || impl->expand_folders))
5857 toplevel = get_toplevel (GTK_WIDGET (impl));
5859 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5862 gtk_window_get_position (toplevel, &x, &y);
5863 gtk_window_get_size (toplevel, &width, &height);
5865 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5866 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5870 settings_save (GtkFileChooserDefault *impl)
5872 settings_ensure (impl);
5874 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5875 g_settings_set_boolean (impl->settings, SETTINGS_KEY_EXPAND_FOLDERS, impl->expand_folders);
5876 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5877 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5878 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5879 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5880 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5882 save_dialog_geometry (impl);
5884 /* Now apply the settings */
5885 g_settings_apply (impl->settings);
5887 g_object_unref (impl->settings);
5888 impl->settings = NULL;
5891 /* GtkWidget::realize method */
5893 gtk_file_chooser_default_realize (GtkWidget *widget)
5895 GtkFileChooserDefault *impl;
5897 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5899 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5901 emit_default_size_changed (impl);
5904 /* GtkWidget::map method */
5906 gtk_file_chooser_default_map (GtkWidget *widget)
5908 GtkFileChooserDefault *impl;
5909 char *current_working_dir;
5911 profile_start ("start", NULL);
5913 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5915 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5917 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5919 switch (impl->reload_state)
5922 /* The user didn't explicitly give us a folder to
5923 * display, so we'll use the cwd
5925 current_working_dir = g_get_current_dir ();
5926 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
5927 current_working_dir);
5928 g_free (current_working_dir);
5931 case RELOAD_HAS_FOLDER:
5932 /* Nothing; we are already loading or loaded, so we
5933 * don't need to reload
5938 g_assert_not_reached ();
5942 volumes_bookmarks_changed_cb (impl->file_system, impl);
5944 settings_load (impl);
5946 profile_end ("end", NULL);
5949 /* GtkWidget::unmap method */
5951 gtk_file_chooser_default_unmap (GtkWidget *widget)
5953 GtkFileChooserDefault *impl;
5955 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5957 settings_save (impl);
5959 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5963 install_list_model_filter (GtkFileChooserDefault *impl)
5965 _gtk_file_system_model_set_filter (impl->browse_files_model,
5966 impl->current_filter);
5969 #define COMPARE_DIRECTORIES \
5970 GtkFileChooserDefault *impl = user_data; \
5971 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
5972 gboolean dir_a, dir_b; \
5974 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
5975 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
5977 if (dir_a != dir_b) \
5978 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5980 /* Sort callback for the filename column */
5982 name_sort_func (GtkTreeModel *model,
5987 COMPARE_DIRECTORIES;
5990 const char *key_a, *key_b;
5993 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
5994 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
5997 result = strcmp (key_a, key_b);
6009 /* Sort callback for the size column */
6011 size_sort_func (GtkTreeModel *model,
6016 COMPARE_DIRECTORIES;
6019 gint64 size_a, size_b;
6021 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6022 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6024 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6028 /* Sort callback for the mtime column */
6030 mtime_sort_func (GtkTreeModel *model,
6035 COMPARE_DIRECTORIES;
6040 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6041 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6043 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6047 /* Callback used when the sort column changes. We cache the sort order for use
6048 * in name_sort_func().
6051 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6052 GtkFileChooserDefault *impl)
6054 gint sort_column_id;
6055 GtkSortType sort_type;
6057 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6059 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6060 impl->sort_column = sort_column_id;
6061 impl->sort_order = sort_type;
6066 set_busy_cursor (GtkFileChooserDefault *impl,
6070 GtkWindow *toplevel;
6071 GdkDisplay *display;
6074 toplevel = get_toplevel (GTK_WIDGET (impl));
6075 widget = GTK_WIDGET (toplevel);
6076 if (!toplevel || !gtk_widget_get_realized (widget))
6079 display = gtk_widget_get_display (widget);
6082 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6086 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6087 gdk_display_flush (display);
6090 g_object_unref (cursor);
6093 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6095 load_set_model (GtkFileChooserDefault *impl)
6097 profile_start ("start", NULL);
6099 g_assert (impl->browse_files_model != NULL);
6101 profile_msg (" gtk_tree_view_set_model start", NULL);
6102 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6103 GTK_TREE_MODEL (impl->browse_files_model));
6104 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6105 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6107 set_sort_column (impl);
6108 profile_msg (" gtk_tree_view_set_model end", NULL);
6109 impl->list_sort_ascending = TRUE;
6111 profile_end ("end", NULL);
6114 /* Timeout callback used when the loading timer expires */
6116 load_timeout_cb (gpointer data)
6118 GtkFileChooserDefault *impl;
6120 profile_start ("start", NULL);
6122 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6123 g_assert (impl->load_state == LOAD_PRELOAD);
6124 g_assert (impl->load_timeout_id != 0);
6125 g_assert (impl->browse_files_model != NULL);
6127 impl->load_timeout_id = 0;
6128 impl->load_state = LOAD_LOADING;
6130 load_set_model (impl);
6132 profile_end ("end", NULL);
6137 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6139 load_setup_timer (GtkFileChooserDefault *impl)
6141 g_assert (impl->load_timeout_id == 0);
6142 g_assert (impl->load_state != LOAD_PRELOAD);
6144 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6145 impl->load_state = LOAD_PRELOAD;
6148 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6150 load_remove_timer (GtkFileChooserDefault *impl)
6152 if (impl->load_timeout_id != 0)
6154 g_assert (impl->load_state == LOAD_PRELOAD);
6156 g_source_remove (impl->load_timeout_id);
6157 impl->load_timeout_id = 0;
6158 impl->load_state = LOAD_EMPTY;
6161 g_assert (impl->load_state == LOAD_EMPTY ||
6162 impl->load_state == LOAD_LOADING ||
6163 impl->load_state == LOAD_FINISHED);
6166 /* Selects the first row in the file list */
6168 browse_files_select_first_row (GtkFileChooserDefault *impl)
6171 GtkTreeIter dummy_iter;
6172 GtkTreeModel *tree_model;
6174 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6179 path = gtk_tree_path_new_from_indices (0, -1);
6181 /* If the list is empty, do nothing. */
6182 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6183 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6185 gtk_tree_path_free (path);
6188 struct center_selected_row_closure {
6189 GtkFileChooserDefault *impl;
6190 gboolean already_centered;
6193 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6194 * selected row in the tree view.
6197 center_selected_row_foreach_cb (GtkTreeModel *model,
6202 struct center_selected_row_closure *closure;
6205 if (closure->already_centered)
6208 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6209 closure->already_centered = TRUE;
6212 /* Centers the selected row in the tree view */
6214 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6216 struct center_selected_row_closure closure;
6217 GtkTreeSelection *selection;
6219 closure.impl = impl;
6220 closure.already_centered = FALSE;
6222 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6223 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6227 show_and_select_files (GtkFileChooserDefault *impl,
6230 GtkTreeSelection *selection;
6231 GtkFileSystemModel *fsmodel;
6232 gboolean can_have_hidden, can_have_filtered, selected_a_file;
6235 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6236 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6237 can_have_hidden = !impl->show_hidden;
6238 can_have_filtered = impl->current_filter != NULL;
6239 selected_a_file = FALSE;
6241 for (walk = files; walk && (can_have_hidden || can_have_filtered); walk = walk->next)
6243 GFile *file = walk->data;
6246 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6249 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6251 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6253 if (can_have_hidden &&
6254 (g_file_info_get_is_hidden (info) ||
6255 g_file_info_get_is_backup (info)))
6257 g_object_set (impl, "show-hidden", TRUE, NULL);
6258 can_have_hidden = FALSE;
6261 if (can_have_filtered)
6263 set_current_filter (impl, NULL);
6264 can_have_filtered = FALSE;
6268 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6272 gtk_tree_selection_select_iter (selection, &iter);
6274 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6275 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6277 gtk_tree_path_free (path);
6279 selected_a_file = TRUE;
6283 browse_files_center_selected_row (impl);
6285 return selected_a_file;
6288 /* Processes the pending operation when a folder is finished loading */
6290 pending_select_files_process (GtkFileChooserDefault *impl)
6292 g_assert (impl->load_state == LOAD_FINISHED);
6293 g_assert (impl->browse_files_model != NULL);
6295 if (impl->pending_select_files)
6297 show_and_select_files (impl, impl->pending_select_files);
6298 pending_select_files_free (impl);
6299 browse_files_center_selected_row (impl);
6303 /* We only select the first row if the chooser is actually mapped ---
6304 * selecting the first row is to help the user when he is interacting with
6305 * the chooser, but sometimes a chooser works not on behalf of the user,
6306 * but rather on behalf of something else like GtkFileChooserButton. In
6307 * that case, the chooser's selection should be what the caller expects,
6308 * as the user can't see that something else got selected. See bug #165264.
6310 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6311 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6312 browse_files_select_first_row (impl);
6315 g_assert (impl->pending_select_files == NULL);
6319 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6324 info = g_file_query_info (impl->current_folder,
6325 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6326 G_FILE_QUERY_INFO_NONE,
6331 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6332 g_object_unref (info);
6335 msg = g_strdup (_("Could not read the contents of the folder"));
6337 error_message (impl, msg, error->message);
6341 /* Callback used when the file system model finishes loading */
6343 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6345 GtkFileChooserDefault *impl)
6347 profile_start ("start", NULL);
6350 show_error_on_reading_current_folder (impl, error);
6352 if (impl->load_state == LOAD_PRELOAD)
6354 load_remove_timer (impl);
6355 load_set_model (impl);
6357 else if (impl->load_state == LOAD_LOADING)
6363 /* We can't g_assert_not_reached(), as something other than us may have
6364 * initiated a folder reload. See #165556.
6366 profile_end ("end", NULL);
6370 g_assert (impl->load_timeout_id == 0);
6372 impl->load_state = LOAD_FINISHED;
6374 pending_select_files_process (impl);
6375 set_busy_cursor (impl, FALSE);
6376 #ifdef PROFILE_FILE_CHOOSER
6377 access ("MARK: *** FINISHED LOADING", F_OK);
6380 profile_end ("end", NULL);
6384 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6385 gboolean remove_from_treeview)
6387 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6389 if (impl->browse_files_model)
6391 g_object_unref (impl->browse_files_model);
6392 impl->browse_files_model = NULL;
6395 if (remove_from_treeview)
6396 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6400 my_g_format_time_for_display (glong secs)
6405 time_t time_mtime, time_now;
6406 const gchar *format;
6407 gchar *locale_format = NULL;
6409 char *date_str = NULL;
6411 const char *locale, *dot = NULL;
6412 gint64 codepage = -1;
6418 #ifdef HAVE_LOCALTIME_R
6419 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6422 struct tm *ptm = localtime ((time_t *) &time_mtime);
6426 g_warning ("ptm != NULL failed");
6428 return g_strdup (_("Unknown"));
6431 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6433 #endif /* HAVE_LOCALTIME_R */
6435 g_date_set_time_t (&mtime, time_mtime);
6436 time_now = time (NULL);
6437 g_date_set_time_t (&now, time_now);
6439 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6441 /* Translators: %H means "hours" and %M means "minutes" */
6443 format = _("%H:%M");
6444 else if (days_diff == 1)
6445 format = _("Yesterday at %H:%M");
6448 if (days_diff > 1 && days_diff < 7)
6449 format = "%A"; /* Days from last week */
6451 format = "%x"; /* Any other date */
6455 /* g_locale_from_utf8() returns a string in the system
6456 * code-page, which is not always the same as that used by the C
6457 * library. For instance when running a GTK+ program with
6458 * LANG=ko on an English version of Windows, the system
6459 * code-page is 1252, but the code-page used by the C library is
6460 * 949. (It's GTK+ itself that sets the C library locale when it
6461 * notices the LANG environment variable. See gtkmain.c The
6462 * Microsoft C library doesn't look at any locale environment
6463 * variables.) We need to pass strftime() a string in the C
6464 * library's code-page. See bug #509885.
6466 locale = setlocale (LC_ALL, NULL);
6468 dot = strchr (locale, '.');
6471 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6473 /* All codepages should fit in 16 bits AFAIK */
6474 if (codepage > 0 && codepage < 65536)
6476 sprintf (charset, "CP%u", (guint) codepage);
6477 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6481 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6483 if (locale_format != NULL &&
6484 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6487 /* As above but in opposite direction... */
6488 if (codepage > 0 && codepage < 65536)
6489 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6491 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6495 if (date_str == NULL)
6496 date_str = g_strdup (_("Unknown"));
6498 g_free (locale_format);
6503 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6505 GFileAttributeType type;
6508 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6509 g_file_info_set_attribute (to, attribute, type, value);
6513 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6515 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6516 GFile *file = G_FILE (object);
6517 GFileInfo *queried, *info;
6520 queried = g_file_query_info_finish (file, res, NULL);
6521 if (queried == NULL)
6524 /* now we know model is valid */
6526 /* file was deleted */
6527 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6530 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6532 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6533 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6534 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6536 _gtk_file_system_model_update_file (model, file, info, FALSE);
6538 g_object_unref (info);
6542 file_system_model_set (GtkFileSystemModel *model,
6549 GtkFileChooserDefault *impl = data;
6553 case MODEL_COL_FILE:
6554 g_value_set_object (value, file);
6556 case MODEL_COL_NAME:
6558 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6560 g_value_set_string (value, g_file_info_get_display_name (info));
6562 case MODEL_COL_NAME_COLLATED:
6564 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6566 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6568 case MODEL_COL_IS_FOLDER:
6569 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6571 case MODEL_COL_PIXBUF:
6574 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6576 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6580 GtkTreeModel *tree_model;
6581 GtkTreePath *path, *start, *end;
6584 if (impl->browse_files_tree_view == NULL ||
6585 g_file_info_has_attribute (info, "filechooser::queried"))
6588 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6589 if (tree_model != GTK_TREE_MODEL (model))
6592 if (!_gtk_file_system_model_get_iter_for_file (model,
6595 g_assert_not_reached ();
6596 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6598 path = gtk_tree_model_get_path (tree_model, &iter);
6599 if (gtk_tree_path_compare (start, path) != 1 &&
6600 gtk_tree_path_compare (path, end) != 1)
6602 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6603 g_file_query_info_async (file,
6604 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6605 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6606 G_FILE_ATTRIBUTE_STANDARD_ICON,
6607 G_FILE_QUERY_INFO_NONE,
6609 _gtk_file_system_model_get_cancellable (model),
6610 file_system_model_got_thumbnail,
6613 gtk_tree_path_free (path);
6614 gtk_tree_path_free (start);
6615 gtk_tree_path_free (end);
6620 g_value_set_object (value, NULL);
6622 case MODEL_COL_SIZE:
6623 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6625 case MODEL_COL_SIZE_TEXT:
6626 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6627 g_value_set_string (value, NULL);
6629 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6631 case MODEL_COL_MTIME:
6632 case MODEL_COL_MTIME_TEXT:
6637 g_file_info_get_modification_time (info, &tv);
6638 if (column == MODEL_COL_MTIME)
6639 g_value_set_long (value, tv.tv_sec);
6640 else if (tv.tv_sec == 0)
6641 g_value_set_static_string (value, _("Unknown"));
6643 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6646 case MODEL_COL_ELLIPSIZE:
6647 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6650 g_assert_not_reached ();
6657 /* Gets rid of the old list model and creates a new one for the current folder */
6659 set_list_model (GtkFileChooserDefault *impl,
6662 g_assert (impl->current_folder != NULL);
6664 profile_start ("start", NULL);
6666 stop_loading_and_clear_list_model (impl, TRUE);
6668 set_busy_cursor (impl, TRUE);
6670 impl->browse_files_model =
6671 _gtk_file_system_model_new_for_directory (impl->current_folder,
6673 file_system_model_set,
6675 MODEL_COLUMN_TYPES);
6677 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6679 profile_msg (" set sort function", NULL);
6680 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6681 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6682 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6683 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6684 set_sort_column (impl);
6685 impl->list_sort_ascending = TRUE;
6686 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6687 G_CALLBACK (list_sort_column_changed_cb), impl);
6689 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6691 g_signal_connect (impl->browse_files_model, "finished-loading",
6692 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6694 install_list_model_filter (impl);
6696 profile_end ("end", NULL);
6701 struct update_chooser_entry_selected_foreach_closure {
6703 GtkTreeIter first_selected_iter;
6707 compare_utf8_filenames (const gchar *a,
6710 gchar *a_folded, *b_folded;
6713 a_folded = g_utf8_strdown (a, -1);
6714 b_folded = g_utf8_strdown (b, -1);
6716 retval = strcmp (a_folded, b_folded);
6725 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6730 struct update_chooser_entry_selected_foreach_closure *closure;
6733 closure->num_selected++;
6735 if (closure->num_selected == 1)
6736 closure->first_selected_iter = *iter;
6740 update_chooser_entry (GtkFileChooserDefault *impl)
6742 GtkTreeSelection *selection;
6743 struct update_chooser_entry_selected_foreach_closure closure;
6745 /* no need to update the file chooser's entry if there's no entry */
6746 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6747 impl->operation_mode == OPERATION_MODE_RECENT ||
6748 !impl->location_entry)
6751 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6752 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6753 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6754 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6755 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6758 g_assert (impl->location_entry != NULL);
6760 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6761 closure.num_selected = 0;
6762 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6764 if (closure.num_selected == 0)
6766 goto maybe_clear_entry;
6768 else if (closure.num_selected == 1)
6770 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6773 gboolean change_entry;
6775 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6777 /* If the cursor moved to the row of the newly created folder,
6778 * retrieving info will return NULL.
6783 g_free (impl->browse_files_last_selected_name);
6784 impl->browse_files_last_selected_name =
6785 g_strdup (g_file_info_get_display_name (info));
6787 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6788 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6789 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6791 /* Don't change the name when clicking on a folder... */
6792 change_entry = (! _gtk_file_info_consider_as_directory (info));
6795 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6799 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6801 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6802 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6810 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6811 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6813 /* Multiple selection, so just clear the entry. */
6814 g_free (impl->browse_files_last_selected_name);
6815 impl->browse_files_last_selected_name = NULL;
6817 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6823 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6824 && impl->browse_files_last_selected_name)
6826 const char *entry_text;
6828 gboolean clear_entry;
6830 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6831 len = strlen (entry_text);
6834 /* The file chooser entry may have appended a "/" to its text.
6835 * So take it out, and compare the result to the old selection.
6837 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6841 tmp = g_strndup (entry_text, len - 1);
6842 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6846 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6849 clear_entry = FALSE;
6852 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6857 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6861 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6865 struct UpdateCurrentFolderData
6867 GtkFileChooserDefault *impl;
6869 gboolean keep_trail;
6870 gboolean clear_entry;
6871 GFile *original_file;
6872 GError *original_error;
6876 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6877 GtkFileSystemVolume *volume,
6878 const GError *error,
6881 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6882 struct UpdateCurrentFolderData *data = user_data;
6883 GtkFileChooserDefault *impl = data->impl;
6885 if (cancellable != impl->update_current_folder_cancellable)
6888 impl->update_current_folder_cancellable = NULL;
6889 set_busy_cursor (impl, FALSE);
6896 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6897 impl->reload_state = RELOAD_EMPTY;
6901 change_folder_and_display_error (impl, data->file, data->clear_entry);
6904 g_object_unref (data->file);
6907 g_object_unref (cancellable);
6911 update_current_folder_get_info_cb (GCancellable *cancellable,
6913 const GError *error,
6916 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6917 struct UpdateCurrentFolderData *data = user_data;
6918 GtkFileChooserDefault *impl = data->impl;
6920 if (cancellable != impl->update_current_folder_cancellable)
6923 impl->update_current_folder_cancellable = NULL;
6924 impl->reload_state = RELOAD_EMPTY;
6926 set_busy_cursor (impl, FALSE);
6935 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6937 GMountOperation *mount_operation;
6938 GtkWidget *toplevel;
6940 g_object_unref (cancellable);
6941 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6943 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6945 set_busy_cursor (impl, TRUE);
6947 impl->update_current_folder_cancellable =
6948 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
6950 update_current_folder_mount_enclosing_volume_cb,
6956 if (!data->original_file)
6958 data->original_file = g_object_ref (data->file);
6959 data->original_error = g_error_copy (error);
6962 parent_file = g_file_get_parent (data->file);
6964 /* get parent path and try to change the folder to that */
6967 g_object_unref (data->file);
6968 data->file = parent_file;
6970 g_object_unref (cancellable);
6972 /* restart the update current folder operation */
6973 impl->reload_state = RELOAD_HAS_FOLDER;
6975 impl->update_current_folder_cancellable =
6976 _gtk_file_system_get_info (impl->file_system, data->file,
6978 update_current_folder_get_info_cb,
6981 set_busy_cursor (impl, TRUE);
6987 /* Error and bail out, ignoring "not found" errors since they're useless:
6988 * they only happen when a program defaults to a folder that has been (re)moved.
6990 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
6991 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6993 g_error_free (data->original_error);
6995 g_object_unref (data->original_file);
7001 if (data->original_file)
7003 /* Error and bail out, ignoring "not found" errors since they're useless:
7004 * they only happen when a program defaults to a folder that has been (re)moved.
7006 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7007 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7009 g_error_free (data->original_error);
7011 g_object_unref (data->original_file);
7014 if (! _gtk_file_info_consider_as_directory (info))
7017 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7020 if (impl->current_folder != data->file)
7022 if (impl->current_folder)
7023 g_object_unref (impl->current_folder);
7025 impl->current_folder = g_object_ref (data->file);
7028 impl->reload_state = RELOAD_HAS_FOLDER;
7030 /* Update the widgets that may trigger a folder change themselves. */
7032 if (!impl->changing_folder)
7034 impl->changing_folder = TRUE;
7036 shortcuts_update_current_folder (impl);
7038 impl->changing_folder = FALSE;
7041 /* Set the folder on the save entry */
7043 if (impl->location_entry)
7045 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7046 impl->current_folder);
7048 if (data->clear_entry)
7049 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7052 /* Create a new list model. This is slightly evil; we store the result value
7053 * but perform more actions rather than returning immediately even if it
7054 * generates an error.
7056 set_list_model (impl, NULL);
7058 /* Refresh controls */
7060 shortcuts_find_current_folder (impl);
7062 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7064 check_preview_change (impl);
7065 bookmarks_check_add_sensitivity (impl);
7067 g_signal_emit_by_name (impl, "selection-changed", 0);
7070 g_object_unref (data->file);
7073 g_object_unref (cancellable);
7077 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7079 gboolean keep_trail,
7080 gboolean clear_entry,
7083 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7084 struct UpdateCurrentFolderData *data;
7086 profile_start ("start", NULL);
7088 g_object_ref (file);
7090 switch (impl->operation_mode)
7092 case OPERATION_MODE_SEARCH:
7093 search_switch_to_browse_mode (impl);
7095 case OPERATION_MODE_RECENT:
7096 recent_switch_to_browse_mode (impl);
7098 case OPERATION_MODE_BROWSE:
7102 if (impl->local_only && !g_file_is_native (file))
7104 g_set_error_literal (error,
7105 GTK_FILE_CHOOSER_ERROR,
7106 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7107 _("Cannot change to folder because it is not local"));
7109 g_object_unref (file);
7110 profile_end ("end - not local", NULL);
7114 if (impl->update_current_folder_cancellable)
7115 g_cancellable_cancel (impl->update_current_folder_cancellable);
7117 /* Test validity of path here. */
7118 data = g_new0 (struct UpdateCurrentFolderData, 1);
7120 data->file = g_object_ref (file);
7121 data->keep_trail = keep_trail;
7122 data->clear_entry = clear_entry;
7124 impl->reload_state = RELOAD_HAS_FOLDER;
7126 impl->update_current_folder_cancellable =
7127 _gtk_file_system_get_info (impl->file_system, file,
7129 update_current_folder_get_info_cb,
7132 set_busy_cursor (impl, TRUE);
7133 g_object_unref (file);
7135 profile_end ("end", NULL);
7140 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7142 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7144 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7145 impl->operation_mode == OPERATION_MODE_RECENT)
7148 if (impl->reload_state == RELOAD_EMPTY)
7150 char *current_working_dir;
7153 /* We are unmapped, or we had an error while loading the last folder. We'll return
7154 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7155 * explicitly calls set_current_folder() on us.
7157 current_working_dir = g_get_current_dir ();
7158 file = g_file_new_for_path (current_working_dir);
7159 g_free (current_working_dir);
7163 if (impl->current_folder)
7164 return g_object_ref (impl->current_folder);
7170 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7173 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7175 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7176 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7178 pending_select_files_free (impl);
7179 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7183 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7187 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7191 parent_file = g_file_get_parent (file);
7194 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7196 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7197 impl->operation_mode == OPERATION_MODE_RECENT ||
7198 impl->load_state == LOAD_EMPTY)
7204 g_assert (impl->current_folder != NULL);
7206 same_path = g_file_equal (parent_file, impl->current_folder);
7209 if (same_path && impl->load_state == LOAD_FINISHED)
7214 files.data = (gpointer) file;
7217 result = show_and_select_files (impl, &files);
7218 g_object_unref (parent_file);
7222 pending_select_files_add (impl, file);
7228 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7229 g_object_unref (parent_file);
7233 g_object_unref (parent_file);
7238 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7241 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7242 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7245 if (!impl->browse_files_model)
7248 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7253 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7258 maybe_select (GtkTreeModel *model,
7263 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7264 GtkTreeSelection *selection;
7267 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7269 gtk_tree_model_get (model, iter,
7270 MODEL_COL_IS_FOLDER, &is_folder,
7273 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7274 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7275 gtk_tree_selection_select_iter (selection, iter);
7277 gtk_tree_selection_unselect_iter (selection, iter);
7283 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7285 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7287 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7288 impl->operation_mode == OPERATION_MODE_RECENT)
7290 GtkTreeSelection *selection;
7292 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7293 gtk_tree_selection_select_all (selection);
7297 if (impl->select_multiple)
7298 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7299 maybe_select, impl);
7303 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7305 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7306 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7308 gtk_tree_selection_unselect_all (selection);
7309 pending_select_files_free (impl);
7312 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7314 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7316 * is_empty_ret - whether the file entry is totally empty
7318 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7319 * the path will be "$cwd/foobar")
7322 check_save_entry (GtkFileChooserDefault *impl,
7324 gboolean *is_well_formed_ret,
7325 gboolean *is_empty_ret,
7326 gboolean *is_file_part_empty_ret,
7327 gboolean *is_folder)
7329 GtkFileChooserEntry *chooser_entry;
7330 GFile *current_folder;
7331 const char *file_part;
7335 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7336 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7337 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7338 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7339 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7341 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7343 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7346 *is_well_formed_ret = TRUE;
7347 *is_empty_ret = TRUE;
7348 *is_file_part_empty_ret = TRUE;
7354 *is_empty_ret = FALSE;
7356 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7357 if (!current_folder)
7360 *is_well_formed_ret = FALSE;
7361 *is_file_part_empty_ret = FALSE;
7367 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7369 if (!file_part || file_part[0] == '\0')
7371 *file_ret = g_object_ref (current_folder);
7372 *is_well_formed_ret = TRUE;
7373 *is_file_part_empty_ret = TRUE;
7379 *is_file_part_empty_ret = FALSE;
7382 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7386 error_building_filename_dialog (impl, error);
7388 *is_well_formed_ret = FALSE;
7395 *is_well_formed_ret = TRUE;
7396 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7399 struct get_files_closure {
7400 GtkFileChooserDefault *impl;
7402 GFile *file_from_entry;
7406 get_files_foreach (GtkTreeModel *model,
7411 struct get_files_closure *info;
7413 GtkFileSystemModel *fs_model;
7416 fs_model = info->impl->browse_files_model;
7418 file = _gtk_file_system_model_get_file (fs_model, iter);
7420 return; /* We are on the editable row */
7422 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7423 info->result = g_slist_prepend (info->result, g_object_ref (file));
7427 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7429 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7430 struct get_files_closure info;
7431 GtkWindow *toplevel;
7432 GtkWidget *current_focus;
7433 gboolean file_list_seen;
7435 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7436 return search_get_selected_files (impl);
7438 if (impl->operation_mode == OPERATION_MODE_RECENT)
7439 return recent_get_selected_files (impl);
7443 info.file_from_entry = NULL;
7445 toplevel = get_toplevel (GTK_WIDGET (impl));
7447 current_focus = gtk_window_get_focus (toplevel);
7449 current_focus = NULL;
7451 file_list_seen = FALSE;
7452 if (current_focus == impl->browse_files_tree_view)
7454 GtkTreeSelection *selection;
7458 file_list_seen = TRUE;
7459 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7460 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7462 /* If there is no selection in the file list, we probably have this situation:
7464 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7465 * 2. He then double-clicked on a folder ("bar") in the file list
7467 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7468 * filename entry to see if that is the case.
7470 if (info.result == NULL && impl->location_entry)
7473 else if (impl->location_entry && current_focus == impl->location_entry)
7475 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7479 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7484 if (!is_well_formed)
7487 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7489 g_object_unref (info.file_from_entry);
7493 if (info.file_from_entry)
7494 info.result = g_slist_prepend (info.result, info.file_from_entry);
7495 else if (!file_list_seen)
7500 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7502 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7506 /* The focus is on a dialog's action area button or something else */
7507 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7508 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7516 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7517 * fall back to the current directory */
7518 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7519 info.result == NULL)
7521 GFile *current_folder;
7523 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7526 info.result = g_slist_prepend (info.result, current_folder);
7529 return g_slist_reverse (info.result);
7533 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7535 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7537 if (impl->preview_file)
7538 return g_object_ref (impl->preview_file);
7543 static GtkFileSystem *
7544 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7546 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7548 return impl->file_system;
7551 /* Shows or hides the filter widgets */
7553 show_filters (GtkFileChooserDefault *impl,
7557 gtk_widget_show (impl->filter_combo_hbox);
7559 gtk_widget_hide (impl->filter_combo_hbox);
7563 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7564 GtkFileFilter *filter)
7566 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7569 if (g_slist_find (impl->filters, filter))
7571 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7575 g_object_ref_sink (filter);
7576 impl->filters = g_slist_append (impl->filters, filter);
7578 name = gtk_file_filter_get_name (filter);
7580 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7582 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7584 if (!g_slist_find (impl->filters, impl->current_filter))
7585 set_current_filter (impl, filter);
7587 show_filters (impl, TRUE);
7591 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7592 GtkFileFilter *filter)
7594 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7595 GtkTreeModel *model;
7599 filter_index = g_slist_index (impl->filters, filter);
7601 if (filter_index < 0)
7603 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7607 impl->filters = g_slist_remove (impl->filters, filter);
7609 if (filter == impl->current_filter)
7612 set_current_filter (impl, impl->filters->data);
7614 set_current_filter (impl, NULL);
7617 /* Remove row from the combo box */
7618 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7619 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7620 g_assert_not_reached ();
7622 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7624 g_object_unref (filter);
7627 show_filters (impl, FALSE);
7631 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7633 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7635 return g_slist_copy (impl->filters);
7638 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7640 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7643 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7646 struct AddShortcutData
7648 GtkFileChooserDefault *impl;
7653 add_shortcut_get_info_cb (GCancellable *cancellable,
7655 const GError *error,
7659 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7660 struct AddShortcutData *data = user_data;
7662 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7665 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7667 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7670 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7672 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7675 g_object_unref (data->impl);
7676 g_object_unref (data->file);
7679 g_object_unref (cancellable);
7683 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7687 GCancellable *cancellable;
7688 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7689 struct AddShortcutData *data;
7693 /* Avoid adding duplicates */
7694 pos = shortcut_find_position (impl, file);
7695 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7699 uri = g_file_get_uri (file);
7700 /* translators, "Shortcut" means "Bookmark" here */
7702 GTK_FILE_CHOOSER_ERROR,
7703 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7704 _("Shortcut %s already exists"),
7711 for (l = impl->loading_shortcuts; l; l = l->next)
7713 GCancellable *c = l->data;
7716 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7717 if (f && g_file_equal (file, f))
7721 uri = g_file_get_uri (file);
7723 GTK_FILE_CHOOSER_ERROR,
7724 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7725 _("Shortcut %s already exists"),
7733 data = g_new0 (struct AddShortcutData, 1);
7734 data->impl = g_object_ref (impl);
7735 data->file = g_object_ref (file);
7737 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7739 add_shortcut_get_info_cb, data);
7744 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7745 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7751 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7755 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7762 for (l = impl->loading_shortcuts; l; l = l->next)
7764 GCancellable *c = l->data;
7767 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7768 if (f && g_file_equal (file, f))
7770 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7771 g_cancellable_cancel (c);
7776 if (impl->num_shortcuts == 0)
7779 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7780 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7781 g_assert_not_reached ();
7783 for (i = 0; i < impl->num_shortcuts; i++)
7786 ShortcutType shortcut_type;
7789 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7790 SHORTCUTS_COL_DATA, &col_data,
7791 SHORTCUTS_COL_TYPE, &shortcut_type,
7793 g_assert (col_data != NULL);
7794 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7796 shortcut = col_data;
7797 if (g_file_equal (shortcut, file))
7799 shortcuts_remove_rows (impl, pos + i, 1);
7800 impl->num_shortcuts--;
7804 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7805 g_assert_not_reached ();
7810 uri = g_file_get_uri (file);
7811 /* translators, "Shortcut" means "Bookmark" here */
7813 GTK_FILE_CHOOSER_ERROR,
7814 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7815 _("Shortcut %s does not exist"),
7823 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7825 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7831 if (impl->num_shortcuts == 0)
7834 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7835 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7836 g_assert_not_reached ();
7840 for (i = 0; i < impl->num_shortcuts; i++)
7843 ShortcutType shortcut_type;
7846 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7847 SHORTCUTS_COL_DATA, &col_data,
7848 SHORTCUTS_COL_TYPE, &shortcut_type,
7850 g_assert (col_data != NULL);
7851 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7853 shortcut = col_data;
7854 list = g_slist_prepend (list, g_object_ref (shortcut));
7856 if (i != impl->num_shortcuts - 1)
7858 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7859 g_assert_not_reached ();
7863 return g_slist_reverse (list);
7866 /* Guesses a size based upon font sizes */
7868 find_good_size_from_style (GtkWidget *widget,
7872 GtkStyleContext *context;
7873 GtkStateFlags state;
7878 context = gtk_widget_get_style_context (widget);
7879 state = gtk_widget_get_state_flags (widget);
7881 screen = gtk_widget_get_screen (widget);
7884 resolution = gdk_screen_get_resolution (screen);
7885 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7889 resolution = 96.0; /* wheeee */
7891 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7892 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7894 *width = font_size * NUM_CHARS;
7895 *height = font_size * NUM_LINES;
7899 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7900 gint *default_width,
7901 gint *default_height)
7903 GtkFileChooserDefault *impl;
7906 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7908 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7909 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7910 || impl->expand_folders)
7912 int x, y, width, height;
7914 settings_ensure (impl);
7916 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7917 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7919 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7921 *default_width = width;
7922 *default_height = height;
7926 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7928 if (impl->preview_widget_active &&
7929 impl->preview_widget &&
7930 gtk_widget_get_visible (impl->preview_widget))
7932 gtk_widget_get_preferred_size (impl->preview_box,
7934 *default_width += PREVIEW_HBOX_SPACING + req.width;
7937 if (impl->extra_widget &&
7938 gtk_widget_get_visible (impl->extra_widget))
7940 gtk_widget_get_preferred_size (impl->extra_align,
7942 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
7947 gtk_widget_get_preferred_size (GTK_WIDGET (impl),
7949 *default_width = req.width;
7950 *default_height = req.height;
7954 struct switch_folder_closure {
7955 GtkFileChooserDefault *impl;
7960 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7962 switch_folder_foreach_cb (GtkTreeModel *model,
7967 struct switch_folder_closure *closure;
7971 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
7972 closure->num_selected++;
7975 /* Changes to the selected folder in the list view */
7977 switch_to_selected_folder (GtkFileChooserDefault *impl)
7979 GtkTreeSelection *selection;
7980 struct switch_folder_closure closure;
7982 /* We do this with foreach() rather than get_selected() as we may be in
7983 * multiple selection mode
7986 closure.impl = impl;
7987 closure.file = NULL;
7988 closure.num_selected = 0;
7990 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7991 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7993 g_assert (closure.file && closure.num_selected == 1);
7995 change_folder_and_display_error (impl, closure.file, FALSE);
7998 /* Gets the GFileInfo for the selected row in the file list; assumes single
8002 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8003 gboolean *had_selection)
8005 GtkTreeSelection *selection;
8009 g_assert (!impl->select_multiple);
8010 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8011 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8013 *had_selection = FALSE;
8017 *had_selection = TRUE;
8019 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8023 /* Gets the display name of the selected file in the file list; assumes single
8024 * selection mode and that something is selected.
8026 static const gchar *
8027 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8030 gboolean had_selection;
8032 info = get_selected_file_info_from_file_list (impl, &had_selection);
8033 g_assert (had_selection);
8034 g_assert (info != NULL);
8036 return g_file_info_get_display_name (info);
8040 add_custom_button_to_dialog (GtkDialog *dialog,
8041 const gchar *mnemonic_label,
8042 const gchar *stock_id,
8047 button = gtk_button_new_with_mnemonic (mnemonic_label);
8048 gtk_widget_set_can_default (button, TRUE);
8049 gtk_button_set_image (GTK_BUTTON (button),
8050 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8051 gtk_widget_show (button);
8053 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8056 /* Presents an overwrite confirmation dialog; returns whether we should accept
8060 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8061 const gchar *file_part,
8062 const gchar *folder_display_name)
8064 GtkWindow *toplevel;
8068 toplevel = get_toplevel (GTK_WIDGET (impl));
8070 dialog = gtk_message_dialog_new (toplevel,
8071 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8072 GTK_MESSAGE_QUESTION,
8074 _("A file named \"%s\" already exists. Do you want to replace it?"),
8076 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8077 _("The file already exists in \"%s\". Replacing it will "
8078 "overwrite its contents."),
8079 folder_display_name);
8081 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8082 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8083 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8084 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8085 GTK_RESPONSE_ACCEPT,
8086 GTK_RESPONSE_CANCEL,
8088 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8090 if (gtk_window_has_group (toplevel))
8091 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8092 GTK_WINDOW (dialog));
8094 response = gtk_dialog_run (GTK_DIALOG (dialog));
8096 gtk_widget_destroy (dialog);
8098 return (response == GTK_RESPONSE_ACCEPT);
8101 struct GetDisplayNameData
8103 GtkFileChooserDefault *impl;
8108 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8110 const GError *error,
8113 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8114 gboolean should_respond = FALSE;
8115 struct GetDisplayNameData *data = user_data;
8117 if (cancellable != data->impl->should_respond_get_info_cancellable)
8120 data->impl->should_respond_get_info_cancellable = NULL;
8126 /* Huh? Did the folder disappear? Let the caller deal with it */
8127 should_respond = TRUE;
8129 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8131 set_busy_cursor (data->impl, FALSE);
8133 g_signal_emit_by_name (data->impl, "response-requested");
8136 g_object_unref (data->impl);
8137 g_free (data->file_part);
8140 g_object_unref (cancellable);
8143 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8144 * should respond. Can get the file part from the file list or the save entry.
8147 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8148 const gchar *file_part,
8151 GtkFileChooserConfirmation conf;
8153 if (!impl->do_overwrite_confirmation)
8156 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8158 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8162 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8164 struct GetDisplayNameData *data;
8166 g_assert (file_part != NULL);
8168 data = g_new0 (struct GetDisplayNameData, 1);
8169 data->impl = g_object_ref (impl);
8170 data->file_part = g_strdup (file_part);
8172 if (impl->should_respond_get_info_cancellable)
8173 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8175 impl->should_respond_get_info_cancellable =
8176 _gtk_file_system_get_info (impl->file_system, parent_file,
8177 "standard::display-name",
8178 confirmation_confirm_get_info_cb,
8180 set_busy_cursor (data->impl, TRUE);
8184 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8187 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8191 g_assert_not_reached ();
8196 struct FileExistsData
8198 GtkFileChooserDefault *impl;
8199 gboolean file_exists_and_is_not_folder;
8205 name_entry_get_parent_info_cb (GCancellable *cancellable,
8207 const GError *error,
8210 gboolean parent_is_folder;
8211 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8212 struct FileExistsData *data = user_data;
8214 if (cancellable != data->impl->should_respond_get_info_cancellable)
8217 data->impl->should_respond_get_info_cancellable = NULL;
8219 set_busy_cursor (data->impl, FALSE);
8225 parent_is_folder = FALSE;
8227 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8229 if (parent_is_folder)
8231 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8233 g_signal_emit_by_name (data->impl, "response-requested"); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8235 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8237 if (data->file_exists_and_is_not_folder)
8242 /* Dup the string because the string may be modified
8243 * depending on what clients do in the confirm-overwrite
8244 * signal and this corrupts the pointer
8246 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8247 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8251 g_signal_emit_by_name (data->impl, "response-requested");
8254 g_signal_emit_by_name (data->impl, "response-requested");
8256 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8257 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8259 GError *mkdir_error = NULL;
8261 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8262 * "/blah/nonexistent" you *will* want a folder created.
8265 set_busy_cursor (data->impl, TRUE);
8266 g_file_make_directory (data->file, NULL, &mkdir_error);
8267 set_busy_cursor (data->impl, FALSE);
8270 g_signal_emit_by_name (data->impl, "response-requested");
8272 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8275 g_assert_not_reached ();
8281 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8282 error_with_file_under_nonfolder (data->impl, data->parent_file);
8288 /* The parent folder is not readable for some reason */
8290 error_copy = g_error_copy (error);
8291 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8296 g_object_unref (data->impl);
8297 g_object_unref (data->file);
8298 g_object_unref (data->parent_file);
8301 g_object_unref (cancellable);
8305 file_exists_get_info_cb (GCancellable *cancellable,
8307 const GError *error,
8310 gboolean data_ownership_taken = FALSE;
8311 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8312 gboolean file_exists;
8314 gboolean needs_parent_check = FALSE;
8315 struct FileExistsData *data = user_data;
8317 if (cancellable != data->impl->file_exists_get_info_cancellable)
8320 data->impl->file_exists_get_info_cancellable = NULL;
8322 set_busy_cursor (data->impl, FALSE);
8327 file_exists = (info != NULL);
8328 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8330 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8333 change_folder_and_display_error (data->impl, data->file, TRUE);
8337 g_signal_emit_by_name (data->impl, "response-requested"); /* user typed an existing filename; we are done */
8339 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8342 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8344 if (file_exists && !is_folder)
8346 /* Oops, the user typed the name of an existing path which is not
8349 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8350 g_error_copy (error));
8354 needs_parent_check = TRUE;
8357 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8361 needs_parent_check = TRUE;
8367 /* User typed a folder; we are done */
8368 g_signal_emit_by_name (data->impl, "response-requested");
8371 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8374 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8377 change_folder_and_display_error (data->impl, data->file, TRUE);
8379 needs_parent_check = TRUE;
8383 g_assert_not_reached();
8386 if (needs_parent_check) {
8387 /* check that everything up to the last path component exists (i.e. the parent) */
8389 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8390 data_ownership_taken = TRUE;
8392 if (data->impl->should_respond_get_info_cancellable)
8393 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8395 data->impl->should_respond_get_info_cancellable =
8396 _gtk_file_system_get_info (data->impl->file_system,
8399 name_entry_get_parent_info_cb,
8401 set_busy_cursor (data->impl, TRUE);
8405 if (!data_ownership_taken)
8407 g_object_unref (data->impl);
8408 g_object_unref (data->file);
8409 g_object_unref (data->parent_file);
8413 g_object_unref (cancellable);
8417 paste_text_received (GtkClipboard *clipboard,
8419 GtkFileChooserDefault *impl)
8426 file = g_file_new_for_uri (text);
8428 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8429 location_popup_handler (impl, text);
8431 g_object_unref (file);
8434 /* Handler for the "location-popup-on-paste" keybinding signal */
8436 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8438 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8439 GDK_SELECTION_CLIPBOARD);
8440 gtk_clipboard_request_text (clipboard,
8441 (GtkClipboardTextReceivedFunc) paste_text_received,
8446 /* Implementation for GtkFileChooserEmbed::should_respond() */
8448 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8450 GtkFileChooserDefault *impl;
8451 GtkWidget *toplevel;
8452 GtkWidget *current_focus;
8454 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8456 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8457 g_assert (GTK_IS_WINDOW (toplevel));
8459 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8461 if (current_focus == impl->browse_files_tree_view)
8463 /* The following array encodes what we do based on the impl->action and the
8464 * number of files selected.
8467 NOOP, /* Do nothing (don't respond) */
8468 RESPOND, /* Respond immediately */
8469 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8470 ALL_FILES, /* Respond only if everything selected is a file */
8471 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8472 SAVE_ENTRY, /* Go to the code for handling the save entry */
8473 NOT_REACHED /* Sanity check */
8475 static const ActionToTake what_to_do[4][3] = {
8476 /* 0 selected 1 selected many selected */
8477 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8478 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8479 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8480 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8484 gboolean all_files, all_folders;
8486 ActionToTake action;
8490 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8492 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8493 return search_should_respond (impl);
8495 if (impl->operation_mode == OPERATION_MODE_RECENT)
8496 return recent_should_respond (impl);
8498 selection_check (impl, &num_selected, &all_files, &all_folders);
8500 if (num_selected > 2)
8505 action = what_to_do [impl->action] [k];
8515 case RESPOND_OR_SWITCH:
8516 g_assert (num_selected == 1);
8520 switch_to_selected_folder (impl);
8523 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8524 return should_respond_after_confirm_overwrite (impl,
8525 get_display_name_from_file_list (impl),
8526 impl->current_folder);
8540 g_assert_not_reached ();
8543 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8546 gboolean is_well_formed, is_empty, is_file_part_empty;
8549 GtkFileChooserEntry *entry;
8554 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8555 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8556 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8557 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8558 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8560 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8561 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8563 if (!is_well_formed)
8568 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8574 g_assert (file != NULL);
8579 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8580 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8582 change_folder_and_display_error (impl, file, TRUE);
8585 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8586 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8588 /* The folder already exists, so we do not need to create it.
8589 * Just respond to terminate the dialog.
8595 g_assert_not_reached ();
8601 struct FileExistsData *data;
8603 /* We need to check whether file exists and whether it is a folder -
8604 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8605 * negative (it doesn't know yet if your last path component is a
8609 data = g_new0 (struct FileExistsData, 1);
8610 data->impl = g_object_ref (impl);
8611 data->file = g_object_ref (file);
8612 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8614 if (impl->file_exists_get_info_cancellable)
8615 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8617 impl->file_exists_get_info_cancellable =
8618 _gtk_file_system_get_info (impl->file_system, file,
8620 file_exists_get_info_cb,
8623 set_busy_cursor (impl, TRUE);
8627 g_error_free (error);
8630 g_object_unref (file);
8633 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8635 /* The focus is on a dialog's action area button, *and* the widget that
8636 * was focused immediately before it is the file list.
8640 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8642 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8645 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8647 /* The focus is on a dialog's action area button, *and* the widget that
8648 * was focused immediately before it is the location entry.
8653 /* The focus is on a dialog's action area button or something else */
8654 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8655 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8660 g_assert_not_reached ();
8664 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8666 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8668 GtkFileChooserDefault *impl;
8671 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8673 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8674 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8676 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8677 widget = impl->browse_files_tree_view;
8679 widget = impl->location_entry;
8681 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8682 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8683 widget = impl->location_entry;
8686 g_assert_not_reached ();
8690 g_assert (widget != NULL);
8691 gtk_widget_grab_focus (widget);
8694 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8696 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8706 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8707 *list = g_slist_prepend (*list, g_object_ref (file));
8710 /* Constructs a list of the selected paths in search mode */
8712 search_get_selected_files (GtkFileChooserDefault *impl)
8715 GtkTreeSelection *selection;
8719 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8720 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8721 result = g_slist_reverse (result);
8726 /* Called from ::should_respond(). We return whether there are selected files
8727 * in the search list.
8730 search_should_respond (GtkFileChooserDefault *impl)
8732 GtkTreeSelection *selection;
8734 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8736 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8737 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8740 /* Adds one hit from the search engine to the search_model */
8742 search_add_hit (GtkFileChooserDefault *impl,
8747 file = g_file_new_for_uri (uri);
8751 if (!g_file_is_native (file))
8753 g_object_unref (file);
8757 _gtk_file_system_model_add_and_query_file (impl->search_model,
8761 g_object_unref (file);
8764 /* Callback used from GtkSearchEngine when we get new hits */
8766 search_engine_hits_added_cb (GtkSearchEngine *engine,
8770 GtkFileChooserDefault *impl;
8773 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8775 for (l = hits; l; l = l->next)
8776 search_add_hit (impl, (gchar*)l->data);
8779 /* Callback used from GtkSearchEngine when the query is done running */
8781 search_engine_finished_cb (GtkSearchEngine *engine,
8784 GtkFileChooserDefault *impl;
8786 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8789 /* EB: setting the model here will avoid loads of row events,
8790 * but it'll make the search look like blocked.
8792 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8793 GTK_TREE_MODEL (impl->search_model));
8796 /* FMQ: if search was empty, say that we got no hits */
8797 set_busy_cursor (impl, FALSE);
8800 /* Displays a generic error when we cannot create a GtkSearchEngine.
8801 * It would be better if _gtk_search_engine_new() gave us a GError
8802 * with a better message, but it doesn't do that right now.
8805 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8807 error_message (impl,
8808 _("Could not start the search process"),
8809 _("The program was not able to create a connection to the indexer "
8810 "daemon. Please make sure it is running."));
8814 search_engine_error_cb (GtkSearchEngine *engine,
8815 const gchar *message,
8818 GtkFileChooserDefault *impl;
8820 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8822 search_stop_searching (impl, TRUE);
8823 error_message (impl, _("Could not send the search request"), message);
8825 set_busy_cursor (impl, FALSE);
8828 /* Frees the data in the search_model */
8830 search_clear_model (GtkFileChooserDefault *impl,
8831 gboolean remove_from_treeview)
8833 if (!impl->search_model)
8836 g_object_unref (impl->search_model);
8837 impl->search_model = NULL;
8839 if (remove_from_treeview)
8840 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8843 /* Stops any ongoing searches; does not touch the search_model */
8845 search_stop_searching (GtkFileChooserDefault *impl,
8846 gboolean remove_query)
8848 if (remove_query && impl->search_query)
8850 g_object_unref (impl->search_query);
8851 impl->search_query = NULL;
8854 if (impl->search_engine)
8856 _gtk_search_engine_stop (impl->search_engine);
8858 g_object_unref (impl->search_engine);
8859 impl->search_engine = NULL;
8863 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8865 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8867 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8869 search_stop_searching (impl, FALSE);
8870 search_clear_model (impl, TRUE);
8872 gtk_widget_destroy (impl->search_hbox);
8873 impl->search_hbox = NULL;
8874 impl->search_entry = NULL;
8876 gtk_widget_show (impl->browse_path_bar);
8877 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8878 gtk_widget_hide (impl->browse_new_folder_button);
8880 gtk_widget_show (impl->browse_new_folder_button);
8883 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8884 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8886 gtk_widget_show (impl->location_button);
8888 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8889 gtk_widget_show (impl->location_entry_box);
8892 impl->operation_mode = OPERATION_MODE_BROWSE;
8894 file_list_set_sort_column_ids (impl);
8897 /* Creates the search_model and puts it in the tree view */
8899 search_setup_model (GtkFileChooserDefault *impl)
8901 g_assert (impl->search_model == NULL);
8903 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8905 MODEL_COLUMN_TYPES);
8907 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8911 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8915 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8919 set_sort_column (impl);
8921 /* EB: setting the model here will make the hits list update feel
8922 * more "alive" than setting the model at the end of the search
8925 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8926 GTK_TREE_MODEL (impl->search_model));
8929 /* Creates a new query with the specified text and launches it */
8931 search_start_query (GtkFileChooserDefault *impl,
8932 const gchar *query_text)
8934 search_stop_searching (impl, FALSE);
8935 search_clear_model (impl, TRUE);
8936 search_setup_model (impl);
8937 set_busy_cursor (impl, TRUE);
8939 if (impl->search_engine == NULL)
8940 impl->search_engine = _gtk_search_engine_new ();
8942 if (!impl->search_engine)
8944 set_busy_cursor (impl, FALSE);
8945 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
8949 if (!impl->search_query)
8951 impl->search_query = _gtk_query_new ();
8952 _gtk_query_set_text (impl->search_query, query_text);
8955 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
8957 g_signal_connect (impl->search_engine, "hits-added",
8958 G_CALLBACK (search_engine_hits_added_cb), impl);
8959 g_signal_connect (impl->search_engine, "finished",
8960 G_CALLBACK (search_engine_finished_cb), impl);
8961 g_signal_connect (impl->search_engine, "error",
8962 G_CALLBACK (search_engine_error_cb), impl);
8964 _gtk_search_engine_start (impl->search_engine);
8967 /* Callback used when the user presses Enter while typing on the search
8968 * entry; starts the query
8971 search_entry_activate_cb (GtkEntry *entry,
8974 GtkFileChooserDefault *impl;
8977 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8979 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
8980 if (strlen (text) == 0)
8983 /* reset any existing query object */
8984 if (impl->search_query)
8986 g_object_unref (impl->search_query);
8987 impl->search_query = NULL;
8990 search_start_query (impl, text);
8994 focus_entry_idle_cb (GtkFileChooserDefault *impl)
8996 GDK_THREADS_ENTER ();
8998 g_source_destroy (impl->focus_entry_idle);
8999 impl->focus_entry_idle = NULL;
9001 if (impl->search_entry)
9002 gtk_widget_grab_focus (impl->search_entry);
9004 GDK_THREADS_LEAVE ();
9010 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9012 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9013 * pane, we get a selection-changed signal and we set up the search widgets.
9014 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9015 * the change to the selection. So, we need to re-focus the search entry
9016 * after the treeview has finished doing its work; we'll do that in an idle
9020 if (!impl->focus_entry_idle)
9021 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9024 /* Hides the path bar and creates the search entry */
9026 search_setup_widgets (GtkFileChooserDefault *impl)
9032 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9036 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9037 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9041 label = gtk_label_new (NULL);
9042 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9043 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9044 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9049 impl->search_entry = gtk_entry_new ();
9050 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9051 g_signal_connect (impl->search_entry, "activate",
9052 G_CALLBACK (search_entry_activate_cb),
9054 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9056 /* if there already is a query, restart it */
9057 if (impl->search_query)
9059 gchar *query = _gtk_query_get_text (impl->search_query);
9063 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9064 search_start_query (impl, query);
9070 g_object_unref (impl->search_query);
9071 impl->search_query = NULL;
9075 gtk_widget_hide (impl->browse_path_bar);
9076 gtk_widget_hide (impl->browse_new_folder_button);
9078 /* Box for search widgets */
9079 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9080 gtk_widget_show_all (impl->search_hbox);
9081 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9083 /* Hide the location widgets temporarily */
9085 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9086 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9088 gtk_widget_hide (impl->location_button);
9089 gtk_widget_hide (impl->location_entry_box);
9092 focus_search_entry_in_idle (impl);
9094 /* FMQ: hide the filter combo? */
9097 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9099 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9103 case OPERATION_MODE_BROWSE:
9104 stop_loading_and_clear_list_model (impl, TRUE);
9107 case OPERATION_MODE_SEARCH:
9108 search_stop_searching (impl, FALSE);
9109 search_clear_model (impl, TRUE);
9111 gtk_widget_destroy (impl->search_hbox);
9112 impl->search_hbox = NULL;
9113 impl->search_entry = NULL;
9116 case OPERATION_MODE_RECENT:
9117 recent_stop_loading (impl);
9118 recent_clear_model (impl, TRUE);
9120 gtk_widget_destroy (impl->recent_hbox);
9121 impl->recent_hbox = NULL;
9126 /* Main entry point to the searching functions; this gets called when the user
9127 * activates the Search shortcut.
9130 search_activate (GtkFileChooserDefault *impl)
9132 OperationMode previous_mode;
9134 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9136 focus_search_entry_in_idle (impl);
9140 previous_mode = impl->operation_mode;
9141 impl->operation_mode = OPERATION_MODE_SEARCH;
9143 stop_operation (impl, previous_mode);
9145 g_assert (impl->search_hbox == NULL);
9146 g_assert (impl->search_entry == NULL);
9147 g_assert (impl->search_model == NULL);
9149 search_setup_widgets (impl);
9150 file_list_set_sort_column_ids (impl);
9154 * Recent files support
9157 /* Frees the data in the recent_model */
9159 recent_clear_model (GtkFileChooserDefault *impl,
9160 gboolean remove_from_treeview)
9162 if (!impl->recent_model)
9165 if (remove_from_treeview)
9166 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9168 g_object_unref (impl->recent_model);
9169 impl->recent_model = NULL;
9172 /* Stops any ongoing loading of the recent files list; does
9173 * not touch the recent_model
9176 recent_stop_loading (GtkFileChooserDefault *impl)
9178 if (impl->load_recent_id)
9180 g_source_remove (impl->load_recent_id);
9181 impl->load_recent_id = 0;
9185 /* Stops any pending load, clears the file list, and switches
9186 * back to OPERATION_MODE_BROWSE
9189 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9191 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9193 recent_stop_loading (impl);
9194 recent_clear_model (impl, TRUE);
9196 gtk_widget_destroy (impl->recent_hbox);
9197 impl->recent_hbox = NULL;
9199 gtk_widget_show (impl->browse_path_bar);
9200 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9201 gtk_widget_hide (impl->browse_new_folder_button);
9203 gtk_widget_show (impl->browse_new_folder_button);
9205 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9206 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9208 gtk_widget_show (impl->location_button);
9210 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9211 gtk_widget_show (impl->location_entry_box);
9214 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9216 impl->operation_mode = OPERATION_MODE_BROWSE;
9218 file_list_set_sort_column_ids (impl);
9222 recent_setup_model (GtkFileChooserDefault *impl)
9224 g_assert (impl->recent_model == NULL);
9226 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9228 MODEL_COLUMN_TYPES);
9230 _gtk_file_system_model_set_filter (impl->recent_model,
9231 impl->current_filter);
9232 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9236 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9240 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9244 set_sort_column (impl);
9249 GtkFileChooserDefault *impl;
9252 gint n_loaded_items;
9253 guint needs_sorting : 1;
9257 recent_idle_cleanup (gpointer data)
9259 RecentLoadData *load_data = data;
9260 GtkFileChooserDefault *impl = load_data->impl;
9262 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9263 GTK_TREE_MODEL (impl->recent_model));
9265 set_busy_cursor (impl, FALSE);
9267 impl->load_recent_id = 0;
9269 if (load_data->items)
9271 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9272 g_list_free (load_data->items);
9279 recent_sort_mru (gconstpointer a,
9282 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9283 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9285 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9289 get_recent_files_limit (GtkWidget *widget)
9291 GtkSettings *settings;
9294 if (gtk_widget_has_screen (widget))
9295 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9297 settings = gtk_settings_get_default ();
9299 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9305 recent_idle_load (gpointer data)
9307 RecentLoadData *load_data = data;
9308 GtkFileChooserDefault *impl = load_data->impl;
9312 if (!impl->recent_manager)
9315 /* first iteration: load all the items */
9316 if (!load_data->items)
9318 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9319 if (!load_data->items)
9322 load_data->needs_sorting = TRUE;
9327 /* second iteration: preliminary MRU sorting and clamping */
9328 if (load_data->needs_sorting)
9332 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9333 load_data->n_items = g_list_length (load_data->items);
9335 limit = get_recent_files_limit (GTK_WIDGET (impl));
9337 if (limit != -1 && (load_data->n_items > limit))
9341 clamp = g_list_nth (load_data->items, limit - 1);
9342 if (G_LIKELY (clamp))
9347 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9350 load_data->n_items = limit;
9354 load_data->n_loaded_items = 0;
9355 load_data->needs_sorting = FALSE;
9360 /* finished loading items */
9361 for (walk = load_data->items; walk; walk = walk->next)
9363 GtkRecentInfo *info = walk->data;
9364 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9366 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9369 gtk_recent_info_unref (walk->data);
9370 g_object_unref (file);
9373 g_list_free (load_data->items);
9374 load_data->items = NULL;
9380 recent_start_loading (GtkFileChooserDefault *impl)
9382 RecentLoadData *load_data;
9384 recent_stop_loading (impl);
9385 recent_clear_model (impl, TRUE);
9386 recent_setup_model (impl);
9387 set_busy_cursor (impl, TRUE);
9389 g_assert (impl->load_recent_id == 0);
9391 load_data = g_new (RecentLoadData, 1);
9392 load_data->impl = impl;
9393 load_data->items = NULL;
9394 load_data->n_items = 0;
9395 load_data->n_loaded_items = 0;
9396 load_data->needs_sorting = TRUE;
9398 /* begin lazy loading the recent files into the model */
9399 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9402 recent_idle_cleanup);
9406 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9416 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9417 *list = g_slist_prepend (*list, file);
9420 /* Constructs a list of the selected paths in recent files mode */
9422 recent_get_selected_files (GtkFileChooserDefault *impl)
9425 GtkTreeSelection *selection;
9429 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9430 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9431 result = g_slist_reverse (result);
9436 /* Called from ::should_respond(). We return whether there are selected
9437 * files in the recent files list.
9440 recent_should_respond (GtkFileChooserDefault *impl)
9442 GtkTreeSelection *selection;
9444 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9446 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9447 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9450 /* Hide the location widgets temporarily */
9452 recent_hide_entry (GtkFileChooserDefault *impl)
9458 impl->recent_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9461 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9462 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9465 label = gtk_label_new (NULL);
9466 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9467 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9468 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9471 gtk_widget_hide (impl->browse_path_bar);
9472 gtk_widget_hide (impl->browse_new_folder_button);
9474 /* Box for recent widgets */
9475 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9476 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->recent_hbox);
9477 gtk_widget_show_all (impl->recent_hbox);
9479 /* Hide the location widgets temporarily */
9480 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9481 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9483 gtk_widget_hide (impl->location_button);
9484 gtk_widget_hide (impl->location_entry_box);
9488 /* Main entry point to the recent files functions; this gets called when
9489 * the user activates the Recently Used shortcut.
9492 recent_activate (GtkFileChooserDefault *impl)
9494 OperationMode previous_mode;
9496 if (impl->operation_mode == OPERATION_MODE_RECENT)
9499 previous_mode = impl->operation_mode;
9500 impl->operation_mode = OPERATION_MODE_RECENT;
9502 stop_operation (impl, previous_mode);
9504 recent_hide_entry (impl);
9506 file_list_set_sort_column_ids (impl);
9507 recent_start_loading (impl);
9511 set_current_filter (GtkFileChooserDefault *impl,
9512 GtkFileFilter *filter)
9514 if (impl->current_filter != filter)
9518 /* NULL filters are allowed to reset to non-filtered status
9520 filter_index = g_slist_index (impl->filters, filter);
9521 if (impl->filters && filter && filter_index < 0)
9524 if (impl->current_filter)
9525 g_object_unref (impl->current_filter);
9526 impl->current_filter = filter;
9527 if (impl->current_filter)
9529 g_object_ref_sink (impl->current_filter);
9533 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9536 if (impl->browse_files_model)
9537 install_list_model_filter (impl);
9539 if (impl->search_model)
9540 _gtk_file_system_model_set_filter (impl->search_model, filter);
9542 if (impl->recent_model)
9543 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9545 g_object_notify (G_OBJECT (impl), "filter");
9550 filter_combo_changed (GtkComboBox *combo_box,
9551 GtkFileChooserDefault *impl)
9553 gint new_index = gtk_combo_box_get_active (combo_box);
9554 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9556 set_current_filter (impl, new_filter);
9560 check_preview_change (GtkFileChooserDefault *impl)
9562 GtkTreePath *cursor_path;
9564 char *new_display_name;
9565 GtkTreeModel *model;
9567 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9568 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9573 gtk_tree_model_get_iter (model, &iter, cursor_path);
9574 gtk_tree_model_get (model, &iter,
9575 MODEL_COL_FILE, &new_file,
9576 MODEL_COL_NAME, &new_display_name,
9579 gtk_tree_path_free (cursor_path);
9584 new_display_name = NULL;
9587 if (new_file != impl->preview_file &&
9588 !(new_file && impl->preview_file &&
9589 g_file_equal (new_file, impl->preview_file)))
9591 if (impl->preview_file)
9593 g_object_unref (impl->preview_file);
9594 g_free (impl->preview_display_name);
9599 impl->preview_file = new_file;
9600 impl->preview_display_name = new_display_name;
9604 impl->preview_file = NULL;
9605 impl->preview_display_name = NULL;
9606 g_free (new_display_name);
9609 if (impl->use_preview_label && impl->preview_label)
9610 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9612 g_signal_emit_by_name (impl, "update-preview");
9617 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9618 GtkFileSystemVolume *volume,
9619 const GError *error,
9623 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9624 GtkFileChooserDefault *impl = data;
9626 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9629 impl->shortcuts_activate_iter_cancellable = NULL;
9631 set_busy_cursor (impl, FALSE);
9638 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9642 name = _gtk_file_system_volume_get_display_name (volume);
9643 msg = g_strdup_printf (_("Could not mount %s"), name);
9645 error_message (impl, msg, error->message);
9654 file = _gtk_file_system_volume_get_root (volume);
9657 change_folder_and_display_error (impl, file, FALSE);
9658 g_object_unref (file);
9662 g_object_unref (impl);
9663 g_object_unref (cancellable);
9667 /* Activates a volume by mounting it if necessary and then switching to its
9671 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9672 GtkFileSystemVolume *volume)
9676 switch (impl->operation_mode)
9678 case OPERATION_MODE_BROWSE:
9680 case OPERATION_MODE_SEARCH:
9681 search_switch_to_browse_mode (impl);
9683 case OPERATION_MODE_RECENT:
9684 recent_switch_to_browse_mode (impl);
9688 /* We ref the file chooser since volume_mount() may run a main loop, and the
9689 * user could close the file chooser window in the meantime.
9691 g_object_ref (impl);
9693 if (!_gtk_file_system_volume_is_mounted (volume))
9695 GMountOperation *mount_op;
9697 set_busy_cursor (impl, TRUE);
9699 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9700 impl->shortcuts_activate_iter_cancellable =
9701 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9702 shortcuts_activate_volume_mount_cb,
9703 g_object_ref (impl));
9704 g_object_unref (mount_op);
9708 file = _gtk_file_system_volume_get_root (volume);
9711 change_folder_and_display_error (impl, file, FALSE);
9712 g_object_unref (file);
9716 g_object_unref (impl);
9719 /* Opens the folder or volume at the specified iter in the shortcuts model */
9720 struct ShortcutsActivateData
9722 GtkFileChooserDefault *impl;
9727 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9729 const GError *error,
9732 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9733 struct ShortcutsActivateData *data = user_data;
9735 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9738 data->impl->shortcuts_activate_iter_cancellable = NULL;
9743 if (!error && _gtk_file_info_consider_as_directory (info))
9744 change_folder_and_display_error (data->impl, data->file, FALSE);
9746 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9751 g_object_unref (data->impl);
9752 g_object_unref (data->file);
9755 g_object_unref (cancellable);
9759 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9760 GtkFileSystemVolume *volume,
9761 const GError *error,
9764 struct ShortcutsActivateData *data = user_data;
9768 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9770 g_object_unref (data->impl);
9771 g_object_unref (data->file);
9777 data->impl->shortcuts_activate_iter_cancellable =
9778 _gtk_file_system_get_info (data->impl->file_system, data->file,
9780 shortcuts_activate_get_info_cb, data);
9783 _gtk_file_system_volume_unref (volume);
9787 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9791 ShortcutType shortcut_type;
9793 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9794 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9795 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9796 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9798 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9799 SHORTCUTS_COL_DATA, &col_data,
9800 SHORTCUTS_COL_TYPE, &shortcut_type,
9803 if (impl->shortcuts_activate_iter_cancellable)
9805 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9806 impl->shortcuts_activate_iter_cancellable = NULL;
9809 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9811 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9813 GtkFileSystemVolume *volume;
9817 shortcuts_activate_volume (impl, volume);
9819 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9821 struct ShortcutsActivateData *data;
9822 GtkFileSystemVolume *volume;
9824 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9826 data = g_new0 (struct ShortcutsActivateData, 1);
9827 data->impl = g_object_ref (impl);
9828 data->file = g_object_ref (col_data);
9830 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9832 GMountOperation *mount_operation;
9833 GtkWidget *toplevel;
9835 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9837 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9839 impl->shortcuts_activate_iter_cancellable =
9840 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9842 shortcuts_activate_mount_enclosing_volume,
9847 impl->shortcuts_activate_iter_cancellable =
9848 _gtk_file_system_get_info (impl->file_system, data->file,
9850 shortcuts_activate_get_info_cb, data);
9853 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9855 search_activate (impl);
9857 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9859 recent_activate (impl);
9863 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9865 shortcuts_key_press_event_cb (GtkWidget *widget,
9867 GtkFileChooserDefault *impl)
9871 modifiers = gtk_accelerator_get_default_mod_mask ();
9873 if (key_is_left_or_right (event))
9875 gtk_widget_grab_focus (impl->browse_files_tree_view);
9879 if ((event->keyval == GDK_KEY_BackSpace
9880 || event->keyval == GDK_KEY_Delete
9881 || event->keyval == GDK_KEY_KP_Delete)
9882 && (event->state & modifiers) == 0)
9884 remove_selected_bookmarks (impl);
9888 if ((event->keyval == GDK_KEY_F2)
9889 && (event->state & modifiers) == 0)
9891 rename_selected_bookmark (impl);
9899 shortcuts_select_func (GtkTreeSelection *selection,
9900 GtkTreeModel *model,
9902 gboolean path_currently_selected,
9905 GtkFileChooserDefault *impl = data;
9906 GtkTreeIter filter_iter;
9907 ShortcutType shortcut_type;
9909 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9910 g_assert_not_reached ();
9912 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9914 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9918 list_select_func (GtkTreeSelection *selection,
9919 GtkTreeModel *model,
9921 gboolean path_currently_selected,
9924 GtkFileChooserDefault *impl = data;
9926 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
9927 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9932 if (!gtk_tree_model_get_iter (model, &iter, path))
9934 gtk_tree_model_get (model, &iter,
9935 MODEL_COL_IS_FOLDER, &is_folder,
9945 list_selection_changed (GtkTreeSelection *selection,
9946 GtkFileChooserDefault *impl)
9948 /* See if we are in the new folder editable row for Save mode */
9949 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
9950 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9953 gboolean had_selection;
9955 info = get_selected_file_info_from_file_list (impl, &had_selection);
9957 goto out; /* normal processing */
9960 return; /* We are on the editable row for New Folder */
9965 if (impl->location_entry)
9966 update_chooser_entry (impl);
9968 check_preview_change (impl);
9969 bookmarks_check_add_sensitivity (impl);
9971 g_signal_emit_by_name (impl, "selection-changed", 0);
9974 /* Callback used when a row in the file list is activated */
9976 list_row_activated (GtkTreeView *tree_view,
9978 GtkTreeViewColumn *column,
9979 GtkFileChooserDefault *impl)
9983 GtkTreeModel *model;
9986 model = gtk_tree_view_get_model (tree_view);
9988 if (!gtk_tree_model_get_iter (model, &iter, path))
9991 gtk_tree_model_get (model, &iter,
9992 MODEL_COL_FILE, &file,
9993 MODEL_COL_IS_FOLDER, &is_folder,
9996 if (is_folder && file)
9998 change_folder_and_display_error (impl, file, FALSE);
10002 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10003 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10004 g_signal_emit_by_name (impl, "file-activated");
10007 g_object_unref (file);
10011 path_bar_clicked (GtkPathBar *path_bar,
10014 gboolean child_is_hidden,
10015 GtkFileChooserDefault *impl)
10018 pending_select_files_add (impl, child_file);
10020 if (!change_folder_and_display_error (impl, file, FALSE))
10023 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10024 * show hidden files so that ".baz" appears in the file list, as it will still
10025 * be shown in the path bar: "/foo/[bar]/.baz"
10027 if (child_is_hidden)
10028 g_object_set (impl, "show-hidden", TRUE, NULL);
10032 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10034 GtkTreeViewColumn *column;
10035 GtkCellRenderer *renderer;
10036 GList *walk, *list;
10037 gboolean always_sensitive;
10039 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10040 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10042 /* Keep the following column numbers in sync with create_file_list() */
10045 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10046 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10047 for (walk = list; walk; walk = walk->next)
10049 renderer = walk->data;
10050 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10052 gtk_tree_view_column_set_attributes (column, renderer,
10053 "pixbuf", MODEL_COL_PIXBUF,
10058 gtk_tree_view_column_set_attributes (column, renderer,
10059 "text", MODEL_COL_NAME,
10060 "ellipsize", MODEL_COL_ELLIPSIZE,
10063 if (always_sensitive)
10064 g_object_set (renderer, "sensitive", TRUE, NULL);
10066 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10068 g_list_free (list);
10071 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10072 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10073 renderer = list->data;
10074 gtk_tree_view_column_set_attributes (column, renderer,
10075 "text", MODEL_COL_SIZE_TEXT,
10077 if (always_sensitive)
10078 g_object_set (renderer, "sensitive", TRUE, NULL);
10080 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10081 g_list_free (list);
10084 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10085 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10086 renderer = list->data;
10087 gtk_tree_view_column_set_attributes (column, renderer,
10088 "text", MODEL_COL_MTIME_TEXT,
10090 if (always_sensitive)
10091 g_object_set (renderer, "sensitive", TRUE, NULL);
10093 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10094 g_list_free (list);
10098 _gtk_file_chooser_default_new (void)
10100 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10104 location_set_user_text (GtkFileChooserDefault *impl,
10107 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10108 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10112 location_popup_handler (GtkFileChooserDefault *impl,
10115 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10117 GtkWidget *widget_to_focus;
10119 /* This will give us the location widgets back */
10120 switch (impl->operation_mode)
10122 case OPERATION_MODE_SEARCH:
10123 search_switch_to_browse_mode (impl);
10125 case OPERATION_MODE_RECENT:
10126 recent_switch_to_browse_mode (impl);
10128 case OPERATION_MODE_BROWSE:
10129 g_assert_not_reached ();
10133 if (impl->current_folder)
10134 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10136 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10137 widget_to_focus = impl->browse_files_tree_view;
10139 widget_to_focus = impl->location_entry;
10141 gtk_widget_grab_focus (widget_to_focus);
10145 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10146 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10148 LocationMode new_mode;
10152 /* since the user typed something, we unconditionally want to turn on the entry */
10153 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10155 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10156 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10157 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10158 new_mode = LOCATION_MODE_PATH_BAR;
10161 g_assert_not_reached ();
10165 location_mode_set (impl, new_mode, TRUE);
10166 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10169 location_set_user_text (impl, path);
10172 location_entry_set_initial_text (impl);
10173 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10177 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10178 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10180 gtk_widget_grab_focus (impl->location_entry);
10182 location_set_user_text (impl, path);
10185 g_assert_not_reached ();
10188 /* Handler for the "up-folder" keybinding signal */
10190 up_folder_handler (GtkFileChooserDefault *impl)
10192 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10195 /* Handler for the "down-folder" keybinding signal */
10197 down_folder_handler (GtkFileChooserDefault *impl)
10199 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10202 /* Switches to the shortcut in the specified index */
10204 switch_to_shortcut (GtkFileChooserDefault *impl,
10209 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10210 g_assert_not_reached ();
10212 shortcuts_activate_iter (impl, &iter);
10215 /* Handler for the "home-folder" keybinding signal */
10217 home_folder_handler (GtkFileChooserDefault *impl)
10219 if (impl->has_home)
10220 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10223 /* Handler for the "desktop-folder" keybinding signal */
10225 desktop_folder_handler (GtkFileChooserDefault *impl)
10227 if (impl->has_desktop)
10228 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10231 /* Handler for the "search-shortcut" keybinding signal */
10233 search_shortcut_handler (GtkFileChooserDefault *impl)
10235 if (impl->has_search)
10237 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10239 /* we want the entry widget to grab the focus the first
10240 * time, not the browse_files_tree_view widget.
10242 if (impl->search_entry)
10243 gtk_widget_grab_focus (impl->search_entry);
10247 /* Handler for the "recent-shortcut" keybinding signal */
10249 recent_shortcut_handler (GtkFileChooserDefault *impl)
10251 if (impl->has_recent)
10252 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10256 quick_bookmark_handler (GtkFileChooserDefault *impl,
10257 gint bookmark_index)
10262 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10265 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10267 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10268 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10271 gtk_tree_path_free (path);
10273 switch_to_shortcut (impl, bookmark_pos);
10277 show_hidden_handler (GtkFileChooserDefault *impl)
10279 g_object_set (impl,
10280 "show-hidden", !impl->show_hidden,
10285 /* Drag and drop interfaces */
10288 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10293 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10295 model->impl = NULL;
10298 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10300 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10303 ShortcutsPaneModelFilter *model;
10307 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10309 pos = *gtk_tree_path_get_indices (path);
10310 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10312 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10315 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10319 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10321 GtkSelectionData *selection_data)
10328 /* Fill the GtkTreeDragSourceIface vtable */
10330 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10332 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10333 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10337 /* Fill the GtkTreeDragDestIface vtable */
10339 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10341 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10342 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10346 static GtkTreeModel *
10347 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10348 GtkTreeModel *child_model,
10351 ShortcutsPaneModelFilter *model;
10353 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10354 "child-model", child_model,
10355 "virtual-root", root,
10358 model->impl = impl;
10360 return GTK_TREE_MODEL (model);