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, see <http://www.gnu.org/licenses/>.
22 #include "gtkfilechooserdefault.h"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrendererpixbuf.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcheckmenuitem.h"
29 #include "gtkclipboard.h"
30 #include "gtkcomboboxtext.h"
32 #include "gtkexpander.h"
33 #include "gtkfilechooserprivate.h"
34 #include "gtkfilechooserdialog.h"
35 #include "gtkfilechooserembed.h"
36 #include "gtkfilechooserentry.h"
37 #include "gtkfilechooserutils.h"
38 #include "gtkfilechooser.h"
39 #include "gtkfilesystem.h"
40 #include "gtkfilesystemmodel.h"
43 #include "gtkiconfactory.h"
44 #include "gtkicontheme.h"
46 #include "gtkimagemenuitem.h"
47 #include "gtkinfobar.h"
49 #include "gtkmarshalers.h"
50 #include "gtkmessagedialog.h"
51 #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 "gtksettings.h"
61 #include "gtksizegroup.h"
62 #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>
88 /* Values for GtkSelection-related "info" fields */
89 #define SELECTION_TEXT 0
90 #define SELECTION_URI 1
93 #undef PROFILE_FILE_CHOOSER
94 #ifdef PROFILE_FILE_CHOOSER
101 #define PROFILE_INDENT 4
103 static int profile_indent;
106 profile_add_indent (int indent)
108 profile_indent += indent;
109 if (profile_indent < 0)
110 g_error ("You screwed up your indentation");
114 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
119 profile_add_indent (indent);
121 if (profile_indent == 0)
122 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
124 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
130 profile_add_indent (indent);
133 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
134 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
135 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
137 #define profile_start(x, y)
138 #define profile_end(x, y)
139 #define profile_msg(x, y)
144 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
146 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
147 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
148 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
150 #define MAX_LOADING_TIME 500
152 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
154 struct _GtkFileChooserDefaultClass
156 GtkBoxClass parent_class;
162 LOCATION_POPUP_ON_PASTE,
168 LOCATION_TOGGLE_POPUP,
176 static guint signals[LAST_SIGNAL] = { 0 };
178 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
180 SHORTCUTS_COL_PIXBUF,
184 SHORTCUTS_COL_REMOVABLE,
185 SHORTCUTS_COL_PIXBUF_VISIBLE,
186 SHORTCUTS_COL_CANCELLABLE,
187 SHORTCUTS_COL_NUM_COLUMNS
192 SHORTCUT_TYPE_VOLUME,
193 SHORTCUT_TYPE_SEPARATOR,
194 SHORTCUT_TYPE_SEARCH,
198 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
199 "standard::is-hidden,standard::is-backup,standard::size," \
200 "standard::content-type,time::modified"
202 /* the first 3 must be these due to settings caching sort column */
207 MODEL_COL_NAME_COLLATED,
209 MODEL_COL_IS_SENSITIVE,
212 MODEL_COL_MTIME_TEXT,
214 MODEL_COL_NUM_COLUMNS
217 /* This list of types is passed to _gtk_file_system_model_new*() */
218 #define MODEL_COLUMN_TYPES \
219 MODEL_COL_NUM_COLUMNS, \
220 G_TYPE_STRING, /* MODEL_COL_NAME */ \
221 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
222 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
223 G_TYPE_FILE, /* MODEL_COL_FILE */ \
224 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
225 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
226 G_TYPE_BOOLEAN, /* MODEL_COL_IS_SENSITIVE */ \
227 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
228 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
229 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
230 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
232 /* Identifiers for target types */
237 /* Interesting places in the shortcuts bar */
241 SHORTCUTS_RECENT_SEPARATOR,
246 SHORTCUTS_BOOKMARKS_SEPARATOR,
248 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
249 SHORTCUTS_CURRENT_FOLDER
252 /* Icon size for if we can't get it from the theme */
253 #define FALLBACK_ICON_SIZE 16
255 #define PREVIEW_HBOX_SPACING 12
259 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
260 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
261 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
262 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
263 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
264 #define SETTINGS_KEY_SORT_ORDER "sort-order"
265 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
266 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
268 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
269 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
271 static GObject* gtk_file_chooser_default_constructor (GType type,
272 guint n_construct_properties,
273 GObjectConstructParam *construct_params);
274 static void gtk_file_chooser_default_finalize (GObject *object);
275 static void gtk_file_chooser_default_set_property (GObject *object,
279 static void gtk_file_chooser_default_get_property (GObject *object,
283 static void gtk_file_chooser_default_dispose (GObject *object);
284 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
285 static void gtk_file_chooser_default_realize (GtkWidget *widget);
286 static void gtk_file_chooser_default_map (GtkWidget *widget);
287 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
288 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
289 GtkWidget *previous_toplevel);
290 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
291 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
292 GdkScreen *previous_screen);
293 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
294 GtkAllocation *allocation);
296 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
299 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
302 gboolean clear_entry,
304 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
305 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
307 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
310 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
312 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
313 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
314 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
315 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
316 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
317 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
318 GtkFileFilter *filter);
319 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
320 GtkFileFilter *filter);
321 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
322 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
325 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
328 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
330 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
332 gint *default_height);
333 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
334 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
336 static void add_selection_to_recent_list (GtkFileChooserDefault *impl);
338 static void location_popup_handler (GtkFileChooserDefault *impl,
340 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
341 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
342 static void up_folder_handler (GtkFileChooserDefault *impl);
343 static void down_folder_handler (GtkFileChooserDefault *impl);
344 static void home_folder_handler (GtkFileChooserDefault *impl);
345 static void desktop_folder_handler (GtkFileChooserDefault *impl);
346 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
347 gint bookmark_index);
348 static void show_hidden_handler (GtkFileChooserDefault *impl);
349 static void search_shortcut_handler (GtkFileChooserDefault *impl);
350 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
351 static void update_appearance (GtkFileChooserDefault *impl);
353 static void set_current_filter (GtkFileChooserDefault *impl,
354 GtkFileFilter *filter);
355 static void check_preview_change (GtkFileChooserDefault *impl);
357 static void filter_combo_changed (GtkComboBox *combo_box,
358 GtkFileChooserDefault *impl);
360 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
362 GtkFileChooserDefault *impl);
364 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
367 gboolean path_currently_selected,
369 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
371 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
373 static int shortcuts_get_index (GtkFileChooserDefault *impl,
374 ShortcutsIndex where);
375 static int shortcut_find_position (GtkFileChooserDefault *impl,
378 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
380 static gboolean list_select_func (GtkTreeSelection *selection,
383 gboolean path_currently_selected,
386 static void list_selection_changed (GtkTreeSelection *tree_selection,
387 GtkFileChooserDefault *impl);
388 static void list_row_activated (GtkTreeView *tree_view,
390 GtkTreeViewColumn *column,
391 GtkFileChooserDefault *impl);
393 static void path_bar_clicked (GtkPathBar *path_bar,
396 gboolean child_is_hidden,
397 GtkFileChooserDefault *impl);
399 static void add_bookmark_button_clicked_cb (GtkButton *button,
400 GtkFileChooserDefault *impl);
401 static void remove_bookmark_button_clicked_cb (GtkButton *button,
402 GtkFileChooserDefault *impl);
404 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
406 static void load_remove_timer (GtkFileChooserDefault *impl);
407 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
409 static void location_button_toggled_cb (GtkToggleButton *toggle,
410 GtkFileChooserDefault *impl);
411 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
413 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
414 gboolean remove_from_treeview);
416 static void search_setup_widgets (GtkFileChooserDefault *impl);
417 static void search_stop_searching (GtkFileChooserDefault *impl,
418 gboolean remove_query);
419 static void search_clear_model (GtkFileChooserDefault *impl,
420 gboolean remove_from_treeview);
421 static gboolean search_should_respond (GtkFileChooserDefault *impl);
422 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
423 static void search_entry_activate_cb (GtkEntry *entry,
425 static void settings_load (GtkFileChooserDefault *impl);
427 static void recent_start_loading (GtkFileChooserDefault *impl);
428 static void recent_stop_loading (GtkFileChooserDefault *impl);
429 static void recent_clear_model (GtkFileChooserDefault *impl,
430 gboolean remove_from_treeview);
431 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
432 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
433 static void set_file_system_backend (GtkFileChooserDefault *impl);
434 static void unset_file_system_backend (GtkFileChooserDefault *impl);
440 /* Drag and drop interface declarations */
443 GtkTreeModelFilter parent;
445 GtkFileChooserDefault *impl;
446 } ShortcutsPaneModelFilter;
449 GtkTreeModelFilterClass parent_class;
450 } ShortcutsPaneModelFilterClass;
452 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
453 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
455 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
457 GType _shortcuts_pane_model_filter_get_type (void);
459 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
460 _shortcuts_pane_model_filter,
461 GTK_TYPE_TREE_MODEL_FILTER,
462 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
463 shortcuts_pane_model_filter_drag_source_iface_init))
465 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
466 GtkTreeModel *child_model,
471 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
472 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
473 gtk_file_chooser_default_iface_init)
474 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
475 gtk_file_chooser_embed_default_iface_init));
479 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
481 GdkModifierType modifiers,
482 const gchar *signal_name)
484 gtk_binding_entry_add_signal (binding_set,
488 gtk_binding_entry_add_signal (binding_set,
489 keyval, modifiers | GDK_SHIFT_MASK,
494 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
496 static const guint quick_bookmark_keyvals[10] = {
497 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
499 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
500 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
501 GtkBindingSet *binding_set;
504 gobject_class->finalize = gtk_file_chooser_default_finalize;
505 gobject_class->constructor = gtk_file_chooser_default_constructor;
506 gobject_class->set_property = gtk_file_chooser_default_set_property;
507 gobject_class->get_property = gtk_file_chooser_default_get_property;
508 gobject_class->dispose = gtk_file_chooser_default_dispose;
510 widget_class->show_all = gtk_file_chooser_default_show_all;
511 widget_class->realize = gtk_file_chooser_default_realize;
512 widget_class->map = gtk_file_chooser_default_map;
513 widget_class->unmap = gtk_file_chooser_default_unmap;
514 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
515 widget_class->style_updated = gtk_file_chooser_default_style_updated;
516 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
517 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
519 signals[LOCATION_POPUP] =
520 g_signal_new_class_handler (I_("location-popup"),
521 G_OBJECT_CLASS_TYPE (class),
522 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
523 G_CALLBACK (location_popup_handler),
525 _gtk_marshal_VOID__STRING,
526 G_TYPE_NONE, 1, G_TYPE_STRING);
528 signals[LOCATION_POPUP_ON_PASTE] =
529 g_signal_new_class_handler (I_("location-popup-on-paste"),
530 G_OBJECT_CLASS_TYPE (class),
531 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
532 G_CALLBACK (location_popup_on_paste_handler),
534 _gtk_marshal_VOID__VOID,
537 signals[LOCATION_TOGGLE_POPUP] =
538 g_signal_new_class_handler (I_("location-toggle-popup"),
539 G_OBJECT_CLASS_TYPE (class),
540 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
541 G_CALLBACK (location_toggle_popup_handler),
543 _gtk_marshal_VOID__VOID,
547 g_signal_new_class_handler (I_("up-folder"),
548 G_OBJECT_CLASS_TYPE (class),
549 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
550 G_CALLBACK (up_folder_handler),
552 _gtk_marshal_VOID__VOID,
555 signals[DOWN_FOLDER] =
556 g_signal_new_class_handler (I_("down-folder"),
557 G_OBJECT_CLASS_TYPE (class),
558 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
559 G_CALLBACK (down_folder_handler),
561 _gtk_marshal_VOID__VOID,
564 signals[HOME_FOLDER] =
565 g_signal_new_class_handler (I_("home-folder"),
566 G_OBJECT_CLASS_TYPE (class),
567 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
568 G_CALLBACK (home_folder_handler),
570 _gtk_marshal_VOID__VOID,
573 signals[DESKTOP_FOLDER] =
574 g_signal_new_class_handler (I_("desktop-folder"),
575 G_OBJECT_CLASS_TYPE (class),
576 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
577 G_CALLBACK (desktop_folder_handler),
579 _gtk_marshal_VOID__VOID,
582 signals[QUICK_BOOKMARK] =
583 g_signal_new_class_handler (I_("quick-bookmark"),
584 G_OBJECT_CLASS_TYPE (class),
585 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
586 G_CALLBACK (quick_bookmark_handler),
588 _gtk_marshal_VOID__INT,
589 G_TYPE_NONE, 1, G_TYPE_INT);
591 signals[SHOW_HIDDEN] =
592 g_signal_new_class_handler (I_("show-hidden"),
593 G_OBJECT_CLASS_TYPE (class),
594 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
595 G_CALLBACK (show_hidden_handler),
597 _gtk_marshal_VOID__VOID,
600 signals[SEARCH_SHORTCUT] =
601 g_signal_new_class_handler (I_("search-shortcut"),
602 G_OBJECT_CLASS_TYPE (class),
603 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
604 G_CALLBACK (search_shortcut_handler),
606 _gtk_marshal_VOID__VOID,
609 signals[RECENT_SHORTCUT] =
610 g_signal_new_class_handler (I_("recent-shortcut"),
611 G_OBJECT_CLASS_TYPE (class),
612 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
613 G_CALLBACK (recent_shortcut_handler),
615 _gtk_marshal_VOID__VOID,
618 binding_set = gtk_binding_set_by_class (class);
620 gtk_binding_entry_add_signal (binding_set,
621 GDK_KEY_l, GDK_CONTROL_MASK,
622 "location-toggle-popup",
625 gtk_binding_entry_add_signal (binding_set,
628 1, G_TYPE_STRING, "/");
629 gtk_binding_entry_add_signal (binding_set,
630 GDK_KEY_KP_Divide, 0,
632 1, G_TYPE_STRING, "/");
635 gtk_binding_entry_add_signal (binding_set,
636 GDK_KEY_asciitilde, 0,
638 1, G_TYPE_STRING, "~");
641 gtk_binding_entry_add_signal (binding_set,
642 GDK_KEY_v, GDK_CONTROL_MASK,
643 "location-popup-on-paste",
645 gtk_binding_entry_add_signal (binding_set,
646 GDK_KEY_BackSpace, 0,
650 add_normal_and_shifted_binding (binding_set,
651 GDK_KEY_Up, GDK_MOD1_MASK,
654 add_normal_and_shifted_binding (binding_set,
655 GDK_KEY_KP_Up, GDK_MOD1_MASK,
658 add_normal_and_shifted_binding (binding_set,
659 GDK_KEY_Down, GDK_MOD1_MASK,
661 add_normal_and_shifted_binding (binding_set,
662 GDK_KEY_KP_Down, GDK_MOD1_MASK,
665 gtk_binding_entry_add_signal (binding_set,
666 GDK_KEY_Home, GDK_MOD1_MASK,
669 gtk_binding_entry_add_signal (binding_set,
670 GDK_KEY_KP_Home, GDK_MOD1_MASK,
673 gtk_binding_entry_add_signal (binding_set,
674 GDK_KEY_d, GDK_MOD1_MASK,
677 gtk_binding_entry_add_signal (binding_set,
678 GDK_KEY_h, GDK_CONTROL_MASK,
681 gtk_binding_entry_add_signal (binding_set,
682 GDK_KEY_s, GDK_MOD1_MASK,
685 gtk_binding_entry_add_signal (binding_set,
686 GDK_KEY_r, GDK_MOD1_MASK,
690 for (i = 0; i < 10; i++)
691 gtk_binding_entry_add_signal (binding_set,
692 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
696 _gtk_file_chooser_install_properties (gobject_class);
700 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
702 iface->select_file = gtk_file_chooser_default_select_file;
703 iface->unselect_file = gtk_file_chooser_default_unselect_file;
704 iface->select_all = gtk_file_chooser_default_select_all;
705 iface->unselect_all = gtk_file_chooser_default_unselect_all;
706 iface->get_files = gtk_file_chooser_default_get_files;
707 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
708 iface->get_file_system = gtk_file_chooser_default_get_file_system;
709 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
710 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
711 iface->set_current_name = gtk_file_chooser_default_set_current_name;
712 iface->add_filter = gtk_file_chooser_default_add_filter;
713 iface->remove_filter = gtk_file_chooser_default_remove_filter;
714 iface->list_filters = gtk_file_chooser_default_list_filters;
715 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
716 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
717 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
721 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
723 iface->get_default_size = gtk_file_chooser_default_get_default_size;
724 iface->should_respond = gtk_file_chooser_default_should_respond;
725 iface->initial_focus = gtk_file_chooser_default_initial_focus;
729 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
731 profile_start ("start", NULL);
732 #ifdef PROFILE_FILE_CHOOSER
733 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
735 impl->local_only = TRUE;
736 impl->preview_widget_active = TRUE;
737 impl->use_preview_label = TRUE;
738 impl->select_multiple = FALSE;
739 impl->show_hidden = FALSE;
740 impl->show_size_column = TRUE;
741 impl->icon_size = FALLBACK_ICON_SIZE;
742 impl->load_state = LOAD_EMPTY;
743 impl->reload_state = RELOAD_EMPTY;
744 impl->pending_select_files = NULL;
745 impl->location_mode = LOCATION_MODE_PATH_BAR;
746 impl->operation_mode = OPERATION_MODE_BROWSE;
747 impl->sort_column = MODEL_COL_NAME;
748 impl->sort_order = GTK_SORT_ASCENDING;
749 impl->recent_manager = gtk_recent_manager_get_default ();
750 impl->create_folders = TRUE;
752 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
753 GTK_ORIENTATION_VERTICAL);
754 gtk_box_set_spacing (GTK_BOX (impl), 12);
756 set_file_system_backend (impl);
758 profile_end ("end", NULL);
761 /* Frees the data columns for the specified iter in the shortcuts model*/
763 shortcuts_free_row_data (GtkFileChooserDefault *impl,
767 ShortcutType shortcut_type;
768 GCancellable *cancellable;
770 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
771 SHORTCUTS_COL_DATA, &col_data,
772 SHORTCUTS_COL_TYPE, &shortcut_type,
773 SHORTCUTS_COL_CANCELLABLE, &cancellable,
778 g_cancellable_cancel (cancellable);
779 g_object_unref (cancellable);
782 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
783 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
787 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
789 GtkFileSystemVolume *volume;
792 _gtk_file_system_volume_unref (volume);
794 if (shortcut_type == SHORTCUT_TYPE_FILE)
799 g_object_unref (file);
803 /* Frees all the data columns in the shortcuts model */
805 shortcuts_free (GtkFileChooserDefault *impl)
809 if (!impl->shortcuts_model)
812 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
815 shortcuts_free_row_data (impl, &iter);
817 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
819 g_object_unref (impl->shortcuts_model);
820 impl->shortcuts_model = NULL;
824 pending_select_files_free (GtkFileChooserDefault *impl)
826 g_slist_free_full (impl->pending_select_files, g_object_unref);
827 impl->pending_select_files = NULL;
831 pending_select_files_add (GtkFileChooserDefault *impl,
834 impl->pending_select_files =
835 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
839 gtk_file_chooser_default_finalize (GObject *object)
841 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
844 unset_file_system_backend (impl);
846 if (impl->shortcuts_pane_filter_model)
847 g_object_unref (impl->shortcuts_pane_filter_model);
849 shortcuts_free (impl);
851 g_free (impl->browse_files_last_selected_name);
853 for (l = impl->filters; l; l = l->next)
855 GtkFileFilter *filter;
857 filter = GTK_FILE_FILTER (l->data);
858 g_object_unref (filter);
860 g_slist_free (impl->filters);
862 if (impl->current_filter)
863 g_object_unref (impl->current_filter);
865 if (impl->current_volume_file)
866 g_object_unref (impl->current_volume_file);
868 if (impl->current_folder)
869 g_object_unref (impl->current_folder);
871 if (impl->preview_file)
872 g_object_unref (impl->preview_file);
874 if (impl->browse_path_bar_size_group)
875 g_object_unref (impl->browse_path_bar_size_group);
877 /* Free all the Models we have */
878 stop_loading_and_clear_list_model (impl, FALSE);
879 search_clear_model (impl, FALSE);
880 recent_clear_model (impl, FALSE);
882 /* stopping the load above should have cleared this */
883 g_assert (impl->load_timeout_id == 0);
885 g_free (impl->preview_display_name);
887 g_free (impl->edited_new_text);
889 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
892 /* Shows an error dialog set as transient for the specified window */
894 error_message_with_parent (GtkWindow *parent,
900 dialog = gtk_message_dialog_new (parent,
901 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
906 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
909 if (parent && gtk_window_has_group (parent))
910 gtk_window_group_add_window (gtk_window_get_group (parent),
911 GTK_WINDOW (dialog));
913 gtk_dialog_run (GTK_DIALOG (dialog));
914 gtk_widget_destroy (dialog);
917 /* Returns a toplevel GtkWindow, or NULL if none */
919 get_toplevel (GtkWidget *widget)
923 toplevel = gtk_widget_get_toplevel (widget);
924 if (!gtk_widget_is_toplevel (toplevel))
927 return GTK_WINDOW (toplevel);
930 /* Shows an error dialog for the file chooser */
932 error_message (GtkFileChooserDefault *impl,
936 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
939 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
941 error_dialog (GtkFileChooserDefault *impl,
952 uri = g_file_get_uri (file);
953 text = g_strdup_printf (msg, uri);
954 error_message (impl, text, error->message);
957 g_error_free (error);
961 /* Displays an error message about not being able to get information for a file.
962 * Frees the GError as well.
965 error_getting_info_dialog (GtkFileChooserDefault *impl,
970 _("Could not retrieve information about the file"),
974 /* Shows an error dialog about not being able to add a bookmark */
976 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
981 _("Could not add a bookmark"),
985 /* Shows an error dialog about not being able to remove a bookmark */
987 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
992 _("Could not remove bookmark"),
996 /* Shows an error dialog about not being able to create a folder */
998 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1003 _("The folder could not be created"),
1007 /* Shows an error about not being able to create a folder because a file with
1008 * the same name is already there.
1011 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1016 _("The folder could not be created, as a file with the same "
1017 "name already exists. Try using a different name for the "
1018 "folder, or rename the file first."),
1023 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1029 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1030 _("You need to choose a valid filename."));
1033 _("Cannot create a file under %s as it is not a folder"),
1034 parent_file, error);
1037 /* Shows an error about not being able to select a folder because a file with
1038 * the same name is already there.
1041 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1045 _("You may only select folders. The item that you selected is not a folder; "
1046 "try using a different item."),
1050 /* Shows an error dialog about not being able to create a filename */
1052 error_building_filename_dialog (GtkFileChooserDefault *impl,
1055 error_dialog (impl, _("Invalid file name"),
1059 /* Shows an error dialog when we cannot switch to a folder */
1061 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1065 error_dialog (impl, _("The folder contents could not be displayed"),
1069 /* Changes folders, displaying an error dialog if this fails */
1071 change_folder_and_display_error (GtkFileChooserDefault *impl,
1073 gboolean clear_entry)
1078 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1080 /* We copy the path because of this case:
1082 * list_row_activated()
1083 * fetches path from model; path belongs to the model (*)
1084 * calls change_folder_and_display_error()
1085 * calls gtk_file_chooser_set_current_folder_file()
1086 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1090 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1093 error_changing_folder_dialog (impl, file, error);
1099 emit_default_size_changed (GtkFileChooserDefault *impl)
1101 profile_msg (" emit default-size-changed start", NULL);
1102 g_signal_emit_by_name (impl, "default-size-changed");
1103 profile_msg (" emit default-size-changed end", NULL);
1107 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1109 if (impl->use_preview_label)
1111 if (!impl->preview_label)
1113 impl->preview_label = gtk_label_new (impl->preview_display_name);
1114 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1115 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1116 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1117 gtk_widget_show (impl->preview_label);
1122 if (impl->preview_label)
1124 gtk_widget_destroy (impl->preview_label);
1125 impl->preview_label = NULL;
1129 if (impl->preview_widget_active && impl->preview_widget)
1130 gtk_widget_show (impl->preview_box);
1132 gtk_widget_hide (impl->preview_box);
1134 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1135 emit_default_size_changed (impl);
1139 set_preview_widget (GtkFileChooserDefault *impl,
1140 GtkWidget *preview_widget)
1142 if (preview_widget == impl->preview_widget)
1145 if (impl->preview_widget)
1146 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1147 impl->preview_widget);
1149 impl->preview_widget = preview_widget;
1150 if (impl->preview_widget)
1152 gtk_widget_show (impl->preview_widget);
1153 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1154 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1155 impl->preview_widget,
1156 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1159 update_preview_widget_visibility (impl);
1162 /* Renders a "Search" icon at an appropriate size for a tree view */
1164 render_search_icon (GtkFileChooserDefault *impl)
1166 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1170 render_recent_icon (GtkFileChooserDefault *impl)
1172 GtkIconTheme *theme;
1175 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1176 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1178 theme = gtk_icon_theme_get_default ();
1180 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1186 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1192 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1193 struct ReloadIconsData
1195 GtkFileChooserDefault *impl;
1196 GtkTreeRowReference *row_ref;
1200 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1202 const GError *error,
1208 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1209 struct ReloadIconsData *data = user_data;
1211 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1214 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1216 if (cancelled || error)
1219 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1221 path = gtk_tree_row_reference_get_path (data->row_ref);
1224 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1225 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1226 SHORTCUTS_COL_PIXBUF, pixbuf,
1228 gtk_tree_path_free (path);
1232 g_object_unref (pixbuf);
1235 gtk_tree_row_reference_free (data->row_ref);
1236 g_object_unref (data->impl);
1239 g_object_unref (cancellable);
1243 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1248 profile_start ("start", NULL);
1250 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1253 for (l = impl->reload_icon_cancellables; l; l = l->next)
1255 GCancellable *cancellable = G_CANCELLABLE (l->data);
1256 g_cancellable_cancel (cancellable);
1258 g_slist_free (impl->reload_icon_cancellables);
1259 impl->reload_icon_cancellables = NULL;
1264 ShortcutType shortcut_type;
1265 gboolean pixbuf_visible;
1268 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1269 SHORTCUTS_COL_DATA, &data,
1270 SHORTCUTS_COL_TYPE, &shortcut_type,
1271 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1277 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1279 GtkFileSystemVolume *volume;
1282 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1283 impl->icon_size, NULL);
1285 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1287 if (g_file_is_native (G_FILE (data)))
1290 struct ReloadIconsData *info;
1291 GtkTreePath *tree_path;
1292 GCancellable *cancellable;
1296 info = g_new0 (struct ReloadIconsData, 1);
1297 info->impl = g_object_ref (impl);
1298 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1299 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1300 gtk_tree_path_free (tree_path);
1302 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1304 shortcuts_reload_icons_get_info_cb,
1306 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1310 GtkIconTheme *icon_theme;
1312 /* Don't call get_info for remote paths to avoid latency and
1314 * If we switch to a better bookmarks file format (XBEL), we
1315 * should use mime info to get a better icon.
1317 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1318 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1319 impl->icon_size, 0, NULL);
1322 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1324 pixbuf = render_search_icon (impl);
1326 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1328 pixbuf = render_recent_icon (impl);
1331 gtk_list_store_set (impl->shortcuts_model, &iter,
1332 SHORTCUTS_COL_PIXBUF, pixbuf,
1336 g_object_unref (pixbuf);
1340 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1344 profile_end ("end", NULL);
1348 shortcuts_find_folder (GtkFileChooserDefault *impl,
1351 GtkTreeSelection *selection;
1355 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1357 g_assert (folder != NULL);
1358 pos = shortcut_find_position (impl, folder);
1361 gtk_tree_selection_unselect_all (selection);
1365 path = gtk_tree_path_new_from_indices (pos, -1);
1366 gtk_tree_selection_select_path (selection, path);
1367 gtk_tree_path_free (path);
1370 /* If a shortcut corresponds to the current folder, selects it */
1372 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1374 shortcuts_find_folder (impl, impl->current_folder);
1377 /* Removes the specified number of rows from the shortcuts list */
1379 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1385 path = gtk_tree_path_new_from_indices (start_row, -1);
1387 for (; n_rows; n_rows--)
1391 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1392 g_assert_not_reached ();
1394 shortcuts_free_row_data (impl, &iter);
1395 gtk_list_store_remove (impl->shortcuts_model, &iter);
1398 gtk_tree_path_free (path);
1402 shortcuts_update_count (GtkFileChooserDefault *impl,
1403 ShortcutsIndex type,
1408 case SHORTCUTS_HOME:
1410 impl->has_home = FALSE;
1412 impl->has_home = TRUE;
1415 case SHORTCUTS_DESKTOP:
1417 impl->has_desktop = FALSE;
1419 impl->has_desktop = TRUE;
1422 case SHORTCUTS_VOLUMES:
1423 impl->num_volumes += value;
1426 case SHORTCUTS_SHORTCUTS:
1427 impl->num_shortcuts += value;
1430 case SHORTCUTS_BOOKMARKS:
1431 impl->num_bookmarks += value;
1434 case SHORTCUTS_CURRENT_FOLDER:
1436 impl->shortcuts_current_folder_active = FALSE;
1438 impl->shortcuts_current_folder_active = TRUE;
1447 struct ShortcutsInsertRequest
1449 GtkFileChooserDefault *impl;
1453 GtkTreeRowReference *row_ref;
1454 ShortcutsIndex type;
1460 get_file_info_finished (GCancellable *cancellable,
1462 const GError *error,
1465 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1469 GCancellable *model_cancellable = NULL;
1470 struct ShortcutsInsertRequest *request = data;
1472 path = gtk_tree_row_reference_get_path (request->row_ref);
1474 /* Handle doesn't exist anymore in the model */
1477 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1479 gtk_tree_path_free (path);
1481 /* validate cancellable, else goto out */
1482 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1483 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1485 if (cancellable != model_cancellable)
1488 /* set the cancellable to NULL in the model (we unref later on) */
1489 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1490 SHORTCUTS_COL_CANCELLABLE, NULL,
1498 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1499 shortcuts_update_count (request->impl, request->type, -1);
1501 if (request->type == SHORTCUTS_HOME)
1505 home = g_file_new_for_path (g_get_home_dir ());
1506 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1507 g_object_unref (home);
1509 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1511 /* Remove the current folder separator */
1512 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1513 shortcuts_remove_rows (request->impl, separator_pos, 1);
1519 if (!request->label_copy)
1520 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1521 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1522 request->impl->icon_size);
1524 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1525 SHORTCUTS_COL_PIXBUF, pixbuf,
1526 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1527 SHORTCUTS_COL_NAME, request->label_copy,
1528 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1529 SHORTCUTS_COL_REMOVABLE, request->removable,
1532 if (request->impl->shortcuts_pane_filter_model)
1533 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1536 g_object_unref (pixbuf);
1539 g_object_unref (request->impl);
1540 g_object_unref (request->file);
1541 gtk_tree_row_reference_free (request->row_ref);
1542 g_free (request->label_copy);
1545 if (model_cancellable)
1546 g_object_unref (model_cancellable);
1549 /* FIXME: GtkFileSystem needs a function to split a remote path
1550 * into hostname and path components, or maybe just have a
1551 * gtk_file_system_path_get_display_name().
1553 * This function is also used in gtkfilechooserbutton.c
1556 _gtk_file_chooser_label_for_file (GFile *file)
1558 const gchar *path, *start, *end, *p;
1559 gchar *uri, *host, *label;
1561 uri = g_file_get_uri (file);
1563 start = strstr (uri, "://");
1567 path = strchr (start, '/');
1572 end = uri + strlen (uri);
1576 /* strip username */
1577 p = strchr (start, '@');
1581 p = strchr (start, ':');
1585 host = g_strndup (start, end - start);
1587 /* Translators: the first string is a path and the second string
1588 * is a hostname. Nautilus and the panel contain the same string
1591 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1597 label = g_strdup (uri);
1605 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1606 * inserts a volume. A position of -1 indicates the end of the tree.
1609 shortcuts_insert_file (GtkFileChooserDefault *impl,
1611 ShortcutType shortcut_type,
1612 GtkFileSystemVolume *volume,
1616 ShortcutsIndex type)
1619 GdkPixbuf *pixbuf = NULL;
1620 gpointer data = NULL;
1622 GtkIconTheme *icon_theme;
1624 profile_start ("start shortcut", NULL);
1626 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1629 label_copy = _gtk_file_system_volume_get_display_name (volume);
1630 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1631 impl->icon_size, NULL);
1633 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1635 if (g_file_is_native (file))
1637 struct ShortcutsInsertRequest *request;
1638 GCancellable *cancellable;
1641 request = g_new0 (struct ShortcutsInsertRequest, 1);
1642 request->impl = g_object_ref (impl);
1643 request->file = g_object_ref (file);
1644 request->name_only = TRUE;
1645 request->removable = removable;
1647 request->type = type;
1649 request->label_copy = g_strdup (label);
1652 gtk_list_store_append (impl->shortcuts_model, &iter);
1654 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1656 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1657 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1658 gtk_tree_path_free (p);
1660 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1661 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1662 get_file_info_finished, request);
1664 gtk_list_store_set (impl->shortcuts_model, &iter,
1665 SHORTCUTS_COL_DATA, g_object_ref (file),
1666 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1667 SHORTCUTS_COL_CANCELLABLE, cancellable,
1670 shortcuts_update_count (impl, type, 1);
1676 /* Don't call get_info for remote paths to avoid latency and
1679 data = g_object_ref (file);
1681 label_copy = g_strdup (label);
1683 label_copy = _gtk_file_chooser_label_for_file (file);
1685 /* If we switch to a better bookmarks file format (XBEL), we
1686 * should use mime info to get a better icon.
1688 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1689 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1690 impl->icon_size, 0, NULL);
1695 g_assert_not_reached ();
1701 gtk_list_store_append (impl->shortcuts_model, &iter);
1703 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1705 shortcuts_update_count (impl, type, 1);
1707 gtk_list_store_set (impl->shortcuts_model, &iter,
1708 SHORTCUTS_COL_PIXBUF, pixbuf,
1709 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1710 SHORTCUTS_COL_NAME, label_copy,
1711 SHORTCUTS_COL_DATA, data,
1712 SHORTCUTS_COL_TYPE, shortcut_type,
1713 SHORTCUTS_COL_REMOVABLE, removable,
1714 SHORTCUTS_COL_CANCELLABLE, NULL,
1717 if (impl->shortcuts_pane_filter_model)
1718 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1720 g_free (label_copy);
1723 g_object_unref (pixbuf);
1725 profile_end ("end", NULL);
1729 shortcuts_append_search (GtkFileChooserDefault *impl)
1734 pixbuf = render_search_icon (impl);
1736 gtk_list_store_append (impl->shortcuts_model, &iter);
1737 gtk_list_store_set (impl->shortcuts_model, &iter,
1738 SHORTCUTS_COL_PIXBUF, pixbuf,
1739 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1740 SHORTCUTS_COL_NAME, _("Search"),
1741 SHORTCUTS_COL_DATA, NULL,
1742 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1743 SHORTCUTS_COL_REMOVABLE, FALSE,
1747 g_object_unref (pixbuf);
1749 impl->has_search = TRUE;
1753 shortcuts_append_recent (GtkFileChooserDefault *impl)
1758 pixbuf = render_recent_icon (impl);
1760 gtk_list_store_append (impl->shortcuts_model, &iter);
1761 gtk_list_store_set (impl->shortcuts_model, &iter,
1762 SHORTCUTS_COL_PIXBUF, pixbuf,
1763 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1764 SHORTCUTS_COL_NAME, _("Recently Used"),
1765 SHORTCUTS_COL_DATA, NULL,
1766 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1767 SHORTCUTS_COL_REMOVABLE, FALSE,
1771 g_object_unref (pixbuf);
1774 /* Appends an item for the user's home directory to the shortcuts model */
1776 shortcuts_append_home (GtkFileChooserDefault *impl)
1778 const char *home_path;
1781 profile_start ("start", NULL);
1783 home_path = g_get_home_dir ();
1784 if (home_path == NULL)
1786 profile_end ("end - no home directory!?", NULL);
1790 home = g_file_new_for_path (home_path);
1791 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1792 impl->has_home = TRUE;
1794 g_object_unref (home);
1796 profile_end ("end", NULL);
1799 /* Appends the ~/Desktop directory to the shortcuts model */
1801 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1806 profile_start ("start", NULL);
1808 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1809 /* "To disable a directory, point it to the homedir."
1810 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1812 if (!g_strcmp0 (name, g_get_home_dir ()))
1814 profile_end ("end", NULL);
1818 file = g_file_new_for_path (name);
1819 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1820 impl->has_desktop = TRUE;
1822 /* We do not actually pop up an error dialog if there is no desktop directory
1823 * because some people may really not want to have one.
1826 g_object_unref (file);
1828 profile_end ("end", NULL);
1831 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1833 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1840 profile_start ("start", NULL);
1842 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1845 for (; bookmarks; bookmarks = bookmarks->next)
1849 file = bookmarks->data;
1851 if (impl->local_only && !g_file_is_native (file))
1854 if (shortcut_find_position (impl, file) != -1)
1857 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1859 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1865 profile_end ("end", NULL);
1867 return num_inserted;
1870 /* Returns the index for the corresponding item in the shortcuts bar */
1872 shortcuts_get_index (GtkFileChooserDefault *impl,
1873 ShortcutsIndex where)
1879 if (where == SHORTCUTS_SEARCH)
1882 n += impl->has_search ? 1 : 0;
1884 if (where == SHORTCUTS_RECENT)
1887 n += 1; /* we always have the recently-used item */
1889 if (where == SHORTCUTS_RECENT_SEPARATOR)
1892 n += 1; /* we always have the separator after the recently-used item */
1894 if (where == SHORTCUTS_HOME)
1897 n += impl->has_home ? 1 : 0;
1899 if (where == SHORTCUTS_DESKTOP)
1902 n += impl->has_desktop ? 1 : 0;
1904 if (where == SHORTCUTS_VOLUMES)
1907 n += impl->num_volumes;
1909 if (where == SHORTCUTS_SHORTCUTS)
1912 n += impl->num_shortcuts;
1914 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1917 /* If there are no bookmarks there won't be a separator */
1918 n += (impl->num_bookmarks > 0) ? 1 : 0;
1920 if (where == SHORTCUTS_BOOKMARKS)
1923 n += impl->num_bookmarks;
1925 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1930 if (where == SHORTCUTS_CURRENT_FOLDER)
1933 g_assert_not_reached ();
1940 /* Adds all the file system volumes to the shortcuts model */
1942 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1947 gboolean old_changing_folders;
1949 profile_start ("start", NULL);
1951 old_changing_folders = impl->changing_folder;
1952 impl->changing_folder = TRUE;
1954 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1955 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1956 impl->num_volumes = 0;
1958 list = _gtk_file_system_list_volumes (impl->file_system);
1962 for (l = list; l; l = l->next)
1964 GtkFileSystemVolume *volume;
1968 if (impl->local_only)
1970 if (_gtk_file_system_volume_is_mounted (volume))
1973 gboolean base_is_native = TRUE;
1975 base_file = _gtk_file_system_volume_get_root (volume);
1976 if (base_file != NULL)
1978 base_is_native = g_file_is_native (base_file);
1979 g_object_unref (base_file);
1982 if (!base_is_native)
1987 shortcuts_insert_file (impl,
1989 SHORTCUT_TYPE_VOLUME,
1990 _gtk_file_system_volume_ref (volume),
1998 impl->num_volumes = n;
1999 g_slist_free (list);
2001 if (impl->shortcuts_pane_filter_model)
2002 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2004 impl->changing_folder = old_changing_folders;
2006 profile_end ("end", NULL);
2009 /* Inserts a separator node in the shortcuts list */
2011 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2012 ShortcutsIndex where)
2016 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2017 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2018 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2020 gtk_list_store_insert (impl->shortcuts_model, &iter,
2021 shortcuts_get_index (impl, where));
2022 gtk_list_store_set (impl->shortcuts_model, &iter,
2023 SHORTCUTS_COL_PIXBUF, NULL,
2024 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2025 SHORTCUTS_COL_NAME, NULL,
2026 SHORTCUTS_COL_DATA, NULL,
2027 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2031 /* Updates the list of bookmarks */
2033 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2036 gboolean old_changing_folders;
2038 GFile *list_selected = NULL;
2039 ShortcutType shortcut_type;
2042 profile_start ("start", NULL);
2044 old_changing_folders = impl->changing_folder;
2045 impl->changing_folder = TRUE;
2047 if (shortcuts_get_selected (impl, &iter))
2049 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2051 SHORTCUTS_COL_DATA, &col_data,
2052 SHORTCUTS_COL_TYPE, &shortcut_type,
2055 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2056 list_selected = g_object_ref (col_data);
2059 if (impl->num_bookmarks > 0)
2060 shortcuts_remove_rows (impl,
2061 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2062 impl->num_bookmarks + 1);
2064 impl->num_bookmarks = 0;
2065 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2067 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2068 shortcuts_append_bookmarks (impl, bookmarks);
2069 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2070 g_slist_free (bookmarks);
2072 if (impl->num_bookmarks == 0)
2073 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2075 if (impl->shortcuts_pane_filter_model)
2076 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2080 shortcuts_find_folder (impl, list_selected);
2081 g_object_unref (list_selected);
2084 impl->changing_folder = old_changing_folders;
2086 profile_end ("end", NULL);
2089 /* Appends a separator and a row to the shortcuts list for the current folder */
2091 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2095 g_assert (!impl->shortcuts_current_folder_active);
2097 g_assert (impl->current_folder != NULL);
2099 pos = shortcut_find_position (impl, impl->current_folder);
2102 GtkFileSystemVolume *volume;
2106 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2109 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2111 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2113 base_file = _gtk_file_system_volume_get_root (volume);
2117 if (base_file && g_file_equal (base_file, impl->current_folder))
2118 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2120 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2123 g_object_unref (base_file);
2127 /* Updates the current folder row in the shortcuts model */
2129 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2133 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2135 if (impl->shortcuts_current_folder_active)
2137 shortcuts_remove_rows (impl, pos, 2);
2138 impl->shortcuts_current_folder_active = FALSE;
2141 shortcuts_add_current_folder (impl);
2144 /* Filter function used for the shortcuts filter model */
2146 shortcuts_pane_filter_cb (GtkTreeModel *model,
2150 GtkFileChooserDefault *impl;
2154 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2156 path = gtk_tree_model_get_path (model, iter);
2160 pos = *gtk_tree_path_get_indices (path);
2161 gtk_tree_path_free (path);
2163 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2166 /* Creates the list model for shortcuts */
2168 shortcuts_model_create (GtkFileChooserDefault *impl)
2170 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2171 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2172 GDK_TYPE_PIXBUF, /* pixbuf */
2173 G_TYPE_STRING, /* name */
2174 G_TYPE_POINTER, /* path or volume */
2175 G_TYPE_INT, /* ShortcutType */
2176 G_TYPE_BOOLEAN, /* removable */
2177 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2178 G_TYPE_POINTER); /* GCancellable */
2180 shortcuts_append_search (impl);
2182 if (impl->recent_manager)
2184 shortcuts_append_recent (impl);
2185 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2188 if (impl->file_system)
2190 shortcuts_append_home (impl);
2191 shortcuts_append_desktop (impl);
2192 shortcuts_add_volumes (impl);
2195 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2196 GTK_TREE_MODEL (impl->shortcuts_model),
2199 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2200 shortcuts_pane_filter_cb,
2205 /* Callback used when the "New Folder" button is clicked */
2207 new_folder_button_clicked (GtkButton *button,
2208 GtkFileChooserDefault *impl)
2213 if (!impl->browse_files_model)
2214 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2216 /* Prevent button from being clicked twice */
2217 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2219 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2221 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2222 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2223 path, impl->list_name_column,
2226 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2227 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2229 impl->list_name_column,
2232 gtk_tree_path_free (path);
2236 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2240 source = g_idle_source_new ();
2241 g_source_set_closure (source,
2242 g_cclosure_new_object (callback, G_OBJECT (impl)));
2243 g_source_attach (source, NULL);
2248 /* Idle handler for creating a new folder after editing its name cell, or for
2249 * canceling the editing.
2252 edited_idle_cb (GtkFileChooserDefault *impl)
2254 gdk_threads_enter ();
2256 g_source_destroy (impl->edited_idle);
2257 impl->edited_idle = NULL;
2259 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2260 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2262 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2264 if (impl->edited_new_text /* not cancelled? */
2265 && (strlen (impl->edited_new_text) != 0)
2266 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2268 GError *error = NULL;
2271 file = g_file_get_child_for_display_name (impl->current_folder,
2272 impl->edited_new_text,
2276 GError *error = NULL;
2278 if (g_file_make_directory (file, NULL, &error))
2279 change_folder_and_display_error (impl, file, FALSE);
2281 error_creating_folder_dialog (impl, file, error);
2283 g_object_unref (file);
2286 error_creating_folder_dialog (impl, file, error);
2288 g_free (impl->edited_new_text);
2289 impl->edited_new_text = NULL;
2292 gdk_threads_leave ();
2298 queue_edited_idle (GtkFileChooserDefault *impl,
2299 const gchar *new_text)
2301 /* We create the folder in an idle handler so that we don't modify the tree
2305 if (!impl->edited_idle)
2306 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2308 g_free (impl->edited_new_text);
2309 impl->edited_new_text = g_strdup (new_text);
2312 /* Callback used from the text cell renderer when the new folder is named */
2314 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2316 const gchar *new_text,
2317 GtkFileChooserDefault *impl)
2319 /* work around bug #154921 */
2320 g_object_set (cell_renderer_text,
2321 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2322 queue_edited_idle (impl, new_text);
2325 /* Callback used from the text cell renderer when the new folder edition gets
2329 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2330 GtkFileChooserDefault *impl)
2332 /* work around bug #154921 */
2333 g_object_set (cell_renderer_text,
2334 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2335 queue_edited_idle (impl, NULL);
2338 /* Creates the widgets for the filter combo box */
2340 filter_create (GtkFileChooserDefault *impl)
2342 GtkCellRenderer *cell;
2345 impl->filter_combo = gtk_combo_box_text_new ();
2346 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2348 /* Get the combo's text renderer and set ellipsize parameters */
2349 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2353 g_object_set (G_OBJECT (cell),
2354 "ellipsize", PANGO_ELLIPSIZE_END,
2357 g_list_free (cells);
2359 g_signal_connect (impl->filter_combo, "changed",
2360 G_CALLBACK (filter_combo_changed), impl);
2362 gtk_widget_set_tooltip_text (impl->filter_combo,
2363 _("Select which types of files are shown"));
2365 return impl->filter_combo;
2369 toolbutton_new (GtkFileChooserDefault *impl,
2378 item = gtk_tool_button_new (NULL, NULL);
2379 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2380 gtk_widget_show (image);
2381 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2383 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2384 g_signal_connect (item, "clicked", callback, impl);
2387 gtk_widget_show (GTK_WIDGET (item));
2389 return GTK_WIDGET (item);
2392 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2394 shortcut_find_position (GtkFileChooserDefault *impl,
2399 int current_folder_separator_idx;
2401 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2404 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2407 /* FIXME: is this still needed? */
2408 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2412 for (i = 0; i < current_folder_separator_idx; i++)
2415 ShortcutType shortcut_type;
2417 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2418 SHORTCUTS_COL_DATA, &col_data,
2419 SHORTCUTS_COL_TYPE, &shortcut_type,
2424 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2426 GtkFileSystemVolume *volume;
2431 base_file = _gtk_file_system_volume_get_root (volume);
2433 exists = base_file && g_file_equal (file, base_file);
2436 g_object_unref (base_file);
2441 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2445 model_file = col_data;
2447 if (model_file && g_file_equal (model_file, file))
2452 if (i < current_folder_separator_idx - 1)
2454 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2455 g_assert_not_reached ();
2462 /* Tries to add a bookmark from a path name */
2464 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2470 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2472 if (shortcut_find_position (impl, file) != -1)
2476 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2478 error_adding_bookmark_dialog (impl, file, error);
2486 add_bookmark_foreach_cb (GtkTreeModel *model,
2491 GtkFileChooserDefault *impl;
2494 impl = (GtkFileChooserDefault *) data;
2496 gtk_tree_model_get (model, iter,
2497 MODEL_COL_FILE, &file,
2500 shortcuts_add_bookmark_from_file (impl, file, -1);
2502 g_object_unref (file);
2505 /* Adds a bookmark from the currently selected item in the file list */
2507 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2509 GtkTreeSelection *selection;
2511 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2513 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2514 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2516 gtk_tree_selection_selected_foreach (selection,
2517 add_bookmark_foreach_cb,
2521 /* Callback used when the "Add bookmark" button is clicked */
2523 add_bookmark_button_clicked_cb (GtkButton *button,
2524 GtkFileChooserDefault *impl)
2526 bookmarks_add_selected_folder (impl);
2529 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2530 * returns FALSE if no shortcut is selected.
2533 shortcuts_get_selected (GtkFileChooserDefault *impl,
2536 GtkTreeSelection *selection;
2537 GtkTreeIter parent_iter;
2539 if (!impl->browse_shortcuts_tree_view)
2542 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2544 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2547 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2553 /* Removes the selected bookmarks */
2555 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2563 if (!shortcuts_get_selected (impl, &iter))
2566 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2567 SHORTCUTS_COL_DATA, &col_data,
2568 SHORTCUTS_COL_REMOVABLE, &removable,
2574 g_assert (col_data != NULL);
2579 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2580 error_removing_bookmark_dialog (impl, file, error);
2583 /* Callback used when the "Remove bookmark" button is clicked */
2585 remove_bookmark_button_clicked_cb (GtkButton *button,
2586 GtkFileChooserDefault *impl)
2588 remove_selected_bookmarks (impl);
2591 struct selection_check_closure {
2592 GtkFileChooserDefault *impl;
2595 gboolean all_folders;
2598 /* Used from gtk_tree_selection_selected_foreach() */
2600 selection_check_foreach_cb (GtkTreeModel *model,
2605 struct selection_check_closure *closure;
2609 gtk_tree_model_get (model, iter,
2610 MODEL_COL_FILE, &file,
2611 MODEL_COL_IS_FOLDER, &is_folder,
2617 g_object_unref (file);
2620 closure->num_selected++;
2622 closure->all_folders = closure->all_folders && is_folder;
2623 closure->all_files = closure->all_files && !is_folder;
2626 /* Checks whether the selected items in the file list are all files or all folders */
2628 selection_check (GtkFileChooserDefault *impl,
2630 gboolean *all_files,
2631 gboolean *all_folders)
2633 struct selection_check_closure closure;
2634 GtkTreeSelection *selection;
2636 closure.impl = impl;
2637 closure.num_selected = 0;
2638 closure.all_files = TRUE;
2639 closure.all_folders = TRUE;
2641 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2642 gtk_tree_selection_selected_foreach (selection,
2643 selection_check_foreach_cb,
2646 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2649 *num_selected = closure.num_selected;
2652 *all_files = closure.all_files;
2655 *all_folders = closure.all_folders;
2658 struct get_selected_file_closure {
2659 GtkFileChooserDefault *impl;
2664 get_selected_file_foreach_cb (GtkTreeModel *model,
2669 struct get_selected_file_closure *closure = data;
2673 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2674 g_object_unref (closure->file);
2675 closure->file = NULL;
2678 gtk_tree_model_get (model, iter,
2679 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2683 /* Returns a selected path from the file list */
2685 get_selected_file (GtkFileChooserDefault *impl)
2687 struct get_selected_file_closure closure;
2688 GtkTreeSelection *selection;
2690 closure.impl = impl;
2691 closure.file = NULL;
2693 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2694 gtk_tree_selection_selected_foreach (selection,
2695 get_selected_file_foreach_cb,
2698 return closure.file;
2702 GtkFileChooserDefault *impl;
2704 } UpdateTooltipData;
2707 update_tooltip (GtkTreeModel *model,
2712 UpdateTooltipData *udata = data;
2714 if (udata->tip == NULL)
2716 gchar *display_name;
2718 gtk_tree_model_get (model, iter,
2719 MODEL_COL_NAME, &display_name,
2722 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2724 g_free (display_name);
2729 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2730 * if there are no selected items *and* the current folder is not in the
2731 * bookmarks list. De-sensitize the button otherwise.
2734 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2737 gboolean all_folders;
2741 selection_check (impl, &num_selected, NULL, &all_folders);
2743 if (num_selected == 0)
2744 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2745 else if (num_selected == 1)
2749 file = get_selected_file (impl);
2750 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2752 g_object_unref (file);
2755 active = all_folders;
2757 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2759 if (impl->browse_files_popup_menu_add_shortcut_item)
2760 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2761 (num_selected == 0) ? FALSE : active);
2765 if (num_selected == 0)
2766 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2767 else if (num_selected > 1)
2768 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2771 GtkTreeSelection *selection;
2772 UpdateTooltipData data;
2774 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2777 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2781 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2786 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2787 * bookmark row is selected in the shortcuts tree.
2790 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2793 gboolean removable = FALSE;
2797 if (shortcuts_get_selected (impl, &iter))
2799 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2800 SHORTCUTS_COL_REMOVABLE, &removable,
2801 SHORTCUTS_COL_NAME, &name,
2803 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2806 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2808 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2810 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2814 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2815 _("Remove the selected bookmark"));
2820 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2823 gboolean removable = FALSE;
2825 if (impl->browse_shortcuts_popup_menu == NULL)
2828 if (shortcuts_get_selected (impl, &iter))
2829 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2830 SHORTCUTS_COL_REMOVABLE, &removable,
2833 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2834 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2837 /* GtkWidget::drag-begin handler for the shortcuts list. */
2839 shortcuts_drag_begin_cb (GtkWidget *widget,
2840 GdkDragContext *context,
2841 GtkFileChooserDefault *impl)
2844 impl->shortcuts_drag_context = g_object_ref (context);
2849 /* Removes the idle handler for outside drags */
2851 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2853 if (!impl->shortcuts_drag_outside_idle)
2856 g_source_destroy (impl->shortcuts_drag_outside_idle);
2857 impl->shortcuts_drag_outside_idle = NULL;
2861 /* GtkWidget::drag-end handler for the shortcuts list. */
2863 shortcuts_drag_end_cb (GtkWidget *widget,
2864 GdkDragContext *context,
2865 GtkFileChooserDefault *impl)
2868 g_object_unref (impl->shortcuts_drag_context);
2870 shortcuts_cancel_drag_outside_idle (impl);
2872 if (!impl->shortcuts_drag_outside)
2875 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2877 impl->shortcuts_drag_outside = FALSE;
2881 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2883 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2884 GdkDragContext *context,
2885 GtkFileChooserDefault *impl)
2887 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2890 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2894 shortcuts_drag_leave_cb (GtkWidget *widget,
2895 GdkDragContext *context,
2897 GtkFileChooserDefault *impl)
2900 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2902 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2906 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2908 GTK_TREE_VIEW_DROP_BEFORE);
2910 g_signal_stop_emission_by_name (widget, "drag-leave");
2913 /* Computes the appropriate row and position for dropping */
2915 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2919 GtkTreeViewDropPosition *pos)
2921 GtkTreeView *tree_view;
2922 GtkTreeViewColumn *column;
2926 int bookmarks_index;
2927 int header_height = 0;
2929 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2931 if (gtk_tree_view_get_headers_visible (tree_view))
2932 header_height = _gtk_tree_view_get_header_height (tree_view);
2934 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2936 if (!gtk_tree_view_get_path_at_pos (tree_view,
2944 row = bookmarks_index + impl->num_bookmarks - 1;
2945 *path = gtk_tree_path_new_from_indices (row, -1);
2946 *pos = GTK_TREE_VIEW_DROP_AFTER;
2950 row = *gtk_tree_path_get_indices (*path);
2951 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2952 gtk_tree_path_free (*path);
2954 if (row < bookmarks_index)
2956 row = bookmarks_index;
2957 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2959 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2961 row = bookmarks_index + impl->num_bookmarks - 1;
2962 *pos = GTK_TREE_VIEW_DROP_AFTER;
2966 if (cell_y < cell.height / 2)
2967 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2969 *pos = GTK_TREE_VIEW_DROP_AFTER;
2972 *path = gtk_tree_path_new_from_indices (row, -1);
2975 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2976 * implement the destination side of DnD by hand, due to limitations in
2977 * GtkTreeView's DnD API.
2980 shortcuts_drag_motion_cb (GtkWidget *widget,
2981 GdkDragContext *context,
2985 GtkFileChooserDefault *impl)
2988 GtkTreeViewDropPosition pos;
2989 GdkDragAction action;
2992 if (gtk_drag_get_source_widget (context) == widget)
2994 shortcuts_cancel_drag_outside_idle (impl);
2996 if (impl->shortcuts_drag_outside)
2998 shortcuts_drag_set_delete_cursor (impl, FALSE);
2999 impl->shortcuts_drag_outside = FALSE;
3004 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3005 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3006 action = GDK_ACTION_COPY;
3007 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3008 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3009 action = GDK_ACTION_MOVE;
3016 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3017 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3018 gtk_tree_path_free (path);
3022 g_signal_stop_emission_by_name (widget, "drag-motion");
3026 gdk_drag_status (context, action, time_);
3033 /* GtkWidget::drag-drop handler for the shortcuts list. */
3035 shortcuts_drag_drop_cb (GtkWidget *widget,
3036 GdkDragContext *context,
3040 GtkFileChooserDefault *impl)
3043 shortcuts_cancel_drag_outside_idle (impl);
3046 g_signal_stop_emission_by_name (widget, "drag-drop");
3050 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3052 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3053 GtkSelectionData *selection_data,
3059 uris = gtk_selection_data_get_uris (selection_data);
3063 for (i = 0; uris[i]; i++)
3069 file = g_file_new_for_uri (uri);
3071 if (shortcuts_add_bookmark_from_file (impl, file, position))
3074 g_object_unref (file);
3080 /* Reorders the selected bookmark to the specified position */
3082 shortcuts_reorder (GtkFileChooserDefault *impl,
3087 ShortcutType shortcut_type;
3090 int bookmarks_index;
3095 /* Get the selected path */
3097 if (!shortcuts_get_selected (impl, &iter))
3098 g_assert_not_reached ();
3100 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3101 old_position = *gtk_tree_path_get_indices (path);
3102 gtk_tree_path_free (path);
3104 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3105 old_position -= bookmarks_index;
3106 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3108 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3109 SHORTCUTS_COL_NAME, &name,
3110 SHORTCUTS_COL_DATA, &col_data,
3111 SHORTCUTS_COL_TYPE, &shortcut_type,
3113 g_assert (col_data != NULL);
3114 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3117 g_object_ref (file); /* removal below will free file, so we need a new ref */
3119 /* Remove the path from the old position and insert it in the new one */
3121 if (new_position > old_position)
3124 if (old_position == new_position)
3128 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3130 shortcuts_add_bookmark_from_file (impl, file, new_position);
3131 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3134 error_adding_bookmark_dialog (impl, file, error);
3138 g_object_unref (file);
3142 /* Callback used when we get the drag data for the bookmarks list. We add the
3143 * received URIs as bookmarks if they are folders.
3146 shortcuts_drag_data_received_cb (GtkWidget *widget,
3147 GdkDragContext *context,
3150 GtkSelectionData *selection_data,
3155 GtkFileChooserDefault *impl;
3156 GtkTreePath *tree_path;
3157 GtkTreeViewDropPosition tree_pos;
3160 int bookmarks_index;
3162 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3164 /* Compute position */
3166 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3168 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3169 position = *gtk_tree_path_get_indices (tree_path);
3170 gtk_tree_path_free (tree_path);
3172 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3175 g_assert (position >= bookmarks_index);
3176 position -= bookmarks_index;
3178 target = gtk_selection_data_get_target (selection_data);
3180 if (gtk_targets_include_uri (&target, 1))
3181 shortcuts_drop_uris (impl, selection_data, position);
3182 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3183 shortcuts_reorder (impl, position);
3185 g_signal_stop_emission_by_name (widget, "drag-data-received");
3188 /* Callback used to display a tooltip in the shortcuts tree */
3190 shortcuts_query_tooltip_cb (GtkWidget *widget,
3193 gboolean keyboard_mode,
3194 GtkTooltip *tooltip,
3195 GtkFileChooserDefault *impl)
3197 GtkTreeModel *model;
3200 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3208 ShortcutType shortcut_type;
3210 gtk_tree_model_get (model, &iter,
3211 SHORTCUTS_COL_DATA, &col_data,
3212 SHORTCUTS_COL_TYPE, &shortcut_type,
3215 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3217 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3219 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3224 file = G_FILE (col_data);
3225 parse_name = g_file_get_parse_name (file);
3227 gtk_tooltip_set_text (tooltip, parse_name);
3229 g_free (parse_name);
3233 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3237 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3247 /* Callback used when the selection in the shortcuts tree changes */
3249 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3250 GtkFileChooserDefault *impl)
3253 GtkTreeIter child_iter;
3255 bookmarks_check_remove_sensitivity (impl);
3256 shortcuts_check_popup_sensitivity (impl);
3258 if (impl->changing_folder)
3261 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3263 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3266 shortcuts_activate_iter (impl, &child_iter);
3271 shortcuts_row_separator_func (GtkTreeModel *model,
3275 ShortcutType shortcut_type;
3277 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3279 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3283 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3285 GtkFileChooserDefault *impl)
3289 /* don't screw up focus switching with Tab */
3290 if (event->keyval == GDK_KEY_Tab
3291 || event->keyval == GDK_KEY_KP_Tab
3292 || event->keyval == GDK_KEY_ISO_Left_Tab
3293 || event->length < 1)
3296 if (impl->location_entry)
3297 entry = impl->location_entry;
3298 else if (impl->search_entry)
3299 entry = impl->search_entry;
3305 gtk_widget_grab_focus (entry);
3306 return gtk_widget_event (entry, (GdkEvent *) event);
3312 /* Callback used when the file list's popup menu is detached */
3314 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3317 GtkFileChooserDefault *impl;
3319 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3320 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3322 impl->browse_shortcuts_popup_menu = NULL;
3323 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3324 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3328 remove_shortcut_cb (GtkMenuItem *item,
3329 GtkFileChooserDefault *impl)
3331 remove_selected_bookmarks (impl);
3334 /* Rename the selected bookmark */
3336 rename_selected_bookmark (GtkFileChooserDefault *impl)
3340 GtkTreeViewColumn *column;
3341 GtkCellRenderer *cell;
3344 if (shortcuts_get_selected (impl, &iter))
3346 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3347 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3348 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3349 cell = g_list_nth_data (renderers, 1);
3350 g_list_free (renderers);
3351 g_object_set (cell, "editable", TRUE, NULL);
3352 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3353 path, column, cell, TRUE);
3354 gtk_tree_path_free (path);
3359 rename_shortcut_cb (GtkMenuItem *item,
3360 GtkFileChooserDefault *impl)
3362 rename_selected_bookmark (impl);
3365 /* Constructs the popup menu for the file list if needed */
3367 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3371 if (impl->browse_shortcuts_popup_menu)
3374 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3375 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3376 impl->browse_shortcuts_tree_view,
3377 shortcuts_popup_menu_detach_cb);
3379 item = gtk_image_menu_item_new_with_label (_("Remove"));
3380 impl->browse_shortcuts_popup_menu_remove_item = item;
3381 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3382 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3383 g_signal_connect (item, "activate",
3384 G_CALLBACK (remove_shortcut_cb), impl);
3385 gtk_widget_show (item);
3386 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3388 item = gtk_menu_item_new_with_label (_("Rename..."));
3389 impl->browse_shortcuts_popup_menu_rename_item = item;
3390 g_signal_connect (item, "activate",
3391 G_CALLBACK (rename_shortcut_cb), impl);
3392 gtk_widget_show (item);
3393 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3397 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3399 shortcuts_build_popup_menu (impl);
3400 shortcuts_check_popup_sensitivity (impl);
3404 popup_position_func (GtkMenu *menu,
3408 gpointer user_data);
3411 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3412 GdkEventButton *event)
3414 shortcuts_update_popup_menu (impl);
3416 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3417 NULL, NULL, NULL, NULL,
3418 event->button, event->time);
3421 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3423 popup_position_func, impl->browse_shortcuts_tree_view,
3424 0, GDK_CURRENT_TIME);
3425 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3430 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3432 shortcuts_popup_menu_cb (GtkWidget *widget,
3433 GtkFileChooserDefault *impl)
3435 shortcuts_popup_menu (impl, NULL);
3439 /* Callback used when a button is pressed on the shortcuts list.
3440 * We trap button 3 to bring up a popup menu.
3443 shortcuts_button_press_event_cb (GtkWidget *widget,
3444 GdkEventButton *event,
3445 GtkFileChooserDefault *impl)
3447 static gboolean in_press = FALSE;
3453 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
3457 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3463 shortcuts_popup_menu (impl, event);
3468 shortcuts_edited (GtkCellRenderer *cell,
3471 GtkFileChooserDefault *impl)
3477 g_object_set (cell, "editable", FALSE, NULL);
3479 path = gtk_tree_path_new_from_string (path_string);
3480 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3481 g_assert_not_reached ();
3483 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3484 SHORTCUTS_COL_DATA, &shortcut,
3486 gtk_tree_path_free (path);
3488 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3492 shortcuts_editing_canceled (GtkCellRenderer *cell,
3493 GtkFileChooserDefault *impl)
3495 g_object_set (cell, "editable", FALSE, NULL);
3498 /* Creates the widgets for the shortcuts and bookmarks tree */
3500 shortcuts_list_create (GtkFileChooserDefault *impl)
3503 GtkTreeSelection *selection;
3504 GtkTreeViewColumn *column;
3505 GtkCellRenderer *renderer;
3507 /* Target types for dragging a row to/from the shortcuts list */
3508 const GtkTargetEntry tree_model_row_targets[] = {
3509 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3512 /* Scrolled window */
3514 swin = gtk_scrolled_window_new (NULL, NULL);
3515 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3516 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3517 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3519 gtk_widget_show (swin);
3522 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3523 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3524 GTK_STYLE_CLASS_SIDEBAR);
3525 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3526 #ifdef PROFILE_FILE_CHOOSER
3527 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3530 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3532 * 1. user brings up a SAVE dialog
3533 * 2. user clicks on a shortcut in the shortcuts pane
3534 * 3. user starts typing a filename
3536 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3537 * support interactive search. However, we'd rather focus the location entry
3538 * so that the user can type *there*.
3540 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3541 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3542 * if the user starts typing while the focus is in the shortcuts pane.
3544 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3545 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3547 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3548 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3549 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3550 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3551 /* Accessible object name for the file chooser's shortcuts pane */
3552 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3554 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3556 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3558 tree_model_row_targets,
3559 G_N_ELEMENTS (tree_model_row_targets),
3562 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3563 GTK_DEST_DEFAULT_ALL,
3564 tree_model_row_targets,
3565 G_N_ELEMENTS (tree_model_row_targets),
3566 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3567 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3569 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3570 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3571 gtk_tree_selection_set_select_function (selection,
3572 shortcuts_select_func,
3575 g_signal_connect (selection, "changed",
3576 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3578 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3579 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3581 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3582 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3583 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3584 G_CALLBACK (shortcuts_drag_end_cb), impl);
3585 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3586 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3588 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3589 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3590 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3591 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3592 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3593 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3594 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3595 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3597 /* Support tooltips */
3598 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3599 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3600 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3602 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3603 gtk_widget_show (impl->browse_shortcuts_tree_view);
3607 column = gtk_tree_view_column_new ();
3608 /* Column header for the file chooser's shortcuts pane */
3609 gtk_tree_view_column_set_title (column, _("_Places"));
3611 renderer = gtk_cell_renderer_pixbuf_new ();
3612 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3613 gtk_tree_view_column_set_attributes (column, renderer,
3614 "pixbuf", SHORTCUTS_COL_PIXBUF,
3615 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3618 renderer = gtk_cell_renderer_text_new ();
3619 g_object_set (renderer,
3621 "ellipsize", PANGO_ELLIPSIZE_END,
3623 g_signal_connect (renderer, "edited",
3624 G_CALLBACK (shortcuts_edited), impl);
3625 g_signal_connect (renderer, "editing-canceled",
3626 G_CALLBACK (shortcuts_editing_canceled), impl);
3627 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3628 gtk_tree_view_column_set_attributes (column, renderer,
3629 "text", SHORTCUTS_COL_NAME,
3632 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3633 shortcuts_row_separator_func,
3636 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3641 /* Creates the widgets for the shortcuts/bookmarks pane */
3643 shortcuts_pane_create (GtkFileChooserDefault *impl,
3644 GtkSizeGroup *size_group)
3649 GtkStyleContext *context;
3652 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3653 gtk_widget_show (vbox);
3655 /* Shortcuts tree */
3657 widget = shortcuts_list_create (impl);
3659 gtk_size_group_add_widget (size_group, widget);
3660 context = gtk_widget_get_style_context (widget);
3661 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3663 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3665 /* Box for buttons */
3667 toolbar = gtk_toolbar_new ();
3668 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
3669 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3671 context = gtk_widget_get_style_context (toolbar);
3672 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3673 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3675 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3676 gtk_widget_show (toolbar);
3678 /* Add bookmark button */
3679 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3680 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3684 G_CALLBACK (add_bookmark_button_clicked_cb));
3685 g_object_unref (icon);
3687 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3688 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3689 _("Add the selected folder to the Bookmarks"));
3691 /* Remove bookmark button */
3692 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3693 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3697 G_CALLBACK (remove_bookmark_button_clicked_cb));
3698 g_object_unref (icon);
3699 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3700 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3701 _("Remove the selected bookmark"));
3707 key_is_left_or_right (GdkEventKey *event)
3711 modifiers = gtk_accelerator_get_default_mod_mask ();
3713 return ((event->keyval == GDK_KEY_Right
3714 || event->keyval == GDK_KEY_KP_Right
3715 || event->keyval == GDK_KEY_Left
3716 || event->keyval == GDK_KEY_KP_Left)
3717 && (event->state & modifiers) == 0);
3720 /* Handles key press events on the file list, so that we can trap Enter to
3721 * activate the default button on our own. Also, checks to see if '/' has been
3725 browse_files_key_press_event_cb (GtkWidget *widget,
3729 GtkFileChooserDefault *impl;
3730 GdkModifierType no_text_input_mask;
3732 impl = (GtkFileChooserDefault *) data;
3734 no_text_input_mask =
3735 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
3737 if ((event->keyval == GDK_KEY_slash
3738 || event->keyval == GDK_KEY_KP_Divide
3740 || event->keyval == GDK_KEY_asciitilde
3742 ) && !(event->state & no_text_input_mask))
3744 location_popup_handler (impl, event->string);
3748 if (key_is_left_or_right (event))
3750 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3754 if ((event->keyval == GDK_KEY_Return
3755 || event->keyval == GDK_KEY_ISO_Enter
3756 || event->keyval == GDK_KEY_KP_Enter
3757 || event->keyval == GDK_KEY_space
3758 || event->keyval == GDK_KEY_KP_Space)
3759 && !(event->state & gtk_accelerator_get_default_mod_mask ())
3760 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3761 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3765 window = get_toplevel (widget);
3768 GtkWidget *default_widget, *focus_widget;
3770 default_widget = gtk_window_get_default_widget (window);
3771 focus_widget = gtk_window_get_focus (window);
3773 if (widget != default_widget &&
3774 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3776 gtk_window_activate_default (window);
3786 /* Callback used when the file list's popup menu is detached */
3788 popup_menu_detach_cb (GtkWidget *attach_widget,
3791 GtkFileChooserDefault *impl;
3793 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3794 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3796 impl->browse_files_popup_menu = NULL;
3797 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3798 impl->browse_files_popup_menu_hidden_files_item = NULL;
3799 impl->browse_files_popup_menu_copy_file_location_item = NULL;
3802 /* Callback used when the "Add to Bookmarks" menu item is activated */
3804 add_to_shortcuts_cb (GtkMenuItem *item,
3805 GtkFileChooserDefault *impl)
3807 bookmarks_add_selected_folder (impl);
3810 /* callback used to set data to clipboard */
3812 copy_file_get_cb (GtkClipboard *clipboard,
3813 GtkSelectionData *selection_data,
3817 GSList *selected_files = data;
3821 gint num_files = g_slist_length (selected_files);
3826 uris = g_new (gchar *, num_files + 1);
3827 uris[num_files] = NULL; /* null terminator */
3831 for (l = selected_files; l; l = l->next)
3833 GFile *file = (GFile *) l->data;
3835 if (info == SELECTION_URI)
3836 uris[i] = g_file_get_uri (file);
3837 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3838 uris[i] = g_file_get_parse_name (file);
3843 if (info == SELECTION_URI)
3844 gtk_selection_data_set_uris (selection_data, uris);
3845 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3847 char *str = g_strjoinv (" ", uris);
3848 gtk_selection_data_set_text (selection_data, str, -1);
3856 /* callback used to clear the clipboard data */
3858 copy_file_clear_cb (GtkClipboard *clipboard,
3861 GSList *selected_files = data;
3863 g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3864 g_slist_free (selected_files);
3867 /* Callback used when the "Copy file's location" menu item is activated */
3869 copy_file_location_cb (GtkMenuItem *item,
3870 GtkFileChooserDefault *impl)
3872 GSList *selected_files = NULL;
3874 selected_files = search_get_selected_files (impl);
3878 GtkClipboard *clipboard;
3879 GtkTargetList *target_list;
3880 GtkTargetEntry *targets;
3883 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3885 target_list = gtk_target_list_new (NULL, 0);
3886 gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3887 gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3889 targets = gtk_target_table_new_from_list (target_list, &n_targets);
3890 gtk_target_list_unref (target_list);
3892 gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3897 gtk_target_table_free (targets, n_targets);
3901 /* Callback used when the "Visit this file" menu item is activated */
3903 visit_file_cb (GtkMenuItem *item,
3904 GtkFileChooserDefault *impl)
3908 files = search_get_selected_files (impl);
3910 /* Sigh, just use the first one */
3913 GFile *file = files->data;
3915 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL); /* NULL-GError */
3918 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
3919 g_slist_free (files);
3922 /* callback used when the "Show Hidden Files" menu item is toggled */
3924 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3925 GtkFileChooserDefault *impl)
3928 "show-hidden", gtk_check_menu_item_get_active (item),
3932 /* Callback used when the "Show Size Column" menu item is toggled */
3934 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3935 GtkFileChooserDefault *impl)
3937 impl->show_size_column = gtk_check_menu_item_get_active (item);
3939 gtk_tree_view_column_set_visible (impl->list_size_column,
3940 impl->show_size_column);
3943 /* Shows an error dialog about not being able to select a dragged file */
3945 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3950 _("Could not select file"),
3955 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3960 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3962 for (i = 1; uris[i]; i++)
3965 GError *error = NULL;
3968 file = g_file_new_for_uri (uri);
3970 gtk_file_chooser_default_select_file (chooser, file, &error);
3972 error_selecting_dragged_file_dialog (impl, file, error);
3974 g_object_unref (file);
3978 struct FileListDragData
3980 GtkFileChooserDefault *impl;
3986 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3988 const GError *error,
3991 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3992 struct FileListDragData *data = user_data;
3993 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3995 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3998 data->impl->file_list_drag_data_received_cancellable = NULL;
4000 if (cancelled || error)
4003 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4004 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4005 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4006 change_folder_and_display_error (data->impl, data->file, FALSE);
4009 GError *error = NULL;
4011 gtk_file_chooser_default_unselect_all (chooser);
4012 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4014 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4016 browse_files_center_selected_row (data->impl);
4019 if (data->impl->select_multiple)
4020 file_list_drag_data_select_uris (data->impl, data->uris);
4023 g_object_unref (data->impl);
4024 g_strfreev (data->uris);
4025 g_object_unref (data->file);
4028 g_object_unref (cancellable);
4032 file_list_drag_data_received_cb (GtkWidget *widget,
4033 GdkDragContext *context,
4036 GtkSelectionData *selection_data,
4041 GtkFileChooserDefault *impl;
4046 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4048 /* Allow only drags from other widgets; see bug #533891. */
4049 if (gtk_drag_get_source_widget (context) == widget)
4051 g_signal_stop_emission_by_name (widget, "drag-data-received");
4055 /* Parse the text/uri-list string, navigate to the first one */
4056 uris = gtk_selection_data_get_uris (selection_data);
4057 if (uris && uris[0])
4059 struct FileListDragData *data;
4062 file = g_file_new_for_uri (uri);
4064 data = g_new0 (struct FileListDragData, 1);
4065 data->impl = g_object_ref (impl);
4069 if (impl->file_list_drag_data_received_cancellable)
4070 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4072 impl->file_list_drag_data_received_cancellable =
4073 _gtk_file_system_get_info (impl->file_system, file,
4075 file_list_drag_data_received_get_info_cb,
4079 g_signal_stop_emission_by_name (widget, "drag-data-received");
4082 /* Don't do anything with the drag_drop signal */
4084 file_list_drag_drop_cb (GtkWidget *widget,
4085 GdkDragContext *context,
4089 GtkFileChooserDefault *impl)
4091 g_signal_stop_emission_by_name (widget, "drag-drop");
4095 /* Disable the normal tree drag motion handler, it makes it look like you're
4096 dropping the dragged item onto a tree item */
4098 file_list_drag_motion_cb (GtkWidget *widget,
4099 GdkDragContext *context,
4103 GtkFileChooserDefault *impl)
4105 g_signal_stop_emission_by_name (widget, "drag-motion");
4109 /* Sensitizes the "Copy file's location" context menu item if there is actually
4110 * a selection active.
4113 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4115 GtkTreeSelection *selection;
4118 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4119 if (gtk_tree_selection_count_selected_rows (selection) == 0)
4124 if (impl->browse_files_popup_menu_copy_file_location_item)
4125 gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4129 file_list_add_image_menu_item (GtkFileChooserDefault *impl,
4130 const char *stock_name,
4131 const char *mnemonic_label,
4136 item = gtk_image_menu_item_new_with_mnemonic (mnemonic_label);
4137 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4138 gtk_image_new_from_stock (stock_name, GTK_ICON_SIZE_MENU));
4139 g_signal_connect (item, "activate", callback, impl);
4140 gtk_widget_show (item);
4141 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4147 file_list_add_check_menu_item (GtkFileChooserDefault *impl,
4148 const char *mnemonic_label,
4153 item = gtk_check_menu_item_new_with_mnemonic (mnemonic_label);
4154 g_signal_connect (item, "toggled", callback, impl);
4155 gtk_widget_show (item);
4156 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4161 /* Constructs the popup menu for the file list if needed */
4163 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4167 if (impl->browse_files_popup_menu)
4170 impl->browse_files_popup_menu = gtk_menu_new ();
4171 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4172 impl->browse_files_tree_view,
4173 popup_menu_detach_cb);
4175 impl->browse_files_popup_menu_visit_file_item = file_list_add_image_menu_item (impl, GTK_STOCK_DIRECTORY, _("_Visit this file"),
4176 G_CALLBACK (visit_file_cb));
4178 impl->browse_files_popup_menu_copy_file_location_item = file_list_add_image_menu_item (impl, GTK_STOCK_COPY, _("_Copy file's location"),
4179 G_CALLBACK (copy_file_location_cb));
4181 impl->browse_files_popup_menu_add_shortcut_item = file_list_add_image_menu_item (impl, GTK_STOCK_ADD, _("_Add to Bookmarks"),
4182 G_CALLBACK (add_to_shortcuts_cb));
4184 item = gtk_separator_menu_item_new ();
4185 gtk_widget_show (item);
4186 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4188 impl->browse_files_popup_menu_hidden_files_item = file_list_add_check_menu_item (impl, _("Show _Hidden Files"),
4189 G_CALLBACK (show_hidden_toggled_cb));
4191 impl->browse_files_popup_menu_size_column_item = file_list_add_check_menu_item (impl, _("Show _Size Column"),
4192 G_CALLBACK (show_size_column_toggled_cb));
4194 bookmarks_check_add_sensitivity (impl);
4195 check_copy_file_location_sensitivity (impl);
4198 /* Updates the popup menu for the file list, creating it if necessary */
4200 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4202 file_list_build_popup_menu (impl);
4204 /* The sensitivity of the Add to Bookmarks item is set in
4205 * bookmarks_check_add_sensitivity()
4208 /* 'Visit this file' */
4209 gtk_widget_set_visible (impl->browse_files_popup_menu_visit_file_item, (impl->operation_mode != OPERATION_MODE_BROWSE));
4211 /* 'Show Hidden Files' */
4212 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4213 G_CALLBACK (show_hidden_toggled_cb), impl);
4214 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4216 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4217 G_CALLBACK (show_hidden_toggled_cb), impl);
4219 /* 'Show Size Column' */
4220 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4221 G_CALLBACK (show_size_column_toggled_cb), impl);
4222 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4223 impl->show_size_column);
4224 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4225 G_CALLBACK (show_size_column_toggled_cb), impl);
4229 popup_position_func (GtkMenu *menu,
4235 GtkAllocation allocation;
4236 GtkWidget *widget = GTK_WIDGET (user_data);
4237 GdkScreen *screen = gtk_widget_get_screen (widget);
4240 GdkRectangle monitor;
4242 g_return_if_fail (gtk_widget_get_realized (widget));
4244 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4246 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4249 gtk_widget_get_allocation (widget, &allocation);
4250 *x += (allocation.width - req.width) / 2;
4251 *y += (allocation.height - req.height) / 2;
4253 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4254 gtk_menu_set_monitor (menu, monitor_num);
4255 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
4257 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4258 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4264 file_list_popup_menu (GtkFileChooserDefault *impl,
4265 GdkEventButton *event)
4267 file_list_update_popup_menu (impl);
4269 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4270 NULL, NULL, NULL, NULL,
4271 event->button, event->time);
4274 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4276 popup_position_func, impl->browse_files_tree_view,
4277 0, GDK_CURRENT_TIME);
4278 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4284 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4286 list_popup_menu_cb (GtkWidget *widget,
4287 GtkFileChooserDefault *impl)
4289 file_list_popup_menu (impl, NULL);
4293 /* Callback used when a button is pressed on the file list. We trap button 3 to
4294 * bring up a popup menu.
4297 list_button_press_event_cb (GtkWidget *widget,
4298 GdkEventButton *event,
4299 GtkFileChooserDefault *impl)
4301 static gboolean in_press = FALSE;
4306 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
4310 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4313 file_list_popup_menu (impl, event);
4318 OperationMode operation_mode;
4319 gint general_column;
4323 /* Sets the sort column IDs for the file list; needs to be done whenever we
4324 * change the model on the treeview.
4327 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4329 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4330 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4331 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4335 file_list_query_tooltip_cb (GtkWidget *widget,
4338 gboolean keyboard_tip,
4339 GtkTooltip *tooltip,
4342 GtkFileChooserDefault *impl = user_data;
4343 GtkTreeModel *model;
4349 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4353 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4356 &model, &path, &iter))
4359 gtk_tree_model_get (model, &iter,
4360 MODEL_COL_FILE, &file,
4365 gtk_tree_path_free (path);
4369 filename = g_file_get_path (file);
4370 gtk_tooltip_set_text (tooltip, filename);
4371 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4376 g_object_unref (file);
4377 gtk_tree_path_free (path);
4383 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4387 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4388 gtk_cell_renderer_set_fixed_size (renderer,
4389 xpad * 2 + impl->icon_size,
4390 ypad * 2 + impl->icon_size);
4393 /* Creates the widgets for the file list */
4395 create_file_list (GtkFileChooserDefault *impl)
4398 GtkTreeSelection *selection;
4399 GtkTreeViewColumn *column;
4400 GtkCellRenderer *renderer;
4402 /* Scrolled window */
4403 swin = gtk_scrolled_window_new (NULL, NULL);
4404 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4405 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4406 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4409 /* Tree/list view */
4411 impl->browse_files_tree_view = gtk_tree_view_new ();
4412 #ifdef PROFILE_FILE_CHOOSER
4413 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4415 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4416 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4418 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4419 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4421 gtk_drag_dest_set (impl->browse_files_tree_view,
4422 GTK_DEST_DEFAULT_ALL,
4424 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4425 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4427 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4428 G_CALLBACK (list_row_activated), impl);
4429 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4430 G_CALLBACK (browse_files_key_press_event_cb), impl);
4431 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4432 G_CALLBACK (list_popup_menu_cb), impl);
4433 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4434 G_CALLBACK (list_button_press_event_cb), impl);
4436 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4437 G_CALLBACK (file_list_drag_data_received_cb), impl);
4438 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4439 G_CALLBACK (file_list_drag_drop_cb), impl);
4440 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4441 G_CALLBACK (file_list_drag_motion_cb), impl);
4443 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4444 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4445 G_CALLBACK (file_list_query_tooltip_cb), impl);
4447 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4448 gtk_tree_selection_set_select_function (selection,
4451 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4454 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4455 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4457 g_signal_connect (selection, "changed",
4458 G_CALLBACK (list_selection_changed), impl);
4460 /* Keep the column order in sync with update_cell_renderer_attributes() */
4462 /* Filename column */
4464 impl->list_name_column = gtk_tree_view_column_new ();
4465 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4466 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4467 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4469 renderer = gtk_cell_renderer_pixbuf_new ();
4470 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4471 set_icon_cell_renderer_fixed_size (impl, renderer);
4472 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4474 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4475 g_object_set (impl->list_name_renderer,
4476 "ellipsize", PANGO_ELLIPSIZE_END,
4478 g_signal_connect (impl->list_name_renderer, "edited",
4479 G_CALLBACK (renderer_edited_cb), impl);
4480 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4481 G_CALLBACK (renderer_editing_canceled_cb), impl);
4482 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4484 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4488 column = gtk_tree_view_column_new ();
4489 gtk_tree_view_column_set_resizable (column, TRUE);
4490 gtk_tree_view_column_set_title (column, _("Size"));
4492 renderer = gtk_cell_renderer_text_new ();
4493 g_object_set (renderer,
4494 "alignment", PANGO_ALIGN_RIGHT,
4496 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4497 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4498 impl->list_size_column = column;
4500 /* Modification time column */
4502 column = gtk_tree_view_column_new ();
4503 gtk_tree_view_column_set_resizable (column, TRUE);
4504 gtk_tree_view_column_set_title (column, _("Modified"));
4506 renderer = gtk_cell_renderer_text_new ();
4507 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4508 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4509 impl->list_mtime_column = column;
4511 file_list_set_sort_column_ids (impl);
4512 update_cell_renderer_attributes (impl);
4514 gtk_widget_show_all (swin);
4519 /* Creates the widgets for the files/folders pane */
4521 file_pane_create (GtkFileChooserDefault *impl,
4522 GtkSizeGroup *size_group)
4528 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4529 gtk_widget_show (vbox);
4531 /* Box for lists and preview */
4533 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4534 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4535 gtk_widget_show (hbox);
4539 widget = create_file_list (impl);
4540 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4541 gtk_size_group_add_widget (size_group, widget);
4545 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4546 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4547 /* Don't show preview box initially */
4551 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4553 widget = filter_create (impl);
4555 gtk_widget_show (widget);
4556 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4558 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4564 location_entry_create (GtkFileChooserDefault *impl)
4566 if (!impl->location_entry)
4567 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4569 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4570 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4571 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4572 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4575 /* Creates the widgets specific to Save mode */
4577 save_widgets_create (GtkFileChooserDefault *impl)
4582 if (impl->save_widgets != NULL)
4585 location_switch_to_path_bar (impl);
4587 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4589 impl->save_widgets_table = gtk_grid_new ();
4590 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4591 gtk_widget_show (impl->save_widgets_table);
4592 gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4593 gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4597 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4598 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4599 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4600 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4601 gtk_widget_show (widget);
4603 /* Location entry */
4605 location_entry_create (impl);
4606 gtk_widget_set_hexpand (impl->location_entry, TRUE);
4607 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4608 gtk_widget_show (impl->location_entry);
4609 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4612 impl->save_folder_label = gtk_label_new (NULL);
4613 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4614 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4615 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4616 gtk_widget_show (impl->save_folder_label);
4618 impl->save_widgets = vbox;
4619 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4620 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4621 gtk_widget_show (impl->save_widgets);
4624 /* Destroys the widgets specific to Save mode */
4626 save_widgets_destroy (GtkFileChooserDefault *impl)
4628 if (impl->save_widgets == NULL)
4631 gtk_widget_destroy (impl->save_widgets);
4632 impl->save_widgets = NULL;
4633 impl->save_widgets_table = NULL;
4634 impl->location_entry = NULL;
4635 impl->save_folder_label = NULL;
4638 /* Turns on the path bar widget. Can be called even if we are already in that
4642 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4644 if (impl->location_entry)
4646 gtk_widget_destroy (impl->location_entry);
4647 impl->location_entry = NULL;
4650 gtk_widget_hide (impl->location_entry_box);
4653 /* Turns on the location entry. Can be called even if we are already in that
4657 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4659 /* when in search or recent files mode, we are not showing the
4660 * location_entry_box container, so there's no point in switching
4663 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4664 impl->operation_mode == OPERATION_MODE_RECENT)
4669 gtk_widget_show (impl->location_entry_box);
4673 if (!impl->location_entry)
4675 location_entry_create (impl);
4676 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4677 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4680 /* Configure the entry */
4682 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4686 gtk_widget_show (impl->location_entry);
4687 gtk_widget_grab_focus (impl->location_entry);
4690 /* Sets a new location mode. set_buttons determines whether the toggle button
4691 * for the mode will also be changed.
4694 location_mode_set (GtkFileChooserDefault *impl,
4695 LocationMode new_mode,
4696 gboolean set_button)
4698 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4699 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4701 GtkWindow *toplevel;
4702 GtkWidget *current_focus;
4703 gboolean button_active;
4704 gboolean switch_to_file_list;
4708 case LOCATION_MODE_PATH_BAR:
4709 button_active = FALSE;
4711 /* The location_entry will disappear when we switch to path bar mode. So,
4712 * we'll focus the file list in that case, to avoid having a window with
4713 * no focused widget.
4715 toplevel = get_toplevel (GTK_WIDGET (impl));
4716 switch_to_file_list = FALSE;
4719 current_focus = gtk_window_get_focus (toplevel);
4720 if (!current_focus || current_focus == impl->location_entry)
4721 switch_to_file_list = TRUE;
4724 location_switch_to_path_bar (impl);
4726 if (switch_to_file_list)
4727 gtk_widget_grab_focus (impl->browse_files_tree_view);
4731 case LOCATION_MODE_FILENAME_ENTRY:
4732 button_active = TRUE;
4733 location_switch_to_filename_entry (impl);
4737 g_assert_not_reached ();
4743 g_signal_handlers_block_by_func (impl->location_button,
4744 G_CALLBACK (location_button_toggled_cb), impl);
4746 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4748 g_signal_handlers_unblock_by_func (impl->location_button,
4749 G_CALLBACK (location_button_toggled_cb), impl);
4753 impl->location_mode = new_mode;
4757 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4759 /* when in search or recent files mode, we are not showing the
4760 * location_entry_box container, so there's no point in switching
4763 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4764 impl->operation_mode == OPERATION_MODE_RECENT)
4767 /* If the file entry is not visible, show it.
4768 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4770 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4772 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4774 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4776 if (gtk_widget_has_focus (impl->location_entry))
4778 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4782 gtk_widget_grab_focus (impl->location_entry);
4787 /* Callback used when one of the location mode buttons is toggled */
4789 location_button_toggled_cb (GtkToggleButton *toggle,
4790 GtkFileChooserDefault *impl)
4793 LocationMode new_mode;
4795 is_active = gtk_toggle_button_get_active (toggle);
4799 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4800 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4804 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4805 new_mode = LOCATION_MODE_PATH_BAR;
4808 location_mode_set (impl, new_mode, FALSE);
4811 /* Creates a toggle button for the location entry. */
4813 location_button_create (GtkFileChooserDefault *impl)
4818 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4819 gtk_widget_show (image);
4821 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4825 g_signal_connect (impl->location_button, "toggled",
4826 G_CALLBACK (location_button_toggled_cb), impl);
4828 str = _("Type a file name");
4830 gtk_widget_set_tooltip_text (impl->location_button, str);
4831 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4835 PATH_BAR_FOLDER_PATH,
4836 PATH_BAR_SELECT_A_FOLDER,
4837 PATH_BAR_ERROR_NO_FILENAME,
4838 PATH_BAR_ERROR_NO_FOLDER,
4839 PATH_BAR_RECENTLY_USED,
4843 /* Creates the info bar for informational messages or warnings, with its icon and label */
4845 info_bar_create (GtkFileChooserDefault *impl)
4847 GtkWidget *content_area;
4849 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4850 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4851 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4853 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4855 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4856 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4858 gtk_widget_show (impl->browse_select_a_folder_icon);
4859 gtk_widget_show (impl->browse_select_a_folder_label);
4862 /* Sets the info bar to show the appropriate informational or warning message */
4864 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4868 GtkMessageType message_type;
4874 case PATH_BAR_SELECT_A_FOLDER:
4875 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4877 message_type = GTK_MESSAGE_OTHER;
4880 case PATH_BAR_ERROR_NO_FILENAME:
4881 str = _("Please type a file name");
4882 message_type = GTK_MESSAGE_WARNING;
4885 case PATH_BAR_ERROR_NO_FOLDER:
4886 str = _("Please select a folder below");
4887 message_type = GTK_MESSAGE_WARNING;
4891 g_assert_not_reached ();
4895 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4896 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4897 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4898 GTK_ICON_SIZE_MENU);
4899 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4905 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4907 special_mode_widgets_create (GtkFileChooserDefault *impl)
4909 impl->browse_special_mode_icon = gtk_image_new ();
4910 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4911 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4913 impl->browse_special_mode_label = gtk_label_new (NULL);
4914 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4915 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4918 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4920 path_bar_widgets_create (GtkFileChooserDefault *impl)
4922 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4923 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4924 gtk_widget_show (impl->browse_path_bar_hbox);
4926 /* Size group that allows the path bar to be the same size between modes */
4927 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4928 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4930 /* Location button */
4931 location_button_create (impl);
4932 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4933 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4936 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4937 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4938 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4940 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4941 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4944 info_bar_create (impl);
4945 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4946 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4948 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4949 special_mode_widgets_create (impl);
4952 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4953 g_signal_connect (impl->browse_new_folder_button, "clicked",
4954 G_CALLBACK (new_folder_button_clicked), impl);
4955 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4956 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4959 /* Sets the path bar's mode to show a label, the actual folder path, or a
4960 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4961 * if the pathbar is already showing the widgets you expect; otherwise, call
4962 * path_bar_update() instead to set the appropriate widgets automatically.
4965 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4967 gboolean path_bar_visible = FALSE;
4968 gboolean special_mode_widgets_visible = FALSE;
4969 gboolean info_bar_visible = FALSE;
4970 gboolean create_folder_visible = FALSE;
4976 case PATH_BAR_FOLDER_PATH:
4977 path_bar_visible = TRUE;
4980 case PATH_BAR_SELECT_A_FOLDER:
4981 case PATH_BAR_ERROR_NO_FILENAME:
4982 case PATH_BAR_ERROR_NO_FOLDER:
4983 info_bar_set (impl, mode);
4984 info_bar_visible = TRUE;
4987 case PATH_BAR_RECENTLY_USED:
4988 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4990 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4991 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4994 special_mode_widgets_visible = TRUE;
4997 case PATH_BAR_SEARCH:
4998 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
5000 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
5001 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
5004 special_mode_widgets_visible = TRUE;
5008 g_assert_not_reached ();
5011 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
5012 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
5013 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
5014 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
5016 if (path_bar_visible)
5018 if (impl->create_folders
5019 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
5020 && impl->operation_mode != OPERATION_MODE_RECENT)
5021 create_folder_visible = TRUE;
5024 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
5027 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5029 browse_widgets_create (GtkFileChooserDefault *impl)
5033 GtkSizeGroup *size_group;
5035 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5036 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5037 gtk_widget_show (impl->browse_widgets_box);
5039 impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5040 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5041 gtk_widget_show (impl->browse_header_box);
5043 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5044 path_bar_widgets_create (impl);
5046 /* Box for the location label and entry */
5048 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5049 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5051 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5052 gtk_widget_show (impl->location_label);
5053 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5055 /* size group is used by the scrolled windows of the panes */
5056 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5060 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5061 gtk_widget_show (hpaned);
5062 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5064 widget = shortcuts_pane_create (impl, size_group);
5065 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5066 widget = file_pane_create (impl, size_group);
5067 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5068 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5069 g_object_unref (size_group);
5073 gtk_file_chooser_default_constructor (GType type,
5074 guint n_construct_properties,
5075 GObjectConstructParam *construct_params)
5077 GtkFileChooserDefault *impl;
5080 profile_start ("start", NULL);
5082 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5083 n_construct_properties,
5085 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5087 g_assert (impl->file_system);
5089 gtk_widget_push_composite_child ();
5091 /* Shortcuts model */
5092 shortcuts_model_create (impl);
5094 /* The browse widgets */
5095 browse_widgets_create (impl);
5097 /* Alignment to hold extra widget */
5098 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5099 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5100 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5101 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5103 gtk_widget_pop_composite_child ();
5104 update_appearance (impl);
5106 profile_end ("end", NULL);
5111 /* Sets the extra_widget by packing it in the appropriate place */
5113 set_extra_widget (GtkFileChooserDefault *impl,
5114 GtkWidget *extra_widget)
5118 g_object_ref (extra_widget);
5119 /* FIXME: is this right ? */
5120 gtk_widget_show (extra_widget);
5123 if (impl->extra_widget)
5125 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5126 g_object_unref (impl->extra_widget);
5129 impl->extra_widget = extra_widget;
5130 if (impl->extra_widget)
5132 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5133 gtk_widget_show (impl->extra_align);
5136 gtk_widget_hide (impl->extra_align);
5140 set_local_only (GtkFileChooserDefault *impl,
5141 gboolean local_only)
5143 if (local_only != impl->local_only)
5145 impl->local_only = local_only;
5147 if (impl->location_entry)
5148 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5150 if (impl->shortcuts_model && impl->file_system)
5152 shortcuts_add_volumes (impl);
5153 shortcuts_add_bookmarks (impl);
5156 if (local_only && impl->current_folder &&
5157 !g_file_is_native (impl->current_folder))
5159 /* If we are pointing to a non-local folder, make an effort to change
5160 * back to a local folder, but it's really up to the app to not cause
5161 * such a situation, so we ignore errors.
5163 const gchar *home = g_get_home_dir ();
5169 home_file = g_file_new_for_path (home);
5171 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5173 g_object_unref (home_file);
5179 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5180 GtkFileChooserDefault *impl)
5182 shortcuts_add_volumes (impl);
5183 shortcuts_add_bookmarks (impl);
5185 bookmarks_check_add_sensitivity (impl);
5186 bookmarks_check_remove_sensitivity (impl);
5187 shortcuts_check_popup_sensitivity (impl);
5190 /* Sets the file chooser to multiple selection mode */
5192 set_select_multiple (GtkFileChooserDefault *impl,
5193 gboolean select_multiple,
5194 gboolean property_notify)
5196 GtkTreeSelection *selection;
5197 GtkSelectionMode mode;
5199 if (select_multiple == impl->select_multiple)
5202 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
5204 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5205 gtk_tree_selection_set_mode (selection, mode);
5207 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5209 impl->select_multiple = select_multiple;
5210 g_object_notify (G_OBJECT (impl), "select-multiple");
5212 check_preview_change (impl);
5216 set_file_system_backend (GtkFileChooserDefault *impl)
5218 profile_start ("start for backend", "default");
5220 impl->file_system = _gtk_file_system_new ();
5222 g_signal_connect (impl->file_system, "volumes-changed",
5223 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5224 g_signal_connect (impl->file_system, "bookmarks-changed",
5225 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5227 profile_end ("end", NULL);
5231 unset_file_system_backend (GtkFileChooserDefault *impl)
5233 g_signal_handlers_disconnect_by_func (impl->file_system,
5234 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5236 g_object_unref (impl->file_system);
5238 impl->file_system = NULL;
5241 /* Saves the widgets around the pathbar so they can be reparented later
5242 * in the correct place. This function must be called paired with
5243 * restore_path_bar().
5246 save_path_bar (GtkFileChooserDefault *impl)
5250 g_object_ref (impl->browse_path_bar_hbox);
5252 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5254 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5257 /* Reparents the path bar and the "Create folder" button to the right place:
5258 * Above the file list in Open mode, or to the right of the "Save in folder:"
5259 * label in Save mode. The save_path_bar() function must be called before this
5263 restore_path_bar (GtkFileChooserDefault *impl)
5265 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5266 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5268 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5269 gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5271 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5272 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5274 gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5275 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5276 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5279 g_assert_not_reached ();
5281 g_object_unref (impl->browse_path_bar_hbox);
5284 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5286 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5290 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5291 MODEL_COL_FILE, &file,
5293 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE);
5294 g_object_unref (file);
5297 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5298 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5299 * function will update all the pathbar's widgets.
5302 path_bar_update (GtkFileChooserDefault *impl)
5306 switch (impl->operation_mode)
5308 case OPERATION_MODE_BROWSE:
5309 mode = PATH_BAR_FOLDER_PATH;
5312 case OPERATION_MODE_RECENT:
5313 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5315 GtkTreeSelection *selection;
5316 gboolean have_selected;
5319 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5321 /* Save mode means single-selection mode, so the following is valid */
5322 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5326 mode = PATH_BAR_FOLDER_PATH;
5327 put_recent_folder_in_pathbar (impl, &iter);
5330 mode = PATH_BAR_SELECT_A_FOLDER;
5333 mode = PATH_BAR_RECENTLY_USED;
5337 case OPERATION_MODE_SEARCH:
5338 mode = PATH_BAR_SEARCH;
5342 g_assert_not_reached ();
5346 path_bar_set_mode (impl, mode);
5350 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5352 if (impl->search_hbox)
5354 gtk_widget_destroy (impl->search_hbox);
5356 impl->search_hbox = NULL;
5357 impl->search_entry = NULL;
5361 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5363 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5367 case OPERATION_MODE_BROWSE:
5368 stop_loading_and_clear_list_model (impl, TRUE);
5371 case OPERATION_MODE_SEARCH:
5372 search_stop_searching (impl, FALSE);
5373 search_clear_model (impl, TRUE);
5375 operation_mode_discard_search_widgets (impl);
5378 case OPERATION_MODE_RECENT:
5379 recent_stop_loading (impl);
5380 recent_clear_model (impl, TRUE);
5384 g_assert_not_reached ();
5389 operation_mode_set_browse (GtkFileChooserDefault *impl)
5391 path_bar_update (impl);
5393 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5394 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5396 gtk_widget_show (impl->location_button);
5397 location_mode_set (impl, impl->location_mode, TRUE);
5399 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5400 gtk_widget_show (impl->location_entry_box);
5405 operation_mode_set_search (GtkFileChooserDefault *impl)
5407 g_assert (impl->search_hbox == NULL);
5408 g_assert (impl->search_entry == NULL);
5409 g_assert (impl->search_model == NULL);
5411 search_setup_widgets (impl);
5415 operation_mode_set_recent (GtkFileChooserDefault *impl)
5417 path_bar_update (impl);
5419 /* Hide the location widgets temporarily */
5420 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5421 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5423 gtk_widget_hide (impl->location_button);
5424 gtk_widget_hide (impl->location_entry_box);
5427 recent_start_loading (impl);
5430 /* Sometimes we need to frob the selection in the shortcuts list manually */
5432 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5434 GtkTreeSelection *selection;
5437 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5439 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5441 path = gtk_tree_path_new_from_indices (pos, -1);
5442 gtk_tree_selection_select_path (selection, path);
5443 gtk_tree_path_free (path);
5445 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5449 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5451 ShortcutsIndex shortcut_to_select;
5453 operation_mode_stop (impl, impl->operation_mode);
5455 impl->operation_mode = mode;
5457 switch (impl->operation_mode)
5459 case OPERATION_MODE_BROWSE:
5460 operation_mode_set_browse (impl);
5461 shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5464 case OPERATION_MODE_SEARCH:
5465 operation_mode_set_search (impl);
5466 shortcut_to_select = SHORTCUTS_SEARCH;
5469 case OPERATION_MODE_RECENT:
5470 operation_mode_set_recent (impl);
5471 shortcut_to_select = SHORTCUTS_RECENT;
5475 g_assert_not_reached ();
5479 if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5480 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5483 /* This function is basically a do_all function.
5485 * It sets the visibility on all the widgets based on the current state, and
5486 * moves the custom_widget if needed.
5489 update_appearance (GtkFileChooserDefault *impl)
5491 save_path_bar (impl);
5493 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5494 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5498 gtk_widget_hide (impl->location_button);
5499 save_widgets_create (impl);
5501 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5502 text = _("Save in _folder:");
5504 text = _("Create in _folder:");
5506 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5508 if (impl->select_multiple)
5510 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5511 "Re-setting to single selection mode.");
5512 set_select_multiple (impl, FALSE, TRUE);
5515 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5516 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5518 gtk_widget_show (impl->location_button);
5519 save_widgets_destroy (impl);
5520 location_mode_set (impl, impl->location_mode, TRUE);
5523 if (impl->location_entry)
5524 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5526 restore_path_bar (impl);
5527 path_bar_update (impl);
5529 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5530 * of files may change depending whether we are in a file or folder-only mode.
5532 gtk_widget_queue_draw (impl->browse_files_tree_view);
5534 emit_default_size_changed (impl);
5538 gtk_file_chooser_default_set_property (GObject *object,
5540 const GValue *value,
5544 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5548 case GTK_FILE_CHOOSER_PROP_ACTION:
5550 GtkFileChooserAction action = g_value_get_enum (value);
5552 if (action != impl->action)
5554 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5556 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5557 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5558 && impl->select_multiple)
5560 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5561 "this is not allowed in multiple selection mode. Resetting the file chooser "
5562 "to single selection mode.");
5563 set_select_multiple (impl, FALSE, TRUE);
5565 impl->action = action;
5566 update_cell_renderer_attributes (impl);
5567 update_appearance (impl);
5568 settings_load (impl);
5573 case GTK_FILE_CHOOSER_PROP_FILTER:
5574 set_current_filter (impl, g_value_get_object (value));
5577 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5578 set_local_only (impl, g_value_get_boolean (value));
5581 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5582 set_preview_widget (impl, g_value_get_object (value));
5585 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5586 impl->preview_widget_active = g_value_get_boolean (value);
5587 update_preview_widget_visibility (impl);
5590 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5591 impl->use_preview_label = g_value_get_boolean (value);
5592 update_preview_widget_visibility (impl);
5595 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5596 set_extra_widget (impl, g_value_get_object (value));
5599 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5601 gboolean select_multiple = g_value_get_boolean (value);
5602 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5603 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5606 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5607 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5608 "leaving the file chooser in single selection mode.");
5612 set_select_multiple (impl, select_multiple, FALSE);
5616 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5618 gboolean show_hidden = g_value_get_boolean (value);
5619 if (show_hidden != impl->show_hidden)
5621 impl->show_hidden = show_hidden;
5623 if (impl->browse_files_model)
5624 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5629 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5631 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5632 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5636 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5638 gboolean create_folders = g_value_get_boolean (value);
5639 impl->create_folders = create_folders;
5640 update_appearance (impl);
5645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5651 gtk_file_chooser_default_get_property (GObject *object,
5656 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5660 case GTK_FILE_CHOOSER_PROP_ACTION:
5661 g_value_set_enum (value, impl->action);
5664 case GTK_FILE_CHOOSER_PROP_FILTER:
5665 g_value_set_object (value, impl->current_filter);
5668 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5669 g_value_set_boolean (value, impl->local_only);
5672 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5673 g_value_set_object (value, impl->preview_widget);
5676 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5677 g_value_set_boolean (value, impl->preview_widget_active);
5680 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5681 g_value_set_boolean (value, impl->use_preview_label);
5684 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5685 g_value_set_object (value, impl->extra_widget);
5688 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5689 g_value_set_boolean (value, impl->select_multiple);
5692 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5693 g_value_set_boolean (value, impl->show_hidden);
5696 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5697 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5700 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5701 g_value_set_boolean (value, impl->create_folders);
5705 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5710 /* This cancels everything that may be going on in the background. */
5712 cancel_all_operations (GtkFileChooserDefault *impl)
5716 pending_select_files_free (impl);
5718 /* cancel all pending operations */
5719 if (impl->pending_cancellables)
5721 for (l = impl->pending_cancellables; l; l = l->next)
5723 GCancellable *cancellable = G_CANCELLABLE (l->data);
5724 g_cancellable_cancel (cancellable);
5726 g_slist_free (impl->pending_cancellables);
5727 impl->pending_cancellables = NULL;
5730 if (impl->reload_icon_cancellables)
5732 for (l = impl->reload_icon_cancellables; l; l = l->next)
5734 GCancellable *cancellable = G_CANCELLABLE (l->data);
5735 g_cancellable_cancel (cancellable);
5737 g_slist_free (impl->reload_icon_cancellables);
5738 impl->reload_icon_cancellables = NULL;
5741 if (impl->loading_shortcuts)
5743 for (l = impl->loading_shortcuts; l; l = l->next)
5745 GCancellable *cancellable = G_CANCELLABLE (l->data);
5746 g_cancellable_cancel (cancellable);
5748 g_slist_free (impl->loading_shortcuts);
5749 impl->loading_shortcuts = NULL;
5752 if (impl->file_list_drag_data_received_cancellable)
5754 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5755 impl->file_list_drag_data_received_cancellable = NULL;
5758 if (impl->update_current_folder_cancellable)
5760 g_cancellable_cancel (impl->update_current_folder_cancellable);
5761 impl->update_current_folder_cancellable = NULL;
5764 if (impl->should_respond_get_info_cancellable)
5766 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5767 impl->should_respond_get_info_cancellable = NULL;
5770 if (impl->update_from_entry_cancellable)
5772 g_cancellable_cancel (impl->update_from_entry_cancellable);
5773 impl->update_from_entry_cancellable = NULL;
5776 if (impl->shortcuts_activate_iter_cancellable)
5778 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5779 impl->shortcuts_activate_iter_cancellable = NULL;
5782 search_stop_searching (impl, TRUE);
5783 recent_stop_loading (impl);
5786 /* Removes the settings signal handler. It's safe to call multiple times */
5788 remove_settings_signal (GtkFileChooserDefault *impl,
5791 if (impl->settings_signal_id)
5793 GtkSettings *settings;
5795 settings = gtk_settings_get_for_screen (screen);
5796 g_signal_handler_disconnect (settings,
5797 impl->settings_signal_id);
5798 impl->settings_signal_id = 0;
5803 gtk_file_chooser_default_dispose (GObject *object)
5805 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5807 cancel_all_operations (impl);
5809 if (impl->extra_widget)
5811 g_object_unref (impl->extra_widget);
5812 impl->extra_widget = NULL;
5815 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5817 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5820 /* We override show-all since we have internal widgets that
5821 * shouldn't be shown when you call show_all(), like the filter
5825 gtk_file_chooser_default_show_all (GtkWidget *widget)
5827 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5829 gtk_widget_show (widget);
5831 if (impl->extra_widget)
5832 gtk_widget_show_all (impl->extra_widget);
5835 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5836 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5839 toplevel_set_focus_cb (GtkWindow *window,
5841 GtkFileChooserDefault *impl)
5843 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5846 /* We monitor the focus widget on our toplevel to be able to know which widget
5847 * was last focused at the time our "should_respond" method gets called.
5850 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5851 GtkWidget *previous_toplevel)
5853 GtkFileChooserDefault *impl;
5854 GtkWidget *toplevel;
5856 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5857 toplevel = gtk_widget_get_toplevel (widget);
5859 if (previous_toplevel &&
5860 impl->toplevel_set_focus_id != 0)
5862 g_signal_handler_disconnect (previous_toplevel,
5863 impl->toplevel_set_focus_id);
5864 impl->toplevel_set_focus_id = 0;
5865 impl->toplevel_last_focus_widget = NULL;
5868 if (gtk_widget_is_toplevel (toplevel))
5870 g_assert (impl->toplevel_set_focus_id == 0);
5871 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5872 G_CALLBACK (toplevel_set_focus_cb), impl);
5873 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5877 /* Changes the icons wherever it is needed */
5879 change_icon_theme (GtkFileChooserDefault *impl)
5881 GtkSettings *settings;
5883 GtkCellRenderer *renderer;
5886 profile_start ("start", NULL);
5888 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5890 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5891 impl->icon_size = MAX (width, height);
5893 impl->icon_size = FALLBACK_ICON_SIZE;
5895 shortcuts_reload_icons (impl);
5896 /* the first cell in the first column is the icon column, and we have a fixed size there */
5897 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5898 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5899 renderer = GTK_CELL_RENDERER (cells->data);
5900 set_icon_cell_renderer_fixed_size (impl, renderer);
5901 g_list_free (cells);
5902 if (impl->browse_files_model)
5903 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5904 gtk_widget_queue_resize (impl->browse_files_tree_view);
5906 profile_end ("end", NULL);
5909 /* Callback used when a GtkSettings value changes */
5911 settings_notify_cb (GObject *object,
5913 GtkFileChooserDefault *impl)
5917 profile_start ("start", NULL);
5919 name = g_param_spec_get_name (pspec);
5921 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5922 strcmp (name, "gtk-icon-sizes") == 0)
5923 change_icon_theme (impl);
5925 profile_end ("end", NULL);
5928 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5932 check_icon_theme (GtkFileChooserDefault *impl)
5934 GtkSettings *settings;
5936 profile_start ("start", NULL);
5938 if (impl->settings_signal_id)
5940 profile_end ("end", NULL);
5944 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5946 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5947 impl->settings_signal_id = g_signal_connect (settings, "notify",
5948 G_CALLBACK (settings_notify_cb), impl);
5950 change_icon_theme (impl);
5953 profile_end ("end", NULL);
5957 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5959 GtkFileChooserDefault *impl;
5961 profile_start ("start", NULL);
5963 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5965 profile_msg (" parent class style_udpated start", NULL);
5966 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5967 profile_msg (" parent class style_updated end", NULL);
5969 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5970 change_icon_theme (impl);
5972 emit_default_size_changed (impl);
5974 profile_end ("end", NULL);
5978 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5979 GdkScreen *previous_screen)
5981 GtkFileChooserDefault *impl;
5983 profile_start ("start", NULL);
5985 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5987 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5988 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5990 remove_settings_signal (impl, previous_screen);
5991 check_icon_theme (impl);
5993 emit_default_size_changed (impl);
5995 profile_end ("end", NULL);
5999 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
6000 GtkAllocation *allocation)
6002 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6006 set_sort_column (GtkFileChooserDefault *impl)
6008 GtkTreeSortable *sortable;
6010 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6012 /* can happen when we're still populating the model */
6013 if (sortable == NULL)
6016 gtk_tree_sortable_set_sort_column_id (sortable,
6022 settings_ensure (GtkFileChooserDefault *impl)
6024 if (impl->settings != NULL)
6027 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
6028 "/org/gtk/settings/file-chooser/");
6029 g_settings_delay (impl->settings);
6033 settings_load (GtkFileChooserDefault *impl)
6035 LocationMode location_mode;
6036 gboolean show_hidden;
6037 gboolean show_size_column;
6039 GtkSortType sort_order;
6041 settings_ensure (impl);
6043 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
6044 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
6045 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6046 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
6047 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
6049 location_mode_set (impl, location_mode, TRUE);
6051 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6053 impl->show_size_column = show_size_column;
6054 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6056 impl->sort_column = sort_column;
6057 impl->sort_order = sort_order;
6058 /* We don't call set_sort_column() here as the models may not have been
6059 * created yet. The individual functions that create and set the models will
6060 * call set_sort_column() themselves.
6065 save_dialog_geometry (GtkFileChooserDefault *impl)
6067 GtkWindow *toplevel;
6068 int x, y, width, height;
6070 toplevel = get_toplevel (GTK_WIDGET (impl));
6072 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6075 gtk_window_get_position (toplevel, &x, &y);
6076 gtk_window_get_size (toplevel, &width, &height);
6078 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
6079 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
6083 settings_save (GtkFileChooserDefault *impl)
6085 char *current_folder_uri;
6087 settings_ensure (impl);
6089 /* Current folder */
6091 if (impl->current_folder)
6092 current_folder_uri = g_file_get_uri (impl->current_folder);
6094 current_folder_uri = "";
6096 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
6098 if (impl->current_folder)
6099 g_free (current_folder_uri);
6101 /* All the other state */
6103 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6104 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
6105 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6106 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6107 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6108 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6110 save_dialog_geometry (impl);
6112 /* Now apply the settings */
6113 g_settings_apply (impl->settings);
6115 g_object_unref (impl->settings);
6116 impl->settings = NULL;
6119 /* GtkWidget::realize method */
6121 gtk_file_chooser_default_realize (GtkWidget *widget)
6123 GtkFileChooserDefault *impl;
6125 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6127 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6129 emit_default_size_changed (impl);
6133 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
6135 char *last_folder_uri;
6138 settings_ensure (impl);
6140 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
6142 /* If no last folder is set, we use the user's home directory, since
6143 * this is the starting point for most documents.
6145 if (last_folder_uri[0] == '\0')
6146 file = g_file_new_for_path (g_get_home_dir ());
6148 file = g_file_new_for_uri (last_folder_uri);
6150 g_free (last_folder_uri);
6155 /* GtkWidget::map method */
6157 gtk_file_chooser_default_map (GtkWidget *widget)
6159 GtkFileChooserDefault *impl;
6161 profile_start ("start", NULL);
6163 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6165 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6167 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6169 switch (impl->reload_state)
6172 recent_shortcut_handler (impl);
6175 case RELOAD_HAS_FOLDER:
6176 /* Nothing; we are already loading or loaded, so we
6177 * don't need to reload
6182 g_assert_not_reached ();
6186 volumes_bookmarks_changed_cb (impl->file_system, impl);
6188 settings_load (impl);
6190 profile_end ("end", NULL);
6193 /* GtkWidget::unmap method */
6195 gtk_file_chooser_default_unmap (GtkWidget *widget)
6197 GtkFileChooserDefault *impl;
6199 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6201 settings_save (impl);
6203 cancel_all_operations (impl);
6204 impl->reload_state = RELOAD_EMPTY;
6206 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6209 #define COMPARE_DIRECTORIES \
6210 GtkFileChooserDefault *impl = user_data; \
6211 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6212 gboolean dir_a, dir_b; \
6214 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6215 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6217 if (dir_a != dir_b) \
6218 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6220 /* Sort callback for the filename column */
6222 name_sort_func (GtkTreeModel *model,
6227 COMPARE_DIRECTORIES;
6230 const char *key_a, *key_b;
6233 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6234 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6237 result = strcmp (key_a, key_b);
6249 /* Sort callback for the size column */
6251 size_sort_func (GtkTreeModel *model,
6256 COMPARE_DIRECTORIES;
6259 gint64 size_a, size_b;
6261 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6262 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6264 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6268 /* Sort callback for the mtime column */
6270 mtime_sort_func (GtkTreeModel *model,
6275 COMPARE_DIRECTORIES;
6280 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6281 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6283 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6287 /* Callback used when the sort column changes. We cache the sort order for use
6288 * in name_sort_func().
6291 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6292 GtkFileChooserDefault *impl)
6294 gint sort_column_id;
6295 GtkSortType sort_type;
6297 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6299 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6300 impl->sort_column = sort_column_id;
6301 impl->sort_order = sort_type;
6306 set_busy_cursor (GtkFileChooserDefault *impl,
6310 GtkWindow *toplevel;
6311 GdkDisplay *display;
6314 toplevel = get_toplevel (GTK_WIDGET (impl));
6315 widget = GTK_WIDGET (toplevel);
6316 if (!toplevel || !gtk_widget_get_realized (widget))
6319 display = gtk_widget_get_display (widget);
6322 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6326 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6327 gdk_display_flush (display);
6330 g_object_unref (cursor);
6333 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6335 load_set_model (GtkFileChooserDefault *impl)
6337 profile_start ("start", NULL);
6339 g_assert (impl->browse_files_model != NULL);
6341 profile_msg (" gtk_tree_view_set_model start", NULL);
6342 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6343 GTK_TREE_MODEL (impl->browse_files_model));
6344 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6345 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6347 file_list_set_sort_column_ids (impl);
6348 set_sort_column (impl);
6349 profile_msg (" gtk_tree_view_set_model end", NULL);
6350 impl->list_sort_ascending = TRUE;
6352 profile_end ("end", NULL);
6355 /* Timeout callback used when the loading timer expires */
6357 load_timeout_cb (gpointer data)
6359 GtkFileChooserDefault *impl;
6361 profile_start ("start", NULL);
6363 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6364 g_assert (impl->load_state == LOAD_PRELOAD);
6365 g_assert (impl->load_timeout_id != 0);
6366 g_assert (impl->browse_files_model != NULL);
6368 impl->load_timeout_id = 0;
6369 impl->load_state = LOAD_LOADING;
6371 load_set_model (impl);
6373 profile_end ("end", NULL);
6378 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6380 load_setup_timer (GtkFileChooserDefault *impl)
6382 g_assert (impl->load_timeout_id == 0);
6383 g_assert (impl->load_state != LOAD_PRELOAD);
6385 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6386 impl->load_state = LOAD_PRELOAD;
6389 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6391 load_remove_timer (GtkFileChooserDefault *impl)
6393 if (impl->load_timeout_id != 0)
6395 g_assert (impl->load_state == LOAD_PRELOAD);
6397 g_source_remove (impl->load_timeout_id);
6398 impl->load_timeout_id = 0;
6399 impl->load_state = LOAD_EMPTY;
6402 g_assert (impl->load_state == LOAD_EMPTY ||
6403 impl->load_state == LOAD_LOADING ||
6404 impl->load_state == LOAD_FINISHED);
6407 /* Selects the first row in the file list */
6409 browse_files_select_first_row (GtkFileChooserDefault *impl)
6412 GtkTreeIter dummy_iter;
6413 GtkTreeModel *tree_model;
6415 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6420 path = gtk_tree_path_new_from_indices (0, -1);
6422 /* If the list is empty, do nothing. */
6423 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6424 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6426 gtk_tree_path_free (path);
6429 struct center_selected_row_closure {
6430 GtkFileChooserDefault *impl;
6431 gboolean already_centered;
6434 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6435 * selected row in the tree view.
6438 center_selected_row_foreach_cb (GtkTreeModel *model,
6443 struct center_selected_row_closure *closure;
6446 if (closure->already_centered)
6449 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6450 closure->already_centered = TRUE;
6453 /* Centers the selected row in the tree view */
6455 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6457 struct center_selected_row_closure closure;
6458 GtkTreeSelection *selection;
6460 closure.impl = impl;
6461 closure.already_centered = FALSE;
6463 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6464 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6468 show_and_select_files (GtkFileChooserDefault *impl,
6471 GtkTreeSelection *selection;
6472 GtkFileSystemModel *fsmodel;
6473 gboolean enabled_hidden, removed_filters;
6474 gboolean selected_a_file;
6477 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6478 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6480 enabled_hidden = impl->show_hidden;
6481 removed_filters = (impl->current_filter == NULL);
6483 selected_a_file = FALSE;
6485 for (walk = files; walk; walk = walk->next)
6487 GFile *file = walk->data;
6490 /* Is it a hidden file? */
6492 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6495 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6497 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6499 if (!enabled_hidden &&
6500 (g_file_info_get_is_hidden (info) ||
6501 g_file_info_get_is_backup (info)))
6503 g_object_set (impl, "show-hidden", TRUE, NULL);
6504 enabled_hidden = TRUE;
6508 /* Is it a filtered file? */
6510 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6511 continue; /* re-get the iter as it may change when the model refilters */
6513 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6515 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6516 if (!removed_filters)
6518 set_current_filter (impl, NULL);
6519 removed_filters = TRUE;
6523 /* Okay, can we select the file now? */
6525 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6528 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6532 gtk_tree_selection_select_iter (selection, &iter);
6534 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6535 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6537 gtk_tree_path_free (path);
6539 selected_a_file = TRUE;
6543 browse_files_center_selected_row (impl);
6545 return selected_a_file;
6548 /* Processes the pending operation when a folder is finished loading */
6550 pending_select_files_process (GtkFileChooserDefault *impl)
6552 g_assert (impl->load_state == LOAD_FINISHED);
6553 g_assert (impl->browse_files_model != NULL);
6555 if (impl->pending_select_files)
6557 show_and_select_files (impl, impl->pending_select_files);
6558 pending_select_files_free (impl);
6559 browse_files_center_selected_row (impl);
6563 /* We only select the first row if the chooser is actually mapped ---
6564 * selecting the first row is to help the user when he is interacting with
6565 * the chooser, but sometimes a chooser works not on behalf of the user,
6566 * but rather on behalf of something else like GtkFileChooserButton. In
6567 * that case, the chooser's selection should be what the caller expects,
6568 * as the user can't see that something else got selected. See bug #165264.
6570 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6571 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6572 browse_files_select_first_row (impl);
6575 g_assert (impl->pending_select_files == NULL);
6579 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6584 info = g_file_query_info (impl->current_folder,
6585 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6586 G_FILE_QUERY_INFO_NONE,
6591 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6592 g_object_unref (info);
6595 msg = g_strdup (_("Could not read the contents of the folder"));
6597 error_message (impl, msg, error->message);
6601 /* Callback used when the file system model finishes loading */
6603 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6605 GtkFileChooserDefault *impl)
6607 profile_start ("start", NULL);
6610 show_error_on_reading_current_folder (impl, error);
6612 if (impl->load_state == LOAD_PRELOAD)
6614 load_remove_timer (impl);
6615 load_set_model (impl);
6617 else if (impl->load_state == LOAD_LOADING)
6623 /* We can't g_assert_not_reached(), as something other than us may have
6624 * initiated a folder reload. See #165556.
6626 profile_end ("end", NULL);
6630 g_assert (impl->load_timeout_id == 0);
6632 impl->load_state = LOAD_FINISHED;
6634 pending_select_files_process (impl);
6635 set_busy_cursor (impl, FALSE);
6636 #ifdef PROFILE_FILE_CHOOSER
6637 access ("MARK: *** FINISHED LOADING", F_OK);
6640 profile_end ("end", NULL);
6644 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6645 gboolean remove_from_treeview)
6647 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6649 if (impl->browse_files_model)
6651 g_object_unref (impl->browse_files_model);
6652 impl->browse_files_model = NULL;
6655 if (remove_from_treeview)
6656 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6660 my_g_format_time_for_display (glong secs)
6665 time_t time_mtime, time_now;
6666 const gchar *format;
6667 gchar *locale_format = NULL;
6669 char *date_str = NULL;
6671 const char *locale, *dot = NULL;
6672 gint64 codepage = -1;
6678 #ifdef HAVE_LOCALTIME_R
6679 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6682 struct tm *ptm = localtime ((time_t *) &time_mtime);
6686 g_warning ("ptm != NULL failed");
6688 return g_strdup (_("Unknown"));
6691 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6693 #endif /* HAVE_LOCALTIME_R */
6695 g_date_set_time_t (&mtime, time_mtime);
6696 time_now = time (NULL);
6697 g_date_set_time_t (&now, time_now);
6699 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6701 /* Translators: %H means "hours" and %M means "minutes" */
6703 format = _("%H:%M");
6704 else if (days_diff == 1)
6705 format = _("Yesterday at %H:%M");
6708 if (days_diff > 1 && days_diff < 7)
6709 format = "%A"; /* Days from last week */
6711 format = "%x"; /* Any other date */
6715 /* g_locale_from_utf8() returns a string in the system
6716 * code-page, which is not always the same as that used by the C
6717 * library. For instance when running a GTK+ program with
6718 * LANG=ko on an English version of Windows, the system
6719 * code-page is 1252, but the code-page used by the C library is
6720 * 949. (It's GTK+ itself that sets the C library locale when it
6721 * notices the LANG environment variable. See gtkmain.c The
6722 * Microsoft C library doesn't look at any locale environment
6723 * variables.) We need to pass strftime() a string in the C
6724 * library's code-page. See bug #509885.
6726 locale = setlocale (LC_ALL, NULL);
6728 dot = strchr (locale, '.');
6731 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6733 /* All codepages should fit in 16 bits AFAIK */
6734 if (codepage > 0 && codepage < 65536)
6736 sprintf (charset, "CP%u", (guint) codepage);
6737 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6741 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6743 if (locale_format != NULL &&
6744 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6747 /* As above but in opposite direction... */
6748 if (codepage > 0 && codepage < 65536)
6749 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6751 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6755 if (date_str == NULL)
6756 date_str = g_strdup (_("Unknown"));
6758 g_free (locale_format);
6763 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6765 GFileAttributeType type;
6768 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6769 g_file_info_set_attribute (to, attribute, type, value);
6773 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6775 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6776 GFile *file = G_FILE (object);
6777 GFileInfo *queried, *info;
6780 queried = g_file_query_info_finish (file, res, NULL);
6781 if (queried == NULL)
6784 gdk_threads_enter ();
6786 /* now we know model is valid */
6788 /* file was deleted */
6789 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6791 gdk_threads_leave ();
6795 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6797 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6798 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6799 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6801 _gtk_file_system_model_update_file (model, file, info, FALSE);
6803 g_object_unref (info);
6805 gdk_threads_leave ();
6809 file_system_model_set (GtkFileSystemModel *model,
6816 GtkFileChooserDefault *impl = data;
6820 case MODEL_COL_FILE:
6821 g_value_set_object (value, file);
6823 case MODEL_COL_NAME:
6825 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6827 g_value_set_string (value, g_file_info_get_display_name (info));
6829 case MODEL_COL_NAME_COLLATED:
6831 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6833 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6835 case MODEL_COL_IS_FOLDER:
6836 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6838 case MODEL_COL_IS_SENSITIVE:
6841 gboolean sensitive = TRUE;
6843 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6844 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
6846 sensitive = TRUE; /* for file modes... */
6848 else if (!_gtk_file_info_consider_as_directory (info))
6850 sensitive = FALSE; /* for folder modes, files are not sensitive... */
6854 /* ... and for folder modes, folders are sensitive only if the filter says so */
6856 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6857 g_assert_not_reached ();
6858 sensitive = !_gtk_file_system_model_iter_is_filtered_out (model, &iter);
6861 g_value_set_boolean (value, sensitive);
6864 g_value_set_boolean (value, TRUE);
6866 case MODEL_COL_PIXBUF:
6869 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6871 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6875 GtkTreeModel *tree_model;
6876 GtkTreePath *path, *start, *end;
6879 if (impl->browse_files_tree_view == NULL ||
6880 g_file_info_has_attribute (info, "filechooser::queried"))
6883 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6884 if (tree_model != GTK_TREE_MODEL (model))
6887 if (!_gtk_file_system_model_get_iter_for_file (model,
6890 g_assert_not_reached ();
6891 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6893 path = gtk_tree_model_get_path (tree_model, &iter);
6894 if (gtk_tree_path_compare (start, path) != 1 &&
6895 gtk_tree_path_compare (path, end) != 1)
6897 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6898 g_file_query_info_async (file,
6899 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6900 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6901 G_FILE_ATTRIBUTE_STANDARD_ICON,
6902 G_FILE_QUERY_INFO_NONE,
6904 _gtk_file_system_model_get_cancellable (model),
6905 file_system_model_got_thumbnail,
6908 gtk_tree_path_free (path);
6909 gtk_tree_path_free (start);
6910 gtk_tree_path_free (end);
6915 g_value_set_object (value, NULL);
6917 case MODEL_COL_SIZE:
6918 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6920 case MODEL_COL_SIZE_TEXT:
6921 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6922 g_value_set_string (value, NULL);
6924 g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6926 case MODEL_COL_MTIME:
6927 case MODEL_COL_MTIME_TEXT:
6932 g_file_info_get_modification_time (info, &tv);
6933 if (column == MODEL_COL_MTIME)
6934 g_value_set_long (value, tv.tv_sec);
6935 else if (tv.tv_sec == 0)
6936 g_value_set_static_string (value, _("Unknown"));
6938 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6941 case MODEL_COL_ELLIPSIZE:
6942 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6945 g_assert_not_reached ();
6952 /* Gets rid of the old list model and creates a new one for the current folder */
6954 set_list_model (GtkFileChooserDefault *impl,
6957 g_assert (impl->current_folder != NULL);
6959 profile_start ("start", NULL);
6961 stop_loading_and_clear_list_model (impl, TRUE);
6963 set_busy_cursor (impl, TRUE);
6965 impl->browse_files_model =
6966 _gtk_file_system_model_new_for_directory (impl->current_folder,
6968 file_system_model_set,
6970 MODEL_COLUMN_TYPES);
6972 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6974 profile_msg (" set sort function", NULL);
6975 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6976 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6977 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6978 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6979 set_sort_column (impl);
6980 impl->list_sort_ascending = TRUE;
6981 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6982 G_CALLBACK (list_sort_column_changed_cb), impl);
6984 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6986 g_signal_connect (impl->browse_files_model, "finished-loading",
6987 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6989 _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
6991 profile_end ("end", NULL);
6996 struct update_chooser_entry_selected_foreach_closure {
6998 GtkTreeIter first_selected_iter;
7002 compare_utf8_filenames (const gchar *a,
7005 gchar *a_folded, *b_folded;
7008 a_folded = g_utf8_strdown (a, -1);
7009 b_folded = g_utf8_strdown (b, -1);
7011 retval = strcmp (a_folded, b_folded);
7020 update_chooser_entry_selected_foreach (GtkTreeModel *model,
7025 struct update_chooser_entry_selected_foreach_closure *closure;
7028 closure->num_selected++;
7030 if (closure->num_selected == 1)
7031 closure->first_selected_iter = *iter;
7035 update_chooser_entry (GtkFileChooserDefault *impl)
7037 GtkTreeSelection *selection;
7038 struct update_chooser_entry_selected_foreach_closure closure;
7040 /* no need to update the file chooser's entry if there's no entry */
7041 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7042 !impl->location_entry)
7045 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7046 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7047 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7048 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7049 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
7052 g_assert (impl->location_entry != NULL);
7054 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7055 closure.num_selected = 0;
7056 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7058 if (closure.num_selected == 0)
7060 if (impl->operation_mode == OPERATION_MODE_RECENT)
7061 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
7063 goto maybe_clear_entry;
7065 else if (closure.num_selected == 1)
7067 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7070 gboolean change_entry;
7072 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7074 /* If the cursor moved to the row of the newly created folder,
7075 * retrieving info will return NULL.
7080 g_free (impl->browse_files_last_selected_name);
7081 impl->browse_files_last_selected_name =
7082 g_strdup (g_file_info_get_display_name (info));
7084 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7085 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7086 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7088 /* Don't change the name when clicking on a folder... */
7089 change_entry = (! _gtk_file_info_consider_as_directory (info));
7092 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7096 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7098 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7099 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7104 else if (impl->operation_mode == OPERATION_MODE_RECENT
7105 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7109 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7111 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7112 MODEL_COL_FILE, &folder,
7114 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7115 g_object_unref (folder);
7121 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7122 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7124 /* Multiple selection, so just clear the entry. */
7125 g_free (impl->browse_files_last_selected_name);
7126 impl->browse_files_last_selected_name = NULL;
7128 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7134 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7135 && impl->browse_files_last_selected_name)
7137 const char *entry_text;
7139 gboolean clear_entry;
7141 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7142 len = strlen (entry_text);
7145 /* The file chooser entry may have appended a "/" to its text.
7146 * So take it out, and compare the result to the old selection.
7148 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7152 tmp = g_strndup (entry_text, len - 1);
7153 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7157 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7160 clear_entry = FALSE;
7163 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7168 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7172 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7176 struct UpdateCurrentFolderData
7178 GtkFileChooserDefault *impl;
7180 gboolean keep_trail;
7181 gboolean clear_entry;
7182 GFile *original_file;
7183 GError *original_error;
7187 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7188 GtkFileSystemVolume *volume,
7189 const GError *error,
7192 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7193 struct UpdateCurrentFolderData *data = user_data;
7194 GtkFileChooserDefault *impl = data->impl;
7196 if (cancellable != impl->update_current_folder_cancellable)
7199 impl->update_current_folder_cancellable = NULL;
7200 set_busy_cursor (impl, FALSE);
7207 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7208 impl->reload_state = RELOAD_EMPTY;
7212 change_folder_and_display_error (impl, data->file, data->clear_entry);
7215 g_object_unref (data->file);
7218 g_object_unref (cancellable);
7222 update_current_folder_get_info_cb (GCancellable *cancellable,
7224 const GError *error,
7227 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7228 struct UpdateCurrentFolderData *data = user_data;
7229 GtkFileChooserDefault *impl = data->impl;
7231 if (cancellable != impl->update_current_folder_cancellable)
7234 impl->update_current_folder_cancellable = NULL;
7235 impl->reload_state = RELOAD_EMPTY;
7237 set_busy_cursor (impl, FALSE);
7246 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7248 GMountOperation *mount_operation;
7249 GtkWidget *toplevel;
7251 g_object_unref (cancellable);
7252 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7254 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7256 set_busy_cursor (impl, TRUE);
7258 impl->update_current_folder_cancellable =
7259 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7261 update_current_folder_mount_enclosing_volume_cb,
7267 if (!data->original_file)
7269 data->original_file = g_object_ref (data->file);
7270 data->original_error = g_error_copy (error);
7273 parent_file = g_file_get_parent (data->file);
7275 /* get parent path and try to change the folder to that */
7278 g_object_unref (data->file);
7279 data->file = parent_file;
7281 g_object_unref (cancellable);
7283 /* restart the update current folder operation */
7284 impl->reload_state = RELOAD_HAS_FOLDER;
7286 impl->update_current_folder_cancellable =
7287 _gtk_file_system_get_info (impl->file_system, data->file,
7289 update_current_folder_get_info_cb,
7292 set_busy_cursor (impl, TRUE);
7298 /* Error and bail out, ignoring "not found" errors since they're useless:
7299 * they only happen when a program defaults to a folder that has been (re)moved.
7301 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7302 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7304 g_error_free (data->original_error);
7306 g_object_unref (data->original_file);
7312 if (data->original_file)
7314 /* Error and bail out, ignoring "not found" errors since they're useless:
7315 * they only happen when a program defaults to a folder that has been (re)moved.
7317 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7318 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7320 g_error_free (data->original_error);
7322 g_object_unref (data->original_file);
7325 if (! _gtk_file_info_consider_as_directory (info))
7328 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail);
7330 if (impl->current_folder != data->file)
7332 if (impl->current_folder)
7333 g_object_unref (impl->current_folder);
7335 impl->current_folder = g_object_ref (data->file);
7338 impl->reload_state = RELOAD_HAS_FOLDER;
7340 /* Update the widgets that may trigger a folder change themselves. */
7342 if (!impl->changing_folder)
7344 impl->changing_folder = TRUE;
7346 shortcuts_update_current_folder (impl);
7348 impl->changing_folder = FALSE;
7351 /* Set the folder on the save entry */
7353 if (impl->location_entry)
7355 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7356 impl->current_folder);
7358 if (data->clear_entry)
7359 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7362 /* Create a new list model. This is slightly evil; we store the result value
7363 * but perform more actions rather than returning immediately even if it
7364 * generates an error.
7366 set_list_model (impl, NULL);
7368 /* Refresh controls */
7370 shortcuts_find_current_folder (impl);
7372 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7374 check_preview_change (impl);
7375 bookmarks_check_add_sensitivity (impl);
7377 g_signal_emit_by_name (impl, "selection-changed", 0);
7380 g_object_unref (data->file);
7383 g_object_unref (cancellable);
7387 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7389 gboolean keep_trail,
7390 gboolean clear_entry,
7393 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7394 struct UpdateCurrentFolderData *data;
7396 profile_start ("start", NULL);
7398 g_object_ref (file);
7400 operation_mode_set (impl, OPERATION_MODE_BROWSE);
7402 if (impl->local_only && !g_file_is_native (file))
7404 g_set_error_literal (error,
7405 GTK_FILE_CHOOSER_ERROR,
7406 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7407 _("Cannot change to folder because it is not local"));
7409 g_object_unref (file);
7410 profile_end ("end - not local", NULL);
7414 if (impl->update_current_folder_cancellable)
7415 g_cancellable_cancel (impl->update_current_folder_cancellable);
7417 /* Test validity of path here. */
7418 data = g_new0 (struct UpdateCurrentFolderData, 1);
7420 data->file = g_object_ref (file);
7421 data->keep_trail = keep_trail;
7422 data->clear_entry = clear_entry;
7424 impl->reload_state = RELOAD_HAS_FOLDER;
7426 impl->update_current_folder_cancellable =
7427 _gtk_file_system_get_info (impl->file_system, file,
7429 update_current_folder_get_info_cb,
7432 set_busy_cursor (impl, TRUE);
7433 g_object_unref (file);
7435 profile_end ("end", NULL);
7440 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7442 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7444 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7445 impl->operation_mode == OPERATION_MODE_RECENT)
7448 if (impl->reload_state == RELOAD_EMPTY)
7450 /* We are unmapped, or we had an error while loading the last folder.
7451 * We'll return the folder used by the last invocation of the file chooser
7452 * since once we get (re)mapped, we'll load *that* folder anyway unless
7453 * the caller explicitly calls set_current_folder() on us.
7455 return get_file_for_last_folder_opened (impl);
7458 if (impl->current_folder)
7459 return g_object_ref (impl->current_folder);
7465 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7468 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7470 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7471 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7473 pending_select_files_free (impl);
7474 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), name);
7478 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7482 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7486 parent_file = g_file_get_parent (file);
7489 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7491 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7492 impl->operation_mode == OPERATION_MODE_RECENT ||
7493 impl->load_state == LOAD_EMPTY)
7499 g_assert (impl->current_folder != NULL);
7501 same_path = g_file_equal (parent_file, impl->current_folder);
7504 if (same_path && impl->load_state == LOAD_FINISHED)
7509 files.data = (gpointer) file;
7512 result = show_and_select_files (impl, &files);
7513 g_object_unref (parent_file);
7517 pending_select_files_add (impl, file);
7523 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7524 g_object_unref (parent_file);
7528 g_object_unref (parent_file);
7533 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7536 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7537 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7540 if (!impl->browse_files_model)
7543 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7548 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7553 maybe_select (GtkTreeModel *model,
7558 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7559 GtkTreeSelection *selection;
7560 gboolean is_sensitive;
7563 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7565 gtk_tree_model_get (model, iter,
7566 MODEL_COL_IS_FOLDER, &is_folder,
7567 MODEL_COL_IS_SENSITIVE, &is_sensitive,
7571 ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7572 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)))
7573 gtk_tree_selection_select_iter (selection, iter);
7575 gtk_tree_selection_unselect_iter (selection, iter);
7581 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7583 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7585 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7586 impl->operation_mode == OPERATION_MODE_RECENT)
7588 GtkTreeSelection *selection;
7590 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7591 gtk_tree_selection_select_all (selection);
7595 if (impl->select_multiple)
7596 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7597 maybe_select, impl);
7601 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7603 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7604 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7606 gtk_tree_selection_unselect_all (selection);
7607 pending_select_files_free (impl);
7610 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7612 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7614 * is_empty_ret - whether the file entry is totally empty
7616 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7617 * the path will be "$cwd/foobar")
7620 check_save_entry (GtkFileChooserDefault *impl,
7622 gboolean *is_well_formed_ret,
7623 gboolean *is_empty_ret,
7624 gboolean *is_file_part_empty_ret,
7625 gboolean *is_folder)
7627 GtkFileChooserEntry *chooser_entry;
7628 GFile *current_folder;
7629 const char *file_part;
7633 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7634 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7635 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7636 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7637 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7639 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7641 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7644 *is_well_formed_ret = TRUE;
7645 *is_empty_ret = TRUE;
7646 *is_file_part_empty_ret = TRUE;
7652 *is_empty_ret = FALSE;
7654 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7655 if (!current_folder)
7658 *is_well_formed_ret = FALSE;
7659 *is_file_part_empty_ret = FALSE;
7665 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7667 if (!file_part || file_part[0] == '\0')
7669 *file_ret = current_folder;
7670 *is_well_formed_ret = TRUE;
7671 *is_file_part_empty_ret = TRUE;
7677 *is_file_part_empty_ret = FALSE;
7680 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7681 g_object_unref (current_folder);
7685 error_building_filename_dialog (impl, error);
7687 *is_well_formed_ret = FALSE;
7694 *is_well_formed_ret = TRUE;
7695 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7698 struct get_files_closure {
7699 GtkFileChooserDefault *impl;
7701 GFile *file_from_entry;
7705 get_files_foreach (GtkTreeModel *model,
7710 struct get_files_closure *info;
7712 GtkFileSystemModel *fs_model;
7715 fs_model = info->impl->browse_files_model;
7717 file = _gtk_file_system_model_get_file (fs_model, iter);
7719 return; /* We are on the editable row */
7721 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7722 info->result = g_slist_prepend (info->result, g_object_ref (file));
7726 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7728 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7729 struct get_files_closure info;
7730 GtkWindow *toplevel;
7731 GtkWidget *current_focus;
7732 gboolean file_list_seen;
7736 info.file_from_entry = NULL;
7738 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7739 return search_get_selected_files (impl);
7741 if (impl->operation_mode == OPERATION_MODE_RECENT)
7743 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7745 file_list_seen = TRUE;
7749 return recent_get_selected_files (impl);
7752 toplevel = get_toplevel (GTK_WIDGET (impl));
7754 current_focus = gtk_window_get_focus (toplevel);
7756 current_focus = NULL;
7758 file_list_seen = FALSE;
7759 if (current_focus == impl->browse_files_tree_view)
7761 GtkTreeSelection *selection;
7765 file_list_seen = TRUE;
7766 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7767 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7769 /* If there is no selection in the file list, we probably have this situation:
7771 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7772 * 2. He then double-clicked on a folder ("bar") in the file list
7774 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7775 * filename entry to see if that is the case.
7777 if (info.result == NULL && impl->location_entry)
7780 else if (impl->location_entry && current_focus == impl->location_entry)
7782 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7786 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7791 if (!is_well_formed)
7794 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7796 g_object_unref (info.file_from_entry);
7800 if (info.file_from_entry)
7801 info.result = g_slist_prepend (info.result, info.file_from_entry);
7802 else if (!file_list_seen)
7807 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7809 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7813 /* The focus is on a dialog's action area button or something else */
7814 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7815 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7823 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7824 * fall back to the current directory */
7825 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7826 info.result == NULL)
7828 GFile *current_folder;
7830 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7833 info.result = g_slist_prepend (info.result, current_folder);
7836 return g_slist_reverse (info.result);
7840 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7842 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7844 if (impl->preview_file)
7845 return g_object_ref (impl->preview_file);
7850 static GtkFileSystem *
7851 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7853 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7855 return impl->file_system;
7858 /* Shows or hides the filter widgets */
7860 show_filters (GtkFileChooserDefault *impl,
7864 gtk_widget_show (impl->filter_combo_hbox);
7866 gtk_widget_hide (impl->filter_combo_hbox);
7870 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7871 GtkFileFilter *filter)
7873 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7876 if (g_slist_find (impl->filters, filter))
7878 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7882 g_object_ref_sink (filter);
7883 impl->filters = g_slist_append (impl->filters, filter);
7885 name = gtk_file_filter_get_name (filter);
7887 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7889 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7891 if (!g_slist_find (impl->filters, impl->current_filter))
7892 set_current_filter (impl, filter);
7894 show_filters (impl, TRUE);
7898 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7899 GtkFileFilter *filter)
7901 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7902 GtkTreeModel *model;
7906 filter_index = g_slist_index (impl->filters, filter);
7908 if (filter_index < 0)
7910 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7914 impl->filters = g_slist_remove (impl->filters, filter);
7916 if (filter == impl->current_filter)
7919 set_current_filter (impl, impl->filters->data);
7921 set_current_filter (impl, NULL);
7924 /* Remove row from the combo box */
7925 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7926 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7927 g_assert_not_reached ();
7929 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7931 g_object_unref (filter);
7934 show_filters (impl, FALSE);
7938 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7940 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7942 return g_slist_copy (impl->filters);
7945 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7947 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7950 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7953 struct AddShortcutData
7955 GtkFileChooserDefault *impl;
7960 add_shortcut_get_info_cb (GCancellable *cancellable,
7962 const GError *error,
7966 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7967 struct AddShortcutData *data = user_data;
7969 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7972 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7974 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7977 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7979 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7982 g_object_unref (data->impl);
7983 g_object_unref (data->file);
7986 g_object_unref (cancellable);
7990 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7994 GCancellable *cancellable;
7995 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7996 struct AddShortcutData *data;
8000 /* Avoid adding duplicates */
8001 pos = shortcut_find_position (impl, file);
8002 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
8006 uri = g_file_get_uri (file);
8007 /* translators, "Shortcut" means "Bookmark" here */
8009 GTK_FILE_CHOOSER_ERROR,
8010 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8011 _("Shortcut %s already exists"),
8018 for (l = impl->loading_shortcuts; l; l = l->next)
8020 GCancellable *c = l->data;
8023 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8024 if (f && g_file_equal (file, f))
8028 uri = g_file_get_uri (file);
8030 GTK_FILE_CHOOSER_ERROR,
8031 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8032 _("Shortcut %s already exists"),
8040 data = g_new0 (struct AddShortcutData, 1);
8041 data->impl = g_object_ref (impl);
8042 data->file = g_object_ref (file);
8044 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8046 add_shortcut_get_info_cb, data);
8051 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8052 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8058 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8062 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8069 for (l = impl->loading_shortcuts; l; l = l->next)
8071 GCancellable *c = l->data;
8074 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8075 if (f && g_file_equal (file, f))
8077 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8078 g_cancellable_cancel (c);
8083 if (impl->num_shortcuts == 0)
8086 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8087 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8088 g_assert_not_reached ();
8090 for (i = 0; i < impl->num_shortcuts; i++)
8093 ShortcutType shortcut_type;
8096 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8097 SHORTCUTS_COL_DATA, &col_data,
8098 SHORTCUTS_COL_TYPE, &shortcut_type,
8100 g_assert (col_data != NULL);
8101 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8103 shortcut = col_data;
8104 if (g_file_equal (shortcut, file))
8106 shortcuts_remove_rows (impl, pos + i, 1);
8107 impl->num_shortcuts--;
8111 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8112 g_assert_not_reached ();
8117 uri = g_file_get_uri (file);
8118 /* translators, "Shortcut" means "Bookmark" here */
8120 GTK_FILE_CHOOSER_ERROR,
8121 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8122 _("Shortcut %s does not exist"),
8130 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8132 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8138 if (impl->num_shortcuts == 0)
8141 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8142 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8143 g_assert_not_reached ();
8147 for (i = 0; i < impl->num_shortcuts; i++)
8150 ShortcutType shortcut_type;
8153 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8154 SHORTCUTS_COL_DATA, &col_data,
8155 SHORTCUTS_COL_TYPE, &shortcut_type,
8157 g_assert (col_data != NULL);
8158 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8160 shortcut = col_data;
8161 list = g_slist_prepend (list, g_object_ref (shortcut));
8163 if (i != impl->num_shortcuts - 1)
8165 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8166 g_assert_not_reached ();
8170 return g_slist_reverse (list);
8173 /* Guesses a size based upon font sizes */
8175 find_good_size_from_style (GtkWidget *widget,
8179 GtkStyleContext *context;
8180 GtkStateFlags state;
8185 context = gtk_widget_get_style_context (widget);
8186 state = gtk_widget_get_state_flags (widget);
8188 screen = gtk_widget_get_screen (widget);
8191 resolution = gdk_screen_get_resolution (screen);
8192 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8196 resolution = 96.0; /* wheeee */
8198 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
8199 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8201 *width = font_size * NUM_CHARS;
8202 *height = font_size * NUM_LINES;
8206 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8207 gint *default_width,
8208 gint *default_height)
8210 GtkFileChooserDefault *impl;
8212 int x, y, width, height;
8214 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8216 settings_ensure (impl);
8218 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8219 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8221 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8223 *default_width = width;
8224 *default_height = height;
8228 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8230 if (impl->preview_widget_active &&
8231 impl->preview_widget &&
8232 gtk_widget_get_visible (impl->preview_widget))
8234 gtk_widget_get_preferred_size (impl->preview_box,
8236 *default_width += PREVIEW_HBOX_SPACING + req.width;
8239 if (impl->extra_widget &&
8240 gtk_widget_get_visible (impl->extra_widget))
8242 gtk_widget_get_preferred_size (impl->extra_align,
8244 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8248 struct switch_folder_closure {
8249 GtkFileChooserDefault *impl;
8254 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8256 switch_folder_foreach_cb (GtkTreeModel *model,
8261 struct switch_folder_closure *closure;
8265 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8266 closure->num_selected++;
8269 /* Changes to the selected folder in the list view */
8271 switch_to_selected_folder (GtkFileChooserDefault *impl)
8273 GtkTreeSelection *selection;
8274 struct switch_folder_closure closure;
8276 /* We do this with foreach() rather than get_selected() as we may be in
8277 * multiple selection mode
8280 closure.impl = impl;
8281 closure.file = NULL;
8282 closure.num_selected = 0;
8284 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8285 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8287 g_assert (closure.file && closure.num_selected == 1);
8289 change_folder_and_display_error (impl, closure.file, FALSE);
8292 /* Gets the GFileInfo for the selected row in the file list; assumes single
8296 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8297 gboolean *had_selection)
8299 GtkTreeSelection *selection;
8303 g_assert (!impl->select_multiple);
8304 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8305 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8307 *had_selection = FALSE;
8311 *had_selection = TRUE;
8313 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8317 /* Gets the display name of the selected file in the file list; assumes single
8318 * selection mode and that something is selected.
8320 static const gchar *
8321 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8324 gboolean had_selection;
8326 info = get_selected_file_info_from_file_list (impl, &had_selection);
8327 g_assert (had_selection);
8328 g_assert (info != NULL);
8330 return g_file_info_get_display_name (info);
8334 add_custom_button_to_dialog (GtkDialog *dialog,
8335 const gchar *mnemonic_label,
8336 const gchar *stock_id,
8341 button = gtk_button_new_with_mnemonic (mnemonic_label);
8342 gtk_widget_set_can_default (button, TRUE);
8343 gtk_button_set_image (GTK_BUTTON (button),
8344 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8345 gtk_widget_show (button);
8347 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8350 /* Presents an overwrite confirmation dialog; returns whether we should accept
8354 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8355 const gchar *file_part,
8356 const gchar *folder_display_name)
8358 GtkWindow *toplevel;
8362 toplevel = get_toplevel (GTK_WIDGET (impl));
8364 dialog = gtk_message_dialog_new (toplevel,
8365 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8366 GTK_MESSAGE_QUESTION,
8368 _("A file named \"%s\" already exists. Do you want to replace it?"),
8370 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8371 _("The file already exists in \"%s\". Replacing it will "
8372 "overwrite its contents."),
8373 folder_display_name);
8375 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8376 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8377 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8378 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8379 GTK_RESPONSE_ACCEPT,
8380 GTK_RESPONSE_CANCEL,
8382 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8384 if (gtk_window_has_group (toplevel))
8385 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8386 GTK_WINDOW (dialog));
8388 response = gtk_dialog_run (GTK_DIALOG (dialog));
8390 gtk_widget_destroy (dialog);
8392 return (response == GTK_RESPONSE_ACCEPT);
8395 struct GetDisplayNameData
8397 GtkFileChooserDefault *impl;
8401 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8402 * as requesting a response means, "the dialog is confirmed".
8405 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8407 g_signal_emit_by_name (impl, "response-requested");
8408 add_selection_to_recent_list (impl);
8412 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8414 const GError *error,
8417 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8418 gboolean should_respond = FALSE;
8419 struct GetDisplayNameData *data = user_data;
8421 if (cancellable != data->impl->should_respond_get_info_cancellable)
8424 data->impl->should_respond_get_info_cancellable = NULL;
8430 /* Huh? Did the folder disappear? Let the caller deal with it */
8431 should_respond = TRUE;
8433 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8435 set_busy_cursor (data->impl, FALSE);
8437 request_response_and_add_to_recent_list (data->impl);
8440 g_object_unref (data->impl);
8441 g_free (data->file_part);
8444 g_object_unref (cancellable);
8447 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8448 * should respond. Can get the file part from the file list or the save entry.
8451 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8452 const gchar *file_part,
8455 GtkFileChooserConfirmation conf;
8457 if (!impl->do_overwrite_confirmation)
8460 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8462 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8466 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8468 struct GetDisplayNameData *data;
8470 g_assert (file_part != NULL);
8472 data = g_new0 (struct GetDisplayNameData, 1);
8473 data->impl = g_object_ref (impl);
8474 data->file_part = g_strdup (file_part);
8476 if (impl->should_respond_get_info_cancellable)
8477 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8479 impl->should_respond_get_info_cancellable =
8480 _gtk_file_system_get_info (impl->file_system, parent_file,
8481 "standard::display-name",
8482 confirmation_confirm_get_info_cb,
8484 set_busy_cursor (data->impl, TRUE);
8488 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8491 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8495 g_assert_not_reached ();
8500 struct FileExistsData
8502 GtkFileChooserDefault *impl;
8503 gboolean file_exists_and_is_not_folder;
8509 name_entry_get_parent_info_cb (GCancellable *cancellable,
8511 const GError *error,
8514 gboolean parent_is_folder;
8515 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8516 struct FileExistsData *data = user_data;
8518 if (cancellable != data->impl->should_respond_get_info_cancellable)
8521 data->impl->should_respond_get_info_cancellable = NULL;
8523 set_busy_cursor (data->impl, FALSE);
8529 parent_is_folder = FALSE;
8531 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8533 if (parent_is_folder)
8535 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8537 request_response_and_add_to_recent_list (data->impl); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8539 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8541 if (data->file_exists_and_is_not_folder)
8546 /* Dup the string because the string may be modified
8547 * depending on what clients do in the confirm-overwrite
8548 * signal and this corrupts the pointer
8550 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8551 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8555 request_response_and_add_to_recent_list (data->impl);
8558 request_response_and_add_to_recent_list (data->impl);
8560 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8561 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8563 GError *mkdir_error = NULL;
8565 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8566 * "/blah/nonexistent" you *will* want a folder created.
8569 set_busy_cursor (data->impl, TRUE);
8570 g_file_make_directory (data->file, NULL, &mkdir_error);
8571 set_busy_cursor (data->impl, FALSE);
8574 request_response_and_add_to_recent_list (data->impl);
8576 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8579 g_assert_not_reached ();
8585 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8586 error_with_file_under_nonfolder (data->impl, data->parent_file);
8592 /* The parent folder is not readable for some reason */
8594 error_copy = g_error_copy (error);
8595 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8600 g_object_unref (data->impl);
8601 g_object_unref (data->file);
8602 g_object_unref (data->parent_file);
8605 g_object_unref (cancellable);
8609 file_exists_get_info_cb (GCancellable *cancellable,
8611 const GError *error,
8614 gboolean data_ownership_taken = FALSE;
8615 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8616 gboolean file_exists;
8618 gboolean needs_parent_check = FALSE;
8619 struct FileExistsData *data = user_data;
8621 if (cancellable != data->impl->file_exists_get_info_cancellable)
8624 data->impl->file_exists_get_info_cancellable = NULL;
8626 set_busy_cursor (data->impl, FALSE);
8631 file_exists = (info != NULL);
8632 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8634 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8637 change_folder_and_display_error (data->impl, data->file, TRUE);
8641 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8643 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8646 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8648 if (file_exists && !is_folder)
8650 /* Oops, the user typed the name of an existing path which is not
8653 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8654 g_error_copy (error));
8658 needs_parent_check = TRUE;
8661 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8665 needs_parent_check = TRUE;
8671 /* User typed a folder; we are done */
8672 request_response_and_add_to_recent_list (data->impl);
8675 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8678 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8681 change_folder_and_display_error (data->impl, data->file, TRUE);
8683 needs_parent_check = TRUE;
8687 g_assert_not_reached();
8690 if (needs_parent_check) {
8691 /* check that everything up to the last path component exists (i.e. the parent) */
8693 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8694 data_ownership_taken = TRUE;
8696 if (data->impl->should_respond_get_info_cancellable)
8697 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8699 data->impl->should_respond_get_info_cancellable =
8700 _gtk_file_system_get_info (data->impl->file_system,
8703 name_entry_get_parent_info_cb,
8705 set_busy_cursor (data->impl, TRUE);
8709 if (!data_ownership_taken)
8711 g_object_unref (data->impl);
8712 g_object_unref (data->file);
8713 g_object_unref (data->parent_file);
8717 g_object_unref (cancellable);
8721 paste_text_received (GtkClipboard *clipboard,
8723 GtkFileChooserDefault *impl)
8730 file = g_file_new_for_uri (text);
8732 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8733 location_popup_handler (impl, text);
8735 g_object_unref (file);
8738 /* Handler for the "location-popup-on-paste" keybinding signal */
8740 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8742 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8743 GDK_SELECTION_CLIPBOARD);
8744 gtk_clipboard_request_text (clipboard,
8745 (GtkClipboardTextReceivedFunc) paste_text_received,
8749 /* Implementation for GtkFileChooserEmbed::should_respond() */
8751 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8756 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8758 for (l = files; l; l = l->next)
8760 GFile *file = l->data;
8763 uri = g_file_get_uri (file);
8766 gtk_recent_manager_add_item (impl->recent_manager, uri);
8771 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8772 g_slist_free (files);
8776 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8778 GtkFileChooserDefault *impl;
8779 GtkWidget *toplevel;
8780 GtkWidget *current_focus;
8783 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8785 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8786 g_assert (GTK_IS_WINDOW (toplevel));
8790 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8792 if (current_focus == impl->browse_files_tree_view)
8794 /* The following array encodes what we do based on the impl->action and the
8795 * number of files selected.
8798 NOOP, /* Do nothing (don't respond) */
8799 RESPOND, /* Respond immediately */
8800 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8801 ALL_FILES, /* Respond only if everything selected is a file */
8802 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8803 SAVE_ENTRY, /* Go to the code for handling the save entry */
8804 NOT_REACHED /* Sanity check */
8806 static const ActionToTake what_to_do[4][3] = {
8807 /* 0 selected 1 selected many selected */
8808 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8809 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8810 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8811 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8815 gboolean all_files, all_folders;
8817 ActionToTake action;
8821 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8823 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8825 retval = search_should_respond (impl);
8829 if (impl->operation_mode == OPERATION_MODE_RECENT)
8831 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8835 retval = recent_should_respond (impl);
8840 selection_check (impl, &num_selected, &all_files, &all_folders);
8842 if (num_selected > 2)
8847 action = what_to_do [impl->action] [k];
8858 case RESPOND_OR_SWITCH:
8859 g_assert (num_selected == 1);
8863 switch_to_selected_folder (impl);
8866 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8868 retval = should_respond_after_confirm_overwrite (impl,
8869 get_display_name_from_file_list (impl),
8870 impl->current_folder);
8884 retval = all_folders;
8891 g_assert_not_reached ();
8894 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8897 gboolean is_well_formed, is_empty, is_file_part_empty;
8899 GtkFileChooserEntry *entry;
8904 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8905 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8906 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8907 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8908 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8910 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8911 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8913 if (!is_well_formed)
8916 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8917 && impl->operation_mode == OPERATION_MODE_RECENT)
8919 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8921 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8922 * thus changing our assumption that no selection is present - setting
8923 * a selection causes the error message from path_bar_set_mode() to go away,
8924 * but we want the user to see that message!
8926 gtk_widget_grab_focus (impl->browse_files_tree_view);
8929 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8936 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8937 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8939 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8940 gtk_widget_grab_focus (impl->location_entry);
8947 g_assert (file != NULL);
8952 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8953 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8955 change_folder_and_display_error (impl, file, TRUE);
8957 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8958 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8960 /* The folder already exists, so we do not need to create it.
8961 * Just respond to terminate the dialog.
8967 g_assert_not_reached ();
8972 struct FileExistsData *data;
8974 /* We need to check whether file exists and whether it is a folder -
8975 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8976 * negative (it doesn't know yet if your last path component is a
8980 data = g_new0 (struct FileExistsData, 1);
8981 data->impl = g_object_ref (impl);
8982 data->file = g_object_ref (file);
8983 data->parent_file = _gtk_file_chooser_entry_get_current_folder (entry);
8985 if (impl->file_exists_get_info_cancellable)
8986 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8988 impl->file_exists_get_info_cancellable =
8989 _gtk_file_system_get_info (impl->file_system, file,
8991 file_exists_get_info_cb,
8994 set_busy_cursor (impl, TRUE);
8997 g_error_free (error);
9000 g_object_unref (file);
9002 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
9004 /* The focus is on a dialog's action area button, *and* the widget that
9005 * was focused immediately before it is the file list.
9009 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
9011 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
9014 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
9016 /* The focus is on a dialog's action area button, *and* the widget that
9017 * was focused immediately before it is the location entry.
9022 /* The focus is on a dialog's action area button or something else */
9023 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9024 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9032 add_selection_to_recent_list (impl);
9037 /* Implementation for GtkFileChooserEmbed::initial_focus() */
9039 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
9041 GtkFileChooserDefault *impl;
9044 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
9046 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9047 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9049 if (impl->location_mode == LOCATION_MODE_PATH_BAR
9050 || impl->operation_mode == OPERATION_MODE_RECENT)
9051 widget = impl->browse_files_tree_view;
9053 widget = impl->location_entry;
9055 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
9056 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9057 widget = impl->location_entry;
9060 g_assert_not_reached ();
9064 g_assert (widget != NULL);
9065 gtk_widget_grab_focus (widget);
9068 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
9070 search_selected_foreach_get_file_cb (GtkTreeModel *model,
9080 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9081 *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 */
9084 /* Constructs a list of the selected paths in search mode */
9086 search_get_selected_files (GtkFileChooserDefault *impl)
9089 GtkTreeSelection *selection;
9093 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9094 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9095 result = g_slist_reverse (result);
9100 /* Called from ::should_respond(). We return whether there are selected files
9101 * in the search list.
9104 search_should_respond (GtkFileChooserDefault *impl)
9106 GtkTreeSelection *selection;
9108 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9110 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9111 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9114 /* Adds one hit from the search engine to the search_model */
9116 search_add_hit (GtkFileChooserDefault *impl,
9121 file = g_file_new_for_uri (uri);
9125 if (!g_file_is_native (file))
9127 g_object_unref (file);
9131 _gtk_file_system_model_add_and_query_file (impl->search_model,
9135 g_object_unref (file);
9138 /* Callback used from GtkSearchEngine when we get new hits */
9140 search_engine_hits_added_cb (GtkSearchEngine *engine,
9144 GtkFileChooserDefault *impl;
9147 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9149 for (l = hits; l; l = l->next)
9150 search_add_hit (impl, (gchar*)l->data);
9153 /* Callback used from GtkSearchEngine when the query is done running */
9155 search_engine_finished_cb (GtkSearchEngine *engine,
9158 GtkFileChooserDefault *impl;
9160 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9163 /* EB: setting the model here will avoid loads of row events,
9164 * but it'll make the search look like blocked.
9166 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9167 GTK_TREE_MODEL (impl->search_model));
9168 file_list_set_sort_column_ids (impl);
9171 /* FMQ: if search was empty, say that we got no hits */
9172 set_busy_cursor (impl, FALSE);
9175 /* Displays a generic error when we cannot create a GtkSearchEngine.
9176 * It would be better if _gtk_search_engine_new() gave us a GError
9177 * with a better message, but it doesn't do that right now.
9180 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9182 error_message (impl,
9183 _("Could not start the search process"),
9184 _("The program was not able to create a connection to the indexer "
9185 "daemon. Please make sure it is running."));
9189 search_engine_error_cb (GtkSearchEngine *engine,
9190 const gchar *message,
9193 GtkFileChooserDefault *impl;
9195 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9197 search_stop_searching (impl, TRUE);
9198 error_message (impl, _("Could not send the search request"), message);
9200 set_busy_cursor (impl, FALSE);
9203 /* Frees the data in the search_model */
9205 search_clear_model (GtkFileChooserDefault *impl,
9206 gboolean remove_from_treeview)
9208 if (!impl->search_model)
9211 g_object_unref (impl->search_model);
9212 impl->search_model = NULL;
9214 if (remove_from_treeview)
9215 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9218 /* Stops any ongoing searches; does not touch the search_model */
9220 search_stop_searching (GtkFileChooserDefault *impl,
9221 gboolean remove_query)
9223 if (remove_query && impl->search_query)
9225 g_object_unref (impl->search_query);
9226 impl->search_query = NULL;
9229 if (impl->search_engine)
9231 _gtk_search_engine_stop (impl->search_engine);
9233 g_object_unref (impl->search_engine);
9234 impl->search_engine = NULL;
9238 /* Creates the search_model and puts it in the tree view */
9240 search_setup_model (GtkFileChooserDefault *impl)
9242 g_assert (impl->search_model == NULL);
9244 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9246 MODEL_COLUMN_TYPES);
9248 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9252 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9256 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9260 set_sort_column (impl);
9262 /* EB: setting the model here will make the hits list update feel
9263 * more "alive" than setting the model at the end of the search
9266 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9267 GTK_TREE_MODEL (impl->search_model));
9268 file_list_set_sort_column_ids (impl);
9271 /* Creates a new query with the specified text and launches it */
9273 search_start_query (GtkFileChooserDefault *impl,
9274 const gchar *query_text)
9276 search_stop_searching (impl, FALSE);
9277 search_clear_model (impl, TRUE);
9278 search_setup_model (impl);
9279 set_busy_cursor (impl, TRUE);
9281 if (impl->search_engine == NULL)
9282 impl->search_engine = _gtk_search_engine_new ();
9284 if (!impl->search_engine)
9286 set_busy_cursor (impl, FALSE);
9287 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9291 if (!impl->search_query)
9293 impl->search_query = _gtk_query_new ();
9294 _gtk_query_set_text (impl->search_query, query_text);
9297 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9299 g_signal_connect (impl->search_engine, "hits-added",
9300 G_CALLBACK (search_engine_hits_added_cb), impl);
9301 g_signal_connect (impl->search_engine, "finished",
9302 G_CALLBACK (search_engine_finished_cb), impl);
9303 g_signal_connect (impl->search_engine, "error",
9304 G_CALLBACK (search_engine_error_cb), impl);
9306 _gtk_search_engine_start (impl->search_engine);
9309 /* Callback used when the user presses Enter while typing on the search
9310 * entry; starts the query
9313 search_entry_activate_cb (GtkEntry *entry,
9316 GtkFileChooserDefault *impl;
9319 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9321 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9322 if (strlen (text) == 0)
9325 /* reset any existing query object */
9326 if (impl->search_query)
9328 g_object_unref (impl->search_query);
9329 impl->search_query = NULL;
9332 search_start_query (impl, text);
9336 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9338 gdk_threads_enter ();
9340 g_source_destroy (impl->focus_entry_idle);
9341 impl->focus_entry_idle = NULL;
9343 if (impl->search_entry)
9344 gtk_widget_grab_focus (impl->search_entry);
9346 gdk_threads_leave ();
9352 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9354 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9355 * pane, we get a selection-changed signal and we set up the search widgets.
9356 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9357 * the change to the selection. So, we need to re-focus the search entry
9358 * after the treeview has finished doing its work; we'll do that in an idle
9362 if (!impl->focus_entry_idle)
9363 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9366 /* Hides the path bar and creates the search entry */
9368 search_setup_widgets (GtkFileChooserDefault *impl)
9370 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9372 path_bar_update (impl);
9374 impl->search_entry = gtk_entry_new ();
9375 g_signal_connect (impl->search_entry, "activate",
9376 G_CALLBACK (search_entry_activate_cb),
9378 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9380 /* if there already is a query, restart it */
9381 if (impl->search_query)
9383 gchar *query = _gtk_query_get_text (impl->search_query);
9387 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9388 search_start_query (impl, query);
9394 g_object_unref (impl->search_query);
9395 impl->search_query = NULL;
9399 /* Box for search widgets */
9400 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9401 gtk_widget_show_all (impl->search_hbox);
9402 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9404 /* Hide the location widgets temporarily */
9406 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9407 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9409 gtk_widget_hide (impl->location_button);
9410 gtk_widget_hide (impl->location_entry_box);
9413 focus_search_entry_in_idle (impl);
9415 /* FMQ: hide the filter combo? */
9419 * Recent files support
9422 /* Frees the data in the recent_model */
9424 recent_clear_model (GtkFileChooserDefault *impl,
9425 gboolean remove_from_treeview)
9427 if (!impl->recent_model)
9430 if (remove_from_treeview)
9431 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9433 g_object_unref (impl->recent_model);
9434 impl->recent_model = NULL;
9437 /* Stops any ongoing loading of the recent files list; does
9438 * not touch the recent_model
9441 recent_stop_loading (GtkFileChooserDefault *impl)
9443 if (impl->load_recent_id)
9445 g_source_remove (impl->load_recent_id);
9446 impl->load_recent_id = 0;
9451 recent_setup_model (GtkFileChooserDefault *impl)
9453 g_assert (impl->recent_model == NULL);
9455 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9457 MODEL_COLUMN_TYPES);
9459 _gtk_file_system_model_set_filter (impl->recent_model,
9460 impl->current_filter);
9461 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9465 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9469 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9473 set_sort_column (impl);
9478 GtkFileChooserDefault *impl;
9483 recent_idle_cleanup (gpointer data)
9485 RecentLoadData *load_data = data;
9486 GtkFileChooserDefault *impl = load_data->impl;
9488 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9489 GTK_TREE_MODEL (impl->recent_model));
9490 file_list_set_sort_column_ids (impl);
9491 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9493 set_busy_cursor (impl, FALSE);
9495 impl->load_recent_id = 0;
9501 get_recent_files_limit (GtkWidget *widget)
9503 GtkSettings *settings;
9506 if (gtk_widget_has_screen (widget))
9507 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9509 settings = gtk_settings_get_default ();
9511 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9516 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9518 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9524 limit = get_recent_files_limit (GTK_WIDGET (impl));
9528 for (l = items; l; l = l->next)
9530 GtkRecentInfo *info = l->data;
9533 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9534 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9537 g_object_unref (file);
9540 if (limit != -1 && n >= limit)
9546 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9551 folders = _gtk_file_chooser_extract_recent_folders (items);
9553 for (l = folders; l; l = l->next)
9555 GFile *folder = l->data;
9557 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9562 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9563 g_list_free (folders);
9567 recent_idle_load (gpointer data)
9569 RecentLoadData *load_data = data;
9570 GtkFileChooserDefault *impl = load_data->impl;
9572 if (!impl->recent_manager)
9575 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9576 if (!load_data->items)
9579 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9580 populate_model_with_recent_items (impl, load_data->items);
9582 populate_model_with_folders (impl, load_data->items);
9584 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9585 g_list_free (load_data->items);
9586 load_data->items = NULL;
9592 recent_start_loading (GtkFileChooserDefault *impl)
9594 RecentLoadData *load_data;
9596 recent_stop_loading (impl);
9597 recent_clear_model (impl, TRUE);
9598 recent_setup_model (impl);
9599 set_busy_cursor (impl, TRUE);
9601 g_assert (impl->load_recent_id == 0);
9603 load_data = g_new (RecentLoadData, 1);
9604 load_data->impl = impl;
9605 load_data->items = NULL;
9607 /* begin lazy loading the recent files into the model */
9608 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9611 recent_idle_cleanup);
9615 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9625 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9626 *list = g_slist_prepend (*list, file);
9629 /* Constructs a list of the selected paths in recent files mode */
9631 recent_get_selected_files (GtkFileChooserDefault *impl)
9634 GtkTreeSelection *selection;
9638 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9639 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9640 result = g_slist_reverse (result);
9645 /* Called from ::should_respond(). We return whether there are selected
9646 * files in the recent files list.
9649 recent_should_respond (GtkFileChooserDefault *impl)
9651 GtkTreeSelection *selection;
9653 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9655 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9656 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9660 set_current_filter (GtkFileChooserDefault *impl,
9661 GtkFileFilter *filter)
9663 if (impl->current_filter != filter)
9667 /* NULL filters are allowed to reset to non-filtered status
9669 filter_index = g_slist_index (impl->filters, filter);
9670 if (impl->filters && filter && filter_index < 0)
9673 if (impl->current_filter)
9674 g_object_unref (impl->current_filter);
9675 impl->current_filter = filter;
9676 if (impl->current_filter)
9678 g_object_ref_sink (impl->current_filter);
9682 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9685 if (impl->browse_files_model)
9687 _gtk_file_system_model_set_filter (impl->browse_files_model, impl->current_filter);
9688 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_IS_SENSITIVE);
9691 if (impl->search_model)
9693 _gtk_file_system_model_set_filter (impl->search_model, filter);
9694 _gtk_file_system_model_clear_cache (impl->search_model, MODEL_COL_IS_SENSITIVE);
9697 if (impl->recent_model)
9699 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9700 _gtk_file_system_model_clear_cache (impl->recent_model, MODEL_COL_IS_SENSITIVE);
9703 g_object_notify (G_OBJECT (impl), "filter");
9708 filter_combo_changed (GtkComboBox *combo_box,
9709 GtkFileChooserDefault *impl)
9711 gint new_index = gtk_combo_box_get_active (combo_box);
9712 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9714 set_current_filter (impl, new_filter);
9718 check_preview_change (GtkFileChooserDefault *impl)
9720 GtkTreePath *cursor_path;
9722 char *new_display_name;
9723 GtkTreeModel *model;
9725 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9726 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9731 gtk_tree_model_get_iter (model, &iter, cursor_path);
9732 gtk_tree_model_get (model, &iter,
9733 MODEL_COL_FILE, &new_file,
9734 MODEL_COL_NAME, &new_display_name,
9737 gtk_tree_path_free (cursor_path);
9742 new_display_name = NULL;
9745 if (new_file != impl->preview_file &&
9746 !(new_file && impl->preview_file &&
9747 g_file_equal (new_file, impl->preview_file)))
9749 if (impl->preview_file)
9751 g_object_unref (impl->preview_file);
9752 g_free (impl->preview_display_name);
9757 impl->preview_file = new_file;
9758 impl->preview_display_name = new_display_name;
9762 impl->preview_file = NULL;
9763 impl->preview_display_name = NULL;
9764 g_free (new_display_name);
9767 if (impl->use_preview_label && impl->preview_label)
9768 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9770 g_signal_emit_by_name (impl, "update-preview");
9775 g_object_unref (new_file);
9777 g_free (new_display_name);
9782 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9783 GtkFileSystemVolume *volume,
9784 const GError *error,
9788 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9789 GtkFileChooserDefault *impl = data;
9791 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9794 impl->shortcuts_activate_iter_cancellable = NULL;
9796 set_busy_cursor (impl, FALSE);
9803 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9807 name = _gtk_file_system_volume_get_display_name (volume);
9808 msg = g_strdup_printf (_("Could not mount %s"), name);
9810 error_message (impl, msg, error->message);
9819 file = _gtk_file_system_volume_get_root (volume);
9822 change_folder_and_display_error (impl, file, FALSE);
9823 g_object_unref (file);
9827 g_object_unref (impl);
9828 g_object_unref (cancellable);
9832 /* Activates a volume by mounting it if necessary and then switching to its
9836 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9837 GtkFileSystemVolume *volume)
9841 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9843 /* We ref the file chooser since volume_mount() may run a main loop, and the
9844 * user could close the file chooser window in the meantime.
9846 g_object_ref (impl);
9848 if (!_gtk_file_system_volume_is_mounted (volume))
9850 GMountOperation *mount_op;
9852 set_busy_cursor (impl, TRUE);
9854 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9855 impl->shortcuts_activate_iter_cancellable =
9856 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9857 shortcuts_activate_volume_mount_cb,
9858 g_object_ref (impl));
9859 g_object_unref (mount_op);
9863 file = _gtk_file_system_volume_get_root (volume);
9866 change_folder_and_display_error (impl, file, FALSE);
9867 g_object_unref (file);
9871 g_object_unref (impl);
9874 /* Opens the folder or volume at the specified iter in the shortcuts model */
9875 struct ShortcutsActivateData
9877 GtkFileChooserDefault *impl;
9882 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9884 const GError *error,
9887 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9888 struct ShortcutsActivateData *data = user_data;
9890 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9893 data->impl->shortcuts_activate_iter_cancellable = NULL;
9898 if (!error && _gtk_file_info_consider_as_directory (info))
9899 change_folder_and_display_error (data->impl, data->file, FALSE);
9901 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9906 g_object_unref (data->impl);
9907 g_object_unref (data->file);
9910 g_object_unref (cancellable);
9914 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9915 GtkFileSystemVolume *volume,
9916 const GError *error,
9919 struct ShortcutsActivateData *data = user_data;
9923 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9925 g_object_unref (data->impl);
9926 g_object_unref (data->file);
9932 data->impl->shortcuts_activate_iter_cancellable =
9933 _gtk_file_system_get_info (data->impl->file_system, data->file,
9935 shortcuts_activate_get_info_cb, data);
9938 _gtk_file_system_volume_unref (volume);
9942 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9946 ShortcutType shortcut_type;
9948 /* In the Save modes, we want to preserve what the uesr typed in the filename
9949 * entry, so that he may choose another folder without erasing his typed name.
9951 if (impl->location_entry
9952 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9953 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9954 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
9956 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9957 SHORTCUTS_COL_DATA, &col_data,
9958 SHORTCUTS_COL_TYPE, &shortcut_type,
9961 if (impl->shortcuts_activate_iter_cancellable)
9963 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9964 impl->shortcuts_activate_iter_cancellable = NULL;
9967 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9969 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9971 GtkFileSystemVolume *volume;
9975 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9977 shortcuts_activate_volume (impl, volume);
9979 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9981 struct ShortcutsActivateData *data;
9982 GtkFileSystemVolume *volume;
9984 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9986 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9988 data = g_new0 (struct ShortcutsActivateData, 1);
9989 data->impl = g_object_ref (impl);
9990 data->file = g_object_ref (col_data);
9992 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9994 GMountOperation *mount_operation;
9995 GtkWidget *toplevel;
9997 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9999 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10001 impl->shortcuts_activate_iter_cancellable =
10002 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10004 shortcuts_activate_mount_enclosing_volume,
10009 impl->shortcuts_activate_iter_cancellable =
10010 _gtk_file_system_get_info (impl->file_system, data->file,
10012 shortcuts_activate_get_info_cb, data);
10015 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10017 operation_mode_set (impl, OPERATION_MODE_SEARCH);
10019 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10021 operation_mode_set (impl, OPERATION_MODE_RECENT);
10025 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10027 shortcuts_key_press_event_cb (GtkWidget *widget,
10028 GdkEventKey *event,
10029 GtkFileChooserDefault *impl)
10033 modifiers = gtk_accelerator_get_default_mod_mask ();
10035 if (key_is_left_or_right (event))
10037 gtk_widget_grab_focus (impl->browse_files_tree_view);
10041 if ((event->keyval == GDK_KEY_BackSpace
10042 || event->keyval == GDK_KEY_Delete
10043 || event->keyval == GDK_KEY_KP_Delete)
10044 && (event->state & modifiers) == 0)
10046 remove_selected_bookmarks (impl);
10050 if ((event->keyval == GDK_KEY_F2)
10051 && (event->state & modifiers) == 0)
10053 rename_selected_bookmark (impl);
10061 shortcuts_select_func (GtkTreeSelection *selection,
10062 GtkTreeModel *model,
10064 gboolean path_currently_selected,
10067 GtkFileChooserDefault *impl = data;
10068 GtkTreeIter filter_iter;
10069 ShortcutType shortcut_type;
10071 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10072 g_assert_not_reached ();
10074 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10076 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10080 list_select_func (GtkTreeSelection *selection,
10081 GtkTreeModel *model,
10083 gboolean path_currently_selected,
10086 GtkFileChooserDefault *impl = data;
10088 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10089 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10092 gboolean is_sensitive;
10093 gboolean is_folder;
10095 if (!gtk_tree_model_get_iter (model, &iter, path))
10097 gtk_tree_model_get (model, &iter,
10098 MODEL_COL_IS_SENSITIVE, &is_sensitive,
10099 MODEL_COL_IS_FOLDER, &is_folder,
10101 if (!is_sensitive || !is_folder)
10109 list_selection_changed (GtkTreeSelection *selection,
10110 GtkFileChooserDefault *impl)
10112 /* See if we are in the new folder editable row for Save mode */
10113 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10114 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10117 gboolean had_selection;
10119 info = get_selected_file_info_from_file_list (impl, &had_selection);
10120 if (!had_selection)
10121 goto out; /* normal processing */
10124 return; /* We are on the editable row for New Folder */
10129 if (impl->location_entry)
10130 update_chooser_entry (impl);
10132 path_bar_update (impl);
10134 check_preview_change (impl);
10135 bookmarks_check_add_sensitivity (impl);
10136 check_copy_file_location_sensitivity (impl);
10138 g_signal_emit_by_name (impl, "selection-changed", 0);
10141 /* Callback used when a row in the file list is activated */
10143 list_row_activated (GtkTreeView *tree_view,
10145 GtkTreeViewColumn *column,
10146 GtkFileChooserDefault *impl)
10150 GtkTreeModel *model;
10151 gboolean is_folder;
10152 gboolean is_sensitive;
10154 model = gtk_tree_view_get_model (tree_view);
10156 if (!gtk_tree_model_get_iter (model, &iter, path))
10159 gtk_tree_model_get (model, &iter,
10160 MODEL_COL_FILE, &file,
10161 MODEL_COL_IS_FOLDER, &is_folder,
10162 MODEL_COL_IS_SENSITIVE, &is_sensitive,
10165 if (is_sensitive && is_folder && file)
10167 change_folder_and_display_error (impl, file, FALSE);
10171 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10172 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10173 g_signal_emit_by_name (impl, "file-activated");
10178 g_object_unref (file);
10182 path_bar_clicked (GtkPathBar *path_bar,
10185 gboolean child_is_hidden,
10186 GtkFileChooserDefault *impl)
10189 pending_select_files_add (impl, child_file);
10191 if (!change_folder_and_display_error (impl, file, FALSE))
10194 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10195 * show hidden files so that ".baz" appears in the file list, as it will still
10196 * be shown in the path bar: "/foo/[bar]/.baz"
10198 if (child_is_hidden)
10199 g_object_set (impl, "show-hidden", TRUE, NULL);
10203 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10205 GtkTreeViewColumn *column;
10206 GtkCellRenderer *renderer;
10207 GList *walk, *list;
10209 /* Keep the following column numbers in sync with create_file_list() */
10212 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10213 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10214 for (walk = list; walk; walk = walk->next)
10216 renderer = walk->data;
10217 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10219 gtk_tree_view_column_set_attributes (column, renderer,
10220 "pixbuf", MODEL_COL_PIXBUF,
10225 gtk_tree_view_column_set_attributes (column, renderer,
10226 "text", MODEL_COL_NAME,
10227 "ellipsize", MODEL_COL_ELLIPSIZE,
10231 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10233 g_list_free (list);
10236 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10237 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10238 renderer = list->data;
10239 gtk_tree_view_column_set_attributes (column, renderer,
10240 "text", MODEL_COL_SIZE_TEXT,
10243 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10244 g_list_free (list);
10247 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10248 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10249 renderer = list->data;
10250 gtk_tree_view_column_set_attributes (column, renderer,
10251 "text", MODEL_COL_MTIME_TEXT,
10253 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_SENSITIVE);
10254 g_list_free (list);
10258 _gtk_file_chooser_default_new (void)
10260 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10264 location_set_user_text (GtkFileChooserDefault *impl,
10267 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), path);
10268 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10272 location_popup_handler (GtkFileChooserDefault *impl,
10275 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10277 GtkWidget *widget_to_focus;
10279 operation_mode_set (impl, OPERATION_MODE_BROWSE);
10281 if (impl->current_folder)
10282 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10284 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10285 widget_to_focus = impl->browse_files_tree_view;
10287 widget_to_focus = impl->location_entry;
10289 gtk_widget_grab_focus (widget_to_focus);
10293 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10294 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10299 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10300 location_set_user_text (impl, path);
10302 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10303 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10305 gtk_widget_grab_focus (impl->location_entry);
10307 location_set_user_text (impl, path);
10310 g_assert_not_reached ();
10313 /* Handler for the "up-folder" keybinding signal */
10315 up_folder_handler (GtkFileChooserDefault *impl)
10317 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10320 /* Handler for the "down-folder" keybinding signal */
10322 down_folder_handler (GtkFileChooserDefault *impl)
10324 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10327 /* Switches to the shortcut in the specified index */
10329 switch_to_shortcut (GtkFileChooserDefault *impl,
10334 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10335 g_assert_not_reached ();
10337 shortcuts_activate_iter (impl, &iter);
10340 /* Handler for the "home-folder" keybinding signal */
10342 home_folder_handler (GtkFileChooserDefault *impl)
10344 if (impl->has_home)
10345 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10348 /* Handler for the "desktop-folder" keybinding signal */
10350 desktop_folder_handler (GtkFileChooserDefault *impl)
10352 if (impl->has_desktop)
10353 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10356 /* Handler for the "search-shortcut" keybinding signal */
10358 search_shortcut_handler (GtkFileChooserDefault *impl)
10360 if (impl->has_search)
10362 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10364 /* we want the entry widget to grab the focus the first
10365 * time, not the browse_files_tree_view widget.
10367 if (impl->search_entry)
10368 gtk_widget_grab_focus (impl->search_entry);
10372 /* Handler for the "recent-shortcut" keybinding signal */
10374 recent_shortcut_handler (GtkFileChooserDefault *impl)
10376 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10380 quick_bookmark_handler (GtkFileChooserDefault *impl,
10381 gint bookmark_index)
10386 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10389 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10391 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10392 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10395 gtk_tree_path_free (path);
10397 switch_to_shortcut (impl, bookmark_pos);
10401 show_hidden_handler (GtkFileChooserDefault *impl)
10403 g_object_set (impl,
10404 "show-hidden", !impl->show_hidden,
10409 /* Drag and drop interfaces */
10412 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10417 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10419 model->impl = NULL;
10422 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10424 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10427 ShortcutsPaneModelFilter *model;
10431 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10433 pos = *gtk_tree_path_get_indices (path);
10434 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10436 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10439 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10443 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10445 GtkSelectionData *selection_data)
10452 /* Fill the GtkTreeDragSourceIface vtable */
10454 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10456 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10457 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10461 /* Fill the GtkTreeDragDestIface vtable */
10463 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10465 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10466 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10470 static GtkTreeModel *
10471 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10472 GtkTreeModel *child_model,
10475 ShortcutsPaneModelFilter *model;
10477 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10478 "child-model", child_model,
10479 "virtual-root", root,
10482 model->impl = impl;
10484 return GTK_TREE_MODEL (model);