1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechoosersettings.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystem.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkmountoperation.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkrecentfilter.h"
59 #include "gtkrecentmanager.h"
60 #include "gtkscrolledwindow.h"
61 #include "gtkseparatormenuitem.h"
62 #include "gtksizegroup.h"
65 #include "gtktooltip.h"
66 #include "gtktreednd.h"
67 #include "gtktreeprivate.h"
68 #include "gtktreeselection.h"
78 #include <sys/types.h>
89 #undef PROFILE_FILE_CHOOSER
90 #ifdef PROFILE_FILE_CHOOSER
97 #define PROFILE_INDENT 4
98 static int profile_indent;
101 profile_add_indent (int indent)
103 profile_indent += indent;
104 if (profile_indent < 0)
105 g_error ("You screwed up your indentation");
109 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
114 profile_add_indent (indent);
116 if (profile_indent == 0)
117 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
119 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 profile_add_indent (indent);
128 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
129 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
130 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
132 #define profile_start(x, y)
133 #define profile_end(x, y)
134 #define profile_msg(x, y)
139 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
141 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
142 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
143 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define MAX_LOADING_TIME 500
147 struct _GtkFileChooserDefaultClass
149 GtkVBoxClass parent_class;
155 LOCATION_POPUP_ON_PASTE,
161 LOCATION_TOGGLE_POPUP,
169 static guint signals[LAST_SIGNAL] = { 0 };
171 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
173 SHORTCUTS_COL_PIXBUF,
177 SHORTCUTS_COL_REMOVABLE,
178 SHORTCUTS_COL_PIXBUF_VISIBLE,
179 SHORTCUTS_COL_CANCELLABLE,
180 SHORTCUTS_COL_NUM_COLUMNS
185 SHORTCUT_TYPE_VOLUME,
186 SHORTCUT_TYPE_SEPARATOR,
187 SHORTCUT_TYPE_SEARCH,
191 /* Column numbers for the file list */
196 FILE_LIST_COL_NUM_COLUMNS
199 /* Column numbers for the search model.
200 * Keep this in sync with search_setup_model()
203 SEARCH_MODEL_COL_FILE,
204 SEARCH_MODEL_COL_DISPLAY_NAME,
205 SEARCH_MODEL_COL_COLLATION_KEY,
206 SEARCH_MODEL_COL_STAT,
207 SEARCH_MODEL_COL_CANCELLABLE,
208 SEARCH_MODEL_COL_PIXBUF,
209 SEARCH_MODEL_COL_MIME_TYPE,
210 SEARCH_MODEL_COL_IS_FOLDER,
211 SEARCH_MODEL_COL_NUM_COLUMNS
215 RECENT_MODEL_COL_FILE,
216 RECENT_MODEL_COL_DISPLAY_NAME,
217 RECENT_MODEL_COL_INFO,
218 RECENT_MODEL_COL_IS_FOLDER,
219 RECENT_MODEL_COL_CANCELLABLE,
220 RECENT_MODEL_COL_NUM_COLUMNS
223 /* Identifiers for target types */
229 search_is_possible (GtkFileChooserDefault *impl)
231 if (impl->search_engine == NULL)
232 impl->search_engine = _gtk_search_engine_new ();
234 return impl->search_engine != NULL;
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 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
260 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
262 static GObject* gtk_file_chooser_default_constructor (GType type,
263 guint n_construct_properties,
264 GObjectConstructParam *construct_params);
265 static void gtk_file_chooser_default_finalize (GObject *object);
266 static void gtk_file_chooser_default_set_property (GObject *object,
270 static void gtk_file_chooser_default_get_property (GObject *object,
274 static void gtk_file_chooser_default_dispose (GObject *object);
275 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
276 static void gtk_file_chooser_default_map (GtkWidget *widget);
277 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
278 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
279 GtkWidget *previous_toplevel);
280 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
281 GtkStyle *previous_style);
282 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
283 GdkScreen *previous_screen);
284 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
285 GtkAllocation *allocation);
287 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
290 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
293 gboolean clear_entry,
295 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
296 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
298 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
301 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
305 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
306 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
307 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
313 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
316 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
319 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
321 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
323 gint *default_height);
324 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
325 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
327 static void location_popup_handler (GtkFileChooserDefault *impl,
329 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
330 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
331 static void up_folder_handler (GtkFileChooserDefault *impl);
332 static void down_folder_handler (GtkFileChooserDefault *impl);
333 static void home_folder_handler (GtkFileChooserDefault *impl);
334 static void desktop_folder_handler (GtkFileChooserDefault *impl);
335 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
336 gint bookmark_index);
337 static void show_hidden_handler (GtkFileChooserDefault *impl);
338 static void search_shortcut_handler (GtkFileChooserDefault *impl);
339 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
340 static void update_appearance (GtkFileChooserDefault *impl);
342 static void set_current_filter (GtkFileChooserDefault *impl,
343 GtkFileFilter *filter);
344 static void check_preview_change (GtkFileChooserDefault *impl);
346 static void filter_combo_changed (GtkComboBox *combo_box,
347 GtkFileChooserDefault *impl);
349 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
351 GtkFileChooserDefault *impl);
353 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
356 gboolean path_currently_selected,
358 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
360 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
362 static int shortcuts_get_index (GtkFileChooserDefault *impl,
363 ShortcutsIndex where);
364 static int shortcut_find_position (GtkFileChooserDefault *impl,
367 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
369 static gboolean list_select_func (GtkTreeSelection *selection,
372 gboolean path_currently_selected,
375 static void list_selection_changed (GtkTreeSelection *tree_selection,
376 GtkFileChooserDefault *impl);
377 static void list_row_activated (GtkTreeView *tree_view,
379 GtkTreeViewColumn *column,
380 GtkFileChooserDefault *impl);
382 static void select_func (GtkFileSystemModel *model,
387 static void path_bar_clicked (GtkPathBar *path_bar,
390 gboolean child_is_hidden,
391 GtkFileChooserDefault *impl);
393 static void add_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
395 static void remove_bookmark_button_clicked_cb (GtkButton *button,
396 GtkFileChooserDefault *impl);
397 static void save_folder_combo_changed_cb (GtkComboBox *combo,
398 GtkFileChooserDefault *impl);
400 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
401 GtkCellRenderer *cell,
402 GtkTreeModel *tree_model,
405 static void list_name_data_func (GtkTreeViewColumn *tree_column,
406 GtkCellRenderer *cell,
407 GtkTreeModel *tree_model,
411 static void list_size_data_func (GtkTreeViewColumn *tree_column,
412 GtkCellRenderer *cell,
413 GtkTreeModel *tree_model,
417 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
418 GtkCellRenderer *cell,
419 GtkTreeModel *tree_model,
423 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
426 static void load_remove_timer (GtkFileChooserDefault *impl);
427 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
429 static void location_button_toggled_cb (GtkToggleButton *toggle,
430 GtkFileChooserDefault *impl);
431 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
433 static void search_stop_searching (GtkFileChooserDefault *impl,
434 gboolean remove_query);
435 static void search_clear_model (GtkFileChooserDefault *impl,
436 gboolean remove_from_treeview);
437 static gboolean search_should_respond (GtkFileChooserDefault *impl);
438 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
439 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
440 static void search_entry_activate_cb (GtkEntry *entry,
442 static void settings_load (GtkFileChooserDefault *impl);
443 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
444 GtkTreeIter *child_iter,
447 static void recent_stop_loading (GtkFileChooserDefault *impl);
448 static void recent_clear_model (GtkFileChooserDefault *impl,
449 gboolean remove_from_treeview);
450 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
451 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
452 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
453 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
454 GtkTreeIter *child_iter,
456 static void set_file_system_backend (GtkFileChooserDefault *impl);
462 /* Drag and drop interface declarations */
465 GtkTreeModelFilter parent;
467 GtkFileChooserDefault *impl;
468 } ShortcutsPaneModelFilter;
471 GtkTreeModelFilterClass parent_class;
472 } ShortcutsPaneModelFilterClass;
474 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
475 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
477 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
479 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
480 _shortcuts_pane_model_filter,
481 GTK_TYPE_TREE_MODEL_FILTER,
482 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
483 shortcuts_pane_model_filter_drag_source_iface_init))
485 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
486 GtkTreeModel *child_model,
491 GtkTreeModelSort parent;
493 GtkFileChooserDefault *impl;
497 GtkTreeModelSortClass parent_class;
498 } RecentModelSortClass;
500 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
501 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
503 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
505 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
507 GTK_TYPE_TREE_MODEL_SORT,
508 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
509 recent_model_sort_drag_source_iface_init));
511 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
512 GtkTreeModel *child_model);
516 GtkTreeModelSort parent;
518 GtkFileChooserDefault *impl;
522 GtkTreeModelSortClass parent_class;
523 } SearchModelSortClass;
525 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
526 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
528 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
530 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
532 GTK_TYPE_TREE_MODEL_SORT,
533 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
534 search_model_sort_drag_source_iface_init));
536 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
537 GtkTreeModel *child_model);
541 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
542 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
543 gtk_file_chooser_default_iface_init)
544 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
545 gtk_file_chooser_embed_default_iface_init));
548 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
550 static const guint quick_bookmark_keyvals[10] = {
551 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
553 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
554 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
555 GtkBindingSet *binding_set;
558 gobject_class->finalize = gtk_file_chooser_default_finalize;
559 gobject_class->constructor = gtk_file_chooser_default_constructor;
560 gobject_class->set_property = gtk_file_chooser_default_set_property;
561 gobject_class->get_property = gtk_file_chooser_default_get_property;
562 gobject_class->dispose = gtk_file_chooser_default_dispose;
564 widget_class->show_all = gtk_file_chooser_default_show_all;
565 widget_class->map = gtk_file_chooser_default_map;
566 widget_class->unmap = gtk_file_chooser_default_unmap;
567 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
568 widget_class->style_set = gtk_file_chooser_default_style_set;
569 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
570 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
572 signals[LOCATION_POPUP] =
573 _gtk_binding_signal_new (I_("location-popup"),
574 G_OBJECT_CLASS_TYPE (class),
575 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
576 G_CALLBACK (location_popup_handler),
578 _gtk_marshal_VOID__STRING,
579 G_TYPE_NONE, 1, G_TYPE_STRING);
580 signals[LOCATION_POPUP_ON_PASTE] =
581 _gtk_binding_signal_new ("location-popup-on-paste",
582 G_OBJECT_CLASS_TYPE (class),
583 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
584 G_CALLBACK (location_popup_on_paste_handler),
586 _gtk_marshal_VOID__VOID,
588 signals[LOCATION_TOGGLE_POPUP] =
589 _gtk_binding_signal_new (I_("location-toggle-popup"),
590 G_OBJECT_CLASS_TYPE (class),
591 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
592 G_CALLBACK (location_toggle_popup_handler),
594 _gtk_marshal_VOID__VOID,
597 _gtk_binding_signal_new (I_("up-folder"),
598 G_OBJECT_CLASS_TYPE (class),
599 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
600 G_CALLBACK (up_folder_handler),
602 _gtk_marshal_VOID__VOID,
604 signals[DOWN_FOLDER] =
605 _gtk_binding_signal_new (I_("down-folder"),
606 G_OBJECT_CLASS_TYPE (class),
607 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
608 G_CALLBACK (down_folder_handler),
610 _gtk_marshal_VOID__VOID,
612 signals[HOME_FOLDER] =
613 _gtk_binding_signal_new (I_("home-folder"),
614 G_OBJECT_CLASS_TYPE (class),
615 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
616 G_CALLBACK (home_folder_handler),
618 _gtk_marshal_VOID__VOID,
620 signals[DESKTOP_FOLDER] =
621 _gtk_binding_signal_new (I_("desktop-folder"),
622 G_OBJECT_CLASS_TYPE (class),
623 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
624 G_CALLBACK (desktop_folder_handler),
626 _gtk_marshal_VOID__VOID,
628 signals[QUICK_BOOKMARK] =
629 _gtk_binding_signal_new (I_("quick-bookmark"),
630 G_OBJECT_CLASS_TYPE (class),
631 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
632 G_CALLBACK (quick_bookmark_handler),
634 _gtk_marshal_VOID__INT,
635 G_TYPE_NONE, 1, G_TYPE_INT);
636 signals[SHOW_HIDDEN] =
637 _gtk_binding_signal_new ("show-hidden",
638 G_OBJECT_CLASS_TYPE (class),
639 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
640 G_CALLBACK (show_hidden_handler),
642 _gtk_marshal_VOID__VOID,
644 signals[SEARCH_SHORTCUT] =
645 _gtk_binding_signal_new ("search-shortcut",
646 G_OBJECT_CLASS_TYPE (class),
647 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
648 G_CALLBACK (search_shortcut_handler),
650 _gtk_marshal_VOID__VOID,
652 signals[RECENT_SHORTCUT] =
653 _gtk_binding_signal_new ("recent-shortcut",
654 G_OBJECT_CLASS_TYPE (class),
655 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
656 G_CALLBACK (recent_shortcut_handler),
658 _gtk_marshal_VOID__VOID,
661 binding_set = gtk_binding_set_by_class (class);
663 gtk_binding_entry_add_signal (binding_set,
664 GDK_l, GDK_CONTROL_MASK,
665 "location-toggle-popup",
668 gtk_binding_entry_add_signal (binding_set,
671 1, G_TYPE_STRING, "/");
672 gtk_binding_entry_add_signal (binding_set,
675 1, G_TYPE_STRING, "/");
678 gtk_binding_entry_add_signal (binding_set,
681 1, G_TYPE_STRING, "~");
684 gtk_binding_entry_add_signal (binding_set,
685 GDK_v, GDK_CONTROL_MASK,
686 "location-popup-on-paste",
688 gtk_binding_entry_add_signal (binding_set,
689 GDK_Up, GDK_MOD1_MASK,
692 gtk_binding_entry_add_signal (binding_set,
696 gtk_binding_entry_add_signal (binding_set,
697 GDK_KP_Up, GDK_MOD1_MASK,
701 gtk_binding_entry_add_signal (binding_set,
702 GDK_Down, GDK_MOD1_MASK,
705 gtk_binding_entry_add_signal (binding_set,
706 GDK_KP_Down, GDK_MOD1_MASK,
710 gtk_binding_entry_add_signal (binding_set,
711 GDK_Home, GDK_MOD1_MASK,
714 gtk_binding_entry_add_signal (binding_set,
715 GDK_KP_Home, GDK_MOD1_MASK,
718 gtk_binding_entry_add_signal (binding_set,
719 GDK_d, GDK_MOD1_MASK,
722 gtk_binding_entry_add_signal (binding_set,
723 GDK_h, GDK_CONTROL_MASK,
726 gtk_binding_entry_add_signal (binding_set,
727 GDK_s, GDK_MOD1_MASK,
730 gtk_binding_entry_add_signal (binding_set,
731 GDK_r, GDK_MOD1_MASK,
735 for (i = 0; i < 10; i++)
736 gtk_binding_entry_add_signal (binding_set,
737 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
741 _gtk_file_chooser_install_properties (gobject_class);
745 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
747 iface->select_file = gtk_file_chooser_default_select_file;
748 iface->unselect_file = gtk_file_chooser_default_unselect_file;
749 iface->select_all = gtk_file_chooser_default_select_all;
750 iface->unselect_all = gtk_file_chooser_default_unselect_all;
751 iface->get_files = gtk_file_chooser_default_get_files;
752 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
753 iface->get_file_system = gtk_file_chooser_default_get_file_system;
754 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
755 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
756 iface->set_current_name = gtk_file_chooser_default_set_current_name;
757 iface->add_filter = gtk_file_chooser_default_add_filter;
758 iface->remove_filter = gtk_file_chooser_default_remove_filter;
759 iface->list_filters = gtk_file_chooser_default_list_filters;
760 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
761 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
762 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
766 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
768 iface->get_default_size = gtk_file_chooser_default_get_default_size;
769 iface->should_respond = gtk_file_chooser_default_should_respond;
770 iface->initial_focus = gtk_file_chooser_default_initial_focus;
774 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
776 profile_start ("start", NULL);
777 #ifdef PROFILE_FILE_CHOOSER
778 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
780 impl->local_only = TRUE;
781 impl->preview_widget_active = TRUE;
782 impl->use_preview_label = TRUE;
783 impl->select_multiple = FALSE;
784 impl->show_hidden = FALSE;
785 impl->icon_size = FALLBACK_ICON_SIZE;
786 impl->load_state = LOAD_EMPTY;
787 impl->reload_state = RELOAD_EMPTY;
788 impl->pending_select_files = NULL;
789 impl->location_mode = LOCATION_MODE_PATH_BAR;
790 impl->operation_mode = OPERATION_MODE_BROWSE;
791 impl->recent_manager = gtk_recent_manager_get_default ();
793 gtk_box_set_spacing (GTK_BOX (impl), 12);
795 set_file_system_backend (impl);
797 profile_end ("end", NULL);
800 /* Frees the data columns for the specified iter in the shortcuts model*/
802 shortcuts_free_row_data (GtkFileChooserDefault *impl,
806 ShortcutType shortcut_type;
807 GCancellable *cancellable;
809 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
810 SHORTCUTS_COL_DATA, &col_data,
811 SHORTCUTS_COL_TYPE, &shortcut_type,
812 SHORTCUTS_COL_CANCELLABLE, &cancellable,
816 g_cancellable_cancel (cancellable);
818 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
819 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
823 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
825 GtkFileSystemVolume *volume;
828 _gtk_file_system_volume_free (volume);
834 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
837 g_object_unref (file);
841 /* Frees all the data columns in the shortcuts model */
843 shortcuts_free (GtkFileChooserDefault *impl)
847 if (!impl->shortcuts_model)
850 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
853 shortcuts_free_row_data (impl, &iter);
855 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
857 g_object_unref (impl->shortcuts_model);
858 impl->shortcuts_model = NULL;
862 pending_select_files_free (GtkFileChooserDefault *impl)
864 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
865 g_slist_free (impl->pending_select_files);
866 impl->pending_select_files = NULL;
870 pending_select_files_add (GtkFileChooserDefault *impl,
873 impl->pending_select_files =
874 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
877 /* Used from gtk_tree_selection_selected_foreach() */
879 store_selection_foreach (GtkTreeModel *model,
884 GtkFileChooserDefault *impl;
885 GtkTreeIter child_iter;
888 impl = GTK_FILE_CHOOSER_DEFAULT (data);
890 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
892 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
893 pending_select_files_add (impl, file);
896 /* Stores the current selection in the list of paths to select; this is used to
897 * preserve the selection when reloading the current folder.
900 pending_select_files_store_selection (GtkFileChooserDefault *impl)
902 GtkTreeSelection *selection;
904 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
905 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
909 gtk_file_chooser_default_finalize (GObject *object)
911 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
914 if (impl->shortcuts_pane_filter_model)
915 g_object_unref (impl->shortcuts_pane_filter_model);
917 if (impl->shortcuts_combo_filter_model)
918 g_object_unref (impl->shortcuts_combo_filter_model);
920 shortcuts_free (impl);
922 g_object_unref (impl->file_system);
924 g_free (impl->browse_files_last_selected_name);
926 for (l = impl->filters; l; l = l->next)
928 GtkFileFilter *filter;
930 filter = GTK_FILE_FILTER (l->data);
931 g_object_unref (filter);
933 g_slist_free (impl->filters);
935 if (impl->current_filter)
936 g_object_unref (impl->current_filter);
938 if (impl->current_volume_file)
939 g_object_unref (impl->current_volume_file);
941 if (impl->current_folder)
942 g_object_unref (impl->current_folder);
944 if (impl->preview_file)
945 g_object_unref (impl->preview_file);
947 load_remove_timer (impl);
949 /* Free all the Models we have */
950 if (impl->browse_files_model)
951 g_object_unref (impl->browse_files_model);
953 if (impl->sort_model)
954 g_object_unref (impl->sort_model);
956 search_clear_model (impl, FALSE);
957 recent_clear_model (impl, FALSE);
959 g_free (impl->preview_display_name);
961 g_free (impl->edited_new_text);
963 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
966 /* Shows an error dialog set as transient for the specified window */
968 error_message_with_parent (GtkWindow *parent,
974 dialog = gtk_message_dialog_new (parent,
975 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
980 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
984 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
986 gtk_dialog_run (GTK_DIALOG (dialog));
987 gtk_widget_destroy (dialog);
990 /* Returns a toplevel GtkWindow, or NULL if none */
992 get_toplevel (GtkWidget *widget)
996 toplevel = gtk_widget_get_toplevel (widget);
997 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1000 return GTK_WINDOW (toplevel);
1003 /* Shows an error dialog for the file chooser */
1005 error_message (GtkFileChooserDefault *impl,
1009 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1012 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1014 error_dialog (GtkFileChooserDefault *impl,
1025 uri = g_file_get_uri (file);
1026 text = g_strdup_printf (msg, uri);
1027 error_message (impl, text, error->message);
1030 g_error_free (error);
1034 /* Displays an error message about not being able to get information for a file.
1035 * Frees the GError as well.
1038 error_getting_info_dialog (GtkFileChooserDefault *impl,
1043 _("Could not retrieve information about the file"),
1047 /* Shows an error dialog about not being able to add a bookmark */
1049 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1054 _("Could not add a bookmark"),
1058 /* Shows an error dialog about not being able to remove a bookmark */
1060 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1065 _("Could not remove bookmark"),
1069 /* Shows an error dialog about not being able to create a folder */
1071 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1076 _("The folder could not be created"),
1080 /* Shows an error about not being able to create a folder because a file with
1081 * the same name is already there.
1084 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1089 _("The folder could not be created, as a file with the same "
1090 "name already exists. Try using a different name for the "
1091 "folder, or rename the file first."),
1095 /* Shows an error dialog about not being able to create a filename */
1097 error_building_filename_dialog (GtkFileChooserDefault *impl,
1100 error_dialog (impl, _("Invalid file name"),
1104 /* Shows an error dialog when we cannot switch to a folder */
1106 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1110 error_dialog (impl, _("The folder contents could not be displayed"),
1114 /* Changes folders, displaying an error dialog if this fails */
1116 change_folder_and_display_error (GtkFileChooserDefault *impl,
1118 gboolean clear_entry)
1123 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1125 /* We copy the path because of this case:
1127 * list_row_activated()
1128 * fetches path from model; path belongs to the model (*)
1129 * calls change_folder_and_display_error()
1130 * calls _gtk_file_chooser_set_current_folder_file()
1131 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1135 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1138 error_changing_folder_dialog (impl, file, error);
1144 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1146 if (impl->use_preview_label)
1148 if (!impl->preview_label)
1150 impl->preview_label = gtk_label_new (impl->preview_display_name);
1151 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1152 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1153 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1154 gtk_widget_show (impl->preview_label);
1159 if (impl->preview_label)
1161 gtk_widget_destroy (impl->preview_label);
1162 impl->preview_label = NULL;
1166 if (impl->preview_widget_active && impl->preview_widget)
1167 gtk_widget_show (impl->preview_box);
1169 gtk_widget_hide (impl->preview_box);
1171 g_signal_emit_by_name (impl, "default-size-changed");
1175 set_preview_widget (GtkFileChooserDefault *impl,
1176 GtkWidget *preview_widget)
1178 if (preview_widget == impl->preview_widget)
1181 if (impl->preview_widget)
1182 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1183 impl->preview_widget);
1185 impl->preview_widget = preview_widget;
1186 if (impl->preview_widget)
1188 gtk_widget_show (impl->preview_widget);
1189 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1190 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1191 impl->preview_widget,
1192 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1195 update_preview_widget_visibility (impl);
1198 /* Renders a "Search" icon at an appropriate size for a tree view */
1200 render_search_icon (GtkFileChooserDefault *impl)
1202 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1206 render_recent_icon (GtkFileChooserDefault *impl)
1208 GtkIconTheme *theme;
1211 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1212 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1214 theme = gtk_icon_theme_get_default ();
1216 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1222 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1228 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1229 struct ReloadIconsData
1231 GtkFileChooserDefault *impl;
1232 GtkTreeRowReference *row_ref;
1236 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1238 const GError *error,
1244 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1245 struct ReloadIconsData *data = user_data;
1247 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1250 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1252 if (cancelled || error)
1255 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1257 path = gtk_tree_row_reference_get_path (data->row_ref);
1258 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1259 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1260 SHORTCUTS_COL_PIXBUF, pixbuf,
1262 gtk_tree_path_free (path);
1265 g_object_unref (pixbuf);
1268 gtk_tree_row_reference_free (data->row_ref);
1269 g_object_unref (data->impl);
1272 g_object_unref (cancellable);
1276 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1281 profile_start ("start", NULL);
1283 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1286 for (l = impl->reload_icon_cancellables; l; l = l->next)
1288 GCancellable *cancellable = G_CANCELLABLE (l->data);
1289 g_cancellable_cancel (cancellable);
1291 g_slist_free (impl->reload_icon_cancellables);
1292 impl->reload_icon_cancellables = NULL;
1297 ShortcutType shortcut_type;
1298 gboolean pixbuf_visible;
1301 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1302 SHORTCUTS_COL_DATA, &data,
1303 SHORTCUTS_COL_TYPE, &shortcut_type,
1304 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1310 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1312 GtkFileSystemVolume *volume;
1315 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1316 impl->icon_size, NULL);
1318 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1320 if (g_file_is_native (G_FILE (data)))
1323 struct ReloadIconsData *info;
1324 GtkTreePath *tree_path;
1325 GCancellable *cancellable;
1329 info = g_new0 (struct ReloadIconsData, 1);
1330 info->impl = g_object_ref (impl);
1331 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1332 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1333 gtk_tree_path_free (tree_path);
1335 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1337 shortcuts_reload_icons_get_info_cb,
1339 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1343 GtkIconTheme *icon_theme;
1345 /* Don't call get_info for remote paths to avoid latency and
1347 * If we switch to a better bookmarks file format (XBEL), we
1348 * should use mime info to get a better icon.
1350 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1351 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1352 impl->icon_size, 0, NULL);
1355 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1357 pixbuf = render_search_icon (impl);
1359 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1361 pixbuf = render_recent_icon (impl);
1364 gtk_list_store_set (impl->shortcuts_model, &iter,
1365 SHORTCUTS_COL_PIXBUF, pixbuf,
1369 g_object_unref (pixbuf);
1373 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1377 profile_end ("end", NULL);
1381 shortcuts_find_folder (GtkFileChooserDefault *impl,
1384 GtkTreeSelection *selection;
1388 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1390 g_assert (folder != NULL);
1391 pos = shortcut_find_position (impl, folder);
1394 gtk_tree_selection_unselect_all (selection);
1398 path = gtk_tree_path_new_from_indices (pos, -1);
1399 gtk_tree_selection_select_path (selection, path);
1400 gtk_tree_path_free (path);
1403 /* If a shortcut corresponds to the current folder, selects it */
1405 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1407 shortcuts_find_folder (impl, impl->current_folder);
1410 /* Removes the specified number of rows from the shortcuts list */
1412 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1418 path = gtk_tree_path_new_from_indices (start_row, -1);
1420 for (; n_rows; n_rows--)
1424 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1425 g_assert_not_reached ();
1427 shortcuts_free_row_data (impl, &iter);
1428 gtk_list_store_remove (impl->shortcuts_model, &iter);
1431 gtk_tree_path_free (path);
1435 shortcuts_update_count (GtkFileChooserDefault *impl,
1436 ShortcutsIndex type,
1441 case SHORTCUTS_HOME:
1443 impl->has_home = FALSE;
1445 impl->has_home = TRUE;
1448 case SHORTCUTS_DESKTOP:
1450 impl->has_desktop = FALSE;
1452 impl->has_desktop = TRUE;
1455 case SHORTCUTS_VOLUMES:
1456 impl->num_volumes += value;
1459 case SHORTCUTS_SHORTCUTS:
1460 impl->num_shortcuts += value;
1463 case SHORTCUTS_BOOKMARKS:
1464 impl->num_bookmarks += value;
1467 case SHORTCUTS_CURRENT_FOLDER:
1469 impl->shortcuts_current_folder_active = FALSE;
1471 impl->shortcuts_current_folder_active = TRUE;
1480 struct ShortcutsInsertRequest
1482 GtkFileChooserDefault *impl;
1486 GtkTreeRowReference *row_ref;
1487 ShortcutsIndex type;
1493 get_file_info_finished (GCancellable *cancellable,
1495 const GError *error,
1499 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1503 GCancellable *model_cancellable;
1504 struct ShortcutsInsertRequest *request = data;
1506 path = gtk_tree_row_reference_get_path (request->row_ref);
1508 /* Handle doesn't exist anymore in the model */
1511 pos = gtk_tree_path_get_indices (path)[0];
1512 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1514 gtk_tree_path_free (path);
1516 /* validate cancellable, else goto out */
1517 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1518 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1520 if (cancellable != model_cancellable)
1523 /* set the cancellable to NULL in the model (we unref later on) */
1524 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1525 SHORTCUTS_COL_CANCELLABLE, NULL,
1533 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1534 shortcuts_update_count (request->impl, request->type, -1);
1536 if (request->type == SHORTCUTS_HOME)
1540 home = g_file_new_for_path (g_get_home_dir ());
1541 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1542 g_object_unref (home);
1544 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1546 /* Remove the current folder separator */
1547 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1548 shortcuts_remove_rows (request->impl, separator_pos, 1);
1554 if (!request->label_copy)
1555 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1556 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1557 request->impl->icon_size);
1559 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1560 SHORTCUTS_COL_PIXBUF, pixbuf,
1561 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1562 SHORTCUTS_COL_NAME, request->label_copy,
1563 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1564 SHORTCUTS_COL_REMOVABLE, request->removable,
1567 if (request->impl->shortcuts_pane_filter_model)
1568 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1570 if (request->impl->shortcuts_combo_filter_model)
1571 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1573 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1574 request->impl->save_folder_combo != NULL)
1576 /* The current folder is updated via _activate_iter(), don't
1577 * have save_folder_combo_changed_cb() call _activate_iter()
1580 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1581 G_CALLBACK (save_folder_combo_changed_cb),
1584 if (request->impl->has_search)
1587 if (request->impl->has_recent)
1590 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1591 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1592 G_CALLBACK (save_folder_combo_changed_cb),
1597 g_object_unref (pixbuf);
1600 g_object_unref (request->impl);
1601 g_object_unref (request->file);
1602 gtk_tree_row_reference_free (request->row_ref);
1603 g_free (request->label_copy);
1606 g_object_unref (cancellable);
1609 /* FIXME: GtkFileSystem needs a function to split a remote path
1610 * into hostname and path components, or maybe just have a
1611 * gtk_file_system_path_get_display_name().
1613 * This function is also used in gtkfilechooserbutton.c
1616 _gtk_file_chooser_label_for_file (GFile *file)
1618 const gchar *path, *start, *end, *p;
1619 gchar *uri, *host, *label;
1621 uri = g_file_get_uri (file);
1623 start = strstr (uri, "://");
1625 path = strchr (start, '/');
1631 end = uri + strlen (uri);
1635 /* strip username */
1636 p = strchr (start, '@');
1642 p = strchr (start, ':');
1646 host = g_strndup (start, end - start);
1648 /* Translators: the first string is a path and the second string
1649 * is a hostname. Nautilus and the panel contain the same string
1652 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1660 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1661 * inserts a volume. A position of -1 indicates the end of the tree.
1664 shortcuts_insert_file (GtkFileChooserDefault *impl,
1666 ShortcutType shortcut_type,
1667 GtkFileSystemVolume *volume,
1671 ShortcutsIndex type)
1674 GdkPixbuf *pixbuf = NULL;
1675 gpointer data = NULL;
1677 GtkIconTheme *icon_theme;
1679 profile_start ("start shortcut", NULL);
1681 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1684 label_copy = _gtk_file_system_volume_get_display_name (volume);
1685 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1686 impl->icon_size, NULL);
1688 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1690 if (g_file_is_native (file))
1692 struct ShortcutsInsertRequest *request;
1693 GCancellable *cancellable;
1696 request = g_new0 (struct ShortcutsInsertRequest, 1);
1697 request->impl = g_object_ref (impl);
1698 request->file = g_object_ref (file);
1699 request->name_only = TRUE;
1700 request->removable = removable;
1702 request->type = type;
1704 request->label_copy = g_strdup (label);
1707 gtk_list_store_append (impl->shortcuts_model, &iter);
1709 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1711 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1712 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1713 gtk_tree_path_free (p);
1715 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1716 "standard::is-hidden,standard::display-name,standard::icon",
1717 get_file_info_finished, request);
1719 gtk_list_store_set (impl->shortcuts_model, &iter,
1720 SHORTCUTS_COL_DATA, g_object_ref (file),
1721 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1722 SHORTCUTS_COL_CANCELLABLE, cancellable,
1725 shortcuts_update_count (impl, type, 1);
1731 /* Don't call get_info for remote paths to avoid latency and
1734 data = g_object_ref (file);
1736 label_copy = g_strdup (label);
1738 label_copy = _gtk_file_chooser_label_for_file (file);
1740 /* If we switch to a better bookmarks file format (XBEL), we
1741 * should use mime info to get a better icon.
1743 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1744 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1745 impl->icon_size, 0, NULL);
1750 g_assert_not_reached ();
1756 gtk_list_store_append (impl->shortcuts_model, &iter);
1758 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1760 shortcuts_update_count (impl, type, 1);
1762 gtk_list_store_set (impl->shortcuts_model, &iter,
1763 SHORTCUTS_COL_PIXBUF, pixbuf,
1764 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1765 SHORTCUTS_COL_NAME, label_copy,
1766 SHORTCUTS_COL_DATA, data,
1767 SHORTCUTS_COL_TYPE, shortcut_type,
1768 SHORTCUTS_COL_REMOVABLE, removable,
1769 SHORTCUTS_COL_CANCELLABLE, NULL,
1772 if (impl->shortcuts_pane_filter_model)
1773 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1775 if (impl->shortcuts_combo_filter_model)
1776 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1778 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1780 /* The current folder is updated via _activate_iter(), don't
1781 * have save_folder_combo_changed_cb() call _activate_iter()
1784 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1786 if (impl->has_search)
1789 if (impl->has_recent)
1792 g_signal_handlers_block_by_func (impl->save_folder_combo,
1793 G_CALLBACK (save_folder_combo_changed_cb),
1796 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1797 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1798 G_CALLBACK (save_folder_combo_changed_cb),
1802 g_free (label_copy);
1805 g_object_unref (pixbuf);
1807 profile_end ("end", NULL);
1811 shortcuts_append_search (GtkFileChooserDefault *impl)
1816 pixbuf = render_search_icon (impl);
1818 gtk_list_store_append (impl->shortcuts_model, &iter);
1819 gtk_list_store_set (impl->shortcuts_model, &iter,
1820 SHORTCUTS_COL_PIXBUF, pixbuf,
1821 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1822 SHORTCUTS_COL_NAME, _("Search"),
1823 SHORTCUTS_COL_DATA, NULL,
1824 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1825 SHORTCUTS_COL_REMOVABLE, FALSE,
1829 g_object_unref (pixbuf);
1831 impl->has_search = TRUE;
1835 shortcuts_append_recent (GtkFileChooserDefault *impl)
1840 pixbuf = render_recent_icon (impl);
1842 gtk_list_store_append (impl->shortcuts_model, &iter);
1843 gtk_list_store_set (impl->shortcuts_model, &iter,
1844 SHORTCUTS_COL_PIXBUF, pixbuf,
1845 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1846 SHORTCUTS_COL_NAME, _("Recently Used"),
1847 SHORTCUTS_COL_DATA, NULL,
1848 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1849 SHORTCUTS_COL_REMOVABLE, FALSE,
1853 g_object_unref (pixbuf);
1855 impl->has_recent = TRUE;
1858 /* Appends an item for the user's home directory to the shortcuts model */
1860 shortcuts_append_home (GtkFileChooserDefault *impl)
1862 const char *home_path;
1865 profile_start ("start", NULL);
1867 home_path = g_get_home_dir ();
1868 if (home_path == NULL)
1870 profile_end ("end - no home directory!?", NULL);
1874 home = g_file_new_for_path (home_path);
1875 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1876 impl->has_home = TRUE;
1878 g_object_unref (home);
1880 profile_end ("end", NULL);
1883 /* Appends the ~/Desktop directory to the shortcuts model */
1885 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1890 profile_start ("start", NULL);
1892 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1893 file = g_file_new_for_path (name);
1894 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1895 impl->has_desktop = TRUE;
1897 /* We do not actually pop up an error dialog if there is no desktop directory
1898 * because some people may really not want to have one.
1901 g_object_unref (file);
1903 profile_end ("end", NULL);
1906 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1908 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1915 profile_start ("start", NULL);
1917 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1920 for (; bookmarks; bookmarks = bookmarks->next)
1924 file = bookmarks->data;
1926 if (impl->local_only && !g_file_is_native (file))
1929 if (shortcut_find_position (impl, file) != -1)
1932 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1934 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1938 profile_end ("end", NULL);
1940 return num_inserted;
1943 /* Returns the index for the corresponding item in the shortcuts bar */
1945 shortcuts_get_index (GtkFileChooserDefault *impl,
1946 ShortcutsIndex where)
1952 if (where == SHORTCUTS_SEARCH)
1955 n += impl->has_search ? 1 : 0;
1957 if (where == SHORTCUTS_RECENT)
1960 n += impl->has_recent ? 1 : 0;
1962 if (where == SHORTCUTS_RECENT_SEPARATOR)
1965 n += impl->has_recent ? 1 : 0;
1967 if (where == SHORTCUTS_HOME)
1970 n += impl->has_home ? 1 : 0;
1972 if (where == SHORTCUTS_DESKTOP)
1975 n += impl->has_desktop ? 1 : 0;
1977 if (where == SHORTCUTS_VOLUMES)
1980 n += impl->num_volumes;
1982 if (where == SHORTCUTS_SHORTCUTS)
1985 n += impl->num_shortcuts;
1987 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1990 /* If there are no bookmarks there won't be a separator */
1991 n += (impl->num_bookmarks > 0) ? 1 : 0;
1993 if (where == SHORTCUTS_BOOKMARKS)
1996 n += impl->num_bookmarks;
1998 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2003 if (where == SHORTCUTS_CURRENT_FOLDER)
2006 g_assert_not_reached ();
2013 /* Adds all the file system volumes to the shortcuts model */
2015 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2020 gboolean old_changing_folders;
2022 profile_start ("start", NULL);
2024 old_changing_folders = impl->changing_folder;
2025 impl->changing_folder = TRUE;
2027 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2028 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2029 impl->num_volumes = 0;
2031 list = _gtk_file_system_list_volumes (impl->file_system);
2035 for (l = list; l; l = l->next)
2037 GtkFileSystemVolume *volume;
2041 if (impl->local_only)
2043 if (_gtk_file_system_volume_is_mounted (volume))
2047 base_file = _gtk_file_system_volume_get_root (volume);
2048 if (base_file != NULL && !g_file_is_native (base_file))
2053 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2057 impl->num_volumes = n;
2058 g_slist_free (list);
2060 if (impl->shortcuts_pane_filter_model)
2061 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2063 if (impl->shortcuts_combo_filter_model)
2064 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2066 impl->changing_folder = old_changing_folders;
2068 profile_end ("end", NULL);
2071 /* Inserts a separator node in the shortcuts list */
2073 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2074 ShortcutsIndex where)
2078 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2079 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2080 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2082 gtk_list_store_insert (impl->shortcuts_model, &iter,
2083 shortcuts_get_index (impl, where));
2084 gtk_list_store_set (impl->shortcuts_model, &iter,
2085 SHORTCUTS_COL_PIXBUF, NULL,
2086 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2087 SHORTCUTS_COL_NAME, NULL,
2088 SHORTCUTS_COL_DATA, NULL,
2089 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2093 /* Updates the list of bookmarks */
2095 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2098 gboolean old_changing_folders;
2100 GFile *list_selected = NULL;
2101 GFile *combo_selected = NULL;
2102 ShortcutType shortcut_type;
2105 profile_start ("start", NULL);
2107 old_changing_folders = impl->changing_folder;
2108 impl->changing_folder = TRUE;
2110 if (shortcuts_get_selected (impl, &iter))
2112 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2114 SHORTCUTS_COL_DATA, &col_data,
2115 SHORTCUTS_COL_TYPE, &shortcut_type,
2118 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2119 list_selected = g_object_ref (col_data);
2122 if (impl->save_folder_combo &&
2123 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2126 GtkTreeIter child_iter;
2128 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2131 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2133 SHORTCUTS_COL_DATA, &col_data,
2134 SHORTCUTS_COL_TYPE, &shortcut_type,
2137 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2138 combo_selected = g_object_ref (col_data);
2141 if (impl->num_bookmarks > 0)
2142 shortcuts_remove_rows (impl,
2143 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2144 impl->num_bookmarks + 1);
2146 impl->num_bookmarks = 0;
2147 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2149 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2150 shortcuts_append_bookmarks (impl, bookmarks);
2151 g_slist_free (bookmarks);
2153 if (impl->num_bookmarks == 0)
2154 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2156 if (impl->shortcuts_pane_filter_model)
2157 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2159 if (impl->shortcuts_combo_filter_model)
2160 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2164 shortcuts_find_folder (impl, list_selected);
2165 g_object_unref (list_selected);
2172 pos = shortcut_find_position (impl, combo_selected);
2175 if (impl->has_search)
2178 if (impl->has_recent)
2181 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2184 g_object_unref (combo_selected);
2187 impl->changing_folder = old_changing_folders;
2189 profile_end ("end", NULL);
2192 /* Appends a separator and a row to the shortcuts list for the current folder */
2194 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2199 g_assert (!impl->shortcuts_current_folder_active);
2203 g_assert (impl->current_folder != NULL);
2205 pos = shortcut_find_position (impl, impl->current_folder);
2208 GtkFileSystemVolume *volume;
2213 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2217 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2219 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2221 base_file = _gtk_file_system_volume_get_root (volume);
2225 if (base_file && g_file_equal (base_file, impl->current_folder))
2226 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2228 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2231 g_object_unref (base_file);
2233 else if (impl->save_folder_combo != NULL)
2235 if (impl->has_search)
2238 if (impl->has_recent)
2239 pos -= 2; /* + separator */
2241 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2245 /* Updates the current folder row in the shortcuts model */
2247 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2251 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2253 if (impl->shortcuts_current_folder_active)
2255 shortcuts_remove_rows (impl, pos, 2);
2256 impl->shortcuts_current_folder_active = FALSE;
2259 shortcuts_add_current_folder (impl);
2262 /* Filter function used for the shortcuts filter model */
2264 shortcuts_pane_filter_cb (GtkTreeModel *model,
2268 GtkFileChooserDefault *impl;
2272 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2274 path = gtk_tree_model_get_path (model, iter);
2278 pos = *gtk_tree_path_get_indices (path);
2279 gtk_tree_path_free (path);
2281 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2284 /* Creates the list model for shortcuts */
2286 shortcuts_model_create (GtkFileChooserDefault *impl)
2288 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2289 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2290 GDK_TYPE_PIXBUF, /* pixbuf */
2291 G_TYPE_STRING, /* name */
2292 G_TYPE_POINTER, /* path or volume */
2293 G_TYPE_INT, /* ShortcutType */
2294 G_TYPE_BOOLEAN, /* removable */
2295 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2296 G_TYPE_POINTER); /* GCancellable */
2298 if (search_is_possible (impl))
2300 shortcuts_append_search (impl);
2303 if (impl->recent_manager)
2305 shortcuts_append_recent (impl);
2306 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2309 if (impl->file_system)
2311 shortcuts_append_home (impl);
2312 shortcuts_append_desktop (impl);
2313 shortcuts_add_volumes (impl);
2316 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2317 GTK_TREE_MODEL (impl->shortcuts_model),
2320 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2321 shortcuts_pane_filter_cb,
2326 /* Callback used when the "New Folder" button is clicked */
2328 new_folder_button_clicked (GtkButton *button,
2329 GtkFileChooserDefault *impl)
2334 if (!impl->browse_files_model)
2335 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2337 /* Prevent button from being clicked twice */
2338 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2340 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2342 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2343 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2344 path, impl->list_name_column,
2347 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2348 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2350 impl->list_name_column,
2353 gtk_tree_path_free (path);
2356 /* Idle handler for creating a new folder after editing its name cell, or for
2357 * canceling the editing.
2360 edited_idle_cb (GtkFileChooserDefault *impl)
2362 GDK_THREADS_ENTER ();
2364 g_source_destroy (impl->edited_idle);
2365 impl->edited_idle = NULL;
2367 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2368 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2370 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2372 if (impl->edited_new_text) /* not cancelled? */
2374 GError *error = NULL;
2377 file = g_file_get_child_for_display_name (impl->current_folder,
2378 impl->edited_new_text,
2382 GError *error = NULL;
2384 if (!g_file_make_directory (file, NULL, &error))
2385 change_folder_and_display_error (impl, file, FALSE);
2387 error_creating_folder_dialog (impl, file, error);
2389 g_object_unref (file);
2392 error_creating_folder_dialog (impl, file, error);
2394 g_free (impl->edited_new_text);
2395 impl->edited_new_text = NULL;
2398 GDK_THREADS_LEAVE ();
2404 queue_edited_idle (GtkFileChooserDefault *impl,
2405 const gchar *new_text)
2407 /* We create the folder in an idle handler so that we don't modify the tree
2411 if (!impl->edited_idle)
2413 impl->edited_idle = g_idle_source_new ();
2414 g_source_set_closure (impl->edited_idle,
2415 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2417 g_source_attach (impl->edited_idle, NULL);
2420 g_free (impl->edited_new_text);
2421 impl->edited_new_text = g_strdup (new_text);
2424 /* Callback used from the text cell renderer when the new folder is named */
2426 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2428 const gchar *new_text,
2429 GtkFileChooserDefault *impl)
2431 /* work around bug #154921 */
2432 g_object_set (cell_renderer_text,
2433 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2434 queue_edited_idle (impl, new_text);
2437 /* Callback used from the text cell renderer when the new folder edition gets
2441 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2442 GtkFileChooserDefault *impl)
2444 /* work around bug #154921 */
2445 g_object_set (cell_renderer_text,
2446 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2447 queue_edited_idle (impl, NULL);
2450 /* Creates the widgets for the filter combo box */
2452 filter_create (GtkFileChooserDefault *impl)
2454 impl->filter_combo = gtk_combo_box_new_text ();
2455 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2457 g_signal_connect (impl->filter_combo, "changed",
2458 G_CALLBACK (filter_combo_changed), impl);
2460 gtk_widget_set_tooltip_text (impl->filter_combo,
2461 _("Select which types of files are shown"));
2463 return impl->filter_combo;
2467 button_new (GtkFileChooserDefault *impl,
2469 const char *stock_id,
2477 button = gtk_button_new_with_mnemonic (text);
2478 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2479 gtk_button_set_image (GTK_BUTTON (button), image);
2481 gtk_widget_set_sensitive (button, sensitive);
2482 g_signal_connect (button, "clicked", callback, impl);
2485 gtk_widget_show (button);
2490 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2492 shortcut_find_position (GtkFileChooserDefault *impl,
2497 int current_folder_separator_idx;
2499 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2502 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2505 /* FIXME: is this still needed? */
2506 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2510 for (i = 0; i < current_folder_separator_idx; i++)
2513 ShortcutType shortcut_type;
2515 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2516 SHORTCUTS_COL_DATA, &col_data,
2517 SHORTCUTS_COL_TYPE, &shortcut_type,
2522 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2524 GtkFileSystemVolume *volume;
2529 base_file = _gtk_file_system_volume_get_root (volume);
2531 exists = base_file && g_file_equal (file, base_file);
2534 g_object_unref (base_file);
2539 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2543 model_file = col_data;
2545 if (model_file && g_file_equal (model_file, file))
2550 if (i < current_folder_separator_idx - 1)
2552 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2553 g_assert_not_reached ();
2560 /* Tries to add a bookmark from a path name */
2562 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2568 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2570 if (shortcut_find_position (impl, file) != -1)
2574 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2576 error_adding_bookmark_dialog (impl, file, error);
2584 add_bookmark_foreach_cb (GtkTreeModel *model,
2589 GtkFileChooserDefault *impl;
2590 GtkFileSystemModel *fs_model;
2591 GtkTreeIter child_iter;
2594 impl = (GtkFileChooserDefault *) data;
2596 switch (impl->operation_mode)
2598 case OPERATION_MODE_BROWSE:
2599 fs_model = impl->browse_files_model;
2600 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2601 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2604 case OPERATION_MODE_SEARCH:
2605 search_get_valid_child_iter (impl, &child_iter, iter);
2606 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2607 SEARCH_MODEL_COL_FILE, &file,
2611 case OPERATION_MODE_RECENT:
2612 recent_get_valid_child_iter (impl, &child_iter, iter);
2613 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2614 RECENT_MODEL_COL_FILE, &file,
2619 shortcuts_add_bookmark_from_file (impl, file, -1);
2622 /* Adds a bookmark from the currently selected item in the file list */
2624 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2626 GtkTreeSelection *selection;
2628 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2630 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2631 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2633 gtk_tree_selection_selected_foreach (selection,
2634 add_bookmark_foreach_cb,
2638 /* Callback used when the "Add bookmark" button is clicked */
2640 add_bookmark_button_clicked_cb (GtkButton *button,
2641 GtkFileChooserDefault *impl)
2643 bookmarks_add_selected_folder (impl);
2646 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2647 * returns FALSE if no shortcut is selected.
2650 shortcuts_get_selected (GtkFileChooserDefault *impl,
2653 GtkTreeSelection *selection;
2654 GtkTreeIter parent_iter;
2656 if (!impl->browse_shortcuts_tree_view)
2659 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2661 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2664 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2670 /* Removes the selected bookmarks */
2672 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2680 if (!shortcuts_get_selected (impl, &iter))
2683 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2684 SHORTCUTS_COL_DATA, &col_data,
2685 SHORTCUTS_COL_REMOVABLE, &removable,
2691 g_assert (col_data != NULL);
2696 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2697 error_removing_bookmark_dialog (impl, file, error);
2700 /* Callback used when the "Remove bookmark" button is clicked */
2702 remove_bookmark_button_clicked_cb (GtkButton *button,
2703 GtkFileChooserDefault *impl)
2705 remove_selected_bookmarks (impl);
2708 struct selection_check_closure {
2709 GtkFileChooserDefault *impl;
2712 gboolean all_folders;
2715 /* Used from gtk_tree_selection_selected_foreach() */
2717 selection_check_foreach_cb (GtkTreeModel *model,
2722 struct selection_check_closure *closure;
2723 GtkTreeIter child_iter;
2728 closure->num_selected++;
2730 switch (closure->impl->operation_mode)
2732 case OPERATION_MODE_BROWSE:
2733 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2734 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2735 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2738 case OPERATION_MODE_SEARCH:
2739 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2740 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2741 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2745 case OPERATION_MODE_RECENT:
2746 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2747 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2748 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2753 closure->all_folders = closure->all_folders && is_folder;
2754 closure->all_files = closure->all_files && !is_folder;
2757 /* Checks whether the selected items in the file list are all files or all folders */
2759 selection_check (GtkFileChooserDefault *impl,
2761 gboolean *all_files,
2762 gboolean *all_folders)
2764 struct selection_check_closure closure;
2765 GtkTreeSelection *selection;
2767 closure.impl = impl;
2768 closure.num_selected = 0;
2769 closure.all_files = TRUE;
2770 closure.all_folders = TRUE;
2772 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2773 gtk_tree_selection_selected_foreach (selection,
2774 selection_check_foreach_cb,
2777 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2780 *num_selected = closure.num_selected;
2783 *all_files = closure.all_files;
2786 *all_folders = closure.all_folders;
2789 struct get_selected_file_closure {
2790 GtkFileChooserDefault *impl;
2795 get_selected_file_foreach_cb (GtkTreeModel *model,
2800 struct get_selected_file_closure *closure;
2801 GtkTreeIter child_iter;
2805 switch (closure->impl->operation_mode)
2807 case OPERATION_MODE_BROWSE:
2808 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2809 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2812 case OPERATION_MODE_SEARCH:
2813 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2814 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2815 SEARCH_MODEL_COL_FILE, &closure->file,
2819 case OPERATION_MODE_RECENT:
2820 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2821 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2822 RECENT_MODEL_COL_FILE, &closure->file,
2828 /* Returns a selected path from the file list */
2830 get_selected_file (GtkFileChooserDefault *impl)
2832 struct get_selected_file_closure closure;
2833 GtkTreeSelection *selection;
2835 closure.impl = impl;
2836 closure.file = NULL;
2838 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2839 gtk_tree_selection_selected_foreach (selection,
2840 get_selected_file_foreach_cb,
2843 return closure.file;
2847 GtkFileChooserDefault *impl;
2849 } UpdateTooltipData;
2852 update_tooltip (GtkTreeModel *model,
2857 UpdateTooltipData *udata = data;
2858 GtkTreeIter child_iter;
2861 if (udata->tip == NULL)
2863 const gchar *display_name;
2865 switch (udata->impl->operation_mode)
2867 case OPERATION_MODE_BROWSE:
2868 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2871 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2872 display_name = g_file_info_get_display_name (info);
2875 case OPERATION_MODE_SEARCH:
2876 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2877 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2878 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2882 case OPERATION_MODE_RECENT:
2883 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2884 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2885 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2890 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2896 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2897 * if there are no selected items *and* the current folder is not in the
2898 * bookmarks list. De-sensitize the button otherwise.
2901 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2904 gboolean all_folders;
2908 selection_check (impl, &num_selected, NULL, &all_folders);
2910 if (num_selected == 0)
2911 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2912 else if (num_selected == 1)
2916 file = get_selected_file (impl);
2917 active = all_folders && (shortcut_find_position (impl, file) == -1);
2920 active = all_folders;
2922 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2924 if (impl->browse_files_popup_menu_add_shortcut_item)
2925 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2926 (num_selected == 0) ? FALSE : active);
2930 if (num_selected == 0)
2931 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2932 else if (num_selected > 1)
2933 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2936 GtkTreeSelection *selection;
2937 UpdateTooltipData data;
2939 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2942 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2946 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2951 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2952 * bookmark row is selected in the shortcuts tree.
2955 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2958 gboolean removable = FALSE;
2961 if (shortcuts_get_selected (impl, &iter))
2962 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2963 SHORTCUTS_COL_REMOVABLE, &removable,
2964 SHORTCUTS_COL_NAME, &name,
2967 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2973 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2974 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2982 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2985 gboolean removable = FALSE;
2987 if (impl->browse_shortcuts_popup_menu == NULL)
2990 if (shortcuts_get_selected (impl, &iter))
2991 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2992 SHORTCUTS_COL_REMOVABLE, &removable,
2995 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2996 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2999 /* GtkWidget::drag-begin handler for the shortcuts list. */
3001 shortcuts_drag_begin_cb (GtkWidget *widget,
3002 GdkDragContext *context,
3003 GtkFileChooserDefault *impl)
3006 impl->shortcuts_drag_context = g_object_ref (context);
3011 /* Removes the idle handler for outside drags */
3013 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3015 if (!impl->shortcuts_drag_outside_idle)
3018 g_source_destroy (impl->shortcuts_drag_outside_idle);
3019 impl->shortcuts_drag_outside_idle = NULL;
3023 /* GtkWidget::drag-end handler for the shortcuts list. */
3025 shortcuts_drag_end_cb (GtkWidget *widget,
3026 GdkDragContext *context,
3027 GtkFileChooserDefault *impl)
3030 g_object_unref (impl->shortcuts_drag_context);
3032 shortcuts_cancel_drag_outside_idle (impl);
3034 if (!impl->shortcuts_drag_outside)
3037 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3039 impl->shortcuts_drag_outside = FALSE;
3043 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3045 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3046 GdkDragContext *context,
3047 GtkFileChooserDefault *impl)
3049 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3053 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3057 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3060 GtkTreeView *tree_view;
3063 GdkPixmap *row_pixmap;
3068 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3070 /* Find the selected path and get its drag pixmap */
3072 if (!shortcuts_get_selected (impl, &iter))
3073 g_assert_not_reached ();
3075 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3077 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3078 gtk_tree_path_free (path);
3087 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3093 GdkPixmap *composite;
3094 int row_pixmap_width, row_pixmap_height;
3095 int pixbuf_width, pixbuf_height;
3096 int composite_width, composite_height;
3097 int pixbuf_x, pixbuf_y;
3098 GdkGC *gc, *mask_gc;
3100 GdkBitmap *pixbuf_mask;
3102 /* Create pixmap and mask for composite image */
3104 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3105 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3106 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3108 composite_width = MAX (row_pixmap_width, pixbuf_width);
3109 composite_height = MAX (row_pixmap_height, pixbuf_height);
3111 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3113 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3116 pixbuf_x = composite_width - pixbuf_width;
3118 pixbuf_y = (composite_height - pixbuf_height) / 2;
3120 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3121 gc = gdk_gc_new (composite);
3123 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3124 mask_gc = gdk_gc_new (mask);
3126 gdk_gc_set_foreground (mask_gc, &color);
3127 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3130 color.green = 0xffff;
3131 color.blue = 0xffff;
3132 gdk_gc_set_rgb_fg_color (gc, &color);
3133 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3135 /* Composite the row pixmap and the pixbuf */
3137 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3139 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3140 NULL, &pixbuf_mask, 128);
3141 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3144 pixbuf_width, pixbuf_height);
3145 g_object_unref (pixbuf_mask);
3147 gdk_draw_drawable (composite, gc, row_pixmap,
3150 row_pixmap_width, row_pixmap_height);
3152 gdk_gc_set_foreground (mask_gc, &color);
3153 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3155 gdk_draw_pixbuf (composite, gc, pixbuf,
3158 pixbuf_width, pixbuf_height,
3162 g_object_unref (pixbuf);
3163 g_object_unref (row_pixmap);
3165 row_pixmap = composite;
3169 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3171 gtk_tree_view_get_path_at_pos (tree_view,
3172 tree_view->priv->press_start_x,
3173 tree_view->priv->press_start_y,
3179 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3180 gdk_drawable_get_colormap (row_pixmap),
3183 tree_view->priv->press_start_x + 1,
3184 row_pixmap_y + cell_y + 1);
3186 g_object_unref (row_pixmap);
3188 g_object_unref (mask);
3191 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3192 * handler so that we can tell apart the drag_leave event that comes right
3193 * before a drag_drop, from a normal drag_leave. We don't want to set the
3194 * cursor nor the flag in the latter case.
3197 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3199 GDK_THREADS_ENTER ();
3201 shortcuts_drag_set_delete_cursor (impl, TRUE);
3202 impl->shortcuts_drag_outside = TRUE;
3204 shortcuts_cancel_drag_outside_idle (impl);
3206 GDK_THREADS_LEAVE ();
3212 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3216 shortcuts_drag_leave_cb (GtkWidget *widget,
3217 GdkDragContext *context,
3219 GtkFileChooserDefault *impl)
3222 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3224 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3225 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3226 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3228 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3232 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3234 GTK_TREE_VIEW_DROP_BEFORE);
3236 g_signal_stop_emission_by_name (widget, "drag_leave");
3239 /* Computes the appropriate row and position for dropping */
3241 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3245 GtkTreeViewDropPosition *pos)
3247 GtkTreeView *tree_view;
3248 GtkTreeViewColumn *column;
3252 int bookmarks_index;
3254 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3256 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3258 if (!gtk_tree_view_get_path_at_pos (tree_view,
3260 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3266 row = bookmarks_index + impl->num_bookmarks - 1;
3267 *path = gtk_tree_path_new_from_indices (row, -1);
3268 *pos = GTK_TREE_VIEW_DROP_AFTER;
3272 row = *gtk_tree_path_get_indices (*path);
3273 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3274 gtk_tree_path_free (*path);
3276 if (row < bookmarks_index)
3278 row = bookmarks_index;
3279 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3281 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3283 row = bookmarks_index + impl->num_bookmarks - 1;
3284 *pos = GTK_TREE_VIEW_DROP_AFTER;
3288 if (cell_y < cell.height / 2)
3289 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3291 *pos = GTK_TREE_VIEW_DROP_AFTER;
3294 *path = gtk_tree_path_new_from_indices (row, -1);
3297 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3298 * implement the destination side of DnD by hand, due to limitations in
3299 * GtkTreeView's DnD API.
3302 shortcuts_drag_motion_cb (GtkWidget *widget,
3303 GdkDragContext *context,
3307 GtkFileChooserDefault *impl)
3310 GtkTreeViewDropPosition pos;
3311 GdkDragAction action;
3314 if (gtk_drag_get_source_widget (context) == widget)
3316 shortcuts_cancel_drag_outside_idle (impl);
3318 if (impl->shortcuts_drag_outside)
3320 shortcuts_drag_set_delete_cursor (impl, FALSE);
3321 impl->shortcuts_drag_outside = FALSE;
3326 if (context->suggested_action == GDK_ACTION_COPY ||
3327 (context->actions & GDK_ACTION_COPY) != 0)
3328 action = GDK_ACTION_COPY;
3329 else if (context->suggested_action == GDK_ACTION_MOVE ||
3330 (context->actions & GDK_ACTION_MOVE) != 0)
3331 action = GDK_ACTION_MOVE;
3338 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3339 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3340 gtk_tree_path_free (path);
3344 g_signal_stop_emission_by_name (widget, "drag_motion");
3348 gdk_drag_status (context, action, time_);
3355 /* GtkWidget::drag-drop handler for the shortcuts list. */
3357 shortcuts_drag_drop_cb (GtkWidget *widget,
3358 GdkDragContext *context,
3362 GtkFileChooserDefault *impl)
3365 shortcuts_cancel_drag_outside_idle (impl);
3368 g_signal_stop_emission_by_name (widget, "drag_drop");
3372 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3374 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3375 GtkSelectionData *selection_data,
3381 uris = gtk_selection_data_get_uris (selection_data);
3385 for (i = 0; uris[i]; i++)
3391 file = g_file_new_for_uri (uri);
3393 if (shortcuts_add_bookmark_from_file (impl, file, position))
3396 g_object_unref (file);
3402 /* Reorders the selected bookmark to the specified position */
3404 shortcuts_reorder (GtkFileChooserDefault *impl,
3409 ShortcutType shortcut_type;
3412 int bookmarks_index;
3417 /* Get the selected path */
3419 if (!shortcuts_get_selected (impl, &iter))
3420 g_assert_not_reached ();
3422 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3423 old_position = *gtk_tree_path_get_indices (path);
3424 gtk_tree_path_free (path);
3426 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3427 old_position -= bookmarks_index;
3428 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3430 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3431 SHORTCUTS_COL_NAME, &name,
3432 SHORTCUTS_COL_DATA, &col_data,
3433 SHORTCUTS_COL_TYPE, &shortcut_type,
3435 g_assert (col_data != NULL);
3436 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3439 g_object_ref (file); /* removal below will free file, so we need a new ref */
3441 /* Remove the path from the old position and insert it in the new one */
3443 if (new_position > old_position)
3446 if (old_position == new_position)
3450 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3452 shortcuts_add_bookmark_from_file (impl, file, new_position);
3453 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3456 error_adding_bookmark_dialog (impl, file, error);
3460 g_object_unref (file);
3463 /* Callback used when we get the drag data for the bookmarks list. We add the
3464 * received URIs as bookmarks if they are folders.
3467 shortcuts_drag_data_received_cb (GtkWidget *widget,
3468 GdkDragContext *context,
3471 GtkSelectionData *selection_data,
3476 GtkFileChooserDefault *impl;
3477 GtkTreePath *tree_path;
3478 GtkTreeViewDropPosition tree_pos;
3480 int bookmarks_index;
3482 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3484 /* Compute position */
3486 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3488 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3489 position = *gtk_tree_path_get_indices (tree_path);
3490 gtk_tree_path_free (tree_path);
3492 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3495 g_assert (position >= bookmarks_index);
3496 position -= bookmarks_index;
3498 if (gtk_targets_include_uri (&selection_data->target, 1))
3499 shortcuts_drop_uris (impl, selection_data, position);
3500 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3501 shortcuts_reorder (impl, position);
3503 g_signal_stop_emission_by_name (widget, "drag_data_received");
3506 /* Callback used to display a tooltip in the shortcuts tree */
3508 shortcuts_query_tooltip_cb (GtkWidget *widget,
3511 gboolean keyboard_mode,
3512 GtkTooltip *tooltip,
3513 GtkFileChooserDefault *impl)
3515 GtkTreeModel *model;
3518 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3526 ShortcutType shortcut_type;
3528 gtk_tree_model_get (model, &iter,
3529 SHORTCUTS_COL_DATA, &col_data,
3530 SHORTCUTS_COL_TYPE, &shortcut_type,
3533 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3535 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3539 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3544 file = G_FILE (col_data);
3545 parse_name = g_file_get_parse_name (file);
3547 gtk_tooltip_set_text (tooltip, parse_name);
3549 g_free (parse_name);
3553 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3557 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3567 /* Callback used when the selection in the shortcuts tree changes */
3569 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3570 GtkFileChooserDefault *impl)
3573 GtkTreeIter child_iter;
3575 bookmarks_check_remove_sensitivity (impl);
3576 shortcuts_check_popup_sensitivity (impl);
3578 if (impl->changing_folder)
3581 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3583 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3586 shortcuts_activate_iter (impl, &child_iter);
3591 shortcuts_row_separator_func (GtkTreeModel *model,
3595 ShortcutType shortcut_type;
3597 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3599 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3602 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3603 * keypresses before the TreeView gets them.
3606 tree_view_keybinding_cb (GtkWidget *tree_view,
3608 GtkFileChooserDefault *impl)
3610 if ((event->keyval == GDK_slash
3611 || event->keyval == GDK_KP_Divide
3613 || event->keyval == GDK_asciitilde
3615 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3617 location_popup_handler (impl, event->string);
3624 /* Callback used when the file list's popup menu is detached */
3626 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3629 GtkFileChooserDefault *impl;
3631 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3632 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3634 impl->browse_shortcuts_popup_menu = NULL;
3635 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3636 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3640 remove_shortcut_cb (GtkMenuItem *item,
3641 GtkFileChooserDefault *impl)
3643 remove_selected_bookmarks (impl);
3646 /* Rename the selected bookmark */
3648 rename_selected_bookmark (GtkFileChooserDefault *impl)
3652 GtkTreeViewColumn *column;
3653 GtkCellRenderer *cell;
3656 if (shortcuts_get_selected (impl, &iter))
3658 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3659 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3660 renderers = gtk_tree_view_column_get_cell_renderers (column);
3661 cell = g_list_nth_data (renderers, 1);
3662 g_list_free (renderers);
3663 g_object_set (cell, "editable", TRUE, NULL);
3664 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3665 path, column, cell, TRUE);
3666 gtk_tree_path_free (path);
3671 rename_shortcut_cb (GtkMenuItem *item,
3672 GtkFileChooserDefault *impl)
3674 rename_selected_bookmark (impl);
3677 /* Constructs the popup menu for the file list if needed */
3679 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3683 if (impl->browse_shortcuts_popup_menu)
3686 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3687 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3688 impl->browse_shortcuts_tree_view,
3689 shortcuts_popup_menu_detach_cb);
3691 item = gtk_image_menu_item_new_with_label (_("Remove"));
3692 impl->browse_shortcuts_popup_menu_remove_item = item;
3693 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3694 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3695 g_signal_connect (item, "activate",
3696 G_CALLBACK (remove_shortcut_cb), impl);
3697 gtk_widget_show (item);
3698 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3700 item = gtk_menu_item_new_with_label (_("Rename..."));
3701 impl->browse_shortcuts_popup_menu_rename_item = item;
3702 g_signal_connect (item, "activate",
3703 G_CALLBACK (rename_shortcut_cb), impl);
3704 gtk_widget_show (item);
3705 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3707 shortcuts_check_popup_sensitivity (impl);
3711 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3713 shortcuts_build_popup_menu (impl);
3717 popup_position_func (GtkMenu *menu,
3721 gpointer user_data);
3724 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3725 GdkEventButton *event)
3727 shortcuts_update_popup_menu (impl);
3729 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3730 NULL, NULL, NULL, NULL,
3731 event->button, event->time);
3734 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3736 popup_position_func, impl->browse_shortcuts_tree_view,
3737 0, GDK_CURRENT_TIME);
3738 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3743 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3745 shortcuts_popup_menu_cb (GtkWidget *widget,
3746 GtkFileChooserDefault *impl)
3748 shortcuts_popup_menu (impl, NULL);
3752 /* Callback used when a button is pressed on the shortcuts list.
3753 * We trap button 3 to bring up a popup menu.
3756 shortcuts_button_press_event_cb (GtkWidget *widget,
3757 GdkEventButton *event,
3758 GtkFileChooserDefault *impl)
3760 static gboolean in_press = FALSE;
3766 if (event->button != 3)
3770 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3776 shortcuts_popup_menu (impl, event);
3781 shortcuts_edited (GtkCellRenderer *cell,
3784 GtkFileChooserDefault *impl)
3790 g_object_set (cell, "editable", FALSE, NULL);
3792 path = gtk_tree_path_new_from_string (path_string);
3793 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3794 g_assert_not_reached ();
3796 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3797 SHORTCUTS_COL_DATA, &shortcut,
3799 gtk_tree_path_free (path);
3801 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3805 shortcuts_editing_canceled (GtkCellRenderer *cell,
3806 GtkFileChooserDefault *impl)
3808 g_object_set (cell, "editable", FALSE, NULL);
3811 /* Creates the widgets for the shortcuts and bookmarks tree */
3813 shortcuts_list_create (GtkFileChooserDefault *impl)
3816 GtkTreeSelection *selection;
3817 GtkTreeViewColumn *column;
3818 GtkCellRenderer *renderer;
3820 /* Target types for dragging a row to/from the shortcuts list */
3821 const GtkTargetEntry tree_model_row_targets[] = {
3822 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3825 /* Scrolled window */
3827 swin = gtk_scrolled_window_new (NULL, NULL);
3828 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3829 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3830 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3832 gtk_widget_show (swin);
3836 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3837 #ifdef PROFILE_FILE_CHOOSER
3838 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3840 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3841 G_CALLBACK (tree_view_keybinding_cb), impl);
3842 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3843 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3844 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3845 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3846 /* Accessible object name for the file chooser's shortcuts pane */
3847 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3849 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3851 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3853 tree_model_row_targets,
3854 G_N_ELEMENTS (tree_model_row_targets),
3857 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3858 GTK_DEST_DEFAULT_ALL,
3859 tree_model_row_targets,
3860 G_N_ELEMENTS (tree_model_row_targets),
3861 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3862 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3864 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3865 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3866 gtk_tree_selection_set_select_function (selection,
3867 shortcuts_select_func,
3870 g_signal_connect (selection, "changed",
3871 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3873 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3874 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3876 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3877 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3878 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3879 G_CALLBACK (shortcuts_drag_end_cb), impl);
3880 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3881 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3883 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3884 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3885 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3886 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3887 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3888 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3889 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3890 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3892 /* Support tooltips */
3893 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3894 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3895 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3897 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3898 gtk_widget_show (impl->browse_shortcuts_tree_view);
3902 column = gtk_tree_view_column_new ();
3903 /* Column header for the file chooser's shortcuts pane */
3904 gtk_tree_view_column_set_title (column, _("_Places"));
3906 renderer = gtk_cell_renderer_pixbuf_new ();
3907 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3908 gtk_tree_view_column_set_attributes (column, renderer,
3909 "pixbuf", SHORTCUTS_COL_PIXBUF,
3910 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3913 renderer = gtk_cell_renderer_text_new ();
3914 g_signal_connect (renderer, "edited",
3915 G_CALLBACK (shortcuts_edited), impl);
3916 g_signal_connect (renderer, "editing-canceled",
3917 G_CALLBACK (shortcuts_editing_canceled), impl);
3918 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3919 gtk_tree_view_column_set_attributes (column, renderer,
3920 "text", SHORTCUTS_COL_NAME,
3923 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3924 shortcuts_row_separator_func,
3927 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3932 /* Creates the widgets for the shortcuts/bookmarks pane */
3934 shortcuts_pane_create (GtkFileChooserDefault *impl,
3935 GtkSizeGroup *size_group)
3941 vbox = gtk_vbox_new (FALSE, 6);
3942 gtk_widget_show (vbox);
3944 /* Shortcuts tree */
3946 widget = shortcuts_list_create (impl);
3947 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3949 /* Box for buttons */
3951 hbox = gtk_hbox_new (TRUE, 6);
3952 gtk_size_group_add_widget (size_group, hbox);
3953 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3954 gtk_widget_show (hbox);
3956 /* Add bookmark button */
3958 impl->browse_shortcuts_add_button = button_new (impl,
3963 G_CALLBACK (add_bookmark_button_clicked_cb));
3964 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3965 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3966 _("Add the selected folder to the Bookmarks"));
3968 /* Remove bookmark button */
3970 impl->browse_shortcuts_remove_button = button_new (impl,
3975 G_CALLBACK (remove_bookmark_button_clicked_cb));
3976 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3977 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3978 _("Remove the selected bookmark"));
3983 /* Handles key press events on the file list, so that we can trap Enter to
3984 * activate the default button on our own. Also, checks to see if '/' has been
3985 * pressed. See comment by tree_view_keybinding_cb() for more details.
3988 trap_activate_cb (GtkWidget *widget,
3992 GtkFileChooserDefault *impl;
3995 impl = (GtkFileChooserDefault *) data;
3997 modifiers = gtk_accelerator_get_default_mod_mask ();
3999 if ((event->keyval == GDK_slash
4000 || event->keyval == GDK_KP_Divide
4002 || event->keyval == GDK_asciitilde
4004 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4006 location_popup_handler (impl, event->string);
4010 if ((event->keyval == GDK_Return
4011 || event->keyval == GDK_ISO_Enter
4012 || event->keyval == GDK_KP_Enter
4013 || event->keyval == GDK_space
4014 || event->keyval == GDK_KP_Space)
4015 && ((event->state & modifiers) == 0)
4016 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4017 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4021 window = get_toplevel (widget);
4023 && widget != window->default_widget
4024 && !(widget == window->focus_widget &&
4025 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4027 gtk_window_activate_default (window);
4035 /* Callback used when the file list's popup menu is detached */
4037 popup_menu_detach_cb (GtkWidget *attach_widget,
4040 GtkFileChooserDefault *impl;
4042 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4043 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4045 impl->browse_files_popup_menu = NULL;
4046 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4047 impl->browse_files_popup_menu_hidden_files_item = NULL;
4050 /* Callback used when the "Add to Bookmarks" menu item is activated */
4052 add_to_shortcuts_cb (GtkMenuItem *item,
4053 GtkFileChooserDefault *impl)
4055 bookmarks_add_selected_folder (impl);
4058 /* Callback used when the "Show Hidden Files" menu item is toggled */
4060 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4061 GtkFileChooserDefault *impl)
4064 "show-hidden", gtk_check_menu_item_get_active (item),
4068 /* Shows an error dialog about not being able to select a dragged file */
4070 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4075 _("Could not select file"),
4080 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4085 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4087 for (i = 1; uris[i]; i++)
4090 GError *error = NULL;
4093 file = g_file_new_for_uri (uri);
4095 gtk_file_chooser_default_select_file (chooser, file, &error);
4097 error_selecting_dragged_file_dialog (impl, file, error);
4099 g_object_unref (file);
4103 struct FileListDragData
4105 GtkFileChooserDefault *impl;
4111 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4113 const GError *error,
4116 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4117 struct FileListDragData *data = user_data;
4118 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4120 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4123 data->impl->file_list_drag_data_received_cancellable = NULL;
4125 if (cancelled || error)
4128 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4129 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4130 data->uris[1] == 0 && !error &&
4131 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4132 change_folder_and_display_error (data->impl, data->file, FALSE);
4135 GError *error = NULL;
4137 gtk_file_chooser_default_unselect_all (chooser);
4138 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4140 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4142 browse_files_center_selected_row (data->impl);
4145 if (data->impl->select_multiple)
4146 file_list_drag_data_select_uris (data->impl, data->uris);
4149 g_object_unref (data->impl);
4150 g_strfreev (data->uris);
4151 g_object_unref (data->file);
4154 g_object_unref (cancellable);
4158 file_list_drag_data_received_cb (GtkWidget *widget,
4159 GdkDragContext *context,
4162 GtkSelectionData *selection_data,
4167 GtkFileChooserDefault *impl;
4168 GtkFileChooser *chooser;
4173 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4174 chooser = GTK_FILE_CHOOSER (data);
4176 /* Allow only drags from other widgets; see bug #533891. */
4177 if (gtk_drag_get_source_widget (context) == widget)
4180 /* Parse the text/uri-list string, navigate to the first one */
4181 uris = gtk_selection_data_get_uris (selection_data);
4182 if (uris && uris[0])
4184 struct FileListDragData *data;
4187 file = g_file_new_for_uri (uri);
4189 data = g_new0 (struct FileListDragData, 1);
4190 data->impl = g_object_ref (impl);
4194 if (impl->file_list_drag_data_received_cancellable)
4195 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4197 impl->file_list_drag_data_received_cancellable =
4198 _gtk_file_system_get_info (impl->file_system, file,
4200 file_list_drag_data_received_get_info_cb,
4204 g_signal_stop_emission_by_name (widget, "drag_data_received");
4207 /* Don't do anything with the drag_drop signal */
4209 file_list_drag_drop_cb (GtkWidget *widget,
4210 GdkDragContext *context,
4214 GtkFileChooserDefault *impl)
4216 g_signal_stop_emission_by_name (widget, "drag_drop");
4220 /* Disable the normal tree drag motion handler, it makes it look like you're
4221 dropping the dragged item onto a tree item */
4223 file_list_drag_motion_cb (GtkWidget *widget,
4224 GdkDragContext *context,
4228 GtkFileChooserDefault *impl)
4230 g_signal_stop_emission_by_name (widget, "drag_motion");
4234 /* Constructs the popup menu for the file list if needed */
4236 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4240 if (impl->browse_files_popup_menu)
4243 impl->browse_files_popup_menu = gtk_menu_new ();
4244 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4245 impl->browse_files_tree_view,
4246 popup_menu_detach_cb);
4248 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4249 impl->browse_files_popup_menu_add_shortcut_item = item;
4250 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4251 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4252 gtk_widget_set_sensitive (item, FALSE);
4253 g_signal_connect (item, "activate",
4254 G_CALLBACK (add_to_shortcuts_cb), impl);
4255 gtk_widget_show (item);
4256 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4258 item = gtk_separator_menu_item_new ();
4259 gtk_widget_show (item);
4260 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4262 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4263 impl->browse_files_popup_menu_hidden_files_item = item;
4264 g_signal_connect (item, "toggled",
4265 G_CALLBACK (show_hidden_toggled_cb), impl);
4266 gtk_widget_show (item);
4267 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4270 /* Updates the popup menu for the file list, creating it if necessary */
4272 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4274 file_list_build_popup_menu (impl);
4276 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4278 /* The sensitivity of the Add to Bookmarks item is set in
4279 * bookmarks_check_add_sensitivity()
4282 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4283 G_CALLBACK (show_hidden_toggled_cb), impl);
4284 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4286 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4287 G_CALLBACK (show_hidden_toggled_cb), impl);
4291 popup_position_func (GtkMenu *menu,
4297 GtkWidget *widget = GTK_WIDGET (user_data);
4298 GdkScreen *screen = gtk_widget_get_screen (widget);
4301 GdkRectangle monitor;
4303 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4305 gdk_window_get_origin (widget->window, x, y);
4307 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4309 *x += (widget->allocation.width - req.width) / 2;
4310 *y += (widget->allocation.height - req.height) / 2;
4312 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4313 gtk_menu_set_monitor (menu, monitor_num);
4314 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4316 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4317 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4323 file_list_popup_menu (GtkFileChooserDefault *impl,
4324 GdkEventButton *event)
4326 file_list_update_popup_menu (impl);
4328 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4329 NULL, NULL, NULL, NULL,
4330 event->button, event->time);
4333 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4335 popup_position_func, impl->browse_files_tree_view,
4336 0, GDK_CURRENT_TIME);
4337 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4343 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4345 list_popup_menu_cb (GtkWidget *widget,
4346 GtkFileChooserDefault *impl)
4348 file_list_popup_menu (impl, NULL);
4352 /* Callback used when a button is pressed on the file list. We trap button 3 to
4353 * bring up a popup menu.
4356 list_button_press_event_cb (GtkWidget *widget,
4357 GdkEventButton *event,
4358 GtkFileChooserDefault *impl)
4360 static gboolean in_press = FALSE;
4366 if (event->button != 3)
4370 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4373 file_list_popup_menu (impl, event);
4377 /* Sets the sort column IDs for the file list based on the operation mode */
4379 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4381 int name_id, mtime_id;
4383 name_id = mtime_id = 0;
4385 switch (impl->operation_mode)
4387 case OPERATION_MODE_BROWSE:
4388 name_id = FILE_LIST_COL_NAME;
4389 mtime_id = FILE_LIST_COL_MTIME;
4391 case OPERATION_MODE_SEARCH:
4392 name_id = SEARCH_MODEL_COL_FILE;
4393 mtime_id = SEARCH_MODEL_COL_STAT;
4395 case OPERATION_MODE_RECENT:
4396 name_id = RECENT_MODEL_COL_FILE;
4397 mtime_id = RECENT_MODEL_COL_INFO;
4401 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4402 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4406 file_list_query_tooltip_cb (GtkWidget *widget,
4409 gboolean keyboard_tip,
4410 GtkTooltip *tooltip,
4413 GtkFileChooserDefault *impl = user_data;
4414 GtkTreeIter iter, child_iter;
4415 GtkTreePath *path = NULL;
4419 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4423 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4431 switch (impl->operation_mode)
4433 case OPERATION_MODE_SEARCH:
4434 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4436 gtk_tree_path_free (path);
4440 search_get_valid_child_iter (impl, &child_iter, &iter);
4441 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4442 SEARCH_MODEL_COL_FILE, &file,
4446 case OPERATION_MODE_RECENT:
4447 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4449 gtk_tree_path_free (path);
4453 recent_get_valid_child_iter (impl, &child_iter, &iter);
4454 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4455 RECENT_MODEL_COL_FILE, &file,
4459 case OPERATION_MODE_BROWSE:
4460 g_assert_not_reached ();
4466 gtk_tree_path_free (path);
4470 filename = g_file_get_path (file);
4471 gtk_tooltip_set_text (tooltip, filename);
4472 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4477 gtk_tree_path_free (path);
4482 /* Creates the widgets for the file list */
4484 create_file_list (GtkFileChooserDefault *impl)
4487 GtkTreeSelection *selection;
4488 GtkTreeViewColumn *column;
4489 GtkCellRenderer *renderer;
4491 /* Scrolled window */
4492 swin = gtk_scrolled_window_new (NULL, NULL);
4493 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4494 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4495 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4498 /* Tree/list view */
4500 impl->browse_files_tree_view = gtk_tree_view_new ();
4501 #ifdef PROFILE_FILE_CHOOSER
4502 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4504 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4505 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4507 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4508 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4510 gtk_drag_dest_set (impl->browse_files_tree_view,
4511 GTK_DEST_DEFAULT_ALL,
4513 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4514 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4516 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4517 G_CALLBACK (list_row_activated), impl);
4518 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4519 G_CALLBACK (trap_activate_cb), impl);
4520 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4521 G_CALLBACK (list_popup_menu_cb), impl);
4522 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4523 G_CALLBACK (list_button_press_event_cb), impl);
4525 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4526 G_CALLBACK (file_list_drag_data_received_cb), impl);
4527 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4528 G_CALLBACK (file_list_drag_drop_cb), impl);
4529 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4530 G_CALLBACK (file_list_drag_motion_cb), impl);
4532 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4533 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4534 G_CALLBACK (file_list_query_tooltip_cb), impl);
4536 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4537 gtk_tree_selection_set_select_function (selection,
4540 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4543 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4544 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4546 g_signal_connect (selection, "changed",
4547 G_CALLBACK (list_selection_changed), impl);
4549 /* Filename column */
4551 impl->list_name_column = gtk_tree_view_column_new ();
4552 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4553 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4554 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4555 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4557 renderer = gtk_cell_renderer_pixbuf_new ();
4558 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4559 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4560 list_icon_data_func, impl, NULL);
4562 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4563 g_object_set (impl->list_name_renderer,
4564 "ellipsize", PANGO_ELLIPSIZE_END,
4566 g_signal_connect (impl->list_name_renderer, "edited",
4567 G_CALLBACK (renderer_edited_cb), impl);
4568 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4569 G_CALLBACK (renderer_editing_canceled_cb), impl);
4570 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4571 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4572 list_name_data_func, impl, NULL);
4574 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4578 column = gtk_tree_view_column_new ();
4579 gtk_tree_view_column_set_title (column, _("Size"));
4581 renderer = gtk_cell_renderer_text_new ();
4582 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4583 gtk_tree_view_column_set_cell_data_func (column, renderer,
4584 list_size_data_func, impl, NULL);
4585 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4586 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4589 /* Modification time column */
4591 column = gtk_tree_view_column_new ();
4592 gtk_tree_view_column_set_resizable (column, TRUE);
4593 gtk_tree_view_column_set_title (column, _("Modified"));
4595 renderer = gtk_cell_renderer_text_new ();
4596 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4597 gtk_tree_view_column_set_cell_data_func (column, renderer,
4598 list_mtime_data_func, impl, NULL);
4599 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4600 impl->list_mtime_column = column;
4602 file_list_set_sort_column_ids (impl);
4604 gtk_widget_show_all (swin);
4610 create_path_bar (GtkFileChooserDefault *impl)
4612 GtkWidget *path_bar;
4614 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4615 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4620 /* Creates the widgets for the files/folders pane */
4622 file_pane_create (GtkFileChooserDefault *impl,
4623 GtkSizeGroup *size_group)
4629 vbox = gtk_vbox_new (FALSE, 6);
4630 gtk_widget_show (vbox);
4632 /* Box for lists and preview */
4634 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4635 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4636 gtk_widget_show (hbox);
4640 widget = create_file_list (impl);
4641 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4645 impl->preview_box = gtk_vbox_new (FALSE, 12);
4646 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4647 /* Don't show preview box initially */
4651 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4653 widget = filter_create (impl);
4655 gtk_widget_show (widget);
4656 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4658 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4659 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4664 /* Callback used when the "Browse for more folders" expander is toggled */
4666 expander_changed_cb (GtkExpander *expander,
4668 GtkFileChooserDefault *impl)
4670 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4671 update_appearance (impl);
4674 /* Callback used when the selection changes in the save folder combo box */
4676 save_folder_combo_changed_cb (GtkComboBox *combo,
4677 GtkFileChooserDefault *impl)
4681 if (impl->changing_folder)
4684 if (gtk_combo_box_get_active_iter (combo, &iter))
4686 GtkTreeIter child_iter;
4688 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4691 shortcuts_activate_iter (impl, &child_iter);
4695 /* Filter function used to filter out the Search item and its separator.
4696 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4699 shortcuts_combo_filter_func (GtkTreeModel *model,
4703 GtkFileChooserDefault *impl;
4704 GtkTreePath *tree_path;
4709 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4711 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4713 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4714 g_assert (tree_path != NULL);
4716 indices = gtk_tree_path_get_indices (tree_path);
4720 if (impl->has_search)
4722 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4723 if (idx == indices[0])
4727 if (impl->has_recent)
4729 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4730 if (idx == indices[0])
4734 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4735 if (idx == indices[0])
4740 gtk_tree_path_free (tree_path);
4745 /* Creates the combo box with the save folders */
4747 save_folder_combo_create (GtkFileChooserDefault *impl)
4750 GtkCellRenderer *cell;
4752 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4753 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4754 shortcuts_combo_filter_func,
4758 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4759 "model", impl->shortcuts_combo_filter_model,
4760 "focus-on-click", FALSE,
4762 gtk_widget_show (combo);
4764 cell = gtk_cell_renderer_pixbuf_new ();
4765 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4766 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4767 "pixbuf", SHORTCUTS_COL_PIXBUF,
4768 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4769 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4772 cell = gtk_cell_renderer_text_new ();
4773 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4774 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4775 "text", SHORTCUTS_COL_NAME,
4776 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4779 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4780 shortcuts_row_separator_func,
4783 g_signal_connect (combo, "changed",
4784 G_CALLBACK (save_folder_combo_changed_cb), impl);
4789 /* Creates the widgets specific to Save mode */
4791 save_widgets_create (GtkFileChooserDefault *impl)
4796 GtkWidget *alignment;
4798 if (impl->save_widgets != NULL)
4801 location_switch_to_path_bar (impl);
4803 vbox = gtk_vbox_new (FALSE, 12);
4805 table = gtk_table_new (2, 2, FALSE);
4806 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4807 gtk_widget_show (table);
4808 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4809 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4813 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4814 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4815 gtk_table_attach (GTK_TABLE (table), widget,
4819 gtk_widget_show (widget);
4821 /* Location entry */
4823 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4824 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4826 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4827 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4828 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4830 GTK_EXPAND | GTK_FILL, 0,
4832 gtk_widget_show (impl->location_entry);
4833 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4836 impl->save_folder_label = gtk_label_new (NULL);
4837 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4838 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4842 gtk_widget_show (impl->save_folder_label);
4844 impl->save_folder_combo = save_folder_combo_create (impl);
4845 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4847 GTK_EXPAND | GTK_FILL, GTK_FILL,
4849 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4852 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4853 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4855 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4856 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4857 g_signal_connect (impl->save_expander, "notify::expanded",
4858 G_CALLBACK (expander_changed_cb),
4860 gtk_widget_show_all (alignment);
4862 impl->save_widgets = vbox;
4863 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4864 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4865 gtk_widget_show (impl->save_widgets);
4868 /* Destroys the widgets specific to Save mode */
4870 save_widgets_destroy (GtkFileChooserDefault *impl)
4872 if (impl->save_widgets == NULL)
4875 gtk_widget_destroy (impl->save_widgets);
4876 impl->save_widgets = NULL;
4877 impl->location_entry = NULL;
4878 impl->save_folder_label = NULL;
4879 impl->save_folder_combo = NULL;
4880 impl->save_expander = NULL;
4883 /* Turns on the path bar widget. Can be called even if we are already in that
4887 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4889 if (impl->location_entry)
4891 gtk_widget_destroy (impl->location_entry);
4892 impl->location_entry = NULL;
4895 gtk_widget_hide (impl->location_entry_box);
4898 /* Sets the full path of the current folder as the text in the location entry. */
4900 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4902 gchar *text, *filename;
4904 if (!impl->current_folder)
4907 filename = g_file_get_path (impl->current_folder);
4911 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4915 text = g_file_get_uri (impl->current_folder);
4919 gboolean need_slash;
4922 len = strlen (text);
4923 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4929 slash_text = g_new (char, len + 2);
4930 strcpy (slash_text, text);
4931 slash_text[len] = G_DIR_SEPARATOR;
4932 slash_text[len + 1] = 0;
4938 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4945 /* Turns on the location entry. Can be called even if we are already in that
4949 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4951 /* when in search or recent files mode, we are not showing the
4952 * location_entry_box container, so there's no point in switching
4955 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4956 impl->operation_mode == OPERATION_MODE_RECENT)
4959 if (impl->location_entry)
4960 gtk_widget_destroy (impl->location_entry);
4964 gtk_widget_show (impl->location_entry_box);
4968 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4969 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4971 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4972 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4974 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4975 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4977 /* Configure the entry */
4979 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4983 gtk_widget_show (impl->location_entry);
4984 gtk_widget_grab_focus (impl->location_entry);
4987 /* Sets a new location mode. set_buttons determines whether the toggle button
4988 * for the mode will also be changed.
4991 location_mode_set (GtkFileChooserDefault *impl,
4992 LocationMode new_mode,
4993 gboolean set_button)
4995 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4996 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4998 GtkWindow *toplevel;
4999 GtkWidget *current_focus;
5000 gboolean button_active;
5001 gboolean switch_to_file_list;
5005 case LOCATION_MODE_PATH_BAR:
5006 button_active = FALSE;
5008 /* The location_entry will disappear when we switch to path bar mode. So,
5009 * we'll focus the file list in that case, to avoid having a window with
5010 * no focused widget.
5012 toplevel = get_toplevel (GTK_WIDGET (impl));
5013 switch_to_file_list = FALSE;
5016 current_focus = gtk_window_get_focus (toplevel);
5017 if (!current_focus || current_focus == impl->location_entry)
5018 switch_to_file_list = TRUE;
5021 location_switch_to_path_bar (impl);
5023 if (switch_to_file_list)
5024 gtk_widget_grab_focus (impl->browse_files_tree_view);
5028 case LOCATION_MODE_FILENAME_ENTRY:
5029 button_active = TRUE;
5030 location_switch_to_filename_entry (impl);
5034 g_assert_not_reached ();
5040 g_signal_handlers_block_by_func (impl->location_button,
5041 G_CALLBACK (location_button_toggled_cb), impl);
5043 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5045 g_signal_handlers_unblock_by_func (impl->location_button,
5046 G_CALLBACK (location_button_toggled_cb), impl);
5050 impl->location_mode = new_mode;
5054 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5056 /* when in search or recent files mode, we are not showing the
5057 * location_entry_box container, so there's no point in switching
5060 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5061 impl->operation_mode == OPERATION_MODE_RECENT)
5064 /* If the file entry is not visible, show it.
5065 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5067 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5069 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5071 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5073 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5075 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5079 gtk_widget_grab_focus (impl->location_entry);
5084 /* Callback used when one of the location mode buttons is toggled */
5086 location_button_toggled_cb (GtkToggleButton *toggle,
5087 GtkFileChooserDefault *impl)
5090 LocationMode new_mode;
5092 is_active = gtk_toggle_button_get_active (toggle);
5096 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5097 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5101 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5102 new_mode = LOCATION_MODE_PATH_BAR;
5105 location_mode_set (impl, new_mode, FALSE);
5108 /* Creates a toggle button for the location entry. */
5110 location_button_create (GtkFileChooserDefault *impl)
5115 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5116 gtk_widget_show (image);
5118 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5122 g_signal_connect (impl->location_button, "toggled",
5123 G_CALLBACK (location_button_toggled_cb), impl);
5125 str = _("Type a file name");
5127 gtk_widget_set_tooltip_text (impl->location_button, str);
5128 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5131 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5133 browse_widgets_create (GtkFileChooserDefault *impl)
5139 GtkSizeGroup *size_group;
5141 /* size group is used by the [+][-] buttons and the filter combo */
5142 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5143 vbox = gtk_vbox_new (FALSE, 12);
5145 /* Location widgets */
5146 hbox = gtk_hbox_new (FALSE, 12);
5147 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5148 gtk_widget_show (hbox);
5149 impl->browse_path_bar_hbox = hbox;
5151 location_button_create (impl);
5152 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5156 impl->browse_path_bar = create_path_bar (impl);
5157 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5158 gtk_widget_show_all (impl->browse_path_bar);
5159 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5162 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5163 g_signal_connect (impl->browse_new_folder_button, "clicked",
5164 G_CALLBACK (new_folder_button_clicked), impl);
5165 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5167 /* Box for the location label and entry */
5169 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5170 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5172 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5173 gtk_widget_show (impl->location_label);
5174 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5177 hpaned = gtk_hpaned_new ();
5178 gtk_widget_show (hpaned);
5179 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5181 widget = shortcuts_pane_create (impl, size_group);
5182 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5183 widget = file_pane_create (impl, size_group);
5184 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5186 g_object_unref (size_group);
5192 gtk_file_chooser_default_constructor (GType type,
5193 guint n_construct_properties,
5194 GObjectConstructParam *construct_params)
5196 GtkFileChooserDefault *impl;
5199 profile_start ("start", NULL);
5201 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5202 n_construct_properties,
5204 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5206 g_assert (impl->file_system);
5208 gtk_widget_push_composite_child ();
5210 /* Shortcuts model */
5211 shortcuts_model_create (impl);
5213 /* The browse widgets */
5214 impl->browse_widgets = browse_widgets_create (impl);
5215 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5217 /* Alignment to hold extra widget */
5218 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5219 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5221 gtk_widget_pop_composite_child ();
5222 update_appearance (impl);
5224 profile_end ("end", NULL);
5229 /* Sets the extra_widget by packing it in the appropriate place */
5231 set_extra_widget (GtkFileChooserDefault *impl,
5232 GtkWidget *extra_widget)
5236 g_object_ref (extra_widget);
5237 /* FIXME: is this right ? */
5238 gtk_widget_show (extra_widget);
5241 if (impl->extra_widget)
5243 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5244 g_object_unref (impl->extra_widget);
5247 impl->extra_widget = extra_widget;
5248 if (impl->extra_widget)
5250 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5251 gtk_widget_show (impl->extra_align);
5254 gtk_widget_hide (impl->extra_align);
5258 set_local_only (GtkFileChooserDefault *impl,
5259 gboolean local_only)
5261 if (local_only != impl->local_only)
5263 impl->local_only = local_only;
5265 if (impl->shortcuts_model && impl->file_system)
5267 shortcuts_add_volumes (impl);
5268 shortcuts_add_bookmarks (impl);
5271 if (local_only && !g_file_is_native (impl->current_folder))
5273 /* If we are pointing to a non-local folder, make an effort to change
5274 * back to a local folder, but it's really up to the app to not cause
5275 * such a situation, so we ignore errors.
5277 const gchar *home = g_get_home_dir ();
5283 home_file = g_file_new_for_path (home);
5285 _gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5287 g_object_unref (home_file);
5293 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5294 GtkFileChooserDefault *impl)
5296 shortcuts_add_volumes (impl);
5297 shortcuts_add_bookmarks (impl);
5299 bookmarks_check_add_sensitivity (impl);
5300 bookmarks_check_remove_sensitivity (impl);
5301 shortcuts_check_popup_sensitivity (impl);
5304 /* Sets the file chooser to multiple selection mode */
5306 set_select_multiple (GtkFileChooserDefault *impl,
5307 gboolean select_multiple,
5308 gboolean property_notify)
5310 GtkTreeSelection *selection;
5311 GtkSelectionMode mode;
5313 if (select_multiple == impl->select_multiple)
5316 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5318 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5319 gtk_tree_selection_set_mode (selection, mode);
5321 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5323 impl->select_multiple = select_multiple;
5324 g_object_notify (G_OBJECT (impl), "select-multiple");
5326 check_preview_change (impl);
5330 set_file_system_backend (GtkFileChooserDefault *impl)
5332 profile_start ("start for backend", "default");
5334 impl->file_system = _gtk_file_system_new ();
5336 g_signal_connect (impl->file_system, "volumes-changed",
5337 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5338 g_signal_connect (impl->file_system, "bookmarks-changed",
5339 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5341 profile_end ("end", NULL);
5344 /* This function is basically a do_all function.
5346 * It sets the visibility on all the widgets based on the current state, and
5347 * moves the custom_widget if needed.
5350 update_appearance (GtkFileChooserDefault *impl)
5352 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5353 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5357 gtk_widget_hide (impl->location_button);
5358 save_widgets_create (impl);
5360 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5361 text = _("Save in _folder:");
5363 text = _("Create in _folder:");
5365 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5367 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5369 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5370 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5371 gtk_widget_show (impl->browse_widgets);
5375 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5376 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5377 gtk_widget_hide (impl->browse_widgets);
5380 gtk_widget_show (impl->browse_new_folder_button);
5382 if (impl->select_multiple)
5384 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5385 "Re-setting to single selection mode.");
5386 set_select_multiple (impl, FALSE, TRUE);
5389 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5390 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5392 gtk_widget_show (impl->location_button);
5393 save_widgets_destroy (impl);
5394 gtk_widget_show (impl->browse_widgets);
5395 location_mode_set (impl, impl->location_mode, TRUE);
5398 if (impl->location_entry)
5399 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5401 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5402 gtk_widget_hide (impl->browse_new_folder_button);
5404 gtk_widget_show (impl->browse_new_folder_button);
5406 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5407 * of files may change depending whether we are in a file or folder-only mode.
5409 gtk_widget_queue_draw (impl->browse_files_tree_view);
5411 g_signal_emit_by_name (impl, "default-size-changed");
5415 gtk_file_chooser_default_set_property (GObject *object,
5417 const GValue *value,
5421 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5425 case GTK_FILE_CHOOSER_PROP_ACTION:
5427 GtkFileChooserAction action = g_value_get_enum (value);
5429 if (action != impl->action)
5431 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5433 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5434 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5435 && impl->select_multiple)
5437 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5438 "this is not allowed in multiple selection mode. Resetting the file chooser "
5439 "to single selection mode.");
5440 set_select_multiple (impl, FALSE, TRUE);
5442 impl->action = action;
5443 update_appearance (impl);
5444 settings_load (impl);
5449 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5450 /* Ignore property */
5453 case GTK_FILE_CHOOSER_PROP_FILTER:
5454 set_current_filter (impl, g_value_get_object (value));
5457 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5458 set_local_only (impl, g_value_get_boolean (value));
5461 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5462 set_preview_widget (impl, g_value_get_object (value));
5465 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5466 impl->preview_widget_active = g_value_get_boolean (value);
5467 update_preview_widget_visibility (impl);
5470 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5471 impl->use_preview_label = g_value_get_boolean (value);
5472 update_preview_widget_visibility (impl);
5475 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5476 set_extra_widget (impl, g_value_get_object (value));
5479 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5481 gboolean select_multiple = g_value_get_boolean (value);
5482 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5483 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5486 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5487 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5488 "leaving the file chooser in single selection mode.");
5492 set_select_multiple (impl, select_multiple, FALSE);
5496 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5498 gboolean show_hidden = g_value_get_boolean (value);
5499 if (show_hidden != impl->show_hidden)
5501 impl->show_hidden = show_hidden;
5503 if (impl->browse_files_model)
5504 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5509 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5511 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5512 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5523 gtk_file_chooser_default_get_property (GObject *object,
5528 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5532 case GTK_FILE_CHOOSER_PROP_ACTION:
5533 g_value_set_enum (value, impl->action);
5536 case GTK_FILE_CHOOSER_PROP_FILTER:
5537 g_value_set_object (value, impl->current_filter);
5540 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5541 g_value_set_boolean (value, impl->local_only);
5544 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5545 g_value_set_object (value, impl->preview_widget);
5548 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5549 g_value_set_boolean (value, impl->preview_widget_active);
5552 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5553 g_value_set_boolean (value, impl->use_preview_label);
5556 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5557 g_value_set_object (value, impl->extra_widget);
5560 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5561 g_value_set_boolean (value, impl->select_multiple);
5564 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5565 g_value_set_boolean (value, impl->show_hidden);
5568 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5569 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5578 /* Removes the settings signal handler. It's safe to call multiple times */
5580 remove_settings_signal (GtkFileChooserDefault *impl,
5583 if (impl->settings_signal_id)
5585 GtkSettings *settings;
5587 settings = gtk_settings_get_for_screen (screen);
5588 g_signal_handler_disconnect (settings,
5589 impl->settings_signal_id);
5590 impl->settings_signal_id = 0;
5595 gtk_file_chooser_default_dispose (GObject *object)
5598 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5600 if (impl->extra_widget)
5602 g_object_unref (impl->extra_widget);
5603 impl->extra_widget = NULL;
5606 pending_select_files_free (impl);
5608 /* cancel all pending operations */
5609 if (impl->pending_cancellables)
5611 for (l = impl->pending_cancellables; l; l = l->next)
5613 GCancellable *cancellable = G_CANCELLABLE (l->data);
5614 g_cancellable_cancel (cancellable);
5616 g_slist_free (impl->pending_cancellables);
5617 impl->pending_cancellables = NULL;
5620 if (impl->reload_icon_cancellables)
5622 for (l = impl->reload_icon_cancellables; l; l = l->next)
5624 GCancellable *cancellable = G_CANCELLABLE (l->data);
5625 g_cancellable_cancel (cancellable);
5627 g_slist_free (impl->reload_icon_cancellables);
5628 impl->reload_icon_cancellables = NULL;
5631 if (impl->loading_shortcuts)
5633 for (l = impl->loading_shortcuts; l; l = l->next)
5635 GCancellable *cancellable = G_CANCELLABLE (l->data);
5636 g_cancellable_cancel (cancellable);
5638 g_slist_free (impl->loading_shortcuts);
5639 impl->loading_shortcuts = NULL;
5642 if (impl->file_list_drag_data_received_cancellable)
5644 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5645 impl->file_list_drag_data_received_cancellable = NULL;
5648 if (impl->update_current_folder_cancellable)
5650 g_cancellable_cancel (impl->update_current_folder_cancellable);
5651 impl->update_current_folder_cancellable = NULL;
5654 if (impl->show_and_select_files_cancellable)
5656 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5657 impl->show_and_select_files_cancellable = NULL;
5660 if (impl->should_respond_get_info_cancellable)
5662 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5663 impl->should_respond_get_info_cancellable = NULL;
5666 if (impl->update_from_entry_cancellable)
5668 g_cancellable_cancel (impl->update_from_entry_cancellable);
5669 impl->update_from_entry_cancellable = NULL;
5672 if (impl->shortcuts_activate_iter_cancellable)
5674 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5675 impl->shortcuts_activate_iter_cancellable = NULL;
5678 search_stop_searching (impl, TRUE);
5679 recent_stop_loading (impl);
5681 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5683 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5686 /* We override show-all since we have internal widgets that
5687 * shouldn't be shown when you call show_all(), like the filter
5691 gtk_file_chooser_default_show_all (GtkWidget *widget)
5693 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5695 gtk_widget_show (widget);
5697 if (impl->extra_widget)
5698 gtk_widget_show_all (impl->extra_widget);
5701 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5702 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5705 toplevel_set_focus_cb (GtkWindow *window,
5707 GtkFileChooserDefault *impl)
5709 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5712 /* We monitor the focus widget on our toplevel to be able to know which widget
5713 * was last focused at the time our "should_respond" method gets called.
5716 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5717 GtkWidget *previous_toplevel)
5719 GtkFileChooserDefault *impl;
5720 GtkWidget *toplevel;
5722 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5724 if (previous_toplevel)
5726 g_assert (impl->toplevel_set_focus_id != 0);
5727 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5728 impl->toplevel_set_focus_id = 0;
5729 impl->toplevel_last_focus_widget = NULL;
5732 g_assert (impl->toplevel_set_focus_id == 0);
5734 toplevel = gtk_widget_get_toplevel (widget);
5735 if (GTK_IS_WINDOW (toplevel))
5737 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5738 G_CALLBACK (toplevel_set_focus_cb), impl);
5739 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5743 /* Changes the icons wherever it is needed */
5745 change_icon_theme (GtkFileChooserDefault *impl)
5747 GtkSettings *settings;
5750 profile_start ("start", NULL);
5752 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5754 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5755 impl->icon_size = MAX (width, height);
5757 impl->icon_size = FALLBACK_ICON_SIZE;
5759 shortcuts_reload_icons (impl);
5760 gtk_widget_queue_resize (impl->browse_files_tree_view);
5762 profile_end ("end", NULL);
5765 /* Callback used when a GtkSettings value changes */
5767 settings_notify_cb (GObject *object,
5769 GtkFileChooserDefault *impl)
5773 profile_start ("start", NULL);
5775 name = g_param_spec_get_name (pspec);
5777 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5778 strcmp (name, "gtk-icon-sizes") == 0)
5779 change_icon_theme (impl);
5781 profile_end ("end", NULL);
5784 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5788 check_icon_theme (GtkFileChooserDefault *impl)
5790 GtkSettings *settings;
5792 profile_start ("start", NULL);
5794 if (impl->settings_signal_id)
5796 profile_end ("end", NULL);
5800 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5802 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5803 impl->settings_signal_id = g_signal_connect (settings, "notify",
5804 G_CALLBACK (settings_notify_cb), impl);
5806 change_icon_theme (impl);
5809 profile_end ("end", NULL);
5813 gtk_file_chooser_default_style_set (GtkWidget *widget,
5814 GtkStyle *previous_style)
5816 GtkFileChooserDefault *impl;
5818 profile_start ("start", NULL);
5820 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5822 profile_msg (" parent class style_set start", NULL);
5823 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5824 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5825 profile_msg (" parent class style_set end", NULL);
5827 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5828 change_icon_theme (impl);
5830 profile_msg (" emit default-size-changed start", NULL);
5831 g_signal_emit_by_name (widget, "default-size-changed");
5832 profile_msg (" emit default-size-changed end", NULL);
5834 profile_end ("end", NULL);
5838 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5839 GdkScreen *previous_screen)
5841 GtkFileChooserDefault *impl;
5843 profile_start ("start", NULL);
5845 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5847 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5848 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5850 remove_settings_signal (impl, previous_screen);
5851 check_icon_theme (impl);
5853 g_signal_emit_by_name (widget, "default-size-changed");
5855 profile_end ("end", NULL);
5859 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5860 GtkAllocation *allocation)
5862 GtkFileChooserDefault *impl;
5864 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5866 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5868 impl->default_width = allocation->width;
5869 impl->default_height = allocation->height;
5871 if (impl->preview_widget_active &&
5872 impl->preview_widget &&
5873 GTK_WIDGET_DRAWABLE (impl->preview_widget))
5874 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
5876 if (impl->extra_widget &&
5877 GTK_WIDGET_DRAWABLE (impl->extra_widget))
5878 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
5882 get_is_file_filtered (GtkFileChooserDefault *impl,
5884 GFileInfo *file_info)
5886 GtkFileFilterInfo filter_info;
5887 GtkFileFilterFlags needed;
5890 if (!impl->current_filter)
5893 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5895 needed = gtk_file_filter_get_needed (impl->current_filter);
5897 filter_info.display_name = g_file_info_get_display_name (file_info);
5898 filter_info.mime_type = g_file_info_get_content_type (file_info);
5900 if (needed & GTK_FILE_FILTER_FILENAME)
5902 filter_info.filename = g_file_get_path (file);
5903 if (filter_info.filename)
5904 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5907 filter_info.filename = NULL;
5909 if (needed & GTK_FILE_FILTER_URI)
5911 filter_info.uri = g_file_get_uri (file);
5912 if (filter_info.uri)
5913 filter_info.contains |= GTK_FILE_FILTER_URI;
5916 filter_info.uri = NULL;
5918 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5920 if (filter_info.filename)
5921 g_free ((gchar *)filter_info.filename);
5922 if (filter_info.uri)
5923 g_free ((gchar *)filter_info.uri);
5929 settings_load (GtkFileChooserDefault *impl)
5931 GtkFileChooserSettings *settings;
5932 LocationMode location_mode;
5933 gboolean show_hidden;
5934 gboolean expand_folders;
5936 settings = _gtk_file_chooser_settings_new ();
5938 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5939 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5940 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5942 g_object_unref (settings);
5944 location_mode_set (impl, location_mode, TRUE);
5945 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5946 impl->expand_folders = expand_folders;
5947 if (impl->save_expander)
5948 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5952 settings_save (GtkFileChooserDefault *impl)
5954 GtkFileChooserSettings *settings;
5956 settings = _gtk_file_chooser_settings_new ();
5958 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5959 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5960 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5963 _gtk_file_chooser_settings_save (settings, NULL);
5965 g_object_unref (settings);
5968 /* GtkWidget::map method */
5970 gtk_file_chooser_default_map (GtkWidget *widget)
5972 GtkFileChooserDefault *impl;
5973 char *current_working_dir;
5975 profile_start ("start", NULL);
5977 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5979 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5981 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5983 switch (impl->reload_state)
5986 /* The user didn't explicitly give us a folder to
5987 * display, so we'll use the cwd
5989 current_working_dir = g_get_current_dir ();
5990 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
5991 current_working_dir);
5992 g_free (current_working_dir);
5995 case RELOAD_HAS_FOLDER:
5996 /* Nothing; we are already loading or loaded, so we
5997 * don't need to reload
6001 case RELOAD_WAS_UNMAPPED:
6002 /* Just reload the current folder; else continue
6005 if (impl->current_folder)
6007 pending_select_files_store_selection (impl);
6008 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6013 g_assert_not_reached ();
6017 volumes_bookmarks_changed_cb (impl->file_system, impl);
6019 settings_load (impl);
6021 profile_end ("end", NULL);
6024 /* GtkWidget::unmap method */
6026 gtk_file_chooser_default_unmap (GtkWidget *widget)
6028 GtkFileChooserDefault *impl;
6030 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6032 settings_save (impl);
6034 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6036 impl->reload_state = RELOAD_WAS_UNMAPPED;
6040 list_model_filter_func (GtkFileSystemModel *model,
6042 GFileInfo *file_info,
6045 GtkFileChooserDefault *impl = user_data;
6047 if (!impl->current_filter)
6050 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6053 return !get_is_file_filtered (impl, file, file_info);
6057 install_list_model_filter (GtkFileChooserDefault *impl)
6059 GtkFileSystemModelFilter filter;
6062 g_assert (impl->browse_files_model != NULL);
6064 if (impl->current_filter)
6066 filter = list_model_filter_func;
6075 _gtk_file_system_model_set_filter (impl->browse_files_model,
6080 #define COMPARE_DIRECTORIES \
6081 GtkFileChooserDefault *impl = user_data; \
6082 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6083 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6084 gboolean dir_a, dir_b; \
6087 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6089 return impl->list_sort_ascending ? -1 : 1; \
6092 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6094 return impl->list_sort_ascending ? 1 : -1; \
6096 if (dir_a != dir_b) \
6097 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6099 /* Sort callback for the filename column */
6101 name_sort_func (GtkTreeModel *model,
6106 COMPARE_DIRECTORIES;
6109 gchar *key_a, *key_b;
6112 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6113 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6114 result = strcmp (key_a, key_b);
6123 /* Sort callback for the size column */
6125 size_sort_func (GtkTreeModel *model,
6130 COMPARE_DIRECTORIES;
6133 goffset size_a = g_file_info_get_size (info_a);
6134 goffset size_b = g_file_info_get_size (info_b);
6136 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6140 /* Sort callback for the mtime column */
6142 mtime_sort_func (GtkTreeModel *model,
6147 COMPARE_DIRECTORIES;
6152 g_file_info_get_modification_time (info_a, &ta);
6153 g_file_info_get_modification_time (info_b, &tb);
6155 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6159 /* Callback used when the sort column changes. We cache the sort order for use
6160 * in name_sort_func().
6163 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6164 GtkFileChooserDefault *impl)
6166 GtkSortType sort_type;
6168 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6169 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6173 set_busy_cursor (GtkFileChooserDefault *impl,
6176 GtkWindow *toplevel;
6177 GdkDisplay *display;
6180 toplevel = get_toplevel (GTK_WIDGET (impl));
6181 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6184 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6187 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6191 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6192 gdk_display_flush (display);
6195 gdk_cursor_unref (cursor);
6198 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6200 load_set_model (GtkFileChooserDefault *impl)
6202 profile_start ("start", NULL);
6204 g_assert (impl->browse_files_model != NULL);
6205 g_assert (impl->sort_model == NULL);
6207 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6208 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6209 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6210 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6211 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6212 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6213 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6214 impl->list_sort_ascending = TRUE;
6215 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6217 g_signal_connect (impl->sort_model, "sort_column_changed",
6218 G_CALLBACK (list_sort_column_changed_cb), impl);
6220 profile_msg (" gtk_tree_view_set_model start", NULL);
6221 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6222 GTK_TREE_MODEL (impl->sort_model));
6223 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6224 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6225 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6226 profile_msg (" gtk_tree_view_set_model end", NULL);
6228 profile_end ("end", NULL);
6231 /* Timeout callback used when the loading timer expires */
6233 load_timeout_cb (gpointer data)
6235 GtkFileChooserDefault *impl;
6237 profile_start ("start", NULL);
6239 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6240 g_assert (impl->load_state == LOAD_PRELOAD);
6241 g_assert (impl->load_timeout_id != 0);
6242 g_assert (impl->browse_files_model != NULL);
6244 impl->load_timeout_id = 0;
6245 impl->load_state = LOAD_LOADING;
6247 load_set_model (impl);
6249 profile_end ("end", NULL);
6254 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6256 load_setup_timer (GtkFileChooserDefault *impl)
6258 g_assert (impl->load_timeout_id == 0);
6259 g_assert (impl->load_state != LOAD_PRELOAD);
6261 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6262 impl->load_state = LOAD_PRELOAD;
6265 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6267 load_remove_timer (GtkFileChooserDefault *impl)
6269 if (impl->load_timeout_id != 0)
6271 g_assert (impl->load_state == LOAD_PRELOAD);
6273 g_source_remove (impl->load_timeout_id);
6274 impl->load_timeout_id = 0;
6275 impl->load_state = LOAD_EMPTY;
6278 g_assert (impl->load_state == LOAD_EMPTY ||
6279 impl->load_state == LOAD_LOADING ||
6280 impl->load_state == LOAD_FINISHED);
6283 /* Selects the first row in the file list */
6285 browse_files_select_first_row (GtkFileChooserDefault *impl)
6289 if (!impl->sort_model)
6292 path = gtk_tree_path_new_from_indices (0, -1);
6293 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6294 gtk_tree_path_free (path);
6297 struct center_selected_row_closure {
6298 GtkFileChooserDefault *impl;
6299 gboolean already_centered;
6302 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6303 * selected row in the tree view.
6306 center_selected_row_foreach_cb (GtkTreeModel *model,
6311 struct center_selected_row_closure *closure;
6314 if (closure->already_centered)
6317 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6318 closure->already_centered = TRUE;
6321 /* Centers the selected row in the tree view */
6323 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6325 struct center_selected_row_closure closure;
6326 GtkTreeSelection *selection;
6328 closure.impl = impl;
6329 closure.already_centered = FALSE;
6331 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6332 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6335 struct ShowAndSelectPathsData
6337 GtkFileChooserDefault *impl;
6342 show_and_select_files_finished_loading (GtkFolder *folder,
6345 gboolean have_hidden;
6346 gboolean have_filtered;
6348 struct ShowAndSelectPathsData *data = user_data;
6350 have_hidden = FALSE;
6351 have_filtered = FALSE;
6353 for (l = data->files; l; l = l->next)
6360 info = _gtk_folder_get_info (folder, file);
6364 have_hidden = g_file_info_get_is_hidden (info);
6367 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6368 get_is_file_filtered (data->impl, file, info);
6370 g_object_unref (info);
6372 if (have_hidden && have_filtered)
6373 break; /* we now have all the information we need */
6377 g_signal_handlers_disconnect_by_func (folder,
6378 show_and_select_files_finished_loading,
6382 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6385 set_current_filter (data->impl, NULL);
6387 for (l = data->files; l; l = l->next)
6392 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6393 select_func, data->impl);
6396 browse_files_center_selected_row (data->impl);
6398 g_object_unref (data->impl);
6399 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6400 g_slist_free (data->files);
6405 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6407 const GError *error,
6410 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6411 struct ShowAndSelectPathsData *data = user_data;
6413 if (data->impl->show_and_select_files_cancellable != cancellable)
6416 data->impl->show_and_select_files_cancellable = NULL;
6418 if (cancelled || error)
6421 g_object_unref (cancellable);
6423 if (_gtk_folder_is_finished_loading (folder))
6424 show_and_select_files_finished_loading (folder, user_data);
6426 g_signal_connect (folder, "finished-loading",
6427 G_CALLBACK (show_and_select_files_finished_loading),
6433 g_object_unref (data->impl);
6434 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6435 g_slist_free (data->files);
6438 g_object_unref (cancellable);
6442 show_and_select_files (GtkFileChooserDefault *impl,
6447 struct ShowAndSelectPathsData *info;
6449 profile_start ("start", NULL);
6453 profile_end ("end", NULL);
6457 info = g_new (struct ShowAndSelectPathsData, 1);
6458 info->impl = g_object_ref (impl);
6459 info->files = g_slist_copy (files);
6460 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6462 if (impl->show_and_select_files_cancellable)
6463 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6465 impl->show_and_select_files_cancellable =
6466 _gtk_file_system_get_folder (impl->file_system, parent_file,
6467 "standard::is-hidden,standard::type,standard::name",
6468 show_and_select_files_get_folder_cb, info);
6470 profile_end ("end", NULL);
6474 /* Processes the pending operation when a folder is finished loading */
6476 pending_select_files_process (GtkFileChooserDefault *impl)
6478 g_assert (impl->load_state == LOAD_FINISHED);
6479 g_assert (impl->browse_files_model != NULL);
6480 g_assert (impl->sort_model != NULL);
6482 if (impl->pending_select_files)
6485 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6486 pending_select_files_free (impl);
6487 browse_files_center_selected_row (impl);
6491 /* We only select the first row if the chooser is actually mapped ---
6492 * selecting the first row is to help the user when he is interacting with
6493 * the chooser, but sometimes a chooser works not on behalf of the user,
6494 * but rather on behalf of something else like GtkFileChooserButton. In
6495 * that case, the chooser's selection should be what the caller expects,
6496 * as the user can't see that something else got selected. See bug #165264.
6498 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6499 browse_files_select_first_row (impl);
6502 g_assert (impl->pending_select_files == NULL);
6505 /* Callback used when the file system model finishes loading */
6507 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6508 GtkFileChooserDefault *impl)
6510 profile_start ("start", NULL);
6512 if (impl->load_state == LOAD_PRELOAD)
6514 load_remove_timer (impl);
6515 load_set_model (impl);
6517 else if (impl->load_state == LOAD_LOADING)
6523 /* We can't g_assert_not_reached(), as something other than us may have
6524 * initiated a folder reload. See #165556.
6526 profile_end ("end", NULL);
6530 g_assert (impl->load_timeout_id == 0);
6532 impl->load_state = LOAD_FINISHED;
6534 pending_select_files_process (impl);
6535 set_busy_cursor (impl, FALSE);
6536 #ifdef PROFILE_FILE_CHOOSER
6537 access ("MARK: *** FINISHED LOADING", F_OK);
6540 profile_end ("end", NULL);
6544 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6546 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6548 if (impl->browse_files_model)
6550 g_object_unref (impl->browse_files_model);
6551 impl->browse_files_model = NULL;
6554 if (impl->sort_model)
6556 g_object_unref (impl->sort_model);
6557 impl->sort_model = NULL;
6560 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6563 /* Gets rid of the old list model and creates a new one for the current folder */
6565 set_list_model (GtkFileChooserDefault *impl,
6568 g_assert (impl->current_folder != NULL);
6570 profile_start ("start", NULL);
6572 stop_loading_and_clear_list_model (impl);
6574 set_busy_cursor (impl, TRUE);
6575 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6577 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6578 impl->current_folder, 0,
6579 "standard,time,thumbnail::*",
6581 if (!impl->browse_files_model)
6583 set_busy_cursor (impl, FALSE);
6584 profile_end ("end", NULL);
6588 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6590 g_signal_connect (impl->browse_files_model, "finished-loading",
6591 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6593 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6595 install_list_model_filter (impl);
6597 profile_end ("end", NULL);
6602 struct update_chooser_entry_selected_foreach_closure {
6604 GtkTreeIter first_selected_iter;
6608 compare_utf8_filenames (const gchar *a,
6611 gchar *a_folded, *b_folded;
6614 a_folded = g_utf8_strdown (a, -1);
6615 b_folded = g_utf8_strdown (b, -1);
6617 retval = strcmp (a_folded, b_folded);
6626 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6631 struct update_chooser_entry_selected_foreach_closure *closure;
6634 closure->num_selected++;
6636 if (closure->num_selected == 1)
6637 closure->first_selected_iter = *iter;
6641 update_chooser_entry (GtkFileChooserDefault *impl)
6643 GtkTreeSelection *selection;
6644 struct update_chooser_entry_selected_foreach_closure closure;
6645 const char *file_part;
6647 /* no need to update the file chooser's entry if there's no entry */
6648 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6649 impl->operation_mode == OPERATION_MODE_RECENT ||
6650 !impl->location_entry)
6653 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6654 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6655 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6656 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6657 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6660 g_assert (impl->location_entry != NULL);
6662 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6663 closure.num_selected = 0;
6664 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6668 if (closure.num_selected == 0)
6670 goto maybe_clear_entry;
6672 else if (closure.num_selected == 1)
6674 GtkTreeIter child_iter;
6676 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6679 gboolean change_entry;
6681 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6683 &closure.first_selected_iter);
6685 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6687 /* If the cursor moved to the row of the newly created folder,
6688 * retrieving info will return NULL.
6693 g_free (impl->browse_files_last_selected_name);
6694 impl->browse_files_last_selected_name =
6695 g_strdup (g_file_info_get_display_name (info));
6697 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6698 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6699 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6701 /* We don't want the name to change when clicking on a folder... */
6702 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6705 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6709 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6711 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6712 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6720 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6721 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6723 /* Multiple selection, so just clear the entry. */
6725 g_free (impl->browse_files_last_selected_name);
6726 impl->browse_files_last_selected_name = NULL;
6728 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6734 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6735 && impl->browse_files_last_selected_name)
6737 const char *entry_text;
6739 gboolean clear_entry;
6741 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6742 len = strlen (entry_text);
6745 /* The file chooser entry may have appended a "/" to its text. So
6746 * take it out, and compare the result to the old selection.
6748 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6752 tmp = g_strndup (entry_text, len - 1);
6753 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6757 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6760 clear_entry = FALSE;
6763 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6768 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6772 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6776 struct UpdateCurrentFolderData
6778 GtkFileChooserDefault *impl;
6780 gboolean keep_trail;
6781 gboolean clear_entry;
6782 GFile *original_file;
6783 GError *original_error;
6787 update_current_folder_get_info_cb (GCancellable *cancellable,
6789 const GError *error,
6792 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6793 struct UpdateCurrentFolderData *data = user_data;
6794 GtkFileChooserDefault *impl = data->impl;
6796 if (cancellable != impl->update_current_folder_cancellable)
6799 impl->update_current_folder_cancellable = NULL;
6800 impl->reload_state = RELOAD_EMPTY;
6802 set_busy_cursor (impl, FALSE);
6811 if (!data->original_file)
6813 data->original_file = g_object_ref (data->file);
6814 data->original_error = g_error_copy (error);
6817 parent_file = g_file_get_parent (data->file);
6819 /* get parent path and try to change the folder to that */
6822 g_object_unref (data->file);
6823 data->file = parent_file;
6825 g_object_unref (cancellable);
6827 /* restart the update current folder operation */
6828 impl->reload_state = RELOAD_HAS_FOLDER;
6830 impl->update_current_folder_cancellable =
6831 _gtk_file_system_get_info (impl->file_system, data->file,
6833 update_current_folder_get_info_cb,
6836 set_busy_cursor (impl, TRUE);
6842 /* error and bail out */
6843 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6844 g_object_unref (data->original_file);
6850 if (data->original_file)
6852 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6854 g_object_unref (data->original_file);
6857 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6860 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6863 if (impl->current_folder != data->file)
6865 if (impl->current_folder)
6866 g_object_unref (impl->current_folder);
6868 impl->current_folder = g_object_ref (data->file);
6870 impl->reload_state = RELOAD_HAS_FOLDER;
6873 /* Update the widgets that may trigger a folder change themselves. */
6875 if (!impl->changing_folder)
6877 impl->changing_folder = TRUE;
6879 shortcuts_update_current_folder (impl);
6881 impl->changing_folder = FALSE;
6884 /* Set the folder on the save entry */
6886 if (impl->location_entry)
6888 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6889 impl->current_folder);
6891 if (data->clear_entry)
6892 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6895 /* Create a new list model. This is slightly evil; we store the result value
6896 * but perform more actions rather than returning immediately even if it
6897 * generates an error.
6899 set_list_model (impl, NULL);
6901 /* Refresh controls */
6903 shortcuts_find_current_folder (impl);
6905 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6907 check_preview_change (impl);
6908 bookmarks_check_add_sensitivity (impl);
6910 g_signal_emit_by_name (impl, "selection-changed", 0);
6913 g_object_unref (data->file);
6916 g_object_unref (cancellable);
6920 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6922 gboolean keep_trail,
6923 gboolean clear_entry,
6926 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6927 struct UpdateCurrentFolderData *data;
6929 profile_start ("start", NULL);
6931 g_object_ref (file);
6933 switch (impl->operation_mode)
6935 case OPERATION_MODE_SEARCH:
6936 search_switch_to_browse_mode (impl);
6938 case OPERATION_MODE_RECENT:
6939 recent_switch_to_browse_mode (impl);
6941 case OPERATION_MODE_BROWSE:
6945 if (impl->local_only && !g_file_is_native (file))
6947 g_set_error_literal (error,
6948 GTK_FILE_CHOOSER_ERROR,
6949 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6950 _("Cannot change to folder because it is not local"));
6952 g_object_unref (file);
6953 profile_end ("end - not local", NULL);
6957 if (impl->update_current_folder_cancellable)
6958 g_cancellable_cancel (impl->update_current_folder_cancellable);
6960 /* Test validity of path here. */
6961 data = g_new0 (struct UpdateCurrentFolderData, 1);
6963 data->file = g_object_ref (file);
6964 data->keep_trail = keep_trail;
6965 data->clear_entry = clear_entry;
6967 impl->reload_state = RELOAD_HAS_FOLDER;
6969 impl->update_current_folder_cancellable =
6970 _gtk_file_system_get_info (impl->file_system, file,
6972 update_current_folder_get_info_cb,
6975 set_busy_cursor (impl, TRUE);
6976 g_object_unref (file);
6978 profile_end ("end", NULL);
6983 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6985 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6987 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6988 impl->operation_mode == OPERATION_MODE_RECENT)
6991 if (impl->reload_state == RELOAD_EMPTY)
6993 char *current_working_dir;
6996 /* We are unmapped, or we had an error while loading the last folder. We'll return
6997 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6998 * explicitly calls set_current_folder() on us.
7000 current_working_dir = g_get_current_dir ();
7001 file = g_file_new_for_path (current_working_dir);
7002 g_free (current_working_dir);
7006 if (impl->current_folder)
7007 return g_object_ref (impl->current_folder);
7013 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7016 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7018 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7019 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7021 pending_select_files_free (impl);
7022 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7026 select_func (GtkFileSystemModel *model,
7031 GtkFileChooserDefault *impl = user_data;
7032 GtkTreeSelection *selection;
7033 GtkTreeIter sorted_iter;
7035 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7037 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7038 gtk_tree_selection_select_iter (selection, &sorted_iter);
7042 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7046 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7050 parent_file = g_file_get_parent (file);
7053 return _gtk_file_chooser_set_current_folder_file (chooser, file, error);
7055 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7056 impl->operation_mode == OPERATION_MODE_RECENT ||
7057 impl->load_state == LOAD_EMPTY)
7063 g_assert (impl->current_folder != NULL);
7065 same_path = g_file_equal (parent_file, impl->current_folder);
7068 if (same_path && impl->load_state == LOAD_FINISHED)
7073 files.data = (gpointer) file;
7076 result = show_and_select_files (impl, parent_file, &files, error);
7077 g_object_unref (parent_file);
7081 pending_select_files_add (impl, file);
7087 result = _gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7088 g_object_unref (parent_file);
7092 g_object_unref (parent_file);
7097 unselect_func (GtkFileSystemModel *model,
7102 GtkFileChooserDefault *impl = user_data;
7103 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7104 GtkTreePath *sorted_path;
7106 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7108 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7110 gtk_tree_path_free (sorted_path);
7114 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7117 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7119 if (!impl->browse_files_model)
7122 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7123 unselect_func, impl);
7127 maybe_select (GtkTreeModel *model,
7132 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7133 GtkTreeSelection *selection;
7137 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7139 info = get_list_file_info (impl, iter);
7140 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7142 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7143 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7144 gtk_tree_selection_select_iter (selection, iter);
7146 gtk_tree_selection_unselect_iter (selection, iter);
7152 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7154 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7156 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7157 impl->operation_mode == OPERATION_MODE_RECENT)
7159 GtkTreeSelection *selection;
7161 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7162 gtk_tree_selection_select_all (selection);
7166 if (impl->select_multiple)
7167 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7168 maybe_select, impl);
7172 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7174 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7175 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7177 gtk_tree_selection_unselect_all (selection);
7178 pending_select_files_free (impl);
7181 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7183 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7185 * is_empty_ret - whether the file entry is totally empty
7187 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7188 * the path will be "$cwd/foobar")
7191 check_save_entry (GtkFileChooserDefault *impl,
7193 gboolean *is_well_formed_ret,
7194 gboolean *is_empty_ret,
7195 gboolean *is_file_part_empty_ret,
7196 gboolean *is_folder)
7198 GtkFileChooserEntry *chooser_entry;
7199 GFile *current_folder;
7200 const char *file_part;
7204 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7205 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7206 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7207 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7208 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7210 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7212 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7215 *is_well_formed_ret = TRUE;
7216 *is_empty_ret = TRUE;
7217 *is_file_part_empty_ret = TRUE;
7223 *is_empty_ret = FALSE;
7225 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7226 if (!current_folder)
7229 *is_well_formed_ret = FALSE;
7230 *is_file_part_empty_ret = FALSE;
7236 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7238 if (!file_part || file_part[0] == '\0')
7240 *file_ret = g_object_ref (current_folder);
7241 *is_well_formed_ret = TRUE;
7242 *is_file_part_empty_ret = TRUE;
7248 *is_file_part_empty_ret = FALSE;
7251 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7255 error_building_filename_dialog (impl, error);
7257 *is_well_formed_ret = FALSE;
7264 *is_well_formed_ret = TRUE;
7265 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7268 struct get_files_closure {
7269 GtkFileChooserDefault *impl;
7271 GFile *file_from_entry;
7275 get_files_foreach (GtkTreeModel *model,
7280 struct get_files_closure *info;
7282 GtkFileSystemModel *fs_model;
7283 GtkTreeIter sel_iter;
7286 fs_model = info->impl->browse_files_model;
7287 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7289 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7291 return; /* We are on the editable row */
7293 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7294 info->result = g_slist_prepend (info->result, g_object_ref (file));
7298 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7300 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7301 struct get_files_closure info;
7302 GtkWindow *toplevel;
7303 GtkWidget *current_focus;
7304 gboolean file_list_seen;
7306 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7307 return search_get_selected_files (impl);
7309 if (impl->operation_mode == OPERATION_MODE_RECENT)
7310 return recent_get_selected_files (impl);
7314 info.file_from_entry = NULL;
7316 toplevel = get_toplevel (GTK_WIDGET (impl));
7318 current_focus = gtk_window_get_focus (toplevel);
7320 current_focus = NULL;
7322 file_list_seen = FALSE;
7323 if (current_focus == impl->browse_files_tree_view)
7325 GtkTreeSelection *selection;
7329 file_list_seen = TRUE;
7330 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7331 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7333 /* If there is no selection in the file list, we probably have this situation:
7335 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7336 * 2. He then double-clicked on a folder ("bar") in the file list
7338 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7339 * filename entry to see if that is the case.
7341 if (info.result == NULL && impl->location_entry)
7344 else if (impl->location_entry && current_focus == impl->location_entry)
7346 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7350 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7355 if (!is_well_formed)
7358 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7360 g_object_unref (info.file_from_entry);
7364 if (info.file_from_entry)
7365 info.result = g_slist_prepend (info.result, info.file_from_entry);
7366 else if (!file_list_seen)
7371 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7373 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7377 /* The focus is on a dialog's action area button or something else */
7378 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7379 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7387 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7388 * fall back to the current directory */
7389 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7390 info.result == NULL)
7392 GFile *current_folder;
7394 current_folder = _gtk_file_chooser_get_current_folder_file (chooser);
7397 info.result = g_slist_prepend (info.result, current_folder);
7400 return g_slist_reverse (info.result);
7404 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7406 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7408 if (impl->preview_file)
7409 return g_object_ref (impl->preview_file);
7414 static GtkFileSystem *
7415 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7417 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7419 return impl->file_system;
7422 /* Shows or hides the filter widgets */
7424 show_filters (GtkFileChooserDefault *impl,
7428 gtk_widget_show (impl->filter_combo_hbox);
7430 gtk_widget_hide (impl->filter_combo_hbox);
7434 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7435 GtkFileFilter *filter)
7437 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7440 if (g_slist_find (impl->filters, filter))
7442 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7446 g_object_ref_sink (filter);
7447 impl->filters = g_slist_append (impl->filters, filter);
7449 name = gtk_file_filter_get_name (filter);
7451 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7453 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7455 if (!g_slist_find (impl->filters, impl->current_filter))
7456 set_current_filter (impl, filter);
7458 show_filters (impl, TRUE);
7462 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7463 GtkFileFilter *filter)
7465 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7466 GtkTreeModel *model;
7470 filter_index = g_slist_index (impl->filters, filter);
7472 if (filter_index < 0)
7474 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7478 impl->filters = g_slist_remove (impl->filters, filter);
7480 if (filter == impl->current_filter)
7483 set_current_filter (impl, impl->filters->data);
7485 set_current_filter (impl, NULL);
7488 /* Remove row from the combo box */
7489 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7490 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7491 g_assert_not_reached ();
7493 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7495 g_object_unref (filter);
7498 show_filters (impl, FALSE);
7502 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7504 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7506 return g_slist_copy (impl->filters);
7509 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7511 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7514 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7517 struct AddShortcutData
7519 GtkFileChooserDefault *impl;
7524 add_shortcut_get_info_cb (GCancellable *cancellable,
7526 const GError *error,
7530 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7531 struct AddShortcutData *data = user_data;
7533 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7536 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7538 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7541 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7543 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7546 g_object_unref (data->impl);
7547 g_object_unref (data->file);
7550 g_object_unref (cancellable);
7554 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7558 GCancellable *cancellable;
7559 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7560 struct AddShortcutData *data;
7564 /* Avoid adding duplicates */
7565 pos = shortcut_find_position (impl, file);
7566 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7570 uri = g_file_get_uri (file);
7571 /* translators, "Shortcut" means "Bookmark" here */
7573 GTK_FILE_CHOOSER_ERROR,
7574 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7575 _("Shortcut %s already exists"),
7582 for (l = impl->loading_shortcuts; l; l = l->next)
7584 GCancellable *c = l->data;
7587 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7588 if (f && g_file_equal (file, f))
7592 uri = g_file_get_uri (file);
7594 GTK_FILE_CHOOSER_ERROR,
7595 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7596 _("Shortcut %s already exists"),
7604 data = g_new0 (struct AddShortcutData, 1);
7605 data->impl = g_object_ref (impl);
7606 data->file = g_object_ref (file);
7608 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7610 add_shortcut_get_info_cb, data);
7615 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7616 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7622 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7626 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7633 for (l = impl->loading_shortcuts; l; l = l->next)
7635 GCancellable *c = l->data;
7638 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7639 if (f && g_file_equal (file, f))
7641 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7642 g_cancellable_cancel (c);
7647 if (impl->num_shortcuts == 0)
7650 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7651 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7652 g_assert_not_reached ();
7654 for (i = 0; i < impl->num_shortcuts; i++)
7657 ShortcutType shortcut_type;
7660 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7661 SHORTCUTS_COL_DATA, &col_data,
7662 SHORTCUTS_COL_TYPE, &shortcut_type,
7664 g_assert (col_data != NULL);
7665 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7667 shortcut = col_data;
7668 if (g_file_equal (shortcut, file))
7670 shortcuts_remove_rows (impl, pos + i, 1);
7671 impl->num_shortcuts--;
7675 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7676 g_assert_not_reached ();
7681 uri = g_file_get_uri (file);
7682 /* translators, "Shortcut" means "Bookmark" here */
7684 GTK_FILE_CHOOSER_ERROR,
7685 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7686 _("Shortcut %s does not exist"),
7694 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7696 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7702 if (impl->num_shortcuts == 0)
7705 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7706 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7707 g_assert_not_reached ();
7711 for (i = 0; i < impl->num_shortcuts; i++)
7714 ShortcutType shortcut_type;
7717 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7718 SHORTCUTS_COL_DATA, &col_data,
7719 SHORTCUTS_COL_TYPE, &shortcut_type,
7721 g_assert (col_data != NULL);
7722 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7724 shortcut = col_data;
7725 list = g_slist_prepend (list, g_object_ref (shortcut));
7727 if (i != impl->num_shortcuts - 1)
7729 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7730 g_assert_not_reached ();
7734 return g_slist_reverse (list);
7737 /* Guesses a size based upon font sizes */
7739 find_good_size_from_style (GtkWidget *widget,
7743 GtkFileChooserDefault *impl;
7748 g_assert (widget->style != NULL);
7749 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7751 if (impl->default_width == 0 &&
7752 impl->default_height == 0)
7754 screen = gtk_widget_get_screen (widget);
7757 resolution = gdk_screen_get_resolution (screen);
7758 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7762 resolution = 96.0; /* wheeee */
7764 font_size = pango_font_description_get_size (widget->style->font_desc);
7765 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7767 impl->default_width = font_size * NUM_CHARS;
7768 impl->default_height = font_size * NUM_LINES;
7771 *width = impl->default_width;
7772 *height = impl->default_height;
7776 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7777 gint *default_width,
7778 gint *default_height)
7780 GtkFileChooserDefault *impl;
7783 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7784 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7786 if (impl->preview_widget_active &&
7787 impl->preview_widget &&
7788 GTK_WIDGET_VISIBLE (impl->preview_widget))
7790 gtk_widget_size_request (impl->preview_box, &req);
7791 *default_width += PREVIEW_HBOX_SPACING + req.width;
7794 if (impl->extra_widget &&
7795 GTK_WIDGET_VISIBLE (impl->extra_widget))
7797 gtk_widget_size_request (impl->extra_align, &req);
7798 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7802 struct switch_folder_closure {
7803 GtkFileChooserDefault *impl;
7808 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7810 switch_folder_foreach_cb (GtkTreeModel *model,
7815 struct switch_folder_closure *closure;
7816 GtkTreeIter child_iter;
7820 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7822 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7823 closure->num_selected++;
7826 /* Changes to the selected folder in the list view */
7828 switch_to_selected_folder (GtkFileChooserDefault *impl)
7830 GtkTreeSelection *selection;
7831 struct switch_folder_closure closure;
7833 /* We do this with foreach() rather than get_selected() as we may be in
7834 * multiple selection mode
7837 closure.impl = impl;
7838 closure.file = NULL;
7839 closure.num_selected = 0;
7841 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7842 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7844 g_assert (closure.file && closure.num_selected == 1);
7846 change_folder_and_display_error (impl, closure.file, FALSE);
7849 /* Gets the GFileInfo for the selected row in the file list; assumes single
7853 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7854 gboolean *had_selection)
7856 GtkTreeSelection *selection;
7857 GtkTreeIter iter, child_iter;
7860 g_assert (!impl->select_multiple);
7861 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7862 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7864 *had_selection = FALSE;
7868 *had_selection = TRUE;
7870 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7874 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7878 /* Gets the display name of the selected file in the file list; assumes single
7879 * selection mode and that something is selected.
7881 static const gchar *
7882 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7885 gboolean had_selection;
7887 info = get_selected_file_info_from_file_list (impl, &had_selection);
7888 g_assert (had_selection);
7889 g_assert (info != NULL);
7891 return g_file_info_get_display_name (info);
7895 add_custom_button_to_dialog (GtkDialog *dialog,
7896 const gchar *mnemonic_label,
7897 const gchar *stock_id,
7902 button = gtk_button_new_with_mnemonic (mnemonic_label);
7903 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7904 gtk_button_set_image (GTK_BUTTON (button),
7905 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7906 gtk_widget_show (button);
7908 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7911 /* Presents an overwrite confirmation dialog; returns whether we should accept
7915 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7916 const gchar *file_part,
7917 const gchar *folder_display_name)
7919 GtkWindow *toplevel;
7923 toplevel = get_toplevel (GTK_WIDGET (impl));
7925 dialog = gtk_message_dialog_new (toplevel,
7926 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7927 GTK_MESSAGE_QUESTION,
7929 _("A file named \"%s\" already exists. Do you want to replace it?"),
7931 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7932 _("The file already exists in \"%s\". Replacing it will "
7933 "overwrite its contents."),
7934 folder_display_name);
7936 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7937 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7938 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7939 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7940 GTK_RESPONSE_ACCEPT,
7941 GTK_RESPONSE_CANCEL,
7943 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7945 if (toplevel->group)
7946 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7948 response = gtk_dialog_run (GTK_DIALOG (dialog));
7950 gtk_widget_destroy (dialog);
7952 return (response == GTK_RESPONSE_ACCEPT);
7955 struct GetDisplayNameData
7957 GtkFileChooserDefault *impl;
7962 confirmation_confirm_get_info_cb (GCancellable *cancellable,
7964 const GError *error,
7967 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7968 gboolean should_respond = FALSE;
7969 struct GetDisplayNameData *data = user_data;
7971 if (cancellable != data->impl->should_respond_get_info_cancellable)
7974 data->impl->should_respond_get_info_cancellable = NULL;
7980 /* Huh? Did the folder disappear? Let the caller deal with it */
7981 should_respond = TRUE;
7983 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
7985 set_busy_cursor (data->impl, FALSE);
7987 g_signal_emit_by_name (data->impl, "response-requested");
7990 g_object_unref (data->impl);
7991 g_free (data->file_part);
7994 g_object_unref (cancellable);
7997 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7998 * should respond. Can get the file part from the file list or the save entry.
8001 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8002 const gchar *file_part,
8005 GtkFileChooserConfirmation conf;
8007 if (!impl->do_overwrite_confirmation)
8010 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8012 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8016 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8018 struct GetDisplayNameData *data;
8020 g_assert (file_part != NULL);
8022 data = g_new0 (struct GetDisplayNameData, 1);
8023 data->impl = g_object_ref (impl);
8024 data->file_part = g_strdup (file_part);
8026 if (impl->should_respond_get_info_cancellable)
8027 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8029 impl->should_respond_get_info_cancellable =
8030 _gtk_file_system_get_info (impl->file_system, parent_file,
8031 "standard::display-name",
8032 confirmation_confirm_get_info_cb,
8034 set_busy_cursor (data->impl, TRUE);
8038 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8041 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8045 g_assert_not_reached ();
8050 struct FileExistsData
8052 GtkFileChooserDefault *impl;
8053 gboolean file_exists_and_is_not_folder;
8059 save_entry_get_info_cb (GCancellable *cancellable,
8061 const GError *error,
8064 gboolean parent_is_folder;
8065 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8066 struct FileExistsData *data = user_data;
8068 if (cancellable != data->impl->should_respond_get_info_cancellable)
8071 data->impl->should_respond_get_info_cancellable = NULL;
8073 set_busy_cursor (data->impl, FALSE);
8079 parent_is_folder = FALSE;
8081 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8083 if (parent_is_folder)
8085 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8087 if (data->file_exists_and_is_not_folder)
8090 const char *file_part;
8092 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8093 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8096 g_signal_emit_by_name (data->impl, "response-requested");
8099 g_signal_emit_by_name (data->impl, "response-requested");
8101 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8103 GError *error = NULL;
8105 set_busy_cursor (data->impl, TRUE);
8106 g_file_make_directory (data->file, NULL, &error);
8107 set_busy_cursor (data->impl, FALSE);
8110 g_signal_emit_by_name (data->impl, "response-requested");
8112 error_creating_folder_dialog (data->impl, data->file, error);
8117 /* This will display an error, which is what we want */
8118 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8122 g_object_unref (data->impl);
8123 g_object_unref (data->file);
8124 g_object_unref (data->parent_file);
8127 g_object_unref (cancellable);
8131 file_exists_get_info_cb (GCancellable *cancellable,
8133 const GError *error,
8136 gboolean data_ownership_taken = FALSE;
8137 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8138 gboolean file_exists_and_is_not_folder;
8139 struct FileExistsData *data = user_data;
8141 if (cancellable != data->impl->file_exists_get_info_cancellable)
8144 data->impl->file_exists_get_info_cancellable = NULL;
8146 set_busy_cursor (data->impl, FALSE);
8151 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8153 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8154 /* user typed a filename; we are done */
8155 g_signal_emit_by_name (data->impl, "response-requested");
8156 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8157 && file_exists_and_is_not_folder)
8159 /* Oops, the user typed the name of an existing path which is not
8162 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8163 g_error_copy (error));
8167 /* check that everything up to the last component exists */
8169 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8170 data_ownership_taken = TRUE;
8172 if (data->impl->should_respond_get_info_cancellable)
8173 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8175 data->impl->should_respond_get_info_cancellable =
8176 _gtk_file_system_get_info (data->impl->file_system,
8179 save_entry_get_info_cb,
8181 set_busy_cursor (data->impl, TRUE);
8185 if (!data_ownership_taken)
8187 g_object_unref (data->impl);
8188 g_object_unref (data->file);
8189 g_object_unref (data->parent_file);
8193 g_object_unref (cancellable);
8197 paste_text_received (GtkClipboard *clipboard,
8199 GtkFileChooserDefault *impl)
8206 file = g_file_new_for_uri (text);
8208 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8209 location_popup_handler (impl, text);
8211 g_object_unref (file);
8214 /* Handler for the "location-popup-on-paste" keybinding signal */
8216 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8218 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8219 GDK_SELECTION_CLIPBOARD);
8220 gtk_clipboard_request_text (clipboard,
8221 (GtkClipboardTextReceivedFunc) paste_text_received,
8226 /* Implementation for GtkFileChooserEmbed::should_respond() */
8228 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8230 GtkFileChooserDefault *impl;
8231 GtkWidget *toplevel;
8232 GtkWidget *current_focus;
8234 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8236 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8237 g_assert (GTK_IS_WINDOW (toplevel));
8239 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8241 if (current_focus == impl->browse_files_tree_view)
8243 /* The following array encodes what we do based on the impl->action and the
8244 * number of files selected.
8247 NOOP, /* Do nothing (don't respond) */
8248 RESPOND, /* Respond immediately */
8249 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8250 ALL_FILES, /* Respond only if everything selected is a file */
8251 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8252 SAVE_ENTRY, /* Go to the code for handling the save entry */
8253 NOT_REACHED /* Sanity check */
8255 static const ActionToTake what_to_do[4][3] = {
8256 /* 0 selected 1 selected many selected */
8257 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8258 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8259 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8260 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8264 gboolean all_files, all_folders;
8266 ActionToTake action;
8270 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8272 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8273 return search_should_respond (impl);
8275 if (impl->operation_mode == OPERATION_MODE_RECENT)
8276 return recent_should_respond (impl);
8278 selection_check (impl, &num_selected, &all_files, &all_folders);
8280 if (num_selected > 2)
8285 action = what_to_do [impl->action] [k];
8295 case RESPOND_OR_SWITCH:
8296 g_assert (num_selected == 1);
8300 switch_to_selected_folder (impl);
8303 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8304 return should_respond_after_confirm_overwrite (impl,
8305 get_display_name_from_file_list (impl),
8306 impl->current_folder);
8320 g_assert_not_reached ();
8323 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8326 gboolean is_well_formed, is_empty, is_file_part_empty;
8329 GtkFileChooserEntry *entry;
8334 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8335 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8336 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8337 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8338 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8340 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8341 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8343 if (is_empty || !is_well_formed)
8346 g_assert (file != NULL);
8351 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8352 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8354 change_folder_and_display_error (impl, file, TRUE);
8357 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8358 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8360 /* The folder already exists, so we do not need to create it.
8361 * Just respond to terminate the dialog.
8367 g_assert_not_reached ();
8373 struct FileExistsData *data;
8375 /* We need to check whether file exists and is not a folder */
8377 data = g_new0 (struct FileExistsData, 1);
8378 data->impl = g_object_ref (impl);
8379 data->file = g_object_ref (file);
8380 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8382 if (impl->file_exists_get_info_cancellable)
8383 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8385 impl->file_exists_get_info_cancellable =
8386 _gtk_file_system_get_info (impl->file_system, file,
8388 file_exists_get_info_cb,
8391 set_busy_cursor (impl, TRUE);
8395 g_error_free (error);
8398 g_object_unref (file);
8401 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8403 /* The focus is on a dialog's action area button, *and* the widget that
8404 * was focused immediately before it is the file list.
8408 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8410 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8413 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8415 /* The focus is on a dialog's action area button, *and* the widget that
8416 * was focused immediately before it is the location entry.
8421 /* The focus is on a dialog's action area button or something else */
8422 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8423 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8428 g_assert_not_reached ();
8432 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8434 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8436 GtkFileChooserDefault *impl;
8439 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8441 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8442 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8444 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8445 widget = impl->browse_files_tree_view;
8447 widget = impl->location_entry;
8449 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8450 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8451 widget = impl->location_entry;
8454 g_assert_not_reached ();
8458 g_assert (widget != NULL);
8459 gtk_widget_grab_focus (widget);
8462 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8464 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8474 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8475 *list = g_slist_prepend (*list, file);
8478 /* Constructs a list of the selected paths in search mode */
8480 search_get_selected_files (GtkFileChooserDefault *impl)
8483 GtkTreeSelection *selection;
8487 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8488 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8489 result = g_slist_reverse (result);
8494 /* Called from ::should_respond(). We return whether there are selected files
8495 * in the search list.
8498 search_should_respond (GtkFileChooserDefault *impl)
8500 GtkTreeSelection *selection;
8502 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8504 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8505 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8508 struct SearchHitInsertRequest
8510 GtkFileChooserDefault *impl;
8512 GtkTreeRowReference *row_ref;
8516 search_hit_get_info_cb (GCancellable *cancellable,
8518 const GError *error,
8521 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8522 GdkPixbuf *pixbuf = NULL;
8525 GCancellable *model_cancellable;
8526 gboolean is_folder = FALSE;
8529 struct SearchHitInsertRequest *request = data;
8531 if (!request->impl->search_model)
8534 path = gtk_tree_row_reference_get_path (request->row_ref);
8538 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8540 gtk_tree_path_free (path);
8542 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8543 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8545 if (cancellable != model_cancellable)
8548 /* set the cancellable to NULL in the model */
8549 gtk_list_store_set (request->impl->search_model, &iter,
8550 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8558 gtk_list_store_remove (request->impl->search_model, &iter);
8562 display_name = g_strdup (g_file_info_get_display_name (info));
8563 mime_type = g_strdup (g_file_info_get_content_type (info));
8564 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8565 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8566 request->impl->icon_size);
8568 gtk_list_store_set (request->impl->search_model, &iter,
8569 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8570 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8571 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8572 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8576 g_object_unref (pixbuf);
8579 g_object_unref (request->impl);
8580 g_object_unref (request->file);
8581 gtk_tree_row_reference_free (request->row_ref);
8584 g_object_unref (cancellable);
8587 /* Adds one hit from the search engine to the search_model */
8589 search_add_hit (GtkFileChooserDefault *impl,
8595 char *collation_key;
8596 struct stat statbuf;
8597 struct stat *statbuf_copy;
8600 GCancellable *cancellable;
8601 struct SearchHitInsertRequest *request;
8603 file = g_file_new_for_uri (uri);
8605 if (!g_file_is_native (file))
8607 g_object_unref (file);
8611 filename = g_file_get_path (file);
8613 if (stat (filename, &statbuf) != 0)
8615 g_object_unref (file);
8620 statbuf_copy = g_new (struct stat, 1);
8621 *statbuf_copy = statbuf;
8623 tmp = g_file_get_parse_name (file);
8624 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8627 request = g_new0 (struct SearchHitInsertRequest, 1);
8628 request->impl = g_object_ref (impl);
8629 request->file = g_object_ref (file);
8631 gtk_list_store_append (impl->search_model, &iter);
8632 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8634 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8635 gtk_tree_path_free (p);
8637 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8638 "standard::type,standard::icon,"
8639 "standard::content-type,standard::display-name",
8640 search_hit_get_info_cb,
8643 gtk_list_store_set (impl->search_model, &iter,
8644 SEARCH_MODEL_COL_FILE, file,
8645 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8646 SEARCH_MODEL_COL_STAT, statbuf_copy,
8647 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8650 g_object_unref (file);
8654 /* Callback used from GtkSearchEngine when we get new hits */
8656 search_engine_hits_added_cb (GtkSearchEngine *engine,
8660 GtkFileChooserDefault *impl;
8663 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8665 for (l = hits; l; l = l->next)
8666 search_add_hit (impl, (gchar*)l->data);
8669 /* Callback used from GtkSearchEngine when the query is done running */
8671 search_engine_finished_cb (GtkSearchEngine *engine,
8674 GtkFileChooserDefault *impl;
8676 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8679 /* EB: setting the model here will avoid loads of row events,
8680 * but it'll make the search look like blocked.
8682 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8683 GTK_TREE_MODEL (impl->search_model_filter));
8686 /* FMQ: if search was empty, say that we got no hits */
8687 set_busy_cursor (impl, FALSE);
8690 /* Displays a generic error when we cannot create a GtkSearchEngine.
8691 * It would be better if _gtk_search_engine_new() gave us a GError
8692 * with a better message, but it doesn't do that right now.
8695 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8697 error_message (impl,
8698 _("Could not start the search process"),
8699 _("The program was not able to create a connection to the indexer "
8700 "daemon. Please make sure it is running."));
8704 search_engine_error_cb (GtkSearchEngine *engine,
8705 const gchar *message,
8708 GtkFileChooserDefault *impl;
8710 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8712 search_stop_searching (impl, TRUE);
8713 error_message (impl, _("Could not send the search request"), message);
8715 set_busy_cursor (impl, FALSE);
8718 /* Frees the data in the search_model */
8720 search_clear_model (GtkFileChooserDefault *impl,
8721 gboolean remove_from_treeview)
8723 GtkTreeModel *model;
8726 if (!impl->search_model)
8729 model = GTK_TREE_MODEL (impl->search_model);
8731 if (gtk_tree_model_get_iter_first (model, &iter))
8734 GCancellable *cancellable;
8736 gtk_tree_model_get (model, &iter,
8737 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8741 g_cancellable_cancel (cancellable);
8743 while (gtk_tree_model_iter_next (model, &iter));
8745 g_object_unref (impl->search_model);
8746 impl->search_model = NULL;
8748 g_object_unref (impl->search_model_filter);
8749 impl->search_model_filter = NULL;
8751 g_object_unref (impl->search_model_sort);
8752 impl->search_model_sort = NULL;
8754 if (remove_from_treeview)
8755 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8758 /* Stops any ongoing searches; does not touch the search_model */
8760 search_stop_searching (GtkFileChooserDefault *impl,
8761 gboolean remove_query)
8763 if (remove_query && impl->search_query)
8765 g_object_unref (impl->search_query);
8766 impl->search_query = NULL;
8769 if (impl->search_engine)
8771 _gtk_search_engine_stop (impl->search_engine);
8773 g_object_unref (impl->search_engine);
8774 impl->search_engine = NULL;
8778 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8780 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8782 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8784 search_stop_searching (impl, FALSE);
8785 search_clear_model (impl, TRUE);
8787 gtk_widget_destroy (impl->search_hbox);
8788 impl->search_hbox = NULL;
8789 impl->search_entry = NULL;
8791 gtk_widget_show (impl->browse_path_bar);
8792 gtk_widget_show (impl->browse_new_folder_button);
8794 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8795 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8797 gtk_widget_show (impl->location_button);
8799 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8800 gtk_widget_show (impl->location_entry_box);
8803 impl->operation_mode = OPERATION_MODE_BROWSE;
8805 file_list_set_sort_column_ids (impl);
8808 /* Sort callback from the path column */
8810 search_column_path_sort_func (GtkTreeModel *model,
8815 GtkFileChooserDefault *impl = user_data;
8816 GtkTreeIter child_a, child_b;
8817 const char *collation_key_a, *collation_key_b;
8818 gboolean is_folder_a, is_folder_b;
8820 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8821 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8823 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8824 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8825 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8827 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8828 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8829 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8832 if (!collation_key_a)
8835 if (!collation_key_b)
8838 /* always show folders first */
8839 if (is_folder_a != is_folder_b)
8840 return is_folder_a ? 1 : -1;
8842 return strcmp (collation_key_a, collation_key_b);
8845 /* Sort callback from the modification time column */
8847 search_column_mtime_sort_func (GtkTreeModel *model,
8852 GtkFileChooserDefault *impl = user_data;
8853 GtkTreeIter child_a, child_b;
8854 const struct stat *statbuf_a, *statbuf_b;
8855 gboolean is_folder_a, is_folder_b;
8857 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8858 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8860 /* Note that although we store a whole struct stat in the model, we only
8861 * compare the mtime here. If we add another column relative to a struct stat
8862 * (e.g. a file size column), we'll want another sort callback similar to this
8865 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8866 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8867 SEARCH_MODEL_COL_STAT, &statbuf_a,
8869 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8870 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8871 SEARCH_MODEL_COL_STAT, &statbuf_b,
8880 if (is_folder_a != is_folder_b)
8881 return is_folder_a ? 1 : -1;
8883 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8885 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8892 search_get_is_filtered (GtkFileChooserDefault *impl,
8894 const gchar *display_name,
8895 const gchar *mime_type)
8897 GtkFileFilterInfo filter_info;
8898 GtkFileFilterFlags needed;
8901 if (!impl->current_filter)
8904 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8905 needed = gtk_file_filter_get_needed (impl->current_filter);
8907 filter_info.display_name = display_name;
8908 filter_info.mime_type = mime_type;
8910 if (needed & GTK_FILE_FILTER_FILENAME)
8912 filter_info.filename = g_file_get_path (file);
8913 if (filter_info.filename)
8914 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8917 filter_info.filename = NULL;
8919 if (needed & GTK_FILE_FILTER_URI)
8921 filter_info.uri = g_file_get_uri (file);
8922 if (filter_info.uri)
8923 filter_info.contains |= GTK_FILE_FILTER_URI;
8926 filter_info.uri = NULL;
8928 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8930 if (filter_info.filename)
8931 g_free ((gchar *) filter_info.filename);
8932 if (filter_info.uri)
8933 g_free ((gchar *) filter_info.uri);
8939 /* Visibility function for the recent filter model */
8941 search_model_visible_func (GtkTreeModel *model,
8945 GtkFileChooserDefault *impl = user_data;
8947 gchar *display_name, *mime_type;
8950 if (!impl->current_filter)
8953 gtk_tree_model_get (model, iter,
8954 SEARCH_MODEL_COL_FILE, &file,
8955 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
8956 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8957 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8966 return !search_get_is_filtered (impl, file, display_name, mime_type);
8969 /* Creates the search_model and puts it in the tree view */
8971 search_setup_model (GtkFileChooserDefault *impl)
8973 g_assert (impl->search_model == NULL);
8974 g_assert (impl->search_model_filter == NULL);
8975 g_assert (impl->search_model_sort == NULL);
8977 /* We store these columns in the search model:
8979 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
8980 * as a pointer not as a G_TYPE_FILE
8981 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
8982 * as a pointer not as a G_TYPE_STRING
8983 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
8984 * as a pointer not as a G_TYPE_STRING
8985 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
8986 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
8987 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
8988 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
8989 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
8991 * Keep this in sync with the enumeration defined near the beginning
8994 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9004 impl->search_model_filter =
9005 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9006 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9007 search_model_visible_func,
9010 impl->search_model_sort =
9011 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9012 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9013 SEARCH_MODEL_COL_FILE,
9014 search_column_path_sort_func,
9016 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9017 SEARCH_MODEL_COL_STAT,
9018 search_column_mtime_sort_func,
9020 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9021 SEARCH_MODEL_COL_STAT,
9022 GTK_SORT_DESCENDING);
9024 /* EB: setting the model here will make the hits list update feel
9025 * more "alive" than setting the model at the end of the search
9028 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9029 GTK_TREE_MODEL (impl->search_model_sort));
9033 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9034 GtkTreeIter *child_iter,
9039 if (!impl->search_model)
9042 if (!impl->search_model_filter || !impl->search_model_sort)
9045 /* pass 1: get the iterator in the filter model */
9046 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9049 /* pass 2: get the iterator in the real model */
9050 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9051 child_iter, &middle);
9054 /* Creates a new query with the specified text and launches it */
9056 search_start_query (GtkFileChooserDefault *impl,
9057 const gchar *query_text)
9059 search_stop_searching (impl, FALSE);
9060 search_clear_model (impl, TRUE);
9061 search_setup_model (impl);
9062 set_busy_cursor (impl, TRUE);
9064 if (impl->search_engine == NULL)
9065 impl->search_engine = _gtk_search_engine_new ();
9067 if (!impl->search_engine)
9069 set_busy_cursor (impl, FALSE);
9070 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9074 if (!impl->search_query)
9076 impl->search_query = _gtk_query_new ();
9077 _gtk_query_set_text (impl->search_query, query_text);
9080 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9082 g_signal_connect (impl->search_engine, "hits-added",
9083 G_CALLBACK (search_engine_hits_added_cb), impl);
9084 g_signal_connect (impl->search_engine, "finished",
9085 G_CALLBACK (search_engine_finished_cb), impl);
9086 g_signal_connect (impl->search_engine, "error",
9087 G_CALLBACK (search_engine_error_cb), impl);
9089 _gtk_search_engine_start (impl->search_engine);
9092 /* Callback used when the user presses Enter while typing on the search
9093 * entry; starts the query
9096 search_entry_activate_cb (GtkEntry *entry,
9099 GtkFileChooserDefault *impl;
9102 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9104 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9105 if (strlen (text) == 0)
9108 /* reset any existing query object */
9109 if (impl->search_query)
9111 g_object_unref (impl->search_query);
9112 impl->search_query = NULL;
9115 search_start_query (impl, text);
9118 /* Hides the path bar and creates the search entry */
9120 search_setup_widgets (GtkFileChooserDefault *impl)
9124 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9128 label = gtk_label_new_with_mnemonic (_("_Search:"));
9129 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9133 impl->search_entry = gtk_entry_new ();
9134 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9135 g_signal_connect (impl->search_entry, "activate",
9136 G_CALLBACK (search_entry_activate_cb),
9138 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9140 /* if there already is a query, restart it */
9141 if (impl->search_query)
9143 gchar *query = _gtk_query_get_text (impl->search_query);
9147 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9148 search_start_query (impl, query);
9154 g_object_unref (impl->search_query);
9155 impl->search_query = NULL;
9159 gtk_widget_hide (impl->browse_path_bar);
9160 gtk_widget_hide (impl->browse_new_folder_button);
9162 /* Box for search widgets */
9163 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9164 gtk_widget_show_all (impl->search_hbox);
9166 /* Hide the location widgets temporarily */
9168 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9169 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9171 gtk_widget_hide (impl->location_button);
9172 gtk_widget_hide (impl->location_entry_box);
9175 gtk_widget_grab_focus (impl->search_entry);
9177 /* FMQ: hide the filter combo? */
9180 /* Main entry point to the searching functions; this gets called when the user
9181 * activates the Search shortcut.
9184 search_activate (GtkFileChooserDefault *impl)
9186 OperationMode previous_mode;
9188 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9190 gtk_widget_grab_focus (impl->search_entry);
9194 previous_mode = impl->operation_mode;
9195 impl->operation_mode = OPERATION_MODE_SEARCH;
9197 switch (previous_mode)
9199 case OPERATION_MODE_RECENT:
9200 recent_stop_loading (impl);
9201 recent_clear_model (impl, TRUE);
9204 case OPERATION_MODE_BROWSE:
9205 stop_loading_and_clear_list_model (impl);
9208 case OPERATION_MODE_SEARCH:
9209 g_assert_not_reached ();
9213 g_assert (impl->search_hbox == NULL);
9214 g_assert (impl->search_entry == NULL);
9215 g_assert (impl->search_model == NULL);
9216 g_assert (impl->search_model_filter == NULL);
9218 search_setup_widgets (impl);
9219 file_list_set_sort_column_ids (impl);
9223 * Recent files support
9226 /* Frees the data in the recent_model */
9228 recent_clear_model (GtkFileChooserDefault *impl,
9229 gboolean remove_from_treeview)
9231 GtkTreeModel *model;
9234 if (!impl->recent_model)
9237 model = GTK_TREE_MODEL (impl->recent_model);
9239 if (remove_from_treeview)
9240 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9242 if (gtk_tree_model_get_iter_first (model, &iter))
9247 GCancellable *cancellable;
9248 GtkRecentInfo *recent_info;
9249 gchar *display_name;
9251 gtk_tree_model_get (model, &iter,
9252 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9253 RECENT_MODEL_COL_FILE, &file,
9254 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9255 RECENT_MODEL_COL_INFO, &recent_info,
9259 g_cancellable_cancel (cancellable);
9261 g_object_unref (file);
9262 gtk_recent_info_unref (recent_info);
9263 g_free (display_name);
9265 while (gtk_tree_model_iter_next (model, &iter));
9268 g_object_unref (impl->recent_model);
9269 impl->recent_model = NULL;
9271 g_object_unref (impl->recent_model_filter);
9272 impl->recent_model_filter = NULL;
9274 g_object_unref (impl->recent_model_sort);
9275 impl->recent_model_sort = NULL;
9278 /* Stops any ongoing loading of the recent files list; does
9279 * not touch the recent_model
9282 recent_stop_loading (GtkFileChooserDefault *impl)
9284 if (impl->load_recent_id)
9286 g_source_remove (impl->load_recent_id);
9287 impl->load_recent_id = 0;
9291 /* Stops any pending load, clears the file list, and switches
9292 * back to OPERATION_MODE_BROWSE
9295 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9297 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9299 recent_stop_loading (impl);
9300 recent_clear_model (impl, TRUE);
9302 gtk_widget_show (impl->browse_path_bar);
9303 gtk_widget_show (impl->browse_new_folder_button);
9305 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9306 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9308 gtk_widget_show (impl->location_button);
9310 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9311 gtk_widget_show (impl->location_entry_box);
9314 impl->operation_mode = OPERATION_MODE_BROWSE;
9316 file_list_set_sort_column_ids (impl);
9319 /* Sort callback from the modification time column */
9321 recent_column_mtime_sort_func (GtkTreeModel *model,
9326 GtkFileChooserDefault *impl = user_data;
9327 GtkTreeIter child_a, child_b;
9328 GtkRecentInfo *info_a, *info_b;
9329 gboolean is_folder_a, is_folder_b;
9331 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9332 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9334 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9335 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9336 RECENT_MODEL_COL_INFO, &info_a,
9338 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9339 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9340 RECENT_MODEL_COL_INFO, &info_b,
9349 /* folders always go first */
9350 if (is_folder_a != is_folder_b)
9351 return is_folder_a ? 1 : -1;
9353 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9355 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9362 recent_column_path_sort_func (GtkTreeModel *model,
9367 GtkFileChooserDefault *impl = user_data;
9368 GtkTreeIter child_a, child_b;
9369 gboolean is_folder_a, is_folder_b;
9370 gchar *name_a, *name_b;
9372 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9373 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9375 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9376 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9377 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9379 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9380 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9381 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9390 if (is_folder_a != is_folder_b)
9391 return is_folder_a ? 1 : -1;
9393 return strcmp (name_a, name_b);
9397 recent_get_is_filtered (GtkFileChooserDefault *impl,
9399 GtkRecentInfo *recent_info)
9401 GtkFileFilterInfo filter_info;
9402 GtkFileFilterFlags needed;
9405 if (!impl->current_filter)
9408 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9409 needed = gtk_file_filter_get_needed (impl->current_filter);
9411 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9412 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9414 if (needed & GTK_FILE_FILTER_FILENAME)
9416 filter_info.filename = g_file_get_path (file);
9417 if (filter_info.filename)
9418 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9421 filter_info.filename = NULL;
9423 if (needed & GTK_FILE_FILTER_URI)
9425 filter_info.uri = g_file_get_uri (file);
9426 if (filter_info.uri)
9427 filter_info.contains |= GTK_FILE_FILTER_URI;
9430 filter_info.uri = NULL;
9432 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9434 if (filter_info.filename)
9435 g_free ((gchar *) filter_info.filename);
9436 if (filter_info.uri)
9437 g_free ((gchar *) filter_info.uri);
9442 /* Visibility function for the recent filter model */
9444 recent_model_visible_func (GtkTreeModel *model,
9448 GtkFileChooserDefault *impl = user_data;
9450 GtkRecentInfo *recent_info;
9453 if (!impl->current_filter)
9456 gtk_tree_model_get (model, iter,
9457 RECENT_MODEL_COL_INFO, &recent_info,
9458 RECENT_MODEL_COL_FILE, &file,
9459 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9468 return !recent_get_is_filtered (impl, file, recent_info);
9472 recent_setup_model (GtkFileChooserDefault *impl)
9474 g_assert (impl->recent_model == NULL);
9475 g_assert (impl->recent_model_filter == NULL);
9476 g_assert (impl->recent_model_sort == NULL);
9478 /* We store these columns in the search model:
9480 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9481 * stored as a pointer and not as a G_TYPE_FILE;
9482 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9483 * stored as a pointer and not as a G_TYPE_STRING;
9484 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9485 * as a GTK_TYPE_RECENT_INFO;
9486 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9487 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9488 * and not as a G_TYPE_CANCELLABLE;
9490 * Keep this in sync with the enumeration defined near the beginning of
9493 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9500 impl->recent_model_filter =
9501 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9502 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9503 recent_model_visible_func,
9507 /* this is the model that will actually be added to
9508 * the browse_files_tree_view widget; remember: we are
9509 * stuffing the real model into a filter model and then
9510 * into a sort model; this means we'll have to translate
9511 * the child iterator *twice* to get from a path or an
9512 * iterator coming from the tree view widget to the
9513 * real data inside the model.
9515 impl->recent_model_sort =
9516 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9517 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9518 RECENT_MODEL_COL_FILE,
9519 recent_column_path_sort_func,
9521 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9522 RECENT_MODEL_COL_INFO,
9523 recent_column_mtime_sort_func,
9525 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9526 RECENT_MODEL_COL_INFO,
9527 GTK_SORT_DESCENDING);
9532 GtkFileChooserDefault *impl;
9535 gint n_loaded_items;
9536 guint needs_sorting : 1;
9540 recent_idle_cleanup (gpointer data)
9542 RecentLoadData *load_data = data;
9543 GtkFileChooserDefault *impl = load_data->impl;
9545 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9546 GTK_TREE_MODEL (impl->recent_model_sort));
9548 set_busy_cursor (impl, FALSE);
9550 impl->load_recent_id = 0;
9552 if (load_data->items)
9554 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9555 g_list_free (load_data->items);
9561 struct RecentItemInsertRequest
9563 GtkFileChooserDefault *impl;
9565 GtkTreeRowReference *row_ref;
9569 recent_item_get_info_cb (GCancellable *cancellable,
9571 const GError *error,
9574 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9577 GCancellable *model_cancellable;
9578 gboolean is_folder = FALSE;
9579 struct RecentItemInsertRequest *request = data;
9581 if (!request->impl->recent_model)
9584 path = gtk_tree_row_reference_get_path (request->row_ref);
9588 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9590 gtk_tree_path_free (path);
9592 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9593 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9595 if (cancellable != model_cancellable)
9598 gtk_list_store_set (request->impl->recent_model, &iter,
9599 RECENT_MODEL_COL_CANCELLABLE, NULL,
9607 gtk_list_store_remove (request->impl->recent_model, &iter);
9611 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9613 gtk_list_store_set (request->impl->recent_model, &iter,
9614 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9618 g_object_unref (request->impl);
9619 g_object_unref (request->file);
9620 gtk_tree_row_reference_free (request->row_ref);
9623 g_object_unref (cancellable);
9627 recent_sort_mru (gconstpointer a,
9630 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9631 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9633 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9637 get_recent_files_limit (GtkWidget *widget)
9639 GtkSettings *settings;
9642 if (gtk_widget_has_screen (widget))
9643 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9645 settings = gtk_settings_get_default ();
9647 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9653 recent_idle_load (gpointer data)
9655 RecentLoadData *load_data = data;
9656 GtkFileChooserDefault *impl = load_data->impl;
9659 GtkRecentInfo *info;
9660 const gchar *uri, *display_name;
9662 GCancellable *cancellable;
9663 struct RecentItemInsertRequest *request;
9665 if (!impl->recent_manager)
9668 /* first iteration: load all the items */
9669 if (!load_data->items)
9671 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9672 if (!load_data->items)
9675 load_data->needs_sorting = TRUE;
9680 /* second iteration: preliminary MRU sorting and clamping */
9681 if (load_data->needs_sorting)
9685 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9686 load_data->n_items = g_list_length (load_data->items);
9688 limit = get_recent_files_limit (GTK_WIDGET (impl));
9690 if (limit != -1 && (load_data->n_items > limit))
9694 clamp = g_list_nth (load_data->items, limit - 1);
9695 if (G_LIKELY (clamp))
9700 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9703 load_data->n_items = limit;
9707 load_data->n_loaded_items = 0;
9708 load_data->needs_sorting = FALSE;
9713 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9714 g_assert (info != NULL);
9716 uri = gtk_recent_info_get_uri (info);
9717 display_name = gtk_recent_info_get_display_name (info);
9718 file = g_file_new_for_uri (uri);
9720 gtk_list_store_append (impl->recent_model, &iter);
9721 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9723 request = g_new0 (struct RecentItemInsertRequest, 1);
9724 request->impl = g_object_ref (impl);
9725 request->file = g_object_ref (file);
9726 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9727 gtk_tree_path_free (p);
9729 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9731 recent_item_get_info_cb,
9734 gtk_list_store_set (impl->recent_model, &iter,
9735 RECENT_MODEL_COL_FILE, file,
9736 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9737 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9738 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9741 load_data->n_loaded_items += 1;
9743 /* finished loading items */
9744 if (load_data->n_loaded_items == load_data->n_items)
9746 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9747 g_list_free (load_data->items);
9748 load_data->items = NULL;
9757 recent_start_loading (GtkFileChooserDefault *impl)
9759 RecentLoadData *load_data;
9761 recent_stop_loading (impl);
9762 recent_clear_model (impl, TRUE);
9763 recent_setup_model (impl);
9764 set_busy_cursor (impl, TRUE);
9766 g_assert (impl->load_recent_id == 0);
9768 load_data = g_new (RecentLoadData, 1);
9769 load_data->impl = impl;
9770 load_data->items = NULL;
9771 load_data->n_items = 0;
9772 load_data->n_loaded_items = 0;
9773 load_data->needs_sorting = TRUE;
9775 /* begin lazy loading the recent files into the model */
9776 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9779 recent_idle_cleanup);
9783 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9793 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9794 *list = g_slist_prepend (*list, g_object_ref (file));
9797 /* Constructs a list of the selected paths in recent files mode */
9799 recent_get_selected_files (GtkFileChooserDefault *impl)
9802 GtkTreeSelection *selection;
9806 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9807 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9808 result = g_slist_reverse (result);
9813 /* Called from ::should_respond(). We return whether there are selected
9814 * files in the recent files list.
9817 recent_should_respond (GtkFileChooserDefault *impl)
9819 GtkTreeSelection *selection;
9821 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9823 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9824 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9827 /* Hide the location widgets temporarily */
9829 recent_hide_entry (GtkFileChooserDefault *impl)
9831 gtk_widget_hide (impl->browse_path_bar);
9832 gtk_widget_hide (impl->browse_new_folder_button);
9834 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9835 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9837 gtk_widget_hide (impl->location_button);
9838 gtk_widget_hide (impl->location_entry_box);
9842 /* Main entry point to the recent files functions; this gets called when
9843 * the user activates the Recently Used shortcut.
9846 recent_activate (GtkFileChooserDefault *impl)
9848 OperationMode previous_mode;
9850 if (impl->operation_mode == OPERATION_MODE_RECENT)
9853 previous_mode = impl->operation_mode;
9854 impl->operation_mode = OPERATION_MODE_RECENT;
9856 switch (previous_mode)
9858 case OPERATION_MODE_SEARCH:
9859 search_stop_searching (impl, FALSE);
9860 search_clear_model (impl, TRUE);
9862 gtk_widget_destroy (impl->search_hbox);
9863 impl->search_hbox = NULL;
9864 impl->search_entry = NULL;
9867 case OPERATION_MODE_BROWSE:
9868 stop_loading_and_clear_list_model (impl);
9871 case OPERATION_MODE_RECENT:
9872 g_assert_not_reached ();
9876 recent_hide_entry (impl);
9877 file_list_set_sort_column_ids (impl);
9878 recent_start_loading (impl);
9881 /* convert an iterator coming from the model bound to
9882 * browse_files_tree_view to an interator inside the
9886 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9887 GtkTreeIter *child_iter,
9892 if (!impl->recent_model)
9895 if (!impl->recent_model_filter || !impl->recent_model_sort)
9898 /* pass 1: get the iterator in the filter model */
9899 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9902 /* pass 2: get the iterator in the real model */
9903 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9910 set_current_filter (GtkFileChooserDefault *impl,
9911 GtkFileFilter *filter)
9913 if (impl->current_filter != filter)
9917 /* NULL filters are allowed to reset to non-filtered status
9919 filter_index = g_slist_index (impl->filters, filter);
9920 if (impl->filters && filter && filter_index < 0)
9923 if (impl->current_filter)
9924 g_object_unref (impl->current_filter);
9925 impl->current_filter = filter;
9926 if (impl->current_filter)
9928 g_object_ref_sink (impl->current_filter);
9932 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9935 if (impl->browse_files_model)
9936 install_list_model_filter (impl);
9938 if (impl->search_model_filter)
9939 gtk_tree_model_filter_refilter (impl->search_model_filter);
9941 if (impl->recent_model_filter)
9942 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9944 g_object_notify (G_OBJECT (impl), "filter");
9949 filter_combo_changed (GtkComboBox *combo_box,
9950 GtkFileChooserDefault *impl)
9952 gint new_index = gtk_combo_box_get_active (combo_box);
9953 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9955 set_current_filter (impl, new_filter);
9959 check_preview_change (GtkFileChooserDefault *impl)
9961 GtkTreePath *cursor_path;
9963 const char *new_display_name;
9965 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9967 new_display_name = NULL;
9970 GtkTreeIter child_iter;
9972 if (impl->operation_mode == OPERATION_MODE_BROWSE)
9974 if (impl->sort_model)
9977 GFileInfo *new_info;
9979 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
9980 gtk_tree_path_free (cursor_path);
9982 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
9984 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
9985 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
9987 new_display_name = g_file_info_get_display_name (new_info);
9990 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
9994 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
9995 &iter, cursor_path);
9996 gtk_tree_path_free (cursor_path);
9998 search_get_valid_child_iter (impl, &child_iter, &iter);
9999 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10000 SEARCH_MODEL_COL_FILE, &new_file,
10001 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10004 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10008 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10009 &iter, cursor_path);
10010 gtk_tree_path_free (cursor_path);
10012 recent_get_valid_child_iter (impl, &child_iter, &iter);
10013 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10014 RECENT_MODEL_COL_FILE, &new_file,
10015 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10020 if (new_file != impl->preview_file &&
10021 !(new_file && impl->preview_file &&
10022 g_file_equal (new_file, impl->preview_file)))
10024 if (impl->preview_file)
10026 g_object_unref (impl->preview_file);
10027 g_free (impl->preview_display_name);
10032 impl->preview_file = g_object_ref (new_file);
10033 impl->preview_display_name = g_strdup (new_display_name);
10037 impl->preview_file = NULL;
10038 impl->preview_display_name = NULL;
10041 if (impl->use_preview_label && impl->preview_label)
10042 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10044 g_signal_emit_by_name (impl, "update-preview");
10049 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10050 GtkFileSystemVolume *volume,
10051 const GError *error,
10055 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10056 GtkFileChooserDefault *impl = data;
10058 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10061 impl->shortcuts_activate_iter_cancellable = NULL;
10063 set_busy_cursor (impl, FALSE);
10070 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10074 name = _gtk_file_system_volume_get_display_name (volume);
10075 msg = g_strdup_printf (_("Could not mount %s"), name);
10077 error_message (impl, msg, error->message);
10086 file = _gtk_file_system_volume_get_root (volume);
10089 change_folder_and_display_error (impl, file, FALSE);
10090 g_object_unref (file);
10094 g_object_unref (impl);
10095 g_object_unref (cancellable);
10099 /* Activates a volume by mounting it if necessary and then switching to its
10103 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10104 GtkFileSystemVolume *volume)
10108 switch (impl->operation_mode)
10110 case OPERATION_MODE_BROWSE:
10112 case OPERATION_MODE_SEARCH:
10113 search_switch_to_browse_mode (impl);
10115 case OPERATION_MODE_RECENT:
10116 recent_switch_to_browse_mode (impl);
10120 /* We ref the file chooser since volume_mount() may run a main loop, and the
10121 * user could close the file chooser window in the meantime.
10123 g_object_ref (impl);
10125 if (!_gtk_file_system_volume_is_mounted (volume))
10127 set_busy_cursor (impl, TRUE);
10129 impl->shortcuts_activate_iter_cancellable =
10130 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10131 shortcuts_activate_volume_mount_cb,
10132 g_object_ref (impl));
10136 file = _gtk_file_system_volume_get_root (volume);
10139 change_folder_and_display_error (impl, file, FALSE);
10140 g_object_unref (file);
10144 g_object_unref (impl);
10147 /* Opens the folder or volume at the specified iter in the shortcuts model */
10148 struct ShortcutsActivateData
10150 GtkFileChooserDefault *impl;
10155 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10157 const GError *error,
10158 gpointer user_data)
10160 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10161 struct ShortcutsActivateData *data = user_data;
10163 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10166 data->impl->shortcuts_activate_iter_cancellable = NULL;
10171 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10172 change_folder_and_display_error (data->impl, data->file, FALSE);
10174 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10179 g_object_unref (data->impl);
10180 g_object_unref (data->file);
10183 g_object_unref (cancellable);
10187 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10188 GtkFileSystemVolume *volume,
10189 const GError *error,
10190 gpointer user_data)
10192 struct ShortcutsActivateData *data = user_data;
10196 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10198 g_object_unref (data->impl);
10199 g_object_unref (data->file);
10205 data->impl->shortcuts_activate_iter_cancellable =
10206 _gtk_file_system_get_info (data->impl->file_system, data->file,
10208 shortcuts_activate_get_info_cb, data);
10212 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10216 ShortcutType shortcut_type;
10218 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10219 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10220 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10221 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10223 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10224 SHORTCUTS_COL_DATA, &col_data,
10225 SHORTCUTS_COL_TYPE, &shortcut_type,
10228 if (impl->shortcuts_activate_iter_cancellable)
10230 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10231 impl->shortcuts_activate_iter_cancellable = NULL;
10234 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10236 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10238 GtkFileSystemVolume *volume;
10242 shortcuts_activate_volume (impl, volume);
10244 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10246 struct ShortcutsActivateData *data;
10247 GtkFileSystemVolume *volume;
10249 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10251 data = g_new0 (struct ShortcutsActivateData, 1);
10252 data->impl = g_object_ref (impl);
10253 data->file = g_object_ref (col_data);
10255 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10257 GMountOperation *mount_operation;
10258 GtkWidget *toplevel;
10260 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10262 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10264 impl->shortcuts_activate_iter_cancellable =
10265 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10267 shortcuts_activate_mount_enclosing_volume,
10272 impl->shortcuts_activate_iter_cancellable =
10273 _gtk_file_system_get_info (impl->file_system, data->file,
10275 shortcuts_activate_get_info_cb, data);
10278 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10280 search_activate (impl);
10282 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10284 recent_activate (impl);
10288 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10290 shortcuts_key_press_event_cb (GtkWidget *widget,
10291 GdkEventKey *event,
10292 GtkFileChooserDefault *impl)
10296 modifiers = gtk_accelerator_get_default_mod_mask ();
10298 if ((event->keyval == GDK_BackSpace
10299 || event->keyval == GDK_Delete
10300 || event->keyval == GDK_KP_Delete)
10301 && (event->state & modifiers) == 0)
10303 remove_selected_bookmarks (impl);
10307 if ((event->keyval == GDK_F2)
10308 && (event->state & modifiers) == 0)
10310 rename_selected_bookmark (impl);
10318 shortcuts_select_func (GtkTreeSelection *selection,
10319 GtkTreeModel *model,
10321 gboolean path_currently_selected,
10324 GtkFileChooserDefault *impl = data;
10325 GtkTreeIter filter_iter;
10326 ShortcutType shortcut_type;
10328 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10329 g_assert_not_reached ();
10331 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10333 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10337 list_select_func (GtkTreeSelection *selection,
10338 GtkTreeModel *model,
10340 gboolean path_currently_selected,
10343 GtkFileChooserDefault *impl = data;
10345 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10346 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10348 GtkTreeIter iter, child_iter;
10350 switch (impl->operation_mode)
10352 case OPERATION_MODE_SEARCH:
10354 gboolean is_folder;
10356 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10359 search_get_valid_child_iter (impl, &child_iter, &iter);
10360 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10361 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10368 case OPERATION_MODE_RECENT:
10370 gboolean is_folder;
10372 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10375 recent_get_valid_child_iter (impl, &child_iter, &iter);
10376 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10377 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10384 case OPERATION_MODE_BROWSE:
10388 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10391 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10392 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10393 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10404 list_selection_changed (GtkTreeSelection *selection,
10405 GtkFileChooserDefault *impl)
10407 /* See if we are in the new folder editable row for Save mode */
10408 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10409 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10412 gboolean had_selection;
10414 info = get_selected_file_info_from_file_list (impl, &had_selection);
10415 if (!had_selection)
10416 goto out; /* normal processing */
10419 return; /* We are on the editable row for New Folder */
10424 if (impl->location_entry)
10425 update_chooser_entry (impl);
10427 check_preview_change (impl);
10428 bookmarks_check_add_sensitivity (impl);
10430 g_signal_emit_by_name (impl, "selection-changed", 0);
10433 /* Callback used when a row in the file list is activated */
10435 list_row_activated (GtkTreeView *tree_view,
10437 GtkTreeViewColumn *column,
10438 GtkFileChooserDefault *impl)
10441 GtkTreeIter child_iter;
10443 switch (impl->operation_mode)
10445 case OPERATION_MODE_SEARCH:
10448 gboolean is_folder;
10450 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10453 search_get_valid_child_iter (impl, &child_iter, &iter);
10454 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10455 SEARCH_MODEL_COL_FILE, &file,
10456 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10461 change_folder_and_display_error (impl, file, FALSE);
10465 g_signal_emit_by_name (impl, "file-activated");
10469 case OPERATION_MODE_RECENT:
10472 gboolean is_folder;
10474 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10477 recent_get_valid_child_iter (impl, &child_iter, &iter);
10478 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10479 RECENT_MODEL_COL_FILE, &file,
10480 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10485 change_folder_and_display_error (impl, file, FALSE);
10489 g_signal_emit_by_name (impl, "file-activated");
10493 case OPERATION_MODE_BROWSE:
10497 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10500 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10501 &child_iter, &iter);
10502 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10505 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10509 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10510 change_folder_and_display_error (impl, file, FALSE);
10514 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10515 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10516 g_signal_emit_by_name (impl, "file-activated");
10523 path_bar_clicked (GtkPathBar *path_bar,
10526 gboolean child_is_hidden,
10527 GtkFileChooserDefault *impl)
10530 pending_select_files_add (impl, child_file);
10532 if (!change_folder_and_display_error (impl, file, FALSE))
10535 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10536 * show hidden files so that ".baz" appears in the file list, as it will still
10537 * be shown in the path bar: "/foo/[bar]/.baz"
10539 if (child_is_hidden)
10540 g_object_set (impl, "show-hidden", TRUE, NULL);
10544 get_list_file_info (GtkFileChooserDefault *impl,
10547 GtkTreeIter child_iter;
10549 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10553 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10557 list_icon_data_func (GtkTreeViewColumn *tree_column,
10558 GtkCellRenderer *cell,
10559 GtkTreeModel *tree_model,
10563 GtkFileChooserDefault *impl = data;
10564 GtkTreeIter child_iter;
10565 GdkPixbuf *pixbuf = NULL;
10566 gboolean sensitive = TRUE;
10568 profile_start ("start", NULL);
10570 switch (impl->operation_mode)
10572 case OPERATION_MODE_SEARCH:
10574 GtkTreeIter child_iter;
10575 gboolean is_folder;
10577 search_get_valid_child_iter (impl, &child_iter, iter);
10578 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10579 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10580 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10583 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10584 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10585 sensitive = is_folder;
10589 case OPERATION_MODE_RECENT:
10591 GtkTreeIter child_iter;
10592 GtkRecentInfo *info;
10593 gboolean is_folder;
10595 recent_get_valid_child_iter (impl, &child_iter, iter);
10596 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10597 RECENT_MODEL_COL_INFO, &info,
10598 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10601 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10603 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10604 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10605 sensitive = is_folder;
10609 case OPERATION_MODE_BROWSE:
10614 info = get_list_file_info (impl, iter);
10616 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10619 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10624 /* FIXME: NULL GError */
10625 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10630 /* We are on the editable row */
10635 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10636 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10637 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10642 g_object_set (cell,
10644 "sensitive", sensitive,
10648 g_object_unref (pixbuf);
10650 profile_end ("end", NULL);
10654 list_name_data_func (GtkTreeViewColumn *tree_column,
10655 GtkCellRenderer *cell,
10656 GtkTreeModel *tree_model,
10660 GtkFileChooserDefault *impl = data;
10662 gboolean sensitive = TRUE;
10664 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10666 GtkTreeIter child_iter;
10667 gchar *display_name;
10668 gboolean is_folder;
10670 search_get_valid_child_iter (impl, &child_iter, iter);
10671 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10672 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10673 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10676 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10677 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10679 sensitive = is_folder;
10682 g_object_set (cell,
10683 "text", display_name,
10684 "sensitive", sensitive,
10685 "ellipsize", PANGO_ELLIPSIZE_END,
10691 if (impl->operation_mode == OPERATION_MODE_RECENT)
10693 GtkTreeIter child_iter;
10694 GtkRecentInfo *recent_info;
10695 gchar *display_name;
10696 gboolean is_folder;
10698 recent_get_valid_child_iter (impl, &child_iter, iter);
10699 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10700 RECENT_MODEL_COL_INFO, &recent_info,
10701 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10704 display_name = gtk_recent_info_get_short_name (recent_info);
10706 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10707 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10709 sensitive = is_folder;
10712 g_object_set (cell,
10713 "text", display_name,
10714 "sensitive", sensitive,
10715 "ellipsize", PANGO_ELLIPSIZE_END,
10718 g_free (display_name);
10723 info = get_list_file_info (impl, iter);
10728 g_object_set (cell,
10729 "text", _("Type name of new folder"),
10731 "ellipsize", PANGO_ELLIPSIZE_NONE,
10738 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10739 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10741 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10744 g_object_set (cell,
10745 "text", g_file_info_get_display_name (info),
10746 "sensitive", sensitive,
10747 "ellipsize", PANGO_ELLIPSIZE_END,
10753 list_size_data_func (GtkTreeViewColumn *tree_column,
10754 GtkCellRenderer *cell,
10755 GtkTreeModel *tree_model,
10759 GtkFileChooserDefault *impl = data;
10760 GFileInfo *info = get_list_file_info (impl, iter);
10763 gboolean sensitive = TRUE;
10765 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10767 g_object_set (cell,
10769 "sensitive", sensitive,
10774 size = gtk_file_info_get_size (info);
10776 if (size < (gint64)1024)
10777 str = g_strdup_printf (g_dngettext (GETTEXT_DOMAIN, "%d byte", "%d bytes", (gint)size), (gint)size);
10778 else if (size < (gint64)1024*1024)
10779 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10780 else if (size < (gint64)1024*1024*1024)
10781 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10783 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10785 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10786 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10787 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10790 g_object_set (cell,
10792 "sensitive", sensitive,
10793 "alignment", PANGO_ALIGN_RIGHT,
10800 /* Tree column data callback for the file list; fetches the mtime of a file */
10802 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10803 GtkCellRenderer *cell,
10804 GtkTreeModel *tree_model,
10808 GtkFileChooserDefault *impl;
10809 GTimeVal timeval = { 0, };
10811 gchar *date_str = NULL;
10812 gboolean sensitive = TRUE;
10814 const char *locale, *dot = NULL;
10815 gint64 codepage = -1;
10821 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10823 GtkTreeIter child_iter;
10824 struct stat *statbuf;
10825 gboolean is_folder;
10827 search_get_valid_child_iter (impl, &child_iter, iter);
10828 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10829 SEARCH_MODEL_COL_STAT, &statbuf,
10830 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10833 time_mtime = statbuf->st_mtime;
10838 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10839 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10840 sensitive = is_folder;
10842 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10844 GtkTreeIter child_iter;
10845 GtkRecentInfo *info;
10846 gboolean is_folder;
10848 recent_get_valid_child_iter (impl, &child_iter, iter);
10849 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10850 RECENT_MODEL_COL_INFO, &info,
10851 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10855 time_mtime = gtk_recent_info_get_modified (info);
10859 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10860 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10861 sensitive = is_folder;
10867 info = get_list_file_info (impl, iter);
10870 g_object_set (cell,
10877 g_file_info_get_modification_time (info, &timeval);
10878 time_mtime = timeval.tv_sec;
10880 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10881 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10882 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10885 if (G_UNLIKELY (time_mtime == 0))
10886 date_str = g_strdup (_("Unknown"));
10891 struct tm tm_mtime;
10893 const gchar *format;
10894 gchar *locale_format = NULL;
10897 #ifdef HAVE_LOCALTIME_R
10898 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10901 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10905 g_warning ("ptm != NULL failed");
10907 g_object_set (cell,
10908 "text", _("Unknown"),
10909 "sensitive", sensitive,
10914 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10916 #endif /* HAVE_LOCALTIME_R */
10918 g_date_set_time_t (&mtime, time_mtime);
10919 time_now = time (NULL);
10920 g_date_set_time_t (&now, time_now);
10922 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10924 /* Translators: %H means "hours" and %M means "minutes" */
10925 if (days_diff == 0)
10926 format = _("%H:%M");
10927 else if (days_diff == 1)
10928 format = _("Yesterday at %H:%M");
10931 if (days_diff > 1 && days_diff < 7)
10932 format = "%A"; /* Days from last week */
10934 format = "%x"; /* Any other date */
10938 /* g_locale_from_utf8() returns a string in the system
10939 * code-page, which is not always the same as that used by the C
10940 * library. For instance when running a GTK+ program with
10941 * LANG=ko on an English version of Windows, the system
10942 * code-page is 1252, but the code-page used by the C library is
10943 * 949. (It's GTK+ itself that sets the C library locale when it
10944 * notices the LANG environment variable. See gtkmain.c The
10945 * Microsoft C library doesn't look at any locale environment
10946 * variables.) We need to pass strftime() a string in the C
10947 * library's code-page. See bug #509885.
10949 locale = setlocale (LC_ALL, NULL);
10950 if (locale != NULL)
10951 dot = strchr (locale, '.');
10954 codepage = g_ascii_strtoll (dot+1, NULL, 10);
10956 /* All codepages should fit in 16 bits AFAIK */
10957 if (codepage > 0 && codepage < 65536)
10959 sprintf (charset, "CP%u", (guint) codepage);
10960 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
10964 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
10966 if (locale_format != NULL &&
10967 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
10970 /* As above but in opposite direction... */
10971 if (codepage > 0 && codepage < 65536)
10972 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
10974 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
10978 if (date_str == NULL)
10979 date_str = g_strdup (_("Unknown"));
10981 g_free (locale_format);
10984 g_object_set (cell,
10986 "sensitive", sensitive,
10992 _gtk_file_chooser_default_new (const char *file_system)
10994 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
10995 "file-system-backend", file_system,
11000 location_set_user_text (GtkFileChooserDefault *impl,
11003 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11004 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11008 location_popup_handler (GtkFileChooserDefault *impl,
11011 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11013 GtkWidget *widget_to_focus;
11015 /* This will give us the location widgets back */
11016 switch (impl->operation_mode)
11018 case OPERATION_MODE_SEARCH:
11019 search_switch_to_browse_mode (impl);
11021 case OPERATION_MODE_RECENT:
11022 recent_switch_to_browse_mode (impl);
11024 case OPERATION_MODE_BROWSE:
11025 g_assert_not_reached ();
11029 if (impl->current_folder)
11030 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11032 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11033 widget_to_focus = impl->browse_files_tree_view;
11035 widget_to_focus = impl->location_entry;
11037 gtk_widget_grab_focus (widget_to_focus);
11041 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11042 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11044 LocationMode new_mode;
11048 /* since the user typed something, we unconditionally want to turn on the entry */
11049 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11051 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11052 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11053 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11054 new_mode = LOCATION_MODE_PATH_BAR;
11057 g_assert_not_reached ();
11061 location_mode_set (impl, new_mode, TRUE);
11062 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11065 location_set_user_text (impl, path);
11068 location_entry_set_initial_text (impl);
11069 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11073 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11074 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11076 gtk_widget_grab_focus (impl->location_entry);
11078 location_set_user_text (impl, path);
11081 g_assert_not_reached ();
11084 /* Handler for the "up-folder" keybinding signal */
11086 up_folder_handler (GtkFileChooserDefault *impl)
11088 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11091 /* Handler for the "down-folder" keybinding signal */
11093 down_folder_handler (GtkFileChooserDefault *impl)
11095 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11098 /* Switches to the shortcut in the specified index */
11100 switch_to_shortcut (GtkFileChooserDefault *impl,
11105 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11106 g_assert_not_reached ();
11108 shortcuts_activate_iter (impl, &iter);
11111 /* Handler for the "home-folder" keybinding signal */
11113 home_folder_handler (GtkFileChooserDefault *impl)
11115 if (impl->has_home)
11116 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11119 /* Handler for the "desktop-folder" keybinding signal */
11121 desktop_folder_handler (GtkFileChooserDefault *impl)
11123 if (impl->has_desktop)
11124 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11127 /* Handler for the "search-shortcut" keybinding signal */
11129 search_shortcut_handler (GtkFileChooserDefault *impl)
11131 if (impl->has_search)
11133 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11135 /* we want the entry widget to grab the focus the first
11136 * time, not the browse_files_tree_view widget.
11138 if (impl->search_entry)
11139 gtk_widget_grab_focus (impl->search_entry);
11143 /* Handler for the "recent-shortcut" keybinding signal */
11145 recent_shortcut_handler (GtkFileChooserDefault *impl)
11147 if (impl->has_recent)
11148 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11152 quick_bookmark_handler (GtkFileChooserDefault *impl,
11153 gint bookmark_index)
11158 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11161 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11163 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11164 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11167 gtk_tree_path_free (path);
11169 switch_to_shortcut (impl, bookmark_pos);
11173 show_hidden_handler (GtkFileChooserDefault *impl)
11175 g_object_set (impl,
11176 "show-hidden", !impl->show_hidden,
11181 /* Drag and drop interfaces */
11184 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11189 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11191 model->impl = NULL;
11194 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11196 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11199 ShortcutsPaneModelFilter *model;
11203 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11205 pos = *gtk_tree_path_get_indices (path);
11206 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11208 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11211 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11213 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11215 GtkSelectionData *selection_data)
11217 ShortcutsPaneModelFilter *model;
11219 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11226 /* Fill the GtkTreeDragSourceIface vtable */
11228 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11230 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11231 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11235 /* Fill the GtkTreeDragDestIface vtable */
11237 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11239 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11240 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11244 static GtkTreeModel *
11245 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11246 GtkTreeModel *child_model,
11249 ShortcutsPaneModelFilter *model;
11251 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11252 "child-model", child_model,
11253 "virtual-root", root,
11256 model->impl = impl;
11258 return GTK_TREE_MODEL (model);
11264 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11267 RecentModelSort *model;
11268 GtkTreeIter iter, child_iter;
11269 gboolean is_folder;
11271 model = RECENT_MODEL_SORT (drag_source);
11272 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11275 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11276 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11277 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11284 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11286 GtkSelectionData *selection_data)
11288 RecentModelSort *model;
11289 GtkTreeIter iter, child_iter;
11293 model = RECENT_MODEL_SORT (drag_source);
11294 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11297 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11298 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11299 RECENT_MODEL_COL_FILE, &file,
11301 g_assert (file != NULL);
11303 uris[0] = g_file_get_uri (file);
11306 gtk_selection_data_set_uris (selection_data, uris);
11314 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11316 iface->row_draggable = recent_model_sort_row_draggable;
11317 iface->drag_data_get = recent_model_sort_drag_data_get;
11321 _recent_model_sort_class_init (RecentModelSortClass *klass)
11327 _recent_model_sort_init (RecentModelSort *model)
11329 model->impl = NULL;
11332 static GtkTreeModel *
11333 recent_model_sort_new (GtkFileChooserDefault *impl,
11334 GtkTreeModel *child_model)
11336 RecentModelSort *model;
11338 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11339 "model", child_model,
11341 model->impl = impl;
11343 return GTK_TREE_MODEL (model);
11349 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11352 SearchModelSort *model;
11353 GtkTreeIter iter, child_iter;
11354 gboolean is_folder;
11356 model = SEARCH_MODEL_SORT (drag_source);
11357 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11360 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11361 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11362 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11369 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11371 GtkSelectionData *selection_data)
11373 SearchModelSort *model;
11374 GtkTreeIter iter, child_iter;
11378 model = SEARCH_MODEL_SORT (drag_source);
11379 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11382 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11383 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11384 RECENT_MODEL_COL_FILE, &file,
11386 g_assert (file != NULL);
11388 uris[0] = g_file_get_uri (file);
11391 gtk_selection_data_set_uris (selection_data, uris);
11399 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11401 iface->row_draggable = search_model_sort_row_draggable;
11402 iface->drag_data_get = search_model_sort_drag_data_get;
11406 _search_model_sort_class_init (SearchModelSortClass *klass)
11412 _search_model_sort_init (SearchModelSort *model)
11414 model->impl = NULL;
11417 static GtkTreeModel *
11418 search_model_sort_new (GtkFileChooserDefault *impl,
11419 GtkTreeModel *child_model)
11421 SearchModelSort *model;
11423 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11424 "model", child_model,
11426 model->impl = impl;
11428 return GTK_TREE_MODEL (model);