1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtkfilechooserdefault.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcomboboxtext.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdialog.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechooserutils.h"
40 #include "gtkfilechooser.h"
41 #include "gtkfilesystem.h"
42 #include "gtkfilesystemmodel.h"
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
48 #include "gtkimagemenuitem.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmessagedialog.h"
52 #include "gtkmountoperation.h"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkradiobutton.h"
56 #include "gtkrecentfilter.h"
57 #include "gtkrecentmanager.h"
58 #include "gtkscrolledwindow.h"
59 #include "gtkseparatormenuitem.h"
60 #include "gtksizegroup.h"
61 #include "gtksizerequest.h"
64 #include "gtktoolbar.h"
65 #include "gtktoolbutton.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
71 #include "gtkorientable.h"
78 #include <sys/types.h>
89 #undef PROFILE_FILE_CHOOSER
90 #ifdef PROFILE_FILE_CHOOSER
97 #define PROFILE_INDENT 4
98 static int profile_indent;
101 profile_add_indent (int indent)
103 profile_indent += indent;
104 if (profile_indent < 0)
105 g_error ("You screwed up your indentation");
109 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
114 profile_add_indent (indent);
116 if (profile_indent == 0)
117 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
119 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 profile_add_indent (indent);
128 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
129 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
130 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
132 #define profile_start(x, y)
133 #define profile_end(x, y)
134 #define profile_msg(x, y)
139 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
141 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
142 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
143 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define MAX_LOADING_TIME 500
147 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
149 struct _GtkFileChooserDefaultClass
151 GtkBoxClass parent_class;
157 LOCATION_POPUP_ON_PASTE,
163 LOCATION_TOGGLE_POPUP,
171 static guint signals[LAST_SIGNAL] = { 0 };
173 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
175 SHORTCUTS_COL_PIXBUF,
179 SHORTCUTS_COL_REMOVABLE,
180 SHORTCUTS_COL_PIXBUF_VISIBLE,
181 SHORTCUTS_COL_CANCELLABLE,
182 SHORTCUTS_COL_NUM_COLUMNS
187 SHORTCUT_TYPE_VOLUME,
188 SHORTCUT_TYPE_SEPARATOR,
189 SHORTCUT_TYPE_SEARCH,
193 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
194 "standard::is-hidden,standard::is-backup,standard::size," \
195 "standard::content-type,time::modified"
197 /* the first 3 must be these due to settings caching sort column */
202 MODEL_COL_NAME_COLLATED,
206 MODEL_COL_MTIME_TEXT,
208 MODEL_COL_NUM_COLUMNS
211 /* This list of types is passed to _gtk_file_system_model_new*() */
212 #define MODEL_COLUMN_TYPES \
213 MODEL_COL_NUM_COLUMNS, \
214 G_TYPE_STRING, /* MODEL_COL_NAME */ \
215 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
216 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
217 G_TYPE_FILE, /* MODEL_COL_FILE */ \
218 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
219 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
220 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
221 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
222 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
223 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
225 /* Identifiers for target types */
230 /* Interesting places in the shortcuts bar */
234 SHORTCUTS_RECENT_SEPARATOR,
239 SHORTCUTS_BOOKMARKS_SEPARATOR,
241 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
242 SHORTCUTS_CURRENT_FOLDER
245 /* Icon size for if we can't get it from the theme */
246 #define FALLBACK_ICON_SIZE 16
248 #define PREVIEW_HBOX_SPACING 12
252 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
253 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
254 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
255 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
256 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
257 #define SETTINGS_KEY_SORT_ORDER "sort-order"
258 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
259 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
261 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
262 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
264 static GObject* gtk_file_chooser_default_constructor (GType type,
265 guint n_construct_properties,
266 GObjectConstructParam *construct_params);
267 static void gtk_file_chooser_default_finalize (GObject *object);
268 static void gtk_file_chooser_default_set_property (GObject *object,
272 static void gtk_file_chooser_default_get_property (GObject *object,
276 static void gtk_file_chooser_default_dispose (GObject *object);
277 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
278 static void gtk_file_chooser_default_realize (GtkWidget *widget);
279 static void gtk_file_chooser_default_map (GtkWidget *widget);
280 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
281 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
282 GtkWidget *previous_toplevel);
283 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
284 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
285 GdkScreen *previous_screen);
286 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
287 GtkAllocation *allocation);
289 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
292 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
295 gboolean clear_entry,
297 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
298 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
300 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
305 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
306 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
307 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
308 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
309 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
310 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
313 GtkFileFilter *filter);
314 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
315 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
318 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
321 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
323 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
325 gint *default_height);
326 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
327 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
329 static void location_popup_handler (GtkFileChooserDefault *impl,
331 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
332 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
333 static void up_folder_handler (GtkFileChooserDefault *impl);
334 static void down_folder_handler (GtkFileChooserDefault *impl);
335 static void home_folder_handler (GtkFileChooserDefault *impl);
336 static void desktop_folder_handler (GtkFileChooserDefault *impl);
337 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
338 gint bookmark_index);
339 static void show_hidden_handler (GtkFileChooserDefault *impl);
340 static void search_shortcut_handler (GtkFileChooserDefault *impl);
341 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
342 static void update_appearance (GtkFileChooserDefault *impl);
344 static void set_current_filter (GtkFileChooserDefault *impl,
345 GtkFileFilter *filter);
346 static void check_preview_change (GtkFileChooserDefault *impl);
348 static void filter_combo_changed (GtkComboBox *combo_box,
349 GtkFileChooserDefault *impl);
351 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
353 GtkFileChooserDefault *impl);
355 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
358 gboolean path_currently_selected,
360 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
362 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
364 static int shortcuts_get_index (GtkFileChooserDefault *impl,
365 ShortcutsIndex where);
366 static int shortcut_find_position (GtkFileChooserDefault *impl,
369 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
371 static gboolean list_select_func (GtkTreeSelection *selection,
374 gboolean path_currently_selected,
377 static void list_selection_changed (GtkTreeSelection *tree_selection,
378 GtkFileChooserDefault *impl);
379 static void list_row_activated (GtkTreeView *tree_view,
381 GtkTreeViewColumn *column,
382 GtkFileChooserDefault *impl);
384 static void path_bar_clicked (GtkPathBar *path_bar,
387 gboolean child_is_hidden,
388 GtkFileChooserDefault *impl);
390 static void add_bookmark_button_clicked_cb (GtkButton *button,
391 GtkFileChooserDefault *impl);
392 static void remove_bookmark_button_clicked_cb (GtkButton *button,
393 GtkFileChooserDefault *impl);
394 static void save_folder_combo_changed_cb (GtkComboBox *combo,
395 GtkFileChooserDefault *impl);
397 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
399 static void load_remove_timer (GtkFileChooserDefault *impl);
400 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
402 static void location_button_toggled_cb (GtkToggleButton *toggle,
403 GtkFileChooserDefault *impl);
404 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
406 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
407 gboolean remove_from_treeview);
408 static void search_stop_searching (GtkFileChooserDefault *impl,
409 gboolean remove_query);
410 static void search_clear_model (GtkFileChooserDefault *impl,
411 gboolean remove_from_treeview);
412 static gboolean search_should_respond (GtkFileChooserDefault *impl);
413 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
414 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
415 static void search_entry_activate_cb (GtkEntry *entry,
417 static void settings_load (GtkFileChooserDefault *impl);
419 static void recent_stop_loading (GtkFileChooserDefault *impl);
420 static void recent_clear_model (GtkFileChooserDefault *impl,
421 gboolean remove_from_treeview);
422 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
423 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
424 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
425 static void set_file_system_backend (GtkFileChooserDefault *impl);
426 static void unset_file_system_backend (GtkFileChooserDefault *impl);
432 /* Drag and drop interface declarations */
435 GtkTreeModelFilter parent;
437 GtkFileChooserDefault *impl;
438 } ShortcutsPaneModelFilter;
441 GtkTreeModelFilterClass parent_class;
442 } ShortcutsPaneModelFilterClass;
444 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
445 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
447 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
449 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
450 _shortcuts_pane_model_filter,
451 GTK_TYPE_TREE_MODEL_FILTER,
452 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
453 shortcuts_pane_model_filter_drag_source_iface_init))
455 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
456 GtkTreeModel *child_model,
461 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
462 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
463 gtk_file_chooser_default_iface_init)
464 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
465 gtk_file_chooser_embed_default_iface_init));
469 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
471 GdkModifierType modifiers,
472 const gchar *signal_name)
474 gtk_binding_entry_add_signal (binding_set,
478 gtk_binding_entry_add_signal (binding_set,
479 keyval, modifiers | GDK_SHIFT_MASK,
484 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
486 static const guint quick_bookmark_keyvals[10] = {
487 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
489 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
490 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
491 GtkBindingSet *binding_set;
494 gobject_class->finalize = gtk_file_chooser_default_finalize;
495 gobject_class->constructor = gtk_file_chooser_default_constructor;
496 gobject_class->set_property = gtk_file_chooser_default_set_property;
497 gobject_class->get_property = gtk_file_chooser_default_get_property;
498 gobject_class->dispose = gtk_file_chooser_default_dispose;
500 widget_class->show_all = gtk_file_chooser_default_show_all;
501 widget_class->realize = gtk_file_chooser_default_realize;
502 widget_class->map = gtk_file_chooser_default_map;
503 widget_class->unmap = gtk_file_chooser_default_unmap;
504 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
505 widget_class->style_updated = gtk_file_chooser_default_style_updated;
506 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
507 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
509 signals[LOCATION_POPUP] =
510 g_signal_new_class_handler (I_("location-popup"),
511 G_OBJECT_CLASS_TYPE (class),
512 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
513 G_CALLBACK (location_popup_handler),
515 _gtk_marshal_VOID__STRING,
516 G_TYPE_NONE, 1, G_TYPE_STRING);
518 signals[LOCATION_POPUP_ON_PASTE] =
519 g_signal_new_class_handler (I_("location-popup-on-paste"),
520 G_OBJECT_CLASS_TYPE (class),
521 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
522 G_CALLBACK (location_popup_on_paste_handler),
524 _gtk_marshal_VOID__VOID,
527 signals[LOCATION_TOGGLE_POPUP] =
528 g_signal_new_class_handler (I_("location-toggle-popup"),
529 G_OBJECT_CLASS_TYPE (class),
530 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
531 G_CALLBACK (location_toggle_popup_handler),
533 _gtk_marshal_VOID__VOID,
537 g_signal_new_class_handler (I_("up-folder"),
538 G_OBJECT_CLASS_TYPE (class),
539 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
540 G_CALLBACK (up_folder_handler),
542 _gtk_marshal_VOID__VOID,
545 signals[DOWN_FOLDER] =
546 g_signal_new_class_handler (I_("down-folder"),
547 G_OBJECT_CLASS_TYPE (class),
548 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
549 G_CALLBACK (down_folder_handler),
551 _gtk_marshal_VOID__VOID,
554 signals[HOME_FOLDER] =
555 g_signal_new_class_handler (I_("home-folder"),
556 G_OBJECT_CLASS_TYPE (class),
557 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
558 G_CALLBACK (home_folder_handler),
560 _gtk_marshal_VOID__VOID,
563 signals[DESKTOP_FOLDER] =
564 g_signal_new_class_handler (I_("desktop-folder"),
565 G_OBJECT_CLASS_TYPE (class),
566 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
567 G_CALLBACK (desktop_folder_handler),
569 _gtk_marshal_VOID__VOID,
572 signals[QUICK_BOOKMARK] =
573 g_signal_new_class_handler (I_("quick-bookmark"),
574 G_OBJECT_CLASS_TYPE (class),
575 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
576 G_CALLBACK (quick_bookmark_handler),
578 _gtk_marshal_VOID__INT,
579 G_TYPE_NONE, 1, G_TYPE_INT);
581 signals[SHOW_HIDDEN] =
582 g_signal_new_class_handler (I_("show-hidden"),
583 G_OBJECT_CLASS_TYPE (class),
584 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
585 G_CALLBACK (show_hidden_handler),
587 _gtk_marshal_VOID__VOID,
590 signals[SEARCH_SHORTCUT] =
591 g_signal_new_class_handler (I_("search-shortcut"),
592 G_OBJECT_CLASS_TYPE (class),
593 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
594 G_CALLBACK (search_shortcut_handler),
596 _gtk_marshal_VOID__VOID,
599 signals[RECENT_SHORTCUT] =
600 g_signal_new_class_handler (I_("recent-shortcut"),
601 G_OBJECT_CLASS_TYPE (class),
602 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
603 G_CALLBACK (recent_shortcut_handler),
605 _gtk_marshal_VOID__VOID,
608 binding_set = gtk_binding_set_by_class (class);
610 gtk_binding_entry_add_signal (binding_set,
611 GDK_KEY_l, GDK_CONTROL_MASK,
612 "location-toggle-popup",
615 gtk_binding_entry_add_signal (binding_set,
618 1, G_TYPE_STRING, "/");
619 gtk_binding_entry_add_signal (binding_set,
620 GDK_KEY_KP_Divide, 0,
622 1, G_TYPE_STRING, "/");
625 gtk_binding_entry_add_signal (binding_set,
626 GDK_KEY_asciitilde, 0,
628 1, G_TYPE_STRING, "~");
631 gtk_binding_entry_add_signal (binding_set,
632 GDK_KEY_v, GDK_CONTROL_MASK,
633 "location-popup-on-paste",
635 gtk_binding_entry_add_signal (binding_set,
636 GDK_KEY_BackSpace, 0,
640 add_normal_and_shifted_binding (binding_set,
641 GDK_KEY_Up, GDK_MOD1_MASK,
644 add_normal_and_shifted_binding (binding_set,
645 GDK_KEY_KP_Up, GDK_MOD1_MASK,
648 add_normal_and_shifted_binding (binding_set,
649 GDK_KEY_Down, GDK_MOD1_MASK,
651 add_normal_and_shifted_binding (binding_set,
652 GDK_KEY_KP_Down, GDK_MOD1_MASK,
655 gtk_binding_entry_add_signal (binding_set,
656 GDK_KEY_Home, GDK_MOD1_MASK,
659 gtk_binding_entry_add_signal (binding_set,
660 GDK_KEY_KP_Home, GDK_MOD1_MASK,
663 gtk_binding_entry_add_signal (binding_set,
664 GDK_KEY_d, GDK_MOD1_MASK,
667 gtk_binding_entry_add_signal (binding_set,
668 GDK_KEY_h, GDK_CONTROL_MASK,
671 gtk_binding_entry_add_signal (binding_set,
672 GDK_KEY_s, GDK_MOD1_MASK,
675 gtk_binding_entry_add_signal (binding_set,
676 GDK_KEY_r, GDK_MOD1_MASK,
680 for (i = 0; i < 10; i++)
681 gtk_binding_entry_add_signal (binding_set,
682 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
686 _gtk_file_chooser_install_properties (gobject_class);
690 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
692 iface->select_file = gtk_file_chooser_default_select_file;
693 iface->unselect_file = gtk_file_chooser_default_unselect_file;
694 iface->select_all = gtk_file_chooser_default_select_all;
695 iface->unselect_all = gtk_file_chooser_default_unselect_all;
696 iface->get_files = gtk_file_chooser_default_get_files;
697 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
698 iface->get_file_system = gtk_file_chooser_default_get_file_system;
699 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
700 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
701 iface->set_current_name = gtk_file_chooser_default_set_current_name;
702 iface->add_filter = gtk_file_chooser_default_add_filter;
703 iface->remove_filter = gtk_file_chooser_default_remove_filter;
704 iface->list_filters = gtk_file_chooser_default_list_filters;
705 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
706 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
707 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
711 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
713 iface->get_default_size = gtk_file_chooser_default_get_default_size;
714 iface->should_respond = gtk_file_chooser_default_should_respond;
715 iface->initial_focus = gtk_file_chooser_default_initial_focus;
719 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
721 profile_start ("start", NULL);
722 #ifdef PROFILE_FILE_CHOOSER
723 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
725 impl->local_only = TRUE;
726 impl->preview_widget_active = TRUE;
727 impl->use_preview_label = TRUE;
728 impl->select_multiple = FALSE;
729 impl->show_hidden = FALSE;
730 impl->show_size_column = TRUE;
731 impl->icon_size = FALLBACK_ICON_SIZE;
732 impl->load_state = LOAD_EMPTY;
733 impl->reload_state = RELOAD_EMPTY;
734 impl->pending_select_files = NULL;
735 impl->location_mode = LOCATION_MODE_PATH_BAR;
736 impl->operation_mode = OPERATION_MODE_BROWSE;
737 impl->sort_column = MODEL_COL_NAME;
738 impl->sort_order = GTK_SORT_ASCENDING;
739 impl->recent_manager = gtk_recent_manager_get_default ();
740 impl->create_folders = TRUE;
742 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
743 GTK_ORIENTATION_VERTICAL);
744 gtk_box_set_spacing (GTK_BOX (impl), 12);
746 set_file_system_backend (impl);
748 profile_end ("end", NULL);
751 /* Frees the data columns for the specified iter in the shortcuts model*/
753 shortcuts_free_row_data (GtkFileChooserDefault *impl,
757 ShortcutType shortcut_type;
758 GCancellable *cancellable;
760 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
761 SHORTCUTS_COL_DATA, &col_data,
762 SHORTCUTS_COL_TYPE, &shortcut_type,
763 SHORTCUTS_COL_CANCELLABLE, &cancellable,
768 g_cancellable_cancel (cancellable);
769 g_object_unref (cancellable);
772 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
773 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
777 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
779 GtkFileSystemVolume *volume;
782 _gtk_file_system_volume_unref (volume);
784 if (shortcut_type == SHORTCUT_TYPE_FILE)
789 g_object_unref (file);
793 /* Frees all the data columns in the shortcuts model */
795 shortcuts_free (GtkFileChooserDefault *impl)
799 if (!impl->shortcuts_model)
802 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
805 shortcuts_free_row_data (impl, &iter);
807 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
809 g_object_unref (impl->shortcuts_model);
810 impl->shortcuts_model = NULL;
814 pending_select_files_free (GtkFileChooserDefault *impl)
816 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
817 g_slist_free (impl->pending_select_files);
818 impl->pending_select_files = NULL;
822 pending_select_files_add (GtkFileChooserDefault *impl,
825 impl->pending_select_files =
826 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
830 gtk_file_chooser_default_finalize (GObject *object)
832 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
835 unset_file_system_backend (impl);
837 if (impl->shortcuts_pane_filter_model)
838 g_object_unref (impl->shortcuts_pane_filter_model);
840 if (impl->shortcuts_combo_filter_model)
841 g_object_unref (impl->shortcuts_combo_filter_model);
843 shortcuts_free (impl);
845 g_free (impl->browse_files_last_selected_name);
847 for (l = impl->filters; l; l = l->next)
849 GtkFileFilter *filter;
851 filter = GTK_FILE_FILTER (l->data);
852 g_object_unref (filter);
854 g_slist_free (impl->filters);
856 if (impl->current_filter)
857 g_object_unref (impl->current_filter);
859 if (impl->current_volume_file)
860 g_object_unref (impl->current_volume_file);
862 if (impl->current_folder)
863 g_object_unref (impl->current_folder);
865 if (impl->preview_file)
866 g_object_unref (impl->preview_file);
868 if (impl->browse_path_bar_size_group)
869 g_object_unref (impl->browse_path_bar_size_group);
871 /* Free all the Models we have */
872 stop_loading_and_clear_list_model (impl, FALSE);
873 search_clear_model (impl, FALSE);
874 recent_clear_model (impl, FALSE);
876 /* stopping the load above should have cleared this */
877 g_assert (impl->load_timeout_id == 0);
879 g_free (impl->preview_display_name);
881 g_free (impl->edited_new_text);
883 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
886 /* Shows an error dialog set as transient for the specified window */
888 error_message_with_parent (GtkWindow *parent,
894 dialog = gtk_message_dialog_new (parent,
895 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
900 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
903 if (parent && gtk_window_has_group (parent))
904 gtk_window_group_add_window (gtk_window_get_group (parent),
905 GTK_WINDOW (dialog));
907 gtk_dialog_run (GTK_DIALOG (dialog));
908 gtk_widget_destroy (dialog);
911 /* Returns a toplevel GtkWindow, or NULL if none */
913 get_toplevel (GtkWidget *widget)
917 toplevel = gtk_widget_get_toplevel (widget);
918 if (!gtk_widget_is_toplevel (toplevel))
921 return GTK_WINDOW (toplevel);
924 /* Shows an error dialog for the file chooser */
926 error_message (GtkFileChooserDefault *impl,
930 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
933 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
935 error_dialog (GtkFileChooserDefault *impl,
946 uri = g_file_get_uri (file);
947 text = g_strdup_printf (msg, uri);
948 error_message (impl, text, error->message);
951 g_error_free (error);
955 /* Displays an error message about not being able to get information for a file.
956 * Frees the GError as well.
959 error_getting_info_dialog (GtkFileChooserDefault *impl,
964 _("Could not retrieve information about the file"),
968 /* Shows an error dialog about not being able to add a bookmark */
970 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
975 _("Could not add a bookmark"),
979 /* Shows an error dialog about not being able to remove a bookmark */
981 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
986 _("Could not remove bookmark"),
990 /* Shows an error dialog about not being able to create a folder */
992 error_creating_folder_dialog (GtkFileChooserDefault *impl,
997 _("The folder could not be created"),
1001 /* Shows an error about not being able to create a folder because a file with
1002 * the same name is already there.
1005 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1010 _("The folder could not be created, as a file with the same "
1011 "name already exists. Try using a different name for the "
1012 "folder, or rename the file first."),
1017 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1023 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1024 _("You need to choose a valid filename."));
1027 _("Cannot create a file under %s as it is not a folder"),
1028 parent_file, error);
1031 /* Shows an error about not being able to select a folder because a file with
1032 * the same name is already there.
1035 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1039 _("You may only select folders. The item that you selected is not a folder; "
1040 "try using a different item."),
1044 /* Shows an error dialog about not being able to create a filename */
1046 error_building_filename_dialog (GtkFileChooserDefault *impl,
1049 error_dialog (impl, _("Invalid file name"),
1053 /* Shows an error dialog when we cannot switch to a folder */
1055 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1059 error_dialog (impl, _("The folder contents could not be displayed"),
1063 /* Changes folders, displaying an error dialog if this fails */
1065 change_folder_and_display_error (GtkFileChooserDefault *impl,
1067 gboolean clear_entry)
1072 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1074 /* We copy the path because of this case:
1076 * list_row_activated()
1077 * fetches path from model; path belongs to the model (*)
1078 * calls change_folder_and_display_error()
1079 * calls gtk_file_chooser_set_current_folder_file()
1080 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1084 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1087 error_changing_folder_dialog (impl, file, error);
1093 emit_default_size_changed (GtkFileChooserDefault *impl)
1095 profile_msg (" emit default-size-changed start", NULL);
1096 g_signal_emit_by_name (impl, "default-size-changed");
1097 profile_msg (" emit default-size-changed end", NULL);
1101 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1103 if (impl->use_preview_label)
1105 if (!impl->preview_label)
1107 impl->preview_label = gtk_label_new (impl->preview_display_name);
1108 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1109 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1110 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1111 gtk_widget_show (impl->preview_label);
1116 if (impl->preview_label)
1118 gtk_widget_destroy (impl->preview_label);
1119 impl->preview_label = NULL;
1123 if (impl->preview_widget_active && impl->preview_widget)
1124 gtk_widget_show (impl->preview_box);
1126 gtk_widget_hide (impl->preview_box);
1128 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1129 emit_default_size_changed (impl);
1133 set_preview_widget (GtkFileChooserDefault *impl,
1134 GtkWidget *preview_widget)
1136 if (preview_widget == impl->preview_widget)
1139 if (impl->preview_widget)
1140 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1141 impl->preview_widget);
1143 impl->preview_widget = preview_widget;
1144 if (impl->preview_widget)
1146 gtk_widget_show (impl->preview_widget);
1147 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1148 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1149 impl->preview_widget,
1150 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1153 update_preview_widget_visibility (impl);
1156 /* Renders a "Search" icon at an appropriate size for a tree view */
1158 render_search_icon (GtkFileChooserDefault *impl)
1160 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1164 render_recent_icon (GtkFileChooserDefault *impl)
1166 GtkIconTheme *theme;
1169 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1170 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1172 theme = gtk_icon_theme_get_default ();
1174 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1180 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1186 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1187 struct ReloadIconsData
1189 GtkFileChooserDefault *impl;
1190 GtkTreeRowReference *row_ref;
1194 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1196 const GError *error,
1202 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1203 struct ReloadIconsData *data = user_data;
1205 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1208 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1210 if (cancelled || error)
1213 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1215 path = gtk_tree_row_reference_get_path (data->row_ref);
1218 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1219 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1220 SHORTCUTS_COL_PIXBUF, pixbuf,
1222 gtk_tree_path_free (path);
1226 g_object_unref (pixbuf);
1229 gtk_tree_row_reference_free (data->row_ref);
1230 g_object_unref (data->impl);
1233 g_object_unref (cancellable);
1237 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1242 profile_start ("start", NULL);
1244 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1247 for (l = impl->reload_icon_cancellables; l; l = l->next)
1249 GCancellable *cancellable = G_CANCELLABLE (l->data);
1250 g_cancellable_cancel (cancellable);
1252 g_slist_free (impl->reload_icon_cancellables);
1253 impl->reload_icon_cancellables = NULL;
1258 ShortcutType shortcut_type;
1259 gboolean pixbuf_visible;
1262 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1263 SHORTCUTS_COL_DATA, &data,
1264 SHORTCUTS_COL_TYPE, &shortcut_type,
1265 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1271 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1273 GtkFileSystemVolume *volume;
1276 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1277 impl->icon_size, NULL);
1279 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1281 if (g_file_is_native (G_FILE (data)))
1284 struct ReloadIconsData *info;
1285 GtkTreePath *tree_path;
1286 GCancellable *cancellable;
1290 info = g_new0 (struct ReloadIconsData, 1);
1291 info->impl = g_object_ref (impl);
1292 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1293 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1294 gtk_tree_path_free (tree_path);
1296 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1298 shortcuts_reload_icons_get_info_cb,
1300 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1304 GtkIconTheme *icon_theme;
1306 /* Don't call get_info for remote paths to avoid latency and
1308 * If we switch to a better bookmarks file format (XBEL), we
1309 * should use mime info to get a better icon.
1311 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1312 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1313 impl->icon_size, 0, NULL);
1316 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1318 pixbuf = render_search_icon (impl);
1320 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1322 pixbuf = render_recent_icon (impl);
1325 gtk_list_store_set (impl->shortcuts_model, &iter,
1326 SHORTCUTS_COL_PIXBUF, pixbuf,
1330 g_object_unref (pixbuf);
1334 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1338 profile_end ("end", NULL);
1342 shortcuts_find_folder (GtkFileChooserDefault *impl,
1345 GtkTreeSelection *selection;
1349 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1351 g_assert (folder != NULL);
1352 pos = shortcut_find_position (impl, folder);
1355 gtk_tree_selection_unselect_all (selection);
1359 path = gtk_tree_path_new_from_indices (pos, -1);
1360 gtk_tree_selection_select_path (selection, path);
1361 gtk_tree_path_free (path);
1364 /* If a shortcut corresponds to the current folder, selects it */
1366 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1368 shortcuts_find_folder (impl, impl->current_folder);
1371 /* Removes the specified number of rows from the shortcuts list */
1373 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1379 path = gtk_tree_path_new_from_indices (start_row, -1);
1381 for (; n_rows; n_rows--)
1385 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1386 g_assert_not_reached ();
1388 shortcuts_free_row_data (impl, &iter);
1389 gtk_list_store_remove (impl->shortcuts_model, &iter);
1392 gtk_tree_path_free (path);
1396 shortcuts_update_count (GtkFileChooserDefault *impl,
1397 ShortcutsIndex type,
1402 case SHORTCUTS_HOME:
1404 impl->has_home = FALSE;
1406 impl->has_home = TRUE;
1409 case SHORTCUTS_DESKTOP:
1411 impl->has_desktop = FALSE;
1413 impl->has_desktop = TRUE;
1416 case SHORTCUTS_VOLUMES:
1417 impl->num_volumes += value;
1420 case SHORTCUTS_SHORTCUTS:
1421 impl->num_shortcuts += value;
1424 case SHORTCUTS_BOOKMARKS:
1425 impl->num_bookmarks += value;
1428 case SHORTCUTS_CURRENT_FOLDER:
1430 impl->shortcuts_current_folder_active = FALSE;
1432 impl->shortcuts_current_folder_active = TRUE;
1441 struct ShortcutsInsertRequest
1443 GtkFileChooserDefault *impl;
1447 GtkTreeRowReference *row_ref;
1448 ShortcutsIndex type;
1454 get_file_info_finished (GCancellable *cancellable,
1456 const GError *error,
1460 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1464 GCancellable *model_cancellable = NULL;
1465 struct ShortcutsInsertRequest *request = data;
1467 path = gtk_tree_row_reference_get_path (request->row_ref);
1469 /* Handle doesn't exist anymore in the model */
1472 pos = gtk_tree_path_get_indices (path)[0];
1473 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1475 gtk_tree_path_free (path);
1477 /* validate cancellable, else goto out */
1478 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1479 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1481 if (cancellable != model_cancellable)
1484 /* set the cancellable to NULL in the model (we unref later on) */
1485 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1486 SHORTCUTS_COL_CANCELLABLE, NULL,
1494 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1495 shortcuts_update_count (request->impl, request->type, -1);
1497 if (request->type == SHORTCUTS_HOME)
1501 home = g_file_new_for_path (g_get_home_dir ());
1502 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1503 g_object_unref (home);
1505 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1507 /* Remove the current folder separator */
1508 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1509 shortcuts_remove_rows (request->impl, separator_pos, 1);
1515 if (!request->label_copy)
1516 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1517 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1518 request->impl->icon_size);
1520 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1521 SHORTCUTS_COL_PIXBUF, pixbuf,
1522 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1523 SHORTCUTS_COL_NAME, request->label_copy,
1524 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1525 SHORTCUTS_COL_REMOVABLE, request->removable,
1528 if (request->impl->shortcuts_pane_filter_model)
1529 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1531 if (request->impl->shortcuts_combo_filter_model)
1532 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1534 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1535 request->impl->save_folder_combo != NULL)
1537 /* The current folder is updated via _activate_iter(), don't
1538 * have save_folder_combo_changed_cb() call _activate_iter()
1541 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1542 G_CALLBACK (save_folder_combo_changed_cb),
1545 if (request->impl->has_search)
1548 if (request->impl->has_recent)
1551 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1552 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1553 G_CALLBACK (save_folder_combo_changed_cb),
1558 g_object_unref (pixbuf);
1561 g_object_unref (request->impl);
1562 g_object_unref (request->file);
1563 gtk_tree_row_reference_free (request->row_ref);
1564 g_free (request->label_copy);
1567 if (model_cancellable)
1568 g_object_unref (model_cancellable);
1571 /* FIXME: GtkFileSystem needs a function to split a remote path
1572 * into hostname and path components, or maybe just have a
1573 * gtk_file_system_path_get_display_name().
1575 * This function is also used in gtkfilechooserbutton.c
1578 _gtk_file_chooser_label_for_file (GFile *file)
1580 const gchar *path, *start, *end, *p;
1581 gchar *uri, *host, *label;
1583 uri = g_file_get_uri (file);
1585 start = strstr (uri, "://");
1589 path = strchr (start, '/');
1594 end = uri + strlen (uri);
1598 /* strip username */
1599 p = strchr (start, '@');
1603 p = strchr (start, ':');
1607 host = g_strndup (start, end - start);
1609 /* Translators: the first string is a path and the second string
1610 * is a hostname. Nautilus and the panel contain the same string
1613 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1619 label = g_strdup (uri);
1627 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1628 * inserts a volume. A position of -1 indicates the end of the tree.
1631 shortcuts_insert_file (GtkFileChooserDefault *impl,
1633 ShortcutType shortcut_type,
1634 GtkFileSystemVolume *volume,
1638 ShortcutsIndex type)
1641 GdkPixbuf *pixbuf = NULL;
1642 gpointer data = NULL;
1644 GtkIconTheme *icon_theme;
1646 profile_start ("start shortcut", NULL);
1648 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1651 label_copy = _gtk_file_system_volume_get_display_name (volume);
1652 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1653 impl->icon_size, NULL);
1655 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1657 if (g_file_is_native (file))
1659 struct ShortcutsInsertRequest *request;
1660 GCancellable *cancellable;
1663 request = g_new0 (struct ShortcutsInsertRequest, 1);
1664 request->impl = g_object_ref (impl);
1665 request->file = g_object_ref (file);
1666 request->name_only = TRUE;
1667 request->removable = removable;
1669 request->type = type;
1671 request->label_copy = g_strdup (label);
1674 gtk_list_store_append (impl->shortcuts_model, &iter);
1676 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1678 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1679 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1680 gtk_tree_path_free (p);
1682 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1683 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1684 get_file_info_finished, request);
1686 gtk_list_store_set (impl->shortcuts_model, &iter,
1687 SHORTCUTS_COL_DATA, g_object_ref (file),
1688 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1689 SHORTCUTS_COL_CANCELLABLE, cancellable,
1692 shortcuts_update_count (impl, type, 1);
1698 /* Don't call get_info for remote paths to avoid latency and
1701 data = g_object_ref (file);
1703 label_copy = g_strdup (label);
1705 label_copy = _gtk_file_chooser_label_for_file (file);
1707 /* If we switch to a better bookmarks file format (XBEL), we
1708 * should use mime info to get a better icon.
1710 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1711 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1712 impl->icon_size, 0, NULL);
1717 g_assert_not_reached ();
1723 gtk_list_store_append (impl->shortcuts_model, &iter);
1725 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1727 shortcuts_update_count (impl, type, 1);
1729 gtk_list_store_set (impl->shortcuts_model, &iter,
1730 SHORTCUTS_COL_PIXBUF, pixbuf,
1731 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1732 SHORTCUTS_COL_NAME, label_copy,
1733 SHORTCUTS_COL_DATA, data,
1734 SHORTCUTS_COL_TYPE, shortcut_type,
1735 SHORTCUTS_COL_REMOVABLE, removable,
1736 SHORTCUTS_COL_CANCELLABLE, NULL,
1739 if (impl->shortcuts_pane_filter_model)
1740 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1742 if (impl->shortcuts_combo_filter_model)
1743 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1745 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1747 /* The current folder is updated via _activate_iter(), don't
1748 * have save_folder_combo_changed_cb() call _activate_iter()
1751 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1753 if (impl->has_search)
1756 if (impl->has_recent)
1759 g_signal_handlers_block_by_func (impl->save_folder_combo,
1760 G_CALLBACK (save_folder_combo_changed_cb),
1763 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1764 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1765 G_CALLBACK (save_folder_combo_changed_cb),
1769 g_free (label_copy);
1772 g_object_unref (pixbuf);
1774 profile_end ("end", NULL);
1778 shortcuts_append_search (GtkFileChooserDefault *impl)
1783 pixbuf = render_search_icon (impl);
1785 gtk_list_store_append (impl->shortcuts_model, &iter);
1786 gtk_list_store_set (impl->shortcuts_model, &iter,
1787 SHORTCUTS_COL_PIXBUF, pixbuf,
1788 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1789 SHORTCUTS_COL_NAME, _("Search"),
1790 SHORTCUTS_COL_DATA, NULL,
1791 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1792 SHORTCUTS_COL_REMOVABLE, FALSE,
1796 g_object_unref (pixbuf);
1798 impl->has_search = TRUE;
1802 shortcuts_append_recent (GtkFileChooserDefault *impl)
1807 pixbuf = render_recent_icon (impl);
1809 gtk_list_store_append (impl->shortcuts_model, &iter);
1810 gtk_list_store_set (impl->shortcuts_model, &iter,
1811 SHORTCUTS_COL_PIXBUF, pixbuf,
1812 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1813 SHORTCUTS_COL_NAME, _("Recently Used"),
1814 SHORTCUTS_COL_DATA, NULL,
1815 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1816 SHORTCUTS_COL_REMOVABLE, FALSE,
1820 g_object_unref (pixbuf);
1822 impl->has_recent = TRUE;
1825 /* Appends an item for the user's home directory to the shortcuts model */
1827 shortcuts_append_home (GtkFileChooserDefault *impl)
1829 const char *home_path;
1832 profile_start ("start", NULL);
1834 home_path = g_get_home_dir ();
1835 if (home_path == NULL)
1837 profile_end ("end - no home directory!?", NULL);
1841 home = g_file_new_for_path (home_path);
1842 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1843 impl->has_home = TRUE;
1845 g_object_unref (home);
1847 profile_end ("end", NULL);
1850 /* Appends the ~/Desktop directory to the shortcuts model */
1852 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1857 profile_start ("start", NULL);
1859 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1860 /* "To disable a directory, point it to the homedir."
1861 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1863 if (!g_strcmp0 (name, g_get_home_dir ()))
1865 profile_end ("end", NULL);
1869 file = g_file_new_for_path (name);
1870 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1871 impl->has_desktop = TRUE;
1873 /* We do not actually pop up an error dialog if there is no desktop directory
1874 * because some people may really not want to have one.
1877 g_object_unref (file);
1879 profile_end ("end", NULL);
1882 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1884 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1891 profile_start ("start", NULL);
1893 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1896 for (; bookmarks; bookmarks = bookmarks->next)
1900 file = bookmarks->data;
1902 if (impl->local_only && !g_file_is_native (file))
1905 if (shortcut_find_position (impl, file) != -1)
1908 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1910 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1916 profile_end ("end", NULL);
1918 return num_inserted;
1921 /* Returns the index for the corresponding item in the shortcuts bar */
1923 shortcuts_get_index (GtkFileChooserDefault *impl,
1924 ShortcutsIndex where)
1930 if (where == SHORTCUTS_SEARCH)
1933 n += impl->has_search ? 1 : 0;
1935 if (where == SHORTCUTS_RECENT)
1938 n += impl->has_recent ? 1 : 0;
1940 if (where == SHORTCUTS_RECENT_SEPARATOR)
1943 n += impl->has_recent ? 1 : 0;
1945 if (where == SHORTCUTS_HOME)
1948 n += impl->has_home ? 1 : 0;
1950 if (where == SHORTCUTS_DESKTOP)
1953 n += impl->has_desktop ? 1 : 0;
1955 if (where == SHORTCUTS_VOLUMES)
1958 n += impl->num_volumes;
1960 if (where == SHORTCUTS_SHORTCUTS)
1963 n += impl->num_shortcuts;
1965 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1968 /* If there are no bookmarks there won't be a separator */
1969 n += (impl->num_bookmarks > 0) ? 1 : 0;
1971 if (where == SHORTCUTS_BOOKMARKS)
1974 n += impl->num_bookmarks;
1976 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1981 if (where == SHORTCUTS_CURRENT_FOLDER)
1984 g_assert_not_reached ();
1991 /* Adds all the file system volumes to the shortcuts model */
1993 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1998 gboolean old_changing_folders;
2000 profile_start ("start", NULL);
2002 old_changing_folders = impl->changing_folder;
2003 impl->changing_folder = TRUE;
2005 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2006 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2007 impl->num_volumes = 0;
2009 list = _gtk_file_system_list_volumes (impl->file_system);
2013 for (l = list; l; l = l->next)
2015 GtkFileSystemVolume *volume;
2019 if (impl->local_only)
2021 if (_gtk_file_system_volume_is_mounted (volume))
2024 gboolean base_is_native = TRUE;
2026 base_file = _gtk_file_system_volume_get_root (volume);
2027 if (base_file != NULL)
2029 base_is_native = g_file_is_native (base_file);
2030 g_object_unref (base_file);
2033 if (!base_is_native)
2038 shortcuts_insert_file (impl,
2040 SHORTCUT_TYPE_VOLUME,
2041 _gtk_file_system_volume_ref (volume),
2049 impl->num_volumes = n;
2050 g_slist_free (list);
2052 if (impl->shortcuts_pane_filter_model)
2053 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2055 if (impl->shortcuts_combo_filter_model)
2056 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2058 impl->changing_folder = old_changing_folders;
2060 profile_end ("end", NULL);
2063 /* Inserts a separator node in the shortcuts list */
2065 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2066 ShortcutsIndex where)
2070 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2071 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2072 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2074 gtk_list_store_insert (impl->shortcuts_model, &iter,
2075 shortcuts_get_index (impl, where));
2076 gtk_list_store_set (impl->shortcuts_model, &iter,
2077 SHORTCUTS_COL_PIXBUF, NULL,
2078 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2079 SHORTCUTS_COL_NAME, NULL,
2080 SHORTCUTS_COL_DATA, NULL,
2081 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2085 /* Updates the list of bookmarks */
2087 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2090 gboolean old_changing_folders;
2092 GFile *list_selected = NULL;
2093 GFile *combo_selected = NULL;
2094 ShortcutType shortcut_type;
2097 profile_start ("start", NULL);
2099 old_changing_folders = impl->changing_folder;
2100 impl->changing_folder = TRUE;
2102 if (shortcuts_get_selected (impl, &iter))
2104 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2106 SHORTCUTS_COL_DATA, &col_data,
2107 SHORTCUTS_COL_TYPE, &shortcut_type,
2110 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2111 list_selected = g_object_ref (col_data);
2114 if (impl->save_folder_combo &&
2115 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2118 GtkTreeIter child_iter;
2120 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2123 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2125 SHORTCUTS_COL_DATA, &col_data,
2126 SHORTCUTS_COL_TYPE, &shortcut_type,
2129 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2130 combo_selected = g_object_ref (col_data);
2133 if (impl->num_bookmarks > 0)
2134 shortcuts_remove_rows (impl,
2135 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2136 impl->num_bookmarks + 1);
2138 impl->num_bookmarks = 0;
2139 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2141 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2142 shortcuts_append_bookmarks (impl, bookmarks);
2143 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2144 g_slist_free (bookmarks);
2146 if (impl->num_bookmarks == 0)
2147 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2149 if (impl->shortcuts_pane_filter_model)
2150 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2152 if (impl->shortcuts_combo_filter_model)
2153 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2157 shortcuts_find_folder (impl, list_selected);
2158 g_object_unref (list_selected);
2165 pos = shortcut_find_position (impl, combo_selected);
2168 if (impl->has_search)
2171 if (impl->has_recent)
2174 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2177 g_object_unref (combo_selected);
2180 impl->changing_folder = old_changing_folders;
2182 profile_end ("end", NULL);
2185 /* Appends a separator and a row to the shortcuts list for the current folder */
2187 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2191 g_assert (!impl->shortcuts_current_folder_active);
2193 g_assert (impl->current_folder != NULL);
2195 pos = shortcut_find_position (impl, impl->current_folder);
2198 GtkFileSystemVolume *volume;
2202 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2205 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2207 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2209 base_file = _gtk_file_system_volume_get_root (volume);
2213 if (base_file && g_file_equal (base_file, impl->current_folder))
2214 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2216 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2219 g_object_unref (base_file);
2221 else if (impl->save_folder_combo != NULL)
2223 if (impl->has_search)
2226 if (impl->has_recent)
2227 pos -= 2; /* + separator */
2229 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2233 /* Updates the current folder row in the shortcuts model */
2235 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2239 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2241 if (impl->shortcuts_current_folder_active)
2243 shortcuts_remove_rows (impl, pos, 2);
2244 impl->shortcuts_current_folder_active = FALSE;
2247 shortcuts_add_current_folder (impl);
2250 /* Filter function used for the shortcuts filter model */
2252 shortcuts_pane_filter_cb (GtkTreeModel *model,
2256 GtkFileChooserDefault *impl;
2260 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2262 path = gtk_tree_model_get_path (model, iter);
2266 pos = *gtk_tree_path_get_indices (path);
2267 gtk_tree_path_free (path);
2269 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2272 /* Creates the list model for shortcuts */
2274 shortcuts_model_create (GtkFileChooserDefault *impl)
2276 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2277 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2278 GDK_TYPE_PIXBUF, /* pixbuf */
2279 G_TYPE_STRING, /* name */
2280 G_TYPE_POINTER, /* path or volume */
2281 G_TYPE_INT, /* ShortcutType */
2282 G_TYPE_BOOLEAN, /* removable */
2283 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2284 G_TYPE_POINTER); /* GCancellable */
2286 shortcuts_append_search (impl);
2288 if (impl->recent_manager)
2290 shortcuts_append_recent (impl);
2291 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2294 if (impl->file_system)
2296 shortcuts_append_home (impl);
2297 shortcuts_append_desktop (impl);
2298 shortcuts_add_volumes (impl);
2301 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2302 GTK_TREE_MODEL (impl->shortcuts_model),
2305 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2306 shortcuts_pane_filter_cb,
2311 /* Callback used when the "New Folder" button is clicked */
2313 new_folder_button_clicked (GtkButton *button,
2314 GtkFileChooserDefault *impl)
2319 if (!impl->browse_files_model)
2320 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2322 /* Prevent button from being clicked twice */
2323 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2325 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2327 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2328 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2329 path, impl->list_name_column,
2332 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2333 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2335 impl->list_name_column,
2338 gtk_tree_path_free (path);
2342 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2346 source = g_idle_source_new ();
2347 g_source_set_closure (source,
2348 g_cclosure_new_object (callback, G_OBJECT (impl)));
2349 g_source_attach (source, NULL);
2354 /* Idle handler for creating a new folder after editing its name cell, or for
2355 * canceling the editing.
2358 edited_idle_cb (GtkFileChooserDefault *impl)
2360 GDK_THREADS_ENTER ();
2362 g_source_destroy (impl->edited_idle);
2363 impl->edited_idle = NULL;
2365 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2366 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2368 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2370 if (impl->edited_new_text /* not cancelled? */
2371 && (strlen (impl->edited_new_text) != 0)
2372 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2374 GError *error = NULL;
2377 file = g_file_get_child_for_display_name (impl->current_folder,
2378 impl->edited_new_text,
2382 GError *error = NULL;
2384 if (g_file_make_directory (file, NULL, &error))
2385 change_folder_and_display_error (impl, file, FALSE);
2387 error_creating_folder_dialog (impl, file, error);
2389 g_object_unref (file);
2392 error_creating_folder_dialog (impl, file, error);
2394 g_free (impl->edited_new_text);
2395 impl->edited_new_text = NULL;
2398 GDK_THREADS_LEAVE ();
2404 queue_edited_idle (GtkFileChooserDefault *impl,
2405 const gchar *new_text)
2407 /* We create the folder in an idle handler so that we don't modify the tree
2411 if (!impl->edited_idle)
2412 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2414 g_free (impl->edited_new_text);
2415 impl->edited_new_text = g_strdup (new_text);
2418 /* Callback used from the text cell renderer when the new folder is named */
2420 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2422 const gchar *new_text,
2423 GtkFileChooserDefault *impl)
2425 /* work around bug #154921 */
2426 g_object_set (cell_renderer_text,
2427 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2428 queue_edited_idle (impl, new_text);
2431 /* Callback used from the text cell renderer when the new folder edition gets
2435 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2436 GtkFileChooserDefault *impl)
2438 /* work around bug #154921 */
2439 g_object_set (cell_renderer_text,
2440 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2441 queue_edited_idle (impl, NULL);
2444 /* Creates the widgets for the filter combo box */
2446 filter_create (GtkFileChooserDefault *impl)
2448 GtkCellRenderer *cell;
2451 impl->filter_combo = gtk_combo_box_text_new ();
2452 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2454 /* Get the combo's text renderer and set ellipsize parameters */
2455 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2459 g_object_set (G_OBJECT (cell),
2460 "ellipsize", PANGO_ELLIPSIZE_END,
2463 g_list_free (cells);
2465 g_signal_connect (impl->filter_combo, "changed",
2466 G_CALLBACK (filter_combo_changed), impl);
2468 gtk_widget_set_tooltip_text (impl->filter_combo,
2469 _("Select which types of files are shown"));
2471 return impl->filter_combo;
2475 toolbutton_new (GtkFileChooserDefault *impl,
2484 item = gtk_tool_button_new (NULL, NULL);
2485 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2486 gtk_widget_show (image);
2487 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2489 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2490 g_signal_connect (item, "clicked", callback, impl);
2493 gtk_widget_show (GTK_WIDGET (item));
2495 return GTK_WIDGET (item);
2498 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2500 shortcut_find_position (GtkFileChooserDefault *impl,
2505 int current_folder_separator_idx;
2507 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2510 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2513 /* FIXME: is this still needed? */
2514 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2518 for (i = 0; i < current_folder_separator_idx; i++)
2521 ShortcutType shortcut_type;
2523 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2524 SHORTCUTS_COL_DATA, &col_data,
2525 SHORTCUTS_COL_TYPE, &shortcut_type,
2530 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2532 GtkFileSystemVolume *volume;
2537 base_file = _gtk_file_system_volume_get_root (volume);
2539 exists = base_file && g_file_equal (file, base_file);
2542 g_object_unref (base_file);
2547 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2551 model_file = col_data;
2553 if (model_file && g_file_equal (model_file, file))
2558 if (i < current_folder_separator_idx - 1)
2560 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2561 g_assert_not_reached ();
2568 /* Tries to add a bookmark from a path name */
2570 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2576 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2578 if (shortcut_find_position (impl, file) != -1)
2582 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2584 error_adding_bookmark_dialog (impl, file, error);
2592 add_bookmark_foreach_cb (GtkTreeModel *model,
2597 GtkFileChooserDefault *impl;
2600 impl = (GtkFileChooserDefault *) data;
2602 gtk_tree_model_get (model, iter,
2603 MODEL_COL_FILE, &file,
2606 shortcuts_add_bookmark_from_file (impl, file, -1);
2608 g_object_unref (file);
2611 /* Adds a bookmark from the currently selected item in the file list */
2613 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2615 GtkTreeSelection *selection;
2617 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2619 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2620 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2622 gtk_tree_selection_selected_foreach (selection,
2623 add_bookmark_foreach_cb,
2627 /* Callback used when the "Add bookmark" button is clicked */
2629 add_bookmark_button_clicked_cb (GtkButton *button,
2630 GtkFileChooserDefault *impl)
2632 bookmarks_add_selected_folder (impl);
2635 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2636 * returns FALSE if no shortcut is selected.
2639 shortcuts_get_selected (GtkFileChooserDefault *impl,
2642 GtkTreeSelection *selection;
2643 GtkTreeIter parent_iter;
2645 if (!impl->browse_shortcuts_tree_view)
2648 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2650 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2653 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2659 /* Removes the selected bookmarks */
2661 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2669 if (!shortcuts_get_selected (impl, &iter))
2672 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2673 SHORTCUTS_COL_DATA, &col_data,
2674 SHORTCUTS_COL_REMOVABLE, &removable,
2680 g_assert (col_data != NULL);
2685 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2686 error_removing_bookmark_dialog (impl, file, error);
2689 /* Callback used when the "Remove bookmark" button is clicked */
2691 remove_bookmark_button_clicked_cb (GtkButton *button,
2692 GtkFileChooserDefault *impl)
2694 remove_selected_bookmarks (impl);
2697 struct selection_check_closure {
2698 GtkFileChooserDefault *impl;
2701 gboolean all_folders;
2704 /* Used from gtk_tree_selection_selected_foreach() */
2706 selection_check_foreach_cb (GtkTreeModel *model,
2711 struct selection_check_closure *closure;
2715 gtk_tree_model_get (model, iter,
2716 MODEL_COL_FILE, &file,
2717 MODEL_COL_IS_FOLDER, &is_folder,
2723 g_object_unref (file);
2726 closure->num_selected++;
2728 closure->all_folders = closure->all_folders && is_folder;
2729 closure->all_files = closure->all_files && !is_folder;
2732 /* Checks whether the selected items in the file list are all files or all folders */
2734 selection_check (GtkFileChooserDefault *impl,
2736 gboolean *all_files,
2737 gboolean *all_folders)
2739 struct selection_check_closure closure;
2740 GtkTreeSelection *selection;
2742 closure.impl = impl;
2743 closure.num_selected = 0;
2744 closure.all_files = TRUE;
2745 closure.all_folders = TRUE;
2747 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2748 gtk_tree_selection_selected_foreach (selection,
2749 selection_check_foreach_cb,
2752 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2755 *num_selected = closure.num_selected;
2758 *all_files = closure.all_files;
2761 *all_folders = closure.all_folders;
2764 struct get_selected_file_closure {
2765 GtkFileChooserDefault *impl;
2770 get_selected_file_foreach_cb (GtkTreeModel *model,
2775 struct get_selected_file_closure *closure = data;
2779 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2780 g_object_unref (closure->file);
2781 closure->file = NULL;
2784 gtk_tree_model_get (model, iter,
2785 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2789 /* Returns a selected path from the file list */
2791 get_selected_file (GtkFileChooserDefault *impl)
2793 struct get_selected_file_closure closure;
2794 GtkTreeSelection *selection;
2796 closure.impl = impl;
2797 closure.file = NULL;
2799 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2800 gtk_tree_selection_selected_foreach (selection,
2801 get_selected_file_foreach_cb,
2804 return closure.file;
2808 GtkFileChooserDefault *impl;
2810 } UpdateTooltipData;
2813 update_tooltip (GtkTreeModel *model,
2818 UpdateTooltipData *udata = data;
2820 if (udata->tip == NULL)
2822 gchar *display_name;
2824 gtk_tree_model_get (model, iter,
2825 MODEL_COL_NAME, &display_name,
2828 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2830 g_free (display_name);
2835 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2836 * if there are no selected items *and* the current folder is not in the
2837 * bookmarks list. De-sensitize the button otherwise.
2840 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2843 gboolean all_folders;
2847 selection_check (impl, &num_selected, NULL, &all_folders);
2849 if (num_selected == 0)
2850 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2851 else if (num_selected == 1)
2855 file = get_selected_file (impl);
2856 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2858 g_object_unref (file);
2861 active = all_folders;
2863 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2865 if (impl->browse_files_popup_menu_add_shortcut_item)
2866 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2867 (num_selected == 0) ? FALSE : active);
2871 if (num_selected == 0)
2872 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2873 else if (num_selected > 1)
2874 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2877 GtkTreeSelection *selection;
2878 UpdateTooltipData data;
2880 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2883 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2887 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2892 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2893 * bookmark row is selected in the shortcuts tree.
2896 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2899 gboolean removable = FALSE;
2903 if (shortcuts_get_selected (impl, &iter))
2905 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2906 SHORTCUTS_COL_REMOVABLE, &removable,
2907 SHORTCUTS_COL_NAME, &name,
2909 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2912 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2914 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2916 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2920 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2921 _("Remove the selected bookmark"));
2926 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2929 gboolean removable = FALSE;
2931 if (impl->browse_shortcuts_popup_menu == NULL)
2934 if (shortcuts_get_selected (impl, &iter))
2935 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2936 SHORTCUTS_COL_REMOVABLE, &removable,
2939 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2940 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2943 /* GtkWidget::drag-begin handler for the shortcuts list. */
2945 shortcuts_drag_begin_cb (GtkWidget *widget,
2946 GdkDragContext *context,
2947 GtkFileChooserDefault *impl)
2950 impl->shortcuts_drag_context = g_object_ref (context);
2955 /* Removes the idle handler for outside drags */
2957 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2959 if (!impl->shortcuts_drag_outside_idle)
2962 g_source_destroy (impl->shortcuts_drag_outside_idle);
2963 impl->shortcuts_drag_outside_idle = NULL;
2967 /* GtkWidget::drag-end handler for the shortcuts list. */
2969 shortcuts_drag_end_cb (GtkWidget *widget,
2970 GdkDragContext *context,
2971 GtkFileChooserDefault *impl)
2974 g_object_unref (impl->shortcuts_drag_context);
2976 shortcuts_cancel_drag_outside_idle (impl);
2978 if (!impl->shortcuts_drag_outside)
2981 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2983 impl->shortcuts_drag_outside = FALSE;
2987 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2989 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2990 GdkDragContext *context,
2991 GtkFileChooserDefault *impl)
2993 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2996 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3000 shortcuts_drag_leave_cb (GtkWidget *widget,
3001 GdkDragContext *context,
3003 GtkFileChooserDefault *impl)
3006 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3008 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
3012 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3014 GTK_TREE_VIEW_DROP_BEFORE);
3016 g_signal_stop_emission_by_name (widget, "drag-leave");
3019 /* Computes the appropriate row and position for dropping */
3021 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3025 GtkTreeViewDropPosition *pos)
3027 GtkTreeView *tree_view;
3028 GtkTreeViewColumn *column;
3032 int bookmarks_index;
3033 int header_height = 0;
3035 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3037 if (gtk_tree_view_get_headers_visible (tree_view))
3038 header_height = _gtk_tree_view_get_header_height (tree_view);
3040 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3042 if (!gtk_tree_view_get_path_at_pos (tree_view,
3050 row = bookmarks_index + impl->num_bookmarks - 1;
3051 *path = gtk_tree_path_new_from_indices (row, -1);
3052 *pos = GTK_TREE_VIEW_DROP_AFTER;
3056 row = *gtk_tree_path_get_indices (*path);
3057 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3058 gtk_tree_path_free (*path);
3060 if (row < bookmarks_index)
3062 row = bookmarks_index;
3063 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3065 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3067 row = bookmarks_index + impl->num_bookmarks - 1;
3068 *pos = GTK_TREE_VIEW_DROP_AFTER;
3072 if (cell_y < cell.height / 2)
3073 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3075 *pos = GTK_TREE_VIEW_DROP_AFTER;
3078 *path = gtk_tree_path_new_from_indices (row, -1);
3081 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3082 * implement the destination side of DnD by hand, due to limitations in
3083 * GtkTreeView's DnD API.
3086 shortcuts_drag_motion_cb (GtkWidget *widget,
3087 GdkDragContext *context,
3091 GtkFileChooserDefault *impl)
3094 GtkTreeViewDropPosition pos;
3095 GdkDragAction action;
3098 if (gtk_drag_get_source_widget (context) == widget)
3100 shortcuts_cancel_drag_outside_idle (impl);
3102 if (impl->shortcuts_drag_outside)
3104 shortcuts_drag_set_delete_cursor (impl, FALSE);
3105 impl->shortcuts_drag_outside = FALSE;
3110 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3111 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3112 action = GDK_ACTION_COPY;
3113 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3114 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3115 action = GDK_ACTION_MOVE;
3122 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3123 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3124 gtk_tree_path_free (path);
3128 g_signal_stop_emission_by_name (widget, "drag-motion");
3132 gdk_drag_status (context, action, time_);
3139 /* GtkWidget::drag-drop handler for the shortcuts list. */
3141 shortcuts_drag_drop_cb (GtkWidget *widget,
3142 GdkDragContext *context,
3146 GtkFileChooserDefault *impl)
3149 shortcuts_cancel_drag_outside_idle (impl);
3152 g_signal_stop_emission_by_name (widget, "drag-drop");
3156 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3158 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3159 GtkSelectionData *selection_data,
3165 uris = gtk_selection_data_get_uris (selection_data);
3169 for (i = 0; uris[i]; i++)
3175 file = g_file_new_for_uri (uri);
3177 if (shortcuts_add_bookmark_from_file (impl, file, position))
3180 g_object_unref (file);
3186 /* Reorders the selected bookmark to the specified position */
3188 shortcuts_reorder (GtkFileChooserDefault *impl,
3193 ShortcutType shortcut_type;
3196 int bookmarks_index;
3201 /* Get the selected path */
3203 if (!shortcuts_get_selected (impl, &iter))
3204 g_assert_not_reached ();
3206 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3207 old_position = *gtk_tree_path_get_indices (path);
3208 gtk_tree_path_free (path);
3210 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3211 old_position -= bookmarks_index;
3212 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3214 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3215 SHORTCUTS_COL_NAME, &name,
3216 SHORTCUTS_COL_DATA, &col_data,
3217 SHORTCUTS_COL_TYPE, &shortcut_type,
3219 g_assert (col_data != NULL);
3220 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3223 g_object_ref (file); /* removal below will free file, so we need a new ref */
3225 /* Remove the path from the old position and insert it in the new one */
3227 if (new_position > old_position)
3230 if (old_position == new_position)
3234 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3236 shortcuts_add_bookmark_from_file (impl, file, new_position);
3237 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3240 error_adding_bookmark_dialog (impl, file, error);
3244 g_object_unref (file);
3248 /* Callback used when we get the drag data for the bookmarks list. We add the
3249 * received URIs as bookmarks if they are folders.
3252 shortcuts_drag_data_received_cb (GtkWidget *widget,
3253 GdkDragContext *context,
3256 GtkSelectionData *selection_data,
3261 GtkFileChooserDefault *impl;
3262 GtkTreePath *tree_path;
3263 GtkTreeViewDropPosition tree_pos;
3266 int bookmarks_index;
3268 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3270 /* Compute position */
3272 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3274 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3275 position = *gtk_tree_path_get_indices (tree_path);
3276 gtk_tree_path_free (tree_path);
3278 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3281 g_assert (position >= bookmarks_index);
3282 position -= bookmarks_index;
3284 target = gtk_selection_data_get_target (selection_data);
3286 if (gtk_targets_include_uri (&target, 1))
3287 shortcuts_drop_uris (impl, selection_data, position);
3288 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3289 shortcuts_reorder (impl, position);
3291 g_signal_stop_emission_by_name (widget, "drag-data-received");
3294 /* Callback used to display a tooltip in the shortcuts tree */
3296 shortcuts_query_tooltip_cb (GtkWidget *widget,
3299 gboolean keyboard_mode,
3300 GtkTooltip *tooltip,
3301 GtkFileChooserDefault *impl)
3303 GtkTreeModel *model;
3306 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3314 ShortcutType shortcut_type;
3316 gtk_tree_model_get (model, &iter,
3317 SHORTCUTS_COL_DATA, &col_data,
3318 SHORTCUTS_COL_TYPE, &shortcut_type,
3321 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3323 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3325 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3330 file = G_FILE (col_data);
3331 parse_name = g_file_get_parse_name (file);
3333 gtk_tooltip_set_text (tooltip, parse_name);
3335 g_free (parse_name);
3339 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3343 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3353 /* Callback used when the selection in the shortcuts tree changes */
3355 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3356 GtkFileChooserDefault *impl)
3359 GtkTreeIter child_iter;
3361 bookmarks_check_remove_sensitivity (impl);
3362 shortcuts_check_popup_sensitivity (impl);
3364 if (impl->changing_folder)
3367 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3369 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3372 shortcuts_activate_iter (impl, &child_iter);
3377 shortcuts_row_separator_func (GtkTreeModel *model,
3381 ShortcutType shortcut_type;
3383 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3385 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3389 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3391 GtkFileChooserDefault *impl)
3395 /* don't screw up focus switching with Tab */
3396 if (event->keyval == GDK_KEY_Tab
3397 || event->keyval == GDK_KEY_KP_Tab
3398 || event->keyval == GDK_KEY_ISO_Left_Tab
3399 || event->length < 1)
3402 if (impl->location_entry)
3403 entry = impl->location_entry;
3404 else if (impl->search_entry)
3405 entry = impl->search_entry;
3411 gtk_widget_grab_focus (entry);
3412 return gtk_widget_event (entry, (GdkEvent *) event);
3418 /* Callback used when the file list's popup menu is detached */
3420 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3423 GtkFileChooserDefault *impl;
3425 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3426 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3428 impl->browse_shortcuts_popup_menu = NULL;
3429 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3430 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3434 remove_shortcut_cb (GtkMenuItem *item,
3435 GtkFileChooserDefault *impl)
3437 remove_selected_bookmarks (impl);
3440 /* Rename the selected bookmark */
3442 rename_selected_bookmark (GtkFileChooserDefault *impl)
3446 GtkTreeViewColumn *column;
3447 GtkCellRenderer *cell;
3450 if (shortcuts_get_selected (impl, &iter))
3452 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3453 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3454 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3455 cell = g_list_nth_data (renderers, 1);
3456 g_list_free (renderers);
3457 g_object_set (cell, "editable", TRUE, NULL);
3458 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3459 path, column, cell, TRUE);
3460 gtk_tree_path_free (path);
3465 rename_shortcut_cb (GtkMenuItem *item,
3466 GtkFileChooserDefault *impl)
3468 rename_selected_bookmark (impl);
3471 /* Constructs the popup menu for the file list if needed */
3473 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3477 if (impl->browse_shortcuts_popup_menu)
3480 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3481 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3482 impl->browse_shortcuts_tree_view,
3483 shortcuts_popup_menu_detach_cb);
3485 item = gtk_image_menu_item_new_with_label (_("Remove"));
3486 impl->browse_shortcuts_popup_menu_remove_item = item;
3487 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3488 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3489 g_signal_connect (item, "activate",
3490 G_CALLBACK (remove_shortcut_cb), impl);
3491 gtk_widget_show (item);
3492 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3494 item = gtk_menu_item_new_with_label (_("Rename..."));
3495 impl->browse_shortcuts_popup_menu_rename_item = item;
3496 g_signal_connect (item, "activate",
3497 G_CALLBACK (rename_shortcut_cb), impl);
3498 gtk_widget_show (item);
3499 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3503 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3505 shortcuts_build_popup_menu (impl);
3506 shortcuts_check_popup_sensitivity (impl);
3510 popup_position_func (GtkMenu *menu,
3514 gpointer user_data);
3517 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3518 GdkEventButton *event)
3520 shortcuts_update_popup_menu (impl);
3522 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3523 NULL, NULL, NULL, NULL,
3524 event->button, event->time);
3527 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3529 popup_position_func, impl->browse_shortcuts_tree_view,
3530 0, GDK_CURRENT_TIME);
3531 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3536 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3538 shortcuts_popup_menu_cb (GtkWidget *widget,
3539 GtkFileChooserDefault *impl)
3541 shortcuts_popup_menu (impl, NULL);
3545 /* Callback used when a button is pressed on the shortcuts list.
3546 * We trap button 3 to bring up a popup menu.
3549 shortcuts_button_press_event_cb (GtkWidget *widget,
3550 GdkEventButton *event,
3551 GtkFileChooserDefault *impl)
3553 static gboolean in_press = FALSE;
3559 if (event->button != 3)
3563 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3569 shortcuts_popup_menu (impl, event);
3574 shortcuts_edited (GtkCellRenderer *cell,
3577 GtkFileChooserDefault *impl)
3583 g_object_set (cell, "editable", FALSE, NULL);
3585 path = gtk_tree_path_new_from_string (path_string);
3586 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3587 g_assert_not_reached ();
3589 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3590 SHORTCUTS_COL_DATA, &shortcut,
3592 gtk_tree_path_free (path);
3594 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3598 shortcuts_editing_canceled (GtkCellRenderer *cell,
3599 GtkFileChooserDefault *impl)
3601 g_object_set (cell, "editable", FALSE, NULL);
3604 /* Creates the widgets for the shortcuts and bookmarks tree */
3606 shortcuts_list_create (GtkFileChooserDefault *impl)
3609 GtkTreeSelection *selection;
3610 GtkTreeViewColumn *column;
3611 GtkCellRenderer *renderer;
3613 /* Target types for dragging a row to/from the shortcuts list */
3614 const GtkTargetEntry tree_model_row_targets[] = {
3615 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3618 /* Scrolled window */
3620 swin = gtk_scrolled_window_new (NULL, NULL);
3621 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3622 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3623 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3625 gtk_widget_show (swin);
3628 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3629 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3630 GTK_STYLE_CLASS_SIDEBAR);
3631 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3632 #ifdef PROFILE_FILE_CHOOSER
3633 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3636 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3638 * 1. user brings up a SAVE dialog
3639 * 2. user clicks on a shortcut in the shortcuts pane
3640 * 3. user starts typing a filename
3642 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3643 * support interactive search. However, we'd rather focus the location entry
3644 * so that the user can type *there*.
3646 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3647 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3648 * if the user starts typing while the focus is in the shortcuts pane.
3650 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3651 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3653 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3654 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3655 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3656 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3657 /* Accessible object name for the file chooser's shortcuts pane */
3658 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3660 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3662 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3664 tree_model_row_targets,
3665 G_N_ELEMENTS (tree_model_row_targets),
3668 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3669 GTK_DEST_DEFAULT_ALL,
3670 tree_model_row_targets,
3671 G_N_ELEMENTS (tree_model_row_targets),
3672 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3673 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3675 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3676 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3677 gtk_tree_selection_set_select_function (selection,
3678 shortcuts_select_func,
3681 g_signal_connect (selection, "changed",
3682 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3684 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3685 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3687 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3688 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3689 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3690 G_CALLBACK (shortcuts_drag_end_cb), impl);
3691 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3692 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3694 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3695 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3696 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3697 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3698 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3699 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3700 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3701 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3703 /* Support tooltips */
3704 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3705 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3706 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3708 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3709 gtk_widget_show (impl->browse_shortcuts_tree_view);
3713 column = gtk_tree_view_column_new ();
3714 /* Column header for the file chooser's shortcuts pane */
3715 gtk_tree_view_column_set_title (column, _("_Places"));
3717 renderer = gtk_cell_renderer_pixbuf_new ();
3718 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3719 gtk_tree_view_column_set_attributes (column, renderer,
3720 "pixbuf", SHORTCUTS_COL_PIXBUF,
3721 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3724 renderer = gtk_cell_renderer_text_new ();
3725 g_object_set (renderer,
3727 "ellipsize", PANGO_ELLIPSIZE_END,
3729 g_signal_connect (renderer, "edited",
3730 G_CALLBACK (shortcuts_edited), impl);
3731 g_signal_connect (renderer, "editing-canceled",
3732 G_CALLBACK (shortcuts_editing_canceled), impl);
3733 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3734 gtk_tree_view_column_set_attributes (column, renderer,
3735 "text", SHORTCUTS_COL_NAME,
3738 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3739 shortcuts_row_separator_func,
3742 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3747 /* Creates the widgets for the shortcuts/bookmarks pane */
3749 shortcuts_pane_create (GtkFileChooserDefault *impl,
3750 GtkSizeGroup *size_group)
3755 GtkStyleContext *context;
3758 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3759 gtk_widget_show (vbox);
3761 /* Shortcuts tree */
3763 widget = shortcuts_list_create (impl);
3765 gtk_size_group_add_widget (size_group, widget);
3766 context = gtk_widget_get_style_context (widget);
3767 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3769 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3771 /* Box for buttons */
3773 toolbar = gtk_toolbar_new ();
3774 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3776 context = gtk_widget_get_style_context (toolbar);
3777 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3778 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3780 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3781 gtk_widget_show (toolbar);
3783 /* Add bookmark button */
3784 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3785 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3789 G_CALLBACK (add_bookmark_button_clicked_cb));
3790 g_object_unref (icon);
3792 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3793 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3794 _("Add the selected folder to the Bookmarks"));
3796 /* Remove bookmark button */
3797 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3798 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3802 G_CALLBACK (remove_bookmark_button_clicked_cb));
3803 g_object_unref (icon);
3804 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3805 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3806 _("Remove the selected bookmark"));
3812 key_is_left_or_right (GdkEventKey *event)
3816 modifiers = gtk_accelerator_get_default_mod_mask ();
3818 return ((event->keyval == GDK_KEY_Right
3819 || event->keyval == GDK_KEY_KP_Right
3820 || event->keyval == GDK_KEY_Left
3821 || event->keyval == GDK_KEY_KP_Left)
3822 && (event->state & modifiers) == 0);
3825 /* Handles key press events on the file list, so that we can trap Enter to
3826 * activate the default button on our own. Also, checks to see if '/' has been
3827 * pressed. See comment by tree_view_keybinding_cb() for more details.
3830 browse_files_key_press_event_cb (GtkWidget *widget,
3834 GtkFileChooserDefault *impl;
3837 impl = (GtkFileChooserDefault *) data;
3839 modifiers = gtk_accelerator_get_default_mod_mask ();
3841 if ((event->keyval == GDK_KEY_slash
3842 || event->keyval == GDK_KEY_KP_Divide
3844 || event->keyval == GDK_KEY_asciitilde
3846 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3848 location_popup_handler (impl, event->string);
3852 if (key_is_left_or_right (event))
3854 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3858 if ((event->keyval == GDK_KEY_Return
3859 || event->keyval == GDK_KEY_ISO_Enter
3860 || event->keyval == GDK_KEY_KP_Enter
3861 || event->keyval == GDK_KEY_space
3862 || event->keyval == GDK_KEY_KP_Space)
3863 && ((event->state & modifiers) == 0)
3864 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3865 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3869 window = get_toplevel (widget);
3872 GtkWidget *default_widget, *focus_widget;
3874 default_widget = gtk_window_get_default_widget (window);
3875 focus_widget = gtk_window_get_focus (window);
3877 if (widget != default_widget &&
3878 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3880 gtk_window_activate_default (window);
3890 /* Callback used when the file list's popup menu is detached */
3892 popup_menu_detach_cb (GtkWidget *attach_widget,
3895 GtkFileChooserDefault *impl;
3897 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3898 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3900 impl->browse_files_popup_menu = NULL;
3901 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3902 impl->browse_files_popup_menu_hidden_files_item = NULL;
3905 /* Callback used when the "Add to Bookmarks" menu item is activated */
3907 add_to_shortcuts_cb (GtkMenuItem *item,
3908 GtkFileChooserDefault *impl)
3910 bookmarks_add_selected_folder (impl);
3913 /* Callback used when the "Show Hidden Files" menu item is toggled */
3915 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3916 GtkFileChooserDefault *impl)
3919 "show-hidden", gtk_check_menu_item_get_active (item),
3923 /* Callback used when the "Show Size Column" menu item is toggled */
3925 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3926 GtkFileChooserDefault *impl)
3928 impl->show_size_column = gtk_check_menu_item_get_active (item);
3930 gtk_tree_view_column_set_visible (impl->list_size_column,
3931 impl->show_size_column);
3934 /* Shows an error dialog about not being able to select a dragged file */
3936 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3941 _("Could not select file"),
3946 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3951 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3953 for (i = 1; uris[i]; i++)
3956 GError *error = NULL;
3959 file = g_file_new_for_uri (uri);
3961 gtk_file_chooser_default_select_file (chooser, file, &error);
3963 error_selecting_dragged_file_dialog (impl, file, error);
3965 g_object_unref (file);
3969 struct FileListDragData
3971 GtkFileChooserDefault *impl;
3977 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3979 const GError *error,
3982 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3983 struct FileListDragData *data = user_data;
3984 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3986 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3989 data->impl->file_list_drag_data_received_cancellable = NULL;
3991 if (cancelled || error)
3994 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3995 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3996 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3997 change_folder_and_display_error (data->impl, data->file, FALSE);
4000 GError *error = NULL;
4002 gtk_file_chooser_default_unselect_all (chooser);
4003 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4005 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4007 browse_files_center_selected_row (data->impl);
4010 if (data->impl->select_multiple)
4011 file_list_drag_data_select_uris (data->impl, data->uris);
4014 g_object_unref (data->impl);
4015 g_strfreev (data->uris);
4016 g_object_unref (data->file);
4019 g_object_unref (cancellable);
4023 file_list_drag_data_received_cb (GtkWidget *widget,
4024 GdkDragContext *context,
4027 GtkSelectionData *selection_data,
4032 GtkFileChooserDefault *impl;
4037 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4039 /* Allow only drags from other widgets; see bug #533891. */
4040 if (gtk_drag_get_source_widget (context) == widget)
4042 g_signal_stop_emission_by_name (widget, "drag-data-received");
4046 /* Parse the text/uri-list string, navigate to the first one */
4047 uris = gtk_selection_data_get_uris (selection_data);
4048 if (uris && uris[0])
4050 struct FileListDragData *data;
4053 file = g_file_new_for_uri (uri);
4055 data = g_new0 (struct FileListDragData, 1);
4056 data->impl = g_object_ref (impl);
4060 if (impl->file_list_drag_data_received_cancellable)
4061 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4063 impl->file_list_drag_data_received_cancellable =
4064 _gtk_file_system_get_info (impl->file_system, file,
4066 file_list_drag_data_received_get_info_cb,
4070 g_signal_stop_emission_by_name (widget, "drag-data-received");
4073 /* Don't do anything with the drag_drop signal */
4075 file_list_drag_drop_cb (GtkWidget *widget,
4076 GdkDragContext *context,
4080 GtkFileChooserDefault *impl)
4082 g_signal_stop_emission_by_name (widget, "drag-drop");
4086 /* Disable the normal tree drag motion handler, it makes it look like you're
4087 dropping the dragged item onto a tree item */
4089 file_list_drag_motion_cb (GtkWidget *widget,
4090 GdkDragContext *context,
4094 GtkFileChooserDefault *impl)
4096 g_signal_stop_emission_by_name (widget, "drag-motion");
4100 /* Constructs the popup menu for the file list if needed */
4102 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4106 if (impl->browse_files_popup_menu)
4109 impl->browse_files_popup_menu = gtk_menu_new ();
4110 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4111 impl->browse_files_tree_view,
4112 popup_menu_detach_cb);
4114 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4115 impl->browse_files_popup_menu_add_shortcut_item = item;
4116 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4117 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4118 g_signal_connect (item, "activate",
4119 G_CALLBACK (add_to_shortcuts_cb), impl);
4120 gtk_widget_show (item);
4121 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4123 item = gtk_separator_menu_item_new ();
4124 gtk_widget_show (item);
4125 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4127 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4128 impl->browse_files_popup_menu_hidden_files_item = item;
4129 g_signal_connect (item, "toggled",
4130 G_CALLBACK (show_hidden_toggled_cb), impl);
4131 gtk_widget_show (item);
4132 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4134 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4135 impl->browse_files_popup_menu_size_column_item = item;
4136 g_signal_connect (item, "toggled",
4137 G_CALLBACK (show_size_column_toggled_cb), impl);
4138 gtk_widget_show (item);
4139 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4141 bookmarks_check_add_sensitivity (impl);
4144 /* Updates the popup menu for the file list, creating it if necessary */
4146 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4148 file_list_build_popup_menu (impl);
4150 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4152 /* The sensitivity of the Add to Bookmarks item is set in
4153 * bookmarks_check_add_sensitivity()
4156 /* 'Show Hidden Files' */
4157 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4158 G_CALLBACK (show_hidden_toggled_cb), impl);
4159 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4161 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4162 G_CALLBACK (show_hidden_toggled_cb), impl);
4164 /* 'Show Size Column' */
4165 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4166 G_CALLBACK (show_size_column_toggled_cb), impl);
4167 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4168 impl->show_size_column);
4169 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4170 G_CALLBACK (show_size_column_toggled_cb), impl);
4174 popup_position_func (GtkMenu *menu,
4180 GtkAllocation allocation;
4181 GtkWidget *widget = GTK_WIDGET (user_data);
4182 GdkScreen *screen = gtk_widget_get_screen (widget);
4185 GdkRectangle monitor;
4187 g_return_if_fail (gtk_widget_get_realized (widget));
4189 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4191 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4194 gtk_widget_get_allocation (widget, &allocation);
4195 *x += (allocation.width - req.width) / 2;
4196 *y += (allocation.height - req.height) / 2;
4198 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4199 gtk_menu_set_monitor (menu, monitor_num);
4200 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4202 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4203 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4209 file_list_popup_menu (GtkFileChooserDefault *impl,
4210 GdkEventButton *event)
4212 file_list_update_popup_menu (impl);
4214 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4215 NULL, NULL, NULL, NULL,
4216 event->button, event->time);
4219 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4221 popup_position_func, impl->browse_files_tree_view,
4222 0, GDK_CURRENT_TIME);
4223 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4229 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4231 list_popup_menu_cb (GtkWidget *widget,
4232 GtkFileChooserDefault *impl)
4234 file_list_popup_menu (impl, NULL);
4238 /* Callback used when a button is pressed on the file list. We trap button 3 to
4239 * bring up a popup menu.
4242 list_button_press_event_cb (GtkWidget *widget,
4243 GdkEventButton *event,
4244 GtkFileChooserDefault *impl)
4246 static gboolean in_press = FALSE;
4251 if (event->button != 3)
4255 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4258 file_list_popup_menu (impl, event);
4263 OperationMode operation_mode;
4264 gint general_column;
4268 /* Sets the sort column IDs for the file list based on the operation mode */
4270 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4272 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4273 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4274 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4278 file_list_query_tooltip_cb (GtkWidget *widget,
4281 gboolean keyboard_tip,
4282 GtkTooltip *tooltip,
4285 GtkFileChooserDefault *impl = user_data;
4286 GtkTreeModel *model;
4292 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4296 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4299 &model, &path, &iter))
4302 gtk_tree_model_get (model, &iter,
4303 MODEL_COL_FILE, &file,
4308 gtk_tree_path_free (path);
4312 filename = g_file_get_path (file);
4313 gtk_tooltip_set_text (tooltip, filename);
4314 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4319 g_object_unref (file);
4320 gtk_tree_path_free (path);
4326 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4330 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4331 gtk_cell_renderer_set_fixed_size (renderer,
4332 xpad * 2 + impl->icon_size,
4333 ypad * 2 + impl->icon_size);
4336 /* Creates the widgets for the file list */
4338 create_file_list (GtkFileChooserDefault *impl)
4341 GtkTreeSelection *selection;
4342 GtkTreeViewColumn *column;
4343 GtkCellRenderer *renderer;
4345 /* Scrolled window */
4346 swin = gtk_scrolled_window_new (NULL, NULL);
4347 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4348 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4349 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4352 /* Tree/list view */
4354 impl->browse_files_tree_view = gtk_tree_view_new ();
4355 #ifdef PROFILE_FILE_CHOOSER
4356 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4358 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4359 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4361 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4362 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4364 gtk_drag_dest_set (impl->browse_files_tree_view,
4365 GTK_DEST_DEFAULT_ALL,
4367 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4368 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4370 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4371 G_CALLBACK (list_row_activated), impl);
4372 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4373 G_CALLBACK (browse_files_key_press_event_cb), impl);
4374 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4375 G_CALLBACK (list_popup_menu_cb), impl);
4376 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4377 G_CALLBACK (list_button_press_event_cb), impl);
4379 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4380 G_CALLBACK (file_list_drag_data_received_cb), impl);
4381 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4382 G_CALLBACK (file_list_drag_drop_cb), impl);
4383 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4384 G_CALLBACK (file_list_drag_motion_cb), impl);
4386 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4387 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4388 G_CALLBACK (file_list_query_tooltip_cb), impl);
4390 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4391 gtk_tree_selection_set_select_function (selection,
4394 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4397 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4398 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4400 g_signal_connect (selection, "changed",
4401 G_CALLBACK (list_selection_changed), impl);
4403 /* Keep the column order in sync with update_cell_renderer_attributes() */
4405 /* Filename column */
4407 impl->list_name_column = gtk_tree_view_column_new ();
4408 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4409 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4410 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4412 renderer = gtk_cell_renderer_pixbuf_new ();
4413 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4414 set_icon_cell_renderer_fixed_size (impl, renderer);
4415 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4417 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4418 g_object_set (impl->list_name_renderer,
4419 "ellipsize", PANGO_ELLIPSIZE_END,
4421 g_signal_connect (impl->list_name_renderer, "edited",
4422 G_CALLBACK (renderer_edited_cb), impl);
4423 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4424 G_CALLBACK (renderer_editing_canceled_cb), impl);
4425 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4427 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4431 column = gtk_tree_view_column_new ();
4432 gtk_tree_view_column_set_resizable (column, TRUE);
4433 gtk_tree_view_column_set_title (column, _("Size"));
4435 renderer = gtk_cell_renderer_text_new ();
4436 g_object_set (renderer,
4437 "alignment", PANGO_ALIGN_RIGHT,
4439 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4440 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4441 impl->list_size_column = column;
4443 /* Modification time column */
4445 column = gtk_tree_view_column_new ();
4446 gtk_tree_view_column_set_resizable (column, TRUE);
4447 gtk_tree_view_column_set_title (column, _("Modified"));
4449 renderer = gtk_cell_renderer_text_new ();
4450 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4451 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4452 impl->list_mtime_column = column;
4454 file_list_set_sort_column_ids (impl);
4455 update_cell_renderer_attributes (impl);
4457 gtk_widget_show_all (swin);
4463 create_path_bar (GtkFileChooserDefault *impl)
4465 GtkWidget *path_bar;
4467 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4468 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4473 /* Creates the widgets for the files/folders pane */
4475 file_pane_create (GtkFileChooserDefault *impl,
4476 GtkSizeGroup *size_group)
4482 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4483 gtk_widget_show (vbox);
4485 /* Box for lists and preview */
4487 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4488 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4489 gtk_widget_show (hbox);
4493 widget = create_file_list (impl);
4494 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4495 gtk_size_group_add_widget (size_group, widget);
4499 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4500 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4501 /* Don't show preview box initially */
4505 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4507 widget = filter_create (impl);
4509 gtk_widget_show (widget);
4510 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4512 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4517 /* Callback used when the selection changes in the save folder combo box */
4519 save_folder_combo_changed_cb (GtkComboBox *combo,
4520 GtkFileChooserDefault *impl)
4524 if (impl->changing_folder)
4527 if (gtk_combo_box_get_active_iter (combo, &iter))
4529 GtkTreeIter child_iter;
4531 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4534 shortcuts_activate_iter (impl, &child_iter);
4539 save_folder_update_tooltip (GtkComboBox *combo,
4540 GtkFileChooserDefault *impl)
4547 if (gtk_combo_box_get_active_iter (combo, &iter))
4549 GtkTreeIter child_iter;
4551 ShortcutType shortcut_type;
4553 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4556 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4557 SHORTCUTS_COL_DATA, &col_data,
4558 SHORTCUTS_COL_TYPE, &shortcut_type,
4561 if (shortcut_type == SHORTCUT_TYPE_FILE)
4562 tooltip = g_file_get_parse_name (G_FILE (col_data));
4565 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4566 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4567 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4571 /* Filter function used to filter out the Search item and its separator.
4572 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4575 shortcuts_combo_filter_func (GtkTreeModel *model,
4579 GtkFileChooserDefault *impl;
4580 GtkTreePath *tree_path;
4585 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4587 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4589 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4590 g_assert (tree_path != NULL);
4592 indices = gtk_tree_path_get_indices (tree_path);
4596 if (impl->has_search)
4598 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4599 if (idx == indices[0])
4603 if (impl->has_recent)
4605 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4606 if (idx == indices[0])
4610 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4611 if (idx == indices[0])
4616 gtk_tree_path_free (tree_path);
4621 /* Creates the combo box with the save folders */
4623 save_folder_combo_create (GtkFileChooserDefault *impl)
4626 GtkCellRenderer *cell;
4628 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4629 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4630 shortcuts_combo_filter_func,
4634 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4635 "model", impl->shortcuts_combo_filter_model,
4636 "focus-on-click", FALSE,
4638 gtk_widget_show (combo);
4640 cell = gtk_cell_renderer_pixbuf_new ();
4641 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4642 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4643 "pixbuf", SHORTCUTS_COL_PIXBUF,
4644 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4645 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4648 cell = gtk_cell_renderer_text_new ();
4649 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4650 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4651 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4652 "text", SHORTCUTS_COL_NAME,
4653 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4656 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4657 shortcuts_row_separator_func,
4660 g_signal_connect (combo, "changed",
4661 G_CALLBACK (save_folder_combo_changed_cb), impl);
4662 g_signal_connect (combo, "changed",
4663 G_CALLBACK (save_folder_update_tooltip), impl);
4668 /* Creates the widgets specific to Save mode */
4670 save_widgets_create (GtkFileChooserDefault *impl)
4676 if (impl->save_widgets != NULL)
4679 location_switch_to_path_bar (impl);
4681 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4683 table = gtk_table_new (2, 2, FALSE);
4684 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4685 gtk_widget_show (table);
4686 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4687 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4691 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4692 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4693 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4694 gtk_table_attach (GTK_TABLE (table), widget,
4698 gtk_widget_show (widget);
4700 /* Location entry */
4702 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4703 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4705 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4706 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4707 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4708 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4710 GTK_EXPAND | GTK_FILL, 0,
4712 gtk_widget_show (impl->location_entry);
4713 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4716 impl->save_folder_label = gtk_label_new (NULL);
4717 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4718 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4719 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4723 gtk_widget_show (impl->save_folder_label);
4725 impl->save_folder_combo = save_folder_combo_create (impl);
4726 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4728 GTK_EXPAND | GTK_FILL, GTK_FILL,
4730 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4732 impl->save_widgets = vbox;
4733 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4734 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4735 gtk_widget_show (impl->save_widgets);
4738 /* Destroys the widgets specific to Save mode */
4740 save_widgets_destroy (GtkFileChooserDefault *impl)
4742 if (impl->save_widgets == NULL)
4745 gtk_widget_destroy (impl->save_widgets);
4746 impl->save_widgets = NULL;
4747 impl->location_entry = NULL;
4748 impl->save_folder_label = NULL;
4749 impl->save_folder_combo = NULL;
4752 /* Turns on the path bar widget. Can be called even if we are already in that
4756 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4758 if (impl->location_entry)
4760 gtk_widget_destroy (impl->location_entry);
4761 impl->location_entry = NULL;
4764 gtk_widget_hide (impl->location_entry_box);
4767 /* Sets the full path of the current folder as the text in the location entry. */
4769 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4771 gchar *text, *filename;
4773 if (!impl->current_folder)
4776 filename = g_file_get_path (impl->current_folder);
4780 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4784 text = g_file_get_uri (impl->current_folder);
4788 gboolean need_slash;
4791 len = strlen (text);
4792 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4798 slash_text = g_new (char, len + 2);
4799 strcpy (slash_text, text);
4800 slash_text[len] = G_DIR_SEPARATOR;
4801 slash_text[len + 1] = 0;
4807 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4814 /* Turns on the location entry. Can be called even if we are already in that
4818 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4820 /* when in search or recent files mode, we are not showing the
4821 * location_entry_box container, so there's no point in switching
4824 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4825 impl->operation_mode == OPERATION_MODE_RECENT)
4828 if (impl->location_entry)
4829 gtk_widget_destroy (impl->location_entry);
4833 gtk_widget_show (impl->location_entry_box);
4837 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4838 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4840 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4841 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4843 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4844 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4846 /* Configure the entry */
4848 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4849 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4853 gtk_widget_show (impl->location_entry);
4854 gtk_widget_grab_focus (impl->location_entry);
4857 /* Sets a new location mode. set_buttons determines whether the toggle button
4858 * for the mode will also be changed.
4861 location_mode_set (GtkFileChooserDefault *impl,
4862 LocationMode new_mode,
4863 gboolean set_button)
4865 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4866 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4868 GtkWindow *toplevel;
4869 GtkWidget *current_focus;
4870 gboolean button_active;
4871 gboolean switch_to_file_list;
4875 case LOCATION_MODE_PATH_BAR:
4876 button_active = FALSE;
4878 /* The location_entry will disappear when we switch to path bar mode. So,
4879 * we'll focus the file list in that case, to avoid having a window with
4880 * no focused widget.
4882 toplevel = get_toplevel (GTK_WIDGET (impl));
4883 switch_to_file_list = FALSE;
4886 current_focus = gtk_window_get_focus (toplevel);
4887 if (!current_focus || current_focus == impl->location_entry)
4888 switch_to_file_list = TRUE;
4891 location_switch_to_path_bar (impl);
4893 if (switch_to_file_list)
4894 gtk_widget_grab_focus (impl->browse_files_tree_view);
4898 case LOCATION_MODE_FILENAME_ENTRY:
4899 button_active = TRUE;
4900 location_switch_to_filename_entry (impl);
4904 g_assert_not_reached ();
4910 g_signal_handlers_block_by_func (impl->location_button,
4911 G_CALLBACK (location_button_toggled_cb), impl);
4913 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4915 g_signal_handlers_unblock_by_func (impl->location_button,
4916 G_CALLBACK (location_button_toggled_cb), impl);
4920 impl->location_mode = new_mode;
4924 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4926 /* when in search or recent files mode, we are not showing the
4927 * location_entry_box container, so there's no point in switching
4930 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4931 impl->operation_mode == OPERATION_MODE_RECENT)
4934 /* If the file entry is not visible, show it.
4935 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4937 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4939 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4941 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4943 if (gtk_widget_has_focus (impl->location_entry))
4945 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4949 gtk_widget_grab_focus (impl->location_entry);
4954 /* Callback used when one of the location mode buttons is toggled */
4956 location_button_toggled_cb (GtkToggleButton *toggle,
4957 GtkFileChooserDefault *impl)
4960 LocationMode new_mode;
4962 is_active = gtk_toggle_button_get_active (toggle);
4966 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4967 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4971 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4972 new_mode = LOCATION_MODE_PATH_BAR;
4975 location_mode_set (impl, new_mode, FALSE);
4978 /* Creates a toggle button for the location entry. */
4980 location_button_create (GtkFileChooserDefault *impl)
4985 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4986 gtk_widget_show (image);
4988 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4992 g_signal_connect (impl->location_button, "toggled",
4993 G_CALLBACK (location_button_toggled_cb), impl);
4995 str = _("Type a file name");
4997 gtk_widget_set_tooltip_text (impl->location_button, str);
4998 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5001 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5003 browse_widgets_create (GtkFileChooserDefault *impl)
5008 GtkSizeGroup *size_group;
5010 /* size group is used by the scrolled windows of the panes */
5011 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5012 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5014 /* Location widgets */
5015 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5016 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5017 gtk_widget_show (impl->browse_path_bar_hbox);
5019 /* Size group that allows the path bar to be the same size between modes */
5020 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5021 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5023 /* Location button */
5025 location_button_create (impl);
5026 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5027 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5031 impl->browse_path_bar = create_path_bar (impl);
5032 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5033 gtk_widget_show_all (impl->browse_path_bar);
5034 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5035 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
5038 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5039 g_signal_connect (impl->browse_new_folder_button, "clicked",
5040 G_CALLBACK (new_folder_button_clicked), impl);
5041 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5043 /* Box for the location label and entry */
5045 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5046 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5048 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5049 gtk_widget_show (impl->location_label);
5050 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5054 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5055 gtk_widget_show (hpaned);
5056 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5058 widget = shortcuts_pane_create (impl, size_group);
5059 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5060 widget = file_pane_create (impl, size_group);
5061 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5062 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5063 g_object_unref (size_group);
5069 gtk_file_chooser_default_constructor (GType type,
5070 guint n_construct_properties,
5071 GObjectConstructParam *construct_params)
5073 GtkFileChooserDefault *impl;
5076 profile_start ("start", NULL);
5078 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5079 n_construct_properties,
5081 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5083 g_assert (impl->file_system);
5085 gtk_widget_push_composite_child ();
5087 /* Shortcuts model */
5088 shortcuts_model_create (impl);
5090 /* The browse widgets */
5091 impl->browse_widgets = browse_widgets_create (impl);
5092 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5094 /* Alignment to hold extra widget */
5095 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5096 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5097 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5098 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5100 gtk_widget_pop_composite_child ();
5101 update_appearance (impl);
5103 profile_end ("end", NULL);
5108 /* Sets the extra_widget by packing it in the appropriate place */
5110 set_extra_widget (GtkFileChooserDefault *impl,
5111 GtkWidget *extra_widget)
5115 g_object_ref (extra_widget);
5116 /* FIXME: is this right ? */
5117 gtk_widget_show (extra_widget);
5120 if (impl->extra_widget)
5122 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5123 g_object_unref (impl->extra_widget);
5126 impl->extra_widget = extra_widget;
5127 if (impl->extra_widget)
5129 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5130 gtk_widget_show (impl->extra_align);
5133 gtk_widget_hide (impl->extra_align);
5137 set_local_only (GtkFileChooserDefault *impl,
5138 gboolean local_only)
5140 if (local_only != impl->local_only)
5142 impl->local_only = local_only;
5144 if (impl->location_entry)
5145 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5147 if (impl->shortcuts_model && impl->file_system)
5149 shortcuts_add_volumes (impl);
5150 shortcuts_add_bookmarks (impl);
5153 if (local_only && impl->current_folder &&
5154 !g_file_is_native (impl->current_folder))
5156 /* If we are pointing to a non-local folder, make an effort to change
5157 * back to a local folder, but it's really up to the app to not cause
5158 * such a situation, so we ignore errors.
5160 const gchar *home = g_get_home_dir ();
5166 home_file = g_file_new_for_path (home);
5168 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5170 g_object_unref (home_file);
5176 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5177 GtkFileChooserDefault *impl)
5179 shortcuts_add_volumes (impl);
5180 shortcuts_add_bookmarks (impl);
5182 bookmarks_check_add_sensitivity (impl);
5183 bookmarks_check_remove_sensitivity (impl);
5184 shortcuts_check_popup_sensitivity (impl);
5187 /* Sets the file chooser to multiple selection mode */
5189 set_select_multiple (GtkFileChooserDefault *impl,
5190 gboolean select_multiple,
5191 gboolean property_notify)
5193 GtkTreeSelection *selection;
5194 GtkSelectionMode mode;
5196 if (select_multiple == impl->select_multiple)
5199 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5201 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5202 gtk_tree_selection_set_mode (selection, mode);
5204 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5206 impl->select_multiple = select_multiple;
5207 g_object_notify (G_OBJECT (impl), "select-multiple");
5209 check_preview_change (impl);
5213 set_file_system_backend (GtkFileChooserDefault *impl)
5215 profile_start ("start for backend", "default");
5217 impl->file_system = _gtk_file_system_new ();
5219 g_signal_connect (impl->file_system, "volumes-changed",
5220 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5221 g_signal_connect (impl->file_system, "bookmarks-changed",
5222 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5224 profile_end ("end", NULL);
5228 unset_file_system_backend (GtkFileChooserDefault *impl)
5230 g_signal_handlers_disconnect_by_func (impl->file_system,
5231 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5233 g_object_unref (impl->file_system);
5235 impl->file_system = NULL;
5238 /* This function is basically a do_all function.
5240 * It sets the visibility on all the widgets based on the current state, and
5241 * moves the custom_widget if needed.
5244 update_appearance (GtkFileChooserDefault *impl)
5246 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5247 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5251 gtk_widget_hide (impl->location_button);
5252 save_widgets_create (impl);
5254 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5255 text = _("Save in _folder:");
5257 text = _("Create in _folder:");
5259 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5261 if (impl->select_multiple)
5263 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5264 "Re-setting to single selection mode.");
5265 set_select_multiple (impl, FALSE, TRUE);
5268 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5269 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5271 gtk_widget_show (impl->location_button);
5272 save_widgets_destroy (impl);
5273 gtk_widget_show (impl->browse_widgets);
5274 location_mode_set (impl, impl->location_mode, TRUE);
5277 if (impl->location_entry)
5278 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5280 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5281 gtk_widget_hide (impl->browse_new_folder_button);
5283 gtk_widget_show (impl->browse_new_folder_button);
5285 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5286 * of files may change depending whether we are in a file or folder-only mode.
5288 gtk_widget_queue_draw (impl->browse_files_tree_view);
5290 emit_default_size_changed (impl);
5294 gtk_file_chooser_default_set_property (GObject *object,
5296 const GValue *value,
5300 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5304 case GTK_FILE_CHOOSER_PROP_ACTION:
5306 GtkFileChooserAction action = g_value_get_enum (value);
5308 if (action != impl->action)
5310 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5312 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5313 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5314 && impl->select_multiple)
5316 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5317 "this is not allowed in multiple selection mode. Resetting the file chooser "
5318 "to single selection mode.");
5319 set_select_multiple (impl, FALSE, TRUE);
5321 impl->action = action;
5322 update_cell_renderer_attributes (impl);
5323 update_appearance (impl);
5324 settings_load (impl);
5329 case GTK_FILE_CHOOSER_PROP_FILTER:
5330 set_current_filter (impl, g_value_get_object (value));
5333 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5334 set_local_only (impl, g_value_get_boolean (value));
5337 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5338 set_preview_widget (impl, g_value_get_object (value));
5341 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5342 impl->preview_widget_active = g_value_get_boolean (value);
5343 update_preview_widget_visibility (impl);
5346 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5347 impl->use_preview_label = g_value_get_boolean (value);
5348 update_preview_widget_visibility (impl);
5351 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5352 set_extra_widget (impl, g_value_get_object (value));
5355 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5357 gboolean select_multiple = g_value_get_boolean (value);
5358 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5359 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5362 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5363 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5364 "leaving the file chooser in single selection mode.");
5368 set_select_multiple (impl, select_multiple, FALSE);
5372 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5374 gboolean show_hidden = g_value_get_boolean (value);
5375 if (show_hidden != impl->show_hidden)
5377 impl->show_hidden = show_hidden;
5379 if (impl->browse_files_model)
5380 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5385 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5387 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5388 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5392 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5394 gboolean create_folders = g_value_get_boolean (value);
5395 impl->create_folders = create_folders;
5396 update_appearance (impl);
5401 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5407 gtk_file_chooser_default_get_property (GObject *object,
5412 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5416 case GTK_FILE_CHOOSER_PROP_ACTION:
5417 g_value_set_enum (value, impl->action);
5420 case GTK_FILE_CHOOSER_PROP_FILTER:
5421 g_value_set_object (value, impl->current_filter);
5424 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5425 g_value_set_boolean (value, impl->local_only);
5428 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5429 g_value_set_object (value, impl->preview_widget);
5432 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5433 g_value_set_boolean (value, impl->preview_widget_active);
5436 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5437 g_value_set_boolean (value, impl->use_preview_label);
5440 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5441 g_value_set_object (value, impl->extra_widget);
5444 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5445 g_value_set_boolean (value, impl->select_multiple);
5448 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5449 g_value_set_boolean (value, impl->show_hidden);
5452 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5453 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5456 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5457 g_value_set_boolean (value, impl->create_folders);
5461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5466 /* Removes the settings signal handler. It's safe to call multiple times */
5468 remove_settings_signal (GtkFileChooserDefault *impl,
5471 if (impl->settings_signal_id)
5473 GtkSettings *settings;
5475 settings = gtk_settings_get_for_screen (screen);
5476 g_signal_handler_disconnect (settings,
5477 impl->settings_signal_id);
5478 impl->settings_signal_id = 0;
5483 gtk_file_chooser_default_dispose (GObject *object)
5486 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5488 if (impl->extra_widget)
5490 g_object_unref (impl->extra_widget);
5491 impl->extra_widget = NULL;
5494 pending_select_files_free (impl);
5496 /* cancel all pending operations */
5497 if (impl->pending_cancellables)
5499 for (l = impl->pending_cancellables; l; l = l->next)
5501 GCancellable *cancellable = G_CANCELLABLE (l->data);
5502 g_cancellable_cancel (cancellable);
5504 g_slist_free (impl->pending_cancellables);
5505 impl->pending_cancellables = NULL;
5508 if (impl->reload_icon_cancellables)
5510 for (l = impl->reload_icon_cancellables; l; l = l->next)
5512 GCancellable *cancellable = G_CANCELLABLE (l->data);
5513 g_cancellable_cancel (cancellable);
5515 g_slist_free (impl->reload_icon_cancellables);
5516 impl->reload_icon_cancellables = NULL;
5519 if (impl->loading_shortcuts)
5521 for (l = impl->loading_shortcuts; l; l = l->next)
5523 GCancellable *cancellable = G_CANCELLABLE (l->data);
5524 g_cancellable_cancel (cancellable);
5526 g_slist_free (impl->loading_shortcuts);
5527 impl->loading_shortcuts = NULL;
5530 if (impl->file_list_drag_data_received_cancellable)
5532 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5533 impl->file_list_drag_data_received_cancellable = NULL;
5536 if (impl->update_current_folder_cancellable)
5538 g_cancellable_cancel (impl->update_current_folder_cancellable);
5539 impl->update_current_folder_cancellable = NULL;
5542 if (impl->should_respond_get_info_cancellable)
5544 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5545 impl->should_respond_get_info_cancellable = NULL;
5548 if (impl->update_from_entry_cancellable)
5550 g_cancellable_cancel (impl->update_from_entry_cancellable);
5551 impl->update_from_entry_cancellable = NULL;
5554 if (impl->shortcuts_activate_iter_cancellable)
5556 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5557 impl->shortcuts_activate_iter_cancellable = NULL;
5560 search_stop_searching (impl, TRUE);
5561 recent_stop_loading (impl);
5563 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5565 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5568 /* We override show-all since we have internal widgets that
5569 * shouldn't be shown when you call show_all(), like the filter
5573 gtk_file_chooser_default_show_all (GtkWidget *widget)
5575 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5577 gtk_widget_show (widget);
5579 if (impl->extra_widget)
5580 gtk_widget_show_all (impl->extra_widget);
5583 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5584 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5587 toplevel_set_focus_cb (GtkWindow *window,
5589 GtkFileChooserDefault *impl)
5591 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5594 /* We monitor the focus widget on our toplevel to be able to know which widget
5595 * was last focused at the time our "should_respond" method gets called.
5598 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5599 GtkWidget *previous_toplevel)
5601 GtkFileChooserDefault *impl;
5602 GtkWidget *toplevel;
5604 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5605 toplevel = gtk_widget_get_toplevel (widget);
5607 if (previous_toplevel &&
5608 impl->toplevel_set_focus_id != 0)
5610 g_signal_handler_disconnect (previous_toplevel,
5611 impl->toplevel_set_focus_id);
5612 impl->toplevel_set_focus_id = 0;
5613 impl->toplevel_last_focus_widget = NULL;
5616 if (gtk_widget_is_toplevel (toplevel))
5618 g_assert (impl->toplevel_set_focus_id == 0);
5619 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5620 G_CALLBACK (toplevel_set_focus_cb), impl);
5621 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5625 /* Changes the icons wherever it is needed */
5627 change_icon_theme (GtkFileChooserDefault *impl)
5629 GtkSettings *settings;
5631 GtkCellRenderer *renderer;
5634 profile_start ("start", NULL);
5636 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5638 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5639 impl->icon_size = MAX (width, height);
5641 impl->icon_size = FALLBACK_ICON_SIZE;
5643 shortcuts_reload_icons (impl);
5644 /* the first cell in the first column is the icon column, and we have a fixed size there */
5645 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5646 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5647 renderer = GTK_CELL_RENDERER (cells->data);
5648 set_icon_cell_renderer_fixed_size (impl, renderer);
5649 g_list_free (cells);
5650 if (impl->browse_files_model)
5651 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5652 gtk_widget_queue_resize (impl->browse_files_tree_view);
5654 profile_end ("end", NULL);
5657 /* Callback used when a GtkSettings value changes */
5659 settings_notify_cb (GObject *object,
5661 GtkFileChooserDefault *impl)
5665 profile_start ("start", NULL);
5667 name = g_param_spec_get_name (pspec);
5669 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5670 strcmp (name, "gtk-icon-sizes") == 0)
5671 change_icon_theme (impl);
5673 profile_end ("end", NULL);
5676 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5680 check_icon_theme (GtkFileChooserDefault *impl)
5682 GtkSettings *settings;
5684 profile_start ("start", NULL);
5686 if (impl->settings_signal_id)
5688 profile_end ("end", NULL);
5692 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5694 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5695 impl->settings_signal_id = g_signal_connect (settings, "notify",
5696 G_CALLBACK (settings_notify_cb), impl);
5698 change_icon_theme (impl);
5701 profile_end ("end", NULL);
5705 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5707 GtkFileChooserDefault *impl;
5709 profile_start ("start", NULL);
5711 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5713 profile_msg (" parent class style_udpated start", NULL);
5714 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5715 profile_msg (" parent class style_updated end", NULL);
5717 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5718 change_icon_theme (impl);
5720 emit_default_size_changed (impl);
5722 profile_end ("end", NULL);
5726 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5727 GdkScreen *previous_screen)
5729 GtkFileChooserDefault *impl;
5731 profile_start ("start", NULL);
5733 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5735 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5736 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5738 remove_settings_signal (impl, previous_screen);
5739 check_icon_theme (impl);
5741 emit_default_size_changed (impl);
5743 profile_end ("end", NULL);
5747 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5748 GtkAllocation *allocation)
5750 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5754 set_sort_column (GtkFileChooserDefault *impl)
5756 GtkTreeSortable *sortable;
5758 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5760 /* can happen when we're still populating the model */
5761 if (sortable == NULL)
5764 gtk_tree_sortable_set_sort_column_id (sortable,
5770 settings_ensure (GtkFileChooserDefault *impl)
5772 if (impl->settings != NULL)
5775 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5776 "/org/gtk/settings/file-chooser/");
5777 g_settings_delay (impl->settings);
5781 settings_load (GtkFileChooserDefault *impl)
5783 LocationMode location_mode;
5784 gboolean show_hidden;
5785 gboolean show_size_column;
5787 GtkSortType sort_order;
5789 settings_ensure (impl);
5791 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5792 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5793 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5794 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5795 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5797 location_mode_set (impl, location_mode, TRUE);
5799 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5801 impl->show_size_column = show_size_column;
5802 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5804 impl->sort_column = sort_column;
5805 impl->sort_order = sort_order;
5806 /* We don't call set_sort_column() here as the models may not have been
5807 * created yet. The individual functions that create and set the models will
5808 * call set_sort_column() themselves.
5813 save_dialog_geometry (GtkFileChooserDefault *impl)
5815 GtkWindow *toplevel;
5816 int x, y, width, height;
5818 toplevel = get_toplevel (GTK_WIDGET (impl));
5820 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5823 gtk_window_get_position (toplevel, &x, &y);
5824 gtk_window_get_size (toplevel, &width, &height);
5826 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5827 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5831 settings_save (GtkFileChooserDefault *impl)
5833 char *current_folder_uri;
5835 settings_ensure (impl);
5837 /* Current folder */
5839 if (impl->current_folder)
5840 current_folder_uri = g_file_get_uri (impl->current_folder);
5842 current_folder_uri = "";
5844 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5846 if (impl->current_folder)
5847 g_free (current_folder_uri);
5849 /* All the other state */
5851 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5852 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5853 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5854 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5855 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5856 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5858 save_dialog_geometry (impl);
5860 /* Now apply the settings */
5861 g_settings_apply (impl->settings);
5863 g_object_unref (impl->settings);
5864 impl->settings = NULL;
5867 /* GtkWidget::realize method */
5869 gtk_file_chooser_default_realize (GtkWidget *widget)
5871 GtkFileChooserDefault *impl;
5873 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5875 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5877 emit_default_size_changed (impl);
5881 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5883 char *last_folder_uri;
5886 settings_ensure (impl);
5888 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5890 /* If no last folder is set, we use the user's home directory, since
5891 * this is the starting point for most documents.
5893 if (last_folder_uri[0] == '\0')
5894 file = g_file_new_for_path (g_get_home_dir ());
5896 file = g_file_new_for_uri (last_folder_uri);
5898 g_free (last_folder_uri);
5903 /* GtkWidget::map method */
5905 gtk_file_chooser_default_map (GtkWidget *widget)
5907 GtkFileChooserDefault *impl;
5909 profile_start ("start", NULL);
5911 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5913 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5915 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5919 switch (impl->reload_state)
5922 /* The user didn't explicitly give us a folder to display, so we'll
5923 * use the saved one from the last invocation of the file chooser
5925 folder = get_file_for_last_folder_opened (impl);
5926 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), folder, NULL);
5927 g_object_unref (folder);
5930 case RELOAD_HAS_FOLDER:
5931 /* Nothing; we are already loading or loaded, so we
5932 * don't need to reload
5937 g_assert_not_reached ();
5941 volumes_bookmarks_changed_cb (impl->file_system, impl);
5943 settings_load (impl);
5945 profile_end ("end", NULL);
5948 /* GtkWidget::unmap method */
5950 gtk_file_chooser_default_unmap (GtkWidget *widget)
5952 GtkFileChooserDefault *impl;
5954 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5956 settings_save (impl);
5958 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5962 install_list_model_filter (GtkFileChooserDefault *impl)
5964 _gtk_file_system_model_set_filter (impl->browse_files_model,
5965 impl->current_filter);
5968 #define COMPARE_DIRECTORIES \
5969 GtkFileChooserDefault *impl = user_data; \
5970 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
5971 gboolean dir_a, dir_b; \
5973 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
5974 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
5976 if (dir_a != dir_b) \
5977 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5979 /* Sort callback for the filename column */
5981 name_sort_func (GtkTreeModel *model,
5986 COMPARE_DIRECTORIES;
5989 const char *key_a, *key_b;
5992 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
5993 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
5996 result = strcmp (key_a, key_b);
6008 /* Sort callback for the size column */
6010 size_sort_func (GtkTreeModel *model,
6015 COMPARE_DIRECTORIES;
6018 gint64 size_a, size_b;
6020 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6021 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6023 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6027 /* Sort callback for the mtime column */
6029 mtime_sort_func (GtkTreeModel *model,
6034 COMPARE_DIRECTORIES;
6039 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6040 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6042 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6046 /* Callback used when the sort column changes. We cache the sort order for use
6047 * in name_sort_func().
6050 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6051 GtkFileChooserDefault *impl)
6053 gint sort_column_id;
6054 GtkSortType sort_type;
6056 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6058 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6059 impl->sort_column = sort_column_id;
6060 impl->sort_order = sort_type;
6065 set_busy_cursor (GtkFileChooserDefault *impl,
6069 GtkWindow *toplevel;
6070 GdkDisplay *display;
6073 toplevel = get_toplevel (GTK_WIDGET (impl));
6074 widget = GTK_WIDGET (toplevel);
6075 if (!toplevel || !gtk_widget_get_realized (widget))
6078 display = gtk_widget_get_display (widget);
6081 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6085 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6086 gdk_display_flush (display);
6089 g_object_unref (cursor);
6092 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6094 load_set_model (GtkFileChooserDefault *impl)
6096 profile_start ("start", NULL);
6098 g_assert (impl->browse_files_model != NULL);
6100 profile_msg (" gtk_tree_view_set_model start", NULL);
6101 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6102 GTK_TREE_MODEL (impl->browse_files_model));
6103 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6104 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6106 set_sort_column (impl);
6107 profile_msg (" gtk_tree_view_set_model end", NULL);
6108 impl->list_sort_ascending = TRUE;
6110 profile_end ("end", NULL);
6113 /* Timeout callback used when the loading timer expires */
6115 load_timeout_cb (gpointer data)
6117 GtkFileChooserDefault *impl;
6119 profile_start ("start", NULL);
6121 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6122 g_assert (impl->load_state == LOAD_PRELOAD);
6123 g_assert (impl->load_timeout_id != 0);
6124 g_assert (impl->browse_files_model != NULL);
6126 impl->load_timeout_id = 0;
6127 impl->load_state = LOAD_LOADING;
6129 load_set_model (impl);
6131 profile_end ("end", NULL);
6136 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6138 load_setup_timer (GtkFileChooserDefault *impl)
6140 g_assert (impl->load_timeout_id == 0);
6141 g_assert (impl->load_state != LOAD_PRELOAD);
6143 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6144 impl->load_state = LOAD_PRELOAD;
6147 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6149 load_remove_timer (GtkFileChooserDefault *impl)
6151 if (impl->load_timeout_id != 0)
6153 g_assert (impl->load_state == LOAD_PRELOAD);
6155 g_source_remove (impl->load_timeout_id);
6156 impl->load_timeout_id = 0;
6157 impl->load_state = LOAD_EMPTY;
6160 g_assert (impl->load_state == LOAD_EMPTY ||
6161 impl->load_state == LOAD_LOADING ||
6162 impl->load_state == LOAD_FINISHED);
6165 /* Selects the first row in the file list */
6167 browse_files_select_first_row (GtkFileChooserDefault *impl)
6170 GtkTreeIter dummy_iter;
6171 GtkTreeModel *tree_model;
6173 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6178 path = gtk_tree_path_new_from_indices (0, -1);
6180 /* If the list is empty, do nothing. */
6181 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6182 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6184 gtk_tree_path_free (path);
6187 struct center_selected_row_closure {
6188 GtkFileChooserDefault *impl;
6189 gboolean already_centered;
6192 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6193 * selected row in the tree view.
6196 center_selected_row_foreach_cb (GtkTreeModel *model,
6201 struct center_selected_row_closure *closure;
6204 if (closure->already_centered)
6207 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6208 closure->already_centered = TRUE;
6211 /* Centers the selected row in the tree view */
6213 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6215 struct center_selected_row_closure closure;
6216 GtkTreeSelection *selection;
6218 closure.impl = impl;
6219 closure.already_centered = FALSE;
6221 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6222 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6226 show_and_select_files (GtkFileChooserDefault *impl,
6229 GtkTreeSelection *selection;
6230 GtkFileSystemModel *fsmodel;
6231 gboolean enabled_hidden, removed_filters;
6232 gboolean 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)));
6238 enabled_hidden = impl->show_hidden;
6239 removed_filters = (impl->current_filter == NULL);
6241 selected_a_file = FALSE;
6243 for (walk = files; walk; walk = walk->next)
6245 GFile *file = walk->data;
6248 /* Is it a hidden file? */
6250 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6253 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6255 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6257 if (!enabled_hidden &&
6258 (g_file_info_get_is_hidden (info) ||
6259 g_file_info_get_is_backup (info)))
6261 g_object_set (impl, "show-hidden", TRUE, NULL);
6262 enabled_hidden = TRUE;
6266 /* Is it a filtered file? */
6268 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6269 continue; /* re-get the iter as it may change when the model refilters */
6271 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6273 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6274 if (!removed_filters)
6276 set_current_filter (impl, NULL);
6277 removed_filters = TRUE;
6281 /* Okay, can we select the file now? */
6283 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6286 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6290 gtk_tree_selection_select_iter (selection, &iter);
6292 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6293 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6295 gtk_tree_path_free (path);
6297 selected_a_file = TRUE;
6301 browse_files_center_selected_row (impl);
6303 return selected_a_file;
6306 /* Processes the pending operation when a folder is finished loading */
6308 pending_select_files_process (GtkFileChooserDefault *impl)
6310 g_assert (impl->load_state == LOAD_FINISHED);
6311 g_assert (impl->browse_files_model != NULL);
6313 if (impl->pending_select_files)
6315 show_and_select_files (impl, impl->pending_select_files);
6316 pending_select_files_free (impl);
6317 browse_files_center_selected_row (impl);
6321 /* We only select the first row if the chooser is actually mapped ---
6322 * selecting the first row is to help the user when he is interacting with
6323 * the chooser, but sometimes a chooser works not on behalf of the user,
6324 * but rather on behalf of something else like GtkFileChooserButton. In
6325 * that case, the chooser's selection should be what the caller expects,
6326 * as the user can't see that something else got selected. See bug #165264.
6328 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6329 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6330 browse_files_select_first_row (impl);
6333 g_assert (impl->pending_select_files == NULL);
6337 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6342 info = g_file_query_info (impl->current_folder,
6343 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6344 G_FILE_QUERY_INFO_NONE,
6349 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6350 g_object_unref (info);
6353 msg = g_strdup (_("Could not read the contents of the folder"));
6355 error_message (impl, msg, error->message);
6359 /* Callback used when the file system model finishes loading */
6361 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6363 GtkFileChooserDefault *impl)
6365 profile_start ("start", NULL);
6368 show_error_on_reading_current_folder (impl, error);
6370 if (impl->load_state == LOAD_PRELOAD)
6372 load_remove_timer (impl);
6373 load_set_model (impl);
6375 else if (impl->load_state == LOAD_LOADING)
6381 /* We can't g_assert_not_reached(), as something other than us may have
6382 * initiated a folder reload. See #165556.
6384 profile_end ("end", NULL);
6388 g_assert (impl->load_timeout_id == 0);
6390 impl->load_state = LOAD_FINISHED;
6392 pending_select_files_process (impl);
6393 set_busy_cursor (impl, FALSE);
6394 #ifdef PROFILE_FILE_CHOOSER
6395 access ("MARK: *** FINISHED LOADING", F_OK);
6398 profile_end ("end", NULL);
6402 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6403 gboolean remove_from_treeview)
6405 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6407 if (impl->browse_files_model)
6409 g_object_unref (impl->browse_files_model);
6410 impl->browse_files_model = NULL;
6413 if (remove_from_treeview)
6414 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6418 my_g_format_time_for_display (glong secs)
6423 time_t time_mtime, time_now;
6424 const gchar *format;
6425 gchar *locale_format = NULL;
6427 char *date_str = NULL;
6429 const char *locale, *dot = NULL;
6430 gint64 codepage = -1;
6436 #ifdef HAVE_LOCALTIME_R
6437 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6440 struct tm *ptm = localtime ((time_t *) &time_mtime);
6444 g_warning ("ptm != NULL failed");
6446 return g_strdup (_("Unknown"));
6449 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6451 #endif /* HAVE_LOCALTIME_R */
6453 g_date_set_time_t (&mtime, time_mtime);
6454 time_now = time (NULL);
6455 g_date_set_time_t (&now, time_now);
6457 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6459 /* Translators: %H means "hours" and %M means "minutes" */
6461 format = _("%H:%M");
6462 else if (days_diff == 1)
6463 format = _("Yesterday at %H:%M");
6466 if (days_diff > 1 && days_diff < 7)
6467 format = "%A"; /* Days from last week */
6469 format = "%x"; /* Any other date */
6473 /* g_locale_from_utf8() returns a string in the system
6474 * code-page, which is not always the same as that used by the C
6475 * library. For instance when running a GTK+ program with
6476 * LANG=ko on an English version of Windows, the system
6477 * code-page is 1252, but the code-page used by the C library is
6478 * 949. (It's GTK+ itself that sets the C library locale when it
6479 * notices the LANG environment variable. See gtkmain.c The
6480 * Microsoft C library doesn't look at any locale environment
6481 * variables.) We need to pass strftime() a string in the C
6482 * library's code-page. See bug #509885.
6484 locale = setlocale (LC_ALL, NULL);
6486 dot = strchr (locale, '.');
6489 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6491 /* All codepages should fit in 16 bits AFAIK */
6492 if (codepage > 0 && codepage < 65536)
6494 sprintf (charset, "CP%u", (guint) codepage);
6495 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6499 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6501 if (locale_format != NULL &&
6502 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6505 /* As above but in opposite direction... */
6506 if (codepage > 0 && codepage < 65536)
6507 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6509 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6513 if (date_str == NULL)
6514 date_str = g_strdup (_("Unknown"));
6516 g_free (locale_format);
6521 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6523 GFileAttributeType type;
6526 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6527 g_file_info_set_attribute (to, attribute, type, value);
6531 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6533 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6534 GFile *file = G_FILE (object);
6535 GFileInfo *queried, *info;
6538 queried = g_file_query_info_finish (file, res, NULL);
6539 if (queried == NULL)
6542 GDK_THREADS_ENTER ();
6544 /* now we know model is valid */
6546 /* file was deleted */
6547 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6549 GDK_THREADS_LEAVE ();
6553 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6555 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6556 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6557 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6559 _gtk_file_system_model_update_file (model, file, info, FALSE);
6561 g_object_unref (info);
6563 GDK_THREADS_LEAVE ();
6567 file_system_model_set (GtkFileSystemModel *model,
6574 GtkFileChooserDefault *impl = data;
6578 case MODEL_COL_FILE:
6579 g_value_set_object (value, file);
6581 case MODEL_COL_NAME:
6583 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6585 g_value_set_string (value, g_file_info_get_display_name (info));
6587 case MODEL_COL_NAME_COLLATED:
6589 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6591 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6593 case MODEL_COL_IS_FOLDER:
6594 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6596 case MODEL_COL_PIXBUF:
6599 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6601 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6605 GtkTreeModel *tree_model;
6606 GtkTreePath *path, *start, *end;
6609 if (impl->browse_files_tree_view == NULL ||
6610 g_file_info_has_attribute (info, "filechooser::queried"))
6613 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6614 if (tree_model != GTK_TREE_MODEL (model))
6617 if (!_gtk_file_system_model_get_iter_for_file (model,
6620 g_assert_not_reached ();
6621 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6623 path = gtk_tree_model_get_path (tree_model, &iter);
6624 if (gtk_tree_path_compare (start, path) != 1 &&
6625 gtk_tree_path_compare (path, end) != 1)
6627 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6628 g_file_query_info_async (file,
6629 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6630 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6631 G_FILE_ATTRIBUTE_STANDARD_ICON,
6632 G_FILE_QUERY_INFO_NONE,
6634 _gtk_file_system_model_get_cancellable (model),
6635 file_system_model_got_thumbnail,
6638 gtk_tree_path_free (path);
6639 gtk_tree_path_free (start);
6640 gtk_tree_path_free (end);
6645 g_value_set_object (value, NULL);
6647 case MODEL_COL_SIZE:
6648 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6650 case MODEL_COL_SIZE_TEXT:
6651 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6652 g_value_set_string (value, NULL);
6654 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6656 case MODEL_COL_MTIME:
6657 case MODEL_COL_MTIME_TEXT:
6662 g_file_info_get_modification_time (info, &tv);
6663 if (column == MODEL_COL_MTIME)
6664 g_value_set_long (value, tv.tv_sec);
6665 else if (tv.tv_sec == 0)
6666 g_value_set_static_string (value, _("Unknown"));
6668 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6671 case MODEL_COL_ELLIPSIZE:
6672 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6675 g_assert_not_reached ();
6682 /* Gets rid of the old list model and creates a new one for the current folder */
6684 set_list_model (GtkFileChooserDefault *impl,
6687 g_assert (impl->current_folder != NULL);
6689 profile_start ("start", NULL);
6691 stop_loading_and_clear_list_model (impl, TRUE);
6693 set_busy_cursor (impl, TRUE);
6695 impl->browse_files_model =
6696 _gtk_file_system_model_new_for_directory (impl->current_folder,
6698 file_system_model_set,
6700 MODEL_COLUMN_TYPES);
6702 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6704 profile_msg (" set sort function", NULL);
6705 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6706 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6707 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6708 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6709 set_sort_column (impl);
6710 impl->list_sort_ascending = TRUE;
6711 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6712 G_CALLBACK (list_sort_column_changed_cb), impl);
6714 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6716 g_signal_connect (impl->browse_files_model, "finished-loading",
6717 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6719 install_list_model_filter (impl);
6721 profile_end ("end", NULL);
6726 struct update_chooser_entry_selected_foreach_closure {
6728 GtkTreeIter first_selected_iter;
6732 compare_utf8_filenames (const gchar *a,
6735 gchar *a_folded, *b_folded;
6738 a_folded = g_utf8_strdown (a, -1);
6739 b_folded = g_utf8_strdown (b, -1);
6741 retval = strcmp (a_folded, b_folded);
6750 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6755 struct update_chooser_entry_selected_foreach_closure *closure;
6758 closure->num_selected++;
6760 if (closure->num_selected == 1)
6761 closure->first_selected_iter = *iter;
6765 update_chooser_entry (GtkFileChooserDefault *impl)
6767 GtkTreeSelection *selection;
6768 struct update_chooser_entry_selected_foreach_closure closure;
6770 /* no need to update the file chooser's entry if there's no entry */
6771 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6772 impl->operation_mode == OPERATION_MODE_RECENT ||
6773 !impl->location_entry)
6776 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6777 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6778 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6779 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6780 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6783 g_assert (impl->location_entry != NULL);
6785 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6786 closure.num_selected = 0;
6787 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6789 if (closure.num_selected == 0)
6791 goto maybe_clear_entry;
6793 else if (closure.num_selected == 1)
6795 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6798 gboolean change_entry;
6800 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6802 /* If the cursor moved to the row of the newly created folder,
6803 * retrieving info will return NULL.
6808 g_free (impl->browse_files_last_selected_name);
6809 impl->browse_files_last_selected_name =
6810 g_strdup (g_file_info_get_display_name (info));
6812 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6813 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6814 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6816 /* Don't change the name when clicking on a folder... */
6817 change_entry = (! _gtk_file_info_consider_as_directory (info));
6820 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6824 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6826 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6827 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6835 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6836 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6838 /* Multiple selection, so just clear the entry. */
6839 g_free (impl->browse_files_last_selected_name);
6840 impl->browse_files_last_selected_name = NULL;
6842 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6848 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6849 && impl->browse_files_last_selected_name)
6851 const char *entry_text;
6853 gboolean clear_entry;
6855 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6856 len = strlen (entry_text);
6859 /* The file chooser entry may have appended a "/" to its text.
6860 * So take it out, and compare the result to the old selection.
6862 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6866 tmp = g_strndup (entry_text, len - 1);
6867 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6871 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6874 clear_entry = FALSE;
6877 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6882 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6886 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6890 struct UpdateCurrentFolderData
6892 GtkFileChooserDefault *impl;
6894 gboolean keep_trail;
6895 gboolean clear_entry;
6896 GFile *original_file;
6897 GError *original_error;
6901 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6902 GtkFileSystemVolume *volume,
6903 const GError *error,
6906 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6907 struct UpdateCurrentFolderData *data = user_data;
6908 GtkFileChooserDefault *impl = data->impl;
6910 if (cancellable != impl->update_current_folder_cancellable)
6913 impl->update_current_folder_cancellable = NULL;
6914 set_busy_cursor (impl, FALSE);
6921 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6922 impl->reload_state = RELOAD_EMPTY;
6926 change_folder_and_display_error (impl, data->file, data->clear_entry);
6929 g_object_unref (data->file);
6932 g_object_unref (cancellable);
6936 update_current_folder_get_info_cb (GCancellable *cancellable,
6938 const GError *error,
6941 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6942 struct UpdateCurrentFolderData *data = user_data;
6943 GtkFileChooserDefault *impl = data->impl;
6945 if (cancellable != impl->update_current_folder_cancellable)
6948 impl->update_current_folder_cancellable = NULL;
6949 impl->reload_state = RELOAD_EMPTY;
6951 set_busy_cursor (impl, FALSE);
6960 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6962 GMountOperation *mount_operation;
6963 GtkWidget *toplevel;
6965 g_object_unref (cancellable);
6966 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6968 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6970 set_busy_cursor (impl, TRUE);
6972 impl->update_current_folder_cancellable =
6973 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
6975 update_current_folder_mount_enclosing_volume_cb,
6981 if (!data->original_file)
6983 data->original_file = g_object_ref (data->file);
6984 data->original_error = g_error_copy (error);
6987 parent_file = g_file_get_parent (data->file);
6989 /* get parent path and try to change the folder to that */
6992 g_object_unref (data->file);
6993 data->file = parent_file;
6995 g_object_unref (cancellable);
6997 /* restart the update current folder operation */
6998 impl->reload_state = RELOAD_HAS_FOLDER;
7000 impl->update_current_folder_cancellable =
7001 _gtk_file_system_get_info (impl->file_system, data->file,
7003 update_current_folder_get_info_cb,
7006 set_busy_cursor (impl, TRUE);
7012 /* Error and bail out, ignoring "not found" errors since they're useless:
7013 * they only happen when a program defaults to a folder that has been (re)moved.
7015 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7016 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7018 g_error_free (data->original_error);
7020 g_object_unref (data->original_file);
7026 if (data->original_file)
7028 /* Error and bail out, ignoring "not found" errors since they're useless:
7029 * they only happen when a program defaults to a folder that has been (re)moved.
7031 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7032 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7034 g_error_free (data->original_error);
7036 g_object_unref (data->original_file);
7039 if (! _gtk_file_info_consider_as_directory (info))
7042 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7045 if (impl->current_folder != data->file)
7047 if (impl->current_folder)
7048 g_object_unref (impl->current_folder);
7050 impl->current_folder = g_object_ref (data->file);
7053 impl->reload_state = RELOAD_HAS_FOLDER;
7055 /* Update the widgets that may trigger a folder change themselves. */
7057 if (!impl->changing_folder)
7059 impl->changing_folder = TRUE;
7061 shortcuts_update_current_folder (impl);
7063 impl->changing_folder = FALSE;
7066 /* Set the folder on the save entry */
7068 if (impl->location_entry)
7070 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7071 impl->current_folder);
7073 if (data->clear_entry)
7074 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7077 /* Create a new list model. This is slightly evil; we store the result value
7078 * but perform more actions rather than returning immediately even if it
7079 * generates an error.
7081 set_list_model (impl, NULL);
7083 /* Refresh controls */
7085 shortcuts_find_current_folder (impl);
7087 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7089 check_preview_change (impl);
7090 bookmarks_check_add_sensitivity (impl);
7092 g_signal_emit_by_name (impl, "selection-changed", 0);
7095 g_object_unref (data->file);
7098 g_object_unref (cancellable);
7102 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7104 gboolean keep_trail,
7105 gboolean clear_entry,
7108 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7109 struct UpdateCurrentFolderData *data;
7111 profile_start ("start", NULL);
7113 g_object_ref (file);
7115 switch (impl->operation_mode)
7117 case OPERATION_MODE_SEARCH:
7118 search_switch_to_browse_mode (impl);
7120 case OPERATION_MODE_RECENT:
7121 recent_switch_to_browse_mode (impl);
7123 case OPERATION_MODE_BROWSE:
7127 if (impl->local_only && !g_file_is_native (file))
7129 g_set_error_literal (error,
7130 GTK_FILE_CHOOSER_ERROR,
7131 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7132 _("Cannot change to folder because it is not local"));
7134 g_object_unref (file);
7135 profile_end ("end - not local", NULL);
7139 if (impl->update_current_folder_cancellable)
7140 g_cancellable_cancel (impl->update_current_folder_cancellable);
7142 /* Test validity of path here. */
7143 data = g_new0 (struct UpdateCurrentFolderData, 1);
7145 data->file = g_object_ref (file);
7146 data->keep_trail = keep_trail;
7147 data->clear_entry = clear_entry;
7149 impl->reload_state = RELOAD_HAS_FOLDER;
7151 impl->update_current_folder_cancellable =
7152 _gtk_file_system_get_info (impl->file_system, file,
7154 update_current_folder_get_info_cb,
7157 set_busy_cursor (impl, TRUE);
7158 g_object_unref (file);
7160 profile_end ("end", NULL);
7165 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7167 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7169 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7170 impl->operation_mode == OPERATION_MODE_RECENT)
7173 if (impl->reload_state == RELOAD_EMPTY)
7175 /* We are unmapped, or we had an error while loading the last folder.
7176 * We'll return the folder used by the last invocation of the file chooser
7177 * since once we get (re)mapped, we'll load *that* folder anyway unless
7178 * the caller explicitly calls set_current_folder() on us.
7180 return get_file_for_last_folder_opened (impl);
7183 if (impl->current_folder)
7184 return g_object_ref (impl->current_folder);
7190 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7193 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7195 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7196 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7198 pending_select_files_free (impl);
7199 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7203 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7207 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7211 parent_file = g_file_get_parent (file);
7214 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7216 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7217 impl->operation_mode == OPERATION_MODE_RECENT ||
7218 impl->load_state == LOAD_EMPTY)
7224 g_assert (impl->current_folder != NULL);
7226 same_path = g_file_equal (parent_file, impl->current_folder);
7229 if (same_path && impl->load_state == LOAD_FINISHED)
7234 files.data = (gpointer) file;
7237 result = show_and_select_files (impl, &files);
7238 g_object_unref (parent_file);
7242 pending_select_files_add (impl, file);
7248 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7249 g_object_unref (parent_file);
7253 g_object_unref (parent_file);
7258 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7261 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7262 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7265 if (!impl->browse_files_model)
7268 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7273 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7278 maybe_select (GtkTreeModel *model,
7283 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7284 GtkTreeSelection *selection;
7287 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7289 gtk_tree_model_get (model, iter,
7290 MODEL_COL_IS_FOLDER, &is_folder,
7293 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7294 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7295 gtk_tree_selection_select_iter (selection, iter);
7297 gtk_tree_selection_unselect_iter (selection, iter);
7303 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7305 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7307 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7308 impl->operation_mode == OPERATION_MODE_RECENT)
7310 GtkTreeSelection *selection;
7312 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7313 gtk_tree_selection_select_all (selection);
7317 if (impl->select_multiple)
7318 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7319 maybe_select, impl);
7323 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7325 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7326 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7328 gtk_tree_selection_unselect_all (selection);
7329 pending_select_files_free (impl);
7332 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7334 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7336 * is_empty_ret - whether the file entry is totally empty
7338 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7339 * the path will be "$cwd/foobar")
7342 check_save_entry (GtkFileChooserDefault *impl,
7344 gboolean *is_well_formed_ret,
7345 gboolean *is_empty_ret,
7346 gboolean *is_file_part_empty_ret,
7347 gboolean *is_folder)
7349 GtkFileChooserEntry *chooser_entry;
7350 GFile *current_folder;
7351 const char *file_part;
7355 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7356 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7357 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7358 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7359 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7361 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7363 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7366 *is_well_formed_ret = TRUE;
7367 *is_empty_ret = TRUE;
7368 *is_file_part_empty_ret = TRUE;
7374 *is_empty_ret = FALSE;
7376 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7377 if (!current_folder)
7380 *is_well_formed_ret = FALSE;
7381 *is_file_part_empty_ret = FALSE;
7387 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7389 if (!file_part || file_part[0] == '\0')
7391 *file_ret = g_object_ref (current_folder);
7392 *is_well_formed_ret = TRUE;
7393 *is_file_part_empty_ret = TRUE;
7399 *is_file_part_empty_ret = FALSE;
7402 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7406 error_building_filename_dialog (impl, error);
7408 *is_well_formed_ret = FALSE;
7415 *is_well_formed_ret = TRUE;
7416 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7419 struct get_files_closure {
7420 GtkFileChooserDefault *impl;
7422 GFile *file_from_entry;
7426 get_files_foreach (GtkTreeModel *model,
7431 struct get_files_closure *info;
7433 GtkFileSystemModel *fs_model;
7436 fs_model = info->impl->browse_files_model;
7438 file = _gtk_file_system_model_get_file (fs_model, iter);
7440 return; /* We are on the editable row */
7442 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7443 info->result = g_slist_prepend (info->result, g_object_ref (file));
7447 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7449 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7450 struct get_files_closure info;
7451 GtkWindow *toplevel;
7452 GtkWidget *current_focus;
7453 gboolean file_list_seen;
7455 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7456 return search_get_selected_files (impl);
7458 if (impl->operation_mode == OPERATION_MODE_RECENT)
7459 return recent_get_selected_files (impl);
7463 info.file_from_entry = NULL;
7465 toplevel = get_toplevel (GTK_WIDGET (impl));
7467 current_focus = gtk_window_get_focus (toplevel);
7469 current_focus = NULL;
7471 file_list_seen = FALSE;
7472 if (current_focus == impl->browse_files_tree_view)
7474 GtkTreeSelection *selection;
7478 file_list_seen = TRUE;
7479 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7480 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7482 /* If there is no selection in the file list, we probably have this situation:
7484 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7485 * 2. He then double-clicked on a folder ("bar") in the file list
7487 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7488 * filename entry to see if that is the case.
7490 if (info.result == NULL && impl->location_entry)
7493 else if (impl->location_entry && current_focus == impl->location_entry)
7495 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7499 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7504 if (!is_well_formed)
7507 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7509 g_object_unref (info.file_from_entry);
7513 if (info.file_from_entry)
7514 info.result = g_slist_prepend (info.result, info.file_from_entry);
7515 else if (!file_list_seen)
7520 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7522 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7526 /* The focus is on a dialog's action area button or something else */
7527 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7528 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7536 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7537 * fall back to the current directory */
7538 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7539 info.result == NULL)
7541 GFile *current_folder;
7543 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7546 info.result = g_slist_prepend (info.result, current_folder);
7549 return g_slist_reverse (info.result);
7553 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7555 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7557 if (impl->preview_file)
7558 return g_object_ref (impl->preview_file);
7563 static GtkFileSystem *
7564 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7566 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7568 return impl->file_system;
7571 /* Shows or hides the filter widgets */
7573 show_filters (GtkFileChooserDefault *impl,
7577 gtk_widget_show (impl->filter_combo_hbox);
7579 gtk_widget_hide (impl->filter_combo_hbox);
7583 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7584 GtkFileFilter *filter)
7586 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7589 if (g_slist_find (impl->filters, filter))
7591 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7595 g_object_ref_sink (filter);
7596 impl->filters = g_slist_append (impl->filters, filter);
7598 name = gtk_file_filter_get_name (filter);
7600 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7602 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7604 if (!g_slist_find (impl->filters, impl->current_filter))
7605 set_current_filter (impl, filter);
7607 show_filters (impl, TRUE);
7611 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7612 GtkFileFilter *filter)
7614 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7615 GtkTreeModel *model;
7619 filter_index = g_slist_index (impl->filters, filter);
7621 if (filter_index < 0)
7623 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7627 impl->filters = g_slist_remove (impl->filters, filter);
7629 if (filter == impl->current_filter)
7632 set_current_filter (impl, impl->filters->data);
7634 set_current_filter (impl, NULL);
7637 /* Remove row from the combo box */
7638 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7639 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7640 g_assert_not_reached ();
7642 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7644 g_object_unref (filter);
7647 show_filters (impl, FALSE);
7651 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7653 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7655 return g_slist_copy (impl->filters);
7658 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7660 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7663 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7666 struct AddShortcutData
7668 GtkFileChooserDefault *impl;
7673 add_shortcut_get_info_cb (GCancellable *cancellable,
7675 const GError *error,
7679 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7680 struct AddShortcutData *data = user_data;
7682 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7685 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7687 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7690 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7692 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7695 g_object_unref (data->impl);
7696 g_object_unref (data->file);
7699 g_object_unref (cancellable);
7703 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7707 GCancellable *cancellable;
7708 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7709 struct AddShortcutData *data;
7713 /* Avoid adding duplicates */
7714 pos = shortcut_find_position (impl, file);
7715 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7719 uri = g_file_get_uri (file);
7720 /* translators, "Shortcut" means "Bookmark" here */
7722 GTK_FILE_CHOOSER_ERROR,
7723 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7724 _("Shortcut %s already exists"),
7731 for (l = impl->loading_shortcuts; l; l = l->next)
7733 GCancellable *c = l->data;
7736 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7737 if (f && g_file_equal (file, f))
7741 uri = g_file_get_uri (file);
7743 GTK_FILE_CHOOSER_ERROR,
7744 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7745 _("Shortcut %s already exists"),
7753 data = g_new0 (struct AddShortcutData, 1);
7754 data->impl = g_object_ref (impl);
7755 data->file = g_object_ref (file);
7757 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7759 add_shortcut_get_info_cb, data);
7764 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7765 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7771 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7775 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7782 for (l = impl->loading_shortcuts; l; l = l->next)
7784 GCancellable *c = l->data;
7787 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7788 if (f && g_file_equal (file, f))
7790 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7791 g_cancellable_cancel (c);
7796 if (impl->num_shortcuts == 0)
7799 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7800 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7801 g_assert_not_reached ();
7803 for (i = 0; i < impl->num_shortcuts; i++)
7806 ShortcutType shortcut_type;
7809 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7810 SHORTCUTS_COL_DATA, &col_data,
7811 SHORTCUTS_COL_TYPE, &shortcut_type,
7813 g_assert (col_data != NULL);
7814 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7816 shortcut = col_data;
7817 if (g_file_equal (shortcut, file))
7819 shortcuts_remove_rows (impl, pos + i, 1);
7820 impl->num_shortcuts--;
7824 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7825 g_assert_not_reached ();
7830 uri = g_file_get_uri (file);
7831 /* translators, "Shortcut" means "Bookmark" here */
7833 GTK_FILE_CHOOSER_ERROR,
7834 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7835 _("Shortcut %s does not exist"),
7843 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7845 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7851 if (impl->num_shortcuts == 0)
7854 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7855 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7856 g_assert_not_reached ();
7860 for (i = 0; i < impl->num_shortcuts; i++)
7863 ShortcutType shortcut_type;
7866 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7867 SHORTCUTS_COL_DATA, &col_data,
7868 SHORTCUTS_COL_TYPE, &shortcut_type,
7870 g_assert (col_data != NULL);
7871 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7873 shortcut = col_data;
7874 list = g_slist_prepend (list, g_object_ref (shortcut));
7876 if (i != impl->num_shortcuts - 1)
7878 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7879 g_assert_not_reached ();
7883 return g_slist_reverse (list);
7886 /* Guesses a size based upon font sizes */
7888 find_good_size_from_style (GtkWidget *widget,
7892 GtkStyleContext *context;
7893 GtkStateFlags state;
7898 context = gtk_widget_get_style_context (widget);
7899 state = gtk_widget_get_state_flags (widget);
7901 screen = gtk_widget_get_screen (widget);
7904 resolution = gdk_screen_get_resolution (screen);
7905 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7909 resolution = 96.0; /* wheeee */
7911 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7912 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7914 *width = font_size * NUM_CHARS;
7915 *height = font_size * NUM_LINES;
7919 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7920 gint *default_width,
7921 gint *default_height)
7923 GtkFileChooserDefault *impl;
7925 int x, y, width, height;
7927 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7929 settings_ensure (impl);
7931 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7932 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7934 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7936 *default_width = width;
7937 *default_height = height;
7941 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7943 if (impl->preview_widget_active &&
7944 impl->preview_widget &&
7945 gtk_widget_get_visible (impl->preview_widget))
7947 gtk_widget_get_preferred_size (impl->preview_box,
7949 *default_width += PREVIEW_HBOX_SPACING + req.width;
7952 if (impl->extra_widget &&
7953 gtk_widget_get_visible (impl->extra_widget))
7955 gtk_widget_get_preferred_size (impl->extra_align,
7957 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
7961 struct switch_folder_closure {
7962 GtkFileChooserDefault *impl;
7967 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7969 switch_folder_foreach_cb (GtkTreeModel *model,
7974 struct switch_folder_closure *closure;
7978 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
7979 closure->num_selected++;
7982 /* Changes to the selected folder in the list view */
7984 switch_to_selected_folder (GtkFileChooserDefault *impl)
7986 GtkTreeSelection *selection;
7987 struct switch_folder_closure closure;
7989 /* We do this with foreach() rather than get_selected() as we may be in
7990 * multiple selection mode
7993 closure.impl = impl;
7994 closure.file = NULL;
7995 closure.num_selected = 0;
7997 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7998 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8000 g_assert (closure.file && closure.num_selected == 1);
8002 change_folder_and_display_error (impl, closure.file, FALSE);
8005 /* Gets the GFileInfo for the selected row in the file list; assumes single
8009 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8010 gboolean *had_selection)
8012 GtkTreeSelection *selection;
8016 g_assert (!impl->select_multiple);
8017 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8018 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8020 *had_selection = FALSE;
8024 *had_selection = TRUE;
8026 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8030 /* Gets the display name of the selected file in the file list; assumes single
8031 * selection mode and that something is selected.
8033 static const gchar *
8034 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8037 gboolean had_selection;
8039 info = get_selected_file_info_from_file_list (impl, &had_selection);
8040 g_assert (had_selection);
8041 g_assert (info != NULL);
8043 return g_file_info_get_display_name (info);
8047 add_custom_button_to_dialog (GtkDialog *dialog,
8048 const gchar *mnemonic_label,
8049 const gchar *stock_id,
8054 button = gtk_button_new_with_mnemonic (mnemonic_label);
8055 gtk_widget_set_can_default (button, TRUE);
8056 gtk_button_set_image (GTK_BUTTON (button),
8057 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8058 gtk_widget_show (button);
8060 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8063 /* Presents an overwrite confirmation dialog; returns whether we should accept
8067 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8068 const gchar *file_part,
8069 const gchar *folder_display_name)
8071 GtkWindow *toplevel;
8075 toplevel = get_toplevel (GTK_WIDGET (impl));
8077 dialog = gtk_message_dialog_new (toplevel,
8078 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8079 GTK_MESSAGE_QUESTION,
8081 _("A file named \"%s\" already exists. Do you want to replace it?"),
8083 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8084 _("The file already exists in \"%s\". Replacing it will "
8085 "overwrite its contents."),
8086 folder_display_name);
8088 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8089 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8090 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8091 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8092 GTK_RESPONSE_ACCEPT,
8093 GTK_RESPONSE_CANCEL,
8095 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8097 if (gtk_window_has_group (toplevel))
8098 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8099 GTK_WINDOW (dialog));
8101 response = gtk_dialog_run (GTK_DIALOG (dialog));
8103 gtk_widget_destroy (dialog);
8105 return (response == GTK_RESPONSE_ACCEPT);
8108 struct GetDisplayNameData
8110 GtkFileChooserDefault *impl;
8115 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8117 const GError *error,
8120 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8121 gboolean should_respond = FALSE;
8122 struct GetDisplayNameData *data = user_data;
8124 if (cancellable != data->impl->should_respond_get_info_cancellable)
8127 data->impl->should_respond_get_info_cancellable = NULL;
8133 /* Huh? Did the folder disappear? Let the caller deal with it */
8134 should_respond = TRUE;
8136 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8138 set_busy_cursor (data->impl, FALSE);
8140 g_signal_emit_by_name (data->impl, "response-requested");
8143 g_object_unref (data->impl);
8144 g_free (data->file_part);
8147 g_object_unref (cancellable);
8150 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8151 * should respond. Can get the file part from the file list or the save entry.
8154 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8155 const gchar *file_part,
8158 GtkFileChooserConfirmation conf;
8160 if (!impl->do_overwrite_confirmation)
8163 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8165 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8169 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8171 struct GetDisplayNameData *data;
8173 g_assert (file_part != NULL);
8175 data = g_new0 (struct GetDisplayNameData, 1);
8176 data->impl = g_object_ref (impl);
8177 data->file_part = g_strdup (file_part);
8179 if (impl->should_respond_get_info_cancellable)
8180 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8182 impl->should_respond_get_info_cancellable =
8183 _gtk_file_system_get_info (impl->file_system, parent_file,
8184 "standard::display-name",
8185 confirmation_confirm_get_info_cb,
8187 set_busy_cursor (data->impl, TRUE);
8191 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8194 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8198 g_assert_not_reached ();
8203 struct FileExistsData
8205 GtkFileChooserDefault *impl;
8206 gboolean file_exists_and_is_not_folder;
8212 name_entry_get_parent_info_cb (GCancellable *cancellable,
8214 const GError *error,
8217 gboolean parent_is_folder;
8218 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8219 struct FileExistsData *data = user_data;
8221 if (cancellable != data->impl->should_respond_get_info_cancellable)
8224 data->impl->should_respond_get_info_cancellable = NULL;
8226 set_busy_cursor (data->impl, FALSE);
8232 parent_is_folder = FALSE;
8234 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8236 if (parent_is_folder)
8238 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8240 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) */
8242 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8244 if (data->file_exists_and_is_not_folder)
8249 /* Dup the string because the string may be modified
8250 * depending on what clients do in the confirm-overwrite
8251 * signal and this corrupts the pointer
8253 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8254 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8258 g_signal_emit_by_name (data->impl, "response-requested");
8261 g_signal_emit_by_name (data->impl, "response-requested");
8263 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8264 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8266 GError *mkdir_error = NULL;
8268 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8269 * "/blah/nonexistent" you *will* want a folder created.
8272 set_busy_cursor (data->impl, TRUE);
8273 g_file_make_directory (data->file, NULL, &mkdir_error);
8274 set_busy_cursor (data->impl, FALSE);
8277 g_signal_emit_by_name (data->impl, "response-requested");
8279 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8282 g_assert_not_reached ();
8288 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8289 error_with_file_under_nonfolder (data->impl, data->parent_file);
8295 /* The parent folder is not readable for some reason */
8297 error_copy = g_error_copy (error);
8298 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8303 g_object_unref (data->impl);
8304 g_object_unref (data->file);
8305 g_object_unref (data->parent_file);
8308 g_object_unref (cancellable);
8312 file_exists_get_info_cb (GCancellable *cancellable,
8314 const GError *error,
8317 gboolean data_ownership_taken = FALSE;
8318 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8319 gboolean file_exists;
8321 gboolean needs_parent_check = FALSE;
8322 struct FileExistsData *data = user_data;
8324 if (cancellable != data->impl->file_exists_get_info_cancellable)
8327 data->impl->file_exists_get_info_cancellable = NULL;
8329 set_busy_cursor (data->impl, FALSE);
8334 file_exists = (info != NULL);
8335 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8337 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8340 change_folder_and_display_error (data->impl, data->file, TRUE);
8344 g_signal_emit_by_name (data->impl, "response-requested"); /* user typed an existing filename; we are done */
8346 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8349 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8351 if (file_exists && !is_folder)
8353 /* Oops, the user typed the name of an existing path which is not
8356 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8357 g_error_copy (error));
8361 needs_parent_check = TRUE;
8364 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8368 needs_parent_check = TRUE;
8374 /* User typed a folder; we are done */
8375 g_signal_emit_by_name (data->impl, "response-requested");
8378 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8381 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8384 change_folder_and_display_error (data->impl, data->file, TRUE);
8386 needs_parent_check = TRUE;
8390 g_assert_not_reached();
8393 if (needs_parent_check) {
8394 /* check that everything up to the last path component exists (i.e. the parent) */
8396 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8397 data_ownership_taken = TRUE;
8399 if (data->impl->should_respond_get_info_cancellable)
8400 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8402 data->impl->should_respond_get_info_cancellable =
8403 _gtk_file_system_get_info (data->impl->file_system,
8406 name_entry_get_parent_info_cb,
8408 set_busy_cursor (data->impl, TRUE);
8412 if (!data_ownership_taken)
8414 g_object_unref (data->impl);
8415 g_object_unref (data->file);
8416 g_object_unref (data->parent_file);
8420 g_object_unref (cancellable);
8424 paste_text_received (GtkClipboard *clipboard,
8426 GtkFileChooserDefault *impl)
8433 file = g_file_new_for_uri (text);
8435 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8436 location_popup_handler (impl, text);
8438 g_object_unref (file);
8441 /* Handler for the "location-popup-on-paste" keybinding signal */
8443 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8445 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8446 GDK_SELECTION_CLIPBOARD);
8447 gtk_clipboard_request_text (clipboard,
8448 (GtkClipboardTextReceivedFunc) paste_text_received,
8453 /* Implementation for GtkFileChooserEmbed::should_respond() */
8455 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8457 GtkFileChooserDefault *impl;
8458 GtkWidget *toplevel;
8459 GtkWidget *current_focus;
8461 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8463 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8464 g_assert (GTK_IS_WINDOW (toplevel));
8466 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8468 if (current_focus == impl->browse_files_tree_view)
8470 /* The following array encodes what we do based on the impl->action and the
8471 * number of files selected.
8474 NOOP, /* Do nothing (don't respond) */
8475 RESPOND, /* Respond immediately */
8476 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8477 ALL_FILES, /* Respond only if everything selected is a file */
8478 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8479 SAVE_ENTRY, /* Go to the code for handling the save entry */
8480 NOT_REACHED /* Sanity check */
8482 static const ActionToTake what_to_do[4][3] = {
8483 /* 0 selected 1 selected many selected */
8484 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8485 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8486 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8487 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8491 gboolean all_files, all_folders;
8493 ActionToTake action;
8497 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8499 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8500 return search_should_respond (impl);
8502 if (impl->operation_mode == OPERATION_MODE_RECENT)
8503 return recent_should_respond (impl);
8505 selection_check (impl, &num_selected, &all_files, &all_folders);
8507 if (num_selected > 2)
8512 action = what_to_do [impl->action] [k];
8522 case RESPOND_OR_SWITCH:
8523 g_assert (num_selected == 1);
8527 switch_to_selected_folder (impl);
8530 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8531 return should_respond_after_confirm_overwrite (impl,
8532 get_display_name_from_file_list (impl),
8533 impl->current_folder);
8547 g_assert_not_reached ();
8550 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8553 gboolean is_well_formed, is_empty, is_file_part_empty;
8556 GtkFileChooserEntry *entry;
8561 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8562 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8563 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8564 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8565 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8567 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8568 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8570 if (!is_well_formed)
8575 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8576 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8582 g_assert (file != NULL);
8587 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8588 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8590 change_folder_and_display_error (impl, file, TRUE);
8593 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8594 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8596 /* The folder already exists, so we do not need to create it.
8597 * Just respond to terminate the dialog.
8603 g_assert_not_reached ();
8609 struct FileExistsData *data;
8611 /* We need to check whether file exists and whether it is a folder -
8612 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8613 * negative (it doesn't know yet if your last path component is a
8617 data = g_new0 (struct FileExistsData, 1);
8618 data->impl = g_object_ref (impl);
8619 data->file = g_object_ref (file);
8620 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8622 if (impl->file_exists_get_info_cancellable)
8623 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8625 impl->file_exists_get_info_cancellable =
8626 _gtk_file_system_get_info (impl->file_system, file,
8628 file_exists_get_info_cb,
8631 set_busy_cursor (impl, TRUE);
8635 g_error_free (error);
8638 g_object_unref (file);
8641 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8643 /* The focus is on a dialog's action area button, *and* the widget that
8644 * was focused immediately before it is the file list.
8648 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8650 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8653 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8655 /* The focus is on a dialog's action area button, *and* the widget that
8656 * was focused immediately before it is the location entry.
8661 /* The focus is on a dialog's action area button or something else */
8662 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8663 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8668 g_assert_not_reached ();
8672 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8674 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8676 GtkFileChooserDefault *impl;
8679 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8681 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8682 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8684 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8685 widget = impl->browse_files_tree_view;
8687 widget = impl->location_entry;
8689 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8690 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8691 widget = impl->location_entry;
8694 g_assert_not_reached ();
8698 g_assert (widget != NULL);
8699 gtk_widget_grab_focus (widget);
8702 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8704 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8714 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8715 *list = g_slist_prepend (*list, file); /* The file already has a new ref courtesy of gtk_tree_model_get(); this will be unreffed by the caller */
8718 /* Constructs a list of the selected paths in search mode */
8720 search_get_selected_files (GtkFileChooserDefault *impl)
8723 GtkTreeSelection *selection;
8727 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8728 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8729 result = g_slist_reverse (result);
8734 /* Called from ::should_respond(). We return whether there are selected files
8735 * in the search list.
8738 search_should_respond (GtkFileChooserDefault *impl)
8740 GtkTreeSelection *selection;
8742 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8744 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8745 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8748 /* Adds one hit from the search engine to the search_model */
8750 search_add_hit (GtkFileChooserDefault *impl,
8755 file = g_file_new_for_uri (uri);
8759 if (!g_file_is_native (file))
8761 g_object_unref (file);
8765 _gtk_file_system_model_add_and_query_file (impl->search_model,
8769 g_object_unref (file);
8772 /* Callback used from GtkSearchEngine when we get new hits */
8774 search_engine_hits_added_cb (GtkSearchEngine *engine,
8778 GtkFileChooserDefault *impl;
8781 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8783 for (l = hits; l; l = l->next)
8784 search_add_hit (impl, (gchar*)l->data);
8787 /* Callback used from GtkSearchEngine when the query is done running */
8789 search_engine_finished_cb (GtkSearchEngine *engine,
8792 GtkFileChooserDefault *impl;
8794 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8797 /* EB: setting the model here will avoid loads of row events,
8798 * but it'll make the search look like blocked.
8800 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8801 GTK_TREE_MODEL (impl->search_model));
8804 /* FMQ: if search was empty, say that we got no hits */
8805 set_busy_cursor (impl, FALSE);
8808 /* Displays a generic error when we cannot create a GtkSearchEngine.
8809 * It would be better if _gtk_search_engine_new() gave us a GError
8810 * with a better message, but it doesn't do that right now.
8813 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8815 error_message (impl,
8816 _("Could not start the search process"),
8817 _("The program was not able to create a connection to the indexer "
8818 "daemon. Please make sure it is running."));
8822 search_engine_error_cb (GtkSearchEngine *engine,
8823 const gchar *message,
8826 GtkFileChooserDefault *impl;
8828 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8830 search_stop_searching (impl, TRUE);
8831 error_message (impl, _("Could not send the search request"), message);
8833 set_busy_cursor (impl, FALSE);
8836 /* Frees the data in the search_model */
8838 search_clear_model (GtkFileChooserDefault *impl,
8839 gboolean remove_from_treeview)
8841 if (!impl->search_model)
8844 g_object_unref (impl->search_model);
8845 impl->search_model = NULL;
8847 if (remove_from_treeview)
8848 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8851 /* Stops any ongoing searches; does not touch the search_model */
8853 search_stop_searching (GtkFileChooserDefault *impl,
8854 gboolean remove_query)
8856 if (remove_query && impl->search_query)
8858 g_object_unref (impl->search_query);
8859 impl->search_query = NULL;
8862 if (impl->search_engine)
8864 _gtk_search_engine_stop (impl->search_engine);
8866 g_object_unref (impl->search_engine);
8867 impl->search_engine = NULL;
8871 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8873 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8875 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8877 search_stop_searching (impl, FALSE);
8878 search_clear_model (impl, TRUE);
8880 gtk_widget_destroy (impl->search_hbox);
8881 impl->search_hbox = NULL;
8882 impl->search_entry = NULL;
8884 gtk_widget_show (impl->browse_path_bar);
8885 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8886 gtk_widget_hide (impl->browse_new_folder_button);
8888 gtk_widget_show (impl->browse_new_folder_button);
8891 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8892 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8894 gtk_widget_show (impl->location_button);
8896 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8897 gtk_widget_show (impl->location_entry_box);
8900 impl->operation_mode = OPERATION_MODE_BROWSE;
8902 file_list_set_sort_column_ids (impl);
8905 /* Creates the search_model and puts it in the tree view */
8907 search_setup_model (GtkFileChooserDefault *impl)
8909 g_assert (impl->search_model == NULL);
8911 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8913 MODEL_COLUMN_TYPES);
8915 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8919 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8923 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8927 set_sort_column (impl);
8929 /* EB: setting the model here will make the hits list update feel
8930 * more "alive" than setting the model at the end of the search
8933 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8934 GTK_TREE_MODEL (impl->search_model));
8937 /* Creates a new query with the specified text and launches it */
8939 search_start_query (GtkFileChooserDefault *impl,
8940 const gchar *query_text)
8942 search_stop_searching (impl, FALSE);
8943 search_clear_model (impl, TRUE);
8944 search_setup_model (impl);
8945 set_busy_cursor (impl, TRUE);
8947 if (impl->search_engine == NULL)
8948 impl->search_engine = _gtk_search_engine_new ();
8950 if (!impl->search_engine)
8952 set_busy_cursor (impl, FALSE);
8953 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
8957 if (!impl->search_query)
8959 impl->search_query = _gtk_query_new ();
8960 _gtk_query_set_text (impl->search_query, query_text);
8963 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
8965 g_signal_connect (impl->search_engine, "hits-added",
8966 G_CALLBACK (search_engine_hits_added_cb), impl);
8967 g_signal_connect (impl->search_engine, "finished",
8968 G_CALLBACK (search_engine_finished_cb), impl);
8969 g_signal_connect (impl->search_engine, "error",
8970 G_CALLBACK (search_engine_error_cb), impl);
8972 _gtk_search_engine_start (impl->search_engine);
8975 /* Callback used when the user presses Enter while typing on the search
8976 * entry; starts the query
8979 search_entry_activate_cb (GtkEntry *entry,
8982 GtkFileChooserDefault *impl;
8985 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8987 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
8988 if (strlen (text) == 0)
8991 /* reset any existing query object */
8992 if (impl->search_query)
8994 g_object_unref (impl->search_query);
8995 impl->search_query = NULL;
8998 search_start_query (impl, text);
9002 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9004 GDK_THREADS_ENTER ();
9006 g_source_destroy (impl->focus_entry_idle);
9007 impl->focus_entry_idle = NULL;
9009 if (impl->search_entry)
9010 gtk_widget_grab_focus (impl->search_entry);
9012 GDK_THREADS_LEAVE ();
9018 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9020 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9021 * pane, we get a selection-changed signal and we set up the search widgets.
9022 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9023 * the change to the selection. So, we need to re-focus the search entry
9024 * after the treeview has finished doing its work; we'll do that in an idle
9028 if (!impl->focus_entry_idle)
9029 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9032 /* Hides the path bar and creates the search entry */
9034 search_setup_widgets (GtkFileChooserDefault *impl)
9040 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9044 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9045 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9049 label = gtk_label_new (NULL);
9050 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9051 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9052 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9057 impl->search_entry = gtk_entry_new ();
9058 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9059 g_signal_connect (impl->search_entry, "activate",
9060 G_CALLBACK (search_entry_activate_cb),
9062 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9064 /* if there already is a query, restart it */
9065 if (impl->search_query)
9067 gchar *query = _gtk_query_get_text (impl->search_query);
9071 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9072 search_start_query (impl, query);
9078 g_object_unref (impl->search_query);
9079 impl->search_query = NULL;
9083 gtk_widget_hide (impl->browse_path_bar);
9084 gtk_widget_hide (impl->browse_new_folder_button);
9086 /* Box for search widgets */
9087 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9088 gtk_widget_show_all (impl->search_hbox);
9089 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9091 /* Hide the location widgets temporarily */
9093 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9094 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9096 gtk_widget_hide (impl->location_button);
9097 gtk_widget_hide (impl->location_entry_box);
9100 focus_search_entry_in_idle (impl);
9102 /* FMQ: hide the filter combo? */
9105 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9107 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9111 case OPERATION_MODE_BROWSE:
9112 stop_loading_and_clear_list_model (impl, TRUE);
9115 case OPERATION_MODE_SEARCH:
9116 search_stop_searching (impl, FALSE);
9117 search_clear_model (impl, TRUE);
9119 gtk_widget_destroy (impl->search_hbox);
9120 impl->search_hbox = NULL;
9121 impl->search_entry = NULL;
9124 case OPERATION_MODE_RECENT:
9125 recent_stop_loading (impl);
9126 recent_clear_model (impl, TRUE);
9128 gtk_widget_destroy (impl->recent_hbox);
9129 impl->recent_hbox = NULL;
9134 /* Main entry point to the searching functions; this gets called when the user
9135 * activates the Search shortcut.
9138 search_activate (GtkFileChooserDefault *impl)
9140 OperationMode previous_mode;
9142 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9144 focus_search_entry_in_idle (impl);
9148 previous_mode = impl->operation_mode;
9149 impl->operation_mode = OPERATION_MODE_SEARCH;
9151 stop_operation (impl, previous_mode);
9153 g_assert (impl->search_hbox == NULL);
9154 g_assert (impl->search_entry == NULL);
9155 g_assert (impl->search_model == NULL);
9157 search_setup_widgets (impl);
9158 file_list_set_sort_column_ids (impl);
9162 * Recent files support
9165 /* Frees the data in the recent_model */
9167 recent_clear_model (GtkFileChooserDefault *impl,
9168 gboolean remove_from_treeview)
9170 if (!impl->recent_model)
9173 if (remove_from_treeview)
9174 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9176 g_object_unref (impl->recent_model);
9177 impl->recent_model = NULL;
9180 /* Stops any ongoing loading of the recent files list; does
9181 * not touch the recent_model
9184 recent_stop_loading (GtkFileChooserDefault *impl)
9186 if (impl->load_recent_id)
9188 g_source_remove (impl->load_recent_id);
9189 impl->load_recent_id = 0;
9193 /* Stops any pending load, clears the file list, and switches
9194 * back to OPERATION_MODE_BROWSE
9197 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9199 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9201 recent_stop_loading (impl);
9202 recent_clear_model (impl, TRUE);
9204 gtk_widget_destroy (impl->recent_hbox);
9205 impl->recent_hbox = NULL;
9207 gtk_widget_show (impl->browse_path_bar);
9208 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9209 gtk_widget_hide (impl->browse_new_folder_button);
9211 gtk_widget_show (impl->browse_new_folder_button);
9213 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9214 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9216 gtk_widget_show (impl->location_button);
9218 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9219 gtk_widget_show (impl->location_entry_box);
9222 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9224 impl->operation_mode = OPERATION_MODE_BROWSE;
9226 file_list_set_sort_column_ids (impl);
9230 recent_setup_model (GtkFileChooserDefault *impl)
9232 g_assert (impl->recent_model == NULL);
9234 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9236 MODEL_COLUMN_TYPES);
9238 _gtk_file_system_model_set_filter (impl->recent_model,
9239 impl->current_filter);
9240 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9244 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9248 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9252 set_sort_column (impl);
9257 GtkFileChooserDefault *impl;
9260 gint n_loaded_items;
9261 guint needs_sorting : 1;
9265 recent_idle_cleanup (gpointer data)
9267 RecentLoadData *load_data = data;
9268 GtkFileChooserDefault *impl = load_data->impl;
9270 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9271 GTK_TREE_MODEL (impl->recent_model));
9273 set_busy_cursor (impl, FALSE);
9275 impl->load_recent_id = 0;
9277 if (load_data->items)
9279 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9280 g_list_free (load_data->items);
9287 recent_sort_mru (gconstpointer a,
9290 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9291 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9293 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9297 get_recent_files_limit (GtkWidget *widget)
9299 GtkSettings *settings;
9302 if (gtk_widget_has_screen (widget))
9303 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9305 settings = gtk_settings_get_default ();
9307 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9313 recent_idle_load (gpointer data)
9315 RecentLoadData *load_data = data;
9316 GtkFileChooserDefault *impl = load_data->impl;
9320 if (!impl->recent_manager)
9323 /* first iteration: load all the items */
9324 if (!load_data->items)
9326 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9327 if (!load_data->items)
9330 load_data->needs_sorting = TRUE;
9335 /* second iteration: preliminary MRU sorting and clamping */
9336 if (load_data->needs_sorting)
9340 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9341 load_data->n_items = g_list_length (load_data->items);
9343 limit = get_recent_files_limit (GTK_WIDGET (impl));
9345 if (limit != -1 && (load_data->n_items > limit))
9349 clamp = g_list_nth (load_data->items, limit - 1);
9350 if (G_LIKELY (clamp))
9355 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9358 load_data->n_items = limit;
9362 load_data->n_loaded_items = 0;
9363 load_data->needs_sorting = FALSE;
9368 /* finished loading items */
9369 for (walk = load_data->items; walk; walk = walk->next)
9371 GtkRecentInfo *info = walk->data;
9372 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9374 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9377 gtk_recent_info_unref (walk->data);
9378 g_object_unref (file);
9381 g_list_free (load_data->items);
9382 load_data->items = NULL;
9388 recent_start_loading (GtkFileChooserDefault *impl)
9390 RecentLoadData *load_data;
9392 recent_stop_loading (impl);
9393 recent_clear_model (impl, TRUE);
9394 recent_setup_model (impl);
9395 set_busy_cursor (impl, TRUE);
9397 g_assert (impl->load_recent_id == 0);
9399 load_data = g_new (RecentLoadData, 1);
9400 load_data->impl = impl;
9401 load_data->items = NULL;
9402 load_data->n_items = 0;
9403 load_data->n_loaded_items = 0;
9404 load_data->needs_sorting = TRUE;
9406 /* begin lazy loading the recent files into the model */
9407 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9410 recent_idle_cleanup);
9414 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9424 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9425 *list = g_slist_prepend (*list, file);
9428 /* Constructs a list of the selected paths in recent files mode */
9430 recent_get_selected_files (GtkFileChooserDefault *impl)
9433 GtkTreeSelection *selection;
9437 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9438 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9439 result = g_slist_reverse (result);
9444 /* Called from ::should_respond(). We return whether there are selected
9445 * files in the recent files list.
9448 recent_should_respond (GtkFileChooserDefault *impl)
9450 GtkTreeSelection *selection;
9452 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9454 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9455 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9458 /* Hide the location widgets temporarily */
9460 recent_hide_entry (GtkFileChooserDefault *impl)
9466 impl->recent_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9469 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9470 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9473 label = gtk_label_new (NULL);
9474 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9475 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9476 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9479 gtk_widget_hide (impl->browse_path_bar);
9480 gtk_widget_hide (impl->browse_new_folder_button);
9482 /* Box for recent widgets */
9483 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9484 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->recent_hbox);
9485 gtk_widget_show_all (impl->recent_hbox);
9487 /* Hide the location widgets temporarily */
9488 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9489 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9491 gtk_widget_hide (impl->location_button);
9492 gtk_widget_hide (impl->location_entry_box);
9496 /* Main entry point to the recent files functions; this gets called when
9497 * the user activates the Recently Used shortcut.
9500 recent_activate (GtkFileChooserDefault *impl)
9502 OperationMode previous_mode;
9504 if (impl->operation_mode == OPERATION_MODE_RECENT)
9507 previous_mode = impl->operation_mode;
9508 impl->operation_mode = OPERATION_MODE_RECENT;
9510 stop_operation (impl, previous_mode);
9512 recent_hide_entry (impl);
9514 file_list_set_sort_column_ids (impl);
9515 recent_start_loading (impl);
9519 set_current_filter (GtkFileChooserDefault *impl,
9520 GtkFileFilter *filter)
9522 if (impl->current_filter != filter)
9526 /* NULL filters are allowed to reset to non-filtered status
9528 filter_index = g_slist_index (impl->filters, filter);
9529 if (impl->filters && filter && filter_index < 0)
9532 if (impl->current_filter)
9533 g_object_unref (impl->current_filter);
9534 impl->current_filter = filter;
9535 if (impl->current_filter)
9537 g_object_ref_sink (impl->current_filter);
9541 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9544 if (impl->browse_files_model)
9545 install_list_model_filter (impl);
9547 if (impl->search_model)
9548 _gtk_file_system_model_set_filter (impl->search_model, filter);
9550 if (impl->recent_model)
9551 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9553 g_object_notify (G_OBJECT (impl), "filter");
9558 filter_combo_changed (GtkComboBox *combo_box,
9559 GtkFileChooserDefault *impl)
9561 gint new_index = gtk_combo_box_get_active (combo_box);
9562 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9564 set_current_filter (impl, new_filter);
9568 check_preview_change (GtkFileChooserDefault *impl)
9570 GtkTreePath *cursor_path;
9572 char *new_display_name;
9573 GtkTreeModel *model;
9575 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9576 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9581 gtk_tree_model_get_iter (model, &iter, cursor_path);
9582 gtk_tree_model_get (model, &iter,
9583 MODEL_COL_FILE, &new_file,
9584 MODEL_COL_NAME, &new_display_name,
9587 gtk_tree_path_free (cursor_path);
9592 new_display_name = NULL;
9595 if (new_file != impl->preview_file &&
9596 !(new_file && impl->preview_file &&
9597 g_file_equal (new_file, impl->preview_file)))
9599 if (impl->preview_file)
9601 g_object_unref (impl->preview_file);
9602 g_free (impl->preview_display_name);
9607 impl->preview_file = new_file;
9608 impl->preview_display_name = new_display_name;
9612 impl->preview_file = NULL;
9613 impl->preview_display_name = NULL;
9614 g_free (new_display_name);
9617 if (impl->use_preview_label && impl->preview_label)
9618 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9620 g_signal_emit_by_name (impl, "update-preview");
9625 g_object_unref (new_file);
9627 g_free (new_display_name);
9632 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9633 GtkFileSystemVolume *volume,
9634 const GError *error,
9638 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9639 GtkFileChooserDefault *impl = data;
9641 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9644 impl->shortcuts_activate_iter_cancellable = NULL;
9646 set_busy_cursor (impl, FALSE);
9653 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9657 name = _gtk_file_system_volume_get_display_name (volume);
9658 msg = g_strdup_printf (_("Could not mount %s"), name);
9660 error_message (impl, msg, error->message);
9669 file = _gtk_file_system_volume_get_root (volume);
9672 change_folder_and_display_error (impl, file, FALSE);
9673 g_object_unref (file);
9677 g_object_unref (impl);
9678 g_object_unref (cancellable);
9682 /* Activates a volume by mounting it if necessary and then switching to its
9686 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9687 GtkFileSystemVolume *volume)
9691 switch (impl->operation_mode)
9693 case OPERATION_MODE_BROWSE:
9695 case OPERATION_MODE_SEARCH:
9696 search_switch_to_browse_mode (impl);
9698 case OPERATION_MODE_RECENT:
9699 recent_switch_to_browse_mode (impl);
9703 /* We ref the file chooser since volume_mount() may run a main loop, and the
9704 * user could close the file chooser window in the meantime.
9706 g_object_ref (impl);
9708 if (!_gtk_file_system_volume_is_mounted (volume))
9710 GMountOperation *mount_op;
9712 set_busy_cursor (impl, TRUE);
9714 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9715 impl->shortcuts_activate_iter_cancellable =
9716 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9717 shortcuts_activate_volume_mount_cb,
9718 g_object_ref (impl));
9719 g_object_unref (mount_op);
9723 file = _gtk_file_system_volume_get_root (volume);
9726 change_folder_and_display_error (impl, file, FALSE);
9727 g_object_unref (file);
9731 g_object_unref (impl);
9734 /* Opens the folder or volume at the specified iter in the shortcuts model */
9735 struct ShortcutsActivateData
9737 GtkFileChooserDefault *impl;
9742 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9744 const GError *error,
9747 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9748 struct ShortcutsActivateData *data = user_data;
9750 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9753 data->impl->shortcuts_activate_iter_cancellable = NULL;
9758 if (!error && _gtk_file_info_consider_as_directory (info))
9759 change_folder_and_display_error (data->impl, data->file, FALSE);
9761 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9766 g_object_unref (data->impl);
9767 g_object_unref (data->file);
9770 g_object_unref (cancellable);
9774 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9775 GtkFileSystemVolume *volume,
9776 const GError *error,
9779 struct ShortcutsActivateData *data = user_data;
9783 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9785 g_object_unref (data->impl);
9786 g_object_unref (data->file);
9792 data->impl->shortcuts_activate_iter_cancellable =
9793 _gtk_file_system_get_info (data->impl->file_system, data->file,
9795 shortcuts_activate_get_info_cb, data);
9798 _gtk_file_system_volume_unref (volume);
9802 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9806 ShortcutType shortcut_type;
9808 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9809 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9810 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9811 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9813 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9814 SHORTCUTS_COL_DATA, &col_data,
9815 SHORTCUTS_COL_TYPE, &shortcut_type,
9818 if (impl->shortcuts_activate_iter_cancellable)
9820 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9821 impl->shortcuts_activate_iter_cancellable = NULL;
9824 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9826 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9828 GtkFileSystemVolume *volume;
9832 shortcuts_activate_volume (impl, volume);
9834 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9836 struct ShortcutsActivateData *data;
9837 GtkFileSystemVolume *volume;
9839 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9841 data = g_new0 (struct ShortcutsActivateData, 1);
9842 data->impl = g_object_ref (impl);
9843 data->file = g_object_ref (col_data);
9845 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9847 GMountOperation *mount_operation;
9848 GtkWidget *toplevel;
9850 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9852 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9854 impl->shortcuts_activate_iter_cancellable =
9855 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9857 shortcuts_activate_mount_enclosing_volume,
9862 impl->shortcuts_activate_iter_cancellable =
9863 _gtk_file_system_get_info (impl->file_system, data->file,
9865 shortcuts_activate_get_info_cb, data);
9868 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9870 search_activate (impl);
9872 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9874 recent_activate (impl);
9878 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9880 shortcuts_key_press_event_cb (GtkWidget *widget,
9882 GtkFileChooserDefault *impl)
9886 modifiers = gtk_accelerator_get_default_mod_mask ();
9888 if (key_is_left_or_right (event))
9890 gtk_widget_grab_focus (impl->browse_files_tree_view);
9894 if ((event->keyval == GDK_KEY_BackSpace
9895 || event->keyval == GDK_KEY_Delete
9896 || event->keyval == GDK_KEY_KP_Delete)
9897 && (event->state & modifiers) == 0)
9899 remove_selected_bookmarks (impl);
9903 if ((event->keyval == GDK_KEY_F2)
9904 && (event->state & modifiers) == 0)
9906 rename_selected_bookmark (impl);
9914 shortcuts_select_func (GtkTreeSelection *selection,
9915 GtkTreeModel *model,
9917 gboolean path_currently_selected,
9920 GtkFileChooserDefault *impl = data;
9921 GtkTreeIter filter_iter;
9922 ShortcutType shortcut_type;
9924 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9925 g_assert_not_reached ();
9927 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9929 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9933 list_select_func (GtkTreeSelection *selection,
9934 GtkTreeModel *model,
9936 gboolean path_currently_selected,
9939 GtkFileChooserDefault *impl = data;
9941 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
9942 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9947 if (!gtk_tree_model_get_iter (model, &iter, path))
9949 gtk_tree_model_get (model, &iter,
9950 MODEL_COL_IS_FOLDER, &is_folder,
9960 list_selection_changed (GtkTreeSelection *selection,
9961 GtkFileChooserDefault *impl)
9963 /* See if we are in the new folder editable row for Save mode */
9964 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
9965 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9968 gboolean had_selection;
9970 info = get_selected_file_info_from_file_list (impl, &had_selection);
9972 goto out; /* normal processing */
9975 return; /* We are on the editable row for New Folder */
9980 if (impl->location_entry)
9981 update_chooser_entry (impl);
9983 check_preview_change (impl);
9984 bookmarks_check_add_sensitivity (impl);
9986 g_signal_emit_by_name (impl, "selection-changed", 0);
9989 /* Callback used when a row in the file list is activated */
9991 list_row_activated (GtkTreeView *tree_view,
9993 GtkTreeViewColumn *column,
9994 GtkFileChooserDefault *impl)
9998 GtkTreeModel *model;
10001 model = gtk_tree_view_get_model (tree_view);
10003 if (!gtk_tree_model_get_iter (model, &iter, path))
10006 gtk_tree_model_get (model, &iter,
10007 MODEL_COL_FILE, &file,
10008 MODEL_COL_IS_FOLDER, &is_folder,
10011 if (is_folder && file)
10013 change_folder_and_display_error (impl, file, FALSE);
10014 g_object_unref (file);
10018 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10019 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10020 g_signal_emit_by_name (impl, "file-activated");
10025 g_object_unref (file);
10029 path_bar_clicked (GtkPathBar *path_bar,
10032 gboolean child_is_hidden,
10033 GtkFileChooserDefault *impl)
10036 pending_select_files_add (impl, child_file);
10038 if (!change_folder_and_display_error (impl, file, FALSE))
10041 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10042 * show hidden files so that ".baz" appears in the file list, as it will still
10043 * be shown in the path bar: "/foo/[bar]/.baz"
10045 if (child_is_hidden)
10046 g_object_set (impl, "show-hidden", TRUE, NULL);
10050 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10052 GtkTreeViewColumn *column;
10053 GtkCellRenderer *renderer;
10054 GList *walk, *list;
10055 gboolean always_sensitive;
10057 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10058 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10060 /* Keep the following column numbers in sync with create_file_list() */
10063 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10064 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10065 for (walk = list; walk; walk = walk->next)
10067 renderer = walk->data;
10068 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10070 gtk_tree_view_column_set_attributes (column, renderer,
10071 "pixbuf", MODEL_COL_PIXBUF,
10076 gtk_tree_view_column_set_attributes (column, renderer,
10077 "text", MODEL_COL_NAME,
10078 "ellipsize", MODEL_COL_ELLIPSIZE,
10081 if (always_sensitive)
10082 g_object_set (renderer, "sensitive", TRUE, NULL);
10084 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10086 g_list_free (list);
10089 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10090 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10091 renderer = list->data;
10092 gtk_tree_view_column_set_attributes (column, renderer,
10093 "text", MODEL_COL_SIZE_TEXT,
10095 if (always_sensitive)
10096 g_object_set (renderer, "sensitive", TRUE, NULL);
10098 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10099 g_list_free (list);
10102 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10103 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10104 renderer = list->data;
10105 gtk_tree_view_column_set_attributes (column, renderer,
10106 "text", MODEL_COL_MTIME_TEXT,
10108 if (always_sensitive)
10109 g_object_set (renderer, "sensitive", TRUE, NULL);
10111 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10112 g_list_free (list);
10116 _gtk_file_chooser_default_new (void)
10118 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10122 location_set_user_text (GtkFileChooserDefault *impl,
10125 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10126 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10130 location_popup_handler (GtkFileChooserDefault *impl,
10133 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10135 GtkWidget *widget_to_focus;
10137 /* This will give us the location widgets back */
10138 switch (impl->operation_mode)
10140 case OPERATION_MODE_SEARCH:
10141 search_switch_to_browse_mode (impl);
10143 case OPERATION_MODE_RECENT:
10144 recent_switch_to_browse_mode (impl);
10146 case OPERATION_MODE_BROWSE:
10147 g_assert_not_reached ();
10151 if (impl->current_folder)
10152 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10154 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10155 widget_to_focus = impl->browse_files_tree_view;
10157 widget_to_focus = impl->location_entry;
10159 gtk_widget_grab_focus (widget_to_focus);
10163 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10164 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10166 LocationMode new_mode;
10170 /* since the user typed something, we unconditionally want to turn on the entry */
10171 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10173 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10174 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10175 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10176 new_mode = LOCATION_MODE_PATH_BAR;
10179 g_assert_not_reached ();
10183 location_mode_set (impl, new_mode, TRUE);
10184 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10187 location_set_user_text (impl, path);
10190 location_entry_set_initial_text (impl);
10191 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10195 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10196 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10198 gtk_widget_grab_focus (impl->location_entry);
10200 location_set_user_text (impl, path);
10203 g_assert_not_reached ();
10206 /* Handler for the "up-folder" keybinding signal */
10208 up_folder_handler (GtkFileChooserDefault *impl)
10210 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10213 /* Handler for the "down-folder" keybinding signal */
10215 down_folder_handler (GtkFileChooserDefault *impl)
10217 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10220 /* Switches to the shortcut in the specified index */
10222 switch_to_shortcut (GtkFileChooserDefault *impl,
10227 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10228 g_assert_not_reached ();
10230 shortcuts_activate_iter (impl, &iter);
10233 /* Handler for the "home-folder" keybinding signal */
10235 home_folder_handler (GtkFileChooserDefault *impl)
10237 if (impl->has_home)
10238 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10241 /* Handler for the "desktop-folder" keybinding signal */
10243 desktop_folder_handler (GtkFileChooserDefault *impl)
10245 if (impl->has_desktop)
10246 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10249 /* Handler for the "search-shortcut" keybinding signal */
10251 search_shortcut_handler (GtkFileChooserDefault *impl)
10253 if (impl->has_search)
10255 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10257 /* we want the entry widget to grab the focus the first
10258 * time, not the browse_files_tree_view widget.
10260 if (impl->search_entry)
10261 gtk_widget_grab_focus (impl->search_entry);
10265 /* Handler for the "recent-shortcut" keybinding signal */
10267 recent_shortcut_handler (GtkFileChooserDefault *impl)
10269 if (impl->has_recent)
10270 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10274 quick_bookmark_handler (GtkFileChooserDefault *impl,
10275 gint bookmark_index)
10280 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10283 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10285 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10286 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10289 gtk_tree_path_free (path);
10291 switch_to_shortcut (impl, bookmark_pos);
10295 show_hidden_handler (GtkFileChooserDefault *impl)
10297 g_object_set (impl,
10298 "show-hidden", !impl->show_hidden,
10303 /* Drag and drop interfaces */
10306 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10311 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10313 model->impl = NULL;
10316 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10318 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10321 ShortcutsPaneModelFilter *model;
10325 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10327 pos = *gtk_tree_path_get_indices (path);
10328 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10330 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10333 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10337 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10339 GtkSelectionData *selection_data)
10346 /* Fill the GtkTreeDragSourceIface vtable */
10348 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10350 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10351 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10355 /* Fill the GtkTreeDragDestIface vtable */
10357 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10359 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10360 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10364 static GtkTreeModel *
10365 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10366 GtkTreeModel *child_model,
10369 ShortcutsPaneModelFilter *model;
10371 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10372 "child-model", child_model,
10373 "virtual-root", root,
10376 model->impl = impl;
10378 return GTK_TREE_MODEL (model);