1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechoosersettings.h"
40 #include "gtkfilechooserutils.h"
41 #include "gtkfilechooser.h"
42 #include "gtkfilesystem.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
52 #include "gtkmarshalers.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkmountoperation.h"
55 #include "gtkpathbar.h"
56 #include "gtkprivate.h"
57 #include "gtkradiobutton.h"
58 #include "gtkrecentfilter.h"
59 #include "gtkrecentmanager.h"
60 #include "gtkscrolledwindow.h"
61 #include "gtkseparatormenuitem.h"
62 #include "gtksizegroup.h"
65 #include "gtktooltip.h"
66 #include "gtktreednd.h"
67 #include "gtktreeprivate.h"
68 #include "gtktreeselection.h"
78 #include <sys/types.h>
89 #undef PROFILE_FILE_CHOOSER
90 #ifdef PROFILE_FILE_CHOOSER
97 #define PROFILE_INDENT 4
98 static int profile_indent;
101 profile_add_indent (int indent)
103 profile_indent += indent;
104 if (profile_indent < 0)
105 g_error ("You screwed up your indentation");
109 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
114 profile_add_indent (indent);
116 if (profile_indent == 0)
117 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
119 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 profile_add_indent (indent);
128 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
129 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
130 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
132 #define profile_start(x, y)
133 #define profile_end(x, y)
134 #define profile_msg(x, y)
139 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
141 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
142 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
143 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define MAX_LOADING_TIME 500
147 struct _GtkFileChooserDefaultClass
149 GtkVBoxClass parent_class;
155 LOCATION_POPUP_ON_PASTE,
161 LOCATION_TOGGLE_POPUP,
169 static guint signals[LAST_SIGNAL] = { 0 };
171 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
173 SHORTCUTS_COL_PIXBUF,
177 SHORTCUTS_COL_REMOVABLE,
178 SHORTCUTS_COL_PIXBUF_VISIBLE,
179 SHORTCUTS_COL_CANCELLABLE,
180 SHORTCUTS_COL_NUM_COLUMNS
185 SHORTCUT_TYPE_VOLUME,
186 SHORTCUT_TYPE_SEPARATOR,
187 SHORTCUT_TYPE_SEARCH,
191 /* Column numbers for the file list */
196 FILE_LIST_COL_NUM_COLUMNS
199 /* Column numbers for the search model.
200 * Keep this in sync with search_setup_model()
203 SEARCH_MODEL_COL_FILE,
204 SEARCH_MODEL_COL_DISPLAY_NAME,
205 SEARCH_MODEL_COL_COLLATION_KEY,
206 SEARCH_MODEL_COL_STAT,
207 SEARCH_MODEL_COL_CANCELLABLE,
208 SEARCH_MODEL_COL_PIXBUF,
209 SEARCH_MODEL_COL_MIME_TYPE,
210 SEARCH_MODEL_COL_IS_FOLDER,
211 SEARCH_MODEL_COL_NUM_COLUMNS
215 RECENT_MODEL_COL_FILE,
216 RECENT_MODEL_COL_DISPLAY_NAME,
217 RECENT_MODEL_COL_INFO,
218 RECENT_MODEL_COL_IS_FOLDER,
219 RECENT_MODEL_COL_CANCELLABLE,
220 RECENT_MODEL_COL_NUM_COLUMNS
223 /* Identifiers for target types */
229 search_is_possible (GtkFileChooserDefault *impl)
231 if (impl->search_engine == NULL)
232 impl->search_engine = _gtk_search_engine_new ();
234 return impl->search_engine != NULL;
237 /* Interesting places in the shortcuts bar */
241 SHORTCUTS_RECENT_SEPARATOR,
246 SHORTCUTS_BOOKMARKS_SEPARATOR,
248 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
249 SHORTCUTS_CURRENT_FOLDER
252 /* Icon size for if we can't get it from the theme */
253 #define FALLBACK_ICON_SIZE 16
255 #define PREVIEW_HBOX_SPACING 12
259 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
260 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
262 static GObject* gtk_file_chooser_default_constructor (GType type,
263 guint n_construct_properties,
264 GObjectConstructParam *construct_params);
265 static void gtk_file_chooser_default_finalize (GObject *object);
266 static void gtk_file_chooser_default_set_property (GObject *object,
270 static void gtk_file_chooser_default_get_property (GObject *object,
274 static void gtk_file_chooser_default_dispose (GObject *object);
275 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
276 static void gtk_file_chooser_default_map (GtkWidget *widget);
277 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
278 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
279 GtkWidget *previous_toplevel);
280 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
281 GtkStyle *previous_style);
282 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
283 GdkScreen *previous_screen);
284 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
285 GtkAllocation *allocation);
287 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
290 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
293 gboolean clear_entry,
295 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
296 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
298 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
301 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
305 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
306 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
307 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
313 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
316 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
319 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
321 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
323 gint *default_height);
324 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
325 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
327 static void location_popup_handler (GtkFileChooserDefault *impl,
329 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
330 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
331 static void up_folder_handler (GtkFileChooserDefault *impl);
332 static void down_folder_handler (GtkFileChooserDefault *impl);
333 static void home_folder_handler (GtkFileChooserDefault *impl);
334 static void desktop_folder_handler (GtkFileChooserDefault *impl);
335 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
336 gint bookmark_index);
337 static void show_hidden_handler (GtkFileChooserDefault *impl);
338 static void search_shortcut_handler (GtkFileChooserDefault *impl);
339 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
340 static void update_appearance (GtkFileChooserDefault *impl);
342 static void set_current_filter (GtkFileChooserDefault *impl,
343 GtkFileFilter *filter);
344 static void check_preview_change (GtkFileChooserDefault *impl);
346 static void filter_combo_changed (GtkComboBox *combo_box,
347 GtkFileChooserDefault *impl);
349 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
351 GtkFileChooserDefault *impl);
353 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
356 gboolean path_currently_selected,
358 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
360 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
362 static int shortcuts_get_index (GtkFileChooserDefault *impl,
363 ShortcutsIndex where);
364 static int shortcut_find_position (GtkFileChooserDefault *impl,
367 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
369 static gboolean list_select_func (GtkTreeSelection *selection,
372 gboolean path_currently_selected,
375 static void list_selection_changed (GtkTreeSelection *tree_selection,
376 GtkFileChooserDefault *impl);
377 static void list_row_activated (GtkTreeView *tree_view,
379 GtkTreeViewColumn *column,
380 GtkFileChooserDefault *impl);
382 static void select_func (GtkFileSystemModel *model,
387 static void path_bar_clicked (GtkPathBar *path_bar,
390 gboolean child_is_hidden,
391 GtkFileChooserDefault *impl);
393 static void add_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
395 static void remove_bookmark_button_clicked_cb (GtkButton *button,
396 GtkFileChooserDefault *impl);
397 static void save_folder_combo_changed_cb (GtkComboBox *combo,
398 GtkFileChooserDefault *impl);
400 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
401 GtkCellRenderer *cell,
402 GtkTreeModel *tree_model,
405 static void list_name_data_func (GtkTreeViewColumn *tree_column,
406 GtkCellRenderer *cell,
407 GtkTreeModel *tree_model,
411 static void list_size_data_func (GtkTreeViewColumn *tree_column,
412 GtkCellRenderer *cell,
413 GtkTreeModel *tree_model,
417 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
418 GtkCellRenderer *cell,
419 GtkTreeModel *tree_model,
423 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
426 static void load_remove_timer (GtkFileChooserDefault *impl);
427 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
429 static void location_button_toggled_cb (GtkToggleButton *toggle,
430 GtkFileChooserDefault *impl);
431 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
433 static void search_stop_searching (GtkFileChooserDefault *impl,
434 gboolean remove_query);
435 static void search_clear_model (GtkFileChooserDefault *impl,
436 gboolean remove_from_treeview);
437 static gboolean search_should_respond (GtkFileChooserDefault *impl);
438 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
439 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
440 static void search_entry_activate_cb (GtkEntry *entry,
442 static void settings_load (GtkFileChooserDefault *impl);
443 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
444 GtkTreeIter *child_iter,
447 static void recent_stop_loading (GtkFileChooserDefault *impl);
448 static void recent_clear_model (GtkFileChooserDefault *impl,
449 gboolean remove_from_treeview);
450 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
451 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
452 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
453 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
454 GtkTreeIter *child_iter,
456 static void set_file_system_backend (GtkFileChooserDefault *impl);
462 /* Drag and drop interface declarations */
465 GtkTreeModelFilter parent;
467 GtkFileChooserDefault *impl;
468 } ShortcutsPaneModelFilter;
471 GtkTreeModelFilterClass parent_class;
472 } ShortcutsPaneModelFilterClass;
474 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
475 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
477 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
479 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
480 _shortcuts_pane_model_filter,
481 GTK_TYPE_TREE_MODEL_FILTER,
482 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
483 shortcuts_pane_model_filter_drag_source_iface_init))
485 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
486 GtkTreeModel *child_model,
491 GtkTreeModelSort parent;
493 GtkFileChooserDefault *impl;
497 GtkTreeModelSortClass parent_class;
498 } RecentModelSortClass;
500 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
501 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
503 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
505 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
507 GTK_TYPE_TREE_MODEL_SORT,
508 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
509 recent_model_sort_drag_source_iface_init));
511 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
512 GtkTreeModel *child_model);
516 GtkTreeModelSort parent;
518 GtkFileChooserDefault *impl;
522 GtkTreeModelSortClass parent_class;
523 } SearchModelSortClass;
525 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
526 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
528 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
530 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
532 GTK_TYPE_TREE_MODEL_SORT,
533 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
534 search_model_sort_drag_source_iface_init));
536 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
537 GtkTreeModel *child_model);
541 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
542 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
543 gtk_file_chooser_default_iface_init)
544 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
545 gtk_file_chooser_embed_default_iface_init));
548 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
550 static const guint quick_bookmark_keyvals[10] = {
551 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
553 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
554 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
555 GtkBindingSet *binding_set;
558 gobject_class->finalize = gtk_file_chooser_default_finalize;
559 gobject_class->constructor = gtk_file_chooser_default_constructor;
560 gobject_class->set_property = gtk_file_chooser_default_set_property;
561 gobject_class->get_property = gtk_file_chooser_default_get_property;
562 gobject_class->dispose = gtk_file_chooser_default_dispose;
564 widget_class->show_all = gtk_file_chooser_default_show_all;
565 widget_class->map = gtk_file_chooser_default_map;
566 widget_class->unmap = gtk_file_chooser_default_unmap;
567 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
568 widget_class->style_set = gtk_file_chooser_default_style_set;
569 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
570 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
572 signals[LOCATION_POPUP] =
573 g_signal_new_class_handler (I_("location-popup"),
574 G_OBJECT_CLASS_TYPE (class),
575 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
576 G_CALLBACK (location_popup_handler),
578 _gtk_marshal_VOID__STRING,
579 G_TYPE_NONE, 1, G_TYPE_STRING);
581 signals[LOCATION_POPUP_ON_PASTE] =
582 g_signal_new_class_handler (I_("location-popup-on-paste"),
583 G_OBJECT_CLASS_TYPE (class),
584 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
585 G_CALLBACK (location_popup_on_paste_handler),
587 _gtk_marshal_VOID__VOID,
590 signals[LOCATION_TOGGLE_POPUP] =
591 g_signal_new_class_handler (I_("location-toggle-popup"),
592 G_OBJECT_CLASS_TYPE (class),
593 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
594 G_CALLBACK (location_toggle_popup_handler),
596 _gtk_marshal_VOID__VOID,
600 g_signal_new_class_handler (I_("up-folder"),
601 G_OBJECT_CLASS_TYPE (class),
602 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
603 G_CALLBACK (up_folder_handler),
605 _gtk_marshal_VOID__VOID,
608 signals[DOWN_FOLDER] =
609 g_signal_new_class_handler (I_("down-folder"),
610 G_OBJECT_CLASS_TYPE (class),
611 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
612 G_CALLBACK (down_folder_handler),
614 _gtk_marshal_VOID__VOID,
617 signals[HOME_FOLDER] =
618 g_signal_new_class_handler (I_("home-folder"),
619 G_OBJECT_CLASS_TYPE (class),
620 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
621 G_CALLBACK (home_folder_handler),
623 _gtk_marshal_VOID__VOID,
626 signals[DESKTOP_FOLDER] =
627 g_signal_new_class_handler (I_("desktop-folder"),
628 G_OBJECT_CLASS_TYPE (class),
629 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
630 G_CALLBACK (desktop_folder_handler),
632 _gtk_marshal_VOID__VOID,
635 signals[QUICK_BOOKMARK] =
636 g_signal_new_class_handler (I_("quick-bookmark"),
637 G_OBJECT_CLASS_TYPE (class),
638 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
639 G_CALLBACK (quick_bookmark_handler),
641 _gtk_marshal_VOID__INT,
642 G_TYPE_NONE, 1, G_TYPE_INT);
644 signals[SHOW_HIDDEN] =
645 g_signal_new_class_handler (I_("show-hidden"),
646 G_OBJECT_CLASS_TYPE (class),
647 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
648 G_CALLBACK (show_hidden_handler),
650 _gtk_marshal_VOID__VOID,
653 signals[SEARCH_SHORTCUT] =
654 g_signal_new_class_handler (I_("search-shortcut"),
655 G_OBJECT_CLASS_TYPE (class),
656 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
657 G_CALLBACK (search_shortcut_handler),
659 _gtk_marshal_VOID__VOID,
662 signals[RECENT_SHORTCUT] =
663 g_signal_new_class_handler (I_("recent-shortcut"),
664 G_OBJECT_CLASS_TYPE (class),
665 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
666 G_CALLBACK (recent_shortcut_handler),
668 _gtk_marshal_VOID__VOID,
671 binding_set = gtk_binding_set_by_class (class);
673 gtk_binding_entry_add_signal (binding_set,
674 GDK_l, GDK_CONTROL_MASK,
675 "location-toggle-popup",
678 gtk_binding_entry_add_signal (binding_set,
681 1, G_TYPE_STRING, "/");
682 gtk_binding_entry_add_signal (binding_set,
685 1, G_TYPE_STRING, "/");
688 gtk_binding_entry_add_signal (binding_set,
691 1, G_TYPE_STRING, "~");
694 gtk_binding_entry_add_signal (binding_set,
695 GDK_v, GDK_CONTROL_MASK,
696 "location-popup-on-paste",
698 gtk_binding_entry_add_signal (binding_set,
699 GDK_Up, GDK_MOD1_MASK,
702 gtk_binding_entry_add_signal (binding_set,
706 gtk_binding_entry_add_signal (binding_set,
707 GDK_KP_Up, GDK_MOD1_MASK,
711 gtk_binding_entry_add_signal (binding_set,
712 GDK_Down, GDK_MOD1_MASK,
715 gtk_binding_entry_add_signal (binding_set,
716 GDK_KP_Down, GDK_MOD1_MASK,
720 gtk_binding_entry_add_signal (binding_set,
721 GDK_Home, GDK_MOD1_MASK,
724 gtk_binding_entry_add_signal (binding_set,
725 GDK_KP_Home, GDK_MOD1_MASK,
728 gtk_binding_entry_add_signal (binding_set,
729 GDK_d, GDK_MOD1_MASK,
732 gtk_binding_entry_add_signal (binding_set,
733 GDK_h, GDK_CONTROL_MASK,
736 gtk_binding_entry_add_signal (binding_set,
737 GDK_s, GDK_MOD1_MASK,
740 gtk_binding_entry_add_signal (binding_set,
741 GDK_r, GDK_MOD1_MASK,
745 for (i = 0; i < 10; i++)
746 gtk_binding_entry_add_signal (binding_set,
747 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
751 _gtk_file_chooser_install_properties (gobject_class);
755 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
757 iface->select_file = gtk_file_chooser_default_select_file;
758 iface->unselect_file = gtk_file_chooser_default_unselect_file;
759 iface->select_all = gtk_file_chooser_default_select_all;
760 iface->unselect_all = gtk_file_chooser_default_unselect_all;
761 iface->get_files = gtk_file_chooser_default_get_files;
762 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
763 iface->get_file_system = gtk_file_chooser_default_get_file_system;
764 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
765 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
766 iface->set_current_name = gtk_file_chooser_default_set_current_name;
767 iface->add_filter = gtk_file_chooser_default_add_filter;
768 iface->remove_filter = gtk_file_chooser_default_remove_filter;
769 iface->list_filters = gtk_file_chooser_default_list_filters;
770 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
771 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
772 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
776 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
778 iface->get_default_size = gtk_file_chooser_default_get_default_size;
779 iface->should_respond = gtk_file_chooser_default_should_respond;
780 iface->initial_focus = gtk_file_chooser_default_initial_focus;
784 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
786 profile_start ("start", NULL);
787 #ifdef PROFILE_FILE_CHOOSER
788 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
790 impl->local_only = TRUE;
791 impl->preview_widget_active = TRUE;
792 impl->use_preview_label = TRUE;
793 impl->select_multiple = FALSE;
794 impl->show_hidden = FALSE;
795 impl->icon_size = FALLBACK_ICON_SIZE;
796 impl->load_state = LOAD_EMPTY;
797 impl->reload_state = RELOAD_EMPTY;
798 impl->pending_select_files = NULL;
799 impl->location_mode = LOCATION_MODE_PATH_BAR;
800 impl->operation_mode = OPERATION_MODE_BROWSE;
801 impl->recent_manager = gtk_recent_manager_get_default ();
803 gtk_box_set_spacing (GTK_BOX (impl), 12);
805 set_file_system_backend (impl);
807 profile_end ("end", NULL);
810 /* Frees the data columns for the specified iter in the shortcuts model*/
812 shortcuts_free_row_data (GtkFileChooserDefault *impl,
816 ShortcutType shortcut_type;
817 GCancellable *cancellable;
819 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
820 SHORTCUTS_COL_DATA, &col_data,
821 SHORTCUTS_COL_TYPE, &shortcut_type,
822 SHORTCUTS_COL_CANCELLABLE, &cancellable,
826 g_cancellable_cancel (cancellable);
828 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
829 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
833 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
835 GtkFileSystemVolume *volume;
838 _gtk_file_system_volume_free (volume);
844 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
847 g_object_unref (file);
851 /* Frees all the data columns in the shortcuts model */
853 shortcuts_free (GtkFileChooserDefault *impl)
857 if (!impl->shortcuts_model)
860 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
863 shortcuts_free_row_data (impl, &iter);
865 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
867 g_object_unref (impl->shortcuts_model);
868 impl->shortcuts_model = NULL;
872 pending_select_files_free (GtkFileChooserDefault *impl)
874 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
875 g_slist_free (impl->pending_select_files);
876 impl->pending_select_files = NULL;
880 pending_select_files_add (GtkFileChooserDefault *impl,
883 impl->pending_select_files =
884 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
887 /* Used from gtk_tree_selection_selected_foreach() */
889 store_selection_foreach (GtkTreeModel *model,
894 GtkFileChooserDefault *impl;
895 GtkTreeIter child_iter;
898 impl = GTK_FILE_CHOOSER_DEFAULT (data);
900 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
902 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
903 pending_select_files_add (impl, file);
906 /* Stores the current selection in the list of paths to select; this is used to
907 * preserve the selection when reloading the current folder.
910 pending_select_files_store_selection (GtkFileChooserDefault *impl)
912 GtkTreeSelection *selection;
914 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
915 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
919 gtk_file_chooser_default_finalize (GObject *object)
921 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
924 if (impl->shortcuts_pane_filter_model)
925 g_object_unref (impl->shortcuts_pane_filter_model);
927 if (impl->shortcuts_combo_filter_model)
928 g_object_unref (impl->shortcuts_combo_filter_model);
930 shortcuts_free (impl);
932 g_object_unref (impl->file_system);
934 g_free (impl->browse_files_last_selected_name);
936 for (l = impl->filters; l; l = l->next)
938 GtkFileFilter *filter;
940 filter = GTK_FILE_FILTER (l->data);
941 g_object_unref (filter);
943 g_slist_free (impl->filters);
945 if (impl->current_filter)
946 g_object_unref (impl->current_filter);
948 if (impl->current_volume_file)
949 g_object_unref (impl->current_volume_file);
951 if (impl->current_folder)
952 g_object_unref (impl->current_folder);
954 if (impl->preview_file)
955 g_object_unref (impl->preview_file);
957 load_remove_timer (impl);
959 /* Free all the Models we have */
960 if (impl->browse_files_model)
961 g_object_unref (impl->browse_files_model);
963 if (impl->sort_model)
964 g_object_unref (impl->sort_model);
966 search_clear_model (impl, FALSE);
967 recent_clear_model (impl, FALSE);
969 g_free (impl->preview_display_name);
971 g_free (impl->edited_new_text);
973 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
976 /* Shows an error dialog set as transient for the specified window */
978 error_message_with_parent (GtkWindow *parent,
984 dialog = gtk_message_dialog_new (parent,
985 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
990 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
993 if (parent && parent->group)
994 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
996 gtk_dialog_run (GTK_DIALOG (dialog));
997 gtk_widget_destroy (dialog);
1000 /* Returns a toplevel GtkWindow, or NULL if none */
1002 get_toplevel (GtkWidget *widget)
1004 GtkWidget *toplevel;
1006 toplevel = gtk_widget_get_toplevel (widget);
1007 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1010 return GTK_WINDOW (toplevel);
1013 /* Shows an error dialog for the file chooser */
1015 error_message (GtkFileChooserDefault *impl,
1019 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1022 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1024 error_dialog (GtkFileChooserDefault *impl,
1035 uri = g_file_get_uri (file);
1036 text = g_strdup_printf (msg, uri);
1037 error_message (impl, text, error->message);
1040 g_error_free (error);
1044 /* Displays an error message about not being able to get information for a file.
1045 * Frees the GError as well.
1048 error_getting_info_dialog (GtkFileChooserDefault *impl,
1053 _("Could not retrieve information about the file"),
1057 /* Shows an error dialog about not being able to add a bookmark */
1059 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1064 _("Could not add a bookmark"),
1068 /* Shows an error dialog about not being able to remove a bookmark */
1070 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1075 _("Could not remove bookmark"),
1079 /* Shows an error dialog about not being able to create a folder */
1081 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1086 _("The folder could not be created"),
1090 /* Shows an error about not being able to create a folder because a file with
1091 * the same name is already there.
1094 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1099 _("The folder could not be created, as a file with the same "
1100 "name already exists. Try using a different name for the "
1101 "folder, or rename the file first."),
1105 /* Shows an error dialog about not being able to create a filename */
1107 error_building_filename_dialog (GtkFileChooserDefault *impl,
1110 error_dialog (impl, _("Invalid file name"),
1114 /* Shows an error dialog when we cannot switch to a folder */
1116 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1120 error_dialog (impl, _("The folder contents could not be displayed"),
1124 /* Changes folders, displaying an error dialog if this fails */
1126 change_folder_and_display_error (GtkFileChooserDefault *impl,
1128 gboolean clear_entry)
1133 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1135 /* We copy the path because of this case:
1137 * list_row_activated()
1138 * fetches path from model; path belongs to the model (*)
1139 * calls change_folder_and_display_error()
1140 * calls gtk_file_chooser_set_current_folder_file()
1141 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1145 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1148 error_changing_folder_dialog (impl, file, error);
1154 emit_default_size_changed (GtkFileChooserDefault *impl)
1156 if (!GTK_WIDGET_MAPPED (impl))
1159 profile_msg (" emit default-size-changed start", NULL);
1160 g_signal_emit_by_name (impl, "default-size-changed");
1161 profile_msg (" emit default-size-changed end", NULL);
1165 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1167 if (impl->use_preview_label)
1169 if (!impl->preview_label)
1171 impl->preview_label = gtk_label_new (impl->preview_display_name);
1172 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1173 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1174 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1175 gtk_widget_show (impl->preview_label);
1180 if (impl->preview_label)
1182 gtk_widget_destroy (impl->preview_label);
1183 impl->preview_label = NULL;
1187 if (impl->preview_widget_active && impl->preview_widget)
1188 gtk_widget_show (impl->preview_box);
1190 gtk_widget_hide (impl->preview_box);
1192 emit_default_size_changed (impl);
1196 set_preview_widget (GtkFileChooserDefault *impl,
1197 GtkWidget *preview_widget)
1199 if (preview_widget == impl->preview_widget)
1202 if (impl->preview_widget)
1203 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1204 impl->preview_widget);
1206 impl->preview_widget = preview_widget;
1207 if (impl->preview_widget)
1209 gtk_widget_show (impl->preview_widget);
1210 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1211 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1212 impl->preview_widget,
1213 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1216 update_preview_widget_visibility (impl);
1219 /* Renders a "Search" icon at an appropriate size for a tree view */
1221 render_search_icon (GtkFileChooserDefault *impl)
1223 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1227 render_recent_icon (GtkFileChooserDefault *impl)
1229 GtkIconTheme *theme;
1232 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1233 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1235 theme = gtk_icon_theme_get_default ();
1237 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1243 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1249 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1250 struct ReloadIconsData
1252 GtkFileChooserDefault *impl;
1253 GtkTreeRowReference *row_ref;
1257 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1259 const GError *error,
1265 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1266 struct ReloadIconsData *data = user_data;
1268 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1271 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1273 if (cancelled || error)
1276 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1278 path = gtk_tree_row_reference_get_path (data->row_ref);
1279 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1280 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1281 SHORTCUTS_COL_PIXBUF, pixbuf,
1283 gtk_tree_path_free (path);
1286 g_object_unref (pixbuf);
1289 gtk_tree_row_reference_free (data->row_ref);
1290 g_object_unref (data->impl);
1293 g_object_unref (cancellable);
1297 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1302 profile_start ("start", NULL);
1304 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1307 for (l = impl->reload_icon_cancellables; l; l = l->next)
1309 GCancellable *cancellable = G_CANCELLABLE (l->data);
1310 g_cancellable_cancel (cancellable);
1312 g_slist_free (impl->reload_icon_cancellables);
1313 impl->reload_icon_cancellables = NULL;
1318 ShortcutType shortcut_type;
1319 gboolean pixbuf_visible;
1322 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1323 SHORTCUTS_COL_DATA, &data,
1324 SHORTCUTS_COL_TYPE, &shortcut_type,
1325 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1331 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1333 GtkFileSystemVolume *volume;
1336 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1337 impl->icon_size, NULL);
1339 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1341 if (g_file_is_native (G_FILE (data)))
1344 struct ReloadIconsData *info;
1345 GtkTreePath *tree_path;
1346 GCancellable *cancellable;
1350 info = g_new0 (struct ReloadIconsData, 1);
1351 info->impl = g_object_ref (impl);
1352 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1353 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1354 gtk_tree_path_free (tree_path);
1356 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1358 shortcuts_reload_icons_get_info_cb,
1360 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1364 GtkIconTheme *icon_theme;
1366 /* Don't call get_info for remote paths to avoid latency and
1368 * If we switch to a better bookmarks file format (XBEL), we
1369 * should use mime info to get a better icon.
1371 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1372 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1373 impl->icon_size, 0, NULL);
1376 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1378 pixbuf = render_search_icon (impl);
1380 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1382 pixbuf = render_recent_icon (impl);
1385 gtk_list_store_set (impl->shortcuts_model, &iter,
1386 SHORTCUTS_COL_PIXBUF, pixbuf,
1390 g_object_unref (pixbuf);
1394 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1398 profile_end ("end", NULL);
1402 shortcuts_find_folder (GtkFileChooserDefault *impl,
1405 GtkTreeSelection *selection;
1409 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1411 g_assert (folder != NULL);
1412 pos = shortcut_find_position (impl, folder);
1415 gtk_tree_selection_unselect_all (selection);
1419 path = gtk_tree_path_new_from_indices (pos, -1);
1420 gtk_tree_selection_select_path (selection, path);
1421 gtk_tree_path_free (path);
1424 /* If a shortcut corresponds to the current folder, selects it */
1426 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1428 shortcuts_find_folder (impl, impl->current_folder);
1431 /* Removes the specified number of rows from the shortcuts list */
1433 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1439 path = gtk_tree_path_new_from_indices (start_row, -1);
1441 for (; n_rows; n_rows--)
1445 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1446 g_assert_not_reached ();
1448 shortcuts_free_row_data (impl, &iter);
1449 gtk_list_store_remove (impl->shortcuts_model, &iter);
1452 gtk_tree_path_free (path);
1456 shortcuts_update_count (GtkFileChooserDefault *impl,
1457 ShortcutsIndex type,
1462 case SHORTCUTS_HOME:
1464 impl->has_home = FALSE;
1466 impl->has_home = TRUE;
1469 case SHORTCUTS_DESKTOP:
1471 impl->has_desktop = FALSE;
1473 impl->has_desktop = TRUE;
1476 case SHORTCUTS_VOLUMES:
1477 impl->num_volumes += value;
1480 case SHORTCUTS_SHORTCUTS:
1481 impl->num_shortcuts += value;
1484 case SHORTCUTS_BOOKMARKS:
1485 impl->num_bookmarks += value;
1488 case SHORTCUTS_CURRENT_FOLDER:
1490 impl->shortcuts_current_folder_active = FALSE;
1492 impl->shortcuts_current_folder_active = TRUE;
1501 struct ShortcutsInsertRequest
1503 GtkFileChooserDefault *impl;
1507 GtkTreeRowReference *row_ref;
1508 ShortcutsIndex type;
1514 get_file_info_finished (GCancellable *cancellable,
1516 const GError *error,
1520 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1524 GCancellable *model_cancellable;
1525 struct ShortcutsInsertRequest *request = data;
1527 path = gtk_tree_row_reference_get_path (request->row_ref);
1529 /* Handle doesn't exist anymore in the model */
1532 pos = gtk_tree_path_get_indices (path)[0];
1533 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1535 gtk_tree_path_free (path);
1537 /* validate cancellable, else goto out */
1538 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1539 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1541 if (cancellable != model_cancellable)
1544 /* set the cancellable to NULL in the model (we unref later on) */
1545 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1546 SHORTCUTS_COL_CANCELLABLE, NULL,
1554 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1555 shortcuts_update_count (request->impl, request->type, -1);
1557 if (request->type == SHORTCUTS_HOME)
1561 home = g_file_new_for_path (g_get_home_dir ());
1562 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1563 g_object_unref (home);
1565 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1567 /* Remove the current folder separator */
1568 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1569 shortcuts_remove_rows (request->impl, separator_pos, 1);
1575 if (!request->label_copy)
1576 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1577 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1578 request->impl->icon_size);
1580 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1581 SHORTCUTS_COL_PIXBUF, pixbuf,
1582 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1583 SHORTCUTS_COL_NAME, request->label_copy,
1584 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1585 SHORTCUTS_COL_REMOVABLE, request->removable,
1588 if (request->impl->shortcuts_pane_filter_model)
1589 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1591 if (request->impl->shortcuts_combo_filter_model)
1592 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1594 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1595 request->impl->save_folder_combo != NULL)
1597 /* The current folder is updated via _activate_iter(), don't
1598 * have save_folder_combo_changed_cb() call _activate_iter()
1601 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1602 G_CALLBACK (save_folder_combo_changed_cb),
1605 if (request->impl->has_search)
1608 if (request->impl->has_recent)
1611 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1612 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1613 G_CALLBACK (save_folder_combo_changed_cb),
1618 g_object_unref (pixbuf);
1621 g_object_unref (request->impl);
1622 g_object_unref (request->file);
1623 gtk_tree_row_reference_free (request->row_ref);
1624 g_free (request->label_copy);
1627 g_object_unref (cancellable);
1630 /* FIXME: GtkFileSystem needs a function to split a remote path
1631 * into hostname and path components, or maybe just have a
1632 * gtk_file_system_path_get_display_name().
1634 * This function is also used in gtkfilechooserbutton.c
1637 _gtk_file_chooser_label_for_file (GFile *file)
1639 const gchar *path, *start, *end, *p;
1640 gchar *uri, *host, *label;
1642 uri = g_file_get_uri (file);
1644 start = strstr (uri, "://");
1648 path = strchr (start, '/');
1653 end = uri + strlen (uri);
1657 /* strip username */
1658 p = strchr (start, '@');
1662 p = strchr (start, ':');
1666 host = g_strndup (start, end - start);
1668 /* Translators: the first string is a path and the second string
1669 * is a hostname. Nautilus and the panel contain the same string
1672 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1678 label = g_strdup (uri);
1686 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1687 * inserts a volume. A position of -1 indicates the end of the tree.
1690 shortcuts_insert_file (GtkFileChooserDefault *impl,
1692 ShortcutType shortcut_type,
1693 GtkFileSystemVolume *volume,
1697 ShortcutsIndex type)
1700 GdkPixbuf *pixbuf = NULL;
1701 gpointer data = NULL;
1703 GtkIconTheme *icon_theme;
1705 profile_start ("start shortcut", NULL);
1707 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1710 label_copy = _gtk_file_system_volume_get_display_name (volume);
1711 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1712 impl->icon_size, NULL);
1714 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1716 if (g_file_is_native (file))
1718 struct ShortcutsInsertRequest *request;
1719 GCancellable *cancellable;
1722 request = g_new0 (struct ShortcutsInsertRequest, 1);
1723 request->impl = g_object_ref (impl);
1724 request->file = g_object_ref (file);
1725 request->name_only = TRUE;
1726 request->removable = removable;
1728 request->type = type;
1730 request->label_copy = g_strdup (label);
1733 gtk_list_store_append (impl->shortcuts_model, &iter);
1735 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1737 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1738 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1739 gtk_tree_path_free (p);
1741 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1742 "standard::is-hidden,standard::display-name,standard::icon",
1743 get_file_info_finished, request);
1745 gtk_list_store_set (impl->shortcuts_model, &iter,
1746 SHORTCUTS_COL_DATA, g_object_ref (file),
1747 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1748 SHORTCUTS_COL_CANCELLABLE, cancellable,
1751 shortcuts_update_count (impl, type, 1);
1757 /* Don't call get_info for remote paths to avoid latency and
1760 data = g_object_ref (file);
1762 label_copy = g_strdup (label);
1764 label_copy = _gtk_file_chooser_label_for_file (file);
1766 /* If we switch to a better bookmarks file format (XBEL), we
1767 * should use mime info to get a better icon.
1769 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1770 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1771 impl->icon_size, 0, NULL);
1776 g_assert_not_reached ();
1782 gtk_list_store_append (impl->shortcuts_model, &iter);
1784 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1786 shortcuts_update_count (impl, type, 1);
1788 gtk_list_store_set (impl->shortcuts_model, &iter,
1789 SHORTCUTS_COL_PIXBUF, pixbuf,
1790 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1791 SHORTCUTS_COL_NAME, label_copy,
1792 SHORTCUTS_COL_DATA, data,
1793 SHORTCUTS_COL_TYPE, shortcut_type,
1794 SHORTCUTS_COL_REMOVABLE, removable,
1795 SHORTCUTS_COL_CANCELLABLE, NULL,
1798 if (impl->shortcuts_pane_filter_model)
1799 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1801 if (impl->shortcuts_combo_filter_model)
1802 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1804 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1806 /* The current folder is updated via _activate_iter(), don't
1807 * have save_folder_combo_changed_cb() call _activate_iter()
1810 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1812 if (impl->has_search)
1815 if (impl->has_recent)
1818 g_signal_handlers_block_by_func (impl->save_folder_combo,
1819 G_CALLBACK (save_folder_combo_changed_cb),
1822 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1823 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1824 G_CALLBACK (save_folder_combo_changed_cb),
1828 g_free (label_copy);
1831 g_object_unref (pixbuf);
1833 profile_end ("end", NULL);
1837 shortcuts_append_search (GtkFileChooserDefault *impl)
1842 pixbuf = render_search_icon (impl);
1844 gtk_list_store_append (impl->shortcuts_model, &iter);
1845 gtk_list_store_set (impl->shortcuts_model, &iter,
1846 SHORTCUTS_COL_PIXBUF, pixbuf,
1847 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1848 SHORTCUTS_COL_NAME, _("Search"),
1849 SHORTCUTS_COL_DATA, NULL,
1850 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1851 SHORTCUTS_COL_REMOVABLE, FALSE,
1855 g_object_unref (pixbuf);
1857 impl->has_search = TRUE;
1861 shortcuts_append_recent (GtkFileChooserDefault *impl)
1866 pixbuf = render_recent_icon (impl);
1868 gtk_list_store_append (impl->shortcuts_model, &iter);
1869 gtk_list_store_set (impl->shortcuts_model, &iter,
1870 SHORTCUTS_COL_PIXBUF, pixbuf,
1871 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1872 SHORTCUTS_COL_NAME, _("Recently Used"),
1873 SHORTCUTS_COL_DATA, NULL,
1874 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1875 SHORTCUTS_COL_REMOVABLE, FALSE,
1879 g_object_unref (pixbuf);
1881 impl->has_recent = TRUE;
1884 /* Appends an item for the user's home directory to the shortcuts model */
1886 shortcuts_append_home (GtkFileChooserDefault *impl)
1888 const char *home_path;
1891 profile_start ("start", NULL);
1893 home_path = g_get_home_dir ();
1894 if (home_path == NULL)
1896 profile_end ("end - no home directory!?", NULL);
1900 home = g_file_new_for_path (home_path);
1901 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1902 impl->has_home = TRUE;
1904 g_object_unref (home);
1906 profile_end ("end", NULL);
1909 /* Appends the ~/Desktop directory to the shortcuts model */
1911 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1916 profile_start ("start", NULL);
1918 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1919 file = g_file_new_for_path (name);
1920 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1921 impl->has_desktop = TRUE;
1923 /* We do not actually pop up an error dialog if there is no desktop directory
1924 * because some people may really not want to have one.
1927 g_object_unref (file);
1929 profile_end ("end", NULL);
1932 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1934 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1941 profile_start ("start", NULL);
1943 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1946 for (; bookmarks; bookmarks = bookmarks->next)
1950 file = bookmarks->data;
1952 if (impl->local_only && !g_file_is_native (file))
1955 if (shortcut_find_position (impl, file) != -1)
1958 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1960 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1964 profile_end ("end", NULL);
1966 return num_inserted;
1969 /* Returns the index for the corresponding item in the shortcuts bar */
1971 shortcuts_get_index (GtkFileChooserDefault *impl,
1972 ShortcutsIndex where)
1978 if (where == SHORTCUTS_SEARCH)
1981 n += impl->has_search ? 1 : 0;
1983 if (where == SHORTCUTS_RECENT)
1986 n += impl->has_recent ? 1 : 0;
1988 if (where == SHORTCUTS_RECENT_SEPARATOR)
1991 n += impl->has_recent ? 1 : 0;
1993 if (where == SHORTCUTS_HOME)
1996 n += impl->has_home ? 1 : 0;
1998 if (where == SHORTCUTS_DESKTOP)
2001 n += impl->has_desktop ? 1 : 0;
2003 if (where == SHORTCUTS_VOLUMES)
2006 n += impl->num_volumes;
2008 if (where == SHORTCUTS_SHORTCUTS)
2011 n += impl->num_shortcuts;
2013 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2016 /* If there are no bookmarks there won't be a separator */
2017 n += (impl->num_bookmarks > 0) ? 1 : 0;
2019 if (where == SHORTCUTS_BOOKMARKS)
2022 n += impl->num_bookmarks;
2024 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2029 if (where == SHORTCUTS_CURRENT_FOLDER)
2032 g_assert_not_reached ();
2039 /* Adds all the file system volumes to the shortcuts model */
2041 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2046 gboolean old_changing_folders;
2048 profile_start ("start", NULL);
2050 old_changing_folders = impl->changing_folder;
2051 impl->changing_folder = TRUE;
2053 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2054 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2055 impl->num_volumes = 0;
2057 list = _gtk_file_system_list_volumes (impl->file_system);
2061 for (l = list; l; l = l->next)
2063 GtkFileSystemVolume *volume;
2067 if (impl->local_only)
2069 if (_gtk_file_system_volume_is_mounted (volume))
2073 base_file = _gtk_file_system_volume_get_root (volume);
2074 if (base_file != NULL && !g_file_is_native (base_file))
2079 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2083 impl->num_volumes = n;
2084 g_slist_free (list);
2086 if (impl->shortcuts_pane_filter_model)
2087 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2089 if (impl->shortcuts_combo_filter_model)
2090 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2092 impl->changing_folder = old_changing_folders;
2094 profile_end ("end", NULL);
2097 /* Inserts a separator node in the shortcuts list */
2099 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2100 ShortcutsIndex where)
2104 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2105 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2106 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2108 gtk_list_store_insert (impl->shortcuts_model, &iter,
2109 shortcuts_get_index (impl, where));
2110 gtk_list_store_set (impl->shortcuts_model, &iter,
2111 SHORTCUTS_COL_PIXBUF, NULL,
2112 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2113 SHORTCUTS_COL_NAME, NULL,
2114 SHORTCUTS_COL_DATA, NULL,
2115 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2119 /* Updates the list of bookmarks */
2121 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2124 gboolean old_changing_folders;
2126 GFile *list_selected = NULL;
2127 GFile *combo_selected = NULL;
2128 ShortcutType shortcut_type;
2131 profile_start ("start", NULL);
2133 old_changing_folders = impl->changing_folder;
2134 impl->changing_folder = TRUE;
2136 if (shortcuts_get_selected (impl, &iter))
2138 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2140 SHORTCUTS_COL_DATA, &col_data,
2141 SHORTCUTS_COL_TYPE, &shortcut_type,
2144 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2145 list_selected = g_object_ref (col_data);
2148 if (impl->save_folder_combo &&
2149 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2152 GtkTreeIter child_iter;
2154 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2157 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2159 SHORTCUTS_COL_DATA, &col_data,
2160 SHORTCUTS_COL_TYPE, &shortcut_type,
2163 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2164 combo_selected = g_object_ref (col_data);
2167 if (impl->num_bookmarks > 0)
2168 shortcuts_remove_rows (impl,
2169 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2170 impl->num_bookmarks + 1);
2172 impl->num_bookmarks = 0;
2173 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2175 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2176 shortcuts_append_bookmarks (impl, bookmarks);
2177 g_slist_free (bookmarks);
2179 if (impl->num_bookmarks == 0)
2180 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2182 if (impl->shortcuts_pane_filter_model)
2183 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2185 if (impl->shortcuts_combo_filter_model)
2186 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2190 shortcuts_find_folder (impl, list_selected);
2191 g_object_unref (list_selected);
2198 pos = shortcut_find_position (impl, combo_selected);
2201 if (impl->has_search)
2204 if (impl->has_recent)
2207 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2210 g_object_unref (combo_selected);
2213 impl->changing_folder = old_changing_folders;
2215 profile_end ("end", NULL);
2218 /* Appends a separator and a row to the shortcuts list for the current folder */
2220 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2225 g_assert (!impl->shortcuts_current_folder_active);
2229 g_assert (impl->current_folder != NULL);
2231 pos = shortcut_find_position (impl, impl->current_folder);
2234 GtkFileSystemVolume *volume;
2239 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2243 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2245 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2247 base_file = _gtk_file_system_volume_get_root (volume);
2251 if (base_file && g_file_equal (base_file, impl->current_folder))
2252 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2254 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2257 g_object_unref (base_file);
2259 else if (impl->save_folder_combo != NULL)
2261 if (impl->has_search)
2264 if (impl->has_recent)
2265 pos -= 2; /* + separator */
2267 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2271 /* Updates the current folder row in the shortcuts model */
2273 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2277 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2279 if (impl->shortcuts_current_folder_active)
2281 shortcuts_remove_rows (impl, pos, 2);
2282 impl->shortcuts_current_folder_active = FALSE;
2285 shortcuts_add_current_folder (impl);
2288 /* Filter function used for the shortcuts filter model */
2290 shortcuts_pane_filter_cb (GtkTreeModel *model,
2294 GtkFileChooserDefault *impl;
2298 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2300 path = gtk_tree_model_get_path (model, iter);
2304 pos = *gtk_tree_path_get_indices (path);
2305 gtk_tree_path_free (path);
2307 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2310 /* Creates the list model for shortcuts */
2312 shortcuts_model_create (GtkFileChooserDefault *impl)
2314 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2315 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2316 GDK_TYPE_PIXBUF, /* pixbuf */
2317 G_TYPE_STRING, /* name */
2318 G_TYPE_POINTER, /* path or volume */
2319 G_TYPE_INT, /* ShortcutType */
2320 G_TYPE_BOOLEAN, /* removable */
2321 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2322 G_TYPE_POINTER); /* GCancellable */
2324 if (search_is_possible (impl))
2326 shortcuts_append_search (impl);
2329 if (impl->recent_manager)
2331 shortcuts_append_recent (impl);
2332 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2335 if (impl->file_system)
2337 shortcuts_append_home (impl);
2338 shortcuts_append_desktop (impl);
2339 shortcuts_add_volumes (impl);
2342 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2343 GTK_TREE_MODEL (impl->shortcuts_model),
2346 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2347 shortcuts_pane_filter_cb,
2352 /* Callback used when the "New Folder" button is clicked */
2354 new_folder_button_clicked (GtkButton *button,
2355 GtkFileChooserDefault *impl)
2360 if (!impl->browse_files_model)
2361 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2363 /* Prevent button from being clicked twice */
2364 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2366 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2368 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2369 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2370 path, impl->list_name_column,
2373 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2374 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2376 impl->list_name_column,
2379 gtk_tree_path_free (path);
2382 /* Idle handler for creating a new folder after editing its name cell, or for
2383 * canceling the editing.
2386 edited_idle_cb (GtkFileChooserDefault *impl)
2388 GDK_THREADS_ENTER ();
2390 g_source_destroy (impl->edited_idle);
2391 impl->edited_idle = NULL;
2393 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2394 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2396 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2398 if (impl->edited_new_text) /* not cancelled? */
2400 GError *error = NULL;
2403 file = g_file_get_child_for_display_name (impl->current_folder,
2404 impl->edited_new_text,
2408 GError *error = NULL;
2410 if (!g_file_make_directory (file, NULL, &error))
2411 change_folder_and_display_error (impl, file, FALSE);
2413 error_creating_folder_dialog (impl, file, error);
2415 g_object_unref (file);
2418 error_creating_folder_dialog (impl, file, error);
2420 g_free (impl->edited_new_text);
2421 impl->edited_new_text = NULL;
2424 GDK_THREADS_LEAVE ();
2430 queue_edited_idle (GtkFileChooserDefault *impl,
2431 const gchar *new_text)
2433 /* We create the folder in an idle handler so that we don't modify the tree
2437 if (!impl->edited_idle)
2439 impl->edited_idle = g_idle_source_new ();
2440 g_source_set_closure (impl->edited_idle,
2441 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2443 g_source_attach (impl->edited_idle, NULL);
2446 g_free (impl->edited_new_text);
2447 impl->edited_new_text = g_strdup (new_text);
2450 /* Callback used from the text cell renderer when the new folder is named */
2452 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2454 const gchar *new_text,
2455 GtkFileChooserDefault *impl)
2457 /* work around bug #154921 */
2458 g_object_set (cell_renderer_text,
2459 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2460 queue_edited_idle (impl, new_text);
2463 /* Callback used from the text cell renderer when the new folder edition gets
2467 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2468 GtkFileChooserDefault *impl)
2470 /* work around bug #154921 */
2471 g_object_set (cell_renderer_text,
2472 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2473 queue_edited_idle (impl, NULL);
2476 /* Creates the widgets for the filter combo box */
2478 filter_create (GtkFileChooserDefault *impl)
2480 impl->filter_combo = gtk_combo_box_new_text ();
2481 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2483 g_signal_connect (impl->filter_combo, "changed",
2484 G_CALLBACK (filter_combo_changed), impl);
2486 gtk_widget_set_tooltip_text (impl->filter_combo,
2487 _("Select which types of files are shown"));
2489 return impl->filter_combo;
2493 button_new (GtkFileChooserDefault *impl,
2495 const char *stock_id,
2503 button = gtk_button_new_with_mnemonic (text);
2504 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2505 gtk_button_set_image (GTK_BUTTON (button), image);
2507 gtk_widget_set_sensitive (button, sensitive);
2508 g_signal_connect (button, "clicked", callback, impl);
2511 gtk_widget_show (button);
2516 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2518 shortcut_find_position (GtkFileChooserDefault *impl,
2523 int current_folder_separator_idx;
2525 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2528 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2531 /* FIXME: is this still needed? */
2532 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2536 for (i = 0; i < current_folder_separator_idx; i++)
2539 ShortcutType shortcut_type;
2541 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2542 SHORTCUTS_COL_DATA, &col_data,
2543 SHORTCUTS_COL_TYPE, &shortcut_type,
2548 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2550 GtkFileSystemVolume *volume;
2555 base_file = _gtk_file_system_volume_get_root (volume);
2557 exists = base_file && g_file_equal (file, base_file);
2560 g_object_unref (base_file);
2565 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2569 model_file = col_data;
2571 if (model_file && g_file_equal (model_file, file))
2576 if (i < current_folder_separator_idx - 1)
2578 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2579 g_assert_not_reached ();
2586 /* Tries to add a bookmark from a path name */
2588 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2594 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2596 if (shortcut_find_position (impl, file) != -1)
2600 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2602 error_adding_bookmark_dialog (impl, file, error);
2610 add_bookmark_foreach_cb (GtkTreeModel *model,
2615 GtkFileChooserDefault *impl;
2616 GtkFileSystemModel *fs_model;
2617 GtkTreeIter child_iter;
2620 impl = (GtkFileChooserDefault *) data;
2622 switch (impl->operation_mode)
2624 case OPERATION_MODE_BROWSE:
2625 fs_model = impl->browse_files_model;
2626 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2627 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2630 case OPERATION_MODE_SEARCH:
2631 search_get_valid_child_iter (impl, &child_iter, iter);
2632 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2633 SEARCH_MODEL_COL_FILE, &file,
2637 case OPERATION_MODE_RECENT:
2638 recent_get_valid_child_iter (impl, &child_iter, iter);
2639 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2640 RECENT_MODEL_COL_FILE, &file,
2645 shortcuts_add_bookmark_from_file (impl, file, -1);
2648 /* Adds a bookmark from the currently selected item in the file list */
2650 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2652 GtkTreeSelection *selection;
2654 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2656 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2657 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2659 gtk_tree_selection_selected_foreach (selection,
2660 add_bookmark_foreach_cb,
2664 /* Callback used when the "Add bookmark" button is clicked */
2666 add_bookmark_button_clicked_cb (GtkButton *button,
2667 GtkFileChooserDefault *impl)
2669 bookmarks_add_selected_folder (impl);
2672 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2673 * returns FALSE if no shortcut is selected.
2676 shortcuts_get_selected (GtkFileChooserDefault *impl,
2679 GtkTreeSelection *selection;
2680 GtkTreeIter parent_iter;
2682 if (!impl->browse_shortcuts_tree_view)
2685 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2687 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2690 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2696 /* Removes the selected bookmarks */
2698 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2706 if (!shortcuts_get_selected (impl, &iter))
2709 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2710 SHORTCUTS_COL_DATA, &col_data,
2711 SHORTCUTS_COL_REMOVABLE, &removable,
2717 g_assert (col_data != NULL);
2722 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2723 error_removing_bookmark_dialog (impl, file, error);
2726 /* Callback used when the "Remove bookmark" button is clicked */
2728 remove_bookmark_button_clicked_cb (GtkButton *button,
2729 GtkFileChooserDefault *impl)
2731 remove_selected_bookmarks (impl);
2734 struct selection_check_closure {
2735 GtkFileChooserDefault *impl;
2738 gboolean all_folders;
2741 /* Used from gtk_tree_selection_selected_foreach() */
2743 selection_check_foreach_cb (GtkTreeModel *model,
2748 struct selection_check_closure *closure;
2749 GtkTreeIter child_iter;
2754 closure->num_selected++;
2756 switch (closure->impl->operation_mode)
2758 case OPERATION_MODE_BROWSE:
2759 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2760 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2761 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2764 case OPERATION_MODE_SEARCH:
2765 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2766 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2767 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2771 case OPERATION_MODE_RECENT:
2772 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2773 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2774 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2779 closure->all_folders = closure->all_folders && is_folder;
2780 closure->all_files = closure->all_files && !is_folder;
2783 /* Checks whether the selected items in the file list are all files or all folders */
2785 selection_check (GtkFileChooserDefault *impl,
2787 gboolean *all_files,
2788 gboolean *all_folders)
2790 struct selection_check_closure closure;
2791 GtkTreeSelection *selection;
2793 closure.impl = impl;
2794 closure.num_selected = 0;
2795 closure.all_files = TRUE;
2796 closure.all_folders = TRUE;
2798 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2799 gtk_tree_selection_selected_foreach (selection,
2800 selection_check_foreach_cb,
2803 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2806 *num_selected = closure.num_selected;
2809 *all_files = closure.all_files;
2812 *all_folders = closure.all_folders;
2815 struct get_selected_file_closure {
2816 GtkFileChooserDefault *impl;
2821 get_selected_file_foreach_cb (GtkTreeModel *model,
2826 struct get_selected_file_closure *closure;
2827 GtkTreeIter child_iter;
2831 switch (closure->impl->operation_mode)
2833 case OPERATION_MODE_BROWSE:
2834 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2835 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2838 case OPERATION_MODE_SEARCH:
2839 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2840 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2841 SEARCH_MODEL_COL_FILE, &closure->file,
2845 case OPERATION_MODE_RECENT:
2846 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2847 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2848 RECENT_MODEL_COL_FILE, &closure->file,
2854 /* Returns a selected path from the file list */
2856 get_selected_file (GtkFileChooserDefault *impl)
2858 struct get_selected_file_closure closure;
2859 GtkTreeSelection *selection;
2861 closure.impl = impl;
2862 closure.file = NULL;
2864 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2865 gtk_tree_selection_selected_foreach (selection,
2866 get_selected_file_foreach_cb,
2869 return closure.file;
2873 GtkFileChooserDefault *impl;
2875 } UpdateTooltipData;
2878 update_tooltip (GtkTreeModel *model,
2883 UpdateTooltipData *udata = data;
2884 GtkTreeIter child_iter;
2887 if (udata->tip == NULL)
2889 const gchar *display_name;
2891 switch (udata->impl->operation_mode)
2893 case OPERATION_MODE_BROWSE:
2894 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2897 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2898 display_name = g_file_info_get_display_name (info);
2901 case OPERATION_MODE_SEARCH:
2902 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2903 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2904 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2908 case OPERATION_MODE_RECENT:
2909 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2910 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2911 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2916 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2922 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2923 * if there are no selected items *and* the current folder is not in the
2924 * bookmarks list. De-sensitize the button otherwise.
2927 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2930 gboolean all_folders;
2934 selection_check (impl, &num_selected, NULL, &all_folders);
2936 if (num_selected == 0)
2937 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2938 else if (num_selected == 1)
2942 file = get_selected_file (impl);
2943 active = all_folders && (shortcut_find_position (impl, file) == -1);
2946 active = all_folders;
2948 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2950 if (impl->browse_files_popup_menu_add_shortcut_item)
2951 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2952 (num_selected == 0) ? FALSE : active);
2956 if (num_selected == 0)
2957 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2958 else if (num_selected > 1)
2959 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2962 GtkTreeSelection *selection;
2963 UpdateTooltipData data;
2965 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2968 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2972 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2977 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2978 * bookmark row is selected in the shortcuts tree.
2981 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2984 gboolean removable = FALSE;
2987 if (shortcuts_get_selected (impl, &iter))
2988 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2989 SHORTCUTS_COL_REMOVABLE, &removable,
2990 SHORTCUTS_COL_NAME, &name,
2993 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2999 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3000 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3008 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3011 gboolean removable = FALSE;
3013 if (impl->browse_shortcuts_popup_menu == NULL)
3016 if (shortcuts_get_selected (impl, &iter))
3017 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3018 SHORTCUTS_COL_REMOVABLE, &removable,
3021 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3022 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3025 /* GtkWidget::drag-begin handler for the shortcuts list. */
3027 shortcuts_drag_begin_cb (GtkWidget *widget,
3028 GdkDragContext *context,
3029 GtkFileChooserDefault *impl)
3032 impl->shortcuts_drag_context = g_object_ref (context);
3037 /* Removes the idle handler for outside drags */
3039 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3041 if (!impl->shortcuts_drag_outside_idle)
3044 g_source_destroy (impl->shortcuts_drag_outside_idle);
3045 impl->shortcuts_drag_outside_idle = NULL;
3049 /* GtkWidget::drag-end handler for the shortcuts list. */
3051 shortcuts_drag_end_cb (GtkWidget *widget,
3052 GdkDragContext *context,
3053 GtkFileChooserDefault *impl)
3056 g_object_unref (impl->shortcuts_drag_context);
3058 shortcuts_cancel_drag_outside_idle (impl);
3060 if (!impl->shortcuts_drag_outside)
3063 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3065 impl->shortcuts_drag_outside = FALSE;
3069 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3071 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3072 GdkDragContext *context,
3073 GtkFileChooserDefault *impl)
3075 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3079 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3083 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3086 GtkTreeView *tree_view;
3089 GdkPixmap *row_pixmap;
3094 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3096 /* Find the selected path and get its drag pixmap */
3098 if (!shortcuts_get_selected (impl, &iter))
3099 g_assert_not_reached ();
3101 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3103 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3104 gtk_tree_path_free (path);
3113 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3119 GdkPixmap *composite;
3120 int row_pixmap_width, row_pixmap_height;
3121 int pixbuf_width, pixbuf_height;
3122 int composite_width, composite_height;
3123 int pixbuf_x, pixbuf_y;
3124 GdkGC *gc, *mask_gc;
3126 GdkBitmap *pixbuf_mask;
3128 /* Create pixmap and mask for composite image */
3130 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3131 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3132 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3134 composite_width = MAX (row_pixmap_width, pixbuf_width);
3135 composite_height = MAX (row_pixmap_height, pixbuf_height);
3137 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3139 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3142 pixbuf_x = composite_width - pixbuf_width;
3144 pixbuf_y = (composite_height - pixbuf_height) / 2;
3146 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3147 gc = gdk_gc_new (composite);
3149 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3150 mask_gc = gdk_gc_new (mask);
3152 gdk_gc_set_foreground (mask_gc, &color);
3153 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3156 color.green = 0xffff;
3157 color.blue = 0xffff;
3158 gdk_gc_set_rgb_fg_color (gc, &color);
3159 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3161 /* Composite the row pixmap and the pixbuf */
3163 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3165 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3166 NULL, &pixbuf_mask, 128);
3167 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3170 pixbuf_width, pixbuf_height);
3171 g_object_unref (pixbuf_mask);
3173 gdk_draw_drawable (composite, gc, row_pixmap,
3176 row_pixmap_width, row_pixmap_height);
3178 gdk_gc_set_foreground (mask_gc, &color);
3179 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3181 gdk_draw_pixbuf (composite, gc, pixbuf,
3184 pixbuf_width, pixbuf_height,
3188 g_object_unref (pixbuf);
3189 g_object_unref (row_pixmap);
3191 row_pixmap = composite;
3195 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3197 gtk_tree_view_get_path_at_pos (tree_view,
3198 tree_view->priv->press_start_x,
3199 tree_view->priv->press_start_y,
3205 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3206 gdk_drawable_get_colormap (row_pixmap),
3209 tree_view->priv->press_start_x + 1,
3210 row_pixmap_y + cell_y + 1);
3212 g_object_unref (row_pixmap);
3214 g_object_unref (mask);
3217 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3218 * handler so that we can tell apart the drag_leave event that comes right
3219 * before a drag_drop, from a normal drag_leave. We don't want to set the
3220 * cursor nor the flag in the latter case.
3223 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3225 GDK_THREADS_ENTER ();
3227 shortcuts_drag_set_delete_cursor (impl, TRUE);
3228 impl->shortcuts_drag_outside = TRUE;
3230 shortcuts_cancel_drag_outside_idle (impl);
3232 GDK_THREADS_LEAVE ();
3238 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3242 shortcuts_drag_leave_cb (GtkWidget *widget,
3243 GdkDragContext *context,
3245 GtkFileChooserDefault *impl)
3248 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3250 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3251 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3252 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3254 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3258 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3260 GTK_TREE_VIEW_DROP_BEFORE);
3262 g_signal_stop_emission_by_name (widget, "drag-leave");
3265 /* Computes the appropriate row and position for dropping */
3267 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3271 GtkTreeViewDropPosition *pos)
3273 GtkTreeView *tree_view;
3274 GtkTreeViewColumn *column;
3278 int bookmarks_index;
3280 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3282 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3284 if (!gtk_tree_view_get_path_at_pos (tree_view,
3286 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3292 row = bookmarks_index + impl->num_bookmarks - 1;
3293 *path = gtk_tree_path_new_from_indices (row, -1);
3294 *pos = GTK_TREE_VIEW_DROP_AFTER;
3298 row = *gtk_tree_path_get_indices (*path);
3299 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3300 gtk_tree_path_free (*path);
3302 if (row < bookmarks_index)
3304 row = bookmarks_index;
3305 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3307 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3309 row = bookmarks_index + impl->num_bookmarks - 1;
3310 *pos = GTK_TREE_VIEW_DROP_AFTER;
3314 if (cell_y < cell.height / 2)
3315 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3317 *pos = GTK_TREE_VIEW_DROP_AFTER;
3320 *path = gtk_tree_path_new_from_indices (row, -1);
3323 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3324 * implement the destination side of DnD by hand, due to limitations in
3325 * GtkTreeView's DnD API.
3328 shortcuts_drag_motion_cb (GtkWidget *widget,
3329 GdkDragContext *context,
3333 GtkFileChooserDefault *impl)
3336 GtkTreeViewDropPosition pos;
3337 GdkDragAction action;
3340 if (gtk_drag_get_source_widget (context) == widget)
3342 shortcuts_cancel_drag_outside_idle (impl);
3344 if (impl->shortcuts_drag_outside)
3346 shortcuts_drag_set_delete_cursor (impl, FALSE);
3347 impl->shortcuts_drag_outside = FALSE;
3352 if (context->suggested_action == GDK_ACTION_COPY ||
3353 (context->actions & GDK_ACTION_COPY) != 0)
3354 action = GDK_ACTION_COPY;
3355 else if (context->suggested_action == GDK_ACTION_MOVE ||
3356 (context->actions & GDK_ACTION_MOVE) != 0)
3357 action = GDK_ACTION_MOVE;
3364 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3365 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3366 gtk_tree_path_free (path);
3370 g_signal_stop_emission_by_name (widget, "drag-motion");
3374 gdk_drag_status (context, action, time_);
3381 /* GtkWidget::drag-drop handler for the shortcuts list. */
3383 shortcuts_drag_drop_cb (GtkWidget *widget,
3384 GdkDragContext *context,
3388 GtkFileChooserDefault *impl)
3391 shortcuts_cancel_drag_outside_idle (impl);
3394 g_signal_stop_emission_by_name (widget, "drag-drop");
3398 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3400 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3401 GtkSelectionData *selection_data,
3407 uris = gtk_selection_data_get_uris (selection_data);
3411 for (i = 0; uris[i]; i++)
3417 file = g_file_new_for_uri (uri);
3419 if (shortcuts_add_bookmark_from_file (impl, file, position))
3422 g_object_unref (file);
3428 /* Reorders the selected bookmark to the specified position */
3430 shortcuts_reorder (GtkFileChooserDefault *impl,
3435 ShortcutType shortcut_type;
3438 int bookmarks_index;
3443 /* Get the selected path */
3445 if (!shortcuts_get_selected (impl, &iter))
3446 g_assert_not_reached ();
3448 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3449 old_position = *gtk_tree_path_get_indices (path);
3450 gtk_tree_path_free (path);
3452 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3453 old_position -= bookmarks_index;
3454 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3456 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3457 SHORTCUTS_COL_NAME, &name,
3458 SHORTCUTS_COL_DATA, &col_data,
3459 SHORTCUTS_COL_TYPE, &shortcut_type,
3461 g_assert (col_data != NULL);
3462 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3465 g_object_ref (file); /* removal below will free file, so we need a new ref */
3467 /* Remove the path from the old position and insert it in the new one */
3469 if (new_position > old_position)
3472 if (old_position == new_position)
3476 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3478 shortcuts_add_bookmark_from_file (impl, file, new_position);
3479 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3482 error_adding_bookmark_dialog (impl, file, error);
3486 g_object_unref (file);
3489 /* Callback used when we get the drag data for the bookmarks list. We add the
3490 * received URIs as bookmarks if they are folders.
3493 shortcuts_drag_data_received_cb (GtkWidget *widget,
3494 GdkDragContext *context,
3497 GtkSelectionData *selection_data,
3502 GtkFileChooserDefault *impl;
3503 GtkTreePath *tree_path;
3504 GtkTreeViewDropPosition tree_pos;
3506 int bookmarks_index;
3508 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3510 /* Compute position */
3512 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3514 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3515 position = *gtk_tree_path_get_indices (tree_path);
3516 gtk_tree_path_free (tree_path);
3518 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3521 g_assert (position >= bookmarks_index);
3522 position -= bookmarks_index;
3524 if (gtk_targets_include_uri (&selection_data->target, 1))
3525 shortcuts_drop_uris (impl, selection_data, position);
3526 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3527 shortcuts_reorder (impl, position);
3529 g_signal_stop_emission_by_name (widget, "drag-data-received");
3532 /* Callback used to display a tooltip in the shortcuts tree */
3534 shortcuts_query_tooltip_cb (GtkWidget *widget,
3537 gboolean keyboard_mode,
3538 GtkTooltip *tooltip,
3539 GtkFileChooserDefault *impl)
3541 GtkTreeModel *model;
3544 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3552 ShortcutType shortcut_type;
3554 gtk_tree_model_get (model, &iter,
3555 SHORTCUTS_COL_DATA, &col_data,
3556 SHORTCUTS_COL_TYPE, &shortcut_type,
3559 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3561 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3565 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3570 file = G_FILE (col_data);
3571 parse_name = g_file_get_parse_name (file);
3573 gtk_tooltip_set_text (tooltip, parse_name);
3575 g_free (parse_name);
3579 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3583 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3593 /* Callback used when the selection in the shortcuts tree changes */
3595 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3596 GtkFileChooserDefault *impl)
3599 GtkTreeIter child_iter;
3601 bookmarks_check_remove_sensitivity (impl);
3602 shortcuts_check_popup_sensitivity (impl);
3604 if (impl->changing_folder)
3607 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3609 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3612 shortcuts_activate_iter (impl, &child_iter);
3617 shortcuts_row_separator_func (GtkTreeModel *model,
3621 ShortcutType shortcut_type;
3623 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3625 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3628 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3629 * keypresses before the TreeView gets them.
3632 tree_view_keybinding_cb (GtkWidget *tree_view,
3634 GtkFileChooserDefault *impl)
3636 if ((event->keyval == GDK_slash
3637 || event->keyval == GDK_KP_Divide
3639 || event->keyval == GDK_asciitilde
3641 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3643 location_popup_handler (impl, event->string);
3650 /* Callback used when the file list's popup menu is detached */
3652 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3655 GtkFileChooserDefault *impl;
3657 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3658 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3660 impl->browse_shortcuts_popup_menu = NULL;
3661 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3662 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3666 remove_shortcut_cb (GtkMenuItem *item,
3667 GtkFileChooserDefault *impl)
3669 remove_selected_bookmarks (impl);
3672 /* Rename the selected bookmark */
3674 rename_selected_bookmark (GtkFileChooserDefault *impl)
3678 GtkTreeViewColumn *column;
3679 GtkCellRenderer *cell;
3682 if (shortcuts_get_selected (impl, &iter))
3684 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3685 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3686 renderers = gtk_tree_view_column_get_cell_renderers (column);
3687 cell = g_list_nth_data (renderers, 1);
3688 g_list_free (renderers);
3689 g_object_set (cell, "editable", TRUE, NULL);
3690 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3691 path, column, cell, TRUE);
3692 gtk_tree_path_free (path);
3697 rename_shortcut_cb (GtkMenuItem *item,
3698 GtkFileChooserDefault *impl)
3700 rename_selected_bookmark (impl);
3703 /* Constructs the popup menu for the file list if needed */
3705 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3709 if (impl->browse_shortcuts_popup_menu)
3712 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3713 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3714 impl->browse_shortcuts_tree_view,
3715 shortcuts_popup_menu_detach_cb);
3717 item = gtk_image_menu_item_new_with_label (_("Remove"));
3718 impl->browse_shortcuts_popup_menu_remove_item = item;
3719 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3720 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3721 g_signal_connect (item, "activate",
3722 G_CALLBACK (remove_shortcut_cb), impl);
3723 gtk_widget_show (item);
3724 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3726 item = gtk_menu_item_new_with_label (_("Rename..."));
3727 impl->browse_shortcuts_popup_menu_rename_item = item;
3728 g_signal_connect (item, "activate",
3729 G_CALLBACK (rename_shortcut_cb), impl);
3730 gtk_widget_show (item);
3731 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3735 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3737 shortcuts_build_popup_menu (impl);
3738 shortcuts_check_popup_sensitivity (impl);
3742 popup_position_func (GtkMenu *menu,
3746 gpointer user_data);
3749 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3750 GdkEventButton *event)
3752 shortcuts_update_popup_menu (impl);
3754 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3755 NULL, NULL, NULL, NULL,
3756 event->button, event->time);
3759 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3761 popup_position_func, impl->browse_shortcuts_tree_view,
3762 0, GDK_CURRENT_TIME);
3763 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3768 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3770 shortcuts_popup_menu_cb (GtkWidget *widget,
3771 GtkFileChooserDefault *impl)
3773 shortcuts_popup_menu (impl, NULL);
3777 /* Callback used when a button is pressed on the shortcuts list.
3778 * We trap button 3 to bring up a popup menu.
3781 shortcuts_button_press_event_cb (GtkWidget *widget,
3782 GdkEventButton *event,
3783 GtkFileChooserDefault *impl)
3785 static gboolean in_press = FALSE;
3791 if (event->button != 3)
3795 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3801 shortcuts_popup_menu (impl, event);
3806 shortcuts_edited (GtkCellRenderer *cell,
3809 GtkFileChooserDefault *impl)
3815 g_object_set (cell, "editable", FALSE, NULL);
3817 path = gtk_tree_path_new_from_string (path_string);
3818 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3819 g_assert_not_reached ();
3821 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3822 SHORTCUTS_COL_DATA, &shortcut,
3824 gtk_tree_path_free (path);
3826 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3830 shortcuts_editing_canceled (GtkCellRenderer *cell,
3831 GtkFileChooserDefault *impl)
3833 g_object_set (cell, "editable", FALSE, NULL);
3836 /* Creates the widgets for the shortcuts and bookmarks tree */
3838 shortcuts_list_create (GtkFileChooserDefault *impl)
3841 GtkTreeSelection *selection;
3842 GtkTreeViewColumn *column;
3843 GtkCellRenderer *renderer;
3845 /* Target types for dragging a row to/from the shortcuts list */
3846 const GtkTargetEntry tree_model_row_targets[] = {
3847 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3850 /* Scrolled window */
3852 swin = gtk_scrolled_window_new (NULL, NULL);
3853 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3854 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3855 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3857 gtk_widget_show (swin);
3861 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3862 #ifdef PROFILE_FILE_CHOOSER
3863 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3865 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3866 G_CALLBACK (tree_view_keybinding_cb), impl);
3867 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3868 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3869 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3870 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3871 /* Accessible object name for the file chooser's shortcuts pane */
3872 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3874 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3876 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3878 tree_model_row_targets,
3879 G_N_ELEMENTS (tree_model_row_targets),
3882 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3883 GTK_DEST_DEFAULT_ALL,
3884 tree_model_row_targets,
3885 G_N_ELEMENTS (tree_model_row_targets),
3886 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3887 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3889 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3890 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3891 gtk_tree_selection_set_select_function (selection,
3892 shortcuts_select_func,
3895 g_signal_connect (selection, "changed",
3896 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3898 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3899 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3901 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3902 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3903 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3904 G_CALLBACK (shortcuts_drag_end_cb), impl);
3905 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3906 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3908 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3909 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3910 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3911 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3912 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3913 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3914 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3915 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3917 /* Support tooltips */
3918 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3919 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3920 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3922 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3923 gtk_widget_show (impl->browse_shortcuts_tree_view);
3927 column = gtk_tree_view_column_new ();
3928 /* Column header for the file chooser's shortcuts pane */
3929 gtk_tree_view_column_set_title (column, _("_Places"));
3931 renderer = gtk_cell_renderer_pixbuf_new ();
3932 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3933 gtk_tree_view_column_set_attributes (column, renderer,
3934 "pixbuf", SHORTCUTS_COL_PIXBUF,
3935 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3938 renderer = gtk_cell_renderer_text_new ();
3939 g_signal_connect (renderer, "edited",
3940 G_CALLBACK (shortcuts_edited), impl);
3941 g_signal_connect (renderer, "editing-canceled",
3942 G_CALLBACK (shortcuts_editing_canceled), impl);
3943 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3944 gtk_tree_view_column_set_attributes (column, renderer,
3945 "text", SHORTCUTS_COL_NAME,
3948 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3949 shortcuts_row_separator_func,
3952 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3957 /* Creates the widgets for the shortcuts/bookmarks pane */
3959 shortcuts_pane_create (GtkFileChooserDefault *impl,
3960 GtkSizeGroup *size_group)
3966 vbox = gtk_vbox_new (FALSE, 6);
3967 gtk_widget_show (vbox);
3969 /* Shortcuts tree */
3971 widget = shortcuts_list_create (impl);
3972 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3974 /* Box for buttons */
3976 hbox = gtk_hbox_new (TRUE, 6);
3977 gtk_size_group_add_widget (size_group, hbox);
3978 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3979 gtk_widget_show (hbox);
3981 /* Add bookmark button */
3983 impl->browse_shortcuts_add_button = button_new (impl,
3988 G_CALLBACK (add_bookmark_button_clicked_cb));
3989 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3990 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3991 _("Add the selected folder to the Bookmarks"));
3993 /* Remove bookmark button */
3995 impl->browse_shortcuts_remove_button = button_new (impl,
4000 G_CALLBACK (remove_bookmark_button_clicked_cb));
4001 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4002 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4003 _("Remove the selected bookmark"));
4008 /* Handles key press events on the file list, so that we can trap Enter to
4009 * activate the default button on our own. Also, checks to see if '/' has been
4010 * pressed. See comment by tree_view_keybinding_cb() for more details.
4013 trap_activate_cb (GtkWidget *widget,
4017 GtkFileChooserDefault *impl;
4020 impl = (GtkFileChooserDefault *) data;
4022 modifiers = gtk_accelerator_get_default_mod_mask ();
4024 if ((event->keyval == GDK_slash
4025 || event->keyval == GDK_KP_Divide
4027 || event->keyval == GDK_asciitilde
4029 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4031 location_popup_handler (impl, event->string);
4035 if ((event->keyval == GDK_Return
4036 || event->keyval == GDK_ISO_Enter
4037 || event->keyval == GDK_KP_Enter
4038 || event->keyval == GDK_space
4039 || event->keyval == GDK_KP_Space)
4040 && ((event->state & modifiers) == 0)
4041 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4042 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4046 window = get_toplevel (widget);
4048 && widget != window->default_widget
4049 && !(widget == window->focus_widget &&
4050 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4052 gtk_window_activate_default (window);
4060 /* Callback used when the file list's popup menu is detached */
4062 popup_menu_detach_cb (GtkWidget *attach_widget,
4065 GtkFileChooserDefault *impl;
4067 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4068 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4070 impl->browse_files_popup_menu = NULL;
4071 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4072 impl->browse_files_popup_menu_hidden_files_item = NULL;
4075 /* Callback used when the "Add to Bookmarks" menu item is activated */
4077 add_to_shortcuts_cb (GtkMenuItem *item,
4078 GtkFileChooserDefault *impl)
4080 bookmarks_add_selected_folder (impl);
4083 /* Callback used when the "Show Hidden Files" menu item is toggled */
4085 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4086 GtkFileChooserDefault *impl)
4089 "show-hidden", gtk_check_menu_item_get_active (item),
4093 /* Shows an error dialog about not being able to select a dragged file */
4095 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4100 _("Could not select file"),
4105 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4110 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4112 for (i = 1; uris[i]; i++)
4115 GError *error = NULL;
4118 file = g_file_new_for_uri (uri);
4120 gtk_file_chooser_default_select_file (chooser, file, &error);
4122 error_selecting_dragged_file_dialog (impl, file, error);
4124 g_object_unref (file);
4128 struct FileListDragData
4130 GtkFileChooserDefault *impl;
4136 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4138 const GError *error,
4141 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4142 struct FileListDragData *data = user_data;
4143 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4145 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4148 data->impl->file_list_drag_data_received_cancellable = NULL;
4150 if (cancelled || error)
4153 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4154 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4155 data->uris[1] == 0 && !error &&
4156 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4157 change_folder_and_display_error (data->impl, data->file, FALSE);
4160 GError *error = NULL;
4162 gtk_file_chooser_default_unselect_all (chooser);
4163 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4165 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4167 browse_files_center_selected_row (data->impl);
4170 if (data->impl->select_multiple)
4171 file_list_drag_data_select_uris (data->impl, data->uris);
4174 g_object_unref (data->impl);
4175 g_strfreev (data->uris);
4176 g_object_unref (data->file);
4179 g_object_unref (cancellable);
4183 file_list_drag_data_received_cb (GtkWidget *widget,
4184 GdkDragContext *context,
4187 GtkSelectionData *selection_data,
4192 GtkFileChooserDefault *impl;
4193 GtkFileChooser *chooser;
4198 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4199 chooser = GTK_FILE_CHOOSER (data);
4201 /* Allow only drags from other widgets; see bug #533891. */
4202 if (gtk_drag_get_source_widget (context) == widget)
4205 /* Parse the text/uri-list string, navigate to the first one */
4206 uris = gtk_selection_data_get_uris (selection_data);
4207 if (uris && uris[0])
4209 struct FileListDragData *data;
4212 file = g_file_new_for_uri (uri);
4214 data = g_new0 (struct FileListDragData, 1);
4215 data->impl = g_object_ref (impl);
4219 if (impl->file_list_drag_data_received_cancellable)
4220 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4222 impl->file_list_drag_data_received_cancellable =
4223 _gtk_file_system_get_info (impl->file_system, file,
4225 file_list_drag_data_received_get_info_cb,
4229 g_signal_stop_emission_by_name (widget, "drag-data-received");
4232 /* Don't do anything with the drag_drop signal */
4234 file_list_drag_drop_cb (GtkWidget *widget,
4235 GdkDragContext *context,
4239 GtkFileChooserDefault *impl)
4241 g_signal_stop_emission_by_name (widget, "drag-drop");
4245 /* Disable the normal tree drag motion handler, it makes it look like you're
4246 dropping the dragged item onto a tree item */
4248 file_list_drag_motion_cb (GtkWidget *widget,
4249 GdkDragContext *context,
4253 GtkFileChooserDefault *impl)
4255 g_signal_stop_emission_by_name (widget, "drag-motion");
4259 /* Constructs the popup menu for the file list if needed */
4261 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4265 if (impl->browse_files_popup_menu)
4268 impl->browse_files_popup_menu = gtk_menu_new ();
4269 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4270 impl->browse_files_tree_view,
4271 popup_menu_detach_cb);
4273 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4274 impl->browse_files_popup_menu_add_shortcut_item = item;
4275 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4276 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4277 gtk_widget_set_sensitive (item, FALSE);
4278 g_signal_connect (item, "activate",
4279 G_CALLBACK (add_to_shortcuts_cb), impl);
4280 gtk_widget_show (item);
4281 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4283 item = gtk_separator_menu_item_new ();
4284 gtk_widget_show (item);
4285 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4287 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4288 impl->browse_files_popup_menu_hidden_files_item = item;
4289 g_signal_connect (item, "toggled",
4290 G_CALLBACK (show_hidden_toggled_cb), impl);
4291 gtk_widget_show (item);
4292 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4295 /* Updates the popup menu for the file list, creating it if necessary */
4297 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4299 file_list_build_popup_menu (impl);
4301 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4303 /* The sensitivity of the Add to Bookmarks item is set in
4304 * bookmarks_check_add_sensitivity()
4307 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4308 G_CALLBACK (show_hidden_toggled_cb), impl);
4309 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4311 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4312 G_CALLBACK (show_hidden_toggled_cb), impl);
4316 popup_position_func (GtkMenu *menu,
4322 GtkWidget *widget = GTK_WIDGET (user_data);
4323 GdkScreen *screen = gtk_widget_get_screen (widget);
4326 GdkRectangle monitor;
4328 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4330 gdk_window_get_origin (widget->window, x, y);
4332 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4334 *x += (widget->allocation.width - req.width) / 2;
4335 *y += (widget->allocation.height - req.height) / 2;
4337 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4338 gtk_menu_set_monitor (menu, monitor_num);
4339 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4341 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4342 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4348 file_list_popup_menu (GtkFileChooserDefault *impl,
4349 GdkEventButton *event)
4351 file_list_update_popup_menu (impl);
4353 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4354 NULL, NULL, NULL, NULL,
4355 event->button, event->time);
4358 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4360 popup_position_func, impl->browse_files_tree_view,
4361 0, GDK_CURRENT_TIME);
4362 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4368 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4370 list_popup_menu_cb (GtkWidget *widget,
4371 GtkFileChooserDefault *impl)
4373 file_list_popup_menu (impl, NULL);
4377 /* Callback used when a button is pressed on the file list. We trap button 3 to
4378 * bring up a popup menu.
4381 list_button_press_event_cb (GtkWidget *widget,
4382 GdkEventButton *event,
4383 GtkFileChooserDefault *impl)
4385 static gboolean in_press = FALSE;
4391 if (event->button != 3)
4395 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4398 file_list_popup_menu (impl, event);
4402 /* Sets the sort column IDs for the file list based on the operation mode */
4404 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4406 int name_id, mtime_id;
4408 name_id = mtime_id = 0;
4410 switch (impl->operation_mode)
4412 case OPERATION_MODE_BROWSE:
4413 name_id = FILE_LIST_COL_NAME;
4414 mtime_id = FILE_LIST_COL_MTIME;
4416 case OPERATION_MODE_SEARCH:
4417 name_id = SEARCH_MODEL_COL_FILE;
4418 mtime_id = SEARCH_MODEL_COL_STAT;
4420 case OPERATION_MODE_RECENT:
4421 name_id = RECENT_MODEL_COL_FILE;
4422 mtime_id = RECENT_MODEL_COL_INFO;
4426 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4427 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4431 file_list_query_tooltip_cb (GtkWidget *widget,
4434 gboolean keyboard_tip,
4435 GtkTooltip *tooltip,
4438 GtkFileChooserDefault *impl = user_data;
4439 GtkTreeIter iter, child_iter;
4440 GtkTreePath *path = NULL;
4444 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4448 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4456 switch (impl->operation_mode)
4458 case OPERATION_MODE_SEARCH:
4459 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4461 gtk_tree_path_free (path);
4465 search_get_valid_child_iter (impl, &child_iter, &iter);
4466 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4467 SEARCH_MODEL_COL_FILE, &file,
4471 case OPERATION_MODE_RECENT:
4472 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4474 gtk_tree_path_free (path);
4478 recent_get_valid_child_iter (impl, &child_iter, &iter);
4479 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4480 RECENT_MODEL_COL_FILE, &file,
4484 case OPERATION_MODE_BROWSE:
4485 g_assert_not_reached ();
4491 gtk_tree_path_free (path);
4495 filename = g_file_get_path (file);
4496 gtk_tooltip_set_text (tooltip, filename);
4497 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4502 gtk_tree_path_free (path);
4507 /* Creates the widgets for the file list */
4509 create_file_list (GtkFileChooserDefault *impl)
4512 GtkTreeSelection *selection;
4513 GtkTreeViewColumn *column;
4514 GtkCellRenderer *renderer;
4516 /* Scrolled window */
4517 swin = gtk_scrolled_window_new (NULL, NULL);
4518 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4519 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4520 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4523 /* Tree/list view */
4525 impl->browse_files_tree_view = gtk_tree_view_new ();
4526 #ifdef PROFILE_FILE_CHOOSER
4527 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4529 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4530 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4532 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4533 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4535 gtk_drag_dest_set (impl->browse_files_tree_view,
4536 GTK_DEST_DEFAULT_ALL,
4538 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4539 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4541 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4542 G_CALLBACK (list_row_activated), impl);
4543 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4544 G_CALLBACK (trap_activate_cb), impl);
4545 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4546 G_CALLBACK (list_popup_menu_cb), impl);
4547 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4548 G_CALLBACK (list_button_press_event_cb), impl);
4550 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4551 G_CALLBACK (file_list_drag_data_received_cb), impl);
4552 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4553 G_CALLBACK (file_list_drag_drop_cb), impl);
4554 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4555 G_CALLBACK (file_list_drag_motion_cb), impl);
4557 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4558 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4559 G_CALLBACK (file_list_query_tooltip_cb), impl);
4561 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4562 gtk_tree_selection_set_select_function (selection,
4565 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4568 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4569 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4571 g_signal_connect (selection, "changed",
4572 G_CALLBACK (list_selection_changed), impl);
4574 /* Filename column */
4576 impl->list_name_column = gtk_tree_view_column_new ();
4577 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4578 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4579 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4580 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4582 renderer = gtk_cell_renderer_pixbuf_new ();
4583 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4584 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4585 list_icon_data_func, impl, NULL);
4587 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4588 g_object_set (impl->list_name_renderer,
4589 "ellipsize", PANGO_ELLIPSIZE_END,
4591 g_signal_connect (impl->list_name_renderer, "edited",
4592 G_CALLBACK (renderer_edited_cb), impl);
4593 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4594 G_CALLBACK (renderer_editing_canceled_cb), impl);
4595 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4596 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4597 list_name_data_func, impl, NULL);
4599 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4603 column = gtk_tree_view_column_new ();
4604 gtk_tree_view_column_set_title (column, _("Size"));
4606 renderer = gtk_cell_renderer_text_new ();
4607 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4608 gtk_tree_view_column_set_cell_data_func (column, renderer,
4609 list_size_data_func, impl, NULL);
4610 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4611 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4614 /* Modification time column */
4616 column = gtk_tree_view_column_new ();
4617 gtk_tree_view_column_set_resizable (column, TRUE);
4618 gtk_tree_view_column_set_title (column, _("Modified"));
4620 renderer = gtk_cell_renderer_text_new ();
4621 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4622 gtk_tree_view_column_set_cell_data_func (column, renderer,
4623 list_mtime_data_func, impl, NULL);
4624 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4625 impl->list_mtime_column = column;
4627 file_list_set_sort_column_ids (impl);
4629 gtk_widget_show_all (swin);
4635 create_path_bar (GtkFileChooserDefault *impl)
4637 GtkWidget *path_bar;
4639 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4640 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4645 /* Creates the widgets for the files/folders pane */
4647 file_pane_create (GtkFileChooserDefault *impl,
4648 GtkSizeGroup *size_group)
4654 vbox = gtk_vbox_new (FALSE, 6);
4655 gtk_widget_show (vbox);
4657 /* Box for lists and preview */
4659 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4660 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4661 gtk_widget_show (hbox);
4665 widget = create_file_list (impl);
4666 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4670 impl->preview_box = gtk_vbox_new (FALSE, 12);
4671 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4672 /* Don't show preview box initially */
4676 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4678 widget = filter_create (impl);
4680 gtk_widget_show (widget);
4681 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4683 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4684 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4689 /* Callback used when the "Browse for more folders" expander is toggled */
4691 expander_changed_cb (GtkExpander *expander,
4693 GtkFileChooserDefault *impl)
4695 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4696 update_appearance (impl);
4699 /* Callback used when the selection changes in the save folder combo box */
4701 save_folder_combo_changed_cb (GtkComboBox *combo,
4702 GtkFileChooserDefault *impl)
4706 if (impl->changing_folder)
4709 if (gtk_combo_box_get_active_iter (combo, &iter))
4711 GtkTreeIter child_iter;
4713 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4716 shortcuts_activate_iter (impl, &child_iter);
4720 /* Filter function used to filter out the Search item and its separator.
4721 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4724 shortcuts_combo_filter_func (GtkTreeModel *model,
4728 GtkFileChooserDefault *impl;
4729 GtkTreePath *tree_path;
4734 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4736 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4738 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4739 g_assert (tree_path != NULL);
4741 indices = gtk_tree_path_get_indices (tree_path);
4745 if (impl->has_search)
4747 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4748 if (idx == indices[0])
4752 if (impl->has_recent)
4754 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4755 if (idx == indices[0])
4759 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4760 if (idx == indices[0])
4765 gtk_tree_path_free (tree_path);
4770 /* Creates the combo box with the save folders */
4772 save_folder_combo_create (GtkFileChooserDefault *impl)
4775 GtkCellRenderer *cell;
4777 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4778 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4779 shortcuts_combo_filter_func,
4783 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4784 "model", impl->shortcuts_combo_filter_model,
4785 "focus-on-click", FALSE,
4787 gtk_widget_show (combo);
4789 cell = gtk_cell_renderer_pixbuf_new ();
4790 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4791 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4792 "pixbuf", SHORTCUTS_COL_PIXBUF,
4793 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4794 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4797 cell = gtk_cell_renderer_text_new ();
4798 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4799 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4800 "text", SHORTCUTS_COL_NAME,
4801 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4804 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4805 shortcuts_row_separator_func,
4808 g_signal_connect (combo, "changed",
4809 G_CALLBACK (save_folder_combo_changed_cb), impl);
4814 /* Creates the widgets specific to Save mode */
4816 save_widgets_create (GtkFileChooserDefault *impl)
4821 GtkWidget *alignment;
4823 if (impl->save_widgets != NULL)
4826 location_switch_to_path_bar (impl);
4828 vbox = gtk_vbox_new (FALSE, 12);
4830 table = gtk_table_new (2, 2, FALSE);
4831 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4832 gtk_widget_show (table);
4833 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4834 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4838 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4839 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4840 gtk_table_attach (GTK_TABLE (table), widget,
4844 gtk_widget_show (widget);
4846 /* Location entry */
4848 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4849 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4851 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4852 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4853 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4855 GTK_EXPAND | GTK_FILL, 0,
4857 gtk_widget_show (impl->location_entry);
4858 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4861 impl->save_folder_label = gtk_label_new (NULL);
4862 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4863 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4867 gtk_widget_show (impl->save_folder_label);
4869 impl->save_folder_combo = save_folder_combo_create (impl);
4870 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4872 GTK_EXPAND | GTK_FILL, GTK_FILL,
4874 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4877 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4878 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4880 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4881 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4882 g_signal_connect (impl->save_expander, "notify::expanded",
4883 G_CALLBACK (expander_changed_cb),
4885 gtk_widget_show_all (alignment);
4887 impl->save_widgets = vbox;
4888 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4889 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4890 gtk_widget_show (impl->save_widgets);
4893 /* Destroys the widgets specific to Save mode */
4895 save_widgets_destroy (GtkFileChooserDefault *impl)
4897 if (impl->save_widgets == NULL)
4900 gtk_widget_destroy (impl->save_widgets);
4901 impl->save_widgets = NULL;
4902 impl->location_entry = NULL;
4903 impl->save_folder_label = NULL;
4904 impl->save_folder_combo = NULL;
4905 impl->save_expander = NULL;
4908 /* Turns on the path bar widget. Can be called even if we are already in that
4912 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4914 if (impl->location_entry)
4916 gtk_widget_destroy (impl->location_entry);
4917 impl->location_entry = NULL;
4920 gtk_widget_hide (impl->location_entry_box);
4923 /* Sets the full path of the current folder as the text in the location entry. */
4925 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4927 gchar *text, *filename;
4929 if (!impl->current_folder)
4932 filename = g_file_get_path (impl->current_folder);
4936 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4940 text = g_file_get_uri (impl->current_folder);
4944 gboolean need_slash;
4947 len = strlen (text);
4948 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4954 slash_text = g_new (char, len + 2);
4955 strcpy (slash_text, text);
4956 slash_text[len] = G_DIR_SEPARATOR;
4957 slash_text[len + 1] = 0;
4963 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4970 /* Turns on the location entry. Can be called even if we are already in that
4974 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4976 /* when in search or recent files mode, we are not showing the
4977 * location_entry_box container, so there's no point in switching
4980 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4981 impl->operation_mode == OPERATION_MODE_RECENT)
4984 if (impl->location_entry)
4985 gtk_widget_destroy (impl->location_entry);
4989 gtk_widget_show (impl->location_entry_box);
4993 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4994 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4996 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4997 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4999 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5000 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5002 /* Configure the entry */
5004 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5008 gtk_widget_show (impl->location_entry);
5009 gtk_widget_grab_focus (impl->location_entry);
5012 /* Sets a new location mode. set_buttons determines whether the toggle button
5013 * for the mode will also be changed.
5016 location_mode_set (GtkFileChooserDefault *impl,
5017 LocationMode new_mode,
5018 gboolean set_button)
5020 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5021 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5023 GtkWindow *toplevel;
5024 GtkWidget *current_focus;
5025 gboolean button_active;
5026 gboolean switch_to_file_list;
5030 case LOCATION_MODE_PATH_BAR:
5031 button_active = FALSE;
5033 /* The location_entry will disappear when we switch to path bar mode. So,
5034 * we'll focus the file list in that case, to avoid having a window with
5035 * no focused widget.
5037 toplevel = get_toplevel (GTK_WIDGET (impl));
5038 switch_to_file_list = FALSE;
5041 current_focus = gtk_window_get_focus (toplevel);
5042 if (!current_focus || current_focus == impl->location_entry)
5043 switch_to_file_list = TRUE;
5046 location_switch_to_path_bar (impl);
5048 if (switch_to_file_list)
5049 gtk_widget_grab_focus (impl->browse_files_tree_view);
5053 case LOCATION_MODE_FILENAME_ENTRY:
5054 button_active = TRUE;
5055 location_switch_to_filename_entry (impl);
5059 g_assert_not_reached ();
5065 g_signal_handlers_block_by_func (impl->location_button,
5066 G_CALLBACK (location_button_toggled_cb), impl);
5068 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5070 g_signal_handlers_unblock_by_func (impl->location_button,
5071 G_CALLBACK (location_button_toggled_cb), impl);
5075 impl->location_mode = new_mode;
5079 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5081 /* when in search or recent files mode, we are not showing the
5082 * location_entry_box container, so there's no point in switching
5085 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5086 impl->operation_mode == OPERATION_MODE_RECENT)
5089 /* If the file entry is not visible, show it.
5090 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5092 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5094 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5096 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5098 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5100 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5104 gtk_widget_grab_focus (impl->location_entry);
5109 /* Callback used when one of the location mode buttons is toggled */
5111 location_button_toggled_cb (GtkToggleButton *toggle,
5112 GtkFileChooserDefault *impl)
5115 LocationMode new_mode;
5117 is_active = gtk_toggle_button_get_active (toggle);
5121 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5122 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5126 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5127 new_mode = LOCATION_MODE_PATH_BAR;
5130 location_mode_set (impl, new_mode, FALSE);
5133 /* Creates a toggle button for the location entry. */
5135 location_button_create (GtkFileChooserDefault *impl)
5140 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5141 gtk_widget_show (image);
5143 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5147 g_signal_connect (impl->location_button, "toggled",
5148 G_CALLBACK (location_button_toggled_cb), impl);
5150 str = _("Type a file name");
5152 gtk_widget_set_tooltip_text (impl->location_button, str);
5153 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5156 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5158 browse_widgets_create (GtkFileChooserDefault *impl)
5164 GtkSizeGroup *size_group;
5166 /* size group is used by the [+][-] buttons and the filter combo */
5167 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5168 vbox = gtk_vbox_new (FALSE, 12);
5170 /* Location widgets */
5171 hbox = gtk_hbox_new (FALSE, 12);
5172 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5173 gtk_widget_show (hbox);
5174 impl->browse_path_bar_hbox = hbox;
5176 location_button_create (impl);
5177 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5181 impl->browse_path_bar = create_path_bar (impl);
5182 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5183 gtk_widget_show_all (impl->browse_path_bar);
5184 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5187 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5188 g_signal_connect (impl->browse_new_folder_button, "clicked",
5189 G_CALLBACK (new_folder_button_clicked), impl);
5190 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5192 /* Box for the location label and entry */
5194 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5195 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5197 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5198 gtk_widget_show (impl->location_label);
5199 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5202 hpaned = gtk_hpaned_new ();
5203 gtk_widget_show (hpaned);
5204 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5206 widget = shortcuts_pane_create (impl, size_group);
5207 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5208 widget = file_pane_create (impl, size_group);
5209 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5211 g_object_unref (size_group);
5217 gtk_file_chooser_default_constructor (GType type,
5218 guint n_construct_properties,
5219 GObjectConstructParam *construct_params)
5221 GtkFileChooserDefault *impl;
5224 profile_start ("start", NULL);
5226 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5227 n_construct_properties,
5229 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5231 g_assert (impl->file_system);
5233 gtk_widget_push_composite_child ();
5235 /* Shortcuts model */
5236 shortcuts_model_create (impl);
5238 /* The browse widgets */
5239 impl->browse_widgets = browse_widgets_create (impl);
5240 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5242 /* Alignment to hold extra widget */
5243 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5244 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5246 gtk_widget_pop_composite_child ();
5247 update_appearance (impl);
5249 profile_end ("end", NULL);
5254 /* Sets the extra_widget by packing it in the appropriate place */
5256 set_extra_widget (GtkFileChooserDefault *impl,
5257 GtkWidget *extra_widget)
5261 g_object_ref (extra_widget);
5262 /* FIXME: is this right ? */
5263 gtk_widget_show (extra_widget);
5266 if (impl->extra_widget)
5268 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5269 g_object_unref (impl->extra_widget);
5272 impl->extra_widget = extra_widget;
5273 if (impl->extra_widget)
5275 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5276 gtk_widget_show (impl->extra_align);
5279 gtk_widget_hide (impl->extra_align);
5283 set_local_only (GtkFileChooserDefault *impl,
5284 gboolean local_only)
5286 if (local_only != impl->local_only)
5288 impl->local_only = local_only;
5290 if (impl->shortcuts_model && impl->file_system)
5292 shortcuts_add_volumes (impl);
5293 shortcuts_add_bookmarks (impl);
5296 if (local_only && !g_file_is_native (impl->current_folder))
5298 /* If we are pointing to a non-local folder, make an effort to change
5299 * back to a local folder, but it's really up to the app to not cause
5300 * such a situation, so we ignore errors.
5302 const gchar *home = g_get_home_dir ();
5308 home_file = g_file_new_for_path (home);
5310 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5312 g_object_unref (home_file);
5318 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5319 GtkFileChooserDefault *impl)
5321 shortcuts_add_volumes (impl);
5322 shortcuts_add_bookmarks (impl);
5324 bookmarks_check_add_sensitivity (impl);
5325 bookmarks_check_remove_sensitivity (impl);
5326 shortcuts_check_popup_sensitivity (impl);
5329 /* Sets the file chooser to multiple selection mode */
5331 set_select_multiple (GtkFileChooserDefault *impl,
5332 gboolean select_multiple,
5333 gboolean property_notify)
5335 GtkTreeSelection *selection;
5336 GtkSelectionMode mode;
5338 if (select_multiple == impl->select_multiple)
5341 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5343 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5344 gtk_tree_selection_set_mode (selection, mode);
5346 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5348 impl->select_multiple = select_multiple;
5349 g_object_notify (G_OBJECT (impl), "select-multiple");
5351 check_preview_change (impl);
5355 set_file_system_backend (GtkFileChooserDefault *impl)
5357 profile_start ("start for backend", "default");
5359 impl->file_system = _gtk_file_system_new ();
5361 g_signal_connect (impl->file_system, "volumes-changed",
5362 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5363 g_signal_connect (impl->file_system, "bookmarks-changed",
5364 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5366 profile_end ("end", NULL);
5369 /* This function is basically a do_all function.
5371 * It sets the visibility on all the widgets based on the current state, and
5372 * moves the custom_widget if needed.
5375 update_appearance (GtkFileChooserDefault *impl)
5377 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5378 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5382 gtk_widget_hide (impl->location_button);
5383 save_widgets_create (impl);
5385 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5386 text = _("Save in _folder:");
5388 text = _("Create in _folder:");
5390 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5392 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5394 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5395 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5396 gtk_widget_show (impl->browse_widgets);
5400 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5401 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5402 gtk_widget_hide (impl->browse_widgets);
5405 gtk_widget_show (impl->browse_new_folder_button);
5407 if (impl->select_multiple)
5409 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5410 "Re-setting to single selection mode.");
5411 set_select_multiple (impl, FALSE, TRUE);
5414 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5415 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5417 gtk_widget_show (impl->location_button);
5418 save_widgets_destroy (impl);
5419 gtk_widget_show (impl->browse_widgets);
5420 location_mode_set (impl, impl->location_mode, TRUE);
5423 if (impl->location_entry)
5424 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5426 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5427 gtk_widget_hide (impl->browse_new_folder_button);
5429 gtk_widget_show (impl->browse_new_folder_button);
5431 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5432 * of files may change depending whether we are in a file or folder-only mode.
5434 gtk_widget_queue_draw (impl->browse_files_tree_view);
5436 emit_default_size_changed (impl);
5440 gtk_file_chooser_default_set_property (GObject *object,
5442 const GValue *value,
5446 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5450 case GTK_FILE_CHOOSER_PROP_ACTION:
5452 GtkFileChooserAction action = g_value_get_enum (value);
5454 if (action != impl->action)
5456 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5458 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5459 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5460 && impl->select_multiple)
5462 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5463 "this is not allowed in multiple selection mode. Resetting the file chooser "
5464 "to single selection mode.");
5465 set_select_multiple (impl, FALSE, TRUE);
5467 impl->action = action;
5468 update_appearance (impl);
5469 settings_load (impl);
5474 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5475 /* Ignore property */
5478 case GTK_FILE_CHOOSER_PROP_FILTER:
5479 set_current_filter (impl, g_value_get_object (value));
5482 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5483 set_local_only (impl, g_value_get_boolean (value));
5486 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5487 set_preview_widget (impl, g_value_get_object (value));
5490 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5491 impl->preview_widget_active = g_value_get_boolean (value);
5492 update_preview_widget_visibility (impl);
5495 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5496 impl->use_preview_label = g_value_get_boolean (value);
5497 update_preview_widget_visibility (impl);
5500 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5501 set_extra_widget (impl, g_value_get_object (value));
5504 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5506 gboolean select_multiple = g_value_get_boolean (value);
5507 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5508 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5511 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5512 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5513 "leaving the file chooser in single selection mode.");
5517 set_select_multiple (impl, select_multiple, FALSE);
5521 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5523 gboolean show_hidden = g_value_get_boolean (value);
5524 if (show_hidden != impl->show_hidden)
5526 impl->show_hidden = show_hidden;
5528 if (impl->browse_files_model)
5529 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5534 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5536 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5537 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5548 gtk_file_chooser_default_get_property (GObject *object,
5553 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5557 case GTK_FILE_CHOOSER_PROP_ACTION:
5558 g_value_set_enum (value, impl->action);
5561 case GTK_FILE_CHOOSER_PROP_FILTER:
5562 g_value_set_object (value, impl->current_filter);
5565 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5566 g_value_set_boolean (value, impl->local_only);
5569 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5570 g_value_set_object (value, impl->preview_widget);
5573 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5574 g_value_set_boolean (value, impl->preview_widget_active);
5577 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5578 g_value_set_boolean (value, impl->use_preview_label);
5581 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5582 g_value_set_object (value, impl->extra_widget);
5585 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5586 g_value_set_boolean (value, impl->select_multiple);
5589 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5590 g_value_set_boolean (value, impl->show_hidden);
5593 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5594 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5598 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5603 /* Removes the settings signal handler. It's safe to call multiple times */
5605 remove_settings_signal (GtkFileChooserDefault *impl,
5608 if (impl->settings_signal_id)
5610 GtkSettings *settings;
5612 settings = gtk_settings_get_for_screen (screen);
5613 g_signal_handler_disconnect (settings,
5614 impl->settings_signal_id);
5615 impl->settings_signal_id = 0;
5620 gtk_file_chooser_default_dispose (GObject *object)
5623 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5625 if (impl->extra_widget)
5627 g_object_unref (impl->extra_widget);
5628 impl->extra_widget = NULL;
5631 pending_select_files_free (impl);
5633 /* cancel all pending operations */
5634 if (impl->pending_cancellables)
5636 for (l = impl->pending_cancellables; l; l = l->next)
5638 GCancellable *cancellable = G_CANCELLABLE (l->data);
5639 g_cancellable_cancel (cancellable);
5641 g_slist_free (impl->pending_cancellables);
5642 impl->pending_cancellables = NULL;
5645 if (impl->reload_icon_cancellables)
5647 for (l = impl->reload_icon_cancellables; l; l = l->next)
5649 GCancellable *cancellable = G_CANCELLABLE (l->data);
5650 g_cancellable_cancel (cancellable);
5652 g_slist_free (impl->reload_icon_cancellables);
5653 impl->reload_icon_cancellables = NULL;
5656 if (impl->loading_shortcuts)
5658 for (l = impl->loading_shortcuts; l; l = l->next)
5660 GCancellable *cancellable = G_CANCELLABLE (l->data);
5661 g_cancellable_cancel (cancellable);
5663 g_slist_free (impl->loading_shortcuts);
5664 impl->loading_shortcuts = NULL;
5667 if (impl->file_list_drag_data_received_cancellable)
5669 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5670 impl->file_list_drag_data_received_cancellable = NULL;
5673 if (impl->update_current_folder_cancellable)
5675 g_cancellable_cancel (impl->update_current_folder_cancellable);
5676 impl->update_current_folder_cancellable = NULL;
5679 if (impl->show_and_select_files_cancellable)
5681 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5682 impl->show_and_select_files_cancellable = NULL;
5685 if (impl->should_respond_get_info_cancellable)
5687 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5688 impl->should_respond_get_info_cancellable = NULL;
5691 if (impl->update_from_entry_cancellable)
5693 g_cancellable_cancel (impl->update_from_entry_cancellable);
5694 impl->update_from_entry_cancellable = NULL;
5697 if (impl->shortcuts_activate_iter_cancellable)
5699 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5700 impl->shortcuts_activate_iter_cancellable = NULL;
5703 search_stop_searching (impl, TRUE);
5704 recent_stop_loading (impl);
5706 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5708 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5711 /* We override show-all since we have internal widgets that
5712 * shouldn't be shown when you call show_all(), like the filter
5716 gtk_file_chooser_default_show_all (GtkWidget *widget)
5718 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5720 gtk_widget_show (widget);
5722 if (impl->extra_widget)
5723 gtk_widget_show_all (impl->extra_widget);
5726 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5727 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5730 toplevel_set_focus_cb (GtkWindow *window,
5732 GtkFileChooserDefault *impl)
5734 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5737 /* We monitor the focus widget on our toplevel to be able to know which widget
5738 * was last focused at the time our "should_respond" method gets called.
5741 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5742 GtkWidget *previous_toplevel)
5744 GtkFileChooserDefault *impl;
5745 GtkWidget *toplevel;
5747 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5749 if (previous_toplevel)
5751 g_assert (impl->toplevel_set_focus_id != 0);
5752 g_signal_handler_disconnect (previous_toplevel,
5753 impl->toplevel_set_focus_id);
5754 impl->toplevel_set_focus_id = 0;
5755 impl->toplevel_last_focus_widget = NULL;
5758 g_assert (impl->toplevel_set_focus_id == 0);
5760 toplevel = gtk_widget_get_toplevel (widget);
5761 if (GTK_IS_WINDOW (toplevel))
5763 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5764 G_CALLBACK (toplevel_set_focus_cb), impl);
5765 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5769 /* Changes the icons wherever it is needed */
5771 change_icon_theme (GtkFileChooserDefault *impl)
5773 GtkSettings *settings;
5776 profile_start ("start", NULL);
5778 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5780 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5781 impl->icon_size = MAX (width, height);
5783 impl->icon_size = FALLBACK_ICON_SIZE;
5785 shortcuts_reload_icons (impl);
5786 gtk_widget_queue_resize (impl->browse_files_tree_view);
5788 profile_end ("end", NULL);
5791 /* Callback used when a GtkSettings value changes */
5793 settings_notify_cb (GObject *object,
5795 GtkFileChooserDefault *impl)
5799 profile_start ("start", NULL);
5801 name = g_param_spec_get_name (pspec);
5803 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5804 strcmp (name, "gtk-icon-sizes") == 0)
5805 change_icon_theme (impl);
5807 profile_end ("end", NULL);
5810 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5814 check_icon_theme (GtkFileChooserDefault *impl)
5816 GtkSettings *settings;
5818 profile_start ("start", NULL);
5820 if (impl->settings_signal_id)
5822 profile_end ("end", NULL);
5826 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5828 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5829 impl->settings_signal_id = g_signal_connect (settings, "notify",
5830 G_CALLBACK (settings_notify_cb), impl);
5832 change_icon_theme (impl);
5835 profile_end ("end", NULL);
5839 gtk_file_chooser_default_style_set (GtkWidget *widget,
5840 GtkStyle *previous_style)
5842 GtkFileChooserDefault *impl;
5844 profile_start ("start", NULL);
5846 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5848 profile_msg (" parent class style_set start", NULL);
5849 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5850 profile_msg (" parent class style_set end", NULL);
5852 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5853 change_icon_theme (impl);
5855 emit_default_size_changed (impl);
5857 profile_end ("end", NULL);
5861 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5862 GdkScreen *previous_screen)
5864 GtkFileChooserDefault *impl;
5866 profile_start ("start", NULL);
5868 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5870 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5871 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5873 remove_settings_signal (impl, previous_screen);
5874 check_icon_theme (impl);
5876 emit_default_size_changed (impl);
5878 profile_end ("end", NULL);
5882 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5883 GtkAllocation *allocation)
5885 GtkFileChooserDefault *impl;
5887 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5889 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5893 get_is_file_filtered (GtkFileChooserDefault *impl,
5895 GFileInfo *file_info)
5897 GtkFileFilterInfo filter_info;
5898 GtkFileFilterFlags needed;
5901 if (!impl->current_filter)
5904 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5906 needed = gtk_file_filter_get_needed (impl->current_filter);
5908 filter_info.display_name = g_file_info_get_display_name (file_info);
5909 filter_info.mime_type = g_file_info_get_content_type (file_info);
5911 if (needed & GTK_FILE_FILTER_FILENAME)
5913 filter_info.filename = g_file_get_path (file);
5914 if (filter_info.filename)
5915 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5918 filter_info.filename = NULL;
5920 if (needed & GTK_FILE_FILTER_URI)
5922 filter_info.uri = g_file_get_uri (file);
5923 if (filter_info.uri)
5924 filter_info.contains |= GTK_FILE_FILTER_URI;
5927 filter_info.uri = NULL;
5929 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5931 if (filter_info.filename)
5932 g_free ((gchar *)filter_info.filename);
5933 if (filter_info.uri)
5934 g_free ((gchar *)filter_info.uri);
5940 settings_load (GtkFileChooserDefault *impl)
5942 GtkFileChooserSettings *settings;
5943 LocationMode location_mode;
5944 gboolean show_hidden;
5945 gboolean expand_folders;
5947 settings = _gtk_file_chooser_settings_new ();
5949 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5950 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5951 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5953 g_object_unref (settings);
5955 location_mode_set (impl, location_mode, TRUE);
5956 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5957 impl->expand_folders = expand_folders;
5958 if (impl->save_expander)
5959 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5963 settings_save (GtkFileChooserDefault *impl)
5965 GtkFileChooserSettings *settings;
5967 settings = _gtk_file_chooser_settings_new ();
5969 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5970 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5971 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5974 _gtk_file_chooser_settings_save (settings, NULL);
5976 g_object_unref (settings);
5979 /* GtkWidget::map method */
5981 gtk_file_chooser_default_map (GtkWidget *widget)
5983 GtkFileChooserDefault *impl;
5984 char *current_working_dir;
5986 profile_start ("start", NULL);
5988 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5990 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5992 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5994 switch (impl->reload_state)
5997 /* The user didn't explicitly give us a folder to
5998 * display, so we'll use the cwd
6000 current_working_dir = g_get_current_dir ();
6001 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6002 current_working_dir);
6003 g_free (current_working_dir);
6006 case RELOAD_HAS_FOLDER:
6007 /* Nothing; we are already loading or loaded, so we
6008 * don't need to reload
6012 case RELOAD_WAS_UNMAPPED:
6013 /* Just reload the current folder; else continue
6016 if (impl->current_folder)
6018 pending_select_files_store_selection (impl);
6019 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6024 g_assert_not_reached ();
6028 volumes_bookmarks_changed_cb (impl->file_system, impl);
6030 settings_load (impl);
6032 emit_default_size_changed (impl);
6034 profile_end ("end", NULL);
6037 /* GtkWidget::unmap method */
6039 gtk_file_chooser_default_unmap (GtkWidget *widget)
6041 GtkFileChooserDefault *impl;
6043 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6045 settings_save (impl);
6047 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6049 impl->reload_state = RELOAD_WAS_UNMAPPED;
6053 list_model_filter_func (GtkFileSystemModel *model,
6055 GFileInfo *file_info,
6058 GtkFileChooserDefault *impl = user_data;
6060 if (!impl->current_filter)
6063 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6066 return !get_is_file_filtered (impl, file, file_info);
6070 install_list_model_filter (GtkFileChooserDefault *impl)
6072 GtkFileSystemModelFilter filter;
6075 g_assert (impl->browse_files_model != NULL);
6077 if (impl->current_filter)
6079 filter = list_model_filter_func;
6088 _gtk_file_system_model_set_filter (impl->browse_files_model,
6093 #define COMPARE_DIRECTORIES \
6094 GtkFileChooserDefault *impl = user_data; \
6095 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6096 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6097 gboolean dir_a, dir_b; \
6100 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6102 return impl->list_sort_ascending ? -1 : 1; \
6105 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6107 return impl->list_sort_ascending ? 1 : -1; \
6109 if (dir_a != dir_b) \
6110 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6112 /* Sort callback for the filename column */
6114 name_sort_func (GtkTreeModel *model,
6119 COMPARE_DIRECTORIES;
6122 gchar *key_a, *key_b;
6125 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6126 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6127 result = strcmp (key_a, key_b);
6136 /* Sort callback for the size column */
6138 size_sort_func (GtkTreeModel *model,
6143 COMPARE_DIRECTORIES;
6146 goffset size_a = g_file_info_get_size (info_a);
6147 goffset size_b = g_file_info_get_size (info_b);
6149 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6153 /* Sort callback for the mtime column */
6155 mtime_sort_func (GtkTreeModel *model,
6160 COMPARE_DIRECTORIES;
6165 g_file_info_get_modification_time (info_a, &ta);
6166 g_file_info_get_modification_time (info_b, &tb);
6168 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6172 /* Callback used when the sort column changes. We cache the sort order for use
6173 * in name_sort_func().
6176 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6177 GtkFileChooserDefault *impl)
6179 GtkSortType sort_type;
6181 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6182 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6186 set_busy_cursor (GtkFileChooserDefault *impl,
6189 GtkWindow *toplevel;
6190 GdkDisplay *display;
6193 toplevel = get_toplevel (GTK_WIDGET (impl));
6194 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6197 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6200 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6204 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6205 gdk_display_flush (display);
6208 gdk_cursor_unref (cursor);
6211 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6213 load_set_model (GtkFileChooserDefault *impl)
6215 profile_start ("start", NULL);
6217 g_assert (impl->browse_files_model != NULL);
6218 g_assert (impl->sort_model == NULL);
6220 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6221 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6222 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6223 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6224 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6225 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6226 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6227 impl->list_sort_ascending = TRUE;
6228 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6230 g_signal_connect (impl->sort_model, "sort-column-changed",
6231 G_CALLBACK (list_sort_column_changed_cb), impl);
6233 profile_msg (" gtk_tree_view_set_model start", NULL);
6234 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6235 GTK_TREE_MODEL (impl->sort_model));
6236 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6237 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6238 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6239 profile_msg (" gtk_tree_view_set_model end", NULL);
6241 profile_end ("end", NULL);
6244 /* Timeout callback used when the loading timer expires */
6246 load_timeout_cb (gpointer data)
6248 GtkFileChooserDefault *impl;
6250 profile_start ("start", NULL);
6252 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6253 g_assert (impl->load_state == LOAD_PRELOAD);
6254 g_assert (impl->load_timeout_id != 0);
6255 g_assert (impl->browse_files_model != NULL);
6257 impl->load_timeout_id = 0;
6258 impl->load_state = LOAD_LOADING;
6260 load_set_model (impl);
6262 profile_end ("end", NULL);
6267 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6269 load_setup_timer (GtkFileChooserDefault *impl)
6271 g_assert (impl->load_timeout_id == 0);
6272 g_assert (impl->load_state != LOAD_PRELOAD);
6274 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6275 impl->load_state = LOAD_PRELOAD;
6278 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6280 load_remove_timer (GtkFileChooserDefault *impl)
6282 if (impl->load_timeout_id != 0)
6284 g_assert (impl->load_state == LOAD_PRELOAD);
6286 g_source_remove (impl->load_timeout_id);
6287 impl->load_timeout_id = 0;
6288 impl->load_state = LOAD_EMPTY;
6291 g_assert (impl->load_state == LOAD_EMPTY ||
6292 impl->load_state == LOAD_LOADING ||
6293 impl->load_state == LOAD_FINISHED);
6296 /* Selects the first row in the file list */
6298 browse_files_select_first_row (GtkFileChooserDefault *impl)
6301 GtkTreeIter dummy_iter;
6302 GtkTreeModel *tree_model;
6304 if (!impl->sort_model)
6307 path = gtk_tree_path_new_from_indices (0, -1);
6308 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6310 /* If the list is empty, do nothing. */
6311 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6312 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6314 gtk_tree_path_free (path);
6317 struct center_selected_row_closure {
6318 GtkFileChooserDefault *impl;
6319 gboolean already_centered;
6322 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6323 * selected row in the tree view.
6326 center_selected_row_foreach_cb (GtkTreeModel *model,
6331 struct center_selected_row_closure *closure;
6334 if (closure->already_centered)
6337 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6338 closure->already_centered = TRUE;
6341 /* Centers the selected row in the tree view */
6343 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6345 struct center_selected_row_closure closure;
6346 GtkTreeSelection *selection;
6348 closure.impl = impl;
6349 closure.already_centered = FALSE;
6351 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6352 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6355 struct ShowAndSelectPathsData
6357 GtkFileChooserDefault *impl;
6362 show_and_select_files_finished_loading (GtkFolder *folder,
6365 gboolean have_hidden;
6366 gboolean have_filtered;
6368 struct ShowAndSelectPathsData *data = user_data;
6370 have_hidden = FALSE;
6371 have_filtered = FALSE;
6373 for (l = data->files; l; l = l->next)
6380 info = _gtk_folder_get_info (folder, file);
6384 have_hidden = g_file_info_get_is_hidden (info);
6387 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6388 get_is_file_filtered (data->impl, file, info);
6390 g_object_unref (info);
6392 if (have_hidden && have_filtered)
6393 break; /* we now have all the information we need */
6397 g_signal_handlers_disconnect_by_func (folder,
6398 show_and_select_files_finished_loading,
6402 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6405 set_current_filter (data->impl, NULL);
6407 for (l = data->files; l; l = l->next)
6412 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6413 select_func, data->impl);
6416 browse_files_center_selected_row (data->impl);
6418 g_object_unref (data->impl);
6419 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6420 g_slist_free (data->files);
6425 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6427 const GError *error,
6430 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6431 struct ShowAndSelectPathsData *data = user_data;
6433 if (data->impl->show_and_select_files_cancellable != cancellable)
6436 data->impl->show_and_select_files_cancellable = NULL;
6438 if (cancelled || error)
6441 g_object_unref (cancellable);
6443 if (_gtk_folder_is_finished_loading (folder))
6444 show_and_select_files_finished_loading (folder, user_data);
6446 g_signal_connect (folder, "finished-loading",
6447 G_CALLBACK (show_and_select_files_finished_loading),
6453 g_object_unref (data->impl);
6454 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6455 g_slist_free (data->files);
6458 g_object_unref (cancellable);
6462 show_and_select_files (GtkFileChooserDefault *impl,
6467 struct ShowAndSelectPathsData *info;
6469 profile_start ("start", NULL);
6473 profile_end ("end", NULL);
6477 info = g_new (struct ShowAndSelectPathsData, 1);
6478 info->impl = g_object_ref (impl);
6479 info->files = g_slist_copy (files);
6480 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6482 if (impl->show_and_select_files_cancellable)
6483 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6485 impl->show_and_select_files_cancellable =
6486 _gtk_file_system_get_folder (impl->file_system, parent_file,
6487 "standard::is-hidden,standard::type,standard::name",
6488 show_and_select_files_get_folder_cb, info);
6490 profile_end ("end", NULL);
6494 /* Processes the pending operation when a folder is finished loading */
6496 pending_select_files_process (GtkFileChooserDefault *impl)
6498 g_assert (impl->load_state == LOAD_FINISHED);
6499 g_assert (impl->browse_files_model != NULL);
6500 g_assert (impl->sort_model != NULL);
6502 if (impl->pending_select_files)
6505 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6506 pending_select_files_free (impl);
6507 browse_files_center_selected_row (impl);
6511 /* We only select the first row if the chooser is actually mapped ---
6512 * selecting the first row is to help the user when he is interacting with
6513 * the chooser, but sometimes a chooser works not on behalf of the user,
6514 * but rather on behalf of something else like GtkFileChooserButton. In
6515 * that case, the chooser's selection should be what the caller expects,
6516 * as the user can't see that something else got selected. See bug #165264.
6518 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6519 browse_files_select_first_row (impl);
6522 g_assert (impl->pending_select_files == NULL);
6525 /* Callback used when the file system model finishes loading */
6527 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6528 GtkFileChooserDefault *impl)
6530 profile_start ("start", NULL);
6532 if (impl->load_state == LOAD_PRELOAD)
6534 load_remove_timer (impl);
6535 load_set_model (impl);
6537 else if (impl->load_state == LOAD_LOADING)
6543 /* We can't g_assert_not_reached(), as something other than us may have
6544 * initiated a folder reload. See #165556.
6546 profile_end ("end", NULL);
6550 g_assert (impl->load_timeout_id == 0);
6552 impl->load_state = LOAD_FINISHED;
6554 pending_select_files_process (impl);
6555 set_busy_cursor (impl, FALSE);
6556 #ifdef PROFILE_FILE_CHOOSER
6557 access ("MARK: *** FINISHED LOADING", F_OK);
6560 profile_end ("end", NULL);
6564 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6566 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6568 if (impl->browse_files_model)
6570 g_object_unref (impl->browse_files_model);
6571 impl->browse_files_model = NULL;
6574 if (impl->sort_model)
6576 g_object_unref (impl->sort_model);
6577 impl->sort_model = NULL;
6580 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6583 /* Gets rid of the old list model and creates a new one for the current folder */
6585 set_list_model (GtkFileChooserDefault *impl,
6588 g_assert (impl->current_folder != NULL);
6590 profile_start ("start", NULL);
6592 stop_loading_and_clear_list_model (impl);
6594 set_busy_cursor (impl, TRUE);
6595 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6597 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6598 impl->current_folder, 0,
6599 "standard,time,thumbnail::*",
6601 if (!impl->browse_files_model)
6603 set_busy_cursor (impl, FALSE);
6604 profile_end ("end", NULL);
6608 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6610 g_signal_connect (impl->browse_files_model, "finished-loading",
6611 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6613 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6615 install_list_model_filter (impl);
6617 profile_end ("end", NULL);
6622 struct update_chooser_entry_selected_foreach_closure {
6624 GtkTreeIter first_selected_iter;
6628 compare_utf8_filenames (const gchar *a,
6631 gchar *a_folded, *b_folded;
6634 a_folded = g_utf8_strdown (a, -1);
6635 b_folded = g_utf8_strdown (b, -1);
6637 retval = strcmp (a_folded, b_folded);
6646 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6651 struct update_chooser_entry_selected_foreach_closure *closure;
6654 closure->num_selected++;
6656 if (closure->num_selected == 1)
6657 closure->first_selected_iter = *iter;
6661 update_chooser_entry (GtkFileChooserDefault *impl)
6663 GtkTreeSelection *selection;
6664 struct update_chooser_entry_selected_foreach_closure closure;
6665 const char *file_part;
6667 /* no need to update the file chooser's entry if there's no entry */
6668 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6669 impl->operation_mode == OPERATION_MODE_RECENT ||
6670 !impl->location_entry)
6673 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6674 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6675 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6676 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6677 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6680 g_assert (impl->location_entry != NULL);
6682 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6683 closure.num_selected = 0;
6684 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6688 if (closure.num_selected == 0)
6690 goto maybe_clear_entry;
6692 else if (closure.num_selected == 1)
6694 GtkTreeIter child_iter;
6696 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6699 gboolean change_entry;
6701 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6703 &closure.first_selected_iter);
6705 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6707 /* If the cursor moved to the row of the newly created folder,
6708 * retrieving info will return NULL.
6713 g_free (impl->browse_files_last_selected_name);
6714 impl->browse_files_last_selected_name =
6715 g_strdup (g_file_info_get_display_name (info));
6717 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6718 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6719 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6721 /* We don't want the name to change when clicking on a folder... */
6722 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6725 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6729 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6731 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6732 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6740 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6741 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6743 /* Multiple selection, so just clear the entry. */
6745 g_free (impl->browse_files_last_selected_name);
6746 impl->browse_files_last_selected_name = NULL;
6748 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6754 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6755 && impl->browse_files_last_selected_name)
6757 const char *entry_text;
6759 gboolean clear_entry;
6761 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6762 len = strlen (entry_text);
6765 /* The file chooser entry may have appended a "/" to its text. So
6766 * take it out, and compare the result to the old selection.
6768 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6772 tmp = g_strndup (entry_text, len - 1);
6773 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6777 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6780 clear_entry = FALSE;
6783 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6788 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6792 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6796 struct UpdateCurrentFolderData
6798 GtkFileChooserDefault *impl;
6800 gboolean keep_trail;
6801 gboolean clear_entry;
6802 GFile *original_file;
6803 GError *original_error;
6807 update_current_folder_get_info_cb (GCancellable *cancellable,
6809 const GError *error,
6812 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6813 struct UpdateCurrentFolderData *data = user_data;
6814 GtkFileChooserDefault *impl = data->impl;
6816 if (cancellable != impl->update_current_folder_cancellable)
6819 impl->update_current_folder_cancellable = NULL;
6820 impl->reload_state = RELOAD_EMPTY;
6822 set_busy_cursor (impl, FALSE);
6831 if (!data->original_file)
6833 data->original_file = g_object_ref (data->file);
6834 data->original_error = g_error_copy (error);
6837 parent_file = g_file_get_parent (data->file);
6839 /* get parent path and try to change the folder to that */
6842 g_object_unref (data->file);
6843 data->file = parent_file;
6845 g_object_unref (cancellable);
6847 /* restart the update current folder operation */
6848 impl->reload_state = RELOAD_HAS_FOLDER;
6850 impl->update_current_folder_cancellable =
6851 _gtk_file_system_get_info (impl->file_system, data->file,
6853 update_current_folder_get_info_cb,
6856 set_busy_cursor (impl, TRUE);
6862 /* error and bail out */
6863 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6864 g_object_unref (data->original_file);
6870 if (data->original_file)
6872 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6874 g_object_unref (data->original_file);
6877 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6880 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6883 if (impl->current_folder != data->file)
6885 if (impl->current_folder)
6886 g_object_unref (impl->current_folder);
6888 impl->current_folder = g_object_ref (data->file);
6890 impl->reload_state = RELOAD_HAS_FOLDER;
6893 /* Update the widgets that may trigger a folder change themselves. */
6895 if (!impl->changing_folder)
6897 impl->changing_folder = TRUE;
6899 shortcuts_update_current_folder (impl);
6901 impl->changing_folder = FALSE;
6904 /* Set the folder on the save entry */
6906 if (impl->location_entry)
6908 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6909 impl->current_folder);
6911 if (data->clear_entry)
6912 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6915 /* Create a new list model. This is slightly evil; we store the result value
6916 * but perform more actions rather than returning immediately even if it
6917 * generates an error.
6919 set_list_model (impl, NULL);
6921 /* Refresh controls */
6923 shortcuts_find_current_folder (impl);
6925 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6927 check_preview_change (impl);
6928 bookmarks_check_add_sensitivity (impl);
6930 g_signal_emit_by_name (impl, "selection-changed", 0);
6933 g_object_unref (data->file);
6936 g_object_unref (cancellable);
6940 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6942 gboolean keep_trail,
6943 gboolean clear_entry,
6946 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6947 struct UpdateCurrentFolderData *data;
6949 profile_start ("start", NULL);
6951 g_object_ref (file);
6953 switch (impl->operation_mode)
6955 case OPERATION_MODE_SEARCH:
6956 search_switch_to_browse_mode (impl);
6958 case OPERATION_MODE_RECENT:
6959 recent_switch_to_browse_mode (impl);
6961 case OPERATION_MODE_BROWSE:
6965 if (impl->local_only && !g_file_is_native (file))
6967 g_set_error_literal (error,
6968 GTK_FILE_CHOOSER_ERROR,
6969 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6970 _("Cannot change to folder because it is not local"));
6972 g_object_unref (file);
6973 profile_end ("end - not local", NULL);
6977 if (impl->update_current_folder_cancellable)
6978 g_cancellable_cancel (impl->update_current_folder_cancellable);
6980 /* Test validity of path here. */
6981 data = g_new0 (struct UpdateCurrentFolderData, 1);
6983 data->file = g_object_ref (file);
6984 data->keep_trail = keep_trail;
6985 data->clear_entry = clear_entry;
6987 impl->reload_state = RELOAD_HAS_FOLDER;
6989 impl->update_current_folder_cancellable =
6990 _gtk_file_system_get_info (impl->file_system, file,
6992 update_current_folder_get_info_cb,
6995 set_busy_cursor (impl, TRUE);
6996 g_object_unref (file);
6998 profile_end ("end", NULL);
7003 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7005 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7007 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7008 impl->operation_mode == OPERATION_MODE_RECENT)
7011 if (impl->reload_state == RELOAD_EMPTY)
7013 char *current_working_dir;
7016 /* We are unmapped, or we had an error while loading the last folder. We'll return
7017 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7018 * explicitly calls set_current_folder() on us.
7020 current_working_dir = g_get_current_dir ();
7021 file = g_file_new_for_path (current_working_dir);
7022 g_free (current_working_dir);
7026 if (impl->current_folder)
7027 return g_object_ref (impl->current_folder);
7033 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7036 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7038 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7039 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7041 pending_select_files_free (impl);
7042 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7046 select_func (GtkFileSystemModel *model,
7051 GtkFileChooserDefault *impl = user_data;
7052 GtkTreeSelection *selection;
7053 GtkTreeIter sorted_iter;
7055 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7057 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7058 gtk_tree_selection_select_iter (selection, &sorted_iter);
7062 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7066 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7070 parent_file = g_file_get_parent (file);
7073 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7075 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7076 impl->operation_mode == OPERATION_MODE_RECENT ||
7077 impl->load_state == LOAD_EMPTY)
7083 g_assert (impl->current_folder != NULL);
7085 same_path = g_file_equal (parent_file, impl->current_folder);
7088 if (same_path && impl->load_state == LOAD_FINISHED)
7093 files.data = (gpointer) file;
7096 result = show_and_select_files (impl, parent_file, &files, error);
7097 g_object_unref (parent_file);
7101 pending_select_files_add (impl, file);
7107 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7108 g_object_unref (parent_file);
7112 g_object_unref (parent_file);
7117 unselect_func (GtkFileSystemModel *model,
7122 GtkFileChooserDefault *impl = user_data;
7123 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7124 GtkTreePath *sorted_path;
7126 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7128 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7130 gtk_tree_path_free (sorted_path);
7134 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7137 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7139 if (!impl->browse_files_model)
7142 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7143 unselect_func, impl);
7147 maybe_select (GtkTreeModel *model,
7152 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7153 GtkTreeSelection *selection;
7157 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7159 info = get_list_file_info (impl, iter);
7160 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7162 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7163 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7164 gtk_tree_selection_select_iter (selection, iter);
7166 gtk_tree_selection_unselect_iter (selection, iter);
7172 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7174 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7176 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7177 impl->operation_mode == OPERATION_MODE_RECENT)
7179 GtkTreeSelection *selection;
7181 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7182 gtk_tree_selection_select_all (selection);
7186 if (impl->select_multiple)
7187 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7188 maybe_select, impl);
7192 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7194 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7195 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7197 gtk_tree_selection_unselect_all (selection);
7198 pending_select_files_free (impl);
7201 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7203 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7205 * is_empty_ret - whether the file entry is totally empty
7207 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7208 * the path will be "$cwd/foobar")
7211 check_save_entry (GtkFileChooserDefault *impl,
7213 gboolean *is_well_formed_ret,
7214 gboolean *is_empty_ret,
7215 gboolean *is_file_part_empty_ret,
7216 gboolean *is_folder)
7218 GtkFileChooserEntry *chooser_entry;
7219 GFile *current_folder;
7220 const char *file_part;
7224 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7225 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7226 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7227 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7228 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7230 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7232 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7235 *is_well_formed_ret = TRUE;
7236 *is_empty_ret = TRUE;
7237 *is_file_part_empty_ret = TRUE;
7243 *is_empty_ret = FALSE;
7245 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7246 if (!current_folder)
7249 *is_well_formed_ret = FALSE;
7250 *is_file_part_empty_ret = FALSE;
7256 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7258 if (!file_part || file_part[0] == '\0')
7260 *file_ret = g_object_ref (current_folder);
7261 *is_well_formed_ret = TRUE;
7262 *is_file_part_empty_ret = TRUE;
7268 *is_file_part_empty_ret = FALSE;
7271 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7275 error_building_filename_dialog (impl, error);
7277 *is_well_formed_ret = FALSE;
7284 *is_well_formed_ret = TRUE;
7285 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7288 struct get_files_closure {
7289 GtkFileChooserDefault *impl;
7291 GFile *file_from_entry;
7295 get_files_foreach (GtkTreeModel *model,
7300 struct get_files_closure *info;
7302 GtkFileSystemModel *fs_model;
7303 GtkTreeIter sel_iter;
7306 fs_model = info->impl->browse_files_model;
7307 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7309 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7311 return; /* We are on the editable row */
7313 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7314 info->result = g_slist_prepend (info->result, g_object_ref (file));
7318 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7320 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7321 struct get_files_closure info;
7322 GtkWindow *toplevel;
7323 GtkWidget *current_focus;
7324 gboolean file_list_seen;
7326 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7327 return search_get_selected_files (impl);
7329 if (impl->operation_mode == OPERATION_MODE_RECENT)
7330 return recent_get_selected_files (impl);
7334 info.file_from_entry = NULL;
7336 toplevel = get_toplevel (GTK_WIDGET (impl));
7338 current_focus = gtk_window_get_focus (toplevel);
7340 current_focus = NULL;
7342 file_list_seen = FALSE;
7343 if (current_focus == impl->browse_files_tree_view)
7345 GtkTreeSelection *selection;
7349 file_list_seen = TRUE;
7350 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7351 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7353 /* If there is no selection in the file list, we probably have this situation:
7355 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7356 * 2. He then double-clicked on a folder ("bar") in the file list
7358 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7359 * filename entry to see if that is the case.
7361 if (info.result == NULL && impl->location_entry)
7364 else if (impl->location_entry && current_focus == impl->location_entry)
7366 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7370 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7375 if (!is_well_formed)
7378 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7380 g_object_unref (info.file_from_entry);
7384 if (info.file_from_entry)
7385 info.result = g_slist_prepend (info.result, info.file_from_entry);
7386 else if (!file_list_seen)
7391 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7393 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7397 /* The focus is on a dialog's action area button or something else */
7398 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7399 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7407 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7408 * fall back to the current directory */
7409 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7410 info.result == NULL)
7412 GFile *current_folder;
7414 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7417 info.result = g_slist_prepend (info.result, current_folder);
7420 return g_slist_reverse (info.result);
7424 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7426 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7428 if (impl->preview_file)
7429 return g_object_ref (impl->preview_file);
7434 static GtkFileSystem *
7435 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7437 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7439 return impl->file_system;
7442 /* Shows or hides the filter widgets */
7444 show_filters (GtkFileChooserDefault *impl,
7448 gtk_widget_show (impl->filter_combo_hbox);
7450 gtk_widget_hide (impl->filter_combo_hbox);
7454 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7455 GtkFileFilter *filter)
7457 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7460 if (g_slist_find (impl->filters, filter))
7462 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7466 g_object_ref_sink (filter);
7467 impl->filters = g_slist_append (impl->filters, filter);
7469 name = gtk_file_filter_get_name (filter);
7471 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7473 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7475 if (!g_slist_find (impl->filters, impl->current_filter))
7476 set_current_filter (impl, filter);
7478 show_filters (impl, TRUE);
7482 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7483 GtkFileFilter *filter)
7485 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7486 GtkTreeModel *model;
7490 filter_index = g_slist_index (impl->filters, filter);
7492 if (filter_index < 0)
7494 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7498 impl->filters = g_slist_remove (impl->filters, filter);
7500 if (filter == impl->current_filter)
7503 set_current_filter (impl, impl->filters->data);
7505 set_current_filter (impl, NULL);
7508 /* Remove row from the combo box */
7509 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7510 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7511 g_assert_not_reached ();
7513 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7515 g_object_unref (filter);
7518 show_filters (impl, FALSE);
7522 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7524 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7526 return g_slist_copy (impl->filters);
7529 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7531 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7534 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7537 struct AddShortcutData
7539 GtkFileChooserDefault *impl;
7544 add_shortcut_get_info_cb (GCancellable *cancellable,
7546 const GError *error,
7550 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7551 struct AddShortcutData *data = user_data;
7553 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7556 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7558 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7561 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7563 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7566 g_object_unref (data->impl);
7567 g_object_unref (data->file);
7570 g_object_unref (cancellable);
7574 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7578 GCancellable *cancellable;
7579 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7580 struct AddShortcutData *data;
7584 /* Avoid adding duplicates */
7585 pos = shortcut_find_position (impl, file);
7586 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7590 uri = g_file_get_uri (file);
7591 /* translators, "Shortcut" means "Bookmark" here */
7593 GTK_FILE_CHOOSER_ERROR,
7594 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7595 _("Shortcut %s already exists"),
7602 for (l = impl->loading_shortcuts; l; l = l->next)
7604 GCancellable *c = l->data;
7607 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7608 if (f && g_file_equal (file, f))
7612 uri = g_file_get_uri (file);
7614 GTK_FILE_CHOOSER_ERROR,
7615 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7616 _("Shortcut %s already exists"),
7624 data = g_new0 (struct AddShortcutData, 1);
7625 data->impl = g_object_ref (impl);
7626 data->file = g_object_ref (file);
7628 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7630 add_shortcut_get_info_cb, data);
7635 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7636 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7642 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7646 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7653 for (l = impl->loading_shortcuts; l; l = l->next)
7655 GCancellable *c = l->data;
7658 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7659 if (f && g_file_equal (file, f))
7661 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7662 g_cancellable_cancel (c);
7667 if (impl->num_shortcuts == 0)
7670 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7671 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7672 g_assert_not_reached ();
7674 for (i = 0; i < impl->num_shortcuts; i++)
7677 ShortcutType shortcut_type;
7680 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7681 SHORTCUTS_COL_DATA, &col_data,
7682 SHORTCUTS_COL_TYPE, &shortcut_type,
7684 g_assert (col_data != NULL);
7685 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7687 shortcut = col_data;
7688 if (g_file_equal (shortcut, file))
7690 shortcuts_remove_rows (impl, pos + i, 1);
7691 impl->num_shortcuts--;
7695 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7696 g_assert_not_reached ();
7701 uri = g_file_get_uri (file);
7702 /* translators, "Shortcut" means "Bookmark" here */
7704 GTK_FILE_CHOOSER_ERROR,
7705 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7706 _("Shortcut %s does not exist"),
7714 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7716 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7722 if (impl->num_shortcuts == 0)
7725 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7726 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7727 g_assert_not_reached ();
7731 for (i = 0; i < impl->num_shortcuts; i++)
7734 ShortcutType shortcut_type;
7737 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7738 SHORTCUTS_COL_DATA, &col_data,
7739 SHORTCUTS_COL_TYPE, &shortcut_type,
7741 g_assert (col_data != NULL);
7742 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7744 shortcut = col_data;
7745 list = g_slist_prepend (list, g_object_ref (shortcut));
7747 if (i != impl->num_shortcuts - 1)
7749 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7750 g_assert_not_reached ();
7754 return g_slist_reverse (list);
7757 /* Guesses a size based upon font sizes */
7759 find_good_size_from_style (GtkWidget *widget,
7763 GtkFileChooserDefault *impl;
7768 g_assert (widget->style != NULL);
7769 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7771 screen = gtk_widget_get_screen (widget);
7774 resolution = gdk_screen_get_resolution (screen);
7775 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7779 resolution = 96.0; /* wheeee */
7781 font_size = pango_font_description_get_size (widget->style->font_desc);
7782 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7784 *width = font_size * NUM_CHARS;
7785 *height = font_size * NUM_LINES;
7789 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7790 gint *default_width,
7791 gint *default_height)
7793 GtkFileChooserDefault *impl;
7796 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7798 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7799 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7800 || impl->expand_folders)
7802 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7804 if (impl->preview_widget_active &&
7805 impl->preview_widget &&
7806 GTK_WIDGET_VISIBLE (impl->preview_widget))
7808 gtk_widget_size_request (impl->preview_box, &req);
7809 *default_width += PREVIEW_HBOX_SPACING + req.width;
7812 if (impl->extra_widget &&
7813 GTK_WIDGET_VISIBLE (impl->extra_widget))
7815 gtk_widget_size_request (impl->extra_align, &req);
7816 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7821 gtk_widget_size_request (GTK_WIDGET (impl), &req);
7822 *default_width = req.width;
7823 *default_height = req.height;
7827 struct switch_folder_closure {
7828 GtkFileChooserDefault *impl;
7833 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7835 switch_folder_foreach_cb (GtkTreeModel *model,
7840 struct switch_folder_closure *closure;
7841 GtkTreeIter child_iter;
7845 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7847 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7848 closure->num_selected++;
7851 /* Changes to the selected folder in the list view */
7853 switch_to_selected_folder (GtkFileChooserDefault *impl)
7855 GtkTreeSelection *selection;
7856 struct switch_folder_closure closure;
7858 /* We do this with foreach() rather than get_selected() as we may be in
7859 * multiple selection mode
7862 closure.impl = impl;
7863 closure.file = NULL;
7864 closure.num_selected = 0;
7866 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7867 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7869 g_assert (closure.file && closure.num_selected == 1);
7871 change_folder_and_display_error (impl, closure.file, FALSE);
7874 /* Gets the GFileInfo for the selected row in the file list; assumes single
7878 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7879 gboolean *had_selection)
7881 GtkTreeSelection *selection;
7882 GtkTreeIter iter, child_iter;
7885 g_assert (!impl->select_multiple);
7886 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7887 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7889 *had_selection = FALSE;
7893 *had_selection = TRUE;
7895 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7899 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7903 /* Gets the display name of the selected file in the file list; assumes single
7904 * selection mode and that something is selected.
7906 static const gchar *
7907 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7910 gboolean had_selection;
7912 info = get_selected_file_info_from_file_list (impl, &had_selection);
7913 g_assert (had_selection);
7914 g_assert (info != NULL);
7916 return g_file_info_get_display_name (info);
7920 add_custom_button_to_dialog (GtkDialog *dialog,
7921 const gchar *mnemonic_label,
7922 const gchar *stock_id,
7927 button = gtk_button_new_with_mnemonic (mnemonic_label);
7928 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7929 gtk_button_set_image (GTK_BUTTON (button),
7930 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7931 gtk_widget_show (button);
7933 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7936 /* Presents an overwrite confirmation dialog; returns whether we should accept
7940 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7941 const gchar *file_part,
7942 const gchar *folder_display_name)
7944 GtkWindow *toplevel;
7948 toplevel = get_toplevel (GTK_WIDGET (impl));
7950 dialog = gtk_message_dialog_new (toplevel,
7951 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7952 GTK_MESSAGE_QUESTION,
7954 _("A file named \"%s\" already exists. Do you want to replace it?"),
7956 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7957 _("The file already exists in \"%s\". Replacing it will "
7958 "overwrite its contents."),
7959 folder_display_name);
7961 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7962 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7963 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7964 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7965 GTK_RESPONSE_ACCEPT,
7966 GTK_RESPONSE_CANCEL,
7968 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7970 if (toplevel->group)
7971 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7973 response = gtk_dialog_run (GTK_DIALOG (dialog));
7975 gtk_widget_destroy (dialog);
7977 return (response == GTK_RESPONSE_ACCEPT);
7980 struct GetDisplayNameData
7982 GtkFileChooserDefault *impl;
7987 confirmation_confirm_get_info_cb (GCancellable *cancellable,
7989 const GError *error,
7992 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7993 gboolean should_respond = FALSE;
7994 struct GetDisplayNameData *data = user_data;
7996 if (cancellable != data->impl->should_respond_get_info_cancellable)
7999 data->impl->should_respond_get_info_cancellable = NULL;
8005 /* Huh? Did the folder disappear? Let the caller deal with it */
8006 should_respond = TRUE;
8008 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8010 set_busy_cursor (data->impl, FALSE);
8012 g_signal_emit_by_name (data->impl, "response-requested");
8015 g_object_unref (data->impl);
8016 g_free (data->file_part);
8019 g_object_unref (cancellable);
8022 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8023 * should respond. Can get the file part from the file list or the save entry.
8026 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8027 const gchar *file_part,
8030 GtkFileChooserConfirmation conf;
8032 if (!impl->do_overwrite_confirmation)
8035 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8037 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8041 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8043 struct GetDisplayNameData *data;
8045 g_assert (file_part != NULL);
8047 data = g_new0 (struct GetDisplayNameData, 1);
8048 data->impl = g_object_ref (impl);
8049 data->file_part = g_strdup (file_part);
8051 if (impl->should_respond_get_info_cancellable)
8052 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8054 impl->should_respond_get_info_cancellable =
8055 _gtk_file_system_get_info (impl->file_system, parent_file,
8056 "standard::display-name",
8057 confirmation_confirm_get_info_cb,
8059 set_busy_cursor (data->impl, TRUE);
8063 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8066 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8070 g_assert_not_reached ();
8075 struct FileExistsData
8077 GtkFileChooserDefault *impl;
8078 gboolean file_exists_and_is_not_folder;
8084 save_entry_get_info_cb (GCancellable *cancellable,
8086 const GError *error,
8089 gboolean parent_is_folder;
8090 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8091 struct FileExistsData *data = user_data;
8093 if (cancellable != data->impl->should_respond_get_info_cancellable)
8096 data->impl->should_respond_get_info_cancellable = NULL;
8098 set_busy_cursor (data->impl, FALSE);
8104 parent_is_folder = FALSE;
8106 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8108 if (parent_is_folder)
8110 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8112 if (data->file_exists_and_is_not_folder)
8115 const char *file_part;
8117 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8118 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8121 g_signal_emit_by_name (data->impl, "response-requested");
8124 g_signal_emit_by_name (data->impl, "response-requested");
8126 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8128 GError *error = NULL;
8130 set_busy_cursor (data->impl, TRUE);
8131 g_file_make_directory (data->file, NULL, &error);
8132 set_busy_cursor (data->impl, FALSE);
8135 g_signal_emit_by_name (data->impl, "response-requested");
8137 error_creating_folder_dialog (data->impl, data->file, error);
8142 /* This will display an error, which is what we want */
8143 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8147 g_object_unref (data->impl);
8148 g_object_unref (data->file);
8149 g_object_unref (data->parent_file);
8152 g_object_unref (cancellable);
8156 file_exists_get_info_cb (GCancellable *cancellable,
8158 const GError *error,
8161 gboolean data_ownership_taken = FALSE;
8162 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8163 gboolean file_exists_and_is_not_folder;
8164 struct FileExistsData *data = user_data;
8166 if (cancellable != data->impl->file_exists_get_info_cancellable)
8169 data->impl->file_exists_get_info_cancellable = NULL;
8171 set_busy_cursor (data->impl, FALSE);
8176 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8178 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8179 /* user typed a filename; we are done */
8180 g_signal_emit_by_name (data->impl, "response-requested");
8181 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8182 && file_exists_and_is_not_folder)
8184 /* Oops, the user typed the name of an existing path which is not
8187 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8188 g_error_copy (error));
8192 /* check that everything up to the last component exists */
8194 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8195 data_ownership_taken = TRUE;
8197 if (data->impl->should_respond_get_info_cancellable)
8198 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8200 data->impl->should_respond_get_info_cancellable =
8201 _gtk_file_system_get_info (data->impl->file_system,
8204 save_entry_get_info_cb,
8206 set_busy_cursor (data->impl, TRUE);
8210 if (!data_ownership_taken)
8212 g_object_unref (data->impl);
8213 g_object_unref (data->file);
8214 g_object_unref (data->parent_file);
8218 g_object_unref (cancellable);
8222 paste_text_received (GtkClipboard *clipboard,
8224 GtkFileChooserDefault *impl)
8231 file = g_file_new_for_uri (text);
8233 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8234 location_popup_handler (impl, text);
8236 g_object_unref (file);
8239 /* Handler for the "location-popup-on-paste" keybinding signal */
8241 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8243 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8244 GDK_SELECTION_CLIPBOARD);
8245 gtk_clipboard_request_text (clipboard,
8246 (GtkClipboardTextReceivedFunc) paste_text_received,
8251 /* Implementation for GtkFileChooserEmbed::should_respond() */
8253 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8255 GtkFileChooserDefault *impl;
8256 GtkWidget *toplevel;
8257 GtkWidget *current_focus;
8259 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8261 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8262 g_assert (GTK_IS_WINDOW (toplevel));
8264 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8266 if (current_focus == impl->browse_files_tree_view)
8268 /* The following array encodes what we do based on the impl->action and the
8269 * number of files selected.
8272 NOOP, /* Do nothing (don't respond) */
8273 RESPOND, /* Respond immediately */
8274 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8275 ALL_FILES, /* Respond only if everything selected is a file */
8276 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8277 SAVE_ENTRY, /* Go to the code for handling the save entry */
8278 NOT_REACHED /* Sanity check */
8280 static const ActionToTake what_to_do[4][3] = {
8281 /* 0 selected 1 selected many selected */
8282 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8283 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8284 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8285 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8289 gboolean all_files, all_folders;
8291 ActionToTake action;
8295 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8297 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8298 return search_should_respond (impl);
8300 if (impl->operation_mode == OPERATION_MODE_RECENT)
8301 return recent_should_respond (impl);
8303 selection_check (impl, &num_selected, &all_files, &all_folders);
8305 if (num_selected > 2)
8310 action = what_to_do [impl->action] [k];
8320 case RESPOND_OR_SWITCH:
8321 g_assert (num_selected == 1);
8325 switch_to_selected_folder (impl);
8328 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8329 return should_respond_after_confirm_overwrite (impl,
8330 get_display_name_from_file_list (impl),
8331 impl->current_folder);
8345 g_assert_not_reached ();
8348 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8351 gboolean is_well_formed, is_empty, is_file_part_empty;
8354 GtkFileChooserEntry *entry;
8359 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8360 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8361 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8362 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8363 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8365 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8366 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8368 if (is_empty || !is_well_formed)
8371 g_assert (file != NULL);
8376 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8377 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8379 change_folder_and_display_error (impl, file, TRUE);
8382 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8383 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8385 /* The folder already exists, so we do not need to create it.
8386 * Just respond to terminate the dialog.
8392 g_assert_not_reached ();
8398 struct FileExistsData *data;
8400 /* We need to check whether file exists and is not a folder */
8402 data = g_new0 (struct FileExistsData, 1);
8403 data->impl = g_object_ref (impl);
8404 data->file = g_object_ref (file);
8405 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8407 if (impl->file_exists_get_info_cancellable)
8408 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8410 impl->file_exists_get_info_cancellable =
8411 _gtk_file_system_get_info (impl->file_system, file,
8413 file_exists_get_info_cb,
8416 set_busy_cursor (impl, TRUE);
8420 g_error_free (error);
8423 g_object_unref (file);
8426 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8428 /* The focus is on a dialog's action area button, *and* the widget that
8429 * was focused immediately before it is the file list.
8433 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8435 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8438 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8440 /* The focus is on a dialog's action area button, *and* the widget that
8441 * was focused immediately before it is the location entry.
8446 /* The focus is on a dialog's action area button or something else */
8447 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8448 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8453 g_assert_not_reached ();
8457 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8459 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8461 GtkFileChooserDefault *impl;
8464 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8466 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8467 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8469 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8470 widget = impl->browse_files_tree_view;
8472 widget = impl->location_entry;
8474 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8475 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8476 widget = impl->location_entry;
8479 g_assert_not_reached ();
8483 g_assert (widget != NULL);
8484 gtk_widget_grab_focus (widget);
8487 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8489 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8499 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8500 *list = g_slist_prepend (*list, file);
8503 /* Constructs a list of the selected paths in search mode */
8505 search_get_selected_files (GtkFileChooserDefault *impl)
8508 GtkTreeSelection *selection;
8512 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8513 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8514 result = g_slist_reverse (result);
8519 /* Called from ::should_respond(). We return whether there are selected files
8520 * in the search list.
8523 search_should_respond (GtkFileChooserDefault *impl)
8525 GtkTreeSelection *selection;
8527 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8529 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8530 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8533 struct SearchHitInsertRequest
8535 GtkFileChooserDefault *impl;
8537 GtkTreeRowReference *row_ref;
8541 search_hit_get_info_cb (GCancellable *cancellable,
8543 const GError *error,
8546 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8547 GdkPixbuf *pixbuf = NULL;
8550 GCancellable *model_cancellable;
8551 gboolean is_folder = FALSE;
8554 struct SearchHitInsertRequest *request = data;
8556 if (!request->impl->search_model)
8559 path = gtk_tree_row_reference_get_path (request->row_ref);
8563 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8565 gtk_tree_path_free (path);
8567 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8568 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8570 if (cancellable != model_cancellable)
8573 /* set the cancellable to NULL in the model */
8574 gtk_list_store_set (request->impl->search_model, &iter,
8575 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8583 gtk_list_store_remove (request->impl->search_model, &iter);
8587 display_name = g_strdup (g_file_info_get_display_name (info));
8588 mime_type = g_strdup (g_file_info_get_content_type (info));
8589 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8590 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8591 request->impl->icon_size);
8593 gtk_list_store_set (request->impl->search_model, &iter,
8594 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8595 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8596 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8597 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8601 g_object_unref (pixbuf);
8604 g_object_unref (request->impl);
8605 g_object_unref (request->file);
8606 gtk_tree_row_reference_free (request->row_ref);
8609 g_object_unref (cancellable);
8612 /* Adds one hit from the search engine to the search_model */
8614 search_add_hit (GtkFileChooserDefault *impl,
8620 char *collation_key;
8621 struct stat statbuf;
8622 struct stat *statbuf_copy;
8625 GCancellable *cancellable;
8626 struct SearchHitInsertRequest *request;
8628 file = g_file_new_for_uri (uri);
8630 if (!g_file_is_native (file))
8632 g_object_unref (file);
8636 filename = g_file_get_path (file);
8638 if (stat (filename, &statbuf) != 0)
8640 g_object_unref (file);
8645 statbuf_copy = g_new (struct stat, 1);
8646 *statbuf_copy = statbuf;
8648 tmp = g_file_get_parse_name (file);
8649 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8652 request = g_new0 (struct SearchHitInsertRequest, 1);
8653 request->impl = g_object_ref (impl);
8654 request->file = g_object_ref (file);
8656 gtk_list_store_append (impl->search_model, &iter);
8657 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8659 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8660 gtk_tree_path_free (p);
8662 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8663 "standard::type,standard::icon,"
8664 "standard::content-type,standard::display-name",
8665 search_hit_get_info_cb,
8668 gtk_list_store_set (impl->search_model, &iter,
8669 SEARCH_MODEL_COL_FILE, file,
8670 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8671 SEARCH_MODEL_COL_STAT, statbuf_copy,
8672 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8675 g_object_unref (file);
8679 /* Callback used from GtkSearchEngine when we get new hits */
8681 search_engine_hits_added_cb (GtkSearchEngine *engine,
8685 GtkFileChooserDefault *impl;
8688 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8690 for (l = hits; l; l = l->next)
8691 search_add_hit (impl, (gchar*)l->data);
8694 /* Callback used from GtkSearchEngine when the query is done running */
8696 search_engine_finished_cb (GtkSearchEngine *engine,
8699 GtkFileChooserDefault *impl;
8701 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8704 /* EB: setting the model here will avoid loads of row events,
8705 * but it'll make the search look like blocked.
8707 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8708 GTK_TREE_MODEL (impl->search_model_filter));
8711 /* FMQ: if search was empty, say that we got no hits */
8712 set_busy_cursor (impl, FALSE);
8715 /* Displays a generic error when we cannot create a GtkSearchEngine.
8716 * It would be better if _gtk_search_engine_new() gave us a GError
8717 * with a better message, but it doesn't do that right now.
8720 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8722 error_message (impl,
8723 _("Could not start the search process"),
8724 _("The program was not able to create a connection to the indexer "
8725 "daemon. Please make sure it is running."));
8729 search_engine_error_cb (GtkSearchEngine *engine,
8730 const gchar *message,
8733 GtkFileChooserDefault *impl;
8735 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8737 search_stop_searching (impl, TRUE);
8738 error_message (impl, _("Could not send the search request"), message);
8740 set_busy_cursor (impl, FALSE);
8743 /* Frees the data in the search_model */
8745 search_clear_model (GtkFileChooserDefault *impl,
8746 gboolean remove_from_treeview)
8748 GtkTreeModel *model;
8751 if (!impl->search_model)
8754 model = GTK_TREE_MODEL (impl->search_model);
8756 if (gtk_tree_model_get_iter_first (model, &iter))
8759 GCancellable *cancellable;
8761 gtk_tree_model_get (model, &iter,
8762 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8766 g_cancellable_cancel (cancellable);
8768 while (gtk_tree_model_iter_next (model, &iter));
8770 g_object_unref (impl->search_model);
8771 impl->search_model = NULL;
8773 g_object_unref (impl->search_model_filter);
8774 impl->search_model_filter = NULL;
8776 g_object_unref (impl->search_model_sort);
8777 impl->search_model_sort = NULL;
8779 if (remove_from_treeview)
8780 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8783 /* Stops any ongoing searches; does not touch the search_model */
8785 search_stop_searching (GtkFileChooserDefault *impl,
8786 gboolean remove_query)
8788 if (remove_query && impl->search_query)
8790 g_object_unref (impl->search_query);
8791 impl->search_query = NULL;
8794 if (impl->search_engine)
8796 _gtk_search_engine_stop (impl->search_engine);
8798 g_object_unref (impl->search_engine);
8799 impl->search_engine = NULL;
8803 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8805 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8807 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8809 search_stop_searching (impl, FALSE);
8810 search_clear_model (impl, TRUE);
8812 gtk_widget_destroy (impl->search_hbox);
8813 impl->search_hbox = NULL;
8814 impl->search_entry = NULL;
8816 gtk_widget_show (impl->browse_path_bar);
8817 gtk_widget_show (impl->browse_new_folder_button);
8819 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8820 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8822 gtk_widget_show (impl->location_button);
8824 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8825 gtk_widget_show (impl->location_entry_box);
8828 impl->operation_mode = OPERATION_MODE_BROWSE;
8830 file_list_set_sort_column_ids (impl);
8833 /* Sort callback from the path column */
8835 search_column_path_sort_func (GtkTreeModel *model,
8840 GtkFileChooserDefault *impl = user_data;
8841 GtkTreeIter child_a, child_b;
8842 const char *collation_key_a, *collation_key_b;
8843 gboolean is_folder_a, is_folder_b;
8845 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8846 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8848 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8849 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8850 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8852 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8853 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8854 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8857 if (!collation_key_a)
8860 if (!collation_key_b)
8863 /* always show folders first */
8864 if (is_folder_a != is_folder_b)
8865 return is_folder_a ? 1 : -1;
8867 return strcmp (collation_key_a, collation_key_b);
8870 /* Sort callback from the modification time column */
8872 search_column_mtime_sort_func (GtkTreeModel *model,
8877 GtkFileChooserDefault *impl = user_data;
8878 GtkTreeIter child_a, child_b;
8879 const struct stat *statbuf_a, *statbuf_b;
8880 gboolean is_folder_a, is_folder_b;
8882 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8883 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8885 /* Note that although we store a whole struct stat in the model, we only
8886 * compare the mtime here. If we add another column relative to a struct stat
8887 * (e.g. a file size column), we'll want another sort callback similar to this
8890 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8891 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8892 SEARCH_MODEL_COL_STAT, &statbuf_a,
8894 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8895 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8896 SEARCH_MODEL_COL_STAT, &statbuf_b,
8905 if (is_folder_a != is_folder_b)
8906 return is_folder_a ? 1 : -1;
8908 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8910 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8917 search_get_is_filtered (GtkFileChooserDefault *impl,
8919 const gchar *display_name,
8920 const gchar *mime_type)
8922 GtkFileFilterInfo filter_info;
8923 GtkFileFilterFlags needed;
8926 if (!impl->current_filter)
8929 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8930 needed = gtk_file_filter_get_needed (impl->current_filter);
8932 filter_info.display_name = display_name;
8933 filter_info.mime_type = mime_type;
8935 if (needed & GTK_FILE_FILTER_FILENAME)
8937 filter_info.filename = g_file_get_path (file);
8938 if (filter_info.filename)
8939 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8942 filter_info.filename = NULL;
8944 if (needed & GTK_FILE_FILTER_URI)
8946 filter_info.uri = g_file_get_uri (file);
8947 if (filter_info.uri)
8948 filter_info.contains |= GTK_FILE_FILTER_URI;
8951 filter_info.uri = NULL;
8953 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8955 if (filter_info.filename)
8956 g_free ((gchar *) filter_info.filename);
8957 if (filter_info.uri)
8958 g_free ((gchar *) filter_info.uri);
8964 /* Visibility function for the recent filter model */
8966 search_model_visible_func (GtkTreeModel *model,
8970 GtkFileChooserDefault *impl = user_data;
8972 gchar *display_name, *mime_type;
8975 if (!impl->current_filter)
8978 gtk_tree_model_get (model, iter,
8979 SEARCH_MODEL_COL_FILE, &file,
8980 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
8981 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8982 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8991 return !search_get_is_filtered (impl, file, display_name, mime_type);
8994 /* Creates the search_model and puts it in the tree view */
8996 search_setup_model (GtkFileChooserDefault *impl)
8998 g_assert (impl->search_model == NULL);
8999 g_assert (impl->search_model_filter == NULL);
9000 g_assert (impl->search_model_sort == NULL);
9002 /* We store these columns in the search model:
9004 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9005 * as a pointer not as a G_TYPE_FILE
9006 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9007 * as a pointer not as a G_TYPE_STRING
9008 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9009 * as a pointer not as a G_TYPE_STRING
9010 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9011 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9012 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9013 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9014 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9016 * Keep this in sync with the enumeration defined near the beginning
9019 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9029 impl->search_model_filter =
9030 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9031 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9032 search_model_visible_func,
9035 impl->search_model_sort =
9036 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9037 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9038 SEARCH_MODEL_COL_FILE,
9039 search_column_path_sort_func,
9041 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9042 SEARCH_MODEL_COL_STAT,
9043 search_column_mtime_sort_func,
9045 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9046 SEARCH_MODEL_COL_STAT,
9047 GTK_SORT_DESCENDING);
9049 /* EB: setting the model here will make the hits list update feel
9050 * more "alive" than setting the model at the end of the search
9053 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9054 GTK_TREE_MODEL (impl->search_model_sort));
9058 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9059 GtkTreeIter *child_iter,
9064 if (!impl->search_model)
9067 if (!impl->search_model_filter || !impl->search_model_sort)
9070 /* pass 1: get the iterator in the filter model */
9071 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9074 /* pass 2: get the iterator in the real model */
9075 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9076 child_iter, &middle);
9079 /* Creates a new query with the specified text and launches it */
9081 search_start_query (GtkFileChooserDefault *impl,
9082 const gchar *query_text)
9084 search_stop_searching (impl, FALSE);
9085 search_clear_model (impl, TRUE);
9086 search_setup_model (impl);
9087 set_busy_cursor (impl, TRUE);
9089 if (impl->search_engine == NULL)
9090 impl->search_engine = _gtk_search_engine_new ();
9092 if (!impl->search_engine)
9094 set_busy_cursor (impl, FALSE);
9095 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9099 if (!impl->search_query)
9101 impl->search_query = _gtk_query_new ();
9102 _gtk_query_set_text (impl->search_query, query_text);
9105 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9107 g_signal_connect (impl->search_engine, "hits-added",
9108 G_CALLBACK (search_engine_hits_added_cb), impl);
9109 g_signal_connect (impl->search_engine, "finished",
9110 G_CALLBACK (search_engine_finished_cb), impl);
9111 g_signal_connect (impl->search_engine, "error",
9112 G_CALLBACK (search_engine_error_cb), impl);
9114 _gtk_search_engine_start (impl->search_engine);
9117 /* Callback used when the user presses Enter while typing on the search
9118 * entry; starts the query
9121 search_entry_activate_cb (GtkEntry *entry,
9124 GtkFileChooserDefault *impl;
9127 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9129 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9130 if (strlen (text) == 0)
9133 /* reset any existing query object */
9134 if (impl->search_query)
9136 g_object_unref (impl->search_query);
9137 impl->search_query = NULL;
9140 search_start_query (impl, text);
9143 /* Hides the path bar and creates the search entry */
9145 search_setup_widgets (GtkFileChooserDefault *impl)
9149 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9153 label = gtk_label_new_with_mnemonic (_("_Search:"));
9154 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9158 impl->search_entry = gtk_entry_new ();
9159 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9160 g_signal_connect (impl->search_entry, "activate",
9161 G_CALLBACK (search_entry_activate_cb),
9163 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9165 /* if there already is a query, restart it */
9166 if (impl->search_query)
9168 gchar *query = _gtk_query_get_text (impl->search_query);
9172 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9173 search_start_query (impl, query);
9179 g_object_unref (impl->search_query);
9180 impl->search_query = NULL;
9184 gtk_widget_hide (impl->browse_path_bar);
9185 gtk_widget_hide (impl->browse_new_folder_button);
9187 /* Box for search widgets */
9188 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9189 gtk_widget_show_all (impl->search_hbox);
9191 /* Hide the location widgets temporarily */
9193 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9194 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9196 gtk_widget_hide (impl->location_button);
9197 gtk_widget_hide (impl->location_entry_box);
9200 gtk_widget_grab_focus (impl->search_entry);
9202 /* FMQ: hide the filter combo? */
9205 /* Main entry point to the searching functions; this gets called when the user
9206 * activates the Search shortcut.
9209 search_activate (GtkFileChooserDefault *impl)
9211 OperationMode previous_mode;
9213 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9215 gtk_widget_grab_focus (impl->search_entry);
9219 previous_mode = impl->operation_mode;
9220 impl->operation_mode = OPERATION_MODE_SEARCH;
9222 switch (previous_mode)
9224 case OPERATION_MODE_RECENT:
9225 recent_stop_loading (impl);
9226 recent_clear_model (impl, TRUE);
9229 case OPERATION_MODE_BROWSE:
9230 stop_loading_and_clear_list_model (impl);
9233 case OPERATION_MODE_SEARCH:
9234 g_assert_not_reached ();
9238 g_assert (impl->search_hbox == NULL);
9239 g_assert (impl->search_entry == NULL);
9240 g_assert (impl->search_model == NULL);
9241 g_assert (impl->search_model_filter == NULL);
9243 search_setup_widgets (impl);
9244 file_list_set_sort_column_ids (impl);
9248 * Recent files support
9251 /* Frees the data in the recent_model */
9253 recent_clear_model (GtkFileChooserDefault *impl,
9254 gboolean remove_from_treeview)
9256 GtkTreeModel *model;
9259 if (!impl->recent_model)
9262 model = GTK_TREE_MODEL (impl->recent_model);
9264 if (remove_from_treeview)
9265 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9267 if (gtk_tree_model_get_iter_first (model, &iter))
9272 GCancellable *cancellable;
9273 GtkRecentInfo *recent_info;
9274 gchar *display_name;
9276 gtk_tree_model_get (model, &iter,
9277 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9278 RECENT_MODEL_COL_FILE, &file,
9279 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9280 RECENT_MODEL_COL_INFO, &recent_info,
9284 g_cancellable_cancel (cancellable);
9286 g_object_unref (file);
9287 gtk_recent_info_unref (recent_info);
9288 g_free (display_name);
9290 while (gtk_tree_model_iter_next (model, &iter));
9293 g_object_unref (impl->recent_model);
9294 impl->recent_model = NULL;
9296 g_object_unref (impl->recent_model_filter);
9297 impl->recent_model_filter = NULL;
9299 g_object_unref (impl->recent_model_sort);
9300 impl->recent_model_sort = NULL;
9303 /* Stops any ongoing loading of the recent files list; does
9304 * not touch the recent_model
9307 recent_stop_loading (GtkFileChooserDefault *impl)
9309 if (impl->load_recent_id)
9311 g_source_remove (impl->load_recent_id);
9312 impl->load_recent_id = 0;
9316 /* Stops any pending load, clears the file list, and switches
9317 * back to OPERATION_MODE_BROWSE
9320 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9322 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9324 recent_stop_loading (impl);
9325 recent_clear_model (impl, TRUE);
9327 gtk_widget_show (impl->browse_path_bar);
9328 gtk_widget_show (impl->browse_new_folder_button);
9330 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9331 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9333 gtk_widget_show (impl->location_button);
9335 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9336 gtk_widget_show (impl->location_entry_box);
9339 impl->operation_mode = OPERATION_MODE_BROWSE;
9341 file_list_set_sort_column_ids (impl);
9344 /* Sort callback from the modification time column */
9346 recent_column_mtime_sort_func (GtkTreeModel *model,
9351 GtkFileChooserDefault *impl = user_data;
9352 GtkTreeIter child_a, child_b;
9353 GtkRecentInfo *info_a, *info_b;
9354 gboolean is_folder_a, is_folder_b;
9356 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9357 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9359 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9360 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9361 RECENT_MODEL_COL_INFO, &info_a,
9363 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9364 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9365 RECENT_MODEL_COL_INFO, &info_b,
9374 /* folders always go first */
9375 if (is_folder_a != is_folder_b)
9376 return is_folder_a ? 1 : -1;
9378 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9380 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9387 recent_column_path_sort_func (GtkTreeModel *model,
9392 GtkFileChooserDefault *impl = user_data;
9393 GtkTreeIter child_a, child_b;
9394 gboolean is_folder_a, is_folder_b;
9395 gchar *name_a, *name_b;
9397 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9398 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9400 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9401 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9402 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9404 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9405 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9406 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9415 if (is_folder_a != is_folder_b)
9416 return is_folder_a ? 1 : -1;
9418 return strcmp (name_a, name_b);
9422 recent_get_is_filtered (GtkFileChooserDefault *impl,
9424 GtkRecentInfo *recent_info)
9426 GtkFileFilterInfo filter_info;
9427 GtkFileFilterFlags needed;
9430 if (!impl->current_filter)
9433 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9434 needed = gtk_file_filter_get_needed (impl->current_filter);
9436 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9437 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9439 if (needed & GTK_FILE_FILTER_FILENAME)
9441 filter_info.filename = g_file_get_path (file);
9442 if (filter_info.filename)
9443 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9446 filter_info.filename = NULL;
9448 if (needed & GTK_FILE_FILTER_URI)
9450 filter_info.uri = g_file_get_uri (file);
9451 if (filter_info.uri)
9452 filter_info.contains |= GTK_FILE_FILTER_URI;
9455 filter_info.uri = NULL;
9457 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9459 if (filter_info.filename)
9460 g_free ((gchar *) filter_info.filename);
9461 if (filter_info.uri)
9462 g_free ((gchar *) filter_info.uri);
9467 /* Visibility function for the recent filter model */
9469 recent_model_visible_func (GtkTreeModel *model,
9473 GtkFileChooserDefault *impl = user_data;
9475 GtkRecentInfo *recent_info;
9478 if (!impl->current_filter)
9481 gtk_tree_model_get (model, iter,
9482 RECENT_MODEL_COL_INFO, &recent_info,
9483 RECENT_MODEL_COL_FILE, &file,
9484 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9493 return !recent_get_is_filtered (impl, file, recent_info);
9497 recent_setup_model (GtkFileChooserDefault *impl)
9499 g_assert (impl->recent_model == NULL);
9500 g_assert (impl->recent_model_filter == NULL);
9501 g_assert (impl->recent_model_sort == NULL);
9503 /* We store these columns in the search model:
9505 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9506 * stored as a pointer and not as a G_TYPE_FILE;
9507 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9508 * stored as a pointer and not as a G_TYPE_STRING;
9509 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9510 * as a GTK_TYPE_RECENT_INFO;
9511 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9512 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9513 * and not as a G_TYPE_CANCELLABLE;
9515 * Keep this in sync with the enumeration defined near the beginning of
9518 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9525 impl->recent_model_filter =
9526 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9527 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9528 recent_model_visible_func,
9532 /* this is the model that will actually be added to
9533 * the browse_files_tree_view widget; remember: we are
9534 * stuffing the real model into a filter model and then
9535 * into a sort model; this means we'll have to translate
9536 * the child iterator *twice* to get from a path or an
9537 * iterator coming from the tree view widget to the
9538 * real data inside the model.
9540 impl->recent_model_sort =
9541 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9542 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9543 RECENT_MODEL_COL_FILE,
9544 recent_column_path_sort_func,
9546 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9547 RECENT_MODEL_COL_INFO,
9548 recent_column_mtime_sort_func,
9550 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9551 RECENT_MODEL_COL_INFO,
9552 GTK_SORT_DESCENDING);
9557 GtkFileChooserDefault *impl;
9560 gint n_loaded_items;
9561 guint needs_sorting : 1;
9565 recent_idle_cleanup (gpointer data)
9567 RecentLoadData *load_data = data;
9568 GtkFileChooserDefault *impl = load_data->impl;
9570 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9571 GTK_TREE_MODEL (impl->recent_model_sort));
9573 set_busy_cursor (impl, FALSE);
9575 impl->load_recent_id = 0;
9577 if (load_data->items)
9579 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9580 g_list_free (load_data->items);
9586 struct RecentItemInsertRequest
9588 GtkFileChooserDefault *impl;
9590 GtkTreeRowReference *row_ref;
9594 recent_item_get_info_cb (GCancellable *cancellable,
9596 const GError *error,
9599 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9602 GCancellable *model_cancellable;
9603 gboolean is_folder = FALSE;
9604 struct RecentItemInsertRequest *request = data;
9606 if (!request->impl->recent_model)
9609 path = gtk_tree_row_reference_get_path (request->row_ref);
9613 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9615 gtk_tree_path_free (path);
9617 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9618 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9620 if (cancellable != model_cancellable)
9623 gtk_list_store_set (request->impl->recent_model, &iter,
9624 RECENT_MODEL_COL_CANCELLABLE, NULL,
9632 gtk_list_store_remove (request->impl->recent_model, &iter);
9636 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9638 gtk_list_store_set (request->impl->recent_model, &iter,
9639 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9643 g_object_unref (request->impl);
9644 g_object_unref (request->file);
9645 gtk_tree_row_reference_free (request->row_ref);
9648 g_object_unref (cancellable);
9652 recent_sort_mru (gconstpointer a,
9655 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9656 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9658 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9662 get_recent_files_limit (GtkWidget *widget)
9664 GtkSettings *settings;
9667 if (gtk_widget_has_screen (widget))
9668 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9670 settings = gtk_settings_get_default ();
9672 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9678 recent_idle_load (gpointer data)
9680 RecentLoadData *load_data = data;
9681 GtkFileChooserDefault *impl = load_data->impl;
9684 GtkRecentInfo *info;
9685 const gchar *uri, *display_name;
9687 GCancellable *cancellable;
9688 struct RecentItemInsertRequest *request;
9690 if (!impl->recent_manager)
9693 /* first iteration: load all the items */
9694 if (!load_data->items)
9696 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9697 if (!load_data->items)
9700 load_data->needs_sorting = TRUE;
9705 /* second iteration: preliminary MRU sorting and clamping */
9706 if (load_data->needs_sorting)
9710 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9711 load_data->n_items = g_list_length (load_data->items);
9713 limit = get_recent_files_limit (GTK_WIDGET (impl));
9715 if (limit != -1 && (load_data->n_items > limit))
9719 clamp = g_list_nth (load_data->items, limit - 1);
9720 if (G_LIKELY (clamp))
9725 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9728 load_data->n_items = limit;
9732 load_data->n_loaded_items = 0;
9733 load_data->needs_sorting = FALSE;
9738 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9739 g_assert (info != NULL);
9741 uri = gtk_recent_info_get_uri (info);
9742 display_name = gtk_recent_info_get_display_name (info);
9743 file = g_file_new_for_uri (uri);
9745 gtk_list_store_append (impl->recent_model, &iter);
9746 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9748 request = g_new0 (struct RecentItemInsertRequest, 1);
9749 request->impl = g_object_ref (impl);
9750 request->file = g_object_ref (file);
9751 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9752 gtk_tree_path_free (p);
9754 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9756 recent_item_get_info_cb,
9759 gtk_list_store_set (impl->recent_model, &iter,
9760 RECENT_MODEL_COL_FILE, file,
9761 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9762 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9763 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9766 load_data->n_loaded_items += 1;
9768 /* finished loading items */
9769 if (load_data->n_loaded_items == load_data->n_items)
9771 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9772 g_list_free (load_data->items);
9773 load_data->items = NULL;
9782 recent_start_loading (GtkFileChooserDefault *impl)
9784 RecentLoadData *load_data;
9786 recent_stop_loading (impl);
9787 recent_clear_model (impl, TRUE);
9788 recent_setup_model (impl);
9789 set_busy_cursor (impl, TRUE);
9791 g_assert (impl->load_recent_id == 0);
9793 load_data = g_new (RecentLoadData, 1);
9794 load_data->impl = impl;
9795 load_data->items = NULL;
9796 load_data->n_items = 0;
9797 load_data->n_loaded_items = 0;
9798 load_data->needs_sorting = TRUE;
9800 /* begin lazy loading the recent files into the model */
9801 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9804 recent_idle_cleanup);
9808 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9818 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9819 *list = g_slist_prepend (*list, g_object_ref (file));
9822 /* Constructs a list of the selected paths in recent files mode */
9824 recent_get_selected_files (GtkFileChooserDefault *impl)
9827 GtkTreeSelection *selection;
9831 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9832 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9833 result = g_slist_reverse (result);
9838 /* Called from ::should_respond(). We return whether there are selected
9839 * files in the recent files list.
9842 recent_should_respond (GtkFileChooserDefault *impl)
9844 GtkTreeSelection *selection;
9846 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9848 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9849 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9852 /* Hide the location widgets temporarily */
9854 recent_hide_entry (GtkFileChooserDefault *impl)
9856 gtk_widget_hide (impl->browse_path_bar);
9857 gtk_widget_hide (impl->browse_new_folder_button);
9859 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9860 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9862 gtk_widget_hide (impl->location_button);
9863 gtk_widget_hide (impl->location_entry_box);
9867 /* Main entry point to the recent files functions; this gets called when
9868 * the user activates the Recently Used shortcut.
9871 recent_activate (GtkFileChooserDefault *impl)
9873 OperationMode previous_mode;
9875 if (impl->operation_mode == OPERATION_MODE_RECENT)
9878 previous_mode = impl->operation_mode;
9879 impl->operation_mode = OPERATION_MODE_RECENT;
9881 switch (previous_mode)
9883 case OPERATION_MODE_SEARCH:
9884 search_stop_searching (impl, FALSE);
9885 search_clear_model (impl, TRUE);
9887 gtk_widget_destroy (impl->search_hbox);
9888 impl->search_hbox = NULL;
9889 impl->search_entry = NULL;
9892 case OPERATION_MODE_BROWSE:
9893 stop_loading_and_clear_list_model (impl);
9896 case OPERATION_MODE_RECENT:
9897 g_assert_not_reached ();
9901 recent_hide_entry (impl);
9902 file_list_set_sort_column_ids (impl);
9903 recent_start_loading (impl);
9906 /* convert an iterator coming from the model bound to
9907 * browse_files_tree_view to an interator inside the
9911 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9912 GtkTreeIter *child_iter,
9917 if (!impl->recent_model)
9920 if (!impl->recent_model_filter || !impl->recent_model_sort)
9923 /* pass 1: get the iterator in the filter model */
9924 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9927 /* pass 2: get the iterator in the real model */
9928 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9935 set_current_filter (GtkFileChooserDefault *impl,
9936 GtkFileFilter *filter)
9938 if (impl->current_filter != filter)
9942 /* NULL filters are allowed to reset to non-filtered status
9944 filter_index = g_slist_index (impl->filters, filter);
9945 if (impl->filters && filter && filter_index < 0)
9948 if (impl->current_filter)
9949 g_object_unref (impl->current_filter);
9950 impl->current_filter = filter;
9951 if (impl->current_filter)
9953 g_object_ref_sink (impl->current_filter);
9957 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9960 if (impl->browse_files_model)
9961 install_list_model_filter (impl);
9963 if (impl->search_model_filter)
9964 gtk_tree_model_filter_refilter (impl->search_model_filter);
9966 if (impl->recent_model_filter)
9967 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9969 g_object_notify (G_OBJECT (impl), "filter");
9974 filter_combo_changed (GtkComboBox *combo_box,
9975 GtkFileChooserDefault *impl)
9977 gint new_index = gtk_combo_box_get_active (combo_box);
9978 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9980 set_current_filter (impl, new_filter);
9984 check_preview_change (GtkFileChooserDefault *impl)
9986 GtkTreePath *cursor_path;
9988 const char *new_display_name;
9990 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9992 new_display_name = NULL;
9995 GtkTreeIter child_iter;
9997 if (impl->operation_mode == OPERATION_MODE_BROWSE)
9999 if (impl->sort_model)
10002 GFileInfo *new_info;
10004 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10005 gtk_tree_path_free (cursor_path);
10007 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10009 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10010 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10012 new_display_name = g_file_info_get_display_name (new_info);
10015 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10019 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10020 &iter, cursor_path);
10021 gtk_tree_path_free (cursor_path);
10023 search_get_valid_child_iter (impl, &child_iter, &iter);
10024 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10025 SEARCH_MODEL_COL_FILE, &new_file,
10026 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10029 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10033 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10034 &iter, cursor_path);
10035 gtk_tree_path_free (cursor_path);
10037 recent_get_valid_child_iter (impl, &child_iter, &iter);
10038 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10039 RECENT_MODEL_COL_FILE, &new_file,
10040 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10045 if (new_file != impl->preview_file &&
10046 !(new_file && impl->preview_file &&
10047 g_file_equal (new_file, impl->preview_file)))
10049 if (impl->preview_file)
10051 g_object_unref (impl->preview_file);
10052 g_free (impl->preview_display_name);
10057 impl->preview_file = g_object_ref (new_file);
10058 impl->preview_display_name = g_strdup (new_display_name);
10062 impl->preview_file = NULL;
10063 impl->preview_display_name = NULL;
10066 if (impl->use_preview_label && impl->preview_label)
10067 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10069 g_signal_emit_by_name (impl, "update-preview");
10074 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10075 GtkFileSystemVolume *volume,
10076 const GError *error,
10080 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10081 GtkFileChooserDefault *impl = data;
10083 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10086 impl->shortcuts_activate_iter_cancellable = NULL;
10088 set_busy_cursor (impl, FALSE);
10095 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10099 name = _gtk_file_system_volume_get_display_name (volume);
10100 msg = g_strdup_printf (_("Could not mount %s"), name);
10102 error_message (impl, msg, error->message);
10111 file = _gtk_file_system_volume_get_root (volume);
10114 change_folder_and_display_error (impl, file, FALSE);
10115 g_object_unref (file);
10119 g_object_unref (impl);
10120 g_object_unref (cancellable);
10124 /* Activates a volume by mounting it if necessary and then switching to its
10128 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10129 GtkFileSystemVolume *volume)
10133 switch (impl->operation_mode)
10135 case OPERATION_MODE_BROWSE:
10137 case OPERATION_MODE_SEARCH:
10138 search_switch_to_browse_mode (impl);
10140 case OPERATION_MODE_RECENT:
10141 recent_switch_to_browse_mode (impl);
10145 /* We ref the file chooser since volume_mount() may run a main loop, and the
10146 * user could close the file chooser window in the meantime.
10148 g_object_ref (impl);
10150 if (!_gtk_file_system_volume_is_mounted (volume))
10152 set_busy_cursor (impl, TRUE);
10154 impl->shortcuts_activate_iter_cancellable =
10155 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10156 shortcuts_activate_volume_mount_cb,
10157 g_object_ref (impl));
10161 file = _gtk_file_system_volume_get_root (volume);
10164 change_folder_and_display_error (impl, file, FALSE);
10165 g_object_unref (file);
10169 g_object_unref (impl);
10172 /* Opens the folder or volume at the specified iter in the shortcuts model */
10173 struct ShortcutsActivateData
10175 GtkFileChooserDefault *impl;
10180 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10182 const GError *error,
10183 gpointer user_data)
10185 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10186 struct ShortcutsActivateData *data = user_data;
10188 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10191 data->impl->shortcuts_activate_iter_cancellable = NULL;
10196 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10197 change_folder_and_display_error (data->impl, data->file, FALSE);
10199 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10204 g_object_unref (data->impl);
10205 g_object_unref (data->file);
10208 g_object_unref (cancellable);
10212 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10213 GtkFileSystemVolume *volume,
10214 const GError *error,
10215 gpointer user_data)
10217 struct ShortcutsActivateData *data = user_data;
10221 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10223 g_object_unref (data->impl);
10224 g_object_unref (data->file);
10230 data->impl->shortcuts_activate_iter_cancellable =
10231 _gtk_file_system_get_info (data->impl->file_system, data->file,
10233 shortcuts_activate_get_info_cb, data);
10237 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10241 ShortcutType shortcut_type;
10243 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10244 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10245 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10246 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10248 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10249 SHORTCUTS_COL_DATA, &col_data,
10250 SHORTCUTS_COL_TYPE, &shortcut_type,
10253 if (impl->shortcuts_activate_iter_cancellable)
10255 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10256 impl->shortcuts_activate_iter_cancellable = NULL;
10259 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10261 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10263 GtkFileSystemVolume *volume;
10267 shortcuts_activate_volume (impl, volume);
10269 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10271 struct ShortcutsActivateData *data;
10272 GtkFileSystemVolume *volume;
10274 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10276 data = g_new0 (struct ShortcutsActivateData, 1);
10277 data->impl = g_object_ref (impl);
10278 data->file = g_object_ref (col_data);
10280 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10282 GMountOperation *mount_operation;
10283 GtkWidget *toplevel;
10285 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10287 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10289 impl->shortcuts_activate_iter_cancellable =
10290 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10292 shortcuts_activate_mount_enclosing_volume,
10297 impl->shortcuts_activate_iter_cancellable =
10298 _gtk_file_system_get_info (impl->file_system, data->file,
10300 shortcuts_activate_get_info_cb, data);
10303 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10305 search_activate (impl);
10307 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10309 recent_activate (impl);
10313 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10315 shortcuts_key_press_event_cb (GtkWidget *widget,
10316 GdkEventKey *event,
10317 GtkFileChooserDefault *impl)
10321 modifiers = gtk_accelerator_get_default_mod_mask ();
10323 if ((event->keyval == GDK_BackSpace
10324 || event->keyval == GDK_Delete
10325 || event->keyval == GDK_KP_Delete)
10326 && (event->state & modifiers) == 0)
10328 remove_selected_bookmarks (impl);
10332 if ((event->keyval == GDK_F2)
10333 && (event->state & modifiers) == 0)
10335 rename_selected_bookmark (impl);
10343 shortcuts_select_func (GtkTreeSelection *selection,
10344 GtkTreeModel *model,
10346 gboolean path_currently_selected,
10349 GtkFileChooserDefault *impl = data;
10350 GtkTreeIter filter_iter;
10351 ShortcutType shortcut_type;
10353 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10354 g_assert_not_reached ();
10356 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10358 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10362 list_select_func (GtkTreeSelection *selection,
10363 GtkTreeModel *model,
10365 gboolean path_currently_selected,
10368 GtkFileChooserDefault *impl = data;
10370 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10371 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10373 GtkTreeIter iter, child_iter;
10375 switch (impl->operation_mode)
10377 case OPERATION_MODE_SEARCH:
10379 gboolean is_folder;
10381 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10384 search_get_valid_child_iter (impl, &child_iter, &iter);
10385 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10386 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10393 case OPERATION_MODE_RECENT:
10395 gboolean is_folder;
10397 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10400 recent_get_valid_child_iter (impl, &child_iter, &iter);
10401 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10402 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10409 case OPERATION_MODE_BROWSE:
10413 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10416 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10417 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10418 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10429 list_selection_changed (GtkTreeSelection *selection,
10430 GtkFileChooserDefault *impl)
10432 /* See if we are in the new folder editable row for Save mode */
10433 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10434 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10437 gboolean had_selection;
10439 info = get_selected_file_info_from_file_list (impl, &had_selection);
10440 if (!had_selection)
10441 goto out; /* normal processing */
10444 return; /* We are on the editable row for New Folder */
10449 if (impl->location_entry)
10450 update_chooser_entry (impl);
10452 check_preview_change (impl);
10453 bookmarks_check_add_sensitivity (impl);
10455 g_signal_emit_by_name (impl, "selection-changed", 0);
10458 /* Callback used when a row in the file list is activated */
10460 list_row_activated (GtkTreeView *tree_view,
10462 GtkTreeViewColumn *column,
10463 GtkFileChooserDefault *impl)
10466 GtkTreeIter child_iter;
10468 switch (impl->operation_mode)
10470 case OPERATION_MODE_SEARCH:
10473 gboolean is_folder;
10475 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10478 search_get_valid_child_iter (impl, &child_iter, &iter);
10479 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10480 SEARCH_MODEL_COL_FILE, &file,
10481 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10486 change_folder_and_display_error (impl, file, FALSE);
10490 g_signal_emit_by_name (impl, "file-activated");
10494 case OPERATION_MODE_RECENT:
10497 gboolean is_folder;
10499 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10502 recent_get_valid_child_iter (impl, &child_iter, &iter);
10503 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10504 RECENT_MODEL_COL_FILE, &file,
10505 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10510 change_folder_and_display_error (impl, file, FALSE);
10514 g_signal_emit_by_name (impl, "file-activated");
10518 case OPERATION_MODE_BROWSE:
10522 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10525 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10526 &child_iter, &iter);
10527 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10530 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10534 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10535 change_folder_and_display_error (impl, file, FALSE);
10539 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10540 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10541 g_signal_emit_by_name (impl, "file-activated");
10548 path_bar_clicked (GtkPathBar *path_bar,
10551 gboolean child_is_hidden,
10552 GtkFileChooserDefault *impl)
10555 pending_select_files_add (impl, child_file);
10557 if (!change_folder_and_display_error (impl, file, FALSE))
10560 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10561 * show hidden files so that ".baz" appears in the file list, as it will still
10562 * be shown in the path bar: "/foo/[bar]/.baz"
10564 if (child_is_hidden)
10565 g_object_set (impl, "show-hidden", TRUE, NULL);
10569 get_list_file_info (GtkFileChooserDefault *impl,
10572 GtkTreeIter child_iter;
10574 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10578 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10582 list_icon_data_func (GtkTreeViewColumn *tree_column,
10583 GtkCellRenderer *cell,
10584 GtkTreeModel *tree_model,
10588 GtkFileChooserDefault *impl = data;
10589 GtkTreeIter child_iter;
10590 GdkPixbuf *pixbuf = NULL;
10591 gboolean sensitive = TRUE;
10593 profile_start ("start", NULL);
10595 switch (impl->operation_mode)
10597 case OPERATION_MODE_SEARCH:
10599 GtkTreeIter child_iter;
10600 gboolean is_folder;
10602 search_get_valid_child_iter (impl, &child_iter, iter);
10603 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10604 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10605 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10608 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10609 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10610 sensitive = is_folder;
10614 case OPERATION_MODE_RECENT:
10616 GtkTreeIter child_iter;
10617 GtkRecentInfo *info;
10618 gboolean is_folder;
10620 recent_get_valid_child_iter (impl, &child_iter, iter);
10621 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10622 RECENT_MODEL_COL_INFO, &info,
10623 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10626 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10628 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10629 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10630 sensitive = is_folder;
10634 case OPERATION_MODE_BROWSE:
10639 info = get_list_file_info (impl, iter);
10641 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10644 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10649 /* FIXME: NULL GError */
10650 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10655 /* We are on the editable row */
10660 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10661 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10662 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10667 g_object_set (cell,
10669 "sensitive", sensitive,
10673 g_object_unref (pixbuf);
10675 profile_end ("end", NULL);
10679 list_name_data_func (GtkTreeViewColumn *tree_column,
10680 GtkCellRenderer *cell,
10681 GtkTreeModel *tree_model,
10685 GtkFileChooserDefault *impl = data;
10687 gboolean sensitive = TRUE;
10689 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10691 GtkTreeIter child_iter;
10692 gchar *display_name;
10693 gboolean is_folder;
10695 search_get_valid_child_iter (impl, &child_iter, iter);
10696 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10697 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10698 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10701 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10702 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10704 sensitive = is_folder;
10707 g_object_set (cell,
10708 "text", display_name,
10709 "sensitive", sensitive,
10710 "ellipsize", PANGO_ELLIPSIZE_END,
10716 if (impl->operation_mode == OPERATION_MODE_RECENT)
10718 GtkTreeIter child_iter;
10719 GtkRecentInfo *recent_info;
10720 gchar *display_name;
10721 gboolean is_folder;
10723 recent_get_valid_child_iter (impl, &child_iter, iter);
10724 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10725 RECENT_MODEL_COL_INFO, &recent_info,
10726 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10729 display_name = gtk_recent_info_get_short_name (recent_info);
10731 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10732 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10734 sensitive = is_folder;
10737 g_object_set (cell,
10738 "text", display_name,
10739 "sensitive", sensitive,
10740 "ellipsize", PANGO_ELLIPSIZE_END,
10743 g_free (display_name);
10748 info = get_list_file_info (impl, iter);
10753 g_object_set (cell,
10754 "text", _("Type name of new folder"),
10756 "ellipsize", PANGO_ELLIPSIZE_NONE,
10763 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10764 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10766 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10769 g_object_set (cell,
10770 "text", g_file_info_get_display_name (info),
10771 "sensitive", sensitive,
10772 "ellipsize", PANGO_ELLIPSIZE_END,
10778 list_size_data_func (GtkTreeViewColumn *tree_column,
10779 GtkCellRenderer *cell,
10780 GtkTreeModel *tree_model,
10784 GtkFileChooserDefault *impl = data;
10785 GFileInfo *info = get_list_file_info (impl, iter);
10788 gboolean sensitive = TRUE;
10790 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10792 g_object_set (cell,
10794 "sensitive", sensitive,
10799 size = gtk_file_info_get_size (info);
10801 if (size < (gint64)1024)
10802 str = g_strdup_printf (g_dngettext (GETTEXT_DOMAIN, "%d byte", "%d bytes", (gint)size), (gint)size);
10803 else if (size < (gint64)1024*1024)
10804 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10805 else if (size < (gint64)1024*1024*1024)
10806 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10808 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10810 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10811 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10812 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10815 g_object_set (cell,
10817 "sensitive", sensitive,
10818 "alignment", PANGO_ALIGN_RIGHT,
10825 /* Tree column data callback for the file list; fetches the mtime of a file */
10827 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10828 GtkCellRenderer *cell,
10829 GtkTreeModel *tree_model,
10833 GtkFileChooserDefault *impl;
10834 GTimeVal timeval = { 0, };
10836 gchar *date_str = NULL;
10837 gboolean sensitive = TRUE;
10839 const char *locale, *dot = NULL;
10840 gint64 codepage = -1;
10846 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10848 GtkTreeIter child_iter;
10849 struct stat *statbuf;
10850 gboolean is_folder;
10852 search_get_valid_child_iter (impl, &child_iter, iter);
10853 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10854 SEARCH_MODEL_COL_STAT, &statbuf,
10855 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10858 time_mtime = statbuf->st_mtime;
10863 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10864 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10865 sensitive = is_folder;
10867 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10869 GtkTreeIter child_iter;
10870 GtkRecentInfo *info;
10871 gboolean is_folder;
10873 recent_get_valid_child_iter (impl, &child_iter, iter);
10874 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10875 RECENT_MODEL_COL_INFO, &info,
10876 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10880 time_mtime = gtk_recent_info_get_modified (info);
10884 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10885 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10886 sensitive = is_folder;
10892 info = get_list_file_info (impl, iter);
10895 g_object_set (cell,
10902 g_file_info_get_modification_time (info, &timeval);
10903 time_mtime = timeval.tv_sec;
10905 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10906 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10907 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10910 if (G_UNLIKELY (time_mtime == 0))
10911 date_str = g_strdup (_("Unknown"));
10916 struct tm tm_mtime;
10918 const gchar *format;
10919 gchar *locale_format = NULL;
10922 #ifdef HAVE_LOCALTIME_R
10923 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10926 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10930 g_warning ("ptm != NULL failed");
10932 g_object_set (cell,
10933 "text", _("Unknown"),
10934 "sensitive", sensitive,
10939 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10941 #endif /* HAVE_LOCALTIME_R */
10943 g_date_set_time_t (&mtime, time_mtime);
10944 time_now = time (NULL);
10945 g_date_set_time_t (&now, time_now);
10947 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10949 /* Translators: %H means "hours" and %M means "minutes" */
10950 if (days_diff == 0)
10951 format = _("%H:%M");
10952 else if (days_diff == 1)
10953 format = _("Yesterday at %H:%M");
10956 if (days_diff > 1 && days_diff < 7)
10957 format = "%A"; /* Days from last week */
10959 format = "%x"; /* Any other date */
10963 /* g_locale_from_utf8() returns a string in the system
10964 * code-page, which is not always the same as that used by the C
10965 * library. For instance when running a GTK+ program with
10966 * LANG=ko on an English version of Windows, the system
10967 * code-page is 1252, but the code-page used by the C library is
10968 * 949. (It's GTK+ itself that sets the C library locale when it
10969 * notices the LANG environment variable. See gtkmain.c The
10970 * Microsoft C library doesn't look at any locale environment
10971 * variables.) We need to pass strftime() a string in the C
10972 * library's code-page. See bug #509885.
10974 locale = setlocale (LC_ALL, NULL);
10975 if (locale != NULL)
10976 dot = strchr (locale, '.');
10979 codepage = g_ascii_strtoll (dot+1, NULL, 10);
10981 /* All codepages should fit in 16 bits AFAIK */
10982 if (codepage > 0 && codepage < 65536)
10984 sprintf (charset, "CP%u", (guint) codepage);
10985 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
10989 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
10991 if (locale_format != NULL &&
10992 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
10995 /* As above but in opposite direction... */
10996 if (codepage > 0 && codepage < 65536)
10997 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
10999 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11003 if (date_str == NULL)
11004 date_str = g_strdup (_("Unknown"));
11006 g_free (locale_format);
11009 g_object_set (cell,
11011 "sensitive", sensitive,
11017 _gtk_file_chooser_default_new (void)
11019 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11023 location_set_user_text (GtkFileChooserDefault *impl,
11026 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11027 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11031 location_popup_handler (GtkFileChooserDefault *impl,
11034 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11036 GtkWidget *widget_to_focus;
11038 /* This will give us the location widgets back */
11039 switch (impl->operation_mode)
11041 case OPERATION_MODE_SEARCH:
11042 search_switch_to_browse_mode (impl);
11044 case OPERATION_MODE_RECENT:
11045 recent_switch_to_browse_mode (impl);
11047 case OPERATION_MODE_BROWSE:
11048 g_assert_not_reached ();
11052 if (impl->current_folder)
11053 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11055 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11056 widget_to_focus = impl->browse_files_tree_view;
11058 widget_to_focus = impl->location_entry;
11060 gtk_widget_grab_focus (widget_to_focus);
11064 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11065 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11067 LocationMode new_mode;
11071 /* since the user typed something, we unconditionally want to turn on the entry */
11072 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11074 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11075 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11076 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11077 new_mode = LOCATION_MODE_PATH_BAR;
11080 g_assert_not_reached ();
11084 location_mode_set (impl, new_mode, TRUE);
11085 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11088 location_set_user_text (impl, path);
11091 location_entry_set_initial_text (impl);
11092 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11096 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11097 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11099 gtk_widget_grab_focus (impl->location_entry);
11101 location_set_user_text (impl, path);
11104 g_assert_not_reached ();
11107 /* Handler for the "up-folder" keybinding signal */
11109 up_folder_handler (GtkFileChooserDefault *impl)
11111 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11114 /* Handler for the "down-folder" keybinding signal */
11116 down_folder_handler (GtkFileChooserDefault *impl)
11118 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11121 /* Switches to the shortcut in the specified index */
11123 switch_to_shortcut (GtkFileChooserDefault *impl,
11128 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11129 g_assert_not_reached ();
11131 shortcuts_activate_iter (impl, &iter);
11134 /* Handler for the "home-folder" keybinding signal */
11136 home_folder_handler (GtkFileChooserDefault *impl)
11138 if (impl->has_home)
11139 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11142 /* Handler for the "desktop-folder" keybinding signal */
11144 desktop_folder_handler (GtkFileChooserDefault *impl)
11146 if (impl->has_desktop)
11147 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11150 /* Handler for the "search-shortcut" keybinding signal */
11152 search_shortcut_handler (GtkFileChooserDefault *impl)
11154 if (impl->has_search)
11156 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11158 /* we want the entry widget to grab the focus the first
11159 * time, not the browse_files_tree_view widget.
11161 if (impl->search_entry)
11162 gtk_widget_grab_focus (impl->search_entry);
11166 /* Handler for the "recent-shortcut" keybinding signal */
11168 recent_shortcut_handler (GtkFileChooserDefault *impl)
11170 if (impl->has_recent)
11171 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11175 quick_bookmark_handler (GtkFileChooserDefault *impl,
11176 gint bookmark_index)
11181 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11184 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11186 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11187 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11190 gtk_tree_path_free (path);
11192 switch_to_shortcut (impl, bookmark_pos);
11196 show_hidden_handler (GtkFileChooserDefault *impl)
11198 g_object_set (impl,
11199 "show-hidden", !impl->show_hidden,
11204 /* Drag and drop interfaces */
11207 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11212 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11214 model->impl = NULL;
11217 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11219 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11222 ShortcutsPaneModelFilter *model;
11226 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11228 pos = *gtk_tree_path_get_indices (path);
11229 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11231 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11234 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11236 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11238 GtkSelectionData *selection_data)
11240 ShortcutsPaneModelFilter *model;
11242 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11249 /* Fill the GtkTreeDragSourceIface vtable */
11251 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11253 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11254 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11258 /* Fill the GtkTreeDragDestIface vtable */
11260 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11262 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11263 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11267 static GtkTreeModel *
11268 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11269 GtkTreeModel *child_model,
11272 ShortcutsPaneModelFilter *model;
11274 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11275 "child-model", child_model,
11276 "virtual-root", root,
11279 model->impl = impl;
11281 return GTK_TREE_MODEL (model);
11287 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11290 RecentModelSort *model;
11291 GtkTreeIter iter, child_iter;
11292 gboolean is_folder;
11294 model = RECENT_MODEL_SORT (drag_source);
11295 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11298 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11299 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11300 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11307 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11309 GtkSelectionData *selection_data)
11311 RecentModelSort *model;
11312 GtkTreeIter iter, child_iter;
11316 model = RECENT_MODEL_SORT (drag_source);
11317 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11320 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11321 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11322 RECENT_MODEL_COL_FILE, &file,
11324 g_assert (file != NULL);
11326 uris[0] = g_file_get_uri (file);
11329 gtk_selection_data_set_uris (selection_data, uris);
11337 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11339 iface->row_draggable = recent_model_sort_row_draggable;
11340 iface->drag_data_get = recent_model_sort_drag_data_get;
11344 _recent_model_sort_class_init (RecentModelSortClass *klass)
11350 _recent_model_sort_init (RecentModelSort *model)
11352 model->impl = NULL;
11355 static GtkTreeModel *
11356 recent_model_sort_new (GtkFileChooserDefault *impl,
11357 GtkTreeModel *child_model)
11359 RecentModelSort *model;
11361 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11362 "model", child_model,
11364 model->impl = impl;
11366 return GTK_TREE_MODEL (model);
11372 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11375 SearchModelSort *model;
11376 GtkTreeIter iter, child_iter;
11377 gboolean is_folder;
11379 model = SEARCH_MODEL_SORT (drag_source);
11380 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11383 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11384 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11385 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11392 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11394 GtkSelectionData *selection_data)
11396 SearchModelSort *model;
11397 GtkTreeIter iter, child_iter;
11401 model = SEARCH_MODEL_SORT (drag_source);
11402 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11405 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11406 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11407 RECENT_MODEL_COL_FILE, &file,
11409 g_assert (file != NULL);
11411 uris[0] = g_file_get_uri (file);
11414 gtk_selection_data_set_uris (selection_data, uris);
11422 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11424 iface->row_draggable = search_model_sort_row_draggable;
11425 iface->drag_data_get = search_model_sort_drag_data_get;
11429 _search_model_sort_class_init (SearchModelSortClass *klass)
11435 _search_model_sort_init (SearchModelSort *model)
11437 model->impl = NULL;
11440 static GtkTreeModel *
11441 search_model_sort_new (GtkFileChooserDefault *impl,
11442 GtkTreeModel *child_model)
11444 SearchModelSort *model;
11446 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11447 "model", child_model,
11449 model->impl = impl;
11451 return GTK_TREE_MODEL (model);