1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechoosersettings.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystem.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkmountoperation.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkrecentfilter.h"
59 #include "gtkrecentmanager.h"
60 #include "gtkscrolledwindow.h"
61 #include "gtkseparatormenuitem.h"
62 #include "gtksizegroup.h"
65 #include "gtktooltip.h"
66 #include "gtktreednd.h"
67 #include "gtktreeprivate.h"
68 #include "gtktreeselection.h"
78 #include <sys/types.h>
89 #undef PROFILE_FILE_CHOOSER
90 #ifdef PROFILE_FILE_CHOOSER
97 #define PROFILE_INDENT 4
98 static int profile_indent;
101 profile_add_indent (int indent)
103 profile_indent += indent;
104 if (profile_indent < 0)
105 g_error ("You screwed up your indentation");
109 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
114 profile_add_indent (indent);
116 if (profile_indent == 0)
117 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
119 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 profile_add_indent (indent);
128 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
129 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
130 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
132 #define profile_start(x, y)
133 #define profile_end(x, y)
134 #define profile_msg(x, y)
139 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
141 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
142 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
143 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define MAX_LOADING_TIME 500
147 struct _GtkFileChooserDefaultClass
149 GtkVBoxClass parent_class;
155 LOCATION_POPUP_ON_PASTE,
161 LOCATION_TOGGLE_POPUP,
169 static guint signals[LAST_SIGNAL] = { 0 };
171 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
173 SHORTCUTS_COL_PIXBUF,
177 SHORTCUTS_COL_REMOVABLE,
178 SHORTCUTS_COL_PIXBUF_VISIBLE,
179 SHORTCUTS_COL_CANCELLABLE,
180 SHORTCUTS_COL_NUM_COLUMNS
185 SHORTCUT_TYPE_VOLUME,
186 SHORTCUT_TYPE_SEPARATOR,
187 SHORTCUT_TYPE_SEARCH,
191 /* Column numbers for the file list */
196 FILE_LIST_COL_NUM_COLUMNS
199 /* Column numbers for the search model.
200 * Keep this in sync with search_setup_model()
203 SEARCH_MODEL_COL_FILE,
204 SEARCH_MODEL_COL_DISPLAY_NAME,
205 SEARCH_MODEL_COL_COLLATION_KEY,
206 SEARCH_MODEL_COL_STAT,
207 SEARCH_MODEL_COL_CANCELLABLE,
208 SEARCH_MODEL_COL_PIXBUF,
209 SEARCH_MODEL_COL_MIME_TYPE,
210 SEARCH_MODEL_COL_IS_FOLDER,
211 SEARCH_MODEL_COL_NUM_COLUMNS
215 RECENT_MODEL_COL_FILE,
216 RECENT_MODEL_COL_DISPLAY_NAME,
217 RECENT_MODEL_COL_INFO,
218 RECENT_MODEL_COL_IS_FOLDER,
219 RECENT_MODEL_COL_CANCELLABLE,
220 RECENT_MODEL_COL_NUM_COLUMNS
223 /* Identifiers for target types */
229 search_is_possible (GtkFileChooserDefault *impl)
231 if (impl->search_engine == NULL)
232 impl->search_engine = _gtk_search_engine_new ();
234 return impl->search_engine != NULL;
237 /* Interesting places in the shortcuts bar */
241 SHORTCUTS_RECENT_SEPARATOR,
246 SHORTCUTS_BOOKMARKS_SEPARATOR,
248 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
249 SHORTCUTS_CURRENT_FOLDER
252 /* Icon size for if we can't get it from the theme */
253 #define FALLBACK_ICON_SIZE 16
255 #define PREVIEW_HBOX_SPACING 12
259 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
260 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
262 static GObject* gtk_file_chooser_default_constructor (GType type,
263 guint n_construct_properties,
264 GObjectConstructParam *construct_params);
265 static void gtk_file_chooser_default_finalize (GObject *object);
266 static void gtk_file_chooser_default_set_property (GObject *object,
270 static void gtk_file_chooser_default_get_property (GObject *object,
274 static void gtk_file_chooser_default_dispose (GObject *object);
275 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
276 static void gtk_file_chooser_default_map (GtkWidget *widget);
277 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
278 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
279 GtkWidget *previous_toplevel);
280 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
281 GtkStyle *previous_style);
282 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
283 GdkScreen *previous_screen);
284 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
285 GtkAllocation *allocation);
287 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
290 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
293 gboolean clear_entry,
295 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
296 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
298 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
301 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
305 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
306 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
307 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
313 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
316 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
319 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
321 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
323 gint *default_height);
324 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
325 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
327 static void location_popup_handler (GtkFileChooserDefault *impl,
329 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
330 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
331 static void up_folder_handler (GtkFileChooserDefault *impl);
332 static void down_folder_handler (GtkFileChooserDefault *impl);
333 static void home_folder_handler (GtkFileChooserDefault *impl);
334 static void desktop_folder_handler (GtkFileChooserDefault *impl);
335 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
336 gint bookmark_index);
337 static void show_hidden_handler (GtkFileChooserDefault *impl);
338 static void search_shortcut_handler (GtkFileChooserDefault *impl);
339 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
340 static void update_appearance (GtkFileChooserDefault *impl);
342 static void set_current_filter (GtkFileChooserDefault *impl,
343 GtkFileFilter *filter);
344 static void check_preview_change (GtkFileChooserDefault *impl);
346 static void filter_combo_changed (GtkComboBox *combo_box,
347 GtkFileChooserDefault *impl);
349 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
351 GtkFileChooserDefault *impl);
353 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
356 gboolean path_currently_selected,
358 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
360 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
362 static int shortcuts_get_index (GtkFileChooserDefault *impl,
363 ShortcutsIndex where);
364 static int shortcut_find_position (GtkFileChooserDefault *impl,
367 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
369 static gboolean list_select_func (GtkTreeSelection *selection,
372 gboolean path_currently_selected,
375 static void list_selection_changed (GtkTreeSelection *tree_selection,
376 GtkFileChooserDefault *impl);
377 static void list_row_activated (GtkTreeView *tree_view,
379 GtkTreeViewColumn *column,
380 GtkFileChooserDefault *impl);
382 static void select_func (GtkFileSystemModel *model,
387 static void path_bar_clicked (GtkPathBar *path_bar,
390 gboolean child_is_hidden,
391 GtkFileChooserDefault *impl);
393 static void add_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
395 static void remove_bookmark_button_clicked_cb (GtkButton *button,
396 GtkFileChooserDefault *impl);
397 static void save_folder_combo_changed_cb (GtkComboBox *combo,
398 GtkFileChooserDefault *impl);
400 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
401 GtkCellRenderer *cell,
402 GtkTreeModel *tree_model,
405 static void list_name_data_func (GtkTreeViewColumn *tree_column,
406 GtkCellRenderer *cell,
407 GtkTreeModel *tree_model,
411 static void list_size_data_func (GtkTreeViewColumn *tree_column,
412 GtkCellRenderer *cell,
413 GtkTreeModel *tree_model,
417 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
418 GtkCellRenderer *cell,
419 GtkTreeModel *tree_model,
423 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
426 static void load_remove_timer (GtkFileChooserDefault *impl);
427 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
429 static void location_button_toggled_cb (GtkToggleButton *toggle,
430 GtkFileChooserDefault *impl);
431 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
433 static void search_stop_searching (GtkFileChooserDefault *impl,
434 gboolean remove_query);
435 static void search_clear_model (GtkFileChooserDefault *impl,
436 gboolean remove_from_treeview);
437 static gboolean search_should_respond (GtkFileChooserDefault *impl);
438 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
439 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
440 static void search_entry_activate_cb (GtkEntry *entry,
442 static void settings_load (GtkFileChooserDefault *impl);
443 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
444 GtkTreeIter *child_iter,
447 static void recent_stop_loading (GtkFileChooserDefault *impl);
448 static void recent_clear_model (GtkFileChooserDefault *impl,
449 gboolean remove_from_treeview);
450 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
451 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
452 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
453 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
454 GtkTreeIter *child_iter,
456 static void set_file_system_backend (GtkFileChooserDefault *impl);
462 /* Drag and drop interface declarations */
465 GtkTreeModelFilter parent;
467 GtkFileChooserDefault *impl;
468 } ShortcutsPaneModelFilter;
471 GtkTreeModelFilterClass parent_class;
472 } ShortcutsPaneModelFilterClass;
474 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
475 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
477 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
479 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
480 _shortcuts_pane_model_filter,
481 GTK_TYPE_TREE_MODEL_FILTER,
482 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
483 shortcuts_pane_model_filter_drag_source_iface_init))
485 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
486 GtkTreeModel *child_model,
491 GtkTreeModelSort parent;
493 GtkFileChooserDefault *impl;
497 GtkTreeModelSortClass parent_class;
498 } RecentModelSortClass;
500 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
501 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
503 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
505 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
507 GTK_TYPE_TREE_MODEL_SORT,
508 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
509 recent_model_sort_drag_source_iface_init));
511 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
512 GtkTreeModel *child_model);
516 GtkTreeModelSort parent;
518 GtkFileChooserDefault *impl;
522 GtkTreeModelSortClass parent_class;
523 } SearchModelSortClass;
525 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
526 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
528 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
530 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
532 GTK_TYPE_TREE_MODEL_SORT,
533 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
534 search_model_sort_drag_source_iface_init));
536 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
537 GtkTreeModel *child_model);
541 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
542 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
543 gtk_file_chooser_default_iface_init)
544 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
545 gtk_file_chooser_embed_default_iface_init));
548 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
550 static const guint quick_bookmark_keyvals[10] = {
551 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
553 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
554 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
555 GtkBindingSet *binding_set;
558 gobject_class->finalize = gtk_file_chooser_default_finalize;
559 gobject_class->constructor = gtk_file_chooser_default_constructor;
560 gobject_class->set_property = gtk_file_chooser_default_set_property;
561 gobject_class->get_property = gtk_file_chooser_default_get_property;
562 gobject_class->dispose = gtk_file_chooser_default_dispose;
564 widget_class->show_all = gtk_file_chooser_default_show_all;
565 widget_class->map = gtk_file_chooser_default_map;
566 widget_class->unmap = gtk_file_chooser_default_unmap;
567 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
568 widget_class->style_set = gtk_file_chooser_default_style_set;
569 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
570 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
572 signals[LOCATION_POPUP] =
573 _gtk_binding_signal_new (I_("location-popup"),
574 G_OBJECT_CLASS_TYPE (class),
575 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
576 G_CALLBACK (location_popup_handler),
578 _gtk_marshal_VOID__STRING,
579 G_TYPE_NONE, 1, G_TYPE_STRING);
580 signals[LOCATION_POPUP_ON_PASTE] =
581 _gtk_binding_signal_new ("location-popup-on-paste",
582 G_OBJECT_CLASS_TYPE (class),
583 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
584 G_CALLBACK (location_popup_on_paste_handler),
586 _gtk_marshal_VOID__VOID,
588 signals[LOCATION_TOGGLE_POPUP] =
589 _gtk_binding_signal_new (I_("location-toggle-popup"),
590 G_OBJECT_CLASS_TYPE (class),
591 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
592 G_CALLBACK (location_toggle_popup_handler),
594 _gtk_marshal_VOID__VOID,
597 _gtk_binding_signal_new (I_("up-folder"),
598 G_OBJECT_CLASS_TYPE (class),
599 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
600 G_CALLBACK (up_folder_handler),
602 _gtk_marshal_VOID__VOID,
604 signals[DOWN_FOLDER] =
605 _gtk_binding_signal_new (I_("down-folder"),
606 G_OBJECT_CLASS_TYPE (class),
607 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
608 G_CALLBACK (down_folder_handler),
610 _gtk_marshal_VOID__VOID,
612 signals[HOME_FOLDER] =
613 _gtk_binding_signal_new (I_("home-folder"),
614 G_OBJECT_CLASS_TYPE (class),
615 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
616 G_CALLBACK (home_folder_handler),
618 _gtk_marshal_VOID__VOID,
620 signals[DESKTOP_FOLDER] =
621 _gtk_binding_signal_new (I_("desktop-folder"),
622 G_OBJECT_CLASS_TYPE (class),
623 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
624 G_CALLBACK (desktop_folder_handler),
626 _gtk_marshal_VOID__VOID,
628 signals[QUICK_BOOKMARK] =
629 _gtk_binding_signal_new (I_("quick-bookmark"),
630 G_OBJECT_CLASS_TYPE (class),
631 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
632 G_CALLBACK (quick_bookmark_handler),
634 _gtk_marshal_VOID__INT,
635 G_TYPE_NONE, 1, G_TYPE_INT);
636 signals[SHOW_HIDDEN] =
637 _gtk_binding_signal_new ("show-hidden",
638 G_OBJECT_CLASS_TYPE (class),
639 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
640 G_CALLBACK (show_hidden_handler),
642 _gtk_marshal_VOID__VOID,
644 signals[SEARCH_SHORTCUT] =
645 _gtk_binding_signal_new ("search-shortcut",
646 G_OBJECT_CLASS_TYPE (class),
647 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
648 G_CALLBACK (search_shortcut_handler),
650 _gtk_marshal_VOID__VOID,
652 signals[RECENT_SHORTCUT] =
653 _gtk_binding_signal_new ("recent-shortcut",
654 G_OBJECT_CLASS_TYPE (class),
655 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
656 G_CALLBACK (recent_shortcut_handler),
658 _gtk_marshal_VOID__VOID,
661 binding_set = gtk_binding_set_by_class (class);
663 gtk_binding_entry_add_signal (binding_set,
664 GDK_l, GDK_CONTROL_MASK,
665 "location-toggle-popup",
668 gtk_binding_entry_add_signal (binding_set,
671 1, G_TYPE_STRING, "/");
672 gtk_binding_entry_add_signal (binding_set,
675 1, G_TYPE_STRING, "/");
678 gtk_binding_entry_add_signal (binding_set,
681 1, G_TYPE_STRING, "~");
684 gtk_binding_entry_add_signal (binding_set,
685 GDK_v, GDK_CONTROL_MASK,
686 "location-popup-on-paste",
688 gtk_binding_entry_add_signal (binding_set,
689 GDK_Up, GDK_MOD1_MASK,
692 gtk_binding_entry_add_signal (binding_set,
696 gtk_binding_entry_add_signal (binding_set,
697 GDK_KP_Up, GDK_MOD1_MASK,
701 gtk_binding_entry_add_signal (binding_set,
702 GDK_Down, GDK_MOD1_MASK,
705 gtk_binding_entry_add_signal (binding_set,
706 GDK_KP_Down, GDK_MOD1_MASK,
710 gtk_binding_entry_add_signal (binding_set,
711 GDK_Home, GDK_MOD1_MASK,
714 gtk_binding_entry_add_signal (binding_set,
715 GDK_KP_Home, GDK_MOD1_MASK,
718 gtk_binding_entry_add_signal (binding_set,
719 GDK_d, GDK_MOD1_MASK,
722 gtk_binding_entry_add_signal (binding_set,
723 GDK_h, GDK_CONTROL_MASK,
726 gtk_binding_entry_add_signal (binding_set,
727 GDK_s, GDK_MOD1_MASK,
730 gtk_binding_entry_add_signal (binding_set,
731 GDK_r, GDK_MOD1_MASK,
735 for (i = 0; i < 10; i++)
736 gtk_binding_entry_add_signal (binding_set,
737 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
741 _gtk_file_chooser_install_properties (gobject_class);
745 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
747 iface->select_file = gtk_file_chooser_default_select_file;
748 iface->unselect_file = gtk_file_chooser_default_unselect_file;
749 iface->select_all = gtk_file_chooser_default_select_all;
750 iface->unselect_all = gtk_file_chooser_default_unselect_all;
751 iface->get_files = gtk_file_chooser_default_get_files;
752 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
753 iface->get_file_system = gtk_file_chooser_default_get_file_system;
754 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
755 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
756 iface->set_current_name = gtk_file_chooser_default_set_current_name;
757 iface->add_filter = gtk_file_chooser_default_add_filter;
758 iface->remove_filter = gtk_file_chooser_default_remove_filter;
759 iface->list_filters = gtk_file_chooser_default_list_filters;
760 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
761 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
762 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
766 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
768 iface->get_default_size = gtk_file_chooser_default_get_default_size;
769 iface->should_respond = gtk_file_chooser_default_should_respond;
770 iface->initial_focus = gtk_file_chooser_default_initial_focus;
774 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
776 profile_start ("start", NULL);
777 #ifdef PROFILE_FILE_CHOOSER
778 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
780 impl->local_only = TRUE;
781 impl->preview_widget_active = TRUE;
782 impl->use_preview_label = TRUE;
783 impl->select_multiple = FALSE;
784 impl->show_hidden = FALSE;
785 impl->icon_size = FALLBACK_ICON_SIZE;
786 impl->load_state = LOAD_EMPTY;
787 impl->reload_state = RELOAD_EMPTY;
788 impl->pending_select_files = NULL;
789 impl->location_mode = LOCATION_MODE_PATH_BAR;
790 impl->operation_mode = OPERATION_MODE_BROWSE;
791 impl->recent_manager = gtk_recent_manager_get_default ();
793 gtk_box_set_spacing (GTK_BOX (impl), 12);
795 set_file_system_backend (impl);
797 profile_end ("end", NULL);
800 /* Frees the data columns for the specified iter in the shortcuts model*/
802 shortcuts_free_row_data (GtkFileChooserDefault *impl,
806 ShortcutType shortcut_type;
807 GCancellable *cancellable;
809 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
810 SHORTCUTS_COL_DATA, &col_data,
811 SHORTCUTS_COL_TYPE, &shortcut_type,
812 SHORTCUTS_COL_CANCELLABLE, &cancellable,
816 g_cancellable_cancel (cancellable);
818 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
819 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
823 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
825 GtkFileSystemVolume *volume;
828 _gtk_file_system_volume_free (volume);
834 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
837 g_object_unref (file);
841 /* Frees all the data columns in the shortcuts model */
843 shortcuts_free (GtkFileChooserDefault *impl)
847 if (!impl->shortcuts_model)
850 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
853 shortcuts_free_row_data (impl, &iter);
855 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
857 g_object_unref (impl->shortcuts_model);
858 impl->shortcuts_model = NULL;
862 pending_select_files_free (GtkFileChooserDefault *impl)
864 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
865 g_slist_free (impl->pending_select_files);
866 impl->pending_select_files = NULL;
870 pending_select_files_add (GtkFileChooserDefault *impl,
873 impl->pending_select_files =
874 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
877 /* Used from gtk_tree_selection_selected_foreach() */
879 store_selection_foreach (GtkTreeModel *model,
884 GtkFileChooserDefault *impl;
885 GtkTreeIter child_iter;
888 impl = GTK_FILE_CHOOSER_DEFAULT (data);
890 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
892 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
893 pending_select_files_add (impl, file);
896 /* Stores the current selection in the list of paths to select; this is used to
897 * preserve the selection when reloading the current folder.
900 pending_select_files_store_selection (GtkFileChooserDefault *impl)
902 GtkTreeSelection *selection;
904 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
905 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
909 gtk_file_chooser_default_finalize (GObject *object)
911 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
914 if (impl->shortcuts_pane_filter_model)
915 g_object_unref (impl->shortcuts_pane_filter_model);
917 if (impl->shortcuts_combo_filter_model)
918 g_object_unref (impl->shortcuts_combo_filter_model);
920 shortcuts_free (impl);
922 g_object_unref (impl->file_system);
924 g_free (impl->browse_files_last_selected_name);
926 for (l = impl->filters; l; l = l->next)
928 GtkFileFilter *filter;
930 filter = GTK_FILE_FILTER (l->data);
931 g_object_unref (filter);
933 g_slist_free (impl->filters);
935 if (impl->current_filter)
936 g_object_unref (impl->current_filter);
938 if (impl->current_volume_file)
939 g_object_unref (impl->current_volume_file);
941 if (impl->current_folder)
942 g_object_unref (impl->current_folder);
944 if (impl->preview_file)
945 g_object_unref (impl->preview_file);
947 load_remove_timer (impl);
949 /* Free all the Models we have */
950 if (impl->browse_files_model)
951 g_object_unref (impl->browse_files_model);
953 if (impl->sort_model)
954 g_object_unref (impl->sort_model);
956 search_clear_model (impl, FALSE);
957 recent_clear_model (impl, FALSE);
959 g_free (impl->preview_display_name);
961 g_free (impl->edited_new_text);
963 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
966 /* Shows an error dialog set as transient for the specified window */
968 error_message_with_parent (GtkWindow *parent,
974 dialog = gtk_message_dialog_new (parent,
975 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
980 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
984 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
986 gtk_dialog_run (GTK_DIALOG (dialog));
987 gtk_widget_destroy (dialog);
990 /* Returns a toplevel GtkWindow, or NULL if none */
992 get_toplevel (GtkWidget *widget)
996 toplevel = gtk_widget_get_toplevel (widget);
997 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1000 return GTK_WINDOW (toplevel);
1003 /* Shows an error dialog for the file chooser */
1005 error_message (GtkFileChooserDefault *impl,
1009 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1012 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1014 error_dialog (GtkFileChooserDefault *impl,
1025 uri = g_file_get_uri (file);
1026 text = g_strdup_printf (msg, uri);
1027 error_message (impl, text, error->message);
1030 g_error_free (error);
1034 /* Displays an error message about not being able to get information for a file.
1035 * Frees the GError as well.
1038 error_getting_info_dialog (GtkFileChooserDefault *impl,
1043 _("Could not retrieve information about the file"),
1047 /* Shows an error dialog about not being able to add a bookmark */
1049 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1054 _("Could not add a bookmark"),
1058 /* Shows an error dialog about not being able to remove a bookmark */
1060 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1065 _("Could not remove bookmark"),
1069 /* Shows an error dialog about not being able to create a folder */
1071 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1076 _("The folder could not be created"),
1080 /* Shows an error about not being able to create a folder because a file with
1081 * the same name is already there.
1084 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1089 _("The folder could not be created, as a file with the same "
1090 "name already exists. Try using a different name for the "
1091 "folder, or rename the file first."),
1095 /* Shows an error dialog about not being able to create a filename */
1097 error_building_filename_dialog (GtkFileChooserDefault *impl,
1100 error_dialog (impl, _("Invalid file name"),
1104 /* Shows an error dialog when we cannot switch to a folder */
1106 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1110 error_dialog (impl, _("The folder contents could not be displayed"),
1114 /* Changes folders, displaying an error dialog if this fails */
1116 change_folder_and_display_error (GtkFileChooserDefault *impl,
1118 gboolean clear_entry)
1123 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1125 /* We copy the path because of this case:
1127 * list_row_activated()
1128 * fetches path from model; path belongs to the model (*)
1129 * calls change_folder_and_display_error()
1130 * calls _gtk_file_chooser_set_current_folder_file()
1131 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1135 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1138 error_changing_folder_dialog (impl, file, error);
1144 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1146 if (impl->use_preview_label)
1148 if (!impl->preview_label)
1150 impl->preview_label = gtk_label_new (impl->preview_display_name);
1151 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1152 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1153 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1154 gtk_widget_show (impl->preview_label);
1159 if (impl->preview_label)
1161 gtk_widget_destroy (impl->preview_label);
1162 impl->preview_label = NULL;
1166 if (impl->preview_widget_active && impl->preview_widget)
1167 gtk_widget_show (impl->preview_box);
1169 gtk_widget_hide (impl->preview_box);
1171 g_signal_emit_by_name (impl, "default-size-changed");
1175 set_preview_widget (GtkFileChooserDefault *impl,
1176 GtkWidget *preview_widget)
1178 if (preview_widget == impl->preview_widget)
1181 if (impl->preview_widget)
1182 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1183 impl->preview_widget);
1185 impl->preview_widget = preview_widget;
1186 if (impl->preview_widget)
1188 gtk_widget_show (impl->preview_widget);
1189 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1190 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1191 impl->preview_widget,
1192 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1195 update_preview_widget_visibility (impl);
1198 /* Renders a "Search" icon at an appropriate size for a tree view */
1200 render_search_icon (GtkFileChooserDefault *impl)
1202 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1206 render_recent_icon (GtkFileChooserDefault *impl)
1208 GtkIconTheme *theme;
1211 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1212 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1214 theme = gtk_icon_theme_get_default ();
1216 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1222 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1228 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1229 struct ReloadIconsData
1231 GtkFileChooserDefault *impl;
1232 GtkTreeRowReference *row_ref;
1236 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1238 const GError *error,
1244 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1245 struct ReloadIconsData *data = user_data;
1247 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1250 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1252 if (cancelled || error)
1255 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1257 path = gtk_tree_row_reference_get_path (data->row_ref);
1258 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1259 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1260 SHORTCUTS_COL_PIXBUF, pixbuf,
1262 gtk_tree_path_free (path);
1265 g_object_unref (pixbuf);
1268 gtk_tree_row_reference_free (data->row_ref);
1269 g_object_unref (data->impl);
1272 g_object_unref (cancellable);
1276 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1281 profile_start ("start", NULL);
1283 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1286 for (l = impl->reload_icon_cancellables; l; l = l->next)
1288 GCancellable *cancellable = G_CANCELLABLE (l->data);
1289 g_cancellable_cancel (cancellable);
1291 g_slist_free (impl->reload_icon_cancellables);
1292 impl->reload_icon_cancellables = NULL;
1297 ShortcutType shortcut_type;
1298 gboolean pixbuf_visible;
1301 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1302 SHORTCUTS_COL_DATA, &data,
1303 SHORTCUTS_COL_TYPE, &shortcut_type,
1304 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1310 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1312 GtkFileSystemVolume *volume;
1315 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1316 impl->icon_size, NULL);
1318 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1320 if (g_file_is_native (G_FILE (data)))
1323 struct ReloadIconsData *info;
1324 GtkTreePath *tree_path;
1325 GCancellable *cancellable;
1329 info = g_new0 (struct ReloadIconsData, 1);
1330 info->impl = g_object_ref (impl);
1331 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1332 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1333 gtk_tree_path_free (tree_path);
1335 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1337 shortcuts_reload_icons_get_info_cb,
1339 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1343 GtkIconTheme *icon_theme;
1345 /* Don't call get_info for remote paths to avoid latency and
1347 * If we switch to a better bookmarks file format (XBEL), we
1348 * should use mime info to get a better icon.
1350 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1351 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1352 impl->icon_size, 0, NULL);
1355 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1357 pixbuf = render_search_icon (impl);
1359 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1361 pixbuf = render_recent_icon (impl);
1364 gtk_list_store_set (impl->shortcuts_model, &iter,
1365 SHORTCUTS_COL_PIXBUF, pixbuf,
1369 g_object_unref (pixbuf);
1373 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1377 profile_end ("end", NULL);
1381 shortcuts_find_folder (GtkFileChooserDefault *impl,
1384 GtkTreeSelection *selection;
1388 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1390 g_assert (folder != NULL);
1391 pos = shortcut_find_position (impl, folder);
1394 gtk_tree_selection_unselect_all (selection);
1398 path = gtk_tree_path_new_from_indices (pos, -1);
1399 gtk_tree_selection_select_path (selection, path);
1400 gtk_tree_path_free (path);
1403 /* If a shortcut corresponds to the current folder, selects it */
1405 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1407 shortcuts_find_folder (impl, impl->current_folder);
1410 /* Removes the specified number of rows from the shortcuts list */
1412 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1418 path = gtk_tree_path_new_from_indices (start_row, -1);
1420 for (; n_rows; n_rows--)
1424 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1425 g_assert_not_reached ();
1427 shortcuts_free_row_data (impl, &iter);
1428 gtk_list_store_remove (impl->shortcuts_model, &iter);
1431 gtk_tree_path_free (path);
1435 shortcuts_update_count (GtkFileChooserDefault *impl,
1436 ShortcutsIndex type,
1441 case SHORTCUTS_HOME:
1443 impl->has_home = FALSE;
1445 impl->has_home = TRUE;
1448 case SHORTCUTS_DESKTOP:
1450 impl->has_desktop = FALSE;
1452 impl->has_desktop = TRUE;
1455 case SHORTCUTS_VOLUMES:
1456 impl->num_volumes += value;
1459 case SHORTCUTS_SHORTCUTS:
1460 impl->num_shortcuts += value;
1463 case SHORTCUTS_BOOKMARKS:
1464 impl->num_bookmarks += value;
1467 case SHORTCUTS_CURRENT_FOLDER:
1469 impl->shortcuts_current_folder_active = FALSE;
1471 impl->shortcuts_current_folder_active = TRUE;
1480 struct ShortcutsInsertRequest
1482 GtkFileChooserDefault *impl;
1486 GtkTreeRowReference *row_ref;
1487 ShortcutsIndex type;
1493 get_file_info_finished (GCancellable *cancellable,
1495 const GError *error,
1499 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1503 GCancellable *model_cancellable;
1504 struct ShortcutsInsertRequest *request = data;
1506 path = gtk_tree_row_reference_get_path (request->row_ref);
1508 /* Handle doesn't exist anymore in the model */
1511 pos = gtk_tree_path_get_indices (path)[0];
1512 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1514 gtk_tree_path_free (path);
1516 /* validate cancellable, else goto out */
1517 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1518 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1520 if (cancellable != model_cancellable)
1523 /* set the cancellable to NULL in the model (we unref later on) */
1524 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1525 SHORTCUTS_COL_CANCELLABLE, NULL,
1533 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1534 shortcuts_update_count (request->impl, request->type, -1);
1536 if (request->type == SHORTCUTS_HOME)
1540 home = g_file_new_for_path (g_get_home_dir ());
1541 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1542 g_object_unref (home);
1544 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1546 /* Remove the current folder separator */
1547 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1548 shortcuts_remove_rows (request->impl, separator_pos, 1);
1554 if (!request->label_copy)
1555 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1556 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1557 request->impl->icon_size);
1559 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1560 SHORTCUTS_COL_PIXBUF, pixbuf,
1561 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1562 SHORTCUTS_COL_NAME, request->label_copy,
1563 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1564 SHORTCUTS_COL_REMOVABLE, request->removable,
1567 if (request->impl->shortcuts_pane_filter_model)
1568 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1570 if (request->impl->shortcuts_combo_filter_model)
1571 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1573 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1574 request->impl->save_folder_combo != NULL)
1576 /* The current folder is updated via _activate_iter(), don't
1577 * have save_folder_combo_changed_cb() call _activate_iter()
1580 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1581 G_CALLBACK (save_folder_combo_changed_cb),
1584 if (request->impl->has_search)
1587 if (request->impl->has_recent)
1590 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1591 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1592 G_CALLBACK (save_folder_combo_changed_cb),
1597 g_object_unref (pixbuf);
1600 g_object_unref (request->impl);
1601 g_object_unref (request->file);
1602 gtk_tree_row_reference_free (request->row_ref);
1603 g_free (request->label_copy);
1606 g_object_unref (cancellable);
1609 /* FIXME: GtkFileSystem needs a function to split a remote path
1610 * into hostname and path components, or maybe just have a
1611 * gtk_file_system_path_get_display_name().
1613 * This function is also used in gtkfilechooserbutton.c
1616 _gtk_file_chooser_label_for_file (GFile *file)
1618 const gchar *path, *start, *end, *p;
1619 gchar *uri, *host, *label;
1621 uri = g_file_get_uri (file);
1623 start = strstr (uri, "://");
1625 path = strchr (start, '/');
1631 end = uri + strlen (uri);
1635 /* strip username */
1636 p = strchr (start, '@');
1642 p = strchr (start, ':');
1646 host = g_strndup (start, end - start);
1648 /* Translators: the first string is a path and the second string
1649 * is a hostname. Nautilus and the panel contain the same string
1652 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1660 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1661 * inserts a volume. A position of -1 indicates the end of the tree.
1664 shortcuts_insert_file (GtkFileChooserDefault *impl,
1666 ShortcutType shortcut_type,
1667 GtkFileSystemVolume *volume,
1671 ShortcutsIndex type)
1674 GdkPixbuf *pixbuf = NULL;
1675 gpointer data = NULL;
1677 GtkIconTheme *icon_theme;
1679 profile_start ("start shortcut", NULL);
1681 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1684 label_copy = _gtk_file_system_volume_get_display_name (volume);
1685 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1686 impl->icon_size, NULL);
1688 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1690 if (g_file_is_native (file))
1692 struct ShortcutsInsertRequest *request;
1693 GCancellable *cancellable;
1696 request = g_new0 (struct ShortcutsInsertRequest, 1);
1697 request->impl = g_object_ref (impl);
1698 request->file = g_object_ref (file);
1699 request->name_only = TRUE;
1700 request->removable = removable;
1702 request->type = type;
1704 request->label_copy = g_strdup (label);
1707 gtk_list_store_append (impl->shortcuts_model, &iter);
1709 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1711 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1712 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1713 gtk_tree_path_free (p);
1715 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1716 "standard::is-hidden,standard::display-name,standard::icon",
1717 get_file_info_finished, request);
1719 gtk_list_store_set (impl->shortcuts_model, &iter,
1720 SHORTCUTS_COL_DATA, g_object_ref (file),
1721 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1722 SHORTCUTS_COL_CANCELLABLE, cancellable,
1725 shortcuts_update_count (impl, type, 1);
1731 /* Don't call get_info for remote paths to avoid latency and
1734 data = g_object_ref (file);
1736 label_copy = g_strdup (label);
1738 label_copy = _gtk_file_chooser_label_for_file (file);
1740 /* If we switch to a better bookmarks file format (XBEL), we
1741 * should use mime info to get a better icon.
1743 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1744 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1745 impl->icon_size, 0, NULL);
1750 g_assert_not_reached ();
1756 gtk_list_store_append (impl->shortcuts_model, &iter);
1758 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1760 shortcuts_update_count (impl, type, 1);
1762 gtk_list_store_set (impl->shortcuts_model, &iter,
1763 SHORTCUTS_COL_PIXBUF, pixbuf,
1764 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1765 SHORTCUTS_COL_NAME, label_copy,
1766 SHORTCUTS_COL_DATA, data,
1767 SHORTCUTS_COL_TYPE, shortcut_type,
1768 SHORTCUTS_COL_REMOVABLE, removable,
1769 SHORTCUTS_COL_CANCELLABLE, NULL,
1772 if (impl->shortcuts_pane_filter_model)
1773 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1775 if (impl->shortcuts_combo_filter_model)
1776 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1778 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1780 /* The current folder is updated via _activate_iter(), don't
1781 * have save_folder_combo_changed_cb() call _activate_iter()
1784 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1786 if (impl->has_search)
1789 if (impl->has_recent)
1792 g_signal_handlers_block_by_func (impl->save_folder_combo,
1793 G_CALLBACK (save_folder_combo_changed_cb),
1796 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1797 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1798 G_CALLBACK (save_folder_combo_changed_cb),
1802 g_free (label_copy);
1805 g_object_unref (pixbuf);
1807 profile_end ("end", NULL);
1811 shortcuts_append_search (GtkFileChooserDefault *impl)
1816 pixbuf = render_search_icon (impl);
1818 gtk_list_store_append (impl->shortcuts_model, &iter);
1819 gtk_list_store_set (impl->shortcuts_model, &iter,
1820 SHORTCUTS_COL_PIXBUF, pixbuf,
1821 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1822 SHORTCUTS_COL_NAME, _("Search"),
1823 SHORTCUTS_COL_DATA, NULL,
1824 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1825 SHORTCUTS_COL_REMOVABLE, FALSE,
1829 g_object_unref (pixbuf);
1831 impl->has_search = TRUE;
1835 shortcuts_append_recent (GtkFileChooserDefault *impl)
1840 pixbuf = render_recent_icon (impl);
1842 gtk_list_store_append (impl->shortcuts_model, &iter);
1843 gtk_list_store_set (impl->shortcuts_model, &iter,
1844 SHORTCUTS_COL_PIXBUF, pixbuf,
1845 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1846 SHORTCUTS_COL_NAME, _("Recently Used"),
1847 SHORTCUTS_COL_DATA, NULL,
1848 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1849 SHORTCUTS_COL_REMOVABLE, FALSE,
1853 g_object_unref (pixbuf);
1855 impl->has_recent = TRUE;
1858 /* Appends an item for the user's home directory to the shortcuts model */
1860 shortcuts_append_home (GtkFileChooserDefault *impl)
1862 const char *home_path;
1865 profile_start ("start", NULL);
1867 home_path = g_get_home_dir ();
1868 if (home_path == NULL)
1870 profile_end ("end - no home directory!?", NULL);
1874 home = g_file_new_for_path (home_path);
1875 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1876 impl->has_home = TRUE;
1878 g_object_unref (home);
1880 profile_end ("end", NULL);
1883 /* Appends the ~/Desktop directory to the shortcuts model */
1885 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1890 profile_start ("start", NULL);
1892 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1893 file = g_file_new_for_path (name);
1894 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1895 impl->has_desktop = TRUE;
1897 /* We do not actually pop up an error dialog if there is no desktop directory
1898 * because some people may really not want to have one.
1901 g_object_unref (file);
1903 profile_end ("end", NULL);
1906 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1908 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1915 profile_start ("start", NULL);
1917 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1920 for (; bookmarks; bookmarks = bookmarks->next)
1924 file = bookmarks->data;
1926 if (impl->local_only && !g_file_is_native (file))
1929 if (shortcut_find_position (impl, file) != -1)
1932 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1934 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1938 profile_end ("end", NULL);
1940 return num_inserted;
1943 /* Returns the index for the corresponding item in the shortcuts bar */
1945 shortcuts_get_index (GtkFileChooserDefault *impl,
1946 ShortcutsIndex where)
1952 if (where == SHORTCUTS_SEARCH)
1955 n += impl->has_search ? 1 : 0;
1957 if (where == SHORTCUTS_RECENT)
1960 n += impl->has_recent ? 1 : 0;
1962 if (where == SHORTCUTS_RECENT_SEPARATOR)
1965 n += impl->has_recent ? 1 : 0;
1967 if (where == SHORTCUTS_HOME)
1970 n += impl->has_home ? 1 : 0;
1972 if (where == SHORTCUTS_DESKTOP)
1975 n += impl->has_desktop ? 1 : 0;
1977 if (where == SHORTCUTS_VOLUMES)
1980 n += impl->num_volumes;
1982 if (where == SHORTCUTS_SHORTCUTS)
1985 n += impl->num_shortcuts;
1987 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1990 /* If there are no bookmarks there won't be a separator */
1991 n += (impl->num_bookmarks > 0) ? 1 : 0;
1993 if (where == SHORTCUTS_BOOKMARKS)
1996 n += impl->num_bookmarks;
1998 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2003 if (where == SHORTCUTS_CURRENT_FOLDER)
2006 g_assert_not_reached ();
2013 /* Adds all the file system volumes to the shortcuts model */
2015 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2020 gboolean old_changing_folders;
2022 profile_start ("start", NULL);
2024 old_changing_folders = impl->changing_folder;
2025 impl->changing_folder = TRUE;
2027 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2028 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2029 impl->num_volumes = 0;
2031 list = _gtk_file_system_list_volumes (impl->file_system);
2035 for (l = list; l; l = l->next)
2037 GtkFileSystemVolume *volume;
2041 if (impl->local_only)
2043 if (_gtk_file_system_volume_is_mounted (volume))
2047 base_file = _gtk_file_system_volume_get_root (volume);
2048 if (base_file != NULL && !g_file_is_native (base_file))
2053 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2057 impl->num_volumes = n;
2058 g_slist_free (list);
2060 if (impl->shortcuts_pane_filter_model)
2061 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2063 if (impl->shortcuts_combo_filter_model)
2064 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2066 impl->changing_folder = old_changing_folders;
2068 profile_end ("end", NULL);
2071 /* Inserts a separator node in the shortcuts list */
2073 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2074 ShortcutsIndex where)
2078 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2079 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2080 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2082 gtk_list_store_insert (impl->shortcuts_model, &iter,
2083 shortcuts_get_index (impl, where));
2084 gtk_list_store_set (impl->shortcuts_model, &iter,
2085 SHORTCUTS_COL_PIXBUF, NULL,
2086 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2087 SHORTCUTS_COL_NAME, NULL,
2088 SHORTCUTS_COL_DATA, NULL,
2089 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2093 /* Updates the list of bookmarks */
2095 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2098 gboolean old_changing_folders;
2100 GFile *list_selected = NULL;
2101 GFile *combo_selected = NULL;
2102 ShortcutType shortcut_type;
2105 profile_start ("start", NULL);
2107 old_changing_folders = impl->changing_folder;
2108 impl->changing_folder = TRUE;
2110 if (shortcuts_get_selected (impl, &iter))
2112 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2114 SHORTCUTS_COL_DATA, &col_data,
2115 SHORTCUTS_COL_TYPE, &shortcut_type,
2118 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2119 list_selected = g_object_ref (col_data);
2122 if (impl->save_folder_combo &&
2123 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2126 GtkTreeIter child_iter;
2128 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2131 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2133 SHORTCUTS_COL_DATA, &col_data,
2134 SHORTCUTS_COL_TYPE, &shortcut_type,
2137 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2138 combo_selected = g_object_ref (col_data);
2141 if (impl->num_bookmarks > 0)
2142 shortcuts_remove_rows (impl,
2143 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2144 impl->num_bookmarks + 1);
2146 impl->num_bookmarks = 0;
2147 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2149 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2150 shortcuts_append_bookmarks (impl, bookmarks);
2151 g_slist_free (bookmarks);
2153 if (impl->num_bookmarks == 0)
2154 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2156 if (impl->shortcuts_pane_filter_model)
2157 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2159 if (impl->shortcuts_combo_filter_model)
2160 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2164 shortcuts_find_folder (impl, list_selected);
2165 g_object_unref (list_selected);
2172 pos = shortcut_find_position (impl, combo_selected);
2175 if (impl->has_search)
2178 if (impl->has_recent)
2181 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2184 g_object_unref (combo_selected);
2187 impl->changing_folder = old_changing_folders;
2189 profile_end ("end", NULL);
2192 /* Appends a separator and a row to the shortcuts list for the current folder */
2194 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2199 g_assert (!impl->shortcuts_current_folder_active);
2203 g_assert (impl->current_folder != NULL);
2205 pos = shortcut_find_position (impl, impl->current_folder);
2208 GtkFileSystemVolume *volume;
2213 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2217 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2219 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2221 base_file = _gtk_file_system_volume_get_root (volume);
2225 if (base_file && g_file_equal (base_file, impl->current_folder))
2226 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2228 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2231 g_object_unref (base_file);
2233 else if (impl->save_folder_combo != NULL)
2235 if (impl->has_search)
2238 if (impl->has_recent)
2239 pos -= 2; /* + separator */
2241 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2245 /* Updates the current folder row in the shortcuts model */
2247 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2251 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2253 if (impl->shortcuts_current_folder_active)
2255 shortcuts_remove_rows (impl, pos, 2);
2256 impl->shortcuts_current_folder_active = FALSE;
2259 shortcuts_add_current_folder (impl);
2262 /* Filter function used for the shortcuts filter model */
2264 shortcuts_pane_filter_cb (GtkTreeModel *model,
2268 GtkFileChooserDefault *impl;
2272 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2274 path = gtk_tree_model_get_path (model, iter);
2278 pos = *gtk_tree_path_get_indices (path);
2279 gtk_tree_path_free (path);
2281 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2284 /* Creates the list model for shortcuts */
2286 shortcuts_model_create (GtkFileChooserDefault *impl)
2288 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2289 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2290 GDK_TYPE_PIXBUF, /* pixbuf */
2291 G_TYPE_STRING, /* name */
2292 G_TYPE_POINTER, /* path or volume */
2293 G_TYPE_INT, /* ShortcutType */
2294 G_TYPE_BOOLEAN, /* removable */
2295 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2296 G_TYPE_POINTER); /* GCancellable */
2298 if (search_is_possible (impl))
2300 shortcuts_append_search (impl);
2303 if (impl->recent_manager)
2305 shortcuts_append_recent (impl);
2306 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2309 if (impl->file_system)
2311 shortcuts_append_home (impl);
2312 shortcuts_append_desktop (impl);
2313 shortcuts_add_volumes (impl);
2316 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2317 GTK_TREE_MODEL (impl->shortcuts_model),
2320 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2321 shortcuts_pane_filter_cb,
2326 /* Callback used when the "New Folder" button is clicked */
2328 new_folder_button_clicked (GtkButton *button,
2329 GtkFileChooserDefault *impl)
2334 if (!impl->browse_files_model)
2335 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2337 /* Prevent button from being clicked twice */
2338 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2340 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2342 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2343 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2344 path, impl->list_name_column,
2347 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2348 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2350 impl->list_name_column,
2353 gtk_tree_path_free (path);
2356 /* Idle handler for creating a new folder after editing its name cell, or for
2357 * canceling the editing.
2360 edited_idle_cb (GtkFileChooserDefault *impl)
2362 GDK_THREADS_ENTER ();
2364 g_source_destroy (impl->edited_idle);
2365 impl->edited_idle = NULL;
2367 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2368 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2370 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2372 if (impl->edited_new_text) /* not cancelled? */
2374 GError *error = NULL;
2377 file = g_file_get_child_for_display_name (impl->current_folder,
2378 impl->edited_new_text,
2382 GError *error = NULL;
2384 if (!g_file_make_directory (file, NULL, &error))
2385 change_folder_and_display_error (impl, file, FALSE);
2387 error_creating_folder_dialog (impl, file, error);
2389 g_object_unref (file);
2392 error_creating_folder_dialog (impl, file, error);
2394 g_free (impl->edited_new_text);
2395 impl->edited_new_text = NULL;
2398 GDK_THREADS_LEAVE ();
2404 queue_edited_idle (GtkFileChooserDefault *impl,
2405 const gchar *new_text)
2407 /* We create the folder in an idle handler so that we don't modify the tree
2411 if (!impl->edited_idle)
2413 impl->edited_idle = g_idle_source_new ();
2414 g_source_set_closure (impl->edited_idle,
2415 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2417 g_source_attach (impl->edited_idle, NULL);
2420 g_free (impl->edited_new_text);
2421 impl->edited_new_text = g_strdup (new_text);
2424 /* Callback used from the text cell renderer when the new folder is named */
2426 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2428 const gchar *new_text,
2429 GtkFileChooserDefault *impl)
2431 /* work around bug #154921 */
2432 g_object_set (cell_renderer_text,
2433 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2434 queue_edited_idle (impl, new_text);
2437 /* Callback used from the text cell renderer when the new folder edition gets
2441 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2442 GtkFileChooserDefault *impl)
2444 /* work around bug #154921 */
2445 g_object_set (cell_renderer_text,
2446 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2447 queue_edited_idle (impl, NULL);
2450 /* Creates the widgets for the filter combo box */
2452 filter_create (GtkFileChooserDefault *impl)
2454 impl->filter_combo = gtk_combo_box_new_text ();
2455 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2457 g_signal_connect (impl->filter_combo, "changed",
2458 G_CALLBACK (filter_combo_changed), impl);
2460 gtk_widget_set_tooltip_text (impl->filter_combo,
2461 _("Select which types of files are shown"));
2463 return impl->filter_combo;
2467 button_new (GtkFileChooserDefault *impl,
2469 const char *stock_id,
2477 button = gtk_button_new_with_mnemonic (text);
2478 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2479 gtk_button_set_image (GTK_BUTTON (button), image);
2481 gtk_widget_set_sensitive (button, sensitive);
2482 g_signal_connect (button, "clicked", callback, impl);
2485 gtk_widget_show (button);
2490 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2492 shortcut_find_position (GtkFileChooserDefault *impl,
2497 int current_folder_separator_idx;
2499 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2502 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2505 /* FIXME: is this still needed? */
2506 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2510 for (i = 0; i < current_folder_separator_idx; i++)
2513 ShortcutType shortcut_type;
2515 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2516 SHORTCUTS_COL_DATA, &col_data,
2517 SHORTCUTS_COL_TYPE, &shortcut_type,
2522 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2524 GtkFileSystemVolume *volume;
2529 base_file = _gtk_file_system_volume_get_root (volume);
2531 exists = base_file && g_file_equal (file, base_file);
2534 g_object_unref (base_file);
2539 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2543 model_file = col_data;
2545 if (model_file && g_file_equal (model_file, file))
2550 if (i < current_folder_separator_idx - 1)
2552 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2553 g_assert_not_reached ();
2560 /* Tries to add a bookmark from a path name */
2562 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2568 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2570 if (shortcut_find_position (impl, file) != -1)
2574 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2576 error_adding_bookmark_dialog (impl, file, error);
2584 add_bookmark_foreach_cb (GtkTreeModel *model,
2589 GtkFileChooserDefault *impl;
2590 GtkFileSystemModel *fs_model;
2591 GtkTreeIter child_iter;
2594 impl = (GtkFileChooserDefault *) data;
2596 switch (impl->operation_mode)
2598 case OPERATION_MODE_BROWSE:
2599 fs_model = impl->browse_files_model;
2600 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2601 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2604 case OPERATION_MODE_SEARCH:
2605 search_get_valid_child_iter (impl, &child_iter, iter);
2606 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2607 SEARCH_MODEL_COL_FILE, &file,
2611 case OPERATION_MODE_RECENT:
2612 recent_get_valid_child_iter (impl, &child_iter, iter);
2613 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2614 RECENT_MODEL_COL_FILE, &file,
2619 shortcuts_add_bookmark_from_file (impl, file, -1);
2622 /* Adds a bookmark from the currently selected item in the file list */
2624 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2626 GtkTreeSelection *selection;
2628 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2630 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2631 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2633 gtk_tree_selection_selected_foreach (selection,
2634 add_bookmark_foreach_cb,
2638 /* Callback used when the "Add bookmark" button is clicked */
2640 add_bookmark_button_clicked_cb (GtkButton *button,
2641 GtkFileChooserDefault *impl)
2643 bookmarks_add_selected_folder (impl);
2646 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2647 * returns FALSE if no shortcut is selected.
2650 shortcuts_get_selected (GtkFileChooserDefault *impl,
2653 GtkTreeSelection *selection;
2654 GtkTreeIter parent_iter;
2656 if (!impl->browse_shortcuts_tree_view)
2659 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2661 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2664 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2670 /* Removes the selected bookmarks */
2672 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2680 if (!shortcuts_get_selected (impl, &iter))
2683 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2684 SHORTCUTS_COL_DATA, &col_data,
2685 SHORTCUTS_COL_REMOVABLE, &removable,
2691 g_assert (col_data != NULL);
2696 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2697 error_removing_bookmark_dialog (impl, file, error);
2700 /* Callback used when the "Remove bookmark" button is clicked */
2702 remove_bookmark_button_clicked_cb (GtkButton *button,
2703 GtkFileChooserDefault *impl)
2705 remove_selected_bookmarks (impl);
2708 struct selection_check_closure {
2709 GtkFileChooserDefault *impl;
2712 gboolean all_folders;
2715 /* Used from gtk_tree_selection_selected_foreach() */
2717 selection_check_foreach_cb (GtkTreeModel *model,
2722 struct selection_check_closure *closure;
2723 GtkTreeIter child_iter;
2728 closure->num_selected++;
2730 switch (closure->impl->operation_mode)
2732 case OPERATION_MODE_BROWSE:
2733 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2734 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2735 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2738 case OPERATION_MODE_SEARCH:
2739 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2740 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2741 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2745 case OPERATION_MODE_RECENT:
2746 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2747 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2748 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2753 closure->all_folders = closure->all_folders && is_folder;
2754 closure->all_files = closure->all_files && !is_folder;
2757 /* Checks whether the selected items in the file list are all files or all folders */
2759 selection_check (GtkFileChooserDefault *impl,
2761 gboolean *all_files,
2762 gboolean *all_folders)
2764 struct selection_check_closure closure;
2765 GtkTreeSelection *selection;
2767 closure.impl = impl;
2768 closure.num_selected = 0;
2769 closure.all_files = TRUE;
2770 closure.all_folders = TRUE;
2772 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2773 gtk_tree_selection_selected_foreach (selection,
2774 selection_check_foreach_cb,
2777 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2780 *num_selected = closure.num_selected;
2783 *all_files = closure.all_files;
2786 *all_folders = closure.all_folders;
2789 struct get_selected_file_closure {
2790 GtkFileChooserDefault *impl;
2795 get_selected_file_foreach_cb (GtkTreeModel *model,
2800 struct get_selected_file_closure *closure;
2801 GtkTreeIter child_iter;
2805 switch (closure->impl->operation_mode)
2807 case OPERATION_MODE_BROWSE:
2808 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2809 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2812 case OPERATION_MODE_SEARCH:
2813 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2814 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2815 SEARCH_MODEL_COL_FILE, &closure->file,
2819 case OPERATION_MODE_RECENT:
2820 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2821 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2822 RECENT_MODEL_COL_FILE, &closure->file,
2828 /* Returns a selected path from the file list */
2830 get_selected_file (GtkFileChooserDefault *impl)
2832 struct get_selected_file_closure closure;
2833 GtkTreeSelection *selection;
2835 closure.impl = impl;
2836 closure.file = NULL;
2838 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2839 gtk_tree_selection_selected_foreach (selection,
2840 get_selected_file_foreach_cb,
2843 return closure.file;
2847 GtkFileChooserDefault *impl;
2849 } UpdateTooltipData;
2852 update_tooltip (GtkTreeModel *model,
2857 UpdateTooltipData *udata = data;
2858 GtkTreeIter child_iter;
2861 if (udata->tip == NULL)
2863 const gchar *display_name;
2865 switch (udata->impl->operation_mode)
2867 case OPERATION_MODE_BROWSE:
2868 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2871 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2872 display_name = g_file_info_get_display_name (info);
2875 case OPERATION_MODE_SEARCH:
2876 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2877 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2878 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2882 case OPERATION_MODE_RECENT:
2883 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2884 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2885 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2890 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2896 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2897 * if there are no selected items *and* the current folder is not in the
2898 * bookmarks list. De-sensitize the button otherwise.
2901 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2904 gboolean all_folders;
2908 selection_check (impl, &num_selected, NULL, &all_folders);
2910 if (num_selected == 0)
2911 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2912 else if (num_selected == 1)
2916 file = get_selected_file (impl);
2917 active = all_folders && (shortcut_find_position (impl, file) == -1);
2920 active = all_folders;
2922 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2924 if (impl->browse_files_popup_menu_add_shortcut_item)
2925 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2926 (num_selected == 0) ? FALSE : active);
2930 if (num_selected == 0)
2931 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2932 else if (num_selected > 1)
2933 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2936 GtkTreeSelection *selection;
2937 UpdateTooltipData data;
2939 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2942 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2946 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2951 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2952 * bookmark row is selected in the shortcuts tree.
2955 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2958 gboolean removable = FALSE;
2961 if (shortcuts_get_selected (impl, &iter))
2962 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2963 SHORTCUTS_COL_REMOVABLE, &removable,
2964 SHORTCUTS_COL_NAME, &name,
2967 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2973 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2974 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2982 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2985 gboolean removable = FALSE;
2987 if (impl->browse_shortcuts_popup_menu == NULL)
2990 if (shortcuts_get_selected (impl, &iter))
2991 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2992 SHORTCUTS_COL_REMOVABLE, &removable,
2995 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2996 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2999 /* GtkWidget::drag-begin handler for the shortcuts list. */
3001 shortcuts_drag_begin_cb (GtkWidget *widget,
3002 GdkDragContext *context,
3003 GtkFileChooserDefault *impl)
3006 impl->shortcuts_drag_context = g_object_ref (context);
3011 /* Removes the idle handler for outside drags */
3013 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3015 if (!impl->shortcuts_drag_outside_idle)
3018 g_source_destroy (impl->shortcuts_drag_outside_idle);
3019 impl->shortcuts_drag_outside_idle = NULL;
3023 /* GtkWidget::drag-end handler for the shortcuts list. */
3025 shortcuts_drag_end_cb (GtkWidget *widget,
3026 GdkDragContext *context,
3027 GtkFileChooserDefault *impl)
3030 g_object_unref (impl->shortcuts_drag_context);
3032 shortcuts_cancel_drag_outside_idle (impl);
3034 if (!impl->shortcuts_drag_outside)
3037 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3039 impl->shortcuts_drag_outside = FALSE;
3043 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3045 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3046 GdkDragContext *context,
3047 GtkFileChooserDefault *impl)
3049 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3053 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3057 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3060 GtkTreeView *tree_view;
3063 GdkPixmap *row_pixmap;
3068 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3070 /* Find the selected path and get its drag pixmap */
3072 if (!shortcuts_get_selected (impl, &iter))
3073 g_assert_not_reached ();
3075 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3077 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3078 gtk_tree_path_free (path);
3087 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3093 GdkPixmap *composite;
3094 int row_pixmap_width, row_pixmap_height;
3095 int pixbuf_width, pixbuf_height;
3096 int composite_width, composite_height;
3097 int pixbuf_x, pixbuf_y;
3098 GdkGC *gc, *mask_gc;
3100 GdkBitmap *pixbuf_mask;
3102 /* Create pixmap and mask for composite image */
3104 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3105 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3106 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3108 composite_width = MAX (row_pixmap_width, pixbuf_width);
3109 composite_height = MAX (row_pixmap_height, pixbuf_height);
3111 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3113 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3116 pixbuf_x = composite_width - pixbuf_width;
3118 pixbuf_y = (composite_height - pixbuf_height) / 2;
3120 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3121 gc = gdk_gc_new (composite);
3123 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3124 mask_gc = gdk_gc_new (mask);
3126 gdk_gc_set_foreground (mask_gc, &color);
3127 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3130 color.green = 0xffff;
3131 color.blue = 0xffff;
3132 gdk_gc_set_rgb_fg_color (gc, &color);
3133 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3135 /* Composite the row pixmap and the pixbuf */
3137 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3139 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3140 NULL, &pixbuf_mask, 128);
3141 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3144 pixbuf_width, pixbuf_height);
3145 g_object_unref (pixbuf_mask);
3147 gdk_draw_drawable (composite, gc, row_pixmap,
3150 row_pixmap_width, row_pixmap_height);
3152 gdk_gc_set_foreground (mask_gc, &color);
3153 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3155 gdk_draw_pixbuf (composite, gc, pixbuf,
3158 pixbuf_width, pixbuf_height,
3162 g_object_unref (pixbuf);
3163 g_object_unref (row_pixmap);
3165 row_pixmap = composite;
3169 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3171 gtk_tree_view_get_path_at_pos (tree_view,
3172 tree_view->priv->press_start_x,
3173 tree_view->priv->press_start_y,
3179 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3180 gdk_drawable_get_colormap (row_pixmap),
3183 tree_view->priv->press_start_x + 1,
3184 row_pixmap_y + cell_y + 1);
3186 g_object_unref (row_pixmap);
3188 g_object_unref (mask);
3191 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3192 * handler so that we can tell apart the drag_leave event that comes right
3193 * before a drag_drop, from a normal drag_leave. We don't want to set the
3194 * cursor nor the flag in the latter case.
3197 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3199 GDK_THREADS_ENTER ();
3201 shortcuts_drag_set_delete_cursor (impl, TRUE);
3202 impl->shortcuts_drag_outside = TRUE;
3204 shortcuts_cancel_drag_outside_idle (impl);
3206 GDK_THREADS_LEAVE ();
3212 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3216 shortcuts_drag_leave_cb (GtkWidget *widget,
3217 GdkDragContext *context,
3219 GtkFileChooserDefault *impl)
3222 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3224 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3225 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3226 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3228 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3232 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3234 GTK_TREE_VIEW_DROP_BEFORE);
3236 g_signal_stop_emission_by_name (widget, "drag_leave");
3239 /* Computes the appropriate row and position for dropping */
3241 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3245 GtkTreeViewDropPosition *pos)
3247 GtkTreeView *tree_view;
3248 GtkTreeViewColumn *column;
3252 int bookmarks_index;
3254 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3256 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3258 if (!gtk_tree_view_get_path_at_pos (tree_view,
3260 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3266 row = bookmarks_index + impl->num_bookmarks - 1;
3267 *path = gtk_tree_path_new_from_indices (row, -1);
3268 *pos = GTK_TREE_VIEW_DROP_AFTER;
3272 row = *gtk_tree_path_get_indices (*path);
3273 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3274 gtk_tree_path_free (*path);
3276 if (row < bookmarks_index)
3278 row = bookmarks_index;
3279 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3281 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3283 row = bookmarks_index + impl->num_bookmarks - 1;
3284 *pos = GTK_TREE_VIEW_DROP_AFTER;
3288 if (cell_y < cell.height / 2)
3289 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3291 *pos = GTK_TREE_VIEW_DROP_AFTER;
3294 *path = gtk_tree_path_new_from_indices (row, -1);
3297 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3298 * implement the destination side of DnD by hand, due to limitations in
3299 * GtkTreeView's DnD API.
3302 shortcuts_drag_motion_cb (GtkWidget *widget,
3303 GdkDragContext *context,
3307 GtkFileChooserDefault *impl)
3310 GtkTreeViewDropPosition pos;
3311 GdkDragAction action;
3314 if (gtk_drag_get_source_widget (context) == widget)
3316 shortcuts_cancel_drag_outside_idle (impl);
3318 if (impl->shortcuts_drag_outside)
3320 shortcuts_drag_set_delete_cursor (impl, FALSE);
3321 impl->shortcuts_drag_outside = FALSE;
3326 if (context->suggested_action == GDK_ACTION_COPY ||
3327 (context->actions & GDK_ACTION_COPY) != 0)
3328 action = GDK_ACTION_COPY;
3329 else if (context->suggested_action == GDK_ACTION_MOVE ||
3330 (context->actions & GDK_ACTION_MOVE) != 0)
3331 action = GDK_ACTION_MOVE;
3338 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3339 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3340 gtk_tree_path_free (path);
3344 g_signal_stop_emission_by_name (widget, "drag_motion");
3348 gdk_drag_status (context, action, time_);
3355 /* GtkWidget::drag-drop handler for the shortcuts list. */
3357 shortcuts_drag_drop_cb (GtkWidget *widget,
3358 GdkDragContext *context,
3362 GtkFileChooserDefault *impl)
3365 shortcuts_cancel_drag_outside_idle (impl);
3368 g_signal_stop_emission_by_name (widget, "drag_drop");
3372 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3374 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3375 GtkSelectionData *selection_data,
3381 uris = gtk_selection_data_get_uris (selection_data);
3385 for (i = 0; uris[i]; i++)
3391 file = g_file_new_for_uri (uri);
3393 if (shortcuts_add_bookmark_from_file (impl, file, position))
3396 g_object_unref (file);
3402 /* Reorders the selected bookmark to the specified position */
3404 shortcuts_reorder (GtkFileChooserDefault *impl,
3409 ShortcutType shortcut_type;
3412 int bookmarks_index;
3417 /* Get the selected path */
3419 if (!shortcuts_get_selected (impl, &iter))
3420 g_assert_not_reached ();
3422 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3423 old_position = *gtk_tree_path_get_indices (path);
3424 gtk_tree_path_free (path);
3426 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3427 old_position -= bookmarks_index;
3428 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3430 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3431 SHORTCUTS_COL_NAME, &name,
3432 SHORTCUTS_COL_DATA, &col_data,
3433 SHORTCUTS_COL_TYPE, &shortcut_type,
3435 g_assert (col_data != NULL);
3436 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3439 g_object_ref (file); /* removal below will free file, so we need a new ref */
3441 /* Remove the path from the old position and insert it in the new one */
3443 if (new_position > old_position)
3446 if (old_position == new_position)
3450 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3452 shortcuts_add_bookmark_from_file (impl, file, new_position);
3453 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3456 error_adding_bookmark_dialog (impl, file, error);
3460 g_object_unref (file);
3463 /* Callback used when we get the drag data for the bookmarks list. We add the
3464 * received URIs as bookmarks if they are folders.
3467 shortcuts_drag_data_received_cb (GtkWidget *widget,
3468 GdkDragContext *context,
3471 GtkSelectionData *selection_data,
3476 GtkFileChooserDefault *impl;
3477 GtkTreePath *tree_path;
3478 GtkTreeViewDropPosition tree_pos;
3480 int bookmarks_index;
3482 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3484 /* Compute position */
3486 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3488 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3489 position = *gtk_tree_path_get_indices (tree_path);
3490 gtk_tree_path_free (tree_path);
3492 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3495 g_assert (position >= bookmarks_index);
3496 position -= bookmarks_index;
3498 if (gtk_targets_include_uri (&selection_data->target, 1))
3499 shortcuts_drop_uris (impl, selection_data, position);
3500 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3501 shortcuts_reorder (impl, position);
3503 g_signal_stop_emission_by_name (widget, "drag_data_received");
3506 /* Callback used to display a tooltip in the shortcuts tree */
3508 shortcuts_query_tooltip_cb (GtkWidget *widget,
3511 gboolean keyboard_mode,
3512 GtkTooltip *tooltip,
3513 GtkFileChooserDefault *impl)
3515 GtkTreeModel *model;
3518 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3526 ShortcutType shortcut_type;
3528 gtk_tree_model_get (model, &iter,
3529 SHORTCUTS_COL_DATA, &col_data,
3530 SHORTCUTS_COL_TYPE, &shortcut_type,
3533 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3535 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3539 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3544 file = G_FILE (col_data);
3545 parse_name = g_file_get_parse_name (file);
3547 gtk_tooltip_set_text (tooltip, parse_name);
3549 g_free (parse_name);
3553 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3557 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3567 /* Callback used when the selection in the shortcuts tree changes */
3569 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3570 GtkFileChooserDefault *impl)
3573 GtkTreeIter child_iter;
3575 bookmarks_check_remove_sensitivity (impl);
3576 shortcuts_check_popup_sensitivity (impl);
3578 if (impl->changing_folder)
3581 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3583 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3586 shortcuts_activate_iter (impl, &child_iter);
3591 shortcuts_row_separator_func (GtkTreeModel *model,
3595 ShortcutType shortcut_type;
3597 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3599 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3602 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3603 * keypresses before the TreeView gets them.
3606 tree_view_keybinding_cb (GtkWidget *tree_view,
3608 GtkFileChooserDefault *impl)
3610 if ((event->keyval == GDK_slash
3611 || event->keyval == GDK_KP_Divide
3613 || event->keyval == GDK_asciitilde
3615 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3617 location_popup_handler (impl, event->string);
3624 /* Callback used when the file list's popup menu is detached */
3626 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3629 GtkFileChooserDefault *impl;
3631 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3632 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3634 impl->browse_shortcuts_popup_menu = NULL;
3635 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3636 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3640 remove_shortcut_cb (GtkMenuItem *item,
3641 GtkFileChooserDefault *impl)
3643 remove_selected_bookmarks (impl);
3646 /* Rename the selected bookmark */
3648 rename_selected_bookmark (GtkFileChooserDefault *impl)
3652 GtkTreeViewColumn *column;
3653 GtkCellRenderer *cell;
3656 if (shortcuts_get_selected (impl, &iter))
3658 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3659 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3660 renderers = gtk_tree_view_column_get_cell_renderers (column);
3661 cell = g_list_nth_data (renderers, 1);
3662 g_list_free (renderers);
3663 g_object_set (cell, "editable", TRUE, NULL);
3664 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3665 path, column, cell, TRUE);
3666 gtk_tree_path_free (path);
3671 rename_shortcut_cb (GtkMenuItem *item,
3672 GtkFileChooserDefault *impl)
3674 rename_selected_bookmark (impl);
3677 /* Constructs the popup menu for the file list if needed */
3679 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3683 if (impl->browse_shortcuts_popup_menu)
3686 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3687 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3688 impl->browse_shortcuts_tree_view,
3689 shortcuts_popup_menu_detach_cb);
3691 item = gtk_image_menu_item_new_with_label (_("Remove"));
3692 impl->browse_shortcuts_popup_menu_remove_item = item;
3693 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3694 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3695 g_signal_connect (item, "activate",
3696 G_CALLBACK (remove_shortcut_cb), impl);
3697 gtk_widget_show (item);
3698 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3700 item = gtk_menu_item_new_with_label (_("Rename..."));
3701 impl->browse_shortcuts_popup_menu_rename_item = item;
3702 g_signal_connect (item, "activate",
3703 G_CALLBACK (rename_shortcut_cb), impl);
3704 gtk_widget_show (item);
3705 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3707 shortcuts_check_popup_sensitivity (impl);
3711 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3713 shortcuts_build_popup_menu (impl);
3717 popup_position_func (GtkMenu *menu,
3721 gpointer user_data);
3724 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3725 GdkEventButton *event)
3727 shortcuts_update_popup_menu (impl);
3729 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3730 NULL, NULL, NULL, NULL,
3731 event->button, event->time);
3734 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3736 popup_position_func, impl->browse_shortcuts_tree_view,
3737 0, GDK_CURRENT_TIME);
3738 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3743 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3745 shortcuts_popup_menu_cb (GtkWidget *widget,
3746 GtkFileChooserDefault *impl)
3748 shortcuts_popup_menu (impl, NULL);
3752 /* Callback used when a button is pressed on the shortcuts list.
3753 * We trap button 3 to bring up a popup menu.
3756 shortcuts_button_press_event_cb (GtkWidget *widget,
3757 GdkEventButton *event,
3758 GtkFileChooserDefault *impl)
3760 static gboolean in_press = FALSE;
3766 if (event->button != 3)
3770 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3776 shortcuts_popup_menu (impl, event);
3781 shortcuts_edited (GtkCellRenderer *cell,
3784 GtkFileChooserDefault *impl)
3790 g_object_set (cell, "editable", FALSE, NULL);
3792 path = gtk_tree_path_new_from_string (path_string);
3793 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3794 g_assert_not_reached ();
3796 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3797 SHORTCUTS_COL_DATA, &shortcut,
3799 gtk_tree_path_free (path);
3801 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3805 shortcuts_editing_canceled (GtkCellRenderer *cell,
3806 GtkFileChooserDefault *impl)
3808 g_object_set (cell, "editable", FALSE, NULL);
3811 /* Creates the widgets for the shortcuts and bookmarks tree */
3813 shortcuts_list_create (GtkFileChooserDefault *impl)
3816 GtkTreeSelection *selection;
3817 GtkTreeViewColumn *column;
3818 GtkCellRenderer *renderer;
3820 /* Target types for dragging a row to/from the shortcuts list */
3821 const GtkTargetEntry tree_model_row_targets[] = {
3822 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3825 /* Scrolled window */
3827 swin = gtk_scrolled_window_new (NULL, NULL);
3828 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3829 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3830 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3832 gtk_widget_show (swin);
3836 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3837 #ifdef PROFILE_FILE_CHOOSER
3838 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3840 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3841 G_CALLBACK (tree_view_keybinding_cb), impl);
3842 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3843 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3844 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3845 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3846 /* Accessible object name for the file chooser's shortcuts pane */
3847 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3849 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3851 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3853 tree_model_row_targets,
3854 G_N_ELEMENTS (tree_model_row_targets),
3857 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3858 GTK_DEST_DEFAULT_ALL,
3859 tree_model_row_targets,
3860 G_N_ELEMENTS (tree_model_row_targets),
3861 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3862 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3864 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3865 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3866 gtk_tree_selection_set_select_function (selection,
3867 shortcuts_select_func,
3870 g_signal_connect (selection, "changed",
3871 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3873 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3874 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3876 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3877 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3878 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3879 G_CALLBACK (shortcuts_drag_end_cb), impl);
3880 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3881 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3883 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3884 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3885 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3886 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3887 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3888 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3889 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3890 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3892 /* Support tooltips */
3893 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3894 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3895 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3897 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3898 gtk_widget_show (impl->browse_shortcuts_tree_view);
3902 column = gtk_tree_view_column_new ();
3903 /* Column header for the file chooser's shortcuts pane */
3904 gtk_tree_view_column_set_title (column, _("_Places"));
3906 renderer = gtk_cell_renderer_pixbuf_new ();
3907 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3908 gtk_tree_view_column_set_attributes (column, renderer,
3909 "pixbuf", SHORTCUTS_COL_PIXBUF,
3910 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3913 renderer = gtk_cell_renderer_text_new ();
3914 g_signal_connect (renderer, "edited",
3915 G_CALLBACK (shortcuts_edited), impl);
3916 g_signal_connect (renderer, "editing-canceled",
3917 G_CALLBACK (shortcuts_editing_canceled), impl);
3918 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3919 gtk_tree_view_column_set_attributes (column, renderer,
3920 "text", SHORTCUTS_COL_NAME,
3923 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3924 shortcuts_row_separator_func,
3927 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3932 /* Creates the widgets for the shortcuts/bookmarks pane */
3934 shortcuts_pane_create (GtkFileChooserDefault *impl,
3935 GtkSizeGroup *size_group)
3941 vbox = gtk_vbox_new (FALSE, 6);
3942 gtk_widget_show (vbox);
3944 /* Shortcuts tree */
3946 widget = shortcuts_list_create (impl);
3947 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3949 /* Box for buttons */
3951 hbox = gtk_hbox_new (TRUE, 6);
3952 gtk_size_group_add_widget (size_group, hbox);
3953 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3954 gtk_widget_show (hbox);
3956 /* Add bookmark button */
3958 impl->browse_shortcuts_add_button = button_new (impl,
3963 G_CALLBACK (add_bookmark_button_clicked_cb));
3964 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3965 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3966 _("Add the selected folder to the Bookmarks"));
3968 /* Remove bookmark button */
3970 impl->browse_shortcuts_remove_button = button_new (impl,
3975 G_CALLBACK (remove_bookmark_button_clicked_cb));
3976 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3977 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3978 _("Remove the selected bookmark"));
3983 /* Handles key press events on the file list, so that we can trap Enter to
3984 * activate the default button on our own. Also, checks to see if '/' has been
3985 * pressed. See comment by tree_view_keybinding_cb() for more details.
3988 trap_activate_cb (GtkWidget *widget,
3992 GtkFileChooserDefault *impl;
3995 impl = (GtkFileChooserDefault *) data;
3997 modifiers = gtk_accelerator_get_default_mod_mask ();
3999 if ((event->keyval == GDK_slash
4000 || event->keyval == GDK_KP_Divide
4002 || event->keyval == GDK_asciitilde
4004 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4006 location_popup_handler (impl, event->string);
4010 if ((event->keyval == GDK_Return
4011 || event->keyval == GDK_ISO_Enter
4012 || event->keyval == GDK_KP_Enter
4013 || event->keyval == GDK_space
4014 || event->keyval == GDK_KP_Space)
4015 && ((event->state & modifiers) == 0)
4016 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4017 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4021 window = get_toplevel (widget);
4023 && widget != window->default_widget
4024 && !(widget == window->focus_widget &&
4025 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4027 gtk_window_activate_default (window);
4035 /* Callback used when the file list's popup menu is detached */
4037 popup_menu_detach_cb (GtkWidget *attach_widget,
4040 GtkFileChooserDefault *impl;
4042 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4043 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4045 impl->browse_files_popup_menu = NULL;
4046 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4047 impl->browse_files_popup_menu_hidden_files_item = NULL;
4050 /* Callback used when the "Add to Bookmarks" menu item is activated */
4052 add_to_shortcuts_cb (GtkMenuItem *item,
4053 GtkFileChooserDefault *impl)
4055 bookmarks_add_selected_folder (impl);
4058 /* Callback used when the "Show Hidden Files" menu item is toggled */
4060 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4061 GtkFileChooserDefault *impl)
4064 "show-hidden", gtk_check_menu_item_get_active (item),
4068 /* Shows an error dialog about not being able to select a dragged file */
4070 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4075 _("Could not select file"),
4080 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4085 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4087 for (i = 1; uris[i]; i++)
4090 GError *error = NULL;
4093 file = g_file_new_for_uri (uri);
4095 gtk_file_chooser_default_select_file (chooser, file, &error);
4097 error_selecting_dragged_file_dialog (impl, file, error);
4099 g_object_unref (file);
4103 struct FileListDragData
4105 GtkFileChooserDefault *impl;
4111 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4113 const GError *error,
4116 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4117 struct FileListDragData *data = user_data;
4118 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4120 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4123 data->impl->file_list_drag_data_received_cancellable = NULL;
4125 if (cancelled || error)
4128 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4129 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4130 data->uris[1] == 0 && !error &&
4131 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4132 change_folder_and_display_error (data->impl, data->file, FALSE);
4135 GError *error = NULL;
4137 gtk_file_chooser_default_unselect_all (chooser);
4138 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4140 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4142 browse_files_center_selected_row (data->impl);
4145 if (data->impl->select_multiple)
4146 file_list_drag_data_select_uris (data->impl, data->uris);
4149 g_object_unref (data->impl);
4150 g_strfreev (data->uris);
4151 g_object_unref (data->file);
4154 g_object_unref (cancellable);
4158 file_list_drag_data_received_cb (GtkWidget *widget,
4159 GdkDragContext *context,
4162 GtkSelectionData *selection_data,
4167 GtkFileChooserDefault *impl;
4168 GtkFileChooser *chooser;
4173 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4174 chooser = GTK_FILE_CHOOSER (data);
4176 /* Allow only drags from other widgets; see bug #533891. */
4177 if (gtk_drag_get_source_widget (context) == widget)
4180 /* Parse the text/uri-list string, navigate to the first one */
4181 uris = gtk_selection_data_get_uris (selection_data);
4182 if (uris && uris[0])
4184 struct FileListDragData *data;
4187 file = g_file_new_for_uri (uri);
4189 data = g_new0 (struct FileListDragData, 1);
4190 data->impl = g_object_ref (impl);
4194 if (impl->file_list_drag_data_received_cancellable)
4195 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4197 impl->file_list_drag_data_received_cancellable =
4198 _gtk_file_system_get_info (impl->file_system, file,
4200 file_list_drag_data_received_get_info_cb,
4204 g_signal_stop_emission_by_name (widget, "drag_data_received");
4207 /* Don't do anything with the drag_drop signal */
4209 file_list_drag_drop_cb (GtkWidget *widget,
4210 GdkDragContext *context,
4214 GtkFileChooserDefault *impl)
4216 g_signal_stop_emission_by_name (widget, "drag_drop");
4220 /* Disable the normal tree drag motion handler, it makes it look like you're
4221 dropping the dragged item onto a tree item */
4223 file_list_drag_motion_cb (GtkWidget *widget,
4224 GdkDragContext *context,
4228 GtkFileChooserDefault *impl)
4230 g_signal_stop_emission_by_name (widget, "drag_motion");
4234 /* Constructs the popup menu for the file list if needed */
4236 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4240 if (impl->browse_files_popup_menu)
4243 impl->browse_files_popup_menu = gtk_menu_new ();
4244 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4245 impl->browse_files_tree_view,
4246 popup_menu_detach_cb);
4248 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4249 impl->browse_files_popup_menu_add_shortcut_item = item;
4250 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4251 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4252 gtk_widget_set_sensitive (item, FALSE);
4253 g_signal_connect (item, "activate",
4254 G_CALLBACK (add_to_shortcuts_cb), impl);
4255 gtk_widget_show (item);
4256 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4258 item = gtk_separator_menu_item_new ();
4259 gtk_widget_show (item);
4260 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4262 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4263 impl->browse_files_popup_menu_hidden_files_item = item;
4264 g_signal_connect (item, "toggled",
4265 G_CALLBACK (show_hidden_toggled_cb), impl);
4266 gtk_widget_show (item);
4267 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4270 /* Updates the popup menu for the file list, creating it if necessary */
4272 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4274 file_list_build_popup_menu (impl);
4276 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4278 /* The sensitivity of the Add to Bookmarks item is set in
4279 * bookmarks_check_add_sensitivity()
4282 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4283 G_CALLBACK (show_hidden_toggled_cb), impl);
4284 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4286 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4287 G_CALLBACK (show_hidden_toggled_cb), impl);
4291 popup_position_func (GtkMenu *menu,
4297 GtkWidget *widget = GTK_WIDGET (user_data);
4298 GdkScreen *screen = gtk_widget_get_screen (widget);
4301 GdkRectangle monitor;
4303 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4305 gdk_window_get_origin (widget->window, x, y);
4307 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4309 *x += (widget->allocation.width - req.width) / 2;
4310 *y += (widget->allocation.height - req.height) / 2;
4312 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4313 gtk_menu_set_monitor (menu, monitor_num);
4314 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4316 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4317 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4323 file_list_popup_menu (GtkFileChooserDefault *impl,
4324 GdkEventButton *event)
4326 file_list_update_popup_menu (impl);
4328 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4329 NULL, NULL, NULL, NULL,
4330 event->button, event->time);
4333 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4335 popup_position_func, impl->browse_files_tree_view,
4336 0, GDK_CURRENT_TIME);
4337 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4343 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4345 list_popup_menu_cb (GtkWidget *widget,
4346 GtkFileChooserDefault *impl)
4348 file_list_popup_menu (impl, NULL);
4352 /* Callback used when a button is pressed on the file list. We trap button 3 to
4353 * bring up a popup menu.
4356 list_button_press_event_cb (GtkWidget *widget,
4357 GdkEventButton *event,
4358 GtkFileChooserDefault *impl)
4360 static gboolean in_press = FALSE;
4366 if (event->button != 3)
4370 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4373 file_list_popup_menu (impl, event);
4377 /* Sets the sort column IDs for the file list based on the operation mode */
4379 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4381 int name_id, mtime_id;
4383 name_id = mtime_id = 0;
4385 switch (impl->operation_mode)
4387 case OPERATION_MODE_BROWSE:
4388 name_id = FILE_LIST_COL_NAME;
4389 mtime_id = FILE_LIST_COL_MTIME;
4391 case OPERATION_MODE_SEARCH:
4392 name_id = SEARCH_MODEL_COL_FILE;
4393 mtime_id = SEARCH_MODEL_COL_STAT;
4395 case OPERATION_MODE_RECENT:
4396 name_id = RECENT_MODEL_COL_FILE;
4397 mtime_id = RECENT_MODEL_COL_INFO;
4401 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4402 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4406 file_list_query_tooltip_cb (GtkWidget *widget,
4409 gboolean keyboard_tip,
4410 GtkTooltip *tooltip,
4413 GtkFileChooserDefault *impl = user_data;
4414 GtkTreeIter iter, child_iter;
4415 GtkTreePath *path = NULL;
4419 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4423 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4431 switch (impl->operation_mode)
4433 case OPERATION_MODE_SEARCH:
4434 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4436 gtk_tree_path_free (path);
4440 search_get_valid_child_iter (impl, &child_iter, &iter);
4441 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4442 SEARCH_MODEL_COL_FILE, &file,
4446 case OPERATION_MODE_RECENT:
4447 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4449 gtk_tree_path_free (path);
4453 recent_get_valid_child_iter (impl, &child_iter, &iter);
4454 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4455 RECENT_MODEL_COL_FILE, &file,
4459 case OPERATION_MODE_BROWSE:
4460 g_assert_not_reached ();
4466 gtk_tree_path_free (path);
4470 filename = g_file_get_path (file);
4471 gtk_tooltip_set_text (tooltip, filename);
4472 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4477 gtk_tree_path_free (path);
4482 /* Creates the widgets for the file list */
4484 create_file_list (GtkFileChooserDefault *impl)
4487 GtkTreeSelection *selection;
4488 GtkTreeViewColumn *column;
4489 GtkCellRenderer *renderer;
4491 /* Scrolled window */
4492 swin = gtk_scrolled_window_new (NULL, NULL);
4493 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4494 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4495 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4498 /* Tree/list view */
4500 impl->browse_files_tree_view = gtk_tree_view_new ();
4501 #ifdef PROFILE_FILE_CHOOSER
4502 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4504 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4505 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4507 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4508 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4510 gtk_drag_dest_set (impl->browse_files_tree_view,
4511 GTK_DEST_DEFAULT_ALL,
4513 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4514 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4516 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4517 G_CALLBACK (list_row_activated), impl);
4518 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4519 G_CALLBACK (trap_activate_cb), impl);
4520 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4521 G_CALLBACK (list_popup_menu_cb), impl);
4522 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4523 G_CALLBACK (list_button_press_event_cb), impl);
4525 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4526 G_CALLBACK (file_list_drag_data_received_cb), impl);
4527 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4528 G_CALLBACK (file_list_drag_drop_cb), impl);
4529 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4530 G_CALLBACK (file_list_drag_motion_cb), impl);
4532 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4533 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4534 G_CALLBACK (file_list_query_tooltip_cb), impl);
4536 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4537 gtk_tree_selection_set_select_function (selection,
4540 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4543 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4544 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4546 g_signal_connect (selection, "changed",
4547 G_CALLBACK (list_selection_changed), impl);
4549 /* Filename column */
4551 impl->list_name_column = gtk_tree_view_column_new ();
4552 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4553 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4554 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4555 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4557 renderer = gtk_cell_renderer_pixbuf_new ();
4558 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4559 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4560 list_icon_data_func, impl, NULL);
4562 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4563 g_object_set (impl->list_name_renderer,
4564 "ellipsize", PANGO_ELLIPSIZE_END,
4566 g_signal_connect (impl->list_name_renderer, "edited",
4567 G_CALLBACK (renderer_edited_cb), impl);
4568 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4569 G_CALLBACK (renderer_editing_canceled_cb), impl);
4570 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4571 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4572 list_name_data_func, impl, NULL);
4574 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4578 column = gtk_tree_view_column_new ();
4579 gtk_tree_view_column_set_title (column, _("Size"));
4581 renderer = gtk_cell_renderer_text_new ();
4582 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4583 gtk_tree_view_column_set_cell_data_func (column, renderer,
4584 list_size_data_func, impl, NULL);
4585 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4586 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4589 /* Modification time column */
4591 column = gtk_tree_view_column_new ();
4592 gtk_tree_view_column_set_resizable (column, TRUE);
4593 gtk_tree_view_column_set_title (column, _("Modified"));
4595 renderer = gtk_cell_renderer_text_new ();
4596 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4597 gtk_tree_view_column_set_cell_data_func (column, renderer,
4598 list_mtime_data_func, impl, NULL);
4599 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4600 impl->list_mtime_column = column;
4602 file_list_set_sort_column_ids (impl);
4604 gtk_widget_show_all (swin);
4610 create_path_bar (GtkFileChooserDefault *impl)
4612 GtkWidget *path_bar;
4614 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4615 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4620 /* Creates the widgets for the files/folders pane */
4622 file_pane_create (GtkFileChooserDefault *impl,
4623 GtkSizeGroup *size_group)
4629 vbox = gtk_vbox_new (FALSE, 6);
4630 gtk_widget_show (vbox);
4632 /* Box for lists and preview */
4634 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4635 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4636 gtk_widget_show (hbox);
4640 widget = create_file_list (impl);
4641 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4645 impl->preview_box = gtk_vbox_new (FALSE, 12);
4646 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4647 /* Don't show preview box initially */
4651 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4653 widget = filter_create (impl);
4655 gtk_widget_show (widget);
4656 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4658 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4659 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4664 /* Callback used when the "Browse for more folders" expander is toggled */
4666 expander_changed_cb (GtkExpander *expander,
4668 GtkFileChooserDefault *impl)
4670 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4671 update_appearance (impl);
4674 /* Callback used when the selection changes in the save folder combo box */
4676 save_folder_combo_changed_cb (GtkComboBox *combo,
4677 GtkFileChooserDefault *impl)
4681 if (impl->changing_folder)
4684 if (gtk_combo_box_get_active_iter (combo, &iter))
4686 GtkTreeIter child_iter;
4688 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4691 shortcuts_activate_iter (impl, &child_iter);
4695 /* Filter function used to filter out the Search item and its separator.
4696 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4699 shortcuts_combo_filter_func (GtkTreeModel *model,
4703 GtkFileChooserDefault *impl;
4704 GtkTreePath *tree_path;
4709 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4711 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4713 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4714 g_assert (tree_path != NULL);
4716 indices = gtk_tree_path_get_indices (tree_path);
4720 if (impl->has_search)
4722 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4723 if (idx == indices[0])
4727 if (impl->has_recent)
4729 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4730 if (idx == indices[0])
4734 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4735 if (idx == indices[0])
4740 gtk_tree_path_free (tree_path);
4745 /* Creates the combo box with the save folders */
4747 save_folder_combo_create (GtkFileChooserDefault *impl)
4750 GtkCellRenderer *cell;
4752 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4753 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4754 shortcuts_combo_filter_func,
4758 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4759 "model", impl->shortcuts_combo_filter_model,
4760 "focus-on-click", FALSE,
4762 gtk_widget_show (combo);
4764 cell = gtk_cell_renderer_pixbuf_new ();
4765 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4766 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4767 "pixbuf", SHORTCUTS_COL_PIXBUF,
4768 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4769 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4772 cell = gtk_cell_renderer_text_new ();
4773 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4774 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4775 "text", SHORTCUTS_COL_NAME,
4776 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4779 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4780 shortcuts_row_separator_func,
4783 g_signal_connect (combo, "changed",
4784 G_CALLBACK (save_folder_combo_changed_cb), impl);
4789 /* Creates the widgets specific to Save mode */
4791 save_widgets_create (GtkFileChooserDefault *impl)
4796 GtkWidget *alignment;
4798 if (impl->save_widgets != NULL)
4801 location_switch_to_path_bar (impl);
4803 vbox = gtk_vbox_new (FALSE, 12);
4805 table = gtk_table_new (2, 2, FALSE);
4806 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4807 gtk_widget_show (table);
4808 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4809 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4813 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4814 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4815 gtk_table_attach (GTK_TABLE (table), widget,
4819 gtk_widget_show (widget);
4821 /* Location entry */
4823 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4824 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4826 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4827 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4828 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4830 GTK_EXPAND | GTK_FILL, 0,
4832 gtk_widget_show (impl->location_entry);
4833 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4836 impl->save_folder_label = gtk_label_new (NULL);
4837 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4838 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4842 gtk_widget_show (impl->save_folder_label);
4844 impl->save_folder_combo = save_folder_combo_create (impl);
4845 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4847 GTK_EXPAND | GTK_FILL, GTK_FILL,
4849 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4852 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4853 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4855 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4856 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4857 g_signal_connect (impl->save_expander, "notify::expanded",
4858 G_CALLBACK (expander_changed_cb),
4860 gtk_widget_show_all (alignment);
4862 impl->save_widgets = vbox;
4863 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4864 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4865 gtk_widget_show (impl->save_widgets);
4868 /* Destroys the widgets specific to Save mode */
4870 save_widgets_destroy (GtkFileChooserDefault *impl)
4872 if (impl->save_widgets == NULL)
4875 gtk_widget_destroy (impl->save_widgets);
4876 impl->save_widgets = NULL;
4877 impl->location_entry = NULL;
4878 impl->save_folder_label = NULL;
4879 impl->save_folder_combo = NULL;
4880 impl->save_expander = NULL;
4883 /* Turns on the path bar widget. Can be called even if we are already in that
4887 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4889 if (impl->location_entry)
4891 gtk_widget_destroy (impl->location_entry);
4892 impl->location_entry = NULL;
4895 gtk_widget_hide (impl->location_entry_box);
4898 /* Sets the full path of the current folder as the text in the location entry. */
4900 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4902 gchar *text, *filename;
4904 if (!impl->current_folder)
4907 filename = g_file_get_path (impl->current_folder);
4911 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4915 text = g_file_get_uri (impl->current_folder);
4919 gboolean need_slash;
4922 len = strlen (text);
4923 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4929 slash_text = g_new (char, len + 2);
4930 strcpy (slash_text, text);
4931 slash_text[len] = G_DIR_SEPARATOR;
4932 slash_text[len + 1] = 0;
4938 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4945 /* Turns on the location entry. Can be called even if we are already in that
4949 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4951 /* when in search or recent files mode, we are not showing the
4952 * location_entry_box container, so there's no point in switching
4955 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4956 impl->operation_mode == OPERATION_MODE_RECENT)
4959 if (impl->location_entry)
4960 gtk_widget_destroy (impl->location_entry);
4964 gtk_widget_show (impl->location_entry_box);
4968 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4969 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4971 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4972 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4974 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4975 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4977 /* Configure the entry */
4979 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4983 gtk_widget_show (impl->location_entry);
4984 gtk_widget_grab_focus (impl->location_entry);
4987 /* Sets a new location mode. set_buttons determines whether the toggle button
4988 * for the mode will also be changed.
4991 location_mode_set (GtkFileChooserDefault *impl,
4992 LocationMode new_mode,
4993 gboolean set_button)
4995 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4996 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4998 GtkWindow *toplevel;
4999 GtkWidget *current_focus;
5000 gboolean button_active;
5001 gboolean switch_to_file_list;
5005 case LOCATION_MODE_PATH_BAR:
5006 button_active = FALSE;
5008 /* The location_entry will disappear when we switch to path bar mode. So,
5009 * we'll focus the file list in that case, to avoid having a window with
5010 * no focused widget.
5012 toplevel = get_toplevel (GTK_WIDGET (impl));
5013 switch_to_file_list = FALSE;
5016 current_focus = gtk_window_get_focus (toplevel);
5017 if (!current_focus || current_focus == impl->location_entry)
5018 switch_to_file_list = TRUE;
5021 location_switch_to_path_bar (impl);
5023 if (switch_to_file_list)
5024 gtk_widget_grab_focus (impl->browse_files_tree_view);
5028 case LOCATION_MODE_FILENAME_ENTRY:
5029 button_active = TRUE;
5030 location_switch_to_filename_entry (impl);
5034 g_assert_not_reached ();
5040 g_signal_handlers_block_by_func (impl->location_button,
5041 G_CALLBACK (location_button_toggled_cb), impl);
5043 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5045 g_signal_handlers_unblock_by_func (impl->location_button,
5046 G_CALLBACK (location_button_toggled_cb), impl);
5050 impl->location_mode = new_mode;
5054 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5056 /* when in search or recent files mode, we are not showing the
5057 * location_entry_box container, so there's no point in switching
5060 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5061 impl->operation_mode == OPERATION_MODE_RECENT)
5064 /* If the file entry is not visible, show it.
5065 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5067 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5069 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5071 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5073 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5075 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5079 gtk_widget_grab_focus (impl->location_entry);
5084 /* Callback used when one of the location mode buttons is toggled */
5086 location_button_toggled_cb (GtkToggleButton *toggle,
5087 GtkFileChooserDefault *impl)
5090 LocationMode new_mode;
5092 is_active = gtk_toggle_button_get_active (toggle);
5096 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5097 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5101 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5102 new_mode = LOCATION_MODE_PATH_BAR;
5105 location_mode_set (impl, new_mode, FALSE);
5108 /* Creates a toggle button for the location entry. */
5110 location_button_create (GtkFileChooserDefault *impl)
5115 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5116 gtk_widget_show (image);
5118 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5122 g_signal_connect (impl->location_button, "toggled",
5123 G_CALLBACK (location_button_toggled_cb), impl);
5125 str = _("Type a file name");
5127 gtk_widget_set_tooltip_text (impl->location_button, str);
5128 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5131 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5133 browse_widgets_create (GtkFileChooserDefault *impl)
5139 GtkSizeGroup *size_group;
5141 /* size group is used by the [+][-] buttons and the filter combo */
5142 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5143 vbox = gtk_vbox_new (FALSE, 12);
5145 /* Location widgets */
5146 hbox = gtk_hbox_new (FALSE, 12);
5147 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5148 gtk_widget_show (hbox);
5149 impl->browse_path_bar_hbox = hbox;
5151 location_button_create (impl);
5152 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5156 impl->browse_path_bar = create_path_bar (impl);
5157 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5158 gtk_widget_show_all (impl->browse_path_bar);
5159 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5162 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5163 g_signal_connect (impl->browse_new_folder_button, "clicked",
5164 G_CALLBACK (new_folder_button_clicked), impl);
5165 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5167 /* Box for the location label and entry */
5169 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5170 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5172 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5173 gtk_widget_show (impl->location_label);
5174 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5177 hpaned = gtk_hpaned_new ();
5178 gtk_widget_show (hpaned);
5179 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5181 widget = shortcuts_pane_create (impl, size_group);
5182 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5183 widget = file_pane_create (impl, size_group);
5184 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5186 g_object_unref (size_group);
5192 gtk_file_chooser_default_constructor (GType type,
5193 guint n_construct_properties,
5194 GObjectConstructParam *construct_params)
5196 GtkFileChooserDefault *impl;
5199 profile_start ("start", NULL);
5201 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5202 n_construct_properties,
5204 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5206 g_assert (impl->file_system);
5208 gtk_widget_push_composite_child ();
5210 /* Shortcuts model */
5211 shortcuts_model_create (impl);
5213 /* The browse widgets */
5214 impl->browse_widgets = browse_widgets_create (impl);
5215 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5217 /* Alignment to hold extra widget */
5218 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5219 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5221 gtk_widget_pop_composite_child ();
5222 update_appearance (impl);
5224 profile_end ("end", NULL);
5229 /* Sets the extra_widget by packing it in the appropriate place */
5231 set_extra_widget (GtkFileChooserDefault *impl,
5232 GtkWidget *extra_widget)
5236 g_object_ref (extra_widget);
5237 /* FIXME: is this right ? */
5238 gtk_widget_show (extra_widget);
5241 if (impl->extra_widget)
5243 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5244 g_object_unref (impl->extra_widget);
5247 impl->extra_widget = extra_widget;
5248 if (impl->extra_widget)
5250 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5251 gtk_widget_show (impl->extra_align);
5254 gtk_widget_hide (impl->extra_align);
5258 set_local_only (GtkFileChooserDefault *impl,
5259 gboolean local_only)
5261 if (local_only != impl->local_only)
5263 impl->local_only = local_only;
5265 if (impl->shortcuts_model && impl->file_system)
5267 shortcuts_add_volumes (impl);
5268 shortcuts_add_bookmarks (impl);
5271 if (local_only && !g_file_is_native (impl->current_folder))
5273 /* If we are pointing to a non-local folder, make an effort to change
5274 * back to a local folder, but it's really up to the app to not cause
5275 * such a situation, so we ignore errors.
5277 const gchar *home = g_get_home_dir ();
5283 home_file = g_file_new_for_path (home);
5285 _gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5287 g_object_unref (home_file);
5293 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5294 GtkFileChooserDefault *impl)
5296 shortcuts_add_volumes (impl);
5297 shortcuts_add_bookmarks (impl);
5299 bookmarks_check_add_sensitivity (impl);
5300 bookmarks_check_remove_sensitivity (impl);
5301 shortcuts_check_popup_sensitivity (impl);
5304 /* Sets the file chooser to multiple selection mode */
5306 set_select_multiple (GtkFileChooserDefault *impl,
5307 gboolean select_multiple,
5308 gboolean property_notify)
5310 GtkTreeSelection *selection;
5311 GtkSelectionMode mode;
5313 if (select_multiple == impl->select_multiple)
5316 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5318 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5319 gtk_tree_selection_set_mode (selection, mode);
5321 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5323 impl->select_multiple = select_multiple;
5324 g_object_notify (G_OBJECT (impl), "select-multiple");
5326 check_preview_change (impl);
5330 set_file_system_backend (GtkFileChooserDefault *impl)
5332 profile_start ("start for backend", "default");
5334 impl->file_system = _gtk_file_system_new ();
5336 g_signal_connect (impl->file_system, "volumes-changed",
5337 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5338 g_signal_connect (impl->file_system, "bookmarks-changed",
5339 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5341 profile_end ("end", NULL);
5344 /* This function is basically a do_all function.
5346 * It sets the visibility on all the widgets based on the current state, and
5347 * moves the custom_widget if needed.
5350 update_appearance (GtkFileChooserDefault *impl)
5352 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5353 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5357 gtk_widget_hide (impl->location_button);
5358 save_widgets_create (impl);
5360 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5361 text = _("Save in _folder:");
5363 text = _("Create in _folder:");
5365 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5367 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5369 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5370 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5371 gtk_widget_show (impl->browse_widgets);
5375 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5376 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5377 gtk_widget_hide (impl->browse_widgets);
5380 gtk_widget_show (impl->browse_new_folder_button);
5382 if (impl->select_multiple)
5384 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5385 "Re-setting to single selection mode.");
5386 set_select_multiple (impl, FALSE, TRUE);
5389 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5390 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5392 gtk_widget_show (impl->location_button);
5393 save_widgets_destroy (impl);
5394 gtk_widget_show (impl->browse_widgets);
5395 location_mode_set (impl, impl->location_mode, TRUE);
5398 if (impl->location_entry)
5399 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5401 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5402 gtk_widget_hide (impl->browse_new_folder_button);
5404 gtk_widget_show (impl->browse_new_folder_button);
5406 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5407 * of files may change depending whether we are in a file or folder-only mode.
5409 gtk_widget_queue_draw (impl->browse_files_tree_view);
5411 g_signal_emit_by_name (impl, "default-size-changed");
5415 gtk_file_chooser_default_set_property (GObject *object,
5417 const GValue *value,
5421 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5425 case GTK_FILE_CHOOSER_PROP_ACTION:
5427 GtkFileChooserAction action = g_value_get_enum (value);
5429 if (action != impl->action)
5431 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5433 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5434 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5435 && impl->select_multiple)
5437 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5438 "this is not allowed in multiple selection mode. Resetting the file chooser "
5439 "to single selection mode.");
5440 set_select_multiple (impl, FALSE, TRUE);
5442 impl->action = action;
5443 update_appearance (impl);
5444 settings_load (impl);
5449 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5450 /* Ignore property */
5453 case GTK_FILE_CHOOSER_PROP_FILTER:
5454 set_current_filter (impl, g_value_get_object (value));
5457 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5458 set_local_only (impl, g_value_get_boolean (value));
5461 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5462 set_preview_widget (impl, g_value_get_object (value));
5465 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5466 impl->preview_widget_active = g_value_get_boolean (value);
5467 update_preview_widget_visibility (impl);
5470 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5471 impl->use_preview_label = g_value_get_boolean (value);
5472 update_preview_widget_visibility (impl);
5475 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5476 set_extra_widget (impl, g_value_get_object (value));
5479 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5481 gboolean select_multiple = g_value_get_boolean (value);
5482 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5483 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5486 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5487 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5488 "leaving the file chooser in single selection mode.");
5492 set_select_multiple (impl, select_multiple, FALSE);
5496 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5498 gboolean show_hidden = g_value_get_boolean (value);
5499 if (show_hidden != impl->show_hidden)
5501 impl->show_hidden = show_hidden;
5503 if (impl->browse_files_model)
5504 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5509 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5511 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5512 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5523 gtk_file_chooser_default_get_property (GObject *object,
5528 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5532 case GTK_FILE_CHOOSER_PROP_ACTION:
5533 g_value_set_enum (value, impl->action);
5536 case GTK_FILE_CHOOSER_PROP_FILTER:
5537 g_value_set_object (value, impl->current_filter);
5540 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5541 g_value_set_boolean (value, impl->local_only);
5544 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5545 g_value_set_object (value, impl->preview_widget);
5548 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5549 g_value_set_boolean (value, impl->preview_widget_active);
5552 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5553 g_value_set_boolean (value, impl->use_preview_label);
5556 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5557 g_value_set_object (value, impl->extra_widget);
5560 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5561 g_value_set_boolean (value, impl->select_multiple);
5564 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5565 g_value_set_boolean (value, impl->show_hidden);
5568 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5569 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5578 /* Removes the settings signal handler. It's safe to call multiple times */
5580 remove_settings_signal (GtkFileChooserDefault *impl,
5583 if (impl->settings_signal_id)
5585 GtkSettings *settings;
5587 settings = gtk_settings_get_for_screen (screen);
5588 g_signal_handler_disconnect (settings,
5589 impl->settings_signal_id);
5590 impl->settings_signal_id = 0;
5595 gtk_file_chooser_default_dispose (GObject *object)
5598 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5600 if (impl->extra_widget)
5602 g_object_unref (impl->extra_widget);
5603 impl->extra_widget = NULL;
5606 pending_select_files_free (impl);
5608 /* cancel all pending operations */
5609 if (impl->pending_cancellables)
5611 for (l = impl->pending_cancellables; l; l = l->next)
5613 GCancellable *cancellable = G_CANCELLABLE (l->data);
5614 g_cancellable_cancel (cancellable);
5616 g_slist_free (impl->pending_cancellables);
5617 impl->pending_cancellables = NULL;
5620 if (impl->reload_icon_cancellables)
5622 for (l = impl->reload_icon_cancellables; l; l = l->next)
5624 GCancellable *cancellable = G_CANCELLABLE (l->data);
5625 g_cancellable_cancel (cancellable);
5627 g_slist_free (impl->reload_icon_cancellables);
5628 impl->reload_icon_cancellables = NULL;
5631 if (impl->loading_shortcuts)
5633 for (l = impl->loading_shortcuts; l; l = l->next)
5635 GCancellable *cancellable = G_CANCELLABLE (l->data);
5636 g_cancellable_cancel (cancellable);
5638 g_slist_free (impl->loading_shortcuts);
5639 impl->loading_shortcuts = NULL;
5642 if (impl->file_list_drag_data_received_cancellable)
5644 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5645 impl->file_list_drag_data_received_cancellable = NULL;
5648 if (impl->update_current_folder_cancellable)
5650 g_cancellable_cancel (impl->update_current_folder_cancellable);
5651 impl->update_current_folder_cancellable = NULL;
5654 if (impl->show_and_select_files_cancellable)
5656 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5657 impl->show_and_select_files_cancellable = NULL;
5660 if (impl->should_respond_get_info_cancellable)
5662 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5663 impl->should_respond_get_info_cancellable = NULL;
5666 if (impl->update_from_entry_cancellable)
5668 g_cancellable_cancel (impl->update_from_entry_cancellable);
5669 impl->update_from_entry_cancellable = NULL;
5672 if (impl->shortcuts_activate_iter_cancellable)
5674 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5675 impl->shortcuts_activate_iter_cancellable = NULL;
5678 search_stop_searching (impl, TRUE);
5679 recent_stop_loading (impl);
5681 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5683 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5686 /* We override show-all since we have internal widgets that
5687 * shouldn't be shown when you call show_all(), like the filter
5691 gtk_file_chooser_default_show_all (GtkWidget *widget)
5693 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5695 gtk_widget_show (widget);
5697 if (impl->extra_widget)
5698 gtk_widget_show_all (impl->extra_widget);
5701 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5702 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5705 toplevel_set_focus_cb (GtkWindow *window,
5707 GtkFileChooserDefault *impl)
5709 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5712 /* We monitor the focus widget on our toplevel to be able to know which widget
5713 * was last focused at the time our "should_respond" method gets called.
5716 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5717 GtkWidget *previous_toplevel)
5719 GtkFileChooserDefault *impl;
5720 GtkWidget *toplevel;
5722 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5724 if (previous_toplevel)
5726 g_assert (impl->toplevel_set_focus_id != 0);
5727 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5728 impl->toplevel_set_focus_id = 0;
5729 impl->toplevel_last_focus_widget = NULL;
5732 g_assert (impl->toplevel_set_focus_id == 0);
5734 toplevel = gtk_widget_get_toplevel (widget);
5735 if (GTK_IS_WINDOW (toplevel))
5737 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5738 G_CALLBACK (toplevel_set_focus_cb), impl);
5739 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5743 /* Changes the icons wherever it is needed */
5745 change_icon_theme (GtkFileChooserDefault *impl)
5747 GtkSettings *settings;
5750 profile_start ("start", NULL);
5752 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5754 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5755 impl->icon_size = MAX (width, height);
5757 impl->icon_size = FALLBACK_ICON_SIZE;
5759 shortcuts_reload_icons (impl);
5760 gtk_widget_queue_resize (impl->browse_files_tree_view);
5762 profile_end ("end", NULL);
5765 /* Callback used when a GtkSettings value changes */
5767 settings_notify_cb (GObject *object,
5769 GtkFileChooserDefault *impl)
5773 profile_start ("start", NULL);
5775 name = g_param_spec_get_name (pspec);
5777 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5778 strcmp (name, "gtk-icon-sizes") == 0)
5779 change_icon_theme (impl);
5781 profile_end ("end", NULL);
5784 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5788 check_icon_theme (GtkFileChooserDefault *impl)
5790 GtkSettings *settings;
5792 profile_start ("start", NULL);
5794 if (impl->settings_signal_id)
5796 profile_end ("end", NULL);
5800 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5802 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5803 impl->settings_signal_id = g_signal_connect (settings, "notify",
5804 G_CALLBACK (settings_notify_cb), impl);
5806 change_icon_theme (impl);
5809 profile_end ("end", NULL);
5813 gtk_file_chooser_default_style_set (GtkWidget *widget,
5814 GtkStyle *previous_style)
5816 GtkFileChooserDefault *impl;
5818 profile_start ("start", NULL);
5820 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5822 profile_msg (" parent class style_set start", NULL);
5823 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5824 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5825 profile_msg (" parent class style_set end", NULL);
5827 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5828 change_icon_theme (impl);
5830 profile_msg (" emit default-size-changed start", NULL);
5831 g_signal_emit_by_name (widget, "default-size-changed");
5832 profile_msg (" emit default-size-changed end", NULL);
5834 profile_end ("end", NULL);
5838 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5839 GdkScreen *previous_screen)
5841 GtkFileChooserDefault *impl;
5843 profile_start ("start", NULL);
5845 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5847 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5848 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5850 remove_settings_signal (impl, previous_screen);
5851 check_icon_theme (impl);
5853 g_signal_emit_by_name (widget, "default-size-changed");
5855 profile_end ("end", NULL);
5859 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5860 GtkAllocation *allocation)
5862 GtkFileChooserDefault *impl;
5864 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5866 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5868 impl->default_width = allocation->width;
5869 impl->default_height = allocation->height;
5871 if (impl->preview_widget_active &&
5872 impl->preview_widget &&
5873 GTK_WIDGET_DRAWABLE (impl->preview_widget))
5874 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
5876 if (impl->extra_widget &&
5877 GTK_WIDGET_DRAWABLE (impl->extra_widget))
5878 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
5882 get_is_file_filtered (GtkFileChooserDefault *impl,
5884 GFileInfo *file_info)
5886 GtkFileFilterInfo filter_info;
5887 GtkFileFilterFlags needed;
5890 if (!impl->current_filter)
5893 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5895 needed = gtk_file_filter_get_needed (impl->current_filter);
5897 filter_info.display_name = g_file_info_get_display_name (file_info);
5898 filter_info.mime_type = g_file_info_get_content_type (file_info);
5900 if (needed & GTK_FILE_FILTER_FILENAME)
5902 filter_info.filename = g_file_get_path (file);
5903 if (filter_info.filename)
5904 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5907 filter_info.filename = NULL;
5909 if (needed & GTK_FILE_FILTER_URI)
5911 filter_info.uri = g_file_get_uri (file);
5912 if (filter_info.uri)
5913 filter_info.contains |= GTK_FILE_FILTER_URI;
5916 filter_info.uri = NULL;
5918 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5920 if (filter_info.filename)
5921 g_free ((gchar *)filter_info.filename);
5922 if (filter_info.uri)
5923 g_free ((gchar *)filter_info.uri);
5929 settings_load (GtkFileChooserDefault *impl)
5931 GtkFileChooserSettings *settings;
5932 LocationMode location_mode;
5933 gboolean show_hidden;
5934 gboolean expand_folders;
5936 settings = _gtk_file_chooser_settings_new ();
5938 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5939 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5940 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5942 g_object_unref (settings);
5944 location_mode_set (impl, location_mode, TRUE);
5945 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5946 impl->expand_folders = expand_folders;
5947 if (impl->save_expander)
5948 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5952 settings_save (GtkFileChooserDefault *impl)
5954 GtkFileChooserSettings *settings;
5956 settings = _gtk_file_chooser_settings_new ();
5958 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5959 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5960 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5963 _gtk_file_chooser_settings_save (settings, NULL);
5965 g_object_unref (settings);
5968 /* GtkWidget::map method */
5970 gtk_file_chooser_default_map (GtkWidget *widget)
5972 GtkFileChooserDefault *impl;
5973 char *current_working_dir;
5975 profile_start ("start", NULL);
5977 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5979 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5981 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5983 switch (impl->reload_state)
5986 /* The user didn't explicitly give us a folder to
5987 * display, so we'll use the cwd
5989 current_working_dir = g_get_current_dir ();
5990 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
5991 current_working_dir);
5992 g_free (current_working_dir);
5995 case RELOAD_HAS_FOLDER:
5996 /* Nothing; we are already loading or loaded, so we
5997 * don't need to reload
6001 case RELOAD_WAS_UNMAPPED:
6002 /* Just reload the current folder; else continue
6005 if (impl->current_folder)
6007 pending_select_files_store_selection (impl);
6008 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6013 g_assert_not_reached ();
6017 volumes_bookmarks_changed_cb (impl->file_system, impl);
6019 settings_load (impl);
6021 profile_end ("end", NULL);
6024 /* GtkWidget::unmap method */
6026 gtk_file_chooser_default_unmap (GtkWidget *widget)
6028 GtkFileChooserDefault *impl;
6030 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6032 settings_save (impl);
6034 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6036 impl->reload_state = RELOAD_WAS_UNMAPPED;
6040 list_model_filter_func (GtkFileSystemModel *model,
6042 GFileInfo *file_info,
6045 GtkFileChooserDefault *impl = user_data;
6047 if (!impl->current_filter)
6050 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6053 return !get_is_file_filtered (impl, file, file_info);
6057 install_list_model_filter (GtkFileChooserDefault *impl)
6059 GtkFileSystemModelFilter filter;
6062 g_assert (impl->browse_files_model != NULL);
6064 if (impl->current_filter)
6066 filter = list_model_filter_func;
6075 _gtk_file_system_model_set_filter (impl->browse_files_model,
6080 #define COMPARE_DIRECTORIES \
6081 GtkFileChooserDefault *impl = user_data; \
6082 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6083 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6084 gboolean dir_a, dir_b; \
6087 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6089 return impl->list_sort_ascending ? -1 : 1; \
6092 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6094 return impl->list_sort_ascending ? 1 : -1; \
6096 if (dir_a != dir_b) \
6097 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6099 /* Sort callback for the filename column */
6101 name_sort_func (GtkTreeModel *model,
6106 COMPARE_DIRECTORIES;
6109 gchar *key_a, *key_b;
6112 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6113 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6114 result = strcmp (key_a, key_b);
6123 /* Sort callback for the size column */
6125 size_sort_func (GtkTreeModel *model,
6130 COMPARE_DIRECTORIES;
6133 goffset size_a = g_file_info_get_size (info_a);
6134 goffset size_b = g_file_info_get_size (info_b);
6136 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6140 /* Sort callback for the mtime column */
6142 mtime_sort_func (GtkTreeModel *model,
6147 COMPARE_DIRECTORIES;
6152 g_file_info_get_modification_time (info_a, &ta);
6153 g_file_info_get_modification_time (info_b, &tb);
6155 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6159 /* Callback used when the sort column changes. We cache the sort order for use
6160 * in name_sort_func().
6163 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6164 GtkFileChooserDefault *impl)
6166 GtkSortType sort_type;
6168 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6169 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6173 set_busy_cursor (GtkFileChooserDefault *impl,
6176 GtkWindow *toplevel;
6177 GdkDisplay *display;
6180 toplevel = get_toplevel (GTK_WIDGET (impl));
6181 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6184 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6187 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6191 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6192 gdk_display_flush (display);
6195 gdk_cursor_unref (cursor);
6198 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6200 load_set_model (GtkFileChooserDefault *impl)
6202 profile_start ("start", NULL);
6204 g_assert (impl->browse_files_model != NULL);
6205 g_assert (impl->sort_model == NULL);
6207 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6208 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6209 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6210 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6211 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6212 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6213 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6214 impl->list_sort_ascending = TRUE;
6215 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6217 g_signal_connect (impl->sort_model, "sort_column_changed",
6218 G_CALLBACK (list_sort_column_changed_cb), impl);
6220 profile_msg (" gtk_tree_view_set_model start", NULL);
6221 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6222 GTK_TREE_MODEL (impl->sort_model));
6223 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6224 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6225 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6226 profile_msg (" gtk_tree_view_set_model end", NULL);
6228 profile_end ("end", NULL);
6231 /* Timeout callback used when the loading timer expires */
6233 load_timeout_cb (gpointer data)
6235 GtkFileChooserDefault *impl;
6237 profile_start ("start", NULL);
6239 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6240 g_assert (impl->load_state == LOAD_PRELOAD);
6241 g_assert (impl->load_timeout_id != 0);
6242 g_assert (impl->browse_files_model != NULL);
6244 impl->load_timeout_id = 0;
6245 impl->load_state = LOAD_LOADING;
6247 load_set_model (impl);
6249 profile_end ("end", NULL);
6254 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6256 load_setup_timer (GtkFileChooserDefault *impl)
6258 g_assert (impl->load_timeout_id == 0);
6259 g_assert (impl->load_state != LOAD_PRELOAD);
6261 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6262 impl->load_state = LOAD_PRELOAD;
6265 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6267 load_remove_timer (GtkFileChooserDefault *impl)
6269 if (impl->load_timeout_id != 0)
6271 g_assert (impl->load_state == LOAD_PRELOAD);
6273 g_source_remove (impl->load_timeout_id);
6274 impl->load_timeout_id = 0;
6275 impl->load_state = LOAD_EMPTY;
6278 g_assert (impl->load_state == LOAD_EMPTY ||
6279 impl->load_state == LOAD_LOADING ||
6280 impl->load_state == LOAD_FINISHED);
6283 /* Selects the first row in the file list */
6285 browse_files_select_first_row (GtkFileChooserDefault *impl)
6288 GtkTreeIter dummy_iter;
6289 GtkTreeModel *tree_model;
6291 if (!impl->sort_model)
6294 path = gtk_tree_path_new_from_indices (0, -1);
6295 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6297 /* If the list is empty, do nothing. */
6298 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6299 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6301 gtk_tree_path_free (path);
6304 struct center_selected_row_closure {
6305 GtkFileChooserDefault *impl;
6306 gboolean already_centered;
6309 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6310 * selected row in the tree view.
6313 center_selected_row_foreach_cb (GtkTreeModel *model,
6318 struct center_selected_row_closure *closure;
6321 if (closure->already_centered)
6324 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6325 closure->already_centered = TRUE;
6328 /* Centers the selected row in the tree view */
6330 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6332 struct center_selected_row_closure closure;
6333 GtkTreeSelection *selection;
6335 closure.impl = impl;
6336 closure.already_centered = FALSE;
6338 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6339 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6342 struct ShowAndSelectPathsData
6344 GtkFileChooserDefault *impl;
6349 show_and_select_files_finished_loading (GtkFolder *folder,
6352 gboolean have_hidden;
6353 gboolean have_filtered;
6355 struct ShowAndSelectPathsData *data = user_data;
6357 have_hidden = FALSE;
6358 have_filtered = FALSE;
6360 for (l = data->files; l; l = l->next)
6367 info = _gtk_folder_get_info (folder, file);
6371 have_hidden = g_file_info_get_is_hidden (info);
6374 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6375 get_is_file_filtered (data->impl, file, info);
6377 g_object_unref (info);
6379 if (have_hidden && have_filtered)
6380 break; /* we now have all the information we need */
6384 g_signal_handlers_disconnect_by_func (folder,
6385 show_and_select_files_finished_loading,
6389 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6392 set_current_filter (data->impl, NULL);
6394 for (l = data->files; l; l = l->next)
6399 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6400 select_func, data->impl);
6403 browse_files_center_selected_row (data->impl);
6405 g_object_unref (data->impl);
6406 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6407 g_slist_free (data->files);
6412 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6414 const GError *error,
6417 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6418 struct ShowAndSelectPathsData *data = user_data;
6420 if (data->impl->show_and_select_files_cancellable != cancellable)
6423 data->impl->show_and_select_files_cancellable = NULL;
6425 if (cancelled || error)
6428 g_object_unref (cancellable);
6430 if (_gtk_folder_is_finished_loading (folder))
6431 show_and_select_files_finished_loading (folder, user_data);
6433 g_signal_connect (folder, "finished-loading",
6434 G_CALLBACK (show_and_select_files_finished_loading),
6440 g_object_unref (data->impl);
6441 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6442 g_slist_free (data->files);
6445 g_object_unref (cancellable);
6449 show_and_select_files (GtkFileChooserDefault *impl,
6454 struct ShowAndSelectPathsData *info;
6456 profile_start ("start", NULL);
6460 profile_end ("end", NULL);
6464 info = g_new (struct ShowAndSelectPathsData, 1);
6465 info->impl = g_object_ref (impl);
6466 info->files = g_slist_copy (files);
6467 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6469 if (impl->show_and_select_files_cancellable)
6470 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6472 impl->show_and_select_files_cancellable =
6473 _gtk_file_system_get_folder (impl->file_system, parent_file,
6474 "standard::is-hidden,standard::type,standard::name",
6475 show_and_select_files_get_folder_cb, info);
6477 profile_end ("end", NULL);
6481 /* Processes the pending operation when a folder is finished loading */
6483 pending_select_files_process (GtkFileChooserDefault *impl)
6485 g_assert (impl->load_state == LOAD_FINISHED);
6486 g_assert (impl->browse_files_model != NULL);
6487 g_assert (impl->sort_model != NULL);
6489 if (impl->pending_select_files)
6492 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6493 pending_select_files_free (impl);
6494 browse_files_center_selected_row (impl);
6498 /* We only select the first row if the chooser is actually mapped ---
6499 * selecting the first row is to help the user when he is interacting with
6500 * the chooser, but sometimes a chooser works not on behalf of the user,
6501 * but rather on behalf of something else like GtkFileChooserButton. In
6502 * that case, the chooser's selection should be what the caller expects,
6503 * as the user can't see that something else got selected. See bug #165264.
6505 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6506 browse_files_select_first_row (impl);
6509 g_assert (impl->pending_select_files == NULL);
6512 /* Callback used when the file system model finishes loading */
6514 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6515 GtkFileChooserDefault *impl)
6517 profile_start ("start", NULL);
6519 if (impl->load_state == LOAD_PRELOAD)
6521 load_remove_timer (impl);
6522 load_set_model (impl);
6524 else if (impl->load_state == LOAD_LOADING)
6530 /* We can't g_assert_not_reached(), as something other than us may have
6531 * initiated a folder reload. See #165556.
6533 profile_end ("end", NULL);
6537 g_assert (impl->load_timeout_id == 0);
6539 impl->load_state = LOAD_FINISHED;
6541 pending_select_files_process (impl);
6542 set_busy_cursor (impl, FALSE);
6543 #ifdef PROFILE_FILE_CHOOSER
6544 access ("MARK: *** FINISHED LOADING", F_OK);
6547 profile_end ("end", NULL);
6551 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6553 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6555 if (impl->browse_files_model)
6557 g_object_unref (impl->browse_files_model);
6558 impl->browse_files_model = NULL;
6561 if (impl->sort_model)
6563 g_object_unref (impl->sort_model);
6564 impl->sort_model = NULL;
6567 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6570 /* Gets rid of the old list model and creates a new one for the current folder */
6572 set_list_model (GtkFileChooserDefault *impl,
6575 g_assert (impl->current_folder != NULL);
6577 profile_start ("start", NULL);
6579 stop_loading_and_clear_list_model (impl);
6581 set_busy_cursor (impl, TRUE);
6582 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6584 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6585 impl->current_folder, 0,
6586 "standard,time,thumbnail::*",
6588 if (!impl->browse_files_model)
6590 set_busy_cursor (impl, FALSE);
6591 profile_end ("end", NULL);
6595 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6597 g_signal_connect (impl->browse_files_model, "finished-loading",
6598 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6600 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6602 install_list_model_filter (impl);
6604 profile_end ("end", NULL);
6609 struct update_chooser_entry_selected_foreach_closure {
6611 GtkTreeIter first_selected_iter;
6615 compare_utf8_filenames (const gchar *a,
6618 gchar *a_folded, *b_folded;
6621 a_folded = g_utf8_strdown (a, -1);
6622 b_folded = g_utf8_strdown (b, -1);
6624 retval = strcmp (a_folded, b_folded);
6633 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6638 struct update_chooser_entry_selected_foreach_closure *closure;
6641 closure->num_selected++;
6643 if (closure->num_selected == 1)
6644 closure->first_selected_iter = *iter;
6648 update_chooser_entry (GtkFileChooserDefault *impl)
6650 GtkTreeSelection *selection;
6651 struct update_chooser_entry_selected_foreach_closure closure;
6652 const char *file_part;
6654 /* no need to update the file chooser's entry if there's no entry */
6655 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6656 impl->operation_mode == OPERATION_MODE_RECENT ||
6657 !impl->location_entry)
6660 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6661 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6662 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6663 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6664 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6667 g_assert (impl->location_entry != NULL);
6669 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6670 closure.num_selected = 0;
6671 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6675 if (closure.num_selected == 0)
6677 goto maybe_clear_entry;
6679 else if (closure.num_selected == 1)
6681 GtkTreeIter child_iter;
6683 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6686 gboolean change_entry;
6688 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6690 &closure.first_selected_iter);
6692 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6694 /* If the cursor moved to the row of the newly created folder,
6695 * retrieving info will return NULL.
6700 g_free (impl->browse_files_last_selected_name);
6701 impl->browse_files_last_selected_name =
6702 g_strdup (g_file_info_get_display_name (info));
6704 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6705 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6706 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6708 /* We don't want the name to change when clicking on a folder... */
6709 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6712 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6716 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6718 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6719 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6727 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6728 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6730 /* Multiple selection, so just clear the entry. */
6732 g_free (impl->browse_files_last_selected_name);
6733 impl->browse_files_last_selected_name = NULL;
6735 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6741 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6742 && impl->browse_files_last_selected_name)
6744 const char *entry_text;
6746 gboolean clear_entry;
6748 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6749 len = strlen (entry_text);
6752 /* The file chooser entry may have appended a "/" to its text. So
6753 * take it out, and compare the result to the old selection.
6755 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6759 tmp = g_strndup (entry_text, len - 1);
6760 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6764 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6767 clear_entry = FALSE;
6770 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6775 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6779 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6783 struct UpdateCurrentFolderData
6785 GtkFileChooserDefault *impl;
6787 gboolean keep_trail;
6788 gboolean clear_entry;
6789 GFile *original_file;
6790 GError *original_error;
6794 update_current_folder_get_info_cb (GCancellable *cancellable,
6796 const GError *error,
6799 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6800 struct UpdateCurrentFolderData *data = user_data;
6801 GtkFileChooserDefault *impl = data->impl;
6803 if (cancellable != impl->update_current_folder_cancellable)
6806 impl->update_current_folder_cancellable = NULL;
6807 impl->reload_state = RELOAD_EMPTY;
6809 set_busy_cursor (impl, FALSE);
6818 if (!data->original_file)
6820 data->original_file = g_object_ref (data->file);
6821 data->original_error = g_error_copy (error);
6824 parent_file = g_file_get_parent (data->file);
6826 /* get parent path and try to change the folder to that */
6829 g_object_unref (data->file);
6830 data->file = parent_file;
6832 g_object_unref (cancellable);
6834 /* restart the update current folder operation */
6835 impl->reload_state = RELOAD_HAS_FOLDER;
6837 impl->update_current_folder_cancellable =
6838 _gtk_file_system_get_info (impl->file_system, data->file,
6840 update_current_folder_get_info_cb,
6843 set_busy_cursor (impl, TRUE);
6849 /* error and bail out */
6850 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6851 g_object_unref (data->original_file);
6857 if (data->original_file)
6859 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6861 g_object_unref (data->original_file);
6864 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6867 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6870 if (impl->current_folder != data->file)
6872 if (impl->current_folder)
6873 g_object_unref (impl->current_folder);
6875 impl->current_folder = g_object_ref (data->file);
6877 impl->reload_state = RELOAD_HAS_FOLDER;
6880 /* Update the widgets that may trigger a folder change themselves. */
6882 if (!impl->changing_folder)
6884 impl->changing_folder = TRUE;
6886 shortcuts_update_current_folder (impl);
6888 impl->changing_folder = FALSE;
6891 /* Set the folder on the save entry */
6893 if (impl->location_entry)
6895 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6896 impl->current_folder);
6898 if (data->clear_entry)
6899 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6902 /* Create a new list model. This is slightly evil; we store the result value
6903 * but perform more actions rather than returning immediately even if it
6904 * generates an error.
6906 set_list_model (impl, NULL);
6908 /* Refresh controls */
6910 shortcuts_find_current_folder (impl);
6912 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6914 check_preview_change (impl);
6915 bookmarks_check_add_sensitivity (impl);
6917 g_signal_emit_by_name (impl, "selection-changed", 0);
6920 g_object_unref (data->file);
6923 g_object_unref (cancellable);
6927 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6929 gboolean keep_trail,
6930 gboolean clear_entry,
6933 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6934 struct UpdateCurrentFolderData *data;
6936 profile_start ("start", NULL);
6938 g_object_ref (file);
6940 switch (impl->operation_mode)
6942 case OPERATION_MODE_SEARCH:
6943 search_switch_to_browse_mode (impl);
6945 case OPERATION_MODE_RECENT:
6946 recent_switch_to_browse_mode (impl);
6948 case OPERATION_MODE_BROWSE:
6952 if (impl->local_only && !g_file_is_native (file))
6954 g_set_error_literal (error,
6955 GTK_FILE_CHOOSER_ERROR,
6956 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6957 _("Cannot change to folder because it is not local"));
6959 g_object_unref (file);
6960 profile_end ("end - not local", NULL);
6964 if (impl->update_current_folder_cancellable)
6965 g_cancellable_cancel (impl->update_current_folder_cancellable);
6967 /* Test validity of path here. */
6968 data = g_new0 (struct UpdateCurrentFolderData, 1);
6970 data->file = g_object_ref (file);
6971 data->keep_trail = keep_trail;
6972 data->clear_entry = clear_entry;
6974 impl->reload_state = RELOAD_HAS_FOLDER;
6976 impl->update_current_folder_cancellable =
6977 _gtk_file_system_get_info (impl->file_system, file,
6979 update_current_folder_get_info_cb,
6982 set_busy_cursor (impl, TRUE);
6983 g_object_unref (file);
6985 profile_end ("end", NULL);
6990 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6992 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6994 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6995 impl->operation_mode == OPERATION_MODE_RECENT)
6998 if (impl->reload_state == RELOAD_EMPTY)
7000 char *current_working_dir;
7003 /* We are unmapped, or we had an error while loading the last folder. We'll return
7004 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7005 * explicitly calls set_current_folder() on us.
7007 current_working_dir = g_get_current_dir ();
7008 file = g_file_new_for_path (current_working_dir);
7009 g_free (current_working_dir);
7013 if (impl->current_folder)
7014 return g_object_ref (impl->current_folder);
7020 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7023 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7025 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7026 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7028 pending_select_files_free (impl);
7029 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7033 select_func (GtkFileSystemModel *model,
7038 GtkFileChooserDefault *impl = user_data;
7039 GtkTreeSelection *selection;
7040 GtkTreeIter sorted_iter;
7042 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7044 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7045 gtk_tree_selection_select_iter (selection, &sorted_iter);
7049 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7053 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7057 parent_file = g_file_get_parent (file);
7060 return _gtk_file_chooser_set_current_folder_file (chooser, file, error);
7062 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7063 impl->operation_mode == OPERATION_MODE_RECENT ||
7064 impl->load_state == LOAD_EMPTY)
7070 g_assert (impl->current_folder != NULL);
7072 same_path = g_file_equal (parent_file, impl->current_folder);
7075 if (same_path && impl->load_state == LOAD_FINISHED)
7080 files.data = (gpointer) file;
7083 result = show_and_select_files (impl, parent_file, &files, error);
7084 g_object_unref (parent_file);
7088 pending_select_files_add (impl, file);
7094 result = _gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7095 g_object_unref (parent_file);
7099 g_object_unref (parent_file);
7104 unselect_func (GtkFileSystemModel *model,
7109 GtkFileChooserDefault *impl = user_data;
7110 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7111 GtkTreePath *sorted_path;
7113 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7115 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7117 gtk_tree_path_free (sorted_path);
7121 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7124 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7126 if (!impl->browse_files_model)
7129 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7130 unselect_func, impl);
7134 maybe_select (GtkTreeModel *model,
7139 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7140 GtkTreeSelection *selection;
7144 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7146 info = get_list_file_info (impl, iter);
7147 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7149 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7150 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7151 gtk_tree_selection_select_iter (selection, iter);
7153 gtk_tree_selection_unselect_iter (selection, iter);
7159 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7161 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7163 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7164 impl->operation_mode == OPERATION_MODE_RECENT)
7166 GtkTreeSelection *selection;
7168 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7169 gtk_tree_selection_select_all (selection);
7173 if (impl->select_multiple)
7174 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7175 maybe_select, impl);
7179 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7181 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7182 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7184 gtk_tree_selection_unselect_all (selection);
7185 pending_select_files_free (impl);
7188 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7190 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7192 * is_empty_ret - whether the file entry is totally empty
7194 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7195 * the path will be "$cwd/foobar")
7198 check_save_entry (GtkFileChooserDefault *impl,
7200 gboolean *is_well_formed_ret,
7201 gboolean *is_empty_ret,
7202 gboolean *is_file_part_empty_ret,
7203 gboolean *is_folder)
7205 GtkFileChooserEntry *chooser_entry;
7206 GFile *current_folder;
7207 const char *file_part;
7211 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7212 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7213 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7214 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7215 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7217 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7219 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7222 *is_well_formed_ret = TRUE;
7223 *is_empty_ret = TRUE;
7224 *is_file_part_empty_ret = TRUE;
7230 *is_empty_ret = FALSE;
7232 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7233 if (!current_folder)
7236 *is_well_formed_ret = FALSE;
7237 *is_file_part_empty_ret = FALSE;
7243 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7245 if (!file_part || file_part[0] == '\0')
7247 *file_ret = g_object_ref (current_folder);
7248 *is_well_formed_ret = TRUE;
7249 *is_file_part_empty_ret = TRUE;
7255 *is_file_part_empty_ret = FALSE;
7258 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7262 error_building_filename_dialog (impl, error);
7264 *is_well_formed_ret = FALSE;
7271 *is_well_formed_ret = TRUE;
7272 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7275 struct get_files_closure {
7276 GtkFileChooserDefault *impl;
7278 GFile *file_from_entry;
7282 get_files_foreach (GtkTreeModel *model,
7287 struct get_files_closure *info;
7289 GtkFileSystemModel *fs_model;
7290 GtkTreeIter sel_iter;
7293 fs_model = info->impl->browse_files_model;
7294 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7296 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7298 return; /* We are on the editable row */
7300 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7301 info->result = g_slist_prepend (info->result, g_object_ref (file));
7305 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7307 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7308 struct get_files_closure info;
7309 GtkWindow *toplevel;
7310 GtkWidget *current_focus;
7311 gboolean file_list_seen;
7313 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7314 return search_get_selected_files (impl);
7316 if (impl->operation_mode == OPERATION_MODE_RECENT)
7317 return recent_get_selected_files (impl);
7321 info.file_from_entry = NULL;
7323 toplevel = get_toplevel (GTK_WIDGET (impl));
7325 current_focus = gtk_window_get_focus (toplevel);
7327 current_focus = NULL;
7329 file_list_seen = FALSE;
7330 if (current_focus == impl->browse_files_tree_view)
7332 GtkTreeSelection *selection;
7336 file_list_seen = TRUE;
7337 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7338 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7340 /* If there is no selection in the file list, we probably have this situation:
7342 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7343 * 2. He then double-clicked on a folder ("bar") in the file list
7345 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7346 * filename entry to see if that is the case.
7348 if (info.result == NULL && impl->location_entry)
7351 else if (impl->location_entry && current_focus == impl->location_entry)
7353 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7357 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7362 if (!is_well_formed)
7365 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7367 g_object_unref (info.file_from_entry);
7371 if (info.file_from_entry)
7372 info.result = g_slist_prepend (info.result, info.file_from_entry);
7373 else if (!file_list_seen)
7378 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7380 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7384 /* The focus is on a dialog's action area button or something else */
7385 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7386 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7394 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7395 * fall back to the current directory */
7396 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7397 info.result == NULL)
7399 GFile *current_folder;
7401 current_folder = _gtk_file_chooser_get_current_folder_file (chooser);
7404 info.result = g_slist_prepend (info.result, current_folder);
7407 return g_slist_reverse (info.result);
7411 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7413 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7415 if (impl->preview_file)
7416 return g_object_ref (impl->preview_file);
7421 static GtkFileSystem *
7422 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7424 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7426 return impl->file_system;
7429 /* Shows or hides the filter widgets */
7431 show_filters (GtkFileChooserDefault *impl,
7435 gtk_widget_show (impl->filter_combo_hbox);
7437 gtk_widget_hide (impl->filter_combo_hbox);
7441 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7442 GtkFileFilter *filter)
7444 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7447 if (g_slist_find (impl->filters, filter))
7449 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7453 g_object_ref_sink (filter);
7454 impl->filters = g_slist_append (impl->filters, filter);
7456 name = gtk_file_filter_get_name (filter);
7458 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7460 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7462 if (!g_slist_find (impl->filters, impl->current_filter))
7463 set_current_filter (impl, filter);
7465 show_filters (impl, TRUE);
7469 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7470 GtkFileFilter *filter)
7472 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7473 GtkTreeModel *model;
7477 filter_index = g_slist_index (impl->filters, filter);
7479 if (filter_index < 0)
7481 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7485 impl->filters = g_slist_remove (impl->filters, filter);
7487 if (filter == impl->current_filter)
7490 set_current_filter (impl, impl->filters->data);
7492 set_current_filter (impl, NULL);
7495 /* Remove row from the combo box */
7496 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7497 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7498 g_assert_not_reached ();
7500 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7502 g_object_unref (filter);
7505 show_filters (impl, FALSE);
7509 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7511 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7513 return g_slist_copy (impl->filters);
7516 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7518 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7521 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7524 struct AddShortcutData
7526 GtkFileChooserDefault *impl;
7531 add_shortcut_get_info_cb (GCancellable *cancellable,
7533 const GError *error,
7537 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7538 struct AddShortcutData *data = user_data;
7540 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7543 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7545 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7548 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7550 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7553 g_object_unref (data->impl);
7554 g_object_unref (data->file);
7557 g_object_unref (cancellable);
7561 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7565 GCancellable *cancellable;
7566 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7567 struct AddShortcutData *data;
7571 /* Avoid adding duplicates */
7572 pos = shortcut_find_position (impl, file);
7573 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7577 uri = g_file_get_uri (file);
7578 /* translators, "Shortcut" means "Bookmark" here */
7580 GTK_FILE_CHOOSER_ERROR,
7581 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7582 _("Shortcut %s already exists"),
7589 for (l = impl->loading_shortcuts; l; l = l->next)
7591 GCancellable *c = l->data;
7594 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7595 if (f && g_file_equal (file, f))
7599 uri = g_file_get_uri (file);
7601 GTK_FILE_CHOOSER_ERROR,
7602 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7603 _("Shortcut %s already exists"),
7611 data = g_new0 (struct AddShortcutData, 1);
7612 data->impl = g_object_ref (impl);
7613 data->file = g_object_ref (file);
7615 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7617 add_shortcut_get_info_cb, data);
7622 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7623 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7629 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7633 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7640 for (l = impl->loading_shortcuts; l; l = l->next)
7642 GCancellable *c = l->data;
7645 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7646 if (f && g_file_equal (file, f))
7648 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7649 g_cancellable_cancel (c);
7654 if (impl->num_shortcuts == 0)
7657 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7658 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7659 g_assert_not_reached ();
7661 for (i = 0; i < impl->num_shortcuts; i++)
7664 ShortcutType shortcut_type;
7667 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7668 SHORTCUTS_COL_DATA, &col_data,
7669 SHORTCUTS_COL_TYPE, &shortcut_type,
7671 g_assert (col_data != NULL);
7672 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7674 shortcut = col_data;
7675 if (g_file_equal (shortcut, file))
7677 shortcuts_remove_rows (impl, pos + i, 1);
7678 impl->num_shortcuts--;
7682 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7683 g_assert_not_reached ();
7688 uri = g_file_get_uri (file);
7689 /* translators, "Shortcut" means "Bookmark" here */
7691 GTK_FILE_CHOOSER_ERROR,
7692 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7693 _("Shortcut %s does not exist"),
7701 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7703 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7709 if (impl->num_shortcuts == 0)
7712 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7713 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7714 g_assert_not_reached ();
7718 for (i = 0; i < impl->num_shortcuts; i++)
7721 ShortcutType shortcut_type;
7724 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7725 SHORTCUTS_COL_DATA, &col_data,
7726 SHORTCUTS_COL_TYPE, &shortcut_type,
7728 g_assert (col_data != NULL);
7729 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7731 shortcut = col_data;
7732 list = g_slist_prepend (list, g_object_ref (shortcut));
7734 if (i != impl->num_shortcuts - 1)
7736 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7737 g_assert_not_reached ();
7741 return g_slist_reverse (list);
7744 /* Guesses a size based upon font sizes */
7746 find_good_size_from_style (GtkWidget *widget,
7750 GtkFileChooserDefault *impl;
7755 g_assert (widget->style != NULL);
7756 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7758 if (impl->default_width == 0 &&
7759 impl->default_height == 0)
7761 screen = gtk_widget_get_screen (widget);
7764 resolution = gdk_screen_get_resolution (screen);
7765 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7769 resolution = 96.0; /* wheeee */
7771 font_size = pango_font_description_get_size (widget->style->font_desc);
7772 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7774 impl->default_width = font_size * NUM_CHARS;
7775 impl->default_height = font_size * NUM_LINES;
7778 *width = impl->default_width;
7779 *height = impl->default_height;
7783 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7784 gint *default_width,
7785 gint *default_height)
7787 GtkFileChooserDefault *impl;
7790 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7791 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7793 if (impl->preview_widget_active &&
7794 impl->preview_widget &&
7795 GTK_WIDGET_VISIBLE (impl->preview_widget))
7797 gtk_widget_size_request (impl->preview_box, &req);
7798 *default_width += PREVIEW_HBOX_SPACING + req.width;
7801 if (impl->extra_widget &&
7802 GTK_WIDGET_VISIBLE (impl->extra_widget))
7804 gtk_widget_size_request (impl->extra_align, &req);
7805 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7809 struct switch_folder_closure {
7810 GtkFileChooserDefault *impl;
7815 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7817 switch_folder_foreach_cb (GtkTreeModel *model,
7822 struct switch_folder_closure *closure;
7823 GtkTreeIter child_iter;
7827 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7829 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7830 closure->num_selected++;
7833 /* Changes to the selected folder in the list view */
7835 switch_to_selected_folder (GtkFileChooserDefault *impl)
7837 GtkTreeSelection *selection;
7838 struct switch_folder_closure closure;
7840 /* We do this with foreach() rather than get_selected() as we may be in
7841 * multiple selection mode
7844 closure.impl = impl;
7845 closure.file = NULL;
7846 closure.num_selected = 0;
7848 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7849 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7851 g_assert (closure.file && closure.num_selected == 1);
7853 change_folder_and_display_error (impl, closure.file, FALSE);
7856 /* Gets the GFileInfo for the selected row in the file list; assumes single
7860 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7861 gboolean *had_selection)
7863 GtkTreeSelection *selection;
7864 GtkTreeIter iter, child_iter;
7867 g_assert (!impl->select_multiple);
7868 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7869 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7871 *had_selection = FALSE;
7875 *had_selection = TRUE;
7877 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7881 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7885 /* Gets the display name of the selected file in the file list; assumes single
7886 * selection mode and that something is selected.
7888 static const gchar *
7889 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7892 gboolean had_selection;
7894 info = get_selected_file_info_from_file_list (impl, &had_selection);
7895 g_assert (had_selection);
7896 g_assert (info != NULL);
7898 return g_file_info_get_display_name (info);
7902 add_custom_button_to_dialog (GtkDialog *dialog,
7903 const gchar *mnemonic_label,
7904 const gchar *stock_id,
7909 button = gtk_button_new_with_mnemonic (mnemonic_label);
7910 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7911 gtk_button_set_image (GTK_BUTTON (button),
7912 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7913 gtk_widget_show (button);
7915 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7918 /* Presents an overwrite confirmation dialog; returns whether we should accept
7922 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7923 const gchar *file_part,
7924 const gchar *folder_display_name)
7926 GtkWindow *toplevel;
7930 toplevel = get_toplevel (GTK_WIDGET (impl));
7932 dialog = gtk_message_dialog_new (toplevel,
7933 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7934 GTK_MESSAGE_QUESTION,
7936 _("A file named \"%s\" already exists. Do you want to replace it?"),
7938 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7939 _("The file already exists in \"%s\". Replacing it will "
7940 "overwrite its contents."),
7941 folder_display_name);
7943 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7944 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7945 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7946 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7947 GTK_RESPONSE_ACCEPT,
7948 GTK_RESPONSE_CANCEL,
7950 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7952 if (toplevel->group)
7953 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7955 response = gtk_dialog_run (GTK_DIALOG (dialog));
7957 gtk_widget_destroy (dialog);
7959 return (response == GTK_RESPONSE_ACCEPT);
7962 struct GetDisplayNameData
7964 GtkFileChooserDefault *impl;
7969 confirmation_confirm_get_info_cb (GCancellable *cancellable,
7971 const GError *error,
7974 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7975 gboolean should_respond = FALSE;
7976 struct GetDisplayNameData *data = user_data;
7978 if (cancellable != data->impl->should_respond_get_info_cancellable)
7981 data->impl->should_respond_get_info_cancellable = NULL;
7987 /* Huh? Did the folder disappear? Let the caller deal with it */
7988 should_respond = TRUE;
7990 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
7992 set_busy_cursor (data->impl, FALSE);
7994 g_signal_emit_by_name (data->impl, "response-requested");
7997 g_object_unref (data->impl);
7998 g_free (data->file_part);
8001 g_object_unref (cancellable);
8004 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8005 * should respond. Can get the file part from the file list or the save entry.
8008 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8009 const gchar *file_part,
8012 GtkFileChooserConfirmation conf;
8014 if (!impl->do_overwrite_confirmation)
8017 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8019 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8023 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8025 struct GetDisplayNameData *data;
8027 g_assert (file_part != NULL);
8029 data = g_new0 (struct GetDisplayNameData, 1);
8030 data->impl = g_object_ref (impl);
8031 data->file_part = g_strdup (file_part);
8033 if (impl->should_respond_get_info_cancellable)
8034 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8036 impl->should_respond_get_info_cancellable =
8037 _gtk_file_system_get_info (impl->file_system, parent_file,
8038 "standard::display-name",
8039 confirmation_confirm_get_info_cb,
8041 set_busy_cursor (data->impl, TRUE);
8045 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8048 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8052 g_assert_not_reached ();
8057 struct FileExistsData
8059 GtkFileChooserDefault *impl;
8060 gboolean file_exists_and_is_not_folder;
8066 save_entry_get_info_cb (GCancellable *cancellable,
8068 const GError *error,
8071 gboolean parent_is_folder;
8072 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8073 struct FileExistsData *data = user_data;
8075 if (cancellable != data->impl->should_respond_get_info_cancellable)
8078 data->impl->should_respond_get_info_cancellable = NULL;
8080 set_busy_cursor (data->impl, FALSE);
8086 parent_is_folder = FALSE;
8088 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8090 if (parent_is_folder)
8092 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8094 if (data->file_exists_and_is_not_folder)
8097 const char *file_part;
8099 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8100 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8103 g_signal_emit_by_name (data->impl, "response-requested");
8106 g_signal_emit_by_name (data->impl, "response-requested");
8108 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8110 GError *error = NULL;
8112 set_busy_cursor (data->impl, TRUE);
8113 g_file_make_directory (data->file, NULL, &error);
8114 set_busy_cursor (data->impl, FALSE);
8117 g_signal_emit_by_name (data->impl, "response-requested");
8119 error_creating_folder_dialog (data->impl, data->file, error);
8124 /* This will display an error, which is what we want */
8125 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8129 g_object_unref (data->impl);
8130 g_object_unref (data->file);
8131 g_object_unref (data->parent_file);
8134 g_object_unref (cancellable);
8138 file_exists_get_info_cb (GCancellable *cancellable,
8140 const GError *error,
8143 gboolean data_ownership_taken = FALSE;
8144 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8145 gboolean file_exists_and_is_not_folder;
8146 struct FileExistsData *data = user_data;
8148 if (cancellable != data->impl->file_exists_get_info_cancellable)
8151 data->impl->file_exists_get_info_cancellable = NULL;
8153 set_busy_cursor (data->impl, FALSE);
8158 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8160 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8161 /* user typed a filename; we are done */
8162 g_signal_emit_by_name (data->impl, "response-requested");
8163 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8164 && file_exists_and_is_not_folder)
8166 /* Oops, the user typed the name of an existing path which is not
8169 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8170 g_error_copy (error));
8174 /* check that everything up to the last component exists */
8176 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8177 data_ownership_taken = TRUE;
8179 if (data->impl->should_respond_get_info_cancellable)
8180 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8182 data->impl->should_respond_get_info_cancellable =
8183 _gtk_file_system_get_info (data->impl->file_system,
8186 save_entry_get_info_cb,
8188 set_busy_cursor (data->impl, TRUE);
8192 if (!data_ownership_taken)
8194 g_object_unref (data->impl);
8195 g_object_unref (data->file);
8196 g_object_unref (data->parent_file);
8200 g_object_unref (cancellable);
8204 paste_text_received (GtkClipboard *clipboard,
8206 GtkFileChooserDefault *impl)
8213 file = g_file_new_for_uri (text);
8215 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8216 location_popup_handler (impl, text);
8218 g_object_unref (file);
8221 /* Handler for the "location-popup-on-paste" keybinding signal */
8223 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8225 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8226 GDK_SELECTION_CLIPBOARD);
8227 gtk_clipboard_request_text (clipboard,
8228 (GtkClipboardTextReceivedFunc) paste_text_received,
8233 /* Implementation for GtkFileChooserEmbed::should_respond() */
8235 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8237 GtkFileChooserDefault *impl;
8238 GtkWidget *toplevel;
8239 GtkWidget *current_focus;
8241 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8243 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8244 g_assert (GTK_IS_WINDOW (toplevel));
8246 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8248 if (current_focus == impl->browse_files_tree_view)
8250 /* The following array encodes what we do based on the impl->action and the
8251 * number of files selected.
8254 NOOP, /* Do nothing (don't respond) */
8255 RESPOND, /* Respond immediately */
8256 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8257 ALL_FILES, /* Respond only if everything selected is a file */
8258 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8259 SAVE_ENTRY, /* Go to the code for handling the save entry */
8260 NOT_REACHED /* Sanity check */
8262 static const ActionToTake what_to_do[4][3] = {
8263 /* 0 selected 1 selected many selected */
8264 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8265 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8266 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8267 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8271 gboolean all_files, all_folders;
8273 ActionToTake action;
8277 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8279 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8280 return search_should_respond (impl);
8282 if (impl->operation_mode == OPERATION_MODE_RECENT)
8283 return recent_should_respond (impl);
8285 selection_check (impl, &num_selected, &all_files, &all_folders);
8287 if (num_selected > 2)
8292 action = what_to_do [impl->action] [k];
8302 case RESPOND_OR_SWITCH:
8303 g_assert (num_selected == 1);
8307 switch_to_selected_folder (impl);
8310 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8311 return should_respond_after_confirm_overwrite (impl,
8312 get_display_name_from_file_list (impl),
8313 impl->current_folder);
8327 g_assert_not_reached ();
8330 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8333 gboolean is_well_formed, is_empty, is_file_part_empty;
8336 GtkFileChooserEntry *entry;
8341 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8342 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8343 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8344 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8345 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8347 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8348 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8350 if (is_empty || !is_well_formed)
8353 g_assert (file != NULL);
8358 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8359 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8361 change_folder_and_display_error (impl, file, TRUE);
8364 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8365 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8367 /* The folder already exists, so we do not need to create it.
8368 * Just respond to terminate the dialog.
8374 g_assert_not_reached ();
8380 struct FileExistsData *data;
8382 /* We need to check whether file exists and is not a folder */
8384 data = g_new0 (struct FileExistsData, 1);
8385 data->impl = g_object_ref (impl);
8386 data->file = g_object_ref (file);
8387 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8389 if (impl->file_exists_get_info_cancellable)
8390 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8392 impl->file_exists_get_info_cancellable =
8393 _gtk_file_system_get_info (impl->file_system, file,
8395 file_exists_get_info_cb,
8398 set_busy_cursor (impl, TRUE);
8402 g_error_free (error);
8405 g_object_unref (file);
8408 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8410 /* The focus is on a dialog's action area button, *and* the widget that
8411 * was focused immediately before it is the file list.
8415 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8417 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8420 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8422 /* The focus is on a dialog's action area button, *and* the widget that
8423 * was focused immediately before it is the location entry.
8428 /* The focus is on a dialog's action area button or something else */
8429 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8430 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8435 g_assert_not_reached ();
8439 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8441 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8443 GtkFileChooserDefault *impl;
8446 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8448 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8449 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8451 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8452 widget = impl->browse_files_tree_view;
8454 widget = impl->location_entry;
8456 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8457 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8458 widget = impl->location_entry;
8461 g_assert_not_reached ();
8465 g_assert (widget != NULL);
8466 gtk_widget_grab_focus (widget);
8469 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8471 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8481 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8482 *list = g_slist_prepend (*list, file);
8485 /* Constructs a list of the selected paths in search mode */
8487 search_get_selected_files (GtkFileChooserDefault *impl)
8490 GtkTreeSelection *selection;
8494 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8495 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8496 result = g_slist_reverse (result);
8501 /* Called from ::should_respond(). We return whether there are selected files
8502 * in the search list.
8505 search_should_respond (GtkFileChooserDefault *impl)
8507 GtkTreeSelection *selection;
8509 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8511 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8512 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8515 struct SearchHitInsertRequest
8517 GtkFileChooserDefault *impl;
8519 GtkTreeRowReference *row_ref;
8523 search_hit_get_info_cb (GCancellable *cancellable,
8525 const GError *error,
8528 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8529 GdkPixbuf *pixbuf = NULL;
8532 GCancellable *model_cancellable;
8533 gboolean is_folder = FALSE;
8536 struct SearchHitInsertRequest *request = data;
8538 if (!request->impl->search_model)
8541 path = gtk_tree_row_reference_get_path (request->row_ref);
8545 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8547 gtk_tree_path_free (path);
8549 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8550 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8552 if (cancellable != model_cancellable)
8555 /* set the cancellable to NULL in the model */
8556 gtk_list_store_set (request->impl->search_model, &iter,
8557 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8565 gtk_list_store_remove (request->impl->search_model, &iter);
8569 display_name = g_strdup (g_file_info_get_display_name (info));
8570 mime_type = g_strdup (g_file_info_get_content_type (info));
8571 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8572 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8573 request->impl->icon_size);
8575 gtk_list_store_set (request->impl->search_model, &iter,
8576 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8577 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8578 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8579 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8583 g_object_unref (pixbuf);
8586 g_object_unref (request->impl);
8587 g_object_unref (request->file);
8588 gtk_tree_row_reference_free (request->row_ref);
8591 g_object_unref (cancellable);
8594 /* Adds one hit from the search engine to the search_model */
8596 search_add_hit (GtkFileChooserDefault *impl,
8602 char *collation_key;
8603 struct stat statbuf;
8604 struct stat *statbuf_copy;
8607 GCancellable *cancellable;
8608 struct SearchHitInsertRequest *request;
8610 file = g_file_new_for_uri (uri);
8612 if (!g_file_is_native (file))
8614 g_object_unref (file);
8618 filename = g_file_get_path (file);
8620 if (stat (filename, &statbuf) != 0)
8622 g_object_unref (file);
8627 statbuf_copy = g_new (struct stat, 1);
8628 *statbuf_copy = statbuf;
8630 tmp = g_file_get_parse_name (file);
8631 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8634 request = g_new0 (struct SearchHitInsertRequest, 1);
8635 request->impl = g_object_ref (impl);
8636 request->file = g_object_ref (file);
8638 gtk_list_store_append (impl->search_model, &iter);
8639 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8641 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8642 gtk_tree_path_free (p);
8644 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8645 "standard::type,standard::icon,"
8646 "standard::content-type,standard::display-name",
8647 search_hit_get_info_cb,
8650 gtk_list_store_set (impl->search_model, &iter,
8651 SEARCH_MODEL_COL_FILE, file,
8652 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8653 SEARCH_MODEL_COL_STAT, statbuf_copy,
8654 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8657 g_object_unref (file);
8661 /* Callback used from GtkSearchEngine when we get new hits */
8663 search_engine_hits_added_cb (GtkSearchEngine *engine,
8667 GtkFileChooserDefault *impl;
8670 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8672 for (l = hits; l; l = l->next)
8673 search_add_hit (impl, (gchar*)l->data);
8676 /* Callback used from GtkSearchEngine when the query is done running */
8678 search_engine_finished_cb (GtkSearchEngine *engine,
8681 GtkFileChooserDefault *impl;
8683 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8686 /* EB: setting the model here will avoid loads of row events,
8687 * but it'll make the search look like blocked.
8689 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8690 GTK_TREE_MODEL (impl->search_model_filter));
8693 /* FMQ: if search was empty, say that we got no hits */
8694 set_busy_cursor (impl, FALSE);
8697 /* Displays a generic error when we cannot create a GtkSearchEngine.
8698 * It would be better if _gtk_search_engine_new() gave us a GError
8699 * with a better message, but it doesn't do that right now.
8702 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8704 error_message (impl,
8705 _("Could not start the search process"),
8706 _("The program was not able to create a connection to the indexer "
8707 "daemon. Please make sure it is running."));
8711 search_engine_error_cb (GtkSearchEngine *engine,
8712 const gchar *message,
8715 GtkFileChooserDefault *impl;
8717 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8719 search_stop_searching (impl, TRUE);
8720 error_message (impl, _("Could not send the search request"), message);
8722 set_busy_cursor (impl, FALSE);
8725 /* Frees the data in the search_model */
8727 search_clear_model (GtkFileChooserDefault *impl,
8728 gboolean remove_from_treeview)
8730 GtkTreeModel *model;
8733 if (!impl->search_model)
8736 model = GTK_TREE_MODEL (impl->search_model);
8738 if (gtk_tree_model_get_iter_first (model, &iter))
8741 GCancellable *cancellable;
8743 gtk_tree_model_get (model, &iter,
8744 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8748 g_cancellable_cancel (cancellable);
8750 while (gtk_tree_model_iter_next (model, &iter));
8752 g_object_unref (impl->search_model);
8753 impl->search_model = NULL;
8755 g_object_unref (impl->search_model_filter);
8756 impl->search_model_filter = NULL;
8758 g_object_unref (impl->search_model_sort);
8759 impl->search_model_sort = NULL;
8761 if (remove_from_treeview)
8762 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8765 /* Stops any ongoing searches; does not touch the search_model */
8767 search_stop_searching (GtkFileChooserDefault *impl,
8768 gboolean remove_query)
8770 if (remove_query && impl->search_query)
8772 g_object_unref (impl->search_query);
8773 impl->search_query = NULL;
8776 if (impl->search_engine)
8778 _gtk_search_engine_stop (impl->search_engine);
8780 g_object_unref (impl->search_engine);
8781 impl->search_engine = NULL;
8785 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8787 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8789 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8791 search_stop_searching (impl, FALSE);
8792 search_clear_model (impl, TRUE);
8794 gtk_widget_destroy (impl->search_hbox);
8795 impl->search_hbox = NULL;
8796 impl->search_entry = NULL;
8798 gtk_widget_show (impl->browse_path_bar);
8799 gtk_widget_show (impl->browse_new_folder_button);
8801 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8802 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8804 gtk_widget_show (impl->location_button);
8806 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8807 gtk_widget_show (impl->location_entry_box);
8810 impl->operation_mode = OPERATION_MODE_BROWSE;
8812 file_list_set_sort_column_ids (impl);
8815 /* Sort callback from the path column */
8817 search_column_path_sort_func (GtkTreeModel *model,
8822 GtkFileChooserDefault *impl = user_data;
8823 GtkTreeIter child_a, child_b;
8824 const char *collation_key_a, *collation_key_b;
8825 gboolean is_folder_a, is_folder_b;
8827 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8828 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8830 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8831 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8832 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8834 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8835 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8836 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8839 if (!collation_key_a)
8842 if (!collation_key_b)
8845 /* always show folders first */
8846 if (is_folder_a != is_folder_b)
8847 return is_folder_a ? 1 : -1;
8849 return strcmp (collation_key_a, collation_key_b);
8852 /* Sort callback from the modification time column */
8854 search_column_mtime_sort_func (GtkTreeModel *model,
8859 GtkFileChooserDefault *impl = user_data;
8860 GtkTreeIter child_a, child_b;
8861 const struct stat *statbuf_a, *statbuf_b;
8862 gboolean is_folder_a, is_folder_b;
8864 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8865 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8867 /* Note that although we store a whole struct stat in the model, we only
8868 * compare the mtime here. If we add another column relative to a struct stat
8869 * (e.g. a file size column), we'll want another sort callback similar to this
8872 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8873 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8874 SEARCH_MODEL_COL_STAT, &statbuf_a,
8876 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8877 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8878 SEARCH_MODEL_COL_STAT, &statbuf_b,
8887 if (is_folder_a != is_folder_b)
8888 return is_folder_a ? 1 : -1;
8890 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8892 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8899 search_get_is_filtered (GtkFileChooserDefault *impl,
8901 const gchar *display_name,
8902 const gchar *mime_type)
8904 GtkFileFilterInfo filter_info;
8905 GtkFileFilterFlags needed;
8908 if (!impl->current_filter)
8911 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8912 needed = gtk_file_filter_get_needed (impl->current_filter);
8914 filter_info.display_name = display_name;
8915 filter_info.mime_type = mime_type;
8917 if (needed & GTK_FILE_FILTER_FILENAME)
8919 filter_info.filename = g_file_get_path (file);
8920 if (filter_info.filename)
8921 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8924 filter_info.filename = NULL;
8926 if (needed & GTK_FILE_FILTER_URI)
8928 filter_info.uri = g_file_get_uri (file);
8929 if (filter_info.uri)
8930 filter_info.contains |= GTK_FILE_FILTER_URI;
8933 filter_info.uri = NULL;
8935 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8937 if (filter_info.filename)
8938 g_free ((gchar *) filter_info.filename);
8939 if (filter_info.uri)
8940 g_free ((gchar *) filter_info.uri);
8946 /* Visibility function for the recent filter model */
8948 search_model_visible_func (GtkTreeModel *model,
8952 GtkFileChooserDefault *impl = user_data;
8954 gchar *display_name, *mime_type;
8957 if (!impl->current_filter)
8960 gtk_tree_model_get (model, iter,
8961 SEARCH_MODEL_COL_FILE, &file,
8962 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
8963 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8964 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8973 return !search_get_is_filtered (impl, file, display_name, mime_type);
8976 /* Creates the search_model and puts it in the tree view */
8978 search_setup_model (GtkFileChooserDefault *impl)
8980 g_assert (impl->search_model == NULL);
8981 g_assert (impl->search_model_filter == NULL);
8982 g_assert (impl->search_model_sort == NULL);
8984 /* We store these columns in the search model:
8986 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
8987 * as a pointer not as a G_TYPE_FILE
8988 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
8989 * as a pointer not as a G_TYPE_STRING
8990 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
8991 * as a pointer not as a G_TYPE_STRING
8992 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
8993 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
8994 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
8995 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
8996 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
8998 * Keep this in sync with the enumeration defined near the beginning
9001 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9011 impl->search_model_filter =
9012 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9013 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9014 search_model_visible_func,
9017 impl->search_model_sort =
9018 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9019 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9020 SEARCH_MODEL_COL_FILE,
9021 search_column_path_sort_func,
9023 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9024 SEARCH_MODEL_COL_STAT,
9025 search_column_mtime_sort_func,
9027 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9028 SEARCH_MODEL_COL_STAT,
9029 GTK_SORT_DESCENDING);
9031 /* EB: setting the model here will make the hits list update feel
9032 * more "alive" than setting the model at the end of the search
9035 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9036 GTK_TREE_MODEL (impl->search_model_sort));
9040 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9041 GtkTreeIter *child_iter,
9046 if (!impl->search_model)
9049 if (!impl->search_model_filter || !impl->search_model_sort)
9052 /* pass 1: get the iterator in the filter model */
9053 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9056 /* pass 2: get the iterator in the real model */
9057 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9058 child_iter, &middle);
9061 /* Creates a new query with the specified text and launches it */
9063 search_start_query (GtkFileChooserDefault *impl,
9064 const gchar *query_text)
9066 search_stop_searching (impl, FALSE);
9067 search_clear_model (impl, TRUE);
9068 search_setup_model (impl);
9069 set_busy_cursor (impl, TRUE);
9071 if (impl->search_engine == NULL)
9072 impl->search_engine = _gtk_search_engine_new ();
9074 if (!impl->search_engine)
9076 set_busy_cursor (impl, FALSE);
9077 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9081 if (!impl->search_query)
9083 impl->search_query = _gtk_query_new ();
9084 _gtk_query_set_text (impl->search_query, query_text);
9087 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9089 g_signal_connect (impl->search_engine, "hits-added",
9090 G_CALLBACK (search_engine_hits_added_cb), impl);
9091 g_signal_connect (impl->search_engine, "finished",
9092 G_CALLBACK (search_engine_finished_cb), impl);
9093 g_signal_connect (impl->search_engine, "error",
9094 G_CALLBACK (search_engine_error_cb), impl);
9096 _gtk_search_engine_start (impl->search_engine);
9099 /* Callback used when the user presses Enter while typing on the search
9100 * entry; starts the query
9103 search_entry_activate_cb (GtkEntry *entry,
9106 GtkFileChooserDefault *impl;
9109 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9111 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9112 if (strlen (text) == 0)
9115 /* reset any existing query object */
9116 if (impl->search_query)
9118 g_object_unref (impl->search_query);
9119 impl->search_query = NULL;
9122 search_start_query (impl, text);
9125 /* Hides the path bar and creates the search entry */
9127 search_setup_widgets (GtkFileChooserDefault *impl)
9131 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9135 label = gtk_label_new_with_mnemonic (_("_Search:"));
9136 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9140 impl->search_entry = gtk_entry_new ();
9141 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9142 g_signal_connect (impl->search_entry, "activate",
9143 G_CALLBACK (search_entry_activate_cb),
9145 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9147 /* if there already is a query, restart it */
9148 if (impl->search_query)
9150 gchar *query = _gtk_query_get_text (impl->search_query);
9154 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9155 search_start_query (impl, query);
9161 g_object_unref (impl->search_query);
9162 impl->search_query = NULL;
9166 gtk_widget_hide (impl->browse_path_bar);
9167 gtk_widget_hide (impl->browse_new_folder_button);
9169 /* Box for search widgets */
9170 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9171 gtk_widget_show_all (impl->search_hbox);
9173 /* Hide the location widgets temporarily */
9175 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9176 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9178 gtk_widget_hide (impl->location_button);
9179 gtk_widget_hide (impl->location_entry_box);
9182 gtk_widget_grab_focus (impl->search_entry);
9184 /* FMQ: hide the filter combo? */
9187 /* Main entry point to the searching functions; this gets called when the user
9188 * activates the Search shortcut.
9191 search_activate (GtkFileChooserDefault *impl)
9193 OperationMode previous_mode;
9195 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9197 gtk_widget_grab_focus (impl->search_entry);
9201 previous_mode = impl->operation_mode;
9202 impl->operation_mode = OPERATION_MODE_SEARCH;
9204 switch (previous_mode)
9206 case OPERATION_MODE_RECENT:
9207 recent_stop_loading (impl);
9208 recent_clear_model (impl, TRUE);
9211 case OPERATION_MODE_BROWSE:
9212 stop_loading_and_clear_list_model (impl);
9215 case OPERATION_MODE_SEARCH:
9216 g_assert_not_reached ();
9220 g_assert (impl->search_hbox == NULL);
9221 g_assert (impl->search_entry == NULL);
9222 g_assert (impl->search_model == NULL);
9223 g_assert (impl->search_model_filter == NULL);
9225 search_setup_widgets (impl);
9226 file_list_set_sort_column_ids (impl);
9230 * Recent files support
9233 /* Frees the data in the recent_model */
9235 recent_clear_model (GtkFileChooserDefault *impl,
9236 gboolean remove_from_treeview)
9238 GtkTreeModel *model;
9241 if (!impl->recent_model)
9244 model = GTK_TREE_MODEL (impl->recent_model);
9246 if (remove_from_treeview)
9247 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9249 if (gtk_tree_model_get_iter_first (model, &iter))
9254 GCancellable *cancellable;
9255 GtkRecentInfo *recent_info;
9256 gchar *display_name;
9258 gtk_tree_model_get (model, &iter,
9259 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9260 RECENT_MODEL_COL_FILE, &file,
9261 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9262 RECENT_MODEL_COL_INFO, &recent_info,
9266 g_cancellable_cancel (cancellable);
9268 g_object_unref (file);
9269 gtk_recent_info_unref (recent_info);
9270 g_free (display_name);
9272 while (gtk_tree_model_iter_next (model, &iter));
9275 g_object_unref (impl->recent_model);
9276 impl->recent_model = NULL;
9278 g_object_unref (impl->recent_model_filter);
9279 impl->recent_model_filter = NULL;
9281 g_object_unref (impl->recent_model_sort);
9282 impl->recent_model_sort = NULL;
9285 /* Stops any ongoing loading of the recent files list; does
9286 * not touch the recent_model
9289 recent_stop_loading (GtkFileChooserDefault *impl)
9291 if (impl->load_recent_id)
9293 g_source_remove (impl->load_recent_id);
9294 impl->load_recent_id = 0;
9298 /* Stops any pending load, clears the file list, and switches
9299 * back to OPERATION_MODE_BROWSE
9302 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9304 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9306 recent_stop_loading (impl);
9307 recent_clear_model (impl, TRUE);
9309 gtk_widget_show (impl->browse_path_bar);
9310 gtk_widget_show (impl->browse_new_folder_button);
9312 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9313 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9315 gtk_widget_show (impl->location_button);
9317 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9318 gtk_widget_show (impl->location_entry_box);
9321 impl->operation_mode = OPERATION_MODE_BROWSE;
9323 file_list_set_sort_column_ids (impl);
9326 /* Sort callback from the modification time column */
9328 recent_column_mtime_sort_func (GtkTreeModel *model,
9333 GtkFileChooserDefault *impl = user_data;
9334 GtkTreeIter child_a, child_b;
9335 GtkRecentInfo *info_a, *info_b;
9336 gboolean is_folder_a, is_folder_b;
9338 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9339 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9341 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9342 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9343 RECENT_MODEL_COL_INFO, &info_a,
9345 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9346 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9347 RECENT_MODEL_COL_INFO, &info_b,
9356 /* folders always go first */
9357 if (is_folder_a != is_folder_b)
9358 return is_folder_a ? 1 : -1;
9360 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9362 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9369 recent_column_path_sort_func (GtkTreeModel *model,
9374 GtkFileChooserDefault *impl = user_data;
9375 GtkTreeIter child_a, child_b;
9376 gboolean is_folder_a, is_folder_b;
9377 gchar *name_a, *name_b;
9379 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9380 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9382 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9383 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9384 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9386 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9387 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9388 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9397 if (is_folder_a != is_folder_b)
9398 return is_folder_a ? 1 : -1;
9400 return strcmp (name_a, name_b);
9404 recent_get_is_filtered (GtkFileChooserDefault *impl,
9406 GtkRecentInfo *recent_info)
9408 GtkFileFilterInfo filter_info;
9409 GtkFileFilterFlags needed;
9412 if (!impl->current_filter)
9415 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9416 needed = gtk_file_filter_get_needed (impl->current_filter);
9418 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9419 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9421 if (needed & GTK_FILE_FILTER_FILENAME)
9423 filter_info.filename = g_file_get_path (file);
9424 if (filter_info.filename)
9425 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9428 filter_info.filename = NULL;
9430 if (needed & GTK_FILE_FILTER_URI)
9432 filter_info.uri = g_file_get_uri (file);
9433 if (filter_info.uri)
9434 filter_info.contains |= GTK_FILE_FILTER_URI;
9437 filter_info.uri = NULL;
9439 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9441 if (filter_info.filename)
9442 g_free ((gchar *) filter_info.filename);
9443 if (filter_info.uri)
9444 g_free ((gchar *) filter_info.uri);
9449 /* Visibility function for the recent filter model */
9451 recent_model_visible_func (GtkTreeModel *model,
9455 GtkFileChooserDefault *impl = user_data;
9457 GtkRecentInfo *recent_info;
9460 if (!impl->current_filter)
9463 gtk_tree_model_get (model, iter,
9464 RECENT_MODEL_COL_INFO, &recent_info,
9465 RECENT_MODEL_COL_FILE, &file,
9466 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9475 return !recent_get_is_filtered (impl, file, recent_info);
9479 recent_setup_model (GtkFileChooserDefault *impl)
9481 g_assert (impl->recent_model == NULL);
9482 g_assert (impl->recent_model_filter == NULL);
9483 g_assert (impl->recent_model_sort == NULL);
9485 /* We store these columns in the search model:
9487 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9488 * stored as a pointer and not as a G_TYPE_FILE;
9489 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9490 * stored as a pointer and not as a G_TYPE_STRING;
9491 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9492 * as a GTK_TYPE_RECENT_INFO;
9493 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9494 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9495 * and not as a G_TYPE_CANCELLABLE;
9497 * Keep this in sync with the enumeration defined near the beginning of
9500 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9507 impl->recent_model_filter =
9508 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9509 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9510 recent_model_visible_func,
9514 /* this is the model that will actually be added to
9515 * the browse_files_tree_view widget; remember: we are
9516 * stuffing the real model into a filter model and then
9517 * into a sort model; this means we'll have to translate
9518 * the child iterator *twice* to get from a path or an
9519 * iterator coming from the tree view widget to the
9520 * real data inside the model.
9522 impl->recent_model_sort =
9523 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9524 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9525 RECENT_MODEL_COL_FILE,
9526 recent_column_path_sort_func,
9528 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9529 RECENT_MODEL_COL_INFO,
9530 recent_column_mtime_sort_func,
9532 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9533 RECENT_MODEL_COL_INFO,
9534 GTK_SORT_DESCENDING);
9539 GtkFileChooserDefault *impl;
9542 gint n_loaded_items;
9543 guint needs_sorting : 1;
9547 recent_idle_cleanup (gpointer data)
9549 RecentLoadData *load_data = data;
9550 GtkFileChooserDefault *impl = load_data->impl;
9552 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9553 GTK_TREE_MODEL (impl->recent_model_sort));
9555 set_busy_cursor (impl, FALSE);
9557 impl->load_recent_id = 0;
9559 if (load_data->items)
9561 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9562 g_list_free (load_data->items);
9568 struct RecentItemInsertRequest
9570 GtkFileChooserDefault *impl;
9572 GtkTreeRowReference *row_ref;
9576 recent_item_get_info_cb (GCancellable *cancellable,
9578 const GError *error,
9581 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9584 GCancellable *model_cancellable;
9585 gboolean is_folder = FALSE;
9586 struct RecentItemInsertRequest *request = data;
9588 if (!request->impl->recent_model)
9591 path = gtk_tree_row_reference_get_path (request->row_ref);
9595 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9597 gtk_tree_path_free (path);
9599 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9600 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9602 if (cancellable != model_cancellable)
9605 gtk_list_store_set (request->impl->recent_model, &iter,
9606 RECENT_MODEL_COL_CANCELLABLE, NULL,
9614 gtk_list_store_remove (request->impl->recent_model, &iter);
9618 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9620 gtk_list_store_set (request->impl->recent_model, &iter,
9621 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9625 g_object_unref (request->impl);
9626 g_object_unref (request->file);
9627 gtk_tree_row_reference_free (request->row_ref);
9630 g_object_unref (cancellable);
9634 recent_sort_mru (gconstpointer a,
9637 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9638 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9640 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9644 get_recent_files_limit (GtkWidget *widget)
9646 GtkSettings *settings;
9649 if (gtk_widget_has_screen (widget))
9650 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9652 settings = gtk_settings_get_default ();
9654 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9660 recent_idle_load (gpointer data)
9662 RecentLoadData *load_data = data;
9663 GtkFileChooserDefault *impl = load_data->impl;
9666 GtkRecentInfo *info;
9667 const gchar *uri, *display_name;
9669 GCancellable *cancellable;
9670 struct RecentItemInsertRequest *request;
9672 if (!impl->recent_manager)
9675 /* first iteration: load all the items */
9676 if (!load_data->items)
9678 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9679 if (!load_data->items)
9682 load_data->needs_sorting = TRUE;
9687 /* second iteration: preliminary MRU sorting and clamping */
9688 if (load_data->needs_sorting)
9692 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9693 load_data->n_items = g_list_length (load_data->items);
9695 limit = get_recent_files_limit (GTK_WIDGET (impl));
9697 if (limit != -1 && (load_data->n_items > limit))
9701 clamp = g_list_nth (load_data->items, limit - 1);
9702 if (G_LIKELY (clamp))
9707 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9710 load_data->n_items = limit;
9714 load_data->n_loaded_items = 0;
9715 load_data->needs_sorting = FALSE;
9720 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9721 g_assert (info != NULL);
9723 uri = gtk_recent_info_get_uri (info);
9724 display_name = gtk_recent_info_get_display_name (info);
9725 file = g_file_new_for_uri (uri);
9727 gtk_list_store_append (impl->recent_model, &iter);
9728 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9730 request = g_new0 (struct RecentItemInsertRequest, 1);
9731 request->impl = g_object_ref (impl);
9732 request->file = g_object_ref (file);
9733 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9734 gtk_tree_path_free (p);
9736 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9738 recent_item_get_info_cb,
9741 gtk_list_store_set (impl->recent_model, &iter,
9742 RECENT_MODEL_COL_FILE, file,
9743 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9744 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9745 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9748 load_data->n_loaded_items += 1;
9750 /* finished loading items */
9751 if (load_data->n_loaded_items == load_data->n_items)
9753 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9754 g_list_free (load_data->items);
9755 load_data->items = NULL;
9764 recent_start_loading (GtkFileChooserDefault *impl)
9766 RecentLoadData *load_data;
9768 recent_stop_loading (impl);
9769 recent_clear_model (impl, TRUE);
9770 recent_setup_model (impl);
9771 set_busy_cursor (impl, TRUE);
9773 g_assert (impl->load_recent_id == 0);
9775 load_data = g_new (RecentLoadData, 1);
9776 load_data->impl = impl;
9777 load_data->items = NULL;
9778 load_data->n_items = 0;
9779 load_data->n_loaded_items = 0;
9780 load_data->needs_sorting = TRUE;
9782 /* begin lazy loading the recent files into the model */
9783 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9786 recent_idle_cleanup);
9790 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9800 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9801 *list = g_slist_prepend (*list, g_object_ref (file));
9804 /* Constructs a list of the selected paths in recent files mode */
9806 recent_get_selected_files (GtkFileChooserDefault *impl)
9809 GtkTreeSelection *selection;
9813 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9814 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9815 result = g_slist_reverse (result);
9820 /* Called from ::should_respond(). We return whether there are selected
9821 * files in the recent files list.
9824 recent_should_respond (GtkFileChooserDefault *impl)
9826 GtkTreeSelection *selection;
9828 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9830 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9831 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9834 /* Hide the location widgets temporarily */
9836 recent_hide_entry (GtkFileChooserDefault *impl)
9838 gtk_widget_hide (impl->browse_path_bar);
9839 gtk_widget_hide (impl->browse_new_folder_button);
9841 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9842 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9844 gtk_widget_hide (impl->location_button);
9845 gtk_widget_hide (impl->location_entry_box);
9849 /* Main entry point to the recent files functions; this gets called when
9850 * the user activates the Recently Used shortcut.
9853 recent_activate (GtkFileChooserDefault *impl)
9855 OperationMode previous_mode;
9857 if (impl->operation_mode == OPERATION_MODE_RECENT)
9860 previous_mode = impl->operation_mode;
9861 impl->operation_mode = OPERATION_MODE_RECENT;
9863 switch (previous_mode)
9865 case OPERATION_MODE_SEARCH:
9866 search_stop_searching (impl, FALSE);
9867 search_clear_model (impl, TRUE);
9869 gtk_widget_destroy (impl->search_hbox);
9870 impl->search_hbox = NULL;
9871 impl->search_entry = NULL;
9874 case OPERATION_MODE_BROWSE:
9875 stop_loading_and_clear_list_model (impl);
9878 case OPERATION_MODE_RECENT:
9879 g_assert_not_reached ();
9883 recent_hide_entry (impl);
9884 file_list_set_sort_column_ids (impl);
9885 recent_start_loading (impl);
9888 /* convert an iterator coming from the model bound to
9889 * browse_files_tree_view to an interator inside the
9893 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9894 GtkTreeIter *child_iter,
9899 if (!impl->recent_model)
9902 if (!impl->recent_model_filter || !impl->recent_model_sort)
9905 /* pass 1: get the iterator in the filter model */
9906 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9909 /* pass 2: get the iterator in the real model */
9910 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9917 set_current_filter (GtkFileChooserDefault *impl,
9918 GtkFileFilter *filter)
9920 if (impl->current_filter != filter)
9924 /* NULL filters are allowed to reset to non-filtered status
9926 filter_index = g_slist_index (impl->filters, filter);
9927 if (impl->filters && filter && filter_index < 0)
9930 if (impl->current_filter)
9931 g_object_unref (impl->current_filter);
9932 impl->current_filter = filter;
9933 if (impl->current_filter)
9935 g_object_ref_sink (impl->current_filter);
9939 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9942 if (impl->browse_files_model)
9943 install_list_model_filter (impl);
9945 if (impl->search_model_filter)
9946 gtk_tree_model_filter_refilter (impl->search_model_filter);
9948 if (impl->recent_model_filter)
9949 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9951 g_object_notify (G_OBJECT (impl), "filter");
9956 filter_combo_changed (GtkComboBox *combo_box,
9957 GtkFileChooserDefault *impl)
9959 gint new_index = gtk_combo_box_get_active (combo_box);
9960 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9962 set_current_filter (impl, new_filter);
9966 check_preview_change (GtkFileChooserDefault *impl)
9968 GtkTreePath *cursor_path;
9970 const char *new_display_name;
9972 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9974 new_display_name = NULL;
9977 GtkTreeIter child_iter;
9979 if (impl->operation_mode == OPERATION_MODE_BROWSE)
9981 if (impl->sort_model)
9984 GFileInfo *new_info;
9986 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
9987 gtk_tree_path_free (cursor_path);
9989 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
9991 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
9992 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
9994 new_display_name = g_file_info_get_display_name (new_info);
9997 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10001 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10002 &iter, cursor_path);
10003 gtk_tree_path_free (cursor_path);
10005 search_get_valid_child_iter (impl, &child_iter, &iter);
10006 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10007 SEARCH_MODEL_COL_FILE, &new_file,
10008 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10011 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10015 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10016 &iter, cursor_path);
10017 gtk_tree_path_free (cursor_path);
10019 recent_get_valid_child_iter (impl, &child_iter, &iter);
10020 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10021 RECENT_MODEL_COL_FILE, &new_file,
10022 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10027 if (new_file != impl->preview_file &&
10028 !(new_file && impl->preview_file &&
10029 g_file_equal (new_file, impl->preview_file)))
10031 if (impl->preview_file)
10033 g_object_unref (impl->preview_file);
10034 g_free (impl->preview_display_name);
10039 impl->preview_file = g_object_ref (new_file);
10040 impl->preview_display_name = g_strdup (new_display_name);
10044 impl->preview_file = NULL;
10045 impl->preview_display_name = NULL;
10048 if (impl->use_preview_label && impl->preview_label)
10049 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10051 g_signal_emit_by_name (impl, "update-preview");
10056 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10057 GtkFileSystemVolume *volume,
10058 const GError *error,
10062 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10063 GtkFileChooserDefault *impl = data;
10065 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10068 impl->shortcuts_activate_iter_cancellable = NULL;
10070 set_busy_cursor (impl, FALSE);
10077 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10081 name = _gtk_file_system_volume_get_display_name (volume);
10082 msg = g_strdup_printf (_("Could not mount %s"), name);
10084 error_message (impl, msg, error->message);
10093 file = _gtk_file_system_volume_get_root (volume);
10096 change_folder_and_display_error (impl, file, FALSE);
10097 g_object_unref (file);
10101 g_object_unref (impl);
10102 g_object_unref (cancellable);
10106 /* Activates a volume by mounting it if necessary and then switching to its
10110 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10111 GtkFileSystemVolume *volume)
10115 switch (impl->operation_mode)
10117 case OPERATION_MODE_BROWSE:
10119 case OPERATION_MODE_SEARCH:
10120 search_switch_to_browse_mode (impl);
10122 case OPERATION_MODE_RECENT:
10123 recent_switch_to_browse_mode (impl);
10127 /* We ref the file chooser since volume_mount() may run a main loop, and the
10128 * user could close the file chooser window in the meantime.
10130 g_object_ref (impl);
10132 if (!_gtk_file_system_volume_is_mounted (volume))
10134 set_busy_cursor (impl, TRUE);
10136 impl->shortcuts_activate_iter_cancellable =
10137 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10138 shortcuts_activate_volume_mount_cb,
10139 g_object_ref (impl));
10143 file = _gtk_file_system_volume_get_root (volume);
10146 change_folder_and_display_error (impl, file, FALSE);
10147 g_object_unref (file);
10151 g_object_unref (impl);
10154 /* Opens the folder or volume at the specified iter in the shortcuts model */
10155 struct ShortcutsActivateData
10157 GtkFileChooserDefault *impl;
10162 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10164 const GError *error,
10165 gpointer user_data)
10167 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10168 struct ShortcutsActivateData *data = user_data;
10170 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10173 data->impl->shortcuts_activate_iter_cancellable = NULL;
10178 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10179 change_folder_and_display_error (data->impl, data->file, FALSE);
10181 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10186 g_object_unref (data->impl);
10187 g_object_unref (data->file);
10190 g_object_unref (cancellable);
10194 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10195 GtkFileSystemVolume *volume,
10196 const GError *error,
10197 gpointer user_data)
10199 struct ShortcutsActivateData *data = user_data;
10203 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10205 g_object_unref (data->impl);
10206 g_object_unref (data->file);
10212 data->impl->shortcuts_activate_iter_cancellable =
10213 _gtk_file_system_get_info (data->impl->file_system, data->file,
10215 shortcuts_activate_get_info_cb, data);
10219 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10223 ShortcutType shortcut_type;
10225 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10226 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10227 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10228 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10230 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10231 SHORTCUTS_COL_DATA, &col_data,
10232 SHORTCUTS_COL_TYPE, &shortcut_type,
10235 if (impl->shortcuts_activate_iter_cancellable)
10237 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10238 impl->shortcuts_activate_iter_cancellable = NULL;
10241 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10243 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10245 GtkFileSystemVolume *volume;
10249 shortcuts_activate_volume (impl, volume);
10251 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10253 struct ShortcutsActivateData *data;
10254 GtkFileSystemVolume *volume;
10256 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10258 data = g_new0 (struct ShortcutsActivateData, 1);
10259 data->impl = g_object_ref (impl);
10260 data->file = g_object_ref (col_data);
10262 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10264 GMountOperation *mount_operation;
10265 GtkWidget *toplevel;
10267 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10269 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10271 impl->shortcuts_activate_iter_cancellable =
10272 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10274 shortcuts_activate_mount_enclosing_volume,
10279 impl->shortcuts_activate_iter_cancellable =
10280 _gtk_file_system_get_info (impl->file_system, data->file,
10282 shortcuts_activate_get_info_cb, data);
10285 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10287 search_activate (impl);
10289 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10291 recent_activate (impl);
10295 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10297 shortcuts_key_press_event_cb (GtkWidget *widget,
10298 GdkEventKey *event,
10299 GtkFileChooserDefault *impl)
10303 modifiers = gtk_accelerator_get_default_mod_mask ();
10305 if ((event->keyval == GDK_BackSpace
10306 || event->keyval == GDK_Delete
10307 || event->keyval == GDK_KP_Delete)
10308 && (event->state & modifiers) == 0)
10310 remove_selected_bookmarks (impl);
10314 if ((event->keyval == GDK_F2)
10315 && (event->state & modifiers) == 0)
10317 rename_selected_bookmark (impl);
10325 shortcuts_select_func (GtkTreeSelection *selection,
10326 GtkTreeModel *model,
10328 gboolean path_currently_selected,
10331 GtkFileChooserDefault *impl = data;
10332 GtkTreeIter filter_iter;
10333 ShortcutType shortcut_type;
10335 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10336 g_assert_not_reached ();
10338 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10340 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10344 list_select_func (GtkTreeSelection *selection,
10345 GtkTreeModel *model,
10347 gboolean path_currently_selected,
10350 GtkFileChooserDefault *impl = data;
10352 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10353 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10355 GtkTreeIter iter, child_iter;
10357 switch (impl->operation_mode)
10359 case OPERATION_MODE_SEARCH:
10361 gboolean is_folder;
10363 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10366 search_get_valid_child_iter (impl, &child_iter, &iter);
10367 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10368 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10375 case OPERATION_MODE_RECENT:
10377 gboolean is_folder;
10379 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10382 recent_get_valid_child_iter (impl, &child_iter, &iter);
10383 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10384 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10391 case OPERATION_MODE_BROWSE:
10395 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10398 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10399 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10400 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10411 list_selection_changed (GtkTreeSelection *selection,
10412 GtkFileChooserDefault *impl)
10414 /* See if we are in the new folder editable row for Save mode */
10415 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10416 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10419 gboolean had_selection;
10421 info = get_selected_file_info_from_file_list (impl, &had_selection);
10422 if (!had_selection)
10423 goto out; /* normal processing */
10426 return; /* We are on the editable row for New Folder */
10431 if (impl->location_entry)
10432 update_chooser_entry (impl);
10434 check_preview_change (impl);
10435 bookmarks_check_add_sensitivity (impl);
10437 g_signal_emit_by_name (impl, "selection-changed", 0);
10440 /* Callback used when a row in the file list is activated */
10442 list_row_activated (GtkTreeView *tree_view,
10444 GtkTreeViewColumn *column,
10445 GtkFileChooserDefault *impl)
10448 GtkTreeIter child_iter;
10450 switch (impl->operation_mode)
10452 case OPERATION_MODE_SEARCH:
10455 gboolean is_folder;
10457 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10460 search_get_valid_child_iter (impl, &child_iter, &iter);
10461 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10462 SEARCH_MODEL_COL_FILE, &file,
10463 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10468 change_folder_and_display_error (impl, file, FALSE);
10472 g_signal_emit_by_name (impl, "file-activated");
10476 case OPERATION_MODE_RECENT:
10479 gboolean is_folder;
10481 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10484 recent_get_valid_child_iter (impl, &child_iter, &iter);
10485 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10486 RECENT_MODEL_COL_FILE, &file,
10487 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10492 change_folder_and_display_error (impl, file, FALSE);
10496 g_signal_emit_by_name (impl, "file-activated");
10500 case OPERATION_MODE_BROWSE:
10504 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10507 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10508 &child_iter, &iter);
10509 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10512 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10516 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10517 change_folder_and_display_error (impl, file, FALSE);
10521 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10522 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10523 g_signal_emit_by_name (impl, "file-activated");
10530 path_bar_clicked (GtkPathBar *path_bar,
10533 gboolean child_is_hidden,
10534 GtkFileChooserDefault *impl)
10537 pending_select_files_add (impl, child_file);
10539 if (!change_folder_and_display_error (impl, file, FALSE))
10542 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10543 * show hidden files so that ".baz" appears in the file list, as it will still
10544 * be shown in the path bar: "/foo/[bar]/.baz"
10546 if (child_is_hidden)
10547 g_object_set (impl, "show-hidden", TRUE, NULL);
10551 get_list_file_info (GtkFileChooserDefault *impl,
10554 GtkTreeIter child_iter;
10556 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10560 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10564 list_icon_data_func (GtkTreeViewColumn *tree_column,
10565 GtkCellRenderer *cell,
10566 GtkTreeModel *tree_model,
10570 GtkFileChooserDefault *impl = data;
10571 GtkTreeIter child_iter;
10572 GdkPixbuf *pixbuf = NULL;
10573 gboolean sensitive = TRUE;
10575 profile_start ("start", NULL);
10577 switch (impl->operation_mode)
10579 case OPERATION_MODE_SEARCH:
10581 GtkTreeIter child_iter;
10582 gboolean is_folder;
10584 search_get_valid_child_iter (impl, &child_iter, iter);
10585 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10586 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10587 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10590 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10591 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10592 sensitive = is_folder;
10596 case OPERATION_MODE_RECENT:
10598 GtkTreeIter child_iter;
10599 GtkRecentInfo *info;
10600 gboolean is_folder;
10602 recent_get_valid_child_iter (impl, &child_iter, iter);
10603 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10604 RECENT_MODEL_COL_INFO, &info,
10605 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10608 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10610 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10611 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10612 sensitive = is_folder;
10616 case OPERATION_MODE_BROWSE:
10621 info = get_list_file_info (impl, iter);
10623 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10626 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10631 /* FIXME: NULL GError */
10632 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10637 /* We are on the editable row */
10642 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10643 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10644 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10649 g_object_set (cell,
10651 "sensitive", sensitive,
10655 g_object_unref (pixbuf);
10657 profile_end ("end", NULL);
10661 list_name_data_func (GtkTreeViewColumn *tree_column,
10662 GtkCellRenderer *cell,
10663 GtkTreeModel *tree_model,
10667 GtkFileChooserDefault *impl = data;
10669 gboolean sensitive = TRUE;
10671 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10673 GtkTreeIter child_iter;
10674 gchar *display_name;
10675 gboolean is_folder;
10677 search_get_valid_child_iter (impl, &child_iter, iter);
10678 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10679 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10680 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10683 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10684 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10686 sensitive = is_folder;
10689 g_object_set (cell,
10690 "text", display_name,
10691 "sensitive", sensitive,
10692 "ellipsize", PANGO_ELLIPSIZE_END,
10698 if (impl->operation_mode == OPERATION_MODE_RECENT)
10700 GtkTreeIter child_iter;
10701 GtkRecentInfo *recent_info;
10702 gchar *display_name;
10703 gboolean is_folder;
10705 recent_get_valid_child_iter (impl, &child_iter, iter);
10706 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10707 RECENT_MODEL_COL_INFO, &recent_info,
10708 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10711 display_name = gtk_recent_info_get_short_name (recent_info);
10713 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10714 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10716 sensitive = is_folder;
10719 g_object_set (cell,
10720 "text", display_name,
10721 "sensitive", sensitive,
10722 "ellipsize", PANGO_ELLIPSIZE_END,
10725 g_free (display_name);
10730 info = get_list_file_info (impl, iter);
10735 g_object_set (cell,
10736 "text", _("Type name of new folder"),
10738 "ellipsize", PANGO_ELLIPSIZE_NONE,
10745 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10746 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10748 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10751 g_object_set (cell,
10752 "text", g_file_info_get_display_name (info),
10753 "sensitive", sensitive,
10754 "ellipsize", PANGO_ELLIPSIZE_END,
10760 list_size_data_func (GtkTreeViewColumn *tree_column,
10761 GtkCellRenderer *cell,
10762 GtkTreeModel *tree_model,
10766 GtkFileChooserDefault *impl = data;
10767 GFileInfo *info = get_list_file_info (impl, iter);
10770 gboolean sensitive = TRUE;
10772 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10774 g_object_set (cell,
10776 "sensitive", sensitive,
10781 size = gtk_file_info_get_size (info);
10783 if (size < (gint64)1024)
10784 str = g_strdup_printf (g_dngettext (GETTEXT_DOMAIN, "%d byte", "%d bytes", (gint)size), (gint)size);
10785 else if (size < (gint64)1024*1024)
10786 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10787 else if (size < (gint64)1024*1024*1024)
10788 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10790 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10792 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10793 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10794 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10797 g_object_set (cell,
10799 "sensitive", sensitive,
10800 "alignment", PANGO_ALIGN_RIGHT,
10807 /* Tree column data callback for the file list; fetches the mtime of a file */
10809 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10810 GtkCellRenderer *cell,
10811 GtkTreeModel *tree_model,
10815 GtkFileChooserDefault *impl;
10816 GTimeVal timeval = { 0, };
10818 gchar *date_str = NULL;
10819 gboolean sensitive = TRUE;
10821 const char *locale, *dot = NULL;
10822 gint64 codepage = -1;
10828 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10830 GtkTreeIter child_iter;
10831 struct stat *statbuf;
10832 gboolean is_folder;
10834 search_get_valid_child_iter (impl, &child_iter, iter);
10835 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10836 SEARCH_MODEL_COL_STAT, &statbuf,
10837 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10840 time_mtime = statbuf->st_mtime;
10845 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10846 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10847 sensitive = is_folder;
10849 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10851 GtkTreeIter child_iter;
10852 GtkRecentInfo *info;
10853 gboolean is_folder;
10855 recent_get_valid_child_iter (impl, &child_iter, iter);
10856 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10857 RECENT_MODEL_COL_INFO, &info,
10858 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10862 time_mtime = gtk_recent_info_get_modified (info);
10866 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10867 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10868 sensitive = is_folder;
10874 info = get_list_file_info (impl, iter);
10877 g_object_set (cell,
10884 g_file_info_get_modification_time (info, &timeval);
10885 time_mtime = timeval.tv_sec;
10887 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10888 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10889 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10892 if (G_UNLIKELY (time_mtime == 0))
10893 date_str = g_strdup (_("Unknown"));
10898 struct tm tm_mtime;
10900 const gchar *format;
10901 gchar *locale_format = NULL;
10904 #ifdef HAVE_LOCALTIME_R
10905 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10908 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10912 g_warning ("ptm != NULL failed");
10914 g_object_set (cell,
10915 "text", _("Unknown"),
10916 "sensitive", sensitive,
10921 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10923 #endif /* HAVE_LOCALTIME_R */
10925 g_date_set_time_t (&mtime, time_mtime);
10926 time_now = time (NULL);
10927 g_date_set_time_t (&now, time_now);
10929 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10931 /* Translators: %H means "hours" and %M means "minutes" */
10932 if (days_diff == 0)
10933 format = _("%H:%M");
10934 else if (days_diff == 1)
10935 format = _("Yesterday at %H:%M");
10938 if (days_diff > 1 && days_diff < 7)
10939 format = "%A"; /* Days from last week */
10941 format = "%x"; /* Any other date */
10945 /* g_locale_from_utf8() returns a string in the system
10946 * code-page, which is not always the same as that used by the C
10947 * library. For instance when running a GTK+ program with
10948 * LANG=ko on an English version of Windows, the system
10949 * code-page is 1252, but the code-page used by the C library is
10950 * 949. (It's GTK+ itself that sets the C library locale when it
10951 * notices the LANG environment variable. See gtkmain.c The
10952 * Microsoft C library doesn't look at any locale environment
10953 * variables.) We need to pass strftime() a string in the C
10954 * library's code-page. See bug #509885.
10956 locale = setlocale (LC_ALL, NULL);
10957 if (locale != NULL)
10958 dot = strchr (locale, '.');
10961 codepage = g_ascii_strtoll (dot+1, NULL, 10);
10963 /* All codepages should fit in 16 bits AFAIK */
10964 if (codepage > 0 && codepage < 65536)
10966 sprintf (charset, "CP%u", (guint) codepage);
10967 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
10971 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
10973 if (locale_format != NULL &&
10974 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
10977 /* As above but in opposite direction... */
10978 if (codepage > 0 && codepage < 65536)
10979 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
10981 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
10985 if (date_str == NULL)
10986 date_str = g_strdup (_("Unknown"));
10988 g_free (locale_format);
10991 g_object_set (cell,
10993 "sensitive", sensitive,
10999 _gtk_file_chooser_default_new (const char *file_system)
11001 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
11002 "file-system-backend", file_system,
11007 location_set_user_text (GtkFileChooserDefault *impl,
11010 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11011 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11015 location_popup_handler (GtkFileChooserDefault *impl,
11018 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11020 GtkWidget *widget_to_focus;
11022 /* This will give us the location widgets back */
11023 switch (impl->operation_mode)
11025 case OPERATION_MODE_SEARCH:
11026 search_switch_to_browse_mode (impl);
11028 case OPERATION_MODE_RECENT:
11029 recent_switch_to_browse_mode (impl);
11031 case OPERATION_MODE_BROWSE:
11032 g_assert_not_reached ();
11036 if (impl->current_folder)
11037 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11039 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11040 widget_to_focus = impl->browse_files_tree_view;
11042 widget_to_focus = impl->location_entry;
11044 gtk_widget_grab_focus (widget_to_focus);
11048 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11049 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11051 LocationMode new_mode;
11055 /* since the user typed something, we unconditionally want to turn on the entry */
11056 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11058 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11059 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11060 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11061 new_mode = LOCATION_MODE_PATH_BAR;
11064 g_assert_not_reached ();
11068 location_mode_set (impl, new_mode, TRUE);
11069 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11072 location_set_user_text (impl, path);
11075 location_entry_set_initial_text (impl);
11076 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11080 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11081 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11083 gtk_widget_grab_focus (impl->location_entry);
11085 location_set_user_text (impl, path);
11088 g_assert_not_reached ();
11091 /* Handler for the "up-folder" keybinding signal */
11093 up_folder_handler (GtkFileChooserDefault *impl)
11095 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11098 /* Handler for the "down-folder" keybinding signal */
11100 down_folder_handler (GtkFileChooserDefault *impl)
11102 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11105 /* Switches to the shortcut in the specified index */
11107 switch_to_shortcut (GtkFileChooserDefault *impl,
11112 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11113 g_assert_not_reached ();
11115 shortcuts_activate_iter (impl, &iter);
11118 /* Handler for the "home-folder" keybinding signal */
11120 home_folder_handler (GtkFileChooserDefault *impl)
11122 if (impl->has_home)
11123 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11126 /* Handler for the "desktop-folder" keybinding signal */
11128 desktop_folder_handler (GtkFileChooserDefault *impl)
11130 if (impl->has_desktop)
11131 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11134 /* Handler for the "search-shortcut" keybinding signal */
11136 search_shortcut_handler (GtkFileChooserDefault *impl)
11138 if (impl->has_search)
11140 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11142 /* we want the entry widget to grab the focus the first
11143 * time, not the browse_files_tree_view widget.
11145 if (impl->search_entry)
11146 gtk_widget_grab_focus (impl->search_entry);
11150 /* Handler for the "recent-shortcut" keybinding signal */
11152 recent_shortcut_handler (GtkFileChooserDefault *impl)
11154 if (impl->has_recent)
11155 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11159 quick_bookmark_handler (GtkFileChooserDefault *impl,
11160 gint bookmark_index)
11165 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11168 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11170 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11171 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11174 gtk_tree_path_free (path);
11176 switch_to_shortcut (impl, bookmark_pos);
11180 show_hidden_handler (GtkFileChooserDefault *impl)
11182 g_object_set (impl,
11183 "show-hidden", !impl->show_hidden,
11188 /* Drag and drop interfaces */
11191 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11196 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11198 model->impl = NULL;
11201 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11203 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11206 ShortcutsPaneModelFilter *model;
11210 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11212 pos = *gtk_tree_path_get_indices (path);
11213 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11215 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11218 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11220 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11222 GtkSelectionData *selection_data)
11224 ShortcutsPaneModelFilter *model;
11226 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11233 /* Fill the GtkTreeDragSourceIface vtable */
11235 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11237 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11238 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11242 /* Fill the GtkTreeDragDestIface vtable */
11244 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11246 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11247 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11251 static GtkTreeModel *
11252 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11253 GtkTreeModel *child_model,
11256 ShortcutsPaneModelFilter *model;
11258 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11259 "child-model", child_model,
11260 "virtual-root", root,
11263 model->impl = impl;
11265 return GTK_TREE_MODEL (model);
11271 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11274 RecentModelSort *model;
11275 GtkTreeIter iter, child_iter;
11276 gboolean is_folder;
11278 model = RECENT_MODEL_SORT (drag_source);
11279 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11282 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11283 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11284 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11291 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11293 GtkSelectionData *selection_data)
11295 RecentModelSort *model;
11296 GtkTreeIter iter, child_iter;
11300 model = RECENT_MODEL_SORT (drag_source);
11301 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11304 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11305 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11306 RECENT_MODEL_COL_FILE, &file,
11308 g_assert (file != NULL);
11310 uris[0] = g_file_get_uri (file);
11313 gtk_selection_data_set_uris (selection_data, uris);
11321 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11323 iface->row_draggable = recent_model_sort_row_draggable;
11324 iface->drag_data_get = recent_model_sort_drag_data_get;
11328 _recent_model_sort_class_init (RecentModelSortClass *klass)
11334 _recent_model_sort_init (RecentModelSort *model)
11336 model->impl = NULL;
11339 static GtkTreeModel *
11340 recent_model_sort_new (GtkFileChooserDefault *impl,
11341 GtkTreeModel *child_model)
11343 RecentModelSort *model;
11345 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11346 "model", child_model,
11348 model->impl = impl;
11350 return GTK_TREE_MODEL (model);
11356 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11359 SearchModelSort *model;
11360 GtkTreeIter iter, child_iter;
11361 gboolean is_folder;
11363 model = SEARCH_MODEL_SORT (drag_source);
11364 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11367 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11368 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11369 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11376 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11378 GtkSelectionData *selection_data)
11380 SearchModelSort *model;
11381 GtkTreeIter iter, child_iter;
11385 model = SEARCH_MODEL_SORT (drag_source);
11386 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11389 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11390 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11391 RECENT_MODEL_COL_FILE, &file,
11393 g_assert (file != NULL);
11395 uris[0] = g_file_get_uri (file);
11398 gtk_selection_data_set_uris (selection_data, uris);
11406 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11408 iface->row_draggable = search_model_sort_row_draggable;
11409 iface->drag_data_get = search_model_sort_drag_data_get;
11413 _search_model_sort_class_init (SearchModelSortClass *klass)
11419 _search_model_sort_init (SearchModelSort *model)
11421 model->impl = NULL;
11424 static GtkTreeModel *
11425 search_model_sort_new (GtkFileChooserDefault *impl,
11426 GtkTreeModel *child_model)
11428 SearchModelSort *model;
11430 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11431 "model", child_model,
11433 model->impl = impl;
11435 return GTK_TREE_MODEL (model);