1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystem.h"
44 #include "gtkfilesystemmodel.h"
47 #include "gtkhpaned.h"
48 #include "gtkiconfactory.h"
49 #include "gtkicontheme.h"
51 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkmountoperation.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkrecentfilter.h"
60 #include "gtkrecentmanager.h"
61 #include "gtkscrolledwindow.h"
62 #include "gtkseparatormenuitem.h"
63 #include "gtksizegroup.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 struct _GtkFileChooserDefaultClass
150 GtkVBoxClass parent_class;
156 LOCATION_POPUP_ON_PASTE,
162 LOCATION_TOGGLE_POPUP,
170 static guint signals[LAST_SIGNAL] = { 0 };
172 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
174 SHORTCUTS_COL_PIXBUF,
178 SHORTCUTS_COL_REMOVABLE,
179 SHORTCUTS_COL_PIXBUF_VISIBLE,
180 SHORTCUTS_COL_CANCELLABLE,
181 SHORTCUTS_COL_NUM_COLUMNS
186 SHORTCUT_TYPE_VOLUME,
187 SHORTCUT_TYPE_SEPARATOR,
188 SHORTCUT_TYPE_SEARCH,
192 /* Column numbers for the file list */
197 FILE_LIST_COL_NUM_COLUMNS
200 /* Column numbers for the search model.
201 * Keep this in sync with search_setup_model()
204 SEARCH_MODEL_COL_FILE,
205 SEARCH_MODEL_COL_DISPLAY_NAME,
206 SEARCH_MODEL_COL_COLLATION_KEY,
207 SEARCH_MODEL_COL_MTIME,
208 SEARCH_MODEL_COL_SIZE,
209 SEARCH_MODEL_COL_CANCELLABLE,
210 SEARCH_MODEL_COL_PIXBUF,
211 SEARCH_MODEL_COL_MIME_TYPE,
212 SEARCH_MODEL_COL_IS_FOLDER,
213 SEARCH_MODEL_COL_NUM_COLUMNS
217 RECENT_MODEL_COL_FILE,
218 RECENT_MODEL_COL_DISPLAY_NAME,
219 RECENT_MODEL_COL_INFO,
220 RECENT_MODEL_COL_IS_FOLDER,
221 RECENT_MODEL_COL_CANCELLABLE,
222 RECENT_MODEL_COL_NUM_COLUMNS
225 /* Identifiers for target types */
231 search_is_possible (GtkFileChooserDefault *impl)
233 if (impl->search_engine == NULL)
234 impl->search_engine = _gtk_search_engine_new ();
236 return impl->search_engine != NULL;
239 /* Interesting places in the shortcuts bar */
243 SHORTCUTS_RECENT_SEPARATOR,
248 SHORTCUTS_BOOKMARKS_SEPARATOR,
250 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
251 SHORTCUTS_CURRENT_FOLDER
254 /* Icon size for if we can't get it from the theme */
255 #define FALLBACK_ICON_SIZE 16
257 #define PREVIEW_HBOX_SPACING 12
261 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
262 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
264 static GObject* gtk_file_chooser_default_constructor (GType type,
265 guint n_construct_properties,
266 GObjectConstructParam *construct_params);
267 static void gtk_file_chooser_default_finalize (GObject *object);
268 static void gtk_file_chooser_default_set_property (GObject *object,
272 static void gtk_file_chooser_default_get_property (GObject *object,
276 static void gtk_file_chooser_default_dispose (GObject *object);
277 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
278 static void gtk_file_chooser_default_realize (GtkWidget *widget);
279 static void gtk_file_chooser_default_map (GtkWidget *widget);
280 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
281 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
282 GtkWidget *previous_toplevel);
283 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
284 GtkStyle *previous_style);
285 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
286 GdkScreen *previous_screen);
287 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
296 gboolean clear_entry,
298 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
299 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
301 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
308 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
309 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
310 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
316 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
322 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
324 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
326 gint *default_height);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl,
332 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
333 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
334 static void up_folder_handler (GtkFileChooserDefault *impl);
335 static void down_folder_handler (GtkFileChooserDefault *impl);
336 static void home_folder_handler (GtkFileChooserDefault *impl);
337 static void desktop_folder_handler (GtkFileChooserDefault *impl);
338 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
339 gint bookmark_index);
340 static void show_hidden_handler (GtkFileChooserDefault *impl);
341 static void search_shortcut_handler (GtkFileChooserDefault *impl);
342 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
343 static void update_appearance (GtkFileChooserDefault *impl);
345 static void set_current_filter (GtkFileChooserDefault *impl,
346 GtkFileFilter *filter);
347 static void check_preview_change (GtkFileChooserDefault *impl);
349 static void filter_combo_changed (GtkComboBox *combo_box,
350 GtkFileChooserDefault *impl);
352 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
354 GtkFileChooserDefault *impl);
356 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
359 gboolean path_currently_selected,
361 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
363 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
365 static int shortcuts_get_index (GtkFileChooserDefault *impl,
366 ShortcutsIndex where);
367 static int shortcut_find_position (GtkFileChooserDefault *impl,
370 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
372 static gboolean list_select_func (GtkTreeSelection *selection,
375 gboolean path_currently_selected,
378 static void list_selection_changed (GtkTreeSelection *tree_selection,
379 GtkFileChooserDefault *impl);
380 static void list_row_activated (GtkTreeView *tree_view,
382 GtkTreeViewColumn *column,
383 GtkFileChooserDefault *impl);
385 static void select_func (GtkFileSystemModel *model,
390 static void path_bar_clicked (GtkPathBar *path_bar,
393 gboolean child_is_hidden,
394 GtkFileChooserDefault *impl);
396 static void add_bookmark_button_clicked_cb (GtkButton *button,
397 GtkFileChooserDefault *impl);
398 static void remove_bookmark_button_clicked_cb (GtkButton *button,
399 GtkFileChooserDefault *impl);
400 static void save_folder_combo_changed_cb (GtkComboBox *combo,
401 GtkFileChooserDefault *impl);
403 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
404 GtkCellRenderer *cell,
405 GtkTreeModel *tree_model,
408 static void list_name_data_func (GtkTreeViewColumn *tree_column,
409 GtkCellRenderer *cell,
410 GtkTreeModel *tree_model,
413 static void list_size_data_func (GtkTreeViewColumn *tree_column,
414 GtkCellRenderer *cell,
415 GtkTreeModel *tree_model,
418 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
419 GtkCellRenderer *cell,
420 GtkTreeModel *tree_model,
424 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
427 static void load_remove_timer (GtkFileChooserDefault *impl);
428 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
430 static void location_button_toggled_cb (GtkToggleButton *toggle,
431 GtkFileChooserDefault *impl);
432 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
434 static void search_stop_searching (GtkFileChooserDefault *impl,
435 gboolean remove_query);
436 static void search_clear_model_row (GtkTreeModel *model,
438 static void search_clear_model (GtkFileChooserDefault *impl,
439 gboolean remove_from_treeview);
440 static gboolean search_should_respond (GtkFileChooserDefault *impl);
441 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
442 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
443 static void search_entry_activate_cb (GtkEntry *entry,
445 static void settings_load (GtkFileChooserDefault *impl);
446 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
447 GtkTreeIter *child_iter,
450 static void recent_stop_loading (GtkFileChooserDefault *impl);
451 static void recent_clear_model (GtkFileChooserDefault *impl,
452 gboolean remove_from_treeview);
453 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
454 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
455 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
456 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
457 GtkTreeIter *child_iter,
459 static void set_file_system_backend (GtkFileChooserDefault *impl);
460 static void unset_file_system_backend (GtkFileChooserDefault *impl);
466 /* Drag and drop interface declarations */
469 GtkTreeModelFilter parent;
471 GtkFileChooserDefault *impl;
472 } ShortcutsPaneModelFilter;
475 GtkTreeModelFilterClass parent_class;
476 } ShortcutsPaneModelFilterClass;
478 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
479 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
481 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
483 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
484 _shortcuts_pane_model_filter,
485 GTK_TYPE_TREE_MODEL_FILTER,
486 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
487 shortcuts_pane_model_filter_drag_source_iface_init))
489 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
490 GtkTreeModel *child_model,
495 GtkTreeModelSort parent;
497 GtkFileChooserDefault *impl;
501 GtkTreeModelSortClass parent_class;
502 } RecentModelSortClass;
504 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
505 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
507 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
509 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
511 GTK_TYPE_TREE_MODEL_SORT,
512 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
513 recent_model_sort_drag_source_iface_init));
515 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
516 GtkTreeModel *child_model);
520 GtkTreeModelSort parent;
522 GtkFileChooserDefault *impl;
526 GtkTreeModelSortClass parent_class;
527 } SearchModelSortClass;
529 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
530 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
532 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
534 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
536 GTK_TYPE_TREE_MODEL_SORT,
537 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
538 search_model_sort_drag_source_iface_init));
540 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
541 GtkTreeModel *child_model);
545 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
546 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
547 gtk_file_chooser_default_iface_init)
548 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
549 gtk_file_chooser_embed_default_iface_init));
552 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
554 static const guint quick_bookmark_keyvals[10] = {
555 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
557 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
558 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
559 GtkBindingSet *binding_set;
562 gobject_class->finalize = gtk_file_chooser_default_finalize;
563 gobject_class->constructor = gtk_file_chooser_default_constructor;
564 gobject_class->set_property = gtk_file_chooser_default_set_property;
565 gobject_class->get_property = gtk_file_chooser_default_get_property;
566 gobject_class->dispose = gtk_file_chooser_default_dispose;
568 widget_class->show_all = gtk_file_chooser_default_show_all;
569 widget_class->realize = gtk_file_chooser_default_realize;
570 widget_class->map = gtk_file_chooser_default_map;
571 widget_class->unmap = gtk_file_chooser_default_unmap;
572 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
573 widget_class->style_set = gtk_file_chooser_default_style_set;
574 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
575 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
577 signals[LOCATION_POPUP] =
578 g_signal_new_class_handler (I_("location-popup"),
579 G_OBJECT_CLASS_TYPE (class),
580 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
581 G_CALLBACK (location_popup_handler),
583 _gtk_marshal_VOID__STRING,
584 G_TYPE_NONE, 1, G_TYPE_STRING);
586 signals[LOCATION_POPUP_ON_PASTE] =
587 g_signal_new_class_handler (I_("location-popup-on-paste"),
588 G_OBJECT_CLASS_TYPE (class),
589 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
590 G_CALLBACK (location_popup_on_paste_handler),
592 _gtk_marshal_VOID__VOID,
595 signals[LOCATION_TOGGLE_POPUP] =
596 g_signal_new_class_handler (I_("location-toggle-popup"),
597 G_OBJECT_CLASS_TYPE (class),
598 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
599 G_CALLBACK (location_toggle_popup_handler),
601 _gtk_marshal_VOID__VOID,
605 g_signal_new_class_handler (I_("up-folder"),
606 G_OBJECT_CLASS_TYPE (class),
607 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
608 G_CALLBACK (up_folder_handler),
610 _gtk_marshal_VOID__VOID,
613 signals[DOWN_FOLDER] =
614 g_signal_new_class_handler (I_("down-folder"),
615 G_OBJECT_CLASS_TYPE (class),
616 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
617 G_CALLBACK (down_folder_handler),
619 _gtk_marshal_VOID__VOID,
622 signals[HOME_FOLDER] =
623 g_signal_new_class_handler (I_("home-folder"),
624 G_OBJECT_CLASS_TYPE (class),
625 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
626 G_CALLBACK (home_folder_handler),
628 _gtk_marshal_VOID__VOID,
631 signals[DESKTOP_FOLDER] =
632 g_signal_new_class_handler (I_("desktop-folder"),
633 G_OBJECT_CLASS_TYPE (class),
634 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
635 G_CALLBACK (desktop_folder_handler),
637 _gtk_marshal_VOID__VOID,
640 signals[QUICK_BOOKMARK] =
641 g_signal_new_class_handler (I_("quick-bookmark"),
642 G_OBJECT_CLASS_TYPE (class),
643 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
644 G_CALLBACK (quick_bookmark_handler),
646 _gtk_marshal_VOID__INT,
647 G_TYPE_NONE, 1, G_TYPE_INT);
649 signals[SHOW_HIDDEN] =
650 g_signal_new_class_handler (I_("show-hidden"),
651 G_OBJECT_CLASS_TYPE (class),
652 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
653 G_CALLBACK (show_hidden_handler),
655 _gtk_marshal_VOID__VOID,
658 signals[SEARCH_SHORTCUT] =
659 g_signal_new_class_handler (I_("search-shortcut"),
660 G_OBJECT_CLASS_TYPE (class),
661 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
662 G_CALLBACK (search_shortcut_handler),
664 _gtk_marshal_VOID__VOID,
667 signals[RECENT_SHORTCUT] =
668 g_signal_new_class_handler (I_("recent-shortcut"),
669 G_OBJECT_CLASS_TYPE (class),
670 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
671 G_CALLBACK (recent_shortcut_handler),
673 _gtk_marshal_VOID__VOID,
676 binding_set = gtk_binding_set_by_class (class);
678 gtk_binding_entry_add_signal (binding_set,
679 GDK_l, GDK_CONTROL_MASK,
680 "location-toggle-popup",
683 gtk_binding_entry_add_signal (binding_set,
686 1, G_TYPE_STRING, "/");
687 gtk_binding_entry_add_signal (binding_set,
690 1, G_TYPE_STRING, "/");
693 gtk_binding_entry_add_signal (binding_set,
696 1, G_TYPE_STRING, "~");
699 gtk_binding_entry_add_signal (binding_set,
700 GDK_v, GDK_CONTROL_MASK,
701 "location-popup-on-paste",
703 gtk_binding_entry_add_signal (binding_set,
704 GDK_Up, GDK_MOD1_MASK,
707 gtk_binding_entry_add_signal (binding_set,
711 gtk_binding_entry_add_signal (binding_set,
712 GDK_KP_Up, GDK_MOD1_MASK,
716 gtk_binding_entry_add_signal (binding_set,
717 GDK_Down, GDK_MOD1_MASK,
720 gtk_binding_entry_add_signal (binding_set,
721 GDK_KP_Down, GDK_MOD1_MASK,
725 gtk_binding_entry_add_signal (binding_set,
726 GDK_Home, GDK_MOD1_MASK,
729 gtk_binding_entry_add_signal (binding_set,
730 GDK_KP_Home, GDK_MOD1_MASK,
733 gtk_binding_entry_add_signal (binding_set,
734 GDK_d, GDK_MOD1_MASK,
737 gtk_binding_entry_add_signal (binding_set,
738 GDK_h, GDK_CONTROL_MASK,
741 gtk_binding_entry_add_signal (binding_set,
742 GDK_s, GDK_MOD1_MASK,
745 gtk_binding_entry_add_signal (binding_set,
746 GDK_r, GDK_MOD1_MASK,
750 for (i = 0; i < 10; i++)
751 gtk_binding_entry_add_signal (binding_set,
752 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
756 _gtk_file_chooser_install_properties (gobject_class);
760 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
762 iface->select_file = gtk_file_chooser_default_select_file;
763 iface->unselect_file = gtk_file_chooser_default_unselect_file;
764 iface->select_all = gtk_file_chooser_default_select_all;
765 iface->unselect_all = gtk_file_chooser_default_unselect_all;
766 iface->get_files = gtk_file_chooser_default_get_files;
767 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
768 iface->get_file_system = gtk_file_chooser_default_get_file_system;
769 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
770 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
771 iface->set_current_name = gtk_file_chooser_default_set_current_name;
772 iface->add_filter = gtk_file_chooser_default_add_filter;
773 iface->remove_filter = gtk_file_chooser_default_remove_filter;
774 iface->list_filters = gtk_file_chooser_default_list_filters;
775 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
776 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
777 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
781 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
783 iface->get_default_size = gtk_file_chooser_default_get_default_size;
784 iface->should_respond = gtk_file_chooser_default_should_respond;
785 iface->initial_focus = gtk_file_chooser_default_initial_focus;
789 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
791 profile_start ("start", NULL);
792 #ifdef PROFILE_FILE_CHOOSER
793 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
795 impl->local_only = TRUE;
796 impl->preview_widget_active = TRUE;
797 impl->use_preview_label = TRUE;
798 impl->select_multiple = FALSE;
799 impl->show_hidden = FALSE;
800 impl->show_size_column = FALSE;
801 impl->icon_size = FALLBACK_ICON_SIZE;
802 impl->load_state = LOAD_EMPTY;
803 impl->reload_state = RELOAD_EMPTY;
804 impl->pending_select_files = NULL;
805 impl->location_mode = LOCATION_MODE_PATH_BAR;
806 impl->operation_mode = OPERATION_MODE_BROWSE;
807 impl->recent_manager = gtk_recent_manager_get_default ();
809 gtk_box_set_spacing (GTK_BOX (impl), 12);
811 set_file_system_backend (impl);
813 profile_end ("end", NULL);
816 /* Frees the data columns for the specified iter in the shortcuts model*/
818 shortcuts_free_row_data (GtkFileChooserDefault *impl,
822 ShortcutType shortcut_type;
823 GCancellable *cancellable;
825 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
826 SHORTCUTS_COL_DATA, &col_data,
827 SHORTCUTS_COL_TYPE, &shortcut_type,
828 SHORTCUTS_COL_CANCELLABLE, &cancellable,
832 g_cancellable_cancel (cancellable);
834 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
835 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
839 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
841 GtkFileSystemVolume *volume;
844 _gtk_file_system_volume_free (volume);
850 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
853 g_object_unref (file);
857 /* Frees all the data columns in the shortcuts model */
859 shortcuts_free (GtkFileChooserDefault *impl)
863 if (!impl->shortcuts_model)
866 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
869 shortcuts_free_row_data (impl, &iter);
871 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
873 g_object_unref (impl->shortcuts_model);
874 impl->shortcuts_model = NULL;
878 pending_select_files_free (GtkFileChooserDefault *impl)
880 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
881 g_slist_free (impl->pending_select_files);
882 impl->pending_select_files = NULL;
886 pending_select_files_add (GtkFileChooserDefault *impl,
889 impl->pending_select_files =
890 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
893 /* Used from gtk_tree_selection_selected_foreach() */
895 store_selection_foreach (GtkTreeModel *model,
900 GtkFileChooserDefault *impl;
901 GtkTreeIter child_iter;
904 impl = GTK_FILE_CHOOSER_DEFAULT (data);
906 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
908 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
909 pending_select_files_add (impl, file);
912 /* Stores the current selection in the list of paths to select; this is used to
913 * preserve the selection when reloading the current folder.
916 pending_select_files_store_selection (GtkFileChooserDefault *impl)
918 GtkTreeSelection *selection;
920 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
921 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
925 gtk_file_chooser_default_finalize (GObject *object)
927 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
930 unset_file_system_backend (impl);
932 if (impl->shortcuts_pane_filter_model)
933 g_object_unref (impl->shortcuts_pane_filter_model);
935 if (impl->shortcuts_combo_filter_model)
936 g_object_unref (impl->shortcuts_combo_filter_model);
938 shortcuts_free (impl);
940 g_free (impl->browse_files_last_selected_name);
942 for (l = impl->filters; l; l = l->next)
944 GtkFileFilter *filter;
946 filter = GTK_FILE_FILTER (l->data);
947 g_object_unref (filter);
949 g_slist_free (impl->filters);
951 if (impl->current_filter)
952 g_object_unref (impl->current_filter);
954 if (impl->current_volume_file)
955 g_object_unref (impl->current_volume_file);
957 if (impl->current_folder)
958 g_object_unref (impl->current_folder);
960 if (impl->preview_file)
961 g_object_unref (impl->preview_file);
963 load_remove_timer (impl);
965 /* Free all the Models we have */
966 if (impl->browse_files_model)
967 g_object_unref (impl->browse_files_model);
969 if (impl->sort_model)
970 g_object_unref (impl->sort_model);
972 search_clear_model (impl, FALSE);
973 recent_clear_model (impl, FALSE);
975 g_free (impl->preview_display_name);
977 g_free (impl->edited_new_text);
979 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
982 /* Shows an error dialog set as transient for the specified window */
984 error_message_with_parent (GtkWindow *parent,
990 dialog = gtk_message_dialog_new (parent,
991 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
996 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
999 if (parent && parent->group)
1000 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1002 gtk_dialog_run (GTK_DIALOG (dialog));
1003 gtk_widget_destroy (dialog);
1006 /* Returns a toplevel GtkWindow, or NULL if none */
1008 get_toplevel (GtkWidget *widget)
1010 GtkWidget *toplevel;
1012 toplevel = gtk_widget_get_toplevel (widget);
1013 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1016 return GTK_WINDOW (toplevel);
1019 /* Shows an error dialog for the file chooser */
1021 error_message (GtkFileChooserDefault *impl,
1025 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1028 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1030 error_dialog (GtkFileChooserDefault *impl,
1041 uri = g_file_get_uri (file);
1042 text = g_strdup_printf (msg, uri);
1043 error_message (impl, text, error->message);
1046 g_error_free (error);
1050 /* Displays an error message about not being able to get information for a file.
1051 * Frees the GError as well.
1054 error_getting_info_dialog (GtkFileChooserDefault *impl,
1059 _("Could not retrieve information about the file"),
1063 /* Shows an error dialog about not being able to add a bookmark */
1065 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1070 _("Could not add a bookmark"),
1074 /* Shows an error dialog about not being able to remove a bookmark */
1076 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1081 _("Could not remove bookmark"),
1085 /* Shows an error dialog about not being able to create a folder */
1087 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1092 _("The folder could not be created"),
1096 /* Shows an error about not being able to create a folder because a file with
1097 * the same name is already there.
1100 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1105 _("The folder could not be created, as a file with the same "
1106 "name already exists. Try using a different name for the "
1107 "folder, or rename the file first."),
1111 /* Shows an error dialog about not being able to create a filename */
1113 error_building_filename_dialog (GtkFileChooserDefault *impl,
1116 error_dialog (impl, _("Invalid file name"),
1120 /* Shows an error dialog when we cannot switch to a folder */
1122 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1126 error_dialog (impl, _("The folder contents could not be displayed"),
1130 /* Changes folders, displaying an error dialog if this fails */
1132 change_folder_and_display_error (GtkFileChooserDefault *impl,
1134 gboolean clear_entry)
1139 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1141 /* We copy the path because of this case:
1143 * list_row_activated()
1144 * fetches path from model; path belongs to the model (*)
1145 * calls change_folder_and_display_error()
1146 * calls gtk_file_chooser_set_current_folder_file()
1147 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1151 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1154 error_changing_folder_dialog (impl, file, error);
1160 emit_default_size_changed (GtkFileChooserDefault *impl)
1162 profile_msg (" emit default-size-changed start", NULL);
1163 g_signal_emit_by_name (impl, "default-size-changed");
1164 profile_msg (" emit default-size-changed end", NULL);
1168 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1170 if (impl->use_preview_label)
1172 if (!impl->preview_label)
1174 impl->preview_label = gtk_label_new (impl->preview_display_name);
1175 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1176 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1177 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1178 gtk_widget_show (impl->preview_label);
1183 if (impl->preview_label)
1185 gtk_widget_destroy (impl->preview_label);
1186 impl->preview_label = NULL;
1190 if (impl->preview_widget_active && impl->preview_widget)
1191 gtk_widget_show (impl->preview_box);
1193 gtk_widget_hide (impl->preview_box);
1195 if (!GTK_WIDGET_MAPPED (impl))
1196 emit_default_size_changed (impl);
1200 set_preview_widget (GtkFileChooserDefault *impl,
1201 GtkWidget *preview_widget)
1203 if (preview_widget == impl->preview_widget)
1206 if (impl->preview_widget)
1207 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1208 impl->preview_widget);
1210 impl->preview_widget = preview_widget;
1211 if (impl->preview_widget)
1213 gtk_widget_show (impl->preview_widget);
1214 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1215 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1216 impl->preview_widget,
1217 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1220 update_preview_widget_visibility (impl);
1223 /* Renders a "Search" icon at an appropriate size for a tree view */
1225 render_search_icon (GtkFileChooserDefault *impl)
1227 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1231 render_recent_icon (GtkFileChooserDefault *impl)
1233 GtkIconTheme *theme;
1236 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1237 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1239 theme = gtk_icon_theme_get_default ();
1241 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1247 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1253 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1254 struct ReloadIconsData
1256 GtkFileChooserDefault *impl;
1257 GtkTreeRowReference *row_ref;
1261 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1263 const GError *error,
1269 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1270 struct ReloadIconsData *data = user_data;
1272 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1275 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1277 if (cancelled || error)
1280 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1282 path = gtk_tree_row_reference_get_path (data->row_ref);
1283 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1284 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1285 SHORTCUTS_COL_PIXBUF, pixbuf,
1287 gtk_tree_path_free (path);
1290 g_object_unref (pixbuf);
1293 gtk_tree_row_reference_free (data->row_ref);
1294 g_object_unref (data->impl);
1297 g_object_unref (cancellable);
1301 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1306 profile_start ("start", NULL);
1308 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1311 for (l = impl->reload_icon_cancellables; l; l = l->next)
1313 GCancellable *cancellable = G_CANCELLABLE (l->data);
1314 g_cancellable_cancel (cancellable);
1316 g_slist_free (impl->reload_icon_cancellables);
1317 impl->reload_icon_cancellables = NULL;
1322 ShortcutType shortcut_type;
1323 gboolean pixbuf_visible;
1326 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1327 SHORTCUTS_COL_DATA, &data,
1328 SHORTCUTS_COL_TYPE, &shortcut_type,
1329 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1335 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1337 GtkFileSystemVolume *volume;
1340 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1341 impl->icon_size, NULL);
1343 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1345 if (g_file_is_native (G_FILE (data)))
1348 struct ReloadIconsData *info;
1349 GtkTreePath *tree_path;
1350 GCancellable *cancellable;
1354 info = g_new0 (struct ReloadIconsData, 1);
1355 info->impl = g_object_ref (impl);
1356 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1357 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1358 gtk_tree_path_free (tree_path);
1360 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1362 shortcuts_reload_icons_get_info_cb,
1364 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1368 GtkIconTheme *icon_theme;
1370 /* Don't call get_info for remote paths to avoid latency and
1372 * If we switch to a better bookmarks file format (XBEL), we
1373 * should use mime info to get a better icon.
1375 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1376 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1377 impl->icon_size, 0, NULL);
1380 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1382 pixbuf = render_search_icon (impl);
1384 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1386 pixbuf = render_recent_icon (impl);
1389 gtk_list_store_set (impl->shortcuts_model, &iter,
1390 SHORTCUTS_COL_PIXBUF, pixbuf,
1394 g_object_unref (pixbuf);
1398 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1402 profile_end ("end", NULL);
1406 shortcuts_find_folder (GtkFileChooserDefault *impl,
1409 GtkTreeSelection *selection;
1413 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1415 g_assert (folder != NULL);
1416 pos = shortcut_find_position (impl, folder);
1419 gtk_tree_selection_unselect_all (selection);
1423 path = gtk_tree_path_new_from_indices (pos, -1);
1424 gtk_tree_selection_select_path (selection, path);
1425 gtk_tree_path_free (path);
1428 /* If a shortcut corresponds to the current folder, selects it */
1430 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1432 shortcuts_find_folder (impl, impl->current_folder);
1435 /* Removes the specified number of rows from the shortcuts list */
1437 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1443 path = gtk_tree_path_new_from_indices (start_row, -1);
1445 for (; n_rows; n_rows--)
1449 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1450 g_assert_not_reached ();
1452 shortcuts_free_row_data (impl, &iter);
1453 gtk_list_store_remove (impl->shortcuts_model, &iter);
1456 gtk_tree_path_free (path);
1460 shortcuts_update_count (GtkFileChooserDefault *impl,
1461 ShortcutsIndex type,
1466 case SHORTCUTS_HOME:
1468 impl->has_home = FALSE;
1470 impl->has_home = TRUE;
1473 case SHORTCUTS_DESKTOP:
1475 impl->has_desktop = FALSE;
1477 impl->has_desktop = TRUE;
1480 case SHORTCUTS_VOLUMES:
1481 impl->num_volumes += value;
1484 case SHORTCUTS_SHORTCUTS:
1485 impl->num_shortcuts += value;
1488 case SHORTCUTS_BOOKMARKS:
1489 impl->num_bookmarks += value;
1492 case SHORTCUTS_CURRENT_FOLDER:
1494 impl->shortcuts_current_folder_active = FALSE;
1496 impl->shortcuts_current_folder_active = TRUE;
1505 struct ShortcutsInsertRequest
1507 GtkFileChooserDefault *impl;
1511 GtkTreeRowReference *row_ref;
1512 ShortcutsIndex type;
1518 get_file_info_finished (GCancellable *cancellable,
1520 const GError *error,
1524 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1528 GCancellable *model_cancellable;
1529 struct ShortcutsInsertRequest *request = data;
1531 path = gtk_tree_row_reference_get_path (request->row_ref);
1533 /* Handle doesn't exist anymore in the model */
1536 pos = gtk_tree_path_get_indices (path)[0];
1537 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1539 gtk_tree_path_free (path);
1541 /* validate cancellable, else goto out */
1542 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1543 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1545 if (cancellable != model_cancellable)
1548 /* set the cancellable to NULL in the model (we unref later on) */
1549 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1550 SHORTCUTS_COL_CANCELLABLE, NULL,
1558 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1559 shortcuts_update_count (request->impl, request->type, -1);
1561 if (request->type == SHORTCUTS_HOME)
1565 home = g_file_new_for_path (g_get_home_dir ());
1566 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1567 g_object_unref (home);
1569 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1571 /* Remove the current folder separator */
1572 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1573 shortcuts_remove_rows (request->impl, separator_pos, 1);
1579 if (!request->label_copy)
1580 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1581 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1582 request->impl->icon_size);
1584 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1585 SHORTCUTS_COL_PIXBUF, pixbuf,
1586 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1587 SHORTCUTS_COL_NAME, request->label_copy,
1588 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1589 SHORTCUTS_COL_REMOVABLE, request->removable,
1592 if (request->impl->shortcuts_pane_filter_model)
1593 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1595 if (request->impl->shortcuts_combo_filter_model)
1596 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1598 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1599 request->impl->save_folder_combo != NULL)
1601 /* The current folder is updated via _activate_iter(), don't
1602 * have save_folder_combo_changed_cb() call _activate_iter()
1605 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1606 G_CALLBACK (save_folder_combo_changed_cb),
1609 if (request->impl->has_search)
1612 if (request->impl->has_recent)
1615 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1616 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1617 G_CALLBACK (save_folder_combo_changed_cb),
1622 g_object_unref (pixbuf);
1625 g_object_unref (request->impl);
1626 g_object_unref (request->file);
1627 gtk_tree_row_reference_free (request->row_ref);
1628 g_free (request->label_copy);
1631 g_object_unref (cancellable);
1634 /* FIXME: GtkFileSystem needs a function to split a remote path
1635 * into hostname and path components, or maybe just have a
1636 * gtk_file_system_path_get_display_name().
1638 * This function is also used in gtkfilechooserbutton.c
1641 _gtk_file_chooser_label_for_file (GFile *file)
1643 const gchar *path, *start, *end, *p;
1644 gchar *uri, *host, *label;
1646 uri = g_file_get_uri (file);
1648 start = strstr (uri, "://");
1652 path = strchr (start, '/');
1657 end = uri + strlen (uri);
1661 /* strip username */
1662 p = strchr (start, '@');
1666 p = strchr (start, ':');
1670 host = g_strndup (start, end - start);
1672 /* Translators: the first string is a path and the second string
1673 * is a hostname. Nautilus and the panel contain the same string
1676 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1682 label = g_strdup (uri);
1690 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1691 * inserts a volume. A position of -1 indicates the end of the tree.
1694 shortcuts_insert_file (GtkFileChooserDefault *impl,
1696 ShortcutType shortcut_type,
1697 GtkFileSystemVolume *volume,
1701 ShortcutsIndex type)
1704 GdkPixbuf *pixbuf = NULL;
1705 gpointer data = NULL;
1707 GtkIconTheme *icon_theme;
1709 profile_start ("start shortcut", NULL);
1711 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1714 label_copy = _gtk_file_system_volume_get_display_name (volume);
1715 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1716 impl->icon_size, NULL);
1718 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1720 if (g_file_is_native (file))
1722 struct ShortcutsInsertRequest *request;
1723 GCancellable *cancellable;
1726 request = g_new0 (struct ShortcutsInsertRequest, 1);
1727 request->impl = g_object_ref (impl);
1728 request->file = g_object_ref (file);
1729 request->name_only = TRUE;
1730 request->removable = removable;
1732 request->type = type;
1734 request->label_copy = g_strdup (label);
1737 gtk_list_store_append (impl->shortcuts_model, &iter);
1739 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1741 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1742 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1743 gtk_tree_path_free (p);
1745 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1746 "standard::is-hidden,standard::display-name,standard::icon",
1747 get_file_info_finished, request);
1749 gtk_list_store_set (impl->shortcuts_model, &iter,
1750 SHORTCUTS_COL_DATA, g_object_ref (file),
1751 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1752 SHORTCUTS_COL_CANCELLABLE, cancellable,
1755 shortcuts_update_count (impl, type, 1);
1761 /* Don't call get_info for remote paths to avoid latency and
1764 data = g_object_ref (file);
1766 label_copy = g_strdup (label);
1768 label_copy = _gtk_file_chooser_label_for_file (file);
1770 /* If we switch to a better bookmarks file format (XBEL), we
1771 * should use mime info to get a better icon.
1773 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1774 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1775 impl->icon_size, 0, NULL);
1780 g_assert_not_reached ();
1786 gtk_list_store_append (impl->shortcuts_model, &iter);
1788 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1790 shortcuts_update_count (impl, type, 1);
1792 gtk_list_store_set (impl->shortcuts_model, &iter,
1793 SHORTCUTS_COL_PIXBUF, pixbuf,
1794 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1795 SHORTCUTS_COL_NAME, label_copy,
1796 SHORTCUTS_COL_DATA, data,
1797 SHORTCUTS_COL_TYPE, shortcut_type,
1798 SHORTCUTS_COL_REMOVABLE, removable,
1799 SHORTCUTS_COL_CANCELLABLE, NULL,
1802 if (impl->shortcuts_pane_filter_model)
1803 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1805 if (impl->shortcuts_combo_filter_model)
1806 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1808 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1810 /* The current folder is updated via _activate_iter(), don't
1811 * have save_folder_combo_changed_cb() call _activate_iter()
1814 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1816 if (impl->has_search)
1819 if (impl->has_recent)
1822 g_signal_handlers_block_by_func (impl->save_folder_combo,
1823 G_CALLBACK (save_folder_combo_changed_cb),
1826 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1827 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1828 G_CALLBACK (save_folder_combo_changed_cb),
1832 g_free (label_copy);
1835 g_object_unref (pixbuf);
1837 profile_end ("end", NULL);
1841 shortcuts_append_search (GtkFileChooserDefault *impl)
1846 pixbuf = render_search_icon (impl);
1848 gtk_list_store_append (impl->shortcuts_model, &iter);
1849 gtk_list_store_set (impl->shortcuts_model, &iter,
1850 SHORTCUTS_COL_PIXBUF, pixbuf,
1851 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1852 SHORTCUTS_COL_NAME, _("Search"),
1853 SHORTCUTS_COL_DATA, NULL,
1854 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1855 SHORTCUTS_COL_REMOVABLE, FALSE,
1859 g_object_unref (pixbuf);
1861 impl->has_search = TRUE;
1865 shortcuts_append_recent (GtkFileChooserDefault *impl)
1870 pixbuf = render_recent_icon (impl);
1872 gtk_list_store_append (impl->shortcuts_model, &iter);
1873 gtk_list_store_set (impl->shortcuts_model, &iter,
1874 SHORTCUTS_COL_PIXBUF, pixbuf,
1875 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1876 SHORTCUTS_COL_NAME, _("Recently Used"),
1877 SHORTCUTS_COL_DATA, NULL,
1878 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1879 SHORTCUTS_COL_REMOVABLE, FALSE,
1883 g_object_unref (pixbuf);
1885 impl->has_recent = TRUE;
1888 /* Appends an item for the user's home directory to the shortcuts model */
1890 shortcuts_append_home (GtkFileChooserDefault *impl)
1892 const char *home_path;
1895 profile_start ("start", NULL);
1897 home_path = g_get_home_dir ();
1898 if (home_path == NULL)
1900 profile_end ("end - no home directory!?", NULL);
1904 home = g_file_new_for_path (home_path);
1905 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1906 impl->has_home = TRUE;
1908 g_object_unref (home);
1910 profile_end ("end", NULL);
1913 /* Appends the ~/Desktop directory to the shortcuts model */
1915 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1920 profile_start ("start", NULL);
1922 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1923 /* "To disable a directory, point it to the homedir."
1924 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1926 if (!g_strcmp0 (name, g_get_home_dir ()))
1928 profile_end ("end", NULL);
1932 file = g_file_new_for_path (name);
1933 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1934 impl->has_desktop = TRUE;
1936 /* We do not actually pop up an error dialog if there is no desktop directory
1937 * because some people may really not want to have one.
1940 g_object_unref (file);
1942 profile_end ("end", NULL);
1945 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1947 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1954 profile_start ("start", NULL);
1956 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1959 for (; bookmarks; bookmarks = bookmarks->next)
1963 file = bookmarks->data;
1965 if (impl->local_only && !g_file_is_native (file))
1968 if (shortcut_find_position (impl, file) != -1)
1971 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1973 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1979 profile_end ("end", NULL);
1981 return num_inserted;
1984 /* Returns the index for the corresponding item in the shortcuts bar */
1986 shortcuts_get_index (GtkFileChooserDefault *impl,
1987 ShortcutsIndex where)
1993 if (where == SHORTCUTS_SEARCH)
1996 n += impl->has_search ? 1 : 0;
1998 if (where == SHORTCUTS_RECENT)
2001 n += impl->has_recent ? 1 : 0;
2003 if (where == SHORTCUTS_RECENT_SEPARATOR)
2006 n += impl->has_recent ? 1 : 0;
2008 if (where == SHORTCUTS_HOME)
2011 n += impl->has_home ? 1 : 0;
2013 if (where == SHORTCUTS_DESKTOP)
2016 n += impl->has_desktop ? 1 : 0;
2018 if (where == SHORTCUTS_VOLUMES)
2021 n += impl->num_volumes;
2023 if (where == SHORTCUTS_SHORTCUTS)
2026 n += impl->num_shortcuts;
2028 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2031 /* If there are no bookmarks there won't be a separator */
2032 n += (impl->num_bookmarks > 0) ? 1 : 0;
2034 if (where == SHORTCUTS_BOOKMARKS)
2037 n += impl->num_bookmarks;
2039 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2044 if (where == SHORTCUTS_CURRENT_FOLDER)
2047 g_assert_not_reached ();
2054 /* Adds all the file system volumes to the shortcuts model */
2056 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2061 gboolean old_changing_folders;
2063 profile_start ("start", NULL);
2065 old_changing_folders = impl->changing_folder;
2066 impl->changing_folder = TRUE;
2068 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2069 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2070 impl->num_volumes = 0;
2072 list = _gtk_file_system_list_volumes (impl->file_system);
2076 for (l = list; l; l = l->next)
2078 GtkFileSystemVolume *volume;
2082 if (impl->local_only)
2084 if (_gtk_file_system_volume_is_mounted (volume))
2087 gboolean base_is_native = TRUE;
2089 base_file = _gtk_file_system_volume_get_root (volume);
2090 if (base_file != NULL)
2092 base_is_native = g_file_is_native (base_file);
2093 g_object_unref (base_file);
2096 if (!base_is_native)
2101 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2105 impl->num_volumes = n;
2106 g_slist_free (list);
2108 if (impl->shortcuts_pane_filter_model)
2109 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2111 if (impl->shortcuts_combo_filter_model)
2112 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2114 impl->changing_folder = old_changing_folders;
2116 profile_end ("end", NULL);
2119 /* Inserts a separator node in the shortcuts list */
2121 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2122 ShortcutsIndex where)
2126 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2127 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2128 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2130 gtk_list_store_insert (impl->shortcuts_model, &iter,
2131 shortcuts_get_index (impl, where));
2132 gtk_list_store_set (impl->shortcuts_model, &iter,
2133 SHORTCUTS_COL_PIXBUF, NULL,
2134 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2135 SHORTCUTS_COL_NAME, NULL,
2136 SHORTCUTS_COL_DATA, NULL,
2137 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2141 /* Updates the list of bookmarks */
2143 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2146 gboolean old_changing_folders;
2148 GFile *list_selected = NULL;
2149 GFile *combo_selected = NULL;
2150 ShortcutType shortcut_type;
2153 profile_start ("start", NULL);
2155 old_changing_folders = impl->changing_folder;
2156 impl->changing_folder = TRUE;
2158 if (shortcuts_get_selected (impl, &iter))
2160 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2162 SHORTCUTS_COL_DATA, &col_data,
2163 SHORTCUTS_COL_TYPE, &shortcut_type,
2166 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2167 list_selected = g_object_ref (col_data);
2170 if (impl->save_folder_combo &&
2171 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2174 GtkTreeIter child_iter;
2176 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2179 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2181 SHORTCUTS_COL_DATA, &col_data,
2182 SHORTCUTS_COL_TYPE, &shortcut_type,
2185 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2186 combo_selected = g_object_ref (col_data);
2189 if (impl->num_bookmarks > 0)
2190 shortcuts_remove_rows (impl,
2191 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2192 impl->num_bookmarks + 1);
2194 impl->num_bookmarks = 0;
2195 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2197 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2198 shortcuts_append_bookmarks (impl, bookmarks);
2199 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2200 g_slist_free (bookmarks);
2202 if (impl->num_bookmarks == 0)
2203 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2205 if (impl->shortcuts_pane_filter_model)
2206 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2208 if (impl->shortcuts_combo_filter_model)
2209 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2213 shortcuts_find_folder (impl, list_selected);
2214 g_object_unref (list_selected);
2221 pos = shortcut_find_position (impl, combo_selected);
2224 if (impl->has_search)
2227 if (impl->has_recent)
2230 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2233 g_object_unref (combo_selected);
2236 impl->changing_folder = old_changing_folders;
2238 profile_end ("end", NULL);
2241 /* Appends a separator and a row to the shortcuts list for the current folder */
2243 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2248 g_assert (!impl->shortcuts_current_folder_active);
2252 g_assert (impl->current_folder != NULL);
2254 pos = shortcut_find_position (impl, impl->current_folder);
2257 GtkFileSystemVolume *volume;
2262 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2266 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2268 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2270 base_file = _gtk_file_system_volume_get_root (volume);
2274 if (base_file && g_file_equal (base_file, impl->current_folder))
2275 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2277 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2280 g_object_unref (base_file);
2282 else if (impl->save_folder_combo != NULL)
2284 if (impl->has_search)
2287 if (impl->has_recent)
2288 pos -= 2; /* + separator */
2290 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2294 /* Updates the current folder row in the shortcuts model */
2296 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2300 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2302 if (impl->shortcuts_current_folder_active)
2304 shortcuts_remove_rows (impl, pos, 2);
2305 impl->shortcuts_current_folder_active = FALSE;
2308 shortcuts_add_current_folder (impl);
2311 /* Filter function used for the shortcuts filter model */
2313 shortcuts_pane_filter_cb (GtkTreeModel *model,
2317 GtkFileChooserDefault *impl;
2321 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2323 path = gtk_tree_model_get_path (model, iter);
2327 pos = *gtk_tree_path_get_indices (path);
2328 gtk_tree_path_free (path);
2330 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2333 /* Creates the list model for shortcuts */
2335 shortcuts_model_create (GtkFileChooserDefault *impl)
2337 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2338 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2339 GDK_TYPE_PIXBUF, /* pixbuf */
2340 G_TYPE_STRING, /* name */
2341 G_TYPE_POINTER, /* path or volume */
2342 G_TYPE_INT, /* ShortcutType */
2343 G_TYPE_BOOLEAN, /* removable */
2344 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2345 G_TYPE_POINTER); /* GCancellable */
2347 if (search_is_possible (impl))
2349 shortcuts_append_search (impl);
2352 if (impl->recent_manager)
2354 shortcuts_append_recent (impl);
2355 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2358 if (impl->file_system)
2360 shortcuts_append_home (impl);
2361 shortcuts_append_desktop (impl);
2362 shortcuts_add_volumes (impl);
2365 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2366 GTK_TREE_MODEL (impl->shortcuts_model),
2369 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2370 shortcuts_pane_filter_cb,
2375 /* Callback used when the "New Folder" button is clicked */
2377 new_folder_button_clicked (GtkButton *button,
2378 GtkFileChooserDefault *impl)
2383 if (!impl->browse_files_model)
2384 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2386 /* Prevent button from being clicked twice */
2387 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2389 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2391 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2392 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2393 path, impl->list_name_column,
2396 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2397 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2399 impl->list_name_column,
2402 gtk_tree_path_free (path);
2405 /* Idle handler for creating a new folder after editing its name cell, or for
2406 * canceling the editing.
2409 edited_idle_cb (GtkFileChooserDefault *impl)
2411 GDK_THREADS_ENTER ();
2413 g_source_destroy (impl->edited_idle);
2414 impl->edited_idle = NULL;
2416 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2417 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2419 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2421 if (impl->edited_new_text) /* not cancelled? */
2423 GError *error = NULL;
2426 file = g_file_get_child_for_display_name (impl->current_folder,
2427 impl->edited_new_text,
2431 GError *error = NULL;
2433 if (g_file_make_directory (file, NULL, &error))
2434 change_folder_and_display_error (impl, file, FALSE);
2436 error_creating_folder_dialog (impl, file, error);
2438 g_object_unref (file);
2441 error_creating_folder_dialog (impl, file, error);
2443 g_free (impl->edited_new_text);
2444 impl->edited_new_text = NULL;
2447 GDK_THREADS_LEAVE ();
2453 queue_edited_idle (GtkFileChooserDefault *impl,
2454 const gchar *new_text)
2456 /* We create the folder in an idle handler so that we don't modify the tree
2460 if (!impl->edited_idle)
2462 impl->edited_idle = g_idle_source_new ();
2463 g_source_set_closure (impl->edited_idle,
2464 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2466 g_source_attach (impl->edited_idle, NULL);
2469 g_free (impl->edited_new_text);
2470 impl->edited_new_text = g_strdup (new_text);
2473 /* Callback used from the text cell renderer when the new folder is named */
2475 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2477 const gchar *new_text,
2478 GtkFileChooserDefault *impl)
2480 /* work around bug #154921 */
2481 g_object_set (cell_renderer_text,
2482 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2483 queue_edited_idle (impl, new_text);
2486 /* Callback used from the text cell renderer when the new folder edition gets
2490 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2491 GtkFileChooserDefault *impl)
2493 /* work around bug #154921 */
2494 g_object_set (cell_renderer_text,
2495 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2496 queue_edited_idle (impl, NULL);
2499 /* Creates the widgets for the filter combo box */
2501 filter_create (GtkFileChooserDefault *impl)
2503 impl->filter_combo = gtk_combo_box_new_text ();
2504 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2506 g_signal_connect (impl->filter_combo, "changed",
2507 G_CALLBACK (filter_combo_changed), impl);
2509 gtk_widget_set_tooltip_text (impl->filter_combo,
2510 _("Select which types of files are shown"));
2512 return impl->filter_combo;
2516 button_new (GtkFileChooserDefault *impl,
2518 const char *stock_id,
2526 button = gtk_button_new_with_mnemonic (text);
2527 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2528 gtk_button_set_image (GTK_BUTTON (button), image);
2530 gtk_widget_set_sensitive (button, sensitive);
2531 g_signal_connect (button, "clicked", callback, impl);
2534 gtk_widget_show (button);
2539 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2541 shortcut_find_position (GtkFileChooserDefault *impl,
2546 int current_folder_separator_idx;
2548 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2551 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2554 /* FIXME: is this still needed? */
2555 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2559 for (i = 0; i < current_folder_separator_idx; i++)
2562 ShortcutType shortcut_type;
2564 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2565 SHORTCUTS_COL_DATA, &col_data,
2566 SHORTCUTS_COL_TYPE, &shortcut_type,
2571 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2573 GtkFileSystemVolume *volume;
2578 base_file = _gtk_file_system_volume_get_root (volume);
2580 exists = base_file && g_file_equal (file, base_file);
2583 g_object_unref (base_file);
2588 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2592 model_file = col_data;
2594 if (model_file && g_file_equal (model_file, file))
2599 if (i < current_folder_separator_idx - 1)
2601 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2602 g_assert_not_reached ();
2609 /* Tries to add a bookmark from a path name */
2611 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2617 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2619 if (shortcut_find_position (impl, file) != -1)
2623 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2625 error_adding_bookmark_dialog (impl, file, error);
2633 add_bookmark_foreach_cb (GtkTreeModel *model,
2638 GtkFileChooserDefault *impl;
2639 GtkFileSystemModel *fs_model;
2640 GtkTreeIter child_iter;
2643 impl = (GtkFileChooserDefault *) data;
2645 switch (impl->operation_mode)
2647 case OPERATION_MODE_BROWSE:
2648 fs_model = impl->browse_files_model;
2649 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2650 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2653 case OPERATION_MODE_SEARCH:
2654 search_get_valid_child_iter (impl, &child_iter, iter);
2655 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2656 SEARCH_MODEL_COL_FILE, &file,
2660 case OPERATION_MODE_RECENT:
2661 recent_get_valid_child_iter (impl, &child_iter, iter);
2662 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2663 RECENT_MODEL_COL_FILE, &file,
2668 shortcuts_add_bookmark_from_file (impl, file, -1);
2671 /* Adds a bookmark from the currently selected item in the file list */
2673 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2675 GtkTreeSelection *selection;
2677 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2679 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2680 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2682 gtk_tree_selection_selected_foreach (selection,
2683 add_bookmark_foreach_cb,
2687 /* Callback used when the "Add bookmark" button is clicked */
2689 add_bookmark_button_clicked_cb (GtkButton *button,
2690 GtkFileChooserDefault *impl)
2692 bookmarks_add_selected_folder (impl);
2695 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2696 * returns FALSE if no shortcut is selected.
2699 shortcuts_get_selected (GtkFileChooserDefault *impl,
2702 GtkTreeSelection *selection;
2703 GtkTreeIter parent_iter;
2705 if (!impl->browse_shortcuts_tree_view)
2708 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2710 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2713 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2719 /* Removes the selected bookmarks */
2721 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2729 if (!shortcuts_get_selected (impl, &iter))
2732 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2733 SHORTCUTS_COL_DATA, &col_data,
2734 SHORTCUTS_COL_REMOVABLE, &removable,
2740 g_assert (col_data != NULL);
2745 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2746 error_removing_bookmark_dialog (impl, file, error);
2749 /* Callback used when the "Remove bookmark" button is clicked */
2751 remove_bookmark_button_clicked_cb (GtkButton *button,
2752 GtkFileChooserDefault *impl)
2754 remove_selected_bookmarks (impl);
2757 struct selection_check_closure {
2758 GtkFileChooserDefault *impl;
2761 gboolean all_folders;
2764 /* Used from gtk_tree_selection_selected_foreach() */
2766 selection_check_foreach_cb (GtkTreeModel *model,
2771 struct selection_check_closure *closure;
2772 GtkTreeIter child_iter;
2777 closure->num_selected++;
2779 switch (closure->impl->operation_mode)
2781 case OPERATION_MODE_BROWSE:
2782 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2783 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2784 is_folder = info ? (_gtk_file_info_consider_as_directory (info)) : FALSE;
2787 case OPERATION_MODE_SEARCH:
2788 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2789 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2790 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2794 case OPERATION_MODE_RECENT:
2795 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2796 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2797 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2802 closure->all_folders = closure->all_folders && is_folder;
2803 closure->all_files = closure->all_files && !is_folder;
2806 /* Checks whether the selected items in the file list are all files or all folders */
2808 selection_check (GtkFileChooserDefault *impl,
2810 gboolean *all_files,
2811 gboolean *all_folders)
2813 struct selection_check_closure closure;
2814 GtkTreeSelection *selection;
2816 closure.impl = impl;
2817 closure.num_selected = 0;
2818 closure.all_files = TRUE;
2819 closure.all_folders = TRUE;
2821 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2822 gtk_tree_selection_selected_foreach (selection,
2823 selection_check_foreach_cb,
2826 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2829 *num_selected = closure.num_selected;
2832 *all_files = closure.all_files;
2835 *all_folders = closure.all_folders;
2838 struct get_selected_file_closure {
2839 GtkFileChooserDefault *impl;
2844 get_selected_file_foreach_cb (GtkTreeModel *model,
2849 struct get_selected_file_closure *closure;
2850 GtkTreeIter child_iter;
2854 switch (closure->impl->operation_mode)
2856 case OPERATION_MODE_BROWSE:
2857 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2858 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2861 case OPERATION_MODE_SEARCH:
2862 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2863 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2864 SEARCH_MODEL_COL_FILE, &closure->file,
2868 case OPERATION_MODE_RECENT:
2869 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2870 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2871 RECENT_MODEL_COL_FILE, &closure->file,
2877 /* Returns a selected path from the file list */
2879 get_selected_file (GtkFileChooserDefault *impl)
2881 struct get_selected_file_closure closure;
2882 GtkTreeSelection *selection;
2884 closure.impl = impl;
2885 closure.file = NULL;
2887 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2888 gtk_tree_selection_selected_foreach (selection,
2889 get_selected_file_foreach_cb,
2892 return closure.file;
2896 GtkFileChooserDefault *impl;
2898 } UpdateTooltipData;
2901 update_tooltip (GtkTreeModel *model,
2906 UpdateTooltipData *udata = data;
2907 GtkTreeIter child_iter;
2910 if (udata->tip == NULL)
2912 const gchar *display_name;
2914 switch (udata->impl->operation_mode)
2916 case OPERATION_MODE_BROWSE:
2917 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2920 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2921 display_name = g_file_info_get_display_name (info);
2924 case OPERATION_MODE_SEARCH:
2925 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2926 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2927 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2931 case OPERATION_MODE_RECENT:
2932 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2933 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2934 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2939 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2945 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2946 * if there are no selected items *and* the current folder is not in the
2947 * bookmarks list. De-sensitize the button otherwise.
2950 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2953 gboolean all_folders;
2957 selection_check (impl, &num_selected, NULL, &all_folders);
2959 if (num_selected == 0)
2960 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2961 else if (num_selected == 1)
2965 file = get_selected_file (impl);
2966 active = all_folders && (shortcut_find_position (impl, file) == -1);
2969 active = all_folders;
2971 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2973 if (impl->browse_files_popup_menu_add_shortcut_item)
2974 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2975 (num_selected == 0) ? FALSE : active);
2979 if (num_selected == 0)
2980 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2981 else if (num_selected > 1)
2982 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2985 GtkTreeSelection *selection;
2986 UpdateTooltipData data;
2988 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2991 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2995 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
3000 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
3001 * bookmark row is selected in the shortcuts tree.
3004 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
3007 gboolean removable = FALSE;
3010 if (shortcuts_get_selected (impl, &iter))
3011 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3012 SHORTCUTS_COL_REMOVABLE, &removable,
3013 SHORTCUTS_COL_NAME, &name,
3016 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3022 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3023 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3031 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3034 gboolean removable = FALSE;
3036 if (impl->browse_shortcuts_popup_menu == NULL)
3039 if (shortcuts_get_selected (impl, &iter))
3040 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3041 SHORTCUTS_COL_REMOVABLE, &removable,
3044 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3045 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3048 /* GtkWidget::drag-begin handler for the shortcuts list. */
3050 shortcuts_drag_begin_cb (GtkWidget *widget,
3051 GdkDragContext *context,
3052 GtkFileChooserDefault *impl)
3055 impl->shortcuts_drag_context = g_object_ref (context);
3060 /* Removes the idle handler for outside drags */
3062 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3064 if (!impl->shortcuts_drag_outside_idle)
3067 g_source_destroy (impl->shortcuts_drag_outside_idle);
3068 impl->shortcuts_drag_outside_idle = NULL;
3072 /* GtkWidget::drag-end handler for the shortcuts list. */
3074 shortcuts_drag_end_cb (GtkWidget *widget,
3075 GdkDragContext *context,
3076 GtkFileChooserDefault *impl)
3079 g_object_unref (impl->shortcuts_drag_context);
3081 shortcuts_cancel_drag_outside_idle (impl);
3083 if (!impl->shortcuts_drag_outside)
3086 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3088 impl->shortcuts_drag_outside = FALSE;
3092 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3094 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3095 GdkDragContext *context,
3096 GtkFileChooserDefault *impl)
3098 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3102 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3106 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3109 GtkTreeView *tree_view;
3112 GdkPixmap *row_pixmap;
3117 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3119 /* Find the selected path and get its drag pixmap */
3121 if (!shortcuts_get_selected (impl, &iter))
3122 g_assert_not_reached ();
3124 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3126 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3127 gtk_tree_path_free (path);
3136 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3142 GdkPixmap *composite;
3143 int row_pixmap_width, row_pixmap_height;
3144 int pixbuf_width, pixbuf_height;
3145 int composite_width, composite_height;
3146 int pixbuf_x, pixbuf_y;
3147 GdkGC *gc, *mask_gc;
3149 GdkBitmap *pixbuf_mask;
3151 /* Create pixmap and mask for composite image */
3153 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3154 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3155 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3157 composite_width = MAX (row_pixmap_width, pixbuf_width);
3158 composite_height = MAX (row_pixmap_height, pixbuf_height);
3160 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3162 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3165 pixbuf_x = composite_width - pixbuf_width;
3167 pixbuf_y = (composite_height - pixbuf_height) / 2;
3169 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3170 gc = gdk_gc_new (composite);
3172 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3173 mask_gc = gdk_gc_new (mask);
3175 gdk_gc_set_foreground (mask_gc, &color);
3176 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3179 color.green = 0xffff;
3180 color.blue = 0xffff;
3181 gdk_gc_set_rgb_fg_color (gc, &color);
3182 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3184 /* Composite the row pixmap and the pixbuf */
3186 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3188 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3189 NULL, &pixbuf_mask, 128);
3190 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3193 pixbuf_width, pixbuf_height);
3194 g_object_unref (pixbuf_mask);
3196 gdk_draw_drawable (composite, gc, row_pixmap,
3199 row_pixmap_width, row_pixmap_height);
3201 gdk_gc_set_foreground (mask_gc, &color);
3202 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3204 gdk_draw_pixbuf (composite, gc, pixbuf,
3207 pixbuf_width, pixbuf_height,
3211 g_object_unref (pixbuf);
3212 g_object_unref (row_pixmap);
3214 row_pixmap = composite;
3218 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3220 gtk_tree_view_get_path_at_pos (tree_view,
3221 tree_view->priv->press_start_x,
3222 tree_view->priv->press_start_y,
3228 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3229 gdk_drawable_get_colormap (row_pixmap),
3232 tree_view->priv->press_start_x + 1,
3233 row_pixmap_y + cell_y + 1);
3235 g_object_unref (row_pixmap);
3237 g_object_unref (mask);
3240 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3241 * handler so that we can tell apart the drag_leave event that comes right
3242 * before a drag_drop, from a normal drag_leave. We don't want to set the
3243 * cursor nor the flag in the latter case.
3246 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3248 GDK_THREADS_ENTER ();
3250 shortcuts_drag_set_delete_cursor (impl, TRUE);
3251 impl->shortcuts_drag_outside = TRUE;
3253 shortcuts_cancel_drag_outside_idle (impl);
3255 GDK_THREADS_LEAVE ();
3261 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3265 shortcuts_drag_leave_cb (GtkWidget *widget,
3266 GdkDragContext *context,
3268 GtkFileChooserDefault *impl)
3271 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3273 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3274 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3275 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3277 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3281 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3283 GTK_TREE_VIEW_DROP_BEFORE);
3285 g_signal_stop_emission_by_name (widget, "drag-leave");
3288 /* Computes the appropriate row and position for dropping */
3290 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3294 GtkTreeViewDropPosition *pos)
3296 GtkTreeView *tree_view;
3297 GtkTreeViewColumn *column;
3301 int bookmarks_index;
3303 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3305 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3307 if (!gtk_tree_view_get_path_at_pos (tree_view,
3309 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3315 row = bookmarks_index + impl->num_bookmarks - 1;
3316 *path = gtk_tree_path_new_from_indices (row, -1);
3317 *pos = GTK_TREE_VIEW_DROP_AFTER;
3321 row = *gtk_tree_path_get_indices (*path);
3322 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3323 gtk_tree_path_free (*path);
3325 if (row < bookmarks_index)
3327 row = bookmarks_index;
3328 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3330 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3332 row = bookmarks_index + impl->num_bookmarks - 1;
3333 *pos = GTK_TREE_VIEW_DROP_AFTER;
3337 if (cell_y < cell.height / 2)
3338 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3340 *pos = GTK_TREE_VIEW_DROP_AFTER;
3343 *path = gtk_tree_path_new_from_indices (row, -1);
3346 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3347 * implement the destination side of DnD by hand, due to limitations in
3348 * GtkTreeView's DnD API.
3351 shortcuts_drag_motion_cb (GtkWidget *widget,
3352 GdkDragContext *context,
3356 GtkFileChooserDefault *impl)
3359 GtkTreeViewDropPosition pos;
3360 GdkDragAction action;
3363 if (gtk_drag_get_source_widget (context) == widget)
3365 shortcuts_cancel_drag_outside_idle (impl);
3367 if (impl->shortcuts_drag_outside)
3369 shortcuts_drag_set_delete_cursor (impl, FALSE);
3370 impl->shortcuts_drag_outside = FALSE;
3375 if (context->suggested_action == GDK_ACTION_COPY ||
3376 (context->actions & GDK_ACTION_COPY) != 0)
3377 action = GDK_ACTION_COPY;
3378 else if (context->suggested_action == GDK_ACTION_MOVE ||
3379 (context->actions & GDK_ACTION_MOVE) != 0)
3380 action = GDK_ACTION_MOVE;
3387 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3388 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3389 gtk_tree_path_free (path);
3393 g_signal_stop_emission_by_name (widget, "drag-motion");
3397 gdk_drag_status (context, action, time_);
3404 /* GtkWidget::drag-drop handler for the shortcuts list. */
3406 shortcuts_drag_drop_cb (GtkWidget *widget,
3407 GdkDragContext *context,
3411 GtkFileChooserDefault *impl)
3414 shortcuts_cancel_drag_outside_idle (impl);
3417 g_signal_stop_emission_by_name (widget, "drag-drop");
3421 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3423 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3424 GtkSelectionData *selection_data,
3430 uris = gtk_selection_data_get_uris (selection_data);
3434 for (i = 0; uris[i]; i++)
3440 file = g_file_new_for_uri (uri);
3442 if (shortcuts_add_bookmark_from_file (impl, file, position))
3445 g_object_unref (file);
3451 /* Reorders the selected bookmark to the specified position */
3453 shortcuts_reorder (GtkFileChooserDefault *impl,
3458 ShortcutType shortcut_type;
3461 int bookmarks_index;
3466 /* Get the selected path */
3468 if (!shortcuts_get_selected (impl, &iter))
3469 g_assert_not_reached ();
3471 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3472 old_position = *gtk_tree_path_get_indices (path);
3473 gtk_tree_path_free (path);
3475 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3476 old_position -= bookmarks_index;
3477 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3479 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3480 SHORTCUTS_COL_NAME, &name,
3481 SHORTCUTS_COL_DATA, &col_data,
3482 SHORTCUTS_COL_TYPE, &shortcut_type,
3484 g_assert (col_data != NULL);
3485 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3488 g_object_ref (file); /* removal below will free file, so we need a new ref */
3490 /* Remove the path from the old position and insert it in the new one */
3492 if (new_position > old_position)
3495 if (old_position == new_position)
3499 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3501 shortcuts_add_bookmark_from_file (impl, file, new_position);
3502 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3505 error_adding_bookmark_dialog (impl, file, error);
3509 g_object_unref (file);
3512 /* Callback used when we get the drag data for the bookmarks list. We add the
3513 * received URIs as bookmarks if they are folders.
3516 shortcuts_drag_data_received_cb (GtkWidget *widget,
3517 GdkDragContext *context,
3520 GtkSelectionData *selection_data,
3525 GtkFileChooserDefault *impl;
3526 GtkTreePath *tree_path;
3527 GtkTreeViewDropPosition tree_pos;
3529 int bookmarks_index;
3531 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3533 /* Compute position */
3535 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3537 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3538 position = *gtk_tree_path_get_indices (tree_path);
3539 gtk_tree_path_free (tree_path);
3541 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3544 g_assert (position >= bookmarks_index);
3545 position -= bookmarks_index;
3547 if (gtk_targets_include_uri (&selection_data->target, 1))
3548 shortcuts_drop_uris (impl, selection_data, position);
3549 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3550 shortcuts_reorder (impl, position);
3552 g_signal_stop_emission_by_name (widget, "drag-data-received");
3555 /* Callback used to display a tooltip in the shortcuts tree */
3557 shortcuts_query_tooltip_cb (GtkWidget *widget,
3560 gboolean keyboard_mode,
3561 GtkTooltip *tooltip,
3562 GtkFileChooserDefault *impl)
3564 GtkTreeModel *model;
3567 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3575 ShortcutType shortcut_type;
3577 gtk_tree_model_get (model, &iter,
3578 SHORTCUTS_COL_DATA, &col_data,
3579 SHORTCUTS_COL_TYPE, &shortcut_type,
3582 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3584 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3588 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3593 file = G_FILE (col_data);
3594 parse_name = g_file_get_parse_name (file);
3596 gtk_tooltip_set_text (tooltip, parse_name);
3598 g_free (parse_name);
3602 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3606 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3616 /* Callback used when the selection in the shortcuts tree changes */
3618 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3619 GtkFileChooserDefault *impl)
3622 GtkTreeIter child_iter;
3624 bookmarks_check_remove_sensitivity (impl);
3625 shortcuts_check_popup_sensitivity (impl);
3627 if (impl->changing_folder)
3630 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3632 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3635 shortcuts_activate_iter (impl, &child_iter);
3640 shortcuts_row_separator_func (GtkTreeModel *model,
3644 ShortcutType shortcut_type;
3646 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3648 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3651 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3652 * keypresses before the TreeView gets them.
3655 tree_view_keybinding_cb (GtkWidget *tree_view,
3657 GtkFileChooserDefault *impl)
3659 if ((event->keyval == GDK_slash
3660 || event->keyval == GDK_KP_Divide
3662 || event->keyval == GDK_asciitilde
3664 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3666 location_popup_handler (impl, event->string);
3673 /* Callback used when the file list's popup menu is detached */
3675 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3678 GtkFileChooserDefault *impl;
3680 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3681 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3683 impl->browse_shortcuts_popup_menu = NULL;
3684 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3685 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3689 remove_shortcut_cb (GtkMenuItem *item,
3690 GtkFileChooserDefault *impl)
3692 remove_selected_bookmarks (impl);
3695 /* Rename the selected bookmark */
3697 rename_selected_bookmark (GtkFileChooserDefault *impl)
3701 GtkTreeViewColumn *column;
3702 GtkCellRenderer *cell;
3705 if (shortcuts_get_selected (impl, &iter))
3707 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3708 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3709 renderers = gtk_tree_view_column_get_cell_renderers (column);
3710 cell = g_list_nth_data (renderers, 1);
3711 g_list_free (renderers);
3712 g_object_set (cell, "editable", TRUE, NULL);
3713 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3714 path, column, cell, TRUE);
3715 gtk_tree_path_free (path);
3720 rename_shortcut_cb (GtkMenuItem *item,
3721 GtkFileChooserDefault *impl)
3723 rename_selected_bookmark (impl);
3726 /* Constructs the popup menu for the file list if needed */
3728 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3732 if (impl->browse_shortcuts_popup_menu)
3735 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3736 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3737 impl->browse_shortcuts_tree_view,
3738 shortcuts_popup_menu_detach_cb);
3740 item = gtk_image_menu_item_new_with_label (_("Remove"));
3741 impl->browse_shortcuts_popup_menu_remove_item = item;
3742 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3743 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3744 g_signal_connect (item, "activate",
3745 G_CALLBACK (remove_shortcut_cb), impl);
3746 gtk_widget_show (item);
3747 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3749 item = gtk_menu_item_new_with_label (_("Rename..."));
3750 impl->browse_shortcuts_popup_menu_rename_item = item;
3751 g_signal_connect (item, "activate",
3752 G_CALLBACK (rename_shortcut_cb), impl);
3753 gtk_widget_show (item);
3754 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3758 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3760 shortcuts_build_popup_menu (impl);
3761 shortcuts_check_popup_sensitivity (impl);
3765 popup_position_func (GtkMenu *menu,
3769 gpointer user_data);
3772 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3773 GdkEventButton *event)
3775 shortcuts_update_popup_menu (impl);
3777 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3778 NULL, NULL, NULL, NULL,
3779 event->button, event->time);
3782 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3784 popup_position_func, impl->browse_shortcuts_tree_view,
3785 0, GDK_CURRENT_TIME);
3786 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3791 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3793 shortcuts_popup_menu_cb (GtkWidget *widget,
3794 GtkFileChooserDefault *impl)
3796 shortcuts_popup_menu (impl, NULL);
3800 /* Callback used when a button is pressed on the shortcuts list.
3801 * We trap button 3 to bring up a popup menu.
3804 shortcuts_button_press_event_cb (GtkWidget *widget,
3805 GdkEventButton *event,
3806 GtkFileChooserDefault *impl)
3808 static gboolean in_press = FALSE;
3814 if (event->button != 3)
3818 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3824 shortcuts_popup_menu (impl, event);
3829 shortcuts_edited (GtkCellRenderer *cell,
3832 GtkFileChooserDefault *impl)
3838 g_object_set (cell, "editable", FALSE, NULL);
3840 path = gtk_tree_path_new_from_string (path_string);
3841 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3842 g_assert_not_reached ();
3844 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3845 SHORTCUTS_COL_DATA, &shortcut,
3847 gtk_tree_path_free (path);
3849 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3853 shortcuts_editing_canceled (GtkCellRenderer *cell,
3854 GtkFileChooserDefault *impl)
3856 g_object_set (cell, "editable", FALSE, NULL);
3859 /* Creates the widgets for the shortcuts and bookmarks tree */
3861 shortcuts_list_create (GtkFileChooserDefault *impl)
3864 GtkTreeSelection *selection;
3865 GtkTreeViewColumn *column;
3866 GtkCellRenderer *renderer;
3868 /* Target types for dragging a row to/from the shortcuts list */
3869 const GtkTargetEntry tree_model_row_targets[] = {
3870 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3873 /* Scrolled window */
3875 swin = gtk_scrolled_window_new (NULL, NULL);
3876 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3877 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3878 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3880 gtk_widget_show (swin);
3884 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3885 #ifdef PROFILE_FILE_CHOOSER
3886 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3888 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3889 G_CALLBACK (tree_view_keybinding_cb), impl);
3890 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3891 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3892 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3893 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3894 /* Accessible object name for the file chooser's shortcuts pane */
3895 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3897 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3899 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3901 tree_model_row_targets,
3902 G_N_ELEMENTS (tree_model_row_targets),
3905 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3906 GTK_DEST_DEFAULT_ALL,
3907 tree_model_row_targets,
3908 G_N_ELEMENTS (tree_model_row_targets),
3909 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3910 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3912 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3913 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3914 gtk_tree_selection_set_select_function (selection,
3915 shortcuts_select_func,
3918 g_signal_connect (selection, "changed",
3919 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3921 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3922 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3924 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3925 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3926 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3927 G_CALLBACK (shortcuts_drag_end_cb), impl);
3928 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3929 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3931 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3932 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3933 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3934 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3935 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3936 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3937 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3938 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3940 /* Support tooltips */
3941 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3942 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3943 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3945 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3946 gtk_widget_show (impl->browse_shortcuts_tree_view);
3950 column = gtk_tree_view_column_new ();
3951 /* Column header for the file chooser's shortcuts pane */
3952 gtk_tree_view_column_set_title (column, _("_Places"));
3954 renderer = gtk_cell_renderer_pixbuf_new ();
3955 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3956 gtk_tree_view_column_set_attributes (column, renderer,
3957 "pixbuf", SHORTCUTS_COL_PIXBUF,
3958 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3961 renderer = gtk_cell_renderer_text_new ();
3962 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3963 g_signal_connect (renderer, "edited",
3964 G_CALLBACK (shortcuts_edited), impl);
3965 g_signal_connect (renderer, "editing-canceled",
3966 G_CALLBACK (shortcuts_editing_canceled), impl);
3967 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3968 gtk_tree_view_column_set_attributes (column, renderer,
3969 "text", SHORTCUTS_COL_NAME,
3972 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3973 shortcuts_row_separator_func,
3976 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3981 /* Creates the widgets for the shortcuts/bookmarks pane */
3983 shortcuts_pane_create (GtkFileChooserDefault *impl,
3984 GtkSizeGroup *size_group)
3990 vbox = gtk_vbox_new (FALSE, 6);
3991 gtk_widget_show (vbox);
3993 /* Shortcuts tree */
3995 widget = shortcuts_list_create (impl);
3996 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3998 /* Box for buttons */
4000 hbox = gtk_hbox_new (TRUE, 6);
4001 gtk_size_group_add_widget (size_group, hbox);
4002 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4003 gtk_widget_show (hbox);
4005 /* Add bookmark button */
4007 impl->browse_shortcuts_add_button = button_new (impl,
4012 G_CALLBACK (add_bookmark_button_clicked_cb));
4013 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4014 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4015 _("Add the selected folder to the Bookmarks"));
4017 /* Remove bookmark button */
4019 impl->browse_shortcuts_remove_button = button_new (impl,
4024 G_CALLBACK (remove_bookmark_button_clicked_cb));
4025 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4026 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4027 _("Remove the selected bookmark"));
4032 /* Handles key press events on the file list, so that we can trap Enter to
4033 * activate the default button on our own. Also, checks to see if '/' has been
4034 * pressed. See comment by tree_view_keybinding_cb() for more details.
4037 trap_activate_cb (GtkWidget *widget,
4041 GtkFileChooserDefault *impl;
4044 impl = (GtkFileChooserDefault *) data;
4046 modifiers = gtk_accelerator_get_default_mod_mask ();
4048 if ((event->keyval == GDK_slash
4049 || event->keyval == GDK_KP_Divide
4051 || event->keyval == GDK_asciitilde
4053 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4055 location_popup_handler (impl, event->string);
4059 if ((event->keyval == GDK_Return
4060 || event->keyval == GDK_ISO_Enter
4061 || event->keyval == GDK_KP_Enter
4062 || event->keyval == GDK_space
4063 || event->keyval == GDK_KP_Space)
4064 && ((event->state & modifiers) == 0)
4065 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4066 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4070 window = get_toplevel (widget);
4072 && widget != window->default_widget
4073 && !(widget == window->focus_widget &&
4074 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4076 gtk_window_activate_default (window);
4084 /* Callback used when the file list's popup menu is detached */
4086 popup_menu_detach_cb (GtkWidget *attach_widget,
4089 GtkFileChooserDefault *impl;
4091 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4092 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4094 impl->browse_files_popup_menu = NULL;
4095 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4096 impl->browse_files_popup_menu_hidden_files_item = NULL;
4099 /* Callback used when the "Add to Bookmarks" menu item is activated */
4101 add_to_shortcuts_cb (GtkMenuItem *item,
4102 GtkFileChooserDefault *impl)
4104 bookmarks_add_selected_folder (impl);
4107 /* Callback used when the "Show Hidden Files" menu item is toggled */
4109 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4110 GtkFileChooserDefault *impl)
4113 "show-hidden", gtk_check_menu_item_get_active (item),
4117 /* Callback used when the "Show Size Column" menu item is toggled */
4119 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4120 GtkFileChooserDefault *impl)
4122 impl->show_size_column = gtk_check_menu_item_get_active (item);
4124 if (impl->list_size_column)
4125 gtk_tree_view_column_set_visible (impl->list_size_column,
4126 impl->show_size_column);
4129 /* Shows an error dialog about not being able to select a dragged file */
4131 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4136 _("Could not select file"),
4141 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4146 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4148 for (i = 1; uris[i]; i++)
4151 GError *error = NULL;
4154 file = g_file_new_for_uri (uri);
4156 gtk_file_chooser_default_select_file (chooser, file, &error);
4158 error_selecting_dragged_file_dialog (impl, file, error);
4160 g_object_unref (file);
4164 struct FileListDragData
4166 GtkFileChooserDefault *impl;
4172 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4174 const GError *error,
4177 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4178 struct FileListDragData *data = user_data;
4179 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4181 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4184 data->impl->file_list_drag_data_received_cancellable = NULL;
4186 if (cancelled || error)
4189 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4190 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4191 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4192 change_folder_and_display_error (data->impl, data->file, FALSE);
4195 GError *error = NULL;
4197 gtk_file_chooser_default_unselect_all (chooser);
4198 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4200 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4202 browse_files_center_selected_row (data->impl);
4205 if (data->impl->select_multiple)
4206 file_list_drag_data_select_uris (data->impl, data->uris);
4209 g_object_unref (data->impl);
4210 g_strfreev (data->uris);
4211 g_object_unref (data->file);
4214 g_object_unref (cancellable);
4218 file_list_drag_data_received_cb (GtkWidget *widget,
4219 GdkDragContext *context,
4222 GtkSelectionData *selection_data,
4227 GtkFileChooserDefault *impl;
4228 GtkFileChooser *chooser;
4233 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4234 chooser = GTK_FILE_CHOOSER (data);
4236 /* Allow only drags from other widgets; see bug #533891. */
4237 if (gtk_drag_get_source_widget (context) == widget)
4239 g_signal_stop_emission_by_name (widget, "drag-data-received");
4243 /* Parse the text/uri-list string, navigate to the first one */
4244 uris = gtk_selection_data_get_uris (selection_data);
4245 if (uris && uris[0])
4247 struct FileListDragData *data;
4250 file = g_file_new_for_uri (uri);
4252 data = g_new0 (struct FileListDragData, 1);
4253 data->impl = g_object_ref (impl);
4257 if (impl->file_list_drag_data_received_cancellable)
4258 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4260 impl->file_list_drag_data_received_cancellable =
4261 _gtk_file_system_get_info (impl->file_system, file,
4263 file_list_drag_data_received_get_info_cb,
4267 g_signal_stop_emission_by_name (widget, "drag-data-received");
4270 /* Don't do anything with the drag_drop signal */
4272 file_list_drag_drop_cb (GtkWidget *widget,
4273 GdkDragContext *context,
4277 GtkFileChooserDefault *impl)
4279 g_signal_stop_emission_by_name (widget, "drag-drop");
4283 /* Disable the normal tree drag motion handler, it makes it look like you're
4284 dropping the dragged item onto a tree item */
4286 file_list_drag_motion_cb (GtkWidget *widget,
4287 GdkDragContext *context,
4291 GtkFileChooserDefault *impl)
4293 g_signal_stop_emission_by_name (widget, "drag-motion");
4297 /* Constructs the popup menu for the file list if needed */
4299 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4303 if (impl->browse_files_popup_menu)
4306 impl->browse_files_popup_menu = gtk_menu_new ();
4307 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4308 impl->browse_files_tree_view,
4309 popup_menu_detach_cb);
4311 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4312 impl->browse_files_popup_menu_add_shortcut_item = item;
4313 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4314 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4315 gtk_widget_set_sensitive (item, FALSE);
4316 g_signal_connect (item, "activate",
4317 G_CALLBACK (add_to_shortcuts_cb), impl);
4318 gtk_widget_show (item);
4319 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4321 item = gtk_separator_menu_item_new ();
4322 gtk_widget_show (item);
4323 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4325 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4326 impl->browse_files_popup_menu_hidden_files_item = item;
4327 g_signal_connect (item, "toggled",
4328 G_CALLBACK (show_hidden_toggled_cb), impl);
4329 gtk_widget_show (item);
4330 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4332 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4333 impl->browse_files_popup_menu_size_column_item = item;
4334 g_signal_connect (item, "toggled",
4335 G_CALLBACK (show_size_column_toggled_cb), impl);
4336 gtk_widget_show (item);
4337 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4340 /* Updates the popup menu for the file list, creating it if necessary */
4342 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4344 file_list_build_popup_menu (impl);
4346 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4348 /* The sensitivity of the Add to Bookmarks item is set in
4349 * bookmarks_check_add_sensitivity()
4352 /* 'Show Hidden Files' */
4353 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4354 G_CALLBACK (show_hidden_toggled_cb), impl);
4355 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4357 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4358 G_CALLBACK (show_hidden_toggled_cb), impl);
4360 /* 'Show Size Column' */
4361 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4362 G_CALLBACK (show_size_column_toggled_cb), impl);
4363 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4364 impl->show_size_column);
4365 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4366 G_CALLBACK (show_size_column_toggled_cb), impl);
4370 popup_position_func (GtkMenu *menu,
4376 GtkWidget *widget = GTK_WIDGET (user_data);
4377 GdkScreen *screen = gtk_widget_get_screen (widget);
4380 GdkRectangle monitor;
4382 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4384 gdk_window_get_origin (widget->window, x, y);
4386 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4388 *x += (widget->allocation.width - req.width) / 2;
4389 *y += (widget->allocation.height - req.height) / 2;
4391 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4392 gtk_menu_set_monitor (menu, monitor_num);
4393 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4395 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4396 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4402 file_list_popup_menu (GtkFileChooserDefault *impl,
4403 GdkEventButton *event)
4405 file_list_update_popup_menu (impl);
4407 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4408 NULL, NULL, NULL, NULL,
4409 event->button, event->time);
4412 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4414 popup_position_func, impl->browse_files_tree_view,
4415 0, GDK_CURRENT_TIME);
4416 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4422 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4424 list_popup_menu_cb (GtkWidget *widget,
4425 GtkFileChooserDefault *impl)
4427 file_list_popup_menu (impl, NULL);
4431 /* Callback used when a button is pressed on the file list. We trap button 3 to
4432 * bring up a popup menu.
4435 list_button_press_event_cb (GtkWidget *widget,
4436 GdkEventButton *event,
4437 GtkFileChooserDefault *impl)
4439 static gboolean in_press = FALSE;
4445 if (event->button != 3)
4449 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4452 file_list_popup_menu (impl, event);
4456 /* Sets the sort column IDs for the file list based on the operation mode */
4458 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4460 int name_id, mtime_id, size_id;
4462 name_id = mtime_id = size_id = 0;
4464 switch (impl->operation_mode)
4466 case OPERATION_MODE_BROWSE:
4467 name_id = FILE_LIST_COL_NAME;
4468 mtime_id = FILE_LIST_COL_MTIME;
4469 size_id = FILE_LIST_COL_SIZE;
4471 case OPERATION_MODE_SEARCH:
4472 name_id = SEARCH_MODEL_COL_FILE;
4473 mtime_id = SEARCH_MODEL_COL_MTIME;
4474 size_id = SEARCH_MODEL_COL_SIZE;
4476 case OPERATION_MODE_RECENT:
4477 name_id = RECENT_MODEL_COL_FILE;
4478 mtime_id = RECENT_MODEL_COL_INFO;
4482 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4483 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4484 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, size_id);
4488 file_list_query_tooltip_cb (GtkWidget *widget,
4491 gboolean keyboard_tip,
4492 GtkTooltip *tooltip,
4495 GtkFileChooserDefault *impl = user_data;
4496 GtkTreeIter iter, child_iter;
4497 GtkTreePath *path = NULL;
4501 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4505 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4513 switch (impl->operation_mode)
4515 case OPERATION_MODE_SEARCH:
4516 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4518 gtk_tree_path_free (path);
4522 search_get_valid_child_iter (impl, &child_iter, &iter);
4523 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4524 SEARCH_MODEL_COL_FILE, &file,
4528 case OPERATION_MODE_RECENT:
4529 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4531 gtk_tree_path_free (path);
4535 recent_get_valid_child_iter (impl, &child_iter, &iter);
4536 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4537 RECENT_MODEL_COL_FILE, &file,
4541 case OPERATION_MODE_BROWSE:
4542 g_assert_not_reached ();
4548 gtk_tree_path_free (path);
4552 filename = g_file_get_path (file);
4553 gtk_tooltip_set_text (tooltip, filename);
4554 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4559 gtk_tree_path_free (path);
4564 /* Creates the widgets for the file list */
4566 create_file_list (GtkFileChooserDefault *impl)
4569 GtkTreeSelection *selection;
4570 GtkTreeViewColumn *column;
4571 GtkCellRenderer *renderer;
4573 /* Scrolled window */
4574 swin = gtk_scrolled_window_new (NULL, NULL);
4575 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4576 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4577 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4580 /* Tree/list view */
4582 impl->browse_files_tree_view = gtk_tree_view_new ();
4583 #ifdef PROFILE_FILE_CHOOSER
4584 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4586 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4587 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4589 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4590 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4592 gtk_drag_dest_set (impl->browse_files_tree_view,
4593 GTK_DEST_DEFAULT_ALL,
4595 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4596 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4598 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4599 G_CALLBACK (list_row_activated), impl);
4600 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4601 G_CALLBACK (trap_activate_cb), impl);
4602 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4603 G_CALLBACK (list_popup_menu_cb), impl);
4604 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4605 G_CALLBACK (list_button_press_event_cb), impl);
4607 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4608 G_CALLBACK (file_list_drag_data_received_cb), impl);
4609 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4610 G_CALLBACK (file_list_drag_drop_cb), impl);
4611 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4612 G_CALLBACK (file_list_drag_motion_cb), impl);
4614 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4615 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4616 G_CALLBACK (file_list_query_tooltip_cb), impl);
4618 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4619 gtk_tree_selection_set_select_function (selection,
4622 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4625 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4626 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4628 g_signal_connect (selection, "changed",
4629 G_CALLBACK (list_selection_changed), impl);
4631 /* Filename column */
4633 impl->list_name_column = gtk_tree_view_column_new ();
4634 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4635 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4636 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4637 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4639 renderer = gtk_cell_renderer_pixbuf_new ();
4640 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4641 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4642 list_icon_data_func, impl, NULL);
4644 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4645 g_object_set (impl->list_name_renderer,
4646 "ellipsize", PANGO_ELLIPSIZE_END,
4648 g_signal_connect (impl->list_name_renderer, "edited",
4649 G_CALLBACK (renderer_edited_cb), impl);
4650 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4651 G_CALLBACK (renderer_editing_canceled_cb), impl);
4652 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4653 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4654 list_name_data_func, impl, NULL);
4656 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4660 column = gtk_tree_view_column_new ();
4661 gtk_tree_view_column_set_title (column, _("Size"));
4663 renderer = gtk_cell_renderer_text_new ();
4664 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4665 gtk_tree_view_column_set_cell_data_func (column, renderer,
4666 list_size_data_func, impl, NULL);
4667 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4668 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4669 impl->list_size_column = column;
4671 /* Modification time column */
4673 column = gtk_tree_view_column_new ();
4674 gtk_tree_view_column_set_resizable (column, TRUE);
4675 gtk_tree_view_column_set_title (column, _("Modified"));
4677 renderer = gtk_cell_renderer_text_new ();
4678 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4679 gtk_tree_view_column_set_cell_data_func (column, renderer,
4680 list_mtime_data_func, impl, NULL);
4681 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4682 impl->list_mtime_column = column;
4684 file_list_set_sort_column_ids (impl);
4686 gtk_widget_show_all (swin);
4692 create_path_bar (GtkFileChooserDefault *impl)
4694 GtkWidget *path_bar;
4696 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4697 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4702 /* Creates the widgets for the files/folders pane */
4704 file_pane_create (GtkFileChooserDefault *impl,
4705 GtkSizeGroup *size_group)
4711 vbox = gtk_vbox_new (FALSE, 6);
4712 gtk_widget_show (vbox);
4714 /* Box for lists and preview */
4716 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4717 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4718 gtk_widget_show (hbox);
4722 widget = create_file_list (impl);
4723 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4727 impl->preview_box = gtk_vbox_new (FALSE, 12);
4728 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4729 /* Don't show preview box initially */
4733 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4735 widget = filter_create (impl);
4737 gtk_widget_show (widget);
4738 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4740 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4741 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4746 /* Callback used when the "Browse for more folders" expander is toggled */
4748 expander_changed_cb (GtkExpander *expander,
4750 GtkFileChooserDefault *impl)
4752 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4753 update_appearance (impl);
4756 /* Callback used when the selection changes in the save folder combo box */
4758 save_folder_combo_changed_cb (GtkComboBox *combo,
4759 GtkFileChooserDefault *impl)
4763 if (impl->changing_folder)
4766 if (gtk_combo_box_get_active_iter (combo, &iter))
4768 GtkTreeIter child_iter;
4770 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4773 shortcuts_activate_iter (impl, &child_iter);
4777 /* Filter function used to filter out the Search item and its separator.
4778 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4781 shortcuts_combo_filter_func (GtkTreeModel *model,
4785 GtkFileChooserDefault *impl;
4786 GtkTreePath *tree_path;
4791 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4793 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4795 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4796 g_assert (tree_path != NULL);
4798 indices = gtk_tree_path_get_indices (tree_path);
4802 if (impl->has_search)
4804 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4805 if (idx == indices[0])
4809 if (impl->has_recent)
4811 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4812 if (idx == indices[0])
4816 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4817 if (idx == indices[0])
4822 gtk_tree_path_free (tree_path);
4827 /* Creates the combo box with the save folders */
4829 save_folder_combo_create (GtkFileChooserDefault *impl)
4832 GtkCellRenderer *cell;
4834 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4835 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4836 shortcuts_combo_filter_func,
4840 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4841 "model", impl->shortcuts_combo_filter_model,
4842 "focus-on-click", FALSE,
4844 gtk_widget_show (combo);
4846 cell = gtk_cell_renderer_pixbuf_new ();
4847 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4848 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4849 "pixbuf", SHORTCUTS_COL_PIXBUF,
4850 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4851 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4854 cell = gtk_cell_renderer_text_new ();
4855 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4856 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4857 "text", SHORTCUTS_COL_NAME,
4858 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4861 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4862 shortcuts_row_separator_func,
4865 g_signal_connect (combo, "changed",
4866 G_CALLBACK (save_folder_combo_changed_cb), impl);
4871 /* Creates the widgets specific to Save mode */
4873 save_widgets_create (GtkFileChooserDefault *impl)
4878 GtkWidget *alignment;
4880 if (impl->save_widgets != NULL)
4883 location_switch_to_path_bar (impl);
4885 vbox = gtk_vbox_new (FALSE, 12);
4887 table = gtk_table_new (2, 2, FALSE);
4888 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4889 gtk_widget_show (table);
4890 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4891 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4895 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4896 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4897 gtk_table_attach (GTK_TABLE (table), widget,
4901 gtk_widget_show (widget);
4903 /* Location entry */
4905 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4906 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4908 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4909 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4910 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4911 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4913 GTK_EXPAND | GTK_FILL, 0,
4915 gtk_widget_show (impl->location_entry);
4916 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4919 impl->save_folder_label = gtk_label_new (NULL);
4920 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4921 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4925 gtk_widget_show (impl->save_folder_label);
4927 impl->save_folder_combo = save_folder_combo_create (impl);
4928 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4930 GTK_EXPAND | GTK_FILL, GTK_FILL,
4932 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4935 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4936 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4938 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4939 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4940 g_signal_connect (impl->save_expander, "notify::expanded",
4941 G_CALLBACK (expander_changed_cb),
4943 gtk_widget_show_all (alignment);
4945 impl->save_widgets = vbox;
4946 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4947 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4948 gtk_widget_show (impl->save_widgets);
4951 /* Destroys the widgets specific to Save mode */
4953 save_widgets_destroy (GtkFileChooserDefault *impl)
4955 if (impl->save_widgets == NULL)
4958 gtk_widget_destroy (impl->save_widgets);
4959 impl->save_widgets = NULL;
4960 impl->location_entry = NULL;
4961 impl->save_folder_label = NULL;
4962 impl->save_folder_combo = NULL;
4963 impl->save_expander = NULL;
4966 /* Turns on the path bar widget. Can be called even if we are already in that
4970 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4972 if (impl->location_entry)
4974 gtk_widget_destroy (impl->location_entry);
4975 impl->location_entry = NULL;
4978 gtk_widget_hide (impl->location_entry_box);
4981 /* Sets the full path of the current folder as the text in the location entry. */
4983 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4985 gchar *text, *filename;
4987 if (!impl->current_folder)
4990 filename = g_file_get_path (impl->current_folder);
4994 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4998 text = g_file_get_uri (impl->current_folder);
5002 gboolean need_slash;
5005 len = strlen (text);
5006 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
5012 slash_text = g_new (char, len + 2);
5013 strcpy (slash_text, text);
5014 slash_text[len] = G_DIR_SEPARATOR;
5015 slash_text[len + 1] = 0;
5021 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5028 /* Turns on the location entry. Can be called even if we are already in that
5032 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5034 /* when in search or recent files mode, we are not showing the
5035 * location_entry_box container, so there's no point in switching
5038 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5039 impl->operation_mode == OPERATION_MODE_RECENT)
5042 if (impl->location_entry)
5043 gtk_widget_destroy (impl->location_entry);
5047 gtk_widget_show (impl->location_entry_box);
5051 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5052 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5054 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5055 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5057 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5058 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5060 /* Configure the entry */
5062 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5066 gtk_widget_show (impl->location_entry);
5067 gtk_widget_grab_focus (impl->location_entry);
5070 /* Sets a new location mode. set_buttons determines whether the toggle button
5071 * for the mode will also be changed.
5074 location_mode_set (GtkFileChooserDefault *impl,
5075 LocationMode new_mode,
5076 gboolean set_button)
5078 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5079 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5081 GtkWindow *toplevel;
5082 GtkWidget *current_focus;
5083 gboolean button_active;
5084 gboolean switch_to_file_list;
5088 case LOCATION_MODE_PATH_BAR:
5089 button_active = FALSE;
5091 /* The location_entry will disappear when we switch to path bar mode. So,
5092 * we'll focus the file list in that case, to avoid having a window with
5093 * no focused widget.
5095 toplevel = get_toplevel (GTK_WIDGET (impl));
5096 switch_to_file_list = FALSE;
5099 current_focus = gtk_window_get_focus (toplevel);
5100 if (!current_focus || current_focus == impl->location_entry)
5101 switch_to_file_list = TRUE;
5104 location_switch_to_path_bar (impl);
5106 if (switch_to_file_list)
5107 gtk_widget_grab_focus (impl->browse_files_tree_view);
5111 case LOCATION_MODE_FILENAME_ENTRY:
5112 button_active = TRUE;
5113 location_switch_to_filename_entry (impl);
5117 g_assert_not_reached ();
5123 g_signal_handlers_block_by_func (impl->location_button,
5124 G_CALLBACK (location_button_toggled_cb), impl);
5126 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5128 g_signal_handlers_unblock_by_func (impl->location_button,
5129 G_CALLBACK (location_button_toggled_cb), impl);
5133 impl->location_mode = new_mode;
5137 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5139 /* when in search or recent files mode, we are not showing the
5140 * location_entry_box container, so there's no point in switching
5143 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5144 impl->operation_mode == OPERATION_MODE_RECENT)
5147 /* If the file entry is not visible, show it.
5148 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5150 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5152 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5154 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5156 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5158 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5162 gtk_widget_grab_focus (impl->location_entry);
5167 /* Callback used when one of the location mode buttons is toggled */
5169 location_button_toggled_cb (GtkToggleButton *toggle,
5170 GtkFileChooserDefault *impl)
5173 LocationMode new_mode;
5175 is_active = gtk_toggle_button_get_active (toggle);
5179 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5180 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5184 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5185 new_mode = LOCATION_MODE_PATH_BAR;
5188 location_mode_set (impl, new_mode, FALSE);
5191 /* Creates a toggle button for the location entry. */
5193 location_button_create (GtkFileChooserDefault *impl)
5198 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5199 gtk_widget_show (image);
5201 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5205 g_signal_connect (impl->location_button, "toggled",
5206 G_CALLBACK (location_button_toggled_cb), impl);
5208 str = _("Type a file name");
5210 gtk_widget_set_tooltip_text (impl->location_button, str);
5211 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5214 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5216 browse_widgets_create (GtkFileChooserDefault *impl)
5222 GtkSizeGroup *size_group;
5224 /* size group is used by the [+][-] buttons and the filter combo */
5225 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5226 vbox = gtk_vbox_new (FALSE, 12);
5228 /* Location widgets */
5229 hbox = gtk_hbox_new (FALSE, 12);
5230 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5231 gtk_widget_show (hbox);
5232 impl->browse_path_bar_hbox = hbox;
5234 location_button_create (impl);
5235 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5239 impl->browse_path_bar = create_path_bar (impl);
5240 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5241 gtk_widget_show_all (impl->browse_path_bar);
5242 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5245 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5246 g_signal_connect (impl->browse_new_folder_button, "clicked",
5247 G_CALLBACK (new_folder_button_clicked), impl);
5248 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5250 /* Box for the location label and entry */
5252 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5253 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5255 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5256 gtk_widget_show (impl->location_label);
5257 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5260 hpaned = gtk_hpaned_new ();
5261 gtk_widget_show (hpaned);
5262 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5264 widget = shortcuts_pane_create (impl, size_group);
5265 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5266 widget = file_pane_create (impl, size_group);
5267 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5269 g_object_unref (size_group);
5275 gtk_file_chooser_default_constructor (GType type,
5276 guint n_construct_properties,
5277 GObjectConstructParam *construct_params)
5279 GtkFileChooserDefault *impl;
5282 profile_start ("start", NULL);
5284 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5285 n_construct_properties,
5287 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5289 g_assert (impl->file_system);
5291 gtk_widget_push_composite_child ();
5293 /* Shortcuts model */
5294 shortcuts_model_create (impl);
5296 /* The browse widgets */
5297 impl->browse_widgets = browse_widgets_create (impl);
5298 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5300 /* Alignment to hold extra widget */
5301 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5302 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5304 gtk_widget_pop_composite_child ();
5305 update_appearance (impl);
5307 profile_end ("end", NULL);
5312 /* Sets the extra_widget by packing it in the appropriate place */
5314 set_extra_widget (GtkFileChooserDefault *impl,
5315 GtkWidget *extra_widget)
5319 g_object_ref (extra_widget);
5320 /* FIXME: is this right ? */
5321 gtk_widget_show (extra_widget);
5324 if (impl->extra_widget)
5326 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5327 g_object_unref (impl->extra_widget);
5330 impl->extra_widget = extra_widget;
5331 if (impl->extra_widget)
5333 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5334 gtk_widget_show (impl->extra_align);
5337 gtk_widget_hide (impl->extra_align);
5341 set_local_only (GtkFileChooserDefault *impl,
5342 gboolean local_only)
5344 if (local_only != impl->local_only)
5346 impl->local_only = local_only;
5348 if (impl->location_entry)
5349 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5351 if (impl->shortcuts_model && impl->file_system)
5353 shortcuts_add_volumes (impl);
5354 shortcuts_add_bookmarks (impl);
5357 if (local_only && impl->current_folder &&
5358 !g_file_is_native (impl->current_folder))
5360 /* If we are pointing to a non-local folder, make an effort to change
5361 * back to a local folder, but it's really up to the app to not cause
5362 * such a situation, so we ignore errors.
5364 const gchar *home = g_get_home_dir ();
5370 home_file = g_file_new_for_path (home);
5372 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5374 g_object_unref (home_file);
5380 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5381 GtkFileChooserDefault *impl)
5383 shortcuts_add_volumes (impl);
5384 shortcuts_add_bookmarks (impl);
5386 bookmarks_check_add_sensitivity (impl);
5387 bookmarks_check_remove_sensitivity (impl);
5388 shortcuts_check_popup_sensitivity (impl);
5391 /* Sets the file chooser to multiple selection mode */
5393 set_select_multiple (GtkFileChooserDefault *impl,
5394 gboolean select_multiple,
5395 gboolean property_notify)
5397 GtkTreeSelection *selection;
5398 GtkSelectionMode mode;
5400 if (select_multiple == impl->select_multiple)
5403 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5405 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5406 gtk_tree_selection_set_mode (selection, mode);
5408 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5410 impl->select_multiple = select_multiple;
5411 g_object_notify (G_OBJECT (impl), "select-multiple");
5413 check_preview_change (impl);
5417 set_file_system_backend (GtkFileChooserDefault *impl)
5419 profile_start ("start for backend", "default");
5421 impl->file_system = _gtk_file_system_new ();
5423 g_signal_connect (impl->file_system, "volumes-changed",
5424 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5425 g_signal_connect (impl->file_system, "bookmarks-changed",
5426 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5428 profile_end ("end", NULL);
5432 unset_file_system_backend (GtkFileChooserDefault *impl)
5434 g_signal_handlers_disconnect_by_func (impl->file_system,
5435 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5437 g_object_unref (impl->file_system);
5439 impl->file_system = NULL;
5442 /* This function is basically a do_all function.
5444 * It sets the visibility on all the widgets based on the current state, and
5445 * moves the custom_widget if needed.
5448 update_appearance (GtkFileChooserDefault *impl)
5450 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5451 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5455 gtk_widget_hide (impl->location_button);
5456 save_widgets_create (impl);
5458 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5459 text = _("Save in _folder:");
5461 text = _("Create in _folder:");
5463 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5465 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5467 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5468 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5469 gtk_widget_show (impl->browse_widgets);
5473 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5474 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5475 gtk_widget_hide (impl->browse_widgets);
5478 gtk_widget_show (impl->browse_new_folder_button);
5480 if (impl->select_multiple)
5482 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5483 "Re-setting to single selection mode.");
5484 set_select_multiple (impl, FALSE, TRUE);
5487 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5488 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5490 gtk_widget_show (impl->location_button);
5491 save_widgets_destroy (impl);
5492 gtk_widget_show (impl->browse_widgets);
5493 location_mode_set (impl, impl->location_mode, TRUE);
5496 if (impl->location_entry)
5497 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5499 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5500 gtk_widget_hide (impl->browse_new_folder_button);
5502 gtk_widget_show (impl->browse_new_folder_button);
5504 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5505 * of files may change depending whether we are in a file or folder-only mode.
5507 gtk_widget_queue_draw (impl->browse_files_tree_view);
5509 emit_default_size_changed (impl);
5513 gtk_file_chooser_default_set_property (GObject *object,
5515 const GValue *value,
5519 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5523 case GTK_FILE_CHOOSER_PROP_ACTION:
5525 GtkFileChooserAction action = g_value_get_enum (value);
5527 if (action != impl->action)
5529 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5531 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5532 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5533 && impl->select_multiple)
5535 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5536 "this is not allowed in multiple selection mode. Resetting the file chooser "
5537 "to single selection mode.");
5538 set_select_multiple (impl, FALSE, TRUE);
5540 impl->action = action;
5541 update_appearance (impl);
5542 settings_load (impl);
5547 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5548 /* Ignore property */
5551 case GTK_FILE_CHOOSER_PROP_FILTER:
5552 set_current_filter (impl, g_value_get_object (value));
5555 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5556 set_local_only (impl, g_value_get_boolean (value));
5559 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5560 set_preview_widget (impl, g_value_get_object (value));
5563 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5564 impl->preview_widget_active = g_value_get_boolean (value);
5565 update_preview_widget_visibility (impl);
5568 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5569 impl->use_preview_label = g_value_get_boolean (value);
5570 update_preview_widget_visibility (impl);
5573 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5574 set_extra_widget (impl, g_value_get_object (value));
5577 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5579 gboolean select_multiple = g_value_get_boolean (value);
5580 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5581 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5584 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5585 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5586 "leaving the file chooser in single selection mode.");
5590 set_select_multiple (impl, select_multiple, FALSE);
5594 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5596 gboolean show_hidden = g_value_get_boolean (value);
5597 if (show_hidden != impl->show_hidden)
5599 impl->show_hidden = show_hidden;
5601 if (impl->browse_files_model)
5602 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5607 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5609 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5610 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5621 gtk_file_chooser_default_get_property (GObject *object,
5626 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5630 case GTK_FILE_CHOOSER_PROP_ACTION:
5631 g_value_set_enum (value, impl->action);
5634 case GTK_FILE_CHOOSER_PROP_FILTER:
5635 g_value_set_object (value, impl->current_filter);
5638 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5639 g_value_set_boolean (value, impl->local_only);
5642 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5643 g_value_set_object (value, impl->preview_widget);
5646 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5647 g_value_set_boolean (value, impl->preview_widget_active);
5650 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5651 g_value_set_boolean (value, impl->use_preview_label);
5654 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5655 g_value_set_object (value, impl->extra_widget);
5658 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5659 g_value_set_boolean (value, impl->select_multiple);
5662 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5663 g_value_set_boolean (value, impl->show_hidden);
5666 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5667 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5671 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5676 /* Removes the settings signal handler. It's safe to call multiple times */
5678 remove_settings_signal (GtkFileChooserDefault *impl,
5681 if (impl->settings_signal_id)
5683 GtkSettings *settings;
5685 settings = gtk_settings_get_for_screen (screen);
5686 g_signal_handler_disconnect (settings,
5687 impl->settings_signal_id);
5688 impl->settings_signal_id = 0;
5693 gtk_file_chooser_default_dispose (GObject *object)
5696 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5698 if (impl->extra_widget)
5700 g_object_unref (impl->extra_widget);
5701 impl->extra_widget = NULL;
5704 pending_select_files_free (impl);
5706 /* cancel all pending operations */
5707 if (impl->pending_cancellables)
5709 for (l = impl->pending_cancellables; l; l = l->next)
5711 GCancellable *cancellable = G_CANCELLABLE (l->data);
5712 g_cancellable_cancel (cancellable);
5714 g_slist_free (impl->pending_cancellables);
5715 impl->pending_cancellables = NULL;
5718 if (impl->reload_icon_cancellables)
5720 for (l = impl->reload_icon_cancellables; l; l = l->next)
5722 GCancellable *cancellable = G_CANCELLABLE (l->data);
5723 g_cancellable_cancel (cancellable);
5725 g_slist_free (impl->reload_icon_cancellables);
5726 impl->reload_icon_cancellables = NULL;
5729 if (impl->loading_shortcuts)
5731 for (l = impl->loading_shortcuts; l; l = l->next)
5733 GCancellable *cancellable = G_CANCELLABLE (l->data);
5734 g_cancellable_cancel (cancellable);
5736 g_slist_free (impl->loading_shortcuts);
5737 impl->loading_shortcuts = NULL;
5740 if (impl->file_list_drag_data_received_cancellable)
5742 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5743 impl->file_list_drag_data_received_cancellable = NULL;
5746 if (impl->update_current_folder_cancellable)
5748 g_cancellable_cancel (impl->update_current_folder_cancellable);
5749 impl->update_current_folder_cancellable = NULL;
5752 if (impl->show_and_select_files_cancellable)
5754 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5755 impl->show_and_select_files_cancellable = NULL;
5758 if (impl->should_respond_get_info_cancellable)
5760 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5761 impl->should_respond_get_info_cancellable = NULL;
5764 if (impl->update_from_entry_cancellable)
5766 g_cancellable_cancel (impl->update_from_entry_cancellable);
5767 impl->update_from_entry_cancellable = NULL;
5770 if (impl->shortcuts_activate_iter_cancellable)
5772 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5773 impl->shortcuts_activate_iter_cancellable = NULL;
5776 search_stop_searching (impl, TRUE);
5777 recent_stop_loading (impl);
5779 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5781 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5784 /* We override show-all since we have internal widgets that
5785 * shouldn't be shown when you call show_all(), like the filter
5789 gtk_file_chooser_default_show_all (GtkWidget *widget)
5791 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5793 gtk_widget_show (widget);
5795 if (impl->extra_widget)
5796 gtk_widget_show_all (impl->extra_widget);
5799 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5800 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5803 toplevel_set_focus_cb (GtkWindow *window,
5805 GtkFileChooserDefault *impl)
5807 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5810 /* We monitor the focus widget on our toplevel to be able to know which widget
5811 * was last focused at the time our "should_respond" method gets called.
5814 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5815 GtkWidget *previous_toplevel)
5817 GtkFileChooserDefault *impl;
5818 GtkWidget *toplevel;
5820 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5822 if (previous_toplevel)
5824 g_assert (impl->toplevel_set_focus_id != 0);
5825 g_signal_handler_disconnect (previous_toplevel,
5826 impl->toplevel_set_focus_id);
5827 impl->toplevel_set_focus_id = 0;
5828 impl->toplevel_last_focus_widget = NULL;
5831 g_assert (impl->toplevel_set_focus_id == 0);
5833 toplevel = gtk_widget_get_toplevel (widget);
5834 if (GTK_IS_WINDOW (toplevel))
5836 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5837 G_CALLBACK (toplevel_set_focus_cb), impl);
5838 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5842 /* Changes the icons wherever it is needed */
5844 change_icon_theme (GtkFileChooserDefault *impl)
5846 GtkSettings *settings;
5849 profile_start ("start", NULL);
5851 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5853 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5854 impl->icon_size = MAX (width, height);
5856 impl->icon_size = FALLBACK_ICON_SIZE;
5858 shortcuts_reload_icons (impl);
5859 gtk_widget_queue_resize (impl->browse_files_tree_view);
5861 profile_end ("end", NULL);
5864 /* Callback used when a GtkSettings value changes */
5866 settings_notify_cb (GObject *object,
5868 GtkFileChooserDefault *impl)
5872 profile_start ("start", NULL);
5874 name = g_param_spec_get_name (pspec);
5876 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5877 strcmp (name, "gtk-icon-sizes") == 0)
5878 change_icon_theme (impl);
5880 profile_end ("end", NULL);
5883 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5887 check_icon_theme (GtkFileChooserDefault *impl)
5889 GtkSettings *settings;
5891 profile_start ("start", NULL);
5893 if (impl->settings_signal_id)
5895 profile_end ("end", NULL);
5899 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5901 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5902 impl->settings_signal_id = g_signal_connect (settings, "notify",
5903 G_CALLBACK (settings_notify_cb), impl);
5905 change_icon_theme (impl);
5908 profile_end ("end", NULL);
5912 gtk_file_chooser_default_style_set (GtkWidget *widget,
5913 GtkStyle *previous_style)
5915 GtkFileChooserDefault *impl;
5917 profile_start ("start", NULL);
5919 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5921 profile_msg (" parent class style_set start", NULL);
5922 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5923 profile_msg (" parent class style_set end", NULL);
5925 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5926 change_icon_theme (impl);
5928 emit_default_size_changed (impl);
5930 profile_end ("end", NULL);
5934 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5935 GdkScreen *previous_screen)
5937 GtkFileChooserDefault *impl;
5939 profile_start ("start", NULL);
5941 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5943 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5944 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5946 remove_settings_signal (impl, previous_screen);
5947 check_icon_theme (impl);
5949 emit_default_size_changed (impl);
5951 profile_end ("end", NULL);
5955 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5956 GtkAllocation *allocation)
5958 GtkFileChooserDefault *impl;
5960 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5962 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5966 get_is_file_filtered (GtkFileChooserDefault *impl,
5968 GFileInfo *file_info)
5970 GtkFileFilterInfo filter_info;
5971 GtkFileFilterFlags needed;
5974 if (!impl->current_filter)
5977 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5979 needed = gtk_file_filter_get_needed (impl->current_filter);
5981 filter_info.display_name = g_file_info_get_display_name (file_info);
5982 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
5984 if (needed & GTK_FILE_FILTER_FILENAME)
5986 filter_info.filename = g_file_get_path (file);
5987 if (filter_info.filename)
5988 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5991 filter_info.filename = NULL;
5993 if (needed & GTK_FILE_FILTER_URI)
5995 filter_info.uri = g_file_get_uri (file);
5996 if (filter_info.uri)
5997 filter_info.contains |= GTK_FILE_FILTER_URI;
6000 filter_info.uri = NULL;
6002 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
6004 g_free ((gchar *)filter_info.filename);
6005 g_free ((gchar *)filter_info.uri);
6006 g_free ((gchar *)filter_info.mime_type);
6012 settings_load (GtkFileChooserDefault *impl)
6014 GtkFileChooserSettings *settings;
6015 LocationMode location_mode;
6016 gboolean show_hidden;
6017 gboolean expand_folders;
6018 gboolean show_size_column;
6020 settings = _gtk_file_chooser_settings_new ();
6022 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6023 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6024 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6025 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
6027 g_object_unref (settings);
6029 location_mode_set (impl, location_mode, TRUE);
6031 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6033 impl->expand_folders = expand_folders;
6034 if (impl->save_expander)
6035 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6037 impl->show_size_column = show_size_column;
6038 if (impl->list_size_column)
6039 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6043 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
6045 GtkWindow *toplevel;
6046 int x, y, width, height;
6048 /* We don't save the geometry in non-expanded "save" mode, so that the "little
6049 * dialog" won't make future Open dialogs too small.
6051 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6052 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6053 || impl->expand_folders))
6056 toplevel = get_toplevel (GTK_WIDGET (impl));
6058 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6061 gtk_window_get_position (toplevel, &x, &y);
6062 gtk_window_get_size (toplevel, &width, &height);
6064 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
6068 settings_save (GtkFileChooserDefault *impl)
6070 GtkFileChooserSettings *settings;
6072 settings = _gtk_file_chooser_settings_new ();
6074 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6075 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6076 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6077 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6079 save_dialog_geometry (impl, settings);
6082 _gtk_file_chooser_settings_save (settings, NULL);
6084 g_object_unref (settings);
6087 /* GtkWidget::realize method */
6089 gtk_file_chooser_default_realize (GtkWidget *widget)
6091 GtkFileChooserDefault *impl;
6092 char *current_working_dir;
6094 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6096 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6098 emit_default_size_changed (impl);
6101 /* GtkWidget::map method */
6103 gtk_file_chooser_default_map (GtkWidget *widget)
6105 GtkFileChooserDefault *impl;
6106 char *current_working_dir;
6108 profile_start ("start", NULL);
6110 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6112 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6114 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6116 switch (impl->reload_state)
6119 /* The user didn't explicitly give us a folder to
6120 * display, so we'll use the cwd
6122 current_working_dir = g_get_current_dir ();
6123 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6124 current_working_dir);
6125 g_free (current_working_dir);
6128 case RELOAD_HAS_FOLDER:
6129 /* Nothing; we are already loading or loaded, so we
6130 * don't need to reload
6134 case RELOAD_WAS_UNMAPPED:
6135 /* Just reload the current folder; else continue
6138 if (impl->current_folder)
6140 pending_select_files_store_selection (impl);
6141 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6146 g_assert_not_reached ();
6150 volumes_bookmarks_changed_cb (impl->file_system, impl);
6152 settings_load (impl);
6154 profile_end ("end", NULL);
6157 /* GtkWidget::unmap method */
6159 gtk_file_chooser_default_unmap (GtkWidget *widget)
6161 GtkFileChooserDefault *impl;
6163 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6165 settings_save (impl);
6167 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6169 impl->reload_state = RELOAD_WAS_UNMAPPED;
6173 list_model_filter_func (GtkFileSystemModel *model,
6175 GFileInfo *file_info,
6178 GtkFileChooserDefault *impl = user_data;
6180 if (!impl->current_filter)
6183 if (_gtk_file_info_consider_as_directory (file_info))
6186 return !get_is_file_filtered (impl, file, file_info);
6190 install_list_model_filter (GtkFileChooserDefault *impl)
6192 GtkFileSystemModelFilter filter;
6195 g_assert (impl->browse_files_model != NULL);
6197 if (impl->current_filter)
6199 filter = list_model_filter_func;
6208 _gtk_file_system_model_set_filter (impl->browse_files_model,
6213 #define COMPARE_DIRECTORIES \
6214 GtkFileChooserDefault *impl = user_data; \
6215 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6216 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6217 gboolean dir_a, dir_b; \
6220 dir_a = _gtk_file_info_consider_as_directory (info_a); \
6222 return impl->list_sort_ascending ? -1 : 1; \
6225 dir_b = _gtk_file_info_consider_as_directory (info_b); \
6227 return impl->list_sort_ascending ? 1 : -1; \
6229 if (dir_a != dir_b) \
6230 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6232 /* Sort callback for the filename column */
6234 name_sort_func (GtkTreeModel *model,
6239 COMPARE_DIRECTORIES;
6242 gchar *key_a, *key_b;
6245 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6246 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6247 result = strcmp (key_a, key_b);
6256 /* Sort callback for the size column */
6258 size_sort_func (GtkTreeModel *model,
6263 COMPARE_DIRECTORIES;
6266 goffset size_a = g_file_info_get_size (info_a);
6267 goffset size_b = g_file_info_get_size (info_b);
6269 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6273 /* Sort callback for the mtime column */
6275 mtime_sort_func (GtkTreeModel *model,
6280 COMPARE_DIRECTORIES;
6285 g_file_info_get_modification_time (info_a, &ta);
6286 g_file_info_get_modification_time (info_b, &tb);
6288 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6292 /* Callback used when the sort column changes. We cache the sort order for use
6293 * in name_sort_func().
6296 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6297 GtkFileChooserDefault *impl)
6299 GtkSortType sort_type;
6301 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6302 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6306 set_busy_cursor (GtkFileChooserDefault *impl,
6309 GtkWindow *toplevel;
6310 GdkDisplay *display;
6313 toplevel = get_toplevel (GTK_WIDGET (impl));
6314 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6317 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6320 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6324 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6325 gdk_display_flush (display);
6328 gdk_cursor_unref (cursor);
6331 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6333 load_set_model (GtkFileChooserDefault *impl)
6335 profile_start ("start", NULL);
6337 g_assert (impl->browse_files_model != NULL);
6338 g_assert (impl->sort_model == NULL);
6340 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6341 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6342 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6343 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6344 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6345 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6346 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6347 impl->list_sort_ascending = TRUE;
6348 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6350 g_signal_connect (impl->sort_model, "sort-column-changed",
6351 G_CALLBACK (list_sort_column_changed_cb), impl);
6353 profile_msg (" gtk_tree_view_set_model start", NULL);
6354 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6355 GTK_TREE_MODEL (impl->sort_model));
6356 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6357 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6358 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6359 profile_msg (" gtk_tree_view_set_model end", NULL);
6361 profile_end ("end", NULL);
6364 /* Timeout callback used when the loading timer expires */
6366 load_timeout_cb (gpointer data)
6368 GtkFileChooserDefault *impl;
6370 profile_start ("start", NULL);
6372 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6373 g_assert (impl->load_state == LOAD_PRELOAD);
6374 g_assert (impl->load_timeout_id != 0);
6375 g_assert (impl->browse_files_model != NULL);
6377 impl->load_timeout_id = 0;
6378 impl->load_state = LOAD_LOADING;
6380 load_set_model (impl);
6382 profile_end ("end", NULL);
6387 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6389 load_setup_timer (GtkFileChooserDefault *impl)
6391 g_assert (impl->load_timeout_id == 0);
6392 g_assert (impl->load_state != LOAD_PRELOAD);
6394 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6395 impl->load_state = LOAD_PRELOAD;
6398 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6400 load_remove_timer (GtkFileChooserDefault *impl)
6402 if (impl->load_timeout_id != 0)
6404 g_assert (impl->load_state == LOAD_PRELOAD);
6406 g_source_remove (impl->load_timeout_id);
6407 impl->load_timeout_id = 0;
6408 impl->load_state = LOAD_EMPTY;
6411 g_assert (impl->load_state == LOAD_EMPTY ||
6412 impl->load_state == LOAD_LOADING ||
6413 impl->load_state == LOAD_FINISHED);
6416 /* Selects the first row in the file list */
6418 browse_files_select_first_row (GtkFileChooserDefault *impl)
6421 GtkTreeIter dummy_iter;
6422 GtkTreeModel *tree_model;
6424 if (!impl->sort_model)
6427 path = gtk_tree_path_new_from_indices (0, -1);
6428 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6430 /* If the list is empty, do nothing. */
6431 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6432 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6434 gtk_tree_path_free (path);
6437 struct center_selected_row_closure {
6438 GtkFileChooserDefault *impl;
6439 gboolean already_centered;
6442 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6443 * selected row in the tree view.
6446 center_selected_row_foreach_cb (GtkTreeModel *model,
6451 struct center_selected_row_closure *closure;
6454 if (closure->already_centered)
6457 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6458 closure->already_centered = TRUE;
6461 /* Centers the selected row in the tree view */
6463 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6465 struct center_selected_row_closure closure;
6466 GtkTreeSelection *selection;
6468 closure.impl = impl;
6469 closure.already_centered = FALSE;
6471 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6472 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6475 struct ShowAndSelectPathsData
6477 GtkFileChooserDefault *impl;
6482 show_and_select_files_finished_loading (GtkFolder *folder,
6485 gboolean have_hidden;
6486 gboolean have_filtered;
6488 struct ShowAndSelectPathsData *data = user_data;
6490 have_hidden = FALSE;
6491 have_filtered = FALSE;
6493 for (l = data->files; l; l = l->next)
6500 info = _gtk_folder_get_info (folder, file);
6504 have_hidden = g_file_info_get_is_hidden (info);
6507 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6508 get_is_file_filtered (data->impl, file, info);
6510 g_object_unref (info);
6512 if (have_hidden && have_filtered)
6513 break; /* we now have all the information we need */
6517 g_signal_handlers_disconnect_by_func (folder,
6518 show_and_select_files_finished_loading,
6522 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6525 set_current_filter (data->impl, NULL);
6527 for (l = data->files; l; l = l->next)
6532 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6533 select_func, data->impl);
6536 browse_files_center_selected_row (data->impl);
6538 g_object_unref (data->impl);
6539 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6540 g_slist_free (data->files);
6545 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6547 const GError *error,
6550 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6551 struct ShowAndSelectPathsData *data = user_data;
6553 if (data->impl->show_and_select_files_cancellable != cancellable)
6556 data->impl->show_and_select_files_cancellable = NULL;
6558 if (cancelled || error)
6561 g_object_unref (cancellable);
6563 if (_gtk_folder_is_finished_loading (folder))
6564 show_and_select_files_finished_loading (folder, user_data);
6566 g_signal_connect (folder, "finished-loading",
6567 G_CALLBACK (show_and_select_files_finished_loading),
6573 g_object_unref (data->impl);
6574 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6575 g_slist_free (data->files);
6578 g_object_unref (cancellable);
6582 show_and_select_files (GtkFileChooserDefault *impl,
6587 struct ShowAndSelectPathsData *info;
6589 profile_start ("start", NULL);
6593 profile_end ("end", NULL);
6597 info = g_new (struct ShowAndSelectPathsData, 1);
6598 info->impl = g_object_ref (impl);
6599 info->files = g_slist_copy (files);
6600 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6602 if (impl->show_and_select_files_cancellable)
6603 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6605 impl->show_and_select_files_cancellable =
6606 _gtk_file_system_get_folder (impl->file_system, parent_file,
6607 "standard::is-hidden,standard::type,standard::name,standard::content-type",
6608 show_and_select_files_get_folder_cb, info);
6610 profile_end ("end", NULL);
6614 /* Processes the pending operation when a folder is finished loading */
6616 pending_select_files_process (GtkFileChooserDefault *impl)
6618 g_assert (impl->load_state == LOAD_FINISHED);
6619 g_assert (impl->browse_files_model != NULL);
6620 g_assert (impl->sort_model != NULL);
6622 if (impl->pending_select_files)
6625 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6626 pending_select_files_free (impl);
6627 browse_files_center_selected_row (impl);
6631 /* We only select the first row if the chooser is actually mapped ---
6632 * selecting the first row is to help the user when he is interacting with
6633 * the chooser, but sometimes a chooser works not on behalf of the user,
6634 * but rather on behalf of something else like GtkFileChooserButton. In
6635 * that case, the chooser's selection should be what the caller expects,
6636 * as the user can't see that something else got selected. See bug #165264.
6638 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6639 browse_files_select_first_row (impl);
6642 g_assert (impl->pending_select_files == NULL);
6645 /* Callback used when the file system model finishes loading */
6647 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6648 GtkFileChooserDefault *impl)
6650 profile_start ("start", NULL);
6652 if (impl->load_state == LOAD_PRELOAD)
6654 load_remove_timer (impl);
6655 load_set_model (impl);
6657 else if (impl->load_state == LOAD_LOADING)
6663 /* We can't g_assert_not_reached(), as something other than us may have
6664 * initiated a folder reload. See #165556.
6666 profile_end ("end", NULL);
6670 g_assert (impl->load_timeout_id == 0);
6672 impl->load_state = LOAD_FINISHED;
6674 pending_select_files_process (impl);
6675 set_busy_cursor (impl, FALSE);
6676 #ifdef PROFILE_FILE_CHOOSER
6677 access ("MARK: *** FINISHED LOADING", F_OK);
6680 profile_end ("end", NULL);
6684 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6686 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6688 if (impl->browse_files_model)
6690 g_object_unref (impl->browse_files_model);
6691 impl->browse_files_model = NULL;
6694 if (impl->sort_model)
6696 g_object_unref (impl->sort_model);
6697 impl->sort_model = NULL;
6700 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6703 /* Gets rid of the old list model and creates a new one for the current folder */
6705 set_list_model (GtkFileChooserDefault *impl,
6708 g_assert (impl->current_folder != NULL);
6710 profile_start ("start", NULL);
6712 stop_loading_and_clear_list_model (impl);
6714 set_busy_cursor (impl, TRUE);
6715 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6717 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6718 impl->current_folder, 0,
6719 "standard,time,thumbnail::*",
6721 if (!impl->browse_files_model)
6723 set_busy_cursor (impl, FALSE);
6724 profile_end ("end", NULL);
6728 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6730 g_signal_connect (impl->browse_files_model, "finished-loading",
6731 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6733 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6735 install_list_model_filter (impl);
6737 profile_end ("end", NULL);
6742 struct update_chooser_entry_selected_foreach_closure {
6744 GtkTreeIter first_selected_iter;
6748 compare_utf8_filenames (const gchar *a,
6751 gchar *a_folded, *b_folded;
6754 a_folded = g_utf8_strdown (a, -1);
6755 b_folded = g_utf8_strdown (b, -1);
6757 retval = strcmp (a_folded, b_folded);
6766 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6771 struct update_chooser_entry_selected_foreach_closure *closure;
6774 closure->num_selected++;
6776 if (closure->num_selected == 1)
6777 closure->first_selected_iter = *iter;
6781 update_chooser_entry (GtkFileChooserDefault *impl)
6783 GtkTreeSelection *selection;
6784 struct update_chooser_entry_selected_foreach_closure closure;
6785 const char *file_part;
6787 /* no need to update the file chooser's entry if there's no entry */
6788 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6789 impl->operation_mode == OPERATION_MODE_RECENT ||
6790 !impl->location_entry)
6793 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6794 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6795 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6796 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6797 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6800 g_assert (impl->location_entry != NULL);
6802 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6803 closure.num_selected = 0;
6804 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6808 if (closure.num_selected == 0)
6810 goto maybe_clear_entry;
6812 else if (closure.num_selected == 1)
6814 GtkTreeIter child_iter;
6816 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6819 gboolean change_entry;
6821 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6823 &closure.first_selected_iter);
6825 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6827 /* If the cursor moved to the row of the newly created folder,
6828 * retrieving info will return NULL.
6833 g_free (impl->browse_files_last_selected_name);
6834 impl->browse_files_last_selected_name =
6835 g_strdup (g_file_info_get_display_name (info));
6837 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6838 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6839 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6841 /* We don't want the name to change when clicking on a folder... */
6842 change_entry = (! _gtk_file_info_consider_as_directory (info));
6845 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6849 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6851 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6852 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6860 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6861 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6863 /* Multiple selection, so just clear the entry. */
6865 g_free (impl->browse_files_last_selected_name);
6866 impl->browse_files_last_selected_name = NULL;
6868 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6874 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6875 && impl->browse_files_last_selected_name)
6877 const char *entry_text;
6879 gboolean clear_entry;
6881 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6882 len = strlen (entry_text);
6885 /* The file chooser entry may have appended a "/" to its text. So
6886 * take it out, and compare the result to the old selection.
6888 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6892 tmp = g_strndup (entry_text, len - 1);
6893 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6897 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6900 clear_entry = FALSE;
6903 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6908 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6912 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6916 struct UpdateCurrentFolderData
6918 GtkFileChooserDefault *impl;
6920 gboolean keep_trail;
6921 gboolean clear_entry;
6922 GFile *original_file;
6923 GError *original_error;
6927 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6928 GtkFileSystemVolume *volume,
6929 const GError *error,
6932 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6933 struct UpdateCurrentFolderData *data = user_data;
6934 GtkFileChooserDefault *impl = data->impl;
6936 if (cancellable != impl->update_current_folder_cancellable)
6939 impl->update_current_folder_cancellable = NULL;
6940 set_busy_cursor (impl, FALSE);
6947 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6948 impl->reload_state = RELOAD_EMPTY;
6952 change_folder_and_display_error (impl, data->file, data->clear_entry);
6955 g_object_unref (data->file);
6958 g_object_unref (cancellable);
6962 update_current_folder_get_info_cb (GCancellable *cancellable,
6964 const GError *error,
6967 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6968 struct UpdateCurrentFolderData *data = user_data;
6969 GtkFileChooserDefault *impl = data->impl;
6971 if (cancellable != impl->update_current_folder_cancellable)
6974 impl->update_current_folder_cancellable = NULL;
6975 impl->reload_state = RELOAD_EMPTY;
6977 set_busy_cursor (impl, FALSE);
6986 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6988 GMountOperation *mount_operation;
6989 GtkWidget *toplevel;
6991 g_object_unref (cancellable);
6992 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6994 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6996 set_busy_cursor (impl, TRUE);
6998 impl->update_current_folder_cancellable =
6999 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7001 update_current_folder_mount_enclosing_volume_cb,
7007 if (!data->original_file)
7009 data->original_file = g_object_ref (data->file);
7010 data->original_error = g_error_copy (error);
7013 parent_file = g_file_get_parent (data->file);
7015 /* get parent path and try to change the folder to that */
7018 g_object_unref (data->file);
7019 data->file = parent_file;
7021 g_object_unref (cancellable);
7023 /* restart the update current folder operation */
7024 impl->reload_state = RELOAD_HAS_FOLDER;
7026 impl->update_current_folder_cancellable =
7027 _gtk_file_system_get_info (impl->file_system, data->file,
7029 update_current_folder_get_info_cb,
7032 set_busy_cursor (impl, TRUE);
7038 /* Error and bail out, ignoring "not found" errors since they're useless:
7039 * they only happen when a program defaults to a folder that has been (re)moved.
7041 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7042 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7044 g_error_free (data->original_error);
7045 g_object_unref (data->original_file);
7051 if (data->original_file)
7053 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7055 g_object_unref (data->original_file);
7058 if (! _gtk_file_info_consider_as_directory (info))
7061 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7064 if (impl->current_folder != data->file)
7066 if (impl->current_folder)
7067 g_object_unref (impl->current_folder);
7069 impl->current_folder = g_object_ref (data->file);
7072 impl->reload_state = RELOAD_HAS_FOLDER;
7074 /* Update the widgets that may trigger a folder change themselves. */
7076 if (!impl->changing_folder)
7078 impl->changing_folder = TRUE;
7080 shortcuts_update_current_folder (impl);
7082 impl->changing_folder = FALSE;
7085 /* Set the folder on the save entry */
7087 if (impl->location_entry)
7089 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7090 impl->current_folder);
7092 if (data->clear_entry)
7093 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7096 /* Create a new list model. This is slightly evil; we store the result value
7097 * but perform more actions rather than returning immediately even if it
7098 * generates an error.
7100 set_list_model (impl, NULL);
7102 /* Refresh controls */
7104 shortcuts_find_current_folder (impl);
7106 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7108 check_preview_change (impl);
7109 bookmarks_check_add_sensitivity (impl);
7111 g_signal_emit_by_name (impl, "selection-changed", 0);
7114 g_object_unref (data->file);
7117 g_object_unref (cancellable);
7121 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7123 gboolean keep_trail,
7124 gboolean clear_entry,
7127 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7128 struct UpdateCurrentFolderData *data;
7130 profile_start ("start", NULL);
7132 g_object_ref (file);
7134 switch (impl->operation_mode)
7136 case OPERATION_MODE_SEARCH:
7137 search_switch_to_browse_mode (impl);
7139 case OPERATION_MODE_RECENT:
7140 recent_switch_to_browse_mode (impl);
7142 case OPERATION_MODE_BROWSE:
7146 if (impl->local_only && !g_file_is_native (file))
7148 g_set_error_literal (error,
7149 GTK_FILE_CHOOSER_ERROR,
7150 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7151 _("Cannot change to folder because it is not local"));
7153 g_object_unref (file);
7154 profile_end ("end - not local", NULL);
7158 if (impl->update_current_folder_cancellable)
7159 g_cancellable_cancel (impl->update_current_folder_cancellable);
7161 /* Test validity of path here. */
7162 data = g_new0 (struct UpdateCurrentFolderData, 1);
7164 data->file = g_object_ref (file);
7165 data->keep_trail = keep_trail;
7166 data->clear_entry = clear_entry;
7168 impl->reload_state = RELOAD_HAS_FOLDER;
7170 impl->update_current_folder_cancellable =
7171 _gtk_file_system_get_info (impl->file_system, file,
7173 update_current_folder_get_info_cb,
7176 set_busy_cursor (impl, TRUE);
7177 g_object_unref (file);
7179 profile_end ("end", NULL);
7184 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7186 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7188 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7189 impl->operation_mode == OPERATION_MODE_RECENT)
7192 if (impl->reload_state == RELOAD_EMPTY)
7194 char *current_working_dir;
7197 /* We are unmapped, or we had an error while loading the last folder. We'll return
7198 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7199 * explicitly calls set_current_folder() on us.
7201 current_working_dir = g_get_current_dir ();
7202 file = g_file_new_for_path (current_working_dir);
7203 g_free (current_working_dir);
7207 if (impl->current_folder)
7208 return g_object_ref (impl->current_folder);
7214 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7217 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7219 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7220 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7222 pending_select_files_free (impl);
7223 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7227 select_func (GtkFileSystemModel *model,
7232 GtkFileChooserDefault *impl = user_data;
7233 GtkTreeSelection *selection;
7234 GtkTreeIter sorted_iter;
7236 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7238 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7239 gtk_tree_selection_select_iter (selection, &sorted_iter);
7243 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7247 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7251 parent_file = g_file_get_parent (file);
7254 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7256 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7257 impl->operation_mode == OPERATION_MODE_RECENT ||
7258 impl->load_state == LOAD_EMPTY)
7264 g_assert (impl->current_folder != NULL);
7266 same_path = g_file_equal (parent_file, impl->current_folder);
7269 if (same_path && impl->load_state == LOAD_FINISHED)
7274 files.data = (gpointer) file;
7277 result = show_and_select_files (impl, parent_file, &files, error);
7278 g_object_unref (parent_file);
7282 pending_select_files_add (impl, file);
7288 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7289 g_object_unref (parent_file);
7293 g_object_unref (parent_file);
7298 unselect_func (GtkFileSystemModel *model,
7303 GtkFileChooserDefault *impl = user_data;
7304 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7305 GtkTreePath *sorted_path;
7307 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7309 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7311 gtk_tree_path_free (sorted_path);
7315 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7318 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7320 if (!impl->browse_files_model)
7323 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7324 unselect_func, impl);
7328 maybe_select (GtkTreeModel *model,
7333 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7334 GtkTreeSelection *selection;
7338 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7340 info = get_list_file_info (impl, iter);
7341 is_folder = _gtk_file_info_consider_as_directory (info);
7343 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7344 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7345 gtk_tree_selection_select_iter (selection, iter);
7347 gtk_tree_selection_unselect_iter (selection, iter);
7353 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7355 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7357 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7358 impl->operation_mode == OPERATION_MODE_RECENT)
7360 GtkTreeSelection *selection;
7362 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7363 gtk_tree_selection_select_all (selection);
7367 if (impl->select_multiple)
7368 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7369 maybe_select, impl);
7373 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7375 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7376 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7378 gtk_tree_selection_unselect_all (selection);
7379 pending_select_files_free (impl);
7382 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7384 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7386 * is_empty_ret - whether the file entry is totally empty
7388 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7389 * the path will be "$cwd/foobar")
7392 check_save_entry (GtkFileChooserDefault *impl,
7394 gboolean *is_well_formed_ret,
7395 gboolean *is_empty_ret,
7396 gboolean *is_file_part_empty_ret,
7397 gboolean *is_folder)
7399 GtkFileChooserEntry *chooser_entry;
7400 GFile *current_folder;
7401 const char *file_part;
7405 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7406 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7407 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7408 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7409 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7411 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7413 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7416 *is_well_formed_ret = TRUE;
7417 *is_empty_ret = TRUE;
7418 *is_file_part_empty_ret = TRUE;
7424 *is_empty_ret = FALSE;
7426 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7427 if (!current_folder)
7430 *is_well_formed_ret = FALSE;
7431 *is_file_part_empty_ret = FALSE;
7437 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7439 if (!file_part || file_part[0] == '\0')
7441 *file_ret = g_object_ref (current_folder);
7442 *is_well_formed_ret = TRUE;
7443 *is_file_part_empty_ret = TRUE;
7449 *is_file_part_empty_ret = FALSE;
7452 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7456 error_building_filename_dialog (impl, error);
7458 *is_well_formed_ret = FALSE;
7465 *is_well_formed_ret = TRUE;
7466 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7469 struct get_files_closure {
7470 GtkFileChooserDefault *impl;
7472 GFile *file_from_entry;
7476 get_files_foreach (GtkTreeModel *model,
7481 struct get_files_closure *info;
7483 GtkFileSystemModel *fs_model;
7484 GtkTreeIter sel_iter;
7487 fs_model = info->impl->browse_files_model;
7488 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7490 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7492 return; /* We are on the editable row */
7494 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7495 info->result = g_slist_prepend (info->result, g_object_ref (file));
7499 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7501 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7502 struct get_files_closure info;
7503 GtkWindow *toplevel;
7504 GtkWidget *current_focus;
7505 gboolean file_list_seen;
7507 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7508 return search_get_selected_files (impl);
7510 if (impl->operation_mode == OPERATION_MODE_RECENT)
7511 return recent_get_selected_files (impl);
7515 info.file_from_entry = NULL;
7517 toplevel = get_toplevel (GTK_WIDGET (impl));
7519 current_focus = gtk_window_get_focus (toplevel);
7521 current_focus = NULL;
7523 file_list_seen = FALSE;
7524 if (current_focus == impl->browse_files_tree_view)
7526 GtkTreeSelection *selection;
7530 file_list_seen = TRUE;
7531 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7532 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7534 /* If there is no selection in the file list, we probably have this situation:
7536 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7537 * 2. He then double-clicked on a folder ("bar") in the file list
7539 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7540 * filename entry to see if that is the case.
7542 if (info.result == NULL && impl->location_entry)
7545 else if (impl->location_entry && current_focus == impl->location_entry)
7547 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7551 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7556 if (!is_well_formed)
7559 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7561 g_object_unref (info.file_from_entry);
7565 if (info.file_from_entry)
7566 info.result = g_slist_prepend (info.result, info.file_from_entry);
7567 else if (!file_list_seen)
7572 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7574 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7578 /* The focus is on a dialog's action area button or something else */
7579 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7580 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7588 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7589 * fall back to the current directory */
7590 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7591 info.result == NULL)
7593 GFile *current_folder;
7595 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7598 info.result = g_slist_prepend (info.result, current_folder);
7601 return g_slist_reverse (info.result);
7605 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7607 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7609 if (impl->preview_file)
7610 return g_object_ref (impl->preview_file);
7615 static GtkFileSystem *
7616 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7618 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7620 return impl->file_system;
7623 /* Shows or hides the filter widgets */
7625 show_filters (GtkFileChooserDefault *impl,
7629 gtk_widget_show (impl->filter_combo_hbox);
7631 gtk_widget_hide (impl->filter_combo_hbox);
7635 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7636 GtkFileFilter *filter)
7638 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7641 if (g_slist_find (impl->filters, filter))
7643 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7647 g_object_ref_sink (filter);
7648 impl->filters = g_slist_append (impl->filters, filter);
7650 name = gtk_file_filter_get_name (filter);
7652 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7654 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7656 if (!g_slist_find (impl->filters, impl->current_filter))
7657 set_current_filter (impl, filter);
7659 show_filters (impl, TRUE);
7663 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7664 GtkFileFilter *filter)
7666 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7667 GtkTreeModel *model;
7671 filter_index = g_slist_index (impl->filters, filter);
7673 if (filter_index < 0)
7675 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7679 impl->filters = g_slist_remove (impl->filters, filter);
7681 if (filter == impl->current_filter)
7684 set_current_filter (impl, impl->filters->data);
7686 set_current_filter (impl, NULL);
7689 /* Remove row from the combo box */
7690 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7691 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7692 g_assert_not_reached ();
7694 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7696 g_object_unref (filter);
7699 show_filters (impl, FALSE);
7703 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7705 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7707 return g_slist_copy (impl->filters);
7710 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7712 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7715 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7718 struct AddShortcutData
7720 GtkFileChooserDefault *impl;
7725 add_shortcut_get_info_cb (GCancellable *cancellable,
7727 const GError *error,
7731 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7732 struct AddShortcutData *data = user_data;
7734 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7737 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7739 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7742 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7744 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7747 g_object_unref (data->impl);
7748 g_object_unref (data->file);
7751 g_object_unref (cancellable);
7755 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7759 GCancellable *cancellable;
7760 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7761 struct AddShortcutData *data;
7765 /* Avoid adding duplicates */
7766 pos = shortcut_find_position (impl, file);
7767 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7771 uri = g_file_get_uri (file);
7772 /* translators, "Shortcut" means "Bookmark" here */
7774 GTK_FILE_CHOOSER_ERROR,
7775 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7776 _("Shortcut %s already exists"),
7783 for (l = impl->loading_shortcuts; l; l = l->next)
7785 GCancellable *c = l->data;
7788 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7789 if (f && g_file_equal (file, f))
7793 uri = g_file_get_uri (file);
7795 GTK_FILE_CHOOSER_ERROR,
7796 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7797 _("Shortcut %s already exists"),
7805 data = g_new0 (struct AddShortcutData, 1);
7806 data->impl = g_object_ref (impl);
7807 data->file = g_object_ref (file);
7809 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7811 add_shortcut_get_info_cb, data);
7816 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7817 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7823 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7827 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7834 for (l = impl->loading_shortcuts; l; l = l->next)
7836 GCancellable *c = l->data;
7839 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7840 if (f && g_file_equal (file, f))
7842 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7843 g_cancellable_cancel (c);
7848 if (impl->num_shortcuts == 0)
7851 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7852 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7853 g_assert_not_reached ();
7855 for (i = 0; i < impl->num_shortcuts; i++)
7858 ShortcutType shortcut_type;
7861 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7862 SHORTCUTS_COL_DATA, &col_data,
7863 SHORTCUTS_COL_TYPE, &shortcut_type,
7865 g_assert (col_data != NULL);
7866 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7868 shortcut = col_data;
7869 if (g_file_equal (shortcut, file))
7871 shortcuts_remove_rows (impl, pos + i, 1);
7872 impl->num_shortcuts--;
7876 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7877 g_assert_not_reached ();
7882 uri = g_file_get_uri (file);
7883 /* translators, "Shortcut" means "Bookmark" here */
7885 GTK_FILE_CHOOSER_ERROR,
7886 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7887 _("Shortcut %s does not exist"),
7895 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7897 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7903 if (impl->num_shortcuts == 0)
7906 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7907 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7908 g_assert_not_reached ();
7912 for (i = 0; i < impl->num_shortcuts; i++)
7915 ShortcutType shortcut_type;
7918 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7919 SHORTCUTS_COL_DATA, &col_data,
7920 SHORTCUTS_COL_TYPE, &shortcut_type,
7922 g_assert (col_data != NULL);
7923 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7925 shortcut = col_data;
7926 list = g_slist_prepend (list, g_object_ref (shortcut));
7928 if (i != impl->num_shortcuts - 1)
7930 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7931 g_assert_not_reached ();
7935 return g_slist_reverse (list);
7938 /* Guesses a size based upon font sizes */
7940 find_good_size_from_style (GtkWidget *widget,
7944 GtkFileChooserDefault *impl;
7949 g_assert (widget->style != NULL);
7950 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7952 screen = gtk_widget_get_screen (widget);
7955 resolution = gdk_screen_get_resolution (screen);
7956 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7960 resolution = 96.0; /* wheeee */
7962 font_size = pango_font_description_get_size (widget->style->font_desc);
7963 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7965 *width = font_size * NUM_CHARS;
7966 *height = font_size * NUM_LINES;
7970 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7971 gint *default_width,
7972 gint *default_height)
7974 GtkFileChooserDefault *impl;
7977 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7979 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7980 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7981 || impl->expand_folders)
7983 GtkFileChooserSettings *settings;
7984 int x, y, width, height;
7986 settings = _gtk_file_chooser_settings_new ();
7987 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
7988 g_object_unref (settings);
7990 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7992 *default_width = width;
7993 *default_height = height;
7997 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7999 if (impl->preview_widget_active &&
8000 impl->preview_widget &&
8001 GTK_WIDGET_VISIBLE (impl->preview_widget))
8003 gtk_widget_size_request (impl->preview_box, &req);
8004 *default_width += PREVIEW_HBOX_SPACING + req.width;
8007 if (impl->extra_widget &&
8008 GTK_WIDGET_VISIBLE (impl->extra_widget))
8010 gtk_widget_size_request (impl->extra_align, &req);
8011 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8016 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8017 *default_width = req.width;
8018 *default_height = req.height;
8022 struct switch_folder_closure {
8023 GtkFileChooserDefault *impl;
8028 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8030 switch_folder_foreach_cb (GtkTreeModel *model,
8035 struct switch_folder_closure *closure;
8036 GtkTreeIter child_iter;
8040 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
8042 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
8043 closure->num_selected++;
8046 /* Changes to the selected folder in the list view */
8048 switch_to_selected_folder (GtkFileChooserDefault *impl)
8050 GtkTreeSelection *selection;
8051 struct switch_folder_closure closure;
8053 /* We do this with foreach() rather than get_selected() as we may be in
8054 * multiple selection mode
8057 closure.impl = impl;
8058 closure.file = NULL;
8059 closure.num_selected = 0;
8061 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8062 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8064 g_assert (closure.file && closure.num_selected == 1);
8066 change_folder_and_display_error (impl, closure.file, FALSE);
8069 /* Gets the GFileInfo for the selected row in the file list; assumes single
8073 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8074 gboolean *had_selection)
8076 GtkTreeSelection *selection;
8077 GtkTreeIter iter, child_iter;
8080 g_assert (!impl->select_multiple);
8081 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8082 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8084 *had_selection = FALSE;
8088 *had_selection = TRUE;
8090 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8094 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8098 /* Gets the display name of the selected file in the file list; assumes single
8099 * selection mode and that something is selected.
8101 static const gchar *
8102 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8105 gboolean had_selection;
8107 info = get_selected_file_info_from_file_list (impl, &had_selection);
8108 g_assert (had_selection);
8109 g_assert (info != NULL);
8111 return g_file_info_get_display_name (info);
8115 add_custom_button_to_dialog (GtkDialog *dialog,
8116 const gchar *mnemonic_label,
8117 const gchar *stock_id,
8122 button = gtk_button_new_with_mnemonic (mnemonic_label);
8123 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8124 gtk_button_set_image (GTK_BUTTON (button),
8125 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8126 gtk_widget_show (button);
8128 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8131 /* Presents an overwrite confirmation dialog; returns whether we should accept
8135 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8136 const gchar *file_part,
8137 const gchar *folder_display_name)
8139 GtkWindow *toplevel;
8143 toplevel = get_toplevel (GTK_WIDGET (impl));
8145 dialog = gtk_message_dialog_new (toplevel,
8146 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8147 GTK_MESSAGE_QUESTION,
8149 _("A file named \"%s\" already exists. Do you want to replace it?"),
8151 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8152 _("The file already exists in \"%s\". Replacing it will "
8153 "overwrite its contents."),
8154 folder_display_name);
8156 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8157 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8158 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8159 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8160 GTK_RESPONSE_ACCEPT,
8161 GTK_RESPONSE_CANCEL,
8163 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8165 if (toplevel->group)
8166 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8168 response = gtk_dialog_run (GTK_DIALOG (dialog));
8170 gtk_widget_destroy (dialog);
8172 return (response == GTK_RESPONSE_ACCEPT);
8175 struct GetDisplayNameData
8177 GtkFileChooserDefault *impl;
8182 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8184 const GError *error,
8187 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8188 gboolean should_respond = FALSE;
8189 struct GetDisplayNameData *data = user_data;
8191 if (cancellable != data->impl->should_respond_get_info_cancellable)
8194 data->impl->should_respond_get_info_cancellable = NULL;
8200 /* Huh? Did the folder disappear? Let the caller deal with it */
8201 should_respond = TRUE;
8203 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8205 set_busy_cursor (data->impl, FALSE);
8207 g_signal_emit_by_name (data->impl, "response-requested");
8210 g_object_unref (data->impl);
8211 g_free (data->file_part);
8214 g_object_unref (cancellable);
8217 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8218 * should respond. Can get the file part from the file list or the save entry.
8221 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8222 const gchar *file_part,
8225 GtkFileChooserConfirmation conf;
8227 if (!impl->do_overwrite_confirmation)
8230 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8232 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8236 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8238 struct GetDisplayNameData *data;
8240 g_assert (file_part != NULL);
8242 data = g_new0 (struct GetDisplayNameData, 1);
8243 data->impl = g_object_ref (impl);
8244 data->file_part = g_strdup (file_part);
8246 if (impl->should_respond_get_info_cancellable)
8247 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8249 impl->should_respond_get_info_cancellable =
8250 _gtk_file_system_get_info (impl->file_system, parent_file,
8251 "standard::display-name",
8252 confirmation_confirm_get_info_cb,
8254 set_busy_cursor (data->impl, TRUE);
8258 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8261 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8265 g_assert_not_reached ();
8270 struct FileExistsData
8272 GtkFileChooserDefault *impl;
8273 gboolean file_exists_and_is_not_folder;
8279 save_entry_get_info_cb (GCancellable *cancellable,
8281 const GError *error,
8284 gboolean parent_is_folder;
8285 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8286 struct FileExistsData *data = user_data;
8288 if (cancellable != data->impl->should_respond_get_info_cancellable)
8291 data->impl->should_respond_get_info_cancellable = NULL;
8293 set_busy_cursor (data->impl, FALSE);
8299 parent_is_folder = FALSE;
8301 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8303 if (parent_is_folder)
8305 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8307 if (data->file_exists_and_is_not_folder)
8312 /* Dup the string because the string may be modified
8313 * depending on what clients do in the confirm-overwrite
8314 * signal and this corrupts the pointer
8316 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8317 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8321 g_signal_emit_by_name (data->impl, "response-requested");
8324 g_signal_emit_by_name (data->impl, "response-requested");
8326 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8328 GError *error = NULL;
8330 set_busy_cursor (data->impl, TRUE);
8331 g_file_make_directory (data->file, NULL, &error);
8332 set_busy_cursor (data->impl, FALSE);
8335 g_signal_emit_by_name (data->impl, "response-requested");
8337 error_creating_folder_dialog (data->impl, data->file, error);
8342 /* This will display an error, which is what we want */
8343 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8347 g_object_unref (data->impl);
8348 g_object_unref (data->file);
8349 g_object_unref (data->parent_file);
8352 g_object_unref (cancellable);
8356 file_exists_get_info_cb (GCancellable *cancellable,
8358 const GError *error,
8361 gboolean data_ownership_taken = FALSE;
8362 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8363 gboolean file_exists_and_is_not_folder;
8364 struct FileExistsData *data = user_data;
8366 if (cancellable != data->impl->file_exists_get_info_cancellable)
8369 data->impl->file_exists_get_info_cancellable = NULL;
8371 set_busy_cursor (data->impl, FALSE);
8376 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8378 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8379 /* user typed a filename; we are done */
8380 g_signal_emit_by_name (data->impl, "response-requested");
8381 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8382 && file_exists_and_is_not_folder)
8384 /* Oops, the user typed the name of an existing path which is not
8387 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8388 g_error_copy (error));
8392 /* check that everything up to the last component exists */
8394 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8395 data_ownership_taken = TRUE;
8397 if (data->impl->should_respond_get_info_cancellable)
8398 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8400 data->impl->should_respond_get_info_cancellable =
8401 _gtk_file_system_get_info (data->impl->file_system,
8404 save_entry_get_info_cb,
8406 set_busy_cursor (data->impl, TRUE);
8410 if (!data_ownership_taken)
8412 g_object_unref (data->impl);
8413 g_object_unref (data->file);
8414 g_object_unref (data->parent_file);
8418 g_object_unref (cancellable);
8422 paste_text_received (GtkClipboard *clipboard,
8424 GtkFileChooserDefault *impl)
8431 file = g_file_new_for_uri (text);
8433 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8434 location_popup_handler (impl, text);
8436 g_object_unref (file);
8439 /* Handler for the "location-popup-on-paste" keybinding signal */
8441 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8443 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8444 GDK_SELECTION_CLIPBOARD);
8445 gtk_clipboard_request_text (clipboard,
8446 (GtkClipboardTextReceivedFunc) paste_text_received,
8451 /* Implementation for GtkFileChooserEmbed::should_respond() */
8453 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8455 GtkFileChooserDefault *impl;
8456 GtkWidget *toplevel;
8457 GtkWidget *current_focus;
8459 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8461 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8462 g_assert (GTK_IS_WINDOW (toplevel));
8464 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8466 if (current_focus == impl->browse_files_tree_view)
8468 /* The following array encodes what we do based on the impl->action and the
8469 * number of files selected.
8472 NOOP, /* Do nothing (don't respond) */
8473 RESPOND, /* Respond immediately */
8474 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8475 ALL_FILES, /* Respond only if everything selected is a file */
8476 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8477 SAVE_ENTRY, /* Go to the code for handling the save entry */
8478 NOT_REACHED /* Sanity check */
8480 static const ActionToTake what_to_do[4][3] = {
8481 /* 0 selected 1 selected many selected */
8482 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8483 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8484 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8485 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8489 gboolean all_files, all_folders;
8491 ActionToTake action;
8495 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8497 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8498 return search_should_respond (impl);
8500 if (impl->operation_mode == OPERATION_MODE_RECENT)
8501 return recent_should_respond (impl);
8503 selection_check (impl, &num_selected, &all_files, &all_folders);
8505 if (num_selected > 2)
8510 action = what_to_do [impl->action] [k];
8520 case RESPOND_OR_SWITCH:
8521 g_assert (num_selected == 1);
8525 switch_to_selected_folder (impl);
8528 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8529 return should_respond_after_confirm_overwrite (impl,
8530 get_display_name_from_file_list (impl),
8531 impl->current_folder);
8545 g_assert_not_reached ();
8548 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8551 gboolean is_well_formed, is_empty, is_file_part_empty;
8554 GtkFileChooserEntry *entry;
8559 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8560 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8561 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8562 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8563 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8565 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8566 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8568 if (is_empty || !is_well_formed)
8571 g_assert (file != NULL);
8576 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8577 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8579 change_folder_and_display_error (impl, file, TRUE);
8582 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8583 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8585 /* The folder already exists, so we do not need to create it.
8586 * Just respond to terminate the dialog.
8592 g_assert_not_reached ();
8598 struct FileExistsData *data;
8600 /* We need to check whether file exists and is not a folder */
8602 data = g_new0 (struct FileExistsData, 1);
8603 data->impl = g_object_ref (impl);
8604 data->file = g_object_ref (file);
8605 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8607 if (impl->file_exists_get_info_cancellable)
8608 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8610 impl->file_exists_get_info_cancellable =
8611 _gtk_file_system_get_info (impl->file_system, file,
8613 file_exists_get_info_cb,
8616 set_busy_cursor (impl, TRUE);
8620 g_error_free (error);
8623 g_object_unref (file);
8626 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8628 /* The focus is on a dialog's action area button, *and* the widget that
8629 * was focused immediately before it is the file list.
8633 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8635 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8638 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8640 /* The focus is on a dialog's action area button, *and* the widget that
8641 * was focused immediately before it is the location entry.
8646 /* The focus is on a dialog's action area button or something else */
8647 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8648 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8653 g_assert_not_reached ();
8657 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8659 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8661 GtkFileChooserDefault *impl;
8664 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8666 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8667 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8669 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8670 widget = impl->browse_files_tree_view;
8672 widget = impl->location_entry;
8674 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8675 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8676 widget = impl->location_entry;
8679 g_assert_not_reached ();
8683 g_assert (widget != NULL);
8684 gtk_widget_grab_focus (widget);
8687 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8689 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8699 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8700 *list = g_slist_prepend (*list, g_object_ref (file));
8703 /* Constructs a list of the selected paths in search mode */
8705 search_get_selected_files (GtkFileChooserDefault *impl)
8708 GtkTreeSelection *selection;
8712 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8713 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8714 result = g_slist_reverse (result);
8719 /* Called from ::should_respond(). We return whether there are selected files
8720 * in the search list.
8723 search_should_respond (GtkFileChooserDefault *impl)
8725 GtkTreeSelection *selection;
8727 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8729 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8730 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8733 struct SearchHitInsertRequest
8735 GtkFileChooserDefault *impl;
8737 GtkTreeRowReference *row_ref;
8741 search_hit_get_info_cb (GCancellable *cancellable,
8743 const GError *error,
8746 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8747 GdkPixbuf *pixbuf = NULL;
8750 GCancellable *model_cancellable;
8751 gboolean is_folder = FALSE;
8753 guint64 modification_time = 0;
8757 struct SearchHitInsertRequest *request = data;
8759 if (!request->impl->search_model)
8762 path = gtk_tree_row_reference_get_path (request->row_ref);
8766 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8768 gtk_tree_path_free (path);
8770 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8771 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8773 if (cancellable != model_cancellable)
8776 /* set the cancellable to NULL in the model */
8777 gtk_list_store_set (request->impl->search_model, &iter,
8778 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8786 search_clear_model_row (GTK_TREE_MODEL (request->impl->search_model), &iter);
8787 gtk_list_store_remove (request->impl->search_model, &iter);
8791 display_name = g_strdup (g_file_info_get_display_name (info));
8792 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
8793 g_file_info_get_modification_time (info, &mtime);
8794 modification_time = (guint64) mtime.tv_sec;
8795 size = g_file_info_get_size (info);
8796 is_folder = _gtk_file_info_consider_as_directory (info);
8797 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8798 request->impl->icon_size);
8800 gtk_list_store_set (request->impl->search_model, &iter,
8801 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8802 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8803 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8804 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8805 SEARCH_MODEL_COL_MTIME, modification_time,
8806 SEARCH_MODEL_COL_SIZE, size,
8810 g_object_unref (pixbuf);
8813 g_object_unref (request->impl);
8814 g_object_unref (request->file);
8815 gtk_tree_row_reference_free (request->row_ref);
8818 g_object_unref (cancellable);
8821 /* Adds one hit from the search engine to the search_model */
8823 search_add_hit (GtkFileChooserDefault *impl,
8828 char *collation_key;
8831 GCancellable *cancellable;
8832 struct SearchHitInsertRequest *request;
8834 file = g_file_new_for_uri (uri);
8838 if (!g_file_is_native (file))
8840 g_object_unref (file);
8844 tmp = g_file_get_parse_name (file);
8845 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8848 request = g_new0 (struct SearchHitInsertRequest, 1);
8849 request->impl = g_object_ref (impl);
8850 request->file = g_object_ref (file);
8852 gtk_list_store_append (impl->search_model, &iter);
8853 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8855 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8856 gtk_tree_path_free (p);
8858 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8861 "standard::content-type,"
8862 "standard::display-name,"
8865 search_hit_get_info_cb,
8868 gtk_list_store_set (impl->search_model, &iter,
8869 SEARCH_MODEL_COL_FILE, file,
8870 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8871 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8875 /* Callback used from GtkSearchEngine when we get new hits */
8877 search_engine_hits_added_cb (GtkSearchEngine *engine,
8881 GtkFileChooserDefault *impl;
8884 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8886 for (l = hits; l; l = l->next)
8887 search_add_hit (impl, (gchar*)l->data);
8890 /* Callback used from GtkSearchEngine when the query is done running */
8892 search_engine_finished_cb (GtkSearchEngine *engine,
8895 GtkFileChooserDefault *impl;
8897 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8900 /* EB: setting the model here will avoid loads of row events,
8901 * but it'll make the search look like blocked.
8903 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8904 GTK_TREE_MODEL (impl->search_model_filter));
8907 /* FMQ: if search was empty, say that we got no hits */
8908 set_busy_cursor (impl, FALSE);
8911 /* Displays a generic error when we cannot create a GtkSearchEngine.
8912 * It would be better if _gtk_search_engine_new() gave us a GError
8913 * with a better message, but it doesn't do that right now.
8916 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8918 error_message (impl,
8919 _("Could not start the search process"),
8920 _("The program was not able to create a connection to the indexer "
8921 "daemon. Please make sure it is running."));
8925 search_engine_error_cb (GtkSearchEngine *engine,
8926 const gchar *message,
8929 GtkFileChooserDefault *impl;
8931 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8933 search_stop_searching (impl, TRUE);
8934 error_message (impl, _("Could not send the search request"), message);
8936 set_busy_cursor (impl, FALSE);
8940 search_clear_model_row (GtkTreeModel *model,
8944 gchar *display_name;
8945 gchar *collation_key;
8946 GCancellable *cancellable;
8949 gtk_tree_model_get (model, iter,
8950 SEARCH_MODEL_COL_FILE, &file,
8951 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8952 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
8953 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8954 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8958 g_object_unref (file);
8960 g_free (display_name);
8961 g_free (collation_key);
8965 g_cancellable_cancel (cancellable);
8968 /* Frees the data in the search_model */
8970 search_clear_model (GtkFileChooserDefault *impl,
8971 gboolean remove_from_treeview)
8973 GtkTreeModel *model;
8976 if (!impl->search_model)
8979 model = GTK_TREE_MODEL (impl->search_model);
8981 if (gtk_tree_model_get_iter_first (model, &iter))
8984 search_clear_model_row (model, &iter);
8986 while (gtk_tree_model_iter_next (model, &iter));
8988 g_object_unref (impl->search_model);
8989 impl->search_model = NULL;
8991 g_object_unref (impl->search_model_filter);
8992 impl->search_model_filter = NULL;
8994 g_object_unref (impl->search_model_sort);
8995 impl->search_model_sort = NULL;
8997 if (remove_from_treeview)
8998 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9001 /* Stops any ongoing searches; does not touch the search_model */
9003 search_stop_searching (GtkFileChooserDefault *impl,
9004 gboolean remove_query)
9006 if (remove_query && impl->search_query)
9008 g_object_unref (impl->search_query);
9009 impl->search_query = NULL;
9012 if (impl->search_engine)
9014 _gtk_search_engine_stop (impl->search_engine);
9016 g_object_unref (impl->search_engine);
9017 impl->search_engine = NULL;
9021 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9023 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9025 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9027 search_stop_searching (impl, FALSE);
9028 search_clear_model (impl, TRUE);
9030 gtk_widget_destroy (impl->search_hbox);
9031 impl->search_hbox = NULL;
9032 impl->search_entry = NULL;
9034 gtk_widget_show (impl->browse_path_bar);
9035 gtk_widget_show (impl->browse_new_folder_button);
9037 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9038 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9040 gtk_widget_show (impl->location_button);
9042 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9043 gtk_widget_show (impl->location_entry_box);
9046 impl->operation_mode = OPERATION_MODE_BROWSE;
9048 file_list_set_sort_column_ids (impl);
9051 /* Sort callback from the path column */
9053 search_column_path_sort_func (GtkTreeModel *model,
9058 GtkFileChooserDefault *impl = user_data;
9059 GtkTreeIter child_a, child_b;
9060 const char *collation_key_a, *collation_key_b;
9061 gboolean is_folder_a, is_folder_b;
9063 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9064 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9066 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9067 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9068 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
9070 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9071 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9072 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
9075 if (!collation_key_a)
9078 if (!collation_key_b)
9081 /* always show folders first */
9082 if (is_folder_a != is_folder_b)
9083 return is_folder_a ? 1 : -1;
9085 return strcmp (collation_key_a, collation_key_b);
9088 /* Sort callback from the size column */
9090 search_column_size_sort_func (GtkTreeModel *model,
9095 GtkFileChooserDefault *impl = user_data;
9096 GtkTreeIter child_a, child_b;
9097 gboolean is_folder_a, is_folder_b;
9098 goffset size_a, size_b;
9100 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9101 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9103 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9104 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9105 SEARCH_MODEL_COL_SIZE, &size_a,
9107 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9108 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9109 SEARCH_MODEL_COL_SIZE, &size_b,
9112 if (is_folder_a != is_folder_b)
9113 return is_folder_a ? 1 : -1;
9115 if (size_a < size_b)
9117 else if (size_a > size_b)
9123 /* Sort callback from the modification time column */
9125 search_column_mtime_sort_func (GtkTreeModel *model,
9130 GtkFileChooserDefault *impl = user_data;
9131 GtkTreeIter child_a, child_b;
9132 gboolean is_folder_a, is_folder_b;
9133 guint64 mtime_a, mtime_b;
9135 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9136 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9138 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9139 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9140 SEARCH_MODEL_COL_MTIME, &mtime_a,
9142 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9143 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9144 SEARCH_MODEL_COL_MTIME, &mtime_b,
9147 if (is_folder_a != is_folder_b)
9148 return is_folder_a ? 1 : -1;
9150 if (mtime_a < mtime_b)
9152 else if (mtime_a > mtime_b)
9159 search_get_is_filtered (GtkFileChooserDefault *impl,
9161 const gchar *display_name,
9162 const gchar *mime_type)
9164 GtkFileFilterInfo filter_info;
9165 GtkFileFilterFlags needed;
9168 if (!impl->current_filter)
9171 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9172 needed = gtk_file_filter_get_needed (impl->current_filter);
9174 filter_info.display_name = display_name;
9175 filter_info.mime_type = mime_type;
9177 if (needed & GTK_FILE_FILTER_FILENAME)
9179 filter_info.filename = g_file_get_path (file);
9180 if (filter_info.filename)
9181 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9184 filter_info.filename = NULL;
9186 if (needed & GTK_FILE_FILTER_URI)
9188 filter_info.uri = g_file_get_uri (file);
9189 if (filter_info.uri)
9190 filter_info.contains |= GTK_FILE_FILTER_URI;
9193 filter_info.uri = NULL;
9195 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9197 if (filter_info.filename)
9198 g_free ((gchar *) filter_info.filename);
9199 if (filter_info.uri)
9200 g_free ((gchar *) filter_info.uri);
9206 /* Visibility function for the recent filter model */
9208 search_model_visible_func (GtkTreeModel *model,
9212 GtkFileChooserDefault *impl = user_data;
9214 gchar *display_name, *mime_type;
9217 if (!impl->current_filter)
9220 gtk_tree_model_get (model, iter,
9221 SEARCH_MODEL_COL_FILE, &file,
9222 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9223 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9224 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9233 return !search_get_is_filtered (impl, file, display_name, mime_type);
9236 /* Creates the search_model and puts it in the tree view */
9238 search_setup_model (GtkFileChooserDefault *impl)
9240 g_assert (impl->search_model == NULL);
9241 g_assert (impl->search_model_filter == NULL);
9242 g_assert (impl->search_model_sort == NULL);
9244 /* We store these columns in the search model:
9246 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9247 * as a pointer not as a G_TYPE_FILE
9248 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9249 * as a pointer not as a G_TYPE_STRING
9250 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9251 * as a pointer not as a G_TYPE_STRING
9252 * SEARCH_MODEL_COL_MTIME - G_TYPE_UINT64 for the modification time
9253 * SEARCH_MODEL_COL_SIZE - G_TYPE_INT64 for the size
9254 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9255 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9256 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9257 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9259 * Keep this in sync with the enumeration defined near the beginning
9262 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9263 G_TYPE_POINTER, /* file */
9264 G_TYPE_POINTER, /* display-name */
9265 G_TYPE_POINTER, /* collation-key */
9266 G_TYPE_UINT64, /* mtime */
9267 G_TYPE_INT64, /* size */
9268 G_TYPE_POINTER, /* cancellable */
9269 GDK_TYPE_PIXBUF, /* pixbuf */
9270 G_TYPE_POINTER, /* mime-type */
9271 G_TYPE_BOOLEAN /*is-folder */);
9273 impl->search_model_filter =
9274 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9275 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9276 search_model_visible_func,
9279 impl->search_model_sort =
9280 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9281 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9282 SEARCH_MODEL_COL_FILE,
9283 search_column_path_sort_func,
9285 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9286 SEARCH_MODEL_COL_MTIME,
9287 search_column_mtime_sort_func,
9289 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9290 SEARCH_MODEL_COL_SIZE,
9291 search_column_size_sort_func,
9293 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9294 SEARCH_MODEL_COL_MTIME,
9295 GTK_SORT_DESCENDING);
9297 /* EB: setting the model here will make the hits list update feel
9298 * more "alive" than setting the model at the end of the search
9301 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9302 GTK_TREE_MODEL (impl->search_model_sort));
9306 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9307 GtkTreeIter *child_iter,
9312 if (!impl->search_model)
9315 if (!impl->search_model_filter || !impl->search_model_sort)
9318 /* pass 1: get the iterator in the filter model */
9319 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9322 /* pass 2: get the iterator in the real model */
9323 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9324 child_iter, &middle);
9327 /* Creates a new query with the specified text and launches it */
9329 search_start_query (GtkFileChooserDefault *impl,
9330 const gchar *query_text)
9332 search_stop_searching (impl, FALSE);
9333 search_clear_model (impl, TRUE);
9334 search_setup_model (impl);
9335 set_busy_cursor (impl, TRUE);
9337 if (impl->search_engine == NULL)
9338 impl->search_engine = _gtk_search_engine_new ();
9340 if (!impl->search_engine)
9342 set_busy_cursor (impl, FALSE);
9343 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9347 if (!impl->search_query)
9349 impl->search_query = _gtk_query_new ();
9350 _gtk_query_set_text (impl->search_query, query_text);
9353 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9355 g_signal_connect (impl->search_engine, "hits-added",
9356 G_CALLBACK (search_engine_hits_added_cb), impl);
9357 g_signal_connect (impl->search_engine, "finished",
9358 G_CALLBACK (search_engine_finished_cb), impl);
9359 g_signal_connect (impl->search_engine, "error",
9360 G_CALLBACK (search_engine_error_cb), impl);
9362 _gtk_search_engine_start (impl->search_engine);
9365 /* Callback used when the user presses Enter while typing on the search
9366 * entry; starts the query
9369 search_entry_activate_cb (GtkEntry *entry,
9372 GtkFileChooserDefault *impl;
9375 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9377 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9378 if (strlen (text) == 0)
9381 /* reset any existing query object */
9382 if (impl->search_query)
9384 g_object_unref (impl->search_query);
9385 impl->search_query = NULL;
9388 search_start_query (impl, text);
9391 /* Hides the path bar and creates the search entry */
9393 search_setup_widgets (GtkFileChooserDefault *impl)
9397 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9401 label = gtk_label_new_with_mnemonic (_("_Search:"));
9402 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9406 impl->search_entry = gtk_entry_new ();
9407 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9408 g_signal_connect (impl->search_entry, "activate",
9409 G_CALLBACK (search_entry_activate_cb),
9411 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9413 /* if there already is a query, restart it */
9414 if (impl->search_query)
9416 gchar *query = _gtk_query_get_text (impl->search_query);
9420 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9421 search_start_query (impl, query);
9427 g_object_unref (impl->search_query);
9428 impl->search_query = NULL;
9432 gtk_widget_hide (impl->browse_path_bar);
9433 gtk_widget_hide (impl->browse_new_folder_button);
9435 /* Box for search widgets */
9436 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9437 gtk_widget_show_all (impl->search_hbox);
9439 /* Hide the location widgets temporarily */
9441 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9442 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9444 gtk_widget_hide (impl->location_button);
9445 gtk_widget_hide (impl->location_entry_box);
9448 gtk_widget_grab_focus (impl->search_entry);
9450 /* FMQ: hide the filter combo? */
9453 /* Main entry point to the searching functions; this gets called when the user
9454 * activates the Search shortcut.
9457 search_activate (GtkFileChooserDefault *impl)
9459 OperationMode previous_mode;
9461 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9463 gtk_widget_grab_focus (impl->search_entry);
9467 previous_mode = impl->operation_mode;
9468 impl->operation_mode = OPERATION_MODE_SEARCH;
9470 switch (previous_mode)
9472 case OPERATION_MODE_RECENT:
9473 recent_stop_loading (impl);
9474 recent_clear_model (impl, TRUE);
9477 case OPERATION_MODE_BROWSE:
9478 stop_loading_and_clear_list_model (impl);
9481 case OPERATION_MODE_SEARCH:
9482 g_assert_not_reached ();
9486 g_assert (impl->search_hbox == NULL);
9487 g_assert (impl->search_entry == NULL);
9488 g_assert (impl->search_model == NULL);
9489 g_assert (impl->search_model_filter == NULL);
9491 search_setup_widgets (impl);
9492 file_list_set_sort_column_ids (impl);
9496 * Recent files support
9499 /* Frees the data in the recent_model */
9501 recent_clear_model (GtkFileChooserDefault *impl,
9502 gboolean remove_from_treeview)
9504 GtkTreeModel *model;
9507 if (!impl->recent_model)
9510 model = GTK_TREE_MODEL (impl->recent_model);
9512 if (remove_from_treeview)
9513 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9515 if (gtk_tree_model_get_iter_first (model, &iter))
9520 GCancellable *cancellable;
9521 GtkRecentInfo *recent_info;
9522 gchar *display_name;
9524 gtk_tree_model_get (model, &iter,
9525 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9526 RECENT_MODEL_COL_FILE, &file,
9527 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9528 RECENT_MODEL_COL_INFO, &recent_info,
9532 g_cancellable_cancel (cancellable);
9534 g_object_unref (file);
9535 gtk_recent_info_unref (recent_info);
9536 g_free (display_name);
9538 while (gtk_tree_model_iter_next (model, &iter));
9541 g_object_unref (impl->recent_model);
9542 impl->recent_model = NULL;
9544 g_object_unref (impl->recent_model_filter);
9545 impl->recent_model_filter = NULL;
9547 g_object_unref (impl->recent_model_sort);
9548 impl->recent_model_sort = NULL;
9551 /* Stops any ongoing loading of the recent files list; does
9552 * not touch the recent_model
9555 recent_stop_loading (GtkFileChooserDefault *impl)
9557 if (impl->load_recent_id)
9559 g_source_remove (impl->load_recent_id);
9560 impl->load_recent_id = 0;
9564 /* Stops any pending load, clears the file list, and switches
9565 * back to OPERATION_MODE_BROWSE
9568 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9570 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9572 recent_stop_loading (impl);
9573 recent_clear_model (impl, TRUE);
9575 gtk_widget_show (impl->browse_path_bar);
9576 gtk_widget_show (impl->browse_new_folder_button);
9578 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9579 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9581 gtk_widget_show (impl->location_button);
9583 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9584 gtk_widget_show (impl->location_entry_box);
9587 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9589 impl->operation_mode = OPERATION_MODE_BROWSE;
9591 file_list_set_sort_column_ids (impl);
9594 /* Sort callback from the modification time column */
9596 recent_column_mtime_sort_func (GtkTreeModel *model,
9601 GtkFileChooserDefault *impl = user_data;
9602 GtkTreeIter child_a, child_b;
9603 GtkRecentInfo *info_a, *info_b;
9604 gboolean is_folder_a, is_folder_b;
9606 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9607 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9609 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9610 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9611 RECENT_MODEL_COL_INFO, &info_a,
9613 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9614 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9615 RECENT_MODEL_COL_INFO, &info_b,
9624 /* folders always go first */
9625 if (is_folder_a != is_folder_b)
9626 return is_folder_a ? 1 : -1;
9628 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9630 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9637 recent_column_path_sort_func (GtkTreeModel *model,
9642 GtkFileChooserDefault *impl = user_data;
9643 GtkTreeIter child_a, child_b;
9644 gboolean is_folder_a, is_folder_b;
9645 gchar *name_a, *name_b;
9647 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9648 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9650 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9651 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9652 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9654 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9655 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9656 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9665 if (is_folder_a != is_folder_b)
9666 return is_folder_a ? 1 : -1;
9668 return strcmp (name_a, name_b);
9672 recent_get_is_filtered (GtkFileChooserDefault *impl,
9674 GtkRecentInfo *recent_info)
9676 GtkFileFilterInfo filter_info;
9677 GtkFileFilterFlags needed;
9680 if (!impl->current_filter)
9683 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9684 needed = gtk_file_filter_get_needed (impl->current_filter);
9686 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9687 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9689 if (needed & GTK_FILE_FILTER_FILENAME)
9691 filter_info.filename = g_file_get_path (file);
9692 if (filter_info.filename)
9693 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9696 filter_info.filename = NULL;
9698 if (needed & GTK_FILE_FILTER_URI)
9700 filter_info.uri = g_file_get_uri (file);
9701 if (filter_info.uri)
9702 filter_info.contains |= GTK_FILE_FILTER_URI;
9705 filter_info.uri = NULL;
9707 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9709 if (filter_info.filename)
9710 g_free ((gchar *) filter_info.filename);
9711 if (filter_info.uri)
9712 g_free ((gchar *) filter_info.uri);
9717 /* Visibility function for the recent filter model */
9719 recent_model_visible_func (GtkTreeModel *model,
9723 GtkFileChooserDefault *impl = user_data;
9725 GtkRecentInfo *recent_info;
9728 if (!impl->current_filter)
9731 gtk_tree_model_get (model, iter,
9732 RECENT_MODEL_COL_INFO, &recent_info,
9733 RECENT_MODEL_COL_FILE, &file,
9734 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9743 return !recent_get_is_filtered (impl, file, recent_info);
9747 recent_setup_model (GtkFileChooserDefault *impl)
9749 g_assert (impl->recent_model == NULL);
9750 g_assert (impl->recent_model_filter == NULL);
9751 g_assert (impl->recent_model_sort == NULL);
9753 /* We store these columns in the search model:
9755 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9756 * stored as a pointer and not as a G_TYPE_FILE;
9757 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9758 * stored as a pointer and not as a G_TYPE_STRING;
9759 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9760 * as a GTK_TYPE_RECENT_INFO;
9761 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9762 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9763 * and not as a G_TYPE_CANCELLABLE;
9765 * Keep this in sync with the enumeration defined near the beginning of
9768 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9775 impl->recent_model_filter =
9776 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9777 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9778 recent_model_visible_func,
9782 /* this is the model that will actually be added to
9783 * the browse_files_tree_view widget; remember: we are
9784 * stuffing the real model into a filter model and then
9785 * into a sort model; this means we'll have to translate
9786 * the child iterator *twice* to get from a path or an
9787 * iterator coming from the tree view widget to the
9788 * real data inside the model.
9790 impl->recent_model_sort =
9791 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9792 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9793 RECENT_MODEL_COL_FILE,
9794 recent_column_path_sort_func,
9796 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9797 RECENT_MODEL_COL_INFO,
9798 recent_column_mtime_sort_func,
9800 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9801 RECENT_MODEL_COL_INFO,
9802 GTK_SORT_DESCENDING);
9807 GtkFileChooserDefault *impl;
9810 gint n_loaded_items;
9811 guint needs_sorting : 1;
9815 recent_idle_cleanup (gpointer data)
9817 RecentLoadData *load_data = data;
9818 GtkFileChooserDefault *impl = load_data->impl;
9820 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9821 GTK_TREE_MODEL (impl->recent_model_sort));
9823 set_busy_cursor (impl, FALSE);
9825 impl->load_recent_id = 0;
9827 if (load_data->items)
9829 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9830 g_list_free (load_data->items);
9836 struct RecentItemInsertRequest
9838 GtkFileChooserDefault *impl;
9840 GtkTreeRowReference *row_ref;
9844 recent_item_get_info_cb (GCancellable *cancellable,
9846 const GError *error,
9849 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9852 GCancellable *model_cancellable;
9853 gboolean is_folder = FALSE;
9854 struct RecentItemInsertRequest *request = data;
9856 if (!request->impl->recent_model)
9859 path = gtk_tree_row_reference_get_path (request->row_ref);
9863 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9865 gtk_tree_path_free (path);
9867 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9868 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9870 if (cancellable != model_cancellable)
9873 gtk_list_store_set (request->impl->recent_model, &iter,
9874 RECENT_MODEL_COL_CANCELLABLE, NULL,
9882 gtk_list_store_remove (request->impl->recent_model, &iter);
9886 is_folder = _gtk_file_info_consider_as_directory (info);
9888 gtk_list_store_set (request->impl->recent_model, &iter,
9889 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9893 g_object_unref (request->impl);
9894 g_object_unref (request->file);
9895 gtk_tree_row_reference_free (request->row_ref);
9898 g_object_unref (cancellable);
9902 recent_sort_mru (gconstpointer a,
9905 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9906 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9908 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9912 get_recent_files_limit (GtkWidget *widget)
9914 GtkSettings *settings;
9917 if (gtk_widget_has_screen (widget))
9918 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9920 settings = gtk_settings_get_default ();
9922 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9928 recent_idle_load (gpointer data)
9930 RecentLoadData *load_data = data;
9931 GtkFileChooserDefault *impl = load_data->impl;
9934 GtkRecentInfo *info;
9935 const gchar *uri, *display_name;
9937 GCancellable *cancellable;
9938 struct RecentItemInsertRequest *request;
9940 if (!impl->recent_manager)
9943 /* first iteration: load all the items */
9944 if (!load_data->items)
9946 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9947 if (!load_data->items)
9950 load_data->needs_sorting = TRUE;
9955 /* second iteration: preliminary MRU sorting and clamping */
9956 if (load_data->needs_sorting)
9960 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9961 load_data->n_items = g_list_length (load_data->items);
9963 limit = get_recent_files_limit (GTK_WIDGET (impl));
9965 if (limit != -1 && (load_data->n_items > limit))
9969 clamp = g_list_nth (load_data->items, limit - 1);
9970 if (G_LIKELY (clamp))
9975 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9978 load_data->n_items = limit;
9982 load_data->n_loaded_items = 0;
9983 load_data->needs_sorting = FALSE;
9988 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9989 g_assert (info != NULL);
9991 uri = gtk_recent_info_get_uri (info);
9992 display_name = gtk_recent_info_get_display_name (info);
9993 file = g_file_new_for_uri (uri);
9995 gtk_list_store_append (impl->recent_model, &iter);
9996 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9998 request = g_new0 (struct RecentItemInsertRequest, 1);
9999 request->impl = g_object_ref (impl);
10000 request->file = g_object_ref (file);
10001 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
10002 gtk_tree_path_free (p);
10004 cancellable = _gtk_file_system_get_info (impl->file_system, file,
10006 recent_item_get_info_cb,
10009 gtk_list_store_set (impl->recent_model, &iter,
10010 RECENT_MODEL_COL_FILE, file,
10011 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
10012 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
10013 RECENT_MODEL_COL_CANCELLABLE, cancellable,
10016 load_data->n_loaded_items += 1;
10018 /* finished loading items */
10019 if (load_data->n_loaded_items == load_data->n_items)
10021 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10022 g_list_free (load_data->items);
10023 load_data->items = NULL;
10032 recent_start_loading (GtkFileChooserDefault *impl)
10034 RecentLoadData *load_data;
10036 recent_stop_loading (impl);
10037 recent_clear_model (impl, TRUE);
10038 recent_setup_model (impl);
10039 set_busy_cursor (impl, TRUE);
10041 g_assert (impl->load_recent_id == 0);
10043 load_data = g_new (RecentLoadData, 1);
10044 load_data->impl = impl;
10045 load_data->items = NULL;
10046 load_data->n_items = 0;
10047 load_data->n_loaded_items = 0;
10048 load_data->needs_sorting = TRUE;
10050 /* begin lazy loading the recent files into the model */
10051 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
10054 recent_idle_cleanup);
10058 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
10068 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
10069 *list = g_slist_prepend (*list, g_object_ref (file));
10072 /* Constructs a list of the selected paths in recent files mode */
10074 recent_get_selected_files (GtkFileChooserDefault *impl)
10077 GtkTreeSelection *selection;
10081 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10082 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
10083 result = g_slist_reverse (result);
10088 /* Called from ::should_respond(). We return whether there are selected
10089 * files in the recent files list.
10092 recent_should_respond (GtkFileChooserDefault *impl)
10094 GtkTreeSelection *selection;
10096 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10098 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10099 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10102 /* Hide the location widgets temporarily */
10104 recent_hide_entry (GtkFileChooserDefault *impl)
10106 gtk_widget_hide (impl->browse_path_bar);
10107 gtk_widget_hide (impl->browse_new_folder_button);
10109 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10110 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10112 gtk_widget_hide (impl->location_button);
10113 gtk_widget_hide (impl->location_entry_box);
10117 /* Main entry point to the recent files functions; this gets called when
10118 * the user activates the Recently Used shortcut.
10121 recent_activate (GtkFileChooserDefault *impl)
10123 OperationMode previous_mode;
10125 if (impl->operation_mode == OPERATION_MODE_RECENT)
10128 previous_mode = impl->operation_mode;
10129 impl->operation_mode = OPERATION_MODE_RECENT;
10131 switch (previous_mode)
10133 case OPERATION_MODE_SEARCH:
10134 search_stop_searching (impl, FALSE);
10135 search_clear_model (impl, TRUE);
10137 gtk_widget_destroy (impl->search_hbox);
10138 impl->search_hbox = NULL;
10139 impl->search_entry = NULL;
10142 case OPERATION_MODE_BROWSE:
10143 stop_loading_and_clear_list_model (impl);
10146 case OPERATION_MODE_RECENT:
10147 g_assert_not_reached ();
10151 recent_hide_entry (impl);
10153 /* hide the file size column if it's visible */
10154 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
10156 file_list_set_sort_column_ids (impl);
10157 recent_start_loading (impl);
10160 /* convert an iterator coming from the model bound to
10161 * browse_files_tree_view to an interator inside the
10162 * real recent_model
10165 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10166 GtkTreeIter *child_iter,
10169 GtkTreeIter middle;
10171 if (!impl->recent_model)
10174 if (!impl->recent_model_filter || !impl->recent_model_sort)
10177 /* pass 1: get the iterator in the filter model */
10178 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10181 /* pass 2: get the iterator in the real model */
10182 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10189 set_current_filter (GtkFileChooserDefault *impl,
10190 GtkFileFilter *filter)
10192 if (impl->current_filter != filter)
10196 /* NULL filters are allowed to reset to non-filtered status
10198 filter_index = g_slist_index (impl->filters, filter);
10199 if (impl->filters && filter && filter_index < 0)
10202 if (impl->current_filter)
10203 g_object_unref (impl->current_filter);
10204 impl->current_filter = filter;
10205 if (impl->current_filter)
10207 g_object_ref_sink (impl->current_filter);
10211 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10214 if (impl->browse_files_model)
10215 install_list_model_filter (impl);
10217 if (impl->search_model_filter)
10218 gtk_tree_model_filter_refilter (impl->search_model_filter);
10220 if (impl->recent_model_filter)
10221 gtk_tree_model_filter_refilter (impl->recent_model_filter);
10223 g_object_notify (G_OBJECT (impl), "filter");
10228 filter_combo_changed (GtkComboBox *combo_box,
10229 GtkFileChooserDefault *impl)
10231 gint new_index = gtk_combo_box_get_active (combo_box);
10232 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10234 set_current_filter (impl, new_filter);
10238 check_preview_change (GtkFileChooserDefault *impl)
10240 GtkTreePath *cursor_path;
10242 const char *new_display_name;
10244 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10246 new_display_name = NULL;
10249 GtkTreeIter child_iter;
10251 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10253 if (impl->sort_model)
10256 GFileInfo *new_info;
10258 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10259 gtk_tree_path_free (cursor_path);
10261 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10263 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10264 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10266 new_display_name = g_file_info_get_display_name (new_info);
10269 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10273 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10274 &iter, cursor_path);
10275 gtk_tree_path_free (cursor_path);
10277 search_get_valid_child_iter (impl, &child_iter, &iter);
10278 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10279 SEARCH_MODEL_COL_FILE, &new_file,
10280 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10283 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10287 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10288 &iter, cursor_path);
10289 gtk_tree_path_free (cursor_path);
10291 recent_get_valid_child_iter (impl, &child_iter, &iter);
10292 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10293 RECENT_MODEL_COL_FILE, &new_file,
10294 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10299 if (new_file != impl->preview_file &&
10300 !(new_file && impl->preview_file &&
10301 g_file_equal (new_file, impl->preview_file)))
10303 if (impl->preview_file)
10305 g_object_unref (impl->preview_file);
10306 g_free (impl->preview_display_name);
10311 impl->preview_file = g_object_ref (new_file);
10312 impl->preview_display_name = g_strdup (new_display_name);
10316 impl->preview_file = NULL;
10317 impl->preview_display_name = NULL;
10320 if (impl->use_preview_label && impl->preview_label)
10321 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10323 g_signal_emit_by_name (impl, "update-preview");
10328 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10329 GtkFileSystemVolume *volume,
10330 const GError *error,
10334 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10335 GtkFileChooserDefault *impl = data;
10337 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10340 impl->shortcuts_activate_iter_cancellable = NULL;
10342 set_busy_cursor (impl, FALSE);
10349 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10353 name = _gtk_file_system_volume_get_display_name (volume);
10354 msg = g_strdup_printf (_("Could not mount %s"), name);
10356 error_message (impl, msg, error->message);
10365 file = _gtk_file_system_volume_get_root (volume);
10368 change_folder_and_display_error (impl, file, FALSE);
10369 g_object_unref (file);
10373 g_object_unref (impl);
10374 g_object_unref (cancellable);
10378 /* Activates a volume by mounting it if necessary and then switching to its
10382 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10383 GtkFileSystemVolume *volume)
10387 switch (impl->operation_mode)
10389 case OPERATION_MODE_BROWSE:
10391 case OPERATION_MODE_SEARCH:
10392 search_switch_to_browse_mode (impl);
10394 case OPERATION_MODE_RECENT:
10395 recent_switch_to_browse_mode (impl);
10399 /* We ref the file chooser since volume_mount() may run a main loop, and the
10400 * user could close the file chooser window in the meantime.
10402 g_object_ref (impl);
10404 if (!_gtk_file_system_volume_is_mounted (volume))
10406 GtkMountOperation *mount_op;
10408 set_busy_cursor (impl, TRUE);
10410 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
10411 impl->shortcuts_activate_iter_cancellable =
10412 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
10413 shortcuts_activate_volume_mount_cb,
10414 g_object_ref (impl));
10415 g_object_unref (mount_op);
10419 file = _gtk_file_system_volume_get_root (volume);
10422 change_folder_and_display_error (impl, file, FALSE);
10423 g_object_unref (file);
10427 g_object_unref (impl);
10430 /* Opens the folder or volume at the specified iter in the shortcuts model */
10431 struct ShortcutsActivateData
10433 GtkFileChooserDefault *impl;
10438 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10440 const GError *error,
10441 gpointer user_data)
10443 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10444 struct ShortcutsActivateData *data = user_data;
10446 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10449 data->impl->shortcuts_activate_iter_cancellable = NULL;
10454 if (!error && _gtk_file_info_consider_as_directory (info))
10455 change_folder_and_display_error (data->impl, data->file, FALSE);
10457 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10462 g_object_unref (data->impl);
10463 g_object_unref (data->file);
10466 g_object_unref (cancellable);
10470 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10471 GtkFileSystemVolume *volume,
10472 const GError *error,
10473 gpointer user_data)
10475 struct ShortcutsActivateData *data = user_data;
10479 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10481 g_object_unref (data->impl);
10482 g_object_unref (data->file);
10488 data->impl->shortcuts_activate_iter_cancellable =
10489 _gtk_file_system_get_info (data->impl->file_system, data->file,
10491 shortcuts_activate_get_info_cb, data);
10495 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10499 ShortcutType shortcut_type;
10501 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10502 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10503 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10504 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10506 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10507 SHORTCUTS_COL_DATA, &col_data,
10508 SHORTCUTS_COL_TYPE, &shortcut_type,
10511 if (impl->shortcuts_activate_iter_cancellable)
10513 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10514 impl->shortcuts_activate_iter_cancellable = NULL;
10517 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10519 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10521 GtkFileSystemVolume *volume;
10525 shortcuts_activate_volume (impl, volume);
10527 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10529 struct ShortcutsActivateData *data;
10530 GtkFileSystemVolume *volume;
10532 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10534 data = g_new0 (struct ShortcutsActivateData, 1);
10535 data->impl = g_object_ref (impl);
10536 data->file = g_object_ref (col_data);
10538 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10540 GMountOperation *mount_operation;
10541 GtkWidget *toplevel;
10543 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10545 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10547 impl->shortcuts_activate_iter_cancellable =
10548 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10550 shortcuts_activate_mount_enclosing_volume,
10555 impl->shortcuts_activate_iter_cancellable =
10556 _gtk_file_system_get_info (impl->file_system, data->file,
10558 shortcuts_activate_get_info_cb, data);
10561 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10563 search_activate (impl);
10565 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10567 recent_activate (impl);
10571 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10573 shortcuts_key_press_event_cb (GtkWidget *widget,
10574 GdkEventKey *event,
10575 GtkFileChooserDefault *impl)
10579 modifiers = gtk_accelerator_get_default_mod_mask ();
10581 if ((event->keyval == GDK_BackSpace
10582 || event->keyval == GDK_Delete
10583 || event->keyval == GDK_KP_Delete)
10584 && (event->state & modifiers) == 0)
10586 remove_selected_bookmarks (impl);
10590 if ((event->keyval == GDK_F2)
10591 && (event->state & modifiers) == 0)
10593 rename_selected_bookmark (impl);
10601 shortcuts_select_func (GtkTreeSelection *selection,
10602 GtkTreeModel *model,
10604 gboolean path_currently_selected,
10607 GtkFileChooserDefault *impl = data;
10608 GtkTreeIter filter_iter;
10609 ShortcutType shortcut_type;
10611 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10612 g_assert_not_reached ();
10614 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10616 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10620 list_select_func (GtkTreeSelection *selection,
10621 GtkTreeModel *model,
10623 gboolean path_currently_selected,
10626 GtkFileChooserDefault *impl = data;
10628 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10629 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10631 GtkTreeIter iter, child_iter;
10633 switch (impl->operation_mode)
10635 case OPERATION_MODE_SEARCH:
10637 gboolean is_folder;
10639 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10642 search_get_valid_child_iter (impl, &child_iter, &iter);
10643 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10644 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10651 case OPERATION_MODE_RECENT:
10653 gboolean is_folder;
10655 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10658 recent_get_valid_child_iter (impl, &child_iter, &iter);
10659 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10660 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10667 case OPERATION_MODE_BROWSE:
10671 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10674 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10675 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10676 if (info && (! _gtk_file_info_consider_as_directory (info)))
10687 list_selection_changed (GtkTreeSelection *selection,
10688 GtkFileChooserDefault *impl)
10690 /* See if we are in the new folder editable row for Save mode */
10691 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10692 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10695 gboolean had_selection;
10697 info = get_selected_file_info_from_file_list (impl, &had_selection);
10698 if (!had_selection)
10699 goto out; /* normal processing */
10702 return; /* We are on the editable row for New Folder */
10707 if (impl->location_entry)
10708 update_chooser_entry (impl);
10710 check_preview_change (impl);
10711 bookmarks_check_add_sensitivity (impl);
10713 g_signal_emit_by_name (impl, "selection-changed", 0);
10716 /* Callback used when a row in the file list is activated */
10718 list_row_activated (GtkTreeView *tree_view,
10720 GtkTreeViewColumn *column,
10721 GtkFileChooserDefault *impl)
10724 GtkTreeIter child_iter;
10726 switch (impl->operation_mode)
10728 case OPERATION_MODE_SEARCH:
10731 gboolean is_folder;
10733 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10736 search_get_valid_child_iter (impl, &child_iter, &iter);
10737 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10738 SEARCH_MODEL_COL_FILE, &file,
10739 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10744 change_folder_and_display_error (impl, file, FALSE);
10748 g_signal_emit_by_name (impl, "file-activated");
10752 case OPERATION_MODE_RECENT:
10755 gboolean is_folder;
10757 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10760 recent_get_valid_child_iter (impl, &child_iter, &iter);
10761 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10762 RECENT_MODEL_COL_FILE, &file,
10763 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10768 change_folder_and_display_error (impl, file, FALSE);
10772 g_signal_emit_by_name (impl, "file-activated");
10776 case OPERATION_MODE_BROWSE:
10780 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10783 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10784 &child_iter, &iter);
10785 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10788 if (_gtk_file_info_consider_as_directory (info))
10790 GFile *file, *target_file;
10791 const gchar *target_uri;
10793 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10794 if (g_file_info_get_file_type (info) == G_FILE_TYPE_MOUNTABLE ||
10795 g_file_info_get_file_type (info) == G_FILE_TYPE_SHORTCUT)
10797 target_uri = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
10800 target_file = g_file_new_for_uri (target_uri);
10803 g_object_unref (file);
10804 file = target_file;
10810 change_folder_and_display_error (impl, file, FALSE);
10814 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10815 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10816 g_signal_emit_by_name (impl, "file-activated");
10823 path_bar_clicked (GtkPathBar *path_bar,
10826 gboolean child_is_hidden,
10827 GtkFileChooserDefault *impl)
10830 pending_select_files_add (impl, child_file);
10832 if (!change_folder_and_display_error (impl, file, FALSE))
10835 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10836 * show hidden files so that ".baz" appears in the file list, as it will still
10837 * be shown in the path bar: "/foo/[bar]/.baz"
10839 if (child_is_hidden)
10840 g_object_set (impl, "show-hidden", TRUE, NULL);
10844 get_list_file_info (GtkFileChooserDefault *impl,
10847 GtkTreeIter child_iter;
10849 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10853 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10857 list_icon_data_func (GtkTreeViewColumn *tree_column,
10858 GtkCellRenderer *cell,
10859 GtkTreeModel *tree_model,
10863 GtkFileChooserDefault *impl = data;
10864 GtkTreeIter child_iter;
10865 GdkPixbuf *pixbuf = NULL;
10866 gboolean sensitive = TRUE;
10868 profile_start ("start", NULL);
10870 switch (impl->operation_mode)
10872 case OPERATION_MODE_SEARCH:
10874 GtkTreeIter child_iter;
10875 gboolean is_folder;
10877 search_get_valid_child_iter (impl, &child_iter, iter);
10878 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10879 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10880 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10883 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10884 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10885 sensitive = is_folder;
10889 case 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,
10901 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10903 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10904 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10905 sensitive = is_folder;
10909 case OPERATION_MODE_BROWSE:
10914 info = get_list_file_info (impl, iter);
10916 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10919 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10924 /* FIXME: NULL GError */
10925 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10930 /* We are on the editable row */
10935 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10936 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10937 sensitive = _gtk_file_info_consider_as_directory (info);
10942 g_object_set (cell,
10944 "sensitive", sensitive,
10948 g_object_unref (pixbuf);
10950 profile_end ("end", NULL);
10954 list_name_data_func (GtkTreeViewColumn *tree_column,
10955 GtkCellRenderer *cell,
10956 GtkTreeModel *tree_model,
10960 GtkFileChooserDefault *impl = data;
10962 gboolean sensitive = TRUE;
10964 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10966 GtkTreeIter child_iter;
10967 gchar *display_name;
10968 gboolean is_folder;
10970 search_get_valid_child_iter (impl, &child_iter, iter);
10971 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10972 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10973 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10976 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10977 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10979 sensitive = is_folder;
10982 g_object_set (cell,
10983 "text", display_name,
10984 "sensitive", sensitive,
10985 "ellipsize", PANGO_ELLIPSIZE_END,
10991 if (impl->operation_mode == OPERATION_MODE_RECENT)
10993 GtkTreeIter child_iter;
10994 GtkRecentInfo *recent_info;
10995 gchar *display_name;
10996 gboolean is_folder;
10998 recent_get_valid_child_iter (impl, &child_iter, iter);
10999 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11000 RECENT_MODEL_COL_INFO, &recent_info,
11001 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11004 display_name = gtk_recent_info_get_short_name (recent_info);
11006 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11007 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11009 sensitive = is_folder;
11012 g_object_set (cell,
11013 "text", display_name,
11014 "sensitive", sensitive,
11015 "ellipsize", PANGO_ELLIPSIZE_END,
11018 g_free (display_name);
11023 info = get_list_file_info (impl, iter);
11028 g_object_set (cell,
11029 "text", _("Type name of new folder"),
11031 "ellipsize", PANGO_ELLIPSIZE_NONE,
11038 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11039 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11041 sensitive = _gtk_file_info_consider_as_directory (info);
11044 g_object_set (cell,
11045 "text", g_file_info_get_display_name (info),
11046 "sensitive", sensitive,
11047 "ellipsize", PANGO_ELLIPSIZE_END,
11052 list_size_data_func (GtkTreeViewColumn *tree_column,
11053 GtkCellRenderer *cell,
11054 GtkTreeModel *tree_model,
11058 GtkFileChooserDefault *impl = data;
11062 gboolean sensitive = TRUE;
11064 if (impl->operation_mode == OPERATION_MODE_RECENT)
11067 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11069 GtkTreeIter child_iter;
11070 gboolean is_folder = FALSE;
11072 search_get_valid_child_iter (impl, &child_iter, iter);
11073 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11074 SEARCH_MODEL_COL_SIZE, &size,
11075 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11078 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11079 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11080 sensitive = is_folder ? TRUE : FALSE;
11083 str = g_format_size_for_display (size);
11087 g_object_set (cell,
11089 "sensitive", sensitive,
11097 info = get_list_file_info (impl, iter);
11099 if (!info || _gtk_file_info_consider_as_directory (info))
11101 g_object_set (cell,
11103 "sensitive", sensitive,
11108 size = g_file_info_get_size (info);
11109 str = g_format_size_for_display (size);
11111 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11112 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11115 g_object_set (cell,
11117 "sensitive", sensitive,
11118 "alignment", PANGO_ALIGN_RIGHT,
11124 /* Tree column data callback for the file list; fetches the mtime of a file */
11126 list_mtime_data_func (GtkTreeViewColumn *tree_column,
11127 GtkCellRenderer *cell,
11128 GtkTreeModel *tree_model,
11132 GtkFileChooserDefault *impl;
11133 GTimeVal timeval = { 0, };
11135 gchar *date_str = NULL;
11136 gboolean sensitive = TRUE;
11138 const char *locale, *dot = NULL;
11139 gint64 codepage = -1;
11145 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11147 GtkTreeIter child_iter;
11149 gboolean is_folder;
11151 search_get_valid_child_iter (impl, &child_iter, iter);
11152 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11153 SEARCH_MODEL_COL_MTIME, &mtime,
11154 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11157 time_mtime = (time_t) mtime;
11159 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11160 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11161 sensitive = is_folder;
11163 else if (impl->operation_mode == OPERATION_MODE_RECENT)
11165 GtkTreeIter child_iter;
11166 GtkRecentInfo *info;
11167 gboolean is_folder;
11169 recent_get_valid_child_iter (impl, &child_iter, iter);
11170 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11171 RECENT_MODEL_COL_INFO, &info,
11172 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11176 time_mtime = gtk_recent_info_get_modified (info);
11180 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11181 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11182 sensitive = is_folder;
11188 info = get_list_file_info (impl, iter);
11191 g_object_set (cell,
11198 g_file_info_get_modification_time (info, &timeval);
11199 time_mtime = timeval.tv_sec;
11201 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11202 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11203 sensitive = _gtk_file_info_consider_as_directory (info);
11206 if (G_UNLIKELY (time_mtime == 0))
11207 date_str = g_strdup (_("Unknown"));
11212 struct tm tm_mtime;
11214 const gchar *format;
11215 gchar *locale_format = NULL;
11218 #ifdef HAVE_LOCALTIME_R
11219 localtime_r ((time_t *) &time_mtime, &tm_mtime);
11222 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
11226 g_warning ("ptm != NULL failed");
11228 g_object_set (cell,
11229 "text", _("Unknown"),
11230 "sensitive", sensitive,
11235 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11237 #endif /* HAVE_LOCALTIME_R */
11239 g_date_set_time_t (&mtime, time_mtime);
11240 time_now = time (NULL);
11241 g_date_set_time_t (&now, time_now);
11243 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11245 /* Translators: %H means "hours" and %M means "minutes" */
11246 if (days_diff == 0)
11247 format = _("%H:%M");
11248 else if (days_diff == 1)
11249 format = _("Yesterday at %H:%M");
11252 if (days_diff > 1 && days_diff < 7)
11253 format = "%A"; /* Days from last week */
11255 format = "%x"; /* Any other date */
11259 /* g_locale_from_utf8() returns a string in the system
11260 * code-page, which is not always the same as that used by the C
11261 * library. For instance when running a GTK+ program with
11262 * LANG=ko on an English version of Windows, the system
11263 * code-page is 1252, but the code-page used by the C library is
11264 * 949. (It's GTK+ itself that sets the C library locale when it
11265 * notices the LANG environment variable. See gtkmain.c The
11266 * Microsoft C library doesn't look at any locale environment
11267 * variables.) We need to pass strftime() a string in the C
11268 * library's code-page. See bug #509885.
11270 locale = setlocale (LC_ALL, NULL);
11271 if (locale != NULL)
11272 dot = strchr (locale, '.');
11275 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11277 /* All codepages should fit in 16 bits AFAIK */
11278 if (codepage > 0 && codepage < 65536)
11280 sprintf (charset, "CP%u", (guint) codepage);
11281 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11285 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11287 if (locale_format != NULL &&
11288 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11291 /* As above but in opposite direction... */
11292 if (codepage > 0 && codepage < 65536)
11293 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11295 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11299 if (date_str == NULL)
11300 date_str = g_strdup (_("Unknown"));
11302 g_free (locale_format);
11305 g_object_set (cell,
11307 "sensitive", sensitive,
11313 _gtk_file_chooser_default_new (void)
11315 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11319 location_set_user_text (GtkFileChooserDefault *impl,
11322 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11323 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11327 location_popup_handler (GtkFileChooserDefault *impl,
11330 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11332 GtkWidget *widget_to_focus;
11334 /* This will give us the location widgets back */
11335 switch (impl->operation_mode)
11337 case OPERATION_MODE_SEARCH:
11338 search_switch_to_browse_mode (impl);
11340 case OPERATION_MODE_RECENT:
11341 recent_switch_to_browse_mode (impl);
11343 case OPERATION_MODE_BROWSE:
11344 g_assert_not_reached ();
11348 if (impl->current_folder)
11349 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11351 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11352 widget_to_focus = impl->browse_files_tree_view;
11354 widget_to_focus = impl->location_entry;
11356 gtk_widget_grab_focus (widget_to_focus);
11360 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11361 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11363 LocationMode new_mode;
11367 /* since the user typed something, we unconditionally want to turn on the entry */
11368 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11370 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11371 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11372 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11373 new_mode = LOCATION_MODE_PATH_BAR;
11376 g_assert_not_reached ();
11380 location_mode_set (impl, new_mode, TRUE);
11381 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11384 location_set_user_text (impl, path);
11387 location_entry_set_initial_text (impl);
11388 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11392 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11393 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11395 gtk_widget_grab_focus (impl->location_entry);
11397 location_set_user_text (impl, path);
11400 g_assert_not_reached ();
11403 /* Handler for the "up-folder" keybinding signal */
11405 up_folder_handler (GtkFileChooserDefault *impl)
11407 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11410 /* Handler for the "down-folder" keybinding signal */
11412 down_folder_handler (GtkFileChooserDefault *impl)
11414 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11417 /* Switches to the shortcut in the specified index */
11419 switch_to_shortcut (GtkFileChooserDefault *impl,
11424 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11425 g_assert_not_reached ();
11427 shortcuts_activate_iter (impl, &iter);
11430 /* Handler for the "home-folder" keybinding signal */
11432 home_folder_handler (GtkFileChooserDefault *impl)
11434 if (impl->has_home)
11435 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11438 /* Handler for the "desktop-folder" keybinding signal */
11440 desktop_folder_handler (GtkFileChooserDefault *impl)
11442 if (impl->has_desktop)
11443 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11446 /* Handler for the "search-shortcut" keybinding signal */
11448 search_shortcut_handler (GtkFileChooserDefault *impl)
11450 if (impl->has_search)
11452 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11454 /* we want the entry widget to grab the focus the first
11455 * time, not the browse_files_tree_view widget.
11457 if (impl->search_entry)
11458 gtk_widget_grab_focus (impl->search_entry);
11462 /* Handler for the "recent-shortcut" keybinding signal */
11464 recent_shortcut_handler (GtkFileChooserDefault *impl)
11466 if (impl->has_recent)
11467 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11471 quick_bookmark_handler (GtkFileChooserDefault *impl,
11472 gint bookmark_index)
11477 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11480 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11482 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11483 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11486 gtk_tree_path_free (path);
11488 switch_to_shortcut (impl, bookmark_pos);
11492 show_hidden_handler (GtkFileChooserDefault *impl)
11494 g_object_set (impl,
11495 "show-hidden", !impl->show_hidden,
11500 /* Drag and drop interfaces */
11503 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11508 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11510 model->impl = NULL;
11513 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11515 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11518 ShortcutsPaneModelFilter *model;
11522 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11524 pos = *gtk_tree_path_get_indices (path);
11525 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11527 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11530 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11532 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11534 GtkSelectionData *selection_data)
11536 ShortcutsPaneModelFilter *model;
11538 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11545 /* Fill the GtkTreeDragSourceIface vtable */
11547 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11549 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11550 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11554 /* Fill the GtkTreeDragDestIface vtable */
11556 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11558 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11559 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11563 static GtkTreeModel *
11564 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11565 GtkTreeModel *child_model,
11568 ShortcutsPaneModelFilter *model;
11570 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11571 "child-model", child_model,
11572 "virtual-root", root,
11575 model->impl = impl;
11577 return GTK_TREE_MODEL (model);
11583 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11586 RecentModelSort *model;
11587 GtkTreeIter iter, child_iter;
11588 gboolean is_folder;
11590 model = RECENT_MODEL_SORT (drag_source);
11591 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11594 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11595 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11596 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11603 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11605 GtkSelectionData *selection_data)
11607 RecentModelSort *model;
11608 GtkTreeIter iter, child_iter;
11612 model = RECENT_MODEL_SORT (drag_source);
11613 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11616 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11617 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11618 RECENT_MODEL_COL_FILE, &file,
11620 g_assert (file != NULL);
11622 uris[0] = g_file_get_uri (file);
11625 gtk_selection_data_set_uris (selection_data, uris);
11633 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11635 iface->row_draggable = recent_model_sort_row_draggable;
11636 iface->drag_data_get = recent_model_sort_drag_data_get;
11640 _recent_model_sort_class_init (RecentModelSortClass *klass)
11646 _recent_model_sort_init (RecentModelSort *model)
11648 model->impl = NULL;
11651 static GtkTreeModel *
11652 recent_model_sort_new (GtkFileChooserDefault *impl,
11653 GtkTreeModel *child_model)
11655 RecentModelSort *model;
11657 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11658 "model", child_model,
11660 model->impl = impl;
11662 return GTK_TREE_MODEL (model);
11668 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11671 SearchModelSort *model;
11672 GtkTreeIter iter, child_iter;
11673 gboolean is_folder;
11675 model = SEARCH_MODEL_SORT (drag_source);
11676 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11679 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11680 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11681 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11688 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11690 GtkSelectionData *selection_data)
11692 SearchModelSort *model;
11693 GtkTreeIter iter, child_iter;
11697 model = SEARCH_MODEL_SORT (drag_source);
11698 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11701 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11702 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11703 RECENT_MODEL_COL_FILE, &file,
11705 g_assert (file != NULL);
11707 uris[0] = g_file_get_uri (file);
11710 gtk_selection_data_set_uris (selection_data, uris);
11718 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11720 iface->row_draggable = search_model_sort_row_draggable;
11721 iface->drag_data_get = search_model_sort_drag_data_get;
11725 _search_model_sort_class_init (SearchModelSortClass *klass)
11731 _search_model_sort_init (SearchModelSort *model)
11733 model->impl = NULL;
11736 static GtkTreeModel *
11737 search_model_sort_new (GtkFileChooserDefault *impl,
11738 GtkTreeModel *child_model)
11740 SearchModelSort *model;
11742 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11743 "model", child_model,
11745 model->impl = impl;
11747 return GTK_TREE_MODEL (model);