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_MTIME,
207 SEARCH_MODEL_COL_SIZE,
208 SEARCH_MODEL_COL_CANCELLABLE,
209 SEARCH_MODEL_COL_PIXBUF,
210 SEARCH_MODEL_COL_MIME_TYPE,
211 SEARCH_MODEL_COL_IS_FOLDER,
212 SEARCH_MODEL_COL_NUM_COLUMNS
216 RECENT_MODEL_COL_FILE,
217 RECENT_MODEL_COL_DISPLAY_NAME,
218 RECENT_MODEL_COL_INFO,
219 RECENT_MODEL_COL_IS_FOLDER,
220 RECENT_MODEL_COL_CANCELLABLE,
221 RECENT_MODEL_COL_NUM_COLUMNS
224 /* Identifiers for target types */
230 search_is_possible (GtkFileChooserDefault *impl)
232 if (impl->search_engine == NULL)
233 impl->search_engine = _gtk_search_engine_new ();
235 return impl->search_engine != NULL;
238 /* Interesting places in the shortcuts bar */
242 SHORTCUTS_RECENT_SEPARATOR,
247 SHORTCUTS_BOOKMARKS_SEPARATOR,
249 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
250 SHORTCUTS_CURRENT_FOLDER
253 /* Icon size for if we can't get it from the theme */
254 #define FALLBACK_ICON_SIZE 16
256 #define PREVIEW_HBOX_SPACING 12
260 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
261 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
263 static GObject* gtk_file_chooser_default_constructor (GType type,
264 guint n_construct_properties,
265 GObjectConstructParam *construct_params);
266 static void gtk_file_chooser_default_finalize (GObject *object);
267 static void gtk_file_chooser_default_set_property (GObject *object,
271 static void gtk_file_chooser_default_get_property (GObject *object,
275 static void gtk_file_chooser_default_dispose (GObject *object);
276 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
277 static void gtk_file_chooser_default_map (GtkWidget *widget);
278 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
279 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
280 GtkWidget *previous_toplevel);
281 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
282 GtkStyle *previous_style);
283 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
284 GdkScreen *previous_screen);
285 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
286 GtkAllocation *allocation);
288 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
291 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
294 gboolean clear_entry,
296 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
297 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
299 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
302 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
305 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
306 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
307 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
308 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
309 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
310 GtkFileFilter *filter);
311 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
314 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
317 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
320 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
322 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
324 gint *default_height);
325 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
326 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
328 static void location_popup_handler (GtkFileChooserDefault *impl,
330 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
331 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
332 static void up_folder_handler (GtkFileChooserDefault *impl);
333 static void down_folder_handler (GtkFileChooserDefault *impl);
334 static void home_folder_handler (GtkFileChooserDefault *impl);
335 static void desktop_folder_handler (GtkFileChooserDefault *impl);
336 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
337 gint bookmark_index);
338 static void show_hidden_handler (GtkFileChooserDefault *impl);
339 static void search_shortcut_handler (GtkFileChooserDefault *impl);
340 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
341 static void update_appearance (GtkFileChooserDefault *impl);
343 static void set_current_filter (GtkFileChooserDefault *impl,
344 GtkFileFilter *filter);
345 static void check_preview_change (GtkFileChooserDefault *impl);
347 static void filter_combo_changed (GtkComboBox *combo_box,
348 GtkFileChooserDefault *impl);
350 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
352 GtkFileChooserDefault *impl);
354 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
357 gboolean path_currently_selected,
359 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
361 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
363 static int shortcuts_get_index (GtkFileChooserDefault *impl,
364 ShortcutsIndex where);
365 static int shortcut_find_position (GtkFileChooserDefault *impl,
368 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
370 static gboolean list_select_func (GtkTreeSelection *selection,
373 gboolean path_currently_selected,
376 static void list_selection_changed (GtkTreeSelection *tree_selection,
377 GtkFileChooserDefault *impl);
378 static void list_row_activated (GtkTreeView *tree_view,
380 GtkTreeViewColumn *column,
381 GtkFileChooserDefault *impl);
383 static void select_func (GtkFileSystemModel *model,
388 static void path_bar_clicked (GtkPathBar *path_bar,
391 gboolean child_is_hidden,
392 GtkFileChooserDefault *impl);
394 static void add_bookmark_button_clicked_cb (GtkButton *button,
395 GtkFileChooserDefault *impl);
396 static void remove_bookmark_button_clicked_cb (GtkButton *button,
397 GtkFileChooserDefault *impl);
398 static void save_folder_combo_changed_cb (GtkComboBox *combo,
399 GtkFileChooserDefault *impl);
401 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
402 GtkCellRenderer *cell,
403 GtkTreeModel *tree_model,
406 static void list_name_data_func (GtkTreeViewColumn *tree_column,
407 GtkCellRenderer *cell,
408 GtkTreeModel *tree_model,
411 static void list_size_data_func (GtkTreeViewColumn *tree_column,
412 GtkCellRenderer *cell,
413 GtkTreeModel *tree_model,
416 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
417 GtkCellRenderer *cell,
418 GtkTreeModel *tree_model,
422 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
425 static void load_remove_timer (GtkFileChooserDefault *impl);
426 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
428 static void location_button_toggled_cb (GtkToggleButton *toggle,
429 GtkFileChooserDefault *impl);
430 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
432 static void search_stop_searching (GtkFileChooserDefault *impl,
433 gboolean remove_query);
434 static void search_clear_model_row (GtkTreeModel *model,
436 static void search_clear_model (GtkFileChooserDefault *impl,
437 gboolean remove_from_treeview);
438 static gboolean search_should_respond (GtkFileChooserDefault *impl);
439 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
440 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
441 static void search_entry_activate_cb (GtkEntry *entry,
443 static void settings_load (GtkFileChooserDefault *impl);
444 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
445 GtkTreeIter *child_iter,
448 static void recent_stop_loading (GtkFileChooserDefault *impl);
449 static void recent_clear_model (GtkFileChooserDefault *impl,
450 gboolean remove_from_treeview);
451 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
452 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
453 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
454 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
455 GtkTreeIter *child_iter,
457 static void set_file_system_backend (GtkFileChooserDefault *impl);
458 static void unset_file_system_backend (GtkFileChooserDefault *impl);
464 /* Drag and drop interface declarations */
467 GtkTreeModelFilter parent;
469 GtkFileChooserDefault *impl;
470 } ShortcutsPaneModelFilter;
473 GtkTreeModelFilterClass parent_class;
474 } ShortcutsPaneModelFilterClass;
476 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
477 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
479 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
481 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
482 _shortcuts_pane_model_filter,
483 GTK_TYPE_TREE_MODEL_FILTER,
484 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
485 shortcuts_pane_model_filter_drag_source_iface_init))
487 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
488 GtkTreeModel *child_model,
493 GtkTreeModelSort parent;
495 GtkFileChooserDefault *impl;
499 GtkTreeModelSortClass parent_class;
500 } RecentModelSortClass;
502 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
503 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
505 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
507 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
509 GTK_TYPE_TREE_MODEL_SORT,
510 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
511 recent_model_sort_drag_source_iface_init));
513 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
514 GtkTreeModel *child_model);
518 GtkTreeModelSort parent;
520 GtkFileChooserDefault *impl;
524 GtkTreeModelSortClass parent_class;
525 } SearchModelSortClass;
527 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
528 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
530 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
532 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
534 GTK_TYPE_TREE_MODEL_SORT,
535 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
536 search_model_sort_drag_source_iface_init));
538 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
539 GtkTreeModel *child_model);
543 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
544 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
545 gtk_file_chooser_default_iface_init)
546 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
547 gtk_file_chooser_embed_default_iface_init));
550 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
552 static const guint quick_bookmark_keyvals[10] = {
553 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
555 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
556 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
557 GtkBindingSet *binding_set;
560 gobject_class->finalize = gtk_file_chooser_default_finalize;
561 gobject_class->constructor = gtk_file_chooser_default_constructor;
562 gobject_class->set_property = gtk_file_chooser_default_set_property;
563 gobject_class->get_property = gtk_file_chooser_default_get_property;
564 gobject_class->dispose = gtk_file_chooser_default_dispose;
566 widget_class->show_all = gtk_file_chooser_default_show_all;
567 widget_class->map = gtk_file_chooser_default_map;
568 widget_class->unmap = gtk_file_chooser_default_unmap;
569 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
570 widget_class->style_set = gtk_file_chooser_default_style_set;
571 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
572 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
574 signals[LOCATION_POPUP] =
575 g_signal_new_class_handler (I_("location-popup"),
576 G_OBJECT_CLASS_TYPE (class),
577 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
578 G_CALLBACK (location_popup_handler),
580 _gtk_marshal_VOID__STRING,
581 G_TYPE_NONE, 1, G_TYPE_STRING);
583 signals[LOCATION_POPUP_ON_PASTE] =
584 g_signal_new_class_handler (I_("location-popup-on-paste"),
585 G_OBJECT_CLASS_TYPE (class),
586 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
587 G_CALLBACK (location_popup_on_paste_handler),
589 _gtk_marshal_VOID__VOID,
592 signals[LOCATION_TOGGLE_POPUP] =
593 g_signal_new_class_handler (I_("location-toggle-popup"),
594 G_OBJECT_CLASS_TYPE (class),
595 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
596 G_CALLBACK (location_toggle_popup_handler),
598 _gtk_marshal_VOID__VOID,
602 g_signal_new_class_handler (I_("up-folder"),
603 G_OBJECT_CLASS_TYPE (class),
604 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
605 G_CALLBACK (up_folder_handler),
607 _gtk_marshal_VOID__VOID,
610 signals[DOWN_FOLDER] =
611 g_signal_new_class_handler (I_("down-folder"),
612 G_OBJECT_CLASS_TYPE (class),
613 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
614 G_CALLBACK (down_folder_handler),
616 _gtk_marshal_VOID__VOID,
619 signals[HOME_FOLDER] =
620 g_signal_new_class_handler (I_("home-folder"),
621 G_OBJECT_CLASS_TYPE (class),
622 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
623 G_CALLBACK (home_folder_handler),
625 _gtk_marshal_VOID__VOID,
628 signals[DESKTOP_FOLDER] =
629 g_signal_new_class_handler (I_("desktop-folder"),
630 G_OBJECT_CLASS_TYPE (class),
631 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
632 G_CALLBACK (desktop_folder_handler),
634 _gtk_marshal_VOID__VOID,
637 signals[QUICK_BOOKMARK] =
638 g_signal_new_class_handler (I_("quick-bookmark"),
639 G_OBJECT_CLASS_TYPE (class),
640 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
641 G_CALLBACK (quick_bookmark_handler),
643 _gtk_marshal_VOID__INT,
644 G_TYPE_NONE, 1, G_TYPE_INT);
646 signals[SHOW_HIDDEN] =
647 g_signal_new_class_handler (I_("show-hidden"),
648 G_OBJECT_CLASS_TYPE (class),
649 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
650 G_CALLBACK (show_hidden_handler),
652 _gtk_marshal_VOID__VOID,
655 signals[SEARCH_SHORTCUT] =
656 g_signal_new_class_handler (I_("search-shortcut"),
657 G_OBJECT_CLASS_TYPE (class),
658 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
659 G_CALLBACK (search_shortcut_handler),
661 _gtk_marshal_VOID__VOID,
664 signals[RECENT_SHORTCUT] =
665 g_signal_new_class_handler (I_("recent-shortcut"),
666 G_OBJECT_CLASS_TYPE (class),
667 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
668 G_CALLBACK (recent_shortcut_handler),
670 _gtk_marshal_VOID__VOID,
673 binding_set = gtk_binding_set_by_class (class);
675 gtk_binding_entry_add_signal (binding_set,
676 GDK_l, GDK_CONTROL_MASK,
677 "location-toggle-popup",
680 gtk_binding_entry_add_signal (binding_set,
683 1, G_TYPE_STRING, "/");
684 gtk_binding_entry_add_signal (binding_set,
687 1, G_TYPE_STRING, "/");
690 gtk_binding_entry_add_signal (binding_set,
693 1, G_TYPE_STRING, "~");
696 gtk_binding_entry_add_signal (binding_set,
697 GDK_v, GDK_CONTROL_MASK,
698 "location-popup-on-paste",
700 gtk_binding_entry_add_signal (binding_set,
701 GDK_Up, GDK_MOD1_MASK,
704 gtk_binding_entry_add_signal (binding_set,
708 gtk_binding_entry_add_signal (binding_set,
709 GDK_KP_Up, GDK_MOD1_MASK,
713 gtk_binding_entry_add_signal (binding_set,
714 GDK_Down, GDK_MOD1_MASK,
717 gtk_binding_entry_add_signal (binding_set,
718 GDK_KP_Down, GDK_MOD1_MASK,
722 gtk_binding_entry_add_signal (binding_set,
723 GDK_Home, GDK_MOD1_MASK,
726 gtk_binding_entry_add_signal (binding_set,
727 GDK_KP_Home, GDK_MOD1_MASK,
730 gtk_binding_entry_add_signal (binding_set,
731 GDK_d, GDK_MOD1_MASK,
734 gtk_binding_entry_add_signal (binding_set,
735 GDK_h, GDK_CONTROL_MASK,
738 gtk_binding_entry_add_signal (binding_set,
739 GDK_s, GDK_MOD1_MASK,
742 gtk_binding_entry_add_signal (binding_set,
743 GDK_r, GDK_MOD1_MASK,
747 for (i = 0; i < 10; i++)
748 gtk_binding_entry_add_signal (binding_set,
749 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
753 _gtk_file_chooser_install_properties (gobject_class);
757 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
759 iface->select_file = gtk_file_chooser_default_select_file;
760 iface->unselect_file = gtk_file_chooser_default_unselect_file;
761 iface->select_all = gtk_file_chooser_default_select_all;
762 iface->unselect_all = gtk_file_chooser_default_unselect_all;
763 iface->get_files = gtk_file_chooser_default_get_files;
764 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
765 iface->get_file_system = gtk_file_chooser_default_get_file_system;
766 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
767 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
768 iface->set_current_name = gtk_file_chooser_default_set_current_name;
769 iface->add_filter = gtk_file_chooser_default_add_filter;
770 iface->remove_filter = gtk_file_chooser_default_remove_filter;
771 iface->list_filters = gtk_file_chooser_default_list_filters;
772 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
773 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
774 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
778 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
780 iface->get_default_size = gtk_file_chooser_default_get_default_size;
781 iface->should_respond = gtk_file_chooser_default_should_respond;
782 iface->initial_focus = gtk_file_chooser_default_initial_focus;
786 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
788 profile_start ("start", NULL);
789 #ifdef PROFILE_FILE_CHOOSER
790 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
792 impl->local_only = TRUE;
793 impl->preview_widget_active = TRUE;
794 impl->use_preview_label = TRUE;
795 impl->select_multiple = FALSE;
796 impl->show_hidden = FALSE;
797 impl->show_size_column = FALSE;
798 impl->icon_size = FALLBACK_ICON_SIZE;
799 impl->load_state = LOAD_EMPTY;
800 impl->reload_state = RELOAD_EMPTY;
801 impl->pending_select_files = NULL;
802 impl->location_mode = LOCATION_MODE_PATH_BAR;
803 impl->operation_mode = OPERATION_MODE_BROWSE;
804 impl->recent_manager = gtk_recent_manager_get_default ();
806 gtk_box_set_spacing (GTK_BOX (impl), 12);
808 set_file_system_backend (impl);
810 profile_end ("end", NULL);
813 /* Frees the data columns for the specified iter in the shortcuts model*/
815 shortcuts_free_row_data (GtkFileChooserDefault *impl,
819 ShortcutType shortcut_type;
820 GCancellable *cancellable;
822 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
823 SHORTCUTS_COL_DATA, &col_data,
824 SHORTCUTS_COL_TYPE, &shortcut_type,
825 SHORTCUTS_COL_CANCELLABLE, &cancellable,
829 g_cancellable_cancel (cancellable);
831 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
832 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
836 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
838 GtkFileSystemVolume *volume;
841 _gtk_file_system_volume_free (volume);
847 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
850 g_object_unref (file);
854 /* Frees all the data columns in the shortcuts model */
856 shortcuts_free (GtkFileChooserDefault *impl)
860 if (!impl->shortcuts_model)
863 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
866 shortcuts_free_row_data (impl, &iter);
868 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
870 g_object_unref (impl->shortcuts_model);
871 impl->shortcuts_model = NULL;
875 pending_select_files_free (GtkFileChooserDefault *impl)
877 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
878 g_slist_free (impl->pending_select_files);
879 impl->pending_select_files = NULL;
883 pending_select_files_add (GtkFileChooserDefault *impl,
886 impl->pending_select_files =
887 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
890 /* Used from gtk_tree_selection_selected_foreach() */
892 store_selection_foreach (GtkTreeModel *model,
897 GtkFileChooserDefault *impl;
898 GtkTreeIter child_iter;
901 impl = GTK_FILE_CHOOSER_DEFAULT (data);
903 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
905 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
906 pending_select_files_add (impl, file);
909 /* Stores the current selection in the list of paths to select; this is used to
910 * preserve the selection when reloading the current folder.
913 pending_select_files_store_selection (GtkFileChooserDefault *impl)
915 GtkTreeSelection *selection;
917 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
918 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
922 gtk_file_chooser_default_finalize (GObject *object)
924 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
927 unset_file_system_backend (impl);
929 if (impl->shortcuts_pane_filter_model)
930 g_object_unref (impl->shortcuts_pane_filter_model);
932 if (impl->shortcuts_combo_filter_model)
933 g_object_unref (impl->shortcuts_combo_filter_model);
935 shortcuts_free (impl);
937 g_free (impl->browse_files_last_selected_name);
939 for (l = impl->filters; l; l = l->next)
941 GtkFileFilter *filter;
943 filter = GTK_FILE_FILTER (l->data);
944 g_object_unref (filter);
946 g_slist_free (impl->filters);
948 if (impl->current_filter)
949 g_object_unref (impl->current_filter);
951 if (impl->current_volume_file)
952 g_object_unref (impl->current_volume_file);
954 if (impl->current_folder)
955 g_object_unref (impl->current_folder);
957 if (impl->preview_file)
958 g_object_unref (impl->preview_file);
960 load_remove_timer (impl);
962 /* Free all the Models we have */
963 if (impl->browse_files_model)
964 g_object_unref (impl->browse_files_model);
966 if (impl->sort_model)
967 g_object_unref (impl->sort_model);
969 search_clear_model (impl, FALSE);
970 recent_clear_model (impl, FALSE);
972 g_free (impl->preview_display_name);
974 g_free (impl->edited_new_text);
976 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
979 /* Shows an error dialog set as transient for the specified window */
981 error_message_with_parent (GtkWindow *parent,
987 dialog = gtk_message_dialog_new (parent,
988 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
993 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
996 if (parent && parent->group)
997 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
999 gtk_dialog_run (GTK_DIALOG (dialog));
1000 gtk_widget_destroy (dialog);
1003 /* Returns a toplevel GtkWindow, or NULL if none */
1005 get_toplevel (GtkWidget *widget)
1007 GtkWidget *toplevel;
1009 toplevel = gtk_widget_get_toplevel (widget);
1010 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1013 return GTK_WINDOW (toplevel);
1016 /* Shows an error dialog for the file chooser */
1018 error_message (GtkFileChooserDefault *impl,
1022 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1025 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1027 error_dialog (GtkFileChooserDefault *impl,
1038 uri = g_file_get_uri (file);
1039 text = g_strdup_printf (msg, uri);
1040 error_message (impl, text, error->message);
1043 g_error_free (error);
1047 /* Displays an error message about not being able to get information for a file.
1048 * Frees the GError as well.
1051 error_getting_info_dialog (GtkFileChooserDefault *impl,
1056 _("Could not retrieve information about the file"),
1060 /* Shows an error dialog about not being able to add a bookmark */
1062 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1067 _("Could not add a bookmark"),
1071 /* Shows an error dialog about not being able to remove a bookmark */
1073 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1078 _("Could not remove bookmark"),
1082 /* Shows an error dialog about not being able to create a folder */
1084 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1089 _("The folder could not be created"),
1093 /* Shows an error about not being able to create a folder because a file with
1094 * the same name is already there.
1097 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1102 _("The folder could not be created, as a file with the same "
1103 "name already exists. Try using a different name for the "
1104 "folder, or rename the file first."),
1108 /* Shows an error dialog about not being able to create a filename */
1110 error_building_filename_dialog (GtkFileChooserDefault *impl,
1113 error_dialog (impl, _("Invalid file name"),
1117 /* Shows an error dialog when we cannot switch to a folder */
1119 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1123 error_dialog (impl, _("The folder contents could not be displayed"),
1127 /* Changes folders, displaying an error dialog if this fails */
1129 change_folder_and_display_error (GtkFileChooserDefault *impl,
1131 gboolean clear_entry)
1136 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1138 /* We copy the path because of this case:
1140 * list_row_activated()
1141 * fetches path from model; path belongs to the model (*)
1142 * calls change_folder_and_display_error()
1143 * calls gtk_file_chooser_set_current_folder_file()
1144 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1148 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1151 error_changing_folder_dialog (impl, file, error);
1157 emit_default_size_changed (GtkFileChooserDefault *impl)
1159 if (!GTK_WIDGET_MAPPED (impl))
1162 profile_msg (" emit default-size-changed start", NULL);
1163 g_signal_emit_by_name (impl, "default-size-changed");
1164 profile_msg (" emit default-size-changed end", NULL);
1168 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1170 if (impl->use_preview_label)
1172 if (!impl->preview_label)
1174 impl->preview_label = gtk_label_new (impl->preview_display_name);
1175 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1176 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1177 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1178 gtk_widget_show (impl->preview_label);
1183 if (impl->preview_label)
1185 gtk_widget_destroy (impl->preview_label);
1186 impl->preview_label = NULL;
1190 if (impl->preview_widget_active && impl->preview_widget)
1191 gtk_widget_show (impl->preview_box);
1193 gtk_widget_hide (impl->preview_box);
1195 emit_default_size_changed (impl);
1199 set_preview_widget (GtkFileChooserDefault *impl,
1200 GtkWidget *preview_widget)
1202 if (preview_widget == impl->preview_widget)
1205 if (impl->preview_widget)
1206 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1207 impl->preview_widget);
1209 impl->preview_widget = preview_widget;
1210 if (impl->preview_widget)
1212 gtk_widget_show (impl->preview_widget);
1213 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1214 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1215 impl->preview_widget,
1216 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1219 update_preview_widget_visibility (impl);
1222 /* Renders a "Search" icon at an appropriate size for a tree view */
1224 render_search_icon (GtkFileChooserDefault *impl)
1226 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1230 render_recent_icon (GtkFileChooserDefault *impl)
1232 GtkIconTheme *theme;
1235 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1236 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1238 theme = gtk_icon_theme_get_default ();
1240 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1246 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1252 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1253 struct ReloadIconsData
1255 GtkFileChooserDefault *impl;
1256 GtkTreeRowReference *row_ref;
1260 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1262 const GError *error,
1268 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1269 struct ReloadIconsData *data = user_data;
1271 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1274 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1276 if (cancelled || error)
1279 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1281 path = gtk_tree_row_reference_get_path (data->row_ref);
1282 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1283 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1284 SHORTCUTS_COL_PIXBUF, pixbuf,
1286 gtk_tree_path_free (path);
1289 g_object_unref (pixbuf);
1292 gtk_tree_row_reference_free (data->row_ref);
1293 g_object_unref (data->impl);
1296 g_object_unref (cancellable);
1300 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1305 profile_start ("start", NULL);
1307 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1310 for (l = impl->reload_icon_cancellables; l; l = l->next)
1312 GCancellable *cancellable = G_CANCELLABLE (l->data);
1313 g_cancellable_cancel (cancellable);
1315 g_slist_free (impl->reload_icon_cancellables);
1316 impl->reload_icon_cancellables = NULL;
1321 ShortcutType shortcut_type;
1322 gboolean pixbuf_visible;
1325 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1326 SHORTCUTS_COL_DATA, &data,
1327 SHORTCUTS_COL_TYPE, &shortcut_type,
1328 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1334 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1336 GtkFileSystemVolume *volume;
1339 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1340 impl->icon_size, NULL);
1342 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1344 if (g_file_is_native (G_FILE (data)))
1347 struct ReloadIconsData *info;
1348 GtkTreePath *tree_path;
1349 GCancellable *cancellable;
1353 info = g_new0 (struct ReloadIconsData, 1);
1354 info->impl = g_object_ref (impl);
1355 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1356 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1357 gtk_tree_path_free (tree_path);
1359 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1361 shortcuts_reload_icons_get_info_cb,
1363 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1367 GtkIconTheme *icon_theme;
1369 /* Don't call get_info for remote paths to avoid latency and
1371 * If we switch to a better bookmarks file format (XBEL), we
1372 * should use mime info to get a better icon.
1374 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1375 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1376 impl->icon_size, 0, NULL);
1379 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1381 pixbuf = render_search_icon (impl);
1383 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1385 pixbuf = render_recent_icon (impl);
1388 gtk_list_store_set (impl->shortcuts_model, &iter,
1389 SHORTCUTS_COL_PIXBUF, pixbuf,
1393 g_object_unref (pixbuf);
1397 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1401 profile_end ("end", NULL);
1405 shortcuts_find_folder (GtkFileChooserDefault *impl,
1408 GtkTreeSelection *selection;
1412 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1414 g_assert (folder != NULL);
1415 pos = shortcut_find_position (impl, folder);
1418 gtk_tree_selection_unselect_all (selection);
1422 path = gtk_tree_path_new_from_indices (pos, -1);
1423 gtk_tree_selection_select_path (selection, path);
1424 gtk_tree_path_free (path);
1427 /* If a shortcut corresponds to the current folder, selects it */
1429 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1431 shortcuts_find_folder (impl, impl->current_folder);
1434 /* Removes the specified number of rows from the shortcuts list */
1436 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1442 path = gtk_tree_path_new_from_indices (start_row, -1);
1444 for (; n_rows; n_rows--)
1448 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1449 g_assert_not_reached ();
1451 shortcuts_free_row_data (impl, &iter);
1452 gtk_list_store_remove (impl->shortcuts_model, &iter);
1455 gtk_tree_path_free (path);
1459 shortcuts_update_count (GtkFileChooserDefault *impl,
1460 ShortcutsIndex type,
1465 case SHORTCUTS_HOME:
1467 impl->has_home = FALSE;
1469 impl->has_home = TRUE;
1472 case SHORTCUTS_DESKTOP:
1474 impl->has_desktop = FALSE;
1476 impl->has_desktop = TRUE;
1479 case SHORTCUTS_VOLUMES:
1480 impl->num_volumes += value;
1483 case SHORTCUTS_SHORTCUTS:
1484 impl->num_shortcuts += value;
1487 case SHORTCUTS_BOOKMARKS:
1488 impl->num_bookmarks += value;
1491 case SHORTCUTS_CURRENT_FOLDER:
1493 impl->shortcuts_current_folder_active = FALSE;
1495 impl->shortcuts_current_folder_active = TRUE;
1504 struct ShortcutsInsertRequest
1506 GtkFileChooserDefault *impl;
1510 GtkTreeRowReference *row_ref;
1511 ShortcutsIndex type;
1517 get_file_info_finished (GCancellable *cancellable,
1519 const GError *error,
1523 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1527 GCancellable *model_cancellable;
1528 struct ShortcutsInsertRequest *request = data;
1530 path = gtk_tree_row_reference_get_path (request->row_ref);
1532 /* Handle doesn't exist anymore in the model */
1535 pos = gtk_tree_path_get_indices (path)[0];
1536 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1538 gtk_tree_path_free (path);
1540 /* validate cancellable, else goto out */
1541 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1542 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1544 if (cancellable != model_cancellable)
1547 /* set the cancellable to NULL in the model (we unref later on) */
1548 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1549 SHORTCUTS_COL_CANCELLABLE, NULL,
1557 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1558 shortcuts_update_count (request->impl, request->type, -1);
1560 if (request->type == SHORTCUTS_HOME)
1564 home = g_file_new_for_path (g_get_home_dir ());
1565 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1566 g_object_unref (home);
1568 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1570 /* Remove the current folder separator */
1571 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1572 shortcuts_remove_rows (request->impl, separator_pos, 1);
1578 if (!request->label_copy)
1579 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1580 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1581 request->impl->icon_size);
1583 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1584 SHORTCUTS_COL_PIXBUF, pixbuf,
1585 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1586 SHORTCUTS_COL_NAME, request->label_copy,
1587 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1588 SHORTCUTS_COL_REMOVABLE, request->removable,
1591 if (request->impl->shortcuts_pane_filter_model)
1592 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1594 if (request->impl->shortcuts_combo_filter_model)
1595 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1597 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1598 request->impl->save_folder_combo != NULL)
1600 /* The current folder is updated via _activate_iter(), don't
1601 * have save_folder_combo_changed_cb() call _activate_iter()
1604 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1605 G_CALLBACK (save_folder_combo_changed_cb),
1608 if (request->impl->has_search)
1611 if (request->impl->has_recent)
1614 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1615 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1616 G_CALLBACK (save_folder_combo_changed_cb),
1621 g_object_unref (pixbuf);
1624 g_object_unref (request->impl);
1625 g_object_unref (request->file);
1626 gtk_tree_row_reference_free (request->row_ref);
1627 g_free (request->label_copy);
1630 g_object_unref (cancellable);
1633 /* FIXME: GtkFileSystem needs a function to split a remote path
1634 * into hostname and path components, or maybe just have a
1635 * gtk_file_system_path_get_display_name().
1637 * This function is also used in gtkfilechooserbutton.c
1640 _gtk_file_chooser_label_for_file (GFile *file)
1642 const gchar *path, *start, *end, *p;
1643 gchar *uri, *host, *label;
1645 uri = g_file_get_uri (file);
1647 start = strstr (uri, "://");
1651 path = strchr (start, '/');
1656 end = uri + strlen (uri);
1660 /* strip username */
1661 p = strchr (start, '@');
1665 p = strchr (start, ':');
1669 host = g_strndup (start, end - start);
1671 /* Translators: the first string is a path and the second string
1672 * is a hostname. Nautilus and the panel contain the same string
1675 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1681 label = g_strdup (uri);
1689 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1690 * inserts a volume. A position of -1 indicates the end of the tree.
1693 shortcuts_insert_file (GtkFileChooserDefault *impl,
1695 ShortcutType shortcut_type,
1696 GtkFileSystemVolume *volume,
1700 ShortcutsIndex type)
1703 GdkPixbuf *pixbuf = NULL;
1704 gpointer data = NULL;
1706 GtkIconTheme *icon_theme;
1708 profile_start ("start shortcut", NULL);
1710 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1713 label_copy = _gtk_file_system_volume_get_display_name (volume);
1714 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1715 impl->icon_size, NULL);
1717 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1719 if (g_file_is_native (file))
1721 struct ShortcutsInsertRequest *request;
1722 GCancellable *cancellable;
1725 request = g_new0 (struct ShortcutsInsertRequest, 1);
1726 request->impl = g_object_ref (impl);
1727 request->file = g_object_ref (file);
1728 request->name_only = TRUE;
1729 request->removable = removable;
1731 request->type = type;
1733 request->label_copy = g_strdup (label);
1736 gtk_list_store_append (impl->shortcuts_model, &iter);
1738 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1740 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1741 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1742 gtk_tree_path_free (p);
1744 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1745 "standard::is-hidden,standard::display-name,standard::icon",
1746 get_file_info_finished, request);
1748 gtk_list_store_set (impl->shortcuts_model, &iter,
1749 SHORTCUTS_COL_DATA, g_object_ref (file),
1750 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1751 SHORTCUTS_COL_CANCELLABLE, cancellable,
1754 shortcuts_update_count (impl, type, 1);
1760 /* Don't call get_info for remote paths to avoid latency and
1763 data = g_object_ref (file);
1765 label_copy = g_strdup (label);
1767 label_copy = _gtk_file_chooser_label_for_file (file);
1769 /* If we switch to a better bookmarks file format (XBEL), we
1770 * should use mime info to get a better icon.
1772 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1773 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1774 impl->icon_size, 0, NULL);
1779 g_assert_not_reached ();
1785 gtk_list_store_append (impl->shortcuts_model, &iter);
1787 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1789 shortcuts_update_count (impl, type, 1);
1791 gtk_list_store_set (impl->shortcuts_model, &iter,
1792 SHORTCUTS_COL_PIXBUF, pixbuf,
1793 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1794 SHORTCUTS_COL_NAME, label_copy,
1795 SHORTCUTS_COL_DATA, data,
1796 SHORTCUTS_COL_TYPE, shortcut_type,
1797 SHORTCUTS_COL_REMOVABLE, removable,
1798 SHORTCUTS_COL_CANCELLABLE, NULL,
1801 if (impl->shortcuts_pane_filter_model)
1802 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1804 if (impl->shortcuts_combo_filter_model)
1805 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1807 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1809 /* The current folder is updated via _activate_iter(), don't
1810 * have save_folder_combo_changed_cb() call _activate_iter()
1813 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1815 if (impl->has_search)
1818 if (impl->has_recent)
1821 g_signal_handlers_block_by_func (impl->save_folder_combo,
1822 G_CALLBACK (save_folder_combo_changed_cb),
1825 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1826 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1827 G_CALLBACK (save_folder_combo_changed_cb),
1831 g_free (label_copy);
1834 g_object_unref (pixbuf);
1836 profile_end ("end", NULL);
1840 shortcuts_append_search (GtkFileChooserDefault *impl)
1845 pixbuf = render_search_icon (impl);
1847 gtk_list_store_append (impl->shortcuts_model, &iter);
1848 gtk_list_store_set (impl->shortcuts_model, &iter,
1849 SHORTCUTS_COL_PIXBUF, pixbuf,
1850 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1851 SHORTCUTS_COL_NAME, _("Search"),
1852 SHORTCUTS_COL_DATA, NULL,
1853 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1854 SHORTCUTS_COL_REMOVABLE, FALSE,
1858 g_object_unref (pixbuf);
1860 impl->has_search = TRUE;
1864 shortcuts_append_recent (GtkFileChooserDefault *impl)
1869 pixbuf = render_recent_icon (impl);
1871 gtk_list_store_append (impl->shortcuts_model, &iter);
1872 gtk_list_store_set (impl->shortcuts_model, &iter,
1873 SHORTCUTS_COL_PIXBUF, pixbuf,
1874 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1875 SHORTCUTS_COL_NAME, _("Recently Used"),
1876 SHORTCUTS_COL_DATA, NULL,
1877 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1878 SHORTCUTS_COL_REMOVABLE, FALSE,
1882 g_object_unref (pixbuf);
1884 impl->has_recent = TRUE;
1887 /* Appends an item for the user's home directory to the shortcuts model */
1889 shortcuts_append_home (GtkFileChooserDefault *impl)
1891 const char *home_path;
1894 profile_start ("start", NULL);
1896 home_path = g_get_home_dir ();
1897 if (home_path == NULL)
1899 profile_end ("end - no home directory!?", NULL);
1903 home = g_file_new_for_path (home_path);
1904 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1905 impl->has_home = TRUE;
1907 g_object_unref (home);
1909 profile_end ("end", NULL);
1912 /* Appends the ~/Desktop directory to the shortcuts model */
1914 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1919 profile_start ("start", NULL);
1921 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1922 file = g_file_new_for_path (name);
1923 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1924 impl->has_desktop = TRUE;
1926 /* We do not actually pop up an error dialog if there is no desktop directory
1927 * because some people may really not want to have one.
1930 g_object_unref (file);
1932 profile_end ("end", NULL);
1935 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1937 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1944 profile_start ("start", NULL);
1946 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1949 for (; bookmarks; bookmarks = bookmarks->next)
1953 file = bookmarks->data;
1955 if (impl->local_only && !g_file_is_native (file))
1958 if (shortcut_find_position (impl, file) != -1)
1961 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1963 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1969 profile_end ("end", NULL);
1971 return num_inserted;
1974 /* Returns the index for the corresponding item in the shortcuts bar */
1976 shortcuts_get_index (GtkFileChooserDefault *impl,
1977 ShortcutsIndex where)
1983 if (where == SHORTCUTS_SEARCH)
1986 n += impl->has_search ? 1 : 0;
1988 if (where == SHORTCUTS_RECENT)
1991 n += impl->has_recent ? 1 : 0;
1993 if (where == SHORTCUTS_RECENT_SEPARATOR)
1996 n += impl->has_recent ? 1 : 0;
1998 if (where == SHORTCUTS_HOME)
2001 n += impl->has_home ? 1 : 0;
2003 if (where == SHORTCUTS_DESKTOP)
2006 n += impl->has_desktop ? 1 : 0;
2008 if (where == SHORTCUTS_VOLUMES)
2011 n += impl->num_volumes;
2013 if (where == SHORTCUTS_SHORTCUTS)
2016 n += impl->num_shortcuts;
2018 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2021 /* If there are no bookmarks there won't be a separator */
2022 n += (impl->num_bookmarks > 0) ? 1 : 0;
2024 if (where == SHORTCUTS_BOOKMARKS)
2027 n += impl->num_bookmarks;
2029 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2034 if (where == SHORTCUTS_CURRENT_FOLDER)
2037 g_assert_not_reached ();
2044 /* Adds all the file system volumes to the shortcuts model */
2046 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2051 gboolean old_changing_folders;
2053 profile_start ("start", NULL);
2055 old_changing_folders = impl->changing_folder;
2056 impl->changing_folder = TRUE;
2058 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2059 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2060 impl->num_volumes = 0;
2062 list = _gtk_file_system_list_volumes (impl->file_system);
2066 for (l = list; l; l = l->next)
2068 GtkFileSystemVolume *volume;
2072 if (impl->local_only)
2074 if (_gtk_file_system_volume_is_mounted (volume))
2077 gboolean base_is_native = TRUE;
2079 base_file = _gtk_file_system_volume_get_root (volume);
2080 if (base_file != NULL)
2082 base_is_native = g_file_is_native (base_file);
2083 g_object_unref (base_file);
2086 if (!base_is_native)
2091 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2095 impl->num_volumes = n;
2096 g_slist_free (list);
2098 if (impl->shortcuts_pane_filter_model)
2099 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2101 if (impl->shortcuts_combo_filter_model)
2102 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2104 impl->changing_folder = old_changing_folders;
2106 profile_end ("end", NULL);
2109 /* Inserts a separator node in the shortcuts list */
2111 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2112 ShortcutsIndex where)
2116 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2117 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2118 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2120 gtk_list_store_insert (impl->shortcuts_model, &iter,
2121 shortcuts_get_index (impl, where));
2122 gtk_list_store_set (impl->shortcuts_model, &iter,
2123 SHORTCUTS_COL_PIXBUF, NULL,
2124 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2125 SHORTCUTS_COL_NAME, NULL,
2126 SHORTCUTS_COL_DATA, NULL,
2127 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2131 /* Updates the list of bookmarks */
2133 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2136 gboolean old_changing_folders;
2138 GFile *list_selected = NULL;
2139 GFile *combo_selected = NULL;
2140 ShortcutType shortcut_type;
2143 profile_start ("start", NULL);
2145 old_changing_folders = impl->changing_folder;
2146 impl->changing_folder = TRUE;
2148 if (shortcuts_get_selected (impl, &iter))
2150 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2152 SHORTCUTS_COL_DATA, &col_data,
2153 SHORTCUTS_COL_TYPE, &shortcut_type,
2156 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2157 list_selected = g_object_ref (col_data);
2160 if (impl->save_folder_combo &&
2161 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2164 GtkTreeIter child_iter;
2166 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2169 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2171 SHORTCUTS_COL_DATA, &col_data,
2172 SHORTCUTS_COL_TYPE, &shortcut_type,
2175 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2176 combo_selected = g_object_ref (col_data);
2179 if (impl->num_bookmarks > 0)
2180 shortcuts_remove_rows (impl,
2181 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2182 impl->num_bookmarks + 1);
2184 impl->num_bookmarks = 0;
2185 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2187 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2188 shortcuts_append_bookmarks (impl, bookmarks);
2189 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2190 g_slist_free (bookmarks);
2192 if (impl->num_bookmarks == 0)
2193 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2195 if (impl->shortcuts_pane_filter_model)
2196 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2198 if (impl->shortcuts_combo_filter_model)
2199 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2203 shortcuts_find_folder (impl, list_selected);
2204 g_object_unref (list_selected);
2211 pos = shortcut_find_position (impl, combo_selected);
2214 if (impl->has_search)
2217 if (impl->has_recent)
2220 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2223 g_object_unref (combo_selected);
2226 impl->changing_folder = old_changing_folders;
2228 profile_end ("end", NULL);
2231 /* Appends a separator and a row to the shortcuts list for the current folder */
2233 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2238 g_assert (!impl->shortcuts_current_folder_active);
2242 g_assert (impl->current_folder != NULL);
2244 pos = shortcut_find_position (impl, impl->current_folder);
2247 GtkFileSystemVolume *volume;
2252 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2256 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2258 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2260 base_file = _gtk_file_system_volume_get_root (volume);
2264 if (base_file && g_file_equal (base_file, impl->current_folder))
2265 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2267 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2270 g_object_unref (base_file);
2272 else if (impl->save_folder_combo != NULL)
2274 if (impl->has_search)
2277 if (impl->has_recent)
2278 pos -= 2; /* + separator */
2280 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2284 /* Updates the current folder row in the shortcuts model */
2286 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2290 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2292 if (impl->shortcuts_current_folder_active)
2294 shortcuts_remove_rows (impl, pos, 2);
2295 impl->shortcuts_current_folder_active = FALSE;
2298 shortcuts_add_current_folder (impl);
2301 /* Filter function used for the shortcuts filter model */
2303 shortcuts_pane_filter_cb (GtkTreeModel *model,
2307 GtkFileChooserDefault *impl;
2311 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2313 path = gtk_tree_model_get_path (model, iter);
2317 pos = *gtk_tree_path_get_indices (path);
2318 gtk_tree_path_free (path);
2320 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2323 /* Creates the list model for shortcuts */
2325 shortcuts_model_create (GtkFileChooserDefault *impl)
2327 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2328 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2329 GDK_TYPE_PIXBUF, /* pixbuf */
2330 G_TYPE_STRING, /* name */
2331 G_TYPE_POINTER, /* path or volume */
2332 G_TYPE_INT, /* ShortcutType */
2333 G_TYPE_BOOLEAN, /* removable */
2334 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2335 G_TYPE_POINTER); /* GCancellable */
2337 if (search_is_possible (impl))
2339 shortcuts_append_search (impl);
2342 if (impl->recent_manager)
2344 shortcuts_append_recent (impl);
2345 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2348 if (impl->file_system)
2350 shortcuts_append_home (impl);
2351 shortcuts_append_desktop (impl);
2352 shortcuts_add_volumes (impl);
2355 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2356 GTK_TREE_MODEL (impl->shortcuts_model),
2359 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2360 shortcuts_pane_filter_cb,
2365 /* Callback used when the "New Folder" button is clicked */
2367 new_folder_button_clicked (GtkButton *button,
2368 GtkFileChooserDefault *impl)
2373 if (!impl->browse_files_model)
2374 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2376 /* Prevent button from being clicked twice */
2377 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2379 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2381 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2382 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2383 path, impl->list_name_column,
2386 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2387 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2389 impl->list_name_column,
2392 gtk_tree_path_free (path);
2395 /* Idle handler for creating a new folder after editing its name cell, or for
2396 * canceling the editing.
2399 edited_idle_cb (GtkFileChooserDefault *impl)
2401 GDK_THREADS_ENTER ();
2403 g_source_destroy (impl->edited_idle);
2404 impl->edited_idle = NULL;
2406 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2407 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2409 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2411 if (impl->edited_new_text) /* not cancelled? */
2413 GError *error = NULL;
2416 file = g_file_get_child_for_display_name (impl->current_folder,
2417 impl->edited_new_text,
2421 GError *error = NULL;
2423 if (!g_file_make_directory (file, NULL, &error))
2424 change_folder_and_display_error (impl, file, FALSE);
2426 error_creating_folder_dialog (impl, file, error);
2428 g_object_unref (file);
2431 error_creating_folder_dialog (impl, file, error);
2433 g_free (impl->edited_new_text);
2434 impl->edited_new_text = NULL;
2437 GDK_THREADS_LEAVE ();
2443 queue_edited_idle (GtkFileChooserDefault *impl,
2444 const gchar *new_text)
2446 /* We create the folder in an idle handler so that we don't modify the tree
2450 if (!impl->edited_idle)
2452 impl->edited_idle = g_idle_source_new ();
2453 g_source_set_closure (impl->edited_idle,
2454 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2456 g_source_attach (impl->edited_idle, NULL);
2459 g_free (impl->edited_new_text);
2460 impl->edited_new_text = g_strdup (new_text);
2463 /* Callback used from the text cell renderer when the new folder is named */
2465 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2467 const gchar *new_text,
2468 GtkFileChooserDefault *impl)
2470 /* work around bug #154921 */
2471 g_object_set (cell_renderer_text,
2472 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2473 queue_edited_idle (impl, new_text);
2476 /* Callback used from the text cell renderer when the new folder edition gets
2480 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2481 GtkFileChooserDefault *impl)
2483 /* work around bug #154921 */
2484 g_object_set (cell_renderer_text,
2485 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2486 queue_edited_idle (impl, NULL);
2489 /* Creates the widgets for the filter combo box */
2491 filter_create (GtkFileChooserDefault *impl)
2493 impl->filter_combo = gtk_combo_box_new_text ();
2494 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2496 g_signal_connect (impl->filter_combo, "changed",
2497 G_CALLBACK (filter_combo_changed), impl);
2499 gtk_widget_set_tooltip_text (impl->filter_combo,
2500 _("Select which types of files are shown"));
2502 return impl->filter_combo;
2506 button_new (GtkFileChooserDefault *impl,
2508 const char *stock_id,
2516 button = gtk_button_new_with_mnemonic (text);
2517 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2518 gtk_button_set_image (GTK_BUTTON (button), image);
2520 gtk_widget_set_sensitive (button, sensitive);
2521 g_signal_connect (button, "clicked", callback, impl);
2524 gtk_widget_show (button);
2529 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2531 shortcut_find_position (GtkFileChooserDefault *impl,
2536 int current_folder_separator_idx;
2538 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2541 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2544 /* FIXME: is this still needed? */
2545 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2549 for (i = 0; i < current_folder_separator_idx; i++)
2552 ShortcutType shortcut_type;
2554 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2555 SHORTCUTS_COL_DATA, &col_data,
2556 SHORTCUTS_COL_TYPE, &shortcut_type,
2561 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2563 GtkFileSystemVolume *volume;
2568 base_file = _gtk_file_system_volume_get_root (volume);
2570 exists = base_file && g_file_equal (file, base_file);
2573 g_object_unref (base_file);
2578 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2582 model_file = col_data;
2584 if (model_file && g_file_equal (model_file, file))
2589 if (i < current_folder_separator_idx - 1)
2591 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2592 g_assert_not_reached ();
2599 /* Tries to add a bookmark from a path name */
2601 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2607 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2609 if (shortcut_find_position (impl, file) != -1)
2613 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2615 error_adding_bookmark_dialog (impl, file, error);
2623 add_bookmark_foreach_cb (GtkTreeModel *model,
2628 GtkFileChooserDefault *impl;
2629 GtkFileSystemModel *fs_model;
2630 GtkTreeIter child_iter;
2633 impl = (GtkFileChooserDefault *) data;
2635 switch (impl->operation_mode)
2637 case OPERATION_MODE_BROWSE:
2638 fs_model = impl->browse_files_model;
2639 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2640 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2643 case OPERATION_MODE_SEARCH:
2644 search_get_valid_child_iter (impl, &child_iter, iter);
2645 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2646 SEARCH_MODEL_COL_FILE, &file,
2650 case OPERATION_MODE_RECENT:
2651 recent_get_valid_child_iter (impl, &child_iter, iter);
2652 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2653 RECENT_MODEL_COL_FILE, &file,
2658 shortcuts_add_bookmark_from_file (impl, file, -1);
2661 /* Adds a bookmark from the currently selected item in the file list */
2663 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2665 GtkTreeSelection *selection;
2667 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2669 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2670 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2672 gtk_tree_selection_selected_foreach (selection,
2673 add_bookmark_foreach_cb,
2677 /* Callback used when the "Add bookmark" button is clicked */
2679 add_bookmark_button_clicked_cb (GtkButton *button,
2680 GtkFileChooserDefault *impl)
2682 bookmarks_add_selected_folder (impl);
2685 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2686 * returns FALSE if no shortcut is selected.
2689 shortcuts_get_selected (GtkFileChooserDefault *impl,
2692 GtkTreeSelection *selection;
2693 GtkTreeIter parent_iter;
2695 if (!impl->browse_shortcuts_tree_view)
2698 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2700 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2703 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2709 /* Removes the selected bookmarks */
2711 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2719 if (!shortcuts_get_selected (impl, &iter))
2722 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2723 SHORTCUTS_COL_DATA, &col_data,
2724 SHORTCUTS_COL_REMOVABLE, &removable,
2730 g_assert (col_data != NULL);
2735 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2736 error_removing_bookmark_dialog (impl, file, error);
2739 /* Callback used when the "Remove bookmark" button is clicked */
2741 remove_bookmark_button_clicked_cb (GtkButton *button,
2742 GtkFileChooserDefault *impl)
2744 remove_selected_bookmarks (impl);
2747 struct selection_check_closure {
2748 GtkFileChooserDefault *impl;
2751 gboolean all_folders;
2754 /* Used from gtk_tree_selection_selected_foreach() */
2756 selection_check_foreach_cb (GtkTreeModel *model,
2761 struct selection_check_closure *closure;
2762 GtkTreeIter child_iter;
2767 closure->num_selected++;
2769 switch (closure->impl->operation_mode)
2771 case OPERATION_MODE_BROWSE:
2772 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2773 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2774 is_folder = info ? (_gtk_file_info_consider_as_directory (info)) : FALSE;
2777 case OPERATION_MODE_SEARCH:
2778 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2779 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2780 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2784 case OPERATION_MODE_RECENT:
2785 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2786 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2787 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2792 closure->all_folders = closure->all_folders && is_folder;
2793 closure->all_files = closure->all_files && !is_folder;
2796 /* Checks whether the selected items in the file list are all files or all folders */
2798 selection_check (GtkFileChooserDefault *impl,
2800 gboolean *all_files,
2801 gboolean *all_folders)
2803 struct selection_check_closure closure;
2804 GtkTreeSelection *selection;
2806 closure.impl = impl;
2807 closure.num_selected = 0;
2808 closure.all_files = TRUE;
2809 closure.all_folders = TRUE;
2811 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2812 gtk_tree_selection_selected_foreach (selection,
2813 selection_check_foreach_cb,
2816 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2819 *num_selected = closure.num_selected;
2822 *all_files = closure.all_files;
2825 *all_folders = closure.all_folders;
2828 struct get_selected_file_closure {
2829 GtkFileChooserDefault *impl;
2834 get_selected_file_foreach_cb (GtkTreeModel *model,
2839 struct get_selected_file_closure *closure;
2840 GtkTreeIter child_iter;
2844 switch (closure->impl->operation_mode)
2846 case OPERATION_MODE_BROWSE:
2847 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2848 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2851 case OPERATION_MODE_SEARCH:
2852 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2853 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2854 SEARCH_MODEL_COL_FILE, &closure->file,
2858 case OPERATION_MODE_RECENT:
2859 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2860 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2861 RECENT_MODEL_COL_FILE, &closure->file,
2867 /* Returns a selected path from the file list */
2869 get_selected_file (GtkFileChooserDefault *impl)
2871 struct get_selected_file_closure closure;
2872 GtkTreeSelection *selection;
2874 closure.impl = impl;
2875 closure.file = NULL;
2877 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2878 gtk_tree_selection_selected_foreach (selection,
2879 get_selected_file_foreach_cb,
2882 return closure.file;
2886 GtkFileChooserDefault *impl;
2888 } UpdateTooltipData;
2891 update_tooltip (GtkTreeModel *model,
2896 UpdateTooltipData *udata = data;
2897 GtkTreeIter child_iter;
2900 if (udata->tip == NULL)
2902 const gchar *display_name;
2904 switch (udata->impl->operation_mode)
2906 case OPERATION_MODE_BROWSE:
2907 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2910 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2911 display_name = g_file_info_get_display_name (info);
2914 case OPERATION_MODE_SEARCH:
2915 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2916 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2917 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2921 case OPERATION_MODE_RECENT:
2922 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2923 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2924 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2929 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2935 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2936 * if there are no selected items *and* the current folder is not in the
2937 * bookmarks list. De-sensitize the button otherwise.
2940 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2943 gboolean all_folders;
2947 selection_check (impl, &num_selected, NULL, &all_folders);
2949 if (num_selected == 0)
2950 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2951 else if (num_selected == 1)
2955 file = get_selected_file (impl);
2956 active = all_folders && (shortcut_find_position (impl, file) == -1);
2959 active = all_folders;
2961 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2963 if (impl->browse_files_popup_menu_add_shortcut_item)
2964 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2965 (num_selected == 0) ? FALSE : active);
2969 if (num_selected == 0)
2970 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2971 else if (num_selected > 1)
2972 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2975 GtkTreeSelection *selection;
2976 UpdateTooltipData data;
2978 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2981 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2985 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2990 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2991 * bookmark row is selected in the shortcuts tree.
2994 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2997 gboolean removable = FALSE;
3000 if (shortcuts_get_selected (impl, &iter))
3001 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3002 SHORTCUTS_COL_REMOVABLE, &removable,
3003 SHORTCUTS_COL_NAME, &name,
3006 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3012 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3013 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3021 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3024 gboolean removable = FALSE;
3026 if (impl->browse_shortcuts_popup_menu == NULL)
3029 if (shortcuts_get_selected (impl, &iter))
3030 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3031 SHORTCUTS_COL_REMOVABLE, &removable,
3034 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3035 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3038 /* GtkWidget::drag-begin handler for the shortcuts list. */
3040 shortcuts_drag_begin_cb (GtkWidget *widget,
3041 GdkDragContext *context,
3042 GtkFileChooserDefault *impl)
3045 impl->shortcuts_drag_context = g_object_ref (context);
3050 /* Removes the idle handler for outside drags */
3052 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3054 if (!impl->shortcuts_drag_outside_idle)
3057 g_source_destroy (impl->shortcuts_drag_outside_idle);
3058 impl->shortcuts_drag_outside_idle = NULL;
3062 /* GtkWidget::drag-end handler for the shortcuts list. */
3064 shortcuts_drag_end_cb (GtkWidget *widget,
3065 GdkDragContext *context,
3066 GtkFileChooserDefault *impl)
3069 g_object_unref (impl->shortcuts_drag_context);
3071 shortcuts_cancel_drag_outside_idle (impl);
3073 if (!impl->shortcuts_drag_outside)
3076 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3078 impl->shortcuts_drag_outside = FALSE;
3082 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3084 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3085 GdkDragContext *context,
3086 GtkFileChooserDefault *impl)
3088 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3092 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3096 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3099 GtkTreeView *tree_view;
3102 GdkPixmap *row_pixmap;
3107 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3109 /* Find the selected path and get its drag pixmap */
3111 if (!shortcuts_get_selected (impl, &iter))
3112 g_assert_not_reached ();
3114 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3116 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3117 gtk_tree_path_free (path);
3126 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3132 GdkPixmap *composite;
3133 int row_pixmap_width, row_pixmap_height;
3134 int pixbuf_width, pixbuf_height;
3135 int composite_width, composite_height;
3136 int pixbuf_x, pixbuf_y;
3137 GdkGC *gc, *mask_gc;
3139 GdkBitmap *pixbuf_mask;
3141 /* Create pixmap and mask for composite image */
3143 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3144 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3145 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3147 composite_width = MAX (row_pixmap_width, pixbuf_width);
3148 composite_height = MAX (row_pixmap_height, pixbuf_height);
3150 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3152 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3155 pixbuf_x = composite_width - pixbuf_width;
3157 pixbuf_y = (composite_height - pixbuf_height) / 2;
3159 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3160 gc = gdk_gc_new (composite);
3162 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3163 mask_gc = gdk_gc_new (mask);
3165 gdk_gc_set_foreground (mask_gc, &color);
3166 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3169 color.green = 0xffff;
3170 color.blue = 0xffff;
3171 gdk_gc_set_rgb_fg_color (gc, &color);
3172 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3174 /* Composite the row pixmap and the pixbuf */
3176 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3178 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3179 NULL, &pixbuf_mask, 128);
3180 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3183 pixbuf_width, pixbuf_height);
3184 g_object_unref (pixbuf_mask);
3186 gdk_draw_drawable (composite, gc, row_pixmap,
3189 row_pixmap_width, row_pixmap_height);
3191 gdk_gc_set_foreground (mask_gc, &color);
3192 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3194 gdk_draw_pixbuf (composite, gc, pixbuf,
3197 pixbuf_width, pixbuf_height,
3201 g_object_unref (pixbuf);
3202 g_object_unref (row_pixmap);
3204 row_pixmap = composite;
3208 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3210 gtk_tree_view_get_path_at_pos (tree_view,
3211 tree_view->priv->press_start_x,
3212 tree_view->priv->press_start_y,
3218 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3219 gdk_drawable_get_colormap (row_pixmap),
3222 tree_view->priv->press_start_x + 1,
3223 row_pixmap_y + cell_y + 1);
3225 g_object_unref (row_pixmap);
3227 g_object_unref (mask);
3230 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3231 * handler so that we can tell apart the drag_leave event that comes right
3232 * before a drag_drop, from a normal drag_leave. We don't want to set the
3233 * cursor nor the flag in the latter case.
3236 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3238 GDK_THREADS_ENTER ();
3240 shortcuts_drag_set_delete_cursor (impl, TRUE);
3241 impl->shortcuts_drag_outside = TRUE;
3243 shortcuts_cancel_drag_outside_idle (impl);
3245 GDK_THREADS_LEAVE ();
3251 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3255 shortcuts_drag_leave_cb (GtkWidget *widget,
3256 GdkDragContext *context,
3258 GtkFileChooserDefault *impl)
3261 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3263 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3264 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3265 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3267 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3271 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3273 GTK_TREE_VIEW_DROP_BEFORE);
3275 g_signal_stop_emission_by_name (widget, "drag-leave");
3278 /* Computes the appropriate row and position for dropping */
3280 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3284 GtkTreeViewDropPosition *pos)
3286 GtkTreeView *tree_view;
3287 GtkTreeViewColumn *column;
3291 int bookmarks_index;
3293 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3295 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3297 if (!gtk_tree_view_get_path_at_pos (tree_view,
3299 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3305 row = bookmarks_index + impl->num_bookmarks - 1;
3306 *path = gtk_tree_path_new_from_indices (row, -1);
3307 *pos = GTK_TREE_VIEW_DROP_AFTER;
3311 row = *gtk_tree_path_get_indices (*path);
3312 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3313 gtk_tree_path_free (*path);
3315 if (row < bookmarks_index)
3317 row = bookmarks_index;
3318 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3320 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3322 row = bookmarks_index + impl->num_bookmarks - 1;
3323 *pos = GTK_TREE_VIEW_DROP_AFTER;
3327 if (cell_y < cell.height / 2)
3328 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3330 *pos = GTK_TREE_VIEW_DROP_AFTER;
3333 *path = gtk_tree_path_new_from_indices (row, -1);
3336 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3337 * implement the destination side of DnD by hand, due to limitations in
3338 * GtkTreeView's DnD API.
3341 shortcuts_drag_motion_cb (GtkWidget *widget,
3342 GdkDragContext *context,
3346 GtkFileChooserDefault *impl)
3349 GtkTreeViewDropPosition pos;
3350 GdkDragAction action;
3353 if (gtk_drag_get_source_widget (context) == widget)
3355 shortcuts_cancel_drag_outside_idle (impl);
3357 if (impl->shortcuts_drag_outside)
3359 shortcuts_drag_set_delete_cursor (impl, FALSE);
3360 impl->shortcuts_drag_outside = FALSE;
3365 if (context->suggested_action == GDK_ACTION_COPY ||
3366 (context->actions & GDK_ACTION_COPY) != 0)
3367 action = GDK_ACTION_COPY;
3368 else if (context->suggested_action == GDK_ACTION_MOVE ||
3369 (context->actions & GDK_ACTION_MOVE) != 0)
3370 action = GDK_ACTION_MOVE;
3377 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3378 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3379 gtk_tree_path_free (path);
3383 g_signal_stop_emission_by_name (widget, "drag-motion");
3387 gdk_drag_status (context, action, time_);
3394 /* GtkWidget::drag-drop handler for the shortcuts list. */
3396 shortcuts_drag_drop_cb (GtkWidget *widget,
3397 GdkDragContext *context,
3401 GtkFileChooserDefault *impl)
3404 shortcuts_cancel_drag_outside_idle (impl);
3407 g_signal_stop_emission_by_name (widget, "drag-drop");
3411 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3413 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3414 GtkSelectionData *selection_data,
3420 uris = gtk_selection_data_get_uris (selection_data);
3424 for (i = 0; uris[i]; i++)
3430 file = g_file_new_for_uri (uri);
3432 if (shortcuts_add_bookmark_from_file (impl, file, position))
3435 g_object_unref (file);
3441 /* Reorders the selected bookmark to the specified position */
3443 shortcuts_reorder (GtkFileChooserDefault *impl,
3448 ShortcutType shortcut_type;
3451 int bookmarks_index;
3456 /* Get the selected path */
3458 if (!shortcuts_get_selected (impl, &iter))
3459 g_assert_not_reached ();
3461 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3462 old_position = *gtk_tree_path_get_indices (path);
3463 gtk_tree_path_free (path);
3465 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3466 old_position -= bookmarks_index;
3467 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3469 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3470 SHORTCUTS_COL_NAME, &name,
3471 SHORTCUTS_COL_DATA, &col_data,
3472 SHORTCUTS_COL_TYPE, &shortcut_type,
3474 g_assert (col_data != NULL);
3475 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3478 g_object_ref (file); /* removal below will free file, so we need a new ref */
3480 /* Remove the path from the old position and insert it in the new one */
3482 if (new_position > old_position)
3485 if (old_position == new_position)
3489 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3491 shortcuts_add_bookmark_from_file (impl, file, new_position);
3492 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3495 error_adding_bookmark_dialog (impl, file, error);
3499 g_object_unref (file);
3502 /* Callback used when we get the drag data for the bookmarks list. We add the
3503 * received URIs as bookmarks if they are folders.
3506 shortcuts_drag_data_received_cb (GtkWidget *widget,
3507 GdkDragContext *context,
3510 GtkSelectionData *selection_data,
3515 GtkFileChooserDefault *impl;
3516 GtkTreePath *tree_path;
3517 GtkTreeViewDropPosition tree_pos;
3519 int bookmarks_index;
3521 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3523 /* Compute position */
3525 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3527 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3528 position = *gtk_tree_path_get_indices (tree_path);
3529 gtk_tree_path_free (tree_path);
3531 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3534 g_assert (position >= bookmarks_index);
3535 position -= bookmarks_index;
3537 if (gtk_targets_include_uri (&selection_data->target, 1))
3538 shortcuts_drop_uris (impl, selection_data, position);
3539 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3540 shortcuts_reorder (impl, position);
3542 g_signal_stop_emission_by_name (widget, "drag-data-received");
3545 /* Callback used to display a tooltip in the shortcuts tree */
3547 shortcuts_query_tooltip_cb (GtkWidget *widget,
3550 gboolean keyboard_mode,
3551 GtkTooltip *tooltip,
3552 GtkFileChooserDefault *impl)
3554 GtkTreeModel *model;
3557 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3565 ShortcutType shortcut_type;
3567 gtk_tree_model_get (model, &iter,
3568 SHORTCUTS_COL_DATA, &col_data,
3569 SHORTCUTS_COL_TYPE, &shortcut_type,
3572 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3574 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3578 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3583 file = G_FILE (col_data);
3584 parse_name = g_file_get_parse_name (file);
3586 gtk_tooltip_set_text (tooltip, parse_name);
3588 g_free (parse_name);
3592 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3596 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3606 /* Callback used when the selection in the shortcuts tree changes */
3608 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3609 GtkFileChooserDefault *impl)
3612 GtkTreeIter child_iter;
3614 bookmarks_check_remove_sensitivity (impl);
3615 shortcuts_check_popup_sensitivity (impl);
3617 if (impl->changing_folder)
3620 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3622 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3625 shortcuts_activate_iter (impl, &child_iter);
3630 shortcuts_row_separator_func (GtkTreeModel *model,
3634 ShortcutType shortcut_type;
3636 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3638 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3641 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3642 * keypresses before the TreeView gets them.
3645 tree_view_keybinding_cb (GtkWidget *tree_view,
3647 GtkFileChooserDefault *impl)
3649 if ((event->keyval == GDK_slash
3650 || event->keyval == GDK_KP_Divide
3652 || event->keyval == GDK_asciitilde
3654 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3656 location_popup_handler (impl, event->string);
3663 /* Callback used when the file list's popup menu is detached */
3665 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3668 GtkFileChooserDefault *impl;
3670 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3671 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3673 impl->browse_shortcuts_popup_menu = NULL;
3674 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3675 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3679 remove_shortcut_cb (GtkMenuItem *item,
3680 GtkFileChooserDefault *impl)
3682 remove_selected_bookmarks (impl);
3685 /* Rename the selected bookmark */
3687 rename_selected_bookmark (GtkFileChooserDefault *impl)
3691 GtkTreeViewColumn *column;
3692 GtkCellRenderer *cell;
3695 if (shortcuts_get_selected (impl, &iter))
3697 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3698 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3699 renderers = gtk_tree_view_column_get_cell_renderers (column);
3700 cell = g_list_nth_data (renderers, 1);
3701 g_list_free (renderers);
3702 g_object_set (cell, "editable", TRUE, NULL);
3703 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3704 path, column, cell, TRUE);
3705 gtk_tree_path_free (path);
3710 rename_shortcut_cb (GtkMenuItem *item,
3711 GtkFileChooserDefault *impl)
3713 rename_selected_bookmark (impl);
3716 /* Constructs the popup menu for the file list if needed */
3718 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3722 if (impl->browse_shortcuts_popup_menu)
3725 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3726 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3727 impl->browse_shortcuts_tree_view,
3728 shortcuts_popup_menu_detach_cb);
3730 item = gtk_image_menu_item_new_with_label (_("Remove"));
3731 impl->browse_shortcuts_popup_menu_remove_item = item;
3732 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3733 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3734 g_signal_connect (item, "activate",
3735 G_CALLBACK (remove_shortcut_cb), impl);
3736 gtk_widget_show (item);
3737 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3739 item = gtk_menu_item_new_with_label (_("Rename..."));
3740 impl->browse_shortcuts_popup_menu_rename_item = item;
3741 g_signal_connect (item, "activate",
3742 G_CALLBACK (rename_shortcut_cb), impl);
3743 gtk_widget_show (item);
3744 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3748 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3750 shortcuts_build_popup_menu (impl);
3751 shortcuts_check_popup_sensitivity (impl);
3755 popup_position_func (GtkMenu *menu,
3759 gpointer user_data);
3762 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3763 GdkEventButton *event)
3765 shortcuts_update_popup_menu (impl);
3767 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3768 NULL, NULL, NULL, NULL,
3769 event->button, event->time);
3772 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3774 popup_position_func, impl->browse_shortcuts_tree_view,
3775 0, GDK_CURRENT_TIME);
3776 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3781 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3783 shortcuts_popup_menu_cb (GtkWidget *widget,
3784 GtkFileChooserDefault *impl)
3786 shortcuts_popup_menu (impl, NULL);
3790 /* Callback used when a button is pressed on the shortcuts list.
3791 * We trap button 3 to bring up a popup menu.
3794 shortcuts_button_press_event_cb (GtkWidget *widget,
3795 GdkEventButton *event,
3796 GtkFileChooserDefault *impl)
3798 static gboolean in_press = FALSE;
3804 if (event->button != 3)
3808 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3814 shortcuts_popup_menu (impl, event);
3819 shortcuts_edited (GtkCellRenderer *cell,
3822 GtkFileChooserDefault *impl)
3828 g_object_set (cell, "editable", FALSE, NULL);
3830 path = gtk_tree_path_new_from_string (path_string);
3831 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3832 g_assert_not_reached ();
3834 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3835 SHORTCUTS_COL_DATA, &shortcut,
3837 gtk_tree_path_free (path);
3839 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3843 shortcuts_editing_canceled (GtkCellRenderer *cell,
3844 GtkFileChooserDefault *impl)
3846 g_object_set (cell, "editable", FALSE, NULL);
3849 /* Creates the widgets for the shortcuts and bookmarks tree */
3851 shortcuts_list_create (GtkFileChooserDefault *impl)
3854 GtkTreeSelection *selection;
3855 GtkTreeViewColumn *column;
3856 GtkCellRenderer *renderer;
3858 /* Target types for dragging a row to/from the shortcuts list */
3859 const GtkTargetEntry tree_model_row_targets[] = {
3860 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3863 /* Scrolled window */
3865 swin = gtk_scrolled_window_new (NULL, NULL);
3866 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3867 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3868 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3870 gtk_widget_show (swin);
3874 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3875 #ifdef PROFILE_FILE_CHOOSER
3876 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3878 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3879 G_CALLBACK (tree_view_keybinding_cb), impl);
3880 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3881 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3882 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3883 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3884 /* Accessible object name for the file chooser's shortcuts pane */
3885 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3887 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3889 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3891 tree_model_row_targets,
3892 G_N_ELEMENTS (tree_model_row_targets),
3895 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3896 GTK_DEST_DEFAULT_ALL,
3897 tree_model_row_targets,
3898 G_N_ELEMENTS (tree_model_row_targets),
3899 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3900 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3902 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3903 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3904 gtk_tree_selection_set_select_function (selection,
3905 shortcuts_select_func,
3908 g_signal_connect (selection, "changed",
3909 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3911 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3912 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3914 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3915 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3916 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3917 G_CALLBACK (shortcuts_drag_end_cb), impl);
3918 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3919 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3921 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3922 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3923 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3924 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3925 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3926 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3927 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3928 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3930 /* Support tooltips */
3931 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3932 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3933 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3935 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3936 gtk_widget_show (impl->browse_shortcuts_tree_view);
3940 column = gtk_tree_view_column_new ();
3941 /* Column header for the file chooser's shortcuts pane */
3942 gtk_tree_view_column_set_title (column, _("_Places"));
3944 renderer = gtk_cell_renderer_pixbuf_new ();
3945 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3946 gtk_tree_view_column_set_attributes (column, renderer,
3947 "pixbuf", SHORTCUTS_COL_PIXBUF,
3948 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3951 renderer = gtk_cell_renderer_text_new ();
3952 g_signal_connect (renderer, "edited",
3953 G_CALLBACK (shortcuts_edited), impl);
3954 g_signal_connect (renderer, "editing-canceled",
3955 G_CALLBACK (shortcuts_editing_canceled), impl);
3956 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3957 gtk_tree_view_column_set_attributes (column, renderer,
3958 "text", SHORTCUTS_COL_NAME,
3961 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3962 shortcuts_row_separator_func,
3965 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3970 /* Creates the widgets for the shortcuts/bookmarks pane */
3972 shortcuts_pane_create (GtkFileChooserDefault *impl,
3973 GtkSizeGroup *size_group)
3979 vbox = gtk_vbox_new (FALSE, 6);
3980 gtk_widget_show (vbox);
3982 /* Shortcuts tree */
3984 widget = shortcuts_list_create (impl);
3985 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3987 /* Box for buttons */
3989 hbox = gtk_hbox_new (TRUE, 6);
3990 gtk_size_group_add_widget (size_group, hbox);
3991 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3992 gtk_widget_show (hbox);
3994 /* Add bookmark button */
3996 impl->browse_shortcuts_add_button = button_new (impl,
4001 G_CALLBACK (add_bookmark_button_clicked_cb));
4002 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4003 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4004 _("Add the selected folder to the Bookmarks"));
4006 /* Remove bookmark button */
4008 impl->browse_shortcuts_remove_button = button_new (impl,
4013 G_CALLBACK (remove_bookmark_button_clicked_cb));
4014 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4015 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4016 _("Remove the selected bookmark"));
4021 /* Handles key press events on the file list, so that we can trap Enter to
4022 * activate the default button on our own. Also, checks to see if '/' has been
4023 * pressed. See comment by tree_view_keybinding_cb() for more details.
4026 trap_activate_cb (GtkWidget *widget,
4030 GtkFileChooserDefault *impl;
4033 impl = (GtkFileChooserDefault *) data;
4035 modifiers = gtk_accelerator_get_default_mod_mask ();
4037 if ((event->keyval == GDK_slash
4038 || event->keyval == GDK_KP_Divide
4040 || event->keyval == GDK_asciitilde
4042 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4044 location_popup_handler (impl, event->string);
4048 if ((event->keyval == GDK_Return
4049 || event->keyval == GDK_ISO_Enter
4050 || event->keyval == GDK_KP_Enter
4051 || event->keyval == GDK_space
4052 || event->keyval == GDK_KP_Space)
4053 && ((event->state & modifiers) == 0)
4054 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4055 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4059 window = get_toplevel (widget);
4061 && widget != window->default_widget
4062 && !(widget == window->focus_widget &&
4063 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4065 gtk_window_activate_default (window);
4073 /* Callback used when the file list's popup menu is detached */
4075 popup_menu_detach_cb (GtkWidget *attach_widget,
4078 GtkFileChooserDefault *impl;
4080 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4081 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4083 impl->browse_files_popup_menu = NULL;
4084 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4085 impl->browse_files_popup_menu_hidden_files_item = NULL;
4088 /* Callback used when the "Add to Bookmarks" menu item is activated */
4090 add_to_shortcuts_cb (GtkMenuItem *item,
4091 GtkFileChooserDefault *impl)
4093 bookmarks_add_selected_folder (impl);
4096 /* Callback used when the "Show Hidden Files" menu item is toggled */
4098 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4099 GtkFileChooserDefault *impl)
4102 "show-hidden", gtk_check_menu_item_get_active (item),
4106 /* Callback used when the "Show Size Column" menu item is toggled */
4108 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4109 GtkFileChooserDefault *impl)
4111 impl->show_size_column = gtk_check_menu_item_get_active (item);
4113 if (impl->list_size_column)
4114 gtk_tree_view_column_set_visible (impl->list_size_column,
4115 impl->show_size_column);
4118 /* Shows an error dialog about not being able to select a dragged file */
4120 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4125 _("Could not select file"),
4130 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4135 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4137 for (i = 1; uris[i]; i++)
4140 GError *error = NULL;
4143 file = g_file_new_for_uri (uri);
4145 gtk_file_chooser_default_select_file (chooser, file, &error);
4147 error_selecting_dragged_file_dialog (impl, file, error);
4149 g_object_unref (file);
4153 struct FileListDragData
4155 GtkFileChooserDefault *impl;
4161 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4163 const GError *error,
4166 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4167 struct FileListDragData *data = user_data;
4168 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4170 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4173 data->impl->file_list_drag_data_received_cancellable = NULL;
4175 if (cancelled || error)
4178 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4179 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4180 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4181 change_folder_and_display_error (data->impl, data->file, FALSE);
4184 GError *error = NULL;
4186 gtk_file_chooser_default_unselect_all (chooser);
4187 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4189 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4191 browse_files_center_selected_row (data->impl);
4194 if (data->impl->select_multiple)
4195 file_list_drag_data_select_uris (data->impl, data->uris);
4198 g_object_unref (data->impl);
4199 g_strfreev (data->uris);
4200 g_object_unref (data->file);
4203 g_object_unref (cancellable);
4207 file_list_drag_data_received_cb (GtkWidget *widget,
4208 GdkDragContext *context,
4211 GtkSelectionData *selection_data,
4216 GtkFileChooserDefault *impl;
4217 GtkFileChooser *chooser;
4222 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4223 chooser = GTK_FILE_CHOOSER (data);
4225 /* Allow only drags from other widgets; see bug #533891. */
4226 if (gtk_drag_get_source_widget (context) == widget)
4228 g_signal_stop_emission_by_name (widget, "drag-data-received");
4232 /* Parse the text/uri-list string, navigate to the first one */
4233 uris = gtk_selection_data_get_uris (selection_data);
4234 if (uris && uris[0])
4236 struct FileListDragData *data;
4239 file = g_file_new_for_uri (uri);
4241 data = g_new0 (struct FileListDragData, 1);
4242 data->impl = g_object_ref (impl);
4246 if (impl->file_list_drag_data_received_cancellable)
4247 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4249 impl->file_list_drag_data_received_cancellable =
4250 _gtk_file_system_get_info (impl->file_system, file,
4252 file_list_drag_data_received_get_info_cb,
4256 g_signal_stop_emission_by_name (widget, "drag-data-received");
4259 /* Don't do anything with the drag_drop signal */
4261 file_list_drag_drop_cb (GtkWidget *widget,
4262 GdkDragContext *context,
4266 GtkFileChooserDefault *impl)
4268 g_signal_stop_emission_by_name (widget, "drag-drop");
4272 /* Disable the normal tree drag motion handler, it makes it look like you're
4273 dropping the dragged item onto a tree item */
4275 file_list_drag_motion_cb (GtkWidget *widget,
4276 GdkDragContext *context,
4280 GtkFileChooserDefault *impl)
4282 g_signal_stop_emission_by_name (widget, "drag-motion");
4286 /* Constructs the popup menu for the file list if needed */
4288 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4292 if (impl->browse_files_popup_menu)
4295 impl->browse_files_popup_menu = gtk_menu_new ();
4296 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4297 impl->browse_files_tree_view,
4298 popup_menu_detach_cb);
4300 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4301 impl->browse_files_popup_menu_add_shortcut_item = item;
4302 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4303 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4304 gtk_widget_set_sensitive (item, FALSE);
4305 g_signal_connect (item, "activate",
4306 G_CALLBACK (add_to_shortcuts_cb), impl);
4307 gtk_widget_show (item);
4308 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4310 item = gtk_separator_menu_item_new ();
4311 gtk_widget_show (item);
4312 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4314 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4315 impl->browse_files_popup_menu_hidden_files_item = item;
4316 g_signal_connect (item, "toggled",
4317 G_CALLBACK (show_hidden_toggled_cb), impl);
4318 gtk_widget_show (item);
4319 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4321 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4322 impl->browse_files_popup_menu_size_column_item = item;
4323 g_signal_connect (item, "toggled",
4324 G_CALLBACK (show_size_column_toggled_cb), impl);
4325 gtk_widget_show (item);
4326 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4329 /* Updates the popup menu for the file list, creating it if necessary */
4331 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4333 file_list_build_popup_menu (impl);
4335 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4337 /* The sensitivity of the Add to Bookmarks item is set in
4338 * bookmarks_check_add_sensitivity()
4341 /* 'Show Hidden Files' */
4342 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4343 G_CALLBACK (show_hidden_toggled_cb), impl);
4344 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4346 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4347 G_CALLBACK (show_hidden_toggled_cb), impl);
4349 /* 'Show Size Column' */
4350 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4351 G_CALLBACK (show_size_column_toggled_cb), impl);
4352 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4353 impl->show_size_column);
4354 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4355 G_CALLBACK (show_size_column_toggled_cb), impl);
4359 popup_position_func (GtkMenu *menu,
4365 GtkWidget *widget = GTK_WIDGET (user_data);
4366 GdkScreen *screen = gtk_widget_get_screen (widget);
4369 GdkRectangle monitor;
4371 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4373 gdk_window_get_origin (widget->window, x, y);
4375 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4377 *x += (widget->allocation.width - req.width) / 2;
4378 *y += (widget->allocation.height - req.height) / 2;
4380 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4381 gtk_menu_set_monitor (menu, monitor_num);
4382 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4384 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4385 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4391 file_list_popup_menu (GtkFileChooserDefault *impl,
4392 GdkEventButton *event)
4394 file_list_update_popup_menu (impl);
4396 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4397 NULL, NULL, NULL, NULL,
4398 event->button, event->time);
4401 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4403 popup_position_func, impl->browse_files_tree_view,
4404 0, GDK_CURRENT_TIME);
4405 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4411 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4413 list_popup_menu_cb (GtkWidget *widget,
4414 GtkFileChooserDefault *impl)
4416 file_list_popup_menu (impl, NULL);
4420 /* Callback used when a button is pressed on the file list. We trap button 3 to
4421 * bring up a popup menu.
4424 list_button_press_event_cb (GtkWidget *widget,
4425 GdkEventButton *event,
4426 GtkFileChooserDefault *impl)
4428 static gboolean in_press = FALSE;
4434 if (event->button != 3)
4438 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4441 file_list_popup_menu (impl, event);
4445 /* Sets the sort column IDs for the file list based on the operation mode */
4447 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4449 int name_id, mtime_id, size_id;
4451 name_id = mtime_id = size_id = 0;
4453 switch (impl->operation_mode)
4455 case OPERATION_MODE_BROWSE:
4456 name_id = FILE_LIST_COL_NAME;
4457 mtime_id = FILE_LIST_COL_MTIME;
4458 size_id = FILE_LIST_COL_SIZE;
4460 case OPERATION_MODE_SEARCH:
4461 name_id = SEARCH_MODEL_COL_FILE;
4462 mtime_id = SEARCH_MODEL_COL_MTIME;
4463 size_id = SEARCH_MODEL_COL_SIZE;
4465 case OPERATION_MODE_RECENT:
4466 name_id = RECENT_MODEL_COL_FILE;
4467 mtime_id = RECENT_MODEL_COL_INFO;
4471 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4472 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4473 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, size_id);
4477 file_list_query_tooltip_cb (GtkWidget *widget,
4480 gboolean keyboard_tip,
4481 GtkTooltip *tooltip,
4484 GtkFileChooserDefault *impl = user_data;
4485 GtkTreeIter iter, child_iter;
4486 GtkTreePath *path = NULL;
4490 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4494 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4502 switch (impl->operation_mode)
4504 case OPERATION_MODE_SEARCH:
4505 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4507 gtk_tree_path_free (path);
4511 search_get_valid_child_iter (impl, &child_iter, &iter);
4512 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4513 SEARCH_MODEL_COL_FILE, &file,
4517 case OPERATION_MODE_RECENT:
4518 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4520 gtk_tree_path_free (path);
4524 recent_get_valid_child_iter (impl, &child_iter, &iter);
4525 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4526 RECENT_MODEL_COL_FILE, &file,
4530 case OPERATION_MODE_BROWSE:
4531 g_assert_not_reached ();
4537 gtk_tree_path_free (path);
4541 filename = g_file_get_path (file);
4542 gtk_tooltip_set_text (tooltip, filename);
4543 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4548 gtk_tree_path_free (path);
4553 /* Creates the widgets for the file list */
4555 create_file_list (GtkFileChooserDefault *impl)
4558 GtkTreeSelection *selection;
4559 GtkTreeViewColumn *column;
4560 GtkCellRenderer *renderer;
4562 /* Scrolled window */
4563 swin = gtk_scrolled_window_new (NULL, NULL);
4564 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4565 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4566 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4569 /* Tree/list view */
4571 impl->browse_files_tree_view = gtk_tree_view_new ();
4572 #ifdef PROFILE_FILE_CHOOSER
4573 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4575 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4576 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4578 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4579 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4581 gtk_drag_dest_set (impl->browse_files_tree_view,
4582 GTK_DEST_DEFAULT_ALL,
4584 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4585 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4587 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4588 G_CALLBACK (list_row_activated), impl);
4589 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4590 G_CALLBACK (trap_activate_cb), impl);
4591 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4592 G_CALLBACK (list_popup_menu_cb), impl);
4593 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4594 G_CALLBACK (list_button_press_event_cb), impl);
4596 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4597 G_CALLBACK (file_list_drag_data_received_cb), impl);
4598 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4599 G_CALLBACK (file_list_drag_drop_cb), impl);
4600 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4601 G_CALLBACK (file_list_drag_motion_cb), impl);
4603 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4604 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4605 G_CALLBACK (file_list_query_tooltip_cb), impl);
4607 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4608 gtk_tree_selection_set_select_function (selection,
4611 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4614 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4615 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4617 g_signal_connect (selection, "changed",
4618 G_CALLBACK (list_selection_changed), impl);
4620 /* Filename column */
4622 impl->list_name_column = gtk_tree_view_column_new ();
4623 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4624 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4625 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4626 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4628 renderer = gtk_cell_renderer_pixbuf_new ();
4629 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4630 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4631 list_icon_data_func, impl, NULL);
4633 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4634 g_object_set (impl->list_name_renderer,
4635 "ellipsize", PANGO_ELLIPSIZE_END,
4637 g_signal_connect (impl->list_name_renderer, "edited",
4638 G_CALLBACK (renderer_edited_cb), impl);
4639 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4640 G_CALLBACK (renderer_editing_canceled_cb), impl);
4641 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4642 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4643 list_name_data_func, impl, NULL);
4645 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4649 column = gtk_tree_view_column_new ();
4650 gtk_tree_view_column_set_title (column, _("Size"));
4652 renderer = gtk_cell_renderer_text_new ();
4653 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4654 gtk_tree_view_column_set_cell_data_func (column, renderer,
4655 list_size_data_func, impl, NULL);
4656 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4657 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4658 impl->list_size_column = column;
4660 /* Modification time column */
4662 column = gtk_tree_view_column_new ();
4663 gtk_tree_view_column_set_resizable (column, TRUE);
4664 gtk_tree_view_column_set_title (column, _("Modified"));
4666 renderer = gtk_cell_renderer_text_new ();
4667 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4668 gtk_tree_view_column_set_cell_data_func (column, renderer,
4669 list_mtime_data_func, impl, NULL);
4670 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4671 impl->list_mtime_column = column;
4673 file_list_set_sort_column_ids (impl);
4675 gtk_widget_show_all (swin);
4681 create_path_bar (GtkFileChooserDefault *impl)
4683 GtkWidget *path_bar;
4685 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4686 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4691 /* Creates the widgets for the files/folders pane */
4693 file_pane_create (GtkFileChooserDefault *impl,
4694 GtkSizeGroup *size_group)
4700 vbox = gtk_vbox_new (FALSE, 6);
4701 gtk_widget_show (vbox);
4703 /* Box for lists and preview */
4705 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4706 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4707 gtk_widget_show (hbox);
4711 widget = create_file_list (impl);
4712 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4716 impl->preview_box = gtk_vbox_new (FALSE, 12);
4717 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4718 /* Don't show preview box initially */
4722 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4724 widget = filter_create (impl);
4726 gtk_widget_show (widget);
4727 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4729 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4730 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4735 /* Callback used when the "Browse for more folders" expander is toggled */
4737 expander_changed_cb (GtkExpander *expander,
4739 GtkFileChooserDefault *impl)
4741 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4742 update_appearance (impl);
4745 /* Callback used when the selection changes in the save folder combo box */
4747 save_folder_combo_changed_cb (GtkComboBox *combo,
4748 GtkFileChooserDefault *impl)
4752 if (impl->changing_folder)
4755 if (gtk_combo_box_get_active_iter (combo, &iter))
4757 GtkTreeIter child_iter;
4759 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4762 shortcuts_activate_iter (impl, &child_iter);
4766 /* Filter function used to filter out the Search item and its separator.
4767 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4770 shortcuts_combo_filter_func (GtkTreeModel *model,
4774 GtkFileChooserDefault *impl;
4775 GtkTreePath *tree_path;
4780 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4782 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4784 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4785 g_assert (tree_path != NULL);
4787 indices = gtk_tree_path_get_indices (tree_path);
4791 if (impl->has_search)
4793 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4794 if (idx == indices[0])
4798 if (impl->has_recent)
4800 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4801 if (idx == indices[0])
4805 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4806 if (idx == indices[0])
4811 gtk_tree_path_free (tree_path);
4816 /* Creates the combo box with the save folders */
4818 save_folder_combo_create (GtkFileChooserDefault *impl)
4821 GtkCellRenderer *cell;
4823 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4824 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4825 shortcuts_combo_filter_func,
4829 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4830 "model", impl->shortcuts_combo_filter_model,
4831 "focus-on-click", FALSE,
4833 gtk_widget_show (combo);
4835 cell = gtk_cell_renderer_pixbuf_new ();
4836 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4837 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4838 "pixbuf", SHORTCUTS_COL_PIXBUF,
4839 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4840 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4843 cell = gtk_cell_renderer_text_new ();
4844 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4845 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4846 "text", SHORTCUTS_COL_NAME,
4847 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4850 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4851 shortcuts_row_separator_func,
4854 g_signal_connect (combo, "changed",
4855 G_CALLBACK (save_folder_combo_changed_cb), impl);
4860 /* Creates the widgets specific to Save mode */
4862 save_widgets_create (GtkFileChooserDefault *impl)
4867 GtkWidget *alignment;
4869 if (impl->save_widgets != NULL)
4872 location_switch_to_path_bar (impl);
4874 vbox = gtk_vbox_new (FALSE, 12);
4876 table = gtk_table_new (2, 2, FALSE);
4877 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4878 gtk_widget_show (table);
4879 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4880 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4884 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4885 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4886 gtk_table_attach (GTK_TABLE (table), widget,
4890 gtk_widget_show (widget);
4892 /* Location entry */
4894 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4895 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4897 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4898 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4899 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4901 GTK_EXPAND | GTK_FILL, 0,
4903 gtk_widget_show (impl->location_entry);
4904 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4907 impl->save_folder_label = gtk_label_new (NULL);
4908 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4909 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4913 gtk_widget_show (impl->save_folder_label);
4915 impl->save_folder_combo = save_folder_combo_create (impl);
4916 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4918 GTK_EXPAND | GTK_FILL, GTK_FILL,
4920 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4923 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4924 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4926 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4927 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4928 g_signal_connect (impl->save_expander, "notify::expanded",
4929 G_CALLBACK (expander_changed_cb),
4931 gtk_widget_show_all (alignment);
4933 impl->save_widgets = vbox;
4934 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4935 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4936 gtk_widget_show (impl->save_widgets);
4939 /* Destroys the widgets specific to Save mode */
4941 save_widgets_destroy (GtkFileChooserDefault *impl)
4943 if (impl->save_widgets == NULL)
4946 gtk_widget_destroy (impl->save_widgets);
4947 impl->save_widgets = NULL;
4948 impl->location_entry = NULL;
4949 impl->save_folder_label = NULL;
4950 impl->save_folder_combo = NULL;
4951 impl->save_expander = NULL;
4954 /* Turns on the path bar widget. Can be called even if we are already in that
4958 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4960 if (impl->location_entry)
4962 gtk_widget_destroy (impl->location_entry);
4963 impl->location_entry = NULL;
4966 gtk_widget_hide (impl->location_entry_box);
4969 /* Sets the full path of the current folder as the text in the location entry. */
4971 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4973 gchar *text, *filename;
4975 if (!impl->current_folder)
4978 filename = g_file_get_path (impl->current_folder);
4982 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4986 text = g_file_get_uri (impl->current_folder);
4990 gboolean need_slash;
4993 len = strlen (text);
4994 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
5000 slash_text = g_new (char, len + 2);
5001 strcpy (slash_text, text);
5002 slash_text[len] = G_DIR_SEPARATOR;
5003 slash_text[len + 1] = 0;
5009 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5016 /* Turns on the location entry. Can be called even if we are already in that
5020 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5022 /* when in search or recent files mode, we are not showing the
5023 * location_entry_box container, so there's no point in switching
5026 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5027 impl->operation_mode == OPERATION_MODE_RECENT)
5030 if (impl->location_entry)
5031 gtk_widget_destroy (impl->location_entry);
5035 gtk_widget_show (impl->location_entry_box);
5039 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5040 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5042 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5043 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5045 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5046 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5048 /* Configure the entry */
5050 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5054 gtk_widget_show (impl->location_entry);
5055 gtk_widget_grab_focus (impl->location_entry);
5058 /* Sets a new location mode. set_buttons determines whether the toggle button
5059 * for the mode will also be changed.
5062 location_mode_set (GtkFileChooserDefault *impl,
5063 LocationMode new_mode,
5064 gboolean set_button)
5066 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5067 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5069 GtkWindow *toplevel;
5070 GtkWidget *current_focus;
5071 gboolean button_active;
5072 gboolean switch_to_file_list;
5076 case LOCATION_MODE_PATH_BAR:
5077 button_active = FALSE;
5079 /* The location_entry will disappear when we switch to path bar mode. So,
5080 * we'll focus the file list in that case, to avoid having a window with
5081 * no focused widget.
5083 toplevel = get_toplevel (GTK_WIDGET (impl));
5084 switch_to_file_list = FALSE;
5087 current_focus = gtk_window_get_focus (toplevel);
5088 if (!current_focus || current_focus == impl->location_entry)
5089 switch_to_file_list = TRUE;
5092 location_switch_to_path_bar (impl);
5094 if (switch_to_file_list)
5095 gtk_widget_grab_focus (impl->browse_files_tree_view);
5099 case LOCATION_MODE_FILENAME_ENTRY:
5100 button_active = TRUE;
5101 location_switch_to_filename_entry (impl);
5105 g_assert_not_reached ();
5111 g_signal_handlers_block_by_func (impl->location_button,
5112 G_CALLBACK (location_button_toggled_cb), impl);
5114 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5116 g_signal_handlers_unblock_by_func (impl->location_button,
5117 G_CALLBACK (location_button_toggled_cb), impl);
5121 impl->location_mode = new_mode;
5125 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5127 /* when in search or recent files mode, we are not showing the
5128 * location_entry_box container, so there's no point in switching
5131 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5132 impl->operation_mode == OPERATION_MODE_RECENT)
5135 /* If the file entry is not visible, show it.
5136 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5138 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5140 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5142 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5144 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5146 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5150 gtk_widget_grab_focus (impl->location_entry);
5155 /* Callback used when one of the location mode buttons is toggled */
5157 location_button_toggled_cb (GtkToggleButton *toggle,
5158 GtkFileChooserDefault *impl)
5161 LocationMode new_mode;
5163 is_active = gtk_toggle_button_get_active (toggle);
5167 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5168 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5172 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5173 new_mode = LOCATION_MODE_PATH_BAR;
5176 location_mode_set (impl, new_mode, FALSE);
5179 /* Creates a toggle button for the location entry. */
5181 location_button_create (GtkFileChooserDefault *impl)
5186 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5187 gtk_widget_show (image);
5189 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5193 g_signal_connect (impl->location_button, "toggled",
5194 G_CALLBACK (location_button_toggled_cb), impl);
5196 str = _("Type a file name");
5198 gtk_widget_set_tooltip_text (impl->location_button, str);
5199 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5202 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5204 browse_widgets_create (GtkFileChooserDefault *impl)
5210 GtkSizeGroup *size_group;
5212 /* size group is used by the [+][-] buttons and the filter combo */
5213 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5214 vbox = gtk_vbox_new (FALSE, 12);
5216 /* Location widgets */
5217 hbox = gtk_hbox_new (FALSE, 12);
5218 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5219 gtk_widget_show (hbox);
5220 impl->browse_path_bar_hbox = hbox;
5222 location_button_create (impl);
5223 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5227 impl->browse_path_bar = create_path_bar (impl);
5228 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5229 gtk_widget_show_all (impl->browse_path_bar);
5230 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5233 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5234 g_signal_connect (impl->browse_new_folder_button, "clicked",
5235 G_CALLBACK (new_folder_button_clicked), impl);
5236 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5238 /* Box for the location label and entry */
5240 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5241 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5243 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5244 gtk_widget_show (impl->location_label);
5245 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5248 hpaned = gtk_hpaned_new ();
5249 gtk_widget_show (hpaned);
5250 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5252 widget = shortcuts_pane_create (impl, size_group);
5253 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5254 widget = file_pane_create (impl, size_group);
5255 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5257 g_object_unref (size_group);
5263 gtk_file_chooser_default_constructor (GType type,
5264 guint n_construct_properties,
5265 GObjectConstructParam *construct_params)
5267 GtkFileChooserDefault *impl;
5270 profile_start ("start", NULL);
5272 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5273 n_construct_properties,
5275 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5277 g_assert (impl->file_system);
5279 gtk_widget_push_composite_child ();
5281 /* Shortcuts model */
5282 shortcuts_model_create (impl);
5284 /* The browse widgets */
5285 impl->browse_widgets = browse_widgets_create (impl);
5286 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5288 /* Alignment to hold extra widget */
5289 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5290 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5292 gtk_widget_pop_composite_child ();
5293 update_appearance (impl);
5295 profile_end ("end", NULL);
5300 /* Sets the extra_widget by packing it in the appropriate place */
5302 set_extra_widget (GtkFileChooserDefault *impl,
5303 GtkWidget *extra_widget)
5307 g_object_ref (extra_widget);
5308 /* FIXME: is this right ? */
5309 gtk_widget_show (extra_widget);
5312 if (impl->extra_widget)
5314 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5315 g_object_unref (impl->extra_widget);
5318 impl->extra_widget = extra_widget;
5319 if (impl->extra_widget)
5321 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5322 gtk_widget_show (impl->extra_align);
5325 gtk_widget_hide (impl->extra_align);
5329 set_local_only (GtkFileChooserDefault *impl,
5330 gboolean local_only)
5332 if (local_only != impl->local_only)
5334 impl->local_only = local_only;
5336 if (impl->shortcuts_model && impl->file_system)
5338 shortcuts_add_volumes (impl);
5339 shortcuts_add_bookmarks (impl);
5342 if (local_only && impl->current_folder &&
5343 !g_file_is_native (impl->current_folder))
5345 /* If we are pointing to a non-local folder, make an effort to change
5346 * back to a local folder, but it's really up to the app to not cause
5347 * such a situation, so we ignore errors.
5349 const gchar *home = g_get_home_dir ();
5355 home_file = g_file_new_for_path (home);
5357 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5359 g_object_unref (home_file);
5365 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5366 GtkFileChooserDefault *impl)
5368 shortcuts_add_volumes (impl);
5369 shortcuts_add_bookmarks (impl);
5371 bookmarks_check_add_sensitivity (impl);
5372 bookmarks_check_remove_sensitivity (impl);
5373 shortcuts_check_popup_sensitivity (impl);
5376 /* Sets the file chooser to multiple selection mode */
5378 set_select_multiple (GtkFileChooserDefault *impl,
5379 gboolean select_multiple,
5380 gboolean property_notify)
5382 GtkTreeSelection *selection;
5383 GtkSelectionMode mode;
5385 if (select_multiple == impl->select_multiple)
5388 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5390 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5391 gtk_tree_selection_set_mode (selection, mode);
5393 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5395 impl->select_multiple = select_multiple;
5396 g_object_notify (G_OBJECT (impl), "select-multiple");
5398 check_preview_change (impl);
5402 set_file_system_backend (GtkFileChooserDefault *impl)
5404 profile_start ("start for backend", "default");
5406 impl->file_system = _gtk_file_system_new ();
5408 g_signal_connect (impl->file_system, "volumes-changed",
5409 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5410 g_signal_connect (impl->file_system, "bookmarks-changed",
5411 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5413 profile_end ("end", NULL);
5417 unset_file_system_backend (GtkFileChooserDefault *impl)
5419 g_signal_handlers_disconnect_by_func (impl->file_system,
5420 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5422 g_object_unref (impl->file_system);
5424 impl->file_system = NULL;
5427 /* This function is basically a do_all function.
5429 * It sets the visibility on all the widgets based on the current state, and
5430 * moves the custom_widget if needed.
5433 update_appearance (GtkFileChooserDefault *impl)
5435 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5436 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5440 gtk_widget_hide (impl->location_button);
5441 save_widgets_create (impl);
5443 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5444 text = _("Save in _folder:");
5446 text = _("Create in _folder:");
5448 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5450 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5452 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5453 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5454 gtk_widget_show (impl->browse_widgets);
5458 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5459 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5460 gtk_widget_hide (impl->browse_widgets);
5463 gtk_widget_show (impl->browse_new_folder_button);
5465 if (impl->select_multiple)
5467 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5468 "Re-setting to single selection mode.");
5469 set_select_multiple (impl, FALSE, TRUE);
5472 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5473 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5475 gtk_widget_show (impl->location_button);
5476 save_widgets_destroy (impl);
5477 gtk_widget_show (impl->browse_widgets);
5478 location_mode_set (impl, impl->location_mode, TRUE);
5481 if (impl->location_entry)
5482 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5484 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5485 gtk_widget_hide (impl->browse_new_folder_button);
5487 gtk_widget_show (impl->browse_new_folder_button);
5489 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5490 * of files may change depending whether we are in a file or folder-only mode.
5492 gtk_widget_queue_draw (impl->browse_files_tree_view);
5494 emit_default_size_changed (impl);
5498 gtk_file_chooser_default_set_property (GObject *object,
5500 const GValue *value,
5504 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5508 case GTK_FILE_CHOOSER_PROP_ACTION:
5510 GtkFileChooserAction action = g_value_get_enum (value);
5512 if (action != impl->action)
5514 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5516 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5517 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5518 && impl->select_multiple)
5520 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5521 "this is not allowed in multiple selection mode. Resetting the file chooser "
5522 "to single selection mode.");
5523 set_select_multiple (impl, FALSE, TRUE);
5525 impl->action = action;
5526 update_appearance (impl);
5527 settings_load (impl);
5532 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5533 /* Ignore property */
5536 case GTK_FILE_CHOOSER_PROP_FILTER:
5537 set_current_filter (impl, g_value_get_object (value));
5540 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5541 set_local_only (impl, g_value_get_boolean (value));
5544 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5545 set_preview_widget (impl, g_value_get_object (value));
5548 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5549 impl->preview_widget_active = g_value_get_boolean (value);
5550 update_preview_widget_visibility (impl);
5553 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5554 impl->use_preview_label = g_value_get_boolean (value);
5555 update_preview_widget_visibility (impl);
5558 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5559 set_extra_widget (impl, g_value_get_object (value));
5562 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5564 gboolean select_multiple = g_value_get_boolean (value);
5565 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5566 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5569 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5570 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5571 "leaving the file chooser in single selection mode.");
5575 set_select_multiple (impl, select_multiple, FALSE);
5579 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5581 gboolean show_hidden = g_value_get_boolean (value);
5582 if (show_hidden != impl->show_hidden)
5584 impl->show_hidden = show_hidden;
5586 if (impl->browse_files_model)
5587 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5592 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5594 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5595 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5600 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5606 gtk_file_chooser_default_get_property (GObject *object,
5611 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5615 case GTK_FILE_CHOOSER_PROP_ACTION:
5616 g_value_set_enum (value, impl->action);
5619 case GTK_FILE_CHOOSER_PROP_FILTER:
5620 g_value_set_object (value, impl->current_filter);
5623 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5624 g_value_set_boolean (value, impl->local_only);
5627 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5628 g_value_set_object (value, impl->preview_widget);
5631 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5632 g_value_set_boolean (value, impl->preview_widget_active);
5635 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5636 g_value_set_boolean (value, impl->use_preview_label);
5639 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5640 g_value_set_object (value, impl->extra_widget);
5643 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5644 g_value_set_boolean (value, impl->select_multiple);
5647 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5648 g_value_set_boolean (value, impl->show_hidden);
5651 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5652 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5656 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5661 /* Removes the settings signal handler. It's safe to call multiple times */
5663 remove_settings_signal (GtkFileChooserDefault *impl,
5666 if (impl->settings_signal_id)
5668 GtkSettings *settings;
5670 settings = gtk_settings_get_for_screen (screen);
5671 g_signal_handler_disconnect (settings,
5672 impl->settings_signal_id);
5673 impl->settings_signal_id = 0;
5678 gtk_file_chooser_default_dispose (GObject *object)
5681 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5683 if (impl->extra_widget)
5685 g_object_unref (impl->extra_widget);
5686 impl->extra_widget = NULL;
5689 pending_select_files_free (impl);
5691 /* cancel all pending operations */
5692 if (impl->pending_cancellables)
5694 for (l = impl->pending_cancellables; l; l = l->next)
5696 GCancellable *cancellable = G_CANCELLABLE (l->data);
5697 g_cancellable_cancel (cancellable);
5699 g_slist_free (impl->pending_cancellables);
5700 impl->pending_cancellables = NULL;
5703 if (impl->reload_icon_cancellables)
5705 for (l = impl->reload_icon_cancellables; l; l = l->next)
5707 GCancellable *cancellable = G_CANCELLABLE (l->data);
5708 g_cancellable_cancel (cancellable);
5710 g_slist_free (impl->reload_icon_cancellables);
5711 impl->reload_icon_cancellables = NULL;
5714 if (impl->loading_shortcuts)
5716 for (l = impl->loading_shortcuts; l; l = l->next)
5718 GCancellable *cancellable = G_CANCELLABLE (l->data);
5719 g_cancellable_cancel (cancellable);
5721 g_slist_free (impl->loading_shortcuts);
5722 impl->loading_shortcuts = NULL;
5725 if (impl->file_list_drag_data_received_cancellable)
5727 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5728 impl->file_list_drag_data_received_cancellable = NULL;
5731 if (impl->update_current_folder_cancellable)
5733 g_cancellable_cancel (impl->update_current_folder_cancellable);
5734 impl->update_current_folder_cancellable = NULL;
5737 if (impl->show_and_select_files_cancellable)
5739 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5740 impl->show_and_select_files_cancellable = NULL;
5743 if (impl->should_respond_get_info_cancellable)
5745 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5746 impl->should_respond_get_info_cancellable = NULL;
5749 if (impl->update_from_entry_cancellable)
5751 g_cancellable_cancel (impl->update_from_entry_cancellable);
5752 impl->update_from_entry_cancellable = NULL;
5755 if (impl->shortcuts_activate_iter_cancellable)
5757 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5758 impl->shortcuts_activate_iter_cancellable = NULL;
5761 search_stop_searching (impl, TRUE);
5762 recent_stop_loading (impl);
5764 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5766 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5769 /* We override show-all since we have internal widgets that
5770 * shouldn't be shown when you call show_all(), like the filter
5774 gtk_file_chooser_default_show_all (GtkWidget *widget)
5776 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5778 gtk_widget_show (widget);
5780 if (impl->extra_widget)
5781 gtk_widget_show_all (impl->extra_widget);
5784 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5785 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5788 toplevel_set_focus_cb (GtkWindow *window,
5790 GtkFileChooserDefault *impl)
5792 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5795 /* We monitor the focus widget on our toplevel to be able to know which widget
5796 * was last focused at the time our "should_respond" method gets called.
5799 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5800 GtkWidget *previous_toplevel)
5802 GtkFileChooserDefault *impl;
5803 GtkWidget *toplevel;
5805 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5807 if (previous_toplevel)
5809 g_assert (impl->toplevel_set_focus_id != 0);
5810 g_signal_handler_disconnect (previous_toplevel,
5811 impl->toplevel_set_focus_id);
5812 impl->toplevel_set_focus_id = 0;
5813 impl->toplevel_last_focus_widget = NULL;
5816 g_assert (impl->toplevel_set_focus_id == 0);
5818 toplevel = gtk_widget_get_toplevel (widget);
5819 if (GTK_IS_WINDOW (toplevel))
5821 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5822 G_CALLBACK (toplevel_set_focus_cb), impl);
5823 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5827 /* Changes the icons wherever it is needed */
5829 change_icon_theme (GtkFileChooserDefault *impl)
5831 GtkSettings *settings;
5834 profile_start ("start", NULL);
5836 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5838 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5839 impl->icon_size = MAX (width, height);
5841 impl->icon_size = FALLBACK_ICON_SIZE;
5843 shortcuts_reload_icons (impl);
5844 gtk_widget_queue_resize (impl->browse_files_tree_view);
5846 profile_end ("end", NULL);
5849 /* Callback used when a GtkSettings value changes */
5851 settings_notify_cb (GObject *object,
5853 GtkFileChooserDefault *impl)
5857 profile_start ("start", NULL);
5859 name = g_param_spec_get_name (pspec);
5861 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5862 strcmp (name, "gtk-icon-sizes") == 0)
5863 change_icon_theme (impl);
5865 profile_end ("end", NULL);
5868 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5872 check_icon_theme (GtkFileChooserDefault *impl)
5874 GtkSettings *settings;
5876 profile_start ("start", NULL);
5878 if (impl->settings_signal_id)
5880 profile_end ("end", NULL);
5884 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5886 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5887 impl->settings_signal_id = g_signal_connect (settings, "notify",
5888 G_CALLBACK (settings_notify_cb), impl);
5890 change_icon_theme (impl);
5893 profile_end ("end", NULL);
5897 gtk_file_chooser_default_style_set (GtkWidget *widget,
5898 GtkStyle *previous_style)
5900 GtkFileChooserDefault *impl;
5902 profile_start ("start", NULL);
5904 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5906 profile_msg (" parent class style_set start", NULL);
5907 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5908 profile_msg (" parent class style_set end", NULL);
5910 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5911 change_icon_theme (impl);
5913 emit_default_size_changed (impl);
5915 profile_end ("end", NULL);
5919 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5920 GdkScreen *previous_screen)
5922 GtkFileChooserDefault *impl;
5924 profile_start ("start", NULL);
5926 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5928 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5929 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5931 remove_settings_signal (impl, previous_screen);
5932 check_icon_theme (impl);
5934 emit_default_size_changed (impl);
5936 profile_end ("end", NULL);
5940 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5941 GtkAllocation *allocation)
5943 GtkFileChooserDefault *impl;
5945 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5947 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5951 get_is_file_filtered (GtkFileChooserDefault *impl,
5953 GFileInfo *file_info)
5955 GtkFileFilterInfo filter_info;
5956 GtkFileFilterFlags needed;
5959 if (!impl->current_filter)
5962 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5964 needed = gtk_file_filter_get_needed (impl->current_filter);
5966 filter_info.display_name = g_file_info_get_display_name (file_info);
5967 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
5969 if (needed & GTK_FILE_FILTER_FILENAME)
5971 filter_info.filename = g_file_get_path (file);
5972 if (filter_info.filename)
5973 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5976 filter_info.filename = NULL;
5978 if (needed & GTK_FILE_FILTER_URI)
5980 filter_info.uri = g_file_get_uri (file);
5981 if (filter_info.uri)
5982 filter_info.contains |= GTK_FILE_FILTER_URI;
5985 filter_info.uri = NULL;
5987 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5989 g_free ((gchar *)filter_info.filename);
5990 g_free ((gchar *)filter_info.uri);
5991 g_free ((gchar *)filter_info.mime_type);
5997 settings_load (GtkFileChooserDefault *impl)
5999 GtkFileChooserSettings *settings;
6000 LocationMode location_mode;
6001 gboolean show_hidden;
6002 gboolean expand_folders;
6003 gboolean show_size_column;
6005 settings = _gtk_file_chooser_settings_new ();
6007 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6008 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6009 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6010 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
6012 g_object_unref (settings);
6014 location_mode_set (impl, location_mode, TRUE);
6016 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6018 impl->expand_folders = expand_folders;
6019 if (impl->save_expander)
6020 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6022 impl->show_size_column = show_size_column;
6023 if (impl->list_size_column)
6024 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6028 settings_save (GtkFileChooserDefault *impl)
6030 GtkFileChooserSettings *settings;
6032 settings = _gtk_file_chooser_settings_new ();
6034 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6035 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6036 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6037 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6040 _gtk_file_chooser_settings_save (settings, NULL);
6042 g_object_unref (settings);
6045 /* GtkWidget::map method */
6047 gtk_file_chooser_default_map (GtkWidget *widget)
6049 GtkFileChooserDefault *impl;
6050 char *current_working_dir;
6052 profile_start ("start", NULL);
6054 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6056 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6058 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6060 switch (impl->reload_state)
6063 /* The user didn't explicitly give us a folder to
6064 * display, so we'll use the cwd
6066 current_working_dir = g_get_current_dir ();
6067 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6068 current_working_dir);
6069 g_free (current_working_dir);
6072 case RELOAD_HAS_FOLDER:
6073 /* Nothing; we are already loading or loaded, so we
6074 * don't need to reload
6078 case RELOAD_WAS_UNMAPPED:
6079 /* Just reload the current folder; else continue
6082 if (impl->current_folder)
6084 pending_select_files_store_selection (impl);
6085 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6090 g_assert_not_reached ();
6094 volumes_bookmarks_changed_cb (impl->file_system, impl);
6096 settings_load (impl);
6098 emit_default_size_changed (impl);
6100 profile_end ("end", NULL);
6103 /* GtkWidget::unmap method */
6105 gtk_file_chooser_default_unmap (GtkWidget *widget)
6107 GtkFileChooserDefault *impl;
6109 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6111 settings_save (impl);
6113 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6115 impl->reload_state = RELOAD_WAS_UNMAPPED;
6119 list_model_filter_func (GtkFileSystemModel *model,
6121 GFileInfo *file_info,
6124 GtkFileChooserDefault *impl = user_data;
6126 if (!impl->current_filter)
6129 if (_gtk_file_info_consider_as_directory (file_info))
6132 return !get_is_file_filtered (impl, file, file_info);
6136 install_list_model_filter (GtkFileChooserDefault *impl)
6138 GtkFileSystemModelFilter filter;
6141 g_assert (impl->browse_files_model != NULL);
6143 if (impl->current_filter)
6145 filter = list_model_filter_func;
6154 _gtk_file_system_model_set_filter (impl->browse_files_model,
6159 #define COMPARE_DIRECTORIES \
6160 GtkFileChooserDefault *impl = user_data; \
6161 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6162 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6163 gboolean dir_a, dir_b; \
6166 dir_a = _gtk_file_info_consider_as_directory (info_a); \
6168 return impl->list_sort_ascending ? -1 : 1; \
6171 dir_b = _gtk_file_info_consider_as_directory (info_b); \
6173 return impl->list_sort_ascending ? 1 : -1; \
6175 if (dir_a != dir_b) \
6176 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6178 /* Sort callback for the filename column */
6180 name_sort_func (GtkTreeModel *model,
6185 COMPARE_DIRECTORIES;
6188 gchar *key_a, *key_b;
6191 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6192 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6193 result = strcmp (key_a, key_b);
6202 /* Sort callback for the size column */
6204 size_sort_func (GtkTreeModel *model,
6209 COMPARE_DIRECTORIES;
6212 goffset size_a = g_file_info_get_size (info_a);
6213 goffset size_b = g_file_info_get_size (info_b);
6215 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6219 /* Sort callback for the mtime column */
6221 mtime_sort_func (GtkTreeModel *model,
6226 COMPARE_DIRECTORIES;
6231 g_file_info_get_modification_time (info_a, &ta);
6232 g_file_info_get_modification_time (info_b, &tb);
6234 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6238 /* Callback used when the sort column changes. We cache the sort order for use
6239 * in name_sort_func().
6242 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6243 GtkFileChooserDefault *impl)
6245 GtkSortType sort_type;
6247 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6248 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6252 set_busy_cursor (GtkFileChooserDefault *impl,
6255 GtkWindow *toplevel;
6256 GdkDisplay *display;
6259 toplevel = get_toplevel (GTK_WIDGET (impl));
6260 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6263 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6266 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6270 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6271 gdk_display_flush (display);
6274 gdk_cursor_unref (cursor);
6277 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6279 load_set_model (GtkFileChooserDefault *impl)
6281 profile_start ("start", NULL);
6283 g_assert (impl->browse_files_model != NULL);
6284 g_assert (impl->sort_model == NULL);
6286 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6287 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6288 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6289 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6290 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6291 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6292 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6293 impl->list_sort_ascending = TRUE;
6294 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6296 g_signal_connect (impl->sort_model, "sort-column-changed",
6297 G_CALLBACK (list_sort_column_changed_cb), impl);
6299 profile_msg (" gtk_tree_view_set_model start", NULL);
6300 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6301 GTK_TREE_MODEL (impl->sort_model));
6302 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6303 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6304 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6305 profile_msg (" gtk_tree_view_set_model end", NULL);
6307 profile_end ("end", NULL);
6310 /* Timeout callback used when the loading timer expires */
6312 load_timeout_cb (gpointer data)
6314 GtkFileChooserDefault *impl;
6316 profile_start ("start", NULL);
6318 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6319 g_assert (impl->load_state == LOAD_PRELOAD);
6320 g_assert (impl->load_timeout_id != 0);
6321 g_assert (impl->browse_files_model != NULL);
6323 impl->load_timeout_id = 0;
6324 impl->load_state = LOAD_LOADING;
6326 load_set_model (impl);
6328 profile_end ("end", NULL);
6333 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6335 load_setup_timer (GtkFileChooserDefault *impl)
6337 g_assert (impl->load_timeout_id == 0);
6338 g_assert (impl->load_state != LOAD_PRELOAD);
6340 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6341 impl->load_state = LOAD_PRELOAD;
6344 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6346 load_remove_timer (GtkFileChooserDefault *impl)
6348 if (impl->load_timeout_id != 0)
6350 g_assert (impl->load_state == LOAD_PRELOAD);
6352 g_source_remove (impl->load_timeout_id);
6353 impl->load_timeout_id = 0;
6354 impl->load_state = LOAD_EMPTY;
6357 g_assert (impl->load_state == LOAD_EMPTY ||
6358 impl->load_state == LOAD_LOADING ||
6359 impl->load_state == LOAD_FINISHED);
6362 /* Selects the first row in the file list */
6364 browse_files_select_first_row (GtkFileChooserDefault *impl)
6367 GtkTreeIter dummy_iter;
6368 GtkTreeModel *tree_model;
6370 if (!impl->sort_model)
6373 path = gtk_tree_path_new_from_indices (0, -1);
6374 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6376 /* If the list is empty, do nothing. */
6377 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6378 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6380 gtk_tree_path_free (path);
6383 struct center_selected_row_closure {
6384 GtkFileChooserDefault *impl;
6385 gboolean already_centered;
6388 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6389 * selected row in the tree view.
6392 center_selected_row_foreach_cb (GtkTreeModel *model,
6397 struct center_selected_row_closure *closure;
6400 if (closure->already_centered)
6403 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6404 closure->already_centered = TRUE;
6407 /* Centers the selected row in the tree view */
6409 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6411 struct center_selected_row_closure closure;
6412 GtkTreeSelection *selection;
6414 closure.impl = impl;
6415 closure.already_centered = FALSE;
6417 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6418 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6421 struct ShowAndSelectPathsData
6423 GtkFileChooserDefault *impl;
6428 show_and_select_files_finished_loading (GtkFolder *folder,
6431 gboolean have_hidden;
6432 gboolean have_filtered;
6434 struct ShowAndSelectPathsData *data = user_data;
6436 have_hidden = FALSE;
6437 have_filtered = FALSE;
6439 for (l = data->files; l; l = l->next)
6446 info = _gtk_folder_get_info (folder, file);
6450 have_hidden = g_file_info_get_is_hidden (info);
6453 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6454 get_is_file_filtered (data->impl, file, info);
6456 g_object_unref (info);
6458 if (have_hidden && have_filtered)
6459 break; /* we now have all the information we need */
6463 g_signal_handlers_disconnect_by_func (folder,
6464 show_and_select_files_finished_loading,
6468 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6471 set_current_filter (data->impl, NULL);
6473 for (l = data->files; l; l = l->next)
6478 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6479 select_func, data->impl);
6482 browse_files_center_selected_row (data->impl);
6484 g_object_unref (data->impl);
6485 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6486 g_slist_free (data->files);
6491 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6493 const GError *error,
6496 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6497 struct ShowAndSelectPathsData *data = user_data;
6499 if (data->impl->show_and_select_files_cancellable != cancellable)
6502 data->impl->show_and_select_files_cancellable = NULL;
6504 if (cancelled || error)
6507 g_object_unref (cancellable);
6509 if (_gtk_folder_is_finished_loading (folder))
6510 show_and_select_files_finished_loading (folder, user_data);
6512 g_signal_connect (folder, "finished-loading",
6513 G_CALLBACK (show_and_select_files_finished_loading),
6519 g_object_unref (data->impl);
6520 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6521 g_slist_free (data->files);
6524 g_object_unref (cancellable);
6528 show_and_select_files (GtkFileChooserDefault *impl,
6533 struct ShowAndSelectPathsData *info;
6535 profile_start ("start", NULL);
6539 profile_end ("end", NULL);
6543 info = g_new (struct ShowAndSelectPathsData, 1);
6544 info->impl = g_object_ref (impl);
6545 info->files = g_slist_copy (files);
6546 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6548 if (impl->show_and_select_files_cancellable)
6549 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6551 impl->show_and_select_files_cancellable =
6552 _gtk_file_system_get_folder (impl->file_system, parent_file,
6553 "standard::is-hidden,standard::type,standard::name,standard::content-type",
6554 show_and_select_files_get_folder_cb, info);
6556 profile_end ("end", NULL);
6560 /* Processes the pending operation when a folder is finished loading */
6562 pending_select_files_process (GtkFileChooserDefault *impl)
6564 g_assert (impl->load_state == LOAD_FINISHED);
6565 g_assert (impl->browse_files_model != NULL);
6566 g_assert (impl->sort_model != NULL);
6568 if (impl->pending_select_files)
6571 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6572 pending_select_files_free (impl);
6573 browse_files_center_selected_row (impl);
6577 /* We only select the first row if the chooser is actually mapped ---
6578 * selecting the first row is to help the user when he is interacting with
6579 * the chooser, but sometimes a chooser works not on behalf of the user,
6580 * but rather on behalf of something else like GtkFileChooserButton. In
6581 * that case, the chooser's selection should be what the caller expects,
6582 * as the user can't see that something else got selected. See bug #165264.
6584 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6585 browse_files_select_first_row (impl);
6588 g_assert (impl->pending_select_files == NULL);
6591 /* Callback used when the file system model finishes loading */
6593 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6594 GtkFileChooserDefault *impl)
6596 profile_start ("start", NULL);
6598 if (impl->load_state == LOAD_PRELOAD)
6600 load_remove_timer (impl);
6601 load_set_model (impl);
6603 else if (impl->load_state == LOAD_LOADING)
6609 /* We can't g_assert_not_reached(), as something other than us may have
6610 * initiated a folder reload. See #165556.
6612 profile_end ("end", NULL);
6616 g_assert (impl->load_timeout_id == 0);
6618 impl->load_state = LOAD_FINISHED;
6620 pending_select_files_process (impl);
6621 set_busy_cursor (impl, FALSE);
6622 #ifdef PROFILE_FILE_CHOOSER
6623 access ("MARK: *** FINISHED LOADING", F_OK);
6626 profile_end ("end", NULL);
6630 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6632 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6634 if (impl->browse_files_model)
6636 g_object_unref (impl->browse_files_model);
6637 impl->browse_files_model = NULL;
6640 if (impl->sort_model)
6642 g_object_unref (impl->sort_model);
6643 impl->sort_model = NULL;
6646 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6649 /* Gets rid of the old list model and creates a new one for the current folder */
6651 set_list_model (GtkFileChooserDefault *impl,
6654 g_assert (impl->current_folder != NULL);
6656 profile_start ("start", NULL);
6658 stop_loading_and_clear_list_model (impl);
6660 set_busy_cursor (impl, TRUE);
6661 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6663 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6664 impl->current_folder, 0,
6665 "standard,time,thumbnail::*",
6667 if (!impl->browse_files_model)
6669 set_busy_cursor (impl, FALSE);
6670 profile_end ("end", NULL);
6674 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6676 g_signal_connect (impl->browse_files_model, "finished-loading",
6677 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6679 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6681 install_list_model_filter (impl);
6683 profile_end ("end", NULL);
6688 struct update_chooser_entry_selected_foreach_closure {
6690 GtkTreeIter first_selected_iter;
6694 compare_utf8_filenames (const gchar *a,
6697 gchar *a_folded, *b_folded;
6700 a_folded = g_utf8_strdown (a, -1);
6701 b_folded = g_utf8_strdown (b, -1);
6703 retval = strcmp (a_folded, b_folded);
6712 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6717 struct update_chooser_entry_selected_foreach_closure *closure;
6720 closure->num_selected++;
6722 if (closure->num_selected == 1)
6723 closure->first_selected_iter = *iter;
6727 update_chooser_entry (GtkFileChooserDefault *impl)
6729 GtkTreeSelection *selection;
6730 struct update_chooser_entry_selected_foreach_closure closure;
6731 const char *file_part;
6733 /* no need to update the file chooser's entry if there's no entry */
6734 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6735 impl->operation_mode == OPERATION_MODE_RECENT ||
6736 !impl->location_entry)
6739 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6740 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6741 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6742 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6743 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6746 g_assert (impl->location_entry != NULL);
6748 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6749 closure.num_selected = 0;
6750 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6754 if (closure.num_selected == 0)
6756 goto maybe_clear_entry;
6758 else if (closure.num_selected == 1)
6760 GtkTreeIter child_iter;
6762 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6765 gboolean change_entry;
6767 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6769 &closure.first_selected_iter);
6771 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6773 /* If the cursor moved to the row of the newly created folder,
6774 * retrieving info will return NULL.
6779 g_free (impl->browse_files_last_selected_name);
6780 impl->browse_files_last_selected_name =
6781 g_strdup (g_file_info_get_display_name (info));
6783 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6784 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6785 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6787 /* We don't want the name to change when clicking on a folder... */
6788 change_entry = (! _gtk_file_info_consider_as_directory (info));
6791 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6795 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6797 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6798 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6806 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6807 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6809 /* Multiple selection, so just clear the entry. */
6811 g_free (impl->browse_files_last_selected_name);
6812 impl->browse_files_last_selected_name = NULL;
6814 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6820 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6821 && impl->browse_files_last_selected_name)
6823 const char *entry_text;
6825 gboolean clear_entry;
6827 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6828 len = strlen (entry_text);
6831 /* The file chooser entry may have appended a "/" to its text. So
6832 * take it out, and compare the result to the old selection.
6834 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6838 tmp = g_strndup (entry_text, len - 1);
6839 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6843 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6846 clear_entry = FALSE;
6849 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6854 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6858 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6862 struct UpdateCurrentFolderData
6864 GtkFileChooserDefault *impl;
6866 gboolean keep_trail;
6867 gboolean clear_entry;
6868 GFile *original_file;
6869 GError *original_error;
6873 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6874 GtkFileSystemVolume *volume,
6875 const GError *error,
6878 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6879 struct UpdateCurrentFolderData *data = user_data;
6880 GtkFileChooserDefault *impl = data->impl;
6882 if (cancellable != impl->update_current_folder_cancellable)
6885 impl->update_current_folder_cancellable = NULL;
6886 set_busy_cursor (impl, FALSE);
6893 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6894 impl->reload_state = RELOAD_EMPTY;
6898 change_folder_and_display_error (impl, data->file, data->clear_entry);
6901 g_object_unref (data->file);
6904 g_object_unref (cancellable);
6908 update_current_folder_get_info_cb (GCancellable *cancellable,
6910 const GError *error,
6913 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6914 struct UpdateCurrentFolderData *data = user_data;
6915 GtkFileChooserDefault *impl = data->impl;
6917 if (cancellable != impl->update_current_folder_cancellable)
6920 impl->update_current_folder_cancellable = NULL;
6921 impl->reload_state = RELOAD_EMPTY;
6923 set_busy_cursor (impl, FALSE);
6932 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6934 GMountOperation *mount_operation;
6935 GtkWidget *toplevel;
6937 g_object_unref (cancellable);
6938 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6940 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6942 set_busy_cursor (impl, TRUE);
6944 impl->update_current_folder_cancellable =
6945 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
6947 update_current_folder_mount_enclosing_volume_cb,
6953 if (!data->original_file)
6955 data->original_file = g_object_ref (data->file);
6956 data->original_error = g_error_copy (error);
6959 parent_file = g_file_get_parent (data->file);
6961 /* get parent path and try to change the folder to that */
6964 g_object_unref (data->file);
6965 data->file = parent_file;
6967 g_object_unref (cancellable);
6969 /* restart the update current folder operation */
6970 impl->reload_state = RELOAD_HAS_FOLDER;
6972 impl->update_current_folder_cancellable =
6973 _gtk_file_system_get_info (impl->file_system, data->file,
6975 update_current_folder_get_info_cb,
6978 set_busy_cursor (impl, TRUE);
6984 /* Error and bail out, ignoring "not found" errors since they're useless:
6985 * they only happen when a program defaults to a folder that has been (re)moved.
6987 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
6988 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6990 g_error_free (data->original_error);
6991 g_object_unref (data->original_file);
6997 if (data->original_file)
6999 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7001 g_object_unref (data->original_file);
7004 if (! _gtk_file_info_consider_as_directory (info))
7007 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7010 if (impl->current_folder != data->file)
7012 if (impl->current_folder)
7013 g_object_unref (impl->current_folder);
7015 impl->current_folder = g_object_ref (data->file);
7018 impl->reload_state = RELOAD_HAS_FOLDER;
7020 /* Update the widgets that may trigger a folder change themselves. */
7022 if (!impl->changing_folder)
7024 impl->changing_folder = TRUE;
7026 shortcuts_update_current_folder (impl);
7028 impl->changing_folder = FALSE;
7031 /* Set the folder on the save entry */
7033 if (impl->location_entry)
7035 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7036 impl->current_folder);
7038 if (data->clear_entry)
7039 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7042 /* Create a new list model. This is slightly evil; we store the result value
7043 * but perform more actions rather than returning immediately even if it
7044 * generates an error.
7046 set_list_model (impl, NULL);
7048 /* Refresh controls */
7050 shortcuts_find_current_folder (impl);
7052 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7054 check_preview_change (impl);
7055 bookmarks_check_add_sensitivity (impl);
7057 g_signal_emit_by_name (impl, "selection-changed", 0);
7060 g_object_unref (data->file);
7063 g_object_unref (cancellable);
7067 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7069 gboolean keep_trail,
7070 gboolean clear_entry,
7073 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7074 struct UpdateCurrentFolderData *data;
7076 profile_start ("start", NULL);
7078 g_object_ref (file);
7080 switch (impl->operation_mode)
7082 case OPERATION_MODE_SEARCH:
7083 search_switch_to_browse_mode (impl);
7085 case OPERATION_MODE_RECENT:
7086 recent_switch_to_browse_mode (impl);
7088 case OPERATION_MODE_BROWSE:
7092 if (impl->local_only && !g_file_is_native (file))
7094 g_set_error_literal (error,
7095 GTK_FILE_CHOOSER_ERROR,
7096 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7097 _("Cannot change to folder because it is not local"));
7099 g_object_unref (file);
7100 profile_end ("end - not local", NULL);
7104 if (impl->update_current_folder_cancellable)
7105 g_cancellable_cancel (impl->update_current_folder_cancellable);
7107 /* Test validity of path here. */
7108 data = g_new0 (struct UpdateCurrentFolderData, 1);
7110 data->file = g_object_ref (file);
7111 data->keep_trail = keep_trail;
7112 data->clear_entry = clear_entry;
7114 impl->reload_state = RELOAD_HAS_FOLDER;
7116 impl->update_current_folder_cancellable =
7117 _gtk_file_system_get_info (impl->file_system, file,
7119 update_current_folder_get_info_cb,
7122 set_busy_cursor (impl, TRUE);
7123 g_object_unref (file);
7125 profile_end ("end", NULL);
7130 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7132 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7134 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7135 impl->operation_mode == OPERATION_MODE_RECENT)
7138 if (impl->reload_state == RELOAD_EMPTY)
7140 char *current_working_dir;
7143 /* We are unmapped, or we had an error while loading the last folder. We'll return
7144 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7145 * explicitly calls set_current_folder() on us.
7147 current_working_dir = g_get_current_dir ();
7148 file = g_file_new_for_path (current_working_dir);
7149 g_free (current_working_dir);
7153 if (impl->current_folder)
7154 return g_object_ref (impl->current_folder);
7160 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7163 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7165 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7166 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7168 pending_select_files_free (impl);
7169 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7173 select_func (GtkFileSystemModel *model,
7178 GtkFileChooserDefault *impl = user_data;
7179 GtkTreeSelection *selection;
7180 GtkTreeIter sorted_iter;
7182 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7184 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7185 gtk_tree_selection_select_iter (selection, &sorted_iter);
7189 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7193 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7197 parent_file = g_file_get_parent (file);
7200 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7202 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7203 impl->operation_mode == OPERATION_MODE_RECENT ||
7204 impl->load_state == LOAD_EMPTY)
7210 g_assert (impl->current_folder != NULL);
7212 same_path = g_file_equal (parent_file, impl->current_folder);
7215 if (same_path && impl->load_state == LOAD_FINISHED)
7220 files.data = (gpointer) file;
7223 result = show_and_select_files (impl, parent_file, &files, error);
7224 g_object_unref (parent_file);
7228 pending_select_files_add (impl, file);
7234 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7235 g_object_unref (parent_file);
7239 g_object_unref (parent_file);
7244 unselect_func (GtkFileSystemModel *model,
7249 GtkFileChooserDefault *impl = user_data;
7250 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7251 GtkTreePath *sorted_path;
7253 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7255 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7257 gtk_tree_path_free (sorted_path);
7261 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7264 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7266 if (!impl->browse_files_model)
7269 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7270 unselect_func, impl);
7274 maybe_select (GtkTreeModel *model,
7279 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7280 GtkTreeSelection *selection;
7284 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7286 info = get_list_file_info (impl, iter);
7287 is_folder = _gtk_file_info_consider_as_directory (info);
7289 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7290 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7291 gtk_tree_selection_select_iter (selection, iter);
7293 gtk_tree_selection_unselect_iter (selection, iter);
7299 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7301 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7303 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7304 impl->operation_mode == OPERATION_MODE_RECENT)
7306 GtkTreeSelection *selection;
7308 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7309 gtk_tree_selection_select_all (selection);
7313 if (impl->select_multiple)
7314 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7315 maybe_select, impl);
7319 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7321 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7322 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7324 gtk_tree_selection_unselect_all (selection);
7325 pending_select_files_free (impl);
7328 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7330 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7332 * is_empty_ret - whether the file entry is totally empty
7334 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7335 * the path will be "$cwd/foobar")
7338 check_save_entry (GtkFileChooserDefault *impl,
7340 gboolean *is_well_formed_ret,
7341 gboolean *is_empty_ret,
7342 gboolean *is_file_part_empty_ret,
7343 gboolean *is_folder)
7345 GtkFileChooserEntry *chooser_entry;
7346 GFile *current_folder;
7347 const char *file_part;
7351 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7352 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7353 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7354 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7355 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7357 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7359 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7362 *is_well_formed_ret = TRUE;
7363 *is_empty_ret = TRUE;
7364 *is_file_part_empty_ret = TRUE;
7370 *is_empty_ret = FALSE;
7372 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7373 if (!current_folder)
7376 *is_well_formed_ret = FALSE;
7377 *is_file_part_empty_ret = FALSE;
7383 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7385 if (!file_part || file_part[0] == '\0')
7387 *file_ret = g_object_ref (current_folder);
7388 *is_well_formed_ret = TRUE;
7389 *is_file_part_empty_ret = TRUE;
7395 *is_file_part_empty_ret = FALSE;
7398 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7402 error_building_filename_dialog (impl, error);
7404 *is_well_formed_ret = FALSE;
7411 *is_well_formed_ret = TRUE;
7412 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7415 struct get_files_closure {
7416 GtkFileChooserDefault *impl;
7418 GFile *file_from_entry;
7422 get_files_foreach (GtkTreeModel *model,
7427 struct get_files_closure *info;
7429 GtkFileSystemModel *fs_model;
7430 GtkTreeIter sel_iter;
7433 fs_model = info->impl->browse_files_model;
7434 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7436 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7438 return; /* We are on the editable row */
7440 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7441 info->result = g_slist_prepend (info->result, g_object_ref (file));
7445 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7447 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7448 struct get_files_closure info;
7449 GtkWindow *toplevel;
7450 GtkWidget *current_focus;
7451 gboolean file_list_seen;
7453 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7454 return search_get_selected_files (impl);
7456 if (impl->operation_mode == OPERATION_MODE_RECENT)
7457 return recent_get_selected_files (impl);
7461 info.file_from_entry = NULL;
7463 toplevel = get_toplevel (GTK_WIDGET (impl));
7465 current_focus = gtk_window_get_focus (toplevel);
7467 current_focus = NULL;
7469 file_list_seen = FALSE;
7470 if (current_focus == impl->browse_files_tree_view)
7472 GtkTreeSelection *selection;
7476 file_list_seen = TRUE;
7477 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7478 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7480 /* If there is no selection in the file list, we probably have this situation:
7482 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7483 * 2. He then double-clicked on a folder ("bar") in the file list
7485 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7486 * filename entry to see if that is the case.
7488 if (info.result == NULL && impl->location_entry)
7491 else if (impl->location_entry && current_focus == impl->location_entry)
7493 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7497 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7502 if (!is_well_formed)
7505 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7507 g_object_unref (info.file_from_entry);
7511 if (info.file_from_entry)
7512 info.result = g_slist_prepend (info.result, info.file_from_entry);
7513 else if (!file_list_seen)
7518 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7520 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7524 /* The focus is on a dialog's action area button or something else */
7525 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7526 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7534 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7535 * fall back to the current directory */
7536 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7537 info.result == NULL)
7539 GFile *current_folder;
7541 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7544 info.result = g_slist_prepend (info.result, current_folder);
7547 return g_slist_reverse (info.result);
7551 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7553 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7555 if (impl->preview_file)
7556 return g_object_ref (impl->preview_file);
7561 static GtkFileSystem *
7562 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7564 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7566 return impl->file_system;
7569 /* Shows or hides the filter widgets */
7571 show_filters (GtkFileChooserDefault *impl,
7575 gtk_widget_show (impl->filter_combo_hbox);
7577 gtk_widget_hide (impl->filter_combo_hbox);
7581 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7582 GtkFileFilter *filter)
7584 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7587 if (g_slist_find (impl->filters, filter))
7589 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7593 g_object_ref_sink (filter);
7594 impl->filters = g_slist_append (impl->filters, filter);
7596 name = gtk_file_filter_get_name (filter);
7598 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7600 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7602 if (!g_slist_find (impl->filters, impl->current_filter))
7603 set_current_filter (impl, filter);
7605 show_filters (impl, TRUE);
7609 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7610 GtkFileFilter *filter)
7612 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7613 GtkTreeModel *model;
7617 filter_index = g_slist_index (impl->filters, filter);
7619 if (filter_index < 0)
7621 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7625 impl->filters = g_slist_remove (impl->filters, filter);
7627 if (filter == impl->current_filter)
7630 set_current_filter (impl, impl->filters->data);
7632 set_current_filter (impl, NULL);
7635 /* Remove row from the combo box */
7636 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7637 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7638 g_assert_not_reached ();
7640 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7642 g_object_unref (filter);
7645 show_filters (impl, FALSE);
7649 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7651 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7653 return g_slist_copy (impl->filters);
7656 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7658 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7661 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7664 struct AddShortcutData
7666 GtkFileChooserDefault *impl;
7671 add_shortcut_get_info_cb (GCancellable *cancellable,
7673 const GError *error,
7677 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7678 struct AddShortcutData *data = user_data;
7680 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7683 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7685 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7688 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7690 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7693 g_object_unref (data->impl);
7694 g_object_unref (data->file);
7697 g_object_unref (cancellable);
7701 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7705 GCancellable *cancellable;
7706 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7707 struct AddShortcutData *data;
7711 /* Avoid adding duplicates */
7712 pos = shortcut_find_position (impl, file);
7713 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7717 uri = g_file_get_uri (file);
7718 /* translators, "Shortcut" means "Bookmark" here */
7720 GTK_FILE_CHOOSER_ERROR,
7721 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7722 _("Shortcut %s already exists"),
7729 for (l = impl->loading_shortcuts; l; l = l->next)
7731 GCancellable *c = l->data;
7734 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7735 if (f && g_file_equal (file, f))
7739 uri = g_file_get_uri (file);
7741 GTK_FILE_CHOOSER_ERROR,
7742 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7743 _("Shortcut %s already exists"),
7751 data = g_new0 (struct AddShortcutData, 1);
7752 data->impl = g_object_ref (impl);
7753 data->file = g_object_ref (file);
7755 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7757 add_shortcut_get_info_cb, data);
7762 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7763 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7769 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7773 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7780 for (l = impl->loading_shortcuts; l; l = l->next)
7782 GCancellable *c = l->data;
7785 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7786 if (f && g_file_equal (file, f))
7788 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7789 g_cancellable_cancel (c);
7794 if (impl->num_shortcuts == 0)
7797 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7798 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7799 g_assert_not_reached ();
7801 for (i = 0; i < impl->num_shortcuts; i++)
7804 ShortcutType shortcut_type;
7807 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7808 SHORTCUTS_COL_DATA, &col_data,
7809 SHORTCUTS_COL_TYPE, &shortcut_type,
7811 g_assert (col_data != NULL);
7812 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7814 shortcut = col_data;
7815 if (g_file_equal (shortcut, file))
7817 shortcuts_remove_rows (impl, pos + i, 1);
7818 impl->num_shortcuts--;
7822 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7823 g_assert_not_reached ();
7828 uri = g_file_get_uri (file);
7829 /* translators, "Shortcut" means "Bookmark" here */
7831 GTK_FILE_CHOOSER_ERROR,
7832 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7833 _("Shortcut %s does not exist"),
7841 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7843 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7849 if (impl->num_shortcuts == 0)
7852 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7853 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7854 g_assert_not_reached ();
7858 for (i = 0; i < impl->num_shortcuts; i++)
7861 ShortcutType shortcut_type;
7864 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7865 SHORTCUTS_COL_DATA, &col_data,
7866 SHORTCUTS_COL_TYPE, &shortcut_type,
7868 g_assert (col_data != NULL);
7869 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7871 shortcut = col_data;
7872 list = g_slist_prepend (list, g_object_ref (shortcut));
7874 if (i != impl->num_shortcuts - 1)
7876 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7877 g_assert_not_reached ();
7881 return g_slist_reverse (list);
7884 /* Guesses a size based upon font sizes */
7886 find_good_size_from_style (GtkWidget *widget,
7890 GtkFileChooserDefault *impl;
7895 g_assert (widget->style != NULL);
7896 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7898 screen = gtk_widget_get_screen (widget);
7901 resolution = gdk_screen_get_resolution (screen);
7902 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7906 resolution = 96.0; /* wheeee */
7908 font_size = pango_font_description_get_size (widget->style->font_desc);
7909 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7911 *width = font_size * NUM_CHARS;
7912 *height = font_size * NUM_LINES;
7916 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7917 gint *default_width,
7918 gint *default_height)
7920 GtkFileChooserDefault *impl;
7923 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7925 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7926 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7927 || impl->expand_folders)
7929 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7931 if (impl->preview_widget_active &&
7932 impl->preview_widget &&
7933 GTK_WIDGET_VISIBLE (impl->preview_widget))
7935 gtk_widget_size_request (impl->preview_box, &req);
7936 *default_width += PREVIEW_HBOX_SPACING + req.width;
7939 if (impl->extra_widget &&
7940 GTK_WIDGET_VISIBLE (impl->extra_widget))
7942 gtk_widget_size_request (impl->extra_align, &req);
7943 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7948 gtk_widget_size_request (GTK_WIDGET (impl), &req);
7949 *default_width = req.width;
7950 *default_height = req.height;
7954 struct switch_folder_closure {
7955 GtkFileChooserDefault *impl;
7960 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7962 switch_folder_foreach_cb (GtkTreeModel *model,
7967 struct switch_folder_closure *closure;
7968 GtkTreeIter child_iter;
7972 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7974 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7975 closure->num_selected++;
7978 /* Changes to the selected folder in the list view */
7980 switch_to_selected_folder (GtkFileChooserDefault *impl)
7982 GtkTreeSelection *selection;
7983 struct switch_folder_closure closure;
7985 /* We do this with foreach() rather than get_selected() as we may be in
7986 * multiple selection mode
7989 closure.impl = impl;
7990 closure.file = NULL;
7991 closure.num_selected = 0;
7993 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7994 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7996 g_assert (closure.file && closure.num_selected == 1);
7998 change_folder_and_display_error (impl, closure.file, FALSE);
8001 /* Gets the GFileInfo for the selected row in the file list; assumes single
8005 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8006 gboolean *had_selection)
8008 GtkTreeSelection *selection;
8009 GtkTreeIter iter, child_iter;
8012 g_assert (!impl->select_multiple);
8013 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8014 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8016 *had_selection = FALSE;
8020 *had_selection = TRUE;
8022 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8026 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8030 /* Gets the display name of the selected file in the file list; assumes single
8031 * selection mode and that something is selected.
8033 static const gchar *
8034 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8037 gboolean had_selection;
8039 info = get_selected_file_info_from_file_list (impl, &had_selection);
8040 g_assert (had_selection);
8041 g_assert (info != NULL);
8043 return g_file_info_get_display_name (info);
8047 add_custom_button_to_dialog (GtkDialog *dialog,
8048 const gchar *mnemonic_label,
8049 const gchar *stock_id,
8054 button = gtk_button_new_with_mnemonic (mnemonic_label);
8055 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8056 gtk_button_set_image (GTK_BUTTON (button),
8057 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8058 gtk_widget_show (button);
8060 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8063 /* Presents an overwrite confirmation dialog; returns whether we should accept
8067 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8068 const gchar *file_part,
8069 const gchar *folder_display_name)
8071 GtkWindow *toplevel;
8075 toplevel = get_toplevel (GTK_WIDGET (impl));
8077 dialog = gtk_message_dialog_new (toplevel,
8078 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8079 GTK_MESSAGE_QUESTION,
8081 _("A file named \"%s\" already exists. Do you want to replace it?"),
8083 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8084 _("The file already exists in \"%s\". Replacing it will "
8085 "overwrite its contents."),
8086 folder_display_name);
8088 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8089 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8090 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8091 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8092 GTK_RESPONSE_ACCEPT,
8093 GTK_RESPONSE_CANCEL,
8095 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8097 if (toplevel->group)
8098 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8100 response = gtk_dialog_run (GTK_DIALOG (dialog));
8102 gtk_widget_destroy (dialog);
8104 return (response == GTK_RESPONSE_ACCEPT);
8107 struct GetDisplayNameData
8109 GtkFileChooserDefault *impl;
8114 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8116 const GError *error,
8119 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8120 gboolean should_respond = FALSE;
8121 struct GetDisplayNameData *data = user_data;
8123 if (cancellable != data->impl->should_respond_get_info_cancellable)
8126 data->impl->should_respond_get_info_cancellable = NULL;
8132 /* Huh? Did the folder disappear? Let the caller deal with it */
8133 should_respond = TRUE;
8135 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8137 set_busy_cursor (data->impl, FALSE);
8139 g_signal_emit_by_name (data->impl, "response-requested");
8142 g_object_unref (data->impl);
8143 g_free (data->file_part);
8146 g_object_unref (cancellable);
8149 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8150 * should respond. Can get the file part from the file list or the save entry.
8153 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8154 const gchar *file_part,
8157 GtkFileChooserConfirmation conf;
8159 if (!impl->do_overwrite_confirmation)
8162 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8164 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8168 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8170 struct GetDisplayNameData *data;
8172 g_assert (file_part != NULL);
8174 data = g_new0 (struct GetDisplayNameData, 1);
8175 data->impl = g_object_ref (impl);
8176 data->file_part = g_strdup (file_part);
8178 if (impl->should_respond_get_info_cancellable)
8179 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8181 impl->should_respond_get_info_cancellable =
8182 _gtk_file_system_get_info (impl->file_system, parent_file,
8183 "standard::display-name",
8184 confirmation_confirm_get_info_cb,
8186 set_busy_cursor (data->impl, TRUE);
8190 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8193 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8197 g_assert_not_reached ();
8202 struct FileExistsData
8204 GtkFileChooserDefault *impl;
8205 gboolean file_exists_and_is_not_folder;
8211 save_entry_get_info_cb (GCancellable *cancellable,
8213 const GError *error,
8216 gboolean parent_is_folder;
8217 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8218 struct FileExistsData *data = user_data;
8220 if (cancellable != data->impl->should_respond_get_info_cancellable)
8223 data->impl->should_respond_get_info_cancellable = NULL;
8225 set_busy_cursor (data->impl, FALSE);
8231 parent_is_folder = FALSE;
8233 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8235 if (parent_is_folder)
8237 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8239 if (data->file_exists_and_is_not_folder)
8242 const char *file_part;
8244 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8245 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8248 g_signal_emit_by_name (data->impl, "response-requested");
8251 g_signal_emit_by_name (data->impl, "response-requested");
8253 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8255 GError *error = NULL;
8257 set_busy_cursor (data->impl, TRUE);
8258 g_file_make_directory (data->file, NULL, &error);
8259 set_busy_cursor (data->impl, FALSE);
8262 g_signal_emit_by_name (data->impl, "response-requested");
8264 error_creating_folder_dialog (data->impl, data->file, error);
8269 /* This will display an error, which is what we want */
8270 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8274 g_object_unref (data->impl);
8275 g_object_unref (data->file);
8276 g_object_unref (data->parent_file);
8279 g_object_unref (cancellable);
8283 file_exists_get_info_cb (GCancellable *cancellable,
8285 const GError *error,
8288 gboolean data_ownership_taken = FALSE;
8289 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8290 gboolean file_exists_and_is_not_folder;
8291 struct FileExistsData *data = user_data;
8293 if (cancellable != data->impl->file_exists_get_info_cancellable)
8296 data->impl->file_exists_get_info_cancellable = NULL;
8298 set_busy_cursor (data->impl, FALSE);
8303 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8305 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8306 /* user typed a filename; we are done */
8307 g_signal_emit_by_name (data->impl, "response-requested");
8308 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8309 && file_exists_and_is_not_folder)
8311 /* Oops, the user typed the name of an existing path which is not
8314 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8315 g_error_copy (error));
8319 /* check that everything up to the last component exists */
8321 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8322 data_ownership_taken = TRUE;
8324 if (data->impl->should_respond_get_info_cancellable)
8325 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8327 data->impl->should_respond_get_info_cancellable =
8328 _gtk_file_system_get_info (data->impl->file_system,
8331 save_entry_get_info_cb,
8333 set_busy_cursor (data->impl, TRUE);
8337 if (!data_ownership_taken)
8339 g_object_unref (data->impl);
8340 g_object_unref (data->file);
8341 g_object_unref (data->parent_file);
8345 g_object_unref (cancellable);
8349 paste_text_received (GtkClipboard *clipboard,
8351 GtkFileChooserDefault *impl)
8358 file = g_file_new_for_uri (text);
8360 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8361 location_popup_handler (impl, text);
8363 g_object_unref (file);
8366 /* Handler for the "location-popup-on-paste" keybinding signal */
8368 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8370 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8371 GDK_SELECTION_CLIPBOARD);
8372 gtk_clipboard_request_text (clipboard,
8373 (GtkClipboardTextReceivedFunc) paste_text_received,
8378 /* Implementation for GtkFileChooserEmbed::should_respond() */
8380 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8382 GtkFileChooserDefault *impl;
8383 GtkWidget *toplevel;
8384 GtkWidget *current_focus;
8386 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8388 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8389 g_assert (GTK_IS_WINDOW (toplevel));
8391 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8393 if (current_focus == impl->browse_files_tree_view)
8395 /* The following array encodes what we do based on the impl->action and the
8396 * number of files selected.
8399 NOOP, /* Do nothing (don't respond) */
8400 RESPOND, /* Respond immediately */
8401 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8402 ALL_FILES, /* Respond only if everything selected is a file */
8403 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8404 SAVE_ENTRY, /* Go to the code for handling the save entry */
8405 NOT_REACHED /* Sanity check */
8407 static const ActionToTake what_to_do[4][3] = {
8408 /* 0 selected 1 selected many selected */
8409 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8410 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8411 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8412 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8416 gboolean all_files, all_folders;
8418 ActionToTake action;
8422 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8424 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8425 return search_should_respond (impl);
8427 if (impl->operation_mode == OPERATION_MODE_RECENT)
8428 return recent_should_respond (impl);
8430 selection_check (impl, &num_selected, &all_files, &all_folders);
8432 if (num_selected > 2)
8437 action = what_to_do [impl->action] [k];
8447 case RESPOND_OR_SWITCH:
8448 g_assert (num_selected == 1);
8452 switch_to_selected_folder (impl);
8455 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8456 return should_respond_after_confirm_overwrite (impl,
8457 get_display_name_from_file_list (impl),
8458 impl->current_folder);
8472 g_assert_not_reached ();
8475 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8478 gboolean is_well_formed, is_empty, is_file_part_empty;
8481 GtkFileChooserEntry *entry;
8486 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8487 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8488 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8489 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8490 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8492 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8493 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8495 if (is_empty || !is_well_formed)
8498 g_assert (file != NULL);
8503 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8504 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8506 change_folder_and_display_error (impl, file, TRUE);
8509 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8510 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8512 /* The folder already exists, so we do not need to create it.
8513 * Just respond to terminate the dialog.
8519 g_assert_not_reached ();
8525 struct FileExistsData *data;
8527 /* We need to check whether file exists and is not a folder */
8529 data = g_new0 (struct FileExistsData, 1);
8530 data->impl = g_object_ref (impl);
8531 data->file = g_object_ref (file);
8532 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8534 if (impl->file_exists_get_info_cancellable)
8535 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8537 impl->file_exists_get_info_cancellable =
8538 _gtk_file_system_get_info (impl->file_system, file,
8540 file_exists_get_info_cb,
8543 set_busy_cursor (impl, TRUE);
8547 g_error_free (error);
8550 g_object_unref (file);
8553 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8555 /* The focus is on a dialog's action area button, *and* the widget that
8556 * was focused immediately before it is the file list.
8560 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8562 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8565 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8567 /* The focus is on a dialog's action area button, *and* the widget that
8568 * was focused immediately before it is the location entry.
8573 /* The focus is on a dialog's action area button or something else */
8574 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8575 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8580 g_assert_not_reached ();
8584 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8586 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8588 GtkFileChooserDefault *impl;
8591 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8593 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8594 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8596 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8597 widget = impl->browse_files_tree_view;
8599 widget = impl->location_entry;
8601 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8602 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8603 widget = impl->location_entry;
8606 g_assert_not_reached ();
8610 g_assert (widget != NULL);
8611 gtk_widget_grab_focus (widget);
8614 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8616 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8626 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8627 *list = g_slist_prepend (*list, g_object_ref (file));
8630 /* Constructs a list of the selected paths in search mode */
8632 search_get_selected_files (GtkFileChooserDefault *impl)
8635 GtkTreeSelection *selection;
8639 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8640 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8641 result = g_slist_reverse (result);
8646 /* Called from ::should_respond(). We return whether there are selected files
8647 * in the search list.
8650 search_should_respond (GtkFileChooserDefault *impl)
8652 GtkTreeSelection *selection;
8654 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8656 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8657 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8660 struct SearchHitInsertRequest
8662 GtkFileChooserDefault *impl;
8664 GtkTreeRowReference *row_ref;
8668 search_hit_get_info_cb (GCancellable *cancellable,
8670 const GError *error,
8673 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8674 GdkPixbuf *pixbuf = NULL;
8677 GCancellable *model_cancellable;
8678 gboolean is_folder = FALSE;
8680 guint64 modification_time = 0;
8684 struct SearchHitInsertRequest *request = data;
8686 if (!request->impl->search_model)
8689 path = gtk_tree_row_reference_get_path (request->row_ref);
8693 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8695 gtk_tree_path_free (path);
8697 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8698 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8700 if (cancellable != model_cancellable)
8703 /* set the cancellable to NULL in the model */
8704 gtk_list_store_set (request->impl->search_model, &iter,
8705 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8713 search_clear_model_row (GTK_TREE_MODEL (request->impl->search_model), &iter);
8714 gtk_list_store_remove (request->impl->search_model, &iter);
8718 display_name = g_strdup (g_file_info_get_display_name (info));
8719 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
8720 g_file_info_get_modification_time (info, &mtime);
8721 modification_time = (guint64) mtime.tv_sec;
8722 size = g_file_info_get_size (info);
8723 is_folder = _gtk_file_info_consider_as_directory (info);
8724 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8725 request->impl->icon_size);
8727 gtk_list_store_set (request->impl->search_model, &iter,
8728 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8729 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8730 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8731 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8732 SEARCH_MODEL_COL_MTIME, modification_time,
8733 SEARCH_MODEL_COL_SIZE, size,
8737 g_object_unref (pixbuf);
8740 g_object_unref (request->impl);
8741 g_object_unref (request->file);
8742 gtk_tree_row_reference_free (request->row_ref);
8745 g_object_unref (cancellable);
8748 /* Adds one hit from the search engine to the search_model */
8750 search_add_hit (GtkFileChooserDefault *impl,
8755 char *collation_key;
8758 GCancellable *cancellable;
8759 struct SearchHitInsertRequest *request;
8761 file = g_file_new_for_uri (uri);
8765 if (!g_file_is_native (file))
8767 g_object_unref (file);
8771 tmp = g_file_get_parse_name (file);
8772 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8775 request = g_new0 (struct SearchHitInsertRequest, 1);
8776 request->impl = g_object_ref (impl);
8777 request->file = g_object_ref (file);
8779 gtk_list_store_append (impl->search_model, &iter);
8780 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8782 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8783 gtk_tree_path_free (p);
8785 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8788 "standard::content-type,"
8789 "standard::display-name,"
8792 search_hit_get_info_cb,
8795 gtk_list_store_set (impl->search_model, &iter,
8796 SEARCH_MODEL_COL_FILE, file,
8797 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8798 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8802 /* Callback used from GtkSearchEngine when we get new hits */
8804 search_engine_hits_added_cb (GtkSearchEngine *engine,
8808 GtkFileChooserDefault *impl;
8811 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8813 for (l = hits; l; l = l->next)
8814 search_add_hit (impl, (gchar*)l->data);
8817 /* Callback used from GtkSearchEngine when the query is done running */
8819 search_engine_finished_cb (GtkSearchEngine *engine,
8822 GtkFileChooserDefault *impl;
8824 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8827 /* EB: setting the model here will avoid loads of row events,
8828 * but it'll make the search look like blocked.
8830 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8831 GTK_TREE_MODEL (impl->search_model_filter));
8834 /* FMQ: if search was empty, say that we got no hits */
8835 set_busy_cursor (impl, FALSE);
8838 /* Displays a generic error when we cannot create a GtkSearchEngine.
8839 * It would be better if _gtk_search_engine_new() gave us a GError
8840 * with a better message, but it doesn't do that right now.
8843 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8845 error_message (impl,
8846 _("Could not start the search process"),
8847 _("The program was not able to create a connection to the indexer "
8848 "daemon. Please make sure it is running."));
8852 search_engine_error_cb (GtkSearchEngine *engine,
8853 const gchar *message,
8856 GtkFileChooserDefault *impl;
8858 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8860 search_stop_searching (impl, TRUE);
8861 error_message (impl, _("Could not send the search request"), message);
8863 set_busy_cursor (impl, FALSE);
8867 search_clear_model_row (GtkTreeModel *model,
8871 gchar *display_name;
8872 gchar *collation_key;
8873 GCancellable *cancellable;
8876 gtk_tree_model_get (model, iter,
8877 SEARCH_MODEL_COL_FILE, &file,
8878 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8879 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
8880 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8881 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8885 g_object_unref (file);
8887 g_free (display_name);
8888 g_free (collation_key);
8892 g_cancellable_cancel (cancellable);
8895 /* Frees the data in the search_model */
8897 search_clear_model (GtkFileChooserDefault *impl,
8898 gboolean remove_from_treeview)
8900 GtkTreeModel *model;
8903 if (!impl->search_model)
8906 model = GTK_TREE_MODEL (impl->search_model);
8908 if (gtk_tree_model_get_iter_first (model, &iter))
8911 search_clear_model_row (model, &iter);
8913 while (gtk_tree_model_iter_next (model, &iter));
8915 g_object_unref (impl->search_model);
8916 impl->search_model = NULL;
8918 g_object_unref (impl->search_model_filter);
8919 impl->search_model_filter = NULL;
8921 g_object_unref (impl->search_model_sort);
8922 impl->search_model_sort = NULL;
8924 if (remove_from_treeview)
8925 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8928 /* Stops any ongoing searches; does not touch the search_model */
8930 search_stop_searching (GtkFileChooserDefault *impl,
8931 gboolean remove_query)
8933 if (remove_query && impl->search_query)
8935 g_object_unref (impl->search_query);
8936 impl->search_query = NULL;
8939 if (impl->search_engine)
8941 _gtk_search_engine_stop (impl->search_engine);
8943 g_object_unref (impl->search_engine);
8944 impl->search_engine = NULL;
8948 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8950 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8952 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8954 search_stop_searching (impl, FALSE);
8955 search_clear_model (impl, TRUE);
8957 gtk_widget_destroy (impl->search_hbox);
8958 impl->search_hbox = NULL;
8959 impl->search_entry = NULL;
8961 gtk_widget_show (impl->browse_path_bar);
8962 gtk_widget_show (impl->browse_new_folder_button);
8964 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8965 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8967 gtk_widget_show (impl->location_button);
8969 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8970 gtk_widget_show (impl->location_entry_box);
8973 impl->operation_mode = OPERATION_MODE_BROWSE;
8975 file_list_set_sort_column_ids (impl);
8978 /* Sort callback from the path column */
8980 search_column_path_sort_func (GtkTreeModel *model,
8985 GtkFileChooserDefault *impl = user_data;
8986 GtkTreeIter child_a, child_b;
8987 const char *collation_key_a, *collation_key_b;
8988 gboolean is_folder_a, is_folder_b;
8990 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8991 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8993 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8994 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8995 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8997 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8998 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8999 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
9002 if (!collation_key_a)
9005 if (!collation_key_b)
9008 /* always show folders first */
9009 if (is_folder_a != is_folder_b)
9010 return is_folder_a ? 1 : -1;
9012 return strcmp (collation_key_a, collation_key_b);
9015 /* Sort callback from the size column */
9017 search_column_size_sort_func (GtkTreeModel *model,
9022 GtkFileChooserDefault *impl = user_data;
9023 GtkTreeIter child_a, child_b;
9024 gboolean is_folder_a, is_folder_b;
9025 goffset size_a, size_b;
9027 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9028 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9030 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9031 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9032 SEARCH_MODEL_COL_SIZE, &size_a,
9034 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9035 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9036 SEARCH_MODEL_COL_SIZE, &size_b,
9039 if (is_folder_a != is_folder_b)
9040 return is_folder_a ? 1 : -1;
9042 if (size_a < size_b)
9044 else if (size_a > size_b)
9050 /* Sort callback from the modification time column */
9052 search_column_mtime_sort_func (GtkTreeModel *model,
9057 GtkFileChooserDefault *impl = user_data;
9058 GtkTreeIter child_a, child_b;
9059 gboolean is_folder_a, is_folder_b;
9060 guint64 mtime_a, mtime_b;
9062 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9063 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9065 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9066 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9067 SEARCH_MODEL_COL_MTIME, &mtime_a,
9069 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9070 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9071 SEARCH_MODEL_COL_MTIME, &mtime_b,
9074 if (is_folder_a != is_folder_b)
9075 return is_folder_a ? 1 : -1;
9077 if (mtime_a < mtime_b)
9079 else if (mtime_a > mtime_b)
9086 search_get_is_filtered (GtkFileChooserDefault *impl,
9088 const gchar *display_name,
9089 const gchar *mime_type)
9091 GtkFileFilterInfo filter_info;
9092 GtkFileFilterFlags needed;
9095 if (!impl->current_filter)
9098 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9099 needed = gtk_file_filter_get_needed (impl->current_filter);
9101 filter_info.display_name = display_name;
9102 filter_info.mime_type = mime_type;
9104 if (needed & GTK_FILE_FILTER_FILENAME)
9106 filter_info.filename = g_file_get_path (file);
9107 if (filter_info.filename)
9108 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9111 filter_info.filename = NULL;
9113 if (needed & GTK_FILE_FILTER_URI)
9115 filter_info.uri = g_file_get_uri (file);
9116 if (filter_info.uri)
9117 filter_info.contains |= GTK_FILE_FILTER_URI;
9120 filter_info.uri = NULL;
9122 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9124 if (filter_info.filename)
9125 g_free ((gchar *) filter_info.filename);
9126 if (filter_info.uri)
9127 g_free ((gchar *) filter_info.uri);
9133 /* Visibility function for the recent filter model */
9135 search_model_visible_func (GtkTreeModel *model,
9139 GtkFileChooserDefault *impl = user_data;
9141 gchar *display_name, *mime_type;
9144 if (!impl->current_filter)
9147 gtk_tree_model_get (model, iter,
9148 SEARCH_MODEL_COL_FILE, &file,
9149 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9150 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9151 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9160 return !search_get_is_filtered (impl, file, display_name, mime_type);
9163 /* Creates the search_model and puts it in the tree view */
9165 search_setup_model (GtkFileChooserDefault *impl)
9167 g_assert (impl->search_model == NULL);
9168 g_assert (impl->search_model_filter == NULL);
9169 g_assert (impl->search_model_sort == NULL);
9171 /* We store these columns in the search model:
9173 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9174 * as a pointer not as a G_TYPE_FILE
9175 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9176 * as a pointer not as a G_TYPE_STRING
9177 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9178 * as a pointer not as a G_TYPE_STRING
9179 * SEARCH_MODEL_COL_MTIME - G_TYPE_UINT64 for the modification time
9180 * SEARCH_MODEL_COL_SIZE - G_TYPE_INT64 for the size
9181 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9182 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9183 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9184 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9186 * Keep this in sync with the enumeration defined near the beginning
9189 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9190 G_TYPE_POINTER, /* file */
9191 G_TYPE_POINTER, /* display-name */
9192 G_TYPE_POINTER, /* collation-key */
9193 G_TYPE_UINT64, /* mtime */
9194 G_TYPE_INT64, /* size */
9195 G_TYPE_POINTER, /* cancellable */
9196 GDK_TYPE_PIXBUF, /* pixbuf */
9197 G_TYPE_POINTER, /* mime-type */
9198 G_TYPE_BOOLEAN /*is-folder */);
9200 impl->search_model_filter =
9201 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9202 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9203 search_model_visible_func,
9206 impl->search_model_sort =
9207 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9208 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9209 SEARCH_MODEL_COL_FILE,
9210 search_column_path_sort_func,
9212 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9213 SEARCH_MODEL_COL_MTIME,
9214 search_column_mtime_sort_func,
9216 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9217 SEARCH_MODEL_COL_SIZE,
9218 search_column_size_sort_func,
9220 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9221 SEARCH_MODEL_COL_MTIME,
9222 GTK_SORT_DESCENDING);
9224 /* EB: setting the model here will make the hits list update feel
9225 * more "alive" than setting the model at the end of the search
9228 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9229 GTK_TREE_MODEL (impl->search_model_sort));
9233 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9234 GtkTreeIter *child_iter,
9239 if (!impl->search_model)
9242 if (!impl->search_model_filter || !impl->search_model_sort)
9245 /* pass 1: get the iterator in the filter model */
9246 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9249 /* pass 2: get the iterator in the real model */
9250 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9251 child_iter, &middle);
9254 /* Creates a new query with the specified text and launches it */
9256 search_start_query (GtkFileChooserDefault *impl,
9257 const gchar *query_text)
9259 search_stop_searching (impl, FALSE);
9260 search_clear_model (impl, TRUE);
9261 search_setup_model (impl);
9262 set_busy_cursor (impl, TRUE);
9264 if (impl->search_engine == NULL)
9265 impl->search_engine = _gtk_search_engine_new ();
9267 if (!impl->search_engine)
9269 set_busy_cursor (impl, FALSE);
9270 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9274 if (!impl->search_query)
9276 impl->search_query = _gtk_query_new ();
9277 _gtk_query_set_text (impl->search_query, query_text);
9280 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9282 g_signal_connect (impl->search_engine, "hits-added",
9283 G_CALLBACK (search_engine_hits_added_cb), impl);
9284 g_signal_connect (impl->search_engine, "finished",
9285 G_CALLBACK (search_engine_finished_cb), impl);
9286 g_signal_connect (impl->search_engine, "error",
9287 G_CALLBACK (search_engine_error_cb), impl);
9289 _gtk_search_engine_start (impl->search_engine);
9292 /* Callback used when the user presses Enter while typing on the search
9293 * entry; starts the query
9296 search_entry_activate_cb (GtkEntry *entry,
9299 GtkFileChooserDefault *impl;
9302 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9304 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9305 if (strlen (text) == 0)
9308 /* reset any existing query object */
9309 if (impl->search_query)
9311 g_object_unref (impl->search_query);
9312 impl->search_query = NULL;
9315 search_start_query (impl, text);
9318 /* Hides the path bar and creates the search entry */
9320 search_setup_widgets (GtkFileChooserDefault *impl)
9324 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9328 label = gtk_label_new_with_mnemonic (_("_Search:"));
9329 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9333 impl->search_entry = gtk_entry_new ();
9334 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9335 g_signal_connect (impl->search_entry, "activate",
9336 G_CALLBACK (search_entry_activate_cb),
9338 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9340 /* if there already is a query, restart it */
9341 if (impl->search_query)
9343 gchar *query = _gtk_query_get_text (impl->search_query);
9347 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9348 search_start_query (impl, query);
9354 g_object_unref (impl->search_query);
9355 impl->search_query = NULL;
9359 gtk_widget_hide (impl->browse_path_bar);
9360 gtk_widget_hide (impl->browse_new_folder_button);
9362 /* Box for search widgets */
9363 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9364 gtk_widget_show_all (impl->search_hbox);
9366 /* Hide the location widgets temporarily */
9368 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9369 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9371 gtk_widget_hide (impl->location_button);
9372 gtk_widget_hide (impl->location_entry_box);
9375 gtk_widget_grab_focus (impl->search_entry);
9377 /* FMQ: hide the filter combo? */
9380 /* Main entry point to the searching functions; this gets called when the user
9381 * activates the Search shortcut.
9384 search_activate (GtkFileChooserDefault *impl)
9386 OperationMode previous_mode;
9388 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9390 gtk_widget_grab_focus (impl->search_entry);
9394 previous_mode = impl->operation_mode;
9395 impl->operation_mode = OPERATION_MODE_SEARCH;
9397 switch (previous_mode)
9399 case OPERATION_MODE_RECENT:
9400 recent_stop_loading (impl);
9401 recent_clear_model (impl, TRUE);
9404 case OPERATION_MODE_BROWSE:
9405 stop_loading_and_clear_list_model (impl);
9408 case OPERATION_MODE_SEARCH:
9409 g_assert_not_reached ();
9413 g_assert (impl->search_hbox == NULL);
9414 g_assert (impl->search_entry == NULL);
9415 g_assert (impl->search_model == NULL);
9416 g_assert (impl->search_model_filter == NULL);
9418 search_setup_widgets (impl);
9419 file_list_set_sort_column_ids (impl);
9423 * Recent files support
9426 /* Frees the data in the recent_model */
9428 recent_clear_model (GtkFileChooserDefault *impl,
9429 gboolean remove_from_treeview)
9431 GtkTreeModel *model;
9434 if (!impl->recent_model)
9437 model = GTK_TREE_MODEL (impl->recent_model);
9439 if (remove_from_treeview)
9440 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9442 if (gtk_tree_model_get_iter_first (model, &iter))
9447 GCancellable *cancellable;
9448 GtkRecentInfo *recent_info;
9449 gchar *display_name;
9451 gtk_tree_model_get (model, &iter,
9452 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9453 RECENT_MODEL_COL_FILE, &file,
9454 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9455 RECENT_MODEL_COL_INFO, &recent_info,
9459 g_cancellable_cancel (cancellable);
9461 g_object_unref (file);
9462 gtk_recent_info_unref (recent_info);
9463 g_free (display_name);
9465 while (gtk_tree_model_iter_next (model, &iter));
9468 g_object_unref (impl->recent_model);
9469 impl->recent_model = NULL;
9471 g_object_unref (impl->recent_model_filter);
9472 impl->recent_model_filter = NULL;
9474 g_object_unref (impl->recent_model_sort);
9475 impl->recent_model_sort = NULL;
9478 /* Stops any ongoing loading of the recent files list; does
9479 * not touch the recent_model
9482 recent_stop_loading (GtkFileChooserDefault *impl)
9484 if (impl->load_recent_id)
9486 g_source_remove (impl->load_recent_id);
9487 impl->load_recent_id = 0;
9491 /* Stops any pending load, clears the file list, and switches
9492 * back to OPERATION_MODE_BROWSE
9495 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9497 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9499 recent_stop_loading (impl);
9500 recent_clear_model (impl, TRUE);
9502 gtk_widget_show (impl->browse_path_bar);
9503 gtk_widget_show (impl->browse_new_folder_button);
9505 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9506 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9508 gtk_widget_show (impl->location_button);
9510 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9511 gtk_widget_show (impl->location_entry_box);
9514 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9516 impl->operation_mode = OPERATION_MODE_BROWSE;
9518 file_list_set_sort_column_ids (impl);
9521 /* Sort callback from the modification time column */
9523 recent_column_mtime_sort_func (GtkTreeModel *model,
9528 GtkFileChooserDefault *impl = user_data;
9529 GtkTreeIter child_a, child_b;
9530 GtkRecentInfo *info_a, *info_b;
9531 gboolean is_folder_a, is_folder_b;
9533 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9534 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9536 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9537 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9538 RECENT_MODEL_COL_INFO, &info_a,
9540 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9541 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9542 RECENT_MODEL_COL_INFO, &info_b,
9551 /* folders always go first */
9552 if (is_folder_a != is_folder_b)
9553 return is_folder_a ? 1 : -1;
9555 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9557 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9564 recent_column_path_sort_func (GtkTreeModel *model,
9569 GtkFileChooserDefault *impl = user_data;
9570 GtkTreeIter child_a, child_b;
9571 gboolean is_folder_a, is_folder_b;
9572 gchar *name_a, *name_b;
9574 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9575 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9577 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9578 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9579 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9581 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9582 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9583 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9592 if (is_folder_a != is_folder_b)
9593 return is_folder_a ? 1 : -1;
9595 return strcmp (name_a, name_b);
9599 recent_get_is_filtered (GtkFileChooserDefault *impl,
9601 GtkRecentInfo *recent_info)
9603 GtkFileFilterInfo filter_info;
9604 GtkFileFilterFlags needed;
9607 if (!impl->current_filter)
9610 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9611 needed = gtk_file_filter_get_needed (impl->current_filter);
9613 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9614 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9616 if (needed & GTK_FILE_FILTER_FILENAME)
9618 filter_info.filename = g_file_get_path (file);
9619 if (filter_info.filename)
9620 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9623 filter_info.filename = NULL;
9625 if (needed & GTK_FILE_FILTER_URI)
9627 filter_info.uri = g_file_get_uri (file);
9628 if (filter_info.uri)
9629 filter_info.contains |= GTK_FILE_FILTER_URI;
9632 filter_info.uri = NULL;
9634 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9636 if (filter_info.filename)
9637 g_free ((gchar *) filter_info.filename);
9638 if (filter_info.uri)
9639 g_free ((gchar *) filter_info.uri);
9644 /* Visibility function for the recent filter model */
9646 recent_model_visible_func (GtkTreeModel *model,
9650 GtkFileChooserDefault *impl = user_data;
9652 GtkRecentInfo *recent_info;
9655 if (!impl->current_filter)
9658 gtk_tree_model_get (model, iter,
9659 RECENT_MODEL_COL_INFO, &recent_info,
9660 RECENT_MODEL_COL_FILE, &file,
9661 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9670 return !recent_get_is_filtered (impl, file, recent_info);
9674 recent_setup_model (GtkFileChooserDefault *impl)
9676 g_assert (impl->recent_model == NULL);
9677 g_assert (impl->recent_model_filter == NULL);
9678 g_assert (impl->recent_model_sort == NULL);
9680 /* We store these columns in the search model:
9682 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9683 * stored as a pointer and not as a G_TYPE_FILE;
9684 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9685 * stored as a pointer and not as a G_TYPE_STRING;
9686 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9687 * as a GTK_TYPE_RECENT_INFO;
9688 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9689 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9690 * and not as a G_TYPE_CANCELLABLE;
9692 * Keep this in sync with the enumeration defined near the beginning of
9695 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9702 impl->recent_model_filter =
9703 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9704 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9705 recent_model_visible_func,
9709 /* this is the model that will actually be added to
9710 * the browse_files_tree_view widget; remember: we are
9711 * stuffing the real model into a filter model and then
9712 * into a sort model; this means we'll have to translate
9713 * the child iterator *twice* to get from a path or an
9714 * iterator coming from the tree view widget to the
9715 * real data inside the model.
9717 impl->recent_model_sort =
9718 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9719 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9720 RECENT_MODEL_COL_FILE,
9721 recent_column_path_sort_func,
9723 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9724 RECENT_MODEL_COL_INFO,
9725 recent_column_mtime_sort_func,
9727 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9728 RECENT_MODEL_COL_INFO,
9729 GTK_SORT_DESCENDING);
9734 GtkFileChooserDefault *impl;
9737 gint n_loaded_items;
9738 guint needs_sorting : 1;
9742 recent_idle_cleanup (gpointer data)
9744 RecentLoadData *load_data = data;
9745 GtkFileChooserDefault *impl = load_data->impl;
9747 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9748 GTK_TREE_MODEL (impl->recent_model_sort));
9750 set_busy_cursor (impl, FALSE);
9752 impl->load_recent_id = 0;
9754 if (load_data->items)
9756 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9757 g_list_free (load_data->items);
9763 struct RecentItemInsertRequest
9765 GtkFileChooserDefault *impl;
9767 GtkTreeRowReference *row_ref;
9771 recent_item_get_info_cb (GCancellable *cancellable,
9773 const GError *error,
9776 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9779 GCancellable *model_cancellable;
9780 gboolean is_folder = FALSE;
9781 struct RecentItemInsertRequest *request = data;
9783 if (!request->impl->recent_model)
9786 path = gtk_tree_row_reference_get_path (request->row_ref);
9790 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9792 gtk_tree_path_free (path);
9794 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9795 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9797 if (cancellable != model_cancellable)
9800 gtk_list_store_set (request->impl->recent_model, &iter,
9801 RECENT_MODEL_COL_CANCELLABLE, NULL,
9809 gtk_list_store_remove (request->impl->recent_model, &iter);
9813 is_folder = _gtk_file_info_consider_as_directory (info);
9815 gtk_list_store_set (request->impl->recent_model, &iter,
9816 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9820 g_object_unref (request->impl);
9821 g_object_unref (request->file);
9822 gtk_tree_row_reference_free (request->row_ref);
9825 g_object_unref (cancellable);
9829 recent_sort_mru (gconstpointer a,
9832 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9833 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9835 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9839 get_recent_files_limit (GtkWidget *widget)
9841 GtkSettings *settings;
9844 if (gtk_widget_has_screen (widget))
9845 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9847 settings = gtk_settings_get_default ();
9849 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9855 recent_idle_load (gpointer data)
9857 RecentLoadData *load_data = data;
9858 GtkFileChooserDefault *impl = load_data->impl;
9861 GtkRecentInfo *info;
9862 const gchar *uri, *display_name;
9864 GCancellable *cancellable;
9865 struct RecentItemInsertRequest *request;
9867 if (!impl->recent_manager)
9870 /* first iteration: load all the items */
9871 if (!load_data->items)
9873 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9874 if (!load_data->items)
9877 load_data->needs_sorting = TRUE;
9882 /* second iteration: preliminary MRU sorting and clamping */
9883 if (load_data->needs_sorting)
9887 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9888 load_data->n_items = g_list_length (load_data->items);
9890 limit = get_recent_files_limit (GTK_WIDGET (impl));
9892 if (limit != -1 && (load_data->n_items > limit))
9896 clamp = g_list_nth (load_data->items, limit - 1);
9897 if (G_LIKELY (clamp))
9902 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9905 load_data->n_items = limit;
9909 load_data->n_loaded_items = 0;
9910 load_data->needs_sorting = FALSE;
9915 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9916 g_assert (info != NULL);
9918 uri = gtk_recent_info_get_uri (info);
9919 display_name = gtk_recent_info_get_display_name (info);
9920 file = g_file_new_for_uri (uri);
9922 gtk_list_store_append (impl->recent_model, &iter);
9923 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9925 request = g_new0 (struct RecentItemInsertRequest, 1);
9926 request->impl = g_object_ref (impl);
9927 request->file = g_object_ref (file);
9928 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9929 gtk_tree_path_free (p);
9931 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9933 recent_item_get_info_cb,
9936 gtk_list_store_set (impl->recent_model, &iter,
9937 RECENT_MODEL_COL_FILE, file,
9938 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9939 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9940 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9943 load_data->n_loaded_items += 1;
9945 /* finished loading items */
9946 if (load_data->n_loaded_items == load_data->n_items)
9948 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9949 g_list_free (load_data->items);
9950 load_data->items = NULL;
9959 recent_start_loading (GtkFileChooserDefault *impl)
9961 RecentLoadData *load_data;
9963 recent_stop_loading (impl);
9964 recent_clear_model (impl, TRUE);
9965 recent_setup_model (impl);
9966 set_busy_cursor (impl, TRUE);
9968 g_assert (impl->load_recent_id == 0);
9970 load_data = g_new (RecentLoadData, 1);
9971 load_data->impl = impl;
9972 load_data->items = NULL;
9973 load_data->n_items = 0;
9974 load_data->n_loaded_items = 0;
9975 load_data->needs_sorting = TRUE;
9977 /* begin lazy loading the recent files into the model */
9978 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9981 recent_idle_cleanup);
9985 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9995 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9996 *list = g_slist_prepend (*list, g_object_ref (file));
9999 /* Constructs a list of the selected paths in recent files mode */
10001 recent_get_selected_files (GtkFileChooserDefault *impl)
10004 GtkTreeSelection *selection;
10008 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10009 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
10010 result = g_slist_reverse (result);
10015 /* Called from ::should_respond(). We return whether there are selected
10016 * files in the recent files list.
10019 recent_should_respond (GtkFileChooserDefault *impl)
10021 GtkTreeSelection *selection;
10023 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10025 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10026 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10029 /* Hide the location widgets temporarily */
10031 recent_hide_entry (GtkFileChooserDefault *impl)
10033 gtk_widget_hide (impl->browse_path_bar);
10034 gtk_widget_hide (impl->browse_new_folder_button);
10036 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10037 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10039 gtk_widget_hide (impl->location_button);
10040 gtk_widget_hide (impl->location_entry_box);
10044 /* Main entry point to the recent files functions; this gets called when
10045 * the user activates the Recently Used shortcut.
10048 recent_activate (GtkFileChooserDefault *impl)
10050 OperationMode previous_mode;
10052 if (impl->operation_mode == OPERATION_MODE_RECENT)
10055 previous_mode = impl->operation_mode;
10056 impl->operation_mode = OPERATION_MODE_RECENT;
10058 switch (previous_mode)
10060 case OPERATION_MODE_SEARCH:
10061 search_stop_searching (impl, FALSE);
10062 search_clear_model (impl, TRUE);
10064 gtk_widget_destroy (impl->search_hbox);
10065 impl->search_hbox = NULL;
10066 impl->search_entry = NULL;
10069 case OPERATION_MODE_BROWSE:
10070 stop_loading_and_clear_list_model (impl);
10073 case OPERATION_MODE_RECENT:
10074 g_assert_not_reached ();
10078 recent_hide_entry (impl);
10080 /* hide the file size column if it's visible */
10081 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
10083 file_list_set_sort_column_ids (impl);
10084 recent_start_loading (impl);
10087 /* convert an iterator coming from the model bound to
10088 * browse_files_tree_view to an interator inside the
10089 * real recent_model
10092 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10093 GtkTreeIter *child_iter,
10096 GtkTreeIter middle;
10098 if (!impl->recent_model)
10101 if (!impl->recent_model_filter || !impl->recent_model_sort)
10104 /* pass 1: get the iterator in the filter model */
10105 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10108 /* pass 2: get the iterator in the real model */
10109 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10116 set_current_filter (GtkFileChooserDefault *impl,
10117 GtkFileFilter *filter)
10119 if (impl->current_filter != filter)
10123 /* NULL filters are allowed to reset to non-filtered status
10125 filter_index = g_slist_index (impl->filters, filter);
10126 if (impl->filters && filter && filter_index < 0)
10129 if (impl->current_filter)
10130 g_object_unref (impl->current_filter);
10131 impl->current_filter = filter;
10132 if (impl->current_filter)
10134 g_object_ref_sink (impl->current_filter);
10138 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10141 if (impl->browse_files_model)
10142 install_list_model_filter (impl);
10144 if (impl->search_model_filter)
10145 gtk_tree_model_filter_refilter (impl->search_model_filter);
10147 if (impl->recent_model_filter)
10148 gtk_tree_model_filter_refilter (impl->recent_model_filter);
10150 g_object_notify (G_OBJECT (impl), "filter");
10155 filter_combo_changed (GtkComboBox *combo_box,
10156 GtkFileChooserDefault *impl)
10158 gint new_index = gtk_combo_box_get_active (combo_box);
10159 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10161 set_current_filter (impl, new_filter);
10165 check_preview_change (GtkFileChooserDefault *impl)
10167 GtkTreePath *cursor_path;
10169 const char *new_display_name;
10171 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10173 new_display_name = NULL;
10176 GtkTreeIter child_iter;
10178 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10180 if (impl->sort_model)
10183 GFileInfo *new_info;
10185 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10186 gtk_tree_path_free (cursor_path);
10188 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10190 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10191 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10193 new_display_name = g_file_info_get_display_name (new_info);
10196 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10200 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10201 &iter, cursor_path);
10202 gtk_tree_path_free (cursor_path);
10204 search_get_valid_child_iter (impl, &child_iter, &iter);
10205 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10206 SEARCH_MODEL_COL_FILE, &new_file,
10207 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10210 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10214 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10215 &iter, cursor_path);
10216 gtk_tree_path_free (cursor_path);
10218 recent_get_valid_child_iter (impl, &child_iter, &iter);
10219 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10220 RECENT_MODEL_COL_FILE, &new_file,
10221 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10226 if (new_file != impl->preview_file &&
10227 !(new_file && impl->preview_file &&
10228 g_file_equal (new_file, impl->preview_file)))
10230 if (impl->preview_file)
10232 g_object_unref (impl->preview_file);
10233 g_free (impl->preview_display_name);
10238 impl->preview_file = g_object_ref (new_file);
10239 impl->preview_display_name = g_strdup (new_display_name);
10243 impl->preview_file = NULL;
10244 impl->preview_display_name = NULL;
10247 if (impl->use_preview_label && impl->preview_label)
10248 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10250 g_signal_emit_by_name (impl, "update-preview");
10255 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10256 GtkFileSystemVolume *volume,
10257 const GError *error,
10261 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10262 GtkFileChooserDefault *impl = data;
10264 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10267 impl->shortcuts_activate_iter_cancellable = NULL;
10269 set_busy_cursor (impl, FALSE);
10276 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10280 name = _gtk_file_system_volume_get_display_name (volume);
10281 msg = g_strdup_printf (_("Could not mount %s"), name);
10283 error_message (impl, msg, error->message);
10292 file = _gtk_file_system_volume_get_root (volume);
10295 change_folder_and_display_error (impl, file, FALSE);
10296 g_object_unref (file);
10300 g_object_unref (impl);
10301 g_object_unref (cancellable);
10305 /* Activates a volume by mounting it if necessary and then switching to its
10309 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10310 GtkFileSystemVolume *volume)
10314 switch (impl->operation_mode)
10316 case OPERATION_MODE_BROWSE:
10318 case OPERATION_MODE_SEARCH:
10319 search_switch_to_browse_mode (impl);
10321 case OPERATION_MODE_RECENT:
10322 recent_switch_to_browse_mode (impl);
10326 /* We ref the file chooser since volume_mount() may run a main loop, and the
10327 * user could close the file chooser window in the meantime.
10329 g_object_ref (impl);
10331 if (!_gtk_file_system_volume_is_mounted (volume))
10333 GtkMountOperation *mount_op;
10335 set_busy_cursor (impl, TRUE);
10337 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
10338 impl->shortcuts_activate_iter_cancellable =
10339 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
10340 shortcuts_activate_volume_mount_cb,
10341 g_object_ref (impl));
10342 g_object_unref (mount_op);
10346 file = _gtk_file_system_volume_get_root (volume);
10349 change_folder_and_display_error (impl, file, FALSE);
10350 g_object_unref (file);
10354 g_object_unref (impl);
10357 /* Opens the folder or volume at the specified iter in the shortcuts model */
10358 struct ShortcutsActivateData
10360 GtkFileChooserDefault *impl;
10365 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10367 const GError *error,
10368 gpointer user_data)
10370 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10371 struct ShortcutsActivateData *data = user_data;
10373 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10376 data->impl->shortcuts_activate_iter_cancellable = NULL;
10381 if (!error && _gtk_file_info_consider_as_directory (info))
10382 change_folder_and_display_error (data->impl, data->file, FALSE);
10384 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10389 g_object_unref (data->impl);
10390 g_object_unref (data->file);
10393 g_object_unref (cancellable);
10397 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10398 GtkFileSystemVolume *volume,
10399 const GError *error,
10400 gpointer user_data)
10402 struct ShortcutsActivateData *data = user_data;
10406 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10408 g_object_unref (data->impl);
10409 g_object_unref (data->file);
10415 data->impl->shortcuts_activate_iter_cancellable =
10416 _gtk_file_system_get_info (data->impl->file_system, data->file,
10418 shortcuts_activate_get_info_cb, data);
10422 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10426 ShortcutType shortcut_type;
10428 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10429 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10430 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10431 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10433 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10434 SHORTCUTS_COL_DATA, &col_data,
10435 SHORTCUTS_COL_TYPE, &shortcut_type,
10438 if (impl->shortcuts_activate_iter_cancellable)
10440 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10441 impl->shortcuts_activate_iter_cancellable = NULL;
10444 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10446 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10448 GtkFileSystemVolume *volume;
10452 shortcuts_activate_volume (impl, volume);
10454 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10456 struct ShortcutsActivateData *data;
10457 GtkFileSystemVolume *volume;
10459 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10461 data = g_new0 (struct ShortcutsActivateData, 1);
10462 data->impl = g_object_ref (impl);
10463 data->file = g_object_ref (col_data);
10465 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10467 GMountOperation *mount_operation;
10468 GtkWidget *toplevel;
10470 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10472 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10474 impl->shortcuts_activate_iter_cancellable =
10475 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10477 shortcuts_activate_mount_enclosing_volume,
10482 impl->shortcuts_activate_iter_cancellable =
10483 _gtk_file_system_get_info (impl->file_system, data->file,
10485 shortcuts_activate_get_info_cb, data);
10488 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10490 search_activate (impl);
10492 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10494 recent_activate (impl);
10498 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10500 shortcuts_key_press_event_cb (GtkWidget *widget,
10501 GdkEventKey *event,
10502 GtkFileChooserDefault *impl)
10506 modifiers = gtk_accelerator_get_default_mod_mask ();
10508 if ((event->keyval == GDK_BackSpace
10509 || event->keyval == GDK_Delete
10510 || event->keyval == GDK_KP_Delete)
10511 && (event->state & modifiers) == 0)
10513 remove_selected_bookmarks (impl);
10517 if ((event->keyval == GDK_F2)
10518 && (event->state & modifiers) == 0)
10520 rename_selected_bookmark (impl);
10528 shortcuts_select_func (GtkTreeSelection *selection,
10529 GtkTreeModel *model,
10531 gboolean path_currently_selected,
10534 GtkFileChooserDefault *impl = data;
10535 GtkTreeIter filter_iter;
10536 ShortcutType shortcut_type;
10538 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10539 g_assert_not_reached ();
10541 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10543 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10547 list_select_func (GtkTreeSelection *selection,
10548 GtkTreeModel *model,
10550 gboolean path_currently_selected,
10553 GtkFileChooserDefault *impl = data;
10555 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10556 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10558 GtkTreeIter iter, child_iter;
10560 switch (impl->operation_mode)
10562 case OPERATION_MODE_SEARCH:
10564 gboolean is_folder;
10566 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10569 search_get_valid_child_iter (impl, &child_iter, &iter);
10570 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10571 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10578 case OPERATION_MODE_RECENT:
10580 gboolean is_folder;
10582 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10585 recent_get_valid_child_iter (impl, &child_iter, &iter);
10586 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10587 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10594 case OPERATION_MODE_BROWSE:
10598 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10601 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10602 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10603 if (info && (! _gtk_file_info_consider_as_directory (info)))
10614 list_selection_changed (GtkTreeSelection *selection,
10615 GtkFileChooserDefault *impl)
10617 /* See if we are in the new folder editable row for Save mode */
10618 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10619 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10622 gboolean had_selection;
10624 info = get_selected_file_info_from_file_list (impl, &had_selection);
10625 if (!had_selection)
10626 goto out; /* normal processing */
10629 return; /* We are on the editable row for New Folder */
10634 if (impl->location_entry)
10635 update_chooser_entry (impl);
10637 check_preview_change (impl);
10638 bookmarks_check_add_sensitivity (impl);
10640 g_signal_emit_by_name (impl, "selection-changed", 0);
10643 /* Callback used when a row in the file list is activated */
10645 list_row_activated (GtkTreeView *tree_view,
10647 GtkTreeViewColumn *column,
10648 GtkFileChooserDefault *impl)
10651 GtkTreeIter child_iter;
10653 switch (impl->operation_mode)
10655 case OPERATION_MODE_SEARCH:
10658 gboolean is_folder;
10660 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10663 search_get_valid_child_iter (impl, &child_iter, &iter);
10664 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10665 SEARCH_MODEL_COL_FILE, &file,
10666 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10671 change_folder_and_display_error (impl, file, FALSE);
10675 g_signal_emit_by_name (impl, "file-activated");
10679 case OPERATION_MODE_RECENT:
10682 gboolean is_folder;
10684 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10687 recent_get_valid_child_iter (impl, &child_iter, &iter);
10688 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10689 RECENT_MODEL_COL_FILE, &file,
10690 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10695 change_folder_and_display_error (impl, file, FALSE);
10699 g_signal_emit_by_name (impl, "file-activated");
10703 case OPERATION_MODE_BROWSE:
10707 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10710 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10711 &child_iter, &iter);
10712 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10715 if (_gtk_file_info_consider_as_directory (info))
10717 GFile *file, *target_file;
10718 const gchar *target_uri;
10720 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10721 if (g_file_info_get_file_type (info) == G_FILE_TYPE_MOUNTABLE)
10723 target_uri = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
10726 target_file = g_file_new_for_uri (target_uri);
10729 g_object_unref (file);
10730 file = target_file;
10736 change_folder_and_display_error (impl, file, FALSE);
10740 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10741 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10742 g_signal_emit_by_name (impl, "file-activated");
10749 path_bar_clicked (GtkPathBar *path_bar,
10752 gboolean child_is_hidden,
10753 GtkFileChooserDefault *impl)
10756 pending_select_files_add (impl, child_file);
10758 if (!change_folder_and_display_error (impl, file, FALSE))
10761 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10762 * show hidden files so that ".baz" appears in the file list, as it will still
10763 * be shown in the path bar: "/foo/[bar]/.baz"
10765 if (child_is_hidden)
10766 g_object_set (impl, "show-hidden", TRUE, NULL);
10770 get_list_file_info (GtkFileChooserDefault *impl,
10773 GtkTreeIter child_iter;
10775 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10779 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10783 list_icon_data_func (GtkTreeViewColumn *tree_column,
10784 GtkCellRenderer *cell,
10785 GtkTreeModel *tree_model,
10789 GtkFileChooserDefault *impl = data;
10790 GtkTreeIter child_iter;
10791 GdkPixbuf *pixbuf = NULL;
10792 gboolean sensitive = TRUE;
10794 profile_start ("start", NULL);
10796 switch (impl->operation_mode)
10798 case OPERATION_MODE_SEARCH:
10800 GtkTreeIter child_iter;
10801 gboolean is_folder;
10803 search_get_valid_child_iter (impl, &child_iter, iter);
10804 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10805 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10806 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10809 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10810 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10811 sensitive = is_folder;
10815 case OPERATION_MODE_RECENT:
10817 GtkTreeIter child_iter;
10818 GtkRecentInfo *info;
10819 gboolean is_folder;
10821 recent_get_valid_child_iter (impl, &child_iter, iter);
10822 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10823 RECENT_MODEL_COL_INFO, &info,
10824 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10827 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10829 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10830 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10831 sensitive = is_folder;
10835 case OPERATION_MODE_BROWSE:
10840 info = get_list_file_info (impl, iter);
10842 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10845 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10850 /* FIXME: NULL GError */
10851 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10856 /* We are on the editable row */
10861 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10862 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10863 sensitive = _gtk_file_info_consider_as_directory (info);
10868 g_object_set (cell,
10870 "sensitive", sensitive,
10874 g_object_unref (pixbuf);
10876 profile_end ("end", NULL);
10880 list_name_data_func (GtkTreeViewColumn *tree_column,
10881 GtkCellRenderer *cell,
10882 GtkTreeModel *tree_model,
10886 GtkFileChooserDefault *impl = data;
10888 gboolean sensitive = TRUE;
10890 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10892 GtkTreeIter child_iter;
10893 gchar *display_name;
10894 gboolean is_folder;
10896 search_get_valid_child_iter (impl, &child_iter, iter);
10897 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10898 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10899 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10902 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10903 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10905 sensitive = is_folder;
10908 g_object_set (cell,
10909 "text", display_name,
10910 "sensitive", sensitive,
10911 "ellipsize", PANGO_ELLIPSIZE_END,
10917 if (impl->operation_mode == OPERATION_MODE_RECENT)
10919 GtkTreeIter child_iter;
10920 GtkRecentInfo *recent_info;
10921 gchar *display_name;
10922 gboolean is_folder;
10924 recent_get_valid_child_iter (impl, &child_iter, iter);
10925 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10926 RECENT_MODEL_COL_INFO, &recent_info,
10927 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10930 display_name = gtk_recent_info_get_short_name (recent_info);
10932 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10933 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10935 sensitive = is_folder;
10938 g_object_set (cell,
10939 "text", display_name,
10940 "sensitive", sensitive,
10941 "ellipsize", PANGO_ELLIPSIZE_END,
10944 g_free (display_name);
10949 info = get_list_file_info (impl, iter);
10954 g_object_set (cell,
10955 "text", _("Type name of new folder"),
10957 "ellipsize", PANGO_ELLIPSIZE_NONE,
10964 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10965 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10967 sensitive = _gtk_file_info_consider_as_directory (info);
10970 g_object_set (cell,
10971 "text", g_file_info_get_display_name (info),
10972 "sensitive", sensitive,
10973 "ellipsize", PANGO_ELLIPSIZE_END,
10978 list_size_data_func (GtkTreeViewColumn *tree_column,
10979 GtkCellRenderer *cell,
10980 GtkTreeModel *tree_model,
10984 GtkFileChooserDefault *impl = data;
10988 gboolean sensitive = TRUE;
10990 if (impl->operation_mode == OPERATION_MODE_RECENT)
10993 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10995 GtkTreeIter child_iter;
10996 gboolean is_folder = FALSE;
10998 search_get_valid_child_iter (impl, &child_iter, iter);
10999 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11000 SEARCH_MODEL_COL_SIZE, &size,
11001 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11004 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11005 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11006 sensitive = is_folder ? TRUE : FALSE;
11009 str = g_format_size_for_display (size);
11013 g_object_set (cell,
11015 "sensitive", sensitive,
11023 info = get_list_file_info (impl, iter);
11025 if (!info || _gtk_file_info_consider_as_directory (info))
11027 g_object_set (cell,
11029 "sensitive", sensitive,
11034 size = g_file_info_get_size (info);
11035 str = g_format_size_for_display (size);
11037 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11038 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11041 g_object_set (cell,
11043 "sensitive", sensitive,
11044 "alignment", PANGO_ALIGN_RIGHT,
11050 /* Tree column data callback for the file list; fetches the mtime of a file */
11052 list_mtime_data_func (GtkTreeViewColumn *tree_column,
11053 GtkCellRenderer *cell,
11054 GtkTreeModel *tree_model,
11058 GtkFileChooserDefault *impl;
11059 GTimeVal timeval = { 0, };
11061 gchar *date_str = NULL;
11062 gboolean sensitive = TRUE;
11064 const char *locale, *dot = NULL;
11065 gint64 codepage = -1;
11071 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11073 GtkTreeIter child_iter;
11075 gboolean is_folder;
11077 search_get_valid_child_iter (impl, &child_iter, iter);
11078 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11079 SEARCH_MODEL_COL_MTIME, &mtime,
11080 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11083 time_mtime = (time_t) mtime;
11085 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11086 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11087 sensitive = is_folder;
11089 else if (impl->operation_mode == OPERATION_MODE_RECENT)
11091 GtkTreeIter child_iter;
11092 GtkRecentInfo *info;
11093 gboolean is_folder;
11095 recent_get_valid_child_iter (impl, &child_iter, iter);
11096 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11097 RECENT_MODEL_COL_INFO, &info,
11098 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11102 time_mtime = gtk_recent_info_get_modified (info);
11106 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11107 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11108 sensitive = is_folder;
11114 info = get_list_file_info (impl, iter);
11117 g_object_set (cell,
11124 g_file_info_get_modification_time (info, &timeval);
11125 time_mtime = timeval.tv_sec;
11127 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11128 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11129 sensitive = _gtk_file_info_consider_as_directory (info);
11132 if (G_UNLIKELY (time_mtime == 0))
11133 date_str = g_strdup (_("Unknown"));
11138 struct tm tm_mtime;
11140 const gchar *format;
11141 gchar *locale_format = NULL;
11144 #ifdef HAVE_LOCALTIME_R
11145 localtime_r ((time_t *) &time_mtime, &tm_mtime);
11148 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
11152 g_warning ("ptm != NULL failed");
11154 g_object_set (cell,
11155 "text", _("Unknown"),
11156 "sensitive", sensitive,
11161 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11163 #endif /* HAVE_LOCALTIME_R */
11165 g_date_set_time_t (&mtime, time_mtime);
11166 time_now = time (NULL);
11167 g_date_set_time_t (&now, time_now);
11169 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11171 /* Translators: %H means "hours" and %M means "minutes" */
11172 if (days_diff == 0)
11173 format = _("%H:%M");
11174 else if (days_diff == 1)
11175 format = _("Yesterday at %H:%M");
11178 if (days_diff > 1 && days_diff < 7)
11179 format = "%A"; /* Days from last week */
11181 format = "%x"; /* Any other date */
11185 /* g_locale_from_utf8() returns a string in the system
11186 * code-page, which is not always the same as that used by the C
11187 * library. For instance when running a GTK+ program with
11188 * LANG=ko on an English version of Windows, the system
11189 * code-page is 1252, but the code-page used by the C library is
11190 * 949. (It's GTK+ itself that sets the C library locale when it
11191 * notices the LANG environment variable. See gtkmain.c The
11192 * Microsoft C library doesn't look at any locale environment
11193 * variables.) We need to pass strftime() a string in the C
11194 * library's code-page. See bug #509885.
11196 locale = setlocale (LC_ALL, NULL);
11197 if (locale != NULL)
11198 dot = strchr (locale, '.');
11201 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11203 /* All codepages should fit in 16 bits AFAIK */
11204 if (codepage > 0 && codepage < 65536)
11206 sprintf (charset, "CP%u", (guint) codepage);
11207 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11211 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11213 if (locale_format != NULL &&
11214 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11217 /* As above but in opposite direction... */
11218 if (codepage > 0 && codepage < 65536)
11219 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11221 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11225 if (date_str == NULL)
11226 date_str = g_strdup (_("Unknown"));
11228 g_free (locale_format);
11231 g_object_set (cell,
11233 "sensitive", sensitive,
11239 _gtk_file_chooser_default_new (void)
11241 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11245 location_set_user_text (GtkFileChooserDefault *impl,
11248 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11249 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11253 location_popup_handler (GtkFileChooserDefault *impl,
11256 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11258 GtkWidget *widget_to_focus;
11260 /* This will give us the location widgets back */
11261 switch (impl->operation_mode)
11263 case OPERATION_MODE_SEARCH:
11264 search_switch_to_browse_mode (impl);
11266 case OPERATION_MODE_RECENT:
11267 recent_switch_to_browse_mode (impl);
11269 case OPERATION_MODE_BROWSE:
11270 g_assert_not_reached ();
11274 if (impl->current_folder)
11275 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11277 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11278 widget_to_focus = impl->browse_files_tree_view;
11280 widget_to_focus = impl->location_entry;
11282 gtk_widget_grab_focus (widget_to_focus);
11286 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11287 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11289 LocationMode new_mode;
11293 /* since the user typed something, we unconditionally want to turn on the entry */
11294 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11296 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11297 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11298 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11299 new_mode = LOCATION_MODE_PATH_BAR;
11302 g_assert_not_reached ();
11306 location_mode_set (impl, new_mode, TRUE);
11307 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11310 location_set_user_text (impl, path);
11313 location_entry_set_initial_text (impl);
11314 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11318 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11319 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11321 gtk_widget_grab_focus (impl->location_entry);
11323 location_set_user_text (impl, path);
11326 g_assert_not_reached ();
11329 /* Handler for the "up-folder" keybinding signal */
11331 up_folder_handler (GtkFileChooserDefault *impl)
11333 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11336 /* Handler for the "down-folder" keybinding signal */
11338 down_folder_handler (GtkFileChooserDefault *impl)
11340 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11343 /* Switches to the shortcut in the specified index */
11345 switch_to_shortcut (GtkFileChooserDefault *impl,
11350 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11351 g_assert_not_reached ();
11353 shortcuts_activate_iter (impl, &iter);
11356 /* Handler for the "home-folder" keybinding signal */
11358 home_folder_handler (GtkFileChooserDefault *impl)
11360 if (impl->has_home)
11361 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11364 /* Handler for the "desktop-folder" keybinding signal */
11366 desktop_folder_handler (GtkFileChooserDefault *impl)
11368 if (impl->has_desktop)
11369 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11372 /* Handler for the "search-shortcut" keybinding signal */
11374 search_shortcut_handler (GtkFileChooserDefault *impl)
11376 if (impl->has_search)
11378 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11380 /* we want the entry widget to grab the focus the first
11381 * time, not the browse_files_tree_view widget.
11383 if (impl->search_entry)
11384 gtk_widget_grab_focus (impl->search_entry);
11388 /* Handler for the "recent-shortcut" keybinding signal */
11390 recent_shortcut_handler (GtkFileChooserDefault *impl)
11392 if (impl->has_recent)
11393 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11397 quick_bookmark_handler (GtkFileChooserDefault *impl,
11398 gint bookmark_index)
11403 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11406 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11408 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11409 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11412 gtk_tree_path_free (path);
11414 switch_to_shortcut (impl, bookmark_pos);
11418 show_hidden_handler (GtkFileChooserDefault *impl)
11420 g_object_set (impl,
11421 "show-hidden", !impl->show_hidden,
11426 /* Drag and drop interfaces */
11429 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11434 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11436 model->impl = NULL;
11439 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11441 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11444 ShortcutsPaneModelFilter *model;
11448 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11450 pos = *gtk_tree_path_get_indices (path);
11451 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11453 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11456 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11458 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11460 GtkSelectionData *selection_data)
11462 ShortcutsPaneModelFilter *model;
11464 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11471 /* Fill the GtkTreeDragSourceIface vtable */
11473 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11475 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11476 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11480 /* Fill the GtkTreeDragDestIface vtable */
11482 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11484 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11485 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11489 static GtkTreeModel *
11490 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11491 GtkTreeModel *child_model,
11494 ShortcutsPaneModelFilter *model;
11496 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11497 "child-model", child_model,
11498 "virtual-root", root,
11501 model->impl = impl;
11503 return GTK_TREE_MODEL (model);
11509 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11512 RecentModelSort *model;
11513 GtkTreeIter iter, child_iter;
11514 gboolean is_folder;
11516 model = RECENT_MODEL_SORT (drag_source);
11517 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11520 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11521 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11522 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11529 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11531 GtkSelectionData *selection_data)
11533 RecentModelSort *model;
11534 GtkTreeIter iter, child_iter;
11538 model = RECENT_MODEL_SORT (drag_source);
11539 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11542 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11543 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11544 RECENT_MODEL_COL_FILE, &file,
11546 g_assert (file != NULL);
11548 uris[0] = g_file_get_uri (file);
11551 gtk_selection_data_set_uris (selection_data, uris);
11559 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11561 iface->row_draggable = recent_model_sort_row_draggable;
11562 iface->drag_data_get = recent_model_sort_drag_data_get;
11566 _recent_model_sort_class_init (RecentModelSortClass *klass)
11572 _recent_model_sort_init (RecentModelSort *model)
11574 model->impl = NULL;
11577 static GtkTreeModel *
11578 recent_model_sort_new (GtkFileChooserDefault *impl,
11579 GtkTreeModel *child_model)
11581 RecentModelSort *model;
11583 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11584 "model", child_model,
11586 model->impl = impl;
11588 return GTK_TREE_MODEL (model);
11594 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11597 SearchModelSort *model;
11598 GtkTreeIter iter, child_iter;
11599 gboolean is_folder;
11601 model = SEARCH_MODEL_SORT (drag_source);
11602 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11605 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11606 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11607 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11614 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11616 GtkSelectionData *selection_data)
11618 SearchModelSort *model;
11619 GtkTreeIter iter, child_iter;
11623 model = SEARCH_MODEL_SORT (drag_source);
11624 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11627 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11628 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11629 RECENT_MODEL_COL_FILE, &file,
11631 g_assert (file != NULL);
11633 uris[0] = g_file_get_uri (file);
11636 gtk_selection_data_set_uris (selection_data, uris);
11644 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11646 iface->row_draggable = search_model_sort_row_draggable;
11647 iface->drag_data_get = search_model_sort_drag_data_get;
11651 _search_model_sort_class_init (SearchModelSortClass *klass)
11657 _search_model_sort_init (SearchModelSort *model)
11659 model->impl = NULL;
11662 static GtkTreeModel *
11663 search_model_sort_new (GtkFileChooserDefault *impl,
11664 GtkTreeModel *child_model)
11666 SearchModelSort *model;
11668 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11669 "model", child_model,
11671 model->impl = impl;
11673 return GTK_TREE_MODEL (model);