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.
23 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.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 "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdefault.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystem.h"
44 #include "gtkfilesystemmodel.h"
47 #include "gtkhpaned.h"
48 #include "gtkiconfactory.h"
49 #include "gtkicontheme.h"
51 #include "gtkimagemenuitem.h"
54 #include "gtkmarshalers.h"
55 #include "gtkmenuitem.h"
56 #include "gtkmessagedialog.h"
57 #include "gtkmountoperation.h"
58 #include "gtkpathbar.h"
59 #include "gtkprivate.h"
60 #include "gtkradiobutton.h"
61 #include "gtkrecentfilter.h"
62 #include "gtkrecentmanager.h"
63 #include "gtkscrolledwindow.h"
64 #include "gtkseparatormenuitem.h"
65 #include "gtksizegroup.h"
68 #include "gtktooltip.h"
69 #include "gtktreednd.h"
70 #include "gtktreeprivate.h"
71 #include "gtktreeselection.h"
72 #include "gtktypebuiltins.h"
81 #include <sys/types.h>
92 #undef PROFILE_FILE_CHOOSER
93 #ifdef PROFILE_FILE_CHOOSER
100 #define PROFILE_INDENT 4
101 static int profile_indent;
104 profile_add_indent (int indent)
106 profile_indent += indent;
107 if (profile_indent < 0)
108 g_error ("You screwed up your indentation");
112 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
117 profile_add_indent (indent);
119 if (profile_indent == 0)
120 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
122 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
128 profile_add_indent (indent);
131 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
132 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
133 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
135 #define profile_start(x, y)
136 #define profile_end(x, y)
137 #define profile_msg(x, y)
142 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
144 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
146 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
148 #define MAX_LOADING_TIME 500
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 /* Column numbers for the file list */
199 FILE_LIST_COL_NUM_COLUMNS
202 /* Column numbers for the search model.
203 * Keep this in sync with search_setup_model()
206 SEARCH_MODEL_COL_FILE,
207 SEARCH_MODEL_COL_DISPLAY_NAME,
208 SEARCH_MODEL_COL_COLLATION_KEY,
209 SEARCH_MODEL_COL_STAT,
210 SEARCH_MODEL_COL_CANCELLABLE,
211 SEARCH_MODEL_COL_PIXBUF,
212 SEARCH_MODEL_COL_MIME_TYPE,
213 SEARCH_MODEL_COL_IS_FOLDER,
214 SEARCH_MODEL_COL_NUM_COLUMNS
218 RECENT_MODEL_COL_FILE,
219 RECENT_MODEL_COL_DISPLAY_NAME,
220 RECENT_MODEL_COL_INFO,
221 RECENT_MODEL_COL_IS_FOLDER,
222 RECENT_MODEL_COL_CANCELLABLE,
223 RECENT_MODEL_COL_NUM_COLUMNS
226 /* Identifiers for target types */
232 /* Target types for dragging from the shortcuts list */
233 static const GtkTargetEntry shortcuts_source_targets[] = {
234 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
237 static const int num_shortcuts_source_targets = G_N_ELEMENTS (shortcuts_source_targets);
239 /* Target types for dropping into the shortcuts list */
240 static const GtkTargetEntry shortcuts_dest_targets[] = {
241 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
242 { "text/uri-list", 0, TEXT_URI_LIST }
245 static const int num_shortcuts_dest_targets = G_N_ELEMENTS (shortcuts_dest_targets);
247 /* Target types for DnD from the file list */
248 static const GtkTargetEntry file_list_source_targets[] = {
249 { "text/uri-list", 0, TEXT_URI_LIST }
252 static const int num_file_list_source_targets = G_N_ELEMENTS (file_list_source_targets);
254 /* Target types for dropping into the file list */
255 static const GtkTargetEntry file_list_dest_targets[] = {
256 { "text/uri-list", GTK_TARGET_OTHER_WIDGET, TEXT_URI_LIST }
259 static const int num_file_list_dest_targets = G_N_ELEMENTS (file_list_dest_targets);
261 /* Target types for dragging from the recent files list */
262 static const GtkTargetEntry recent_list_source_targets[] = {
263 { "text/uri-list", 0, TEXT_URI_LIST }
266 static const int num_recent_list_source_targets = G_N_ELEMENTS (recent_list_source_targets);
269 search_is_possible (GtkFileChooserDefault *impl)
271 if (impl->search_engine == NULL)
272 impl->search_engine = _gtk_search_engine_new ();
274 return impl->search_engine != NULL;
277 /* Interesting places in the shortcuts bar */
281 SHORTCUTS_RECENT_SEPARATOR,
286 SHORTCUTS_BOOKMARKS_SEPARATOR,
288 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
289 SHORTCUTS_CURRENT_FOLDER
292 /* Icon size for if we can't get it from the theme */
293 #define FALLBACK_ICON_SIZE 16
295 #define PREVIEW_HBOX_SPACING 12
299 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
300 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
302 static GObject* gtk_file_chooser_default_constructor (GType type,
303 guint n_construct_properties,
304 GObjectConstructParam *construct_params);
305 static void gtk_file_chooser_default_finalize (GObject *object);
306 static void gtk_file_chooser_default_set_property (GObject *object,
310 static void gtk_file_chooser_default_get_property (GObject *object,
314 static void gtk_file_chooser_default_dispose (GObject *object);
315 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
316 static void gtk_file_chooser_default_map (GtkWidget *widget);
317 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
318 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
319 GtkWidget *previous_toplevel);
320 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
321 GtkStyle *previous_style);
322 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
323 GdkScreen *previous_screen);
324 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
325 GtkAllocation *allocation);
327 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
330 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
333 gboolean clear_entry,
335 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
336 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
338 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
341 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
343 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
344 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
345 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
346 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
347 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
348 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
349 GtkFileFilter *filter);
350 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
351 GtkFileFilter *filter);
352 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
353 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
356 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
359 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
361 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
363 gint *default_height);
364 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
365 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
367 static void location_popup_handler (GtkFileChooserDefault *impl,
369 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
370 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
371 static void up_folder_handler (GtkFileChooserDefault *impl);
372 static void down_folder_handler (GtkFileChooserDefault *impl);
373 static void home_folder_handler (GtkFileChooserDefault *impl);
374 static void desktop_folder_handler (GtkFileChooserDefault *impl);
375 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
376 gint bookmark_index);
377 static void show_hidden_handler (GtkFileChooserDefault *impl);
378 static void search_shortcut_handler (GtkFileChooserDefault *impl);
379 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
380 static void update_appearance (GtkFileChooserDefault *impl);
382 static void set_current_filter (GtkFileChooserDefault *impl,
383 GtkFileFilter *filter);
384 static void check_preview_change (GtkFileChooserDefault *impl);
386 static void filter_combo_changed (GtkComboBox *combo_box,
387 GtkFileChooserDefault *impl);
389 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
391 GtkFileChooserDefault *impl);
393 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
396 gboolean path_currently_selected,
398 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
400 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
402 static int shortcuts_get_index (GtkFileChooserDefault *impl,
403 ShortcutsIndex where);
404 static int shortcut_find_position (GtkFileChooserDefault *impl,
407 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
409 static gboolean list_select_func (GtkTreeSelection *selection,
412 gboolean path_currently_selected,
415 static void list_selection_changed (GtkTreeSelection *tree_selection,
416 GtkFileChooserDefault *impl);
417 static void list_row_activated (GtkTreeView *tree_view,
419 GtkTreeViewColumn *column,
420 GtkFileChooserDefault *impl);
422 static void select_func (GtkFileSystemModel *model,
427 static void path_bar_clicked (GtkPathBar *path_bar,
430 gboolean child_is_hidden,
431 GtkFileChooserDefault *impl);
433 static void add_bookmark_button_clicked_cb (GtkButton *button,
434 GtkFileChooserDefault *impl);
435 static void remove_bookmark_button_clicked_cb (GtkButton *button,
436 GtkFileChooserDefault *impl);
437 static void save_folder_combo_changed_cb (GtkComboBox *combo,
438 GtkFileChooserDefault *impl);
440 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
441 GtkCellRenderer *cell,
442 GtkTreeModel *tree_model,
445 static void list_name_data_func (GtkTreeViewColumn *tree_column,
446 GtkCellRenderer *cell,
447 GtkTreeModel *tree_model,
451 static void list_size_data_func (GtkTreeViewColumn *tree_column,
452 GtkCellRenderer *cell,
453 GtkTreeModel *tree_model,
457 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
458 GtkCellRenderer *cell,
459 GtkTreeModel *tree_model,
463 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
466 static void load_remove_timer (GtkFileChooserDefault *impl);
467 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
469 static void location_button_toggled_cb (GtkToggleButton *toggle,
470 GtkFileChooserDefault *impl);
471 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
473 static void search_stop_searching (GtkFileChooserDefault *impl,
474 gboolean remove_query);
475 static void search_clear_model (GtkFileChooserDefault *impl,
476 gboolean remove_from_treeview);
477 static gboolean search_should_respond (GtkFileChooserDefault *impl);
478 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
479 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
480 static void search_entry_activate_cb (GtkEntry *entry,
482 static void settings_load (GtkFileChooserDefault *impl);
483 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
484 GtkTreeIter *child_iter,
487 static void recent_manager_update (GtkFileChooserDefault *impl);
488 static void recent_stop_loading (GtkFileChooserDefault *impl);
489 static void recent_clear_model (GtkFileChooserDefault *impl,
490 gboolean remove_from_treeview);
491 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
492 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
493 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
494 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
495 GtkTreeIter *child_iter,
497 static void set_file_system_backend (GtkFileChooserDefault *impl);
503 /* Drag and drop interface declarations */
506 GtkTreeModelFilter parent;
508 GtkFileChooserDefault *impl;
509 } ShortcutsPaneModelFilter;
512 GtkTreeModelFilterClass parent_class;
513 } ShortcutsPaneModelFilterClass;
515 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
516 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
518 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
520 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
521 _shortcuts_pane_model_filter,
522 GTK_TYPE_TREE_MODEL_FILTER,
523 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
524 shortcuts_pane_model_filter_drag_source_iface_init))
526 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
527 GtkTreeModel *child_model,
532 GtkTreeModelSort parent;
534 GtkFileChooserDefault *impl;
538 GtkTreeModelSortClass parent_class;
539 } RecentModelSortClass;
541 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
542 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
544 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
546 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
548 GTK_TYPE_TREE_MODEL_SORT,
549 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
550 recent_model_sort_drag_source_iface_init));
552 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
553 GtkTreeModel *child_model);
557 GtkTreeModelSort parent;
559 GtkFileChooserDefault *impl;
563 GtkTreeModelSortClass parent_class;
564 } SearchModelSortClass;
566 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
567 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
569 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
571 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
573 GTK_TYPE_TREE_MODEL_SORT,
574 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
575 search_model_sort_drag_source_iface_init));
577 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
578 GtkTreeModel *child_model);
582 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
583 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
584 gtk_file_chooser_default_iface_init)
585 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
586 gtk_file_chooser_embed_default_iface_init));
589 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
591 static const guint quick_bookmark_keyvals[10] = {
592 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
594 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
595 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
596 GtkBindingSet *binding_set;
599 gobject_class->finalize = gtk_file_chooser_default_finalize;
600 gobject_class->constructor = gtk_file_chooser_default_constructor;
601 gobject_class->set_property = gtk_file_chooser_default_set_property;
602 gobject_class->get_property = gtk_file_chooser_default_get_property;
603 gobject_class->dispose = gtk_file_chooser_default_dispose;
605 widget_class->show_all = gtk_file_chooser_default_show_all;
606 widget_class->map = gtk_file_chooser_default_map;
607 widget_class->unmap = gtk_file_chooser_default_unmap;
608 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
609 widget_class->style_set = gtk_file_chooser_default_style_set;
610 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
611 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
613 signals[LOCATION_POPUP] =
614 _gtk_binding_signal_new (I_("location-popup"),
615 G_OBJECT_CLASS_TYPE (class),
616 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
617 G_CALLBACK (location_popup_handler),
619 _gtk_marshal_VOID__STRING,
620 G_TYPE_NONE, 1, G_TYPE_STRING);
621 signals[LOCATION_POPUP_ON_PASTE] =
622 _gtk_binding_signal_new ("location-popup-on-paste",
623 G_OBJECT_CLASS_TYPE (class),
624 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
625 G_CALLBACK (location_popup_on_paste_handler),
627 _gtk_marshal_VOID__VOID,
629 signals[LOCATION_TOGGLE_POPUP] =
630 _gtk_binding_signal_new (I_("location-toggle-popup"),
631 G_OBJECT_CLASS_TYPE (class),
632 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
633 G_CALLBACK (location_toggle_popup_handler),
635 _gtk_marshal_VOID__VOID,
638 _gtk_binding_signal_new (I_("up-folder"),
639 G_OBJECT_CLASS_TYPE (class),
640 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
641 G_CALLBACK (up_folder_handler),
643 _gtk_marshal_VOID__VOID,
645 signals[DOWN_FOLDER] =
646 _gtk_binding_signal_new (I_("down-folder"),
647 G_OBJECT_CLASS_TYPE (class),
648 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
649 G_CALLBACK (down_folder_handler),
651 _gtk_marshal_VOID__VOID,
653 signals[HOME_FOLDER] =
654 _gtk_binding_signal_new (I_("home-folder"),
655 G_OBJECT_CLASS_TYPE (class),
656 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
657 G_CALLBACK (home_folder_handler),
659 _gtk_marshal_VOID__VOID,
661 signals[DESKTOP_FOLDER] =
662 _gtk_binding_signal_new (I_("desktop-folder"),
663 G_OBJECT_CLASS_TYPE (class),
664 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
665 G_CALLBACK (desktop_folder_handler),
667 _gtk_marshal_VOID__VOID,
669 signals[QUICK_BOOKMARK] =
670 _gtk_binding_signal_new (I_("quick-bookmark"),
671 G_OBJECT_CLASS_TYPE (class),
672 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
673 G_CALLBACK (quick_bookmark_handler),
675 _gtk_marshal_VOID__INT,
676 G_TYPE_NONE, 1, G_TYPE_INT);
677 signals[SHOW_HIDDEN] =
678 _gtk_binding_signal_new ("show-hidden",
679 G_OBJECT_CLASS_TYPE (class),
680 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
681 G_CALLBACK (show_hidden_handler),
683 _gtk_marshal_VOID__VOID,
685 signals[SEARCH_SHORTCUT] =
686 _gtk_binding_signal_new ("search-shortcut",
687 G_OBJECT_CLASS_TYPE (class),
688 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
689 G_CALLBACK (search_shortcut_handler),
691 _gtk_marshal_VOID__VOID,
693 signals[RECENT_SHORTCUT] =
694 _gtk_binding_signal_new ("recent-shortcut",
695 G_OBJECT_CLASS_TYPE (class),
696 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
697 G_CALLBACK (recent_shortcut_handler),
699 _gtk_marshal_VOID__VOID,
702 binding_set = gtk_binding_set_by_class (class);
704 gtk_binding_entry_add_signal (binding_set,
705 GDK_l, GDK_CONTROL_MASK,
706 "location-toggle-popup",
709 gtk_binding_entry_add_signal (binding_set,
712 1, G_TYPE_STRING, "/");
713 gtk_binding_entry_add_signal (binding_set,
716 1, G_TYPE_STRING, "/");
719 gtk_binding_entry_add_signal (binding_set,
722 1, G_TYPE_STRING, "~");
725 gtk_binding_entry_add_signal (binding_set,
726 GDK_v, GDK_CONTROL_MASK,
727 "location-popup-on-paste",
729 gtk_binding_entry_add_signal (binding_set,
730 GDK_Up, GDK_MOD1_MASK,
733 gtk_binding_entry_add_signal (binding_set,
737 gtk_binding_entry_add_signal (binding_set,
738 GDK_KP_Up, GDK_MOD1_MASK,
742 gtk_binding_entry_add_signal (binding_set,
743 GDK_Down, GDK_MOD1_MASK,
746 gtk_binding_entry_add_signal (binding_set,
747 GDK_KP_Down, GDK_MOD1_MASK,
751 gtk_binding_entry_add_signal (binding_set,
752 GDK_Home, GDK_MOD1_MASK,
755 gtk_binding_entry_add_signal (binding_set,
756 GDK_KP_Home, GDK_MOD1_MASK,
759 gtk_binding_entry_add_signal (binding_set,
760 GDK_d, GDK_MOD1_MASK,
763 gtk_binding_entry_add_signal (binding_set,
764 GDK_h, GDK_CONTROL_MASK,
767 gtk_binding_entry_add_signal (binding_set,
768 GDK_s, GDK_MOD1_MASK,
771 gtk_binding_entry_add_signal (binding_set,
772 GDK_r, GDK_MOD1_MASK,
776 for (i = 0; i < 10; i++)
777 gtk_binding_entry_add_signal (binding_set,
778 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
782 _gtk_file_chooser_install_properties (gobject_class);
786 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
788 iface->select_file = gtk_file_chooser_default_select_file;
789 iface->unselect_file = gtk_file_chooser_default_unselect_file;
790 iface->select_all = gtk_file_chooser_default_select_all;
791 iface->unselect_all = gtk_file_chooser_default_unselect_all;
792 iface->get_files = gtk_file_chooser_default_get_files;
793 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
794 iface->get_file_system = gtk_file_chooser_default_get_file_system;
795 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
796 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
797 iface->set_current_name = gtk_file_chooser_default_set_current_name;
798 iface->add_filter = gtk_file_chooser_default_add_filter;
799 iface->remove_filter = gtk_file_chooser_default_remove_filter;
800 iface->list_filters = gtk_file_chooser_default_list_filters;
801 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
802 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
803 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
807 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
809 iface->get_default_size = gtk_file_chooser_default_get_default_size;
810 iface->should_respond = gtk_file_chooser_default_should_respond;
811 iface->initial_focus = gtk_file_chooser_default_initial_focus;
815 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
817 profile_start ("start", NULL);
818 #ifdef PROFILE_FILE_CHOOSER
819 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
821 impl->local_only = TRUE;
822 impl->preview_widget_active = TRUE;
823 impl->use_preview_label = TRUE;
824 impl->select_multiple = FALSE;
825 impl->show_hidden = FALSE;
826 impl->icon_size = FALLBACK_ICON_SIZE;
827 impl->load_state = LOAD_EMPTY;
828 impl->reload_state = RELOAD_EMPTY;
829 impl->pending_select_files = NULL;
830 impl->location_mode = LOCATION_MODE_PATH_BAR;
831 impl->operation_mode = OPERATION_MODE_BROWSE;
833 gtk_box_set_spacing (GTK_BOX (impl), 12);
835 impl->tooltips = gtk_tooltips_new ();
836 g_object_ref_sink (impl->tooltips);
838 set_file_system_backend (impl);
840 profile_end ("end", NULL);
843 /* Frees the data columns for the specified iter in the shortcuts model*/
845 shortcuts_free_row_data (GtkFileChooserDefault *impl,
849 ShortcutType shortcut_type;
850 GCancellable *cancellable;
852 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
853 SHORTCUTS_COL_DATA, &col_data,
854 SHORTCUTS_COL_TYPE, &shortcut_type,
855 SHORTCUTS_COL_CANCELLABLE, &cancellable,
859 g_cancellable_cancel (cancellable);
861 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
862 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
866 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
868 GtkFileSystemVolume *volume;
871 gtk_file_system_volume_free (volume);
877 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
880 g_object_unref (file);
884 /* Frees all the data columns in the shortcuts model */
886 shortcuts_free (GtkFileChooserDefault *impl)
890 if (!impl->shortcuts_model)
893 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
896 shortcuts_free_row_data (impl, &iter);
898 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
900 g_object_unref (impl->shortcuts_model);
901 impl->shortcuts_model = NULL;
905 pending_select_files_free (GtkFileChooserDefault *impl)
907 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
908 g_slist_free (impl->pending_select_files);
909 impl->pending_select_files = NULL;
913 pending_select_files_add (GtkFileChooserDefault *impl,
916 impl->pending_select_files =
917 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
920 /* Used from gtk_tree_selection_selected_foreach() */
922 store_selection_foreach (GtkTreeModel *model,
927 GtkFileChooserDefault *impl;
928 GtkTreeIter child_iter;
931 impl = GTK_FILE_CHOOSER_DEFAULT (data);
933 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
935 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
936 pending_select_files_add (impl, file);
939 /* Stores the current selection in the list of paths to select; this is used to
940 * preserve the selection when reloading the current folder.
943 pending_select_files_store_selection (GtkFileChooserDefault *impl)
945 GtkTreeSelection *selection;
947 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
948 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
952 gtk_file_chooser_default_finalize (GObject *object)
954 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
957 if (impl->shortcuts_pane_filter_model)
958 g_object_unref (impl->shortcuts_pane_filter_model);
960 if (impl->shortcuts_combo_filter_model)
961 g_object_unref (impl->shortcuts_combo_filter_model);
963 shortcuts_free (impl);
965 g_object_unref (impl->file_system);
967 g_free (impl->browse_files_last_selected_name);
969 for (l = impl->filters; l; l = l->next)
971 GtkFileFilter *filter;
973 filter = GTK_FILE_FILTER (l->data);
974 g_object_unref (filter);
976 g_slist_free (impl->filters);
978 if (impl->current_filter)
979 g_object_unref (impl->current_filter);
981 if (impl->current_volume_file)
982 g_object_unref (impl->current_volume_file);
984 if (impl->current_folder)
985 g_object_unref (impl->current_folder);
987 if (impl->preview_file)
988 g_object_unref (impl->preview_file);
990 load_remove_timer (impl);
992 /* Free all the Models we have */
993 if (impl->browse_files_model)
994 g_object_unref (impl->browse_files_model);
996 if (impl->sort_model)
997 g_object_unref (impl->sort_model);
999 search_clear_model (impl, FALSE);
1000 recent_clear_model (impl, FALSE);
1002 g_free (impl->preview_display_name);
1004 g_free (impl->edited_new_text);
1006 g_object_unref (impl->tooltips);
1008 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
1011 /* Shows an error dialog set as transient for the specified window */
1013 error_message_with_parent (GtkWindow *parent,
1019 dialog = gtk_message_dialog_new (parent,
1020 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1025 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1029 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1031 gtk_dialog_run (GTK_DIALOG (dialog));
1032 gtk_widget_destroy (dialog);
1035 /* Returns a toplevel GtkWindow, or NULL if none */
1037 get_toplevel (GtkWidget *widget)
1039 GtkWidget *toplevel;
1041 toplevel = gtk_widget_get_toplevel (widget);
1042 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1045 return GTK_WINDOW (toplevel);
1048 /* Shows an error dialog for the file chooser */
1050 error_message (GtkFileChooserDefault *impl,
1054 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1057 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1059 error_dialog (GtkFileChooserDefault *impl,
1070 uri = g_file_get_uri (file);
1071 text = g_strdup_printf (msg, uri);
1072 error_message (impl, text, error->message);
1075 g_error_free (error);
1079 /* Displays an error message about not being able to get information for a file.
1080 * Frees the GError as well.
1083 error_getting_info_dialog (GtkFileChooserDefault *impl,
1088 _("Could not retrieve information about the file"),
1092 /* Shows an error dialog about not being able to add a bookmark */
1094 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1099 _("Could not add a bookmark"),
1103 /* Shows an error dialog about not being able to remove a bookmark */
1105 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1110 _("Could not remove bookmark"),
1114 /* Shows an error dialog about not being able to create a folder */
1116 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1121 _("The folder could not be created"),
1125 /* Shows an error about not being able to create a folder because a file with
1126 * the same name is already there.
1129 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1134 _("The folder could not be created, as a file with the same "
1135 "name already exists. Try using a different name for the "
1136 "folder, or rename the file first."),
1140 /* Shows an error dialog about not being able to create a filename */
1142 error_building_filename_dialog (GtkFileChooserDefault *impl,
1145 error_dialog (impl, _("Invalid file name"),
1149 /* Shows an error dialog when we cannot switch to a folder */
1151 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1155 error_dialog (impl, _("The folder contents could not be displayed"),
1159 /* Changes folders, displaying an error dialog if this fails */
1161 change_folder_and_display_error (GtkFileChooserDefault *impl,
1163 gboolean clear_entry)
1168 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1170 /* We copy the path because of this case:
1172 * list_row_activated()
1173 * fetches path from model; path belongs to the model (*)
1174 * calls change_folder_and_display_error()
1175 * calls _gtk_file_chooser_set_current_folder_file()
1176 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1180 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1183 error_changing_folder_dialog (impl, file, error);
1189 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1191 if (impl->use_preview_label)
1193 if (!impl->preview_label)
1195 impl->preview_label = gtk_label_new (impl->preview_display_name);
1196 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1197 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1198 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1199 gtk_widget_show (impl->preview_label);
1204 if (impl->preview_label)
1206 gtk_widget_destroy (impl->preview_label);
1207 impl->preview_label = NULL;
1211 if (impl->preview_widget_active && impl->preview_widget)
1212 gtk_widget_show (impl->preview_box);
1214 gtk_widget_hide (impl->preview_box);
1216 g_signal_emit_by_name (impl, "default-size-changed");
1220 set_preview_widget (GtkFileChooserDefault *impl,
1221 GtkWidget *preview_widget)
1223 if (preview_widget == impl->preview_widget)
1226 if (impl->preview_widget)
1227 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1228 impl->preview_widget);
1230 impl->preview_widget = preview_widget;
1231 if (impl->preview_widget)
1233 gtk_widget_show (impl->preview_widget);
1234 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1235 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1236 impl->preview_widget,
1237 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1240 update_preview_widget_visibility (impl);
1243 /* Renders a "Search" icon at an appropriate size for a tree view */
1245 render_search_icon (GtkFileChooserDefault *impl)
1247 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1251 render_recent_icon (GtkFileChooserDefault *impl)
1253 GtkIconTheme *theme;
1256 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1257 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1259 theme = gtk_icon_theme_get_default ();
1261 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1267 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1273 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1274 struct ReloadIconsData
1276 GtkFileChooserDefault *impl;
1277 GtkTreeRowReference *row_ref;
1281 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1283 const GError *error,
1289 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1290 struct ReloadIconsData *data = user_data;
1292 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1295 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1297 if (cancelled || error)
1300 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1302 path = gtk_tree_row_reference_get_path (data->row_ref);
1303 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1304 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1305 SHORTCUTS_COL_PIXBUF, pixbuf,
1307 gtk_tree_path_free (path);
1310 g_object_unref (pixbuf);
1313 gtk_tree_row_reference_free (data->row_ref);
1314 g_object_unref (data->impl);
1317 g_object_unref (cancellable);
1321 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1326 profile_start ("start", NULL);
1328 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1331 for (l = impl->reload_icon_cancellables; l; l = l->next)
1333 GCancellable *cancellable = G_CANCELLABLE (l->data);
1334 g_cancellable_cancel (cancellable);
1336 g_slist_free (impl->reload_icon_cancellables);
1337 impl->reload_icon_cancellables = NULL;
1342 ShortcutType shortcut_type;
1343 gboolean pixbuf_visible;
1346 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1347 SHORTCUTS_COL_DATA, &data,
1348 SHORTCUTS_COL_TYPE, &shortcut_type,
1349 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1355 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1357 GtkFileSystemVolume *volume;
1360 pixbuf = gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1361 impl->icon_size, NULL);
1363 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1365 if (g_file_is_native (G_FILE (data)))
1368 struct ReloadIconsData *info;
1369 GtkTreePath *tree_path;
1370 GCancellable *cancellable;
1374 info = g_new0 (struct ReloadIconsData, 1);
1375 info->impl = g_object_ref (impl);
1376 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1377 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1378 gtk_tree_path_free (tree_path);
1380 cancellable = gtk_file_system_get_info (impl->file_system, file,
1382 shortcuts_reload_icons_get_info_cb,
1384 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1388 GtkIconTheme *icon_theme;
1390 /* Don't call get_info for remote paths to avoid latency and
1392 * If we switch to a better bookmarks file format (XBEL), we
1393 * should use mime info to get a better icon.
1395 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1396 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share",
1397 impl->icon_size, 0, NULL);
1400 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1402 pixbuf = render_search_icon (impl);
1404 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1406 pixbuf = render_recent_icon (impl);
1409 gtk_list_store_set (impl->shortcuts_model, &iter,
1410 SHORTCUTS_COL_PIXBUF, pixbuf,
1414 g_object_unref (pixbuf);
1418 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1422 profile_end ("end", NULL);
1426 shortcuts_find_folder (GtkFileChooserDefault *impl,
1429 GtkTreeSelection *selection;
1433 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1435 g_assert (folder != NULL);
1436 pos = shortcut_find_position (impl, folder);
1439 gtk_tree_selection_unselect_all (selection);
1443 path = gtk_tree_path_new_from_indices (pos, -1);
1444 gtk_tree_selection_select_path (selection, path);
1445 gtk_tree_path_free (path);
1448 /* If a shortcut corresponds to the current folder, selects it */
1450 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1452 shortcuts_find_folder (impl, impl->current_folder);
1455 /* Removes the specified number of rows from the shortcuts list */
1457 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1463 path = gtk_tree_path_new_from_indices (start_row, -1);
1465 for (; n_rows; n_rows--)
1469 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1470 g_assert_not_reached ();
1472 shortcuts_free_row_data (impl, &iter);
1473 gtk_list_store_remove (impl->shortcuts_model, &iter);
1476 gtk_tree_path_free (path);
1480 shortcuts_update_count (GtkFileChooserDefault *impl,
1481 ShortcutsIndex type,
1486 case SHORTCUTS_HOME:
1488 impl->has_home = FALSE;
1490 impl->has_home = TRUE;
1493 case SHORTCUTS_DESKTOP:
1495 impl->has_desktop = FALSE;
1497 impl->has_desktop = TRUE;
1500 case SHORTCUTS_VOLUMES:
1501 impl->num_volumes += value;
1504 case SHORTCUTS_SHORTCUTS:
1505 impl->num_shortcuts += value;
1508 case SHORTCUTS_BOOKMARKS:
1509 impl->num_bookmarks += value;
1512 case SHORTCUTS_CURRENT_FOLDER:
1514 impl->shortcuts_current_folder_active = FALSE;
1516 impl->shortcuts_current_folder_active = TRUE;
1525 struct ShortcutsInsertRequest
1527 GtkFileChooserDefault *impl;
1531 GtkTreeRowReference *row_ref;
1532 ShortcutsIndex type;
1538 get_file_info_finished (GCancellable *cancellable,
1540 const GError *error,
1544 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1548 GCancellable *model_cancellable;
1549 struct ShortcutsInsertRequest *request = data;
1551 path = gtk_tree_row_reference_get_path (request->row_ref);
1553 /* Handle doesn't exist anymore in the model */
1556 pos = gtk_tree_path_get_indices (path)[0];
1557 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1559 gtk_tree_path_free (path);
1561 /* validate cancellable, else goto out */
1562 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1563 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1565 if (cancellable != model_cancellable)
1568 /* set the cancellable to NULL in the model (we unref later on) */
1569 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1570 SHORTCUTS_COL_CANCELLABLE, NULL,
1578 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1579 shortcuts_update_count (request->impl, request->type, -1);
1581 if (request->type == SHORTCUTS_HOME)
1585 home = g_file_new_for_path (g_get_home_dir ());
1586 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1587 g_object_unref (home);
1589 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1591 /* Remove the current folder separator */
1592 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1593 shortcuts_remove_rows (request->impl, separator_pos, 1);
1599 if (!request->label_copy)
1600 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1601 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1602 request->impl->icon_size);
1604 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1605 SHORTCUTS_COL_PIXBUF, pixbuf,
1606 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1607 SHORTCUTS_COL_NAME, request->label_copy,
1608 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1609 SHORTCUTS_COL_REMOVABLE, request->removable,
1612 if (request->impl->shortcuts_pane_filter_model)
1613 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1615 if (request->impl->shortcuts_combo_filter_model)
1616 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1618 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1619 request->impl->save_folder_combo != NULL)
1621 /* The current folder is updated via _activate_iter(), don't
1622 * have save_folder_combo_changed_cb() call _activate_iter()
1625 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1626 G_CALLBACK (save_folder_combo_changed_cb),
1629 if (request->impl->has_search)
1632 if (request->impl->has_recent)
1635 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1636 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1637 G_CALLBACK (save_folder_combo_changed_cb),
1642 g_object_unref (pixbuf);
1645 g_object_unref (request->impl);
1646 g_object_unref (request->file);
1647 gtk_tree_row_reference_free (request->row_ref);
1648 g_free (request->label_copy);
1651 g_object_unref (cancellable);
1654 /* FIXME: GtkFileSystem needs a function to split a remote path
1655 * into hostname and path components, or maybe just have a
1656 * gtk_file_system_path_get_display_name().
1658 * This function is also used in gtkfilechooserbutton.c
1661 _gtk_file_chooser_label_for_file (GFile *file)
1663 const gchar *path, *start, *end, *p;
1664 gchar *uri, *host, *label;
1666 uri = g_file_get_uri (file);
1668 start = strstr (uri, "://");
1670 path = strchr (start, '/');
1676 end = uri + strlen (uri);
1680 /* strip username */
1681 p = strchr (start, '@');
1687 p = strchr (start, ':');
1691 host = g_strndup (start, end - start);
1693 /* Translators: the first string is a path and the second string
1694 * is a hostname. Nautilus and the panel contain the same string
1697 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1705 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1706 * inserts a volume. A position of -1 indicates the end of the tree.
1709 shortcuts_insert_file (GtkFileChooserDefault *impl,
1711 ShortcutType shortcut_type,
1712 GtkFileSystemVolume *volume,
1716 ShortcutsIndex type)
1719 GdkPixbuf *pixbuf = NULL;
1720 gpointer data = NULL;
1722 GtkIconTheme *icon_theme;
1724 profile_start ("start shortcut", NULL);
1726 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1729 label_copy = gtk_file_system_volume_get_display_name (volume);
1730 pixbuf = gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1731 impl->icon_size, NULL);
1733 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1735 if (g_file_is_native (file))
1737 struct ShortcutsInsertRequest *request;
1738 GCancellable *cancellable;
1741 request = g_new0 (struct ShortcutsInsertRequest, 1);
1742 request->impl = g_object_ref (impl);
1743 request->file = g_object_ref (file);
1744 request->name_only = TRUE;
1745 request->removable = removable;
1747 request->type = type;
1749 request->label_copy = g_strdup (label);
1752 gtk_list_store_append (impl->shortcuts_model, &iter);
1754 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1756 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1757 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1758 gtk_tree_path_free (p);
1760 cancellable = gtk_file_system_get_info (request->impl->file_system, request->file,
1761 "standard::is-hidden,standard::display-name,standard::icon",
1762 get_file_info_finished, request);
1764 gtk_list_store_set (impl->shortcuts_model, &iter,
1765 SHORTCUTS_COL_DATA, g_object_ref (file),
1766 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1767 SHORTCUTS_COL_CANCELLABLE, cancellable,
1770 shortcuts_update_count (impl, type, 1);
1776 /* Don't call get_info for remote paths to avoid latency and
1779 data = g_object_ref (file);
1781 label_copy = g_strdup (label);
1783 label_copy = _gtk_file_chooser_label_for_file (file);
1785 /* If we switch to a better bookmarks file format (XBEL), we
1786 * should use mime info to get a better icon.
1788 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1789 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share",
1790 impl->icon_size, 0, NULL);
1795 g_assert_not_reached ();
1801 gtk_list_store_append (impl->shortcuts_model, &iter);
1803 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1805 shortcuts_update_count (impl, type, 1);
1807 gtk_list_store_set (impl->shortcuts_model, &iter,
1808 SHORTCUTS_COL_PIXBUF, pixbuf,
1809 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1810 SHORTCUTS_COL_NAME, label_copy,
1811 SHORTCUTS_COL_DATA, data,
1812 SHORTCUTS_COL_TYPE, shortcut_type,
1813 SHORTCUTS_COL_REMOVABLE, removable,
1814 SHORTCUTS_COL_CANCELLABLE, NULL,
1817 if (impl->shortcuts_pane_filter_model)
1818 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1820 if (impl->shortcuts_combo_filter_model)
1821 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1823 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1825 /* The current folder is updated via _activate_iter(), don't
1826 * have save_folder_combo_changed_cb() call _activate_iter()
1829 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1831 if (impl->has_search)
1834 if (impl->has_recent)
1837 g_signal_handlers_block_by_func (impl->save_folder_combo,
1838 G_CALLBACK (save_folder_combo_changed_cb),
1841 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1842 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1843 G_CALLBACK (save_folder_combo_changed_cb),
1847 g_free (label_copy);
1850 g_object_unref (pixbuf);
1852 profile_end ("end", NULL);
1856 shortcuts_append_search (GtkFileChooserDefault *impl)
1861 pixbuf = render_search_icon (impl);
1863 gtk_list_store_append (impl->shortcuts_model, &iter);
1864 gtk_list_store_set (impl->shortcuts_model, &iter,
1865 SHORTCUTS_COL_PIXBUF, pixbuf,
1866 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1867 SHORTCUTS_COL_NAME, _("Search"),
1868 SHORTCUTS_COL_DATA, NULL,
1869 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1870 SHORTCUTS_COL_REMOVABLE, FALSE,
1874 g_object_unref (pixbuf);
1876 impl->has_search = TRUE;
1880 shortcuts_append_recent (GtkFileChooserDefault *impl)
1885 pixbuf = render_recent_icon (impl);
1887 gtk_list_store_append (impl->shortcuts_model, &iter);
1888 gtk_list_store_set (impl->shortcuts_model, &iter,
1889 SHORTCUTS_COL_PIXBUF, pixbuf,
1890 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1891 SHORTCUTS_COL_NAME, _("Recently Used"),
1892 SHORTCUTS_COL_DATA, NULL,
1893 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1894 SHORTCUTS_COL_REMOVABLE, FALSE,
1898 g_object_unref (pixbuf);
1900 impl->has_recent = TRUE;
1903 /* Appends an item for the user's home directory to the shortcuts model */
1905 shortcuts_append_home (GtkFileChooserDefault *impl)
1907 const char *home_path;
1910 profile_start ("start", NULL);
1912 home_path = g_get_home_dir ();
1913 if (home_path == NULL)
1915 profile_end ("end - no home directory!?", NULL);
1919 home = g_file_new_for_path (home_path);
1920 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1921 impl->has_home = TRUE;
1923 g_object_unref (home);
1925 profile_end ("end", NULL);
1928 /* Appends the ~/Desktop directory to the shortcuts model */
1930 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1935 profile_start ("start", NULL);
1937 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1938 file = g_file_new_for_path (name);
1939 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1940 impl->has_desktop = TRUE;
1942 /* We do not actually pop up an error dialog if there is no desktop directory
1943 * because some people may really not want to have one.
1946 g_object_unref (file);
1948 profile_end ("end", NULL);
1951 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1953 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1960 profile_start ("start", NULL);
1962 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1965 for (; bookmarks; bookmarks = bookmarks->next)
1969 file = bookmarks->data;
1971 if (impl->local_only && !g_file_is_native (file))
1974 if (shortcut_find_position (impl, file) != -1)
1977 label = gtk_file_system_get_bookmark_label (impl->file_system, file);
1979 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1983 profile_end ("end", NULL);
1985 return num_inserted;
1988 /* Returns the index for the corresponding item in the shortcuts bar */
1990 shortcuts_get_index (GtkFileChooserDefault *impl,
1991 ShortcutsIndex where)
1997 if (where == SHORTCUTS_SEARCH)
2000 n += impl->has_search ? 1 : 0;
2002 if (where == SHORTCUTS_RECENT)
2005 n += impl->has_recent ? 1 : 0;
2007 if (where == SHORTCUTS_RECENT_SEPARATOR)
2010 n += impl->has_recent ? 1 : 0;
2012 if (where == SHORTCUTS_HOME)
2015 n += impl->has_home ? 1 : 0;
2017 if (where == SHORTCUTS_DESKTOP)
2020 n += impl->has_desktop ? 1 : 0;
2022 if (where == SHORTCUTS_VOLUMES)
2025 n += impl->num_volumes;
2027 if (where == SHORTCUTS_SHORTCUTS)
2030 n += impl->num_shortcuts;
2032 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2035 /* If there are no bookmarks there won't be a separator */
2036 n += (impl->num_bookmarks > 0) ? 1 : 0;
2038 if (where == SHORTCUTS_BOOKMARKS)
2041 n += impl->num_bookmarks;
2043 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2048 if (where == SHORTCUTS_CURRENT_FOLDER)
2051 g_assert_not_reached ();
2058 /* Adds all the file system volumes to the shortcuts model */
2060 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2065 gboolean old_changing_folders;
2067 profile_start ("start", NULL);
2069 old_changing_folders = impl->changing_folder;
2070 impl->changing_folder = TRUE;
2072 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2073 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2074 impl->num_volumes = 0;
2076 list = gtk_file_system_list_volumes (impl->file_system);
2080 for (l = list; l; l = l->next)
2082 GtkFileSystemVolume *volume;
2086 if (impl->local_only)
2088 if (gtk_file_system_volume_is_mounted (volume))
2092 base_file = gtk_file_system_volume_get_root (volume);
2093 if (base_file != NULL && !g_file_is_native (base_file))
2098 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2102 impl->num_volumes = n;
2103 g_slist_free (list);
2105 if (impl->shortcuts_pane_filter_model)
2106 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2108 if (impl->shortcuts_combo_filter_model)
2109 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2111 impl->changing_folder = old_changing_folders;
2113 profile_end ("end", NULL);
2116 /* Inserts a separator node in the shortcuts list */
2118 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2119 ShortcutsIndex where)
2123 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2124 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2125 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2127 gtk_list_store_insert (impl->shortcuts_model, &iter,
2128 shortcuts_get_index (impl, where));
2129 gtk_list_store_set (impl->shortcuts_model, &iter,
2130 SHORTCUTS_COL_PIXBUF, NULL,
2131 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2132 SHORTCUTS_COL_NAME, NULL,
2133 SHORTCUTS_COL_DATA, NULL,
2134 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2138 /* Updates the list of bookmarks */
2140 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2143 gboolean old_changing_folders;
2145 GFile *list_selected = NULL;
2146 GFile *combo_selected = NULL;
2147 ShortcutType shortcut_type;
2150 profile_start ("start", NULL);
2152 old_changing_folders = impl->changing_folder;
2153 impl->changing_folder = TRUE;
2155 if (shortcuts_get_selected (impl, &iter))
2157 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2159 SHORTCUTS_COL_DATA, &col_data,
2160 SHORTCUTS_COL_TYPE, &shortcut_type,
2163 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2164 list_selected = g_object_ref (col_data);
2167 if (impl->save_folder_combo &&
2168 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2171 GtkTreeIter child_iter;
2173 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2176 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2178 SHORTCUTS_COL_DATA, &col_data,
2179 SHORTCUTS_COL_TYPE, &shortcut_type,
2182 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2183 combo_selected = g_object_ref (col_data);
2186 if (impl->num_bookmarks > 0)
2187 shortcuts_remove_rows (impl,
2188 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2189 impl->num_bookmarks + 1);
2191 impl->num_bookmarks = 0;
2192 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2194 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
2195 shortcuts_append_bookmarks (impl, bookmarks);
2196 g_slist_free (bookmarks);
2198 if (impl->num_bookmarks == 0)
2199 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2201 if (impl->shortcuts_pane_filter_model)
2202 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2204 if (impl->shortcuts_combo_filter_model)
2205 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2209 shortcuts_find_folder (impl, list_selected);
2210 g_object_unref (list_selected);
2217 pos = shortcut_find_position (impl, combo_selected);
2220 if (impl->has_search)
2223 if (impl->has_recent)
2226 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2229 g_object_unref (combo_selected);
2232 impl->changing_folder = old_changing_folders;
2234 profile_end ("end", NULL);
2237 /* Appends a separator and a row to the shortcuts list for the current folder */
2239 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2244 g_assert (!impl->shortcuts_current_folder_active);
2248 g_assert (impl->current_folder != NULL);
2250 pos = shortcut_find_position (impl, impl->current_folder);
2253 GtkFileSystemVolume *volume;
2258 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2262 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2264 volume = gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2266 base_file = gtk_file_system_volume_get_root (volume);
2270 if (base_file && g_file_equal (base_file, impl->current_folder))
2271 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2273 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2276 g_object_unref (base_file);
2278 else if (impl->save_folder_combo != NULL)
2280 if (impl->has_search)
2283 if (impl->has_recent)
2284 pos -= 2; /* + separator */
2286 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2290 /* Updates the current folder row in the shortcuts model */
2292 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2296 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2298 if (impl->shortcuts_current_folder_active)
2300 shortcuts_remove_rows (impl, pos, 2);
2301 impl->shortcuts_current_folder_active = FALSE;
2304 shortcuts_add_current_folder (impl);
2307 /* Filter function used for the shortcuts filter model */
2309 shortcuts_pane_filter_cb (GtkTreeModel *model,
2313 GtkFileChooserDefault *impl;
2317 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2319 path = gtk_tree_model_get_path (model, iter);
2323 pos = *gtk_tree_path_get_indices (path);
2324 gtk_tree_path_free (path);
2326 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2329 /* Creates the list model for shortcuts */
2331 shortcuts_model_create (GtkFileChooserDefault *impl)
2333 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2334 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2335 GDK_TYPE_PIXBUF, /* pixbuf */
2336 G_TYPE_STRING, /* name */
2337 G_TYPE_POINTER, /* path or volume */
2338 G_TYPE_INT, /* ShortcutType */
2339 G_TYPE_BOOLEAN, /* removable */
2340 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2341 G_TYPE_POINTER); /* GCancellable */
2343 if (search_is_possible (impl))
2345 shortcuts_append_search (impl);
2348 if (impl->recent_manager)
2350 shortcuts_append_recent (impl);
2351 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2354 if (impl->file_system)
2356 shortcuts_append_home (impl);
2357 shortcuts_append_desktop (impl);
2358 shortcuts_add_volumes (impl);
2361 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2362 GTK_TREE_MODEL (impl->shortcuts_model),
2365 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2366 shortcuts_pane_filter_cb,
2371 /* Callback used when the "New Folder" button is clicked */
2373 new_folder_button_clicked (GtkButton *button,
2374 GtkFileChooserDefault *impl)
2379 if (!impl->browse_files_model)
2380 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2382 /* Prevent button from being clicked twice */
2383 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2385 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2387 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2388 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2389 path, impl->list_name_column,
2392 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2393 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2395 impl->list_name_column,
2398 gtk_tree_path_free (path);
2401 /* Idle handler for creating a new folder after editing its name cell, or for
2402 * canceling the editing.
2405 edited_idle_cb (GtkFileChooserDefault *impl)
2407 GDK_THREADS_ENTER ();
2409 g_source_destroy (impl->edited_idle);
2410 impl->edited_idle = NULL;
2412 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2413 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2415 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2417 if (impl->edited_new_text) /* not cancelled? */
2419 GError *error = NULL;
2422 file = g_file_get_child_for_display_name (impl->current_folder,
2423 impl->edited_new_text,
2427 GError *error = NULL;
2429 if (!g_file_make_directory (file, NULL, &error))
2430 change_folder_and_display_error (impl, file, FALSE);
2432 error_creating_folder_dialog (impl, file, error);
2434 g_object_unref (file);
2437 error_creating_folder_dialog (impl, file, error);
2439 g_free (impl->edited_new_text);
2440 impl->edited_new_text = NULL;
2443 GDK_THREADS_LEAVE ();
2449 queue_edited_idle (GtkFileChooserDefault *impl,
2450 const gchar *new_text)
2452 /* We create the folder in an idle handler so that we don't modify the tree
2456 if (!impl->edited_idle)
2458 impl->edited_idle = g_idle_source_new ();
2459 g_source_set_closure (impl->edited_idle,
2460 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2462 g_source_attach (impl->edited_idle, NULL);
2465 g_free (impl->edited_new_text);
2466 impl->edited_new_text = g_strdup (new_text);
2469 /* Callback used from the text cell renderer when the new folder is named */
2471 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2473 const gchar *new_text,
2474 GtkFileChooserDefault *impl)
2476 /* work around bug #154921 */
2477 g_object_set (cell_renderer_text,
2478 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2479 queue_edited_idle (impl, new_text);
2482 /* Callback used from the text cell renderer when the new folder edition gets
2486 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2487 GtkFileChooserDefault *impl)
2489 /* work around bug #154921 */
2490 g_object_set (cell_renderer_text,
2491 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2492 queue_edited_idle (impl, NULL);
2495 /* Creates the widgets for the filter combo box */
2497 filter_create (GtkFileChooserDefault *impl)
2499 impl->filter_combo = gtk_combo_box_new_text ();
2500 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2502 g_signal_connect (impl->filter_combo, "changed",
2503 G_CALLBACK (filter_combo_changed), impl);
2505 gtk_widget_set_tooltip_text (impl->filter_combo,
2506 _("Select which types of files are shown"));
2508 return impl->filter_combo;
2512 button_new (GtkFileChooserDefault *impl,
2514 const char *stock_id,
2522 button = gtk_button_new_with_mnemonic (text);
2523 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2524 gtk_button_set_image (GTK_BUTTON (button), image);
2526 gtk_widget_set_sensitive (button, sensitive);
2527 g_signal_connect (button, "clicked", callback, impl);
2530 gtk_widget_show (button);
2535 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2537 shortcut_find_position (GtkFileChooserDefault *impl,
2542 int current_folder_separator_idx;
2544 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2547 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2550 /* FIXME: is this still needed? */
2551 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2555 for (i = 0; i < current_folder_separator_idx; i++)
2558 ShortcutType shortcut_type;
2560 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2561 SHORTCUTS_COL_DATA, &col_data,
2562 SHORTCUTS_COL_TYPE, &shortcut_type,
2567 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2569 GtkFileSystemVolume *volume;
2574 base_file = gtk_file_system_volume_get_root (volume);
2576 exists = base_file && g_file_equal (file, base_file);
2579 g_object_unref (base_file);
2584 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2588 model_file = col_data;
2590 if (model_file && g_file_equal (model_file, file))
2595 if (i < current_folder_separator_idx - 1)
2597 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2598 g_assert_not_reached ();
2605 /* Tries to add a bookmark from a path name */
2607 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2613 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2615 if (shortcut_find_position (impl, file) != -1)
2619 if (!gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2621 error_adding_bookmark_dialog (impl, file, error);
2629 add_bookmark_foreach_cb (GtkTreeModel *model,
2634 GtkFileChooserDefault *impl;
2635 GtkFileSystemModel *fs_model;
2636 GtkTreeIter child_iter;
2639 impl = (GtkFileChooserDefault *) data;
2641 switch (impl->operation_mode)
2643 case OPERATION_MODE_BROWSE:
2644 fs_model = impl->browse_files_model;
2645 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2646 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2649 case OPERATION_MODE_SEARCH:
2650 search_get_valid_child_iter (impl, &child_iter, iter);
2651 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2652 SEARCH_MODEL_COL_FILE, &file,
2656 case OPERATION_MODE_RECENT:
2657 recent_get_valid_child_iter (impl, &child_iter, iter);
2658 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2659 RECENT_MODEL_COL_FILE, &file,
2664 shortcuts_add_bookmark_from_file (impl, file, -1);
2667 /* Adds a bookmark from the currently selected item in the file list */
2669 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2671 GtkTreeSelection *selection;
2673 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2675 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2676 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2678 gtk_tree_selection_selected_foreach (selection,
2679 add_bookmark_foreach_cb,
2683 /* Callback used when the "Add bookmark" button is clicked */
2685 add_bookmark_button_clicked_cb (GtkButton *button,
2686 GtkFileChooserDefault *impl)
2688 bookmarks_add_selected_folder (impl);
2691 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2692 * returns FALSE if no shortcut is selected.
2695 shortcuts_get_selected (GtkFileChooserDefault *impl,
2698 GtkTreeSelection *selection;
2699 GtkTreeIter parent_iter;
2701 if (!impl->browse_shortcuts_tree_view)
2704 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2706 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2709 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2715 /* Removes the selected bookmarks */
2717 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2725 if (!shortcuts_get_selected (impl, &iter))
2728 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2729 SHORTCUTS_COL_DATA, &col_data,
2730 SHORTCUTS_COL_REMOVABLE, &removable,
2736 g_assert (col_data != NULL);
2741 if (!gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2742 error_removing_bookmark_dialog (impl, file, error);
2745 /* Callback used when the "Remove bookmark" button is clicked */
2747 remove_bookmark_button_clicked_cb (GtkButton *button,
2748 GtkFileChooserDefault *impl)
2750 remove_selected_bookmarks (impl);
2753 struct selection_check_closure {
2754 GtkFileChooserDefault *impl;
2757 gboolean all_folders;
2760 /* Used from gtk_tree_selection_selected_foreach() */
2762 selection_check_foreach_cb (GtkTreeModel *model,
2767 struct selection_check_closure *closure;
2768 GtkTreeIter child_iter;
2773 closure->num_selected++;
2775 switch (closure->impl->operation_mode)
2777 case OPERATION_MODE_BROWSE:
2778 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2779 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2780 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2783 case OPERATION_MODE_SEARCH:
2784 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2785 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2786 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2790 case OPERATION_MODE_RECENT:
2791 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2792 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2793 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2798 closure->all_folders = closure->all_folders && is_folder;
2799 closure->all_files = closure->all_files && !is_folder;
2802 /* Checks whether the selected items in the file list are all files or all folders */
2804 selection_check (GtkFileChooserDefault *impl,
2806 gboolean *all_files,
2807 gboolean *all_folders)
2809 struct selection_check_closure closure;
2810 GtkTreeSelection *selection;
2812 closure.impl = impl;
2813 closure.num_selected = 0;
2814 closure.all_files = TRUE;
2815 closure.all_folders = TRUE;
2817 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2818 gtk_tree_selection_selected_foreach (selection,
2819 selection_check_foreach_cb,
2822 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2825 *num_selected = closure.num_selected;
2828 *all_files = closure.all_files;
2831 *all_folders = closure.all_folders;
2834 struct get_selected_file_closure {
2835 GtkFileChooserDefault *impl;
2840 get_selected_file_foreach_cb (GtkTreeModel *model,
2845 struct get_selected_file_closure *closure;
2846 GtkTreeIter child_iter;
2850 switch (closure->impl->operation_mode)
2852 case OPERATION_MODE_BROWSE:
2853 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2854 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2857 case OPERATION_MODE_SEARCH:
2858 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2859 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2860 SEARCH_MODEL_COL_FILE, &closure->file,
2864 case OPERATION_MODE_RECENT:
2865 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2866 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2867 RECENT_MODEL_COL_FILE, &closure->file,
2873 /* Returns a selected path from the file list */
2875 get_selected_file (GtkFileChooserDefault *impl)
2877 struct get_selected_file_closure closure;
2878 GtkTreeSelection *selection;
2880 closure.impl = impl;
2881 closure.file = NULL;
2883 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2884 gtk_tree_selection_selected_foreach (selection,
2885 get_selected_file_foreach_cb,
2888 return closure.file;
2892 GtkFileChooserDefault *impl;
2894 } UpdateTooltipData;
2897 update_tooltip (GtkTreeModel *model,
2902 UpdateTooltipData *udata = data;
2903 GtkTreeIter child_iter;
2906 if (udata->tip == NULL)
2908 const gchar *display_name;
2910 switch (udata->impl->operation_mode)
2912 case OPERATION_MODE_BROWSE:
2913 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2916 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2917 display_name = g_file_info_get_display_name (info);
2920 case OPERATION_MODE_SEARCH:
2921 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2922 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2923 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2927 case OPERATION_MODE_RECENT:
2928 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2929 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2930 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2935 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2941 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2942 * if there are no selected items *and* the current folder is not in the
2943 * bookmarks list. De-sensitize the button otherwise.
2946 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2949 gboolean all_folders;
2953 selection_check (impl, &num_selected, NULL, &all_folders);
2955 if (num_selected == 0)
2956 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2957 else if (num_selected == 1)
2961 file = get_selected_file (impl);
2962 active = all_folders && (shortcut_find_position (impl, file) == -1);
2965 active = all_folders;
2967 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2969 if (impl->browse_files_popup_menu_add_shortcut_item)
2970 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2971 (num_selected == 0) ? FALSE : active);
2975 if (num_selected == 0)
2976 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2977 else if (num_selected > 1)
2978 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2981 GtkTreeSelection *selection;
2982 UpdateTooltipData data;
2984 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2987 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2991 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2996 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2997 * bookmark row is selected in the shortcuts tree.
3000 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
3003 gboolean removable = FALSE;
3006 if (shortcuts_get_selected (impl, &iter))
3007 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3008 SHORTCUTS_COL_REMOVABLE, &removable,
3009 SHORTCUTS_COL_NAME, &name,
3012 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3018 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3019 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3027 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3030 gboolean removable = FALSE;
3032 if (impl->browse_shortcuts_popup_menu == NULL)
3035 if (shortcuts_get_selected (impl, &iter))
3036 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3037 SHORTCUTS_COL_REMOVABLE, &removable,
3040 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3041 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3044 /* GtkWidget::drag-begin handler for the shortcuts list. */
3046 shortcuts_drag_begin_cb (GtkWidget *widget,
3047 GdkDragContext *context,
3048 GtkFileChooserDefault *impl)
3051 impl->shortcuts_drag_context = g_object_ref (context);
3056 /* Removes the idle handler for outside drags */
3058 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3060 if (!impl->shortcuts_drag_outside_idle)
3063 g_source_destroy (impl->shortcuts_drag_outside_idle);
3064 impl->shortcuts_drag_outside_idle = NULL;
3068 /* GtkWidget::drag-end handler for the shortcuts list. */
3070 shortcuts_drag_end_cb (GtkWidget *widget,
3071 GdkDragContext *context,
3072 GtkFileChooserDefault *impl)
3075 g_object_unref (impl->shortcuts_drag_context);
3077 shortcuts_cancel_drag_outside_idle (impl);
3079 if (!impl->shortcuts_drag_outside)
3082 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3084 impl->shortcuts_drag_outside = FALSE;
3088 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3090 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3091 GdkDragContext *context,
3092 GtkFileChooserDefault *impl)
3094 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3098 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3102 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3105 GtkTreeView *tree_view;
3108 GdkPixmap *row_pixmap;
3113 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3115 /* Find the selected path and get its drag pixmap */
3117 if (!shortcuts_get_selected (impl, &iter))
3118 g_assert_not_reached ();
3120 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3122 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3123 gtk_tree_path_free (path);
3132 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3138 GdkPixmap *composite;
3139 int row_pixmap_width, row_pixmap_height;
3140 int pixbuf_width, pixbuf_height;
3141 int composite_width, composite_height;
3142 int pixbuf_x, pixbuf_y;
3143 GdkGC *gc, *mask_gc;
3145 GdkBitmap *pixbuf_mask;
3147 /* Create pixmap and mask for composite image */
3149 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3150 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3151 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3153 composite_width = MAX (row_pixmap_width, pixbuf_width);
3154 composite_height = MAX (row_pixmap_height, pixbuf_height);
3156 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3158 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3161 pixbuf_x = composite_width - pixbuf_width;
3163 pixbuf_y = (composite_height - pixbuf_height) / 2;
3165 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3166 gc = gdk_gc_new (composite);
3168 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3169 mask_gc = gdk_gc_new (mask);
3171 gdk_gc_set_foreground (mask_gc, &color);
3172 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3175 color.green = 0xffff;
3176 color.blue = 0xffff;
3177 gdk_gc_set_rgb_fg_color (gc, &color);
3178 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3180 /* Composite the row pixmap and the pixbuf */
3182 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3184 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3185 NULL, &pixbuf_mask, 128);
3186 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3189 pixbuf_width, pixbuf_height);
3190 g_object_unref (pixbuf_mask);
3192 gdk_draw_drawable (composite, gc, row_pixmap,
3195 row_pixmap_width, row_pixmap_height);
3197 gdk_gc_set_foreground (mask_gc, &color);
3198 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3200 gdk_draw_pixbuf (composite, gc, pixbuf,
3203 pixbuf_width, pixbuf_height,
3207 g_object_unref (pixbuf);
3208 g_object_unref (row_pixmap);
3210 row_pixmap = composite;
3214 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3216 gtk_tree_view_get_path_at_pos (tree_view,
3217 tree_view->priv->press_start_x,
3218 tree_view->priv->press_start_y,
3224 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3225 gdk_drawable_get_colormap (row_pixmap),
3228 tree_view->priv->press_start_x + 1,
3229 row_pixmap_y + cell_y + 1);
3231 g_object_unref (row_pixmap);
3233 g_object_unref (mask);
3236 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3237 * handler so that we can tell apart the drag_leave event that comes right
3238 * before a drag_drop, from a normal drag_leave. We don't want to set the
3239 * cursor nor the flag in the latter case.
3242 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3244 GDK_THREADS_ENTER ();
3246 shortcuts_drag_set_delete_cursor (impl, TRUE);
3247 impl->shortcuts_drag_outside = TRUE;
3249 shortcuts_cancel_drag_outside_idle (impl);
3251 GDK_THREADS_LEAVE ();
3257 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3261 shortcuts_drag_leave_cb (GtkWidget *widget,
3262 GdkDragContext *context,
3264 GtkFileChooserDefault *impl)
3267 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3269 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3270 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3271 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3273 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3277 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3279 GTK_TREE_VIEW_DROP_BEFORE);
3281 g_signal_stop_emission_by_name (widget, "drag_leave");
3284 /* Computes the appropriate row and position for dropping */
3286 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3290 GtkTreeViewDropPosition *pos)
3292 GtkTreeView *tree_view;
3293 GtkTreeViewColumn *column;
3297 int bookmarks_index;
3299 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3301 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3303 if (!gtk_tree_view_get_path_at_pos (tree_view,
3305 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3311 row = bookmarks_index + impl->num_bookmarks - 1;
3312 *path = gtk_tree_path_new_from_indices (row, -1);
3313 *pos = GTK_TREE_VIEW_DROP_AFTER;
3317 row = *gtk_tree_path_get_indices (*path);
3318 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3319 gtk_tree_path_free (*path);
3321 if (row < bookmarks_index)
3323 row = bookmarks_index;
3324 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3326 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3328 row = bookmarks_index + impl->num_bookmarks - 1;
3329 *pos = GTK_TREE_VIEW_DROP_AFTER;
3333 if (cell_y < cell.height / 2)
3334 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3336 *pos = GTK_TREE_VIEW_DROP_AFTER;
3339 *path = gtk_tree_path_new_from_indices (row, -1);
3342 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3343 * implement the destination side of DnD by hand, due to limitations in
3344 * GtkTreeView's DnD API.
3347 shortcuts_drag_motion_cb (GtkWidget *widget,
3348 GdkDragContext *context,
3352 GtkFileChooserDefault *impl)
3355 GtkTreeViewDropPosition pos;
3356 GdkDragAction action;
3359 if (gtk_drag_get_source_widget (context) == widget)
3361 shortcuts_cancel_drag_outside_idle (impl);
3363 if (impl->shortcuts_drag_outside)
3365 shortcuts_drag_set_delete_cursor (impl, FALSE);
3366 impl->shortcuts_drag_outside = FALSE;
3371 if (context->suggested_action == GDK_ACTION_COPY ||
3372 (context->actions & GDK_ACTION_COPY) != 0)
3373 action = GDK_ACTION_COPY;
3374 else if (context->suggested_action == GDK_ACTION_MOVE ||
3375 (context->actions & GDK_ACTION_MOVE) != 0)
3376 action = GDK_ACTION_MOVE;
3383 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3384 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3385 gtk_tree_path_free (path);
3389 g_signal_stop_emission_by_name (widget, "drag_motion");
3393 gdk_drag_status (context, action, time_);
3400 /* GtkWidget::drag-drop handler for the shortcuts list. */
3402 shortcuts_drag_drop_cb (GtkWidget *widget,
3403 GdkDragContext *context,
3407 GtkFileChooserDefault *impl)
3410 shortcuts_cancel_drag_outside_idle (impl);
3413 g_signal_stop_emission_by_name (widget, "drag_drop");
3417 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3419 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3426 uris = g_uri_list_extract_uris (data);
3428 for (i = 0; uris[i]; i++)
3434 file = g_file_new_for_uri (uri);
3436 if (shortcuts_add_bookmark_from_file (impl, file, position))
3439 g_object_unref (file);
3445 /* Reorders the selected bookmark to the specified position */
3447 shortcuts_reorder (GtkFileChooserDefault *impl,
3452 ShortcutType shortcut_type;
3455 int bookmarks_index;
3460 /* Get the selected path */
3462 if (!shortcuts_get_selected (impl, &iter))
3463 g_assert_not_reached ();
3465 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3466 old_position = *gtk_tree_path_get_indices (path);
3467 gtk_tree_path_free (path);
3469 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3470 old_position -= bookmarks_index;
3471 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3473 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3474 SHORTCUTS_COL_NAME, &name,
3475 SHORTCUTS_COL_DATA, &col_data,
3476 SHORTCUTS_COL_TYPE, &shortcut_type,
3478 g_assert (col_data != NULL);
3479 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3482 g_object_ref (file); /* removal below will free file, so we need a new ref */
3484 /* Remove the path from the old position and insert it in the new one */
3486 if (new_position > old_position)
3489 if (old_position == new_position)
3493 if (gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3495 shortcuts_add_bookmark_from_file (impl, file, new_position);
3496 gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3499 error_adding_bookmark_dialog (impl, file, error);
3503 g_object_unref (file);
3506 /* Callback used when we get the drag data for the bookmarks list. We add the
3507 * received URIs as bookmarks if they are folders.
3510 shortcuts_drag_data_received_cb (GtkWidget *widget,
3511 GdkDragContext *context,
3514 GtkSelectionData *selection_data,
3519 GtkFileChooserDefault *impl;
3520 GtkTreePath *tree_path;
3521 GtkTreeViewDropPosition tree_pos;
3523 int bookmarks_index;
3525 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3527 /* Compute position */
3529 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3531 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3532 position = *gtk_tree_path_get_indices (tree_path);
3533 gtk_tree_path_free (tree_path);
3535 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3538 g_assert (position >= bookmarks_index);
3539 position -= bookmarks_index;
3541 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3542 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3543 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3544 shortcuts_reorder (impl, position);
3546 g_signal_stop_emission_by_name (widget, "drag_data_received");
3549 /* Callback used to display a tooltip in the shortcuts tree */
3551 shortcuts_query_tooltip_cb (GtkWidget *widget,
3554 gboolean keyboard_mode,
3555 GtkTooltip *tooltip,
3556 GtkFileChooserDefault *impl)
3558 GtkTreeModel *model;
3561 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3569 ShortcutType shortcut_type;
3571 gtk_tree_model_get (model, &iter,
3572 SHORTCUTS_COL_DATA, &col_data,
3573 SHORTCUTS_COL_TYPE, &shortcut_type,
3576 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3578 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3582 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3587 file = G_FILE (col_data);
3588 parse_name = g_file_get_parse_name (file);
3590 gtk_tooltip_set_text (tooltip, parse_name);
3592 g_free (parse_name);
3596 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3600 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3610 /* Callback used when the selection in the shortcuts tree changes */
3612 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3613 GtkFileChooserDefault *impl)
3616 GtkTreeIter child_iter;
3618 bookmarks_check_remove_sensitivity (impl);
3619 shortcuts_check_popup_sensitivity (impl);
3621 if (impl->changing_folder)
3624 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3626 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3629 shortcuts_activate_iter (impl, &child_iter);
3634 shortcuts_row_separator_func (GtkTreeModel *model,
3638 ShortcutType shortcut_type;
3640 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3642 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3645 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3646 * keypresses before the TreeView gets them.
3649 tree_view_keybinding_cb (GtkWidget *tree_view,
3651 GtkFileChooserDefault *impl)
3653 if ((event->keyval == GDK_slash
3654 || event->keyval == GDK_KP_Divide
3656 || event->keyval == GDK_asciitilde
3658 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3660 location_popup_handler (impl, event->string);
3667 /* Callback used when the file list's popup menu is detached */
3669 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3672 GtkFileChooserDefault *impl;
3674 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3675 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3677 impl->browse_shortcuts_popup_menu = NULL;
3678 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3679 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3683 remove_shortcut_cb (GtkMenuItem *item,
3684 GtkFileChooserDefault *impl)
3686 remove_selected_bookmarks (impl);
3689 /* Rename the selected bookmark */
3691 rename_selected_bookmark (GtkFileChooserDefault *impl)
3695 GtkTreeViewColumn *column;
3696 GtkCellRenderer *cell;
3699 if (shortcuts_get_selected (impl, &iter))
3701 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3702 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3703 renderers = gtk_tree_view_column_get_cell_renderers (column);
3704 cell = g_list_nth_data (renderers, 1);
3705 g_list_free (renderers);
3706 g_object_set (cell, "editable", TRUE, NULL);
3707 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3708 path, column, cell, TRUE);
3709 gtk_tree_path_free (path);
3714 rename_shortcut_cb (GtkMenuItem *item,
3715 GtkFileChooserDefault *impl)
3717 rename_selected_bookmark (impl);
3720 /* Constructs the popup menu for the file list if needed */
3722 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3726 if (impl->browse_shortcuts_popup_menu)
3729 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3730 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3731 impl->browse_shortcuts_tree_view,
3732 shortcuts_popup_menu_detach_cb);
3734 item = gtk_image_menu_item_new_with_label (_("Remove"));
3735 impl->browse_shortcuts_popup_menu_remove_item = item;
3736 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3737 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3738 g_signal_connect (item, "activate",
3739 G_CALLBACK (remove_shortcut_cb), impl);
3740 gtk_widget_show (item);
3741 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3743 item = gtk_menu_item_new_with_label (_("Rename..."));
3744 impl->browse_shortcuts_popup_menu_rename_item = item;
3745 g_signal_connect (item, "activate",
3746 G_CALLBACK (rename_shortcut_cb), impl);
3747 gtk_widget_show (item);
3748 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3750 shortcuts_check_popup_sensitivity (impl);
3754 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3756 shortcuts_build_popup_menu (impl);
3760 popup_position_func (GtkMenu *menu,
3764 gpointer user_data);
3767 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3768 GdkEventButton *event)
3770 shortcuts_update_popup_menu (impl);
3772 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3773 NULL, NULL, NULL, NULL,
3774 event->button, event->time);
3777 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3779 popup_position_func, impl->browse_shortcuts_tree_view,
3780 0, GDK_CURRENT_TIME);
3781 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3786 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3788 shortcuts_popup_menu_cb (GtkWidget *widget,
3789 GtkFileChooserDefault *impl)
3791 shortcuts_popup_menu (impl, NULL);
3795 /* Callback used when a button is pressed on the shortcuts list.
3796 * We trap button 3 to bring up a popup menu.
3799 shortcuts_button_press_event_cb (GtkWidget *widget,
3800 GdkEventButton *event,
3801 GtkFileChooserDefault *impl)
3803 static gboolean in_press = FALSE;
3809 if (event->button != 3)
3813 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3819 shortcuts_popup_menu (impl, event);
3824 shortcuts_edited (GtkCellRenderer *cell,
3827 GtkFileChooserDefault *impl)
3833 g_object_set (cell, "editable", FALSE, NULL);
3835 path = gtk_tree_path_new_from_string (path_string);
3836 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3837 g_assert_not_reached ();
3839 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3840 SHORTCUTS_COL_DATA, &shortcut,
3842 gtk_tree_path_free (path);
3844 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3848 shortcuts_editing_canceled (GtkCellRenderer *cell,
3849 GtkFileChooserDefault *impl)
3851 g_object_set (cell, "editable", FALSE, NULL);
3854 /* Creates the widgets for the shortcuts and bookmarks tree */
3856 shortcuts_list_create (GtkFileChooserDefault *impl)
3859 GtkTreeSelection *selection;
3860 GtkTreeViewColumn *column;
3861 GtkCellRenderer *renderer;
3863 /* Scrolled window */
3865 swin = gtk_scrolled_window_new (NULL, NULL);
3866 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3867 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3868 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3870 gtk_widget_show (swin);
3874 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3875 #ifdef PROFILE_FILE_CHOOSER
3876 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3878 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3879 G_CALLBACK (tree_view_keybinding_cb), impl);
3880 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3881 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3882 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3883 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3884 /* Accessible object name for the file chooser's shortcuts pane */
3885 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3887 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3889 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3891 shortcuts_source_targets,
3892 num_shortcuts_source_targets,
3895 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3896 GTK_DEST_DEFAULT_ALL,
3897 shortcuts_dest_targets,
3898 num_shortcuts_dest_targets,
3899 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3901 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3902 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3903 gtk_tree_selection_set_select_function (selection,
3904 shortcuts_select_func,
3907 g_signal_connect (selection, "changed",
3908 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3910 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3911 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3913 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3914 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3915 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3916 G_CALLBACK (shortcuts_drag_end_cb), impl);
3917 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3918 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3920 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3921 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3922 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3923 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3924 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3925 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3926 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3927 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3929 /* Support tooltips */
3930 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3931 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3932 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3934 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3935 gtk_widget_show (impl->browse_shortcuts_tree_view);
3939 column = gtk_tree_view_column_new ();
3940 /* Column header for the file chooser's shortcuts pane */
3941 gtk_tree_view_column_set_title (column, _("_Places"));
3943 renderer = gtk_cell_renderer_pixbuf_new ();
3944 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3945 gtk_tree_view_column_set_attributes (column, renderer,
3946 "pixbuf", SHORTCUTS_COL_PIXBUF,
3947 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3950 renderer = gtk_cell_renderer_text_new ();
3951 g_signal_connect (renderer, "edited",
3952 G_CALLBACK (shortcuts_edited), impl);
3953 g_signal_connect (renderer, "editing-canceled",
3954 G_CALLBACK (shortcuts_editing_canceled), impl);
3955 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3956 gtk_tree_view_column_set_attributes (column, renderer,
3957 "text", SHORTCUTS_COL_NAME,
3960 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3961 shortcuts_row_separator_func,
3964 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3969 /* Creates the widgets for the shortcuts/bookmarks pane */
3971 shortcuts_pane_create (GtkFileChooserDefault *impl,
3972 GtkSizeGroup *size_group)
3978 vbox = gtk_vbox_new (FALSE, 6);
3979 gtk_widget_show (vbox);
3981 /* Shortcuts tree */
3983 widget = shortcuts_list_create (impl);
3984 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3986 /* Box for buttons */
3988 hbox = gtk_hbox_new (TRUE, 6);
3989 gtk_size_group_add_widget (size_group, hbox);
3990 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3991 gtk_widget_show (hbox);
3993 /* Add bookmark button */
3995 impl->browse_shortcuts_add_button = button_new (impl,
4000 G_CALLBACK (add_bookmark_button_clicked_cb));
4001 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4002 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4003 _("Add the selected folder to the Bookmarks"));
4005 /* Remove bookmark button */
4007 impl->browse_shortcuts_remove_button = button_new (impl,
4012 G_CALLBACK (remove_bookmark_button_clicked_cb));
4013 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4014 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4015 _("Remove the selected bookmark"));
4020 /* Handles key press events on the file list, so that we can trap Enter to
4021 * activate the default button on our own. Also, checks to see if '/' has been
4022 * pressed. See comment by tree_view_keybinding_cb() for more details.
4025 trap_activate_cb (GtkWidget *widget,
4029 GtkFileChooserDefault *impl;
4032 impl = (GtkFileChooserDefault *) data;
4034 modifiers = gtk_accelerator_get_default_mod_mask ();
4036 if ((event->keyval == GDK_slash
4037 || event->keyval == GDK_KP_Divide
4039 || event->keyval == GDK_asciitilde
4041 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4043 location_popup_handler (impl, event->string);
4047 if ((event->keyval == GDK_Return
4048 || event->keyval == GDK_ISO_Enter
4049 || event->keyval == GDK_KP_Enter
4050 || event->keyval == GDK_space
4051 || event->keyval == GDK_KP_Space)
4052 && ((event->state & modifiers) == 0)
4053 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4054 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4058 window = get_toplevel (widget);
4060 && widget != window->default_widget
4061 && !(widget == window->focus_widget &&
4062 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4064 gtk_window_activate_default (window);
4072 /* Callback used when the file list's popup menu is detached */
4074 popup_menu_detach_cb (GtkWidget *attach_widget,
4077 GtkFileChooserDefault *impl;
4079 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4080 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4082 impl->browse_files_popup_menu = NULL;
4083 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4084 impl->browse_files_popup_menu_hidden_files_item = NULL;
4087 /* Callback used when the "Add to Bookmarks" menu item is activated */
4089 add_to_shortcuts_cb (GtkMenuItem *item,
4090 GtkFileChooserDefault *impl)
4092 bookmarks_add_selected_folder (impl);
4095 /* Callback used when the "Show Hidden Files" menu item is toggled */
4097 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4098 GtkFileChooserDefault *impl)
4101 "show-hidden", gtk_check_menu_item_get_active (item),
4105 /* Shows an error dialog about not being able to select a dragged file */
4107 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4112 _("Could not select file"),
4117 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4122 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4124 for (i = 1; uris[i]; i++)
4127 GError *error = NULL;
4130 file = g_file_new_for_uri (uri);
4132 gtk_file_chooser_default_select_file (chooser, file, &error);
4134 error_selecting_dragged_file_dialog (impl, file, error);
4136 g_object_unref (file);
4140 struct FileListDragData
4142 GtkFileChooserDefault *impl;
4148 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4150 const GError *error,
4153 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4154 struct FileListDragData *data = user_data;
4155 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4157 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4160 data->impl->file_list_drag_data_received_cancellable = NULL;
4162 if (cancelled || error)
4165 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4166 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4167 data->uris[1] == 0 && !error &&
4168 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4169 change_folder_and_display_error (data->impl, data->file, FALSE);
4172 GError *error = NULL;
4174 gtk_file_chooser_default_unselect_all (chooser);
4175 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4177 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4179 browse_files_center_selected_row (data->impl);
4182 if (data->impl->select_multiple)
4183 file_list_drag_data_select_uris (data->impl, data->uris);
4186 g_object_unref (data->impl);
4187 g_strfreev (data->uris);
4188 g_object_unref (data->file);
4191 g_object_unref (cancellable);
4195 file_list_drag_data_received_cb (GtkWidget *widget,
4196 GdkDragContext *context,
4199 GtkSelectionData *selection_data,
4204 GtkFileChooserDefault *impl;
4205 GtkFileChooser *chooser;
4210 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4211 chooser = GTK_FILE_CHOOSER (data);
4213 /* Parse the text/uri-list string, navigate to the first one */
4214 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
4217 struct FileListDragData *data;
4220 file = g_file_new_for_uri (uri);
4222 data = g_new0 (struct FileListDragData, 1);
4223 data->impl = g_object_ref (impl);
4227 if (impl->file_list_drag_data_received_cancellable)
4228 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4230 impl->file_list_drag_data_received_cancellable =
4231 gtk_file_system_get_info (impl->file_system, file,
4233 file_list_drag_data_received_get_info_cb,
4237 g_signal_stop_emission_by_name (widget, "drag_data_received");
4240 /* Don't do anything with the drag_drop signal */
4242 file_list_drag_drop_cb (GtkWidget *widget,
4243 GdkDragContext *context,
4247 GtkFileChooserDefault *impl)
4249 g_signal_stop_emission_by_name (widget, "drag_drop");
4253 /* Disable the normal tree drag motion handler, it makes it look like you're
4254 dropping the dragged item onto a tree item */
4256 file_list_drag_motion_cb (GtkWidget *widget,
4257 GdkDragContext *context,
4261 GtkFileChooserDefault *impl)
4263 g_signal_stop_emission_by_name (widget, "drag_motion");
4267 /* Constructs the popup menu for the file list if needed */
4269 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4273 if (impl->browse_files_popup_menu)
4276 impl->browse_files_popup_menu = gtk_menu_new ();
4277 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4278 impl->browse_files_tree_view,
4279 popup_menu_detach_cb);
4281 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4282 impl->browse_files_popup_menu_add_shortcut_item = item;
4283 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4284 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4285 gtk_widget_set_sensitive (item, FALSE);
4286 g_signal_connect (item, "activate",
4287 G_CALLBACK (add_to_shortcuts_cb), impl);
4288 gtk_widget_show (item);
4289 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4291 item = gtk_separator_menu_item_new ();
4292 gtk_widget_show (item);
4293 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4295 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4296 impl->browse_files_popup_menu_hidden_files_item = item;
4297 g_signal_connect (item, "toggled",
4298 G_CALLBACK (show_hidden_toggled_cb), impl);
4299 gtk_widget_show (item);
4300 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4303 /* Updates the popup menu for the file list, creating it if necessary */
4305 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4307 file_list_build_popup_menu (impl);
4309 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4311 /* The sensitivity of the Add to Bookmarks item is set in
4312 * bookmarks_check_add_sensitivity()
4315 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4316 G_CALLBACK (show_hidden_toggled_cb), impl);
4317 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4319 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4320 G_CALLBACK (show_hidden_toggled_cb), impl);
4324 popup_position_func (GtkMenu *menu,
4330 GtkWidget *widget = GTK_WIDGET (user_data);
4331 GdkScreen *screen = gtk_widget_get_screen (widget);
4334 GdkRectangle monitor;
4336 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4338 gdk_window_get_origin (widget->window, x, y);
4340 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4342 *x += (widget->allocation.width - req.width) / 2;
4343 *y += (widget->allocation.height - req.height) / 2;
4345 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4346 gtk_menu_set_monitor (menu, monitor_num);
4347 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4349 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4350 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4356 file_list_popup_menu (GtkFileChooserDefault *impl,
4357 GdkEventButton *event)
4359 file_list_update_popup_menu (impl);
4361 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4362 NULL, NULL, NULL, NULL,
4363 event->button, event->time);
4366 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4368 popup_position_func, impl->browse_files_tree_view,
4369 0, GDK_CURRENT_TIME);
4370 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4376 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4378 list_popup_menu_cb (GtkWidget *widget,
4379 GtkFileChooserDefault *impl)
4381 file_list_popup_menu (impl, NULL);
4385 /* Callback used when a button is pressed on the file list. We trap button 3 to
4386 * bring up a popup menu.
4389 list_button_press_event_cb (GtkWidget *widget,
4390 GdkEventButton *event,
4391 GtkFileChooserDefault *impl)
4393 static gboolean in_press = FALSE;
4399 if (event->button != 3)
4403 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4406 file_list_popup_menu (impl, event);
4410 /* Sets the sort column IDs for the file list based on the operation mode */
4412 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4414 int name_id, mtime_id;
4416 name_id = mtime_id = 0;
4418 switch (impl->operation_mode)
4420 case OPERATION_MODE_BROWSE:
4421 name_id = FILE_LIST_COL_NAME;
4422 mtime_id = FILE_LIST_COL_MTIME;
4424 case OPERATION_MODE_SEARCH:
4425 name_id = SEARCH_MODEL_COL_FILE;
4426 mtime_id = SEARCH_MODEL_COL_STAT;
4428 case OPERATION_MODE_RECENT:
4429 name_id = RECENT_MODEL_COL_FILE;
4430 mtime_id = RECENT_MODEL_COL_INFO;
4434 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4435 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4439 file_list_query_tooltip_cb (GtkWidget *widget,
4442 gboolean keyboard_tip,
4443 GtkTooltip *tooltip,
4446 GtkFileChooserDefault *impl = user_data;
4447 GtkTreeIter iter, child_iter;
4448 GtkTreePath *path = NULL;
4452 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4456 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4464 switch (impl->operation_mode)
4466 case OPERATION_MODE_SEARCH:
4467 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4469 gtk_tree_path_free (path);
4473 search_get_valid_child_iter (impl, &child_iter, &iter);
4474 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4475 SEARCH_MODEL_COL_FILE, &file,
4479 case OPERATION_MODE_RECENT:
4480 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4482 gtk_tree_path_free (path);
4486 recent_get_valid_child_iter (impl, &child_iter, &iter);
4487 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4488 RECENT_MODEL_COL_FILE, &file,
4492 case OPERATION_MODE_BROWSE:
4493 g_assert_not_reached ();
4499 gtk_tree_path_free (path);
4503 filename = g_file_get_path (file);
4504 gtk_tooltip_set_text (tooltip, filename);
4505 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4510 gtk_tree_path_free (path);
4515 /* Creates the widgets for the file list */
4517 create_file_list (GtkFileChooserDefault *impl)
4520 GtkTreeSelection *selection;
4521 GtkTreeViewColumn *column;
4522 GtkCellRenderer *renderer;
4524 /* Scrolled window */
4526 swin = gtk_scrolled_window_new (NULL, NULL);
4527 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4528 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4529 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4532 /* Tree/list view */
4534 impl->browse_files_tree_view = gtk_tree_view_new ();
4535 #ifdef PROFILE_FILE_CHOOSER
4536 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4538 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4539 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4541 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4542 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4544 gtk_drag_dest_set (impl->browse_files_tree_view,
4545 GTK_DEST_DEFAULT_ALL,
4546 file_list_dest_targets,
4547 num_file_list_dest_targets,
4548 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4550 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4551 G_CALLBACK (list_row_activated), impl);
4552 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4553 G_CALLBACK (trap_activate_cb), impl);
4554 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4555 G_CALLBACK (list_popup_menu_cb), impl);
4556 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4557 G_CALLBACK (list_button_press_event_cb), impl);
4559 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4560 G_CALLBACK (file_list_drag_data_received_cb), impl);
4561 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4562 G_CALLBACK (file_list_drag_drop_cb), impl);
4563 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4564 G_CALLBACK (file_list_drag_motion_cb), impl);
4566 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4567 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4568 G_CALLBACK (file_list_query_tooltip_cb), impl);
4570 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4571 gtk_tree_selection_set_select_function (selection,
4574 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4576 file_list_source_targets,
4577 num_file_list_source_targets,
4578 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4580 g_signal_connect (selection, "changed",
4581 G_CALLBACK (list_selection_changed), impl);
4583 /* Filename column */
4585 impl->list_name_column = gtk_tree_view_column_new ();
4586 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4587 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4588 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4589 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4591 renderer = gtk_cell_renderer_pixbuf_new ();
4592 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4593 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4594 list_icon_data_func, impl, NULL);
4596 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4597 g_object_set (impl->list_name_renderer,
4598 "ellipsize", PANGO_ELLIPSIZE_END,
4600 g_signal_connect (impl->list_name_renderer, "edited",
4601 G_CALLBACK (renderer_edited_cb), impl);
4602 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4603 G_CALLBACK (renderer_editing_canceled_cb), impl);
4604 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4605 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4606 list_name_data_func, impl, NULL);
4608 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4612 column = gtk_tree_view_column_new ();
4613 gtk_tree_view_column_set_title (column, _("Size"));
4615 renderer = gtk_cell_renderer_text_new ();
4616 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4617 gtk_tree_view_column_set_cell_data_func (column, renderer,
4618 list_size_data_func, impl, NULL);
4619 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4620 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4623 /* Modification time column */
4625 column = gtk_tree_view_column_new ();
4626 gtk_tree_view_column_set_resizable (column, TRUE);
4627 gtk_tree_view_column_set_title (column, _("Modified"));
4629 renderer = gtk_cell_renderer_text_new ();
4630 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4631 gtk_tree_view_column_set_cell_data_func (column, renderer,
4632 list_mtime_data_func, impl, NULL);
4633 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4634 impl->list_mtime_column = column;
4636 file_list_set_sort_column_ids (impl);
4638 gtk_widget_show_all (swin);
4644 create_path_bar (GtkFileChooserDefault *impl)
4646 GtkWidget *path_bar;
4648 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4649 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4654 /* Creates the widgets for the files/folders pane */
4656 file_pane_create (GtkFileChooserDefault *impl,
4657 GtkSizeGroup *size_group)
4663 vbox = gtk_vbox_new (FALSE, 6);
4664 gtk_widget_show (vbox);
4666 /* Box for lists and preview */
4668 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4669 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4670 gtk_widget_show (hbox);
4674 widget = create_file_list (impl);
4675 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4679 impl->preview_box = gtk_vbox_new (FALSE, 12);
4680 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4681 /* Don't show preview box initially */
4685 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4687 widget = filter_create (impl);
4689 gtk_widget_show (widget);
4690 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4692 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4693 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4698 /* Callback used when the "Browse for more folders" expander is toggled */
4700 expander_changed_cb (GtkExpander *expander,
4702 GtkFileChooserDefault *impl)
4704 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4705 update_appearance (impl);
4708 /* Callback used when the selection changes in the save folder combo box */
4710 save_folder_combo_changed_cb (GtkComboBox *combo,
4711 GtkFileChooserDefault *impl)
4715 if (impl->changing_folder)
4718 if (gtk_combo_box_get_active_iter (combo, &iter))
4720 GtkTreeIter child_iter;
4722 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4725 shortcuts_activate_iter (impl, &child_iter);
4729 /* Filter function used to filter out the Search item and its separator.
4730 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4733 shortcuts_combo_filter_func (GtkTreeModel *model,
4737 GtkFileChooserDefault *impl;
4738 GtkTreePath *tree_path;
4743 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4745 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4747 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4748 g_assert (tree_path != NULL);
4750 indices = gtk_tree_path_get_indices (tree_path);
4754 if (impl->has_search)
4756 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4757 if (idx == indices[0])
4761 if (impl->has_recent)
4763 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4764 if (idx == indices[0])
4768 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4769 if (idx == indices[0])
4774 gtk_tree_path_free (tree_path);
4779 /* Creates the combo box with the save folders */
4781 save_folder_combo_create (GtkFileChooserDefault *impl)
4784 GtkCellRenderer *cell;
4786 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4787 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4788 shortcuts_combo_filter_func,
4792 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4793 "model", impl->shortcuts_combo_filter_model,
4794 "focus-on-click", FALSE,
4796 gtk_widget_show (combo);
4798 cell = gtk_cell_renderer_pixbuf_new ();
4799 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4800 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4801 "pixbuf", SHORTCUTS_COL_PIXBUF,
4802 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4803 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4806 cell = gtk_cell_renderer_text_new ();
4807 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4808 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4809 "text", SHORTCUTS_COL_NAME,
4810 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4813 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4814 shortcuts_row_separator_func,
4817 g_signal_connect (combo, "changed",
4818 G_CALLBACK (save_folder_combo_changed_cb), impl);
4823 /* Creates the widgets specific to Save mode */
4825 save_widgets_create (GtkFileChooserDefault *impl)
4830 GtkWidget *alignment;
4832 if (impl->save_widgets != NULL)
4835 location_switch_to_path_bar (impl);
4837 vbox = gtk_vbox_new (FALSE, 12);
4839 table = gtk_table_new (2, 2, FALSE);
4840 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4841 gtk_widget_show (table);
4842 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4843 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4847 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4848 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4849 gtk_table_attach (GTK_TABLE (table), widget,
4853 gtk_widget_show (widget);
4855 /* Location entry */
4857 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4858 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4860 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4861 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4862 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4864 GTK_EXPAND | GTK_FILL, 0,
4866 gtk_widget_show (impl->location_entry);
4867 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4870 impl->save_folder_label = gtk_label_new (NULL);
4871 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4872 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4876 gtk_widget_show (impl->save_folder_label);
4878 impl->save_folder_combo = save_folder_combo_create (impl);
4879 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4881 GTK_EXPAND | GTK_FILL, GTK_FILL,
4883 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4886 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4887 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4889 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4890 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4891 g_signal_connect (impl->save_expander, "notify::expanded",
4892 G_CALLBACK (expander_changed_cb),
4894 gtk_widget_show_all (alignment);
4896 impl->save_widgets = vbox;
4897 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4898 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4899 gtk_widget_show (impl->save_widgets);
4902 /* Destroys the widgets specific to Save mode */
4904 save_widgets_destroy (GtkFileChooserDefault *impl)
4906 if (impl->save_widgets == NULL)
4909 gtk_widget_destroy (impl->save_widgets);
4910 impl->save_widgets = NULL;
4911 impl->location_entry = NULL;
4912 impl->save_folder_label = NULL;
4913 impl->save_folder_combo = NULL;
4914 impl->save_expander = NULL;
4917 /* Turns on the path bar widget. Can be called even if we are already in that
4921 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4923 if (impl->location_entry)
4925 gtk_widget_destroy (impl->location_entry);
4926 impl->location_entry = NULL;
4929 gtk_widget_hide (impl->location_entry_box);
4932 /* Sets the full path of the current folder as the text in the location entry. */
4934 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4936 gchar *text, *filename;
4938 if (!impl->current_folder)
4941 filename = g_file_get_path (impl->current_folder);
4945 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4949 text = g_file_get_uri (impl->current_folder);
4953 gboolean need_slash;
4956 len = strlen (text);
4957 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4963 slash_text = g_new (char, len + 2);
4964 strcpy (slash_text, text);
4965 slash_text[len] = G_DIR_SEPARATOR;
4966 slash_text[len + 1] = 0;
4972 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4979 /* Turns on the location entry. Can be called even if we are already in that
4983 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4985 /* when in search or recent files mode, we are not showing the
4986 * location_entry_box container, so there's no point in switching
4989 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4990 impl->operation_mode == OPERATION_MODE_RECENT)
4993 if (impl->location_entry)
4994 gtk_widget_destroy (impl->location_entry);
4998 gtk_widget_show (impl->location_entry_box);
5002 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5003 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5005 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5006 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5008 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5009 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5011 /* Configure the entry */
5013 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5017 gtk_widget_show (impl->location_entry);
5018 gtk_widget_grab_focus (impl->location_entry);
5021 /* Sets a new location mode. set_buttons determines whether the toggle button
5022 * for the mode will also be changed.
5025 location_mode_set (GtkFileChooserDefault *impl,
5026 LocationMode new_mode,
5027 gboolean set_button)
5029 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5030 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5032 GtkWindow *toplevel;
5033 GtkWidget *current_focus;
5034 gboolean button_active;
5035 gboolean switch_to_file_list;
5039 case LOCATION_MODE_PATH_BAR:
5040 button_active = FALSE;
5042 /* The location_entry will disappear when we switch to path bar mode. So,
5043 * we'll focus the file list in that case, to avoid having a window with
5044 * no focused widget.
5046 toplevel = get_toplevel (GTK_WIDGET (impl));
5047 switch_to_file_list = FALSE;
5050 current_focus = gtk_window_get_focus (toplevel);
5051 if (!current_focus || current_focus == impl->location_entry)
5052 switch_to_file_list = TRUE;
5055 location_switch_to_path_bar (impl);
5057 if (switch_to_file_list)
5058 gtk_widget_grab_focus (impl->browse_files_tree_view);
5062 case LOCATION_MODE_FILENAME_ENTRY:
5063 button_active = TRUE;
5064 location_switch_to_filename_entry (impl);
5068 g_assert_not_reached ();
5074 g_signal_handlers_block_by_func (impl->location_button,
5075 G_CALLBACK (location_button_toggled_cb), impl);
5077 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5079 g_signal_handlers_unblock_by_func (impl->location_button,
5080 G_CALLBACK (location_button_toggled_cb), impl);
5084 impl->location_mode = new_mode;
5088 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5090 /* when in search or recent files mode, we are not showing the
5091 * location_entry_box container, so there's no point in switching
5094 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5095 impl->operation_mode == OPERATION_MODE_RECENT)
5098 /* If the file entry is not visible, show it.
5099 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5101 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5103 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5105 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5107 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5109 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5113 gtk_widget_grab_focus (impl->location_entry);
5118 /* Callback used when one of the location mode buttons is toggled */
5120 location_button_toggled_cb (GtkToggleButton *toggle,
5121 GtkFileChooserDefault *impl)
5124 LocationMode new_mode;
5126 is_active = gtk_toggle_button_get_active (toggle);
5130 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5131 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5135 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5136 new_mode = LOCATION_MODE_PATH_BAR;
5139 location_mode_set (impl, new_mode, FALSE);
5142 /* Creates a toggle button for the location entry. */
5144 location_button_create (GtkFileChooserDefault *impl)
5149 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5150 gtk_widget_show (image);
5152 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5156 g_signal_connect (impl->location_button, "toggled",
5157 G_CALLBACK (location_button_toggled_cb), impl);
5159 str = _("Type a file name");
5161 gtk_widget_set_tooltip_text (impl->location_button, str);
5162 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5165 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5167 browse_widgets_create (GtkFileChooserDefault *impl)
5173 GtkSizeGroup *size_group;
5175 /* size group is used by the [+][-] buttons and the filter combo */
5176 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5177 vbox = gtk_vbox_new (FALSE, 12);
5179 /* Location widgets */
5180 hbox = gtk_hbox_new (FALSE, 12);
5181 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5182 gtk_widget_show (hbox);
5183 impl->browse_path_bar_hbox = hbox;
5185 location_button_create (impl);
5186 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5190 impl->browse_path_bar = create_path_bar (impl);
5191 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5192 gtk_widget_show_all (impl->browse_path_bar);
5193 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5196 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5197 g_signal_connect (impl->browse_new_folder_button, "clicked",
5198 G_CALLBACK (new_folder_button_clicked), impl);
5199 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5201 /* Box for the location label and entry */
5203 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5204 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5206 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5207 gtk_widget_show (impl->location_label);
5208 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5211 hpaned = gtk_hpaned_new ();
5212 gtk_widget_show (hpaned);
5213 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5215 widget = shortcuts_pane_create (impl, size_group);
5216 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5217 widget = file_pane_create (impl, size_group);
5218 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5220 g_object_unref (size_group);
5226 gtk_file_chooser_default_constructor (GType type,
5227 guint n_construct_properties,
5228 GObjectConstructParam *construct_params)
5230 GtkFileChooserDefault *impl;
5233 profile_start ("start", NULL);
5235 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5236 n_construct_properties,
5238 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5240 g_assert (impl->file_system);
5242 gtk_widget_push_composite_child ();
5244 /* Recent files manager */
5245 recent_manager_update (impl);
5247 /* Shortcuts model */
5248 shortcuts_model_create (impl);
5250 /* The browse widgets */
5251 impl->browse_widgets = browse_widgets_create (impl);
5252 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5254 /* Alignment to hold extra widget */
5255 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5256 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5258 gtk_widget_pop_composite_child ();
5259 update_appearance (impl);
5261 profile_end ("end", NULL);
5266 /* Sets the extra_widget by packing it in the appropriate place */
5268 set_extra_widget (GtkFileChooserDefault *impl,
5269 GtkWidget *extra_widget)
5273 g_object_ref (extra_widget);
5274 /* FIXME: is this right ? */
5275 gtk_widget_show (extra_widget);
5278 if (impl->extra_widget)
5280 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5281 g_object_unref (impl->extra_widget);
5284 impl->extra_widget = extra_widget;
5285 if (impl->extra_widget)
5287 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5288 gtk_widget_show (impl->extra_align);
5291 gtk_widget_hide (impl->extra_align);
5295 set_local_only (GtkFileChooserDefault *impl,
5296 gboolean local_only)
5298 if (local_only != impl->local_only)
5300 impl->local_only = local_only;
5302 if (impl->shortcuts_model && impl->file_system)
5304 shortcuts_add_volumes (impl);
5305 shortcuts_add_bookmarks (impl);
5308 if (local_only && !g_file_is_native (impl->current_folder))
5310 /* If we are pointing to a non-local folder, make an effort to change
5311 * back to a local folder, but it's really up to the app to not cause
5312 * such a situation, so we ignore errors.
5314 const gchar *home = g_get_home_dir ();
5320 home_file = g_file_new_for_path (home);
5322 _gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5324 g_object_unref (home_file);
5330 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5331 GtkFileChooserDefault *impl)
5333 shortcuts_add_volumes (impl);
5334 shortcuts_add_bookmarks (impl);
5336 bookmarks_check_add_sensitivity (impl);
5337 bookmarks_check_remove_sensitivity (impl);
5338 shortcuts_check_popup_sensitivity (impl);
5341 /* Sets the file chooser to multiple selection mode */
5343 set_select_multiple (GtkFileChooserDefault *impl,
5344 gboolean select_multiple,
5345 gboolean property_notify)
5347 GtkTreeSelection *selection;
5348 GtkSelectionMode mode;
5350 if (select_multiple == impl->select_multiple)
5353 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5355 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5356 gtk_tree_selection_set_mode (selection, mode);
5358 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5360 impl->select_multiple = select_multiple;
5361 g_object_notify (G_OBJECT (impl), "select-multiple");
5363 check_preview_change (impl);
5367 set_file_system_backend (GtkFileChooserDefault *impl)
5369 profile_start ("start for backend", "default");
5371 impl->file_system = gtk_file_system_new ();
5373 g_signal_connect (impl->file_system, "volumes-changed",
5374 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5375 g_signal_connect (impl->file_system, "bookmarks-changed",
5376 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5378 profile_end ("end", NULL);
5381 /* This function is basically a do_all function.
5383 * It sets the visibility on all the widgets based on the current state, and
5384 * moves the custom_widget if needed.
5387 update_appearance (GtkFileChooserDefault *impl)
5389 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5390 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5394 gtk_widget_hide (impl->location_button);
5395 save_widgets_create (impl);
5397 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5398 text = _("Save in _folder:");
5400 text = _("Create in _folder:");
5402 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5404 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5406 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5407 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5408 gtk_widget_show (impl->browse_widgets);
5412 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5413 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5414 gtk_widget_hide (impl->browse_widgets);
5417 gtk_widget_show (impl->browse_new_folder_button);
5419 if (impl->select_multiple)
5421 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5422 "Re-setting to single selection mode.");
5423 set_select_multiple (impl, FALSE, TRUE);
5426 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5427 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5429 gtk_widget_show (impl->location_button);
5430 save_widgets_destroy (impl);
5431 gtk_widget_show (impl->browse_widgets);
5432 location_mode_set (impl, impl->location_mode, TRUE);
5435 if (impl->location_entry)
5436 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5438 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5439 gtk_widget_hide (impl->browse_new_folder_button);
5441 gtk_widget_show (impl->browse_new_folder_button);
5443 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5444 * of files may change depending whether we are in a file or folder-only mode.
5446 gtk_widget_queue_draw (impl->browse_files_tree_view);
5448 g_signal_emit_by_name (impl, "default-size-changed");
5452 gtk_file_chooser_default_set_property (GObject *object,
5454 const GValue *value,
5458 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5462 case GTK_FILE_CHOOSER_PROP_ACTION:
5464 GtkFileChooserAction action = g_value_get_enum (value);
5466 if (action != impl->action)
5468 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5470 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5471 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5472 && impl->select_multiple)
5474 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5475 "this is not allowed in multiple selection mode. Resetting the file chooser "
5476 "to single selection mode.");
5477 set_select_multiple (impl, FALSE, TRUE);
5479 impl->action = action;
5480 update_appearance (impl);
5481 settings_load (impl);
5486 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5487 /* Ignore property */
5490 case GTK_FILE_CHOOSER_PROP_FILTER:
5491 set_current_filter (impl, g_value_get_object (value));
5494 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5495 set_local_only (impl, g_value_get_boolean (value));
5498 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5499 set_preview_widget (impl, g_value_get_object (value));
5502 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5503 impl->preview_widget_active = g_value_get_boolean (value);
5504 update_preview_widget_visibility (impl);
5507 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5508 impl->use_preview_label = g_value_get_boolean (value);
5509 update_preview_widget_visibility (impl);
5512 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5513 set_extra_widget (impl, g_value_get_object (value));
5516 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5518 gboolean select_multiple = g_value_get_boolean (value);
5519 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5520 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5523 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5524 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5525 "leaving the file chooser in single selection mode.");
5529 set_select_multiple (impl, select_multiple, FALSE);
5533 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5535 gboolean show_hidden = g_value_get_boolean (value);
5536 if (show_hidden != impl->show_hidden)
5538 impl->show_hidden = show_hidden;
5540 if (impl->browse_files_model)
5541 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5546 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5548 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5549 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5560 gtk_file_chooser_default_get_property (GObject *object,
5565 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5569 case GTK_FILE_CHOOSER_PROP_ACTION:
5570 g_value_set_enum (value, impl->action);
5573 case GTK_FILE_CHOOSER_PROP_FILTER:
5574 g_value_set_object (value, impl->current_filter);
5577 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5578 g_value_set_boolean (value, impl->local_only);
5581 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5582 g_value_set_object (value, impl->preview_widget);
5585 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5586 g_value_set_boolean (value, impl->preview_widget_active);
5589 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5590 g_value_set_boolean (value, impl->use_preview_label);
5593 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5594 g_value_set_object (value, impl->extra_widget);
5597 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5598 g_value_set_boolean (value, impl->select_multiple);
5601 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5602 g_value_set_boolean (value, impl->show_hidden);
5605 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5606 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5615 /* Removes the settings signal handler. It's safe to call multiple times */
5617 remove_settings_signal (GtkFileChooserDefault *impl,
5620 if (impl->settings_signal_id)
5622 GtkSettings *settings;
5624 settings = gtk_settings_get_for_screen (screen);
5625 g_signal_handler_disconnect (settings,
5626 impl->settings_signal_id);
5627 impl->settings_signal_id = 0;
5632 gtk_file_chooser_default_dispose (GObject *object)
5635 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5637 if (impl->extra_widget)
5639 g_object_unref (impl->extra_widget);
5640 impl->extra_widget = NULL;
5643 pending_select_files_free (impl);
5645 /* cancel all pending operations */
5646 if (impl->pending_cancellables)
5648 for (l = impl->pending_cancellables; l; l = l->next)
5650 GCancellable *cancellable = G_CANCELLABLE (l->data);
5651 g_cancellable_cancel (cancellable);
5653 g_slist_free (impl->pending_cancellables);
5654 impl->pending_cancellables = NULL;
5657 if (impl->reload_icon_cancellables)
5659 for (l = impl->reload_icon_cancellables; l; l = l->next)
5661 GCancellable *cancellable = G_CANCELLABLE (l->data);
5662 g_cancellable_cancel (cancellable);
5664 g_slist_free (impl->reload_icon_cancellables);
5665 impl->reload_icon_cancellables = NULL;
5668 if (impl->loading_shortcuts)
5670 for (l = impl->loading_shortcuts; l; l = l->next)
5672 GCancellable *cancellable = G_CANCELLABLE (l->data);
5673 g_cancellable_cancel (cancellable);
5675 g_slist_free (impl->loading_shortcuts);
5676 impl->loading_shortcuts = NULL;
5679 if (impl->file_list_drag_data_received_cancellable)
5681 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5682 impl->file_list_drag_data_received_cancellable = NULL;
5685 if (impl->update_current_folder_cancellable)
5687 g_cancellable_cancel (impl->update_current_folder_cancellable);
5688 impl->update_current_folder_cancellable = NULL;
5691 if (impl->show_and_select_files_cancellable)
5693 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5694 impl->show_and_select_files_cancellable = NULL;
5697 if (impl->should_respond_get_info_cancellable)
5699 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5700 impl->should_respond_get_info_cancellable = NULL;
5703 if (impl->update_from_entry_cancellable)
5705 g_cancellable_cancel (impl->update_from_entry_cancellable);
5706 impl->update_from_entry_cancellable = NULL;
5709 if (impl->shortcuts_activate_iter_cancellable)
5711 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5712 impl->shortcuts_activate_iter_cancellable = NULL;
5715 search_stop_searching (impl, TRUE);
5716 recent_stop_loading (impl);
5718 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5720 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5723 /* We override show-all since we have internal widgets that
5724 * shouldn't be shown when you call show_all(), like the filter
5728 gtk_file_chooser_default_show_all (GtkWidget *widget)
5730 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5732 gtk_widget_show (widget);
5734 if (impl->extra_widget)
5735 gtk_widget_show_all (impl->extra_widget);
5738 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5739 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5742 toplevel_set_focus_cb (GtkWindow *window,
5744 GtkFileChooserDefault *impl)
5746 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5749 /* We monitor the focus widget on our toplevel to be able to know which widget
5750 * was last focused at the time our "should_respond" method gets called.
5753 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5754 GtkWidget *previous_toplevel)
5756 GtkFileChooserDefault *impl;
5757 GtkWidget *toplevel;
5759 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5761 if (previous_toplevel)
5763 g_assert (impl->toplevel_set_focus_id != 0);
5764 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5765 impl->toplevel_set_focus_id = 0;
5766 impl->toplevel_last_focus_widget = NULL;
5769 g_assert (impl->toplevel_set_focus_id == 0);
5771 toplevel = gtk_widget_get_toplevel (widget);
5772 if (GTK_IS_WINDOW (toplevel))
5774 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5775 G_CALLBACK (toplevel_set_focus_cb), impl);
5776 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5780 /* Changes the icons wherever it is needed */
5782 change_icon_theme (GtkFileChooserDefault *impl)
5784 GtkSettings *settings;
5787 profile_start ("start", NULL);
5789 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5791 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5792 impl->icon_size = MAX (width, height);
5794 impl->icon_size = FALLBACK_ICON_SIZE;
5796 shortcuts_reload_icons (impl);
5797 gtk_widget_queue_resize (impl->browse_files_tree_view);
5799 profile_end ("end", NULL);
5802 /* Callback used when a GtkSettings value changes */
5804 settings_notify_cb (GObject *object,
5806 GtkFileChooserDefault *impl)
5810 profile_start ("start", NULL);
5812 name = g_param_spec_get_name (pspec);
5814 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5815 strcmp (name, "gtk-icon-sizes") == 0)
5816 change_icon_theme (impl);
5818 profile_end ("end", NULL);
5821 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5825 check_icon_theme (GtkFileChooserDefault *impl)
5827 GtkSettings *settings;
5829 profile_start ("start", NULL);
5831 if (impl->settings_signal_id)
5833 profile_end ("end", NULL);
5837 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5839 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5840 impl->settings_signal_id = g_signal_connect (settings, "notify",
5841 G_CALLBACK (settings_notify_cb), impl);
5843 change_icon_theme (impl);
5846 profile_end ("end", NULL);
5850 recent_manager_update (GtkFileChooserDefault *impl)
5852 GtkRecentManager *manager;
5854 profile_start ("start", NULL);
5856 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5857 manager = gtk_recent_manager_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5859 manager = gtk_recent_manager_get_default ();
5861 if (impl->recent_manager != manager)
5862 impl->recent_manager = manager;
5864 profile_end ("end", NULL);
5868 gtk_file_chooser_default_style_set (GtkWidget *widget,
5869 GtkStyle *previous_style)
5871 GtkFileChooserDefault *impl;
5873 profile_start ("start", NULL);
5875 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5877 profile_msg (" parent class style_set start", NULL);
5878 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5879 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5880 profile_msg (" parent class style_set end", NULL);
5882 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5883 change_icon_theme (impl);
5885 profile_msg (" emit default-size-changed start", NULL);
5886 g_signal_emit_by_name (widget, "default-size-changed");
5887 profile_msg (" emit default-size-changed end", NULL);
5889 profile_end ("end", NULL);
5893 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5894 GdkScreen *previous_screen)
5896 GtkFileChooserDefault *impl;
5898 profile_start ("start", NULL);
5900 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5902 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5903 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5905 remove_settings_signal (impl, previous_screen);
5906 check_icon_theme (impl);
5907 recent_manager_update (impl);
5909 g_signal_emit_by_name (widget, "default-size-changed");
5911 profile_end ("end", NULL);
5915 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5916 GtkAllocation *allocation)
5918 GtkFileChooserDefault *impl;
5920 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5922 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5924 impl->default_width = allocation->width;
5925 impl->default_height = allocation->height;
5927 if (impl->preview_widget_active &&
5928 impl->preview_widget &&
5929 GTK_WIDGET_DRAWABLE (impl->preview_widget))
5930 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
5932 if (impl->extra_widget &&
5933 GTK_WIDGET_DRAWABLE (impl->extra_widget))
5934 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
5938 get_is_file_filtered (GtkFileChooserDefault *impl,
5940 GFileInfo *file_info)
5942 GtkFileFilterInfo filter_info;
5943 GtkFileFilterFlags needed;
5946 if (!impl->current_filter)
5949 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5951 needed = gtk_file_filter_get_needed (impl->current_filter);
5953 filter_info.display_name = g_file_info_get_display_name (file_info);
5954 filter_info.mime_type = g_file_info_get_content_type (file_info);
5956 if (needed & GTK_FILE_FILTER_FILENAME)
5958 filter_info.filename = g_file_get_path (file);
5959 if (filter_info.filename)
5960 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5963 filter_info.filename = NULL;
5965 if (needed & GTK_FILE_FILTER_URI)
5967 filter_info.uri = g_file_get_uri (file);
5968 if (filter_info.uri)
5969 filter_info.contains |= GTK_FILE_FILTER_URI;
5972 filter_info.uri = NULL;
5974 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5976 if (filter_info.filename)
5977 g_free ((gchar *)filter_info.filename);
5978 if (filter_info.uri)
5979 g_free ((gchar *)filter_info.uri);
5985 settings_load (GtkFileChooserDefault *impl)
5987 GtkFileChooserSettings *settings;
5988 LocationMode location_mode;
5989 gboolean show_hidden;
5990 gboolean expand_folders;
5992 settings = _gtk_file_chooser_settings_new ();
5994 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5995 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5996 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5998 g_object_unref (settings);
6000 location_mode_set (impl, location_mode, TRUE);
6001 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6002 impl->expand_folders = expand_folders;
6003 if (impl->save_expander)
6004 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6008 settings_save (GtkFileChooserDefault *impl)
6010 GtkFileChooserSettings *settings;
6012 settings = _gtk_file_chooser_settings_new ();
6014 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6015 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6016 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6019 _gtk_file_chooser_settings_save (settings, NULL);
6021 g_object_unref (settings);
6024 /* GtkWidget::map method */
6026 gtk_file_chooser_default_map (GtkWidget *widget)
6028 GtkFileChooserDefault *impl;
6029 char *current_working_dir;
6031 profile_start ("start", NULL);
6033 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6035 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6037 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6039 switch (impl->reload_state)
6042 /* The user didn't explicitly give us a folder to
6043 * display, so we'll use the cwd
6045 current_working_dir = g_get_current_dir ();
6046 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6047 current_working_dir);
6048 g_free (current_working_dir);
6051 case RELOAD_HAS_FOLDER:
6052 /* Nothing; we are already loading or loaded, so we
6053 * don't need to reload
6057 case RELOAD_WAS_UNMAPPED:
6058 /* Just reload the current folder; else continue
6061 if (impl->current_folder)
6063 pending_select_files_store_selection (impl);
6064 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6069 g_assert_not_reached ();
6073 volumes_bookmarks_changed_cb (impl->file_system, impl);
6075 settings_load (impl);
6077 profile_end ("end", NULL);
6080 /* GtkWidget::unmap method */
6082 gtk_file_chooser_default_unmap (GtkWidget *widget)
6084 GtkFileChooserDefault *impl;
6086 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6088 settings_save (impl);
6090 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6092 impl->reload_state = RELOAD_WAS_UNMAPPED;
6096 list_model_filter_func (GtkFileSystemModel *model,
6098 GFileInfo *file_info,
6101 GtkFileChooserDefault *impl = user_data;
6103 if (!impl->current_filter)
6106 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6109 return !get_is_file_filtered (impl, file, file_info);
6113 install_list_model_filter (GtkFileChooserDefault *impl)
6115 GtkFileSystemModelFilter filter;
6118 g_assert (impl->browse_files_model != NULL);
6120 if (impl->current_filter)
6122 filter = list_model_filter_func;
6131 _gtk_file_system_model_set_filter (impl->browse_files_model,
6136 #define COMPARE_DIRECTORIES \
6137 GtkFileChooserDefault *impl = user_data; \
6138 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6139 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6140 gboolean dir_a, dir_b; \
6143 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6145 return impl->list_sort_ascending ? -1 : 1; \
6148 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6150 return impl->list_sort_ascending ? 1 : -1; \
6152 if (dir_a != dir_b) \
6153 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6155 /* Sort callback for the filename column */
6157 name_sort_func (GtkTreeModel *model,
6162 COMPARE_DIRECTORIES;
6165 gchar *key_a, *key_b;
6168 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6169 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6170 result = strcmp (key_a, key_b);
6179 /* Sort callback for the size column */
6181 size_sort_func (GtkTreeModel *model,
6186 COMPARE_DIRECTORIES;
6189 goffset size_a = g_file_info_get_size (info_a);
6190 goffset size_b = g_file_info_get_size (info_b);
6192 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6196 /* Sort callback for the mtime column */
6198 mtime_sort_func (GtkTreeModel *model,
6203 COMPARE_DIRECTORIES;
6208 g_file_info_get_modification_time (info_a, &ta);
6209 g_file_info_get_modification_time (info_b, &tb);
6211 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6215 /* Callback used when the sort column changes. We cache the sort order for use
6216 * in name_sort_func().
6219 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6220 GtkFileChooserDefault *impl)
6222 GtkSortType sort_type;
6224 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6225 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6229 set_busy_cursor (GtkFileChooserDefault *impl,
6232 GtkWindow *toplevel;
6233 GdkDisplay *display;
6236 toplevel = get_toplevel (GTK_WIDGET (impl));
6237 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6240 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6243 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6247 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6248 gdk_display_flush (display);
6251 gdk_cursor_unref (cursor);
6254 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6256 load_set_model (GtkFileChooserDefault *impl)
6258 profile_start ("start", NULL);
6260 g_assert (impl->browse_files_model != NULL);
6261 g_assert (impl->sort_model == NULL);
6263 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6264 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6265 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6266 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6267 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6268 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6269 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6270 impl->list_sort_ascending = TRUE;
6271 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6273 g_signal_connect (impl->sort_model, "sort_column_changed",
6274 G_CALLBACK (list_sort_column_changed_cb), impl);
6276 profile_msg (" gtk_tree_view_set_model start", NULL);
6277 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6278 GTK_TREE_MODEL (impl->sort_model));
6279 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6280 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6281 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6282 profile_msg (" gtk_tree_view_set_model end", NULL);
6284 profile_end ("end", NULL);
6287 /* Timeout callback used when the loading timer expires */
6289 load_timeout_cb (gpointer data)
6291 GtkFileChooserDefault *impl;
6293 profile_start ("start", NULL);
6295 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6296 g_assert (impl->load_state == LOAD_PRELOAD);
6297 g_assert (impl->load_timeout_id != 0);
6298 g_assert (impl->browse_files_model != NULL);
6300 impl->load_timeout_id = 0;
6301 impl->load_state = LOAD_LOADING;
6303 load_set_model (impl);
6305 profile_end ("end", NULL);
6310 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6312 load_setup_timer (GtkFileChooserDefault *impl)
6314 g_assert (impl->load_timeout_id == 0);
6315 g_assert (impl->load_state != LOAD_PRELOAD);
6317 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6318 impl->load_state = LOAD_PRELOAD;
6321 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6323 load_remove_timer (GtkFileChooserDefault *impl)
6325 if (impl->load_timeout_id != 0)
6327 g_assert (impl->load_state == LOAD_PRELOAD);
6329 g_source_remove (impl->load_timeout_id);
6330 impl->load_timeout_id = 0;
6331 impl->load_state = LOAD_EMPTY;
6334 g_assert (impl->load_state == LOAD_EMPTY ||
6335 impl->load_state == LOAD_LOADING ||
6336 impl->load_state == LOAD_FINISHED);
6339 /* Selects the first row in the file list */
6341 browse_files_select_first_row (GtkFileChooserDefault *impl)
6345 if (!impl->sort_model)
6348 path = gtk_tree_path_new_from_indices (0, -1);
6349 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6350 gtk_tree_path_free (path);
6353 struct center_selected_row_closure {
6354 GtkFileChooserDefault *impl;
6355 gboolean already_centered;
6358 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6359 * selected row in the tree view.
6362 center_selected_row_foreach_cb (GtkTreeModel *model,
6367 struct center_selected_row_closure *closure;
6370 if (closure->already_centered)
6373 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6374 closure->already_centered = TRUE;
6377 /* Centers the selected row in the tree view */
6379 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6381 struct center_selected_row_closure closure;
6382 GtkTreeSelection *selection;
6384 closure.impl = impl;
6385 closure.already_centered = FALSE;
6387 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6388 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6391 struct ShowAndSelectPathsData
6393 GtkFileChooserDefault *impl;
6398 show_and_select_files_finished_loading (GtkFolder *folder,
6401 gboolean have_hidden;
6402 gboolean have_filtered;
6404 struct ShowAndSelectPathsData *data = user_data;
6406 have_hidden = FALSE;
6407 have_filtered = FALSE;
6409 for (l = data->files; l; l = l->next)
6416 info = gtk_folder_get_info (folder, file);
6420 have_hidden = g_file_info_get_is_hidden (info);
6423 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6424 get_is_file_filtered (data->impl, file, info);
6426 g_object_unref (info);
6428 if (have_hidden && have_filtered)
6429 break; /* we now have all the information we need */
6433 g_signal_handlers_disconnect_by_func (folder,
6434 show_and_select_files_finished_loading,
6438 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6441 set_current_filter (data->impl, NULL);
6443 for (l = data->files; l; l = l->next)
6448 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6449 select_func, data->impl);
6452 browse_files_center_selected_row (data->impl);
6454 g_object_unref (data->impl);
6455 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6456 g_slist_free (data->files);
6461 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6463 const GError *error,
6466 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6467 struct ShowAndSelectPathsData *data = user_data;
6469 if (data->impl->show_and_select_files_cancellable != cancellable)
6472 data->impl->show_and_select_files_cancellable = NULL;
6474 if (cancelled || error)
6477 g_object_unref (cancellable);
6479 if (gtk_folder_is_finished_loading (folder))
6480 show_and_select_files_finished_loading (folder, user_data);
6482 g_signal_connect (folder, "finished-loading",
6483 G_CALLBACK (show_and_select_files_finished_loading),
6489 g_object_unref (data->impl);
6490 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6491 g_slist_free (data->files);
6494 g_object_unref (cancellable);
6498 show_and_select_files (GtkFileChooserDefault *impl,
6503 struct ShowAndSelectPathsData *info;
6505 profile_start ("start", NULL);
6509 profile_end ("end", NULL);
6513 info = g_new (struct ShowAndSelectPathsData, 1);
6514 info->impl = g_object_ref (impl);
6515 info->files = g_slist_copy (files);
6516 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6518 if (impl->show_and_select_files_cancellable)
6519 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6521 impl->show_and_select_files_cancellable =
6522 gtk_file_system_get_folder (impl->file_system, parent_file,
6523 "standard::is-hidden,standard::type,standard::name",
6524 show_and_select_files_get_folder_cb, info);
6526 profile_end ("end", NULL);
6530 /* Processes the pending operation when a folder is finished loading */
6532 pending_select_files_process (GtkFileChooserDefault *impl)
6534 g_assert (impl->load_state == LOAD_FINISHED);
6535 g_assert (impl->browse_files_model != NULL);
6536 g_assert (impl->sort_model != NULL);
6538 if (impl->pending_select_files)
6541 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6542 pending_select_files_free (impl);
6543 browse_files_center_selected_row (impl);
6547 /* We only select the first row if the chooser is actually mapped ---
6548 * selecting the first row is to help the user when he is interacting with
6549 * the chooser, but sometimes a chooser works not on behalf of the user,
6550 * but rather on behalf of something else like GtkFileChooserButton. In
6551 * that case, the chooser's selection should be what the caller expects,
6552 * as the user can't see that something else got selected. See bug #165264.
6554 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6555 browse_files_select_first_row (impl);
6558 g_assert (impl->pending_select_files == NULL);
6561 /* Callback used when the file system model finishes loading */
6563 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6564 GtkFileChooserDefault *impl)
6566 profile_start ("start", NULL);
6568 if (impl->load_state == LOAD_PRELOAD)
6570 load_remove_timer (impl);
6571 load_set_model (impl);
6573 else if (impl->load_state == LOAD_LOADING)
6579 /* We can't g_assert_not_reached(), as something other than us may have
6580 * initiated a folder reload. See #165556.
6582 profile_end ("end", NULL);
6586 g_assert (impl->load_timeout_id == 0);
6588 impl->load_state = LOAD_FINISHED;
6590 pending_select_files_process (impl);
6591 set_busy_cursor (impl, FALSE);
6592 #ifdef PROFILE_FILE_CHOOSER
6593 access ("MARK: *** FINISHED LOADING", F_OK);
6596 profile_end ("end", NULL);
6600 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6602 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6604 if (impl->browse_files_model)
6606 g_object_unref (impl->browse_files_model);
6607 impl->browse_files_model = NULL;
6610 if (impl->sort_model)
6612 g_object_unref (impl->sort_model);
6613 impl->sort_model = NULL;
6616 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6619 /* Gets rid of the old list model and creates a new one for the current folder */
6621 set_list_model (GtkFileChooserDefault *impl,
6624 g_assert (impl->current_folder != NULL);
6626 profile_start ("start", NULL);
6628 stop_loading_and_clear_list_model (impl);
6630 set_busy_cursor (impl, TRUE);
6631 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6633 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6634 impl->current_folder, 0,
6635 "standard,time,thumbnail::*",
6637 if (!impl->browse_files_model)
6639 set_busy_cursor (impl, FALSE);
6640 profile_end ("end", NULL);
6644 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6646 g_signal_connect (impl->browse_files_model, "finished-loading",
6647 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6649 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6651 install_list_model_filter (impl);
6653 profile_end ("end", NULL);
6658 struct update_chooser_entry_selected_foreach_closure {
6660 GtkTreeIter first_selected_iter;
6664 compare_utf8_filenames (const gchar *a,
6667 gchar *a_folded, *b_folded;
6670 a_folded = g_utf8_strdown (a, -1);
6671 b_folded = g_utf8_strdown (b, -1);
6673 retval = strcmp (a_folded, b_folded);
6682 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6687 struct update_chooser_entry_selected_foreach_closure *closure;
6690 closure->num_selected++;
6692 if (closure->num_selected == 1)
6693 closure->first_selected_iter = *iter;
6697 update_chooser_entry (GtkFileChooserDefault *impl)
6699 GtkTreeSelection *selection;
6700 struct update_chooser_entry_selected_foreach_closure closure;
6701 const char *file_part;
6703 /* no need to update the file chooser's entry if there's no entry */
6704 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6705 impl->operation_mode == OPERATION_MODE_RECENT ||
6706 !impl->location_entry)
6709 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6710 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6711 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6712 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6713 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6716 g_assert (impl->location_entry != NULL);
6718 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6719 closure.num_selected = 0;
6720 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6724 if (closure.num_selected == 0)
6726 goto maybe_clear_entry;
6728 else if (closure.num_selected == 1)
6730 GtkTreeIter child_iter;
6732 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6735 gboolean change_entry;
6737 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6739 &closure.first_selected_iter);
6741 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6743 /* If the cursor moved to the row of the newly created folder,
6744 * retrieving info will return NULL.
6749 g_free (impl->browse_files_last_selected_name);
6750 impl->browse_files_last_selected_name =
6751 g_strdup (g_file_info_get_display_name (info));
6753 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6754 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6756 /* We don't want the name to change when clicking on a folder... */
6757 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6760 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6764 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6766 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6767 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6775 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6776 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6778 /* Multiple selection, so just clear the entry. */
6780 g_free (impl->browse_files_last_selected_name);
6781 impl->browse_files_last_selected_name = NULL;
6783 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6789 if (impl->browse_files_last_selected_name)
6791 const char *entry_text;
6793 gboolean clear_entry;
6795 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6796 len = strlen (entry_text);
6799 /* The file chooser entry may have appended a "/" to its text. So
6800 * take it out, and compare the result to the old selection.
6802 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6806 tmp = g_strndup (entry_text, len - 1);
6807 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6811 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6814 clear_entry = FALSE;
6817 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6822 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6826 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6830 struct UpdateCurrentFolderData
6832 GtkFileChooserDefault *impl;
6834 gboolean keep_trail;
6835 gboolean clear_entry;
6836 GFile *original_file;
6837 GError *original_error;
6841 update_current_folder_get_info_cb (GCancellable *cancellable,
6843 const GError *error,
6846 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6847 struct UpdateCurrentFolderData *data = user_data;
6848 GtkFileChooserDefault *impl = data->impl;
6850 if (cancellable != impl->update_current_folder_cancellable)
6853 impl->update_current_folder_cancellable = NULL;
6854 impl->reload_state = RELOAD_EMPTY;
6856 set_busy_cursor (impl, FALSE);
6865 if (!data->original_file)
6867 data->original_file = g_object_ref (data->file);
6868 data->original_error = g_error_copy (error);
6871 parent_file = g_file_get_parent (data->file);
6873 /* get parent path and try to change the folder to that */
6876 g_object_unref (data->file);
6877 data->file = parent_file;
6879 g_object_unref (cancellable);
6881 /* restart the update current folder operation */
6882 impl->reload_state = RELOAD_HAS_FOLDER;
6884 impl->update_current_folder_cancellable =
6885 gtk_file_system_get_info (impl->file_system, data->file,
6887 update_current_folder_get_info_cb,
6890 set_busy_cursor (impl, TRUE);
6896 /* error and bail out */
6897 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6898 g_object_unref (data->original_file);
6904 if (data->original_file)
6906 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6908 g_object_unref (data->original_file);
6911 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6914 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6917 if (impl->current_folder != data->file)
6919 if (impl->current_folder)
6920 g_object_unref (impl->current_folder);
6922 impl->current_folder = g_object_ref (data->file);
6924 impl->reload_state = RELOAD_HAS_FOLDER;
6927 /* Update the widgets that may trigger a folder change themselves. */
6929 if (!impl->changing_folder)
6931 impl->changing_folder = TRUE;
6933 shortcuts_update_current_folder (impl);
6935 impl->changing_folder = FALSE;
6938 /* Set the folder on the save entry */
6940 if (impl->location_entry)
6942 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6943 impl->current_folder);
6945 if (data->clear_entry)
6946 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6949 /* Create a new list model. This is slightly evil; we store the result value
6950 * but perform more actions rather than returning immediately even if it
6951 * generates an error.
6953 set_list_model (impl, NULL);
6955 /* Refresh controls */
6957 shortcuts_find_current_folder (impl);
6959 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6961 check_preview_change (impl);
6962 bookmarks_check_add_sensitivity (impl);
6964 g_signal_emit_by_name (impl, "selection-changed", 0);
6967 g_object_unref (data->file);
6970 g_object_unref (cancellable);
6974 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6976 gboolean keep_trail,
6977 gboolean clear_entry,
6980 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6981 struct UpdateCurrentFolderData *data;
6983 profile_start ("start", NULL);
6985 switch (impl->operation_mode)
6987 case OPERATION_MODE_SEARCH:
6988 search_switch_to_browse_mode (impl);
6990 case OPERATION_MODE_RECENT:
6991 recent_switch_to_browse_mode (impl);
6993 case OPERATION_MODE_BROWSE:
6997 if (impl->local_only && !g_file_is_native (file))
7000 GTK_FILE_CHOOSER_ERROR,
7001 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7002 _("Cannot change to folder because it is not local"));
7004 profile_end ("end - not local", NULL);
7008 if (impl->update_current_folder_cancellable)
7009 g_cancellable_cancel (impl->update_current_folder_cancellable);
7011 /* Test validity of path here. */
7012 data = g_new0 (struct UpdateCurrentFolderData, 1);
7014 data->file = g_object_ref (file);
7015 data->keep_trail = keep_trail;
7016 data->clear_entry = clear_entry;
7018 impl->reload_state = RELOAD_HAS_FOLDER;
7020 impl->update_current_folder_cancellable =
7021 gtk_file_system_get_info (impl->file_system, file,
7023 update_current_folder_get_info_cb,
7026 set_busy_cursor (impl, TRUE);
7028 profile_end ("end", NULL);
7033 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7035 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7037 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7038 impl->operation_mode == OPERATION_MODE_RECENT)
7041 if (impl->reload_state == RELOAD_EMPTY)
7043 char *current_working_dir;
7046 /* We are unmapped, or we had an error while loading the last folder. We'll return
7047 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7048 * explicitly calls set_current_folder() on us.
7050 current_working_dir = g_get_current_dir ();
7051 file = g_file_new_for_path (current_working_dir);
7052 g_free (current_working_dir);
7056 return g_object_ref (impl->current_folder);
7060 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7063 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7065 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7066 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7068 pending_select_files_free (impl);
7069 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7073 select_func (GtkFileSystemModel *model,
7078 GtkFileChooserDefault *impl = user_data;
7079 GtkTreeSelection *selection;
7080 GtkTreeIter sorted_iter;
7082 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7084 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7085 gtk_tree_selection_select_iter (selection, &sorted_iter);
7089 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7093 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7097 parent_file = g_file_get_parent (file);
7100 return _gtk_file_chooser_set_current_folder_file (chooser, file, error);
7102 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7103 impl->operation_mode == OPERATION_MODE_RECENT ||
7104 impl->load_state == LOAD_EMPTY)
7110 g_assert (impl->current_folder != NULL);
7112 same_path = g_file_equal (parent_file, impl->current_folder);
7115 if (same_path && impl->load_state == LOAD_FINISHED)
7120 files.data = (gpointer) file;
7123 result = show_and_select_files (impl, parent_file, &files, error);
7124 g_object_unref (parent_file);
7128 pending_select_files_add (impl, file);
7134 result = _gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7135 g_object_unref (parent_file);
7139 g_object_unref (parent_file);
7144 unselect_func (GtkFileSystemModel *model,
7149 GtkFileChooserDefault *impl = user_data;
7150 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7151 GtkTreePath *sorted_path;
7153 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7155 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7157 gtk_tree_path_free (sorted_path);
7161 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7164 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7166 if (!impl->browse_files_model)
7169 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7170 unselect_func, impl);
7174 maybe_select (GtkTreeModel *model,
7179 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7180 GtkTreeSelection *selection;
7184 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7186 info = get_list_file_info (impl, iter);
7187 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7189 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7190 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7191 gtk_tree_selection_select_iter (selection, iter);
7193 gtk_tree_selection_unselect_iter (selection, iter);
7199 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7201 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7203 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7204 impl->operation_mode == OPERATION_MODE_RECENT)
7206 GtkTreeSelection *selection;
7208 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7209 gtk_tree_selection_select_all (selection);
7213 if (impl->select_multiple)
7214 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7215 maybe_select, impl);
7219 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7221 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7222 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7224 gtk_tree_selection_unselect_all (selection);
7225 pending_select_files_free (impl);
7228 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7230 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7232 * is_empty_ret - whether the file entry is totally empty
7234 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7235 * the path will be "$cwd/foobar")
7238 check_save_entry (GtkFileChooserDefault *impl,
7240 gboolean *is_well_formed_ret,
7241 gboolean *is_empty_ret,
7242 gboolean *is_file_part_empty_ret,
7243 gboolean *is_folder)
7245 GtkFileChooserEntry *chooser_entry;
7246 GFile *current_folder;
7247 const char *file_part;
7251 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7252 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7253 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7254 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7255 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7257 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7259 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7262 *is_well_formed_ret = TRUE;
7263 *is_empty_ret = TRUE;
7264 *is_file_part_empty_ret = TRUE;
7270 *is_empty_ret = FALSE;
7272 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7273 if (!current_folder)
7276 *is_well_formed_ret = FALSE;
7277 *is_file_part_empty_ret = FALSE;
7283 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7285 if (!file_part || file_part[0] == '\0')
7287 *file_ret = g_object_ref (current_folder);
7288 *is_well_formed_ret = TRUE;
7289 *is_file_part_empty_ret = TRUE;
7295 *is_file_part_empty_ret = FALSE;
7298 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7302 error_building_filename_dialog (impl, error);
7304 *is_well_formed_ret = FALSE;
7311 *is_well_formed_ret = TRUE;
7312 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7315 struct get_files_closure {
7316 GtkFileChooserDefault *impl;
7318 GFile *file_from_entry;
7322 get_files_foreach (GtkTreeModel *model,
7327 struct get_files_closure *info;
7329 GtkFileSystemModel *fs_model;
7330 GtkTreeIter sel_iter;
7333 fs_model = info->impl->browse_files_model;
7334 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7336 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7338 return; /* We are on the editable row */
7340 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7341 info->result = g_slist_prepend (info->result, g_object_ref (file));
7345 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7347 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7348 struct get_files_closure info;
7349 GtkWindow *toplevel;
7350 GtkWidget *current_focus;
7351 gboolean file_list_seen;
7353 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7354 return search_get_selected_files (impl);
7356 if (impl->operation_mode == OPERATION_MODE_RECENT)
7357 return recent_get_selected_files (impl);
7361 info.file_from_entry = NULL;
7363 toplevel = get_toplevel (GTK_WIDGET (impl));
7365 current_focus = gtk_window_get_focus (toplevel);
7367 current_focus = NULL;
7369 file_list_seen = FALSE;
7370 if (current_focus == impl->browse_files_tree_view)
7372 GtkTreeSelection *selection;
7376 file_list_seen = TRUE;
7377 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7378 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7380 /* If there is no selection in the file list, we probably have this situation:
7382 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7383 * 2. He then double-clicked on a folder ("bar") in the file list
7385 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7386 * filename entry to see if that is the case.
7388 if (info.result == NULL && impl->location_entry)
7391 else if (impl->location_entry && current_focus == impl->location_entry)
7393 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7397 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7402 if (!is_well_formed)
7405 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7407 g_object_unref (info.file_from_entry);
7411 if (info.file_from_entry)
7412 info.result = g_slist_prepend (info.result, info.file_from_entry);
7413 else if (!file_list_seen)
7418 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7420 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7424 /* The focus is on a dialog's action area button or something else */
7425 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7426 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7434 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7435 * fall back to the current directory */
7436 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7437 info.result == NULL)
7439 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_file (chooser));
7442 return g_slist_reverse (info.result);
7446 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7448 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7450 if (impl->preview_file)
7451 return g_object_ref (impl->preview_file);
7456 static GtkFileSystem *
7457 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7459 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7461 return impl->file_system;
7464 /* Shows or hides the filter widgets */
7466 show_filters (GtkFileChooserDefault *impl,
7470 gtk_widget_show (impl->filter_combo_hbox);
7472 gtk_widget_hide (impl->filter_combo_hbox);
7476 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7477 GtkFileFilter *filter)
7479 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7482 if (g_slist_find (impl->filters, filter))
7484 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7488 g_object_ref_sink (filter);
7489 impl->filters = g_slist_append (impl->filters, filter);
7491 name = gtk_file_filter_get_name (filter);
7493 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7495 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7497 if (!g_slist_find (impl->filters, impl->current_filter))
7498 set_current_filter (impl, filter);
7500 show_filters (impl, TRUE);
7504 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7505 GtkFileFilter *filter)
7507 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7508 GtkTreeModel *model;
7512 filter_index = g_slist_index (impl->filters, filter);
7514 if (filter_index < 0)
7516 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7520 impl->filters = g_slist_remove (impl->filters, filter);
7522 if (filter == impl->current_filter)
7525 set_current_filter (impl, impl->filters->data);
7527 set_current_filter (impl, NULL);
7530 /* Remove row from the combo box */
7531 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7532 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7533 g_assert_not_reached ();
7535 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7537 g_object_unref (filter);
7540 show_filters (impl, FALSE);
7544 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7546 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7548 return g_slist_copy (impl->filters);
7551 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7553 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7556 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7559 struct AddShortcutData
7561 GtkFileChooserDefault *impl;
7566 add_shortcut_get_info_cb (GCancellable *cancellable,
7568 const GError *error,
7572 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7573 struct AddShortcutData *data = user_data;
7575 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7578 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7580 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7583 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7585 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7588 g_object_unref (data->impl);
7589 g_object_unref (data->file);
7592 g_object_unref (cancellable);
7596 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7600 GCancellable *cancellable;
7601 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7602 struct AddShortcutData *data;
7606 /* Avoid adding duplicates */
7607 pos = shortcut_find_position (impl, file);
7608 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7612 uri = g_file_get_uri (file);
7613 /* translators, "Shortcut" means "Bookmark" here */
7615 GTK_FILE_CHOOSER_ERROR,
7616 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7617 _("Shortcut %s already exists"),
7624 for (l = impl->loading_shortcuts; l; l = l->next)
7626 GCancellable *c = l->data;
7629 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7630 if (f && g_file_equal (file, f))
7634 uri = g_file_get_uri (file);
7636 GTK_FILE_CHOOSER_ERROR,
7637 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7638 _("Shortcut %s already exists"),
7646 data = g_new0 (struct AddShortcutData, 1);
7647 data->impl = g_object_ref (impl);
7648 data->file = g_object_ref (file);
7650 cancellable = gtk_file_system_get_info (impl->file_system, file,
7652 add_shortcut_get_info_cb, data);
7657 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7658 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7664 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7668 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7675 for (l = impl->loading_shortcuts; l; l = l->next)
7677 GCancellable *c = l->data;
7680 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7681 if (f && g_file_equal (file, f))
7683 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7684 g_cancellable_cancel (c);
7689 if (impl->num_shortcuts == 0)
7692 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7693 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7694 g_assert_not_reached ();
7696 for (i = 0; i < impl->num_shortcuts; i++)
7699 ShortcutType shortcut_type;
7702 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7703 SHORTCUTS_COL_DATA, &col_data,
7704 SHORTCUTS_COL_TYPE, &shortcut_type,
7706 g_assert (col_data != NULL);
7707 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7709 shortcut = col_data;
7710 if (g_file_equal (shortcut, file))
7712 shortcuts_remove_rows (impl, pos + i, 1);
7713 impl->num_shortcuts--;
7717 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7718 g_assert_not_reached ();
7723 uri = g_file_get_uri (file);
7724 /* translators, "Shortcut" means "Bookmark" here */
7726 GTK_FILE_CHOOSER_ERROR,
7727 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7728 _("Shortcut %s does not exist"),
7736 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7738 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7744 if (impl->num_shortcuts == 0)
7747 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7748 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7749 g_assert_not_reached ();
7753 for (i = 0; i < impl->num_shortcuts; i++)
7756 ShortcutType shortcut_type;
7759 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7760 SHORTCUTS_COL_DATA, &col_data,
7761 SHORTCUTS_COL_TYPE, &shortcut_type,
7763 g_assert (col_data != NULL);
7764 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7766 shortcut = col_data;
7767 list = g_slist_prepend (list, g_object_ref (shortcut));
7769 if (i != impl->num_shortcuts - 1)
7771 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7772 g_assert_not_reached ();
7776 return g_slist_reverse (list);
7779 /* Guesses a size based upon font sizes */
7781 find_good_size_from_style (GtkWidget *widget,
7785 GtkFileChooserDefault *impl;
7790 g_assert (widget->style != NULL);
7791 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7793 if (impl->default_width == 0 &&
7794 impl->default_height == 0)
7796 screen = gtk_widget_get_screen (widget);
7799 resolution = gdk_screen_get_resolution (screen);
7800 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7804 resolution = 96.0; /* wheeee */
7806 font_size = pango_font_description_get_size (widget->style->font_desc);
7807 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7809 impl->default_width = font_size * NUM_CHARS;
7810 impl->default_height = font_size * NUM_LINES;
7813 *width = impl->default_width;
7814 *height = impl->default_height;
7818 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7819 gint *default_width,
7820 gint *default_height)
7822 GtkFileChooserDefault *impl;
7825 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7826 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7828 if (impl->preview_widget_active &&
7829 impl->preview_widget &&
7830 GTK_WIDGET_VISIBLE (impl->preview_widget))
7832 gtk_widget_size_request (impl->preview_box, &req);
7833 *default_width += PREVIEW_HBOX_SPACING + req.width;
7836 if (impl->extra_widget &&
7837 GTK_WIDGET_VISIBLE (impl->extra_widget))
7839 gtk_widget_size_request (impl->extra_align, &req);
7840 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7844 struct switch_folder_closure {
7845 GtkFileChooserDefault *impl;
7850 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7852 switch_folder_foreach_cb (GtkTreeModel *model,
7857 struct switch_folder_closure *closure;
7858 GtkTreeIter child_iter;
7862 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7864 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7865 closure->num_selected++;
7868 /* Changes to the selected folder in the list view */
7870 switch_to_selected_folder (GtkFileChooserDefault *impl)
7872 GtkTreeSelection *selection;
7873 struct switch_folder_closure closure;
7875 /* We do this with foreach() rather than get_selected() as we may be in
7876 * multiple selection mode
7879 closure.impl = impl;
7880 closure.file = NULL;
7881 closure.num_selected = 0;
7883 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7884 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7886 g_assert (closure.file && closure.num_selected == 1);
7888 change_folder_and_display_error (impl, closure.file, FALSE);
7891 /* Gets the GFileInfo for the selected row in the file list; assumes single
7895 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7896 gboolean *had_selection)
7898 GtkTreeSelection *selection;
7899 GtkTreeIter iter, child_iter;
7902 g_assert (!impl->select_multiple);
7903 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7904 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7906 *had_selection = FALSE;
7910 *had_selection = TRUE;
7912 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7916 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7920 /* Gets the display name of the selected file in the file list; assumes single
7921 * selection mode and that something is selected.
7923 static const gchar *
7924 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7927 gboolean had_selection;
7929 info = get_selected_file_info_from_file_list (impl, &had_selection);
7930 g_assert (had_selection);
7931 g_assert (info != NULL);
7933 return g_file_info_get_display_name (info);
7937 add_custom_button_to_dialog (GtkDialog *dialog,
7938 const gchar *mnemonic_label,
7939 const gchar *stock_id,
7944 button = gtk_button_new_with_mnemonic (mnemonic_label);
7945 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7946 gtk_button_set_image (GTK_BUTTON (button),
7947 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7948 gtk_widget_show (button);
7950 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7953 /* Presents an overwrite confirmation dialog; returns whether we should accept
7957 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7958 const gchar *file_part,
7959 const gchar *folder_display_name)
7961 GtkWindow *toplevel;
7965 toplevel = get_toplevel (GTK_WIDGET (impl));
7967 dialog = gtk_message_dialog_new (toplevel,
7968 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7969 GTK_MESSAGE_QUESTION,
7971 _("A file named \"%s\" already exists. Do you want to replace it?"),
7973 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7974 _("The file already exists in \"%s\". Replacing it will "
7975 "overwrite its contents."),
7976 folder_display_name);
7978 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7979 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7980 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7981 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7982 GTK_RESPONSE_ACCEPT,
7983 GTK_RESPONSE_CANCEL,
7985 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7987 if (toplevel->group)
7988 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7990 response = gtk_dialog_run (GTK_DIALOG (dialog));
7992 gtk_widget_destroy (dialog);
7994 return (response == GTK_RESPONSE_ACCEPT);
7997 struct GetDisplayNameData
7999 GtkFileChooserDefault *impl;
8004 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8006 const GError *error,
8009 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8010 gboolean should_respond = FALSE;
8011 struct GetDisplayNameData *data = user_data;
8013 if (cancellable != data->impl->should_respond_get_info_cancellable)
8016 data->impl->should_respond_get_info_cancellable = NULL;
8022 /* Huh? Did the folder disappear? Let the caller deal with it */
8023 should_respond = TRUE;
8025 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8027 set_busy_cursor (data->impl, FALSE);
8029 g_signal_emit_by_name (data->impl, "response-requested");
8032 g_object_unref (data->impl);
8033 g_free (data->file_part);
8036 g_object_unref (cancellable);
8039 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8040 * should respond. Can get the file part from the file list or the save entry.
8043 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8044 const gchar *file_part,
8047 GtkFileChooserConfirmation conf;
8049 if (!impl->do_overwrite_confirmation)
8052 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8054 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8058 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8060 struct GetDisplayNameData *data;
8062 g_assert (file_part != NULL);
8064 data = g_new0 (struct GetDisplayNameData, 1);
8065 data->impl = g_object_ref (impl);
8066 data->file_part = g_strdup (file_part);
8068 if (impl->should_respond_get_info_cancellable)
8069 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8071 impl->should_respond_get_info_cancellable =
8072 gtk_file_system_get_info (impl->file_system, parent_file,
8073 "standard::display-name",
8074 confirmation_confirm_get_info_cb,
8076 set_busy_cursor (data->impl, TRUE);
8080 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8083 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8087 g_assert_not_reached ();
8092 struct FileExistsData
8094 GtkFileChooserDefault *impl;
8095 gboolean file_exists_and_is_not_folder;
8101 save_entry_get_info_cb (GCancellable *cancellable,
8103 const GError *error,
8106 gboolean parent_is_folder;
8107 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8108 struct FileExistsData *data = user_data;
8110 if (cancellable != data->impl->should_respond_get_info_cancellable)
8113 data->impl->should_respond_get_info_cancellable = NULL;
8115 set_busy_cursor (data->impl, FALSE);
8121 parent_is_folder = FALSE;
8123 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8125 if (parent_is_folder)
8127 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8129 if (data->file_exists_and_is_not_folder)
8132 const char *file_part;
8134 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8135 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8138 g_signal_emit_by_name (data->impl, "response-requested");
8141 g_signal_emit_by_name (data->impl, "response-requested");
8143 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8145 GError *error = NULL;
8147 set_busy_cursor (data->impl, TRUE);
8148 g_file_make_directory (data->file, NULL, &error);
8149 set_busy_cursor (data->impl, FALSE);
8152 g_signal_emit_by_name (data->impl, "response-requested");
8154 error_creating_folder_dialog (data->impl, data->file, error);
8159 /* This will display an error, which is what we want */
8160 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8164 g_object_unref (data->impl);
8165 g_object_unref (data->file);
8166 g_object_unref (data->parent_file);
8169 g_object_unref (cancellable);
8173 file_exists_get_info_cb (GCancellable *cancellable,
8175 const GError *error,
8178 gboolean data_ownership_taken = FALSE;
8179 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8180 gboolean file_exists_and_is_not_folder;
8181 struct FileExistsData *data = user_data;
8183 if (cancellable != data->impl->file_exists_get_info_cancellable)
8186 data->impl->file_exists_get_info_cancellable = NULL;
8188 set_busy_cursor (data->impl, FALSE);
8193 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8195 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8196 /* user typed a filename; we are done */
8197 g_signal_emit_by_name (data->impl, "response-requested");
8198 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8199 && file_exists_and_is_not_folder)
8201 /* Oops, the user typed the name of an existing path which is not
8204 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8205 g_error_copy (error));
8209 /* check that everything up to the last component exists */
8211 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8212 data_ownership_taken = TRUE;
8214 if (data->impl->should_respond_get_info_cancellable)
8215 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8217 data->impl->should_respond_get_info_cancellable =
8218 gtk_file_system_get_info (data->impl->file_system,
8221 save_entry_get_info_cb,
8223 set_busy_cursor (data->impl, TRUE);
8227 if (!data_ownership_taken)
8229 g_object_unref (data->impl);
8230 g_object_unref (data->file);
8231 g_object_unref (data->parent_file);
8235 g_object_unref (cancellable);
8239 paste_text_received (GtkClipboard *clipboard,
8241 GtkFileChooserDefault *impl)
8248 file = g_file_new_for_uri (text);
8250 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8251 location_popup_handler (impl, text);
8253 g_object_unref (file);
8256 /* Handler for the "location-popup-on-paste" keybinding signal */
8258 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8260 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8261 GDK_SELECTION_CLIPBOARD);
8262 gtk_clipboard_request_text (clipboard,
8263 (GtkClipboardTextReceivedFunc) paste_text_received,
8268 /* Implementation for GtkFileChooserEmbed::should_respond() */
8270 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8272 GtkFileChooserDefault *impl;
8273 GtkWidget *toplevel;
8274 GtkWidget *current_focus;
8276 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8278 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8279 g_assert (GTK_IS_WINDOW (toplevel));
8281 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8283 if (current_focus == impl->browse_files_tree_view)
8285 /* The following array encodes what we do based on the impl->action and the
8286 * number of files selected.
8289 NOOP, /* Do nothing (don't respond) */
8290 RESPOND, /* Respond immediately */
8291 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8292 ALL_FILES, /* Respond only if everything selected is a file */
8293 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8294 SAVE_ENTRY, /* Go to the code for handling the save entry */
8295 NOT_REACHED /* Sanity check */
8297 static const ActionToTake what_to_do[4][3] = {
8298 /* 0 selected 1 selected many selected */
8299 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8300 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8301 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8302 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8306 gboolean all_files, all_folders;
8308 ActionToTake action;
8312 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8314 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8315 return search_should_respond (impl);
8317 if (impl->operation_mode == OPERATION_MODE_RECENT)
8318 return recent_should_respond (impl);
8320 selection_check (impl, &num_selected, &all_files, &all_folders);
8322 if (num_selected > 2)
8327 action = what_to_do [impl->action] [k];
8337 case RESPOND_OR_SWITCH:
8338 g_assert (num_selected == 1);
8342 switch_to_selected_folder (impl);
8345 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8346 return should_respond_after_confirm_overwrite (impl,
8347 get_display_name_from_file_list (impl),
8348 impl->current_folder);
8362 g_assert_not_reached ();
8365 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8368 gboolean is_well_formed, is_empty, is_file_part_empty;
8371 GtkFileChooserEntry *entry;
8376 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8377 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8378 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8379 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8380 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8382 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8383 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8385 if (is_empty || !is_well_formed)
8388 g_assert (file != NULL);
8393 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8394 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8396 change_folder_and_display_error (impl, file, TRUE);
8399 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8400 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8402 /* The folder already exists, so we do not need to create it.
8403 * Just respond to terminate the dialog.
8409 g_assert_not_reached ();
8415 struct FileExistsData *data;
8417 /* We need to check whether file exists and is not a folder */
8419 data = g_new0 (struct FileExistsData, 1);
8420 data->impl = g_object_ref (impl);
8421 data->file = g_object_ref (file);
8422 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8424 if (impl->file_exists_get_info_cancellable)
8425 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8427 impl->file_exists_get_info_cancellable =
8428 gtk_file_system_get_info (impl->file_system, file,
8430 file_exists_get_info_cb,
8433 set_busy_cursor (impl, TRUE);
8437 g_error_free (error);
8440 g_object_unref (file);
8443 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8445 /* The focus is on a dialog's action area button, *and* the widget that
8446 * was focused immediately before it is the file list.
8450 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8452 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8455 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8457 /* The focus is on a dialog's action area button, *and* the widget that
8458 * was focused immediately before it is the location entry.
8463 /* The focus is on a dialog's action area button or something else */
8464 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8465 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8470 g_assert_not_reached ();
8474 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8476 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8478 GtkFileChooserDefault *impl;
8481 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8483 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8484 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8486 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8487 widget = impl->browse_files_tree_view;
8489 widget = impl->location_entry;
8491 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8492 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8493 widget = impl->location_entry;
8496 g_assert_not_reached ();
8500 g_assert (widget != NULL);
8501 gtk_widget_grab_focus (widget);
8504 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8506 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8516 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8517 *list = g_slist_prepend (*list, file);
8520 /* Constructs a list of the selected paths in search mode */
8522 search_get_selected_files (GtkFileChooserDefault *impl)
8525 GtkTreeSelection *selection;
8529 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8530 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8531 result = g_slist_reverse (result);
8536 /* Called from ::should_respond(). We return whether there are selected files
8537 * in the search list.
8540 search_should_respond (GtkFileChooserDefault *impl)
8542 GtkTreeSelection *selection;
8544 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8546 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8547 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8550 struct SearchHitInsertRequest
8552 GtkFileChooserDefault *impl;
8554 GtkTreeRowReference *row_ref;
8558 search_hit_get_info_cb (GCancellable *cancellable,
8560 const GError *error,
8563 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8564 GdkPixbuf *pixbuf = NULL;
8567 GCancellable *model_cancellable;
8568 gboolean is_folder = FALSE;
8571 struct SearchHitInsertRequest *request = data;
8573 if (!request->impl->search_model)
8576 path = gtk_tree_row_reference_get_path (request->row_ref);
8580 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8582 gtk_tree_path_free (path);
8584 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8585 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8587 if (cancellable != model_cancellable)
8590 /* set the cancellable to NULL in the model */
8591 gtk_list_store_set (request->impl->search_model, &iter,
8592 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8600 gtk_list_store_remove (request->impl->search_model, &iter);
8604 display_name = g_strdup (g_file_info_get_display_name (info));
8605 mime_type = g_strdup (g_file_info_get_content_type (info));
8606 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8607 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8608 request->impl->icon_size);
8610 gtk_list_store_set (request->impl->search_model, &iter,
8611 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8612 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8613 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8614 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8618 g_object_unref (pixbuf);
8621 g_object_unref (request->impl);
8622 g_object_unref (request->file);
8623 gtk_tree_row_reference_free (request->row_ref);
8626 g_object_unref (cancellable);
8629 /* Adds one hit from the search engine to the search_model */
8631 search_add_hit (GtkFileChooserDefault *impl,
8637 char *collation_key;
8638 struct stat statbuf;
8639 struct stat *statbuf_copy;
8642 GCancellable *cancellable;
8643 struct SearchHitInsertRequest *request;
8645 file = g_file_new_for_uri (uri);
8647 if (!g_file_is_native (file))
8649 g_object_unref (file);
8653 filename = g_file_get_path (file);
8655 if (stat (filename, &statbuf) != 0)
8657 g_object_unref (file);
8662 statbuf_copy = g_new (struct stat, 1);
8663 *statbuf_copy = statbuf;
8665 tmp = g_file_get_parse_name (file);
8666 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8669 request = g_new0 (struct SearchHitInsertRequest, 1);
8670 request->impl = g_object_ref (impl);
8671 request->file = g_object_ref (file);
8673 gtk_list_store_append (impl->search_model, &iter);
8674 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8676 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8677 gtk_tree_path_free (p);
8679 cancellable = gtk_file_system_get_info (impl->file_system, file,
8680 "standard::type,standard::icon,"
8681 "standard::content-type,standard::display-name",
8682 search_hit_get_info_cb,
8685 gtk_list_store_set (impl->search_model, &iter,
8686 SEARCH_MODEL_COL_FILE, file,
8687 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8688 SEARCH_MODEL_COL_STAT, statbuf_copy,
8689 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8692 g_object_unref (file);
8696 /* Callback used from GtkSearchEngine when we get new hits */
8698 search_engine_hits_added_cb (GtkSearchEngine *engine,
8702 GtkFileChooserDefault *impl;
8705 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8707 for (l = hits; l; l = l->next)
8708 search_add_hit (impl, (gchar*)l->data);
8711 /* Callback used from GtkSearchEngine when the query is done running */
8713 search_engine_finished_cb (GtkSearchEngine *engine,
8716 GtkFileChooserDefault *impl;
8718 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8721 /* EB: setting the model here will avoid loads of row events,
8722 * but it'll make the search look like blocked.
8724 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8725 GTK_TREE_MODEL (impl->search_model_filter));
8728 /* FMQ: if search was empty, say that we got no hits */
8729 set_busy_cursor (impl, FALSE);
8732 /* Displays a generic error when we cannot create a GtkSearchEngine.
8733 * It would be better if _gtk_search_engine_new() gave us a GError
8734 * with a better message, but it doesn't do that right now.
8737 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8739 error_message (impl,
8740 _("Could not start the search process"),
8741 _("The program was not able to create a connection to the indexer "
8742 "daemon. Please make sure it is running."));
8746 search_engine_error_cb (GtkSearchEngine *engine,
8747 const gchar *message,
8750 GtkFileChooserDefault *impl;
8752 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8754 search_stop_searching (impl, TRUE);
8755 error_message (impl, _("Could not send the search request"), message);
8757 set_busy_cursor (impl, FALSE);
8760 /* Frees the data in the search_model */
8762 search_clear_model (GtkFileChooserDefault *impl,
8763 gboolean remove_from_treeview)
8765 GtkTreeModel *model;
8768 if (!impl->search_model)
8771 model = GTK_TREE_MODEL (impl->search_model);
8773 if (gtk_tree_model_get_iter_first (model, &iter))
8776 GCancellable *cancellable;
8778 gtk_tree_model_get (model, &iter,
8779 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8783 g_cancellable_cancel (cancellable);
8785 while (gtk_tree_model_iter_next (model, &iter));
8787 g_object_unref (impl->search_model);
8788 impl->search_model = NULL;
8790 g_object_unref (impl->search_model_filter);
8791 impl->search_model_filter = NULL;
8793 g_object_unref (impl->search_model_sort);
8794 impl->search_model_sort = NULL;
8796 if (remove_from_treeview)
8797 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8800 /* Stops any ongoing searches; does not touch the search_model */
8802 search_stop_searching (GtkFileChooserDefault *impl,
8803 gboolean remove_query)
8805 if (remove_query && impl->search_query)
8807 g_object_unref (impl->search_query);
8808 impl->search_query = NULL;
8811 if (impl->search_engine)
8813 _gtk_search_engine_stop (impl->search_engine);
8815 g_object_unref (impl->search_engine);
8816 impl->search_engine = NULL;
8820 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8822 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8824 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8826 search_stop_searching (impl, FALSE);
8827 search_clear_model (impl, TRUE);
8829 gtk_widget_destroy (impl->search_hbox);
8830 impl->search_hbox = NULL;
8831 impl->search_entry = NULL;
8833 gtk_widget_show (impl->browse_path_bar);
8834 gtk_widget_show (impl->browse_new_folder_button);
8836 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8837 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8839 gtk_widget_show (impl->location_button);
8841 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8842 gtk_widget_show (impl->location_entry_box);
8845 impl->operation_mode = OPERATION_MODE_BROWSE;
8847 file_list_set_sort_column_ids (impl);
8850 /* Sort callback from the path column */
8852 search_column_path_sort_func (GtkTreeModel *model,
8857 GtkFileChooserDefault *impl = user_data;
8858 GtkTreeIter child_a, child_b;
8859 const char *collation_key_a, *collation_key_b;
8860 gboolean is_folder_a, is_folder_b;
8862 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8863 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8865 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8866 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8867 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8869 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8870 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8871 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8874 if (!collation_key_a)
8877 if (!collation_key_b)
8880 /* always show folders first */
8881 if (is_folder_a != is_folder_b)
8882 return is_folder_a ? 1 : -1;
8884 return strcmp (collation_key_a, collation_key_b);
8887 /* Sort callback from the modification time column */
8889 search_column_mtime_sort_func (GtkTreeModel *model,
8894 GtkFileChooserDefault *impl = user_data;
8895 GtkTreeIter child_a, child_b;
8896 const struct stat *statbuf_a, *statbuf_b;
8897 gboolean is_folder_a, is_folder_b;
8899 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8900 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8902 /* Note that although we store a whole struct stat in the model, we only
8903 * compare the mtime here. If we add another column relative to a struct stat
8904 * (e.g. a file size column), we'll want another sort callback similar to this
8907 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8908 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8909 SEARCH_MODEL_COL_STAT, &statbuf_a,
8911 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8912 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8913 SEARCH_MODEL_COL_STAT, &statbuf_b,
8922 if (is_folder_a != is_folder_b)
8923 return is_folder_a ? 1 : -1;
8925 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8927 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8934 search_get_is_filtered (GtkFileChooserDefault *impl,
8936 const gchar *display_name,
8937 const gchar *mime_type)
8939 GtkFileFilterInfo filter_info;
8940 GtkFileFilterFlags needed;
8943 if (!impl->current_filter)
8946 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8947 needed = gtk_file_filter_get_needed (impl->current_filter);
8949 filter_info.display_name = display_name;
8950 filter_info.mime_type = mime_type;
8952 if (needed & GTK_FILE_FILTER_FILENAME)
8954 filter_info.filename = g_file_get_path (file);
8955 if (filter_info.filename)
8956 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8959 filter_info.filename = NULL;
8961 if (needed & GTK_FILE_FILTER_URI)
8963 filter_info.uri = g_file_get_uri (file);
8964 if (filter_info.uri)
8965 filter_info.contains |= GTK_FILE_FILTER_URI;
8968 filter_info.uri = NULL;
8970 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8972 if (filter_info.filename)
8973 g_free ((gchar *) filter_info.filename);
8974 if (filter_info.uri)
8975 g_free ((gchar *) filter_info.uri);
8981 /* Visibility function for the recent filter model */
8983 search_model_visible_func (GtkTreeModel *model,
8987 GtkFileChooserDefault *impl = user_data;
8989 gchar *display_name, *mime_type;
8992 if (!impl->current_filter)
8995 gtk_tree_model_get (model, iter,
8996 SEARCH_MODEL_COL_FILE, &file,
8997 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
8998 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8999 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9008 return !search_get_is_filtered (impl, file, display_name, mime_type);
9011 /* Creates the search_model and puts it in the tree view */
9013 search_setup_model (GtkFileChooserDefault *impl)
9015 g_assert (impl->search_model == NULL);
9016 g_assert (impl->search_model_filter == NULL);
9017 g_assert (impl->search_model_sort == NULL);
9019 /* We store these columns in the search model:
9021 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9022 * as a pointer not as a G_TYPE_FILE
9023 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9024 * as a pointer not as a G_TYPE_STRING
9025 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9026 * as a pointer not as a G_TYPE_STRING
9027 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9028 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9029 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9030 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9031 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9033 * Keep this in sync with the enumeration defined near the beginning
9036 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9046 impl->search_model_filter =
9047 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9048 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9049 search_model_visible_func,
9052 impl->search_model_sort =
9053 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9054 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9055 SEARCH_MODEL_COL_FILE,
9056 search_column_path_sort_func,
9058 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9059 SEARCH_MODEL_COL_STAT,
9060 search_column_mtime_sort_func,
9062 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9063 SEARCH_MODEL_COL_STAT,
9064 GTK_SORT_DESCENDING);
9066 /* EB: setting the model here will make the hits list update feel
9067 * more "alive" than setting the model at the end of the search
9070 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9071 GTK_TREE_MODEL (impl->search_model_sort));
9075 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9076 GtkTreeIter *child_iter,
9081 if (!impl->search_model)
9084 if (!impl->search_model_filter || !impl->search_model_sort)
9087 /* pass 1: get the iterator in the filter model */
9088 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9091 /* pass 2: get the iterator in the real model */
9092 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9093 child_iter, &middle);
9096 /* Creates a new query with the specified text and launches it */
9098 search_start_query (GtkFileChooserDefault *impl,
9099 const gchar *query_text)
9101 search_stop_searching (impl, FALSE);
9102 search_clear_model (impl, TRUE);
9103 search_setup_model (impl);
9104 set_busy_cursor (impl, TRUE);
9106 if (impl->search_engine == NULL)
9107 impl->search_engine = _gtk_search_engine_new ();
9109 if (!impl->search_engine)
9111 set_busy_cursor (impl, FALSE);
9112 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9116 if (!impl->search_query)
9118 impl->search_query = _gtk_query_new ();
9119 _gtk_query_set_text (impl->search_query, query_text);
9122 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9124 g_signal_connect (impl->search_engine, "hits-added",
9125 G_CALLBACK (search_engine_hits_added_cb), impl);
9126 g_signal_connect (impl->search_engine, "finished",
9127 G_CALLBACK (search_engine_finished_cb), impl);
9128 g_signal_connect (impl->search_engine, "error",
9129 G_CALLBACK (search_engine_error_cb), impl);
9131 _gtk_search_engine_start (impl->search_engine);
9134 /* Callback used when the user presses Enter while typing on the search
9135 * entry; starts the query
9138 search_entry_activate_cb (GtkEntry *entry,
9141 GtkFileChooserDefault *impl;
9144 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9146 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9147 if (strlen (text) == 0)
9150 /* reset any existing query object */
9151 if (impl->search_query)
9153 g_object_unref (impl->search_query);
9154 impl->search_query = NULL;
9157 search_start_query (impl, text);
9160 /* Hides the path bar and creates the search entry */
9162 search_setup_widgets (GtkFileChooserDefault *impl)
9166 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9170 label = gtk_label_new_with_mnemonic (_("_Search:"));
9171 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9175 impl->search_entry = gtk_entry_new ();
9176 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9177 g_signal_connect (impl->search_entry, "activate",
9178 G_CALLBACK (search_entry_activate_cb),
9180 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9182 /* if there already is a query, restart it */
9183 if (impl->search_query)
9185 gchar *query = _gtk_query_get_text (impl->search_query);
9189 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9190 search_start_query (impl, query);
9196 g_object_unref (impl->search_query);
9197 impl->search_query = NULL;
9201 gtk_widget_hide (impl->browse_path_bar);
9202 gtk_widget_hide (impl->browse_new_folder_button);
9204 /* Box for search widgets */
9205 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9206 gtk_widget_show_all (impl->search_hbox);
9208 /* Hide the location widgets temporarily */
9210 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9211 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9213 gtk_widget_hide (impl->location_button);
9214 gtk_widget_hide (impl->location_entry_box);
9217 gtk_widget_grab_focus (impl->search_entry);
9219 /* FMQ: hide the filter combo? */
9222 /* Main entry point to the searching functions; this gets called when the user
9223 * activates the Search shortcut.
9226 search_activate (GtkFileChooserDefault *impl)
9228 OperationMode previous_mode;
9230 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9232 gtk_widget_grab_focus (impl->search_entry);
9236 previous_mode = impl->operation_mode;
9237 impl->operation_mode = OPERATION_MODE_SEARCH;
9239 switch (previous_mode)
9241 case OPERATION_MODE_RECENT:
9242 recent_stop_loading (impl);
9243 recent_clear_model (impl, TRUE);
9246 case OPERATION_MODE_BROWSE:
9247 stop_loading_and_clear_list_model (impl);
9250 case OPERATION_MODE_SEARCH:
9251 g_assert_not_reached ();
9255 g_assert (impl->search_hbox == NULL);
9256 g_assert (impl->search_entry == NULL);
9257 g_assert (impl->search_model == NULL);
9258 g_assert (impl->search_model_filter == NULL);
9260 search_setup_widgets (impl);
9261 file_list_set_sort_column_ids (impl);
9265 * Recent files support
9268 /* Frees the data in the recent_model */
9270 recent_clear_model (GtkFileChooserDefault *impl,
9271 gboolean remove_from_treeview)
9273 GtkTreeModel *model;
9276 if (!impl->recent_model)
9279 model = GTK_TREE_MODEL (impl->recent_model);
9281 if (remove_from_treeview)
9282 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9284 if (gtk_tree_model_get_iter_first (model, &iter))
9289 GCancellable *cancellable;
9290 GtkRecentInfo *recent_info;
9291 gchar *display_name;
9293 gtk_tree_model_get (model, &iter,
9294 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9295 RECENT_MODEL_COL_FILE, &file,
9296 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9297 RECENT_MODEL_COL_INFO, &recent_info,
9301 g_cancellable_cancel (cancellable);
9303 g_object_unref (file);
9304 gtk_recent_info_unref (recent_info);
9305 g_free (display_name);
9307 while (gtk_tree_model_iter_next (model, &iter));
9310 g_object_unref (impl->recent_model);
9311 impl->recent_model = NULL;
9313 g_object_unref (impl->recent_model_filter);
9314 impl->recent_model_filter = NULL;
9316 g_object_unref (impl->recent_model_sort);
9317 impl->recent_model_sort = NULL;
9320 /* Stops any ongoing loading of the recent files list; does
9321 * not touch the recent_model
9324 recent_stop_loading (GtkFileChooserDefault *impl)
9326 if (impl->load_recent_id)
9328 g_source_remove (impl->load_recent_id);
9329 impl->load_recent_id = 0;
9333 /* Stops any pending load, clears the file list, and switches
9334 * back to OPERATION_MODE_BROWSE
9337 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9339 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9341 recent_stop_loading (impl);
9342 recent_clear_model (impl, TRUE);
9344 gtk_widget_show (impl->browse_path_bar);
9345 gtk_widget_show (impl->browse_new_folder_button);
9347 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9348 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9350 gtk_widget_show (impl->location_button);
9352 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9353 gtk_widget_show (impl->location_entry_box);
9356 impl->operation_mode = OPERATION_MODE_BROWSE;
9358 file_list_set_sort_column_ids (impl);
9361 /* Sort callback from the modification time column */
9363 recent_column_mtime_sort_func (GtkTreeModel *model,
9368 GtkFileChooserDefault *impl = user_data;
9369 GtkTreeIter child_a, child_b;
9370 GtkRecentInfo *info_a, *info_b;
9371 gboolean is_folder_a, is_folder_b;
9373 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9374 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9376 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9377 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9378 RECENT_MODEL_COL_INFO, &info_a,
9380 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9381 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9382 RECENT_MODEL_COL_INFO, &info_b,
9391 /* folders always go first */
9392 if (is_folder_a != is_folder_b)
9393 return is_folder_a ? 1 : -1;
9395 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9397 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9404 recent_column_path_sort_func (GtkTreeModel *model,
9409 GtkFileChooserDefault *impl = user_data;
9410 GtkTreeIter child_a, child_b;
9411 gboolean is_folder_a, is_folder_b;
9412 gchar *name_a, *name_b;
9414 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9415 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9417 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9418 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9419 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9421 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9422 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9423 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9432 if (is_folder_a != is_folder_b)
9433 return is_folder_a ? 1 : -1;
9435 return strcmp (name_a, name_b);
9439 recent_get_is_filtered (GtkFileChooserDefault *impl,
9441 GtkRecentInfo *recent_info)
9443 GtkFileFilterInfo filter_info;
9444 GtkFileFilterFlags needed;
9447 if (!impl->current_filter)
9450 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9451 needed = gtk_file_filter_get_needed (impl->current_filter);
9453 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9454 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9456 if (needed & GTK_FILE_FILTER_FILENAME)
9458 filter_info.filename = g_file_get_path (file);
9459 if (filter_info.filename)
9460 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9463 filter_info.filename = NULL;
9465 if (needed & GTK_FILE_FILTER_URI)
9467 filter_info.uri = g_file_get_uri (file);
9468 if (filter_info.uri)
9469 filter_info.contains |= GTK_FILE_FILTER_URI;
9472 filter_info.uri = NULL;
9474 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9476 if (filter_info.filename)
9477 g_free ((gchar *) filter_info.filename);
9478 if (filter_info.uri)
9479 g_free ((gchar *) filter_info.uri);
9484 /* Visibility function for the recent filter model */
9486 recent_model_visible_func (GtkTreeModel *model,
9490 GtkFileChooserDefault *impl = user_data;
9492 GtkRecentInfo *recent_info;
9495 if (!impl->current_filter)
9498 gtk_tree_model_get (model, iter,
9499 RECENT_MODEL_COL_INFO, &recent_info,
9500 RECENT_MODEL_COL_FILE, &file,
9501 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9510 return !recent_get_is_filtered (impl, file, recent_info);
9514 recent_setup_model (GtkFileChooserDefault *impl)
9516 g_assert (impl->recent_model == NULL);
9517 g_assert (impl->recent_model_filter == NULL);
9518 g_assert (impl->recent_model_sort == NULL);
9520 /* We store these columns in the search model:
9522 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9523 * stored as a pointer and not as a G_TYPE_FILE;
9524 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9525 * stored as a pointer and not as a G_TYPE_STRING;
9526 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9527 * as a GTK_TYPE_RECENT_INFO;
9528 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9529 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9530 * and not as a G_TYPE_CANCELLABLE;
9532 * Keep this in sync with the enumeration defined near the beginning of
9535 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9542 impl->recent_model_filter =
9543 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9544 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9545 recent_model_visible_func,
9549 /* this is the model that will actually be added to
9550 * the browse_files_tree_view widget; remember: we are
9551 * stuffing the real model into a filter model and then
9552 * into a sort model; this means we'll have to translate
9553 * the child iterator *twice* to get from a path or an
9554 * iterator coming from the tree view widget to the
9555 * real data inside the model.
9557 impl->recent_model_sort =
9558 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9559 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9560 RECENT_MODEL_COL_FILE,
9561 recent_column_path_sort_func,
9563 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9564 RECENT_MODEL_COL_INFO,
9565 recent_column_mtime_sort_func,
9567 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9568 RECENT_MODEL_COL_INFO,
9569 GTK_SORT_DESCENDING);
9574 GtkFileChooserDefault *impl;
9577 gint n_loaded_items;
9578 guint needs_sorting : 1;
9582 recent_idle_cleanup (gpointer data)
9584 RecentLoadData *load_data = data;
9585 GtkFileChooserDefault *impl = load_data->impl;
9587 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9588 GTK_TREE_MODEL (impl->recent_model_sort));
9590 set_busy_cursor (impl, FALSE);
9592 impl->load_recent_id = 0;
9594 if (load_data->items)
9596 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9597 g_list_free (load_data->items);
9603 struct RecentItemInsertRequest
9605 GtkFileChooserDefault *impl;
9607 GtkTreeRowReference *row_ref;
9611 recent_item_get_info_cb (GCancellable *cancellable,
9613 const GError *error,
9616 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9619 GCancellable *model_cancellable;
9620 gboolean is_folder = FALSE;
9621 struct RecentItemInsertRequest *request = data;
9623 if (!request->impl->recent_model)
9626 path = gtk_tree_row_reference_get_path (request->row_ref);
9630 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9632 gtk_tree_path_free (path);
9634 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9635 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9637 if (cancellable != model_cancellable)
9640 gtk_list_store_set (request->impl->recent_model, &iter,
9641 RECENT_MODEL_COL_CANCELLABLE, NULL,
9649 gtk_list_store_remove (request->impl->recent_model, &iter);
9653 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9655 gtk_list_store_set (request->impl->recent_model, &iter,
9656 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9660 g_object_unref (request->impl);
9661 g_object_unref (request->file);
9662 gtk_tree_row_reference_free (request->row_ref);
9665 g_object_unref (cancellable);
9669 recent_sort_mru (gconstpointer a,
9672 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9673 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9675 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9679 get_recent_files_limit (GtkWidget *widget)
9681 GtkSettings *settings;
9684 if (gtk_widget_has_screen (widget))
9685 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9687 settings = gtk_settings_get_default ();
9689 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9695 recent_idle_load (gpointer data)
9697 RecentLoadData *load_data = data;
9698 GtkFileChooserDefault *impl = load_data->impl;
9701 GtkRecentInfo *info;
9702 const gchar *uri, *display_name;
9704 GCancellable *cancellable;
9705 struct RecentItemInsertRequest *request;
9707 if (!impl->recent_manager)
9710 /* first iteration: load all the items */
9711 if (!load_data->items)
9713 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9714 if (!load_data->items)
9717 load_data->needs_sorting = TRUE;
9722 /* second iteration: preliminary MRU sorting and clamping */
9723 if (load_data->needs_sorting)
9727 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9728 load_data->n_items = g_list_length (load_data->items);
9730 limit = get_recent_files_limit (GTK_WIDGET (impl));
9732 if (limit != -1 && (load_data->n_items > limit))
9736 clamp = g_list_nth (load_data->items, limit - 1);
9737 if (G_LIKELY (clamp))
9742 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9745 load_data->n_items = limit;
9749 load_data->n_loaded_items = 0;
9750 load_data->needs_sorting = FALSE;
9755 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9756 g_assert (info != NULL);
9758 uri = gtk_recent_info_get_uri (info);
9759 display_name = gtk_recent_info_get_display_name (info);
9760 file = g_file_new_for_uri (uri);
9762 gtk_list_store_append (impl->recent_model, &iter);
9763 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9765 request = g_new0 (struct RecentItemInsertRequest, 1);
9766 request->impl = g_object_ref (impl);
9767 request->file = g_object_ref (file);
9768 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9769 gtk_tree_path_free (p);
9771 cancellable = gtk_file_system_get_info (impl->file_system, file,
9773 recent_item_get_info_cb,
9776 gtk_list_store_set (impl->recent_model, &iter,
9777 RECENT_MODEL_COL_FILE, file,
9778 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9779 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9780 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9783 load_data->n_loaded_items += 1;
9785 /* finished loading items */
9786 if (load_data->n_loaded_items == load_data->n_items)
9788 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9789 g_list_free (load_data->items);
9790 load_data->items = NULL;
9799 recent_start_loading (GtkFileChooserDefault *impl)
9801 RecentLoadData *load_data;
9803 recent_stop_loading (impl);
9804 recent_clear_model (impl, TRUE);
9805 recent_setup_model (impl);
9806 set_busy_cursor (impl, TRUE);
9808 if (!impl->recent_manager)
9809 recent_manager_update (impl);
9811 g_assert (impl->load_recent_id == 0);
9813 load_data = g_new (RecentLoadData, 1);
9814 load_data->impl = impl;
9815 load_data->items = NULL;
9816 load_data->n_items = 0;
9817 load_data->n_loaded_items = 0;
9818 load_data->needs_sorting = TRUE;
9820 /* begin lazy loading the recent files into the model */
9821 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9824 recent_idle_cleanup);
9828 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9838 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9839 *list = g_slist_prepend (*list, g_object_ref (file));
9842 /* Constructs a list of the selected paths in recent files mode */
9844 recent_get_selected_files (GtkFileChooserDefault *impl)
9847 GtkTreeSelection *selection;
9851 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9852 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9853 result = g_slist_reverse (result);
9858 /* Called from ::should_respond(). We return whether there are selected
9859 * files in the recent files list.
9862 recent_should_respond (GtkFileChooserDefault *impl)
9864 GtkTreeSelection *selection;
9866 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9868 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9869 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9872 /* Hide the location widgets temporarily */
9874 recent_hide_entry (GtkFileChooserDefault *impl)
9876 gtk_widget_hide (impl->browse_path_bar);
9877 gtk_widget_hide (impl->browse_new_folder_button);
9879 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9880 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9882 gtk_widget_hide (impl->location_button);
9883 gtk_widget_hide (impl->location_entry_box);
9887 /* Main entry point to the recent files functions; this gets called when
9888 * the user activates the Recently Used shortcut.
9891 recent_activate (GtkFileChooserDefault *impl)
9893 OperationMode previous_mode;
9895 if (impl->operation_mode == OPERATION_MODE_RECENT)
9898 previous_mode = impl->operation_mode;
9899 impl->operation_mode = OPERATION_MODE_RECENT;
9901 switch (previous_mode)
9903 case OPERATION_MODE_SEARCH:
9904 search_stop_searching (impl, FALSE);
9905 search_clear_model (impl, TRUE);
9907 gtk_widget_destroy (impl->search_hbox);
9908 impl->search_hbox = NULL;
9909 impl->search_entry = NULL;
9912 case OPERATION_MODE_BROWSE:
9913 stop_loading_and_clear_list_model (impl);
9916 case OPERATION_MODE_RECENT:
9917 g_assert_not_reached ();
9921 recent_hide_entry (impl);
9922 file_list_set_sort_column_ids (impl);
9923 recent_start_loading (impl);
9926 /* convert an iterator coming from the model bound to
9927 * browse_files_tree_view to an interator inside the
9931 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9932 GtkTreeIter *child_iter,
9937 if (!impl->recent_model)
9940 if (!impl->recent_model_filter || !impl->recent_model_sort)
9943 /* pass 1: get the iterator in the filter model */
9944 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9947 /* pass 2: get the iterator in the real model */
9948 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9955 set_current_filter (GtkFileChooserDefault *impl,
9956 GtkFileFilter *filter)
9958 if (impl->current_filter != filter)
9962 /* NULL filters are allowed to reset to non-filtered status
9964 filter_index = g_slist_index (impl->filters, filter);
9965 if (impl->filters && filter && filter_index < 0)
9968 if (impl->current_filter)
9969 g_object_unref (impl->current_filter);
9970 impl->current_filter = filter;
9971 if (impl->current_filter)
9973 g_object_ref_sink (impl->current_filter);
9977 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9980 if (impl->browse_files_model)
9981 install_list_model_filter (impl);
9983 if (impl->search_model_filter)
9984 gtk_tree_model_filter_refilter (impl->search_model_filter);
9986 if (impl->recent_model_filter)
9987 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9989 g_object_notify (G_OBJECT (impl), "filter");
9994 filter_combo_changed (GtkComboBox *combo_box,
9995 GtkFileChooserDefault *impl)
9997 gint new_index = gtk_combo_box_get_active (combo_box);
9998 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10000 set_current_filter (impl, new_filter);
10004 check_preview_change (GtkFileChooserDefault *impl)
10006 GtkTreePath *cursor_path;
10008 const char *new_display_name;
10010 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10012 new_display_name = NULL;
10015 GtkTreeIter child_iter;
10017 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10019 if (impl->sort_model)
10022 GFileInfo *new_info;
10024 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10025 gtk_tree_path_free (cursor_path);
10027 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10029 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10030 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10032 new_display_name = g_file_info_get_display_name (new_info);
10035 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10039 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10040 &iter, cursor_path);
10041 gtk_tree_path_free (cursor_path);
10043 search_get_valid_child_iter (impl, &child_iter, &iter);
10044 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10045 SEARCH_MODEL_COL_FILE, &new_file,
10046 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10049 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10053 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10054 &iter, cursor_path);
10055 gtk_tree_path_free (cursor_path);
10057 recent_get_valid_child_iter (impl, &child_iter, &iter);
10058 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10059 RECENT_MODEL_COL_FILE, &new_file,
10060 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10065 if (new_file != impl->preview_file &&
10066 !(new_file && impl->preview_file &&
10067 g_file_equal (new_file, impl->preview_file)))
10069 if (impl->preview_file)
10071 g_object_unref (impl->preview_file);
10072 g_free (impl->preview_display_name);
10077 impl->preview_file = g_object_ref (new_file);
10078 impl->preview_display_name = g_strdup (new_display_name);
10082 impl->preview_file = NULL;
10083 impl->preview_display_name = NULL;
10086 if (impl->use_preview_label && impl->preview_label)
10087 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10089 g_signal_emit_by_name (impl, "update-preview");
10094 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10095 GtkFileSystemVolume *volume,
10096 const GError *error,
10100 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10101 GtkFileChooserDefault *impl = data;
10103 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10106 impl->shortcuts_activate_iter_cancellable = NULL;
10108 set_busy_cursor (impl, FALSE);
10115 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10119 name = gtk_file_system_volume_get_display_name (volume);
10120 msg = g_strdup_printf (_("Could not mount %s"), name);
10122 error_message (impl, msg, error->message);
10131 file = gtk_file_system_volume_get_root (volume);
10134 change_folder_and_display_error (impl, file, FALSE);
10135 g_object_unref (file);
10139 g_object_unref (impl);
10140 g_object_unref (cancellable);
10144 /* Activates a volume by mounting it if necessary and then switching to its
10148 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10149 GtkFileSystemVolume *volume)
10153 switch (impl->operation_mode)
10155 case OPERATION_MODE_BROWSE:
10157 case OPERATION_MODE_SEARCH:
10158 search_switch_to_browse_mode (impl);
10160 case OPERATION_MODE_RECENT:
10161 recent_switch_to_browse_mode (impl);
10165 /* We ref the file chooser since volume_mount() may run a main loop, and the
10166 * user could close the file chooser window in the meantime.
10168 g_object_ref (impl);
10170 if (!gtk_file_system_volume_is_mounted (volume))
10172 set_busy_cursor (impl, TRUE);
10174 impl->shortcuts_activate_iter_cancellable =
10175 gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10176 shortcuts_activate_volume_mount_cb,
10177 g_object_ref (impl));
10181 file = gtk_file_system_volume_get_root (volume);
10184 change_folder_and_display_error (impl, file, FALSE);
10185 g_object_unref (file);
10189 g_object_unref (impl);
10192 /* Opens the folder or volume at the specified iter in the shortcuts model */
10193 struct ShortcutsActivateData
10195 GtkFileChooserDefault *impl;
10200 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10202 const GError *error,
10203 gpointer user_data)
10205 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10206 struct ShortcutsActivateData *data = user_data;
10208 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10211 data->impl->shortcuts_activate_iter_cancellable = NULL;
10216 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10217 change_folder_and_display_error (data->impl, data->file, FALSE);
10219 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10224 g_object_unref (data->impl);
10225 g_object_unref (data->file);
10228 g_object_unref (cancellable);
10232 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10233 GtkFileSystemVolume *volume,
10234 const GError *error,
10235 gpointer user_data)
10237 struct ShortcutsActivateData *data = user_data;
10241 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10243 g_object_unref (data->impl);
10244 g_object_unref (data->file);
10250 data->impl->shortcuts_activate_iter_cancellable =
10251 gtk_file_system_get_info (data->impl->file_system, data->file,
10253 shortcuts_activate_get_info_cb, data);
10257 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10261 ShortcutType shortcut_type;
10263 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
10264 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10266 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10267 SHORTCUTS_COL_DATA, &col_data,
10268 SHORTCUTS_COL_TYPE, &shortcut_type,
10271 if (impl->shortcuts_activate_iter_cancellable)
10273 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10274 impl->shortcuts_activate_iter_cancellable = NULL;
10277 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10279 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10281 GtkFileSystemVolume *volume;
10285 shortcuts_activate_volume (impl, volume);
10287 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10289 struct ShortcutsActivateData *data;
10290 GtkFileSystemVolume *volume;
10292 volume = gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10294 data = g_new0 (struct ShortcutsActivateData, 1);
10295 data->impl = g_object_ref (impl);
10296 data->file = g_object_ref (col_data);
10298 if (!volume || !gtk_file_system_volume_is_mounted (volume))
10300 GMountOperation *mount_operation;
10301 GtkWidget *toplevel;
10303 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10305 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10307 impl->shortcuts_activate_iter_cancellable =
10308 gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10310 shortcuts_activate_mount_enclosing_volume,
10315 impl->shortcuts_activate_iter_cancellable =
10316 gtk_file_system_get_info (impl->file_system, data->file,
10318 shortcuts_activate_get_info_cb, data);
10321 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10323 search_activate (impl);
10325 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10327 recent_activate (impl);
10331 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10333 shortcuts_key_press_event_cb (GtkWidget *widget,
10334 GdkEventKey *event,
10335 GtkFileChooserDefault *impl)
10339 modifiers = gtk_accelerator_get_default_mod_mask ();
10341 if ((event->keyval == GDK_BackSpace
10342 || event->keyval == GDK_Delete
10343 || event->keyval == GDK_KP_Delete)
10344 && (event->state & modifiers) == 0)
10346 remove_selected_bookmarks (impl);
10350 if ((event->keyval == GDK_F2)
10351 && (event->state & modifiers) == 0)
10353 rename_selected_bookmark (impl);
10361 shortcuts_select_func (GtkTreeSelection *selection,
10362 GtkTreeModel *model,
10364 gboolean path_currently_selected,
10367 GtkFileChooserDefault *impl = data;
10368 GtkTreeIter filter_iter;
10369 ShortcutType shortcut_type;
10371 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10372 g_assert_not_reached ();
10374 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10376 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10380 list_select_func (GtkTreeSelection *selection,
10381 GtkTreeModel *model,
10383 gboolean path_currently_selected,
10386 GtkFileChooserDefault *impl = data;
10388 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10389 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10391 GtkTreeIter iter, child_iter;
10393 switch (impl->operation_mode)
10395 case OPERATION_MODE_SEARCH:
10397 gboolean is_folder;
10399 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10402 search_get_valid_child_iter (impl, &child_iter, &iter);
10403 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10404 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10411 case OPERATION_MODE_RECENT:
10413 gboolean is_folder;
10415 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10418 recent_get_valid_child_iter (impl, &child_iter, &iter);
10419 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10420 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10427 case OPERATION_MODE_BROWSE:
10431 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10434 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10435 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10436 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10447 list_selection_changed (GtkTreeSelection *selection,
10448 GtkFileChooserDefault *impl)
10450 /* See if we are in the new folder editable row for Save mode */
10451 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10452 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10455 gboolean had_selection;
10457 info = get_selected_file_info_from_file_list (impl, &had_selection);
10458 if (!had_selection)
10459 goto out; /* normal processing */
10462 return; /* We are on the editable row for New Folder */
10467 if (impl->location_entry)
10468 update_chooser_entry (impl);
10470 check_preview_change (impl);
10471 bookmarks_check_add_sensitivity (impl);
10473 g_signal_emit_by_name (impl, "selection-changed", 0);
10476 /* Callback used when a row in the file list is activated */
10478 list_row_activated (GtkTreeView *tree_view,
10480 GtkTreeViewColumn *column,
10481 GtkFileChooserDefault *impl)
10484 GtkTreeIter child_iter;
10486 switch (impl->operation_mode)
10488 case OPERATION_MODE_SEARCH:
10491 gboolean is_folder;
10493 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10496 search_get_valid_child_iter (impl, &child_iter, &iter);
10497 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10498 SEARCH_MODEL_COL_FILE, &file,
10499 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10504 change_folder_and_display_error (impl, file, FALSE);
10508 g_signal_emit_by_name (impl, "file-activated");
10512 case OPERATION_MODE_RECENT:
10515 gboolean is_folder;
10517 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10520 recent_get_valid_child_iter (impl, &child_iter, &iter);
10521 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10522 RECENT_MODEL_COL_FILE, &file,
10523 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10528 change_folder_and_display_error (impl, file, FALSE);
10532 g_signal_emit_by_name (impl, "file-activated");
10536 case OPERATION_MODE_BROWSE:
10540 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10543 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10544 &child_iter, &iter);
10545 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10548 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10552 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10553 change_folder_and_display_error (impl, file, FALSE);
10557 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10558 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10559 g_signal_emit_by_name (impl, "file-activated");
10566 path_bar_clicked (GtkPathBar *path_bar,
10569 gboolean child_is_hidden,
10570 GtkFileChooserDefault *impl)
10573 pending_select_files_add (impl, child_file);
10575 if (!change_folder_and_display_error (impl, file, FALSE))
10578 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10579 * show hidden files so that ".baz" appears in the file list, as it will still
10580 * be shown in the path bar: "/foo/[bar]/.baz"
10582 if (child_is_hidden)
10583 g_object_set (impl, "show-hidden", TRUE, NULL);
10587 get_list_file_info (GtkFileChooserDefault *impl,
10590 GtkTreeIter child_iter;
10592 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10596 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10600 list_icon_data_func (GtkTreeViewColumn *tree_column,
10601 GtkCellRenderer *cell,
10602 GtkTreeModel *tree_model,
10606 GtkFileChooserDefault *impl = data;
10607 GtkTreeIter child_iter;
10608 GdkPixbuf *pixbuf = NULL;
10609 gboolean sensitive = TRUE;
10611 profile_start ("start", NULL);
10613 switch (impl->operation_mode)
10615 case OPERATION_MODE_SEARCH:
10617 GtkTreeIter child_iter;
10618 gboolean is_folder;
10620 search_get_valid_child_iter (impl, &child_iter, iter);
10621 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10622 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10623 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10626 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10627 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10628 sensitive = is_folder;
10632 case OPERATION_MODE_RECENT:
10634 GtkTreeIter child_iter;
10635 GtkRecentInfo *info;
10636 gboolean is_folder;
10638 recent_get_valid_child_iter (impl, &child_iter, iter);
10639 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10640 RECENT_MODEL_COL_INFO, &info,
10641 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10644 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10646 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10647 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10648 sensitive = is_folder;
10652 case OPERATION_MODE_BROWSE:
10657 info = get_list_file_info (impl, iter);
10659 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10662 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10667 /* FIXME: NULL GError */
10668 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10673 /* We are on the editable row */
10678 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10679 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10680 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10685 g_object_set (cell,
10687 "sensitive", sensitive,
10691 g_object_unref (pixbuf);
10693 profile_end ("end", NULL);
10697 list_name_data_func (GtkTreeViewColumn *tree_column,
10698 GtkCellRenderer *cell,
10699 GtkTreeModel *tree_model,
10703 GtkFileChooserDefault *impl = data;
10705 gboolean sensitive = TRUE;
10707 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10709 GtkTreeIter child_iter;
10710 gchar *display_name;
10711 gboolean is_folder;
10713 search_get_valid_child_iter (impl, &child_iter, iter);
10714 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10715 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10716 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10719 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10720 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10722 sensitive = is_folder;
10725 g_object_set (cell,
10726 "text", display_name,
10727 "sensitive", sensitive,
10728 "ellipsize", PANGO_ELLIPSIZE_END,
10734 if (impl->operation_mode == OPERATION_MODE_RECENT)
10736 GtkTreeIter child_iter;
10737 GtkRecentInfo *recent_info;
10738 gchar *display_name;
10739 gboolean is_folder;
10741 recent_get_valid_child_iter (impl, &child_iter, iter);
10742 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10743 RECENT_MODEL_COL_INFO, &recent_info,
10744 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10747 display_name = gtk_recent_info_get_short_name (recent_info);
10749 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10750 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10752 sensitive = is_folder;
10755 g_object_set (cell,
10756 "text", display_name,
10757 "sensitive", sensitive,
10758 "ellipsize", PANGO_ELLIPSIZE_END,
10761 g_free (display_name);
10766 info = get_list_file_info (impl, iter);
10771 g_object_set (cell,
10772 "text", _("Type name of new folder"),
10774 "ellipsize", PANGO_ELLIPSIZE_NONE,
10781 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10782 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10784 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10787 g_object_set (cell,
10788 "text", g_file_info_get_display_name (info),
10789 "sensitive", sensitive,
10790 "ellipsize", PANGO_ELLIPSIZE_END,
10796 list_size_data_func (GtkTreeViewColumn *tree_column,
10797 GtkCellRenderer *cell,
10798 GtkTreeModel *tree_model,
10802 GtkFileChooserDefault *impl = data;
10803 GFileInfo *info = get_list_file_info (impl, iter);
10806 gboolean sensitive = TRUE;
10808 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10810 g_object_set (cell,
10812 "sensitive", sensitive,
10817 size = gtk_file_info_get_size (info);
10819 if (size < (gint64)1024)
10820 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
10821 else if (size < (gint64)1024*1024)
10822 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10823 else if (size < (gint64)1024*1024*1024)
10824 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10826 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10828 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10829 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10830 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10833 g_object_set (cell,
10835 "sensitive", sensitive,
10836 "alignment", PANGO_ALIGN_RIGHT,
10843 /* Tree column data callback for the file list; fetches the mtime of a file */
10845 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10846 GtkCellRenderer *cell,
10847 GtkTreeModel *tree_model,
10851 GtkFileChooserDefault *impl;
10852 GTimeVal timeval = { 0, };
10854 gchar *date_str = NULL;
10855 gboolean sensitive = TRUE;
10857 const char *locale, *dot = NULL;
10858 gint64 codepage = -1;
10864 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10866 GtkTreeIter child_iter;
10867 struct stat *statbuf;
10868 gboolean is_folder;
10870 search_get_valid_child_iter (impl, &child_iter, iter);
10871 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10872 SEARCH_MODEL_COL_STAT, &statbuf,
10873 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10876 time_mtime = statbuf->st_mtime;
10881 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10882 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10883 sensitive = is_folder;
10885 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10887 GtkTreeIter child_iter;
10888 GtkRecentInfo *info;
10889 gboolean is_folder;
10891 recent_get_valid_child_iter (impl, &child_iter, iter);
10892 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10893 RECENT_MODEL_COL_INFO, &info,
10894 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10898 time_mtime = gtk_recent_info_get_modified (info);
10902 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10903 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10904 sensitive = is_folder;
10910 info = get_list_file_info (impl, iter);
10913 g_object_set (cell,
10920 g_file_info_get_modification_time (info, &timeval);
10921 time_mtime = timeval.tv_sec;
10923 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10924 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10925 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10928 if (G_UNLIKELY (time_mtime == 0))
10929 date_str = g_strdup (_("Unknown"));
10934 struct tm tm_mtime;
10936 const gchar *format;
10937 gchar *locale_format = NULL;
10940 #ifdef HAVE_LOCALTIME_R
10941 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10944 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10948 g_warning ("ptm != NULL failed");
10950 g_object_set (cell,
10951 "text", _("Unknown"),
10952 "sensitive", sensitive,
10957 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10959 #endif /* HAVE_LOCALTIME_R */
10961 g_date_set_time_t (&mtime, time_mtime);
10962 time_now = time (NULL);
10963 g_date_set_time_t (&now, time_now);
10965 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10967 /* Translators: %H means "hours" and %M means "minutes" */
10968 if (days_diff == 0)
10969 format = _("%H:%M");
10970 else if (days_diff == 1)
10971 format = _("Yesterday at %H:%M");
10974 if (days_diff > 1 && days_diff < 7)
10975 format = "%A"; /* Days from last week */
10977 format = "%x"; /* Any other date */
10981 /* g_locale_from_utf8() returns a string in the system
10982 * code-page, which is not always the same as that used by the C
10983 * library. For instance when running a GTK+ program with
10984 * LANG=ko on an English version of Windows, the system
10985 * code-page is 1252, but the code-page used by the C library is
10986 * 949. (It's GTK+ itself that sets the C library locale when it
10987 * notices the LANG environment variable. See gtkmain.c The
10988 * Microsoft C library doesn't look at any locale environment
10989 * variables.) We need to pass strftime() a string in the C
10990 * library's code-page. See bug #509885.
10992 locale = setlocale (LC_ALL, NULL);
10993 if (locale != NULL)
10994 dot = strchr (locale, '.');
10997 codepage = g_ascii_strtoll (dot+1, NULL, 10);
10999 /* All codepages should fit in 16 bits AFAIK */
11000 if (codepage > 0 && codepage < 65536)
11002 sprintf (charset, "CP%u", (guint) codepage);
11003 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11007 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11009 if (locale_format != NULL &&
11010 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11013 /* As above but in opposite direction... */
11014 if (codepage > 0 && codepage < 65536)
11015 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11017 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11021 if (date_str == NULL)
11022 date_str = g_strdup (_("Unknown"));
11024 g_free (locale_format);
11027 g_object_set (cell,
11029 "sensitive", sensitive,
11035 _gtk_file_chooser_default_new (const char *file_system)
11037 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
11038 "file-system-backend", file_system,
11043 location_set_user_text (GtkFileChooserDefault *impl,
11046 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11047 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11051 location_popup_handler (GtkFileChooserDefault *impl,
11054 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11056 GtkWidget *widget_to_focus;
11058 /* This will give us the location widgets back */
11059 switch (impl->operation_mode)
11061 case OPERATION_MODE_SEARCH:
11062 search_switch_to_browse_mode (impl);
11064 case OPERATION_MODE_RECENT:
11065 recent_switch_to_browse_mode (impl);
11067 case OPERATION_MODE_BROWSE:
11068 g_assert_not_reached ();
11072 if (impl->current_folder)
11073 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11075 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11076 widget_to_focus = impl->browse_files_tree_view;
11078 widget_to_focus = impl->location_entry;
11080 gtk_widget_grab_focus (widget_to_focus);
11084 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11085 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11087 LocationMode new_mode;
11091 /* since the user typed something, we unconditionally want to turn on the entry */
11092 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11094 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11095 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11096 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11097 new_mode = LOCATION_MODE_PATH_BAR;
11100 g_assert_not_reached ();
11104 location_mode_set (impl, new_mode, TRUE);
11105 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11108 location_set_user_text (impl, path);
11111 location_entry_set_initial_text (impl);
11112 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11116 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11117 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11119 gtk_widget_grab_focus (impl->location_entry);
11121 location_set_user_text (impl, path);
11124 g_assert_not_reached ();
11127 /* Handler for the "up-folder" keybinding signal */
11129 up_folder_handler (GtkFileChooserDefault *impl)
11131 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11134 /* Handler for the "down-folder" keybinding signal */
11136 down_folder_handler (GtkFileChooserDefault *impl)
11138 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11141 /* Switches to the shortcut in the specified index */
11143 switch_to_shortcut (GtkFileChooserDefault *impl,
11148 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11149 g_assert_not_reached ();
11151 shortcuts_activate_iter (impl, &iter);
11154 /* Handler for the "home-folder" keybinding signal */
11156 home_folder_handler (GtkFileChooserDefault *impl)
11158 if (impl->has_home)
11159 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11162 /* Handler for the "desktop-folder" keybinding signal */
11164 desktop_folder_handler (GtkFileChooserDefault *impl)
11166 if (impl->has_desktop)
11167 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11170 /* Handler for the "search-shortcut" keybinding signal */
11172 search_shortcut_handler (GtkFileChooserDefault *impl)
11174 if (impl->has_search)
11176 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11178 /* we want the entry widget to grab the focus the first
11179 * time, not the browse_files_tree_view widget.
11181 if (impl->search_entry)
11182 gtk_widget_grab_focus (impl->search_entry);
11186 /* Handler for the "recent-shortcut" keybinding signal */
11188 recent_shortcut_handler (GtkFileChooserDefault *impl)
11190 if (impl->has_recent)
11191 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11195 quick_bookmark_handler (GtkFileChooserDefault *impl,
11196 gint bookmark_index)
11201 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11204 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11206 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11207 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11210 gtk_tree_path_free (path);
11212 switch_to_shortcut (impl, bookmark_pos);
11216 show_hidden_handler (GtkFileChooserDefault *impl)
11218 g_object_set (impl,
11219 "show-hidden", !impl->show_hidden,
11224 /* Drag and drop interfaces */
11227 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11232 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11234 model->impl = NULL;
11237 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11239 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11242 ShortcutsPaneModelFilter *model;
11246 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11248 pos = *gtk_tree_path_get_indices (path);
11249 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11251 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11254 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11256 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11258 GtkSelectionData *selection_data)
11260 ShortcutsPaneModelFilter *model;
11262 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11269 /* Fill the GtkTreeDragSourceIface vtable */
11271 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11273 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11274 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11278 /* Fill the GtkTreeDragDestIface vtable */
11280 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11282 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11283 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11287 static GtkTreeModel *
11288 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11289 GtkTreeModel *child_model,
11292 ShortcutsPaneModelFilter *model;
11294 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11295 "child-model", child_model,
11296 "virtual-root", root,
11299 model->impl = impl;
11301 return GTK_TREE_MODEL (model);
11307 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11310 RecentModelSort *model;
11311 GtkTreeIter iter, child_iter;
11312 gboolean is_folder;
11314 model = RECENT_MODEL_SORT (drag_source);
11315 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11318 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11319 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11320 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11327 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11329 GtkSelectionData *selection_data)
11331 RecentModelSort *model;
11332 GtkTreeIter iter, child_iter;
11336 model = RECENT_MODEL_SORT (drag_source);
11337 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11340 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11341 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11342 RECENT_MODEL_COL_FILE, &file,
11344 g_assert (file != NULL);
11346 uris[0] = g_file_get_uri (file);
11349 gtk_selection_data_set_uris (selection_data, uris);
11357 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11359 iface->row_draggable = recent_model_sort_row_draggable;
11360 iface->drag_data_get = recent_model_sort_drag_data_get;
11364 _recent_model_sort_class_init (RecentModelSortClass *klass)
11370 _recent_model_sort_init (RecentModelSort *model)
11372 model->impl = NULL;
11375 static GtkTreeModel *
11376 recent_model_sort_new (GtkFileChooserDefault *impl,
11377 GtkTreeModel *child_model)
11379 RecentModelSort *model;
11381 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11382 "model", child_model,
11384 model->impl = impl;
11386 return GTK_TREE_MODEL (model);
11392 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11395 SearchModelSort *model;
11396 GtkTreeIter iter, child_iter;
11397 gboolean is_folder;
11399 model = SEARCH_MODEL_SORT (drag_source);
11400 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11403 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11404 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11405 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11412 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11414 GtkSelectionData *selection_data)
11416 SearchModelSort *model;
11417 GtkTreeIter iter, child_iter;
11421 model = SEARCH_MODEL_SORT (drag_source);
11422 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11425 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11426 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11427 RECENT_MODEL_COL_FILE, &file,
11429 g_assert (file != NULL);
11431 uris[0] = g_file_get_uri (file);
11434 gtk_selection_data_set_uris (selection_data, uris);
11442 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11444 iface->row_draggable = search_model_sort_row_draggable;
11445 iface->drag_data_get = search_model_sort_drag_data_get;
11449 _search_model_sort_class_init (SearchModelSortClass *klass)
11455 _search_model_sort_init (SearchModelSort *model)
11457 model->impl = NULL;
11460 static GtkTreeModel *
11461 search_model_sort_new (GtkFileChooserDefault *impl,
11462 GtkTreeModel *child_model)
11464 SearchModelSort *model;
11466 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11467 "model", child_model,
11469 model->impl = impl;
11471 return GTK_TREE_MODEL (model);