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 "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystem.h"
44 #include "gtkfilesystemmodel.h"
47 #include "gtkhpaned.h"
48 #include "gtkiconfactory.h"
49 #include "gtkicontheme.h"
51 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkmountoperation.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkrecentfilter.h"
60 #include "gtkrecentmanager.h"
61 #include "gtkscrolledwindow.h"
62 #include "gtkseparatormenuitem.h"
63 #include "gtksizegroup.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
195 /* the first 3 must be these due to settings caching sort column */
200 MODEL_COL_NAME_COLLATED,
204 MODEL_COL_MTIME_TEXT,
206 MODEL_COL_NUM_COLUMNS
209 /* Column numbers for the search model.
210 * Keep this in sync with search_setup_model()
213 SEARCH_MODEL_COL_CANCELLABLE = MODEL_COL_NUM_COLUMNS,
214 SEARCH_MODEL_COL_MIME_TYPE,
215 SEARCH_MODEL_COL_NUM_COLUMNS
219 RECENT_MODEL_COL_CANCELLABLE = MODEL_COL_NUM_COLUMNS,
220 RECENT_MODEL_COL_INFO,
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_realize (GtkWidget *widget);
278 static void gtk_file_chooser_default_map (GtkWidget *widget);
279 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
280 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
281 GtkWidget *previous_toplevel);
282 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
283 GtkStyle *previous_style);
284 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
285 GdkScreen *previous_screen);
286 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
287 GtkAllocation *allocation);
289 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
292 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
295 gboolean clear_entry,
297 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
298 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
300 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
305 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
306 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
307 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
308 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
309 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
310 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
313 GtkFileFilter *filter);
314 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
315 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
318 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
321 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
323 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
325 gint *default_height);
326 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
327 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
329 static void location_popup_handler (GtkFileChooserDefault *impl,
331 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
332 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
333 static void up_folder_handler (GtkFileChooserDefault *impl);
334 static void down_folder_handler (GtkFileChooserDefault *impl);
335 static void home_folder_handler (GtkFileChooserDefault *impl);
336 static void desktop_folder_handler (GtkFileChooserDefault *impl);
337 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
338 gint bookmark_index);
339 static void show_hidden_handler (GtkFileChooserDefault *impl);
340 static void search_shortcut_handler (GtkFileChooserDefault *impl);
341 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
342 static void update_appearance (GtkFileChooserDefault *impl);
344 static void set_current_filter (GtkFileChooserDefault *impl,
345 GtkFileFilter *filter);
346 static void check_preview_change (GtkFileChooserDefault *impl);
348 static void filter_combo_changed (GtkComboBox *combo_box,
349 GtkFileChooserDefault *impl);
351 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
353 GtkFileChooserDefault *impl);
355 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
358 gboolean path_currently_selected,
360 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
362 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
364 static int shortcuts_get_index (GtkFileChooserDefault *impl,
365 ShortcutsIndex where);
366 static int shortcut_find_position (GtkFileChooserDefault *impl,
369 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
371 static gboolean list_select_func (GtkTreeSelection *selection,
374 gboolean path_currently_selected,
377 static void list_selection_changed (GtkTreeSelection *tree_selection,
378 GtkFileChooserDefault *impl);
379 static void list_row_activated (GtkTreeView *tree_view,
381 GtkTreeViewColumn *column,
382 GtkFileChooserDefault *impl);
384 static void select_func (GtkFileSystemModel *model,
389 static void path_bar_clicked (GtkPathBar *path_bar,
392 gboolean child_is_hidden,
393 GtkFileChooserDefault *impl);
395 static void add_bookmark_button_clicked_cb (GtkButton *button,
396 GtkFileChooserDefault *impl);
397 static void remove_bookmark_button_clicked_cb (GtkButton *button,
398 GtkFileChooserDefault *impl);
399 static void save_folder_combo_changed_cb (GtkComboBox *combo,
400 GtkFileChooserDefault *impl);
402 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
403 GtkCellRenderer *cell,
404 GtkTreeModel *tree_model,
407 static void list_name_data_func (GtkTreeViewColumn *tree_column,
408 GtkCellRenderer *cell,
409 GtkTreeModel *tree_model,
412 static void list_size_data_func (GtkTreeViewColumn *tree_column,
413 GtkCellRenderer *cell,
414 GtkTreeModel *tree_model,
417 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
418 GtkCellRenderer *cell,
419 GtkTreeModel *tree_model,
423 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
426 static void load_remove_timer (GtkFileChooserDefault *impl);
427 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
429 static void location_button_toggled_cb (GtkToggleButton *toggle,
430 GtkFileChooserDefault *impl);
431 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
433 static void search_stop_searching (GtkFileChooserDefault *impl,
434 gboolean remove_query);
435 static void search_clear_model_row (GtkTreeModel *model,
437 static void search_clear_model (GtkFileChooserDefault *impl,
438 gboolean remove_from_treeview);
439 static gboolean search_should_respond (GtkFileChooserDefault *impl);
440 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
441 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
442 static void search_entry_activate_cb (GtkEntry *entry,
444 static void settings_load (GtkFileChooserDefault *impl);
445 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
446 GtkTreeIter *child_iter,
449 static void recent_stop_loading (GtkFileChooserDefault *impl);
450 static void recent_clear_model (GtkFileChooserDefault *impl,
451 gboolean remove_from_treeview);
452 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
453 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
454 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
455 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
456 GtkTreeIter *child_iter,
458 static void set_file_system_backend (GtkFileChooserDefault *impl);
459 static void unset_file_system_backend (GtkFileChooserDefault *impl);
465 /* Drag and drop interface declarations */
468 GtkTreeModelFilter parent;
470 GtkFileChooserDefault *impl;
471 } ShortcutsPaneModelFilter;
474 GtkTreeModelFilterClass parent_class;
475 } ShortcutsPaneModelFilterClass;
477 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
478 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
480 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
482 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
483 _shortcuts_pane_model_filter,
484 GTK_TYPE_TREE_MODEL_FILTER,
485 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
486 shortcuts_pane_model_filter_drag_source_iface_init))
488 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
489 GtkTreeModel *child_model,
494 GtkTreeModelSort parent;
496 GtkFileChooserDefault *impl;
500 GtkTreeModelSortClass parent_class;
501 } RecentModelSortClass;
503 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
504 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
506 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
508 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
510 GTK_TYPE_TREE_MODEL_SORT,
511 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
512 recent_model_sort_drag_source_iface_init));
514 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
515 GtkTreeModel *child_model);
519 GtkTreeModelSort parent;
521 GtkFileChooserDefault *impl;
525 GtkTreeModelSortClass parent_class;
526 } SearchModelSortClass;
528 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
529 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
531 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
533 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
535 GTK_TYPE_TREE_MODEL_SORT,
536 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
537 search_model_sort_drag_source_iface_init));
539 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
540 GtkTreeModel *child_model);
544 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
545 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
546 gtk_file_chooser_default_iface_init)
547 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
548 gtk_file_chooser_embed_default_iface_init));
551 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
553 static const guint quick_bookmark_keyvals[10] = {
554 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
556 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
557 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
558 GtkBindingSet *binding_set;
561 gobject_class->finalize = gtk_file_chooser_default_finalize;
562 gobject_class->constructor = gtk_file_chooser_default_constructor;
563 gobject_class->set_property = gtk_file_chooser_default_set_property;
564 gobject_class->get_property = gtk_file_chooser_default_get_property;
565 gobject_class->dispose = gtk_file_chooser_default_dispose;
567 widget_class->show_all = gtk_file_chooser_default_show_all;
568 widget_class->realize = gtk_file_chooser_default_realize;
569 widget_class->map = gtk_file_chooser_default_map;
570 widget_class->unmap = gtk_file_chooser_default_unmap;
571 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
572 widget_class->style_set = gtk_file_chooser_default_style_set;
573 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
574 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
576 signals[LOCATION_POPUP] =
577 g_signal_new_class_handler (I_("location-popup"),
578 G_OBJECT_CLASS_TYPE (class),
579 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
580 G_CALLBACK (location_popup_handler),
582 _gtk_marshal_VOID__STRING,
583 G_TYPE_NONE, 1, G_TYPE_STRING);
585 signals[LOCATION_POPUP_ON_PASTE] =
586 g_signal_new_class_handler (I_("location-popup-on-paste"),
587 G_OBJECT_CLASS_TYPE (class),
588 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
589 G_CALLBACK (location_popup_on_paste_handler),
591 _gtk_marshal_VOID__VOID,
594 signals[LOCATION_TOGGLE_POPUP] =
595 g_signal_new_class_handler (I_("location-toggle-popup"),
596 G_OBJECT_CLASS_TYPE (class),
597 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
598 G_CALLBACK (location_toggle_popup_handler),
600 _gtk_marshal_VOID__VOID,
604 g_signal_new_class_handler (I_("up-folder"),
605 G_OBJECT_CLASS_TYPE (class),
606 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
607 G_CALLBACK (up_folder_handler),
609 _gtk_marshal_VOID__VOID,
612 signals[DOWN_FOLDER] =
613 g_signal_new_class_handler (I_("down-folder"),
614 G_OBJECT_CLASS_TYPE (class),
615 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
616 G_CALLBACK (down_folder_handler),
618 _gtk_marshal_VOID__VOID,
621 signals[HOME_FOLDER] =
622 g_signal_new_class_handler (I_("home-folder"),
623 G_OBJECT_CLASS_TYPE (class),
624 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
625 G_CALLBACK (home_folder_handler),
627 _gtk_marshal_VOID__VOID,
630 signals[DESKTOP_FOLDER] =
631 g_signal_new_class_handler (I_("desktop-folder"),
632 G_OBJECT_CLASS_TYPE (class),
633 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
634 G_CALLBACK (desktop_folder_handler),
636 _gtk_marshal_VOID__VOID,
639 signals[QUICK_BOOKMARK] =
640 g_signal_new_class_handler (I_("quick-bookmark"),
641 G_OBJECT_CLASS_TYPE (class),
642 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
643 G_CALLBACK (quick_bookmark_handler),
645 _gtk_marshal_VOID__INT,
646 G_TYPE_NONE, 1, G_TYPE_INT);
648 signals[SHOW_HIDDEN] =
649 g_signal_new_class_handler (I_("show-hidden"),
650 G_OBJECT_CLASS_TYPE (class),
651 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
652 G_CALLBACK (show_hidden_handler),
654 _gtk_marshal_VOID__VOID,
657 signals[SEARCH_SHORTCUT] =
658 g_signal_new_class_handler (I_("search-shortcut"),
659 G_OBJECT_CLASS_TYPE (class),
660 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
661 G_CALLBACK (search_shortcut_handler),
663 _gtk_marshal_VOID__VOID,
666 signals[RECENT_SHORTCUT] =
667 g_signal_new_class_handler (I_("recent-shortcut"),
668 G_OBJECT_CLASS_TYPE (class),
669 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
670 G_CALLBACK (recent_shortcut_handler),
672 _gtk_marshal_VOID__VOID,
675 binding_set = gtk_binding_set_by_class (class);
677 gtk_binding_entry_add_signal (binding_set,
678 GDK_l, GDK_CONTROL_MASK,
679 "location-toggle-popup",
682 gtk_binding_entry_add_signal (binding_set,
685 1, G_TYPE_STRING, "/");
686 gtk_binding_entry_add_signal (binding_set,
689 1, G_TYPE_STRING, "/");
692 gtk_binding_entry_add_signal (binding_set,
695 1, G_TYPE_STRING, "~");
698 gtk_binding_entry_add_signal (binding_set,
699 GDK_v, GDK_CONTROL_MASK,
700 "location-popup-on-paste",
702 gtk_binding_entry_add_signal (binding_set,
703 GDK_Up, GDK_MOD1_MASK,
706 gtk_binding_entry_add_signal (binding_set,
710 gtk_binding_entry_add_signal (binding_set,
711 GDK_KP_Up, GDK_MOD1_MASK,
715 gtk_binding_entry_add_signal (binding_set,
716 GDK_Down, GDK_MOD1_MASK,
719 gtk_binding_entry_add_signal (binding_set,
720 GDK_KP_Down, GDK_MOD1_MASK,
724 gtk_binding_entry_add_signal (binding_set,
725 GDK_Home, GDK_MOD1_MASK,
728 gtk_binding_entry_add_signal (binding_set,
729 GDK_KP_Home, GDK_MOD1_MASK,
732 gtk_binding_entry_add_signal (binding_set,
733 GDK_d, GDK_MOD1_MASK,
736 gtk_binding_entry_add_signal (binding_set,
737 GDK_h, GDK_CONTROL_MASK,
740 gtk_binding_entry_add_signal (binding_set,
741 GDK_s, GDK_MOD1_MASK,
744 gtk_binding_entry_add_signal (binding_set,
745 GDK_r, GDK_MOD1_MASK,
749 for (i = 0; i < 10; i++)
750 gtk_binding_entry_add_signal (binding_set,
751 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
755 _gtk_file_chooser_install_properties (gobject_class);
759 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
761 iface->select_file = gtk_file_chooser_default_select_file;
762 iface->unselect_file = gtk_file_chooser_default_unselect_file;
763 iface->select_all = gtk_file_chooser_default_select_all;
764 iface->unselect_all = gtk_file_chooser_default_unselect_all;
765 iface->get_files = gtk_file_chooser_default_get_files;
766 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
767 iface->get_file_system = gtk_file_chooser_default_get_file_system;
768 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
769 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
770 iface->set_current_name = gtk_file_chooser_default_set_current_name;
771 iface->add_filter = gtk_file_chooser_default_add_filter;
772 iface->remove_filter = gtk_file_chooser_default_remove_filter;
773 iface->list_filters = gtk_file_chooser_default_list_filters;
774 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
775 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
776 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
780 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
782 iface->get_default_size = gtk_file_chooser_default_get_default_size;
783 iface->should_respond = gtk_file_chooser_default_should_respond;
784 iface->initial_focus = gtk_file_chooser_default_initial_focus;
788 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
790 profile_start ("start", NULL);
791 #ifdef PROFILE_FILE_CHOOSER
792 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
794 impl->local_only = TRUE;
795 impl->preview_widget_active = TRUE;
796 impl->use_preview_label = TRUE;
797 impl->select_multiple = FALSE;
798 impl->show_hidden = FALSE;
799 impl->show_size_column = TRUE;
800 impl->icon_size = FALLBACK_ICON_SIZE;
801 impl->load_state = LOAD_EMPTY;
802 impl->reload_state = RELOAD_EMPTY;
803 impl->pending_select_files = NULL;
804 impl->location_mode = LOCATION_MODE_PATH_BAR;
805 impl->operation_mode = OPERATION_MODE_BROWSE;
806 impl->sort_column = MODEL_COL_NAME;
807 impl->sort_order = GTK_SORT_ASCENDING;
808 impl->recent_manager = gtk_recent_manager_get_default ();
809 impl->create_folders = TRUE;
811 gtk_box_set_spacing (GTK_BOX (impl), 12);
813 set_file_system_backend (impl);
815 profile_end ("end", NULL);
818 /* Frees the data columns for the specified iter in the shortcuts model*/
820 shortcuts_free_row_data (GtkFileChooserDefault *impl,
824 ShortcutType shortcut_type;
825 GCancellable *cancellable;
827 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
828 SHORTCUTS_COL_DATA, &col_data,
829 SHORTCUTS_COL_TYPE, &shortcut_type,
830 SHORTCUTS_COL_CANCELLABLE, &cancellable,
834 g_cancellable_cancel (cancellable);
836 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
837 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
841 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
843 GtkFileSystemVolume *volume;
846 _gtk_file_system_volume_free (volume);
852 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
855 g_object_unref (file);
859 /* Frees all the data columns in the shortcuts model */
861 shortcuts_free (GtkFileChooserDefault *impl)
865 if (!impl->shortcuts_model)
868 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
871 shortcuts_free_row_data (impl, &iter);
873 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
875 g_object_unref (impl->shortcuts_model);
876 impl->shortcuts_model = NULL;
880 pending_select_files_free (GtkFileChooserDefault *impl)
882 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
883 g_slist_free (impl->pending_select_files);
884 impl->pending_select_files = NULL;
888 pending_select_files_add (GtkFileChooserDefault *impl,
891 impl->pending_select_files =
892 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
895 /* Used from gtk_tree_selection_selected_foreach() */
897 store_selection_foreach (GtkTreeModel *model,
902 GtkFileChooserDefault *impl;
903 GtkTreeIter child_iter;
906 impl = GTK_FILE_CHOOSER_DEFAULT (data);
908 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
910 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
911 pending_select_files_add (impl, file);
914 /* Stores the current selection in the list of paths to select; this is used to
915 * preserve the selection when reloading the current folder.
918 pending_select_files_store_selection (GtkFileChooserDefault *impl)
920 GtkTreeSelection *selection;
922 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
923 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
927 gtk_file_chooser_default_finalize (GObject *object)
929 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
932 unset_file_system_backend (impl);
934 if (impl->shortcuts_pane_filter_model)
935 g_object_unref (impl->shortcuts_pane_filter_model);
937 if (impl->shortcuts_combo_filter_model)
938 g_object_unref (impl->shortcuts_combo_filter_model);
940 shortcuts_free (impl);
942 g_free (impl->browse_files_last_selected_name);
944 for (l = impl->filters; l; l = l->next)
946 GtkFileFilter *filter;
948 filter = GTK_FILE_FILTER (l->data);
949 g_object_unref (filter);
951 g_slist_free (impl->filters);
953 if (impl->current_filter)
954 g_object_unref (impl->current_filter);
956 if (impl->current_volume_file)
957 g_object_unref (impl->current_volume_file);
959 if (impl->current_folder)
960 g_object_unref (impl->current_folder);
962 if (impl->preview_file)
963 g_object_unref (impl->preview_file);
965 if (impl->browse_path_bar_size_group)
966 g_object_unref (impl->browse_path_bar_size_group);
968 load_remove_timer (impl);
970 /* Free all the Models we have */
971 if (impl->browse_files_model)
972 g_object_unref (impl->browse_files_model);
974 if (impl->sort_model)
975 g_object_unref (impl->sort_model);
977 search_clear_model (impl, FALSE);
978 recent_clear_model (impl, FALSE);
980 g_free (impl->preview_display_name);
982 g_free (impl->edited_new_text);
984 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
987 /* Shows an error dialog set as transient for the specified window */
989 error_message_with_parent (GtkWindow *parent,
995 dialog = gtk_message_dialog_new (parent,
996 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1001 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1004 if (parent && parent->group)
1005 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1007 gtk_dialog_run (GTK_DIALOG (dialog));
1008 gtk_widget_destroy (dialog);
1011 /* Returns a toplevel GtkWindow, or NULL if none */
1013 get_toplevel (GtkWidget *widget)
1015 GtkWidget *toplevel;
1017 toplevel = gtk_widget_get_toplevel (widget);
1018 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1021 return GTK_WINDOW (toplevel);
1024 /* Shows an error dialog for the file chooser */
1026 error_message (GtkFileChooserDefault *impl,
1030 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1033 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1035 error_dialog (GtkFileChooserDefault *impl,
1046 uri = g_file_get_uri (file);
1047 text = g_strdup_printf (msg, uri);
1048 error_message (impl, text, error->message);
1051 g_error_free (error);
1055 /* Displays an error message about not being able to get information for a file.
1056 * Frees the GError as well.
1059 error_getting_info_dialog (GtkFileChooserDefault *impl,
1064 _("Could not retrieve information about the file"),
1068 /* Shows an error dialog about not being able to add a bookmark */
1070 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1075 _("Could not add a bookmark"),
1079 /* Shows an error dialog about not being able to remove a bookmark */
1081 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1086 _("Could not remove bookmark"),
1090 /* Shows an error dialog about not being able to create a folder */
1092 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1097 _("The folder could not be created"),
1101 /* Shows an error about not being able to create a folder because a file with
1102 * the same name is already there.
1105 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1110 _("The folder could not be created, as a file with the same "
1111 "name already exists. Try using a different name for the "
1112 "folder, or rename the file first."),
1116 /* Shows an error dialog about not being able to create a filename */
1118 error_building_filename_dialog (GtkFileChooserDefault *impl,
1121 error_dialog (impl, _("Invalid file name"),
1125 /* Shows an error dialog when we cannot switch to a folder */
1127 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1131 error_dialog (impl, _("The folder contents could not be displayed"),
1135 /* Changes folders, displaying an error dialog if this fails */
1137 change_folder_and_display_error (GtkFileChooserDefault *impl,
1139 gboolean clear_entry)
1144 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1146 /* We copy the path because of this case:
1148 * list_row_activated()
1149 * fetches path from model; path belongs to the model (*)
1150 * calls change_folder_and_display_error()
1151 * calls gtk_file_chooser_set_current_folder_file()
1152 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1156 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1159 error_changing_folder_dialog (impl, file, error);
1165 emit_default_size_changed (GtkFileChooserDefault *impl)
1167 profile_msg (" emit default-size-changed start", NULL);
1168 g_signal_emit_by_name (impl, "default-size-changed");
1169 profile_msg (" emit default-size-changed end", NULL);
1173 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1175 if (impl->use_preview_label)
1177 if (!impl->preview_label)
1179 impl->preview_label = gtk_label_new (impl->preview_display_name);
1180 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1181 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1182 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1183 gtk_widget_show (impl->preview_label);
1188 if (impl->preview_label)
1190 gtk_widget_destroy (impl->preview_label);
1191 impl->preview_label = NULL;
1195 if (impl->preview_widget_active && impl->preview_widget)
1196 gtk_widget_show (impl->preview_box);
1198 gtk_widget_hide (impl->preview_box);
1200 if (!GTK_WIDGET_MAPPED (impl))
1201 emit_default_size_changed (impl);
1205 set_preview_widget (GtkFileChooserDefault *impl,
1206 GtkWidget *preview_widget)
1208 if (preview_widget == impl->preview_widget)
1211 if (impl->preview_widget)
1212 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1213 impl->preview_widget);
1215 impl->preview_widget = preview_widget;
1216 if (impl->preview_widget)
1218 gtk_widget_show (impl->preview_widget);
1219 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1220 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1221 impl->preview_widget,
1222 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1225 update_preview_widget_visibility (impl);
1228 /* Renders a "Search" icon at an appropriate size for a tree view */
1230 render_search_icon (GtkFileChooserDefault *impl)
1232 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1236 render_recent_icon (GtkFileChooserDefault *impl)
1238 GtkIconTheme *theme;
1241 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1242 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1244 theme = gtk_icon_theme_get_default ();
1246 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1252 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1258 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1259 struct ReloadIconsData
1261 GtkFileChooserDefault *impl;
1262 GtkTreeRowReference *row_ref;
1266 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1268 const GError *error,
1274 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1275 struct ReloadIconsData *data = user_data;
1277 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1280 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1282 if (cancelled || error)
1285 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1287 path = gtk_tree_row_reference_get_path (data->row_ref);
1288 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1289 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1290 SHORTCUTS_COL_PIXBUF, pixbuf,
1292 gtk_tree_path_free (path);
1295 g_object_unref (pixbuf);
1298 gtk_tree_row_reference_free (data->row_ref);
1299 g_object_unref (data->impl);
1302 g_object_unref (cancellable);
1306 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1311 profile_start ("start", NULL);
1313 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1316 for (l = impl->reload_icon_cancellables; l; l = l->next)
1318 GCancellable *cancellable = G_CANCELLABLE (l->data);
1319 g_cancellable_cancel (cancellable);
1321 g_slist_free (impl->reload_icon_cancellables);
1322 impl->reload_icon_cancellables = NULL;
1327 ShortcutType shortcut_type;
1328 gboolean pixbuf_visible;
1331 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1332 SHORTCUTS_COL_DATA, &data,
1333 SHORTCUTS_COL_TYPE, &shortcut_type,
1334 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1340 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1342 GtkFileSystemVolume *volume;
1345 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1346 impl->icon_size, NULL);
1348 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1350 if (g_file_is_native (G_FILE (data)))
1353 struct ReloadIconsData *info;
1354 GtkTreePath *tree_path;
1355 GCancellable *cancellable;
1359 info = g_new0 (struct ReloadIconsData, 1);
1360 info->impl = g_object_ref (impl);
1361 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1362 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1363 gtk_tree_path_free (tree_path);
1365 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1367 shortcuts_reload_icons_get_info_cb,
1369 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1373 GtkIconTheme *icon_theme;
1375 /* Don't call get_info for remote paths to avoid latency and
1377 * If we switch to a better bookmarks file format (XBEL), we
1378 * should use mime info to get a better icon.
1380 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1381 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1382 impl->icon_size, 0, NULL);
1385 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1387 pixbuf = render_search_icon (impl);
1389 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1391 pixbuf = render_recent_icon (impl);
1394 gtk_list_store_set (impl->shortcuts_model, &iter,
1395 SHORTCUTS_COL_PIXBUF, pixbuf,
1399 g_object_unref (pixbuf);
1403 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1407 profile_end ("end", NULL);
1411 shortcuts_find_folder (GtkFileChooserDefault *impl,
1414 GtkTreeSelection *selection;
1418 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1420 g_assert (folder != NULL);
1421 pos = shortcut_find_position (impl, folder);
1424 gtk_tree_selection_unselect_all (selection);
1428 path = gtk_tree_path_new_from_indices (pos, -1);
1429 gtk_tree_selection_select_path (selection, path);
1430 gtk_tree_path_free (path);
1433 /* If a shortcut corresponds to the current folder, selects it */
1435 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1437 shortcuts_find_folder (impl, impl->current_folder);
1440 /* Removes the specified number of rows from the shortcuts list */
1442 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1448 path = gtk_tree_path_new_from_indices (start_row, -1);
1450 for (; n_rows; n_rows--)
1454 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1455 g_assert_not_reached ();
1457 shortcuts_free_row_data (impl, &iter);
1458 gtk_list_store_remove (impl->shortcuts_model, &iter);
1461 gtk_tree_path_free (path);
1465 shortcuts_update_count (GtkFileChooserDefault *impl,
1466 ShortcutsIndex type,
1471 case SHORTCUTS_HOME:
1473 impl->has_home = FALSE;
1475 impl->has_home = TRUE;
1478 case SHORTCUTS_DESKTOP:
1480 impl->has_desktop = FALSE;
1482 impl->has_desktop = TRUE;
1485 case SHORTCUTS_VOLUMES:
1486 impl->num_volumes += value;
1489 case SHORTCUTS_SHORTCUTS:
1490 impl->num_shortcuts += value;
1493 case SHORTCUTS_BOOKMARKS:
1494 impl->num_bookmarks += value;
1497 case SHORTCUTS_CURRENT_FOLDER:
1499 impl->shortcuts_current_folder_active = FALSE;
1501 impl->shortcuts_current_folder_active = TRUE;
1510 struct ShortcutsInsertRequest
1512 GtkFileChooserDefault *impl;
1516 GtkTreeRowReference *row_ref;
1517 ShortcutsIndex type;
1523 get_file_info_finished (GCancellable *cancellable,
1525 const GError *error,
1529 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1533 GCancellable *model_cancellable;
1534 struct ShortcutsInsertRequest *request = data;
1536 path = gtk_tree_row_reference_get_path (request->row_ref);
1538 /* Handle doesn't exist anymore in the model */
1541 pos = gtk_tree_path_get_indices (path)[0];
1542 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1544 gtk_tree_path_free (path);
1546 /* validate cancellable, else goto out */
1547 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1548 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1550 if (cancellable != model_cancellable)
1553 /* set the cancellable to NULL in the model (we unref later on) */
1554 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1555 SHORTCUTS_COL_CANCELLABLE, NULL,
1563 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1564 shortcuts_update_count (request->impl, request->type, -1);
1566 if (request->type == SHORTCUTS_HOME)
1570 home = g_file_new_for_path (g_get_home_dir ());
1571 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1572 g_object_unref (home);
1574 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1576 /* Remove the current folder separator */
1577 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1578 shortcuts_remove_rows (request->impl, separator_pos, 1);
1584 if (!request->label_copy)
1585 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1586 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1587 request->impl->icon_size);
1589 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1590 SHORTCUTS_COL_PIXBUF, pixbuf,
1591 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1592 SHORTCUTS_COL_NAME, request->label_copy,
1593 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1594 SHORTCUTS_COL_REMOVABLE, request->removable,
1597 if (request->impl->shortcuts_pane_filter_model)
1598 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1600 if (request->impl->shortcuts_combo_filter_model)
1601 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1603 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1604 request->impl->save_folder_combo != NULL)
1606 /* The current folder is updated via _activate_iter(), don't
1607 * have save_folder_combo_changed_cb() call _activate_iter()
1610 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1611 G_CALLBACK (save_folder_combo_changed_cb),
1614 if (request->impl->has_search)
1617 if (request->impl->has_recent)
1620 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1621 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1622 G_CALLBACK (save_folder_combo_changed_cb),
1627 g_object_unref (pixbuf);
1630 g_object_unref (request->impl);
1631 g_object_unref (request->file);
1632 gtk_tree_row_reference_free (request->row_ref);
1633 g_free (request->label_copy);
1636 g_object_unref (cancellable);
1639 /* FIXME: GtkFileSystem needs a function to split a remote path
1640 * into hostname and path components, or maybe just have a
1641 * gtk_file_system_path_get_display_name().
1643 * This function is also used in gtkfilechooserbutton.c
1646 _gtk_file_chooser_label_for_file (GFile *file)
1648 const gchar *path, *start, *end, *p;
1649 gchar *uri, *host, *label;
1651 uri = g_file_get_uri (file);
1653 start = strstr (uri, "://");
1657 path = strchr (start, '/');
1662 end = uri + strlen (uri);
1666 /* strip username */
1667 p = strchr (start, '@');
1671 p = strchr (start, ':');
1675 host = g_strndup (start, end - start);
1677 /* Translators: the first string is a path and the second string
1678 * is a hostname. Nautilus and the panel contain the same string
1681 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1687 label = g_strdup (uri);
1695 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1696 * inserts a volume. A position of -1 indicates the end of the tree.
1699 shortcuts_insert_file (GtkFileChooserDefault *impl,
1701 ShortcutType shortcut_type,
1702 GtkFileSystemVolume *volume,
1706 ShortcutsIndex type)
1709 GdkPixbuf *pixbuf = NULL;
1710 gpointer data = NULL;
1712 GtkIconTheme *icon_theme;
1714 profile_start ("start shortcut", NULL);
1716 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1719 label_copy = _gtk_file_system_volume_get_display_name (volume);
1720 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1721 impl->icon_size, NULL);
1723 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1725 if (g_file_is_native (file))
1727 struct ShortcutsInsertRequest *request;
1728 GCancellable *cancellable;
1731 request = g_new0 (struct ShortcutsInsertRequest, 1);
1732 request->impl = g_object_ref (impl);
1733 request->file = g_object_ref (file);
1734 request->name_only = TRUE;
1735 request->removable = removable;
1737 request->type = type;
1739 request->label_copy = g_strdup (label);
1742 gtk_list_store_append (impl->shortcuts_model, &iter);
1744 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1746 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1747 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1748 gtk_tree_path_free (p);
1750 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1751 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1752 get_file_info_finished, request);
1754 gtk_list_store_set (impl->shortcuts_model, &iter,
1755 SHORTCUTS_COL_DATA, g_object_ref (file),
1756 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1757 SHORTCUTS_COL_CANCELLABLE, cancellable,
1760 shortcuts_update_count (impl, type, 1);
1766 /* Don't call get_info for remote paths to avoid latency and
1769 data = g_object_ref (file);
1771 label_copy = g_strdup (label);
1773 label_copy = _gtk_file_chooser_label_for_file (file);
1775 /* If we switch to a better bookmarks file format (XBEL), we
1776 * should use mime info to get a better icon.
1778 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1779 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1780 impl->icon_size, 0, NULL);
1785 g_assert_not_reached ();
1791 gtk_list_store_append (impl->shortcuts_model, &iter);
1793 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1795 shortcuts_update_count (impl, type, 1);
1797 gtk_list_store_set (impl->shortcuts_model, &iter,
1798 SHORTCUTS_COL_PIXBUF, pixbuf,
1799 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1800 SHORTCUTS_COL_NAME, label_copy,
1801 SHORTCUTS_COL_DATA, data,
1802 SHORTCUTS_COL_TYPE, shortcut_type,
1803 SHORTCUTS_COL_REMOVABLE, removable,
1804 SHORTCUTS_COL_CANCELLABLE, NULL,
1807 if (impl->shortcuts_pane_filter_model)
1808 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1810 if (impl->shortcuts_combo_filter_model)
1811 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1813 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1815 /* The current folder is updated via _activate_iter(), don't
1816 * have save_folder_combo_changed_cb() call _activate_iter()
1819 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1821 if (impl->has_search)
1824 if (impl->has_recent)
1827 g_signal_handlers_block_by_func (impl->save_folder_combo,
1828 G_CALLBACK (save_folder_combo_changed_cb),
1831 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1832 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1833 G_CALLBACK (save_folder_combo_changed_cb),
1837 g_free (label_copy);
1840 g_object_unref (pixbuf);
1842 profile_end ("end", NULL);
1846 shortcuts_append_search (GtkFileChooserDefault *impl)
1851 pixbuf = render_search_icon (impl);
1853 gtk_list_store_append (impl->shortcuts_model, &iter);
1854 gtk_list_store_set (impl->shortcuts_model, &iter,
1855 SHORTCUTS_COL_PIXBUF, pixbuf,
1856 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1857 SHORTCUTS_COL_NAME, _("Search"),
1858 SHORTCUTS_COL_DATA, NULL,
1859 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1860 SHORTCUTS_COL_REMOVABLE, FALSE,
1864 g_object_unref (pixbuf);
1866 impl->has_search = TRUE;
1870 shortcuts_append_recent (GtkFileChooserDefault *impl)
1875 pixbuf = render_recent_icon (impl);
1877 gtk_list_store_append (impl->shortcuts_model, &iter);
1878 gtk_list_store_set (impl->shortcuts_model, &iter,
1879 SHORTCUTS_COL_PIXBUF, pixbuf,
1880 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1881 SHORTCUTS_COL_NAME, _("Recently Used"),
1882 SHORTCUTS_COL_DATA, NULL,
1883 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1884 SHORTCUTS_COL_REMOVABLE, FALSE,
1888 g_object_unref (pixbuf);
1890 impl->has_recent = TRUE;
1893 /* Appends an item for the user's home directory to the shortcuts model */
1895 shortcuts_append_home (GtkFileChooserDefault *impl)
1897 const char *home_path;
1900 profile_start ("start", NULL);
1902 home_path = g_get_home_dir ();
1903 if (home_path == NULL)
1905 profile_end ("end - no home directory!?", NULL);
1909 home = g_file_new_for_path (home_path);
1910 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1911 impl->has_home = TRUE;
1913 g_object_unref (home);
1915 profile_end ("end", NULL);
1918 /* Appends the ~/Desktop directory to the shortcuts model */
1920 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1925 profile_start ("start", NULL);
1927 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1928 /* "To disable a directory, point it to the homedir."
1929 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1931 if (!g_strcmp0 (name, g_get_home_dir ()))
1933 profile_end ("end", NULL);
1937 file = g_file_new_for_path (name);
1938 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1939 impl->has_desktop = TRUE;
1941 /* We do not actually pop up an error dialog if there is no desktop directory
1942 * because some people may really not want to have one.
1945 g_object_unref (file);
1947 profile_end ("end", NULL);
1950 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1952 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1959 profile_start ("start", NULL);
1961 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1964 for (; bookmarks; bookmarks = bookmarks->next)
1968 file = bookmarks->data;
1970 if (impl->local_only && !g_file_is_native (file))
1973 if (shortcut_find_position (impl, file) != -1)
1976 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1978 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1984 profile_end ("end", NULL);
1986 return num_inserted;
1989 /* Returns the index for the corresponding item in the shortcuts bar */
1991 shortcuts_get_index (GtkFileChooserDefault *impl,
1992 ShortcutsIndex where)
1998 if (where == SHORTCUTS_SEARCH)
2001 n += impl->has_search ? 1 : 0;
2003 if (where == SHORTCUTS_RECENT)
2006 n += impl->has_recent ? 1 : 0;
2008 if (where == SHORTCUTS_RECENT_SEPARATOR)
2011 n += impl->has_recent ? 1 : 0;
2013 if (where == SHORTCUTS_HOME)
2016 n += impl->has_home ? 1 : 0;
2018 if (where == SHORTCUTS_DESKTOP)
2021 n += impl->has_desktop ? 1 : 0;
2023 if (where == SHORTCUTS_VOLUMES)
2026 n += impl->num_volumes;
2028 if (where == SHORTCUTS_SHORTCUTS)
2031 n += impl->num_shortcuts;
2033 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2036 /* If there are no bookmarks there won't be a separator */
2037 n += (impl->num_bookmarks > 0) ? 1 : 0;
2039 if (where == SHORTCUTS_BOOKMARKS)
2042 n += impl->num_bookmarks;
2044 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2049 if (where == SHORTCUTS_CURRENT_FOLDER)
2052 g_assert_not_reached ();
2059 /* Adds all the file system volumes to the shortcuts model */
2061 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2066 gboolean old_changing_folders;
2068 profile_start ("start", NULL);
2070 old_changing_folders = impl->changing_folder;
2071 impl->changing_folder = TRUE;
2073 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2074 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2075 impl->num_volumes = 0;
2077 list = _gtk_file_system_list_volumes (impl->file_system);
2081 for (l = list; l; l = l->next)
2083 GtkFileSystemVolume *volume;
2087 if (impl->local_only)
2089 if (_gtk_file_system_volume_is_mounted (volume))
2092 gboolean base_is_native = TRUE;
2094 base_file = _gtk_file_system_volume_get_root (volume);
2095 if (base_file != NULL)
2097 base_is_native = g_file_is_native (base_file);
2098 g_object_unref (base_file);
2101 if (!base_is_native)
2106 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2110 impl->num_volumes = n;
2111 g_slist_free (list);
2113 if (impl->shortcuts_pane_filter_model)
2114 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2116 if (impl->shortcuts_combo_filter_model)
2117 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2119 impl->changing_folder = old_changing_folders;
2121 profile_end ("end", NULL);
2124 /* Inserts a separator node in the shortcuts list */
2126 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2127 ShortcutsIndex where)
2131 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2132 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2133 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2135 gtk_list_store_insert (impl->shortcuts_model, &iter,
2136 shortcuts_get_index (impl, where));
2137 gtk_list_store_set (impl->shortcuts_model, &iter,
2138 SHORTCUTS_COL_PIXBUF, NULL,
2139 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2140 SHORTCUTS_COL_NAME, NULL,
2141 SHORTCUTS_COL_DATA, NULL,
2142 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2146 /* Updates the list of bookmarks */
2148 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2151 gboolean old_changing_folders;
2153 GFile *list_selected = NULL;
2154 GFile *combo_selected = NULL;
2155 ShortcutType shortcut_type;
2158 profile_start ("start", NULL);
2160 old_changing_folders = impl->changing_folder;
2161 impl->changing_folder = TRUE;
2163 if (shortcuts_get_selected (impl, &iter))
2165 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2167 SHORTCUTS_COL_DATA, &col_data,
2168 SHORTCUTS_COL_TYPE, &shortcut_type,
2171 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2172 list_selected = g_object_ref (col_data);
2175 if (impl->save_folder_combo &&
2176 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2179 GtkTreeIter child_iter;
2181 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2184 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2186 SHORTCUTS_COL_DATA, &col_data,
2187 SHORTCUTS_COL_TYPE, &shortcut_type,
2190 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2191 combo_selected = g_object_ref (col_data);
2194 if (impl->num_bookmarks > 0)
2195 shortcuts_remove_rows (impl,
2196 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2197 impl->num_bookmarks + 1);
2199 impl->num_bookmarks = 0;
2200 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2202 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2203 shortcuts_append_bookmarks (impl, bookmarks);
2204 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2205 g_slist_free (bookmarks);
2207 if (impl->num_bookmarks == 0)
2208 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2210 if (impl->shortcuts_pane_filter_model)
2211 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2213 if (impl->shortcuts_combo_filter_model)
2214 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2218 shortcuts_find_folder (impl, list_selected);
2219 g_object_unref (list_selected);
2226 pos = shortcut_find_position (impl, combo_selected);
2229 if (impl->has_search)
2232 if (impl->has_recent)
2235 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2238 g_object_unref (combo_selected);
2241 impl->changing_folder = old_changing_folders;
2243 profile_end ("end", NULL);
2246 /* Appends a separator and a row to the shortcuts list for the current folder */
2248 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2253 g_assert (!impl->shortcuts_current_folder_active);
2257 g_assert (impl->current_folder != NULL);
2259 pos = shortcut_find_position (impl, impl->current_folder);
2262 GtkFileSystemVolume *volume;
2267 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2271 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2273 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2275 base_file = _gtk_file_system_volume_get_root (volume);
2279 if (base_file && g_file_equal (base_file, impl->current_folder))
2280 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2282 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2285 g_object_unref (base_file);
2287 else if (impl->save_folder_combo != NULL)
2289 if (impl->has_search)
2292 if (impl->has_recent)
2293 pos -= 2; /* + separator */
2295 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2299 /* Updates the current folder row in the shortcuts model */
2301 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2305 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2307 if (impl->shortcuts_current_folder_active)
2309 shortcuts_remove_rows (impl, pos, 2);
2310 impl->shortcuts_current_folder_active = FALSE;
2313 shortcuts_add_current_folder (impl);
2316 /* Filter function used for the shortcuts filter model */
2318 shortcuts_pane_filter_cb (GtkTreeModel *model,
2322 GtkFileChooserDefault *impl;
2326 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2328 path = gtk_tree_model_get_path (model, iter);
2332 pos = *gtk_tree_path_get_indices (path);
2333 gtk_tree_path_free (path);
2335 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2338 /* Creates the list model for shortcuts */
2340 shortcuts_model_create (GtkFileChooserDefault *impl)
2342 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2343 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2344 GDK_TYPE_PIXBUF, /* pixbuf */
2345 G_TYPE_STRING, /* name */
2346 G_TYPE_POINTER, /* path or volume */
2347 G_TYPE_INT, /* ShortcutType */
2348 G_TYPE_BOOLEAN, /* removable */
2349 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2350 G_TYPE_POINTER); /* GCancellable */
2352 if (search_is_possible (impl))
2354 shortcuts_append_search (impl);
2357 if (impl->recent_manager)
2359 shortcuts_append_recent (impl);
2360 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2363 if (impl->file_system)
2365 shortcuts_append_home (impl);
2366 shortcuts_append_desktop (impl);
2367 shortcuts_add_volumes (impl);
2370 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2371 GTK_TREE_MODEL (impl->shortcuts_model),
2374 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2375 shortcuts_pane_filter_cb,
2380 /* Callback used when the "New Folder" button is clicked */
2382 new_folder_button_clicked (GtkButton *button,
2383 GtkFileChooserDefault *impl)
2388 if (!impl->browse_files_model)
2389 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2391 /* Prevent button from being clicked twice */
2392 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2394 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2396 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2397 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2398 path, impl->list_name_column,
2401 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2402 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2404 impl->list_name_column,
2407 gtk_tree_path_free (path);
2410 /* Idle handler for creating a new folder after editing its name cell, or for
2411 * canceling the editing.
2414 edited_idle_cb (GtkFileChooserDefault *impl)
2416 GDK_THREADS_ENTER ();
2418 g_source_destroy (impl->edited_idle);
2419 impl->edited_idle = NULL;
2421 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2422 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2424 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2426 if (impl->edited_new_text /* not cancelled? */
2427 && (strlen (impl->edited_new_text) != 0)
2428 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2430 GError *error = NULL;
2433 file = g_file_get_child_for_display_name (impl->current_folder,
2434 impl->edited_new_text,
2438 GError *error = NULL;
2440 if (g_file_make_directory (file, NULL, &error))
2441 change_folder_and_display_error (impl, file, FALSE);
2443 error_creating_folder_dialog (impl, file, error);
2445 g_object_unref (file);
2448 error_creating_folder_dialog (impl, file, error);
2450 g_free (impl->edited_new_text);
2451 impl->edited_new_text = NULL;
2454 GDK_THREADS_LEAVE ();
2460 queue_edited_idle (GtkFileChooserDefault *impl,
2461 const gchar *new_text)
2463 /* We create the folder in an idle handler so that we don't modify the tree
2467 if (!impl->edited_idle)
2469 impl->edited_idle = g_idle_source_new ();
2470 g_source_set_closure (impl->edited_idle,
2471 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2473 g_source_attach (impl->edited_idle, NULL);
2476 g_free (impl->edited_new_text);
2477 impl->edited_new_text = g_strdup (new_text);
2480 /* Callback used from the text cell renderer when the new folder is named */
2482 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2484 const gchar *new_text,
2485 GtkFileChooserDefault *impl)
2487 /* work around bug #154921 */
2488 g_object_set (cell_renderer_text,
2489 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2490 queue_edited_idle (impl, new_text);
2493 /* Callback used from the text cell renderer when the new folder edition gets
2497 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2498 GtkFileChooserDefault *impl)
2500 /* work around bug #154921 */
2501 g_object_set (cell_renderer_text,
2502 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2503 queue_edited_idle (impl, NULL);
2506 /* Creates the widgets for the filter combo box */
2508 filter_create (GtkFileChooserDefault *impl)
2510 impl->filter_combo = gtk_combo_box_new_text ();
2511 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2513 g_signal_connect (impl->filter_combo, "changed",
2514 G_CALLBACK (filter_combo_changed), impl);
2516 gtk_widget_set_tooltip_text (impl->filter_combo,
2517 _("Select which types of files are shown"));
2519 return impl->filter_combo;
2523 button_new (GtkFileChooserDefault *impl,
2525 const char *stock_id,
2533 button = gtk_button_new_with_mnemonic (text);
2534 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2535 gtk_button_set_image (GTK_BUTTON (button), image);
2537 gtk_widget_set_sensitive (button, sensitive);
2538 g_signal_connect (button, "clicked", callback, impl);
2541 gtk_widget_show (button);
2546 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2548 shortcut_find_position (GtkFileChooserDefault *impl,
2553 int current_folder_separator_idx;
2555 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2558 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2561 /* FIXME: is this still needed? */
2562 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2566 for (i = 0; i < current_folder_separator_idx; i++)
2569 ShortcutType shortcut_type;
2571 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2572 SHORTCUTS_COL_DATA, &col_data,
2573 SHORTCUTS_COL_TYPE, &shortcut_type,
2578 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2580 GtkFileSystemVolume *volume;
2585 base_file = _gtk_file_system_volume_get_root (volume);
2587 exists = base_file && g_file_equal (file, base_file);
2590 g_object_unref (base_file);
2595 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2599 model_file = col_data;
2601 if (model_file && g_file_equal (model_file, file))
2606 if (i < current_folder_separator_idx - 1)
2608 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2609 g_assert_not_reached ();
2616 /* Tries to add a bookmark from a path name */
2618 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2624 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2626 if (shortcut_find_position (impl, file) != -1)
2630 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2632 error_adding_bookmark_dialog (impl, file, error);
2640 add_bookmark_foreach_cb (GtkTreeModel *model,
2645 GtkFileChooserDefault *impl;
2648 impl = (GtkFileChooserDefault *) data;
2650 gtk_tree_model_get (model, iter,
2651 MODEL_COL_FILE, &file,
2654 shortcuts_add_bookmark_from_file (impl, file, -1);
2656 g_object_unref (file);
2659 /* Adds a bookmark from the currently selected item in the file list */
2661 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2663 GtkTreeSelection *selection;
2665 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2667 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2668 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2670 gtk_tree_selection_selected_foreach (selection,
2671 add_bookmark_foreach_cb,
2675 /* Callback used when the "Add bookmark" button is clicked */
2677 add_bookmark_button_clicked_cb (GtkButton *button,
2678 GtkFileChooserDefault *impl)
2680 bookmarks_add_selected_folder (impl);
2683 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2684 * returns FALSE if no shortcut is selected.
2687 shortcuts_get_selected (GtkFileChooserDefault *impl,
2690 GtkTreeSelection *selection;
2691 GtkTreeIter parent_iter;
2693 if (!impl->browse_shortcuts_tree_view)
2696 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2698 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2701 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2707 /* Removes the selected bookmarks */
2709 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2717 if (!shortcuts_get_selected (impl, &iter))
2720 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2721 SHORTCUTS_COL_DATA, &col_data,
2722 SHORTCUTS_COL_REMOVABLE, &removable,
2728 g_assert (col_data != NULL);
2733 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2734 error_removing_bookmark_dialog (impl, file, error);
2737 /* Callback used when the "Remove bookmark" button is clicked */
2739 remove_bookmark_button_clicked_cb (GtkButton *button,
2740 GtkFileChooserDefault *impl)
2742 remove_selected_bookmarks (impl);
2745 struct selection_check_closure {
2746 GtkFileChooserDefault *impl;
2749 gboolean all_folders;
2752 /* Used from gtk_tree_selection_selected_foreach() */
2754 selection_check_foreach_cb (GtkTreeModel *model,
2759 struct selection_check_closure *closure;
2760 GtkTreeIter child_iter;
2765 closure->num_selected++;
2767 switch (closure->impl->operation_mode)
2769 case OPERATION_MODE_BROWSE:
2770 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2771 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2772 is_folder = info ? (_gtk_file_info_consider_as_directory (info)) : FALSE;
2775 case OPERATION_MODE_SEARCH:
2776 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2777 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2778 MODEL_COL_IS_FOLDER, &is_folder,
2782 case OPERATION_MODE_RECENT:
2783 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2784 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2785 MODEL_COL_IS_FOLDER, &is_folder,
2790 closure->all_folders = closure->all_folders && is_folder;
2791 closure->all_files = closure->all_files && !is_folder;
2794 /* Checks whether the selected items in the file list are all files or all folders */
2796 selection_check (GtkFileChooserDefault *impl,
2798 gboolean *all_files,
2799 gboolean *all_folders)
2801 struct selection_check_closure closure;
2802 GtkTreeSelection *selection;
2804 closure.impl = impl;
2805 closure.num_selected = 0;
2806 closure.all_files = TRUE;
2807 closure.all_folders = TRUE;
2809 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2810 gtk_tree_selection_selected_foreach (selection,
2811 selection_check_foreach_cb,
2814 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2817 *num_selected = closure.num_selected;
2820 *all_files = closure.all_files;
2823 *all_folders = closure.all_folders;
2826 struct get_selected_file_closure {
2827 GtkFileChooserDefault *impl;
2832 get_selected_file_foreach_cb (GtkTreeModel *model,
2837 struct get_selected_file_closure *closure;
2838 GtkTreeIter child_iter;
2842 switch (closure->impl->operation_mode)
2844 case OPERATION_MODE_BROWSE:
2845 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2846 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2849 case OPERATION_MODE_SEARCH:
2850 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2851 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2852 MODEL_COL_FILE, &closure->file,
2856 case OPERATION_MODE_RECENT:
2857 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2858 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2859 MODEL_COL_FILE, &closure->file,
2865 /* Returns a selected path from the file list */
2867 get_selected_file (GtkFileChooserDefault *impl)
2869 struct get_selected_file_closure closure;
2870 GtkTreeSelection *selection;
2872 closure.impl = impl;
2873 closure.file = NULL;
2875 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2876 gtk_tree_selection_selected_foreach (selection,
2877 get_selected_file_foreach_cb,
2880 return closure.file;
2884 GtkFileChooserDefault *impl;
2886 } UpdateTooltipData;
2889 update_tooltip (GtkTreeModel *model,
2894 UpdateTooltipData *udata = data;
2895 GtkTreeIter child_iter;
2898 if (udata->tip == NULL)
2900 const gchar *display_name;
2902 switch (udata->impl->operation_mode)
2904 case OPERATION_MODE_BROWSE:
2905 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2908 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2909 display_name = g_file_info_get_display_name (info);
2912 case OPERATION_MODE_SEARCH:
2913 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2914 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2915 MODEL_COL_NAME, &display_name,
2919 case OPERATION_MODE_RECENT:
2920 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2921 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2922 MODEL_COL_NAME, &display_name,
2927 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2933 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2934 * if there are no selected items *and* the current folder is not in the
2935 * bookmarks list. De-sensitize the button otherwise.
2938 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2941 gboolean all_folders;
2945 selection_check (impl, &num_selected, NULL, &all_folders);
2947 if (num_selected == 0)
2948 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2949 else if (num_selected == 1)
2953 file = get_selected_file (impl);
2954 active = all_folders && (shortcut_find_position (impl, file) == -1);
2957 active = all_folders;
2959 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2961 if (impl->browse_files_popup_menu_add_shortcut_item)
2962 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2963 (num_selected == 0) ? FALSE : active);
2967 if (num_selected == 0)
2968 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2969 else if (num_selected > 1)
2970 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2973 GtkTreeSelection *selection;
2974 UpdateTooltipData data;
2976 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2979 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2983 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2988 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2989 * bookmark row is selected in the shortcuts tree.
2992 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2995 gboolean removable = FALSE;
2999 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,
3005 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3008 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3010 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
3012 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3016 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3017 _("Remove the selected bookmark"));
3022 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3025 gboolean removable = FALSE;
3027 if (impl->browse_shortcuts_popup_menu == NULL)
3030 if (shortcuts_get_selected (impl, &iter))
3031 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3032 SHORTCUTS_COL_REMOVABLE, &removable,
3035 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3036 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3039 /* GtkWidget::drag-begin handler for the shortcuts list. */
3041 shortcuts_drag_begin_cb (GtkWidget *widget,
3042 GdkDragContext *context,
3043 GtkFileChooserDefault *impl)
3046 impl->shortcuts_drag_context = g_object_ref (context);
3051 /* Removes the idle handler for outside drags */
3053 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3055 if (!impl->shortcuts_drag_outside_idle)
3058 g_source_destroy (impl->shortcuts_drag_outside_idle);
3059 impl->shortcuts_drag_outside_idle = NULL;
3063 /* GtkWidget::drag-end handler for the shortcuts list. */
3065 shortcuts_drag_end_cb (GtkWidget *widget,
3066 GdkDragContext *context,
3067 GtkFileChooserDefault *impl)
3070 g_object_unref (impl->shortcuts_drag_context);
3072 shortcuts_cancel_drag_outside_idle (impl);
3074 if (!impl->shortcuts_drag_outside)
3077 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3079 impl->shortcuts_drag_outside = FALSE;
3083 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3085 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3086 GdkDragContext *context,
3087 GtkFileChooserDefault *impl)
3089 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3093 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3097 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3100 GtkTreeView *tree_view;
3103 GdkPixmap *row_pixmap;
3108 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3110 /* Find the selected path and get its drag pixmap */
3112 if (!shortcuts_get_selected (impl, &iter))
3113 g_assert_not_reached ();
3115 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3117 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3118 gtk_tree_path_free (path);
3127 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3133 GdkPixmap *composite;
3134 int row_pixmap_width, row_pixmap_height;
3135 int pixbuf_width, pixbuf_height;
3136 int composite_width, composite_height;
3137 int pixbuf_x, pixbuf_y;
3138 GdkGC *gc, *mask_gc;
3140 GdkBitmap *pixbuf_mask;
3142 /* Create pixmap and mask for composite image */
3144 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3145 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3146 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3148 composite_width = MAX (row_pixmap_width, pixbuf_width);
3149 composite_height = MAX (row_pixmap_height, pixbuf_height);
3151 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3153 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3156 pixbuf_x = composite_width - pixbuf_width;
3158 pixbuf_y = (composite_height - pixbuf_height) / 2;
3160 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3161 gc = gdk_gc_new (composite);
3163 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3164 mask_gc = gdk_gc_new (mask);
3166 gdk_gc_set_foreground (mask_gc, &color);
3167 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3170 color.green = 0xffff;
3171 color.blue = 0xffff;
3172 gdk_gc_set_rgb_fg_color (gc, &color);
3173 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3175 /* Composite the row pixmap and the pixbuf */
3177 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3179 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3180 NULL, &pixbuf_mask, 128);
3181 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3184 pixbuf_width, pixbuf_height);
3185 g_object_unref (pixbuf_mask);
3187 gdk_draw_drawable (composite, gc, row_pixmap,
3190 row_pixmap_width, row_pixmap_height);
3192 gdk_gc_set_foreground (mask_gc, &color);
3193 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3195 gdk_draw_pixbuf (composite, gc, pixbuf,
3198 pixbuf_width, pixbuf_height,
3202 g_object_unref (pixbuf);
3203 g_object_unref (row_pixmap);
3205 row_pixmap = composite;
3209 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3211 gtk_tree_view_get_path_at_pos (tree_view,
3212 tree_view->priv->press_start_x,
3213 tree_view->priv->press_start_y,
3219 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3220 gdk_drawable_get_colormap (row_pixmap),
3223 tree_view->priv->press_start_x + 1,
3224 row_pixmap_y + cell_y + 1);
3226 g_object_unref (row_pixmap);
3228 g_object_unref (mask);
3231 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3232 * handler so that we can tell apart the drag_leave event that comes right
3233 * before a drag_drop, from a normal drag_leave. We don't want to set the
3234 * cursor nor the flag in the latter case.
3237 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3239 GDK_THREADS_ENTER ();
3241 shortcuts_drag_set_delete_cursor (impl, TRUE);
3242 impl->shortcuts_drag_outside = TRUE;
3244 shortcuts_cancel_drag_outside_idle (impl);
3246 GDK_THREADS_LEAVE ();
3252 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3256 shortcuts_drag_leave_cb (GtkWidget *widget,
3257 GdkDragContext *context,
3259 GtkFileChooserDefault *impl)
3262 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3264 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3265 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3266 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3268 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3272 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3274 GTK_TREE_VIEW_DROP_BEFORE);
3276 g_signal_stop_emission_by_name (widget, "drag-leave");
3279 /* Computes the appropriate row and position for dropping */
3281 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3285 GtkTreeViewDropPosition *pos)
3287 GtkTreeView *tree_view;
3288 GtkTreeViewColumn *column;
3292 int bookmarks_index;
3294 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3296 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3298 if (!gtk_tree_view_get_path_at_pos (tree_view,
3300 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3306 row = bookmarks_index + impl->num_bookmarks - 1;
3307 *path = gtk_tree_path_new_from_indices (row, -1);
3308 *pos = GTK_TREE_VIEW_DROP_AFTER;
3312 row = *gtk_tree_path_get_indices (*path);
3313 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3314 gtk_tree_path_free (*path);
3316 if (row < bookmarks_index)
3318 row = bookmarks_index;
3319 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3321 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3323 row = bookmarks_index + impl->num_bookmarks - 1;
3324 *pos = GTK_TREE_VIEW_DROP_AFTER;
3328 if (cell_y < cell.height / 2)
3329 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3331 *pos = GTK_TREE_VIEW_DROP_AFTER;
3334 *path = gtk_tree_path_new_from_indices (row, -1);
3337 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3338 * implement the destination side of DnD by hand, due to limitations in
3339 * GtkTreeView's DnD API.
3342 shortcuts_drag_motion_cb (GtkWidget *widget,
3343 GdkDragContext *context,
3347 GtkFileChooserDefault *impl)
3350 GtkTreeViewDropPosition pos;
3351 GdkDragAction action;
3354 if (gtk_drag_get_source_widget (context) == widget)
3356 shortcuts_cancel_drag_outside_idle (impl);
3358 if (impl->shortcuts_drag_outside)
3360 shortcuts_drag_set_delete_cursor (impl, FALSE);
3361 impl->shortcuts_drag_outside = FALSE;
3366 if (context->suggested_action == GDK_ACTION_COPY ||
3367 (context->actions & GDK_ACTION_COPY) != 0)
3368 action = GDK_ACTION_COPY;
3369 else if (context->suggested_action == GDK_ACTION_MOVE ||
3370 (context->actions & GDK_ACTION_MOVE) != 0)
3371 action = GDK_ACTION_MOVE;
3378 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3379 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3380 gtk_tree_path_free (path);
3384 g_signal_stop_emission_by_name (widget, "drag-motion");
3388 gdk_drag_status (context, action, time_);
3395 /* GtkWidget::drag-drop handler for the shortcuts list. */
3397 shortcuts_drag_drop_cb (GtkWidget *widget,
3398 GdkDragContext *context,
3402 GtkFileChooserDefault *impl)
3405 shortcuts_cancel_drag_outside_idle (impl);
3408 g_signal_stop_emission_by_name (widget, "drag-drop");
3412 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3414 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3415 GtkSelectionData *selection_data,
3421 uris = gtk_selection_data_get_uris (selection_data);
3425 for (i = 0; uris[i]; i++)
3431 file = g_file_new_for_uri (uri);
3433 if (shortcuts_add_bookmark_from_file (impl, file, position))
3436 g_object_unref (file);
3442 /* Reorders the selected bookmark to the specified position */
3444 shortcuts_reorder (GtkFileChooserDefault *impl,
3449 ShortcutType shortcut_type;
3452 int bookmarks_index;
3457 /* Get the selected path */
3459 if (!shortcuts_get_selected (impl, &iter))
3460 g_assert_not_reached ();
3462 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3463 old_position = *gtk_tree_path_get_indices (path);
3464 gtk_tree_path_free (path);
3466 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3467 old_position -= bookmarks_index;
3468 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3470 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3471 SHORTCUTS_COL_NAME, &name,
3472 SHORTCUTS_COL_DATA, &col_data,
3473 SHORTCUTS_COL_TYPE, &shortcut_type,
3475 g_assert (col_data != NULL);
3476 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3479 g_object_ref (file); /* removal below will free file, so we need a new ref */
3481 /* Remove the path from the old position and insert it in the new one */
3483 if (new_position > old_position)
3486 if (old_position == new_position)
3490 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3492 shortcuts_add_bookmark_from_file (impl, file, new_position);
3493 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3496 error_adding_bookmark_dialog (impl, file, error);
3500 g_object_unref (file);
3503 /* Callback used when we get the drag data for the bookmarks list. We add the
3504 * received URIs as bookmarks if they are folders.
3507 shortcuts_drag_data_received_cb (GtkWidget *widget,
3508 GdkDragContext *context,
3511 GtkSelectionData *selection_data,
3516 GtkFileChooserDefault *impl;
3517 GtkTreePath *tree_path;
3518 GtkTreeViewDropPosition tree_pos;
3520 int bookmarks_index;
3522 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3524 /* Compute position */
3526 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3528 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3529 position = *gtk_tree_path_get_indices (tree_path);
3530 gtk_tree_path_free (tree_path);
3532 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3535 g_assert (position >= bookmarks_index);
3536 position -= bookmarks_index;
3538 if (gtk_targets_include_uri (&selection_data->target, 1))
3539 shortcuts_drop_uris (impl, selection_data, position);
3540 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3541 shortcuts_reorder (impl, position);
3543 g_signal_stop_emission_by_name (widget, "drag-data-received");
3546 /* Callback used to display a tooltip in the shortcuts tree */
3548 shortcuts_query_tooltip_cb (GtkWidget *widget,
3551 gboolean keyboard_mode,
3552 GtkTooltip *tooltip,
3553 GtkFileChooserDefault *impl)
3555 GtkTreeModel *model;
3558 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3566 ShortcutType shortcut_type;
3568 gtk_tree_model_get (model, &iter,
3569 SHORTCUTS_COL_DATA, &col_data,
3570 SHORTCUTS_COL_TYPE, &shortcut_type,
3573 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3575 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3579 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3584 file = G_FILE (col_data);
3585 parse_name = g_file_get_parse_name (file);
3587 gtk_tooltip_set_text (tooltip, parse_name);
3589 g_free (parse_name);
3593 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3597 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3607 /* Callback used when the selection in the shortcuts tree changes */
3609 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3610 GtkFileChooserDefault *impl)
3613 GtkTreeIter child_iter;
3615 bookmarks_check_remove_sensitivity (impl);
3616 shortcuts_check_popup_sensitivity (impl);
3618 if (impl->changing_folder)
3621 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3623 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3626 shortcuts_activate_iter (impl, &child_iter);
3631 shortcuts_row_separator_func (GtkTreeModel *model,
3635 ShortcutType shortcut_type;
3637 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3639 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3643 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3645 GtkFileChooserDefault *impl)
3649 /* don't screw up focus switching with Tab */
3650 if (event->keyval == GDK_Tab
3651 || event->keyval == GDK_KP_Tab
3652 || event->keyval == GDK_ISO_Left_Tab
3653 || event->length < 1)
3656 if (impl->location_entry)
3657 entry = impl->location_entry;
3658 else if (impl->search_entry)
3659 entry = impl->search_entry;
3665 gtk_widget_grab_focus (entry);
3666 return gtk_widget_event (entry, (GdkEvent *) event);
3672 /* Callback used when the file list's popup menu is detached */
3674 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3677 GtkFileChooserDefault *impl;
3679 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3680 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3682 impl->browse_shortcuts_popup_menu = NULL;
3683 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3684 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3688 remove_shortcut_cb (GtkMenuItem *item,
3689 GtkFileChooserDefault *impl)
3691 remove_selected_bookmarks (impl);
3694 /* Rename the selected bookmark */
3696 rename_selected_bookmark (GtkFileChooserDefault *impl)
3700 GtkTreeViewColumn *column;
3701 GtkCellRenderer *cell;
3704 if (shortcuts_get_selected (impl, &iter))
3706 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3707 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3708 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3709 cell = g_list_nth_data (renderers, 1);
3710 g_list_free (renderers);
3711 g_object_set (cell, "editable", TRUE, NULL);
3712 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3713 path, column, cell, TRUE);
3714 gtk_tree_path_free (path);
3719 rename_shortcut_cb (GtkMenuItem *item,
3720 GtkFileChooserDefault *impl)
3722 rename_selected_bookmark (impl);
3725 /* Constructs the popup menu for the file list if needed */
3727 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3731 if (impl->browse_shortcuts_popup_menu)
3734 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3735 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3736 impl->browse_shortcuts_tree_view,
3737 shortcuts_popup_menu_detach_cb);
3739 item = gtk_image_menu_item_new_with_label (_("Remove"));
3740 impl->browse_shortcuts_popup_menu_remove_item = item;
3741 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3742 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3743 g_signal_connect (item, "activate",
3744 G_CALLBACK (remove_shortcut_cb), impl);
3745 gtk_widget_show (item);
3746 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3748 item = gtk_menu_item_new_with_label (_("Rename..."));
3749 impl->browse_shortcuts_popup_menu_rename_item = item;
3750 g_signal_connect (item, "activate",
3751 G_CALLBACK (rename_shortcut_cb), impl);
3752 gtk_widget_show (item);
3753 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3757 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3759 shortcuts_build_popup_menu (impl);
3760 shortcuts_check_popup_sensitivity (impl);
3764 popup_position_func (GtkMenu *menu,
3768 gpointer user_data);
3771 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3772 GdkEventButton *event)
3774 shortcuts_update_popup_menu (impl);
3776 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3777 NULL, NULL, NULL, NULL,
3778 event->button, event->time);
3781 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3783 popup_position_func, impl->browse_shortcuts_tree_view,
3784 0, GDK_CURRENT_TIME);
3785 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3790 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3792 shortcuts_popup_menu_cb (GtkWidget *widget,
3793 GtkFileChooserDefault *impl)
3795 shortcuts_popup_menu (impl, NULL);
3799 /* Callback used when a button is pressed on the shortcuts list.
3800 * We trap button 3 to bring up a popup menu.
3803 shortcuts_button_press_event_cb (GtkWidget *widget,
3804 GdkEventButton *event,
3805 GtkFileChooserDefault *impl)
3807 static gboolean in_press = FALSE;
3813 if (event->button != 3)
3817 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3823 shortcuts_popup_menu (impl, event);
3828 shortcuts_edited (GtkCellRenderer *cell,
3831 GtkFileChooserDefault *impl)
3837 g_object_set (cell, "editable", FALSE, NULL);
3839 path = gtk_tree_path_new_from_string (path_string);
3840 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3841 g_assert_not_reached ();
3843 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3844 SHORTCUTS_COL_DATA, &shortcut,
3846 gtk_tree_path_free (path);
3848 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3852 shortcuts_editing_canceled (GtkCellRenderer *cell,
3853 GtkFileChooserDefault *impl)
3855 g_object_set (cell, "editable", FALSE, NULL);
3858 /* Creates the widgets for the shortcuts and bookmarks tree */
3860 shortcuts_list_create (GtkFileChooserDefault *impl)
3863 GtkTreeSelection *selection;
3864 GtkTreeViewColumn *column;
3865 GtkCellRenderer *renderer;
3867 /* Target types for dragging a row to/from the shortcuts list */
3868 const GtkTargetEntry tree_model_row_targets[] = {
3869 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3872 /* Scrolled window */
3874 swin = gtk_scrolled_window_new (NULL, NULL);
3875 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3876 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3877 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3879 gtk_widget_show (swin);
3883 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3884 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3885 #ifdef PROFILE_FILE_CHOOSER
3886 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3889 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3891 * 1. user brings up a SAVE dialog
3892 * 2. user clicks on a shortcut in the shortcuts pane
3893 * 3. user starts typing a filename
3895 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3896 * support interactive search. However, we'd rather focus the location entry
3897 * so that the user can type *there*.
3899 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3900 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3901 * if the user starts typing while the focus is in the shortcuts pane.
3903 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3904 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3906 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3907 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3908 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3909 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3910 /* Accessible object name for the file chooser's shortcuts pane */
3911 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3913 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3915 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3917 tree_model_row_targets,
3918 G_N_ELEMENTS (tree_model_row_targets),
3921 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3922 GTK_DEST_DEFAULT_ALL,
3923 tree_model_row_targets,
3924 G_N_ELEMENTS (tree_model_row_targets),
3925 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3926 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3928 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3929 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3930 gtk_tree_selection_set_select_function (selection,
3931 shortcuts_select_func,
3934 g_signal_connect (selection, "changed",
3935 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3937 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3938 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3940 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3941 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3942 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3943 G_CALLBACK (shortcuts_drag_end_cb), impl);
3944 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3945 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3947 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3948 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3949 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3950 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3951 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3952 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3953 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3954 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3956 /* Support tooltips */
3957 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3958 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3959 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3961 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3962 gtk_widget_show (impl->browse_shortcuts_tree_view);
3966 column = gtk_tree_view_column_new ();
3967 /* Column header for the file chooser's shortcuts pane */
3968 gtk_tree_view_column_set_title (column, _("_Places"));
3970 renderer = gtk_cell_renderer_pixbuf_new ();
3971 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3972 gtk_tree_view_column_set_attributes (column, renderer,
3973 "pixbuf", SHORTCUTS_COL_PIXBUF,
3974 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3977 renderer = gtk_cell_renderer_text_new ();
3978 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3979 g_signal_connect (renderer, "edited",
3980 G_CALLBACK (shortcuts_edited), impl);
3981 g_signal_connect (renderer, "editing-canceled",
3982 G_CALLBACK (shortcuts_editing_canceled), impl);
3983 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3984 gtk_tree_view_column_set_attributes (column, renderer,
3985 "text", SHORTCUTS_COL_NAME,
3988 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3989 shortcuts_row_separator_func,
3992 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3997 /* Creates the widgets for the shortcuts/bookmarks pane */
3999 shortcuts_pane_create (GtkFileChooserDefault *impl,
4000 GtkSizeGroup *size_group)
4006 vbox = gtk_vbox_new (FALSE, 6);
4007 gtk_widget_show (vbox);
4009 /* Shortcuts tree */
4011 widget = shortcuts_list_create (impl);
4012 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
4014 /* Box for buttons */
4016 hbox = gtk_hbox_new (TRUE, 6);
4017 gtk_size_group_add_widget (size_group, hbox);
4018 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4019 gtk_widget_show (hbox);
4021 /* Add bookmark button */
4023 impl->browse_shortcuts_add_button = button_new (impl,
4028 G_CALLBACK (add_bookmark_button_clicked_cb));
4029 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4030 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4031 _("Add the selected folder to the Bookmarks"));
4033 /* Remove bookmark button */
4035 impl->browse_shortcuts_remove_button = button_new (impl,
4040 G_CALLBACK (remove_bookmark_button_clicked_cb));
4041 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4042 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4043 _("Remove the selected bookmark"));
4049 key_is_left_or_right (GdkEventKey *event)
4053 modifiers = gtk_accelerator_get_default_mod_mask ();
4055 return ((event->keyval == GDK_Right
4056 || event->keyval == GDK_KP_Right
4057 || event->keyval == GDK_Left
4058 || event->keyval == GDK_KP_Left)
4059 && (event->state & modifiers) == 0);
4062 /* Handles key press events on the file list, so that we can trap Enter to
4063 * activate the default button on our own. Also, checks to see if '/' has been
4064 * pressed. See comment by tree_view_keybinding_cb() for more details.
4067 browse_files_key_press_event_cb (GtkWidget *widget,
4071 GtkFileChooserDefault *impl;
4074 impl = (GtkFileChooserDefault *) data;
4076 modifiers = gtk_accelerator_get_default_mod_mask ();
4078 if ((event->keyval == GDK_slash
4079 || event->keyval == GDK_KP_Divide
4081 || event->keyval == GDK_asciitilde
4083 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4085 location_popup_handler (impl, event->string);
4089 if (key_is_left_or_right (event))
4091 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
4095 if ((event->keyval == GDK_Return
4096 || event->keyval == GDK_ISO_Enter
4097 || event->keyval == GDK_KP_Enter
4098 || event->keyval == GDK_space
4099 || event->keyval == GDK_KP_Space)
4100 && ((event->state & modifiers) == 0)
4101 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4102 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4106 window = get_toplevel (widget);
4108 && widget != window->default_widget
4109 && !(widget == window->focus_widget &&
4110 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4112 gtk_window_activate_default (window);
4120 /* Callback used when the file list's popup menu is detached */
4122 popup_menu_detach_cb (GtkWidget *attach_widget,
4125 GtkFileChooserDefault *impl;
4127 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4128 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4130 impl->browse_files_popup_menu = NULL;
4131 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4132 impl->browse_files_popup_menu_hidden_files_item = NULL;
4135 /* Callback used when the "Add to Bookmarks" menu item is activated */
4137 add_to_shortcuts_cb (GtkMenuItem *item,
4138 GtkFileChooserDefault *impl)
4140 bookmarks_add_selected_folder (impl);
4143 /* Callback used when the "Show Hidden Files" menu item is toggled */
4145 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4146 GtkFileChooserDefault *impl)
4149 "show-hidden", gtk_check_menu_item_get_active (item),
4153 /* Callback used when the "Show Size Column" menu item is toggled */
4155 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4156 GtkFileChooserDefault *impl)
4158 impl->show_size_column = gtk_check_menu_item_get_active (item);
4160 if (impl->list_size_column)
4161 gtk_tree_view_column_set_visible (impl->list_size_column,
4162 impl->show_size_column);
4165 /* Shows an error dialog about not being able to select a dragged file */
4167 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4172 _("Could not select file"),
4177 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4182 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4184 for (i = 1; uris[i]; i++)
4187 GError *error = NULL;
4190 file = g_file_new_for_uri (uri);
4192 gtk_file_chooser_default_select_file (chooser, file, &error);
4194 error_selecting_dragged_file_dialog (impl, file, error);
4196 g_object_unref (file);
4200 struct FileListDragData
4202 GtkFileChooserDefault *impl;
4208 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4210 const GError *error,
4213 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4214 struct FileListDragData *data = user_data;
4215 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4217 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4220 data->impl->file_list_drag_data_received_cancellable = NULL;
4222 if (cancelled || error)
4225 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4226 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4227 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4228 change_folder_and_display_error (data->impl, data->file, FALSE);
4231 GError *error = NULL;
4233 gtk_file_chooser_default_unselect_all (chooser);
4234 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4236 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4238 browse_files_center_selected_row (data->impl);
4241 if (data->impl->select_multiple)
4242 file_list_drag_data_select_uris (data->impl, data->uris);
4245 g_object_unref (data->impl);
4246 g_strfreev (data->uris);
4247 g_object_unref (data->file);
4250 g_object_unref (cancellable);
4254 file_list_drag_data_received_cb (GtkWidget *widget,
4255 GdkDragContext *context,
4258 GtkSelectionData *selection_data,
4263 GtkFileChooserDefault *impl;
4264 GtkFileChooser *chooser;
4269 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4270 chooser = GTK_FILE_CHOOSER (data);
4272 /* Allow only drags from other widgets; see bug #533891. */
4273 if (gtk_drag_get_source_widget (context) == widget)
4275 g_signal_stop_emission_by_name (widget, "drag-data-received");
4279 /* Parse the text/uri-list string, navigate to the first one */
4280 uris = gtk_selection_data_get_uris (selection_data);
4281 if (uris && uris[0])
4283 struct FileListDragData *data;
4286 file = g_file_new_for_uri (uri);
4288 data = g_new0 (struct FileListDragData, 1);
4289 data->impl = g_object_ref (impl);
4293 if (impl->file_list_drag_data_received_cancellable)
4294 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4296 impl->file_list_drag_data_received_cancellable =
4297 _gtk_file_system_get_info (impl->file_system, file,
4299 file_list_drag_data_received_get_info_cb,
4303 g_signal_stop_emission_by_name (widget, "drag-data-received");
4306 /* Don't do anything with the drag_drop signal */
4308 file_list_drag_drop_cb (GtkWidget *widget,
4309 GdkDragContext *context,
4313 GtkFileChooserDefault *impl)
4315 g_signal_stop_emission_by_name (widget, "drag-drop");
4319 /* Disable the normal tree drag motion handler, it makes it look like you're
4320 dropping the dragged item onto a tree item */
4322 file_list_drag_motion_cb (GtkWidget *widget,
4323 GdkDragContext *context,
4327 GtkFileChooserDefault *impl)
4329 g_signal_stop_emission_by_name (widget, "drag-motion");
4333 /* Constructs the popup menu for the file list if needed */
4335 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4339 if (impl->browse_files_popup_menu)
4342 impl->browse_files_popup_menu = gtk_menu_new ();
4343 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4344 impl->browse_files_tree_view,
4345 popup_menu_detach_cb);
4347 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4348 impl->browse_files_popup_menu_add_shortcut_item = item;
4349 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4350 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4351 g_signal_connect (item, "activate",
4352 G_CALLBACK (add_to_shortcuts_cb), impl);
4353 gtk_widget_show (item);
4354 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4356 item = gtk_separator_menu_item_new ();
4357 gtk_widget_show (item);
4358 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4360 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4361 impl->browse_files_popup_menu_hidden_files_item = item;
4362 g_signal_connect (item, "toggled",
4363 G_CALLBACK (show_hidden_toggled_cb), impl);
4364 gtk_widget_show (item);
4365 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4367 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4368 impl->browse_files_popup_menu_size_column_item = item;
4369 g_signal_connect (item, "toggled",
4370 G_CALLBACK (show_size_column_toggled_cb), impl);
4371 gtk_widget_show (item);
4372 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4374 bookmarks_check_add_sensitivity (impl);
4377 /* Updates the popup menu for the file list, creating it if necessary */
4379 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4381 file_list_build_popup_menu (impl);
4383 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4385 /* The sensitivity of the Add to Bookmarks item is set in
4386 * bookmarks_check_add_sensitivity()
4389 /* 'Show Hidden Files' */
4390 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4391 G_CALLBACK (show_hidden_toggled_cb), impl);
4392 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4394 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4395 G_CALLBACK (show_hidden_toggled_cb), impl);
4397 /* 'Show Size Column' */
4398 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4399 G_CALLBACK (show_size_column_toggled_cb), impl);
4400 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4401 impl->show_size_column);
4402 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4403 G_CALLBACK (show_size_column_toggled_cb), impl);
4407 popup_position_func (GtkMenu *menu,
4413 GtkWidget *widget = GTK_WIDGET (user_data);
4414 GdkScreen *screen = gtk_widget_get_screen (widget);
4417 GdkRectangle monitor;
4419 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4421 gdk_window_get_origin (widget->window, x, y);
4423 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4425 *x += (widget->allocation.width - req.width) / 2;
4426 *y += (widget->allocation.height - req.height) / 2;
4428 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4429 gtk_menu_set_monitor (menu, monitor_num);
4430 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4432 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4433 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4439 file_list_popup_menu (GtkFileChooserDefault *impl,
4440 GdkEventButton *event)
4442 file_list_update_popup_menu (impl);
4444 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4445 NULL, NULL, NULL, NULL,
4446 event->button, event->time);
4449 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4451 popup_position_func, impl->browse_files_tree_view,
4452 0, GDK_CURRENT_TIME);
4453 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4459 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4461 list_popup_menu_cb (GtkWidget *widget,
4462 GtkFileChooserDefault *impl)
4464 file_list_popup_menu (impl, NULL);
4468 /* Callback used when a button is pressed on the file list. We trap button 3 to
4469 * bring up a popup menu.
4472 list_button_press_event_cb (GtkWidget *widget,
4473 GdkEventButton *event,
4474 GtkFileChooserDefault *impl)
4476 static gboolean in_press = FALSE;
4482 if (event->button != 3)
4486 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4489 file_list_popup_menu (impl, event);
4494 OperationMode operation_mode;
4495 gint general_column;
4499 /* Sets the sort column IDs for the file list based on the operation mode */
4501 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4503 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4504 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4505 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4509 file_list_query_tooltip_cb (GtkWidget *widget,
4512 gboolean keyboard_tip,
4513 GtkTooltip *tooltip,
4516 GtkFileChooserDefault *impl = user_data;
4517 GtkTreeIter iter, child_iter;
4518 GtkTreePath *path = NULL;
4522 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4526 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4534 switch (impl->operation_mode)
4536 case OPERATION_MODE_SEARCH:
4537 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4539 gtk_tree_path_free (path);
4543 search_get_valid_child_iter (impl, &child_iter, &iter);
4544 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4545 MODEL_COL_FILE, &file,
4549 case OPERATION_MODE_RECENT:
4550 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4552 gtk_tree_path_free (path);
4556 recent_get_valid_child_iter (impl, &child_iter, &iter);
4557 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4558 MODEL_COL_FILE, &file,
4562 case OPERATION_MODE_BROWSE:
4563 g_assert_not_reached ();
4569 gtk_tree_path_free (path);
4573 filename = g_file_get_path (file);
4574 gtk_tooltip_set_text (tooltip, filename);
4575 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4580 gtk_tree_path_free (path);
4585 /* Creates the widgets for the file list */
4587 create_file_list (GtkFileChooserDefault *impl)
4590 GtkTreeSelection *selection;
4591 GtkTreeViewColumn *column;
4592 GtkCellRenderer *renderer;
4594 /* Scrolled window */
4595 swin = gtk_scrolled_window_new (NULL, NULL);
4596 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4597 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4598 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4601 /* Tree/list view */
4603 impl->browse_files_tree_view = gtk_tree_view_new ();
4604 #ifdef PROFILE_FILE_CHOOSER
4605 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4607 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4608 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4610 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4611 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4613 gtk_drag_dest_set (impl->browse_files_tree_view,
4614 GTK_DEST_DEFAULT_ALL,
4616 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4617 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4619 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4620 G_CALLBACK (list_row_activated), impl);
4621 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4622 G_CALLBACK (browse_files_key_press_event_cb), impl);
4623 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4624 G_CALLBACK (list_popup_menu_cb), impl);
4625 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4626 G_CALLBACK (list_button_press_event_cb), impl);
4628 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4629 G_CALLBACK (file_list_drag_data_received_cb), impl);
4630 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4631 G_CALLBACK (file_list_drag_drop_cb), impl);
4632 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4633 G_CALLBACK (file_list_drag_motion_cb), impl);
4635 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4636 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4637 G_CALLBACK (file_list_query_tooltip_cb), impl);
4639 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4640 gtk_tree_selection_set_select_function (selection,
4643 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4646 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4647 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4649 g_signal_connect (selection, "changed",
4650 G_CALLBACK (list_selection_changed), impl);
4652 /* Filename column */
4654 impl->list_name_column = gtk_tree_view_column_new ();
4655 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4656 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4657 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4659 renderer = gtk_cell_renderer_pixbuf_new ();
4660 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4661 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4662 list_icon_data_func, impl, NULL);
4664 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4665 g_object_set (impl->list_name_renderer,
4666 "ellipsize", PANGO_ELLIPSIZE_END,
4668 g_signal_connect (impl->list_name_renderer, "edited",
4669 G_CALLBACK (renderer_edited_cb), impl);
4670 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4671 G_CALLBACK (renderer_editing_canceled_cb), impl);
4672 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4673 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4674 list_name_data_func, impl, NULL);
4676 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4680 column = gtk_tree_view_column_new ();
4681 gtk_tree_view_column_set_resizable (column, TRUE);
4682 gtk_tree_view_column_set_title (column, _("Size"));
4684 renderer = gtk_cell_renderer_text_new ();
4685 g_object_set (renderer,
4686 "alignment", PANGO_ALIGN_RIGHT,
4688 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4689 gtk_tree_view_column_set_cell_data_func (column, renderer,
4690 list_size_data_func, impl, NULL);
4691 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4692 impl->list_size_column = column;
4694 /* Modification time column */
4696 column = gtk_tree_view_column_new ();
4697 gtk_tree_view_column_set_resizable (column, TRUE);
4698 gtk_tree_view_column_set_title (column, _("Modified"));
4700 renderer = gtk_cell_renderer_text_new ();
4701 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4702 gtk_tree_view_column_set_cell_data_func (column, renderer,
4703 list_mtime_data_func, impl, NULL);
4704 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4705 impl->list_mtime_column = column;
4707 file_list_set_sort_column_ids (impl);
4709 gtk_widget_show_all (swin);
4715 create_path_bar (GtkFileChooserDefault *impl)
4717 GtkWidget *path_bar;
4719 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4720 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4725 /* Creates the widgets for the files/folders pane */
4727 file_pane_create (GtkFileChooserDefault *impl,
4728 GtkSizeGroup *size_group)
4734 vbox = gtk_vbox_new (FALSE, 6);
4735 gtk_widget_show (vbox);
4737 /* Box for lists and preview */
4739 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4740 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4741 gtk_widget_show (hbox);
4745 widget = create_file_list (impl);
4746 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4750 impl->preview_box = gtk_vbox_new (FALSE, 12);
4751 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4752 /* Don't show preview box initially */
4756 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4758 widget = filter_create (impl);
4760 gtk_widget_show (widget);
4761 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4763 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4764 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4769 /* Callback used when the "Browse for more folders" expander is toggled */
4771 expander_changed_cb (GtkExpander *expander,
4773 GtkFileChooserDefault *impl)
4775 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4776 update_appearance (impl);
4779 /* Callback used when the selection changes in the save folder combo box */
4781 save_folder_combo_changed_cb (GtkComboBox *combo,
4782 GtkFileChooserDefault *impl)
4786 if (impl->changing_folder)
4789 if (gtk_combo_box_get_active_iter (combo, &iter))
4791 GtkTreeIter child_iter;
4793 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4796 shortcuts_activate_iter (impl, &child_iter);
4801 save_folder_update_tooltip (GtkComboBox *combo,
4802 GtkFileChooserDefault *impl)
4809 if (gtk_combo_box_get_active_iter (combo, &iter))
4811 GtkTreeIter child_iter;
4813 ShortcutType shortcut_type;
4815 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4818 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4819 SHORTCUTS_COL_DATA, &col_data,
4820 SHORTCUTS_COL_TYPE, &shortcut_type,
4823 if (shortcut_type == SHORTCUT_TYPE_FILE)
4824 tooltip = g_file_get_parse_name (G_FILE (col_data));
4827 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4828 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4829 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4833 /* Filter function used to filter out the Search item and its separator.
4834 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4837 shortcuts_combo_filter_func (GtkTreeModel *model,
4841 GtkFileChooserDefault *impl;
4842 GtkTreePath *tree_path;
4847 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4849 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4851 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4852 g_assert (tree_path != NULL);
4854 indices = gtk_tree_path_get_indices (tree_path);
4858 if (impl->has_search)
4860 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4861 if (idx == indices[0])
4865 if (impl->has_recent)
4867 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4868 if (idx == indices[0])
4872 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4873 if (idx == indices[0])
4878 gtk_tree_path_free (tree_path);
4883 /* Creates the combo box with the save folders */
4885 save_folder_combo_create (GtkFileChooserDefault *impl)
4888 GtkCellRenderer *cell;
4890 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4891 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4892 shortcuts_combo_filter_func,
4896 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4897 "model", impl->shortcuts_combo_filter_model,
4898 "focus-on-click", FALSE,
4900 gtk_widget_show (combo);
4902 cell = gtk_cell_renderer_pixbuf_new ();
4903 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4904 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4905 "pixbuf", SHORTCUTS_COL_PIXBUF,
4906 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4907 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4910 cell = gtk_cell_renderer_text_new ();
4911 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4912 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4913 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4914 "text", SHORTCUTS_COL_NAME,
4915 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4918 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4919 shortcuts_row_separator_func,
4922 g_signal_connect (combo, "changed",
4923 G_CALLBACK (save_folder_combo_changed_cb), impl);
4924 g_signal_connect (combo, "changed",
4925 G_CALLBACK (save_folder_update_tooltip), impl);
4930 /* Creates the widgets specific to Save mode */
4932 save_widgets_create (GtkFileChooserDefault *impl)
4937 GtkWidget *alignment;
4939 if (impl->save_widgets != NULL)
4942 location_switch_to_path_bar (impl);
4944 vbox = gtk_vbox_new (FALSE, 12);
4946 table = gtk_table_new (2, 2, FALSE);
4947 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4948 gtk_widget_show (table);
4949 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4950 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4954 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4955 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4956 gtk_table_attach (GTK_TABLE (table), widget,
4960 gtk_widget_show (widget);
4962 /* Location entry */
4964 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4965 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4967 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4968 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4969 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4970 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4972 GTK_EXPAND | GTK_FILL, 0,
4974 gtk_widget_show (impl->location_entry);
4975 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4978 impl->save_folder_label = gtk_label_new (NULL);
4979 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4980 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4984 gtk_widget_show (impl->save_folder_label);
4986 impl->save_folder_combo = save_folder_combo_create (impl);
4987 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4989 GTK_EXPAND | GTK_FILL, GTK_FILL,
4991 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4994 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4995 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4997 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4998 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4999 g_signal_connect (impl->save_expander, "notify::expanded",
5000 G_CALLBACK (expander_changed_cb),
5002 gtk_widget_show_all (alignment);
5004 impl->save_widgets = vbox;
5005 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
5006 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
5007 gtk_widget_show (impl->save_widgets);
5010 /* Destroys the widgets specific to Save mode */
5012 save_widgets_destroy (GtkFileChooserDefault *impl)
5014 if (impl->save_widgets == NULL)
5017 gtk_widget_destroy (impl->save_widgets);
5018 impl->save_widgets = NULL;
5019 impl->location_entry = NULL;
5020 impl->save_folder_label = NULL;
5021 impl->save_folder_combo = NULL;
5022 impl->save_expander = NULL;
5025 /* Turns on the path bar widget. Can be called even if we are already in that
5029 location_switch_to_path_bar (GtkFileChooserDefault *impl)
5031 if (impl->location_entry)
5033 gtk_widget_destroy (impl->location_entry);
5034 impl->location_entry = NULL;
5037 gtk_widget_hide (impl->location_entry_box);
5040 /* Sets the full path of the current folder as the text in the location entry. */
5042 location_entry_set_initial_text (GtkFileChooserDefault *impl)
5044 gchar *text, *filename;
5046 if (!impl->current_folder)
5049 filename = g_file_get_path (impl->current_folder);
5053 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
5057 text = g_file_get_uri (impl->current_folder);
5061 gboolean need_slash;
5064 len = strlen (text);
5065 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
5071 slash_text = g_new (char, len + 2);
5072 strcpy (slash_text, text);
5073 slash_text[len] = G_DIR_SEPARATOR;
5074 slash_text[len + 1] = 0;
5080 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5087 /* Turns on the location entry. Can be called even if we are already in that
5091 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5093 /* when in search or recent files mode, we are not showing the
5094 * location_entry_box container, so there's no point in switching
5097 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5098 impl->operation_mode == OPERATION_MODE_RECENT)
5101 if (impl->location_entry)
5102 gtk_widget_destroy (impl->location_entry);
5106 gtk_widget_show (impl->location_entry_box);
5110 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5111 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5113 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5114 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5116 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5117 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5119 /* Configure the entry */
5121 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5125 gtk_widget_show (impl->location_entry);
5126 gtk_widget_grab_focus (impl->location_entry);
5129 /* Sets a new location mode. set_buttons determines whether the toggle button
5130 * for the mode will also be changed.
5133 location_mode_set (GtkFileChooserDefault *impl,
5134 LocationMode new_mode,
5135 gboolean set_button)
5137 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5138 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5140 GtkWindow *toplevel;
5141 GtkWidget *current_focus;
5142 gboolean button_active;
5143 gboolean switch_to_file_list;
5147 case LOCATION_MODE_PATH_BAR:
5148 button_active = FALSE;
5150 /* The location_entry will disappear when we switch to path bar mode. So,
5151 * we'll focus the file list in that case, to avoid having a window with
5152 * no focused widget.
5154 toplevel = get_toplevel (GTK_WIDGET (impl));
5155 switch_to_file_list = FALSE;
5158 current_focus = gtk_window_get_focus (toplevel);
5159 if (!current_focus || current_focus == impl->location_entry)
5160 switch_to_file_list = TRUE;
5163 location_switch_to_path_bar (impl);
5165 if (switch_to_file_list)
5166 gtk_widget_grab_focus (impl->browse_files_tree_view);
5170 case LOCATION_MODE_FILENAME_ENTRY:
5171 button_active = TRUE;
5172 location_switch_to_filename_entry (impl);
5176 g_assert_not_reached ();
5182 g_signal_handlers_block_by_func (impl->location_button,
5183 G_CALLBACK (location_button_toggled_cb), impl);
5185 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5187 g_signal_handlers_unblock_by_func (impl->location_button,
5188 G_CALLBACK (location_button_toggled_cb), impl);
5192 impl->location_mode = new_mode;
5196 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5198 /* when in search or recent files mode, we are not showing the
5199 * location_entry_box container, so there's no point in switching
5202 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5203 impl->operation_mode == OPERATION_MODE_RECENT)
5206 /* If the file entry is not visible, show it.
5207 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5209 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5211 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5213 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5215 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5217 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5221 gtk_widget_grab_focus (impl->location_entry);
5226 /* Callback used when one of the location mode buttons is toggled */
5228 location_button_toggled_cb (GtkToggleButton *toggle,
5229 GtkFileChooserDefault *impl)
5232 LocationMode new_mode;
5234 is_active = gtk_toggle_button_get_active (toggle);
5238 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5239 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5243 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5244 new_mode = LOCATION_MODE_PATH_BAR;
5247 location_mode_set (impl, new_mode, FALSE);
5250 /* Creates a toggle button for the location entry. */
5252 location_button_create (GtkFileChooserDefault *impl)
5257 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5258 gtk_widget_show (image);
5260 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5264 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5266 g_signal_connect (impl->location_button, "toggled",
5267 G_CALLBACK (location_button_toggled_cb), impl);
5269 str = _("Type a file name");
5271 gtk_widget_set_tooltip_text (impl->location_button, str);
5272 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5275 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5277 browse_widgets_create (GtkFileChooserDefault *impl)
5282 GtkSizeGroup *size_group;
5284 /* size group is used by the [+][-] buttons and the filter combo */
5285 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5286 vbox = gtk_vbox_new (FALSE, 12);
5288 /* Location widgets */
5289 impl->browse_path_bar_hbox = gtk_hbox_new (FALSE, 12);
5290 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5291 gtk_widget_show (impl->browse_path_bar_hbox);
5293 /* Size group that allows the path bar to be the same size between modes */
5294 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5295 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5297 /* Location button */
5299 location_button_create (impl);
5300 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5304 impl->browse_path_bar = create_path_bar (impl);
5305 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5306 gtk_widget_show_all (impl->browse_path_bar);
5307 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5310 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5311 g_signal_connect (impl->browse_new_folder_button, "clicked",
5312 G_CALLBACK (new_folder_button_clicked), impl);
5313 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5315 /* Box for the location label and entry */
5317 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5318 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5320 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5321 gtk_widget_show (impl->location_label);
5322 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5325 hpaned = gtk_hpaned_new ();
5326 gtk_widget_show (hpaned);
5327 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5329 widget = shortcuts_pane_create (impl, size_group);
5330 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5331 widget = file_pane_create (impl, size_group);
5332 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5334 g_object_unref (size_group);
5340 gtk_file_chooser_default_constructor (GType type,
5341 guint n_construct_properties,
5342 GObjectConstructParam *construct_params)
5344 GtkFileChooserDefault *impl;
5347 profile_start ("start", NULL);
5349 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5350 n_construct_properties,
5352 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5354 g_assert (impl->file_system);
5356 gtk_widget_push_composite_child ();
5358 /* Shortcuts model */
5359 shortcuts_model_create (impl);
5361 /* The browse widgets */
5362 impl->browse_widgets = browse_widgets_create (impl);
5363 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5365 /* Alignment to hold extra widget */
5366 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5367 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5369 gtk_widget_pop_composite_child ();
5370 update_appearance (impl);
5372 profile_end ("end", NULL);
5377 /* Sets the extra_widget by packing it in the appropriate place */
5379 set_extra_widget (GtkFileChooserDefault *impl,
5380 GtkWidget *extra_widget)
5384 g_object_ref (extra_widget);
5385 /* FIXME: is this right ? */
5386 gtk_widget_show (extra_widget);
5389 if (impl->extra_widget)
5391 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5392 g_object_unref (impl->extra_widget);
5395 impl->extra_widget = extra_widget;
5396 if (impl->extra_widget)
5398 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5399 gtk_widget_show (impl->extra_align);
5402 gtk_widget_hide (impl->extra_align);
5406 set_local_only (GtkFileChooserDefault *impl,
5407 gboolean local_only)
5409 if (local_only != impl->local_only)
5411 impl->local_only = local_only;
5413 if (impl->location_entry)
5414 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5416 if (impl->shortcuts_model && impl->file_system)
5418 shortcuts_add_volumes (impl);
5419 shortcuts_add_bookmarks (impl);
5422 if (local_only && impl->current_folder &&
5423 !g_file_is_native (impl->current_folder))
5425 /* If we are pointing to a non-local folder, make an effort to change
5426 * back to a local folder, but it's really up to the app to not cause
5427 * such a situation, so we ignore errors.
5429 const gchar *home = g_get_home_dir ();
5435 home_file = g_file_new_for_path (home);
5437 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5439 g_object_unref (home_file);
5445 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5446 GtkFileChooserDefault *impl)
5448 shortcuts_add_volumes (impl);
5449 shortcuts_add_bookmarks (impl);
5451 bookmarks_check_add_sensitivity (impl);
5452 bookmarks_check_remove_sensitivity (impl);
5453 shortcuts_check_popup_sensitivity (impl);
5456 /* Sets the file chooser to multiple selection mode */
5458 set_select_multiple (GtkFileChooserDefault *impl,
5459 gboolean select_multiple,
5460 gboolean property_notify)
5462 GtkTreeSelection *selection;
5463 GtkSelectionMode mode;
5465 if (select_multiple == impl->select_multiple)
5468 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5470 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5471 gtk_tree_selection_set_mode (selection, mode);
5473 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5475 impl->select_multiple = select_multiple;
5476 g_object_notify (G_OBJECT (impl), "select-multiple");
5478 check_preview_change (impl);
5482 set_file_system_backend (GtkFileChooserDefault *impl)
5484 profile_start ("start for backend", "default");
5486 impl->file_system = _gtk_file_system_new ();
5488 g_signal_connect (impl->file_system, "volumes-changed",
5489 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5490 g_signal_connect (impl->file_system, "bookmarks-changed",
5491 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5493 profile_end ("end", NULL);
5497 unset_file_system_backend (GtkFileChooserDefault *impl)
5499 g_signal_handlers_disconnect_by_func (impl->file_system,
5500 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5502 g_object_unref (impl->file_system);
5504 impl->file_system = NULL;
5507 /* This function is basically a do_all function.
5509 * It sets the visibility on all the widgets based on the current state, and
5510 * moves the custom_widget if needed.
5513 update_appearance (GtkFileChooserDefault *impl)
5515 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5516 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5520 gtk_widget_hide (impl->location_button);
5521 save_widgets_create (impl);
5523 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5524 text = _("Save in _folder:");
5526 text = _("Create in _folder:");
5528 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5530 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5532 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5533 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5534 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5535 gtk_widget_show (impl->browse_widgets);
5539 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5540 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5541 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5542 gtk_widget_hide (impl->browse_widgets);
5545 if (impl->select_multiple)
5547 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5548 "Re-setting to single selection mode.");
5549 set_select_multiple (impl, FALSE, TRUE);
5552 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5553 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5555 gtk_widget_show (impl->location_button);
5556 save_widgets_destroy (impl);
5557 gtk_widget_show (impl->browse_widgets);
5558 location_mode_set (impl, impl->location_mode, TRUE);
5561 if (impl->location_entry)
5562 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5564 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5565 gtk_widget_hide (impl->browse_new_folder_button);
5567 gtk_widget_show (impl->browse_new_folder_button);
5569 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5570 * of files may change depending whether we are in a file or folder-only mode.
5572 gtk_widget_queue_draw (impl->browse_files_tree_view);
5574 emit_default_size_changed (impl);
5578 gtk_file_chooser_default_set_property (GObject *object,
5580 const GValue *value,
5584 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5588 case GTK_FILE_CHOOSER_PROP_ACTION:
5590 GtkFileChooserAction action = g_value_get_enum (value);
5592 if (action != impl->action)
5594 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5596 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5597 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5598 && impl->select_multiple)
5600 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5601 "this is not allowed in multiple selection mode. Resetting the file chooser "
5602 "to single selection mode.");
5603 set_select_multiple (impl, FALSE, TRUE);
5605 impl->action = action;
5606 update_appearance (impl);
5607 settings_load (impl);
5612 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5613 /* Ignore property */
5616 case GTK_FILE_CHOOSER_PROP_FILTER:
5617 set_current_filter (impl, g_value_get_object (value));
5620 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5621 set_local_only (impl, g_value_get_boolean (value));
5624 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5625 set_preview_widget (impl, g_value_get_object (value));
5628 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5629 impl->preview_widget_active = g_value_get_boolean (value);
5630 update_preview_widget_visibility (impl);
5633 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5634 impl->use_preview_label = g_value_get_boolean (value);
5635 update_preview_widget_visibility (impl);
5638 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5639 set_extra_widget (impl, g_value_get_object (value));
5642 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5644 gboolean select_multiple = g_value_get_boolean (value);
5645 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5646 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5649 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5650 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5651 "leaving the file chooser in single selection mode.");
5655 set_select_multiple (impl, select_multiple, FALSE);
5659 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5661 gboolean show_hidden = g_value_get_boolean (value);
5662 if (show_hidden != impl->show_hidden)
5664 impl->show_hidden = show_hidden;
5666 if (impl->browse_files_model)
5667 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5672 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5674 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5675 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5679 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5681 gboolean create_folders = g_value_get_boolean (value);
5682 impl->create_folders = create_folders;
5683 update_appearance (impl);
5688 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5694 gtk_file_chooser_default_get_property (GObject *object,
5699 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5703 case GTK_FILE_CHOOSER_PROP_ACTION:
5704 g_value_set_enum (value, impl->action);
5707 case GTK_FILE_CHOOSER_PROP_FILTER:
5708 g_value_set_object (value, impl->current_filter);
5711 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5712 g_value_set_boolean (value, impl->local_only);
5715 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5716 g_value_set_object (value, impl->preview_widget);
5719 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5720 g_value_set_boolean (value, impl->preview_widget_active);
5723 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5724 g_value_set_boolean (value, impl->use_preview_label);
5727 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5728 g_value_set_object (value, impl->extra_widget);
5731 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5732 g_value_set_boolean (value, impl->select_multiple);
5735 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5736 g_value_set_boolean (value, impl->show_hidden);
5739 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5740 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5743 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5744 g_value_set_boolean (value, impl->create_folders);
5748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5753 /* Removes the settings signal handler. It's safe to call multiple times */
5755 remove_settings_signal (GtkFileChooserDefault *impl,
5758 if (impl->settings_signal_id)
5760 GtkSettings *settings;
5762 settings = gtk_settings_get_for_screen (screen);
5763 g_signal_handler_disconnect (settings,
5764 impl->settings_signal_id);
5765 impl->settings_signal_id = 0;
5770 gtk_file_chooser_default_dispose (GObject *object)
5773 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5775 if (impl->extra_widget)
5777 g_object_unref (impl->extra_widget);
5778 impl->extra_widget = NULL;
5781 pending_select_files_free (impl);
5783 /* cancel all pending operations */
5784 if (impl->pending_cancellables)
5786 for (l = impl->pending_cancellables; l; l = l->next)
5788 GCancellable *cancellable = G_CANCELLABLE (l->data);
5789 g_cancellable_cancel (cancellable);
5791 g_slist_free (impl->pending_cancellables);
5792 impl->pending_cancellables = NULL;
5795 if (impl->reload_icon_cancellables)
5797 for (l = impl->reload_icon_cancellables; l; l = l->next)
5799 GCancellable *cancellable = G_CANCELLABLE (l->data);
5800 g_cancellable_cancel (cancellable);
5802 g_slist_free (impl->reload_icon_cancellables);
5803 impl->reload_icon_cancellables = NULL;
5806 if (impl->loading_shortcuts)
5808 for (l = impl->loading_shortcuts; l; l = l->next)
5810 GCancellable *cancellable = G_CANCELLABLE (l->data);
5811 g_cancellable_cancel (cancellable);
5813 g_slist_free (impl->loading_shortcuts);
5814 impl->loading_shortcuts = NULL;
5817 if (impl->file_list_drag_data_received_cancellable)
5819 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5820 impl->file_list_drag_data_received_cancellable = NULL;
5823 if (impl->update_current_folder_cancellable)
5825 g_cancellable_cancel (impl->update_current_folder_cancellable);
5826 impl->update_current_folder_cancellable = NULL;
5829 if (impl->show_and_select_files_cancellable)
5831 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5832 impl->show_and_select_files_cancellable = NULL;
5835 if (impl->should_respond_get_info_cancellable)
5837 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5838 impl->should_respond_get_info_cancellable = NULL;
5841 if (impl->update_from_entry_cancellable)
5843 g_cancellable_cancel (impl->update_from_entry_cancellable);
5844 impl->update_from_entry_cancellable = NULL;
5847 if (impl->shortcuts_activate_iter_cancellable)
5849 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5850 impl->shortcuts_activate_iter_cancellable = NULL;
5853 search_stop_searching (impl, TRUE);
5854 recent_stop_loading (impl);
5856 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5858 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5861 /* We override show-all since we have internal widgets that
5862 * shouldn't be shown when you call show_all(), like the filter
5866 gtk_file_chooser_default_show_all (GtkWidget *widget)
5868 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5870 gtk_widget_show (widget);
5872 if (impl->extra_widget)
5873 gtk_widget_show_all (impl->extra_widget);
5876 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5877 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5880 toplevel_set_focus_cb (GtkWindow *window,
5882 GtkFileChooserDefault *impl)
5884 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5887 /* We monitor the focus widget on our toplevel to be able to know which widget
5888 * was last focused at the time our "should_respond" method gets called.
5891 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5892 GtkWidget *previous_toplevel)
5894 GtkFileChooserDefault *impl;
5895 GtkWidget *toplevel;
5897 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5899 if (previous_toplevel)
5901 g_assert (impl->toplevel_set_focus_id != 0);
5902 g_signal_handler_disconnect (previous_toplevel,
5903 impl->toplevel_set_focus_id);
5904 impl->toplevel_set_focus_id = 0;
5905 impl->toplevel_last_focus_widget = NULL;
5908 g_assert (impl->toplevel_set_focus_id == 0);
5910 toplevel = gtk_widget_get_toplevel (widget);
5911 if (GTK_IS_WINDOW (toplevel))
5913 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5914 G_CALLBACK (toplevel_set_focus_cb), impl);
5915 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5919 /* Changes the icons wherever it is needed */
5921 change_icon_theme (GtkFileChooserDefault *impl)
5923 GtkSettings *settings;
5926 profile_start ("start", NULL);
5928 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5930 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5931 impl->icon_size = MAX (width, height);
5933 impl->icon_size = FALLBACK_ICON_SIZE;
5935 shortcuts_reload_icons (impl);
5936 gtk_widget_queue_resize (impl->browse_files_tree_view);
5938 profile_end ("end", NULL);
5941 /* Callback used when a GtkSettings value changes */
5943 settings_notify_cb (GObject *object,
5945 GtkFileChooserDefault *impl)
5949 profile_start ("start", NULL);
5951 name = g_param_spec_get_name (pspec);
5953 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5954 strcmp (name, "gtk-icon-sizes") == 0)
5955 change_icon_theme (impl);
5957 profile_end ("end", NULL);
5960 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5964 check_icon_theme (GtkFileChooserDefault *impl)
5966 GtkSettings *settings;
5968 profile_start ("start", NULL);
5970 if (impl->settings_signal_id)
5972 profile_end ("end", NULL);
5976 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5978 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5979 impl->settings_signal_id = g_signal_connect (settings, "notify",
5980 G_CALLBACK (settings_notify_cb), impl);
5982 change_icon_theme (impl);
5985 profile_end ("end", NULL);
5989 gtk_file_chooser_default_style_set (GtkWidget *widget,
5990 GtkStyle *previous_style)
5992 GtkFileChooserDefault *impl;
5994 profile_start ("start", NULL);
5996 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5998 profile_msg (" parent class style_set start", NULL);
5999 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
6000 profile_msg (" parent class style_set end", NULL);
6002 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
6003 change_icon_theme (impl);
6005 emit_default_size_changed (impl);
6007 profile_end ("end", NULL);
6011 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
6012 GdkScreen *previous_screen)
6014 GtkFileChooserDefault *impl;
6016 profile_start ("start", NULL);
6018 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6020 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
6021 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
6023 remove_settings_signal (impl, previous_screen);
6024 check_icon_theme (impl);
6026 emit_default_size_changed (impl);
6028 profile_end ("end", NULL);
6032 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
6033 GtkAllocation *allocation)
6035 GtkFileChooserDefault *impl;
6037 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6039 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6043 get_is_file_filtered (GtkFileChooserDefault *impl,
6045 GFileInfo *file_info)
6047 GtkFileFilterInfo filter_info;
6048 GtkFileFilterFlags needed;
6051 if (!impl->current_filter)
6054 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
6056 needed = gtk_file_filter_get_needed (impl->current_filter);
6058 filter_info.display_name = g_file_info_get_display_name (file_info);
6059 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
6061 if (needed & GTK_FILE_FILTER_FILENAME)
6063 filter_info.filename = g_file_get_path (file);
6064 if (filter_info.filename)
6065 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
6068 filter_info.filename = NULL;
6070 if (needed & GTK_FILE_FILTER_URI)
6072 filter_info.uri = g_file_get_uri (file);
6073 if (filter_info.uri)
6074 filter_info.contains |= GTK_FILE_FILTER_URI;
6077 filter_info.uri = NULL;
6079 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
6081 g_free ((gchar *)filter_info.filename);
6082 g_free ((gchar *)filter_info.uri);
6083 g_free ((gchar *)filter_info.mime_type);
6089 set_sort_column (GtkFileChooserDefault *impl)
6091 GtkTreeSortable *sortable;
6093 switch (impl->operation_mode)
6095 case OPERATION_MODE_BROWSE:
6096 sortable = GTK_TREE_SORTABLE (impl->sort_model);
6099 case OPERATION_MODE_SEARCH:
6100 sortable = GTK_TREE_SORTABLE (impl->search_model_sort);
6103 case OPERATION_MODE_RECENT:
6104 sortable = GTK_TREE_SORTABLE (impl->recent_model_sort);
6108 g_assert_not_reached ();
6112 gtk_tree_sortable_set_sort_column_id (sortable,
6118 settings_load (GtkFileChooserDefault *impl)
6120 GtkFileChooserSettings *settings;
6121 LocationMode location_mode;
6122 gboolean show_hidden;
6123 gboolean expand_folders;
6124 gboolean show_size_column;
6126 GtkSortType sort_order;
6128 settings = _gtk_file_chooser_settings_new ();
6130 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6131 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6132 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6133 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
6134 sort_column = _gtk_file_chooser_settings_get_sort_column (settings);
6135 sort_order = _gtk_file_chooser_settings_get_sort_order (settings);
6137 g_object_unref (settings);
6139 location_mode_set (impl, location_mode, TRUE);
6141 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6143 impl->expand_folders = expand_folders;
6144 if (impl->save_expander)
6145 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6147 impl->show_size_column = show_size_column;
6148 if (impl->list_size_column)
6149 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6151 impl->sort_column = sort_column;
6152 impl->sort_order = sort_order;
6153 /* We don't call set_sort_column() here as the models may not have been
6154 * created yet. The individual functions that create and set the models will
6155 * call set_sort_column() themselves.
6160 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
6162 GtkWindow *toplevel;
6163 int x, y, width, height;
6165 /* We don't save the geometry in non-expanded "save" mode, so that the "little
6166 * dialog" won't make future Open dialogs too small.
6168 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6169 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6170 || impl->expand_folders))
6173 toplevel = get_toplevel (GTK_WIDGET (impl));
6175 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6178 gtk_window_get_position (toplevel, &x, &y);
6179 gtk_window_get_size (toplevel, &width, &height);
6181 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
6185 settings_save (GtkFileChooserDefault *impl)
6187 GtkFileChooserSettings *settings;
6189 settings = _gtk_file_chooser_settings_new ();
6191 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6192 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6193 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6194 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6195 _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column);
6196 _gtk_file_chooser_settings_set_sort_order (settings, impl->sort_order);
6198 save_dialog_geometry (impl, settings);
6201 _gtk_file_chooser_settings_save (settings, NULL);
6203 g_object_unref (settings);
6206 /* GtkWidget::realize method */
6208 gtk_file_chooser_default_realize (GtkWidget *widget)
6210 GtkFileChooserDefault *impl;
6212 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6214 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6216 emit_default_size_changed (impl);
6219 /* GtkWidget::map method */
6221 gtk_file_chooser_default_map (GtkWidget *widget)
6223 GtkFileChooserDefault *impl;
6224 char *current_working_dir;
6226 profile_start ("start", NULL);
6228 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6230 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6232 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6234 switch (impl->reload_state)
6237 /* The user didn't explicitly give us a folder to
6238 * display, so we'll use the cwd
6240 current_working_dir = g_get_current_dir ();
6241 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6242 current_working_dir);
6243 g_free (current_working_dir);
6246 case RELOAD_HAS_FOLDER:
6247 /* Nothing; we are already loading or loaded, so we
6248 * don't need to reload
6252 case RELOAD_WAS_UNMAPPED:
6253 /* Just reload the current folder; else continue
6256 if (impl->current_folder)
6258 pending_select_files_store_selection (impl);
6259 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6264 g_assert_not_reached ();
6268 volumes_bookmarks_changed_cb (impl->file_system, impl);
6270 settings_load (impl);
6272 profile_end ("end", NULL);
6275 /* GtkWidget::unmap method */
6277 gtk_file_chooser_default_unmap (GtkWidget *widget)
6279 GtkFileChooserDefault *impl;
6281 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6283 settings_save (impl);
6285 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6287 impl->reload_state = RELOAD_WAS_UNMAPPED;
6291 list_model_filter_func (GtkFileSystemModel *model,
6293 GFileInfo *file_info,
6296 GtkFileChooserDefault *impl = user_data;
6298 if (!impl->current_filter)
6301 if (_gtk_file_info_consider_as_directory (file_info))
6304 return !get_is_file_filtered (impl, file, file_info);
6308 install_list_model_filter (GtkFileChooserDefault *impl)
6310 GtkFileSystemModelFilter filter;
6313 g_assert (impl->browse_files_model != NULL);
6315 if (impl->current_filter)
6317 filter = list_model_filter_func;
6326 _gtk_file_system_model_set_filter (impl->browse_files_model,
6331 #define COMPARE_DIRECTORIES \
6332 GtkFileChooserDefault *impl = user_data; \
6333 gboolean dir_a, dir_b; \
6335 gtk_tree_model_get (model, a, MODEL_COL_IS_FOLDER, &dir_a, -1); \
6336 gtk_tree_model_get (model, b, MODEL_COL_IS_FOLDER, &dir_b, -1); \
6338 if (dir_a != dir_b) \
6339 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6341 /* Sort callback for the filename column */
6343 name_sort_func (GtkTreeModel *model,
6348 COMPARE_DIRECTORIES;
6351 gchar *key_a, *key_b;
6354 gtk_tree_model_get (model, a, MODEL_COL_NAME_COLLATED, &key_a, -1);
6355 gtk_tree_model_get (model, b, MODEL_COL_NAME_COLLATED, &key_b, -1);
6358 result = strcmp (key_a, key_b);
6373 /* Sort callback for the size column */
6375 size_sort_func (GtkTreeModel *model,
6380 COMPARE_DIRECTORIES;
6383 gint64 size_a, size_b;
6385 gtk_tree_model_get (model, a, MODEL_COL_SIZE, &size_a, -1);
6386 gtk_tree_model_get (model, b, MODEL_COL_SIZE, &size_b, -1);
6388 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6392 /* Sort callback for the mtime column */
6394 mtime_sort_func (GtkTreeModel *model,
6399 COMPARE_DIRECTORIES;
6404 gtk_tree_model_get (model, a, MODEL_COL_MTIME, &ta, -1);
6405 gtk_tree_model_get (model, b, MODEL_COL_MTIME, &tb, -1);
6407 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6411 /* Callback used when the sort column changes. We cache the sort order for use
6412 * in name_sort_func().
6415 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6416 GtkFileChooserDefault *impl)
6418 gint sort_column_id;
6419 GtkSortType sort_type;
6421 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6423 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6424 impl->sort_column = sort_column_id;
6425 impl->sort_order = sort_type;
6430 set_busy_cursor (GtkFileChooserDefault *impl,
6433 GtkWindow *toplevel;
6434 GdkDisplay *display;
6437 toplevel = get_toplevel (GTK_WIDGET (impl));
6438 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6441 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6444 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6448 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6449 gdk_display_flush (display);
6452 gdk_cursor_unref (cursor);
6455 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6457 load_set_model (GtkFileChooserDefault *impl)
6459 profile_start ("start", NULL);
6461 g_assert (impl->browse_files_model != NULL);
6462 g_assert (impl->sort_model == NULL);
6464 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6465 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6466 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6467 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6468 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6469 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6470 set_sort_column (impl);
6471 impl->list_sort_ascending = TRUE;
6472 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6474 g_signal_connect (impl->sort_model, "sort-column-changed",
6475 G_CALLBACK (list_sort_column_changed_cb), impl);
6477 profile_msg (" gtk_tree_view_set_model start", NULL);
6478 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6479 GTK_TREE_MODEL (impl->sort_model));
6480 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6481 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6483 profile_msg (" gtk_tree_view_set_model end", NULL);
6485 profile_end ("end", NULL);
6488 /* Timeout callback used when the loading timer expires */
6490 load_timeout_cb (gpointer data)
6492 GtkFileChooserDefault *impl;
6494 profile_start ("start", NULL);
6496 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6497 g_assert (impl->load_state == LOAD_PRELOAD);
6498 g_assert (impl->load_timeout_id != 0);
6499 g_assert (impl->browse_files_model != NULL);
6501 impl->load_timeout_id = 0;
6502 impl->load_state = LOAD_LOADING;
6504 load_set_model (impl);
6506 profile_end ("end", NULL);
6511 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6513 load_setup_timer (GtkFileChooserDefault *impl)
6515 g_assert (impl->load_timeout_id == 0);
6516 g_assert (impl->load_state != LOAD_PRELOAD);
6518 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6519 impl->load_state = LOAD_PRELOAD;
6522 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6524 load_remove_timer (GtkFileChooserDefault *impl)
6526 if (impl->load_timeout_id != 0)
6528 g_assert (impl->load_state == LOAD_PRELOAD);
6530 g_source_remove (impl->load_timeout_id);
6531 impl->load_timeout_id = 0;
6532 impl->load_state = LOAD_EMPTY;
6535 g_assert (impl->load_state == LOAD_EMPTY ||
6536 impl->load_state == LOAD_LOADING ||
6537 impl->load_state == LOAD_FINISHED);
6540 /* Selects the first row in the file list */
6542 browse_files_select_first_row (GtkFileChooserDefault *impl)
6545 GtkTreeIter dummy_iter;
6546 GtkTreeModel *tree_model;
6548 if (!impl->sort_model)
6551 path = gtk_tree_path_new_from_indices (0, -1);
6552 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6554 /* If the list is empty, do nothing. */
6555 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6556 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6558 gtk_tree_path_free (path);
6561 struct center_selected_row_closure {
6562 GtkFileChooserDefault *impl;
6563 gboolean already_centered;
6566 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6567 * selected row in the tree view.
6570 center_selected_row_foreach_cb (GtkTreeModel *model,
6575 struct center_selected_row_closure *closure;
6578 if (closure->already_centered)
6581 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6582 closure->already_centered = TRUE;
6585 /* Centers the selected row in the tree view */
6587 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6589 struct center_selected_row_closure closure;
6590 GtkTreeSelection *selection;
6592 closure.impl = impl;
6593 closure.already_centered = FALSE;
6595 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6596 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6599 struct ShowAndSelectPathsData
6601 GtkFileChooserDefault *impl;
6606 show_and_select_files_finished_loading (GtkFolder *folder,
6609 gboolean have_hidden;
6610 gboolean have_filtered;
6612 struct ShowAndSelectPathsData *data = user_data;
6614 have_hidden = FALSE;
6615 have_filtered = FALSE;
6617 for (l = data->files; l; l = l->next)
6624 info = _gtk_folder_get_info (folder, file);
6628 have_hidden = g_file_info_get_is_hidden (info)
6629 || g_file_info_get_is_backup (info);
6632 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6633 get_is_file_filtered (data->impl, file, info);
6635 g_object_unref (info);
6637 if (have_hidden && have_filtered)
6638 break; /* we now have all the information we need */
6642 g_signal_handlers_disconnect_by_func (folder,
6643 show_and_select_files_finished_loading,
6647 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6650 set_current_filter (data->impl, NULL);
6652 for (l = data->files; l; l = l->next)
6659 if (!_gtk_file_system_model_get_iter_for_file (data->impl->browse_files_model,
6664 path = gtk_tree_model_get_path (GTK_TREE_MODEL (data->impl->browse_files_model), &iter);
6665 select_func (data->impl->browse_files_model, path, &iter, data->impl);
6666 gtk_tree_path_free (path);
6669 browse_files_center_selected_row (data->impl);
6671 g_object_unref (data->impl);
6672 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6673 g_slist_free (data->files);
6678 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6680 const GError *error,
6683 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6684 struct ShowAndSelectPathsData *data = user_data;
6686 if (data->impl->show_and_select_files_cancellable != cancellable)
6689 data->impl->show_and_select_files_cancellable = NULL;
6691 if (cancelled || error)
6694 g_object_unref (cancellable);
6696 if (_gtk_folder_is_finished_loading (folder))
6697 show_and_select_files_finished_loading (folder, user_data);
6699 g_signal_connect (folder, "finished-loading",
6700 G_CALLBACK (show_and_select_files_finished_loading),
6706 g_object_unref (data->impl);
6707 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6708 g_slist_free (data->files);
6711 g_object_unref (cancellable);
6715 show_and_select_files (GtkFileChooserDefault *impl,
6720 struct ShowAndSelectPathsData *info;
6722 profile_start ("start", NULL);
6726 profile_end ("end", NULL);
6730 info = g_new (struct ShowAndSelectPathsData, 1);
6731 info->impl = g_object_ref (impl);
6732 info->files = g_slist_copy (files);
6733 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6735 if (impl->show_and_select_files_cancellable)
6736 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6738 impl->show_and_select_files_cancellable =
6739 _gtk_file_system_get_folder (impl->file_system, parent_file,
6740 "standard::is-hidden,standard::is-backup,standard::type,standard::name,standard::content-type",
6741 show_and_select_files_get_folder_cb, info);
6743 profile_end ("end", NULL);
6747 /* Processes the pending operation when a folder is finished loading */
6749 pending_select_files_process (GtkFileChooserDefault *impl)
6751 g_assert (impl->load_state == LOAD_FINISHED);
6752 g_assert (impl->browse_files_model != NULL);
6753 g_assert (impl->sort_model != NULL);
6755 if (impl->pending_select_files)
6758 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6759 pending_select_files_free (impl);
6760 browse_files_center_selected_row (impl);
6764 /* We only select the first row if the chooser is actually mapped ---
6765 * selecting the first row is to help the user when he is interacting with
6766 * the chooser, but sometimes a chooser works not on behalf of the user,
6767 * but rather on behalf of something else like GtkFileChooserButton. In
6768 * that case, the chooser's selection should be what the caller expects,
6769 * as the user can't see that something else got selected. See bug #165264.
6771 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6772 browse_files_select_first_row (impl);
6775 g_assert (impl->pending_select_files == NULL);
6778 /* Callback used when the file system model finishes loading */
6780 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6782 GtkFileChooserDefault *impl)
6784 profile_start ("start", NULL);
6786 if (impl->load_state == LOAD_PRELOAD)
6788 load_remove_timer (impl);
6789 load_set_model (impl);
6791 else if (impl->load_state == LOAD_LOADING)
6797 /* We can't g_assert_not_reached(), as something other than us may have
6798 * initiated a folder reload. See #165556.
6800 profile_end ("end", NULL);
6804 g_assert (impl->load_timeout_id == 0);
6806 impl->load_state = LOAD_FINISHED;
6808 pending_select_files_process (impl);
6809 set_busy_cursor (impl, FALSE);
6810 #ifdef PROFILE_FILE_CHOOSER
6811 access ("MARK: *** FINISHED LOADING", F_OK);
6814 profile_end ("end", NULL);
6818 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6820 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6822 if (impl->browse_files_model)
6824 g_object_unref (impl->browse_files_model);
6825 impl->browse_files_model = NULL;
6828 if (impl->sort_model)
6830 g_object_unref (impl->sort_model);
6831 impl->sort_model = NULL;
6834 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6838 my_g_format_time_for_display (glong secs)
6843 time_t time_mtime, time_now;
6844 const gchar *format;
6845 gchar *locale_format = NULL;
6847 char *date_str = NULL;
6851 #ifdef HAVE_LOCALTIME_R
6852 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6855 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
6859 g_warning ("ptm != NULL failed");
6861 return g_strdup (_("Unknown"));
6864 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6866 #endif /* HAVE_LOCALTIME_R */
6868 g_date_set_time_t (&mtime, time_mtime);
6869 time_now = time (NULL);
6870 g_date_set_time_t (&now, time_now);
6872 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6874 /* Translators: %H means "hours" and %M means "minutes" */
6876 format = _("%H:%M");
6877 else if (days_diff == 1)
6878 format = _("Yesterday at %H:%M");
6881 if (days_diff > 1 && days_diff < 7)
6882 format = "%A"; /* Days from last week */
6884 format = "%x"; /* Any other date */
6888 /* g_locale_from_utf8() returns a string in the system
6889 * code-page, which is not always the same as that used by the C
6890 * library. For instance when running a GTK+ program with
6891 * LANG=ko on an English version of Windows, the system
6892 * code-page is 1252, but the code-page used by the C library is
6893 * 949. (It's GTK+ itself that sets the C library locale when it
6894 * notices the LANG environment variable. See gtkmain.c The
6895 * Microsoft C library doesn't look at any locale environment
6896 * variables.) We need to pass strftime() a string in the C
6897 * library's code-page. See bug #509885.
6899 locale = setlocale (LC_ALL, NULL);
6901 dot = strchr (locale, '.');
6904 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6906 /* All codepages should fit in 16 bits AFAIK */
6907 if (codepage > 0 && codepage < 65536)
6909 sprintf (charset, "CP%u", (guint) codepage);
6910 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6914 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6916 if (locale_format != NULL &&
6917 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6920 /* As above but in opposite direction... */
6921 if (codepage > 0 && codepage < 65536)
6922 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6924 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6928 if (date_str == NULL)
6929 date_str = g_strdup (_("Unknown"));
6931 g_free (locale_format);
6936 file_system_model_set (GtkFileSystemModel *model,
6943 GtkFileChooserDefault *impl = data;
6947 case MODEL_COL_FILE:
6948 g_value_set_object (value, file);
6950 case MODEL_COL_NAME:
6952 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6954 g_value_set_string (value, g_file_info_get_display_name (info));
6956 case MODEL_COL_NAME_COLLATED:
6958 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6960 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6962 case MODEL_COL_IS_FOLDER:
6963 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6965 case MODEL_COL_PIXBUF:
6967 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6969 g_value_set_object (value, NULL);
6971 case MODEL_COL_SIZE:
6972 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6974 case MODEL_COL_SIZE_TEXT:
6975 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6976 g_value_set_string (value, NULL);
6978 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6980 case MODEL_COL_MTIME:
6981 case MODEL_COL_MTIME_TEXT:
6986 g_file_info_get_modification_time (info, &tv);
6987 if (column == MODEL_COL_MTIME)
6988 g_value_set_long (value, tv.tv_sec);
6989 else if (tv.tv_sec == 0)
6990 g_value_set_static_string (value, _("Unknown"));
6992 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6995 case MODEL_COL_ELLIPSIZE:
6996 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6999 g_assert_not_reached ();
7006 /* Gets rid of the old list model and creates a new one for the current folder */
7008 set_list_model (GtkFileChooserDefault *impl,
7011 g_assert (impl->current_folder != NULL);
7013 profile_start ("start", NULL);
7015 stop_loading_and_clear_list_model (impl);
7017 set_busy_cursor (impl, TRUE);
7018 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
7020 impl->browse_files_model =
7021 _gtk_file_system_model_new (impl->current_folder,
7022 "standard,time,thumbnail::*",
7023 file_system_model_set,
7025 MODEL_COL_NUM_COLUMNS,
7035 PANGO_TYPE_ELLIPSIZE_MODE);
7037 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
7039 g_signal_connect (impl->browse_files_model, "finished-loading",
7040 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
7042 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
7044 install_list_model_filter (impl);
7046 profile_end ("end", NULL);
7051 struct update_chooser_entry_selected_foreach_closure {
7053 GtkTreeIter first_selected_iter;
7057 compare_utf8_filenames (const gchar *a,
7060 gchar *a_folded, *b_folded;
7063 a_folded = g_utf8_strdown (a, -1);
7064 b_folded = g_utf8_strdown (b, -1);
7066 retval = strcmp (a_folded, b_folded);
7075 update_chooser_entry_selected_foreach (GtkTreeModel *model,
7080 struct update_chooser_entry_selected_foreach_closure *closure;
7083 closure->num_selected++;
7085 if (closure->num_selected == 1)
7086 closure->first_selected_iter = *iter;
7090 update_chooser_entry (GtkFileChooserDefault *impl)
7092 GtkTreeSelection *selection;
7093 struct update_chooser_entry_selected_foreach_closure closure;
7094 const char *file_part;
7096 /* no need to update the file chooser's entry if there's no entry */
7097 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7098 impl->operation_mode == OPERATION_MODE_RECENT ||
7099 !impl->location_entry)
7102 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7103 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7104 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7105 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7106 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
7109 g_assert (impl->location_entry != NULL);
7111 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7112 closure.num_selected = 0;
7113 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7117 if (closure.num_selected == 0)
7119 goto maybe_clear_entry;
7121 else if (closure.num_selected == 1)
7123 GtkTreeIter child_iter;
7125 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7128 gboolean change_entry;
7130 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7132 &closure.first_selected_iter);
7134 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7136 /* If the cursor moved to the row of the newly created folder,
7137 * retrieving info will return NULL.
7142 g_free (impl->browse_files_last_selected_name);
7143 impl->browse_files_last_selected_name =
7144 g_strdup (g_file_info_get_display_name (info));
7146 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7147 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7148 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7150 /* We don't want the name to change when clicking on a folder... */
7151 change_entry = (! _gtk_file_info_consider_as_directory (info));
7154 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7158 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7160 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7161 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7169 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7170 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7172 /* Multiple selection, so just clear the entry. */
7174 g_free (impl->browse_files_last_selected_name);
7175 impl->browse_files_last_selected_name = NULL;
7177 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7183 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7184 && impl->browse_files_last_selected_name)
7186 const char *entry_text;
7188 gboolean clear_entry;
7190 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7191 len = strlen (entry_text);
7194 /* The file chooser entry may have appended a "/" to its text. So
7195 * take it out, and compare the result to the old selection.
7197 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7201 tmp = g_strndup (entry_text, len - 1);
7202 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7206 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7209 clear_entry = FALSE;
7212 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7217 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7221 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7225 struct UpdateCurrentFolderData
7227 GtkFileChooserDefault *impl;
7229 gboolean keep_trail;
7230 gboolean clear_entry;
7231 GFile *original_file;
7232 GError *original_error;
7236 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7237 GtkFileSystemVolume *volume,
7238 const GError *error,
7241 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7242 struct UpdateCurrentFolderData *data = user_data;
7243 GtkFileChooserDefault *impl = data->impl;
7245 if (cancellable != impl->update_current_folder_cancellable)
7248 impl->update_current_folder_cancellable = NULL;
7249 set_busy_cursor (impl, FALSE);
7256 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7257 impl->reload_state = RELOAD_EMPTY;
7261 change_folder_and_display_error (impl, data->file, data->clear_entry);
7264 g_object_unref (data->file);
7267 g_object_unref (cancellable);
7271 update_current_folder_get_info_cb (GCancellable *cancellable,
7273 const GError *error,
7276 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7277 struct UpdateCurrentFolderData *data = user_data;
7278 GtkFileChooserDefault *impl = data->impl;
7280 if (cancellable != impl->update_current_folder_cancellable)
7283 impl->update_current_folder_cancellable = NULL;
7284 impl->reload_state = RELOAD_EMPTY;
7286 set_busy_cursor (impl, FALSE);
7295 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7297 GMountOperation *mount_operation;
7298 GtkWidget *toplevel;
7300 g_object_unref (cancellable);
7301 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7303 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7305 set_busy_cursor (impl, TRUE);
7307 impl->update_current_folder_cancellable =
7308 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7310 update_current_folder_mount_enclosing_volume_cb,
7316 if (!data->original_file)
7318 data->original_file = g_object_ref (data->file);
7319 data->original_error = g_error_copy (error);
7322 parent_file = g_file_get_parent (data->file);
7324 /* get parent path and try to change the folder to that */
7327 g_object_unref (data->file);
7328 data->file = parent_file;
7330 g_object_unref (cancellable);
7332 /* restart the update current folder operation */
7333 impl->reload_state = RELOAD_HAS_FOLDER;
7335 impl->update_current_folder_cancellable =
7336 _gtk_file_system_get_info (impl->file_system, data->file,
7338 update_current_folder_get_info_cb,
7341 set_busy_cursor (impl, TRUE);
7347 /* Error and bail out, ignoring "not found" errors since they're useless:
7348 * they only happen when a program defaults to a folder that has been (re)moved.
7350 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7351 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7353 g_error_free (data->original_error);
7355 g_object_unref (data->original_file);
7361 if (data->original_file)
7363 /* Error and bail out, ignoring "not found" errors since they're useless:
7364 * they only happen when a program defaults to a folder that has been (re)moved.
7366 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7367 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7369 g_error_free (data->original_error);
7371 g_object_unref (data->original_file);
7374 if (! _gtk_file_info_consider_as_directory (info))
7377 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7380 if (impl->current_folder != data->file)
7382 if (impl->current_folder)
7383 g_object_unref (impl->current_folder);
7385 impl->current_folder = g_object_ref (data->file);
7388 impl->reload_state = RELOAD_HAS_FOLDER;
7390 /* Update the widgets that may trigger a folder change themselves. */
7392 if (!impl->changing_folder)
7394 impl->changing_folder = TRUE;
7396 shortcuts_update_current_folder (impl);
7398 impl->changing_folder = FALSE;
7401 /* Set the folder on the save entry */
7403 if (impl->location_entry)
7405 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7406 impl->current_folder);
7408 if (data->clear_entry)
7409 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7412 /* Create a new list model. This is slightly evil; we store the result value
7413 * but perform more actions rather than returning immediately even if it
7414 * generates an error.
7416 set_list_model (impl, NULL);
7418 /* Refresh controls */
7420 shortcuts_find_current_folder (impl);
7422 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7424 check_preview_change (impl);
7425 bookmarks_check_add_sensitivity (impl);
7427 g_signal_emit_by_name (impl, "selection-changed", 0);
7430 g_object_unref (data->file);
7433 g_object_unref (cancellable);
7437 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7439 gboolean keep_trail,
7440 gboolean clear_entry,
7443 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7444 struct UpdateCurrentFolderData *data;
7446 profile_start ("start", NULL);
7448 g_object_ref (file);
7450 switch (impl->operation_mode)
7452 case OPERATION_MODE_SEARCH:
7453 search_switch_to_browse_mode (impl);
7455 case OPERATION_MODE_RECENT:
7456 recent_switch_to_browse_mode (impl);
7458 case OPERATION_MODE_BROWSE:
7462 if (impl->local_only && !g_file_is_native (file))
7464 g_set_error_literal (error,
7465 GTK_FILE_CHOOSER_ERROR,
7466 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7467 _("Cannot change to folder because it is not local"));
7469 g_object_unref (file);
7470 profile_end ("end - not local", NULL);
7474 if (impl->update_current_folder_cancellable)
7475 g_cancellable_cancel (impl->update_current_folder_cancellable);
7477 /* Test validity of path here. */
7478 data = g_new0 (struct UpdateCurrentFolderData, 1);
7480 data->file = g_object_ref (file);
7481 data->keep_trail = keep_trail;
7482 data->clear_entry = clear_entry;
7484 impl->reload_state = RELOAD_HAS_FOLDER;
7486 impl->update_current_folder_cancellable =
7487 _gtk_file_system_get_info (impl->file_system, file,
7489 update_current_folder_get_info_cb,
7492 set_busy_cursor (impl, TRUE);
7493 g_object_unref (file);
7495 profile_end ("end", NULL);
7500 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7502 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7504 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7505 impl->operation_mode == OPERATION_MODE_RECENT)
7508 if (impl->reload_state == RELOAD_EMPTY)
7510 char *current_working_dir;
7513 /* We are unmapped, or we had an error while loading the last folder. We'll return
7514 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7515 * explicitly calls set_current_folder() on us.
7517 current_working_dir = g_get_current_dir ();
7518 file = g_file_new_for_path (current_working_dir);
7519 g_free (current_working_dir);
7523 if (impl->current_folder)
7524 return g_object_ref (impl->current_folder);
7530 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7533 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7535 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7536 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7538 pending_select_files_free (impl);
7539 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7543 select_func (GtkFileSystemModel *model,
7548 GtkFileChooserDefault *impl = user_data;
7549 GtkTreeSelection *selection;
7550 GtkTreeIter sorted_iter;
7552 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7554 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7555 gtk_tree_selection_select_iter (selection, &sorted_iter);
7559 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7563 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7567 parent_file = g_file_get_parent (file);
7570 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7572 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7573 impl->operation_mode == OPERATION_MODE_RECENT ||
7574 impl->load_state == LOAD_EMPTY)
7580 g_assert (impl->current_folder != NULL);
7582 same_path = g_file_equal (parent_file, impl->current_folder);
7585 if (same_path && impl->load_state == LOAD_FINISHED)
7590 files.data = (gpointer) file;
7593 result = show_and_select_files (impl, parent_file, &files, error);
7594 g_object_unref (parent_file);
7598 pending_select_files_add (impl, file);
7604 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7605 g_object_unref (parent_file);
7609 g_object_unref (parent_file);
7614 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7617 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7618 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7619 GtkTreeIter iter, sorted_iter;
7621 if (!impl->browse_files_model)
7624 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7629 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model,
7632 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7637 maybe_select (GtkTreeModel *model,
7642 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7643 GtkTreeSelection *selection;
7647 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7649 info = get_list_file_info (impl, iter);
7650 is_folder = _gtk_file_info_consider_as_directory (info);
7652 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7653 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7654 gtk_tree_selection_select_iter (selection, iter);
7656 gtk_tree_selection_unselect_iter (selection, iter);
7662 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7664 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7666 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7667 impl->operation_mode == OPERATION_MODE_RECENT)
7669 GtkTreeSelection *selection;
7671 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7672 gtk_tree_selection_select_all (selection);
7676 if (impl->select_multiple)
7677 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7678 maybe_select, impl);
7682 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7684 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7685 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7687 gtk_tree_selection_unselect_all (selection);
7688 pending_select_files_free (impl);
7691 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7693 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7695 * is_empty_ret - whether the file entry is totally empty
7697 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7698 * the path will be "$cwd/foobar")
7701 check_save_entry (GtkFileChooserDefault *impl,
7703 gboolean *is_well_formed_ret,
7704 gboolean *is_empty_ret,
7705 gboolean *is_file_part_empty_ret,
7706 gboolean *is_folder)
7708 GtkFileChooserEntry *chooser_entry;
7709 GFile *current_folder;
7710 const char *file_part;
7714 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7715 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7716 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7717 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7718 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7720 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7722 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7725 *is_well_formed_ret = TRUE;
7726 *is_empty_ret = TRUE;
7727 *is_file_part_empty_ret = TRUE;
7733 *is_empty_ret = FALSE;
7735 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7736 if (!current_folder)
7739 *is_well_formed_ret = FALSE;
7740 *is_file_part_empty_ret = FALSE;
7746 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7748 if (!file_part || file_part[0] == '\0')
7750 *file_ret = g_object_ref (current_folder);
7751 *is_well_formed_ret = TRUE;
7752 *is_file_part_empty_ret = TRUE;
7758 *is_file_part_empty_ret = FALSE;
7761 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7765 error_building_filename_dialog (impl, error);
7767 *is_well_formed_ret = FALSE;
7774 *is_well_formed_ret = TRUE;
7775 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7778 struct get_files_closure {
7779 GtkFileChooserDefault *impl;
7781 GFile *file_from_entry;
7785 get_files_foreach (GtkTreeModel *model,
7790 struct get_files_closure *info;
7792 GtkFileSystemModel *fs_model;
7793 GtkTreeIter sel_iter;
7796 fs_model = info->impl->browse_files_model;
7797 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7799 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7801 return; /* We are on the editable row */
7803 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7804 info->result = g_slist_prepend (info->result, g_object_ref (file));
7808 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7810 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7811 struct get_files_closure info;
7812 GtkWindow *toplevel;
7813 GtkWidget *current_focus;
7814 gboolean file_list_seen;
7816 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7817 return search_get_selected_files (impl);
7819 if (impl->operation_mode == OPERATION_MODE_RECENT)
7820 return recent_get_selected_files (impl);
7824 info.file_from_entry = NULL;
7826 toplevel = get_toplevel (GTK_WIDGET (impl));
7828 current_focus = gtk_window_get_focus (toplevel);
7830 current_focus = NULL;
7832 file_list_seen = FALSE;
7833 if (current_focus == impl->browse_files_tree_view)
7835 GtkTreeSelection *selection;
7839 file_list_seen = TRUE;
7840 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7841 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7843 /* If there is no selection in the file list, we probably have this situation:
7845 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7846 * 2. He then double-clicked on a folder ("bar") in the file list
7848 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7849 * filename entry to see if that is the case.
7851 if (info.result == NULL && impl->location_entry)
7854 else if (impl->location_entry && current_focus == impl->location_entry)
7856 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7860 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7865 if (!is_well_formed)
7868 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7870 g_object_unref (info.file_from_entry);
7874 if (info.file_from_entry)
7875 info.result = g_slist_prepend (info.result, info.file_from_entry);
7876 else if (!file_list_seen)
7881 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7883 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7887 /* The focus is on a dialog's action area button or something else */
7888 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7889 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7897 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7898 * fall back to the current directory */
7899 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7900 info.result == NULL)
7902 GFile *current_folder;
7904 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7907 info.result = g_slist_prepend (info.result, current_folder);
7910 return g_slist_reverse (info.result);
7914 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7916 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7918 if (impl->preview_file)
7919 return g_object_ref (impl->preview_file);
7924 static GtkFileSystem *
7925 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7927 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7929 return impl->file_system;
7932 /* Shows or hides the filter widgets */
7934 show_filters (GtkFileChooserDefault *impl,
7938 gtk_widget_show (impl->filter_combo_hbox);
7940 gtk_widget_hide (impl->filter_combo_hbox);
7944 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7945 GtkFileFilter *filter)
7947 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7950 if (g_slist_find (impl->filters, filter))
7952 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7956 g_object_ref_sink (filter);
7957 impl->filters = g_slist_append (impl->filters, filter);
7959 name = gtk_file_filter_get_name (filter);
7961 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7963 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7965 if (!g_slist_find (impl->filters, impl->current_filter))
7966 set_current_filter (impl, filter);
7968 show_filters (impl, TRUE);
7972 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7973 GtkFileFilter *filter)
7975 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7976 GtkTreeModel *model;
7980 filter_index = g_slist_index (impl->filters, filter);
7982 if (filter_index < 0)
7984 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7988 impl->filters = g_slist_remove (impl->filters, filter);
7990 if (filter == impl->current_filter)
7993 set_current_filter (impl, impl->filters->data);
7995 set_current_filter (impl, NULL);
7998 /* Remove row from the combo box */
7999 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
8000 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
8001 g_assert_not_reached ();
8003 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
8005 g_object_unref (filter);
8008 show_filters (impl, FALSE);
8012 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
8014 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8016 return g_slist_copy (impl->filters);
8019 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
8021 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
8024 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
8027 struct AddShortcutData
8029 GtkFileChooserDefault *impl;
8034 add_shortcut_get_info_cb (GCancellable *cancellable,
8036 const GError *error,
8040 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8041 struct AddShortcutData *data = user_data;
8043 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
8046 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
8048 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
8051 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
8053 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
8056 g_object_unref (data->impl);
8057 g_object_unref (data->file);
8060 g_object_unref (cancellable);
8064 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
8068 GCancellable *cancellable;
8069 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8070 struct AddShortcutData *data;
8074 /* Avoid adding duplicates */
8075 pos = shortcut_find_position (impl, file);
8076 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
8080 uri = g_file_get_uri (file);
8081 /* translators, "Shortcut" means "Bookmark" here */
8083 GTK_FILE_CHOOSER_ERROR,
8084 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8085 _("Shortcut %s already exists"),
8092 for (l = impl->loading_shortcuts; l; l = l->next)
8094 GCancellable *c = l->data;
8097 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8098 if (f && g_file_equal (file, f))
8102 uri = g_file_get_uri (file);
8104 GTK_FILE_CHOOSER_ERROR,
8105 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8106 _("Shortcut %s already exists"),
8114 data = g_new0 (struct AddShortcutData, 1);
8115 data->impl = g_object_ref (impl);
8116 data->file = g_object_ref (file);
8118 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8120 add_shortcut_get_info_cb, data);
8125 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8126 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8132 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8136 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8143 for (l = impl->loading_shortcuts; l; l = l->next)
8145 GCancellable *c = l->data;
8148 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8149 if (f && g_file_equal (file, f))
8151 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8152 g_cancellable_cancel (c);
8157 if (impl->num_shortcuts == 0)
8160 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8161 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8162 g_assert_not_reached ();
8164 for (i = 0; i < impl->num_shortcuts; i++)
8167 ShortcutType shortcut_type;
8170 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8171 SHORTCUTS_COL_DATA, &col_data,
8172 SHORTCUTS_COL_TYPE, &shortcut_type,
8174 g_assert (col_data != NULL);
8175 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8177 shortcut = col_data;
8178 if (g_file_equal (shortcut, file))
8180 shortcuts_remove_rows (impl, pos + i, 1);
8181 impl->num_shortcuts--;
8185 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8186 g_assert_not_reached ();
8191 uri = g_file_get_uri (file);
8192 /* translators, "Shortcut" means "Bookmark" here */
8194 GTK_FILE_CHOOSER_ERROR,
8195 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8196 _("Shortcut %s does not exist"),
8204 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8206 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8212 if (impl->num_shortcuts == 0)
8215 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8216 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8217 g_assert_not_reached ();
8221 for (i = 0; i < impl->num_shortcuts; i++)
8224 ShortcutType shortcut_type;
8227 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8228 SHORTCUTS_COL_DATA, &col_data,
8229 SHORTCUTS_COL_TYPE, &shortcut_type,
8231 g_assert (col_data != NULL);
8232 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8234 shortcut = col_data;
8235 list = g_slist_prepend (list, g_object_ref (shortcut));
8237 if (i != impl->num_shortcuts - 1)
8239 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8240 g_assert_not_reached ();
8244 return g_slist_reverse (list);
8247 /* Guesses a size based upon font sizes */
8249 find_good_size_from_style (GtkWidget *widget,
8253 GtkFileChooserDefault *impl;
8258 g_assert (widget->style != NULL);
8259 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
8261 screen = gtk_widget_get_screen (widget);
8264 resolution = gdk_screen_get_resolution (screen);
8265 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8269 resolution = 96.0; /* wheeee */
8271 font_size = pango_font_description_get_size (widget->style->font_desc);
8272 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8274 *width = font_size * NUM_CHARS;
8275 *height = font_size * NUM_LINES;
8279 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8280 gint *default_width,
8281 gint *default_height)
8283 GtkFileChooserDefault *impl;
8286 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8288 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8289 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8290 || impl->expand_folders)
8292 GtkFileChooserSettings *settings;
8293 int x, y, width, height;
8295 settings = _gtk_file_chooser_settings_new ();
8296 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
8297 g_object_unref (settings);
8299 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8301 *default_width = width;
8302 *default_height = height;
8306 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8308 if (impl->preview_widget_active &&
8309 impl->preview_widget &&
8310 GTK_WIDGET_VISIBLE (impl->preview_widget))
8312 gtk_widget_size_request (impl->preview_box, &req);
8313 *default_width += PREVIEW_HBOX_SPACING + req.width;
8316 if (impl->extra_widget &&
8317 GTK_WIDGET_VISIBLE (impl->extra_widget))
8319 gtk_widget_size_request (impl->extra_align, &req);
8320 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8325 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8326 *default_width = req.width;
8327 *default_height = req.height;
8331 struct switch_folder_closure {
8332 GtkFileChooserDefault *impl;
8337 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8339 switch_folder_foreach_cb (GtkTreeModel *model,
8344 struct switch_folder_closure *closure;
8345 GtkTreeIter child_iter;
8349 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
8351 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
8352 closure->num_selected++;
8355 /* Changes to the selected folder in the list view */
8357 switch_to_selected_folder (GtkFileChooserDefault *impl)
8359 GtkTreeSelection *selection;
8360 struct switch_folder_closure closure;
8362 /* We do this with foreach() rather than get_selected() as we may be in
8363 * multiple selection mode
8366 closure.impl = impl;
8367 closure.file = NULL;
8368 closure.num_selected = 0;
8370 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8371 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8373 g_assert (closure.file && closure.num_selected == 1);
8375 change_folder_and_display_error (impl, closure.file, FALSE);
8378 /* Gets the GFileInfo for the selected row in the file list; assumes single
8382 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8383 gboolean *had_selection)
8385 GtkTreeSelection *selection;
8386 GtkTreeIter iter, child_iter;
8389 g_assert (!impl->select_multiple);
8390 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8391 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8393 *had_selection = FALSE;
8397 *had_selection = TRUE;
8399 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8403 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8407 /* Gets the display name of the selected file in the file list; assumes single
8408 * selection mode and that something is selected.
8410 static const gchar *
8411 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8414 gboolean had_selection;
8416 info = get_selected_file_info_from_file_list (impl, &had_selection);
8417 g_assert (had_selection);
8418 g_assert (info != NULL);
8420 return g_file_info_get_display_name (info);
8424 add_custom_button_to_dialog (GtkDialog *dialog,
8425 const gchar *mnemonic_label,
8426 const gchar *stock_id,
8431 button = gtk_button_new_with_mnemonic (mnemonic_label);
8432 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8433 gtk_button_set_image (GTK_BUTTON (button),
8434 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8435 gtk_widget_show (button);
8437 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8440 /* Presents an overwrite confirmation dialog; returns whether we should accept
8444 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8445 const gchar *file_part,
8446 const gchar *folder_display_name)
8448 GtkWindow *toplevel;
8452 toplevel = get_toplevel (GTK_WIDGET (impl));
8454 dialog = gtk_message_dialog_new (toplevel,
8455 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8456 GTK_MESSAGE_QUESTION,
8458 _("A file named \"%s\" already exists. Do you want to replace it?"),
8460 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8461 _("The file already exists in \"%s\". Replacing it will "
8462 "overwrite its contents."),
8463 folder_display_name);
8465 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8466 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8467 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8468 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8469 GTK_RESPONSE_ACCEPT,
8470 GTK_RESPONSE_CANCEL,
8472 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8474 if (toplevel->group)
8475 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8477 response = gtk_dialog_run (GTK_DIALOG (dialog));
8479 gtk_widget_destroy (dialog);
8481 return (response == GTK_RESPONSE_ACCEPT);
8484 struct GetDisplayNameData
8486 GtkFileChooserDefault *impl;
8491 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8493 const GError *error,
8496 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8497 gboolean should_respond = FALSE;
8498 struct GetDisplayNameData *data = user_data;
8500 if (cancellable != data->impl->should_respond_get_info_cancellable)
8503 data->impl->should_respond_get_info_cancellable = NULL;
8509 /* Huh? Did the folder disappear? Let the caller deal with it */
8510 should_respond = TRUE;
8512 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8514 set_busy_cursor (data->impl, FALSE);
8516 g_signal_emit_by_name (data->impl, "response-requested");
8519 g_object_unref (data->impl);
8520 g_free (data->file_part);
8523 g_object_unref (cancellable);
8526 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8527 * should respond. Can get the file part from the file list or the save entry.
8530 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8531 const gchar *file_part,
8534 GtkFileChooserConfirmation conf;
8536 if (!impl->do_overwrite_confirmation)
8539 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8541 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8545 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8547 struct GetDisplayNameData *data;
8549 g_assert (file_part != NULL);
8551 data = g_new0 (struct GetDisplayNameData, 1);
8552 data->impl = g_object_ref (impl);
8553 data->file_part = g_strdup (file_part);
8555 if (impl->should_respond_get_info_cancellable)
8556 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8558 impl->should_respond_get_info_cancellable =
8559 _gtk_file_system_get_info (impl->file_system, parent_file,
8560 "standard::display-name",
8561 confirmation_confirm_get_info_cb,
8563 set_busy_cursor (data->impl, TRUE);
8567 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8570 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8574 g_assert_not_reached ();
8579 struct FileExistsData
8581 GtkFileChooserDefault *impl;
8582 gboolean file_exists_and_is_not_folder;
8588 save_entry_get_info_cb (GCancellable *cancellable,
8590 const GError *error,
8593 gboolean parent_is_folder;
8594 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8595 struct FileExistsData *data = user_data;
8597 if (cancellable != data->impl->should_respond_get_info_cancellable)
8600 data->impl->should_respond_get_info_cancellable = NULL;
8602 set_busy_cursor (data->impl, FALSE);
8608 parent_is_folder = FALSE;
8610 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8612 if (parent_is_folder)
8614 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8616 if (data->file_exists_and_is_not_folder)
8621 /* Dup the string because the string may be modified
8622 * depending on what clients do in the confirm-overwrite
8623 * signal and this corrupts the pointer
8625 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8626 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8630 g_signal_emit_by_name (data->impl, "response-requested");
8633 g_signal_emit_by_name (data->impl, "response-requested");
8635 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8637 GError *error = NULL;
8639 set_busy_cursor (data->impl, TRUE);
8640 g_file_make_directory (data->file, NULL, &error);
8641 set_busy_cursor (data->impl, FALSE);
8644 g_signal_emit_by_name (data->impl, "response-requested");
8646 error_creating_folder_dialog (data->impl, data->file, error);
8651 /* This will display an error, which is what we want */
8652 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8656 g_object_unref (data->impl);
8657 g_object_unref (data->file);
8658 g_object_unref (data->parent_file);
8661 g_object_unref (cancellable);
8665 file_exists_get_info_cb (GCancellable *cancellable,
8667 const GError *error,
8670 gboolean data_ownership_taken = FALSE;
8671 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8672 gboolean file_exists_and_is_not_folder;
8673 struct FileExistsData *data = user_data;
8675 if (cancellable != data->impl->file_exists_get_info_cancellable)
8678 data->impl->file_exists_get_info_cancellable = NULL;
8680 set_busy_cursor (data->impl, FALSE);
8685 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8687 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8688 /* user typed a filename; we are done */
8689 g_signal_emit_by_name (data->impl, "response-requested");
8690 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8691 && file_exists_and_is_not_folder)
8693 /* Oops, the user typed the name of an existing path which is not
8696 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8697 g_error_copy (error));
8701 /* check that everything up to the last component exists */
8703 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8704 data_ownership_taken = TRUE;
8706 if (data->impl->should_respond_get_info_cancellable)
8707 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8709 data->impl->should_respond_get_info_cancellable =
8710 _gtk_file_system_get_info (data->impl->file_system,
8713 save_entry_get_info_cb,
8715 set_busy_cursor (data->impl, TRUE);
8719 if (!data_ownership_taken)
8721 g_object_unref (data->impl);
8722 g_object_unref (data->file);
8723 g_object_unref (data->parent_file);
8727 g_object_unref (cancellable);
8731 paste_text_received (GtkClipboard *clipboard,
8733 GtkFileChooserDefault *impl)
8740 file = g_file_new_for_uri (text);
8742 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8743 location_popup_handler (impl, text);
8745 g_object_unref (file);
8748 /* Handler for the "location-popup-on-paste" keybinding signal */
8750 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8752 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8753 GDK_SELECTION_CLIPBOARD);
8754 gtk_clipboard_request_text (clipboard,
8755 (GtkClipboardTextReceivedFunc) paste_text_received,
8760 /* Implementation for GtkFileChooserEmbed::should_respond() */
8762 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8764 GtkFileChooserDefault *impl;
8765 GtkWidget *toplevel;
8766 GtkWidget *current_focus;
8768 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8770 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8771 g_assert (GTK_IS_WINDOW (toplevel));
8773 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8775 if (current_focus == impl->browse_files_tree_view)
8777 /* The following array encodes what we do based on the impl->action and the
8778 * number of files selected.
8781 NOOP, /* Do nothing (don't respond) */
8782 RESPOND, /* Respond immediately */
8783 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8784 ALL_FILES, /* Respond only if everything selected is a file */
8785 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8786 SAVE_ENTRY, /* Go to the code for handling the save entry */
8787 NOT_REACHED /* Sanity check */
8789 static const ActionToTake what_to_do[4][3] = {
8790 /* 0 selected 1 selected many selected */
8791 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8792 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8793 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8794 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8798 gboolean all_files, all_folders;
8800 ActionToTake action;
8804 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8806 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8807 return search_should_respond (impl);
8809 if (impl->operation_mode == OPERATION_MODE_RECENT)
8810 return recent_should_respond (impl);
8812 selection_check (impl, &num_selected, &all_files, &all_folders);
8814 if (num_selected > 2)
8819 action = what_to_do [impl->action] [k];
8829 case RESPOND_OR_SWITCH:
8830 g_assert (num_selected == 1);
8834 switch_to_selected_folder (impl);
8837 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8838 return should_respond_after_confirm_overwrite (impl,
8839 get_display_name_from_file_list (impl),
8840 impl->current_folder);
8854 g_assert_not_reached ();
8857 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8860 gboolean is_well_formed, is_empty, is_file_part_empty;
8863 GtkFileChooserEntry *entry;
8868 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8869 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8870 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8871 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8872 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8874 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8875 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8877 if (is_empty || !is_well_formed)
8880 g_assert (file != NULL);
8885 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8886 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8888 change_folder_and_display_error (impl, file, TRUE);
8891 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8892 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8894 /* The folder already exists, so we do not need to create it.
8895 * Just respond to terminate the dialog.
8901 g_assert_not_reached ();
8907 struct FileExistsData *data;
8909 /* We need to check whether file exists and is not a folder */
8911 data = g_new0 (struct FileExistsData, 1);
8912 data->impl = g_object_ref (impl);
8913 data->file = g_object_ref (file);
8914 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8916 if (impl->file_exists_get_info_cancellable)
8917 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8919 impl->file_exists_get_info_cancellable =
8920 _gtk_file_system_get_info (impl->file_system, file,
8922 file_exists_get_info_cb,
8925 set_busy_cursor (impl, TRUE);
8929 g_error_free (error);
8932 g_object_unref (file);
8935 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8937 /* The focus is on a dialog's action area button, *and* the widget that
8938 * was focused immediately before it is the file list.
8942 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8944 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8947 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8949 /* The focus is on a dialog's action area button, *and* the widget that
8950 * was focused immediately before it is the location entry.
8955 /* The focus is on a dialog's action area button or something else */
8956 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8957 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8962 g_assert_not_reached ();
8966 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8968 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8970 GtkFileChooserDefault *impl;
8973 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8975 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8976 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8978 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8979 widget = impl->browse_files_tree_view;
8981 widget = impl->location_entry;
8983 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8984 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8985 widget = impl->location_entry;
8988 g_assert_not_reached ();
8992 g_assert (widget != NULL);
8993 gtk_widget_grab_focus (widget);
8996 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8998 search_selected_foreach_get_file_cb (GtkTreeModel *model,
9008 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9009 *list = g_slist_prepend (*list, g_object_ref (file));
9012 /* Constructs a list of the selected paths in search mode */
9014 search_get_selected_files (GtkFileChooserDefault *impl)
9017 GtkTreeSelection *selection;
9021 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9022 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9023 result = g_slist_reverse (result);
9028 /* Called from ::should_respond(). We return whether there are selected files
9029 * in the search list.
9032 search_should_respond (GtkFileChooserDefault *impl)
9034 GtkTreeSelection *selection;
9036 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9038 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9039 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9042 struct SearchHitInsertRequest
9044 GtkFileChooserDefault *impl;
9046 GtkTreeRowReference *row_ref;
9050 search_hit_get_info_cb (GCancellable *cancellable,
9052 const GError *error,
9055 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9056 GdkPixbuf *pixbuf = NULL;
9059 GCancellable *model_cancellable;
9060 gboolean is_folder = FALSE;
9063 char *mime_type, *size_text, *mtime_text;
9064 char *display_name, *collated_name;
9065 struct SearchHitInsertRequest *request = data;
9067 if (!request->impl->search_model)
9070 path = gtk_tree_row_reference_get_path (request->row_ref);
9074 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
9076 gtk_tree_path_free (path);
9078 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
9079 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
9081 if (cancellable != model_cancellable)
9084 /* set the cancellable to NULL in the model */
9085 gtk_list_store_set (request->impl->search_model, &iter,
9086 SEARCH_MODEL_COL_CANCELLABLE, NULL,
9094 search_clear_model_row (GTK_TREE_MODEL (request->impl->search_model), &iter);
9095 gtk_list_store_remove (request->impl->search_model, &iter);
9099 display_name = g_strdup (g_file_info_get_display_name (info));
9101 collated_name = g_utf8_collate_key_for_filename (display_name, -1);
9103 collated_name = NULL;
9104 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
9105 is_folder = _gtk_file_info_consider_as_directory (info);
9106 g_file_info_get_modification_time (info, &mtime);
9107 size = g_file_info_get_size (info);
9108 size_text = is_folder ? NULL : g_format_size_for_display (size);
9109 mtime_text = mtime.tv_sec ? my_g_format_time_for_display (mtime.tv_sec) : g_strdup (_("Unknown"));
9110 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
9111 request->impl->icon_size);
9113 gtk_list_store_set (request->impl->search_model, &iter,
9114 MODEL_COL_NAME, display_name,
9115 MODEL_COL_NAME_COLLATED, collated_name,
9116 MODEL_COL_IS_FOLDER, is_folder,
9117 MODEL_COL_PIXBUF, pixbuf,
9118 MODEL_COL_SIZE, size,
9119 MODEL_COL_SIZE_TEXT, size_text,
9120 MODEL_COL_MTIME, mtime.tv_sec,
9121 MODEL_COL_MTIME_TEXT, mtime_text,
9122 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
9126 g_object_unref (pixbuf);
9127 g_free (display_name);
9128 g_free (collated_name);
9131 g_free (mtime_text);
9134 g_object_unref (request->impl);
9135 g_object_unref (request->file);
9136 gtk_tree_row_reference_free (request->row_ref);
9139 g_object_unref (cancellable);
9142 /* Adds one hit from the search engine to the search_model */
9144 search_add_hit (GtkFileChooserDefault *impl,
9150 GCancellable *cancellable;
9151 struct SearchHitInsertRequest *request;
9153 file = g_file_new_for_uri (uri);
9157 if (!g_file_is_native (file))
9159 g_object_unref (file);
9163 request = g_new0 (struct SearchHitInsertRequest, 1);
9164 request->impl = g_object_ref (impl);
9165 request->file = g_object_ref (file);
9167 gtk_list_store_append (impl->search_model, &iter);
9168 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
9170 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
9171 gtk_tree_path_free (p);
9173 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9176 "standard::content-type,"
9177 "standard::display-name,"
9180 search_hit_get_info_cb,
9183 gtk_list_store_set (impl->search_model, &iter,
9184 MODEL_COL_FILE, file,
9185 MODEL_COL_ELLIPSIZE, PANGO_ELLIPSIZE_END,
9186 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
9189 g_object_unref (file);
9190 g_object_unref (cancellable);
9193 /* Callback used from GtkSearchEngine when we get new hits */
9195 search_engine_hits_added_cb (GtkSearchEngine *engine,
9199 GtkFileChooserDefault *impl;
9202 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9204 for (l = hits; l; l = l->next)
9205 search_add_hit (impl, (gchar*)l->data);
9208 /* Callback used from GtkSearchEngine when the query is done running */
9210 search_engine_finished_cb (GtkSearchEngine *engine,
9213 GtkFileChooserDefault *impl;
9215 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9218 /* EB: setting the model here will avoid loads of row events,
9219 * but it'll make the search look like blocked.
9221 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9222 GTK_TREE_MODEL (impl->search_model_filter));
9225 /* FMQ: if search was empty, say that we got no hits */
9226 set_busy_cursor (impl, FALSE);
9229 /* Displays a generic error when we cannot create a GtkSearchEngine.
9230 * It would be better if _gtk_search_engine_new() gave us a GError
9231 * with a better message, but it doesn't do that right now.
9234 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9236 error_message (impl,
9237 _("Could not start the search process"),
9238 _("The program was not able to create a connection to the indexer "
9239 "daemon. Please make sure it is running."));
9243 search_engine_error_cb (GtkSearchEngine *engine,
9244 const gchar *message,
9247 GtkFileChooserDefault *impl;
9249 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9251 search_stop_searching (impl, TRUE);
9252 error_message (impl, _("Could not send the search request"), message);
9254 set_busy_cursor (impl, FALSE);
9258 search_clear_model_row (GtkTreeModel *model,
9261 GCancellable *cancellable;
9263 gtk_tree_model_get (model, iter,
9264 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
9269 g_cancellable_cancel (cancellable);
9270 g_object_unref (cancellable);
9274 /* Frees the data in the search_model */
9276 search_clear_model (GtkFileChooserDefault *impl,
9277 gboolean remove_from_treeview)
9279 GtkTreeModel *model;
9282 if (!impl->search_model)
9285 model = GTK_TREE_MODEL (impl->search_model);
9287 if (gtk_tree_model_get_iter_first (model, &iter))
9290 search_clear_model_row (model, &iter);
9292 while (gtk_tree_model_iter_next (model, &iter));
9294 g_object_unref (impl->search_model);
9295 impl->search_model = NULL;
9297 g_object_unref (impl->search_model_filter);
9298 impl->search_model_filter = NULL;
9300 g_object_unref (impl->search_model_sort);
9301 impl->search_model_sort = NULL;
9303 if (remove_from_treeview)
9304 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9307 /* Stops any ongoing searches; does not touch the search_model */
9309 search_stop_searching (GtkFileChooserDefault *impl,
9310 gboolean remove_query)
9312 if (remove_query && impl->search_query)
9314 g_object_unref (impl->search_query);
9315 impl->search_query = NULL;
9318 if (impl->search_engine)
9320 _gtk_search_engine_stop (impl->search_engine);
9322 g_object_unref (impl->search_engine);
9323 impl->search_engine = NULL;
9327 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9329 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9331 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9333 search_stop_searching (impl, FALSE);
9334 search_clear_model (impl, TRUE);
9336 gtk_widget_destroy (impl->search_hbox);
9337 impl->search_hbox = NULL;
9338 impl->search_entry = NULL;
9340 gtk_widget_show (impl->browse_path_bar);
9341 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9342 gtk_widget_hide (impl->browse_new_folder_button);
9344 gtk_widget_show (impl->browse_new_folder_button);
9347 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9348 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9350 gtk_widget_show (impl->location_button);
9352 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9353 gtk_widget_show (impl->location_entry_box);
9356 impl->operation_mode = OPERATION_MODE_BROWSE;
9358 file_list_set_sort_column_ids (impl);
9362 search_get_is_filtered (GtkFileChooserDefault *impl,
9364 const gchar *display_name,
9365 const gchar *mime_type)
9367 GtkFileFilterInfo filter_info;
9368 GtkFileFilterFlags needed;
9371 if (!impl->current_filter)
9374 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9375 needed = gtk_file_filter_get_needed (impl->current_filter);
9377 filter_info.display_name = display_name;
9378 filter_info.mime_type = mime_type;
9380 if (needed & GTK_FILE_FILTER_FILENAME)
9382 filter_info.filename = g_file_get_path (file);
9383 if (filter_info.filename)
9384 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9387 filter_info.filename = NULL;
9389 if (needed & GTK_FILE_FILTER_URI)
9391 filter_info.uri = g_file_get_uri (file);
9392 if (filter_info.uri)
9393 filter_info.contains |= GTK_FILE_FILTER_URI;
9396 filter_info.uri = NULL;
9398 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9400 if (filter_info.filename)
9401 g_free ((gchar *) filter_info.filename);
9402 if (filter_info.uri)
9403 g_free ((gchar *) filter_info.uri);
9409 /* Visibility function for the recent filter model */
9411 search_model_visible_func (GtkTreeModel *model,
9415 GtkFileChooserDefault *impl = user_data;
9417 gchar *display_name, *mime_type;
9418 gboolean is_folder, result;
9420 if (!impl->current_filter)
9423 gtk_tree_model_get (model, iter,
9424 MODEL_COL_FILE, &file,
9425 MODEL_COL_IS_FOLDER, &is_folder,
9426 MODEL_COL_NAME, &display_name,
9427 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9435 result = !search_get_is_filtered (impl, file, display_name, mime_type);
9437 g_free (display_name);
9443 /* Creates the search_model and puts it in the tree view */
9445 search_setup_model (GtkFileChooserDefault *impl)
9447 g_assert (impl->search_model == NULL);
9448 g_assert (impl->search_model_filter == NULL);
9449 g_assert (impl->search_model_sort == NULL);
9451 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9452 G_TYPE_STRING, /* MODEL_COL_NAME */
9453 G_TYPE_INT64, /* MODEL_COL_SIZE */
9454 G_TYPE_LONG, /* MODEL_COL_MTIME */
9455 G_TYPE_FILE, /* MODEL_COL_FILE */
9456 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */
9457 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */
9458 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */
9459 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */
9460 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */
9461 PANGO_TYPE_ELLIPSIZE_MODE, /* MODEL_COL_ELLIPSIZE */
9462 G_TYPE_CANCELLABLE, /* SEARCH_MODEL_COL_CANCELLABLE */
9463 G_TYPE_STRING /* SEARCH_MODEL_COL_MIME_TYPE */
9466 impl->search_model_filter =
9467 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9468 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9469 search_model_visible_func,
9472 impl->search_model_sort =
9473 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9474 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9478 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9482 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9486 set_sort_column (impl);
9488 /* EB: setting the model here will make the hits list update feel
9489 * more "alive" than setting the model at the end of the search
9492 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9493 GTK_TREE_MODEL (impl->search_model_sort));
9497 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9498 GtkTreeIter *child_iter,
9503 if (!impl->search_model)
9506 if (!impl->search_model_filter || !impl->search_model_sort)
9509 /* pass 1: get the iterator in the filter model */
9510 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9513 /* pass 2: get the iterator in the real model */
9514 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9515 child_iter, &middle);
9518 /* Creates a new query with the specified text and launches it */
9520 search_start_query (GtkFileChooserDefault *impl,
9521 const gchar *query_text)
9523 search_stop_searching (impl, FALSE);
9524 search_clear_model (impl, TRUE);
9525 search_setup_model (impl);
9526 set_busy_cursor (impl, TRUE);
9528 if (impl->search_engine == NULL)
9529 impl->search_engine = _gtk_search_engine_new ();
9531 if (!impl->search_engine)
9533 set_busy_cursor (impl, FALSE);
9534 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9538 if (!impl->search_query)
9540 impl->search_query = _gtk_query_new ();
9541 _gtk_query_set_text (impl->search_query, query_text);
9544 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9546 g_signal_connect (impl->search_engine, "hits-added",
9547 G_CALLBACK (search_engine_hits_added_cb), impl);
9548 g_signal_connect (impl->search_engine, "finished",
9549 G_CALLBACK (search_engine_finished_cb), impl);
9550 g_signal_connect (impl->search_engine, "error",
9551 G_CALLBACK (search_engine_error_cb), impl);
9553 _gtk_search_engine_start (impl->search_engine);
9556 /* Callback used when the user presses Enter while typing on the search
9557 * entry; starts the query
9560 search_entry_activate_cb (GtkEntry *entry,
9563 GtkFileChooserDefault *impl;
9566 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9568 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9569 if (strlen (text) == 0)
9572 /* reset any existing query object */
9573 if (impl->search_query)
9575 g_object_unref (impl->search_query);
9576 impl->search_query = NULL;
9579 search_start_query (impl, text);
9582 /* Hides the path bar and creates the search entry */
9584 search_setup_widgets (GtkFileChooserDefault *impl)
9590 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9594 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9595 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), image);
9596 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9600 label = gtk_label_new (NULL);
9601 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9602 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9603 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9608 impl->search_entry = gtk_entry_new ();
9609 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9610 g_signal_connect (impl->search_entry, "activate",
9611 G_CALLBACK (search_entry_activate_cb),
9613 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9615 /* if there already is a query, restart it */
9616 if (impl->search_query)
9618 gchar *query = _gtk_query_get_text (impl->search_query);
9622 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9623 search_start_query (impl, query);
9629 g_object_unref (impl->search_query);
9630 impl->search_query = NULL;
9634 gtk_widget_hide (impl->browse_path_bar);
9635 gtk_widget_hide (impl->browse_new_folder_button);
9637 /* Box for search widgets */
9638 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9639 gtk_widget_show_all (impl->search_hbox);
9641 /* Hide the location widgets temporarily */
9643 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9644 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9646 gtk_widget_hide (impl->location_button);
9647 gtk_widget_hide (impl->location_entry_box);
9650 gtk_widget_grab_focus (impl->search_entry);
9652 /* FMQ: hide the filter combo? */
9655 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9657 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9661 case OPERATION_MODE_BROWSE:
9662 stop_loading_and_clear_list_model (impl);
9665 case OPERATION_MODE_SEARCH:
9666 search_stop_searching (impl, FALSE);
9667 search_clear_model (impl, TRUE);
9669 gtk_widget_destroy (impl->search_hbox);
9670 impl->search_hbox = NULL;
9671 impl->search_entry = NULL;
9674 case OPERATION_MODE_RECENT:
9675 recent_stop_loading (impl);
9676 recent_clear_model (impl, TRUE);
9678 gtk_widget_destroy (impl->recent_hbox);
9679 impl->recent_hbox = NULL;
9684 /* Main entry point to the searching functions; this gets called when the user
9685 * activates the Search shortcut.
9688 search_activate (GtkFileChooserDefault *impl)
9690 OperationMode previous_mode;
9692 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9694 gtk_widget_grab_focus (impl->search_entry);
9698 previous_mode = impl->operation_mode;
9699 impl->operation_mode = OPERATION_MODE_SEARCH;
9701 stop_operation (impl, previous_mode);
9703 g_assert (impl->search_hbox == NULL);
9704 g_assert (impl->search_entry == NULL);
9705 g_assert (impl->search_model == NULL);
9706 g_assert (impl->search_model_filter == NULL);
9708 search_setup_widgets (impl);
9709 file_list_set_sort_column_ids (impl);
9713 * Recent files support
9716 /* Frees the data in the recent_model */
9718 recent_clear_model (GtkFileChooserDefault *impl,
9719 gboolean remove_from_treeview)
9721 GtkTreeModel *model;
9724 if (!impl->recent_model)
9727 model = GTK_TREE_MODEL (impl->recent_model);
9729 if (remove_from_treeview)
9730 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9732 if (gtk_tree_model_get_iter_first (model, &iter))
9737 GCancellable *cancellable;
9738 GtkRecentInfo *recent_info;
9739 gchar *display_name;
9741 gtk_tree_model_get (model, &iter,
9742 MODEL_COL_NAME, &display_name,
9743 MODEL_COL_FILE, &file,
9744 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9745 RECENT_MODEL_COL_INFO, &recent_info,
9749 g_cancellable_cancel (cancellable);
9751 g_object_unref (file);
9752 gtk_recent_info_unref (recent_info);
9753 g_free (display_name);
9755 while (gtk_tree_model_iter_next (model, &iter));
9758 g_object_unref (impl->recent_model);
9759 impl->recent_model = NULL;
9761 g_object_unref (impl->recent_model_filter);
9762 impl->recent_model_filter = NULL;
9764 g_object_unref (impl->recent_model_sort);
9765 impl->recent_model_sort = NULL;
9768 /* Stops any ongoing loading of the recent files list; does
9769 * not touch the recent_model
9772 recent_stop_loading (GtkFileChooserDefault *impl)
9774 if (impl->load_recent_id)
9776 g_source_remove (impl->load_recent_id);
9777 impl->load_recent_id = 0;
9781 /* Stops any pending load, clears the file list, and switches
9782 * back to OPERATION_MODE_BROWSE
9785 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9787 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9789 recent_stop_loading (impl);
9790 recent_clear_model (impl, TRUE);
9792 gtk_widget_destroy (impl->recent_hbox);
9793 impl->recent_hbox = NULL;
9795 gtk_widget_show (impl->browse_path_bar);
9796 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9797 gtk_widget_hide (impl->browse_new_folder_button);
9799 gtk_widget_show (impl->browse_new_folder_button);
9801 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9802 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9804 gtk_widget_show (impl->location_button);
9806 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9807 gtk_widget_show (impl->location_entry_box);
9810 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9812 impl->operation_mode = OPERATION_MODE_BROWSE;
9814 file_list_set_sort_column_ids (impl);
9818 recent_get_is_filtered (GtkFileChooserDefault *impl,
9820 GtkRecentInfo *recent_info)
9822 GtkFileFilterInfo filter_info;
9823 GtkFileFilterFlags needed;
9826 if (!impl->current_filter)
9829 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9830 needed = gtk_file_filter_get_needed (impl->current_filter);
9832 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9833 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9835 if (needed & GTK_FILE_FILTER_FILENAME)
9837 filter_info.filename = g_file_get_path (file);
9838 if (filter_info.filename)
9839 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9842 filter_info.filename = NULL;
9844 if (needed & GTK_FILE_FILTER_URI)
9846 filter_info.uri = g_file_get_uri (file);
9847 if (filter_info.uri)
9848 filter_info.contains |= GTK_FILE_FILTER_URI;
9851 filter_info.uri = NULL;
9853 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9855 if (filter_info.filename)
9856 g_free ((gchar *) filter_info.filename);
9857 if (filter_info.uri)
9858 g_free ((gchar *) filter_info.uri);
9863 /* Visibility function for the recent filter model */
9865 recent_model_visible_func (GtkTreeModel *model,
9869 GtkFileChooserDefault *impl = user_data;
9871 GtkRecentInfo *recent_info;
9874 if (!impl->current_filter)
9877 gtk_tree_model_get (model, iter,
9878 RECENT_MODEL_COL_INFO, &recent_info,
9879 MODEL_COL_FILE, &file,
9880 MODEL_COL_IS_FOLDER, &is_folder,
9889 return !recent_get_is_filtered (impl, file, recent_info);
9893 recent_setup_model (GtkFileChooserDefault *impl)
9895 g_assert (impl->recent_model == NULL);
9896 g_assert (impl->recent_model_filter == NULL);
9897 g_assert (impl->recent_model_sort == NULL);
9899 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9900 G_TYPE_STRING, /* MODEL_COL_NAME */
9901 G_TYPE_INT64, /* MODEL_COL_SIZE */
9902 G_TYPE_LONG, /* MODEL_COL_MTIME */
9903 G_TYPE_FILE, /* MODEL_COL_FILE */
9904 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */
9905 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */
9906 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */
9907 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */
9908 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */
9909 PANGO_TYPE_ELLIPSIZE_MODE, /* MODEL_COL_ELLIPSIZE */
9910 G_TYPE_CANCELLABLE, /* RECENT_MODEL_COL_CANCELLABLE */
9911 G_TYPE_POINTER /* RECENT_MODEL_COL_INFO */
9914 impl->recent_model_filter =
9915 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9916 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9917 recent_model_visible_func,
9921 /* this is the model that will actually be added to
9922 * the browse_files_tree_view widget; remember: we are
9923 * stuffing the real model into a filter model and then
9924 * into a sort model; this means we'll have to translate
9925 * the child iterator *twice* to get from a path or an
9926 * iterator coming from the tree view widget to the
9927 * real data inside the model.
9929 impl->recent_model_sort =
9930 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9931 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9935 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9939 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9943 set_sort_column (impl);
9948 GtkFileChooserDefault *impl;
9951 gint n_loaded_items;
9952 guint needs_sorting : 1;
9956 recent_idle_cleanup (gpointer data)
9958 RecentLoadData *load_data = data;
9959 GtkFileChooserDefault *impl = load_data->impl;
9961 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9962 GTK_TREE_MODEL (impl->recent_model_sort));
9964 set_busy_cursor (impl, FALSE);
9966 impl->load_recent_id = 0;
9968 if (load_data->items)
9970 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9971 g_list_free (load_data->items);
9977 struct RecentItemInsertRequest
9979 GtkFileChooserDefault *impl;
9981 GtkTreeRowReference *row_ref;
9985 recent_item_get_info_cb (GCancellable *cancellable,
9987 const GError *error,
9990 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9993 GCancellable *model_cancellable;
9994 gboolean is_folder = FALSE;
9995 struct RecentItemInsertRequest *request = data;
9999 if (!request->impl->recent_model)
10002 path = gtk_tree_row_reference_get_path (request->row_ref);
10006 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
10008 gtk_tree_path_free (path);
10010 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
10011 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
10013 g_object_unref (model_cancellable);
10014 if (cancellable != model_cancellable)
10017 gtk_list_store_set (request->impl->recent_model, &iter,
10018 RECENT_MODEL_COL_CANCELLABLE, NULL,
10026 gtk_list_store_remove (request->impl->recent_model, &iter);
10030 is_folder = _gtk_file_info_consider_as_directory (info);
10031 size = g_file_info_get_size (info);
10032 size_text = is_folder ? g_format_size_for_display (size) : g_strdup ("");
10034 gtk_list_store_set (request->impl->recent_model, &iter,
10035 MODEL_COL_IS_FOLDER, is_folder,
10036 MODEL_COL_SIZE, size,
10037 MODEL_COL_SIZE_TEXT, size_text,
10040 g_free (size_text);
10043 g_object_unref (request->impl);
10044 g_object_unref (request->file);
10045 gtk_tree_row_reference_free (request->row_ref);
10048 g_object_unref (cancellable);
10052 recent_sort_mru (gconstpointer a,
10055 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
10056 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
10058 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
10062 get_recent_files_limit (GtkWidget *widget)
10064 GtkSettings *settings;
10067 if (gtk_widget_has_screen (widget))
10068 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
10070 settings = gtk_settings_get_default ();
10072 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
10078 recent_idle_load (gpointer data)
10080 RecentLoadData *load_data = data;
10081 GtkFileChooserDefault *impl = load_data->impl;
10084 GtkRecentInfo *info;
10085 const gchar *uri, *display_name, *mtime_text, *collated_name;
10088 GCancellable *cancellable;
10089 struct RecentItemInsertRequest *request;
10091 if (!impl->recent_manager)
10094 /* first iteration: load all the items */
10095 if (!load_data->items)
10097 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
10098 if (!load_data->items)
10101 load_data->needs_sorting = TRUE;
10106 /* second iteration: preliminary MRU sorting and clamping */
10107 if (load_data->needs_sorting)
10111 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
10112 load_data->n_items = g_list_length (load_data->items);
10114 limit = get_recent_files_limit (GTK_WIDGET (impl));
10116 if (limit != -1 && (load_data->n_items > limit))
10120 clamp = g_list_nth (load_data->items, limit - 1);
10121 if (G_LIKELY (clamp))
10124 clamp->next = NULL;
10126 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
10129 load_data->n_items = limit;
10133 load_data->n_loaded_items = 0;
10134 load_data->needs_sorting = FALSE;
10139 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
10140 g_assert (info != NULL);
10142 uri = gtk_recent_info_get_uri (info);
10143 display_name = gtk_recent_info_get_display_name (info);
10144 file = g_file_new_for_uri (uri);
10146 gtk_list_store_append (impl->recent_model, &iter);
10147 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
10149 request = g_new0 (struct RecentItemInsertRequest, 1);
10150 request->impl = g_object_ref (impl);
10151 request->file = g_object_ref (file);
10152 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
10153 gtk_tree_path_free (p);
10155 cancellable = _gtk_file_system_get_info (impl->file_system, file,
10156 "standard::type,standard::size",
10157 recent_item_get_info_cb,
10160 collated_name = g_utf8_collate_key_for_filename (gtk_recent_info_get_display_name (info), -1);
10161 mtime = gtk_recent_info_get_modified (info);
10162 mtime_text = mtime ? my_g_format_time_for_display (mtime) : g_strdup (_("Unknown"));
10164 gtk_list_store_set (impl->recent_model, &iter,
10165 MODEL_COL_FILE, file,
10166 MODEL_COL_NAME, gtk_recent_info_get_display_name (info),
10167 MODEL_COL_NAME_COLLATED, collated_name,
10168 MODEL_COL_PIXBUF, gtk_recent_info_get_icon (info, impl->icon_size),
10169 MODEL_COL_MTIME, mtime,
10170 MODEL_COL_MTIME_TEXT, mtime_text,
10171 MODEL_COL_ELLIPSIZE, PANGO_ELLIPSIZE_END,
10172 RECENT_MODEL_COL_CANCELLABLE, cancellable,
10173 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
10176 load_data->n_loaded_items += 1;
10178 /* finished loading items */
10179 if (load_data->n_loaded_items == load_data->n_items)
10181 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10182 g_list_free (load_data->items);
10183 load_data->items = NULL;
10192 recent_start_loading (GtkFileChooserDefault *impl)
10194 RecentLoadData *load_data;
10196 recent_stop_loading (impl);
10197 recent_clear_model (impl, TRUE);
10198 recent_setup_model (impl);
10199 set_busy_cursor (impl, TRUE);
10201 g_assert (impl->load_recent_id == 0);
10203 load_data = g_new (RecentLoadData, 1);
10204 load_data->impl = impl;
10205 load_data->items = NULL;
10206 load_data->n_items = 0;
10207 load_data->n_loaded_items = 0;
10208 load_data->needs_sorting = TRUE;
10210 /* begin lazy loading the recent files into the model */
10211 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
10214 recent_idle_cleanup);
10218 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
10228 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
10229 *list = g_slist_prepend (*list, file);
10232 /* Constructs a list of the selected paths in recent files mode */
10234 recent_get_selected_files (GtkFileChooserDefault *impl)
10237 GtkTreeSelection *selection;
10241 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10242 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
10243 result = g_slist_reverse (result);
10248 /* Called from ::should_respond(). We return whether there are selected
10249 * files in the recent files list.
10252 recent_should_respond (GtkFileChooserDefault *impl)
10254 GtkTreeSelection *selection;
10256 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10258 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10259 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10262 /* Hide the location widgets temporarily */
10264 recent_hide_entry (GtkFileChooserDefault *impl)
10270 impl->recent_hbox = gtk_hbox_new (FALSE, 12);
10273 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
10274 gtk_size_group_add_widget (impl->browse_path_bar_size_group, image);
10275 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
10278 label = gtk_label_new (NULL);
10279 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
10280 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
10281 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
10284 gtk_widget_hide (impl->browse_path_bar);
10285 gtk_widget_hide (impl->browse_new_folder_button);
10287 /* Box for recent widgets */
10288 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
10289 gtk_widget_show_all (impl->recent_hbox);
10291 /* Hide the location widgets temporarily */
10292 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10293 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10295 gtk_widget_hide (impl->location_button);
10296 gtk_widget_hide (impl->location_entry_box);
10300 /* Main entry point to the recent files functions; this gets called when
10301 * the user activates the Recently Used shortcut.
10304 recent_activate (GtkFileChooserDefault *impl)
10306 OperationMode previous_mode;
10308 if (impl->operation_mode == OPERATION_MODE_RECENT)
10311 previous_mode = impl->operation_mode;
10312 impl->operation_mode = OPERATION_MODE_RECENT;
10314 stop_operation (impl, previous_mode);
10316 recent_hide_entry (impl);
10318 /* hide the file size column if it's visible */
10319 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
10321 file_list_set_sort_column_ids (impl);
10322 recent_start_loading (impl);
10325 /* convert an iterator coming from the model bound to
10326 * browse_files_tree_view to an interator inside the
10327 * real recent_model
10330 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10331 GtkTreeIter *child_iter,
10334 GtkTreeIter middle;
10336 if (!impl->recent_model)
10339 if (!impl->recent_model_filter || !impl->recent_model_sort)
10342 /* pass 1: get the iterator in the filter model */
10343 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10346 /* pass 2: get the iterator in the real model */
10347 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10354 set_current_filter (GtkFileChooserDefault *impl,
10355 GtkFileFilter *filter)
10357 if (impl->current_filter != filter)
10361 /* NULL filters are allowed to reset to non-filtered status
10363 filter_index = g_slist_index (impl->filters, filter);
10364 if (impl->filters && filter && filter_index < 0)
10367 if (impl->current_filter)
10368 g_object_unref (impl->current_filter);
10369 impl->current_filter = filter;
10370 if (impl->current_filter)
10372 g_object_ref_sink (impl->current_filter);
10376 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10379 if (impl->browse_files_model)
10380 install_list_model_filter (impl);
10382 if (impl->operation_mode == OPERATION_MODE_SEARCH &&
10383 impl->search_model_filter != NULL)
10384 gtk_tree_model_filter_refilter (impl->search_model_filter);
10386 /* we want to have all the matching results, and not just a
10387 * filter of the previous model
10389 if (impl->operation_mode == OPERATION_MODE_RECENT)
10390 recent_start_loading (impl);
10392 g_object_notify (G_OBJECT (impl), "filter");
10397 filter_combo_changed (GtkComboBox *combo_box,
10398 GtkFileChooserDefault *impl)
10400 gint new_index = gtk_combo_box_get_active (combo_box);
10401 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10403 set_current_filter (impl, new_filter);
10407 check_preview_change (GtkFileChooserDefault *impl)
10409 GtkTreePath *cursor_path;
10411 char *new_display_name;
10412 GtkTreeModel *model;
10414 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10415 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
10420 gtk_tree_model_get_iter (model, &iter, cursor_path);
10421 gtk_tree_model_get (model, &iter,
10422 MODEL_COL_FILE, &new_file,
10423 MODEL_COL_NAME, &new_display_name,
10426 gtk_tree_path_free (cursor_path);
10431 new_display_name = NULL;
10434 if (new_file != impl->preview_file &&
10435 !(new_file && impl->preview_file &&
10436 g_file_equal (new_file, impl->preview_file)))
10438 if (impl->preview_file)
10440 g_object_unref (impl->preview_file);
10441 g_free (impl->preview_display_name);
10446 impl->preview_file = new_file;
10447 impl->preview_display_name = new_display_name;
10451 impl->preview_file = NULL;
10452 impl->preview_display_name = NULL;
10453 g_free (new_display_name);
10456 if (impl->use_preview_label && impl->preview_label)
10457 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10459 g_signal_emit_by_name (impl, "update-preview");
10464 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10465 GtkFileSystemVolume *volume,
10466 const GError *error,
10470 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10471 GtkFileChooserDefault *impl = data;
10473 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10476 impl->shortcuts_activate_iter_cancellable = NULL;
10478 set_busy_cursor (impl, FALSE);
10485 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10489 name = _gtk_file_system_volume_get_display_name (volume);
10490 msg = g_strdup_printf (_("Could not mount %s"), name);
10492 error_message (impl, msg, error->message);
10501 file = _gtk_file_system_volume_get_root (volume);
10504 change_folder_and_display_error (impl, file, FALSE);
10505 g_object_unref (file);
10509 g_object_unref (impl);
10510 g_object_unref (cancellable);
10514 /* Activates a volume by mounting it if necessary and then switching to its
10518 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10519 GtkFileSystemVolume *volume)
10523 switch (impl->operation_mode)
10525 case OPERATION_MODE_BROWSE:
10527 case OPERATION_MODE_SEARCH:
10528 search_switch_to_browse_mode (impl);
10530 case OPERATION_MODE_RECENT:
10531 recent_switch_to_browse_mode (impl);
10535 /* We ref the file chooser since volume_mount() may run a main loop, and the
10536 * user could close the file chooser window in the meantime.
10538 g_object_ref (impl);
10540 if (!_gtk_file_system_volume_is_mounted (volume))
10542 GMountOperation *mount_op;
10544 set_busy_cursor (impl, TRUE);
10546 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
10547 impl->shortcuts_activate_iter_cancellable =
10548 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
10549 shortcuts_activate_volume_mount_cb,
10550 g_object_ref (impl));
10551 g_object_unref (mount_op);
10555 file = _gtk_file_system_volume_get_root (volume);
10558 change_folder_and_display_error (impl, file, FALSE);
10559 g_object_unref (file);
10563 g_object_unref (impl);
10566 /* Opens the folder or volume at the specified iter in the shortcuts model */
10567 struct ShortcutsActivateData
10569 GtkFileChooserDefault *impl;
10574 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10576 const GError *error,
10577 gpointer user_data)
10579 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10580 struct ShortcutsActivateData *data = user_data;
10582 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10585 data->impl->shortcuts_activate_iter_cancellable = NULL;
10590 if (!error && _gtk_file_info_consider_as_directory (info))
10591 change_folder_and_display_error (data->impl, data->file, FALSE);
10593 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10598 g_object_unref (data->impl);
10599 g_object_unref (data->file);
10602 g_object_unref (cancellable);
10606 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10607 GtkFileSystemVolume *volume,
10608 const GError *error,
10609 gpointer user_data)
10611 struct ShortcutsActivateData *data = user_data;
10615 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10617 g_object_unref (data->impl);
10618 g_object_unref (data->file);
10624 data->impl->shortcuts_activate_iter_cancellable =
10625 _gtk_file_system_get_info (data->impl->file_system, data->file,
10627 shortcuts_activate_get_info_cb, data);
10631 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10635 ShortcutType shortcut_type;
10637 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10638 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10639 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10640 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10642 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10643 SHORTCUTS_COL_DATA, &col_data,
10644 SHORTCUTS_COL_TYPE, &shortcut_type,
10647 if (impl->shortcuts_activate_iter_cancellable)
10649 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10650 impl->shortcuts_activate_iter_cancellable = NULL;
10653 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10655 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10657 GtkFileSystemVolume *volume;
10661 shortcuts_activate_volume (impl, volume);
10663 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10665 struct ShortcutsActivateData *data;
10666 GtkFileSystemVolume *volume;
10668 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10670 data = g_new0 (struct ShortcutsActivateData, 1);
10671 data->impl = g_object_ref (impl);
10672 data->file = g_object_ref (col_data);
10674 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10676 GMountOperation *mount_operation;
10677 GtkWidget *toplevel;
10679 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10681 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10683 impl->shortcuts_activate_iter_cancellable =
10684 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10686 shortcuts_activate_mount_enclosing_volume,
10691 impl->shortcuts_activate_iter_cancellable =
10692 _gtk_file_system_get_info (impl->file_system, data->file,
10694 shortcuts_activate_get_info_cb, data);
10697 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10699 search_activate (impl);
10701 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10703 recent_activate (impl);
10707 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10709 shortcuts_key_press_event_cb (GtkWidget *widget,
10710 GdkEventKey *event,
10711 GtkFileChooserDefault *impl)
10715 modifiers = gtk_accelerator_get_default_mod_mask ();
10717 if (key_is_left_or_right (event))
10719 gtk_widget_grab_focus (impl->browse_files_tree_view);
10723 if ((event->keyval == GDK_BackSpace
10724 || event->keyval == GDK_Delete
10725 || event->keyval == GDK_KP_Delete)
10726 && (event->state & modifiers) == 0)
10728 remove_selected_bookmarks (impl);
10732 if ((event->keyval == GDK_F2)
10733 && (event->state & modifiers) == 0)
10735 rename_selected_bookmark (impl);
10743 shortcuts_select_func (GtkTreeSelection *selection,
10744 GtkTreeModel *model,
10746 gboolean path_currently_selected,
10749 GtkFileChooserDefault *impl = data;
10750 GtkTreeIter filter_iter;
10751 ShortcutType shortcut_type;
10753 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10754 g_assert_not_reached ();
10756 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10758 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10762 list_select_func (GtkTreeSelection *selection,
10763 GtkTreeModel *model,
10765 gboolean path_currently_selected,
10768 GtkFileChooserDefault *impl = data;
10770 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10771 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10773 GtkTreeIter iter, child_iter;
10775 switch (impl->operation_mode)
10777 case OPERATION_MODE_SEARCH:
10779 gboolean is_folder;
10781 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10784 search_get_valid_child_iter (impl, &child_iter, &iter);
10785 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10786 MODEL_COL_IS_FOLDER, &is_folder,
10793 case OPERATION_MODE_RECENT:
10795 gboolean is_folder;
10797 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10800 recent_get_valid_child_iter (impl, &child_iter, &iter);
10801 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10802 MODEL_COL_IS_FOLDER, &is_folder,
10809 case OPERATION_MODE_BROWSE:
10813 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10816 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10817 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10818 if (info && (! _gtk_file_info_consider_as_directory (info)))
10829 list_selection_changed (GtkTreeSelection *selection,
10830 GtkFileChooserDefault *impl)
10832 /* See if we are in the new folder editable row for Save mode */
10833 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10834 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10837 gboolean had_selection;
10839 info = get_selected_file_info_from_file_list (impl, &had_selection);
10840 if (!had_selection)
10841 goto out; /* normal processing */
10844 return; /* We are on the editable row for New Folder */
10849 if (impl->location_entry)
10850 update_chooser_entry (impl);
10852 check_preview_change (impl);
10853 bookmarks_check_add_sensitivity (impl);
10855 g_signal_emit_by_name (impl, "selection-changed", 0);
10858 /* Callback used when a row in the file list is activated */
10860 list_row_activated (GtkTreeView *tree_view,
10862 GtkTreeViewColumn *column,
10863 GtkFileChooserDefault *impl)
10867 GtkTreeModel *model;
10868 gboolean is_folder;
10870 model = gtk_tree_view_get_model (tree_view);
10872 if (!gtk_tree_model_get_iter (model, &iter, path))
10875 gtk_tree_model_get (model, &iter,
10876 MODEL_COL_FILE, &file,
10877 MODEL_COL_IS_FOLDER, &is_folder,
10880 if (is_folder && file)
10882 change_folder_and_display_error (impl, file, FALSE);
10886 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10887 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10888 g_signal_emit_by_name (impl, "file-activated");
10891 g_object_unref (file);
10895 path_bar_clicked (GtkPathBar *path_bar,
10898 gboolean child_is_hidden,
10899 GtkFileChooserDefault *impl)
10902 pending_select_files_add (impl, child_file);
10904 if (!change_folder_and_display_error (impl, file, FALSE))
10907 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10908 * show hidden files so that ".baz" appears in the file list, as it will still
10909 * be shown in the path bar: "/foo/[bar]/.baz"
10911 if (child_is_hidden)
10912 g_object_set (impl, "show-hidden", TRUE, NULL);
10916 get_list_file_info (GtkFileChooserDefault *impl,
10919 GtkTreeIter child_iter;
10921 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10925 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10929 list_icon_data_func (GtkTreeViewColumn *tree_column,
10930 GtkCellRenderer *cell,
10931 GtkTreeModel *tree_model,
10935 GtkFileChooserDefault *impl = data;
10936 GtkTreeIter child_iter;
10937 GdkPixbuf *pixbuf = NULL;
10938 gboolean sensitive = TRUE;
10940 profile_start ("start", NULL);
10942 switch (impl->operation_mode)
10944 case OPERATION_MODE_SEARCH:
10946 GtkTreeIter child_iter;
10947 gboolean is_folder;
10949 search_get_valid_child_iter (impl, &child_iter, iter);
10950 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10951 MODEL_COL_PIXBUF, &pixbuf,
10952 MODEL_COL_IS_FOLDER, &is_folder,
10955 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10956 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10957 sensitive = is_folder;
10961 case OPERATION_MODE_RECENT:
10963 GtkTreeIter child_iter;
10964 GtkRecentInfo *info;
10965 gboolean is_folder;
10967 recent_get_valid_child_iter (impl, &child_iter, iter);
10968 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10969 RECENT_MODEL_COL_INFO, &info,
10970 MODEL_COL_IS_FOLDER, &is_folder,
10973 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10975 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10976 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10977 sensitive = is_folder;
10981 case OPERATION_MODE_BROWSE:
10986 info = get_list_file_info (impl, iter);
10988 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10991 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10996 /* FIXME: NULL GError */
10997 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
11002 /* We are on the editable row */
11007 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11008 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
11009 sensitive = _gtk_file_info_consider_as_directory (info);
11014 g_object_set (cell,
11016 "sensitive", sensitive,
11020 g_object_unref (pixbuf);
11022 profile_end ("end", NULL);
11026 list_name_data_func (GtkTreeViewColumn *tree_column,
11027 GtkCellRenderer *cell,
11028 GtkTreeModel *tree_model,
11032 GtkFileChooserDefault *impl = data;
11034 gboolean sensitive = TRUE;
11036 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11038 GtkTreeIter child_iter;
11039 gchar *display_name;
11040 gboolean is_folder;
11042 search_get_valid_child_iter (impl, &child_iter, iter);
11043 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11044 MODEL_COL_NAME, &display_name,
11045 MODEL_COL_IS_FOLDER, &is_folder,
11048 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11049 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11051 sensitive = is_folder;
11054 g_object_set (cell,
11055 "text", display_name,
11056 "sensitive", sensitive,
11057 "ellipsize", PANGO_ELLIPSIZE_END,
11063 if (impl->operation_mode == OPERATION_MODE_RECENT)
11065 GtkTreeIter child_iter;
11066 GtkRecentInfo *recent_info;
11067 gchar *display_name;
11068 gboolean is_folder;
11070 recent_get_valid_child_iter (impl, &child_iter, iter);
11071 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11072 RECENT_MODEL_COL_INFO, &recent_info,
11073 MODEL_COL_IS_FOLDER, &is_folder,
11076 display_name = gtk_recent_info_get_short_name (recent_info);
11078 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11079 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11081 sensitive = is_folder;
11084 g_object_set (cell,
11085 "text", display_name,
11086 "sensitive", sensitive,
11087 "ellipsize", PANGO_ELLIPSIZE_END,
11090 g_free (display_name);
11095 info = get_list_file_info (impl, iter);
11100 g_object_set (cell,
11101 "text", DEFAULT_NEW_FOLDER_NAME,
11103 "ellipsize", PANGO_ELLIPSIZE_NONE,
11110 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11111 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11113 sensitive = _gtk_file_info_consider_as_directory (info);
11116 g_object_set (cell,
11117 "text", g_file_info_get_display_name (info),
11118 "sensitive", sensitive,
11119 "ellipsize", PANGO_ELLIPSIZE_END,
11124 list_size_data_func (GtkTreeViewColumn *tree_column,
11125 GtkCellRenderer *cell,
11126 GtkTreeModel *tree_model,
11130 GtkFileChooserDefault *impl = data;
11134 gboolean sensitive = TRUE;
11136 if (impl->operation_mode == OPERATION_MODE_RECENT)
11139 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11141 GtkTreeIter child_iter;
11142 gboolean is_folder = FALSE;
11144 search_get_valid_child_iter (impl, &child_iter, iter);
11145 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11146 MODEL_COL_SIZE, &size,
11147 MODEL_COL_IS_FOLDER, &is_folder,
11150 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11151 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11152 sensitive = is_folder ? TRUE : FALSE;
11155 str = g_format_size_for_display (size);
11159 g_object_set (cell,
11161 "sensitive", sensitive,
11169 info = get_list_file_info (impl, iter);
11171 if (!info || _gtk_file_info_consider_as_directory (info))
11173 g_object_set (cell,
11175 "sensitive", sensitive,
11180 size = g_file_info_get_size (info);
11181 str = g_format_size_for_display (size);
11183 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11184 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11187 g_object_set (cell,
11189 "sensitive", sensitive,
11195 /* Tree column data callback for the file list; fetches the mtime of a file */
11197 list_mtime_data_func (GtkTreeViewColumn *tree_column,
11198 GtkCellRenderer *cell,
11199 GtkTreeModel *tree_model,
11203 GtkFileChooserDefault *impl;
11204 GTimeVal timeval = { 0, };
11206 gchar *date_str = NULL;
11207 gboolean sensitive = TRUE;
11209 const char *locale, *dot = NULL;
11210 gint64 codepage = -1;
11216 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11218 GtkTreeIter child_iter;
11220 gboolean is_folder;
11222 search_get_valid_child_iter (impl, &child_iter, iter);
11223 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11224 MODEL_COL_MTIME, &mtime,
11225 MODEL_COL_IS_FOLDER, &is_folder,
11228 time_mtime = (time_t) mtime;
11230 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11231 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11232 sensitive = is_folder;
11234 else if (impl->operation_mode == OPERATION_MODE_RECENT)
11236 GtkTreeIter child_iter;
11237 GtkRecentInfo *info;
11238 gboolean is_folder;
11240 recent_get_valid_child_iter (impl, &child_iter, iter);
11241 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11242 RECENT_MODEL_COL_INFO, &info,
11243 MODEL_COL_IS_FOLDER, &is_folder,
11247 time_mtime = gtk_recent_info_get_modified (info);
11251 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11252 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11253 sensitive = is_folder;
11259 info = get_list_file_info (impl, iter);
11262 g_object_set (cell,
11269 g_file_info_get_modification_time (info, &timeval);
11270 time_mtime = timeval.tv_sec;
11272 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11273 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11274 sensitive = _gtk_file_info_consider_as_directory (info);
11277 if (G_UNLIKELY (time_mtime == 0))
11278 date_str = g_strdup (_("Unknown"));
11280 date_str = my_g_format_time_for_display (time_mtime);
11282 g_object_set (cell,
11284 "sensitive", sensitive,
11290 _gtk_file_chooser_default_new (void)
11292 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11296 location_set_user_text (GtkFileChooserDefault *impl,
11299 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11300 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11304 location_popup_handler (GtkFileChooserDefault *impl,
11307 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11309 GtkWidget *widget_to_focus;
11311 /* This will give us the location widgets back */
11312 switch (impl->operation_mode)
11314 case OPERATION_MODE_SEARCH:
11315 search_switch_to_browse_mode (impl);
11317 case OPERATION_MODE_RECENT:
11318 recent_switch_to_browse_mode (impl);
11320 case OPERATION_MODE_BROWSE:
11321 g_assert_not_reached ();
11325 if (impl->current_folder)
11326 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11328 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11329 widget_to_focus = impl->browse_files_tree_view;
11331 widget_to_focus = impl->location_entry;
11333 gtk_widget_grab_focus (widget_to_focus);
11337 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11338 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11340 LocationMode new_mode;
11344 /* since the user typed something, we unconditionally want to turn on the entry */
11345 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11347 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11348 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11349 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11350 new_mode = LOCATION_MODE_PATH_BAR;
11353 g_assert_not_reached ();
11357 location_mode_set (impl, new_mode, TRUE);
11358 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11361 location_set_user_text (impl, path);
11364 location_entry_set_initial_text (impl);
11365 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11369 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11370 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11372 gtk_widget_grab_focus (impl->location_entry);
11374 location_set_user_text (impl, path);
11377 g_assert_not_reached ();
11380 /* Handler for the "up-folder" keybinding signal */
11382 up_folder_handler (GtkFileChooserDefault *impl)
11384 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11387 /* Handler for the "down-folder" keybinding signal */
11389 down_folder_handler (GtkFileChooserDefault *impl)
11391 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11394 /* Switches to the shortcut in the specified index */
11396 switch_to_shortcut (GtkFileChooserDefault *impl,
11401 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11402 g_assert_not_reached ();
11404 shortcuts_activate_iter (impl, &iter);
11407 /* Handler for the "home-folder" keybinding signal */
11409 home_folder_handler (GtkFileChooserDefault *impl)
11411 if (impl->has_home)
11412 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11415 /* Handler for the "desktop-folder" keybinding signal */
11417 desktop_folder_handler (GtkFileChooserDefault *impl)
11419 if (impl->has_desktop)
11420 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11423 /* Handler for the "search-shortcut" keybinding signal */
11425 search_shortcut_handler (GtkFileChooserDefault *impl)
11427 if (impl->has_search)
11429 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11431 /* we want the entry widget to grab the focus the first
11432 * time, not the browse_files_tree_view widget.
11434 if (impl->search_entry)
11435 gtk_widget_grab_focus (impl->search_entry);
11439 /* Handler for the "recent-shortcut" keybinding signal */
11441 recent_shortcut_handler (GtkFileChooserDefault *impl)
11443 if (impl->has_recent)
11444 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11448 quick_bookmark_handler (GtkFileChooserDefault *impl,
11449 gint bookmark_index)
11454 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11457 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11459 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11460 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11463 gtk_tree_path_free (path);
11465 switch_to_shortcut (impl, bookmark_pos);
11469 show_hidden_handler (GtkFileChooserDefault *impl)
11471 g_object_set (impl,
11472 "show-hidden", !impl->show_hidden,
11477 /* Drag and drop interfaces */
11480 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11485 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11487 model->impl = NULL;
11490 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11492 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11495 ShortcutsPaneModelFilter *model;
11499 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11501 pos = *gtk_tree_path_get_indices (path);
11502 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11504 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11507 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11509 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11511 GtkSelectionData *selection_data)
11513 ShortcutsPaneModelFilter *model;
11515 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11522 /* Fill the GtkTreeDragSourceIface vtable */
11524 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11526 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11527 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11531 /* Fill the GtkTreeDragDestIface vtable */
11533 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11535 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11536 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11540 static GtkTreeModel *
11541 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11542 GtkTreeModel *child_model,
11545 ShortcutsPaneModelFilter *model;
11547 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11548 "child-model", child_model,
11549 "virtual-root", root,
11552 model->impl = impl;
11554 return GTK_TREE_MODEL (model);
11560 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11563 RecentModelSort *model;
11564 GtkTreeIter iter, child_iter;
11565 gboolean is_folder;
11567 model = RECENT_MODEL_SORT (drag_source);
11568 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11571 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11572 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11573 MODEL_COL_IS_FOLDER, &is_folder,
11580 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11582 GtkSelectionData *selection_data)
11584 RecentModelSort *model;
11585 GtkTreeIter iter, child_iter;
11589 model = RECENT_MODEL_SORT (drag_source);
11590 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11593 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11594 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11595 MODEL_COL_FILE, &file,
11597 g_assert (file != NULL);
11599 uris[0] = g_file_get_uri (file);
11602 gtk_selection_data_set_uris (selection_data, uris);
11605 g_object_unref (file);
11611 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11613 iface->row_draggable = recent_model_sort_row_draggable;
11614 iface->drag_data_get = recent_model_sort_drag_data_get;
11618 _recent_model_sort_class_init (RecentModelSortClass *klass)
11624 _recent_model_sort_init (RecentModelSort *model)
11626 model->impl = NULL;
11629 static GtkTreeModel *
11630 recent_model_sort_new (GtkFileChooserDefault *impl,
11631 GtkTreeModel *child_model)
11633 RecentModelSort *model;
11635 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11636 "model", child_model,
11638 model->impl = impl;
11640 return GTK_TREE_MODEL (model);
11646 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11649 SearchModelSort *model;
11650 GtkTreeIter iter, child_iter;
11651 gboolean is_folder;
11653 model = SEARCH_MODEL_SORT (drag_source);
11654 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11657 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11658 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11659 MODEL_COL_IS_FOLDER, &is_folder,
11666 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11668 GtkSelectionData *selection_data)
11670 SearchModelSort *model;
11671 GtkTreeIter iter, child_iter;
11675 model = SEARCH_MODEL_SORT (drag_source);
11676 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11679 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11680 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11681 MODEL_COL_FILE, &file,
11683 g_assert (file != NULL);
11685 uris[0] = g_file_get_uri (file);
11688 gtk_selection_data_set_uris (selection_data, uris);
11691 g_object_unref (file);
11697 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11699 iface->row_draggable = search_model_sort_row_draggable;
11700 iface->drag_data_get = search_model_sort_drag_data_get;
11704 _search_model_sort_class_init (SearchModelSortClass *klass)
11710 _search_model_sort_init (SearchModelSort *model)
11712 model->impl = NULL;
11715 static GtkTreeModel *
11716 search_model_sort_new (GtkFileChooserDefault *impl,
11717 GtkTreeModel *child_model)
11719 SearchModelSort *model;
11721 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11722 "model", child_model,
11724 model->impl = impl;
11726 return GTK_TREE_MODEL (model);