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),
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 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1156 if (impl->use_preview_label)
1158 if (!impl->preview_label)
1160 impl->preview_label = gtk_label_new (impl->preview_display_name);
1161 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1162 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1163 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1164 gtk_widget_show (impl->preview_label);
1169 if (impl->preview_label)
1171 gtk_widget_destroy (impl->preview_label);
1172 impl->preview_label = NULL;
1176 if (impl->preview_widget_active && impl->preview_widget)
1177 gtk_widget_show (impl->preview_box);
1179 gtk_widget_hide (impl->preview_box);
1181 g_signal_emit_by_name (impl, "default-size-changed");
1185 set_preview_widget (GtkFileChooserDefault *impl,
1186 GtkWidget *preview_widget)
1188 if (preview_widget == impl->preview_widget)
1191 if (impl->preview_widget)
1192 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1193 impl->preview_widget);
1195 impl->preview_widget = preview_widget;
1196 if (impl->preview_widget)
1198 gtk_widget_show (impl->preview_widget);
1199 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1200 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1201 impl->preview_widget,
1202 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1205 update_preview_widget_visibility (impl);
1208 /* Renders a "Search" icon at an appropriate size for a tree view */
1210 render_search_icon (GtkFileChooserDefault *impl)
1212 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1216 render_recent_icon (GtkFileChooserDefault *impl)
1218 GtkIconTheme *theme;
1221 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1222 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1224 theme = gtk_icon_theme_get_default ();
1226 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1232 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1238 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1239 struct ReloadIconsData
1241 GtkFileChooserDefault *impl;
1242 GtkTreeRowReference *row_ref;
1246 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1248 const GError *error,
1254 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1255 struct ReloadIconsData *data = user_data;
1257 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1260 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1262 if (cancelled || error)
1265 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1267 path = gtk_tree_row_reference_get_path (data->row_ref);
1268 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1269 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1270 SHORTCUTS_COL_PIXBUF, pixbuf,
1272 gtk_tree_path_free (path);
1275 g_object_unref (pixbuf);
1278 gtk_tree_row_reference_free (data->row_ref);
1279 g_object_unref (data->impl);
1282 g_object_unref (cancellable);
1286 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1291 profile_start ("start", NULL);
1293 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1296 for (l = impl->reload_icon_cancellables; l; l = l->next)
1298 GCancellable *cancellable = G_CANCELLABLE (l->data);
1299 g_cancellable_cancel (cancellable);
1301 g_slist_free (impl->reload_icon_cancellables);
1302 impl->reload_icon_cancellables = NULL;
1307 ShortcutType shortcut_type;
1308 gboolean pixbuf_visible;
1311 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1312 SHORTCUTS_COL_DATA, &data,
1313 SHORTCUTS_COL_TYPE, &shortcut_type,
1314 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1320 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1322 GtkFileSystemVolume *volume;
1325 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1326 impl->icon_size, NULL);
1328 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1330 if (g_file_is_native (G_FILE (data)))
1333 struct ReloadIconsData *info;
1334 GtkTreePath *tree_path;
1335 GCancellable *cancellable;
1339 info = g_new0 (struct ReloadIconsData, 1);
1340 info->impl = g_object_ref (impl);
1341 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1342 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1343 gtk_tree_path_free (tree_path);
1345 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1347 shortcuts_reload_icons_get_info_cb,
1349 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1353 GtkIconTheme *icon_theme;
1355 /* Don't call get_info for remote paths to avoid latency and
1357 * If we switch to a better bookmarks file format (XBEL), we
1358 * should use mime info to get a better icon.
1360 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1361 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1362 impl->icon_size, 0, NULL);
1365 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1367 pixbuf = render_search_icon (impl);
1369 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1371 pixbuf = render_recent_icon (impl);
1374 gtk_list_store_set (impl->shortcuts_model, &iter,
1375 SHORTCUTS_COL_PIXBUF, pixbuf,
1379 g_object_unref (pixbuf);
1383 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1387 profile_end ("end", NULL);
1391 shortcuts_find_folder (GtkFileChooserDefault *impl,
1394 GtkTreeSelection *selection;
1398 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1400 g_assert (folder != NULL);
1401 pos = shortcut_find_position (impl, folder);
1404 gtk_tree_selection_unselect_all (selection);
1408 path = gtk_tree_path_new_from_indices (pos, -1);
1409 gtk_tree_selection_select_path (selection, path);
1410 gtk_tree_path_free (path);
1413 /* If a shortcut corresponds to the current folder, selects it */
1415 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1417 shortcuts_find_folder (impl, impl->current_folder);
1420 /* Removes the specified number of rows from the shortcuts list */
1422 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1428 path = gtk_tree_path_new_from_indices (start_row, -1);
1430 for (; n_rows; n_rows--)
1434 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1435 g_assert_not_reached ();
1437 shortcuts_free_row_data (impl, &iter);
1438 gtk_list_store_remove (impl->shortcuts_model, &iter);
1441 gtk_tree_path_free (path);
1445 shortcuts_update_count (GtkFileChooserDefault *impl,
1446 ShortcutsIndex type,
1451 case SHORTCUTS_HOME:
1453 impl->has_home = FALSE;
1455 impl->has_home = TRUE;
1458 case SHORTCUTS_DESKTOP:
1460 impl->has_desktop = FALSE;
1462 impl->has_desktop = TRUE;
1465 case SHORTCUTS_VOLUMES:
1466 impl->num_volumes += value;
1469 case SHORTCUTS_SHORTCUTS:
1470 impl->num_shortcuts += value;
1473 case SHORTCUTS_BOOKMARKS:
1474 impl->num_bookmarks += value;
1477 case SHORTCUTS_CURRENT_FOLDER:
1479 impl->shortcuts_current_folder_active = FALSE;
1481 impl->shortcuts_current_folder_active = TRUE;
1490 struct ShortcutsInsertRequest
1492 GtkFileChooserDefault *impl;
1496 GtkTreeRowReference *row_ref;
1497 ShortcutsIndex type;
1503 get_file_info_finished (GCancellable *cancellable,
1505 const GError *error,
1509 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1513 GCancellable *model_cancellable;
1514 struct ShortcutsInsertRequest *request = data;
1516 path = gtk_tree_row_reference_get_path (request->row_ref);
1518 /* Handle doesn't exist anymore in the model */
1521 pos = gtk_tree_path_get_indices (path)[0];
1522 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1524 gtk_tree_path_free (path);
1526 /* validate cancellable, else goto out */
1527 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1528 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1530 if (cancellable != model_cancellable)
1533 /* set the cancellable to NULL in the model (we unref later on) */
1534 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1535 SHORTCUTS_COL_CANCELLABLE, NULL,
1543 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1544 shortcuts_update_count (request->impl, request->type, -1);
1546 if (request->type == SHORTCUTS_HOME)
1550 home = g_file_new_for_path (g_get_home_dir ());
1551 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1552 g_object_unref (home);
1554 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1556 /* Remove the current folder separator */
1557 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1558 shortcuts_remove_rows (request->impl, separator_pos, 1);
1564 if (!request->label_copy)
1565 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1566 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1567 request->impl->icon_size);
1569 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1570 SHORTCUTS_COL_PIXBUF, pixbuf,
1571 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1572 SHORTCUTS_COL_NAME, request->label_copy,
1573 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1574 SHORTCUTS_COL_REMOVABLE, request->removable,
1577 if (request->impl->shortcuts_pane_filter_model)
1578 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1580 if (request->impl->shortcuts_combo_filter_model)
1581 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1583 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1584 request->impl->save_folder_combo != NULL)
1586 /* The current folder is updated via _activate_iter(), don't
1587 * have save_folder_combo_changed_cb() call _activate_iter()
1590 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1591 G_CALLBACK (save_folder_combo_changed_cb),
1594 if (request->impl->has_search)
1597 if (request->impl->has_recent)
1600 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1601 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1602 G_CALLBACK (save_folder_combo_changed_cb),
1607 g_object_unref (pixbuf);
1610 g_object_unref (request->impl);
1611 g_object_unref (request->file);
1612 gtk_tree_row_reference_free (request->row_ref);
1613 g_free (request->label_copy);
1616 g_object_unref (cancellable);
1619 /* FIXME: GtkFileSystem needs a function to split a remote path
1620 * into hostname and path components, or maybe just have a
1621 * gtk_file_system_path_get_display_name().
1623 * This function is also used in gtkfilechooserbutton.c
1626 _gtk_file_chooser_label_for_file (GFile *file)
1628 const gchar *path, *start, *end, *p;
1629 gchar *uri, *host, *label;
1631 uri = g_file_get_uri (file);
1633 start = strstr (uri, "://");
1637 path = strchr (start, '/');
1642 end = uri + strlen (uri);
1646 /* strip username */
1647 p = strchr (start, '@');
1651 p = strchr (start, ':');
1655 host = g_strndup (start, end - start);
1657 /* Translators: the first string is a path and the second string
1658 * is a hostname. Nautilus and the panel contain the same string
1661 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1667 label = g_strdup (uri);
1675 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1676 * inserts a volume. A position of -1 indicates the end of the tree.
1679 shortcuts_insert_file (GtkFileChooserDefault *impl,
1681 ShortcutType shortcut_type,
1682 GtkFileSystemVolume *volume,
1686 ShortcutsIndex type)
1689 GdkPixbuf *pixbuf = NULL;
1690 gpointer data = NULL;
1692 GtkIconTheme *icon_theme;
1694 profile_start ("start shortcut", NULL);
1696 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1699 label_copy = _gtk_file_system_volume_get_display_name (volume);
1700 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1701 impl->icon_size, NULL);
1703 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1705 if (g_file_is_native (file))
1707 struct ShortcutsInsertRequest *request;
1708 GCancellable *cancellable;
1711 request = g_new0 (struct ShortcutsInsertRequest, 1);
1712 request->impl = g_object_ref (impl);
1713 request->file = g_object_ref (file);
1714 request->name_only = TRUE;
1715 request->removable = removable;
1717 request->type = type;
1719 request->label_copy = g_strdup (label);
1722 gtk_list_store_append (impl->shortcuts_model, &iter);
1724 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1726 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1727 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1728 gtk_tree_path_free (p);
1730 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1731 "standard::is-hidden,standard::display-name,standard::icon",
1732 get_file_info_finished, request);
1734 gtk_list_store_set (impl->shortcuts_model, &iter,
1735 SHORTCUTS_COL_DATA, g_object_ref (file),
1736 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1737 SHORTCUTS_COL_CANCELLABLE, cancellable,
1740 shortcuts_update_count (impl, type, 1);
1746 /* Don't call get_info for remote paths to avoid latency and
1749 data = g_object_ref (file);
1751 label_copy = g_strdup (label);
1753 label_copy = _gtk_file_chooser_label_for_file (file);
1755 /* If we switch to a better bookmarks file format (XBEL), we
1756 * should use mime info to get a better icon.
1758 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1759 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1760 impl->icon_size, 0, NULL);
1765 g_assert_not_reached ();
1771 gtk_list_store_append (impl->shortcuts_model, &iter);
1773 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1775 shortcuts_update_count (impl, type, 1);
1777 gtk_list_store_set (impl->shortcuts_model, &iter,
1778 SHORTCUTS_COL_PIXBUF, pixbuf,
1779 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1780 SHORTCUTS_COL_NAME, label_copy,
1781 SHORTCUTS_COL_DATA, data,
1782 SHORTCUTS_COL_TYPE, shortcut_type,
1783 SHORTCUTS_COL_REMOVABLE, removable,
1784 SHORTCUTS_COL_CANCELLABLE, NULL,
1787 if (impl->shortcuts_pane_filter_model)
1788 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1790 if (impl->shortcuts_combo_filter_model)
1791 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1793 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1795 /* The current folder is updated via _activate_iter(), don't
1796 * have save_folder_combo_changed_cb() call _activate_iter()
1799 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1801 if (impl->has_search)
1804 if (impl->has_recent)
1807 g_signal_handlers_block_by_func (impl->save_folder_combo,
1808 G_CALLBACK (save_folder_combo_changed_cb),
1811 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1812 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1813 G_CALLBACK (save_folder_combo_changed_cb),
1817 g_free (label_copy);
1820 g_object_unref (pixbuf);
1822 profile_end ("end", NULL);
1826 shortcuts_append_search (GtkFileChooserDefault *impl)
1831 pixbuf = render_search_icon (impl);
1833 gtk_list_store_append (impl->shortcuts_model, &iter);
1834 gtk_list_store_set (impl->shortcuts_model, &iter,
1835 SHORTCUTS_COL_PIXBUF, pixbuf,
1836 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1837 SHORTCUTS_COL_NAME, _("Search"),
1838 SHORTCUTS_COL_DATA, NULL,
1839 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1840 SHORTCUTS_COL_REMOVABLE, FALSE,
1844 g_object_unref (pixbuf);
1846 impl->has_search = TRUE;
1850 shortcuts_append_recent (GtkFileChooserDefault *impl)
1855 pixbuf = render_recent_icon (impl);
1857 gtk_list_store_append (impl->shortcuts_model, &iter);
1858 gtk_list_store_set (impl->shortcuts_model, &iter,
1859 SHORTCUTS_COL_PIXBUF, pixbuf,
1860 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1861 SHORTCUTS_COL_NAME, _("Recently Used"),
1862 SHORTCUTS_COL_DATA, NULL,
1863 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1864 SHORTCUTS_COL_REMOVABLE, FALSE,
1868 g_object_unref (pixbuf);
1870 impl->has_recent = TRUE;
1873 /* Appends an item for the user's home directory to the shortcuts model */
1875 shortcuts_append_home (GtkFileChooserDefault *impl)
1877 const char *home_path;
1880 profile_start ("start", NULL);
1882 home_path = g_get_home_dir ();
1883 if (home_path == NULL)
1885 profile_end ("end - no home directory!?", NULL);
1889 home = g_file_new_for_path (home_path);
1890 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1891 impl->has_home = TRUE;
1893 g_object_unref (home);
1895 profile_end ("end", NULL);
1898 /* Appends the ~/Desktop directory to the shortcuts model */
1900 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1905 profile_start ("start", NULL);
1907 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1908 file = g_file_new_for_path (name);
1909 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1910 impl->has_desktop = TRUE;
1912 /* We do not actually pop up an error dialog if there is no desktop directory
1913 * because some people may really not want to have one.
1916 g_object_unref (file);
1918 profile_end ("end", NULL);
1921 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1923 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1930 profile_start ("start", NULL);
1932 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1935 for (; bookmarks; bookmarks = bookmarks->next)
1939 file = bookmarks->data;
1941 if (impl->local_only && !g_file_is_native (file))
1944 if (shortcut_find_position (impl, file) != -1)
1947 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1949 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1953 profile_end ("end", NULL);
1955 return num_inserted;
1958 /* Returns the index for the corresponding item in the shortcuts bar */
1960 shortcuts_get_index (GtkFileChooserDefault *impl,
1961 ShortcutsIndex where)
1967 if (where == SHORTCUTS_SEARCH)
1970 n += impl->has_search ? 1 : 0;
1972 if (where == SHORTCUTS_RECENT)
1975 n += impl->has_recent ? 1 : 0;
1977 if (where == SHORTCUTS_RECENT_SEPARATOR)
1980 n += impl->has_recent ? 1 : 0;
1982 if (where == SHORTCUTS_HOME)
1985 n += impl->has_home ? 1 : 0;
1987 if (where == SHORTCUTS_DESKTOP)
1990 n += impl->has_desktop ? 1 : 0;
1992 if (where == SHORTCUTS_VOLUMES)
1995 n += impl->num_volumes;
1997 if (where == SHORTCUTS_SHORTCUTS)
2000 n += impl->num_shortcuts;
2002 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2005 /* If there are no bookmarks there won't be a separator */
2006 n += (impl->num_bookmarks > 0) ? 1 : 0;
2008 if (where == SHORTCUTS_BOOKMARKS)
2011 n += impl->num_bookmarks;
2013 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2018 if (where == SHORTCUTS_CURRENT_FOLDER)
2021 g_assert_not_reached ();
2028 /* Adds all the file system volumes to the shortcuts model */
2030 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2035 gboolean old_changing_folders;
2037 profile_start ("start", NULL);
2039 old_changing_folders = impl->changing_folder;
2040 impl->changing_folder = TRUE;
2042 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2043 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2044 impl->num_volumes = 0;
2046 list = _gtk_file_system_list_volumes (impl->file_system);
2050 for (l = list; l; l = l->next)
2052 GtkFileSystemVolume *volume;
2056 if (impl->local_only)
2058 if (_gtk_file_system_volume_is_mounted (volume))
2062 base_file = _gtk_file_system_volume_get_root (volume);
2063 if (base_file != NULL && !g_file_is_native (base_file))
2068 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2072 impl->num_volumes = n;
2073 g_slist_free (list);
2075 if (impl->shortcuts_pane_filter_model)
2076 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2078 if (impl->shortcuts_combo_filter_model)
2079 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2081 impl->changing_folder = old_changing_folders;
2083 profile_end ("end", NULL);
2086 /* Inserts a separator node in the shortcuts list */
2088 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2089 ShortcutsIndex where)
2093 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2094 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2095 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2097 gtk_list_store_insert (impl->shortcuts_model, &iter,
2098 shortcuts_get_index (impl, where));
2099 gtk_list_store_set (impl->shortcuts_model, &iter,
2100 SHORTCUTS_COL_PIXBUF, NULL,
2101 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2102 SHORTCUTS_COL_NAME, NULL,
2103 SHORTCUTS_COL_DATA, NULL,
2104 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2108 /* Updates the list of bookmarks */
2110 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2113 gboolean old_changing_folders;
2115 GFile *list_selected = NULL;
2116 GFile *combo_selected = NULL;
2117 ShortcutType shortcut_type;
2120 profile_start ("start", NULL);
2122 old_changing_folders = impl->changing_folder;
2123 impl->changing_folder = TRUE;
2125 if (shortcuts_get_selected (impl, &iter))
2127 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2129 SHORTCUTS_COL_DATA, &col_data,
2130 SHORTCUTS_COL_TYPE, &shortcut_type,
2133 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2134 list_selected = g_object_ref (col_data);
2137 if (impl->save_folder_combo &&
2138 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2141 GtkTreeIter child_iter;
2143 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2146 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2148 SHORTCUTS_COL_DATA, &col_data,
2149 SHORTCUTS_COL_TYPE, &shortcut_type,
2152 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2153 combo_selected = g_object_ref (col_data);
2156 if (impl->num_bookmarks > 0)
2157 shortcuts_remove_rows (impl,
2158 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2159 impl->num_bookmarks + 1);
2161 impl->num_bookmarks = 0;
2162 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2164 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2165 shortcuts_append_bookmarks (impl, bookmarks);
2166 g_slist_free (bookmarks);
2168 if (impl->num_bookmarks == 0)
2169 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2171 if (impl->shortcuts_pane_filter_model)
2172 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2174 if (impl->shortcuts_combo_filter_model)
2175 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2179 shortcuts_find_folder (impl, list_selected);
2180 g_object_unref (list_selected);
2187 pos = shortcut_find_position (impl, combo_selected);
2190 if (impl->has_search)
2193 if (impl->has_recent)
2196 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2199 g_object_unref (combo_selected);
2202 impl->changing_folder = old_changing_folders;
2204 profile_end ("end", NULL);
2207 /* Appends a separator and a row to the shortcuts list for the current folder */
2209 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2214 g_assert (!impl->shortcuts_current_folder_active);
2218 g_assert (impl->current_folder != NULL);
2220 pos = shortcut_find_position (impl, impl->current_folder);
2223 GtkFileSystemVolume *volume;
2228 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2232 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2234 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2236 base_file = _gtk_file_system_volume_get_root (volume);
2240 if (base_file && g_file_equal (base_file, impl->current_folder))
2241 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2243 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2246 g_object_unref (base_file);
2248 else if (impl->save_folder_combo != NULL)
2250 if (impl->has_search)
2253 if (impl->has_recent)
2254 pos -= 2; /* + separator */
2256 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2260 /* Updates the current folder row in the shortcuts model */
2262 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2266 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2268 if (impl->shortcuts_current_folder_active)
2270 shortcuts_remove_rows (impl, pos, 2);
2271 impl->shortcuts_current_folder_active = FALSE;
2274 shortcuts_add_current_folder (impl);
2277 /* Filter function used for the shortcuts filter model */
2279 shortcuts_pane_filter_cb (GtkTreeModel *model,
2283 GtkFileChooserDefault *impl;
2287 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2289 path = gtk_tree_model_get_path (model, iter);
2293 pos = *gtk_tree_path_get_indices (path);
2294 gtk_tree_path_free (path);
2296 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2299 /* Creates the list model for shortcuts */
2301 shortcuts_model_create (GtkFileChooserDefault *impl)
2303 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2304 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2305 GDK_TYPE_PIXBUF, /* pixbuf */
2306 G_TYPE_STRING, /* name */
2307 G_TYPE_POINTER, /* path or volume */
2308 G_TYPE_INT, /* ShortcutType */
2309 G_TYPE_BOOLEAN, /* removable */
2310 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2311 G_TYPE_POINTER); /* GCancellable */
2313 if (search_is_possible (impl))
2315 shortcuts_append_search (impl);
2318 if (impl->recent_manager)
2320 shortcuts_append_recent (impl);
2321 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2324 if (impl->file_system)
2326 shortcuts_append_home (impl);
2327 shortcuts_append_desktop (impl);
2328 shortcuts_add_volumes (impl);
2331 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2332 GTK_TREE_MODEL (impl->shortcuts_model),
2335 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2336 shortcuts_pane_filter_cb,
2341 /* Callback used when the "New Folder" button is clicked */
2343 new_folder_button_clicked (GtkButton *button,
2344 GtkFileChooserDefault *impl)
2349 if (!impl->browse_files_model)
2350 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2352 /* Prevent button from being clicked twice */
2353 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2355 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2357 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2358 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2359 path, impl->list_name_column,
2362 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2363 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2365 impl->list_name_column,
2368 gtk_tree_path_free (path);
2371 /* Idle handler for creating a new folder after editing its name cell, or for
2372 * canceling the editing.
2375 edited_idle_cb (GtkFileChooserDefault *impl)
2377 GDK_THREADS_ENTER ();
2379 g_source_destroy (impl->edited_idle);
2380 impl->edited_idle = NULL;
2382 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2383 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2385 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2387 if (impl->edited_new_text) /* not cancelled? */
2389 GError *error = NULL;
2392 file = g_file_get_child_for_display_name (impl->current_folder,
2393 impl->edited_new_text,
2397 GError *error = NULL;
2399 if (!g_file_make_directory (file, NULL, &error))
2400 change_folder_and_display_error (impl, file, FALSE);
2402 error_creating_folder_dialog (impl, file, error);
2404 g_object_unref (file);
2407 error_creating_folder_dialog (impl, file, error);
2409 g_free (impl->edited_new_text);
2410 impl->edited_new_text = NULL;
2413 GDK_THREADS_LEAVE ();
2419 queue_edited_idle (GtkFileChooserDefault *impl,
2420 const gchar *new_text)
2422 /* We create the folder in an idle handler so that we don't modify the tree
2426 if (!impl->edited_idle)
2428 impl->edited_idle = g_idle_source_new ();
2429 g_source_set_closure (impl->edited_idle,
2430 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2432 g_source_attach (impl->edited_idle, NULL);
2435 g_free (impl->edited_new_text);
2436 impl->edited_new_text = g_strdup (new_text);
2439 /* Callback used from the text cell renderer when the new folder is named */
2441 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2443 const gchar *new_text,
2444 GtkFileChooserDefault *impl)
2446 /* work around bug #154921 */
2447 g_object_set (cell_renderer_text,
2448 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2449 queue_edited_idle (impl, new_text);
2452 /* Callback used from the text cell renderer when the new folder edition gets
2456 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2457 GtkFileChooserDefault *impl)
2459 /* work around bug #154921 */
2460 g_object_set (cell_renderer_text,
2461 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2462 queue_edited_idle (impl, NULL);
2465 /* Creates the widgets for the filter combo box */
2467 filter_create (GtkFileChooserDefault *impl)
2469 impl->filter_combo = gtk_combo_box_new_text ();
2470 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2472 g_signal_connect (impl->filter_combo, "changed",
2473 G_CALLBACK (filter_combo_changed), impl);
2475 gtk_widget_set_tooltip_text (impl->filter_combo,
2476 _("Select which types of files are shown"));
2478 return impl->filter_combo;
2482 button_new (GtkFileChooserDefault *impl,
2484 const char *stock_id,
2492 button = gtk_button_new_with_mnemonic (text);
2493 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2494 gtk_button_set_image (GTK_BUTTON (button), image);
2496 gtk_widget_set_sensitive (button, sensitive);
2497 g_signal_connect (button, "clicked", callback, impl);
2500 gtk_widget_show (button);
2505 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2507 shortcut_find_position (GtkFileChooserDefault *impl,
2512 int current_folder_separator_idx;
2514 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2517 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2520 /* FIXME: is this still needed? */
2521 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2525 for (i = 0; i < current_folder_separator_idx; i++)
2528 ShortcutType shortcut_type;
2530 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2531 SHORTCUTS_COL_DATA, &col_data,
2532 SHORTCUTS_COL_TYPE, &shortcut_type,
2537 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2539 GtkFileSystemVolume *volume;
2544 base_file = _gtk_file_system_volume_get_root (volume);
2546 exists = base_file && g_file_equal (file, base_file);
2549 g_object_unref (base_file);
2554 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2558 model_file = col_data;
2560 if (model_file && g_file_equal (model_file, file))
2565 if (i < current_folder_separator_idx - 1)
2567 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2568 g_assert_not_reached ();
2575 /* Tries to add a bookmark from a path name */
2577 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2583 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2585 if (shortcut_find_position (impl, file) != -1)
2589 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2591 error_adding_bookmark_dialog (impl, file, error);
2599 add_bookmark_foreach_cb (GtkTreeModel *model,
2604 GtkFileChooserDefault *impl;
2605 GtkFileSystemModel *fs_model;
2606 GtkTreeIter child_iter;
2609 impl = (GtkFileChooserDefault *) data;
2611 switch (impl->operation_mode)
2613 case OPERATION_MODE_BROWSE:
2614 fs_model = impl->browse_files_model;
2615 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2616 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2619 case OPERATION_MODE_SEARCH:
2620 search_get_valid_child_iter (impl, &child_iter, iter);
2621 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2622 SEARCH_MODEL_COL_FILE, &file,
2626 case OPERATION_MODE_RECENT:
2627 recent_get_valid_child_iter (impl, &child_iter, iter);
2628 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2629 RECENT_MODEL_COL_FILE, &file,
2634 shortcuts_add_bookmark_from_file (impl, file, -1);
2637 /* Adds a bookmark from the currently selected item in the file list */
2639 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2641 GtkTreeSelection *selection;
2643 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2645 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2646 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2648 gtk_tree_selection_selected_foreach (selection,
2649 add_bookmark_foreach_cb,
2653 /* Callback used when the "Add bookmark" button is clicked */
2655 add_bookmark_button_clicked_cb (GtkButton *button,
2656 GtkFileChooserDefault *impl)
2658 bookmarks_add_selected_folder (impl);
2661 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2662 * returns FALSE if no shortcut is selected.
2665 shortcuts_get_selected (GtkFileChooserDefault *impl,
2668 GtkTreeSelection *selection;
2669 GtkTreeIter parent_iter;
2671 if (!impl->browse_shortcuts_tree_view)
2674 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2676 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2679 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2685 /* Removes the selected bookmarks */
2687 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2695 if (!shortcuts_get_selected (impl, &iter))
2698 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2699 SHORTCUTS_COL_DATA, &col_data,
2700 SHORTCUTS_COL_REMOVABLE, &removable,
2706 g_assert (col_data != NULL);
2711 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2712 error_removing_bookmark_dialog (impl, file, error);
2715 /* Callback used when the "Remove bookmark" button is clicked */
2717 remove_bookmark_button_clicked_cb (GtkButton *button,
2718 GtkFileChooserDefault *impl)
2720 remove_selected_bookmarks (impl);
2723 struct selection_check_closure {
2724 GtkFileChooserDefault *impl;
2727 gboolean all_folders;
2730 /* Used from gtk_tree_selection_selected_foreach() */
2732 selection_check_foreach_cb (GtkTreeModel *model,
2737 struct selection_check_closure *closure;
2738 GtkTreeIter child_iter;
2743 closure->num_selected++;
2745 switch (closure->impl->operation_mode)
2747 case OPERATION_MODE_BROWSE:
2748 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2749 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2750 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2753 case OPERATION_MODE_SEARCH:
2754 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2755 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2756 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2760 case OPERATION_MODE_RECENT:
2761 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2762 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2763 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2768 closure->all_folders = closure->all_folders && is_folder;
2769 closure->all_files = closure->all_files && !is_folder;
2772 /* Checks whether the selected items in the file list are all files or all folders */
2774 selection_check (GtkFileChooserDefault *impl,
2776 gboolean *all_files,
2777 gboolean *all_folders)
2779 struct selection_check_closure closure;
2780 GtkTreeSelection *selection;
2782 closure.impl = impl;
2783 closure.num_selected = 0;
2784 closure.all_files = TRUE;
2785 closure.all_folders = TRUE;
2787 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2788 gtk_tree_selection_selected_foreach (selection,
2789 selection_check_foreach_cb,
2792 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2795 *num_selected = closure.num_selected;
2798 *all_files = closure.all_files;
2801 *all_folders = closure.all_folders;
2804 struct get_selected_file_closure {
2805 GtkFileChooserDefault *impl;
2810 get_selected_file_foreach_cb (GtkTreeModel *model,
2815 struct get_selected_file_closure *closure;
2816 GtkTreeIter child_iter;
2820 switch (closure->impl->operation_mode)
2822 case OPERATION_MODE_BROWSE:
2823 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2824 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2827 case OPERATION_MODE_SEARCH:
2828 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2829 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2830 SEARCH_MODEL_COL_FILE, &closure->file,
2834 case OPERATION_MODE_RECENT:
2835 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2836 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2837 RECENT_MODEL_COL_FILE, &closure->file,
2843 /* Returns a selected path from the file list */
2845 get_selected_file (GtkFileChooserDefault *impl)
2847 struct get_selected_file_closure closure;
2848 GtkTreeSelection *selection;
2850 closure.impl = impl;
2851 closure.file = NULL;
2853 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2854 gtk_tree_selection_selected_foreach (selection,
2855 get_selected_file_foreach_cb,
2858 return closure.file;
2862 GtkFileChooserDefault *impl;
2864 } UpdateTooltipData;
2867 update_tooltip (GtkTreeModel *model,
2872 UpdateTooltipData *udata = data;
2873 GtkTreeIter child_iter;
2876 if (udata->tip == NULL)
2878 const gchar *display_name;
2880 switch (udata->impl->operation_mode)
2882 case OPERATION_MODE_BROWSE:
2883 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2886 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2887 display_name = g_file_info_get_display_name (info);
2890 case OPERATION_MODE_SEARCH:
2891 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2892 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2893 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2897 case OPERATION_MODE_RECENT:
2898 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2899 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2900 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2905 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2911 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2912 * if there are no selected items *and* the current folder is not in the
2913 * bookmarks list. De-sensitize the button otherwise.
2916 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2919 gboolean all_folders;
2923 selection_check (impl, &num_selected, NULL, &all_folders);
2925 if (num_selected == 0)
2926 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2927 else if (num_selected == 1)
2931 file = get_selected_file (impl);
2932 active = all_folders && (shortcut_find_position (impl, file) == -1);
2935 active = all_folders;
2937 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2939 if (impl->browse_files_popup_menu_add_shortcut_item)
2940 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2941 (num_selected == 0) ? FALSE : active);
2945 if (num_selected == 0)
2946 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2947 else if (num_selected > 1)
2948 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2951 GtkTreeSelection *selection;
2952 UpdateTooltipData data;
2954 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2957 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2961 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2966 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2967 * bookmark row is selected in the shortcuts tree.
2970 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2973 gboolean removable = FALSE;
2976 if (shortcuts_get_selected (impl, &iter))
2977 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2978 SHORTCUTS_COL_REMOVABLE, &removable,
2979 SHORTCUTS_COL_NAME, &name,
2982 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2988 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2989 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2997 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3000 gboolean removable = FALSE;
3002 if (impl->browse_shortcuts_popup_menu == NULL)
3005 if (shortcuts_get_selected (impl, &iter))
3006 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3007 SHORTCUTS_COL_REMOVABLE, &removable,
3010 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3011 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3014 /* GtkWidget::drag-begin handler for the shortcuts list. */
3016 shortcuts_drag_begin_cb (GtkWidget *widget,
3017 GdkDragContext *context,
3018 GtkFileChooserDefault *impl)
3021 impl->shortcuts_drag_context = g_object_ref (context);
3026 /* Removes the idle handler for outside drags */
3028 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3030 if (!impl->shortcuts_drag_outside_idle)
3033 g_source_destroy (impl->shortcuts_drag_outside_idle);
3034 impl->shortcuts_drag_outside_idle = NULL;
3038 /* GtkWidget::drag-end handler for the shortcuts list. */
3040 shortcuts_drag_end_cb (GtkWidget *widget,
3041 GdkDragContext *context,
3042 GtkFileChooserDefault *impl)
3045 g_object_unref (impl->shortcuts_drag_context);
3047 shortcuts_cancel_drag_outside_idle (impl);
3049 if (!impl->shortcuts_drag_outside)
3052 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3054 impl->shortcuts_drag_outside = FALSE;
3058 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3060 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3061 GdkDragContext *context,
3062 GtkFileChooserDefault *impl)
3064 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3068 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3072 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3075 GtkTreeView *tree_view;
3078 GdkPixmap *row_pixmap;
3083 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3085 /* Find the selected path and get its drag pixmap */
3087 if (!shortcuts_get_selected (impl, &iter))
3088 g_assert_not_reached ();
3090 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3092 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3093 gtk_tree_path_free (path);
3102 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3108 GdkPixmap *composite;
3109 int row_pixmap_width, row_pixmap_height;
3110 int pixbuf_width, pixbuf_height;
3111 int composite_width, composite_height;
3112 int pixbuf_x, pixbuf_y;
3113 GdkGC *gc, *mask_gc;
3115 GdkBitmap *pixbuf_mask;
3117 /* Create pixmap and mask for composite image */
3119 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3120 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3121 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3123 composite_width = MAX (row_pixmap_width, pixbuf_width);
3124 composite_height = MAX (row_pixmap_height, pixbuf_height);
3126 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3128 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3131 pixbuf_x = composite_width - pixbuf_width;
3133 pixbuf_y = (composite_height - pixbuf_height) / 2;
3135 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3136 gc = gdk_gc_new (composite);
3138 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3139 mask_gc = gdk_gc_new (mask);
3141 gdk_gc_set_foreground (mask_gc, &color);
3142 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3145 color.green = 0xffff;
3146 color.blue = 0xffff;
3147 gdk_gc_set_rgb_fg_color (gc, &color);
3148 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3150 /* Composite the row pixmap and the pixbuf */
3152 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3154 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3155 NULL, &pixbuf_mask, 128);
3156 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3159 pixbuf_width, pixbuf_height);
3160 g_object_unref (pixbuf_mask);
3162 gdk_draw_drawable (composite, gc, row_pixmap,
3165 row_pixmap_width, row_pixmap_height);
3167 gdk_gc_set_foreground (mask_gc, &color);
3168 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3170 gdk_draw_pixbuf (composite, gc, pixbuf,
3173 pixbuf_width, pixbuf_height,
3177 g_object_unref (pixbuf);
3178 g_object_unref (row_pixmap);
3180 row_pixmap = composite;
3184 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3186 gtk_tree_view_get_path_at_pos (tree_view,
3187 tree_view->priv->press_start_x,
3188 tree_view->priv->press_start_y,
3194 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3195 gdk_drawable_get_colormap (row_pixmap),
3198 tree_view->priv->press_start_x + 1,
3199 row_pixmap_y + cell_y + 1);
3201 g_object_unref (row_pixmap);
3203 g_object_unref (mask);
3206 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3207 * handler so that we can tell apart the drag_leave event that comes right
3208 * before a drag_drop, from a normal drag_leave. We don't want to set the
3209 * cursor nor the flag in the latter case.
3212 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3214 GDK_THREADS_ENTER ();
3216 shortcuts_drag_set_delete_cursor (impl, TRUE);
3217 impl->shortcuts_drag_outside = TRUE;
3219 shortcuts_cancel_drag_outside_idle (impl);
3221 GDK_THREADS_LEAVE ();
3227 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3231 shortcuts_drag_leave_cb (GtkWidget *widget,
3232 GdkDragContext *context,
3234 GtkFileChooserDefault *impl)
3237 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3239 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3240 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3241 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3243 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3247 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3249 GTK_TREE_VIEW_DROP_BEFORE);
3251 g_signal_stop_emission_by_name (widget, "drag_leave");
3254 /* Computes the appropriate row and position for dropping */
3256 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3260 GtkTreeViewDropPosition *pos)
3262 GtkTreeView *tree_view;
3263 GtkTreeViewColumn *column;
3267 int bookmarks_index;
3269 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3271 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3273 if (!gtk_tree_view_get_path_at_pos (tree_view,
3275 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3281 row = bookmarks_index + impl->num_bookmarks - 1;
3282 *path = gtk_tree_path_new_from_indices (row, -1);
3283 *pos = GTK_TREE_VIEW_DROP_AFTER;
3287 row = *gtk_tree_path_get_indices (*path);
3288 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3289 gtk_tree_path_free (*path);
3291 if (row < bookmarks_index)
3293 row = bookmarks_index;
3294 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3296 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3298 row = bookmarks_index + impl->num_bookmarks - 1;
3299 *pos = GTK_TREE_VIEW_DROP_AFTER;
3303 if (cell_y < cell.height / 2)
3304 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3306 *pos = GTK_TREE_VIEW_DROP_AFTER;
3309 *path = gtk_tree_path_new_from_indices (row, -1);
3312 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3313 * implement the destination side of DnD by hand, due to limitations in
3314 * GtkTreeView's DnD API.
3317 shortcuts_drag_motion_cb (GtkWidget *widget,
3318 GdkDragContext *context,
3322 GtkFileChooserDefault *impl)
3325 GtkTreeViewDropPosition pos;
3326 GdkDragAction action;
3329 if (gtk_drag_get_source_widget (context) == widget)
3331 shortcuts_cancel_drag_outside_idle (impl);
3333 if (impl->shortcuts_drag_outside)
3335 shortcuts_drag_set_delete_cursor (impl, FALSE);
3336 impl->shortcuts_drag_outside = FALSE;
3341 if (context->suggested_action == GDK_ACTION_COPY ||
3342 (context->actions & GDK_ACTION_COPY) != 0)
3343 action = GDK_ACTION_COPY;
3344 else if (context->suggested_action == GDK_ACTION_MOVE ||
3345 (context->actions & GDK_ACTION_MOVE) != 0)
3346 action = GDK_ACTION_MOVE;
3353 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3354 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3355 gtk_tree_path_free (path);
3359 g_signal_stop_emission_by_name (widget, "drag_motion");
3363 gdk_drag_status (context, action, time_);
3370 /* GtkWidget::drag-drop handler for the shortcuts list. */
3372 shortcuts_drag_drop_cb (GtkWidget *widget,
3373 GdkDragContext *context,
3377 GtkFileChooserDefault *impl)
3380 shortcuts_cancel_drag_outside_idle (impl);
3383 g_signal_stop_emission_by_name (widget, "drag_drop");
3387 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3389 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3390 GtkSelectionData *selection_data,
3396 uris = gtk_selection_data_get_uris (selection_data);
3400 for (i = 0; uris[i]; i++)
3406 file = g_file_new_for_uri (uri);
3408 if (shortcuts_add_bookmark_from_file (impl, file, position))
3411 g_object_unref (file);
3417 /* Reorders the selected bookmark to the specified position */
3419 shortcuts_reorder (GtkFileChooserDefault *impl,
3424 ShortcutType shortcut_type;
3427 int bookmarks_index;
3432 /* Get the selected path */
3434 if (!shortcuts_get_selected (impl, &iter))
3435 g_assert_not_reached ();
3437 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3438 old_position = *gtk_tree_path_get_indices (path);
3439 gtk_tree_path_free (path);
3441 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3442 old_position -= bookmarks_index;
3443 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3445 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3446 SHORTCUTS_COL_NAME, &name,
3447 SHORTCUTS_COL_DATA, &col_data,
3448 SHORTCUTS_COL_TYPE, &shortcut_type,
3450 g_assert (col_data != NULL);
3451 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3454 g_object_ref (file); /* removal below will free file, so we need a new ref */
3456 /* Remove the path from the old position and insert it in the new one */
3458 if (new_position > old_position)
3461 if (old_position == new_position)
3465 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3467 shortcuts_add_bookmark_from_file (impl, file, new_position);
3468 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3471 error_adding_bookmark_dialog (impl, file, error);
3475 g_object_unref (file);
3478 /* Callback used when we get the drag data for the bookmarks list. We add the
3479 * received URIs as bookmarks if they are folders.
3482 shortcuts_drag_data_received_cb (GtkWidget *widget,
3483 GdkDragContext *context,
3486 GtkSelectionData *selection_data,
3491 GtkFileChooserDefault *impl;
3492 GtkTreePath *tree_path;
3493 GtkTreeViewDropPosition tree_pos;
3495 int bookmarks_index;
3497 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3499 /* Compute position */
3501 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3503 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3504 position = *gtk_tree_path_get_indices (tree_path);
3505 gtk_tree_path_free (tree_path);
3507 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3510 g_assert (position >= bookmarks_index);
3511 position -= bookmarks_index;
3513 if (gtk_targets_include_uri (&selection_data->target, 1))
3514 shortcuts_drop_uris (impl, selection_data, position);
3515 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3516 shortcuts_reorder (impl, position);
3518 g_signal_stop_emission_by_name (widget, "drag_data_received");
3521 /* Callback used to display a tooltip in the shortcuts tree */
3523 shortcuts_query_tooltip_cb (GtkWidget *widget,
3526 gboolean keyboard_mode,
3527 GtkTooltip *tooltip,
3528 GtkFileChooserDefault *impl)
3530 GtkTreeModel *model;
3533 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3541 ShortcutType shortcut_type;
3543 gtk_tree_model_get (model, &iter,
3544 SHORTCUTS_COL_DATA, &col_data,
3545 SHORTCUTS_COL_TYPE, &shortcut_type,
3548 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3550 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3554 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3559 file = G_FILE (col_data);
3560 parse_name = g_file_get_parse_name (file);
3562 gtk_tooltip_set_text (tooltip, parse_name);
3564 g_free (parse_name);
3568 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3572 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3582 /* Callback used when the selection in the shortcuts tree changes */
3584 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3585 GtkFileChooserDefault *impl)
3588 GtkTreeIter child_iter;
3590 bookmarks_check_remove_sensitivity (impl);
3591 shortcuts_check_popup_sensitivity (impl);
3593 if (impl->changing_folder)
3596 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3598 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3601 shortcuts_activate_iter (impl, &child_iter);
3606 shortcuts_row_separator_func (GtkTreeModel *model,
3610 ShortcutType shortcut_type;
3612 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3614 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3617 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3618 * keypresses before the TreeView gets them.
3621 tree_view_keybinding_cb (GtkWidget *tree_view,
3623 GtkFileChooserDefault *impl)
3625 if ((event->keyval == GDK_slash
3626 || event->keyval == GDK_KP_Divide
3628 || event->keyval == GDK_asciitilde
3630 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3632 location_popup_handler (impl, event->string);
3639 /* Callback used when the file list's popup menu is detached */
3641 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3644 GtkFileChooserDefault *impl;
3646 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3647 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3649 impl->browse_shortcuts_popup_menu = NULL;
3650 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3651 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3655 remove_shortcut_cb (GtkMenuItem *item,
3656 GtkFileChooserDefault *impl)
3658 remove_selected_bookmarks (impl);
3661 /* Rename the selected bookmark */
3663 rename_selected_bookmark (GtkFileChooserDefault *impl)
3667 GtkTreeViewColumn *column;
3668 GtkCellRenderer *cell;
3671 if (shortcuts_get_selected (impl, &iter))
3673 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3674 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3675 renderers = gtk_tree_view_column_get_cell_renderers (column);
3676 cell = g_list_nth_data (renderers, 1);
3677 g_list_free (renderers);
3678 g_object_set (cell, "editable", TRUE, NULL);
3679 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3680 path, column, cell, TRUE);
3681 gtk_tree_path_free (path);
3686 rename_shortcut_cb (GtkMenuItem *item,
3687 GtkFileChooserDefault *impl)
3689 rename_selected_bookmark (impl);
3692 /* Constructs the popup menu for the file list if needed */
3694 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3698 if (impl->browse_shortcuts_popup_menu)
3701 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3702 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3703 impl->browse_shortcuts_tree_view,
3704 shortcuts_popup_menu_detach_cb);
3706 item = gtk_image_menu_item_new_with_label (_("Remove"));
3707 impl->browse_shortcuts_popup_menu_remove_item = item;
3708 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3709 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3710 g_signal_connect (item, "activate",
3711 G_CALLBACK (remove_shortcut_cb), impl);
3712 gtk_widget_show (item);
3713 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3715 item = gtk_menu_item_new_with_label (_("Rename..."));
3716 impl->browse_shortcuts_popup_menu_rename_item = item;
3717 g_signal_connect (item, "activate",
3718 G_CALLBACK (rename_shortcut_cb), impl);
3719 gtk_widget_show (item);
3720 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3724 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3726 shortcuts_build_popup_menu (impl);
3727 shortcuts_check_popup_sensitivity (impl);
3731 popup_position_func (GtkMenu *menu,
3735 gpointer user_data);
3738 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3739 GdkEventButton *event)
3741 shortcuts_update_popup_menu (impl);
3743 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3744 NULL, NULL, NULL, NULL,
3745 event->button, event->time);
3748 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3750 popup_position_func, impl->browse_shortcuts_tree_view,
3751 0, GDK_CURRENT_TIME);
3752 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3757 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3759 shortcuts_popup_menu_cb (GtkWidget *widget,
3760 GtkFileChooserDefault *impl)
3762 shortcuts_popup_menu (impl, NULL);
3766 /* Callback used when a button is pressed on the shortcuts list.
3767 * We trap button 3 to bring up a popup menu.
3770 shortcuts_button_press_event_cb (GtkWidget *widget,
3771 GdkEventButton *event,
3772 GtkFileChooserDefault *impl)
3774 static gboolean in_press = FALSE;
3780 if (event->button != 3)
3784 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3790 shortcuts_popup_menu (impl, event);
3795 shortcuts_edited (GtkCellRenderer *cell,
3798 GtkFileChooserDefault *impl)
3804 g_object_set (cell, "editable", FALSE, NULL);
3806 path = gtk_tree_path_new_from_string (path_string);
3807 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3808 g_assert_not_reached ();
3810 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3811 SHORTCUTS_COL_DATA, &shortcut,
3813 gtk_tree_path_free (path);
3815 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3819 shortcuts_editing_canceled (GtkCellRenderer *cell,
3820 GtkFileChooserDefault *impl)
3822 g_object_set (cell, "editable", FALSE, NULL);
3825 /* Creates the widgets for the shortcuts and bookmarks tree */
3827 shortcuts_list_create (GtkFileChooserDefault *impl)
3830 GtkTreeSelection *selection;
3831 GtkTreeViewColumn *column;
3832 GtkCellRenderer *renderer;
3834 /* Target types for dragging a row to/from the shortcuts list */
3835 const GtkTargetEntry tree_model_row_targets[] = {
3836 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3839 /* Scrolled window */
3841 swin = gtk_scrolled_window_new (NULL, NULL);
3842 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3843 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3844 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3846 gtk_widget_show (swin);
3850 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3851 #ifdef PROFILE_FILE_CHOOSER
3852 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3854 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3855 G_CALLBACK (tree_view_keybinding_cb), impl);
3856 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3857 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3858 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3859 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3860 /* Accessible object name for the file chooser's shortcuts pane */
3861 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3863 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3865 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3867 tree_model_row_targets,
3868 G_N_ELEMENTS (tree_model_row_targets),
3871 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3872 GTK_DEST_DEFAULT_ALL,
3873 tree_model_row_targets,
3874 G_N_ELEMENTS (tree_model_row_targets),
3875 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3876 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3878 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3879 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3880 gtk_tree_selection_set_select_function (selection,
3881 shortcuts_select_func,
3884 g_signal_connect (selection, "changed",
3885 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3887 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3888 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3890 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3891 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3892 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3893 G_CALLBACK (shortcuts_drag_end_cb), impl);
3894 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3895 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3897 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3898 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3899 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3900 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3901 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3902 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3903 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3904 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3906 /* Support tooltips */
3907 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3908 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3909 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3911 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3912 gtk_widget_show (impl->browse_shortcuts_tree_view);
3916 column = gtk_tree_view_column_new ();
3917 /* Column header for the file chooser's shortcuts pane */
3918 gtk_tree_view_column_set_title (column, _("_Places"));
3920 renderer = gtk_cell_renderer_pixbuf_new ();
3921 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3922 gtk_tree_view_column_set_attributes (column, renderer,
3923 "pixbuf", SHORTCUTS_COL_PIXBUF,
3924 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3927 renderer = gtk_cell_renderer_text_new ();
3928 g_signal_connect (renderer, "edited",
3929 G_CALLBACK (shortcuts_edited), impl);
3930 g_signal_connect (renderer, "editing-canceled",
3931 G_CALLBACK (shortcuts_editing_canceled), impl);
3932 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3933 gtk_tree_view_column_set_attributes (column, renderer,
3934 "text", SHORTCUTS_COL_NAME,
3937 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3938 shortcuts_row_separator_func,
3941 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3946 /* Creates the widgets for the shortcuts/bookmarks pane */
3948 shortcuts_pane_create (GtkFileChooserDefault *impl,
3949 GtkSizeGroup *size_group)
3955 vbox = gtk_vbox_new (FALSE, 6);
3956 gtk_widget_show (vbox);
3958 /* Shortcuts tree */
3960 widget = shortcuts_list_create (impl);
3961 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3963 /* Box for buttons */
3965 hbox = gtk_hbox_new (TRUE, 6);
3966 gtk_size_group_add_widget (size_group, hbox);
3967 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3968 gtk_widget_show (hbox);
3970 /* Add bookmark button */
3972 impl->browse_shortcuts_add_button = button_new (impl,
3977 G_CALLBACK (add_bookmark_button_clicked_cb));
3978 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3979 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3980 _("Add the selected folder to the Bookmarks"));
3982 /* Remove bookmark button */
3984 impl->browse_shortcuts_remove_button = button_new (impl,
3989 G_CALLBACK (remove_bookmark_button_clicked_cb));
3990 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3991 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3992 _("Remove the selected bookmark"));
3997 /* Handles key press events on the file list, so that we can trap Enter to
3998 * activate the default button on our own. Also, checks to see if '/' has been
3999 * pressed. See comment by tree_view_keybinding_cb() for more details.
4002 trap_activate_cb (GtkWidget *widget,
4006 GtkFileChooserDefault *impl;
4009 impl = (GtkFileChooserDefault *) data;
4011 modifiers = gtk_accelerator_get_default_mod_mask ();
4013 if ((event->keyval == GDK_slash
4014 || event->keyval == GDK_KP_Divide
4016 || event->keyval == GDK_asciitilde
4018 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4020 location_popup_handler (impl, event->string);
4024 if ((event->keyval == GDK_Return
4025 || event->keyval == GDK_ISO_Enter
4026 || event->keyval == GDK_KP_Enter
4027 || event->keyval == GDK_space
4028 || event->keyval == GDK_KP_Space)
4029 && ((event->state & modifiers) == 0)
4030 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4031 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4035 window = get_toplevel (widget);
4037 && widget != window->default_widget
4038 && !(widget == window->focus_widget &&
4039 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4041 gtk_window_activate_default (window);
4049 /* Callback used when the file list's popup menu is detached */
4051 popup_menu_detach_cb (GtkWidget *attach_widget,
4054 GtkFileChooserDefault *impl;
4056 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4057 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4059 impl->browse_files_popup_menu = NULL;
4060 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4061 impl->browse_files_popup_menu_hidden_files_item = NULL;
4064 /* Callback used when the "Add to Bookmarks" menu item is activated */
4066 add_to_shortcuts_cb (GtkMenuItem *item,
4067 GtkFileChooserDefault *impl)
4069 bookmarks_add_selected_folder (impl);
4072 /* Callback used when the "Show Hidden Files" menu item is toggled */
4074 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4075 GtkFileChooserDefault *impl)
4078 "show-hidden", gtk_check_menu_item_get_active (item),
4082 /* Shows an error dialog about not being able to select a dragged file */
4084 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4089 _("Could not select file"),
4094 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4099 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4101 for (i = 1; uris[i]; i++)
4104 GError *error = NULL;
4107 file = g_file_new_for_uri (uri);
4109 gtk_file_chooser_default_select_file (chooser, file, &error);
4111 error_selecting_dragged_file_dialog (impl, file, error);
4113 g_object_unref (file);
4117 struct FileListDragData
4119 GtkFileChooserDefault *impl;
4125 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4127 const GError *error,
4130 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4131 struct FileListDragData *data = user_data;
4132 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4134 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4137 data->impl->file_list_drag_data_received_cancellable = NULL;
4139 if (cancelled || error)
4142 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4143 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4144 data->uris[1] == 0 && !error &&
4145 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4146 change_folder_and_display_error (data->impl, data->file, FALSE);
4149 GError *error = NULL;
4151 gtk_file_chooser_default_unselect_all (chooser);
4152 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4154 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4156 browse_files_center_selected_row (data->impl);
4159 if (data->impl->select_multiple)
4160 file_list_drag_data_select_uris (data->impl, data->uris);
4163 g_object_unref (data->impl);
4164 g_strfreev (data->uris);
4165 g_object_unref (data->file);
4168 g_object_unref (cancellable);
4172 file_list_drag_data_received_cb (GtkWidget *widget,
4173 GdkDragContext *context,
4176 GtkSelectionData *selection_data,
4181 GtkFileChooserDefault *impl;
4182 GtkFileChooser *chooser;
4187 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4188 chooser = GTK_FILE_CHOOSER (data);
4190 /* Allow only drags from other widgets; see bug #533891. */
4191 if (gtk_drag_get_source_widget (context) == widget)
4194 /* Parse the text/uri-list string, navigate to the first one */
4195 uris = gtk_selection_data_get_uris (selection_data);
4196 if (uris && uris[0])
4198 struct FileListDragData *data;
4201 file = g_file_new_for_uri (uri);
4203 data = g_new0 (struct FileListDragData, 1);
4204 data->impl = g_object_ref (impl);
4208 if (impl->file_list_drag_data_received_cancellable)
4209 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4211 impl->file_list_drag_data_received_cancellable =
4212 _gtk_file_system_get_info (impl->file_system, file,
4214 file_list_drag_data_received_get_info_cb,
4218 g_signal_stop_emission_by_name (widget, "drag_data_received");
4221 /* Don't do anything with the drag_drop signal */
4223 file_list_drag_drop_cb (GtkWidget *widget,
4224 GdkDragContext *context,
4228 GtkFileChooserDefault *impl)
4230 g_signal_stop_emission_by_name (widget, "drag_drop");
4234 /* Disable the normal tree drag motion handler, it makes it look like you're
4235 dropping the dragged item onto a tree item */
4237 file_list_drag_motion_cb (GtkWidget *widget,
4238 GdkDragContext *context,
4242 GtkFileChooserDefault *impl)
4244 g_signal_stop_emission_by_name (widget, "drag_motion");
4248 /* Constructs the popup menu for the file list if needed */
4250 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4254 if (impl->browse_files_popup_menu)
4257 impl->browse_files_popup_menu = gtk_menu_new ();
4258 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4259 impl->browse_files_tree_view,
4260 popup_menu_detach_cb);
4262 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4263 impl->browse_files_popup_menu_add_shortcut_item = item;
4264 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4265 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4266 gtk_widget_set_sensitive (item, FALSE);
4267 g_signal_connect (item, "activate",
4268 G_CALLBACK (add_to_shortcuts_cb), impl);
4269 gtk_widget_show (item);
4270 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4272 item = gtk_separator_menu_item_new ();
4273 gtk_widget_show (item);
4274 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4276 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4277 impl->browse_files_popup_menu_hidden_files_item = item;
4278 g_signal_connect (item, "toggled",
4279 G_CALLBACK (show_hidden_toggled_cb), impl);
4280 gtk_widget_show (item);
4281 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4284 /* Updates the popup menu for the file list, creating it if necessary */
4286 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4288 file_list_build_popup_menu (impl);
4290 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4292 /* The sensitivity of the Add to Bookmarks item is set in
4293 * bookmarks_check_add_sensitivity()
4296 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4297 G_CALLBACK (show_hidden_toggled_cb), impl);
4298 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4300 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4301 G_CALLBACK (show_hidden_toggled_cb), impl);
4305 popup_position_func (GtkMenu *menu,
4311 GtkWidget *widget = GTK_WIDGET (user_data);
4312 GdkScreen *screen = gtk_widget_get_screen (widget);
4315 GdkRectangle monitor;
4317 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4319 gdk_window_get_origin (widget->window, x, y);
4321 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4323 *x += (widget->allocation.width - req.width) / 2;
4324 *y += (widget->allocation.height - req.height) / 2;
4326 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4327 gtk_menu_set_monitor (menu, monitor_num);
4328 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4330 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4331 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4337 file_list_popup_menu (GtkFileChooserDefault *impl,
4338 GdkEventButton *event)
4340 file_list_update_popup_menu (impl);
4342 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4343 NULL, NULL, NULL, NULL,
4344 event->button, event->time);
4347 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4349 popup_position_func, impl->browse_files_tree_view,
4350 0, GDK_CURRENT_TIME);
4351 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4357 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4359 list_popup_menu_cb (GtkWidget *widget,
4360 GtkFileChooserDefault *impl)
4362 file_list_popup_menu (impl, NULL);
4366 /* Callback used when a button is pressed on the file list. We trap button 3 to
4367 * bring up a popup menu.
4370 list_button_press_event_cb (GtkWidget *widget,
4371 GdkEventButton *event,
4372 GtkFileChooserDefault *impl)
4374 static gboolean in_press = FALSE;
4380 if (event->button != 3)
4384 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4387 file_list_popup_menu (impl, event);
4391 /* Sets the sort column IDs for the file list based on the operation mode */
4393 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4395 int name_id, mtime_id;
4397 name_id = mtime_id = 0;
4399 switch (impl->operation_mode)
4401 case OPERATION_MODE_BROWSE:
4402 name_id = FILE_LIST_COL_NAME;
4403 mtime_id = FILE_LIST_COL_MTIME;
4405 case OPERATION_MODE_SEARCH:
4406 name_id = SEARCH_MODEL_COL_FILE;
4407 mtime_id = SEARCH_MODEL_COL_STAT;
4409 case OPERATION_MODE_RECENT:
4410 name_id = RECENT_MODEL_COL_FILE;
4411 mtime_id = RECENT_MODEL_COL_INFO;
4415 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4416 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4420 file_list_query_tooltip_cb (GtkWidget *widget,
4423 gboolean keyboard_tip,
4424 GtkTooltip *tooltip,
4427 GtkFileChooserDefault *impl = user_data;
4428 GtkTreeIter iter, child_iter;
4429 GtkTreePath *path = NULL;
4433 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4437 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4445 switch (impl->operation_mode)
4447 case OPERATION_MODE_SEARCH:
4448 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4450 gtk_tree_path_free (path);
4454 search_get_valid_child_iter (impl, &child_iter, &iter);
4455 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4456 SEARCH_MODEL_COL_FILE, &file,
4460 case OPERATION_MODE_RECENT:
4461 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4463 gtk_tree_path_free (path);
4467 recent_get_valid_child_iter (impl, &child_iter, &iter);
4468 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4469 RECENT_MODEL_COL_FILE, &file,
4473 case OPERATION_MODE_BROWSE:
4474 g_assert_not_reached ();
4480 gtk_tree_path_free (path);
4484 filename = g_file_get_path (file);
4485 gtk_tooltip_set_text (tooltip, filename);
4486 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4491 gtk_tree_path_free (path);
4496 /* Creates the widgets for the file list */
4498 create_file_list (GtkFileChooserDefault *impl)
4501 GtkTreeSelection *selection;
4502 GtkTreeViewColumn *column;
4503 GtkCellRenderer *renderer;
4505 /* Scrolled window */
4506 swin = gtk_scrolled_window_new (NULL, NULL);
4507 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4508 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4509 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4512 /* Tree/list view */
4514 impl->browse_files_tree_view = gtk_tree_view_new ();
4515 #ifdef PROFILE_FILE_CHOOSER
4516 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4518 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4519 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4521 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4522 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4524 gtk_drag_dest_set (impl->browse_files_tree_view,
4525 GTK_DEST_DEFAULT_ALL,
4527 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4528 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4530 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4531 G_CALLBACK (list_row_activated), impl);
4532 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4533 G_CALLBACK (trap_activate_cb), impl);
4534 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4535 G_CALLBACK (list_popup_menu_cb), impl);
4536 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4537 G_CALLBACK (list_button_press_event_cb), impl);
4539 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4540 G_CALLBACK (file_list_drag_data_received_cb), impl);
4541 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4542 G_CALLBACK (file_list_drag_drop_cb), impl);
4543 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4544 G_CALLBACK (file_list_drag_motion_cb), impl);
4546 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4547 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4548 G_CALLBACK (file_list_query_tooltip_cb), impl);
4550 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4551 gtk_tree_selection_set_select_function (selection,
4554 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4557 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4558 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4560 g_signal_connect (selection, "changed",
4561 G_CALLBACK (list_selection_changed), impl);
4563 /* Filename column */
4565 impl->list_name_column = gtk_tree_view_column_new ();
4566 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4567 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4568 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4569 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4571 renderer = gtk_cell_renderer_pixbuf_new ();
4572 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4573 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4574 list_icon_data_func, impl, NULL);
4576 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4577 g_object_set (impl->list_name_renderer,
4578 "ellipsize", PANGO_ELLIPSIZE_END,
4580 g_signal_connect (impl->list_name_renderer, "edited",
4581 G_CALLBACK (renderer_edited_cb), impl);
4582 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4583 G_CALLBACK (renderer_editing_canceled_cb), impl);
4584 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4585 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4586 list_name_data_func, impl, NULL);
4588 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4592 column = gtk_tree_view_column_new ();
4593 gtk_tree_view_column_set_title (column, _("Size"));
4595 renderer = gtk_cell_renderer_text_new ();
4596 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4597 gtk_tree_view_column_set_cell_data_func (column, renderer,
4598 list_size_data_func, impl, NULL);
4599 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4600 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4603 /* Modification time column */
4605 column = gtk_tree_view_column_new ();
4606 gtk_tree_view_column_set_resizable (column, TRUE);
4607 gtk_tree_view_column_set_title (column, _("Modified"));
4609 renderer = gtk_cell_renderer_text_new ();
4610 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4611 gtk_tree_view_column_set_cell_data_func (column, renderer,
4612 list_mtime_data_func, impl, NULL);
4613 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4614 impl->list_mtime_column = column;
4616 file_list_set_sort_column_ids (impl);
4618 gtk_widget_show_all (swin);
4624 create_path_bar (GtkFileChooserDefault *impl)
4626 GtkWidget *path_bar;
4628 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4629 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4634 /* Creates the widgets for the files/folders pane */
4636 file_pane_create (GtkFileChooserDefault *impl,
4637 GtkSizeGroup *size_group)
4643 vbox = gtk_vbox_new (FALSE, 6);
4644 gtk_widget_show (vbox);
4646 /* Box for lists and preview */
4648 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4649 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4650 gtk_widget_show (hbox);
4654 widget = create_file_list (impl);
4655 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4659 impl->preview_box = gtk_vbox_new (FALSE, 12);
4660 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4661 /* Don't show preview box initially */
4665 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4667 widget = filter_create (impl);
4669 gtk_widget_show (widget);
4670 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4672 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4673 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4678 /* Callback used when the "Browse for more folders" expander is toggled */
4680 expander_changed_cb (GtkExpander *expander,
4682 GtkFileChooserDefault *impl)
4684 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4685 update_appearance (impl);
4688 /* Callback used when the selection changes in the save folder combo box */
4690 save_folder_combo_changed_cb (GtkComboBox *combo,
4691 GtkFileChooserDefault *impl)
4695 if (impl->changing_folder)
4698 if (gtk_combo_box_get_active_iter (combo, &iter))
4700 GtkTreeIter child_iter;
4702 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4705 shortcuts_activate_iter (impl, &child_iter);
4709 /* Filter function used to filter out the Search item and its separator.
4710 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4713 shortcuts_combo_filter_func (GtkTreeModel *model,
4717 GtkFileChooserDefault *impl;
4718 GtkTreePath *tree_path;
4723 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4725 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4727 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4728 g_assert (tree_path != NULL);
4730 indices = gtk_tree_path_get_indices (tree_path);
4734 if (impl->has_search)
4736 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4737 if (idx == indices[0])
4741 if (impl->has_recent)
4743 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4744 if (idx == indices[0])
4748 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4749 if (idx == indices[0])
4754 gtk_tree_path_free (tree_path);
4759 /* Creates the combo box with the save folders */
4761 save_folder_combo_create (GtkFileChooserDefault *impl)
4764 GtkCellRenderer *cell;
4766 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4767 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4768 shortcuts_combo_filter_func,
4772 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4773 "model", impl->shortcuts_combo_filter_model,
4774 "focus-on-click", FALSE,
4776 gtk_widget_show (combo);
4778 cell = gtk_cell_renderer_pixbuf_new ();
4779 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4780 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4781 "pixbuf", SHORTCUTS_COL_PIXBUF,
4782 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4783 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4786 cell = gtk_cell_renderer_text_new ();
4787 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4788 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4789 "text", SHORTCUTS_COL_NAME,
4790 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4793 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4794 shortcuts_row_separator_func,
4797 g_signal_connect (combo, "changed",
4798 G_CALLBACK (save_folder_combo_changed_cb), impl);
4803 /* Creates the widgets specific to Save mode */
4805 save_widgets_create (GtkFileChooserDefault *impl)
4810 GtkWidget *alignment;
4812 if (impl->save_widgets != NULL)
4815 location_switch_to_path_bar (impl);
4817 vbox = gtk_vbox_new (FALSE, 12);
4819 table = gtk_table_new (2, 2, FALSE);
4820 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4821 gtk_widget_show (table);
4822 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4823 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4827 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4828 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4829 gtk_table_attach (GTK_TABLE (table), widget,
4833 gtk_widget_show (widget);
4835 /* Location entry */
4837 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4838 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4840 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4841 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4842 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4844 GTK_EXPAND | GTK_FILL, 0,
4846 gtk_widget_show (impl->location_entry);
4847 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4850 impl->save_folder_label = gtk_label_new (NULL);
4851 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4852 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4856 gtk_widget_show (impl->save_folder_label);
4858 impl->save_folder_combo = save_folder_combo_create (impl);
4859 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4861 GTK_EXPAND | GTK_FILL, GTK_FILL,
4863 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4866 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4867 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4869 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4870 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4871 g_signal_connect (impl->save_expander, "notify::expanded",
4872 G_CALLBACK (expander_changed_cb),
4874 gtk_widget_show_all (alignment);
4876 impl->save_widgets = vbox;
4877 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4878 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4879 gtk_widget_show (impl->save_widgets);
4882 /* Destroys the widgets specific to Save mode */
4884 save_widgets_destroy (GtkFileChooserDefault *impl)
4886 if (impl->save_widgets == NULL)
4889 gtk_widget_destroy (impl->save_widgets);
4890 impl->save_widgets = NULL;
4891 impl->location_entry = NULL;
4892 impl->save_folder_label = NULL;
4893 impl->save_folder_combo = NULL;
4894 impl->save_expander = NULL;
4897 /* Turns on the path bar widget. Can be called even if we are already in that
4901 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4903 if (impl->location_entry)
4905 gtk_widget_destroy (impl->location_entry);
4906 impl->location_entry = NULL;
4909 gtk_widget_hide (impl->location_entry_box);
4912 /* Sets the full path of the current folder as the text in the location entry. */
4914 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4916 gchar *text, *filename;
4918 if (!impl->current_folder)
4921 filename = g_file_get_path (impl->current_folder);
4925 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4929 text = g_file_get_uri (impl->current_folder);
4933 gboolean need_slash;
4936 len = strlen (text);
4937 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4943 slash_text = g_new (char, len + 2);
4944 strcpy (slash_text, text);
4945 slash_text[len] = G_DIR_SEPARATOR;
4946 slash_text[len + 1] = 0;
4952 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4959 /* Turns on the location entry. Can be called even if we are already in that
4963 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4965 /* when in search or recent files mode, we are not showing the
4966 * location_entry_box container, so there's no point in switching
4969 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4970 impl->operation_mode == OPERATION_MODE_RECENT)
4973 if (impl->location_entry)
4974 gtk_widget_destroy (impl->location_entry);
4978 gtk_widget_show (impl->location_entry_box);
4982 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4983 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4985 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4986 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4988 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4989 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4991 /* Configure the entry */
4993 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4997 gtk_widget_show (impl->location_entry);
4998 gtk_widget_grab_focus (impl->location_entry);
5001 /* Sets a new location mode. set_buttons determines whether the toggle button
5002 * for the mode will also be changed.
5005 location_mode_set (GtkFileChooserDefault *impl,
5006 LocationMode new_mode,
5007 gboolean set_button)
5009 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5010 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5012 GtkWindow *toplevel;
5013 GtkWidget *current_focus;
5014 gboolean button_active;
5015 gboolean switch_to_file_list;
5019 case LOCATION_MODE_PATH_BAR:
5020 button_active = FALSE;
5022 /* The location_entry will disappear when we switch to path bar mode. So,
5023 * we'll focus the file list in that case, to avoid having a window with
5024 * no focused widget.
5026 toplevel = get_toplevel (GTK_WIDGET (impl));
5027 switch_to_file_list = FALSE;
5030 current_focus = gtk_window_get_focus (toplevel);
5031 if (!current_focus || current_focus == impl->location_entry)
5032 switch_to_file_list = TRUE;
5035 location_switch_to_path_bar (impl);
5037 if (switch_to_file_list)
5038 gtk_widget_grab_focus (impl->browse_files_tree_view);
5042 case LOCATION_MODE_FILENAME_ENTRY:
5043 button_active = TRUE;
5044 location_switch_to_filename_entry (impl);
5048 g_assert_not_reached ();
5054 g_signal_handlers_block_by_func (impl->location_button,
5055 G_CALLBACK (location_button_toggled_cb), impl);
5057 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5059 g_signal_handlers_unblock_by_func (impl->location_button,
5060 G_CALLBACK (location_button_toggled_cb), impl);
5064 impl->location_mode = new_mode;
5068 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5070 /* when in search or recent files mode, we are not showing the
5071 * location_entry_box container, so there's no point in switching
5074 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5075 impl->operation_mode == OPERATION_MODE_RECENT)
5078 /* If the file entry is not visible, show it.
5079 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5081 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5083 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5085 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5087 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5089 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5093 gtk_widget_grab_focus (impl->location_entry);
5098 /* Callback used when one of the location mode buttons is toggled */
5100 location_button_toggled_cb (GtkToggleButton *toggle,
5101 GtkFileChooserDefault *impl)
5104 LocationMode new_mode;
5106 is_active = gtk_toggle_button_get_active (toggle);
5110 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5111 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5115 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5116 new_mode = LOCATION_MODE_PATH_BAR;
5119 location_mode_set (impl, new_mode, FALSE);
5122 /* Creates a toggle button for the location entry. */
5124 location_button_create (GtkFileChooserDefault *impl)
5129 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5130 gtk_widget_show (image);
5132 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5136 g_signal_connect (impl->location_button, "toggled",
5137 G_CALLBACK (location_button_toggled_cb), impl);
5139 str = _("Type a file name");
5141 gtk_widget_set_tooltip_text (impl->location_button, str);
5142 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5145 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5147 browse_widgets_create (GtkFileChooserDefault *impl)
5153 GtkSizeGroup *size_group;
5155 /* size group is used by the [+][-] buttons and the filter combo */
5156 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5157 vbox = gtk_vbox_new (FALSE, 12);
5159 /* Location widgets */
5160 hbox = gtk_hbox_new (FALSE, 12);
5161 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5162 gtk_widget_show (hbox);
5163 impl->browse_path_bar_hbox = hbox;
5165 location_button_create (impl);
5166 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5170 impl->browse_path_bar = create_path_bar (impl);
5171 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5172 gtk_widget_show_all (impl->browse_path_bar);
5173 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5176 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5177 g_signal_connect (impl->browse_new_folder_button, "clicked",
5178 G_CALLBACK (new_folder_button_clicked), impl);
5179 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5181 /* Box for the location label and entry */
5183 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5184 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5186 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5187 gtk_widget_show (impl->location_label);
5188 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5191 hpaned = gtk_hpaned_new ();
5192 gtk_widget_show (hpaned);
5193 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5195 widget = shortcuts_pane_create (impl, size_group);
5196 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5197 widget = file_pane_create (impl, size_group);
5198 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5200 g_object_unref (size_group);
5206 gtk_file_chooser_default_constructor (GType type,
5207 guint n_construct_properties,
5208 GObjectConstructParam *construct_params)
5210 GtkFileChooserDefault *impl;
5213 profile_start ("start", NULL);
5215 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5216 n_construct_properties,
5218 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5220 g_assert (impl->file_system);
5222 gtk_widget_push_composite_child ();
5224 /* Shortcuts model */
5225 shortcuts_model_create (impl);
5227 /* The browse widgets */
5228 impl->browse_widgets = browse_widgets_create (impl);
5229 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5231 /* Alignment to hold extra widget */
5232 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5233 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5235 gtk_widget_pop_composite_child ();
5236 update_appearance (impl);
5238 profile_end ("end", NULL);
5243 /* Sets the extra_widget by packing it in the appropriate place */
5245 set_extra_widget (GtkFileChooserDefault *impl,
5246 GtkWidget *extra_widget)
5250 g_object_ref (extra_widget);
5251 /* FIXME: is this right ? */
5252 gtk_widget_show (extra_widget);
5255 if (impl->extra_widget)
5257 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5258 g_object_unref (impl->extra_widget);
5261 impl->extra_widget = extra_widget;
5262 if (impl->extra_widget)
5264 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5265 gtk_widget_show (impl->extra_align);
5268 gtk_widget_hide (impl->extra_align);
5272 set_local_only (GtkFileChooserDefault *impl,
5273 gboolean local_only)
5275 if (local_only != impl->local_only)
5277 impl->local_only = local_only;
5279 if (impl->shortcuts_model && impl->file_system)
5281 shortcuts_add_volumes (impl);
5282 shortcuts_add_bookmarks (impl);
5285 if (local_only && !g_file_is_native (impl->current_folder))
5287 /* If we are pointing to a non-local folder, make an effort to change
5288 * back to a local folder, but it's really up to the app to not cause
5289 * such a situation, so we ignore errors.
5291 const gchar *home = g_get_home_dir ();
5297 home_file = g_file_new_for_path (home);
5299 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5301 g_object_unref (home_file);
5307 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5308 GtkFileChooserDefault *impl)
5310 shortcuts_add_volumes (impl);
5311 shortcuts_add_bookmarks (impl);
5313 bookmarks_check_add_sensitivity (impl);
5314 bookmarks_check_remove_sensitivity (impl);
5315 shortcuts_check_popup_sensitivity (impl);
5318 /* Sets the file chooser to multiple selection mode */
5320 set_select_multiple (GtkFileChooserDefault *impl,
5321 gboolean select_multiple,
5322 gboolean property_notify)
5324 GtkTreeSelection *selection;
5325 GtkSelectionMode mode;
5327 if (select_multiple == impl->select_multiple)
5330 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5332 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5333 gtk_tree_selection_set_mode (selection, mode);
5335 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5337 impl->select_multiple = select_multiple;
5338 g_object_notify (G_OBJECT (impl), "select-multiple");
5340 check_preview_change (impl);
5344 set_file_system_backend (GtkFileChooserDefault *impl)
5346 profile_start ("start for backend", "default");
5348 impl->file_system = _gtk_file_system_new ();
5350 g_signal_connect (impl->file_system, "volumes-changed",
5351 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5352 g_signal_connect (impl->file_system, "bookmarks-changed",
5353 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5355 profile_end ("end", NULL);
5358 /* This function is basically a do_all function.
5360 * It sets the visibility on all the widgets based on the current state, and
5361 * moves the custom_widget if needed.
5364 update_appearance (GtkFileChooserDefault *impl)
5366 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5367 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5371 gtk_widget_hide (impl->location_button);
5372 save_widgets_create (impl);
5374 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5375 text = _("Save in _folder:");
5377 text = _("Create in _folder:");
5379 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5381 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5383 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5384 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5385 gtk_widget_show (impl->browse_widgets);
5389 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5390 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5391 gtk_widget_hide (impl->browse_widgets);
5394 gtk_widget_show (impl->browse_new_folder_button);
5396 if (impl->select_multiple)
5398 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5399 "Re-setting to single selection mode.");
5400 set_select_multiple (impl, FALSE, TRUE);
5403 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5404 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5406 gtk_widget_show (impl->location_button);
5407 save_widgets_destroy (impl);
5408 gtk_widget_show (impl->browse_widgets);
5409 location_mode_set (impl, impl->location_mode, TRUE);
5412 if (impl->location_entry)
5413 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5415 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5416 gtk_widget_hide (impl->browse_new_folder_button);
5418 gtk_widget_show (impl->browse_new_folder_button);
5420 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5421 * of files may change depending whether we are in a file or folder-only mode.
5423 gtk_widget_queue_draw (impl->browse_files_tree_view);
5425 g_signal_emit_by_name (impl, "default-size-changed");
5429 gtk_file_chooser_default_set_property (GObject *object,
5431 const GValue *value,
5435 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5439 case GTK_FILE_CHOOSER_PROP_ACTION:
5441 GtkFileChooserAction action = g_value_get_enum (value);
5443 if (action != impl->action)
5445 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5447 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5448 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5449 && impl->select_multiple)
5451 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5452 "this is not allowed in multiple selection mode. Resetting the file chooser "
5453 "to single selection mode.");
5454 set_select_multiple (impl, FALSE, TRUE);
5456 impl->action = action;
5457 update_appearance (impl);
5458 settings_load (impl);
5463 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5464 /* Ignore property */
5467 case GTK_FILE_CHOOSER_PROP_FILTER:
5468 set_current_filter (impl, g_value_get_object (value));
5471 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5472 set_local_only (impl, g_value_get_boolean (value));
5475 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5476 set_preview_widget (impl, g_value_get_object (value));
5479 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5480 impl->preview_widget_active = g_value_get_boolean (value);
5481 update_preview_widget_visibility (impl);
5484 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5485 impl->use_preview_label = g_value_get_boolean (value);
5486 update_preview_widget_visibility (impl);
5489 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5490 set_extra_widget (impl, g_value_get_object (value));
5493 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5495 gboolean select_multiple = g_value_get_boolean (value);
5496 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5497 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5500 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5501 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5502 "leaving the file chooser in single selection mode.");
5506 set_select_multiple (impl, select_multiple, FALSE);
5510 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5512 gboolean show_hidden = g_value_get_boolean (value);
5513 if (show_hidden != impl->show_hidden)
5515 impl->show_hidden = show_hidden;
5517 if (impl->browse_files_model)
5518 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5523 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5525 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5526 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5537 gtk_file_chooser_default_get_property (GObject *object,
5542 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5546 case GTK_FILE_CHOOSER_PROP_ACTION:
5547 g_value_set_enum (value, impl->action);
5550 case GTK_FILE_CHOOSER_PROP_FILTER:
5551 g_value_set_object (value, impl->current_filter);
5554 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5555 g_value_set_boolean (value, impl->local_only);
5558 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5559 g_value_set_object (value, impl->preview_widget);
5562 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5563 g_value_set_boolean (value, impl->preview_widget_active);
5566 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5567 g_value_set_boolean (value, impl->use_preview_label);
5570 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5571 g_value_set_object (value, impl->extra_widget);
5574 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5575 g_value_set_boolean (value, impl->select_multiple);
5578 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5579 g_value_set_boolean (value, impl->show_hidden);
5582 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5583 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5587 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5592 /* Removes the settings signal handler. It's safe to call multiple times */
5594 remove_settings_signal (GtkFileChooserDefault *impl,
5597 if (impl->settings_signal_id)
5599 GtkSettings *settings;
5601 settings = gtk_settings_get_for_screen (screen);
5602 g_signal_handler_disconnect (settings,
5603 impl->settings_signal_id);
5604 impl->settings_signal_id = 0;
5609 gtk_file_chooser_default_dispose (GObject *object)
5612 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5614 if (impl->extra_widget)
5616 g_object_unref (impl->extra_widget);
5617 impl->extra_widget = NULL;
5620 pending_select_files_free (impl);
5622 /* cancel all pending operations */
5623 if (impl->pending_cancellables)
5625 for (l = impl->pending_cancellables; l; l = l->next)
5627 GCancellable *cancellable = G_CANCELLABLE (l->data);
5628 g_cancellable_cancel (cancellable);
5630 g_slist_free (impl->pending_cancellables);
5631 impl->pending_cancellables = NULL;
5634 if (impl->reload_icon_cancellables)
5636 for (l = impl->reload_icon_cancellables; l; l = l->next)
5638 GCancellable *cancellable = G_CANCELLABLE (l->data);
5639 g_cancellable_cancel (cancellable);
5641 g_slist_free (impl->reload_icon_cancellables);
5642 impl->reload_icon_cancellables = NULL;
5645 if (impl->loading_shortcuts)
5647 for (l = impl->loading_shortcuts; l; l = l->next)
5649 GCancellable *cancellable = G_CANCELLABLE (l->data);
5650 g_cancellable_cancel (cancellable);
5652 g_slist_free (impl->loading_shortcuts);
5653 impl->loading_shortcuts = NULL;
5656 if (impl->file_list_drag_data_received_cancellable)
5658 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5659 impl->file_list_drag_data_received_cancellable = NULL;
5662 if (impl->update_current_folder_cancellable)
5664 g_cancellable_cancel (impl->update_current_folder_cancellable);
5665 impl->update_current_folder_cancellable = NULL;
5668 if (impl->show_and_select_files_cancellable)
5670 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5671 impl->show_and_select_files_cancellable = NULL;
5674 if (impl->should_respond_get_info_cancellable)
5676 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5677 impl->should_respond_get_info_cancellable = NULL;
5680 if (impl->update_from_entry_cancellable)
5682 g_cancellable_cancel (impl->update_from_entry_cancellable);
5683 impl->update_from_entry_cancellable = NULL;
5686 if (impl->shortcuts_activate_iter_cancellable)
5688 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5689 impl->shortcuts_activate_iter_cancellable = NULL;
5692 search_stop_searching (impl, TRUE);
5693 recent_stop_loading (impl);
5695 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5697 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5700 /* We override show-all since we have internal widgets that
5701 * shouldn't be shown when you call show_all(), like the filter
5705 gtk_file_chooser_default_show_all (GtkWidget *widget)
5707 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5709 gtk_widget_show (widget);
5711 if (impl->extra_widget)
5712 gtk_widget_show_all (impl->extra_widget);
5715 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5716 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5719 toplevel_set_focus_cb (GtkWindow *window,
5721 GtkFileChooserDefault *impl)
5723 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5726 /* We monitor the focus widget on our toplevel to be able to know which widget
5727 * was last focused at the time our "should_respond" method gets called.
5730 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5731 GtkWidget *previous_toplevel)
5733 GtkFileChooserDefault *impl;
5734 GtkWidget *toplevel;
5736 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5738 if (previous_toplevel)
5740 g_assert (impl->toplevel_set_focus_id != 0);
5741 g_signal_handler_disconnect (previous_toplevel,
5742 impl->toplevel_set_focus_id);
5743 impl->toplevel_set_focus_id = 0;
5744 impl->toplevel_last_focus_widget = NULL;
5747 g_assert (impl->toplevel_set_focus_id == 0);
5749 toplevel = gtk_widget_get_toplevel (widget);
5750 if (GTK_IS_WINDOW (toplevel))
5752 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5753 G_CALLBACK (toplevel_set_focus_cb), impl);
5754 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5758 /* Changes the icons wherever it is needed */
5760 change_icon_theme (GtkFileChooserDefault *impl)
5762 GtkSettings *settings;
5765 profile_start ("start", NULL);
5767 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5769 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5770 impl->icon_size = MAX (width, height);
5772 impl->icon_size = FALLBACK_ICON_SIZE;
5774 shortcuts_reload_icons (impl);
5775 gtk_widget_queue_resize (impl->browse_files_tree_view);
5777 profile_end ("end", NULL);
5780 /* Callback used when a GtkSettings value changes */
5782 settings_notify_cb (GObject *object,
5784 GtkFileChooserDefault *impl)
5788 profile_start ("start", NULL);
5790 name = g_param_spec_get_name (pspec);
5792 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5793 strcmp (name, "gtk-icon-sizes") == 0)
5794 change_icon_theme (impl);
5796 profile_end ("end", NULL);
5799 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5803 check_icon_theme (GtkFileChooserDefault *impl)
5805 GtkSettings *settings;
5807 profile_start ("start", NULL);
5809 if (impl->settings_signal_id)
5811 profile_end ("end", NULL);
5815 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5817 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5818 impl->settings_signal_id = g_signal_connect (settings, "notify",
5819 G_CALLBACK (settings_notify_cb), impl);
5821 change_icon_theme (impl);
5824 profile_end ("end", NULL);
5828 gtk_file_chooser_default_style_set (GtkWidget *widget,
5829 GtkStyle *previous_style)
5831 GtkFileChooserDefault *impl;
5833 profile_start ("start", NULL);
5835 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5837 profile_msg (" parent class style_set start", NULL);
5838 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5839 profile_msg (" parent class style_set end", NULL);
5841 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5842 change_icon_theme (impl);
5844 profile_msg (" emit default-size-changed start", NULL);
5845 g_signal_emit_by_name (widget, "default-size-changed");
5846 profile_msg (" emit default-size-changed end", NULL);
5848 profile_end ("end", NULL);
5852 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5853 GdkScreen *previous_screen)
5855 GtkFileChooserDefault *impl;
5857 profile_start ("start", NULL);
5859 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5861 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5862 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5864 remove_settings_signal (impl, previous_screen);
5865 check_icon_theme (impl);
5867 g_signal_emit_by_name (widget, "default-size-changed");
5869 profile_end ("end", NULL);
5873 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5874 GtkAllocation *allocation)
5876 GtkFileChooserDefault *impl;
5878 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5880 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5882 impl->default_width = allocation->width;
5883 impl->default_height = allocation->height;
5885 if (impl->preview_widget_active &&
5886 impl->preview_widget &&
5887 GTK_WIDGET_DRAWABLE (impl->preview_widget))
5888 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
5890 if (impl->extra_widget &&
5891 GTK_WIDGET_DRAWABLE (impl->extra_widget))
5892 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
5896 get_is_file_filtered (GtkFileChooserDefault *impl,
5898 GFileInfo *file_info)
5900 GtkFileFilterInfo filter_info;
5901 GtkFileFilterFlags needed;
5904 if (!impl->current_filter)
5907 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5909 needed = gtk_file_filter_get_needed (impl->current_filter);
5911 filter_info.display_name = g_file_info_get_display_name (file_info);
5912 filter_info.mime_type = g_file_info_get_content_type (file_info);
5914 if (needed & GTK_FILE_FILTER_FILENAME)
5916 filter_info.filename = g_file_get_path (file);
5917 if (filter_info.filename)
5918 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5921 filter_info.filename = NULL;
5923 if (needed & GTK_FILE_FILTER_URI)
5925 filter_info.uri = g_file_get_uri (file);
5926 if (filter_info.uri)
5927 filter_info.contains |= GTK_FILE_FILTER_URI;
5930 filter_info.uri = NULL;
5932 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5934 if (filter_info.filename)
5935 g_free ((gchar *)filter_info.filename);
5936 if (filter_info.uri)
5937 g_free ((gchar *)filter_info.uri);
5943 settings_load (GtkFileChooserDefault *impl)
5945 GtkFileChooserSettings *settings;
5946 LocationMode location_mode;
5947 gboolean show_hidden;
5948 gboolean expand_folders;
5950 settings = _gtk_file_chooser_settings_new ();
5952 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5953 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5954 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5956 g_object_unref (settings);
5958 location_mode_set (impl, location_mode, TRUE);
5959 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5960 impl->expand_folders = expand_folders;
5961 if (impl->save_expander)
5962 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5966 settings_save (GtkFileChooserDefault *impl)
5968 GtkFileChooserSettings *settings;
5970 settings = _gtk_file_chooser_settings_new ();
5972 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5973 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5974 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5977 _gtk_file_chooser_settings_save (settings, NULL);
5979 g_object_unref (settings);
5982 /* GtkWidget::map method */
5984 gtk_file_chooser_default_map (GtkWidget *widget)
5986 GtkFileChooserDefault *impl;
5987 char *current_working_dir;
5989 profile_start ("start", NULL);
5991 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5993 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5995 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5997 switch (impl->reload_state)
6000 /* The user didn't explicitly give us a folder to
6001 * display, so we'll use the cwd
6003 current_working_dir = g_get_current_dir ();
6004 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6005 current_working_dir);
6006 g_free (current_working_dir);
6009 case RELOAD_HAS_FOLDER:
6010 /* Nothing; we are already loading or loaded, so we
6011 * don't need to reload
6015 case RELOAD_WAS_UNMAPPED:
6016 /* Just reload the current folder; else continue
6019 if (impl->current_folder)
6021 pending_select_files_store_selection (impl);
6022 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6027 g_assert_not_reached ();
6031 volumes_bookmarks_changed_cb (impl->file_system, impl);
6033 settings_load (impl);
6035 profile_end ("end", NULL);
6038 /* GtkWidget::unmap method */
6040 gtk_file_chooser_default_unmap (GtkWidget *widget)
6042 GtkFileChooserDefault *impl;
6044 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6046 settings_save (impl);
6048 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6050 impl->reload_state = RELOAD_WAS_UNMAPPED;
6054 list_model_filter_func (GtkFileSystemModel *model,
6056 GFileInfo *file_info,
6059 GtkFileChooserDefault *impl = user_data;
6061 if (!impl->current_filter)
6064 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6067 return !get_is_file_filtered (impl, file, file_info);
6071 install_list_model_filter (GtkFileChooserDefault *impl)
6073 GtkFileSystemModelFilter filter;
6076 g_assert (impl->browse_files_model != NULL);
6078 if (impl->current_filter)
6080 filter = list_model_filter_func;
6089 _gtk_file_system_model_set_filter (impl->browse_files_model,
6094 #define COMPARE_DIRECTORIES \
6095 GtkFileChooserDefault *impl = user_data; \
6096 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6097 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6098 gboolean dir_a, dir_b; \
6101 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6103 return impl->list_sort_ascending ? -1 : 1; \
6106 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6108 return impl->list_sort_ascending ? 1 : -1; \
6110 if (dir_a != dir_b) \
6111 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6113 /* Sort callback for the filename column */
6115 name_sort_func (GtkTreeModel *model,
6120 COMPARE_DIRECTORIES;
6123 gchar *key_a, *key_b;
6126 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6127 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6128 result = strcmp (key_a, key_b);
6137 /* Sort callback for the size column */
6139 size_sort_func (GtkTreeModel *model,
6144 COMPARE_DIRECTORIES;
6147 goffset size_a = g_file_info_get_size (info_a);
6148 goffset size_b = g_file_info_get_size (info_b);
6150 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6154 /* Sort callback for the mtime column */
6156 mtime_sort_func (GtkTreeModel *model,
6161 COMPARE_DIRECTORIES;
6166 g_file_info_get_modification_time (info_a, &ta);
6167 g_file_info_get_modification_time (info_b, &tb);
6169 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6173 /* Callback used when the sort column changes. We cache the sort order for use
6174 * in name_sort_func().
6177 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6178 GtkFileChooserDefault *impl)
6180 GtkSortType sort_type;
6182 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6183 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6187 set_busy_cursor (GtkFileChooserDefault *impl,
6190 GtkWindow *toplevel;
6191 GdkDisplay *display;
6194 toplevel = get_toplevel (GTK_WIDGET (impl));
6195 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6198 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6201 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6205 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6206 gdk_display_flush (display);
6209 gdk_cursor_unref (cursor);
6212 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6214 load_set_model (GtkFileChooserDefault *impl)
6216 profile_start ("start", NULL);
6218 g_assert (impl->browse_files_model != NULL);
6219 g_assert (impl->sort_model == NULL);
6221 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6222 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6223 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6224 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6225 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6226 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6227 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6228 impl->list_sort_ascending = TRUE;
6229 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6231 g_signal_connect (impl->sort_model, "sort-column-changed",
6232 G_CALLBACK (list_sort_column_changed_cb), impl);
6234 profile_msg (" gtk_tree_view_set_model start", NULL);
6235 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6236 GTK_TREE_MODEL (impl->sort_model));
6237 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6238 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6239 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6240 profile_msg (" gtk_tree_view_set_model end", NULL);
6242 profile_end ("end", NULL);
6245 /* Timeout callback used when the loading timer expires */
6247 load_timeout_cb (gpointer data)
6249 GtkFileChooserDefault *impl;
6251 profile_start ("start", NULL);
6253 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6254 g_assert (impl->load_state == LOAD_PRELOAD);
6255 g_assert (impl->load_timeout_id != 0);
6256 g_assert (impl->browse_files_model != NULL);
6258 impl->load_timeout_id = 0;
6259 impl->load_state = LOAD_LOADING;
6261 load_set_model (impl);
6263 profile_end ("end", NULL);
6268 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6270 load_setup_timer (GtkFileChooserDefault *impl)
6272 g_assert (impl->load_timeout_id == 0);
6273 g_assert (impl->load_state != LOAD_PRELOAD);
6275 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6276 impl->load_state = LOAD_PRELOAD;
6279 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6281 load_remove_timer (GtkFileChooserDefault *impl)
6283 if (impl->load_timeout_id != 0)
6285 g_assert (impl->load_state == LOAD_PRELOAD);
6287 g_source_remove (impl->load_timeout_id);
6288 impl->load_timeout_id = 0;
6289 impl->load_state = LOAD_EMPTY;
6292 g_assert (impl->load_state == LOAD_EMPTY ||
6293 impl->load_state == LOAD_LOADING ||
6294 impl->load_state == LOAD_FINISHED);
6297 /* Selects the first row in the file list */
6299 browse_files_select_first_row (GtkFileChooserDefault *impl)
6302 GtkTreeIter dummy_iter;
6303 GtkTreeModel *tree_model;
6305 if (!impl->sort_model)
6308 path = gtk_tree_path_new_from_indices (0, -1);
6309 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6311 /* If the list is empty, do nothing. */
6312 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6313 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6315 gtk_tree_path_free (path);
6318 struct center_selected_row_closure {
6319 GtkFileChooserDefault *impl;
6320 gboolean already_centered;
6323 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6324 * selected row in the tree view.
6327 center_selected_row_foreach_cb (GtkTreeModel *model,
6332 struct center_selected_row_closure *closure;
6335 if (closure->already_centered)
6338 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6339 closure->already_centered = TRUE;
6342 /* Centers the selected row in the tree view */
6344 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6346 struct center_selected_row_closure closure;
6347 GtkTreeSelection *selection;
6349 closure.impl = impl;
6350 closure.already_centered = FALSE;
6352 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6353 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6356 struct ShowAndSelectPathsData
6358 GtkFileChooserDefault *impl;
6363 show_and_select_files_finished_loading (GtkFolder *folder,
6366 gboolean have_hidden;
6367 gboolean have_filtered;
6369 struct ShowAndSelectPathsData *data = user_data;
6371 have_hidden = FALSE;
6372 have_filtered = FALSE;
6374 for (l = data->files; l; l = l->next)
6381 info = _gtk_folder_get_info (folder, file);
6385 have_hidden = g_file_info_get_is_hidden (info);
6388 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6389 get_is_file_filtered (data->impl, file, info);
6391 g_object_unref (info);
6393 if (have_hidden && have_filtered)
6394 break; /* we now have all the information we need */
6398 g_signal_handlers_disconnect_by_func (folder,
6399 show_and_select_files_finished_loading,
6403 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6406 set_current_filter (data->impl, NULL);
6408 for (l = data->files; l; l = l->next)
6413 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6414 select_func, data->impl);
6417 browse_files_center_selected_row (data->impl);
6419 g_object_unref (data->impl);
6420 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6421 g_slist_free (data->files);
6426 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6428 const GError *error,
6431 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6432 struct ShowAndSelectPathsData *data = user_data;
6434 if (data->impl->show_and_select_files_cancellable != cancellable)
6437 data->impl->show_and_select_files_cancellable = NULL;
6439 if (cancelled || error)
6442 g_object_unref (cancellable);
6444 if (_gtk_folder_is_finished_loading (folder))
6445 show_and_select_files_finished_loading (folder, user_data);
6447 g_signal_connect (folder, "finished-loading",
6448 G_CALLBACK (show_and_select_files_finished_loading),
6454 g_object_unref (data->impl);
6455 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6456 g_slist_free (data->files);
6459 g_object_unref (cancellable);
6463 show_and_select_files (GtkFileChooserDefault *impl,
6468 struct ShowAndSelectPathsData *info;
6470 profile_start ("start", NULL);
6474 profile_end ("end", NULL);
6478 info = g_new (struct ShowAndSelectPathsData, 1);
6479 info->impl = g_object_ref (impl);
6480 info->files = g_slist_copy (files);
6481 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6483 if (impl->show_and_select_files_cancellable)
6484 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6486 impl->show_and_select_files_cancellable =
6487 _gtk_file_system_get_folder (impl->file_system, parent_file,
6488 "standard::is-hidden,standard::type,standard::name",
6489 show_and_select_files_get_folder_cb, info);
6491 profile_end ("end", NULL);
6495 /* Processes the pending operation when a folder is finished loading */
6497 pending_select_files_process (GtkFileChooserDefault *impl)
6499 g_assert (impl->load_state == LOAD_FINISHED);
6500 g_assert (impl->browse_files_model != NULL);
6501 g_assert (impl->sort_model != NULL);
6503 if (impl->pending_select_files)
6506 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6507 pending_select_files_free (impl);
6508 browse_files_center_selected_row (impl);
6512 /* We only select the first row if the chooser is actually mapped ---
6513 * selecting the first row is to help the user when he is interacting with
6514 * the chooser, but sometimes a chooser works not on behalf of the user,
6515 * but rather on behalf of something else like GtkFileChooserButton. In
6516 * that case, the chooser's selection should be what the caller expects,
6517 * as the user can't see that something else got selected. See bug #165264.
6519 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6520 browse_files_select_first_row (impl);
6523 g_assert (impl->pending_select_files == NULL);
6526 /* Callback used when the file system model finishes loading */
6528 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6529 GtkFileChooserDefault *impl)
6531 profile_start ("start", NULL);
6533 if (impl->load_state == LOAD_PRELOAD)
6535 load_remove_timer (impl);
6536 load_set_model (impl);
6538 else if (impl->load_state == LOAD_LOADING)
6544 /* We can't g_assert_not_reached(), as something other than us may have
6545 * initiated a folder reload. See #165556.
6547 profile_end ("end", NULL);
6551 g_assert (impl->load_timeout_id == 0);
6553 impl->load_state = LOAD_FINISHED;
6555 pending_select_files_process (impl);
6556 set_busy_cursor (impl, FALSE);
6557 #ifdef PROFILE_FILE_CHOOSER
6558 access ("MARK: *** FINISHED LOADING", F_OK);
6561 profile_end ("end", NULL);
6565 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6567 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6569 if (impl->browse_files_model)
6571 g_object_unref (impl->browse_files_model);
6572 impl->browse_files_model = NULL;
6575 if (impl->sort_model)
6577 g_object_unref (impl->sort_model);
6578 impl->sort_model = NULL;
6581 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6584 /* Gets rid of the old list model and creates a new one for the current folder */
6586 set_list_model (GtkFileChooserDefault *impl,
6589 g_assert (impl->current_folder != NULL);
6591 profile_start ("start", NULL);
6593 stop_loading_and_clear_list_model (impl);
6595 set_busy_cursor (impl, TRUE);
6596 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6598 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6599 impl->current_folder, 0,
6600 "standard,time,thumbnail::*",
6602 if (!impl->browse_files_model)
6604 set_busy_cursor (impl, FALSE);
6605 profile_end ("end", NULL);
6609 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6611 g_signal_connect (impl->browse_files_model, "finished-loading",
6612 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6614 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6616 install_list_model_filter (impl);
6618 profile_end ("end", NULL);
6623 struct update_chooser_entry_selected_foreach_closure {
6625 GtkTreeIter first_selected_iter;
6629 compare_utf8_filenames (const gchar *a,
6632 gchar *a_folded, *b_folded;
6635 a_folded = g_utf8_strdown (a, -1);
6636 b_folded = g_utf8_strdown (b, -1);
6638 retval = strcmp (a_folded, b_folded);
6647 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6652 struct update_chooser_entry_selected_foreach_closure *closure;
6655 closure->num_selected++;
6657 if (closure->num_selected == 1)
6658 closure->first_selected_iter = *iter;
6662 update_chooser_entry (GtkFileChooserDefault *impl)
6664 GtkTreeSelection *selection;
6665 struct update_chooser_entry_selected_foreach_closure closure;
6666 const char *file_part;
6668 /* no need to update the file chooser's entry if there's no entry */
6669 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6670 impl->operation_mode == OPERATION_MODE_RECENT ||
6671 !impl->location_entry)
6674 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6675 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6676 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6677 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6678 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6681 g_assert (impl->location_entry != NULL);
6683 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6684 closure.num_selected = 0;
6685 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6689 if (closure.num_selected == 0)
6691 goto maybe_clear_entry;
6693 else if (closure.num_selected == 1)
6695 GtkTreeIter child_iter;
6697 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6700 gboolean change_entry;
6702 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6704 &closure.first_selected_iter);
6706 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6708 /* If the cursor moved to the row of the newly created folder,
6709 * retrieving info will return NULL.
6714 g_free (impl->browse_files_last_selected_name);
6715 impl->browse_files_last_selected_name =
6716 g_strdup (g_file_info_get_display_name (info));
6718 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6719 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6720 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6722 /* We don't want the name to change when clicking on a folder... */
6723 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6726 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6730 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6732 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6733 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6741 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6742 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6744 /* Multiple selection, so just clear the entry. */
6746 g_free (impl->browse_files_last_selected_name);
6747 impl->browse_files_last_selected_name = NULL;
6749 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6755 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6756 && impl->browse_files_last_selected_name)
6758 const char *entry_text;
6760 gboolean clear_entry;
6762 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6763 len = strlen (entry_text);
6766 /* The file chooser entry may have appended a "/" to its text. So
6767 * take it out, and compare the result to the old selection.
6769 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6773 tmp = g_strndup (entry_text, len - 1);
6774 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6778 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6781 clear_entry = FALSE;
6784 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6789 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6793 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6797 struct UpdateCurrentFolderData
6799 GtkFileChooserDefault *impl;
6801 gboolean keep_trail;
6802 gboolean clear_entry;
6803 GFile *original_file;
6804 GError *original_error;
6808 update_current_folder_get_info_cb (GCancellable *cancellable,
6810 const GError *error,
6813 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6814 struct UpdateCurrentFolderData *data = user_data;
6815 GtkFileChooserDefault *impl = data->impl;
6817 if (cancellable != impl->update_current_folder_cancellable)
6820 impl->update_current_folder_cancellable = NULL;
6821 impl->reload_state = RELOAD_EMPTY;
6823 set_busy_cursor (impl, FALSE);
6832 if (!data->original_file)
6834 data->original_file = g_object_ref (data->file);
6835 data->original_error = g_error_copy (error);
6838 parent_file = g_file_get_parent (data->file);
6840 /* get parent path and try to change the folder to that */
6843 g_object_unref (data->file);
6844 data->file = parent_file;
6846 g_object_unref (cancellable);
6848 /* restart the update current folder operation */
6849 impl->reload_state = RELOAD_HAS_FOLDER;
6851 impl->update_current_folder_cancellable =
6852 _gtk_file_system_get_info (impl->file_system, data->file,
6854 update_current_folder_get_info_cb,
6857 set_busy_cursor (impl, TRUE);
6863 /* error and bail out */
6864 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6865 g_object_unref (data->original_file);
6871 if (data->original_file)
6873 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6875 g_object_unref (data->original_file);
6878 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6881 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6884 if (impl->current_folder != data->file)
6886 if (impl->current_folder)
6887 g_object_unref (impl->current_folder);
6889 impl->current_folder = g_object_ref (data->file);
6891 impl->reload_state = RELOAD_HAS_FOLDER;
6894 /* Update the widgets that may trigger a folder change themselves. */
6896 if (!impl->changing_folder)
6898 impl->changing_folder = TRUE;
6900 shortcuts_update_current_folder (impl);
6902 impl->changing_folder = FALSE;
6905 /* Set the folder on the save entry */
6907 if (impl->location_entry)
6909 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6910 impl->current_folder);
6912 if (data->clear_entry)
6913 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6916 /* Create a new list model. This is slightly evil; we store the result value
6917 * but perform more actions rather than returning immediately even if it
6918 * generates an error.
6920 set_list_model (impl, NULL);
6922 /* Refresh controls */
6924 shortcuts_find_current_folder (impl);
6926 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6928 check_preview_change (impl);
6929 bookmarks_check_add_sensitivity (impl);
6931 g_signal_emit_by_name (impl, "selection-changed", 0);
6934 g_object_unref (data->file);
6937 g_object_unref (cancellable);
6941 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6943 gboolean keep_trail,
6944 gboolean clear_entry,
6947 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6948 struct UpdateCurrentFolderData *data;
6950 profile_start ("start", NULL);
6952 g_object_ref (file);
6954 switch (impl->operation_mode)
6956 case OPERATION_MODE_SEARCH:
6957 search_switch_to_browse_mode (impl);
6959 case OPERATION_MODE_RECENT:
6960 recent_switch_to_browse_mode (impl);
6962 case OPERATION_MODE_BROWSE:
6966 if (impl->local_only && !g_file_is_native (file))
6968 g_set_error_literal (error,
6969 GTK_FILE_CHOOSER_ERROR,
6970 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6971 _("Cannot change to folder because it is not local"));
6973 g_object_unref (file);
6974 profile_end ("end - not local", NULL);
6978 if (impl->update_current_folder_cancellable)
6979 g_cancellable_cancel (impl->update_current_folder_cancellable);
6981 /* Test validity of path here. */
6982 data = g_new0 (struct UpdateCurrentFolderData, 1);
6984 data->file = g_object_ref (file);
6985 data->keep_trail = keep_trail;
6986 data->clear_entry = clear_entry;
6988 impl->reload_state = RELOAD_HAS_FOLDER;
6990 impl->update_current_folder_cancellable =
6991 _gtk_file_system_get_info (impl->file_system, file,
6993 update_current_folder_get_info_cb,
6996 set_busy_cursor (impl, TRUE);
6997 g_object_unref (file);
6999 profile_end ("end", NULL);
7004 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7006 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7008 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7009 impl->operation_mode == OPERATION_MODE_RECENT)
7012 if (impl->reload_state == RELOAD_EMPTY)
7014 char *current_working_dir;
7017 /* We are unmapped, or we had an error while loading the last folder. We'll return
7018 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7019 * explicitly calls set_current_folder() on us.
7021 current_working_dir = g_get_current_dir ();
7022 file = g_file_new_for_path (current_working_dir);
7023 g_free (current_working_dir);
7027 if (impl->current_folder)
7028 return g_object_ref (impl->current_folder);
7034 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7037 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7039 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7040 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7042 pending_select_files_free (impl);
7043 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7047 select_func (GtkFileSystemModel *model,
7052 GtkFileChooserDefault *impl = user_data;
7053 GtkTreeSelection *selection;
7054 GtkTreeIter sorted_iter;
7056 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7058 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7059 gtk_tree_selection_select_iter (selection, &sorted_iter);
7063 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7067 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7071 parent_file = g_file_get_parent (file);
7074 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7076 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7077 impl->operation_mode == OPERATION_MODE_RECENT ||
7078 impl->load_state == LOAD_EMPTY)
7084 g_assert (impl->current_folder != NULL);
7086 same_path = g_file_equal (parent_file, impl->current_folder);
7089 if (same_path && impl->load_state == LOAD_FINISHED)
7094 files.data = (gpointer) file;
7097 result = show_and_select_files (impl, parent_file, &files, error);
7098 g_object_unref (parent_file);
7102 pending_select_files_add (impl, file);
7108 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7109 g_object_unref (parent_file);
7113 g_object_unref (parent_file);
7118 unselect_func (GtkFileSystemModel *model,
7123 GtkFileChooserDefault *impl = user_data;
7124 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7125 GtkTreePath *sorted_path;
7127 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7129 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7131 gtk_tree_path_free (sorted_path);
7135 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7138 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7140 if (!impl->browse_files_model)
7143 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7144 unselect_func, impl);
7148 maybe_select (GtkTreeModel *model,
7153 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7154 GtkTreeSelection *selection;
7158 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7160 info = get_list_file_info (impl, iter);
7161 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7163 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7164 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7165 gtk_tree_selection_select_iter (selection, iter);
7167 gtk_tree_selection_unselect_iter (selection, iter);
7173 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7175 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7177 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7178 impl->operation_mode == OPERATION_MODE_RECENT)
7180 GtkTreeSelection *selection;
7182 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7183 gtk_tree_selection_select_all (selection);
7187 if (impl->select_multiple)
7188 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7189 maybe_select, impl);
7193 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7195 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7196 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7198 gtk_tree_selection_unselect_all (selection);
7199 pending_select_files_free (impl);
7202 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7204 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7206 * is_empty_ret - whether the file entry is totally empty
7208 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7209 * the path will be "$cwd/foobar")
7212 check_save_entry (GtkFileChooserDefault *impl,
7214 gboolean *is_well_formed_ret,
7215 gboolean *is_empty_ret,
7216 gboolean *is_file_part_empty_ret,
7217 gboolean *is_folder)
7219 GtkFileChooserEntry *chooser_entry;
7220 GFile *current_folder;
7221 const char *file_part;
7225 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7226 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7227 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7228 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7229 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7231 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7233 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7236 *is_well_formed_ret = TRUE;
7237 *is_empty_ret = TRUE;
7238 *is_file_part_empty_ret = TRUE;
7244 *is_empty_ret = FALSE;
7246 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7247 if (!current_folder)
7250 *is_well_formed_ret = FALSE;
7251 *is_file_part_empty_ret = FALSE;
7257 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7259 if (!file_part || file_part[0] == '\0')
7261 *file_ret = g_object_ref (current_folder);
7262 *is_well_formed_ret = TRUE;
7263 *is_file_part_empty_ret = TRUE;
7269 *is_file_part_empty_ret = FALSE;
7272 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7276 error_building_filename_dialog (impl, error);
7278 *is_well_formed_ret = FALSE;
7285 *is_well_formed_ret = TRUE;
7286 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7289 struct get_files_closure {
7290 GtkFileChooserDefault *impl;
7292 GFile *file_from_entry;
7296 get_files_foreach (GtkTreeModel *model,
7301 struct get_files_closure *info;
7303 GtkFileSystemModel *fs_model;
7304 GtkTreeIter sel_iter;
7307 fs_model = info->impl->browse_files_model;
7308 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7310 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7312 return; /* We are on the editable row */
7314 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7315 info->result = g_slist_prepend (info->result, g_object_ref (file));
7319 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7321 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7322 struct get_files_closure info;
7323 GtkWindow *toplevel;
7324 GtkWidget *current_focus;
7325 gboolean file_list_seen;
7327 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7328 return search_get_selected_files (impl);
7330 if (impl->operation_mode == OPERATION_MODE_RECENT)
7331 return recent_get_selected_files (impl);
7335 info.file_from_entry = NULL;
7337 toplevel = get_toplevel (GTK_WIDGET (impl));
7339 current_focus = gtk_window_get_focus (toplevel);
7341 current_focus = NULL;
7343 file_list_seen = FALSE;
7344 if (current_focus == impl->browse_files_tree_view)
7346 GtkTreeSelection *selection;
7350 file_list_seen = TRUE;
7351 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7352 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7354 /* If there is no selection in the file list, we probably have this situation:
7356 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7357 * 2. He then double-clicked on a folder ("bar") in the file list
7359 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7360 * filename entry to see if that is the case.
7362 if (info.result == NULL && impl->location_entry)
7365 else if (impl->location_entry && current_focus == impl->location_entry)
7367 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7371 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7376 if (!is_well_formed)
7379 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7381 g_object_unref (info.file_from_entry);
7385 if (info.file_from_entry)
7386 info.result = g_slist_prepend (info.result, info.file_from_entry);
7387 else if (!file_list_seen)
7392 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7394 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7398 /* The focus is on a dialog's action area button or something else */
7399 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7400 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7408 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7409 * fall back to the current directory */
7410 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7411 info.result == NULL)
7413 GFile *current_folder;
7415 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7418 info.result = g_slist_prepend (info.result, current_folder);
7421 return g_slist_reverse (info.result);
7425 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7427 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7429 if (impl->preview_file)
7430 return g_object_ref (impl->preview_file);
7435 static GtkFileSystem *
7436 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7438 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7440 return impl->file_system;
7443 /* Shows or hides the filter widgets */
7445 show_filters (GtkFileChooserDefault *impl,
7449 gtk_widget_show (impl->filter_combo_hbox);
7451 gtk_widget_hide (impl->filter_combo_hbox);
7455 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7456 GtkFileFilter *filter)
7458 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7461 if (g_slist_find (impl->filters, filter))
7463 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7467 g_object_ref_sink (filter);
7468 impl->filters = g_slist_append (impl->filters, filter);
7470 name = gtk_file_filter_get_name (filter);
7472 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7474 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7476 if (!g_slist_find (impl->filters, impl->current_filter))
7477 set_current_filter (impl, filter);
7479 show_filters (impl, TRUE);
7483 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7484 GtkFileFilter *filter)
7486 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7487 GtkTreeModel *model;
7491 filter_index = g_slist_index (impl->filters, filter);
7493 if (filter_index < 0)
7495 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7499 impl->filters = g_slist_remove (impl->filters, filter);
7501 if (filter == impl->current_filter)
7504 set_current_filter (impl, impl->filters->data);
7506 set_current_filter (impl, NULL);
7509 /* Remove row from the combo box */
7510 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7511 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7512 g_assert_not_reached ();
7514 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7516 g_object_unref (filter);
7519 show_filters (impl, FALSE);
7523 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7525 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7527 return g_slist_copy (impl->filters);
7530 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7532 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7535 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7538 struct AddShortcutData
7540 GtkFileChooserDefault *impl;
7545 add_shortcut_get_info_cb (GCancellable *cancellable,
7547 const GError *error,
7551 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7552 struct AddShortcutData *data = user_data;
7554 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7557 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7559 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7562 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7564 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7567 g_object_unref (data->impl);
7568 g_object_unref (data->file);
7571 g_object_unref (cancellable);
7575 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7579 GCancellable *cancellable;
7580 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7581 struct AddShortcutData *data;
7585 /* Avoid adding duplicates */
7586 pos = shortcut_find_position (impl, file);
7587 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7591 uri = g_file_get_uri (file);
7592 /* translators, "Shortcut" means "Bookmark" here */
7594 GTK_FILE_CHOOSER_ERROR,
7595 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7596 _("Shortcut %s already exists"),
7603 for (l = impl->loading_shortcuts; l; l = l->next)
7605 GCancellable *c = l->data;
7608 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7609 if (f && g_file_equal (file, f))
7613 uri = g_file_get_uri (file);
7615 GTK_FILE_CHOOSER_ERROR,
7616 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7617 _("Shortcut %s already exists"),
7625 data = g_new0 (struct AddShortcutData, 1);
7626 data->impl = g_object_ref (impl);
7627 data->file = g_object_ref (file);
7629 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7631 add_shortcut_get_info_cb, data);
7636 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7637 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7643 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7647 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7654 for (l = impl->loading_shortcuts; l; l = l->next)
7656 GCancellable *c = l->data;
7659 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7660 if (f && g_file_equal (file, f))
7662 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7663 g_cancellable_cancel (c);
7668 if (impl->num_shortcuts == 0)
7671 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7672 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7673 g_assert_not_reached ();
7675 for (i = 0; i < impl->num_shortcuts; i++)
7678 ShortcutType shortcut_type;
7681 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7682 SHORTCUTS_COL_DATA, &col_data,
7683 SHORTCUTS_COL_TYPE, &shortcut_type,
7685 g_assert (col_data != NULL);
7686 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7688 shortcut = col_data;
7689 if (g_file_equal (shortcut, file))
7691 shortcuts_remove_rows (impl, pos + i, 1);
7692 impl->num_shortcuts--;
7696 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7697 g_assert_not_reached ();
7702 uri = g_file_get_uri (file);
7703 /* translators, "Shortcut" means "Bookmark" here */
7705 GTK_FILE_CHOOSER_ERROR,
7706 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7707 _("Shortcut %s does not exist"),
7715 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7717 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7723 if (impl->num_shortcuts == 0)
7726 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7727 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7728 g_assert_not_reached ();
7732 for (i = 0; i < impl->num_shortcuts; i++)
7735 ShortcutType shortcut_type;
7738 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7739 SHORTCUTS_COL_DATA, &col_data,
7740 SHORTCUTS_COL_TYPE, &shortcut_type,
7742 g_assert (col_data != NULL);
7743 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7745 shortcut = col_data;
7746 list = g_slist_prepend (list, g_object_ref (shortcut));
7748 if (i != impl->num_shortcuts - 1)
7750 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7751 g_assert_not_reached ();
7755 return g_slist_reverse (list);
7758 /* Guesses a size based upon font sizes */
7760 find_good_size_from_style (GtkWidget *widget,
7764 GtkFileChooserDefault *impl;
7769 g_assert (widget->style != NULL);
7770 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7772 if (impl->default_width == 0 &&
7773 impl->default_height == 0)
7775 screen = gtk_widget_get_screen (widget);
7778 resolution = gdk_screen_get_resolution (screen);
7779 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7783 resolution = 96.0; /* wheeee */
7785 font_size = pango_font_description_get_size (widget->style->font_desc);
7786 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7788 impl->default_width = font_size * NUM_CHARS;
7789 impl->default_height = font_size * NUM_LINES;
7792 *width = impl->default_width;
7793 *height = impl->default_height;
7797 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7798 gint *default_width,
7799 gint *default_height)
7801 GtkFileChooserDefault *impl;
7804 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7805 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7807 if (impl->preview_widget_active &&
7808 impl->preview_widget &&
7809 GTK_WIDGET_VISIBLE (impl->preview_widget))
7811 gtk_widget_size_request (impl->preview_box, &req);
7812 *default_width += PREVIEW_HBOX_SPACING + req.width;
7815 if (impl->extra_widget &&
7816 GTK_WIDGET_VISIBLE (impl->extra_widget))
7818 gtk_widget_size_request (impl->extra_align, &req);
7819 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7823 struct switch_folder_closure {
7824 GtkFileChooserDefault *impl;
7829 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7831 switch_folder_foreach_cb (GtkTreeModel *model,
7836 struct switch_folder_closure *closure;
7837 GtkTreeIter child_iter;
7841 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7843 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7844 closure->num_selected++;
7847 /* Changes to the selected folder in the list view */
7849 switch_to_selected_folder (GtkFileChooserDefault *impl)
7851 GtkTreeSelection *selection;
7852 struct switch_folder_closure closure;
7854 /* We do this with foreach() rather than get_selected() as we may be in
7855 * multiple selection mode
7858 closure.impl = impl;
7859 closure.file = NULL;
7860 closure.num_selected = 0;
7862 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7863 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7865 g_assert (closure.file && closure.num_selected == 1);
7867 change_folder_and_display_error (impl, closure.file, FALSE);
7870 /* Gets the GFileInfo for the selected row in the file list; assumes single
7874 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7875 gboolean *had_selection)
7877 GtkTreeSelection *selection;
7878 GtkTreeIter iter, child_iter;
7881 g_assert (!impl->select_multiple);
7882 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7883 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7885 *had_selection = FALSE;
7889 *had_selection = TRUE;
7891 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7895 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7899 /* Gets the display name of the selected file in the file list; assumes single
7900 * selection mode and that something is selected.
7902 static const gchar *
7903 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7906 gboolean had_selection;
7908 info = get_selected_file_info_from_file_list (impl, &had_selection);
7909 g_assert (had_selection);
7910 g_assert (info != NULL);
7912 return g_file_info_get_display_name (info);
7916 add_custom_button_to_dialog (GtkDialog *dialog,
7917 const gchar *mnemonic_label,
7918 const gchar *stock_id,
7923 button = gtk_button_new_with_mnemonic (mnemonic_label);
7924 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7925 gtk_button_set_image (GTK_BUTTON (button),
7926 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7927 gtk_widget_show (button);
7929 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7932 /* Presents an overwrite confirmation dialog; returns whether we should accept
7936 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7937 const gchar *file_part,
7938 const gchar *folder_display_name)
7940 GtkWindow *toplevel;
7944 toplevel = get_toplevel (GTK_WIDGET (impl));
7946 dialog = gtk_message_dialog_new (toplevel,
7947 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7948 GTK_MESSAGE_QUESTION,
7950 _("A file named \"%s\" already exists. Do you want to replace it?"),
7952 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7953 _("The file already exists in \"%s\". Replacing it will "
7954 "overwrite its contents."),
7955 folder_display_name);
7957 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7958 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7959 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7960 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7961 GTK_RESPONSE_ACCEPT,
7962 GTK_RESPONSE_CANCEL,
7964 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7966 if (toplevel->group)
7967 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7969 response = gtk_dialog_run (GTK_DIALOG (dialog));
7971 gtk_widget_destroy (dialog);
7973 return (response == GTK_RESPONSE_ACCEPT);
7976 struct GetDisplayNameData
7978 GtkFileChooserDefault *impl;
7983 confirmation_confirm_get_info_cb (GCancellable *cancellable,
7985 const GError *error,
7988 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7989 gboolean should_respond = FALSE;
7990 struct GetDisplayNameData *data = user_data;
7992 if (cancellable != data->impl->should_respond_get_info_cancellable)
7995 data->impl->should_respond_get_info_cancellable = NULL;
8001 /* Huh? Did the folder disappear? Let the caller deal with it */
8002 should_respond = TRUE;
8004 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8006 set_busy_cursor (data->impl, FALSE);
8008 g_signal_emit_by_name (data->impl, "response-requested");
8011 g_object_unref (data->impl);
8012 g_free (data->file_part);
8015 g_object_unref (cancellable);
8018 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8019 * should respond. Can get the file part from the file list or the save entry.
8022 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8023 const gchar *file_part,
8026 GtkFileChooserConfirmation conf;
8028 if (!impl->do_overwrite_confirmation)
8031 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8033 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8037 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8039 struct GetDisplayNameData *data;
8041 g_assert (file_part != NULL);
8043 data = g_new0 (struct GetDisplayNameData, 1);
8044 data->impl = g_object_ref (impl);
8045 data->file_part = g_strdup (file_part);
8047 if (impl->should_respond_get_info_cancellable)
8048 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8050 impl->should_respond_get_info_cancellable =
8051 _gtk_file_system_get_info (impl->file_system, parent_file,
8052 "standard::display-name",
8053 confirmation_confirm_get_info_cb,
8055 set_busy_cursor (data->impl, TRUE);
8059 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8062 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8066 g_assert_not_reached ();
8071 struct FileExistsData
8073 GtkFileChooserDefault *impl;
8074 gboolean file_exists_and_is_not_folder;
8080 save_entry_get_info_cb (GCancellable *cancellable,
8082 const GError *error,
8085 gboolean parent_is_folder;
8086 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8087 struct FileExistsData *data = user_data;
8089 if (cancellable != data->impl->should_respond_get_info_cancellable)
8092 data->impl->should_respond_get_info_cancellable = NULL;
8094 set_busy_cursor (data->impl, FALSE);
8100 parent_is_folder = FALSE;
8102 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8104 if (parent_is_folder)
8106 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8108 if (data->file_exists_and_is_not_folder)
8111 const char *file_part;
8113 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8114 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8117 g_signal_emit_by_name (data->impl, "response-requested");
8120 g_signal_emit_by_name (data->impl, "response-requested");
8122 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8124 GError *error = NULL;
8126 set_busy_cursor (data->impl, TRUE);
8127 g_file_make_directory (data->file, NULL, &error);
8128 set_busy_cursor (data->impl, FALSE);
8131 g_signal_emit_by_name (data->impl, "response-requested");
8133 error_creating_folder_dialog (data->impl, data->file, error);
8138 /* This will display an error, which is what we want */
8139 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8143 g_object_unref (data->impl);
8144 g_object_unref (data->file);
8145 g_object_unref (data->parent_file);
8148 g_object_unref (cancellable);
8152 file_exists_get_info_cb (GCancellable *cancellable,
8154 const GError *error,
8157 gboolean data_ownership_taken = FALSE;
8158 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8159 gboolean file_exists_and_is_not_folder;
8160 struct FileExistsData *data = user_data;
8162 if (cancellable != data->impl->file_exists_get_info_cancellable)
8165 data->impl->file_exists_get_info_cancellable = NULL;
8167 set_busy_cursor (data->impl, FALSE);
8172 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8174 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8175 /* user typed a filename; we are done */
8176 g_signal_emit_by_name (data->impl, "response-requested");
8177 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8178 && file_exists_and_is_not_folder)
8180 /* Oops, the user typed the name of an existing path which is not
8183 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8184 g_error_copy (error));
8188 /* check that everything up to the last component exists */
8190 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8191 data_ownership_taken = TRUE;
8193 if (data->impl->should_respond_get_info_cancellable)
8194 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8196 data->impl->should_respond_get_info_cancellable =
8197 _gtk_file_system_get_info (data->impl->file_system,
8200 save_entry_get_info_cb,
8202 set_busy_cursor (data->impl, TRUE);
8206 if (!data_ownership_taken)
8208 g_object_unref (data->impl);
8209 g_object_unref (data->file);
8210 g_object_unref (data->parent_file);
8214 g_object_unref (cancellable);
8218 paste_text_received (GtkClipboard *clipboard,
8220 GtkFileChooserDefault *impl)
8227 file = g_file_new_for_uri (text);
8229 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8230 location_popup_handler (impl, text);
8232 g_object_unref (file);
8235 /* Handler for the "location-popup-on-paste" keybinding signal */
8237 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8239 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8240 GDK_SELECTION_CLIPBOARD);
8241 gtk_clipboard_request_text (clipboard,
8242 (GtkClipboardTextReceivedFunc) paste_text_received,
8247 /* Implementation for GtkFileChooserEmbed::should_respond() */
8249 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8251 GtkFileChooserDefault *impl;
8252 GtkWidget *toplevel;
8253 GtkWidget *current_focus;
8255 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8257 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8258 g_assert (GTK_IS_WINDOW (toplevel));
8260 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8262 if (current_focus == impl->browse_files_tree_view)
8264 /* The following array encodes what we do based on the impl->action and the
8265 * number of files selected.
8268 NOOP, /* Do nothing (don't respond) */
8269 RESPOND, /* Respond immediately */
8270 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8271 ALL_FILES, /* Respond only if everything selected is a file */
8272 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8273 SAVE_ENTRY, /* Go to the code for handling the save entry */
8274 NOT_REACHED /* Sanity check */
8276 static const ActionToTake what_to_do[4][3] = {
8277 /* 0 selected 1 selected many selected */
8278 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8279 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8280 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8281 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8285 gboolean all_files, all_folders;
8287 ActionToTake action;
8291 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8293 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8294 return search_should_respond (impl);
8296 if (impl->operation_mode == OPERATION_MODE_RECENT)
8297 return recent_should_respond (impl);
8299 selection_check (impl, &num_selected, &all_files, &all_folders);
8301 if (num_selected > 2)
8306 action = what_to_do [impl->action] [k];
8316 case RESPOND_OR_SWITCH:
8317 g_assert (num_selected == 1);
8321 switch_to_selected_folder (impl);
8324 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8325 return should_respond_after_confirm_overwrite (impl,
8326 get_display_name_from_file_list (impl),
8327 impl->current_folder);
8341 g_assert_not_reached ();
8344 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8347 gboolean is_well_formed, is_empty, is_file_part_empty;
8350 GtkFileChooserEntry *entry;
8355 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8356 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8357 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8358 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8359 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8361 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8362 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8364 if (is_empty || !is_well_formed)
8367 g_assert (file != NULL);
8372 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8373 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8375 change_folder_and_display_error (impl, file, TRUE);
8378 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8379 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8381 /* The folder already exists, so we do not need to create it.
8382 * Just respond to terminate the dialog.
8388 g_assert_not_reached ();
8394 struct FileExistsData *data;
8396 /* We need to check whether file exists and is not a folder */
8398 data = g_new0 (struct FileExistsData, 1);
8399 data->impl = g_object_ref (impl);
8400 data->file = g_object_ref (file);
8401 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8403 if (impl->file_exists_get_info_cancellable)
8404 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8406 impl->file_exists_get_info_cancellable =
8407 _gtk_file_system_get_info (impl->file_system, file,
8409 file_exists_get_info_cb,
8412 set_busy_cursor (impl, TRUE);
8416 g_error_free (error);
8419 g_object_unref (file);
8422 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8424 /* The focus is on a dialog's action area button, *and* the widget that
8425 * was focused immediately before it is the file list.
8429 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8431 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8434 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8436 /* The focus is on a dialog's action area button, *and* the widget that
8437 * was focused immediately before it is the location entry.
8442 /* The focus is on a dialog's action area button or something else */
8443 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8444 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8449 g_assert_not_reached ();
8453 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8455 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8457 GtkFileChooserDefault *impl;
8460 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8462 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8463 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8465 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8466 widget = impl->browse_files_tree_view;
8468 widget = impl->location_entry;
8470 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8471 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8472 widget = impl->location_entry;
8475 g_assert_not_reached ();
8479 g_assert (widget != NULL);
8480 gtk_widget_grab_focus (widget);
8483 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8485 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8495 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8496 *list = g_slist_prepend (*list, file);
8499 /* Constructs a list of the selected paths in search mode */
8501 search_get_selected_files (GtkFileChooserDefault *impl)
8504 GtkTreeSelection *selection;
8508 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8509 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8510 result = g_slist_reverse (result);
8515 /* Called from ::should_respond(). We return whether there are selected files
8516 * in the search list.
8519 search_should_respond (GtkFileChooserDefault *impl)
8521 GtkTreeSelection *selection;
8523 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8525 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8526 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8529 struct SearchHitInsertRequest
8531 GtkFileChooserDefault *impl;
8533 GtkTreeRowReference *row_ref;
8537 search_hit_get_info_cb (GCancellable *cancellable,
8539 const GError *error,
8542 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8543 GdkPixbuf *pixbuf = NULL;
8546 GCancellable *model_cancellable;
8547 gboolean is_folder = FALSE;
8550 struct SearchHitInsertRequest *request = data;
8552 if (!request->impl->search_model)
8555 path = gtk_tree_row_reference_get_path (request->row_ref);
8559 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8561 gtk_tree_path_free (path);
8563 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8564 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8566 if (cancellable != model_cancellable)
8569 /* set the cancellable to NULL in the model */
8570 gtk_list_store_set (request->impl->search_model, &iter,
8571 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8579 gtk_list_store_remove (request->impl->search_model, &iter);
8583 display_name = g_strdup (g_file_info_get_display_name (info));
8584 mime_type = g_strdup (g_file_info_get_content_type (info));
8585 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8586 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8587 request->impl->icon_size);
8589 gtk_list_store_set (request->impl->search_model, &iter,
8590 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8591 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8592 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8593 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8597 g_object_unref (pixbuf);
8600 g_object_unref (request->impl);
8601 g_object_unref (request->file);
8602 gtk_tree_row_reference_free (request->row_ref);
8605 g_object_unref (cancellable);
8608 /* Adds one hit from the search engine to the search_model */
8610 search_add_hit (GtkFileChooserDefault *impl,
8616 char *collation_key;
8617 struct stat statbuf;
8618 struct stat *statbuf_copy;
8621 GCancellable *cancellable;
8622 struct SearchHitInsertRequest *request;
8624 file = g_file_new_for_uri (uri);
8626 if (!g_file_is_native (file))
8628 g_object_unref (file);
8632 filename = g_file_get_path (file);
8634 if (stat (filename, &statbuf) != 0)
8636 g_object_unref (file);
8641 statbuf_copy = g_new (struct stat, 1);
8642 *statbuf_copy = statbuf;
8644 tmp = g_file_get_parse_name (file);
8645 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8648 request = g_new0 (struct SearchHitInsertRequest, 1);
8649 request->impl = g_object_ref (impl);
8650 request->file = g_object_ref (file);
8652 gtk_list_store_append (impl->search_model, &iter);
8653 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8655 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8656 gtk_tree_path_free (p);
8658 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8659 "standard::type,standard::icon,"
8660 "standard::content-type,standard::display-name",
8661 search_hit_get_info_cb,
8664 gtk_list_store_set (impl->search_model, &iter,
8665 SEARCH_MODEL_COL_FILE, file,
8666 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8667 SEARCH_MODEL_COL_STAT, statbuf_copy,
8668 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8671 g_object_unref (file);
8675 /* Callback used from GtkSearchEngine when we get new hits */
8677 search_engine_hits_added_cb (GtkSearchEngine *engine,
8681 GtkFileChooserDefault *impl;
8684 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8686 for (l = hits; l; l = l->next)
8687 search_add_hit (impl, (gchar*)l->data);
8690 /* Callback used from GtkSearchEngine when the query is done running */
8692 search_engine_finished_cb (GtkSearchEngine *engine,
8695 GtkFileChooserDefault *impl;
8697 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8700 /* EB: setting the model here will avoid loads of row events,
8701 * but it'll make the search look like blocked.
8703 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8704 GTK_TREE_MODEL (impl->search_model_filter));
8707 /* FMQ: if search was empty, say that we got no hits */
8708 set_busy_cursor (impl, FALSE);
8711 /* Displays a generic error when we cannot create a GtkSearchEngine.
8712 * It would be better if _gtk_search_engine_new() gave us a GError
8713 * with a better message, but it doesn't do that right now.
8716 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8718 error_message (impl,
8719 _("Could not start the search process"),
8720 _("The program was not able to create a connection to the indexer "
8721 "daemon. Please make sure it is running."));
8725 search_engine_error_cb (GtkSearchEngine *engine,
8726 const gchar *message,
8729 GtkFileChooserDefault *impl;
8731 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8733 search_stop_searching (impl, TRUE);
8734 error_message (impl, _("Could not send the search request"), message);
8736 set_busy_cursor (impl, FALSE);
8739 /* Frees the data in the search_model */
8741 search_clear_model (GtkFileChooserDefault *impl,
8742 gboolean remove_from_treeview)
8744 GtkTreeModel *model;
8747 if (!impl->search_model)
8750 model = GTK_TREE_MODEL (impl->search_model);
8752 if (gtk_tree_model_get_iter_first (model, &iter))
8755 GCancellable *cancellable;
8757 gtk_tree_model_get (model, &iter,
8758 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8762 g_cancellable_cancel (cancellable);
8764 while (gtk_tree_model_iter_next (model, &iter));
8766 g_object_unref (impl->search_model);
8767 impl->search_model = NULL;
8769 g_object_unref (impl->search_model_filter);
8770 impl->search_model_filter = NULL;
8772 g_object_unref (impl->search_model_sort);
8773 impl->search_model_sort = NULL;
8775 if (remove_from_treeview)
8776 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8779 /* Stops any ongoing searches; does not touch the search_model */
8781 search_stop_searching (GtkFileChooserDefault *impl,
8782 gboolean remove_query)
8784 if (remove_query && impl->search_query)
8786 g_object_unref (impl->search_query);
8787 impl->search_query = NULL;
8790 if (impl->search_engine)
8792 _gtk_search_engine_stop (impl->search_engine);
8794 g_object_unref (impl->search_engine);
8795 impl->search_engine = NULL;
8799 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8801 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8803 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8805 search_stop_searching (impl, FALSE);
8806 search_clear_model (impl, TRUE);
8808 gtk_widget_destroy (impl->search_hbox);
8809 impl->search_hbox = NULL;
8810 impl->search_entry = NULL;
8812 gtk_widget_show (impl->browse_path_bar);
8813 gtk_widget_show (impl->browse_new_folder_button);
8815 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8816 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8818 gtk_widget_show (impl->location_button);
8820 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8821 gtk_widget_show (impl->location_entry_box);
8824 impl->operation_mode = OPERATION_MODE_BROWSE;
8826 file_list_set_sort_column_ids (impl);
8829 /* Sort callback from the path column */
8831 search_column_path_sort_func (GtkTreeModel *model,
8836 GtkFileChooserDefault *impl = user_data;
8837 GtkTreeIter child_a, child_b;
8838 const char *collation_key_a, *collation_key_b;
8839 gboolean is_folder_a, is_folder_b;
8841 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8842 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8844 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8845 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8846 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8848 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8849 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8850 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8853 if (!collation_key_a)
8856 if (!collation_key_b)
8859 /* always show folders first */
8860 if (is_folder_a != is_folder_b)
8861 return is_folder_a ? 1 : -1;
8863 return strcmp (collation_key_a, collation_key_b);
8866 /* Sort callback from the modification time column */
8868 search_column_mtime_sort_func (GtkTreeModel *model,
8873 GtkFileChooserDefault *impl = user_data;
8874 GtkTreeIter child_a, child_b;
8875 const struct stat *statbuf_a, *statbuf_b;
8876 gboolean is_folder_a, is_folder_b;
8878 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8879 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8881 /* Note that although we store a whole struct stat in the model, we only
8882 * compare the mtime here. If we add another column relative to a struct stat
8883 * (e.g. a file size column), we'll want another sort callback similar to this
8886 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8887 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8888 SEARCH_MODEL_COL_STAT, &statbuf_a,
8890 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8891 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8892 SEARCH_MODEL_COL_STAT, &statbuf_b,
8901 if (is_folder_a != is_folder_b)
8902 return is_folder_a ? 1 : -1;
8904 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8906 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8913 search_get_is_filtered (GtkFileChooserDefault *impl,
8915 const gchar *display_name,
8916 const gchar *mime_type)
8918 GtkFileFilterInfo filter_info;
8919 GtkFileFilterFlags needed;
8922 if (!impl->current_filter)
8925 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8926 needed = gtk_file_filter_get_needed (impl->current_filter);
8928 filter_info.display_name = display_name;
8929 filter_info.mime_type = mime_type;
8931 if (needed & GTK_FILE_FILTER_FILENAME)
8933 filter_info.filename = g_file_get_path (file);
8934 if (filter_info.filename)
8935 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8938 filter_info.filename = NULL;
8940 if (needed & GTK_FILE_FILTER_URI)
8942 filter_info.uri = g_file_get_uri (file);
8943 if (filter_info.uri)
8944 filter_info.contains |= GTK_FILE_FILTER_URI;
8947 filter_info.uri = NULL;
8949 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8951 if (filter_info.filename)
8952 g_free ((gchar *) filter_info.filename);
8953 if (filter_info.uri)
8954 g_free ((gchar *) filter_info.uri);
8960 /* Visibility function for the recent filter model */
8962 search_model_visible_func (GtkTreeModel *model,
8966 GtkFileChooserDefault *impl = user_data;
8968 gchar *display_name, *mime_type;
8971 if (!impl->current_filter)
8974 gtk_tree_model_get (model, iter,
8975 SEARCH_MODEL_COL_FILE, &file,
8976 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
8977 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8978 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8987 return !search_get_is_filtered (impl, file, display_name, mime_type);
8990 /* Creates the search_model and puts it in the tree view */
8992 search_setup_model (GtkFileChooserDefault *impl)
8994 g_assert (impl->search_model == NULL);
8995 g_assert (impl->search_model_filter == NULL);
8996 g_assert (impl->search_model_sort == NULL);
8998 /* We store these columns in the search model:
9000 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9001 * as a pointer not as a G_TYPE_FILE
9002 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9003 * as a pointer not as a G_TYPE_STRING
9004 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9005 * as a pointer not as a G_TYPE_STRING
9006 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9007 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9008 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9009 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9010 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9012 * Keep this in sync with the enumeration defined near the beginning
9015 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9025 impl->search_model_filter =
9026 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9027 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9028 search_model_visible_func,
9031 impl->search_model_sort =
9032 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9033 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9034 SEARCH_MODEL_COL_FILE,
9035 search_column_path_sort_func,
9037 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9038 SEARCH_MODEL_COL_STAT,
9039 search_column_mtime_sort_func,
9041 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9042 SEARCH_MODEL_COL_STAT,
9043 GTK_SORT_DESCENDING);
9045 /* EB: setting the model here will make the hits list update feel
9046 * more "alive" than setting the model at the end of the search
9049 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9050 GTK_TREE_MODEL (impl->search_model_sort));
9054 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9055 GtkTreeIter *child_iter,
9060 if (!impl->search_model)
9063 if (!impl->search_model_filter || !impl->search_model_sort)
9066 /* pass 1: get the iterator in the filter model */
9067 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9070 /* pass 2: get the iterator in the real model */
9071 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9072 child_iter, &middle);
9075 /* Creates a new query with the specified text and launches it */
9077 search_start_query (GtkFileChooserDefault *impl,
9078 const gchar *query_text)
9080 search_stop_searching (impl, FALSE);
9081 search_clear_model (impl, TRUE);
9082 search_setup_model (impl);
9083 set_busy_cursor (impl, TRUE);
9085 if (impl->search_engine == NULL)
9086 impl->search_engine = _gtk_search_engine_new ();
9088 if (!impl->search_engine)
9090 set_busy_cursor (impl, FALSE);
9091 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9095 if (!impl->search_query)
9097 impl->search_query = _gtk_query_new ();
9098 _gtk_query_set_text (impl->search_query, query_text);
9101 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9103 g_signal_connect (impl->search_engine, "hits-added",
9104 G_CALLBACK (search_engine_hits_added_cb), impl);
9105 g_signal_connect (impl->search_engine, "finished",
9106 G_CALLBACK (search_engine_finished_cb), impl);
9107 g_signal_connect (impl->search_engine, "error",
9108 G_CALLBACK (search_engine_error_cb), impl);
9110 _gtk_search_engine_start (impl->search_engine);
9113 /* Callback used when the user presses Enter while typing on the search
9114 * entry; starts the query
9117 search_entry_activate_cb (GtkEntry *entry,
9120 GtkFileChooserDefault *impl;
9123 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9125 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9126 if (strlen (text) == 0)
9129 /* reset any existing query object */
9130 if (impl->search_query)
9132 g_object_unref (impl->search_query);
9133 impl->search_query = NULL;
9136 search_start_query (impl, text);
9139 /* Hides the path bar and creates the search entry */
9141 search_setup_widgets (GtkFileChooserDefault *impl)
9145 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9149 label = gtk_label_new_with_mnemonic (_("_Search:"));
9150 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9154 impl->search_entry = gtk_entry_new ();
9155 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9156 g_signal_connect (impl->search_entry, "activate",
9157 G_CALLBACK (search_entry_activate_cb),
9159 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9161 /* if there already is a query, restart it */
9162 if (impl->search_query)
9164 gchar *query = _gtk_query_get_text (impl->search_query);
9168 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9169 search_start_query (impl, query);
9175 g_object_unref (impl->search_query);
9176 impl->search_query = NULL;
9180 gtk_widget_hide (impl->browse_path_bar);
9181 gtk_widget_hide (impl->browse_new_folder_button);
9183 /* Box for search widgets */
9184 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9185 gtk_widget_show_all (impl->search_hbox);
9187 /* Hide the location widgets temporarily */
9189 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9190 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9192 gtk_widget_hide (impl->location_button);
9193 gtk_widget_hide (impl->location_entry_box);
9196 gtk_widget_grab_focus (impl->search_entry);
9198 /* FMQ: hide the filter combo? */
9201 /* Main entry point to the searching functions; this gets called when the user
9202 * activates the Search shortcut.
9205 search_activate (GtkFileChooserDefault *impl)
9207 OperationMode previous_mode;
9209 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9211 gtk_widget_grab_focus (impl->search_entry);
9215 previous_mode = impl->operation_mode;
9216 impl->operation_mode = OPERATION_MODE_SEARCH;
9218 switch (previous_mode)
9220 case OPERATION_MODE_RECENT:
9221 recent_stop_loading (impl);
9222 recent_clear_model (impl, TRUE);
9225 case OPERATION_MODE_BROWSE:
9226 stop_loading_and_clear_list_model (impl);
9229 case OPERATION_MODE_SEARCH:
9230 g_assert_not_reached ();
9234 g_assert (impl->search_hbox == NULL);
9235 g_assert (impl->search_entry == NULL);
9236 g_assert (impl->search_model == NULL);
9237 g_assert (impl->search_model_filter == NULL);
9239 search_setup_widgets (impl);
9240 file_list_set_sort_column_ids (impl);
9244 * Recent files support
9247 /* Frees the data in the recent_model */
9249 recent_clear_model (GtkFileChooserDefault *impl,
9250 gboolean remove_from_treeview)
9252 GtkTreeModel *model;
9255 if (!impl->recent_model)
9258 model = GTK_TREE_MODEL (impl->recent_model);
9260 if (remove_from_treeview)
9261 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9263 if (gtk_tree_model_get_iter_first (model, &iter))
9268 GCancellable *cancellable;
9269 GtkRecentInfo *recent_info;
9270 gchar *display_name;
9272 gtk_tree_model_get (model, &iter,
9273 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9274 RECENT_MODEL_COL_FILE, &file,
9275 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9276 RECENT_MODEL_COL_INFO, &recent_info,
9280 g_cancellable_cancel (cancellable);
9282 g_object_unref (file);
9283 gtk_recent_info_unref (recent_info);
9284 g_free (display_name);
9286 while (gtk_tree_model_iter_next (model, &iter));
9289 g_object_unref (impl->recent_model);
9290 impl->recent_model = NULL;
9292 g_object_unref (impl->recent_model_filter);
9293 impl->recent_model_filter = NULL;
9295 g_object_unref (impl->recent_model_sort);
9296 impl->recent_model_sort = NULL;
9299 /* Stops any ongoing loading of the recent files list; does
9300 * not touch the recent_model
9303 recent_stop_loading (GtkFileChooserDefault *impl)
9305 if (impl->load_recent_id)
9307 g_source_remove (impl->load_recent_id);
9308 impl->load_recent_id = 0;
9312 /* Stops any pending load, clears the file list, and switches
9313 * back to OPERATION_MODE_BROWSE
9316 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9318 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9320 recent_stop_loading (impl);
9321 recent_clear_model (impl, TRUE);
9323 gtk_widget_show (impl->browse_path_bar);
9324 gtk_widget_show (impl->browse_new_folder_button);
9326 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9327 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9329 gtk_widget_show (impl->location_button);
9331 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9332 gtk_widget_show (impl->location_entry_box);
9335 impl->operation_mode = OPERATION_MODE_BROWSE;
9337 file_list_set_sort_column_ids (impl);
9340 /* Sort callback from the modification time column */
9342 recent_column_mtime_sort_func (GtkTreeModel *model,
9347 GtkFileChooserDefault *impl = user_data;
9348 GtkTreeIter child_a, child_b;
9349 GtkRecentInfo *info_a, *info_b;
9350 gboolean is_folder_a, is_folder_b;
9352 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9353 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9355 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9356 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9357 RECENT_MODEL_COL_INFO, &info_a,
9359 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9360 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9361 RECENT_MODEL_COL_INFO, &info_b,
9370 /* folders always go first */
9371 if (is_folder_a != is_folder_b)
9372 return is_folder_a ? 1 : -1;
9374 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9376 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9383 recent_column_path_sort_func (GtkTreeModel *model,
9388 GtkFileChooserDefault *impl = user_data;
9389 GtkTreeIter child_a, child_b;
9390 gboolean is_folder_a, is_folder_b;
9391 gchar *name_a, *name_b;
9393 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9394 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9396 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9397 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9398 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9400 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9401 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9402 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9411 if (is_folder_a != is_folder_b)
9412 return is_folder_a ? 1 : -1;
9414 return strcmp (name_a, name_b);
9418 recent_get_is_filtered (GtkFileChooserDefault *impl,
9420 GtkRecentInfo *recent_info)
9422 GtkFileFilterInfo filter_info;
9423 GtkFileFilterFlags needed;
9426 if (!impl->current_filter)
9429 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9430 needed = gtk_file_filter_get_needed (impl->current_filter);
9432 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9433 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9435 if (needed & GTK_FILE_FILTER_FILENAME)
9437 filter_info.filename = g_file_get_path (file);
9438 if (filter_info.filename)
9439 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9442 filter_info.filename = NULL;
9444 if (needed & GTK_FILE_FILTER_URI)
9446 filter_info.uri = g_file_get_uri (file);
9447 if (filter_info.uri)
9448 filter_info.contains |= GTK_FILE_FILTER_URI;
9451 filter_info.uri = NULL;
9453 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9455 if (filter_info.filename)
9456 g_free ((gchar *) filter_info.filename);
9457 if (filter_info.uri)
9458 g_free ((gchar *) filter_info.uri);
9463 /* Visibility function for the recent filter model */
9465 recent_model_visible_func (GtkTreeModel *model,
9469 GtkFileChooserDefault *impl = user_data;
9471 GtkRecentInfo *recent_info;
9474 if (!impl->current_filter)
9477 gtk_tree_model_get (model, iter,
9478 RECENT_MODEL_COL_INFO, &recent_info,
9479 RECENT_MODEL_COL_FILE, &file,
9480 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9489 return !recent_get_is_filtered (impl, file, recent_info);
9493 recent_setup_model (GtkFileChooserDefault *impl)
9495 g_assert (impl->recent_model == NULL);
9496 g_assert (impl->recent_model_filter == NULL);
9497 g_assert (impl->recent_model_sort == NULL);
9499 /* We store these columns in the search model:
9501 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9502 * stored as a pointer and not as a G_TYPE_FILE;
9503 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9504 * stored as a pointer and not as a G_TYPE_STRING;
9505 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9506 * as a GTK_TYPE_RECENT_INFO;
9507 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9508 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9509 * and not as a G_TYPE_CANCELLABLE;
9511 * Keep this in sync with the enumeration defined near the beginning of
9514 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9521 impl->recent_model_filter =
9522 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9523 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9524 recent_model_visible_func,
9528 /* this is the model that will actually be added to
9529 * the browse_files_tree_view widget; remember: we are
9530 * stuffing the real model into a filter model and then
9531 * into a sort model; this means we'll have to translate
9532 * the child iterator *twice* to get from a path or an
9533 * iterator coming from the tree view widget to the
9534 * real data inside the model.
9536 impl->recent_model_sort =
9537 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9538 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9539 RECENT_MODEL_COL_FILE,
9540 recent_column_path_sort_func,
9542 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9543 RECENT_MODEL_COL_INFO,
9544 recent_column_mtime_sort_func,
9546 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9547 RECENT_MODEL_COL_INFO,
9548 GTK_SORT_DESCENDING);
9553 GtkFileChooserDefault *impl;
9556 gint n_loaded_items;
9557 guint needs_sorting : 1;
9561 recent_idle_cleanup (gpointer data)
9563 RecentLoadData *load_data = data;
9564 GtkFileChooserDefault *impl = load_data->impl;
9566 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9567 GTK_TREE_MODEL (impl->recent_model_sort));
9569 set_busy_cursor (impl, FALSE);
9571 impl->load_recent_id = 0;
9573 if (load_data->items)
9575 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9576 g_list_free (load_data->items);
9582 struct RecentItemInsertRequest
9584 GtkFileChooserDefault *impl;
9586 GtkTreeRowReference *row_ref;
9590 recent_item_get_info_cb (GCancellable *cancellable,
9592 const GError *error,
9595 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9598 GCancellable *model_cancellable;
9599 gboolean is_folder = FALSE;
9600 struct RecentItemInsertRequest *request = data;
9602 if (!request->impl->recent_model)
9605 path = gtk_tree_row_reference_get_path (request->row_ref);
9609 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9611 gtk_tree_path_free (path);
9613 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9614 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9616 if (cancellable != model_cancellable)
9619 gtk_list_store_set (request->impl->recent_model, &iter,
9620 RECENT_MODEL_COL_CANCELLABLE, NULL,
9628 gtk_list_store_remove (request->impl->recent_model, &iter);
9632 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9634 gtk_list_store_set (request->impl->recent_model, &iter,
9635 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9639 g_object_unref (request->impl);
9640 g_object_unref (request->file);
9641 gtk_tree_row_reference_free (request->row_ref);
9644 g_object_unref (cancellable);
9648 recent_sort_mru (gconstpointer a,
9651 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9652 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9654 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9658 get_recent_files_limit (GtkWidget *widget)
9660 GtkSettings *settings;
9663 if (gtk_widget_has_screen (widget))
9664 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9666 settings = gtk_settings_get_default ();
9668 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9674 recent_idle_load (gpointer data)
9676 RecentLoadData *load_data = data;
9677 GtkFileChooserDefault *impl = load_data->impl;
9680 GtkRecentInfo *info;
9681 const gchar *uri, *display_name;
9683 GCancellable *cancellable;
9684 struct RecentItemInsertRequest *request;
9686 if (!impl->recent_manager)
9689 /* first iteration: load all the items */
9690 if (!load_data->items)
9692 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9693 if (!load_data->items)
9696 load_data->needs_sorting = TRUE;
9701 /* second iteration: preliminary MRU sorting and clamping */
9702 if (load_data->needs_sorting)
9706 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9707 load_data->n_items = g_list_length (load_data->items);
9709 limit = get_recent_files_limit (GTK_WIDGET (impl));
9711 if (limit != -1 && (load_data->n_items > limit))
9715 clamp = g_list_nth (load_data->items, limit - 1);
9716 if (G_LIKELY (clamp))
9721 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9724 load_data->n_items = limit;
9728 load_data->n_loaded_items = 0;
9729 load_data->needs_sorting = FALSE;
9734 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9735 g_assert (info != NULL);
9737 uri = gtk_recent_info_get_uri (info);
9738 display_name = gtk_recent_info_get_display_name (info);
9739 file = g_file_new_for_uri (uri);
9741 gtk_list_store_append (impl->recent_model, &iter);
9742 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9744 request = g_new0 (struct RecentItemInsertRequest, 1);
9745 request->impl = g_object_ref (impl);
9746 request->file = g_object_ref (file);
9747 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9748 gtk_tree_path_free (p);
9750 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9752 recent_item_get_info_cb,
9755 gtk_list_store_set (impl->recent_model, &iter,
9756 RECENT_MODEL_COL_FILE, file,
9757 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9758 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9759 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9762 load_data->n_loaded_items += 1;
9764 /* finished loading items */
9765 if (load_data->n_loaded_items == load_data->n_items)
9767 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9768 g_list_free (load_data->items);
9769 load_data->items = NULL;
9778 recent_start_loading (GtkFileChooserDefault *impl)
9780 RecentLoadData *load_data;
9782 recent_stop_loading (impl);
9783 recent_clear_model (impl, TRUE);
9784 recent_setup_model (impl);
9785 set_busy_cursor (impl, TRUE);
9787 g_assert (impl->load_recent_id == 0);
9789 load_data = g_new (RecentLoadData, 1);
9790 load_data->impl = impl;
9791 load_data->items = NULL;
9792 load_data->n_items = 0;
9793 load_data->n_loaded_items = 0;
9794 load_data->needs_sorting = TRUE;
9796 /* begin lazy loading the recent files into the model */
9797 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9800 recent_idle_cleanup);
9804 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9814 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9815 *list = g_slist_prepend (*list, g_object_ref (file));
9818 /* Constructs a list of the selected paths in recent files mode */
9820 recent_get_selected_files (GtkFileChooserDefault *impl)
9823 GtkTreeSelection *selection;
9827 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9828 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9829 result = g_slist_reverse (result);
9834 /* Called from ::should_respond(). We return whether there are selected
9835 * files in the recent files list.
9838 recent_should_respond (GtkFileChooserDefault *impl)
9840 GtkTreeSelection *selection;
9842 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9844 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9845 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9848 /* Hide the location widgets temporarily */
9850 recent_hide_entry (GtkFileChooserDefault *impl)
9852 gtk_widget_hide (impl->browse_path_bar);
9853 gtk_widget_hide (impl->browse_new_folder_button);
9855 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9856 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9858 gtk_widget_hide (impl->location_button);
9859 gtk_widget_hide (impl->location_entry_box);
9863 /* Main entry point to the recent files functions; this gets called when
9864 * the user activates the Recently Used shortcut.
9867 recent_activate (GtkFileChooserDefault *impl)
9869 OperationMode previous_mode;
9871 if (impl->operation_mode == OPERATION_MODE_RECENT)
9874 previous_mode = impl->operation_mode;
9875 impl->operation_mode = OPERATION_MODE_RECENT;
9877 switch (previous_mode)
9879 case OPERATION_MODE_SEARCH:
9880 search_stop_searching (impl, FALSE);
9881 search_clear_model (impl, TRUE);
9883 gtk_widget_destroy (impl->search_hbox);
9884 impl->search_hbox = NULL;
9885 impl->search_entry = NULL;
9888 case OPERATION_MODE_BROWSE:
9889 stop_loading_and_clear_list_model (impl);
9892 case OPERATION_MODE_RECENT:
9893 g_assert_not_reached ();
9897 recent_hide_entry (impl);
9898 file_list_set_sort_column_ids (impl);
9899 recent_start_loading (impl);
9902 /* convert an iterator coming from the model bound to
9903 * browse_files_tree_view to an interator inside the
9907 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9908 GtkTreeIter *child_iter,
9913 if (!impl->recent_model)
9916 if (!impl->recent_model_filter || !impl->recent_model_sort)
9919 /* pass 1: get the iterator in the filter model */
9920 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9923 /* pass 2: get the iterator in the real model */
9924 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9931 set_current_filter (GtkFileChooserDefault *impl,
9932 GtkFileFilter *filter)
9934 if (impl->current_filter != filter)
9938 /* NULL filters are allowed to reset to non-filtered status
9940 filter_index = g_slist_index (impl->filters, filter);
9941 if (impl->filters && filter && filter_index < 0)
9944 if (impl->current_filter)
9945 g_object_unref (impl->current_filter);
9946 impl->current_filter = filter;
9947 if (impl->current_filter)
9949 g_object_ref_sink (impl->current_filter);
9953 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9956 if (impl->browse_files_model)
9957 install_list_model_filter (impl);
9959 if (impl->search_model_filter)
9960 gtk_tree_model_filter_refilter (impl->search_model_filter);
9962 if (impl->recent_model_filter)
9963 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9965 g_object_notify (G_OBJECT (impl), "filter");
9970 filter_combo_changed (GtkComboBox *combo_box,
9971 GtkFileChooserDefault *impl)
9973 gint new_index = gtk_combo_box_get_active (combo_box);
9974 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9976 set_current_filter (impl, new_filter);
9980 check_preview_change (GtkFileChooserDefault *impl)
9982 GtkTreePath *cursor_path;
9984 const char *new_display_name;
9986 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9988 new_display_name = NULL;
9991 GtkTreeIter child_iter;
9993 if (impl->operation_mode == OPERATION_MODE_BROWSE)
9995 if (impl->sort_model)
9998 GFileInfo *new_info;
10000 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10001 gtk_tree_path_free (cursor_path);
10003 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10005 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10006 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10008 new_display_name = g_file_info_get_display_name (new_info);
10011 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10015 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10016 &iter, cursor_path);
10017 gtk_tree_path_free (cursor_path);
10019 search_get_valid_child_iter (impl, &child_iter, &iter);
10020 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10021 SEARCH_MODEL_COL_FILE, &new_file,
10022 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10025 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10029 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10030 &iter, cursor_path);
10031 gtk_tree_path_free (cursor_path);
10033 recent_get_valid_child_iter (impl, &child_iter, &iter);
10034 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10035 RECENT_MODEL_COL_FILE, &new_file,
10036 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10041 if (new_file != impl->preview_file &&
10042 !(new_file && impl->preview_file &&
10043 g_file_equal (new_file, impl->preview_file)))
10045 if (impl->preview_file)
10047 g_object_unref (impl->preview_file);
10048 g_free (impl->preview_display_name);
10053 impl->preview_file = g_object_ref (new_file);
10054 impl->preview_display_name = g_strdup (new_display_name);
10058 impl->preview_file = NULL;
10059 impl->preview_display_name = NULL;
10062 if (impl->use_preview_label && impl->preview_label)
10063 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10065 g_signal_emit_by_name (impl, "update-preview");
10070 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10071 GtkFileSystemVolume *volume,
10072 const GError *error,
10076 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10077 GtkFileChooserDefault *impl = data;
10079 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10082 impl->shortcuts_activate_iter_cancellable = NULL;
10084 set_busy_cursor (impl, FALSE);
10091 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10095 name = _gtk_file_system_volume_get_display_name (volume);
10096 msg = g_strdup_printf (_("Could not mount %s"), name);
10098 error_message (impl, msg, error->message);
10107 file = _gtk_file_system_volume_get_root (volume);
10110 change_folder_and_display_error (impl, file, FALSE);
10111 g_object_unref (file);
10115 g_object_unref (impl);
10116 g_object_unref (cancellable);
10120 /* Activates a volume by mounting it if necessary and then switching to its
10124 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10125 GtkFileSystemVolume *volume)
10129 switch (impl->operation_mode)
10131 case OPERATION_MODE_BROWSE:
10133 case OPERATION_MODE_SEARCH:
10134 search_switch_to_browse_mode (impl);
10136 case OPERATION_MODE_RECENT:
10137 recent_switch_to_browse_mode (impl);
10141 /* We ref the file chooser since volume_mount() may run a main loop, and the
10142 * user could close the file chooser window in the meantime.
10144 g_object_ref (impl);
10146 if (!_gtk_file_system_volume_is_mounted (volume))
10148 set_busy_cursor (impl, TRUE);
10150 impl->shortcuts_activate_iter_cancellable =
10151 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10152 shortcuts_activate_volume_mount_cb,
10153 g_object_ref (impl));
10157 file = _gtk_file_system_volume_get_root (volume);
10160 change_folder_and_display_error (impl, file, FALSE);
10161 g_object_unref (file);
10165 g_object_unref (impl);
10168 /* Opens the folder or volume at the specified iter in the shortcuts model */
10169 struct ShortcutsActivateData
10171 GtkFileChooserDefault *impl;
10176 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10178 const GError *error,
10179 gpointer user_data)
10181 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10182 struct ShortcutsActivateData *data = user_data;
10184 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10187 data->impl->shortcuts_activate_iter_cancellable = NULL;
10192 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10193 change_folder_and_display_error (data->impl, data->file, FALSE);
10195 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10200 g_object_unref (data->impl);
10201 g_object_unref (data->file);
10204 g_object_unref (cancellable);
10208 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10209 GtkFileSystemVolume *volume,
10210 const GError *error,
10211 gpointer user_data)
10213 struct ShortcutsActivateData *data = user_data;
10217 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10219 g_object_unref (data->impl);
10220 g_object_unref (data->file);
10226 data->impl->shortcuts_activate_iter_cancellable =
10227 _gtk_file_system_get_info (data->impl->file_system, data->file,
10229 shortcuts_activate_get_info_cb, data);
10233 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10237 ShortcutType shortcut_type;
10239 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10240 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10241 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10242 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10244 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10245 SHORTCUTS_COL_DATA, &col_data,
10246 SHORTCUTS_COL_TYPE, &shortcut_type,
10249 if (impl->shortcuts_activate_iter_cancellable)
10251 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10252 impl->shortcuts_activate_iter_cancellable = NULL;
10255 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10257 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10259 GtkFileSystemVolume *volume;
10263 shortcuts_activate_volume (impl, volume);
10265 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10267 struct ShortcutsActivateData *data;
10268 GtkFileSystemVolume *volume;
10270 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10272 data = g_new0 (struct ShortcutsActivateData, 1);
10273 data->impl = g_object_ref (impl);
10274 data->file = g_object_ref (col_data);
10276 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10278 GMountOperation *mount_operation;
10279 GtkWidget *toplevel;
10281 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10283 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10285 impl->shortcuts_activate_iter_cancellable =
10286 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10288 shortcuts_activate_mount_enclosing_volume,
10293 impl->shortcuts_activate_iter_cancellable =
10294 _gtk_file_system_get_info (impl->file_system, data->file,
10296 shortcuts_activate_get_info_cb, data);
10299 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10301 search_activate (impl);
10303 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10305 recent_activate (impl);
10309 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10311 shortcuts_key_press_event_cb (GtkWidget *widget,
10312 GdkEventKey *event,
10313 GtkFileChooserDefault *impl)
10317 modifiers = gtk_accelerator_get_default_mod_mask ();
10319 if ((event->keyval == GDK_BackSpace
10320 || event->keyval == GDK_Delete
10321 || event->keyval == GDK_KP_Delete)
10322 && (event->state & modifiers) == 0)
10324 remove_selected_bookmarks (impl);
10328 if ((event->keyval == GDK_F2)
10329 && (event->state & modifiers) == 0)
10331 rename_selected_bookmark (impl);
10339 shortcuts_select_func (GtkTreeSelection *selection,
10340 GtkTreeModel *model,
10342 gboolean path_currently_selected,
10345 GtkFileChooserDefault *impl = data;
10346 GtkTreeIter filter_iter;
10347 ShortcutType shortcut_type;
10349 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10350 g_assert_not_reached ();
10352 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10354 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10358 list_select_func (GtkTreeSelection *selection,
10359 GtkTreeModel *model,
10361 gboolean path_currently_selected,
10364 GtkFileChooserDefault *impl = data;
10366 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10367 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10369 GtkTreeIter iter, child_iter;
10371 switch (impl->operation_mode)
10373 case OPERATION_MODE_SEARCH:
10375 gboolean is_folder;
10377 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10380 search_get_valid_child_iter (impl, &child_iter, &iter);
10381 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10382 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10389 case OPERATION_MODE_RECENT:
10391 gboolean is_folder;
10393 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10396 recent_get_valid_child_iter (impl, &child_iter, &iter);
10397 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10398 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10405 case OPERATION_MODE_BROWSE:
10409 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10412 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10413 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10414 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10425 list_selection_changed (GtkTreeSelection *selection,
10426 GtkFileChooserDefault *impl)
10428 /* See if we are in the new folder editable row for Save mode */
10429 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10430 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10433 gboolean had_selection;
10435 info = get_selected_file_info_from_file_list (impl, &had_selection);
10436 if (!had_selection)
10437 goto out; /* normal processing */
10440 return; /* We are on the editable row for New Folder */
10445 if (impl->location_entry)
10446 update_chooser_entry (impl);
10448 check_preview_change (impl);
10449 bookmarks_check_add_sensitivity (impl);
10451 g_signal_emit_by_name (impl, "selection-changed", 0);
10454 /* Callback used when a row in the file list is activated */
10456 list_row_activated (GtkTreeView *tree_view,
10458 GtkTreeViewColumn *column,
10459 GtkFileChooserDefault *impl)
10462 GtkTreeIter child_iter;
10464 switch (impl->operation_mode)
10466 case OPERATION_MODE_SEARCH:
10469 gboolean is_folder;
10471 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10474 search_get_valid_child_iter (impl, &child_iter, &iter);
10475 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10476 SEARCH_MODEL_COL_FILE, &file,
10477 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10482 change_folder_and_display_error (impl, file, FALSE);
10486 g_signal_emit_by_name (impl, "file-activated");
10490 case OPERATION_MODE_RECENT:
10493 gboolean is_folder;
10495 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10498 recent_get_valid_child_iter (impl, &child_iter, &iter);
10499 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10500 RECENT_MODEL_COL_FILE, &file,
10501 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10506 change_folder_and_display_error (impl, file, FALSE);
10510 g_signal_emit_by_name (impl, "file-activated");
10514 case OPERATION_MODE_BROWSE:
10518 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10521 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10522 &child_iter, &iter);
10523 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10526 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10530 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10531 change_folder_and_display_error (impl, file, FALSE);
10535 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10536 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10537 g_signal_emit_by_name (impl, "file-activated");
10544 path_bar_clicked (GtkPathBar *path_bar,
10547 gboolean child_is_hidden,
10548 GtkFileChooserDefault *impl)
10551 pending_select_files_add (impl, child_file);
10553 if (!change_folder_and_display_error (impl, file, FALSE))
10556 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10557 * show hidden files so that ".baz" appears in the file list, as it will still
10558 * be shown in the path bar: "/foo/[bar]/.baz"
10560 if (child_is_hidden)
10561 g_object_set (impl, "show-hidden", TRUE, NULL);
10565 get_list_file_info (GtkFileChooserDefault *impl,
10568 GtkTreeIter child_iter;
10570 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10574 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10578 list_icon_data_func (GtkTreeViewColumn *tree_column,
10579 GtkCellRenderer *cell,
10580 GtkTreeModel *tree_model,
10584 GtkFileChooserDefault *impl = data;
10585 GtkTreeIter child_iter;
10586 GdkPixbuf *pixbuf = NULL;
10587 gboolean sensitive = TRUE;
10589 profile_start ("start", NULL);
10591 switch (impl->operation_mode)
10593 case OPERATION_MODE_SEARCH:
10595 GtkTreeIter child_iter;
10596 gboolean is_folder;
10598 search_get_valid_child_iter (impl, &child_iter, iter);
10599 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10600 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10601 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10604 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10605 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10606 sensitive = is_folder;
10610 case OPERATION_MODE_RECENT:
10612 GtkTreeIter child_iter;
10613 GtkRecentInfo *info;
10614 gboolean is_folder;
10616 recent_get_valid_child_iter (impl, &child_iter, iter);
10617 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10618 RECENT_MODEL_COL_INFO, &info,
10619 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10622 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10624 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10625 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10626 sensitive = is_folder;
10630 case OPERATION_MODE_BROWSE:
10635 info = get_list_file_info (impl, iter);
10637 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10640 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10645 /* FIXME: NULL GError */
10646 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10651 /* We are on the editable row */
10656 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10657 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10658 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10663 g_object_set (cell,
10665 "sensitive", sensitive,
10669 g_object_unref (pixbuf);
10671 profile_end ("end", NULL);
10675 list_name_data_func (GtkTreeViewColumn *tree_column,
10676 GtkCellRenderer *cell,
10677 GtkTreeModel *tree_model,
10681 GtkFileChooserDefault *impl = data;
10683 gboolean sensitive = TRUE;
10685 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10687 GtkTreeIter child_iter;
10688 gchar *display_name;
10689 gboolean is_folder;
10691 search_get_valid_child_iter (impl, &child_iter, iter);
10692 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10693 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10694 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10697 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10698 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10700 sensitive = is_folder;
10703 g_object_set (cell,
10704 "text", display_name,
10705 "sensitive", sensitive,
10706 "ellipsize", PANGO_ELLIPSIZE_END,
10712 if (impl->operation_mode == OPERATION_MODE_RECENT)
10714 GtkTreeIter child_iter;
10715 GtkRecentInfo *recent_info;
10716 gchar *display_name;
10717 gboolean is_folder;
10719 recent_get_valid_child_iter (impl, &child_iter, iter);
10720 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10721 RECENT_MODEL_COL_INFO, &recent_info,
10722 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10725 display_name = gtk_recent_info_get_short_name (recent_info);
10727 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10728 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10730 sensitive = is_folder;
10733 g_object_set (cell,
10734 "text", display_name,
10735 "sensitive", sensitive,
10736 "ellipsize", PANGO_ELLIPSIZE_END,
10739 g_free (display_name);
10744 info = get_list_file_info (impl, iter);
10749 g_object_set (cell,
10750 "text", _("Type name of new folder"),
10752 "ellipsize", PANGO_ELLIPSIZE_NONE,
10759 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10760 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10762 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10765 g_object_set (cell,
10766 "text", g_file_info_get_display_name (info),
10767 "sensitive", sensitive,
10768 "ellipsize", PANGO_ELLIPSIZE_END,
10774 list_size_data_func (GtkTreeViewColumn *tree_column,
10775 GtkCellRenderer *cell,
10776 GtkTreeModel *tree_model,
10780 GtkFileChooserDefault *impl = data;
10781 GFileInfo *info = get_list_file_info (impl, iter);
10784 gboolean sensitive = TRUE;
10786 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10788 g_object_set (cell,
10790 "sensitive", sensitive,
10795 size = gtk_file_info_get_size (info);
10797 if (size < (gint64)1024)
10798 str = g_strdup_printf (g_dngettext (GETTEXT_DOMAIN, "%d byte", "%d bytes", (gint)size), (gint)size);
10799 else if (size < (gint64)1024*1024)
10800 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10801 else if (size < (gint64)1024*1024*1024)
10802 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10804 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10806 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10807 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10808 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10811 g_object_set (cell,
10813 "sensitive", sensitive,
10814 "alignment", PANGO_ALIGN_RIGHT,
10821 /* Tree column data callback for the file list; fetches the mtime of a file */
10823 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10824 GtkCellRenderer *cell,
10825 GtkTreeModel *tree_model,
10829 GtkFileChooserDefault *impl;
10830 GTimeVal timeval = { 0, };
10832 gchar *date_str = NULL;
10833 gboolean sensitive = TRUE;
10835 const char *locale, *dot = NULL;
10836 gint64 codepage = -1;
10842 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10844 GtkTreeIter child_iter;
10845 struct stat *statbuf;
10846 gboolean is_folder;
10848 search_get_valid_child_iter (impl, &child_iter, iter);
10849 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10850 SEARCH_MODEL_COL_STAT, &statbuf,
10851 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10854 time_mtime = statbuf->st_mtime;
10859 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10860 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10861 sensitive = is_folder;
10863 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10865 GtkTreeIter child_iter;
10866 GtkRecentInfo *info;
10867 gboolean is_folder;
10869 recent_get_valid_child_iter (impl, &child_iter, iter);
10870 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10871 RECENT_MODEL_COL_INFO, &info,
10872 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10876 time_mtime = gtk_recent_info_get_modified (info);
10880 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10881 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10882 sensitive = is_folder;
10888 info = get_list_file_info (impl, iter);
10891 g_object_set (cell,
10898 g_file_info_get_modification_time (info, &timeval);
10899 time_mtime = timeval.tv_sec;
10901 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10902 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10903 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10906 if (G_UNLIKELY (time_mtime == 0))
10907 date_str = g_strdup (_("Unknown"));
10912 struct tm tm_mtime;
10914 const gchar *format;
10915 gchar *locale_format = NULL;
10918 #ifdef HAVE_LOCALTIME_R
10919 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10922 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10926 g_warning ("ptm != NULL failed");
10928 g_object_set (cell,
10929 "text", _("Unknown"),
10930 "sensitive", sensitive,
10935 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10937 #endif /* HAVE_LOCALTIME_R */
10939 g_date_set_time_t (&mtime, time_mtime);
10940 time_now = time (NULL);
10941 g_date_set_time_t (&now, time_now);
10943 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10945 /* Translators: %H means "hours" and %M means "minutes" */
10946 if (days_diff == 0)
10947 format = _("%H:%M");
10948 else if (days_diff == 1)
10949 format = _("Yesterday at %H:%M");
10952 if (days_diff > 1 && days_diff < 7)
10953 format = "%A"; /* Days from last week */
10955 format = "%x"; /* Any other date */
10959 /* g_locale_from_utf8() returns a string in the system
10960 * code-page, which is not always the same as that used by the C
10961 * library. For instance when running a GTK+ program with
10962 * LANG=ko on an English version of Windows, the system
10963 * code-page is 1252, but the code-page used by the C library is
10964 * 949. (It's GTK+ itself that sets the C library locale when it
10965 * notices the LANG environment variable. See gtkmain.c The
10966 * Microsoft C library doesn't look at any locale environment
10967 * variables.) We need to pass strftime() a string in the C
10968 * library's code-page. See bug #509885.
10970 locale = setlocale (LC_ALL, NULL);
10971 if (locale != NULL)
10972 dot = strchr (locale, '.');
10975 codepage = g_ascii_strtoll (dot+1, NULL, 10);
10977 /* All codepages should fit in 16 bits AFAIK */
10978 if (codepage > 0 && codepage < 65536)
10980 sprintf (charset, "CP%u", (guint) codepage);
10981 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
10985 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
10987 if (locale_format != NULL &&
10988 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
10991 /* As above but in opposite direction... */
10992 if (codepage > 0 && codepage < 65536)
10993 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
10995 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
10999 if (date_str == NULL)
11000 date_str = g_strdup (_("Unknown"));
11002 g_free (locale_format);
11005 g_object_set (cell,
11007 "sensitive", sensitive,
11013 _gtk_file_chooser_default_new (void)
11015 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11019 location_set_user_text (GtkFileChooserDefault *impl,
11022 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11023 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11027 location_popup_handler (GtkFileChooserDefault *impl,
11030 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11032 GtkWidget *widget_to_focus;
11034 /* This will give us the location widgets back */
11035 switch (impl->operation_mode)
11037 case OPERATION_MODE_SEARCH:
11038 search_switch_to_browse_mode (impl);
11040 case OPERATION_MODE_RECENT:
11041 recent_switch_to_browse_mode (impl);
11043 case OPERATION_MODE_BROWSE:
11044 g_assert_not_reached ();
11048 if (impl->current_folder)
11049 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11051 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11052 widget_to_focus = impl->browse_files_tree_view;
11054 widget_to_focus = impl->location_entry;
11056 gtk_widget_grab_focus (widget_to_focus);
11060 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11061 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11063 LocationMode new_mode;
11067 /* since the user typed something, we unconditionally want to turn on the entry */
11068 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11070 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11071 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11072 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11073 new_mode = LOCATION_MODE_PATH_BAR;
11076 g_assert_not_reached ();
11080 location_mode_set (impl, new_mode, TRUE);
11081 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11084 location_set_user_text (impl, path);
11087 location_entry_set_initial_text (impl);
11088 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11092 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11093 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11095 gtk_widget_grab_focus (impl->location_entry);
11097 location_set_user_text (impl, path);
11100 g_assert_not_reached ();
11103 /* Handler for the "up-folder" keybinding signal */
11105 up_folder_handler (GtkFileChooserDefault *impl)
11107 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11110 /* Handler for the "down-folder" keybinding signal */
11112 down_folder_handler (GtkFileChooserDefault *impl)
11114 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11117 /* Switches to the shortcut in the specified index */
11119 switch_to_shortcut (GtkFileChooserDefault *impl,
11124 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11125 g_assert_not_reached ();
11127 shortcuts_activate_iter (impl, &iter);
11130 /* Handler for the "home-folder" keybinding signal */
11132 home_folder_handler (GtkFileChooserDefault *impl)
11134 if (impl->has_home)
11135 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11138 /* Handler for the "desktop-folder" keybinding signal */
11140 desktop_folder_handler (GtkFileChooserDefault *impl)
11142 if (impl->has_desktop)
11143 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11146 /* Handler for the "search-shortcut" keybinding signal */
11148 search_shortcut_handler (GtkFileChooserDefault *impl)
11150 if (impl->has_search)
11152 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11154 /* we want the entry widget to grab the focus the first
11155 * time, not the browse_files_tree_view widget.
11157 if (impl->search_entry)
11158 gtk_widget_grab_focus (impl->search_entry);
11162 /* Handler for the "recent-shortcut" keybinding signal */
11164 recent_shortcut_handler (GtkFileChooserDefault *impl)
11166 if (impl->has_recent)
11167 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11171 quick_bookmark_handler (GtkFileChooserDefault *impl,
11172 gint bookmark_index)
11177 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11180 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11182 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11183 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11186 gtk_tree_path_free (path);
11188 switch_to_shortcut (impl, bookmark_pos);
11192 show_hidden_handler (GtkFileChooserDefault *impl)
11194 g_object_set (impl,
11195 "show-hidden", !impl->show_hidden,
11200 /* Drag and drop interfaces */
11203 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11208 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11210 model->impl = NULL;
11213 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11215 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11218 ShortcutsPaneModelFilter *model;
11222 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11224 pos = *gtk_tree_path_get_indices (path);
11225 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11227 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11230 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11232 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11234 GtkSelectionData *selection_data)
11236 ShortcutsPaneModelFilter *model;
11238 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11245 /* Fill the GtkTreeDragSourceIface vtable */
11247 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11249 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11250 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11254 /* Fill the GtkTreeDragDestIface vtable */
11256 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11258 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11259 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11263 static GtkTreeModel *
11264 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11265 GtkTreeModel *child_model,
11268 ShortcutsPaneModelFilter *model;
11270 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11271 "child-model", child_model,
11272 "virtual-root", root,
11275 model->impl = impl;
11277 return GTK_TREE_MODEL (model);
11283 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11286 RecentModelSort *model;
11287 GtkTreeIter iter, child_iter;
11288 gboolean is_folder;
11290 model = RECENT_MODEL_SORT (drag_source);
11291 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11294 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11295 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11296 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11303 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11305 GtkSelectionData *selection_data)
11307 RecentModelSort *model;
11308 GtkTreeIter iter, child_iter;
11312 model = RECENT_MODEL_SORT (drag_source);
11313 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11316 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11317 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11318 RECENT_MODEL_COL_FILE, &file,
11320 g_assert (file != NULL);
11322 uris[0] = g_file_get_uri (file);
11325 gtk_selection_data_set_uris (selection_data, uris);
11333 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11335 iface->row_draggable = recent_model_sort_row_draggable;
11336 iface->drag_data_get = recent_model_sort_drag_data_get;
11340 _recent_model_sort_class_init (RecentModelSortClass *klass)
11346 _recent_model_sort_init (RecentModelSort *model)
11348 model->impl = NULL;
11351 static GtkTreeModel *
11352 recent_model_sort_new (GtkFileChooserDefault *impl,
11353 GtkTreeModel *child_model)
11355 RecentModelSort *model;
11357 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11358 "model", child_model,
11360 model->impl = impl;
11362 return GTK_TREE_MODEL (model);
11368 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11371 SearchModelSort *model;
11372 GtkTreeIter iter, child_iter;
11373 gboolean is_folder;
11375 model = SEARCH_MODEL_SORT (drag_source);
11376 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11379 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11380 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11381 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11388 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11390 GtkSelectionData *selection_data)
11392 SearchModelSort *model;
11393 GtkTreeIter iter, child_iter;
11397 model = SEARCH_MODEL_SORT (drag_source);
11398 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11401 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11402 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11403 RECENT_MODEL_COL_FILE, &file,
11405 g_assert (file != NULL);
11407 uris[0] = g_file_get_uri (file);
11410 gtk_selection_data_set_uris (selection_data, uris);
11418 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11420 iface->row_draggable = search_model_sort_row_draggable;
11421 iface->drag_data_get = search_model_sort_drag_data_get;
11425 _search_model_sort_class_init (SearchModelSortClass *klass)
11431 _search_model_sort_init (SearchModelSort *model)
11433 model->impl = NULL;
11436 static GtkTreeModel *
11437 search_model_sort_new (GtkFileChooserDefault *impl,
11438 GtkTreeModel *child_model)
11440 SearchModelSort *model;
11442 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11443 "model", child_model,
11445 model->impl = impl;
11447 return GTK_TREE_MODEL (model);