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 g_object_ref (file);
6987 switch (impl->operation_mode)
6989 case OPERATION_MODE_SEARCH:
6990 search_switch_to_browse_mode (impl);
6992 case OPERATION_MODE_RECENT:
6993 recent_switch_to_browse_mode (impl);
6995 case OPERATION_MODE_BROWSE:
6999 if (impl->local_only && !g_file_is_native (file))
7002 GTK_FILE_CHOOSER_ERROR,
7003 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7004 _("Cannot change to folder because it is not local"));
7006 g_object_unref (file);
7007 profile_end ("end - not local", NULL);
7011 if (impl->update_current_folder_cancellable)
7012 g_cancellable_cancel (impl->update_current_folder_cancellable);
7014 /* Test validity of path here. */
7015 data = g_new0 (struct UpdateCurrentFolderData, 1);
7017 data->file = g_object_ref (file);
7018 data->keep_trail = keep_trail;
7019 data->clear_entry = clear_entry;
7021 impl->reload_state = RELOAD_HAS_FOLDER;
7023 impl->update_current_folder_cancellable =
7024 _gtk_file_system_get_info (impl->file_system, file,
7026 update_current_folder_get_info_cb,
7029 set_busy_cursor (impl, TRUE);
7030 g_object_unref (file);
7032 profile_end ("end", NULL);
7037 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7039 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7041 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7042 impl->operation_mode == OPERATION_MODE_RECENT)
7045 if (impl->reload_state == RELOAD_EMPTY)
7047 char *current_working_dir;
7050 /* We are unmapped, or we had an error while loading the last folder. We'll return
7051 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7052 * explicitly calls set_current_folder() on us.
7054 current_working_dir = g_get_current_dir ();
7055 file = g_file_new_for_path (current_working_dir);
7056 g_free (current_working_dir);
7060 return g_object_ref (impl->current_folder);
7064 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7067 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7069 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7070 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7072 pending_select_files_free (impl);
7073 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7077 select_func (GtkFileSystemModel *model,
7082 GtkFileChooserDefault *impl = user_data;
7083 GtkTreeSelection *selection;
7084 GtkTreeIter sorted_iter;
7086 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7088 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7089 gtk_tree_selection_select_iter (selection, &sorted_iter);
7093 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7097 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7101 parent_file = g_file_get_parent (file);
7104 return _gtk_file_chooser_set_current_folder_file (chooser, file, error);
7106 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7107 impl->operation_mode == OPERATION_MODE_RECENT ||
7108 impl->load_state == LOAD_EMPTY)
7114 g_assert (impl->current_folder != NULL);
7116 same_path = g_file_equal (parent_file, impl->current_folder);
7119 if (same_path && impl->load_state == LOAD_FINISHED)
7124 files.data = (gpointer) file;
7127 result = show_and_select_files (impl, parent_file, &files, error);
7128 g_object_unref (parent_file);
7132 pending_select_files_add (impl, file);
7138 result = _gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7139 g_object_unref (parent_file);
7143 g_object_unref (parent_file);
7148 unselect_func (GtkFileSystemModel *model,
7153 GtkFileChooserDefault *impl = user_data;
7154 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7155 GtkTreePath *sorted_path;
7157 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7159 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7161 gtk_tree_path_free (sorted_path);
7165 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7168 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7170 if (!impl->browse_files_model)
7173 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7174 unselect_func, impl);
7178 maybe_select (GtkTreeModel *model,
7183 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7184 GtkTreeSelection *selection;
7188 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7190 info = get_list_file_info (impl, iter);
7191 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7193 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7194 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7195 gtk_tree_selection_select_iter (selection, iter);
7197 gtk_tree_selection_unselect_iter (selection, iter);
7203 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7205 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7207 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7208 impl->operation_mode == OPERATION_MODE_RECENT)
7210 GtkTreeSelection *selection;
7212 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7213 gtk_tree_selection_select_all (selection);
7217 if (impl->select_multiple)
7218 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7219 maybe_select, impl);
7223 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7225 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7226 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7228 gtk_tree_selection_unselect_all (selection);
7229 pending_select_files_free (impl);
7232 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7234 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7236 * is_empty_ret - whether the file entry is totally empty
7238 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7239 * the path will be "$cwd/foobar")
7242 check_save_entry (GtkFileChooserDefault *impl,
7244 gboolean *is_well_formed_ret,
7245 gboolean *is_empty_ret,
7246 gboolean *is_file_part_empty_ret,
7247 gboolean *is_folder)
7249 GtkFileChooserEntry *chooser_entry;
7250 GFile *current_folder;
7251 const char *file_part;
7255 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7256 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7257 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7258 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7259 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7261 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7263 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7266 *is_well_formed_ret = TRUE;
7267 *is_empty_ret = TRUE;
7268 *is_file_part_empty_ret = TRUE;
7274 *is_empty_ret = FALSE;
7276 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7277 if (!current_folder)
7280 *is_well_formed_ret = FALSE;
7281 *is_file_part_empty_ret = FALSE;
7287 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7289 if (!file_part || file_part[0] == '\0')
7291 *file_ret = g_object_ref (current_folder);
7292 *is_well_formed_ret = TRUE;
7293 *is_file_part_empty_ret = TRUE;
7299 *is_file_part_empty_ret = FALSE;
7302 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7306 error_building_filename_dialog (impl, error);
7308 *is_well_formed_ret = FALSE;
7315 *is_well_formed_ret = TRUE;
7316 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7319 struct get_files_closure {
7320 GtkFileChooserDefault *impl;
7322 GFile *file_from_entry;
7326 get_files_foreach (GtkTreeModel *model,
7331 struct get_files_closure *info;
7333 GtkFileSystemModel *fs_model;
7334 GtkTreeIter sel_iter;
7337 fs_model = info->impl->browse_files_model;
7338 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7340 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7342 return; /* We are on the editable row */
7344 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7345 info->result = g_slist_prepend (info->result, g_object_ref (file));
7349 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7351 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7352 struct get_files_closure info;
7353 GtkWindow *toplevel;
7354 GtkWidget *current_focus;
7355 gboolean file_list_seen;
7357 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7358 return search_get_selected_files (impl);
7360 if (impl->operation_mode == OPERATION_MODE_RECENT)
7361 return recent_get_selected_files (impl);
7365 info.file_from_entry = NULL;
7367 toplevel = get_toplevel (GTK_WIDGET (impl));
7369 current_focus = gtk_window_get_focus (toplevel);
7371 current_focus = NULL;
7373 file_list_seen = FALSE;
7374 if (current_focus == impl->browse_files_tree_view)
7376 GtkTreeSelection *selection;
7380 file_list_seen = TRUE;
7381 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7382 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7384 /* If there is no selection in the file list, we probably have this situation:
7386 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7387 * 2. He then double-clicked on a folder ("bar") in the file list
7389 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7390 * filename entry to see if that is the case.
7392 if (info.result == NULL && impl->location_entry)
7395 else if (impl->location_entry && current_focus == impl->location_entry)
7397 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7401 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7406 if (!is_well_formed)
7409 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7411 g_object_unref (info.file_from_entry);
7415 if (info.file_from_entry)
7416 info.result = g_slist_prepend (info.result, info.file_from_entry);
7417 else if (!file_list_seen)
7422 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7424 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7428 /* The focus is on a dialog's action area button or something else */
7429 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7430 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7438 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7439 * fall back to the current directory */
7440 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7441 info.result == NULL)
7443 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_file (chooser));
7446 return g_slist_reverse (info.result);
7450 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7452 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7454 if (impl->preview_file)
7455 return g_object_ref (impl->preview_file);
7460 static GtkFileSystem *
7461 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7463 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7465 return impl->file_system;
7468 /* Shows or hides the filter widgets */
7470 show_filters (GtkFileChooserDefault *impl,
7474 gtk_widget_show (impl->filter_combo_hbox);
7476 gtk_widget_hide (impl->filter_combo_hbox);
7480 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7481 GtkFileFilter *filter)
7483 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7486 if (g_slist_find (impl->filters, filter))
7488 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7492 g_object_ref_sink (filter);
7493 impl->filters = g_slist_append (impl->filters, filter);
7495 name = gtk_file_filter_get_name (filter);
7497 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7499 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7501 if (!g_slist_find (impl->filters, impl->current_filter))
7502 set_current_filter (impl, filter);
7504 show_filters (impl, TRUE);
7508 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7509 GtkFileFilter *filter)
7511 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7512 GtkTreeModel *model;
7516 filter_index = g_slist_index (impl->filters, filter);
7518 if (filter_index < 0)
7520 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7524 impl->filters = g_slist_remove (impl->filters, filter);
7526 if (filter == impl->current_filter)
7529 set_current_filter (impl, impl->filters->data);
7531 set_current_filter (impl, NULL);
7534 /* Remove row from the combo box */
7535 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7536 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7537 g_assert_not_reached ();
7539 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7541 g_object_unref (filter);
7544 show_filters (impl, FALSE);
7548 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7550 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7552 return g_slist_copy (impl->filters);
7555 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7557 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7560 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7563 struct AddShortcutData
7565 GtkFileChooserDefault *impl;
7570 add_shortcut_get_info_cb (GCancellable *cancellable,
7572 const GError *error,
7576 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7577 struct AddShortcutData *data = user_data;
7579 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7582 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7584 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7587 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7589 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7592 g_object_unref (data->impl);
7593 g_object_unref (data->file);
7596 g_object_unref (cancellable);
7600 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7604 GCancellable *cancellable;
7605 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7606 struct AddShortcutData *data;
7610 /* Avoid adding duplicates */
7611 pos = shortcut_find_position (impl, file);
7612 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7616 uri = g_file_get_uri (file);
7617 /* translators, "Shortcut" means "Bookmark" here */
7619 GTK_FILE_CHOOSER_ERROR,
7620 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7621 _("Shortcut %s already exists"),
7628 for (l = impl->loading_shortcuts; l; l = l->next)
7630 GCancellable *c = l->data;
7633 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7634 if (f && g_file_equal (file, f))
7638 uri = g_file_get_uri (file);
7640 GTK_FILE_CHOOSER_ERROR,
7641 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7642 _("Shortcut %s already exists"),
7650 data = g_new0 (struct AddShortcutData, 1);
7651 data->impl = g_object_ref (impl);
7652 data->file = g_object_ref (file);
7654 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7656 add_shortcut_get_info_cb, data);
7661 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7662 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7668 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7672 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7679 for (l = impl->loading_shortcuts; l; l = l->next)
7681 GCancellable *c = l->data;
7684 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7685 if (f && g_file_equal (file, f))
7687 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7688 g_cancellable_cancel (c);
7693 if (impl->num_shortcuts == 0)
7696 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7697 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7698 g_assert_not_reached ();
7700 for (i = 0; i < impl->num_shortcuts; i++)
7703 ShortcutType shortcut_type;
7706 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7707 SHORTCUTS_COL_DATA, &col_data,
7708 SHORTCUTS_COL_TYPE, &shortcut_type,
7710 g_assert (col_data != NULL);
7711 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7713 shortcut = col_data;
7714 if (g_file_equal (shortcut, file))
7716 shortcuts_remove_rows (impl, pos + i, 1);
7717 impl->num_shortcuts--;
7721 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7722 g_assert_not_reached ();
7727 uri = g_file_get_uri (file);
7728 /* translators, "Shortcut" means "Bookmark" here */
7730 GTK_FILE_CHOOSER_ERROR,
7731 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7732 _("Shortcut %s does not exist"),
7740 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7742 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7748 if (impl->num_shortcuts == 0)
7751 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7752 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7753 g_assert_not_reached ();
7757 for (i = 0; i < impl->num_shortcuts; i++)
7760 ShortcutType shortcut_type;
7763 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7764 SHORTCUTS_COL_DATA, &col_data,
7765 SHORTCUTS_COL_TYPE, &shortcut_type,
7767 g_assert (col_data != NULL);
7768 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7770 shortcut = col_data;
7771 list = g_slist_prepend (list, g_object_ref (shortcut));
7773 if (i != impl->num_shortcuts - 1)
7775 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7776 g_assert_not_reached ();
7780 return g_slist_reverse (list);
7783 /* Guesses a size based upon font sizes */
7785 find_good_size_from_style (GtkWidget *widget,
7789 GtkFileChooserDefault *impl;
7794 g_assert (widget->style != NULL);
7795 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7797 if (impl->default_width == 0 &&
7798 impl->default_height == 0)
7800 screen = gtk_widget_get_screen (widget);
7803 resolution = gdk_screen_get_resolution (screen);
7804 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7808 resolution = 96.0; /* wheeee */
7810 font_size = pango_font_description_get_size (widget->style->font_desc);
7811 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7813 impl->default_width = font_size * NUM_CHARS;
7814 impl->default_height = font_size * NUM_LINES;
7817 *width = impl->default_width;
7818 *height = impl->default_height;
7822 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7823 gint *default_width,
7824 gint *default_height)
7826 GtkFileChooserDefault *impl;
7829 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7830 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7832 if (impl->preview_widget_active &&
7833 impl->preview_widget &&
7834 GTK_WIDGET_VISIBLE (impl->preview_widget))
7836 gtk_widget_size_request (impl->preview_box, &req);
7837 *default_width += PREVIEW_HBOX_SPACING + req.width;
7840 if (impl->extra_widget &&
7841 GTK_WIDGET_VISIBLE (impl->extra_widget))
7843 gtk_widget_size_request (impl->extra_align, &req);
7844 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7848 struct switch_folder_closure {
7849 GtkFileChooserDefault *impl;
7854 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7856 switch_folder_foreach_cb (GtkTreeModel *model,
7861 struct switch_folder_closure *closure;
7862 GtkTreeIter child_iter;
7866 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7868 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7869 closure->num_selected++;
7872 /* Changes to the selected folder in the list view */
7874 switch_to_selected_folder (GtkFileChooserDefault *impl)
7876 GtkTreeSelection *selection;
7877 struct switch_folder_closure closure;
7879 /* We do this with foreach() rather than get_selected() as we may be in
7880 * multiple selection mode
7883 closure.impl = impl;
7884 closure.file = NULL;
7885 closure.num_selected = 0;
7887 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7888 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7890 g_assert (closure.file && closure.num_selected == 1);
7892 change_folder_and_display_error (impl, closure.file, FALSE);
7895 /* Gets the GFileInfo for the selected row in the file list; assumes single
7899 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7900 gboolean *had_selection)
7902 GtkTreeSelection *selection;
7903 GtkTreeIter iter, child_iter;
7906 g_assert (!impl->select_multiple);
7907 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7908 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7910 *had_selection = FALSE;
7914 *had_selection = TRUE;
7916 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7920 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7924 /* Gets the display name of the selected file in the file list; assumes single
7925 * selection mode and that something is selected.
7927 static const gchar *
7928 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7931 gboolean had_selection;
7933 info = get_selected_file_info_from_file_list (impl, &had_selection);
7934 g_assert (had_selection);
7935 g_assert (info != NULL);
7937 return g_file_info_get_display_name (info);
7941 add_custom_button_to_dialog (GtkDialog *dialog,
7942 const gchar *mnemonic_label,
7943 const gchar *stock_id,
7948 button = gtk_button_new_with_mnemonic (mnemonic_label);
7949 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7950 gtk_button_set_image (GTK_BUTTON (button),
7951 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7952 gtk_widget_show (button);
7954 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7957 /* Presents an overwrite confirmation dialog; returns whether we should accept
7961 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7962 const gchar *file_part,
7963 const gchar *folder_display_name)
7965 GtkWindow *toplevel;
7969 toplevel = get_toplevel (GTK_WIDGET (impl));
7971 dialog = gtk_message_dialog_new (toplevel,
7972 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7973 GTK_MESSAGE_QUESTION,
7975 _("A file named \"%s\" already exists. Do you want to replace it?"),
7977 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7978 _("The file already exists in \"%s\". Replacing it will "
7979 "overwrite its contents."),
7980 folder_display_name);
7982 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7983 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7984 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7985 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7986 GTK_RESPONSE_ACCEPT,
7987 GTK_RESPONSE_CANCEL,
7989 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7991 if (toplevel->group)
7992 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7994 response = gtk_dialog_run (GTK_DIALOG (dialog));
7996 gtk_widget_destroy (dialog);
7998 return (response == GTK_RESPONSE_ACCEPT);
8001 struct GetDisplayNameData
8003 GtkFileChooserDefault *impl;
8008 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8010 const GError *error,
8013 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8014 gboolean should_respond = FALSE;
8015 struct GetDisplayNameData *data = user_data;
8017 if (cancellable != data->impl->should_respond_get_info_cancellable)
8020 data->impl->should_respond_get_info_cancellable = NULL;
8026 /* Huh? Did the folder disappear? Let the caller deal with it */
8027 should_respond = TRUE;
8029 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8031 set_busy_cursor (data->impl, FALSE);
8033 g_signal_emit_by_name (data->impl, "response-requested");
8036 g_object_unref (data->impl);
8037 g_free (data->file_part);
8040 g_object_unref (cancellable);
8043 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8044 * should respond. Can get the file part from the file list or the save entry.
8047 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8048 const gchar *file_part,
8051 GtkFileChooserConfirmation conf;
8053 if (!impl->do_overwrite_confirmation)
8056 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8058 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8062 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8064 struct GetDisplayNameData *data;
8066 g_assert (file_part != NULL);
8068 data = g_new0 (struct GetDisplayNameData, 1);
8069 data->impl = g_object_ref (impl);
8070 data->file_part = g_strdup (file_part);
8072 if (impl->should_respond_get_info_cancellable)
8073 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8075 impl->should_respond_get_info_cancellable =
8076 _gtk_file_system_get_info (impl->file_system, parent_file,
8077 "standard::display-name",
8078 confirmation_confirm_get_info_cb,
8080 set_busy_cursor (data->impl, TRUE);
8084 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8087 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8091 g_assert_not_reached ();
8096 struct FileExistsData
8098 GtkFileChooserDefault *impl;
8099 gboolean file_exists_and_is_not_folder;
8105 save_entry_get_info_cb (GCancellable *cancellable,
8107 const GError *error,
8110 gboolean parent_is_folder;
8111 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8112 struct FileExistsData *data = user_data;
8114 if (cancellable != data->impl->should_respond_get_info_cancellable)
8117 data->impl->should_respond_get_info_cancellable = NULL;
8119 set_busy_cursor (data->impl, FALSE);
8125 parent_is_folder = FALSE;
8127 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8129 if (parent_is_folder)
8131 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8133 if (data->file_exists_and_is_not_folder)
8136 const char *file_part;
8138 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8139 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8142 g_signal_emit_by_name (data->impl, "response-requested");
8145 g_signal_emit_by_name (data->impl, "response-requested");
8147 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8149 GError *error = NULL;
8151 set_busy_cursor (data->impl, TRUE);
8152 g_file_make_directory (data->file, NULL, &error);
8153 set_busy_cursor (data->impl, FALSE);
8156 g_signal_emit_by_name (data->impl, "response-requested");
8158 error_creating_folder_dialog (data->impl, data->file, error);
8163 /* This will display an error, which is what we want */
8164 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8168 g_object_unref (data->impl);
8169 g_object_unref (data->file);
8170 g_object_unref (data->parent_file);
8173 g_object_unref (cancellable);
8177 file_exists_get_info_cb (GCancellable *cancellable,
8179 const GError *error,
8182 gboolean data_ownership_taken = FALSE;
8183 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8184 gboolean file_exists_and_is_not_folder;
8185 struct FileExistsData *data = user_data;
8187 if (cancellable != data->impl->file_exists_get_info_cancellable)
8190 data->impl->file_exists_get_info_cancellable = NULL;
8192 set_busy_cursor (data->impl, FALSE);
8197 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8199 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8200 /* user typed a filename; we are done */
8201 g_signal_emit_by_name (data->impl, "response-requested");
8202 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8203 && file_exists_and_is_not_folder)
8205 /* Oops, the user typed the name of an existing path which is not
8208 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8209 g_error_copy (error));
8213 /* check that everything up to the last component exists */
8215 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8216 data_ownership_taken = TRUE;
8218 if (data->impl->should_respond_get_info_cancellable)
8219 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8221 data->impl->should_respond_get_info_cancellable =
8222 _gtk_file_system_get_info (data->impl->file_system,
8225 save_entry_get_info_cb,
8227 set_busy_cursor (data->impl, TRUE);
8231 if (!data_ownership_taken)
8233 g_object_unref (data->impl);
8234 g_object_unref (data->file);
8235 g_object_unref (data->parent_file);
8239 g_object_unref (cancellable);
8243 paste_text_received (GtkClipboard *clipboard,
8245 GtkFileChooserDefault *impl)
8252 file = g_file_new_for_uri (text);
8254 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8255 location_popup_handler (impl, text);
8257 g_object_unref (file);
8260 /* Handler for the "location-popup-on-paste" keybinding signal */
8262 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8264 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8265 GDK_SELECTION_CLIPBOARD);
8266 gtk_clipboard_request_text (clipboard,
8267 (GtkClipboardTextReceivedFunc) paste_text_received,
8272 /* Implementation for GtkFileChooserEmbed::should_respond() */
8274 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8276 GtkFileChooserDefault *impl;
8277 GtkWidget *toplevel;
8278 GtkWidget *current_focus;
8280 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8282 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8283 g_assert (GTK_IS_WINDOW (toplevel));
8285 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8287 if (current_focus == impl->browse_files_tree_view)
8289 /* The following array encodes what we do based on the impl->action and the
8290 * number of files selected.
8293 NOOP, /* Do nothing (don't respond) */
8294 RESPOND, /* Respond immediately */
8295 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8296 ALL_FILES, /* Respond only if everything selected is a file */
8297 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8298 SAVE_ENTRY, /* Go to the code for handling the save entry */
8299 NOT_REACHED /* Sanity check */
8301 static const ActionToTake what_to_do[4][3] = {
8302 /* 0 selected 1 selected many selected */
8303 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8304 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8305 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8306 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8310 gboolean all_files, all_folders;
8312 ActionToTake action;
8316 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8318 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8319 return search_should_respond (impl);
8321 if (impl->operation_mode == OPERATION_MODE_RECENT)
8322 return recent_should_respond (impl);
8324 selection_check (impl, &num_selected, &all_files, &all_folders);
8326 if (num_selected > 2)
8331 action = what_to_do [impl->action] [k];
8341 case RESPOND_OR_SWITCH:
8342 g_assert (num_selected == 1);
8346 switch_to_selected_folder (impl);
8349 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8350 return should_respond_after_confirm_overwrite (impl,
8351 get_display_name_from_file_list (impl),
8352 impl->current_folder);
8366 g_assert_not_reached ();
8369 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8372 gboolean is_well_formed, is_empty, is_file_part_empty;
8375 GtkFileChooserEntry *entry;
8380 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8381 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8382 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8383 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8384 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8386 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8387 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8389 if (is_empty || !is_well_formed)
8392 g_assert (file != NULL);
8397 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8398 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8400 change_folder_and_display_error (impl, file, TRUE);
8403 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8404 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8406 /* The folder already exists, so we do not need to create it.
8407 * Just respond to terminate the dialog.
8413 g_assert_not_reached ();
8419 struct FileExistsData *data;
8421 /* We need to check whether file exists and is not a folder */
8423 data = g_new0 (struct FileExistsData, 1);
8424 data->impl = g_object_ref (impl);
8425 data->file = g_object_ref (file);
8426 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8428 if (impl->file_exists_get_info_cancellable)
8429 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8431 impl->file_exists_get_info_cancellable =
8432 _gtk_file_system_get_info (impl->file_system, file,
8434 file_exists_get_info_cb,
8437 set_busy_cursor (impl, TRUE);
8441 g_error_free (error);
8444 g_object_unref (file);
8447 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8449 /* The focus is on a dialog's action area button, *and* the widget that
8450 * was focused immediately before it is the file list.
8454 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8456 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8459 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8461 /* The focus is on a dialog's action area button, *and* the widget that
8462 * was focused immediately before it is the location entry.
8467 /* The focus is on a dialog's action area button or something else */
8468 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8469 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8474 g_assert_not_reached ();
8478 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8480 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8482 GtkFileChooserDefault *impl;
8485 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8487 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8488 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8490 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8491 widget = impl->browse_files_tree_view;
8493 widget = impl->location_entry;
8495 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8496 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8497 widget = impl->location_entry;
8500 g_assert_not_reached ();
8504 g_assert (widget != NULL);
8505 gtk_widget_grab_focus (widget);
8508 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8510 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8520 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8521 *list = g_slist_prepend (*list, file);
8524 /* Constructs a list of the selected paths in search mode */
8526 search_get_selected_files (GtkFileChooserDefault *impl)
8529 GtkTreeSelection *selection;
8533 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8534 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8535 result = g_slist_reverse (result);
8540 /* Called from ::should_respond(). We return whether there are selected files
8541 * in the search list.
8544 search_should_respond (GtkFileChooserDefault *impl)
8546 GtkTreeSelection *selection;
8548 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8550 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8551 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8554 struct SearchHitInsertRequest
8556 GtkFileChooserDefault *impl;
8558 GtkTreeRowReference *row_ref;
8562 search_hit_get_info_cb (GCancellable *cancellable,
8564 const GError *error,
8567 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8568 GdkPixbuf *pixbuf = NULL;
8571 GCancellable *model_cancellable;
8572 gboolean is_folder = FALSE;
8575 struct SearchHitInsertRequest *request = data;
8577 if (!request->impl->search_model)
8580 path = gtk_tree_row_reference_get_path (request->row_ref);
8584 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8586 gtk_tree_path_free (path);
8588 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8589 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8591 if (cancellable != model_cancellable)
8594 /* set the cancellable to NULL in the model */
8595 gtk_list_store_set (request->impl->search_model, &iter,
8596 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8604 gtk_list_store_remove (request->impl->search_model, &iter);
8608 display_name = g_strdup (g_file_info_get_display_name (info));
8609 mime_type = g_strdup (g_file_info_get_content_type (info));
8610 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8611 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8612 request->impl->icon_size);
8614 gtk_list_store_set (request->impl->search_model, &iter,
8615 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8616 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8617 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8618 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8622 g_object_unref (pixbuf);
8625 g_object_unref (request->impl);
8626 g_object_unref (request->file);
8627 gtk_tree_row_reference_free (request->row_ref);
8630 g_object_unref (cancellable);
8633 /* Adds one hit from the search engine to the search_model */
8635 search_add_hit (GtkFileChooserDefault *impl,
8641 char *collation_key;
8642 struct stat statbuf;
8643 struct stat *statbuf_copy;
8646 GCancellable *cancellable;
8647 struct SearchHitInsertRequest *request;
8649 file = g_file_new_for_uri (uri);
8651 if (!g_file_is_native (file))
8653 g_object_unref (file);
8657 filename = g_file_get_path (file);
8659 if (stat (filename, &statbuf) != 0)
8661 g_object_unref (file);
8666 statbuf_copy = g_new (struct stat, 1);
8667 *statbuf_copy = statbuf;
8669 tmp = g_file_get_parse_name (file);
8670 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8673 request = g_new0 (struct SearchHitInsertRequest, 1);
8674 request->impl = g_object_ref (impl);
8675 request->file = g_object_ref (file);
8677 gtk_list_store_append (impl->search_model, &iter);
8678 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8680 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8681 gtk_tree_path_free (p);
8683 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8684 "standard::type,standard::icon,"
8685 "standard::content-type,standard::display-name",
8686 search_hit_get_info_cb,
8689 gtk_list_store_set (impl->search_model, &iter,
8690 SEARCH_MODEL_COL_FILE, file,
8691 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8692 SEARCH_MODEL_COL_STAT, statbuf_copy,
8693 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8696 g_object_unref (file);
8700 /* Callback used from GtkSearchEngine when we get new hits */
8702 search_engine_hits_added_cb (GtkSearchEngine *engine,
8706 GtkFileChooserDefault *impl;
8709 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8711 for (l = hits; l; l = l->next)
8712 search_add_hit (impl, (gchar*)l->data);
8715 /* Callback used from GtkSearchEngine when the query is done running */
8717 search_engine_finished_cb (GtkSearchEngine *engine,
8720 GtkFileChooserDefault *impl;
8722 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8725 /* EB: setting the model here will avoid loads of row events,
8726 * but it'll make the search look like blocked.
8728 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8729 GTK_TREE_MODEL (impl->search_model_filter));
8732 /* FMQ: if search was empty, say that we got no hits */
8733 set_busy_cursor (impl, FALSE);
8736 /* Displays a generic error when we cannot create a GtkSearchEngine.
8737 * It would be better if _gtk_search_engine_new() gave us a GError
8738 * with a better message, but it doesn't do that right now.
8741 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8743 error_message (impl,
8744 _("Could not start the search process"),
8745 _("The program was not able to create a connection to the indexer "
8746 "daemon. Please make sure it is running."));
8750 search_engine_error_cb (GtkSearchEngine *engine,
8751 const gchar *message,
8754 GtkFileChooserDefault *impl;
8756 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8758 search_stop_searching (impl, TRUE);
8759 error_message (impl, _("Could not send the search request"), message);
8761 set_busy_cursor (impl, FALSE);
8764 /* Frees the data in the search_model */
8766 search_clear_model (GtkFileChooserDefault *impl,
8767 gboolean remove_from_treeview)
8769 GtkTreeModel *model;
8772 if (!impl->search_model)
8775 model = GTK_TREE_MODEL (impl->search_model);
8777 if (gtk_tree_model_get_iter_first (model, &iter))
8780 GCancellable *cancellable;
8782 gtk_tree_model_get (model, &iter,
8783 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8787 g_cancellable_cancel (cancellable);
8789 while (gtk_tree_model_iter_next (model, &iter));
8791 g_object_unref (impl->search_model);
8792 impl->search_model = NULL;
8794 g_object_unref (impl->search_model_filter);
8795 impl->search_model_filter = NULL;
8797 g_object_unref (impl->search_model_sort);
8798 impl->search_model_sort = NULL;
8800 if (remove_from_treeview)
8801 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8804 /* Stops any ongoing searches; does not touch the search_model */
8806 search_stop_searching (GtkFileChooserDefault *impl,
8807 gboolean remove_query)
8809 if (remove_query && impl->search_query)
8811 g_object_unref (impl->search_query);
8812 impl->search_query = NULL;
8815 if (impl->search_engine)
8817 _gtk_search_engine_stop (impl->search_engine);
8819 g_object_unref (impl->search_engine);
8820 impl->search_engine = NULL;
8824 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8826 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8828 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8830 search_stop_searching (impl, FALSE);
8831 search_clear_model (impl, TRUE);
8833 gtk_widget_destroy (impl->search_hbox);
8834 impl->search_hbox = NULL;
8835 impl->search_entry = NULL;
8837 gtk_widget_show (impl->browse_path_bar);
8838 gtk_widget_show (impl->browse_new_folder_button);
8840 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8841 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8843 gtk_widget_show (impl->location_button);
8845 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8846 gtk_widget_show (impl->location_entry_box);
8849 impl->operation_mode = OPERATION_MODE_BROWSE;
8851 file_list_set_sort_column_ids (impl);
8854 /* Sort callback from the path column */
8856 search_column_path_sort_func (GtkTreeModel *model,
8861 GtkFileChooserDefault *impl = user_data;
8862 GtkTreeIter child_a, child_b;
8863 const char *collation_key_a, *collation_key_b;
8864 gboolean is_folder_a, is_folder_b;
8866 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8867 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8869 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8870 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8871 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8873 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8874 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8875 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8878 if (!collation_key_a)
8881 if (!collation_key_b)
8884 /* always show folders first */
8885 if (is_folder_a != is_folder_b)
8886 return is_folder_a ? 1 : -1;
8888 return strcmp (collation_key_a, collation_key_b);
8891 /* Sort callback from the modification time column */
8893 search_column_mtime_sort_func (GtkTreeModel *model,
8898 GtkFileChooserDefault *impl = user_data;
8899 GtkTreeIter child_a, child_b;
8900 const struct stat *statbuf_a, *statbuf_b;
8901 gboolean is_folder_a, is_folder_b;
8903 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8904 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8906 /* Note that although we store a whole struct stat in the model, we only
8907 * compare the mtime here. If we add another column relative to a struct stat
8908 * (e.g. a file size column), we'll want another sort callback similar to this
8911 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8912 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8913 SEARCH_MODEL_COL_STAT, &statbuf_a,
8915 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8916 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8917 SEARCH_MODEL_COL_STAT, &statbuf_b,
8926 if (is_folder_a != is_folder_b)
8927 return is_folder_a ? 1 : -1;
8929 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8931 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8938 search_get_is_filtered (GtkFileChooserDefault *impl,
8940 const gchar *display_name,
8941 const gchar *mime_type)
8943 GtkFileFilterInfo filter_info;
8944 GtkFileFilterFlags needed;
8947 if (!impl->current_filter)
8950 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8951 needed = gtk_file_filter_get_needed (impl->current_filter);
8953 filter_info.display_name = display_name;
8954 filter_info.mime_type = mime_type;
8956 if (needed & GTK_FILE_FILTER_FILENAME)
8958 filter_info.filename = g_file_get_path (file);
8959 if (filter_info.filename)
8960 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8963 filter_info.filename = NULL;
8965 if (needed & GTK_FILE_FILTER_URI)
8967 filter_info.uri = g_file_get_uri (file);
8968 if (filter_info.uri)
8969 filter_info.contains |= GTK_FILE_FILTER_URI;
8972 filter_info.uri = NULL;
8974 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8976 if (filter_info.filename)
8977 g_free ((gchar *) filter_info.filename);
8978 if (filter_info.uri)
8979 g_free ((gchar *) filter_info.uri);
8985 /* Visibility function for the recent filter model */
8987 search_model_visible_func (GtkTreeModel *model,
8991 GtkFileChooserDefault *impl = user_data;
8993 gchar *display_name, *mime_type;
8996 if (!impl->current_filter)
8999 gtk_tree_model_get (model, iter,
9000 SEARCH_MODEL_COL_FILE, &file,
9001 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9002 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9003 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9012 return !search_get_is_filtered (impl, file, display_name, mime_type);
9015 /* Creates the search_model and puts it in the tree view */
9017 search_setup_model (GtkFileChooserDefault *impl)
9019 g_assert (impl->search_model == NULL);
9020 g_assert (impl->search_model_filter == NULL);
9021 g_assert (impl->search_model_sort == NULL);
9023 /* We store these columns in the search model:
9025 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9026 * as a pointer not as a G_TYPE_FILE
9027 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9028 * as a pointer not as a G_TYPE_STRING
9029 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9030 * as a pointer not as a G_TYPE_STRING
9031 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9032 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9033 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9034 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9035 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9037 * Keep this in sync with the enumeration defined near the beginning
9040 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9050 impl->search_model_filter =
9051 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9052 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9053 search_model_visible_func,
9056 impl->search_model_sort =
9057 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9058 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9059 SEARCH_MODEL_COL_FILE,
9060 search_column_path_sort_func,
9062 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9063 SEARCH_MODEL_COL_STAT,
9064 search_column_mtime_sort_func,
9066 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9067 SEARCH_MODEL_COL_STAT,
9068 GTK_SORT_DESCENDING);
9070 /* EB: setting the model here will make the hits list update feel
9071 * more "alive" than setting the model at the end of the search
9074 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9075 GTK_TREE_MODEL (impl->search_model_sort));
9079 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9080 GtkTreeIter *child_iter,
9085 if (!impl->search_model)
9088 if (!impl->search_model_filter || !impl->search_model_sort)
9091 /* pass 1: get the iterator in the filter model */
9092 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9095 /* pass 2: get the iterator in the real model */
9096 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9097 child_iter, &middle);
9100 /* Creates a new query with the specified text and launches it */
9102 search_start_query (GtkFileChooserDefault *impl,
9103 const gchar *query_text)
9105 search_stop_searching (impl, FALSE);
9106 search_clear_model (impl, TRUE);
9107 search_setup_model (impl);
9108 set_busy_cursor (impl, TRUE);
9110 if (impl->search_engine == NULL)
9111 impl->search_engine = _gtk_search_engine_new ();
9113 if (!impl->search_engine)
9115 set_busy_cursor (impl, FALSE);
9116 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9120 if (!impl->search_query)
9122 impl->search_query = _gtk_query_new ();
9123 _gtk_query_set_text (impl->search_query, query_text);
9126 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9128 g_signal_connect (impl->search_engine, "hits-added",
9129 G_CALLBACK (search_engine_hits_added_cb), impl);
9130 g_signal_connect (impl->search_engine, "finished",
9131 G_CALLBACK (search_engine_finished_cb), impl);
9132 g_signal_connect (impl->search_engine, "error",
9133 G_CALLBACK (search_engine_error_cb), impl);
9135 _gtk_search_engine_start (impl->search_engine);
9138 /* Callback used when the user presses Enter while typing on the search
9139 * entry; starts the query
9142 search_entry_activate_cb (GtkEntry *entry,
9145 GtkFileChooserDefault *impl;
9148 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9150 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9151 if (strlen (text) == 0)
9154 /* reset any existing query object */
9155 if (impl->search_query)
9157 g_object_unref (impl->search_query);
9158 impl->search_query = NULL;
9161 search_start_query (impl, text);
9164 /* Hides the path bar and creates the search entry */
9166 search_setup_widgets (GtkFileChooserDefault *impl)
9170 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9174 label = gtk_label_new_with_mnemonic (_("_Search:"));
9175 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9179 impl->search_entry = gtk_entry_new ();
9180 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9181 g_signal_connect (impl->search_entry, "activate",
9182 G_CALLBACK (search_entry_activate_cb),
9184 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9186 /* if there already is a query, restart it */
9187 if (impl->search_query)
9189 gchar *query = _gtk_query_get_text (impl->search_query);
9193 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9194 search_start_query (impl, query);
9200 g_object_unref (impl->search_query);
9201 impl->search_query = NULL;
9205 gtk_widget_hide (impl->browse_path_bar);
9206 gtk_widget_hide (impl->browse_new_folder_button);
9208 /* Box for search widgets */
9209 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9210 gtk_widget_show_all (impl->search_hbox);
9212 /* Hide the location widgets temporarily */
9214 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9215 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9217 gtk_widget_hide (impl->location_button);
9218 gtk_widget_hide (impl->location_entry_box);
9221 gtk_widget_grab_focus (impl->search_entry);
9223 /* FMQ: hide the filter combo? */
9226 /* Main entry point to the searching functions; this gets called when the user
9227 * activates the Search shortcut.
9230 search_activate (GtkFileChooserDefault *impl)
9232 OperationMode previous_mode;
9234 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9236 gtk_widget_grab_focus (impl->search_entry);
9240 previous_mode = impl->operation_mode;
9241 impl->operation_mode = OPERATION_MODE_SEARCH;
9243 switch (previous_mode)
9245 case OPERATION_MODE_RECENT:
9246 recent_stop_loading (impl);
9247 recent_clear_model (impl, TRUE);
9250 case OPERATION_MODE_BROWSE:
9251 stop_loading_and_clear_list_model (impl);
9254 case OPERATION_MODE_SEARCH:
9255 g_assert_not_reached ();
9259 g_assert (impl->search_hbox == NULL);
9260 g_assert (impl->search_entry == NULL);
9261 g_assert (impl->search_model == NULL);
9262 g_assert (impl->search_model_filter == NULL);
9264 search_setup_widgets (impl);
9265 file_list_set_sort_column_ids (impl);
9269 * Recent files support
9272 /* Frees the data in the recent_model */
9274 recent_clear_model (GtkFileChooserDefault *impl,
9275 gboolean remove_from_treeview)
9277 GtkTreeModel *model;
9280 if (!impl->recent_model)
9283 model = GTK_TREE_MODEL (impl->recent_model);
9285 if (remove_from_treeview)
9286 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9288 if (gtk_tree_model_get_iter_first (model, &iter))
9293 GCancellable *cancellable;
9294 GtkRecentInfo *recent_info;
9295 gchar *display_name;
9297 gtk_tree_model_get (model, &iter,
9298 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9299 RECENT_MODEL_COL_FILE, &file,
9300 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9301 RECENT_MODEL_COL_INFO, &recent_info,
9305 g_cancellable_cancel (cancellable);
9307 g_object_unref (file);
9308 gtk_recent_info_unref (recent_info);
9309 g_free (display_name);
9311 while (gtk_tree_model_iter_next (model, &iter));
9314 g_object_unref (impl->recent_model);
9315 impl->recent_model = NULL;
9317 g_object_unref (impl->recent_model_filter);
9318 impl->recent_model_filter = NULL;
9320 g_object_unref (impl->recent_model_sort);
9321 impl->recent_model_sort = NULL;
9324 /* Stops any ongoing loading of the recent files list; does
9325 * not touch the recent_model
9328 recent_stop_loading (GtkFileChooserDefault *impl)
9330 if (impl->load_recent_id)
9332 g_source_remove (impl->load_recent_id);
9333 impl->load_recent_id = 0;
9337 /* Stops any pending load, clears the file list, and switches
9338 * back to OPERATION_MODE_BROWSE
9341 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9343 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9345 recent_stop_loading (impl);
9346 recent_clear_model (impl, TRUE);
9348 gtk_widget_show (impl->browse_path_bar);
9349 gtk_widget_show (impl->browse_new_folder_button);
9351 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9352 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9354 gtk_widget_show (impl->location_button);
9356 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9357 gtk_widget_show (impl->location_entry_box);
9360 impl->operation_mode = OPERATION_MODE_BROWSE;
9362 file_list_set_sort_column_ids (impl);
9365 /* Sort callback from the modification time column */
9367 recent_column_mtime_sort_func (GtkTreeModel *model,
9372 GtkFileChooserDefault *impl = user_data;
9373 GtkTreeIter child_a, child_b;
9374 GtkRecentInfo *info_a, *info_b;
9375 gboolean is_folder_a, is_folder_b;
9377 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9378 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9380 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9381 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9382 RECENT_MODEL_COL_INFO, &info_a,
9384 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9385 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9386 RECENT_MODEL_COL_INFO, &info_b,
9395 /* folders always go first */
9396 if (is_folder_a != is_folder_b)
9397 return is_folder_a ? 1 : -1;
9399 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9401 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9408 recent_column_path_sort_func (GtkTreeModel *model,
9413 GtkFileChooserDefault *impl = user_data;
9414 GtkTreeIter child_a, child_b;
9415 gboolean is_folder_a, is_folder_b;
9416 gchar *name_a, *name_b;
9418 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9419 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9421 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9422 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9423 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9425 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9426 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9427 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9436 if (is_folder_a != is_folder_b)
9437 return is_folder_a ? 1 : -1;
9439 return strcmp (name_a, name_b);
9443 recent_get_is_filtered (GtkFileChooserDefault *impl,
9445 GtkRecentInfo *recent_info)
9447 GtkFileFilterInfo filter_info;
9448 GtkFileFilterFlags needed;
9451 if (!impl->current_filter)
9454 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9455 needed = gtk_file_filter_get_needed (impl->current_filter);
9457 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9458 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9460 if (needed & GTK_FILE_FILTER_FILENAME)
9462 filter_info.filename = g_file_get_path (file);
9463 if (filter_info.filename)
9464 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9467 filter_info.filename = NULL;
9469 if (needed & GTK_FILE_FILTER_URI)
9471 filter_info.uri = g_file_get_uri (file);
9472 if (filter_info.uri)
9473 filter_info.contains |= GTK_FILE_FILTER_URI;
9476 filter_info.uri = NULL;
9478 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9480 if (filter_info.filename)
9481 g_free ((gchar *) filter_info.filename);
9482 if (filter_info.uri)
9483 g_free ((gchar *) filter_info.uri);
9488 /* Visibility function for the recent filter model */
9490 recent_model_visible_func (GtkTreeModel *model,
9494 GtkFileChooserDefault *impl = user_data;
9496 GtkRecentInfo *recent_info;
9499 if (!impl->current_filter)
9502 gtk_tree_model_get (model, iter,
9503 RECENT_MODEL_COL_INFO, &recent_info,
9504 RECENT_MODEL_COL_FILE, &file,
9505 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9514 return !recent_get_is_filtered (impl, file, recent_info);
9518 recent_setup_model (GtkFileChooserDefault *impl)
9520 g_assert (impl->recent_model == NULL);
9521 g_assert (impl->recent_model_filter == NULL);
9522 g_assert (impl->recent_model_sort == NULL);
9524 /* We store these columns in the search model:
9526 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9527 * stored as a pointer and not as a G_TYPE_FILE;
9528 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9529 * stored as a pointer and not as a G_TYPE_STRING;
9530 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9531 * as a GTK_TYPE_RECENT_INFO;
9532 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9533 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9534 * and not as a G_TYPE_CANCELLABLE;
9536 * Keep this in sync with the enumeration defined near the beginning of
9539 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9546 impl->recent_model_filter =
9547 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9548 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9549 recent_model_visible_func,
9553 /* this is the model that will actually be added to
9554 * the browse_files_tree_view widget; remember: we are
9555 * stuffing the real model into a filter model and then
9556 * into a sort model; this means we'll have to translate
9557 * the child iterator *twice* to get from a path or an
9558 * iterator coming from the tree view widget to the
9559 * real data inside the model.
9561 impl->recent_model_sort =
9562 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9563 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9564 RECENT_MODEL_COL_FILE,
9565 recent_column_path_sort_func,
9567 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9568 RECENT_MODEL_COL_INFO,
9569 recent_column_mtime_sort_func,
9571 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9572 RECENT_MODEL_COL_INFO,
9573 GTK_SORT_DESCENDING);
9578 GtkFileChooserDefault *impl;
9581 gint n_loaded_items;
9582 guint needs_sorting : 1;
9586 recent_idle_cleanup (gpointer data)
9588 RecentLoadData *load_data = data;
9589 GtkFileChooserDefault *impl = load_data->impl;
9591 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9592 GTK_TREE_MODEL (impl->recent_model_sort));
9594 set_busy_cursor (impl, FALSE);
9596 impl->load_recent_id = 0;
9598 if (load_data->items)
9600 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9601 g_list_free (load_data->items);
9607 struct RecentItemInsertRequest
9609 GtkFileChooserDefault *impl;
9611 GtkTreeRowReference *row_ref;
9615 recent_item_get_info_cb (GCancellable *cancellable,
9617 const GError *error,
9620 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9623 GCancellable *model_cancellable;
9624 gboolean is_folder = FALSE;
9625 struct RecentItemInsertRequest *request = data;
9627 if (!request->impl->recent_model)
9630 path = gtk_tree_row_reference_get_path (request->row_ref);
9634 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9636 gtk_tree_path_free (path);
9638 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9639 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9641 if (cancellable != model_cancellable)
9644 gtk_list_store_set (request->impl->recent_model, &iter,
9645 RECENT_MODEL_COL_CANCELLABLE, NULL,
9653 gtk_list_store_remove (request->impl->recent_model, &iter);
9657 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9659 gtk_list_store_set (request->impl->recent_model, &iter,
9660 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9664 g_object_unref (request->impl);
9665 g_object_unref (request->file);
9666 gtk_tree_row_reference_free (request->row_ref);
9669 g_object_unref (cancellable);
9673 recent_sort_mru (gconstpointer a,
9676 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9677 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9679 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9683 get_recent_files_limit (GtkWidget *widget)
9685 GtkSettings *settings;
9688 if (gtk_widget_has_screen (widget))
9689 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9691 settings = gtk_settings_get_default ();
9693 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9699 recent_idle_load (gpointer data)
9701 RecentLoadData *load_data = data;
9702 GtkFileChooserDefault *impl = load_data->impl;
9705 GtkRecentInfo *info;
9706 const gchar *uri, *display_name;
9708 GCancellable *cancellable;
9709 struct RecentItemInsertRequest *request;
9711 if (!impl->recent_manager)
9714 /* first iteration: load all the items */
9715 if (!load_data->items)
9717 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9718 if (!load_data->items)
9721 load_data->needs_sorting = TRUE;
9726 /* second iteration: preliminary MRU sorting and clamping */
9727 if (load_data->needs_sorting)
9731 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9732 load_data->n_items = g_list_length (load_data->items);
9734 limit = get_recent_files_limit (GTK_WIDGET (impl));
9736 if (limit != -1 && (load_data->n_items > limit))
9740 clamp = g_list_nth (load_data->items, limit - 1);
9741 if (G_LIKELY (clamp))
9746 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9749 load_data->n_items = limit;
9753 load_data->n_loaded_items = 0;
9754 load_data->needs_sorting = FALSE;
9759 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9760 g_assert (info != NULL);
9762 uri = gtk_recent_info_get_uri (info);
9763 display_name = gtk_recent_info_get_display_name (info);
9764 file = g_file_new_for_uri (uri);
9766 gtk_list_store_append (impl->recent_model, &iter);
9767 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9769 request = g_new0 (struct RecentItemInsertRequest, 1);
9770 request->impl = g_object_ref (impl);
9771 request->file = g_object_ref (file);
9772 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9773 gtk_tree_path_free (p);
9775 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9777 recent_item_get_info_cb,
9780 gtk_list_store_set (impl->recent_model, &iter,
9781 RECENT_MODEL_COL_FILE, file,
9782 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9783 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9784 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9787 load_data->n_loaded_items += 1;
9789 /* finished loading items */
9790 if (load_data->n_loaded_items == load_data->n_items)
9792 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9793 g_list_free (load_data->items);
9794 load_data->items = NULL;
9803 recent_start_loading (GtkFileChooserDefault *impl)
9805 RecentLoadData *load_data;
9807 recent_stop_loading (impl);
9808 recent_clear_model (impl, TRUE);
9809 recent_setup_model (impl);
9810 set_busy_cursor (impl, TRUE);
9812 if (!impl->recent_manager)
9813 recent_manager_update (impl);
9815 g_assert (impl->load_recent_id == 0);
9817 load_data = g_new (RecentLoadData, 1);
9818 load_data->impl = impl;
9819 load_data->items = NULL;
9820 load_data->n_items = 0;
9821 load_data->n_loaded_items = 0;
9822 load_data->needs_sorting = TRUE;
9824 /* begin lazy loading the recent files into the model */
9825 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9828 recent_idle_cleanup);
9832 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9842 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9843 *list = g_slist_prepend (*list, g_object_ref (file));
9846 /* Constructs a list of the selected paths in recent files mode */
9848 recent_get_selected_files (GtkFileChooserDefault *impl)
9851 GtkTreeSelection *selection;
9855 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9856 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9857 result = g_slist_reverse (result);
9862 /* Called from ::should_respond(). We return whether there are selected
9863 * files in the recent files list.
9866 recent_should_respond (GtkFileChooserDefault *impl)
9868 GtkTreeSelection *selection;
9870 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9872 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9873 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9876 /* Hide the location widgets temporarily */
9878 recent_hide_entry (GtkFileChooserDefault *impl)
9880 gtk_widget_hide (impl->browse_path_bar);
9881 gtk_widget_hide (impl->browse_new_folder_button);
9883 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9884 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9886 gtk_widget_hide (impl->location_button);
9887 gtk_widget_hide (impl->location_entry_box);
9891 /* Main entry point to the recent files functions; this gets called when
9892 * the user activates the Recently Used shortcut.
9895 recent_activate (GtkFileChooserDefault *impl)
9897 OperationMode previous_mode;
9899 if (impl->operation_mode == OPERATION_MODE_RECENT)
9902 previous_mode = impl->operation_mode;
9903 impl->operation_mode = OPERATION_MODE_RECENT;
9905 switch (previous_mode)
9907 case OPERATION_MODE_SEARCH:
9908 search_stop_searching (impl, FALSE);
9909 search_clear_model (impl, TRUE);
9911 gtk_widget_destroy (impl->search_hbox);
9912 impl->search_hbox = NULL;
9913 impl->search_entry = NULL;
9916 case OPERATION_MODE_BROWSE:
9917 stop_loading_and_clear_list_model (impl);
9920 case OPERATION_MODE_RECENT:
9921 g_assert_not_reached ();
9925 recent_hide_entry (impl);
9926 file_list_set_sort_column_ids (impl);
9927 recent_start_loading (impl);
9930 /* convert an iterator coming from the model bound to
9931 * browse_files_tree_view to an interator inside the
9935 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9936 GtkTreeIter *child_iter,
9941 if (!impl->recent_model)
9944 if (!impl->recent_model_filter || !impl->recent_model_sort)
9947 /* pass 1: get the iterator in the filter model */
9948 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9951 /* pass 2: get the iterator in the real model */
9952 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9959 set_current_filter (GtkFileChooserDefault *impl,
9960 GtkFileFilter *filter)
9962 if (impl->current_filter != filter)
9966 /* NULL filters are allowed to reset to non-filtered status
9968 filter_index = g_slist_index (impl->filters, filter);
9969 if (impl->filters && filter && filter_index < 0)
9972 if (impl->current_filter)
9973 g_object_unref (impl->current_filter);
9974 impl->current_filter = filter;
9975 if (impl->current_filter)
9977 g_object_ref_sink (impl->current_filter);
9981 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9984 if (impl->browse_files_model)
9985 install_list_model_filter (impl);
9987 if (impl->search_model_filter)
9988 gtk_tree_model_filter_refilter (impl->search_model_filter);
9990 if (impl->recent_model_filter)
9991 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9993 g_object_notify (G_OBJECT (impl), "filter");
9998 filter_combo_changed (GtkComboBox *combo_box,
9999 GtkFileChooserDefault *impl)
10001 gint new_index = gtk_combo_box_get_active (combo_box);
10002 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10004 set_current_filter (impl, new_filter);
10008 check_preview_change (GtkFileChooserDefault *impl)
10010 GtkTreePath *cursor_path;
10012 const char *new_display_name;
10014 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10016 new_display_name = NULL;
10019 GtkTreeIter child_iter;
10021 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10023 if (impl->sort_model)
10026 GFileInfo *new_info;
10028 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10029 gtk_tree_path_free (cursor_path);
10031 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10033 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10034 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10036 new_display_name = g_file_info_get_display_name (new_info);
10039 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10043 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10044 &iter, cursor_path);
10045 gtk_tree_path_free (cursor_path);
10047 search_get_valid_child_iter (impl, &child_iter, &iter);
10048 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10049 SEARCH_MODEL_COL_FILE, &new_file,
10050 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10053 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10057 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10058 &iter, cursor_path);
10059 gtk_tree_path_free (cursor_path);
10061 recent_get_valid_child_iter (impl, &child_iter, &iter);
10062 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10063 RECENT_MODEL_COL_FILE, &new_file,
10064 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10069 if (new_file != impl->preview_file &&
10070 !(new_file && impl->preview_file &&
10071 g_file_equal (new_file, impl->preview_file)))
10073 if (impl->preview_file)
10075 g_object_unref (impl->preview_file);
10076 g_free (impl->preview_display_name);
10081 impl->preview_file = g_object_ref (new_file);
10082 impl->preview_display_name = g_strdup (new_display_name);
10086 impl->preview_file = NULL;
10087 impl->preview_display_name = NULL;
10090 if (impl->use_preview_label && impl->preview_label)
10091 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10093 g_signal_emit_by_name (impl, "update-preview");
10098 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10099 GtkFileSystemVolume *volume,
10100 const GError *error,
10104 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10105 GtkFileChooserDefault *impl = data;
10107 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10110 impl->shortcuts_activate_iter_cancellable = NULL;
10112 set_busy_cursor (impl, FALSE);
10119 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10123 name = _gtk_file_system_volume_get_display_name (volume);
10124 msg = g_strdup_printf (_("Could not mount %s"), name);
10126 error_message (impl, msg, error->message);
10135 file = _gtk_file_system_volume_get_root (volume);
10138 change_folder_and_display_error (impl, file, FALSE);
10139 g_object_unref (file);
10143 g_object_unref (impl);
10144 g_object_unref (cancellable);
10148 /* Activates a volume by mounting it if necessary and then switching to its
10152 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10153 GtkFileSystemVolume *volume)
10157 switch (impl->operation_mode)
10159 case OPERATION_MODE_BROWSE:
10161 case OPERATION_MODE_SEARCH:
10162 search_switch_to_browse_mode (impl);
10164 case OPERATION_MODE_RECENT:
10165 recent_switch_to_browse_mode (impl);
10169 /* We ref the file chooser since volume_mount() may run a main loop, and the
10170 * user could close the file chooser window in the meantime.
10172 g_object_ref (impl);
10174 if (!_gtk_file_system_volume_is_mounted (volume))
10176 set_busy_cursor (impl, TRUE);
10178 impl->shortcuts_activate_iter_cancellable =
10179 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10180 shortcuts_activate_volume_mount_cb,
10181 g_object_ref (impl));
10185 file = _gtk_file_system_volume_get_root (volume);
10188 change_folder_and_display_error (impl, file, FALSE);
10189 g_object_unref (file);
10193 g_object_unref (impl);
10196 /* Opens the folder or volume at the specified iter in the shortcuts model */
10197 struct ShortcutsActivateData
10199 GtkFileChooserDefault *impl;
10204 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10206 const GError *error,
10207 gpointer user_data)
10209 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10210 struct ShortcutsActivateData *data = user_data;
10212 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10215 data->impl->shortcuts_activate_iter_cancellable = NULL;
10220 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10221 change_folder_and_display_error (data->impl, data->file, FALSE);
10223 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10228 g_object_unref (data->impl);
10229 g_object_unref (data->file);
10232 g_object_unref (cancellable);
10236 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10237 GtkFileSystemVolume *volume,
10238 const GError *error,
10239 gpointer user_data)
10241 struct ShortcutsActivateData *data = user_data;
10245 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10247 g_object_unref (data->impl);
10248 g_object_unref (data->file);
10254 data->impl->shortcuts_activate_iter_cancellable =
10255 _gtk_file_system_get_info (data->impl->file_system, data->file,
10257 shortcuts_activate_get_info_cb, data);
10261 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10265 ShortcutType shortcut_type;
10267 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
10268 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10270 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10271 SHORTCUTS_COL_DATA, &col_data,
10272 SHORTCUTS_COL_TYPE, &shortcut_type,
10275 if (impl->shortcuts_activate_iter_cancellable)
10277 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10278 impl->shortcuts_activate_iter_cancellable = NULL;
10281 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10283 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10285 GtkFileSystemVolume *volume;
10289 shortcuts_activate_volume (impl, volume);
10291 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10293 struct ShortcutsActivateData *data;
10294 GtkFileSystemVolume *volume;
10296 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10298 data = g_new0 (struct ShortcutsActivateData, 1);
10299 data->impl = g_object_ref (impl);
10300 data->file = g_object_ref (col_data);
10302 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10304 GMountOperation *mount_operation;
10305 GtkWidget *toplevel;
10307 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10309 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10311 impl->shortcuts_activate_iter_cancellable =
10312 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10314 shortcuts_activate_mount_enclosing_volume,
10319 impl->shortcuts_activate_iter_cancellable =
10320 _gtk_file_system_get_info (impl->file_system, data->file,
10322 shortcuts_activate_get_info_cb, data);
10325 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10327 search_activate (impl);
10329 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10331 recent_activate (impl);
10335 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10337 shortcuts_key_press_event_cb (GtkWidget *widget,
10338 GdkEventKey *event,
10339 GtkFileChooserDefault *impl)
10343 modifiers = gtk_accelerator_get_default_mod_mask ();
10345 if ((event->keyval == GDK_BackSpace
10346 || event->keyval == GDK_Delete
10347 || event->keyval == GDK_KP_Delete)
10348 && (event->state & modifiers) == 0)
10350 remove_selected_bookmarks (impl);
10354 if ((event->keyval == GDK_F2)
10355 && (event->state & modifiers) == 0)
10357 rename_selected_bookmark (impl);
10365 shortcuts_select_func (GtkTreeSelection *selection,
10366 GtkTreeModel *model,
10368 gboolean path_currently_selected,
10371 GtkFileChooserDefault *impl = data;
10372 GtkTreeIter filter_iter;
10373 ShortcutType shortcut_type;
10375 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10376 g_assert_not_reached ();
10378 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10380 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10384 list_select_func (GtkTreeSelection *selection,
10385 GtkTreeModel *model,
10387 gboolean path_currently_selected,
10390 GtkFileChooserDefault *impl = data;
10392 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10393 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10395 GtkTreeIter iter, child_iter;
10397 switch (impl->operation_mode)
10399 case OPERATION_MODE_SEARCH:
10401 gboolean is_folder;
10403 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10406 search_get_valid_child_iter (impl, &child_iter, &iter);
10407 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10408 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10415 case OPERATION_MODE_RECENT:
10417 gboolean is_folder;
10419 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10422 recent_get_valid_child_iter (impl, &child_iter, &iter);
10423 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10424 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10431 case OPERATION_MODE_BROWSE:
10435 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10438 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10439 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10440 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10451 list_selection_changed (GtkTreeSelection *selection,
10452 GtkFileChooserDefault *impl)
10454 /* See if we are in the new folder editable row for Save mode */
10455 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10456 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10459 gboolean had_selection;
10461 info = get_selected_file_info_from_file_list (impl, &had_selection);
10462 if (!had_selection)
10463 goto out; /* normal processing */
10466 return; /* We are on the editable row for New Folder */
10471 if (impl->location_entry)
10472 update_chooser_entry (impl);
10474 check_preview_change (impl);
10475 bookmarks_check_add_sensitivity (impl);
10477 g_signal_emit_by_name (impl, "selection-changed", 0);
10480 /* Callback used when a row in the file list is activated */
10482 list_row_activated (GtkTreeView *tree_view,
10484 GtkTreeViewColumn *column,
10485 GtkFileChooserDefault *impl)
10488 GtkTreeIter child_iter;
10490 switch (impl->operation_mode)
10492 case OPERATION_MODE_SEARCH:
10495 gboolean is_folder;
10497 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10500 search_get_valid_child_iter (impl, &child_iter, &iter);
10501 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10502 SEARCH_MODEL_COL_FILE, &file,
10503 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10508 change_folder_and_display_error (impl, file, FALSE);
10512 g_signal_emit_by_name (impl, "file-activated");
10516 case OPERATION_MODE_RECENT:
10519 gboolean is_folder;
10521 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10524 recent_get_valid_child_iter (impl, &child_iter, &iter);
10525 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10526 RECENT_MODEL_COL_FILE, &file,
10527 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10532 change_folder_and_display_error (impl, file, FALSE);
10536 g_signal_emit_by_name (impl, "file-activated");
10540 case OPERATION_MODE_BROWSE:
10544 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10547 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10548 &child_iter, &iter);
10549 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10552 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10556 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10557 change_folder_and_display_error (impl, file, FALSE);
10561 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10562 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10563 g_signal_emit_by_name (impl, "file-activated");
10570 path_bar_clicked (GtkPathBar *path_bar,
10573 gboolean child_is_hidden,
10574 GtkFileChooserDefault *impl)
10577 pending_select_files_add (impl, child_file);
10579 if (!change_folder_and_display_error (impl, file, FALSE))
10582 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10583 * show hidden files so that ".baz" appears in the file list, as it will still
10584 * be shown in the path bar: "/foo/[bar]/.baz"
10586 if (child_is_hidden)
10587 g_object_set (impl, "show-hidden", TRUE, NULL);
10591 get_list_file_info (GtkFileChooserDefault *impl,
10594 GtkTreeIter child_iter;
10596 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10600 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10604 list_icon_data_func (GtkTreeViewColumn *tree_column,
10605 GtkCellRenderer *cell,
10606 GtkTreeModel *tree_model,
10610 GtkFileChooserDefault *impl = data;
10611 GtkTreeIter child_iter;
10612 GdkPixbuf *pixbuf = NULL;
10613 gboolean sensitive = TRUE;
10615 profile_start ("start", NULL);
10617 switch (impl->operation_mode)
10619 case OPERATION_MODE_SEARCH:
10621 GtkTreeIter child_iter;
10622 gboolean is_folder;
10624 search_get_valid_child_iter (impl, &child_iter, iter);
10625 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10626 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10627 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10630 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10631 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10632 sensitive = is_folder;
10636 case OPERATION_MODE_RECENT:
10638 GtkTreeIter child_iter;
10639 GtkRecentInfo *info;
10640 gboolean is_folder;
10642 recent_get_valid_child_iter (impl, &child_iter, iter);
10643 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10644 RECENT_MODEL_COL_INFO, &info,
10645 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10648 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10650 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10651 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10652 sensitive = is_folder;
10656 case OPERATION_MODE_BROWSE:
10661 info = get_list_file_info (impl, iter);
10663 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10666 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10671 /* FIXME: NULL GError */
10672 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10677 /* We are on the editable row */
10682 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10683 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10684 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10689 g_object_set (cell,
10691 "sensitive", sensitive,
10695 g_object_unref (pixbuf);
10697 profile_end ("end", NULL);
10701 list_name_data_func (GtkTreeViewColumn *tree_column,
10702 GtkCellRenderer *cell,
10703 GtkTreeModel *tree_model,
10707 GtkFileChooserDefault *impl = data;
10709 gboolean sensitive = TRUE;
10711 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10713 GtkTreeIter child_iter;
10714 gchar *display_name;
10715 gboolean is_folder;
10717 search_get_valid_child_iter (impl, &child_iter, iter);
10718 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10719 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10720 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10723 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10724 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10726 sensitive = is_folder;
10729 g_object_set (cell,
10730 "text", display_name,
10731 "sensitive", sensitive,
10732 "ellipsize", PANGO_ELLIPSIZE_END,
10738 if (impl->operation_mode == OPERATION_MODE_RECENT)
10740 GtkTreeIter child_iter;
10741 GtkRecentInfo *recent_info;
10742 gchar *display_name;
10743 gboolean is_folder;
10745 recent_get_valid_child_iter (impl, &child_iter, iter);
10746 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10747 RECENT_MODEL_COL_INFO, &recent_info,
10748 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10751 display_name = gtk_recent_info_get_short_name (recent_info);
10753 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10754 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10756 sensitive = is_folder;
10759 g_object_set (cell,
10760 "text", display_name,
10761 "sensitive", sensitive,
10762 "ellipsize", PANGO_ELLIPSIZE_END,
10765 g_free (display_name);
10770 info = get_list_file_info (impl, iter);
10775 g_object_set (cell,
10776 "text", _("Type name of new folder"),
10778 "ellipsize", PANGO_ELLIPSIZE_NONE,
10785 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10786 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10788 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10791 g_object_set (cell,
10792 "text", g_file_info_get_display_name (info),
10793 "sensitive", sensitive,
10794 "ellipsize", PANGO_ELLIPSIZE_END,
10800 list_size_data_func (GtkTreeViewColumn *tree_column,
10801 GtkCellRenderer *cell,
10802 GtkTreeModel *tree_model,
10806 GtkFileChooserDefault *impl = data;
10807 GFileInfo *info = get_list_file_info (impl, iter);
10810 gboolean sensitive = TRUE;
10812 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10814 g_object_set (cell,
10816 "sensitive", sensitive,
10821 size = gtk_file_info_get_size (info);
10823 if (size < (gint64)1024)
10824 str = g_strdup_printf (g_dngettext (GETTEXT_DOMAIN, "%d byte", "%d bytes", (gint)size), (gint)size);
10825 else if (size < (gint64)1024*1024)
10826 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10827 else if (size < (gint64)1024*1024*1024)
10828 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10830 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10832 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10833 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10834 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10837 g_object_set (cell,
10839 "sensitive", sensitive,
10840 "alignment", PANGO_ALIGN_RIGHT,
10847 /* Tree column data callback for the file list; fetches the mtime of a file */
10849 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10850 GtkCellRenderer *cell,
10851 GtkTreeModel *tree_model,
10855 GtkFileChooserDefault *impl;
10856 GTimeVal timeval = { 0, };
10858 gchar *date_str = NULL;
10859 gboolean sensitive = TRUE;
10861 const char *locale, *dot = NULL;
10862 gint64 codepage = -1;
10868 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10870 GtkTreeIter child_iter;
10871 struct stat *statbuf;
10872 gboolean is_folder;
10874 search_get_valid_child_iter (impl, &child_iter, iter);
10875 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10876 SEARCH_MODEL_COL_STAT, &statbuf,
10877 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10880 time_mtime = statbuf->st_mtime;
10885 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10886 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10887 sensitive = is_folder;
10889 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10891 GtkTreeIter child_iter;
10892 GtkRecentInfo *info;
10893 gboolean is_folder;
10895 recent_get_valid_child_iter (impl, &child_iter, iter);
10896 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10897 RECENT_MODEL_COL_INFO, &info,
10898 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10902 time_mtime = gtk_recent_info_get_modified (info);
10906 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10907 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10908 sensitive = is_folder;
10914 info = get_list_file_info (impl, iter);
10917 g_object_set (cell,
10924 g_file_info_get_modification_time (info, &timeval);
10925 time_mtime = timeval.tv_sec;
10927 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10928 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10929 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10932 if (G_UNLIKELY (time_mtime == 0))
10933 date_str = g_strdup (_("Unknown"));
10938 struct tm tm_mtime;
10940 const gchar *format;
10941 gchar *locale_format = NULL;
10944 #ifdef HAVE_LOCALTIME_R
10945 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10948 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10952 g_warning ("ptm != NULL failed");
10954 g_object_set (cell,
10955 "text", _("Unknown"),
10956 "sensitive", sensitive,
10961 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10963 #endif /* HAVE_LOCALTIME_R */
10965 g_date_set_time_t (&mtime, time_mtime);
10966 time_now = time (NULL);
10967 g_date_set_time_t (&now, time_now);
10969 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10971 /* Translators: %H means "hours" and %M means "minutes" */
10972 if (days_diff == 0)
10973 format = _("%H:%M");
10974 else if (days_diff == 1)
10975 format = _("Yesterday at %H:%M");
10978 if (days_diff > 1 && days_diff < 7)
10979 format = "%A"; /* Days from last week */
10981 format = "%x"; /* Any other date */
10985 /* g_locale_from_utf8() returns a string in the system
10986 * code-page, which is not always the same as that used by the C
10987 * library. For instance when running a GTK+ program with
10988 * LANG=ko on an English version of Windows, the system
10989 * code-page is 1252, but the code-page used by the C library is
10990 * 949. (It's GTK+ itself that sets the C library locale when it
10991 * notices the LANG environment variable. See gtkmain.c The
10992 * Microsoft C library doesn't look at any locale environment
10993 * variables.) We need to pass strftime() a string in the C
10994 * library's code-page. See bug #509885.
10996 locale = setlocale (LC_ALL, NULL);
10997 if (locale != NULL)
10998 dot = strchr (locale, '.');
11001 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11003 /* All codepages should fit in 16 bits AFAIK */
11004 if (codepage > 0 && codepage < 65536)
11006 sprintf (charset, "CP%u", (guint) codepage);
11007 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11011 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11013 if (locale_format != NULL &&
11014 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11017 /* As above but in opposite direction... */
11018 if (codepage > 0 && codepage < 65536)
11019 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11021 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11025 if (date_str == NULL)
11026 date_str = g_strdup (_("Unknown"));
11028 g_free (locale_format);
11031 g_object_set (cell,
11033 "sensitive", sensitive,
11039 _gtk_file_chooser_default_new (const char *file_system)
11041 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
11042 "file-system-backend", file_system,
11047 location_set_user_text (GtkFileChooserDefault *impl,
11050 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11051 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11055 location_popup_handler (GtkFileChooserDefault *impl,
11058 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11060 GtkWidget *widget_to_focus;
11062 /* This will give us the location widgets back */
11063 switch (impl->operation_mode)
11065 case OPERATION_MODE_SEARCH:
11066 search_switch_to_browse_mode (impl);
11068 case OPERATION_MODE_RECENT:
11069 recent_switch_to_browse_mode (impl);
11071 case OPERATION_MODE_BROWSE:
11072 g_assert_not_reached ();
11076 if (impl->current_folder)
11077 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11079 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11080 widget_to_focus = impl->browse_files_tree_view;
11082 widget_to_focus = impl->location_entry;
11084 gtk_widget_grab_focus (widget_to_focus);
11088 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11089 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11091 LocationMode new_mode;
11095 /* since the user typed something, we unconditionally want to turn on the entry */
11096 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11098 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11099 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11100 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11101 new_mode = LOCATION_MODE_PATH_BAR;
11104 g_assert_not_reached ();
11108 location_mode_set (impl, new_mode, TRUE);
11109 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11112 location_set_user_text (impl, path);
11115 location_entry_set_initial_text (impl);
11116 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11120 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11121 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11123 gtk_widget_grab_focus (impl->location_entry);
11125 location_set_user_text (impl, path);
11128 g_assert_not_reached ();
11131 /* Handler for the "up-folder" keybinding signal */
11133 up_folder_handler (GtkFileChooserDefault *impl)
11135 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11138 /* Handler for the "down-folder" keybinding signal */
11140 down_folder_handler (GtkFileChooserDefault *impl)
11142 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11145 /* Switches to the shortcut in the specified index */
11147 switch_to_shortcut (GtkFileChooserDefault *impl,
11152 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11153 g_assert_not_reached ();
11155 shortcuts_activate_iter (impl, &iter);
11158 /* Handler for the "home-folder" keybinding signal */
11160 home_folder_handler (GtkFileChooserDefault *impl)
11162 if (impl->has_home)
11163 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11166 /* Handler for the "desktop-folder" keybinding signal */
11168 desktop_folder_handler (GtkFileChooserDefault *impl)
11170 if (impl->has_desktop)
11171 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11174 /* Handler for the "search-shortcut" keybinding signal */
11176 search_shortcut_handler (GtkFileChooserDefault *impl)
11178 if (impl->has_search)
11180 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11182 /* we want the entry widget to grab the focus the first
11183 * time, not the browse_files_tree_view widget.
11185 if (impl->search_entry)
11186 gtk_widget_grab_focus (impl->search_entry);
11190 /* Handler for the "recent-shortcut" keybinding signal */
11192 recent_shortcut_handler (GtkFileChooserDefault *impl)
11194 if (impl->has_recent)
11195 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11199 quick_bookmark_handler (GtkFileChooserDefault *impl,
11200 gint bookmark_index)
11205 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11208 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11210 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11211 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11214 gtk_tree_path_free (path);
11216 switch_to_shortcut (impl, bookmark_pos);
11220 show_hidden_handler (GtkFileChooserDefault *impl)
11222 g_object_set (impl,
11223 "show-hidden", !impl->show_hidden,
11228 /* Drag and drop interfaces */
11231 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11236 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11238 model->impl = NULL;
11241 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11243 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11246 ShortcutsPaneModelFilter *model;
11250 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11252 pos = *gtk_tree_path_get_indices (path);
11253 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11255 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11258 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11260 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11262 GtkSelectionData *selection_data)
11264 ShortcutsPaneModelFilter *model;
11266 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11273 /* Fill the GtkTreeDragSourceIface vtable */
11275 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11277 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11278 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11282 /* Fill the GtkTreeDragDestIface vtable */
11284 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11286 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11287 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11291 static GtkTreeModel *
11292 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11293 GtkTreeModel *child_model,
11296 ShortcutsPaneModelFilter *model;
11298 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11299 "child-model", child_model,
11300 "virtual-root", root,
11303 model->impl = impl;
11305 return GTK_TREE_MODEL (model);
11311 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11314 RecentModelSort *model;
11315 GtkTreeIter iter, child_iter;
11316 gboolean is_folder;
11318 model = RECENT_MODEL_SORT (drag_source);
11319 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11322 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11323 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11324 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11331 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11333 GtkSelectionData *selection_data)
11335 RecentModelSort *model;
11336 GtkTreeIter iter, child_iter;
11340 model = RECENT_MODEL_SORT (drag_source);
11341 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11344 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11345 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11346 RECENT_MODEL_COL_FILE, &file,
11348 g_assert (file != NULL);
11350 uris[0] = g_file_get_uri (file);
11353 gtk_selection_data_set_uris (selection_data, uris);
11361 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11363 iface->row_draggable = recent_model_sort_row_draggable;
11364 iface->drag_data_get = recent_model_sort_drag_data_get;
11368 _recent_model_sort_class_init (RecentModelSortClass *klass)
11374 _recent_model_sort_init (RecentModelSort *model)
11376 model->impl = NULL;
11379 static GtkTreeModel *
11380 recent_model_sort_new (GtkFileChooserDefault *impl,
11381 GtkTreeModel *child_model)
11383 RecentModelSort *model;
11385 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11386 "model", child_model,
11388 model->impl = impl;
11390 return GTK_TREE_MODEL (model);
11396 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11399 SearchModelSort *model;
11400 GtkTreeIter iter, child_iter;
11401 gboolean is_folder;
11403 model = SEARCH_MODEL_SORT (drag_source);
11404 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11407 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11408 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11409 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11416 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11418 GtkSelectionData *selection_data)
11420 SearchModelSort *model;
11421 GtkTreeIter iter, child_iter;
11425 model = SEARCH_MODEL_SORT (drag_source);
11426 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11429 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11430 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11431 RECENT_MODEL_COL_FILE, &file,
11433 g_assert (file != NULL);
11435 uris[0] = g_file_get_uri (file);
11438 gtk_selection_data_set_uris (selection_data, uris);
11446 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11448 iface->row_draggable = search_model_sort_row_draggable;
11449 iface->drag_data_get = search_model_sort_drag_data_get;
11453 _search_model_sort_class_init (SearchModelSortClass *klass)
11459 _search_model_sort_init (SearchModelSort *model)
11461 model->impl = NULL;
11464 static GtkTreeModel *
11465 search_model_sort_new (GtkFileChooserDefault *impl,
11466 GtkTreeModel *child_model)
11468 SearchModelSort *model;
11470 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11471 "model", child_model,
11473 model->impl = impl;
11475 return GTK_TREE_MODEL (model);