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,
410 static void list_size_data_func (GtkTreeViewColumn *tree_column,
411 GtkCellRenderer *cell,
412 GtkTreeModel *tree_model,
415 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
416 GtkCellRenderer *cell,
417 GtkTreeModel *tree_model,
421 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
424 static void load_remove_timer (GtkFileChooserDefault *impl);
425 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
427 static void location_button_toggled_cb (GtkToggleButton *toggle,
428 GtkFileChooserDefault *impl);
429 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
431 static void search_stop_searching (GtkFileChooserDefault *impl,
432 gboolean remove_query);
433 static void search_clear_model_row (GtkTreeModel *model,
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->show_size_column = FALSE;
796 impl->icon_size = FALLBACK_ICON_SIZE;
797 impl->load_state = LOAD_EMPTY;
798 impl->reload_state = RELOAD_EMPTY;
799 impl->pending_select_files = NULL;
800 impl->location_mode = LOCATION_MODE_PATH_BAR;
801 impl->operation_mode = OPERATION_MODE_BROWSE;
802 impl->recent_manager = gtk_recent_manager_get_default ();
804 gtk_box_set_spacing (GTK_BOX (impl), 12);
806 set_file_system_backend (impl);
808 profile_end ("end", NULL);
811 /* Frees the data columns for the specified iter in the shortcuts model*/
813 shortcuts_free_row_data (GtkFileChooserDefault *impl,
817 ShortcutType shortcut_type;
818 GCancellable *cancellable;
820 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
821 SHORTCUTS_COL_DATA, &col_data,
822 SHORTCUTS_COL_TYPE, &shortcut_type,
823 SHORTCUTS_COL_CANCELLABLE, &cancellable,
827 g_cancellable_cancel (cancellable);
829 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
830 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
834 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
836 GtkFileSystemVolume *volume;
839 _gtk_file_system_volume_free (volume);
845 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
848 g_object_unref (file);
852 /* Frees all the data columns in the shortcuts model */
854 shortcuts_free (GtkFileChooserDefault *impl)
858 if (!impl->shortcuts_model)
861 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
864 shortcuts_free_row_data (impl, &iter);
866 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
868 g_object_unref (impl->shortcuts_model);
869 impl->shortcuts_model = NULL;
873 pending_select_files_free (GtkFileChooserDefault *impl)
875 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
876 g_slist_free (impl->pending_select_files);
877 impl->pending_select_files = NULL;
881 pending_select_files_add (GtkFileChooserDefault *impl,
884 impl->pending_select_files =
885 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
888 /* Used from gtk_tree_selection_selected_foreach() */
890 store_selection_foreach (GtkTreeModel *model,
895 GtkFileChooserDefault *impl;
896 GtkTreeIter child_iter;
899 impl = GTK_FILE_CHOOSER_DEFAULT (data);
901 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
903 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
904 pending_select_files_add (impl, file);
907 /* Stores the current selection in the list of paths to select; this is used to
908 * preserve the selection when reloading the current folder.
911 pending_select_files_store_selection (GtkFileChooserDefault *impl)
913 GtkTreeSelection *selection;
915 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
916 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
920 gtk_file_chooser_default_finalize (GObject *object)
922 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
925 if (impl->shortcuts_pane_filter_model)
926 g_object_unref (impl->shortcuts_pane_filter_model);
928 if (impl->shortcuts_combo_filter_model)
929 g_object_unref (impl->shortcuts_combo_filter_model);
931 shortcuts_free (impl);
933 g_object_unref (impl->file_system);
935 g_free (impl->browse_files_last_selected_name);
937 for (l = impl->filters; l; l = l->next)
939 GtkFileFilter *filter;
941 filter = GTK_FILE_FILTER (l->data);
942 g_object_unref (filter);
944 g_slist_free (impl->filters);
946 if (impl->current_filter)
947 g_object_unref (impl->current_filter);
949 if (impl->current_volume_file)
950 g_object_unref (impl->current_volume_file);
952 if (impl->current_folder)
953 g_object_unref (impl->current_folder);
955 if (impl->preview_file)
956 g_object_unref (impl->preview_file);
958 load_remove_timer (impl);
960 /* Free all the Models we have */
961 if (impl->browse_files_model)
962 g_object_unref (impl->browse_files_model);
964 if (impl->sort_model)
965 g_object_unref (impl->sort_model);
967 search_clear_model (impl, FALSE);
968 recent_clear_model (impl, FALSE);
970 g_free (impl->preview_display_name);
972 g_free (impl->edited_new_text);
974 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
977 /* Shows an error dialog set as transient for the specified window */
979 error_message_with_parent (GtkWindow *parent,
985 dialog = gtk_message_dialog_new (parent,
986 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
991 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
994 if (parent && parent->group)
995 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
997 gtk_dialog_run (GTK_DIALOG (dialog));
998 gtk_widget_destroy (dialog);
1001 /* Returns a toplevel GtkWindow, or NULL if none */
1003 get_toplevel (GtkWidget *widget)
1005 GtkWidget *toplevel;
1007 toplevel = gtk_widget_get_toplevel (widget);
1008 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1011 return GTK_WINDOW (toplevel);
1014 /* Shows an error dialog for the file chooser */
1016 error_message (GtkFileChooserDefault *impl,
1020 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1023 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1025 error_dialog (GtkFileChooserDefault *impl,
1036 uri = g_file_get_uri (file);
1037 text = g_strdup_printf (msg, uri);
1038 error_message (impl, text, error->message);
1041 g_error_free (error);
1045 /* Displays an error message about not being able to get information for a file.
1046 * Frees the GError as well.
1049 error_getting_info_dialog (GtkFileChooserDefault *impl,
1054 _("Could not retrieve information about the file"),
1058 /* Shows an error dialog about not being able to add a bookmark */
1060 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1065 _("Could not add a bookmark"),
1069 /* Shows an error dialog about not being able to remove a bookmark */
1071 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1076 _("Could not remove bookmark"),
1080 /* Shows an error dialog about not being able to create a folder */
1082 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1087 _("The folder could not be created"),
1091 /* Shows an error about not being able to create a folder because a file with
1092 * the same name is already there.
1095 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1100 _("The folder could not be created, as a file with the same "
1101 "name already exists. Try using a different name for the "
1102 "folder, or rename the file first."),
1106 /* Shows an error dialog about not being able to create a filename */
1108 error_building_filename_dialog (GtkFileChooserDefault *impl,
1111 error_dialog (impl, _("Invalid file name"),
1115 /* Shows an error dialog when we cannot switch to a folder */
1117 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1121 error_dialog (impl, _("The folder contents could not be displayed"),
1125 /* Changes folders, displaying an error dialog if this fails */
1127 change_folder_and_display_error (GtkFileChooserDefault *impl,
1129 gboolean clear_entry)
1134 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1136 /* We copy the path because of this case:
1138 * list_row_activated()
1139 * fetches path from model; path belongs to the model (*)
1140 * calls change_folder_and_display_error()
1141 * calls gtk_file_chooser_set_current_folder_file()
1142 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1146 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1149 error_changing_folder_dialog (impl, file, error);
1155 emit_default_size_changed (GtkFileChooserDefault *impl)
1157 if (!GTK_WIDGET_MAPPED (impl))
1160 profile_msg (" emit default-size-changed start", NULL);
1161 g_signal_emit_by_name (impl, "default-size-changed");
1162 profile_msg (" emit default-size-changed end", NULL);
1166 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1168 if (impl->use_preview_label)
1170 if (!impl->preview_label)
1172 impl->preview_label = gtk_label_new (impl->preview_display_name);
1173 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1174 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1175 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1176 gtk_widget_show (impl->preview_label);
1181 if (impl->preview_label)
1183 gtk_widget_destroy (impl->preview_label);
1184 impl->preview_label = NULL;
1188 if (impl->preview_widget_active && impl->preview_widget)
1189 gtk_widget_show (impl->preview_box);
1191 gtk_widget_hide (impl->preview_box);
1193 emit_default_size_changed (impl);
1197 set_preview_widget (GtkFileChooserDefault *impl,
1198 GtkWidget *preview_widget)
1200 if (preview_widget == impl->preview_widget)
1203 if (impl->preview_widget)
1204 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1205 impl->preview_widget);
1207 impl->preview_widget = preview_widget;
1208 if (impl->preview_widget)
1210 gtk_widget_show (impl->preview_widget);
1211 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1212 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1213 impl->preview_widget,
1214 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1217 update_preview_widget_visibility (impl);
1220 /* Renders a "Search" icon at an appropriate size for a tree view */
1222 render_search_icon (GtkFileChooserDefault *impl)
1224 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1228 render_recent_icon (GtkFileChooserDefault *impl)
1230 GtkIconTheme *theme;
1233 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1234 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1236 theme = gtk_icon_theme_get_default ();
1238 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1244 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1250 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1251 struct ReloadIconsData
1253 GtkFileChooserDefault *impl;
1254 GtkTreeRowReference *row_ref;
1258 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1260 const GError *error,
1266 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1267 struct ReloadIconsData *data = user_data;
1269 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1272 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1274 if (cancelled || error)
1277 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1279 path = gtk_tree_row_reference_get_path (data->row_ref);
1280 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1281 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1282 SHORTCUTS_COL_PIXBUF, pixbuf,
1284 gtk_tree_path_free (path);
1287 g_object_unref (pixbuf);
1290 gtk_tree_row_reference_free (data->row_ref);
1291 g_object_unref (data->impl);
1294 g_object_unref (cancellable);
1298 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1303 profile_start ("start", NULL);
1305 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1308 for (l = impl->reload_icon_cancellables; l; l = l->next)
1310 GCancellable *cancellable = G_CANCELLABLE (l->data);
1311 g_cancellable_cancel (cancellable);
1313 g_slist_free (impl->reload_icon_cancellables);
1314 impl->reload_icon_cancellables = NULL;
1319 ShortcutType shortcut_type;
1320 gboolean pixbuf_visible;
1323 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1324 SHORTCUTS_COL_DATA, &data,
1325 SHORTCUTS_COL_TYPE, &shortcut_type,
1326 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1332 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1334 GtkFileSystemVolume *volume;
1337 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1338 impl->icon_size, NULL);
1340 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1342 if (g_file_is_native (G_FILE (data)))
1345 struct ReloadIconsData *info;
1346 GtkTreePath *tree_path;
1347 GCancellable *cancellable;
1351 info = g_new0 (struct ReloadIconsData, 1);
1352 info->impl = g_object_ref (impl);
1353 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1354 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1355 gtk_tree_path_free (tree_path);
1357 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1359 shortcuts_reload_icons_get_info_cb,
1361 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1365 GtkIconTheme *icon_theme;
1367 /* Don't call get_info for remote paths to avoid latency and
1369 * If we switch to a better bookmarks file format (XBEL), we
1370 * should use mime info to get a better icon.
1372 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1373 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1374 impl->icon_size, 0, NULL);
1377 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1379 pixbuf = render_search_icon (impl);
1381 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1383 pixbuf = render_recent_icon (impl);
1386 gtk_list_store_set (impl->shortcuts_model, &iter,
1387 SHORTCUTS_COL_PIXBUF, pixbuf,
1391 g_object_unref (pixbuf);
1395 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1399 profile_end ("end", NULL);
1403 shortcuts_find_folder (GtkFileChooserDefault *impl,
1406 GtkTreeSelection *selection;
1410 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1412 g_assert (folder != NULL);
1413 pos = shortcut_find_position (impl, folder);
1416 gtk_tree_selection_unselect_all (selection);
1420 path = gtk_tree_path_new_from_indices (pos, -1);
1421 gtk_tree_selection_select_path (selection, path);
1422 gtk_tree_path_free (path);
1425 /* If a shortcut corresponds to the current folder, selects it */
1427 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1429 shortcuts_find_folder (impl, impl->current_folder);
1432 /* Removes the specified number of rows from the shortcuts list */
1434 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1440 path = gtk_tree_path_new_from_indices (start_row, -1);
1442 for (; n_rows; n_rows--)
1446 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1447 g_assert_not_reached ();
1449 shortcuts_free_row_data (impl, &iter);
1450 gtk_list_store_remove (impl->shortcuts_model, &iter);
1453 gtk_tree_path_free (path);
1457 shortcuts_update_count (GtkFileChooserDefault *impl,
1458 ShortcutsIndex type,
1463 case SHORTCUTS_HOME:
1465 impl->has_home = FALSE;
1467 impl->has_home = TRUE;
1470 case SHORTCUTS_DESKTOP:
1472 impl->has_desktop = FALSE;
1474 impl->has_desktop = TRUE;
1477 case SHORTCUTS_VOLUMES:
1478 impl->num_volumes += value;
1481 case SHORTCUTS_SHORTCUTS:
1482 impl->num_shortcuts += value;
1485 case SHORTCUTS_BOOKMARKS:
1486 impl->num_bookmarks += value;
1489 case SHORTCUTS_CURRENT_FOLDER:
1491 impl->shortcuts_current_folder_active = FALSE;
1493 impl->shortcuts_current_folder_active = TRUE;
1502 struct ShortcutsInsertRequest
1504 GtkFileChooserDefault *impl;
1508 GtkTreeRowReference *row_ref;
1509 ShortcutsIndex type;
1515 get_file_info_finished (GCancellable *cancellable,
1517 const GError *error,
1521 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1525 GCancellable *model_cancellable;
1526 struct ShortcutsInsertRequest *request = data;
1528 path = gtk_tree_row_reference_get_path (request->row_ref);
1530 /* Handle doesn't exist anymore in the model */
1533 pos = gtk_tree_path_get_indices (path)[0];
1534 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1536 gtk_tree_path_free (path);
1538 /* validate cancellable, else goto out */
1539 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1540 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1542 if (cancellable != model_cancellable)
1545 /* set the cancellable to NULL in the model (we unref later on) */
1546 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1547 SHORTCUTS_COL_CANCELLABLE, NULL,
1555 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1556 shortcuts_update_count (request->impl, request->type, -1);
1558 if (request->type == SHORTCUTS_HOME)
1562 home = g_file_new_for_path (g_get_home_dir ());
1563 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1564 g_object_unref (home);
1566 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1568 /* Remove the current folder separator */
1569 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1570 shortcuts_remove_rows (request->impl, separator_pos, 1);
1576 if (!request->label_copy)
1577 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1578 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1579 request->impl->icon_size);
1581 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1582 SHORTCUTS_COL_PIXBUF, pixbuf,
1583 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1584 SHORTCUTS_COL_NAME, request->label_copy,
1585 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1586 SHORTCUTS_COL_REMOVABLE, request->removable,
1589 if (request->impl->shortcuts_pane_filter_model)
1590 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1592 if (request->impl->shortcuts_combo_filter_model)
1593 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1595 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1596 request->impl->save_folder_combo != NULL)
1598 /* The current folder is updated via _activate_iter(), don't
1599 * have save_folder_combo_changed_cb() call _activate_iter()
1602 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1603 G_CALLBACK (save_folder_combo_changed_cb),
1606 if (request->impl->has_search)
1609 if (request->impl->has_recent)
1612 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1613 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1614 G_CALLBACK (save_folder_combo_changed_cb),
1619 g_object_unref (pixbuf);
1622 g_object_unref (request->impl);
1623 g_object_unref (request->file);
1624 gtk_tree_row_reference_free (request->row_ref);
1625 g_free (request->label_copy);
1628 g_object_unref (cancellable);
1631 /* FIXME: GtkFileSystem needs a function to split a remote path
1632 * into hostname and path components, or maybe just have a
1633 * gtk_file_system_path_get_display_name().
1635 * This function is also used in gtkfilechooserbutton.c
1638 _gtk_file_chooser_label_for_file (GFile *file)
1640 const gchar *path, *start, *end, *p;
1641 gchar *uri, *host, *label;
1643 uri = g_file_get_uri (file);
1645 start = strstr (uri, "://");
1649 path = strchr (start, '/');
1654 end = uri + strlen (uri);
1658 /* strip username */
1659 p = strchr (start, '@');
1663 p = strchr (start, ':');
1667 host = g_strndup (start, end - start);
1669 /* Translators: the first string is a path and the second string
1670 * is a hostname. Nautilus and the panel contain the same string
1673 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1679 label = g_strdup (uri);
1687 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1688 * inserts a volume. A position of -1 indicates the end of the tree.
1691 shortcuts_insert_file (GtkFileChooserDefault *impl,
1693 ShortcutType shortcut_type,
1694 GtkFileSystemVolume *volume,
1698 ShortcutsIndex type)
1701 GdkPixbuf *pixbuf = NULL;
1702 gpointer data = NULL;
1704 GtkIconTheme *icon_theme;
1706 profile_start ("start shortcut", NULL);
1708 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1711 label_copy = _gtk_file_system_volume_get_display_name (volume);
1712 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1713 impl->icon_size, NULL);
1715 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1717 if (g_file_is_native (file))
1719 struct ShortcutsInsertRequest *request;
1720 GCancellable *cancellable;
1723 request = g_new0 (struct ShortcutsInsertRequest, 1);
1724 request->impl = g_object_ref (impl);
1725 request->file = g_object_ref (file);
1726 request->name_only = TRUE;
1727 request->removable = removable;
1729 request->type = type;
1731 request->label_copy = g_strdup (label);
1734 gtk_list_store_append (impl->shortcuts_model, &iter);
1736 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1738 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1739 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1740 gtk_tree_path_free (p);
1742 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1743 "standard::is-hidden,standard::display-name,standard::icon",
1744 get_file_info_finished, request);
1746 gtk_list_store_set (impl->shortcuts_model, &iter,
1747 SHORTCUTS_COL_DATA, g_object_ref (file),
1748 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1749 SHORTCUTS_COL_CANCELLABLE, cancellable,
1752 shortcuts_update_count (impl, type, 1);
1758 /* Don't call get_info for remote paths to avoid latency and
1761 data = g_object_ref (file);
1763 label_copy = g_strdup (label);
1765 label_copy = _gtk_file_chooser_label_for_file (file);
1767 /* If we switch to a better bookmarks file format (XBEL), we
1768 * should use mime info to get a better icon.
1770 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1771 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1772 impl->icon_size, 0, NULL);
1777 g_assert_not_reached ();
1783 gtk_list_store_append (impl->shortcuts_model, &iter);
1785 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1787 shortcuts_update_count (impl, type, 1);
1789 gtk_list_store_set (impl->shortcuts_model, &iter,
1790 SHORTCUTS_COL_PIXBUF, pixbuf,
1791 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1792 SHORTCUTS_COL_NAME, label_copy,
1793 SHORTCUTS_COL_DATA, data,
1794 SHORTCUTS_COL_TYPE, shortcut_type,
1795 SHORTCUTS_COL_REMOVABLE, removable,
1796 SHORTCUTS_COL_CANCELLABLE, NULL,
1799 if (impl->shortcuts_pane_filter_model)
1800 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1802 if (impl->shortcuts_combo_filter_model)
1803 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1805 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1807 /* The current folder is updated via _activate_iter(), don't
1808 * have save_folder_combo_changed_cb() call _activate_iter()
1811 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1813 if (impl->has_search)
1816 if (impl->has_recent)
1819 g_signal_handlers_block_by_func (impl->save_folder_combo,
1820 G_CALLBACK (save_folder_combo_changed_cb),
1823 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1824 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1825 G_CALLBACK (save_folder_combo_changed_cb),
1829 g_free (label_copy);
1832 g_object_unref (pixbuf);
1834 profile_end ("end", NULL);
1838 shortcuts_append_search (GtkFileChooserDefault *impl)
1843 pixbuf = render_search_icon (impl);
1845 gtk_list_store_append (impl->shortcuts_model, &iter);
1846 gtk_list_store_set (impl->shortcuts_model, &iter,
1847 SHORTCUTS_COL_PIXBUF, pixbuf,
1848 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1849 SHORTCUTS_COL_NAME, _("Search"),
1850 SHORTCUTS_COL_DATA, NULL,
1851 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1852 SHORTCUTS_COL_REMOVABLE, FALSE,
1856 g_object_unref (pixbuf);
1858 impl->has_search = TRUE;
1862 shortcuts_append_recent (GtkFileChooserDefault *impl)
1867 pixbuf = render_recent_icon (impl);
1869 gtk_list_store_append (impl->shortcuts_model, &iter);
1870 gtk_list_store_set (impl->shortcuts_model, &iter,
1871 SHORTCUTS_COL_PIXBUF, pixbuf,
1872 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1873 SHORTCUTS_COL_NAME, _("Recently Used"),
1874 SHORTCUTS_COL_DATA, NULL,
1875 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1876 SHORTCUTS_COL_REMOVABLE, FALSE,
1880 g_object_unref (pixbuf);
1882 impl->has_recent = TRUE;
1885 /* Appends an item for the user's home directory to the shortcuts model */
1887 shortcuts_append_home (GtkFileChooserDefault *impl)
1889 const char *home_path;
1892 profile_start ("start", NULL);
1894 home_path = g_get_home_dir ();
1895 if (home_path == NULL)
1897 profile_end ("end - no home directory!?", NULL);
1901 home = g_file_new_for_path (home_path);
1902 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1903 impl->has_home = TRUE;
1905 g_object_unref (home);
1907 profile_end ("end", NULL);
1910 /* Appends the ~/Desktop directory to the shortcuts model */
1912 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1917 profile_start ("start", NULL);
1919 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1920 file = g_file_new_for_path (name);
1921 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1922 impl->has_desktop = TRUE;
1924 /* We do not actually pop up an error dialog if there is no desktop directory
1925 * because some people may really not want to have one.
1928 g_object_unref (file);
1930 profile_end ("end", NULL);
1933 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1935 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1942 profile_start ("start", NULL);
1944 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1947 for (; bookmarks; bookmarks = bookmarks->next)
1951 file = bookmarks->data;
1953 if (impl->local_only && !g_file_is_native (file))
1956 if (shortcut_find_position (impl, file) != -1)
1959 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1961 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1967 profile_end ("end", NULL);
1969 return num_inserted;
1972 /* Returns the index for the corresponding item in the shortcuts bar */
1974 shortcuts_get_index (GtkFileChooserDefault *impl,
1975 ShortcutsIndex where)
1981 if (where == SHORTCUTS_SEARCH)
1984 n += impl->has_search ? 1 : 0;
1986 if (where == SHORTCUTS_RECENT)
1989 n += impl->has_recent ? 1 : 0;
1991 if (where == SHORTCUTS_RECENT_SEPARATOR)
1994 n += impl->has_recent ? 1 : 0;
1996 if (where == SHORTCUTS_HOME)
1999 n += impl->has_home ? 1 : 0;
2001 if (where == SHORTCUTS_DESKTOP)
2004 n += impl->has_desktop ? 1 : 0;
2006 if (where == SHORTCUTS_VOLUMES)
2009 n += impl->num_volumes;
2011 if (where == SHORTCUTS_SHORTCUTS)
2014 n += impl->num_shortcuts;
2016 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2019 /* If there are no bookmarks there won't be a separator */
2020 n += (impl->num_bookmarks > 0) ? 1 : 0;
2022 if (where == SHORTCUTS_BOOKMARKS)
2025 n += impl->num_bookmarks;
2027 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2032 if (where == SHORTCUTS_CURRENT_FOLDER)
2035 g_assert_not_reached ();
2042 /* Adds all the file system volumes to the shortcuts model */
2044 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2049 gboolean old_changing_folders;
2051 profile_start ("start", NULL);
2053 old_changing_folders = impl->changing_folder;
2054 impl->changing_folder = TRUE;
2056 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2057 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2058 impl->num_volumes = 0;
2060 list = _gtk_file_system_list_volumes (impl->file_system);
2064 for (l = list; l; l = l->next)
2066 GtkFileSystemVolume *volume;
2070 if (impl->local_only)
2072 if (_gtk_file_system_volume_is_mounted (volume))
2076 base_file = _gtk_file_system_volume_get_root (volume);
2077 if (base_file != NULL && !g_file_is_native (base_file))
2082 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2086 impl->num_volumes = n;
2087 g_slist_free (list);
2089 if (impl->shortcuts_pane_filter_model)
2090 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2092 if (impl->shortcuts_combo_filter_model)
2093 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2095 impl->changing_folder = old_changing_folders;
2097 profile_end ("end", NULL);
2100 /* Inserts a separator node in the shortcuts list */
2102 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2103 ShortcutsIndex where)
2107 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2108 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2109 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2111 gtk_list_store_insert (impl->shortcuts_model, &iter,
2112 shortcuts_get_index (impl, where));
2113 gtk_list_store_set (impl->shortcuts_model, &iter,
2114 SHORTCUTS_COL_PIXBUF, NULL,
2115 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2116 SHORTCUTS_COL_NAME, NULL,
2117 SHORTCUTS_COL_DATA, NULL,
2118 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2122 /* Updates the list of bookmarks */
2124 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2127 gboolean old_changing_folders;
2129 GFile *list_selected = NULL;
2130 GFile *combo_selected = NULL;
2131 ShortcutType shortcut_type;
2134 profile_start ("start", NULL);
2136 old_changing_folders = impl->changing_folder;
2137 impl->changing_folder = TRUE;
2139 if (shortcuts_get_selected (impl, &iter))
2141 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2143 SHORTCUTS_COL_DATA, &col_data,
2144 SHORTCUTS_COL_TYPE, &shortcut_type,
2147 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2148 list_selected = g_object_ref (col_data);
2151 if (impl->save_folder_combo &&
2152 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2155 GtkTreeIter child_iter;
2157 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2160 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2162 SHORTCUTS_COL_DATA, &col_data,
2163 SHORTCUTS_COL_TYPE, &shortcut_type,
2166 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2167 combo_selected = g_object_ref (col_data);
2170 if (impl->num_bookmarks > 0)
2171 shortcuts_remove_rows (impl,
2172 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2173 impl->num_bookmarks + 1);
2175 impl->num_bookmarks = 0;
2176 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2178 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2179 shortcuts_append_bookmarks (impl, bookmarks);
2180 g_slist_free (bookmarks);
2182 if (impl->num_bookmarks == 0)
2183 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2185 if (impl->shortcuts_pane_filter_model)
2186 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2188 if (impl->shortcuts_combo_filter_model)
2189 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2193 shortcuts_find_folder (impl, list_selected);
2194 g_object_unref (list_selected);
2201 pos = shortcut_find_position (impl, combo_selected);
2204 if (impl->has_search)
2207 if (impl->has_recent)
2210 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2213 g_object_unref (combo_selected);
2216 impl->changing_folder = old_changing_folders;
2218 profile_end ("end", NULL);
2221 /* Appends a separator and a row to the shortcuts list for the current folder */
2223 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2228 g_assert (!impl->shortcuts_current_folder_active);
2232 g_assert (impl->current_folder != NULL);
2234 pos = shortcut_find_position (impl, impl->current_folder);
2237 GtkFileSystemVolume *volume;
2242 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2246 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2248 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2250 base_file = _gtk_file_system_volume_get_root (volume);
2254 if (base_file && g_file_equal (base_file, impl->current_folder))
2255 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2257 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2260 g_object_unref (base_file);
2262 else if (impl->save_folder_combo != NULL)
2264 if (impl->has_search)
2267 if (impl->has_recent)
2268 pos -= 2; /* + separator */
2270 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2274 /* Updates the current folder row in the shortcuts model */
2276 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2280 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2282 if (impl->shortcuts_current_folder_active)
2284 shortcuts_remove_rows (impl, pos, 2);
2285 impl->shortcuts_current_folder_active = FALSE;
2288 shortcuts_add_current_folder (impl);
2291 /* Filter function used for the shortcuts filter model */
2293 shortcuts_pane_filter_cb (GtkTreeModel *model,
2297 GtkFileChooserDefault *impl;
2301 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2303 path = gtk_tree_model_get_path (model, iter);
2307 pos = *gtk_tree_path_get_indices (path);
2308 gtk_tree_path_free (path);
2310 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2313 /* Creates the list model for shortcuts */
2315 shortcuts_model_create (GtkFileChooserDefault *impl)
2317 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2318 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2319 GDK_TYPE_PIXBUF, /* pixbuf */
2320 G_TYPE_STRING, /* name */
2321 G_TYPE_POINTER, /* path or volume */
2322 G_TYPE_INT, /* ShortcutType */
2323 G_TYPE_BOOLEAN, /* removable */
2324 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2325 G_TYPE_POINTER); /* GCancellable */
2327 if (search_is_possible (impl))
2329 shortcuts_append_search (impl);
2332 if (impl->recent_manager)
2334 shortcuts_append_recent (impl);
2335 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2338 if (impl->file_system)
2340 shortcuts_append_home (impl);
2341 shortcuts_append_desktop (impl);
2342 shortcuts_add_volumes (impl);
2345 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2346 GTK_TREE_MODEL (impl->shortcuts_model),
2349 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2350 shortcuts_pane_filter_cb,
2355 /* Callback used when the "New Folder" button is clicked */
2357 new_folder_button_clicked (GtkButton *button,
2358 GtkFileChooserDefault *impl)
2363 if (!impl->browse_files_model)
2364 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2366 /* Prevent button from being clicked twice */
2367 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2369 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2371 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2372 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2373 path, impl->list_name_column,
2376 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2377 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2379 impl->list_name_column,
2382 gtk_tree_path_free (path);
2385 /* Idle handler for creating a new folder after editing its name cell, or for
2386 * canceling the editing.
2389 edited_idle_cb (GtkFileChooserDefault *impl)
2391 GDK_THREADS_ENTER ();
2393 g_source_destroy (impl->edited_idle);
2394 impl->edited_idle = NULL;
2396 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2397 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2399 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2401 if (impl->edited_new_text) /* not cancelled? */
2403 GError *error = NULL;
2406 file = g_file_get_child_for_display_name (impl->current_folder,
2407 impl->edited_new_text,
2411 GError *error = NULL;
2413 if (!g_file_make_directory (file, NULL, &error))
2414 change_folder_and_display_error (impl, file, FALSE);
2416 error_creating_folder_dialog (impl, file, error);
2418 g_object_unref (file);
2421 error_creating_folder_dialog (impl, file, error);
2423 g_free (impl->edited_new_text);
2424 impl->edited_new_text = NULL;
2427 GDK_THREADS_LEAVE ();
2433 queue_edited_idle (GtkFileChooserDefault *impl,
2434 const gchar *new_text)
2436 /* We create the folder in an idle handler so that we don't modify the tree
2440 if (!impl->edited_idle)
2442 impl->edited_idle = g_idle_source_new ();
2443 g_source_set_closure (impl->edited_idle,
2444 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2446 g_source_attach (impl->edited_idle, NULL);
2449 g_free (impl->edited_new_text);
2450 impl->edited_new_text = g_strdup (new_text);
2453 /* Callback used from the text cell renderer when the new folder is named */
2455 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2457 const gchar *new_text,
2458 GtkFileChooserDefault *impl)
2460 /* work around bug #154921 */
2461 g_object_set (cell_renderer_text,
2462 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2463 queue_edited_idle (impl, new_text);
2466 /* Callback used from the text cell renderer when the new folder edition gets
2470 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2471 GtkFileChooserDefault *impl)
2473 /* work around bug #154921 */
2474 g_object_set (cell_renderer_text,
2475 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2476 queue_edited_idle (impl, NULL);
2479 /* Creates the widgets for the filter combo box */
2481 filter_create (GtkFileChooserDefault *impl)
2483 impl->filter_combo = gtk_combo_box_new_text ();
2484 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2486 g_signal_connect (impl->filter_combo, "changed",
2487 G_CALLBACK (filter_combo_changed), impl);
2489 gtk_widget_set_tooltip_text (impl->filter_combo,
2490 _("Select which types of files are shown"));
2492 return impl->filter_combo;
2496 button_new (GtkFileChooserDefault *impl,
2498 const char *stock_id,
2506 button = gtk_button_new_with_mnemonic (text);
2507 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2508 gtk_button_set_image (GTK_BUTTON (button), image);
2510 gtk_widget_set_sensitive (button, sensitive);
2511 g_signal_connect (button, "clicked", callback, impl);
2514 gtk_widget_show (button);
2519 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2521 shortcut_find_position (GtkFileChooserDefault *impl,
2526 int current_folder_separator_idx;
2528 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2531 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2534 /* FIXME: is this still needed? */
2535 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2539 for (i = 0; i < current_folder_separator_idx; i++)
2542 ShortcutType shortcut_type;
2544 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2545 SHORTCUTS_COL_DATA, &col_data,
2546 SHORTCUTS_COL_TYPE, &shortcut_type,
2551 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2553 GtkFileSystemVolume *volume;
2558 base_file = _gtk_file_system_volume_get_root (volume);
2560 exists = base_file && g_file_equal (file, base_file);
2563 g_object_unref (base_file);
2568 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2572 model_file = col_data;
2574 if (model_file && g_file_equal (model_file, file))
2579 if (i < current_folder_separator_idx - 1)
2581 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2582 g_assert_not_reached ();
2589 /* Tries to add a bookmark from a path name */
2591 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2597 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2599 if (shortcut_find_position (impl, file) != -1)
2603 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2605 error_adding_bookmark_dialog (impl, file, error);
2613 add_bookmark_foreach_cb (GtkTreeModel *model,
2618 GtkFileChooserDefault *impl;
2619 GtkFileSystemModel *fs_model;
2620 GtkTreeIter child_iter;
2623 impl = (GtkFileChooserDefault *) data;
2625 switch (impl->operation_mode)
2627 case OPERATION_MODE_BROWSE:
2628 fs_model = impl->browse_files_model;
2629 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2630 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2633 case OPERATION_MODE_SEARCH:
2634 search_get_valid_child_iter (impl, &child_iter, iter);
2635 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2636 SEARCH_MODEL_COL_FILE, &file,
2640 case OPERATION_MODE_RECENT:
2641 recent_get_valid_child_iter (impl, &child_iter, iter);
2642 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2643 RECENT_MODEL_COL_FILE, &file,
2648 shortcuts_add_bookmark_from_file (impl, file, -1);
2651 /* Adds a bookmark from the currently selected item in the file list */
2653 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2655 GtkTreeSelection *selection;
2657 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2659 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2660 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2662 gtk_tree_selection_selected_foreach (selection,
2663 add_bookmark_foreach_cb,
2667 /* Callback used when the "Add bookmark" button is clicked */
2669 add_bookmark_button_clicked_cb (GtkButton *button,
2670 GtkFileChooserDefault *impl)
2672 bookmarks_add_selected_folder (impl);
2675 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2676 * returns FALSE if no shortcut is selected.
2679 shortcuts_get_selected (GtkFileChooserDefault *impl,
2682 GtkTreeSelection *selection;
2683 GtkTreeIter parent_iter;
2685 if (!impl->browse_shortcuts_tree_view)
2688 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2690 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2693 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2699 /* Removes the selected bookmarks */
2701 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2709 if (!shortcuts_get_selected (impl, &iter))
2712 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2713 SHORTCUTS_COL_DATA, &col_data,
2714 SHORTCUTS_COL_REMOVABLE, &removable,
2720 g_assert (col_data != NULL);
2725 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2726 error_removing_bookmark_dialog (impl, file, error);
2729 /* Callback used when the "Remove bookmark" button is clicked */
2731 remove_bookmark_button_clicked_cb (GtkButton *button,
2732 GtkFileChooserDefault *impl)
2734 remove_selected_bookmarks (impl);
2737 struct selection_check_closure {
2738 GtkFileChooserDefault *impl;
2741 gboolean all_folders;
2744 /* Used from gtk_tree_selection_selected_foreach() */
2746 selection_check_foreach_cb (GtkTreeModel *model,
2751 struct selection_check_closure *closure;
2752 GtkTreeIter child_iter;
2757 closure->num_selected++;
2759 switch (closure->impl->operation_mode)
2761 case OPERATION_MODE_BROWSE:
2762 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2763 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2764 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2767 case OPERATION_MODE_SEARCH:
2768 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2769 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2770 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2774 case OPERATION_MODE_RECENT:
2775 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2776 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2777 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2782 closure->all_folders = closure->all_folders && is_folder;
2783 closure->all_files = closure->all_files && !is_folder;
2786 /* Checks whether the selected items in the file list are all files or all folders */
2788 selection_check (GtkFileChooserDefault *impl,
2790 gboolean *all_files,
2791 gboolean *all_folders)
2793 struct selection_check_closure closure;
2794 GtkTreeSelection *selection;
2796 closure.impl = impl;
2797 closure.num_selected = 0;
2798 closure.all_files = TRUE;
2799 closure.all_folders = TRUE;
2801 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2802 gtk_tree_selection_selected_foreach (selection,
2803 selection_check_foreach_cb,
2806 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2809 *num_selected = closure.num_selected;
2812 *all_files = closure.all_files;
2815 *all_folders = closure.all_folders;
2818 struct get_selected_file_closure {
2819 GtkFileChooserDefault *impl;
2824 get_selected_file_foreach_cb (GtkTreeModel *model,
2829 struct get_selected_file_closure *closure;
2830 GtkTreeIter child_iter;
2834 switch (closure->impl->operation_mode)
2836 case OPERATION_MODE_BROWSE:
2837 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2838 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2841 case OPERATION_MODE_SEARCH:
2842 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2843 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2844 SEARCH_MODEL_COL_FILE, &closure->file,
2848 case OPERATION_MODE_RECENT:
2849 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2850 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2851 RECENT_MODEL_COL_FILE, &closure->file,
2857 /* Returns a selected path from the file list */
2859 get_selected_file (GtkFileChooserDefault *impl)
2861 struct get_selected_file_closure closure;
2862 GtkTreeSelection *selection;
2864 closure.impl = impl;
2865 closure.file = NULL;
2867 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2868 gtk_tree_selection_selected_foreach (selection,
2869 get_selected_file_foreach_cb,
2872 return closure.file;
2876 GtkFileChooserDefault *impl;
2878 } UpdateTooltipData;
2881 update_tooltip (GtkTreeModel *model,
2886 UpdateTooltipData *udata = data;
2887 GtkTreeIter child_iter;
2890 if (udata->tip == NULL)
2892 const gchar *display_name;
2894 switch (udata->impl->operation_mode)
2896 case OPERATION_MODE_BROWSE:
2897 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2900 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2901 display_name = g_file_info_get_display_name (info);
2904 case OPERATION_MODE_SEARCH:
2905 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2906 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2907 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2911 case OPERATION_MODE_RECENT:
2912 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2913 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2914 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2919 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2925 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2926 * if there are no selected items *and* the current folder is not in the
2927 * bookmarks list. De-sensitize the button otherwise.
2930 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2933 gboolean all_folders;
2937 selection_check (impl, &num_selected, NULL, &all_folders);
2939 if (num_selected == 0)
2940 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2941 else if (num_selected == 1)
2945 file = get_selected_file (impl);
2946 active = all_folders && (shortcut_find_position (impl, file) == -1);
2949 active = all_folders;
2951 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2953 if (impl->browse_files_popup_menu_add_shortcut_item)
2954 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2955 (num_selected == 0) ? FALSE : active);
2959 if (num_selected == 0)
2960 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2961 else if (num_selected > 1)
2962 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2965 GtkTreeSelection *selection;
2966 UpdateTooltipData data;
2968 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2971 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2975 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2980 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2981 * bookmark row is selected in the shortcuts tree.
2984 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2987 gboolean removable = FALSE;
2990 if (shortcuts_get_selected (impl, &iter))
2991 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2992 SHORTCUTS_COL_REMOVABLE, &removable,
2993 SHORTCUTS_COL_NAME, &name,
2996 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3002 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3003 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3011 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3014 gboolean removable = FALSE;
3016 if (impl->browse_shortcuts_popup_menu == NULL)
3019 if (shortcuts_get_selected (impl, &iter))
3020 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3021 SHORTCUTS_COL_REMOVABLE, &removable,
3024 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3025 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3028 /* GtkWidget::drag-begin handler for the shortcuts list. */
3030 shortcuts_drag_begin_cb (GtkWidget *widget,
3031 GdkDragContext *context,
3032 GtkFileChooserDefault *impl)
3035 impl->shortcuts_drag_context = g_object_ref (context);
3040 /* Removes the idle handler for outside drags */
3042 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3044 if (!impl->shortcuts_drag_outside_idle)
3047 g_source_destroy (impl->shortcuts_drag_outside_idle);
3048 impl->shortcuts_drag_outside_idle = NULL;
3052 /* GtkWidget::drag-end handler for the shortcuts list. */
3054 shortcuts_drag_end_cb (GtkWidget *widget,
3055 GdkDragContext *context,
3056 GtkFileChooserDefault *impl)
3059 g_object_unref (impl->shortcuts_drag_context);
3061 shortcuts_cancel_drag_outside_idle (impl);
3063 if (!impl->shortcuts_drag_outside)
3066 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3068 impl->shortcuts_drag_outside = FALSE;
3072 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3074 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3075 GdkDragContext *context,
3076 GtkFileChooserDefault *impl)
3078 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3082 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3086 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3089 GtkTreeView *tree_view;
3092 GdkPixmap *row_pixmap;
3097 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3099 /* Find the selected path and get its drag pixmap */
3101 if (!shortcuts_get_selected (impl, &iter))
3102 g_assert_not_reached ();
3104 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3106 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3107 gtk_tree_path_free (path);
3116 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3122 GdkPixmap *composite;
3123 int row_pixmap_width, row_pixmap_height;
3124 int pixbuf_width, pixbuf_height;
3125 int composite_width, composite_height;
3126 int pixbuf_x, pixbuf_y;
3127 GdkGC *gc, *mask_gc;
3129 GdkBitmap *pixbuf_mask;
3131 /* Create pixmap and mask for composite image */
3133 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3134 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3135 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3137 composite_width = MAX (row_pixmap_width, pixbuf_width);
3138 composite_height = MAX (row_pixmap_height, pixbuf_height);
3140 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3142 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3145 pixbuf_x = composite_width - pixbuf_width;
3147 pixbuf_y = (composite_height - pixbuf_height) / 2;
3149 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3150 gc = gdk_gc_new (composite);
3152 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3153 mask_gc = gdk_gc_new (mask);
3155 gdk_gc_set_foreground (mask_gc, &color);
3156 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3159 color.green = 0xffff;
3160 color.blue = 0xffff;
3161 gdk_gc_set_rgb_fg_color (gc, &color);
3162 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3164 /* Composite the row pixmap and the pixbuf */
3166 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3168 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3169 NULL, &pixbuf_mask, 128);
3170 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3173 pixbuf_width, pixbuf_height);
3174 g_object_unref (pixbuf_mask);
3176 gdk_draw_drawable (composite, gc, row_pixmap,
3179 row_pixmap_width, row_pixmap_height);
3181 gdk_gc_set_foreground (mask_gc, &color);
3182 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3184 gdk_draw_pixbuf (composite, gc, pixbuf,
3187 pixbuf_width, pixbuf_height,
3191 g_object_unref (pixbuf);
3192 g_object_unref (row_pixmap);
3194 row_pixmap = composite;
3198 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3200 gtk_tree_view_get_path_at_pos (tree_view,
3201 tree_view->priv->press_start_x,
3202 tree_view->priv->press_start_y,
3208 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3209 gdk_drawable_get_colormap (row_pixmap),
3212 tree_view->priv->press_start_x + 1,
3213 row_pixmap_y + cell_y + 1);
3215 g_object_unref (row_pixmap);
3217 g_object_unref (mask);
3220 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3221 * handler so that we can tell apart the drag_leave event that comes right
3222 * before a drag_drop, from a normal drag_leave. We don't want to set the
3223 * cursor nor the flag in the latter case.
3226 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3228 GDK_THREADS_ENTER ();
3230 shortcuts_drag_set_delete_cursor (impl, TRUE);
3231 impl->shortcuts_drag_outside = TRUE;
3233 shortcuts_cancel_drag_outside_idle (impl);
3235 GDK_THREADS_LEAVE ();
3241 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3245 shortcuts_drag_leave_cb (GtkWidget *widget,
3246 GdkDragContext *context,
3248 GtkFileChooserDefault *impl)
3251 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3253 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3254 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3255 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3257 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3261 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3263 GTK_TREE_VIEW_DROP_BEFORE);
3265 g_signal_stop_emission_by_name (widget, "drag-leave");
3268 /* Computes the appropriate row and position for dropping */
3270 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3274 GtkTreeViewDropPosition *pos)
3276 GtkTreeView *tree_view;
3277 GtkTreeViewColumn *column;
3281 int bookmarks_index;
3283 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3285 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3287 if (!gtk_tree_view_get_path_at_pos (tree_view,
3289 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3295 row = bookmarks_index + impl->num_bookmarks - 1;
3296 *path = gtk_tree_path_new_from_indices (row, -1);
3297 *pos = GTK_TREE_VIEW_DROP_AFTER;
3301 row = *gtk_tree_path_get_indices (*path);
3302 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3303 gtk_tree_path_free (*path);
3305 if (row < bookmarks_index)
3307 row = bookmarks_index;
3308 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3310 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3312 row = bookmarks_index + impl->num_bookmarks - 1;
3313 *pos = GTK_TREE_VIEW_DROP_AFTER;
3317 if (cell_y < cell.height / 2)
3318 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3320 *pos = GTK_TREE_VIEW_DROP_AFTER;
3323 *path = gtk_tree_path_new_from_indices (row, -1);
3326 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3327 * implement the destination side of DnD by hand, due to limitations in
3328 * GtkTreeView's DnD API.
3331 shortcuts_drag_motion_cb (GtkWidget *widget,
3332 GdkDragContext *context,
3336 GtkFileChooserDefault *impl)
3339 GtkTreeViewDropPosition pos;
3340 GdkDragAction action;
3343 if (gtk_drag_get_source_widget (context) == widget)
3345 shortcuts_cancel_drag_outside_idle (impl);
3347 if (impl->shortcuts_drag_outside)
3349 shortcuts_drag_set_delete_cursor (impl, FALSE);
3350 impl->shortcuts_drag_outside = FALSE;
3355 if (context->suggested_action == GDK_ACTION_COPY ||
3356 (context->actions & GDK_ACTION_COPY) != 0)
3357 action = GDK_ACTION_COPY;
3358 else if (context->suggested_action == GDK_ACTION_MOVE ||
3359 (context->actions & GDK_ACTION_MOVE) != 0)
3360 action = GDK_ACTION_MOVE;
3367 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3368 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3369 gtk_tree_path_free (path);
3373 g_signal_stop_emission_by_name (widget, "drag-motion");
3377 gdk_drag_status (context, action, time_);
3384 /* GtkWidget::drag-drop handler for the shortcuts list. */
3386 shortcuts_drag_drop_cb (GtkWidget *widget,
3387 GdkDragContext *context,
3391 GtkFileChooserDefault *impl)
3394 shortcuts_cancel_drag_outside_idle (impl);
3397 g_signal_stop_emission_by_name (widget, "drag-drop");
3401 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3403 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3404 GtkSelectionData *selection_data,
3410 uris = gtk_selection_data_get_uris (selection_data);
3414 for (i = 0; uris[i]; i++)
3420 file = g_file_new_for_uri (uri);
3422 if (shortcuts_add_bookmark_from_file (impl, file, position))
3425 g_object_unref (file);
3431 /* Reorders the selected bookmark to the specified position */
3433 shortcuts_reorder (GtkFileChooserDefault *impl,
3438 ShortcutType shortcut_type;
3441 int bookmarks_index;
3446 /* Get the selected path */
3448 if (!shortcuts_get_selected (impl, &iter))
3449 g_assert_not_reached ();
3451 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3452 old_position = *gtk_tree_path_get_indices (path);
3453 gtk_tree_path_free (path);
3455 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3456 old_position -= bookmarks_index;
3457 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3459 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3460 SHORTCUTS_COL_NAME, &name,
3461 SHORTCUTS_COL_DATA, &col_data,
3462 SHORTCUTS_COL_TYPE, &shortcut_type,
3464 g_assert (col_data != NULL);
3465 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3468 g_object_ref (file); /* removal below will free file, so we need a new ref */
3470 /* Remove the path from the old position and insert it in the new one */
3472 if (new_position > old_position)
3475 if (old_position == new_position)
3479 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3481 shortcuts_add_bookmark_from_file (impl, file, new_position);
3482 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3485 error_adding_bookmark_dialog (impl, file, error);
3489 g_object_unref (file);
3492 /* Callback used when we get the drag data for the bookmarks list. We add the
3493 * received URIs as bookmarks if they are folders.
3496 shortcuts_drag_data_received_cb (GtkWidget *widget,
3497 GdkDragContext *context,
3500 GtkSelectionData *selection_data,
3505 GtkFileChooserDefault *impl;
3506 GtkTreePath *tree_path;
3507 GtkTreeViewDropPosition tree_pos;
3509 int bookmarks_index;
3511 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3513 /* Compute position */
3515 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3517 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3518 position = *gtk_tree_path_get_indices (tree_path);
3519 gtk_tree_path_free (tree_path);
3521 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3524 g_assert (position >= bookmarks_index);
3525 position -= bookmarks_index;
3527 if (gtk_targets_include_uri (&selection_data->target, 1))
3528 shortcuts_drop_uris (impl, selection_data, position);
3529 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3530 shortcuts_reorder (impl, position);
3532 g_signal_stop_emission_by_name (widget, "drag-data-received");
3535 /* Callback used to display a tooltip in the shortcuts tree */
3537 shortcuts_query_tooltip_cb (GtkWidget *widget,
3540 gboolean keyboard_mode,
3541 GtkTooltip *tooltip,
3542 GtkFileChooserDefault *impl)
3544 GtkTreeModel *model;
3547 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3555 ShortcutType shortcut_type;
3557 gtk_tree_model_get (model, &iter,
3558 SHORTCUTS_COL_DATA, &col_data,
3559 SHORTCUTS_COL_TYPE, &shortcut_type,
3562 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3564 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3568 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3573 file = G_FILE (col_data);
3574 parse_name = g_file_get_parse_name (file);
3576 gtk_tooltip_set_text (tooltip, parse_name);
3578 g_free (parse_name);
3582 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3586 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3596 /* Callback used when the selection in the shortcuts tree changes */
3598 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3599 GtkFileChooserDefault *impl)
3602 GtkTreeIter child_iter;
3604 bookmarks_check_remove_sensitivity (impl);
3605 shortcuts_check_popup_sensitivity (impl);
3607 if (impl->changing_folder)
3610 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3612 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3615 shortcuts_activate_iter (impl, &child_iter);
3620 shortcuts_row_separator_func (GtkTreeModel *model,
3624 ShortcutType shortcut_type;
3626 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3628 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3631 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3632 * keypresses before the TreeView gets them.
3635 tree_view_keybinding_cb (GtkWidget *tree_view,
3637 GtkFileChooserDefault *impl)
3639 if ((event->keyval == GDK_slash
3640 || event->keyval == GDK_KP_Divide
3642 || event->keyval == GDK_asciitilde
3644 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3646 location_popup_handler (impl, event->string);
3653 /* Callback used when the file list's popup menu is detached */
3655 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3658 GtkFileChooserDefault *impl;
3660 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3661 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3663 impl->browse_shortcuts_popup_menu = NULL;
3664 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3665 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3669 remove_shortcut_cb (GtkMenuItem *item,
3670 GtkFileChooserDefault *impl)
3672 remove_selected_bookmarks (impl);
3675 /* Rename the selected bookmark */
3677 rename_selected_bookmark (GtkFileChooserDefault *impl)
3681 GtkTreeViewColumn *column;
3682 GtkCellRenderer *cell;
3685 if (shortcuts_get_selected (impl, &iter))
3687 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3688 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3689 renderers = gtk_tree_view_column_get_cell_renderers (column);
3690 cell = g_list_nth_data (renderers, 1);
3691 g_list_free (renderers);
3692 g_object_set (cell, "editable", TRUE, NULL);
3693 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3694 path, column, cell, TRUE);
3695 gtk_tree_path_free (path);
3700 rename_shortcut_cb (GtkMenuItem *item,
3701 GtkFileChooserDefault *impl)
3703 rename_selected_bookmark (impl);
3706 /* Constructs the popup menu for the file list if needed */
3708 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3712 if (impl->browse_shortcuts_popup_menu)
3715 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3716 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3717 impl->browse_shortcuts_tree_view,
3718 shortcuts_popup_menu_detach_cb);
3720 item = gtk_image_menu_item_new_with_label (_("Remove"));
3721 impl->browse_shortcuts_popup_menu_remove_item = item;
3722 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3723 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3724 g_signal_connect (item, "activate",
3725 G_CALLBACK (remove_shortcut_cb), impl);
3726 gtk_widget_show (item);
3727 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3729 item = gtk_menu_item_new_with_label (_("Rename..."));
3730 impl->browse_shortcuts_popup_menu_rename_item = item;
3731 g_signal_connect (item, "activate",
3732 G_CALLBACK (rename_shortcut_cb), impl);
3733 gtk_widget_show (item);
3734 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3738 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3740 shortcuts_build_popup_menu (impl);
3741 shortcuts_check_popup_sensitivity (impl);
3745 popup_position_func (GtkMenu *menu,
3749 gpointer user_data);
3752 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3753 GdkEventButton *event)
3755 shortcuts_update_popup_menu (impl);
3757 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3758 NULL, NULL, NULL, NULL,
3759 event->button, event->time);
3762 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3764 popup_position_func, impl->browse_shortcuts_tree_view,
3765 0, GDK_CURRENT_TIME);
3766 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3771 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3773 shortcuts_popup_menu_cb (GtkWidget *widget,
3774 GtkFileChooserDefault *impl)
3776 shortcuts_popup_menu (impl, NULL);
3780 /* Callback used when a button is pressed on the shortcuts list.
3781 * We trap button 3 to bring up a popup menu.
3784 shortcuts_button_press_event_cb (GtkWidget *widget,
3785 GdkEventButton *event,
3786 GtkFileChooserDefault *impl)
3788 static gboolean in_press = FALSE;
3794 if (event->button != 3)
3798 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3804 shortcuts_popup_menu (impl, event);
3809 shortcuts_edited (GtkCellRenderer *cell,
3812 GtkFileChooserDefault *impl)
3818 g_object_set (cell, "editable", FALSE, NULL);
3820 path = gtk_tree_path_new_from_string (path_string);
3821 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3822 g_assert_not_reached ();
3824 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3825 SHORTCUTS_COL_DATA, &shortcut,
3827 gtk_tree_path_free (path);
3829 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3833 shortcuts_editing_canceled (GtkCellRenderer *cell,
3834 GtkFileChooserDefault *impl)
3836 g_object_set (cell, "editable", FALSE, NULL);
3839 /* Creates the widgets for the shortcuts and bookmarks tree */
3841 shortcuts_list_create (GtkFileChooserDefault *impl)
3844 GtkTreeSelection *selection;
3845 GtkTreeViewColumn *column;
3846 GtkCellRenderer *renderer;
3848 /* Target types for dragging a row to/from the shortcuts list */
3849 const GtkTargetEntry tree_model_row_targets[] = {
3850 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3853 /* Scrolled window */
3855 swin = gtk_scrolled_window_new (NULL, NULL);
3856 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3857 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3858 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3860 gtk_widget_show (swin);
3864 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3865 #ifdef PROFILE_FILE_CHOOSER
3866 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3868 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3869 G_CALLBACK (tree_view_keybinding_cb), impl);
3870 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3871 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3872 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3873 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3874 /* Accessible object name for the file chooser's shortcuts pane */
3875 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3877 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3879 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3881 tree_model_row_targets,
3882 G_N_ELEMENTS (tree_model_row_targets),
3885 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3886 GTK_DEST_DEFAULT_ALL,
3887 tree_model_row_targets,
3888 G_N_ELEMENTS (tree_model_row_targets),
3889 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3890 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3892 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3893 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3894 gtk_tree_selection_set_select_function (selection,
3895 shortcuts_select_func,
3898 g_signal_connect (selection, "changed",
3899 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3901 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3902 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3904 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3905 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3906 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3907 G_CALLBACK (shortcuts_drag_end_cb), impl);
3908 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3909 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3911 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3912 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3913 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3914 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3915 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3916 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3917 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3918 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3920 /* Support tooltips */
3921 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3922 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3923 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3925 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3926 gtk_widget_show (impl->browse_shortcuts_tree_view);
3930 column = gtk_tree_view_column_new ();
3931 /* Column header for the file chooser's shortcuts pane */
3932 gtk_tree_view_column_set_title (column, _("_Places"));
3934 renderer = gtk_cell_renderer_pixbuf_new ();
3935 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3936 gtk_tree_view_column_set_attributes (column, renderer,
3937 "pixbuf", SHORTCUTS_COL_PIXBUF,
3938 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3941 renderer = gtk_cell_renderer_text_new ();
3942 g_signal_connect (renderer, "edited",
3943 G_CALLBACK (shortcuts_edited), impl);
3944 g_signal_connect (renderer, "editing-canceled",
3945 G_CALLBACK (shortcuts_editing_canceled), impl);
3946 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3947 gtk_tree_view_column_set_attributes (column, renderer,
3948 "text", SHORTCUTS_COL_NAME,
3951 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3952 shortcuts_row_separator_func,
3955 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3960 /* Creates the widgets for the shortcuts/bookmarks pane */
3962 shortcuts_pane_create (GtkFileChooserDefault *impl,
3963 GtkSizeGroup *size_group)
3969 vbox = gtk_vbox_new (FALSE, 6);
3970 gtk_widget_show (vbox);
3972 /* Shortcuts tree */
3974 widget = shortcuts_list_create (impl);
3975 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3977 /* Box for buttons */
3979 hbox = gtk_hbox_new (TRUE, 6);
3980 gtk_size_group_add_widget (size_group, hbox);
3981 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3982 gtk_widget_show (hbox);
3984 /* Add bookmark button */
3986 impl->browse_shortcuts_add_button = button_new (impl,
3991 G_CALLBACK (add_bookmark_button_clicked_cb));
3992 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3993 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3994 _("Add the selected folder to the Bookmarks"));
3996 /* Remove bookmark button */
3998 impl->browse_shortcuts_remove_button = button_new (impl,
4003 G_CALLBACK (remove_bookmark_button_clicked_cb));
4004 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4005 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4006 _("Remove the selected bookmark"));
4011 /* Handles key press events on the file list, so that we can trap Enter to
4012 * activate the default button on our own. Also, checks to see if '/' has been
4013 * pressed. See comment by tree_view_keybinding_cb() for more details.
4016 trap_activate_cb (GtkWidget *widget,
4020 GtkFileChooserDefault *impl;
4023 impl = (GtkFileChooserDefault *) data;
4025 modifiers = gtk_accelerator_get_default_mod_mask ();
4027 if ((event->keyval == GDK_slash
4028 || event->keyval == GDK_KP_Divide
4030 || event->keyval == GDK_asciitilde
4032 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4034 location_popup_handler (impl, event->string);
4038 if ((event->keyval == GDK_Return
4039 || event->keyval == GDK_ISO_Enter
4040 || event->keyval == GDK_KP_Enter
4041 || event->keyval == GDK_space
4042 || event->keyval == GDK_KP_Space)
4043 && ((event->state & modifiers) == 0)
4044 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4045 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4049 window = get_toplevel (widget);
4051 && widget != window->default_widget
4052 && !(widget == window->focus_widget &&
4053 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4055 gtk_window_activate_default (window);
4063 /* Callback used when the file list's popup menu is detached */
4065 popup_menu_detach_cb (GtkWidget *attach_widget,
4068 GtkFileChooserDefault *impl;
4070 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4071 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4073 impl->browse_files_popup_menu = NULL;
4074 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4075 impl->browse_files_popup_menu_hidden_files_item = NULL;
4078 /* Callback used when the "Add to Bookmarks" menu item is activated */
4080 add_to_shortcuts_cb (GtkMenuItem *item,
4081 GtkFileChooserDefault *impl)
4083 bookmarks_add_selected_folder (impl);
4086 /* Callback used when the "Show Hidden Files" menu item is toggled */
4088 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4089 GtkFileChooserDefault *impl)
4092 "show-hidden", gtk_check_menu_item_get_active (item),
4096 /* Callback used when the "Show Size Column" menu item is toggled */
4098 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4099 GtkFileChooserDefault *impl)
4101 impl->show_size_column = gtk_check_menu_item_get_active (item);
4103 if (impl->list_size_column)
4104 gtk_tree_view_column_set_visible (impl->list_size_column,
4105 impl->show_size_column);
4108 /* Shows an error dialog about not being able to select a dragged file */
4110 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4115 _("Could not select file"),
4120 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4125 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4127 for (i = 1; uris[i]; i++)
4130 GError *error = NULL;
4133 file = g_file_new_for_uri (uri);
4135 gtk_file_chooser_default_select_file (chooser, file, &error);
4137 error_selecting_dragged_file_dialog (impl, file, error);
4139 g_object_unref (file);
4143 struct FileListDragData
4145 GtkFileChooserDefault *impl;
4151 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4153 const GError *error,
4156 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4157 struct FileListDragData *data = user_data;
4158 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4160 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4163 data->impl->file_list_drag_data_received_cancellable = NULL;
4165 if (cancelled || error)
4168 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4169 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4170 data->uris[1] == 0 && !error &&
4171 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4172 change_folder_and_display_error (data->impl, data->file, FALSE);
4175 GError *error = NULL;
4177 gtk_file_chooser_default_unselect_all (chooser);
4178 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4180 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4182 browse_files_center_selected_row (data->impl);
4185 if (data->impl->select_multiple)
4186 file_list_drag_data_select_uris (data->impl, data->uris);
4189 g_object_unref (data->impl);
4190 g_strfreev (data->uris);
4191 g_object_unref (data->file);
4194 g_object_unref (cancellable);
4198 file_list_drag_data_received_cb (GtkWidget *widget,
4199 GdkDragContext *context,
4202 GtkSelectionData *selection_data,
4207 GtkFileChooserDefault *impl;
4208 GtkFileChooser *chooser;
4213 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4214 chooser = GTK_FILE_CHOOSER (data);
4216 /* Allow only drags from other widgets; see bug #533891. */
4217 if (gtk_drag_get_source_widget (context) == widget)
4219 g_signal_stop_emission_by_name (widget, "drag-data-received");
4223 /* Parse the text/uri-list string, navigate to the first one */
4224 uris = gtk_selection_data_get_uris (selection_data);
4225 if (uris && uris[0])
4227 struct FileListDragData *data;
4230 file = g_file_new_for_uri (uri);
4232 data = g_new0 (struct FileListDragData, 1);
4233 data->impl = g_object_ref (impl);
4237 if (impl->file_list_drag_data_received_cancellable)
4238 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4240 impl->file_list_drag_data_received_cancellable =
4241 _gtk_file_system_get_info (impl->file_system, file,
4243 file_list_drag_data_received_get_info_cb,
4247 g_signal_stop_emission_by_name (widget, "drag-data-received");
4250 /* Don't do anything with the drag_drop signal */
4252 file_list_drag_drop_cb (GtkWidget *widget,
4253 GdkDragContext *context,
4257 GtkFileChooserDefault *impl)
4259 g_signal_stop_emission_by_name (widget, "drag-drop");
4263 /* Disable the normal tree drag motion handler, it makes it look like you're
4264 dropping the dragged item onto a tree item */
4266 file_list_drag_motion_cb (GtkWidget *widget,
4267 GdkDragContext *context,
4271 GtkFileChooserDefault *impl)
4273 g_signal_stop_emission_by_name (widget, "drag-motion");
4277 /* Constructs the popup menu for the file list if needed */
4279 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4283 if (impl->browse_files_popup_menu)
4286 impl->browse_files_popup_menu = gtk_menu_new ();
4287 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4288 impl->browse_files_tree_view,
4289 popup_menu_detach_cb);
4291 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4292 impl->browse_files_popup_menu_add_shortcut_item = item;
4293 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4294 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4295 gtk_widget_set_sensitive (item, FALSE);
4296 g_signal_connect (item, "activate",
4297 G_CALLBACK (add_to_shortcuts_cb), impl);
4298 gtk_widget_show (item);
4299 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4301 item = gtk_separator_menu_item_new ();
4302 gtk_widget_show (item);
4303 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4305 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4306 impl->browse_files_popup_menu_hidden_files_item = item;
4307 g_signal_connect (item, "toggled",
4308 G_CALLBACK (show_hidden_toggled_cb), impl);
4309 gtk_widget_show (item);
4310 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4312 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4313 impl->browse_files_popup_menu_size_column_item = item;
4314 g_signal_connect (item, "toggled",
4315 G_CALLBACK (show_size_column_toggled_cb), impl);
4316 gtk_widget_show (item);
4317 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4320 /* Updates the popup menu for the file list, creating it if necessary */
4322 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4324 file_list_build_popup_menu (impl);
4326 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4328 /* The sensitivity of the Add to Bookmarks item is set in
4329 * bookmarks_check_add_sensitivity()
4332 /* 'Show Hidden Files' */
4333 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4334 G_CALLBACK (show_hidden_toggled_cb), impl);
4335 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4337 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4338 G_CALLBACK (show_hidden_toggled_cb), impl);
4340 /* 'Show Size Column' */
4341 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4342 G_CALLBACK (show_size_column_toggled_cb), impl);
4343 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4344 impl->show_size_column);
4345 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4346 G_CALLBACK (show_size_column_toggled_cb), impl);
4350 popup_position_func (GtkMenu *menu,
4356 GtkWidget *widget = GTK_WIDGET (user_data);
4357 GdkScreen *screen = gtk_widget_get_screen (widget);
4360 GdkRectangle monitor;
4362 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4364 gdk_window_get_origin (widget->window, x, y);
4366 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4368 *x += (widget->allocation.width - req.width) / 2;
4369 *y += (widget->allocation.height - req.height) / 2;
4371 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4372 gtk_menu_set_monitor (menu, monitor_num);
4373 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4375 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4376 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4382 file_list_popup_menu (GtkFileChooserDefault *impl,
4383 GdkEventButton *event)
4385 file_list_update_popup_menu (impl);
4387 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4388 NULL, NULL, NULL, NULL,
4389 event->button, event->time);
4392 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4394 popup_position_func, impl->browse_files_tree_view,
4395 0, GDK_CURRENT_TIME);
4396 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4402 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4404 list_popup_menu_cb (GtkWidget *widget,
4405 GtkFileChooserDefault *impl)
4407 file_list_popup_menu (impl, NULL);
4411 /* Callback used when a button is pressed on the file list. We trap button 3 to
4412 * bring up a popup menu.
4415 list_button_press_event_cb (GtkWidget *widget,
4416 GdkEventButton *event,
4417 GtkFileChooserDefault *impl)
4419 static gboolean in_press = FALSE;
4425 if (event->button != 3)
4429 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4432 file_list_popup_menu (impl, event);
4436 /* Sets the sort column IDs for the file list based on the operation mode */
4438 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4440 int name_id, mtime_id, size_id;
4442 name_id = mtime_id = size_id = 0;
4444 switch (impl->operation_mode)
4446 case OPERATION_MODE_BROWSE:
4447 name_id = FILE_LIST_COL_NAME;
4448 mtime_id = FILE_LIST_COL_MTIME;
4449 size_id = FILE_LIST_COL_SIZE;
4451 case OPERATION_MODE_SEARCH:
4452 name_id = SEARCH_MODEL_COL_FILE;
4453 mtime_id = SEARCH_MODEL_COL_STAT;
4455 case OPERATION_MODE_RECENT:
4456 name_id = RECENT_MODEL_COL_FILE;
4457 mtime_id = RECENT_MODEL_COL_INFO;
4461 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4462 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4463 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, size_id);
4467 file_list_query_tooltip_cb (GtkWidget *widget,
4470 gboolean keyboard_tip,
4471 GtkTooltip *tooltip,
4474 GtkFileChooserDefault *impl = user_data;
4475 GtkTreeIter iter, child_iter;
4476 GtkTreePath *path = NULL;
4480 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4484 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4492 switch (impl->operation_mode)
4494 case OPERATION_MODE_SEARCH:
4495 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4497 gtk_tree_path_free (path);
4501 search_get_valid_child_iter (impl, &child_iter, &iter);
4502 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4503 SEARCH_MODEL_COL_FILE, &file,
4507 case OPERATION_MODE_RECENT:
4508 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4510 gtk_tree_path_free (path);
4514 recent_get_valid_child_iter (impl, &child_iter, &iter);
4515 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4516 RECENT_MODEL_COL_FILE, &file,
4520 case OPERATION_MODE_BROWSE:
4521 g_assert_not_reached ();
4527 gtk_tree_path_free (path);
4531 filename = g_file_get_path (file);
4532 gtk_tooltip_set_text (tooltip, filename);
4533 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4538 gtk_tree_path_free (path);
4543 /* Creates the widgets for the file list */
4545 create_file_list (GtkFileChooserDefault *impl)
4548 GtkTreeSelection *selection;
4549 GtkTreeViewColumn *column;
4550 GtkCellRenderer *renderer;
4552 /* Scrolled window */
4553 swin = gtk_scrolled_window_new (NULL, NULL);
4554 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4555 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4556 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4559 /* Tree/list view */
4561 impl->browse_files_tree_view = gtk_tree_view_new ();
4562 #ifdef PROFILE_FILE_CHOOSER
4563 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4565 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4566 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4568 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4569 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4571 gtk_drag_dest_set (impl->browse_files_tree_view,
4572 GTK_DEST_DEFAULT_ALL,
4574 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4575 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4577 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4578 G_CALLBACK (list_row_activated), impl);
4579 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4580 G_CALLBACK (trap_activate_cb), impl);
4581 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4582 G_CALLBACK (list_popup_menu_cb), impl);
4583 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4584 G_CALLBACK (list_button_press_event_cb), impl);
4586 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4587 G_CALLBACK (file_list_drag_data_received_cb), impl);
4588 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4589 G_CALLBACK (file_list_drag_drop_cb), impl);
4590 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4591 G_CALLBACK (file_list_drag_motion_cb), impl);
4593 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4594 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4595 G_CALLBACK (file_list_query_tooltip_cb), impl);
4597 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4598 gtk_tree_selection_set_select_function (selection,
4601 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4604 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4605 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4607 g_signal_connect (selection, "changed",
4608 G_CALLBACK (list_selection_changed), impl);
4610 /* Filename column */
4612 impl->list_name_column = gtk_tree_view_column_new ();
4613 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4614 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4615 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4616 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4618 renderer = gtk_cell_renderer_pixbuf_new ();
4619 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4620 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4621 list_icon_data_func, impl, NULL);
4623 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4624 g_object_set (impl->list_name_renderer,
4625 "ellipsize", PANGO_ELLIPSIZE_END,
4627 g_signal_connect (impl->list_name_renderer, "edited",
4628 G_CALLBACK (renderer_edited_cb), impl);
4629 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4630 G_CALLBACK (renderer_editing_canceled_cb), impl);
4631 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4632 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4633 list_name_data_func, impl, NULL);
4635 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4639 column = gtk_tree_view_column_new ();
4640 gtk_tree_view_column_set_title (column, _("Size"));
4642 renderer = gtk_cell_renderer_text_new ();
4643 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4644 gtk_tree_view_column_set_cell_data_func (column, renderer,
4645 list_size_data_func, impl, NULL);
4646 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4647 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4648 impl->list_size_column = column;
4650 /* Modification time column */
4652 column = gtk_tree_view_column_new ();
4653 gtk_tree_view_column_set_resizable (column, TRUE);
4654 gtk_tree_view_column_set_title (column, _("Modified"));
4656 renderer = gtk_cell_renderer_text_new ();
4657 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4658 gtk_tree_view_column_set_cell_data_func (column, renderer,
4659 list_mtime_data_func, impl, NULL);
4660 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4661 impl->list_mtime_column = column;
4663 file_list_set_sort_column_ids (impl);
4665 gtk_widget_show_all (swin);
4671 create_path_bar (GtkFileChooserDefault *impl)
4673 GtkWidget *path_bar;
4675 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4676 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4681 /* Creates the widgets for the files/folders pane */
4683 file_pane_create (GtkFileChooserDefault *impl,
4684 GtkSizeGroup *size_group)
4690 vbox = gtk_vbox_new (FALSE, 6);
4691 gtk_widget_show (vbox);
4693 /* Box for lists and preview */
4695 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4696 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4697 gtk_widget_show (hbox);
4701 widget = create_file_list (impl);
4702 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4706 impl->preview_box = gtk_vbox_new (FALSE, 12);
4707 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4708 /* Don't show preview box initially */
4712 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4714 widget = filter_create (impl);
4716 gtk_widget_show (widget);
4717 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4719 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4720 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4725 /* Callback used when the "Browse for more folders" expander is toggled */
4727 expander_changed_cb (GtkExpander *expander,
4729 GtkFileChooserDefault *impl)
4731 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4732 update_appearance (impl);
4735 /* Callback used when the selection changes in the save folder combo box */
4737 save_folder_combo_changed_cb (GtkComboBox *combo,
4738 GtkFileChooserDefault *impl)
4742 if (impl->changing_folder)
4745 if (gtk_combo_box_get_active_iter (combo, &iter))
4747 GtkTreeIter child_iter;
4749 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4752 shortcuts_activate_iter (impl, &child_iter);
4756 /* Filter function used to filter out the Search item and its separator.
4757 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4760 shortcuts_combo_filter_func (GtkTreeModel *model,
4764 GtkFileChooserDefault *impl;
4765 GtkTreePath *tree_path;
4770 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4772 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4774 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4775 g_assert (tree_path != NULL);
4777 indices = gtk_tree_path_get_indices (tree_path);
4781 if (impl->has_search)
4783 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4784 if (idx == indices[0])
4788 if (impl->has_recent)
4790 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4791 if (idx == indices[0])
4795 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4796 if (idx == indices[0])
4801 gtk_tree_path_free (tree_path);
4806 /* Creates the combo box with the save folders */
4808 save_folder_combo_create (GtkFileChooserDefault *impl)
4811 GtkCellRenderer *cell;
4813 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4814 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4815 shortcuts_combo_filter_func,
4819 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4820 "model", impl->shortcuts_combo_filter_model,
4821 "focus-on-click", FALSE,
4823 gtk_widget_show (combo);
4825 cell = gtk_cell_renderer_pixbuf_new ();
4826 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4827 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4828 "pixbuf", SHORTCUTS_COL_PIXBUF,
4829 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4830 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4833 cell = gtk_cell_renderer_text_new ();
4834 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4835 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4836 "text", SHORTCUTS_COL_NAME,
4837 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4840 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4841 shortcuts_row_separator_func,
4844 g_signal_connect (combo, "changed",
4845 G_CALLBACK (save_folder_combo_changed_cb), impl);
4850 /* Creates the widgets specific to Save mode */
4852 save_widgets_create (GtkFileChooserDefault *impl)
4857 GtkWidget *alignment;
4859 if (impl->save_widgets != NULL)
4862 location_switch_to_path_bar (impl);
4864 vbox = gtk_vbox_new (FALSE, 12);
4866 table = gtk_table_new (2, 2, FALSE);
4867 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4868 gtk_widget_show (table);
4869 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4870 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4874 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4875 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4876 gtk_table_attach (GTK_TABLE (table), widget,
4880 gtk_widget_show (widget);
4882 /* Location entry */
4884 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4885 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4887 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4888 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4889 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4891 GTK_EXPAND | GTK_FILL, 0,
4893 gtk_widget_show (impl->location_entry);
4894 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4897 impl->save_folder_label = gtk_label_new (NULL);
4898 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4899 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4903 gtk_widget_show (impl->save_folder_label);
4905 impl->save_folder_combo = save_folder_combo_create (impl);
4906 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4908 GTK_EXPAND | GTK_FILL, GTK_FILL,
4910 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4913 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4914 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4916 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4917 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4918 g_signal_connect (impl->save_expander, "notify::expanded",
4919 G_CALLBACK (expander_changed_cb),
4921 gtk_widget_show_all (alignment);
4923 impl->save_widgets = vbox;
4924 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4925 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4926 gtk_widget_show (impl->save_widgets);
4929 /* Destroys the widgets specific to Save mode */
4931 save_widgets_destroy (GtkFileChooserDefault *impl)
4933 if (impl->save_widgets == NULL)
4936 gtk_widget_destroy (impl->save_widgets);
4937 impl->save_widgets = NULL;
4938 impl->location_entry = NULL;
4939 impl->save_folder_label = NULL;
4940 impl->save_folder_combo = NULL;
4941 impl->save_expander = NULL;
4944 /* Turns on the path bar widget. Can be called even if we are already in that
4948 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4950 if (impl->location_entry)
4952 gtk_widget_destroy (impl->location_entry);
4953 impl->location_entry = NULL;
4956 gtk_widget_hide (impl->location_entry_box);
4959 /* Sets the full path of the current folder as the text in the location entry. */
4961 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4963 gchar *text, *filename;
4965 if (!impl->current_folder)
4968 filename = g_file_get_path (impl->current_folder);
4972 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4976 text = g_file_get_uri (impl->current_folder);
4980 gboolean need_slash;
4983 len = strlen (text);
4984 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4990 slash_text = g_new (char, len + 2);
4991 strcpy (slash_text, text);
4992 slash_text[len] = G_DIR_SEPARATOR;
4993 slash_text[len + 1] = 0;
4999 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5006 /* Turns on the location entry. Can be called even if we are already in that
5010 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5012 /* when in search or recent files mode, we are not showing the
5013 * location_entry_box container, so there's no point in switching
5016 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5017 impl->operation_mode == OPERATION_MODE_RECENT)
5020 if (impl->location_entry)
5021 gtk_widget_destroy (impl->location_entry);
5025 gtk_widget_show (impl->location_entry_box);
5029 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5030 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5032 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5033 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5035 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5036 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5038 /* Configure the entry */
5040 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5044 gtk_widget_show (impl->location_entry);
5045 gtk_widget_grab_focus (impl->location_entry);
5048 /* Sets a new location mode. set_buttons determines whether the toggle button
5049 * for the mode will also be changed.
5052 location_mode_set (GtkFileChooserDefault *impl,
5053 LocationMode new_mode,
5054 gboolean set_button)
5056 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5057 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5059 GtkWindow *toplevel;
5060 GtkWidget *current_focus;
5061 gboolean button_active;
5062 gboolean switch_to_file_list;
5066 case LOCATION_MODE_PATH_BAR:
5067 button_active = FALSE;
5069 /* The location_entry will disappear when we switch to path bar mode. So,
5070 * we'll focus the file list in that case, to avoid having a window with
5071 * no focused widget.
5073 toplevel = get_toplevel (GTK_WIDGET (impl));
5074 switch_to_file_list = FALSE;
5077 current_focus = gtk_window_get_focus (toplevel);
5078 if (!current_focus || current_focus == impl->location_entry)
5079 switch_to_file_list = TRUE;
5082 location_switch_to_path_bar (impl);
5084 if (switch_to_file_list)
5085 gtk_widget_grab_focus (impl->browse_files_tree_view);
5089 case LOCATION_MODE_FILENAME_ENTRY:
5090 button_active = TRUE;
5091 location_switch_to_filename_entry (impl);
5095 g_assert_not_reached ();
5101 g_signal_handlers_block_by_func (impl->location_button,
5102 G_CALLBACK (location_button_toggled_cb), impl);
5104 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5106 g_signal_handlers_unblock_by_func (impl->location_button,
5107 G_CALLBACK (location_button_toggled_cb), impl);
5111 impl->location_mode = new_mode;
5115 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5117 /* when in search or recent files mode, we are not showing the
5118 * location_entry_box container, so there's no point in switching
5121 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5122 impl->operation_mode == OPERATION_MODE_RECENT)
5125 /* If the file entry is not visible, show it.
5126 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5128 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5130 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5132 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5134 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5136 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5140 gtk_widget_grab_focus (impl->location_entry);
5145 /* Callback used when one of the location mode buttons is toggled */
5147 location_button_toggled_cb (GtkToggleButton *toggle,
5148 GtkFileChooserDefault *impl)
5151 LocationMode new_mode;
5153 is_active = gtk_toggle_button_get_active (toggle);
5157 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5158 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5162 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5163 new_mode = LOCATION_MODE_PATH_BAR;
5166 location_mode_set (impl, new_mode, FALSE);
5169 /* Creates a toggle button for the location entry. */
5171 location_button_create (GtkFileChooserDefault *impl)
5176 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5177 gtk_widget_show (image);
5179 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5183 g_signal_connect (impl->location_button, "toggled",
5184 G_CALLBACK (location_button_toggled_cb), impl);
5186 str = _("Type a file name");
5188 gtk_widget_set_tooltip_text (impl->location_button, str);
5189 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5192 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5194 browse_widgets_create (GtkFileChooserDefault *impl)
5200 GtkSizeGroup *size_group;
5202 /* size group is used by the [+][-] buttons and the filter combo */
5203 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5204 vbox = gtk_vbox_new (FALSE, 12);
5206 /* Location widgets */
5207 hbox = gtk_hbox_new (FALSE, 12);
5208 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5209 gtk_widget_show (hbox);
5210 impl->browse_path_bar_hbox = hbox;
5212 location_button_create (impl);
5213 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5217 impl->browse_path_bar = create_path_bar (impl);
5218 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5219 gtk_widget_show_all (impl->browse_path_bar);
5220 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5223 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5224 g_signal_connect (impl->browse_new_folder_button, "clicked",
5225 G_CALLBACK (new_folder_button_clicked), impl);
5226 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5228 /* Box for the location label and entry */
5230 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5231 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5233 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5234 gtk_widget_show (impl->location_label);
5235 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5238 hpaned = gtk_hpaned_new ();
5239 gtk_widget_show (hpaned);
5240 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5242 widget = shortcuts_pane_create (impl, size_group);
5243 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5244 widget = file_pane_create (impl, size_group);
5245 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5247 g_object_unref (size_group);
5253 gtk_file_chooser_default_constructor (GType type,
5254 guint n_construct_properties,
5255 GObjectConstructParam *construct_params)
5257 GtkFileChooserDefault *impl;
5260 profile_start ("start", NULL);
5262 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5263 n_construct_properties,
5265 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5267 g_assert (impl->file_system);
5269 gtk_widget_push_composite_child ();
5271 /* Shortcuts model */
5272 shortcuts_model_create (impl);
5274 /* The browse widgets */
5275 impl->browse_widgets = browse_widgets_create (impl);
5276 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5278 /* Alignment to hold extra widget */
5279 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5280 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5282 gtk_widget_pop_composite_child ();
5283 update_appearance (impl);
5285 profile_end ("end", NULL);
5290 /* Sets the extra_widget by packing it in the appropriate place */
5292 set_extra_widget (GtkFileChooserDefault *impl,
5293 GtkWidget *extra_widget)
5297 g_object_ref (extra_widget);
5298 /* FIXME: is this right ? */
5299 gtk_widget_show (extra_widget);
5302 if (impl->extra_widget)
5304 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5305 g_object_unref (impl->extra_widget);
5308 impl->extra_widget = extra_widget;
5309 if (impl->extra_widget)
5311 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5312 gtk_widget_show (impl->extra_align);
5315 gtk_widget_hide (impl->extra_align);
5319 set_local_only (GtkFileChooserDefault *impl,
5320 gboolean local_only)
5322 if (local_only != impl->local_only)
5324 impl->local_only = local_only;
5326 if (impl->shortcuts_model && impl->file_system)
5328 shortcuts_add_volumes (impl);
5329 shortcuts_add_bookmarks (impl);
5332 if (local_only && !g_file_is_native (impl->current_folder))
5334 /* If we are pointing to a non-local folder, make an effort to change
5335 * back to a local folder, but it's really up to the app to not cause
5336 * such a situation, so we ignore errors.
5338 const gchar *home = g_get_home_dir ();
5344 home_file = g_file_new_for_path (home);
5346 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5348 g_object_unref (home_file);
5354 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5355 GtkFileChooserDefault *impl)
5357 shortcuts_add_volumes (impl);
5358 shortcuts_add_bookmarks (impl);
5360 bookmarks_check_add_sensitivity (impl);
5361 bookmarks_check_remove_sensitivity (impl);
5362 shortcuts_check_popup_sensitivity (impl);
5365 /* Sets the file chooser to multiple selection mode */
5367 set_select_multiple (GtkFileChooserDefault *impl,
5368 gboolean select_multiple,
5369 gboolean property_notify)
5371 GtkTreeSelection *selection;
5372 GtkSelectionMode mode;
5374 if (select_multiple == impl->select_multiple)
5377 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5379 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5380 gtk_tree_selection_set_mode (selection, mode);
5382 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5384 impl->select_multiple = select_multiple;
5385 g_object_notify (G_OBJECT (impl), "select-multiple");
5387 check_preview_change (impl);
5391 set_file_system_backend (GtkFileChooserDefault *impl)
5393 profile_start ("start for backend", "default");
5395 impl->file_system = _gtk_file_system_new ();
5397 g_signal_connect (impl->file_system, "volumes-changed",
5398 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5399 g_signal_connect (impl->file_system, "bookmarks-changed",
5400 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5402 profile_end ("end", NULL);
5405 /* This function is basically a do_all function.
5407 * It sets the visibility on all the widgets based on the current state, and
5408 * moves the custom_widget if needed.
5411 update_appearance (GtkFileChooserDefault *impl)
5413 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5414 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5418 gtk_widget_hide (impl->location_button);
5419 save_widgets_create (impl);
5421 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5422 text = _("Save in _folder:");
5424 text = _("Create in _folder:");
5426 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5428 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5430 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5431 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5432 gtk_widget_show (impl->browse_widgets);
5436 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5437 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5438 gtk_widget_hide (impl->browse_widgets);
5441 gtk_widget_show (impl->browse_new_folder_button);
5443 if (impl->select_multiple)
5445 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5446 "Re-setting to single selection mode.");
5447 set_select_multiple (impl, FALSE, TRUE);
5450 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5451 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5453 gtk_widget_show (impl->location_button);
5454 save_widgets_destroy (impl);
5455 gtk_widget_show (impl->browse_widgets);
5456 location_mode_set (impl, impl->location_mode, TRUE);
5459 if (impl->location_entry)
5460 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5462 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5463 gtk_widget_hide (impl->browse_new_folder_button);
5465 gtk_widget_show (impl->browse_new_folder_button);
5467 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5468 * of files may change depending whether we are in a file or folder-only mode.
5470 gtk_widget_queue_draw (impl->browse_files_tree_view);
5472 emit_default_size_changed (impl);
5476 gtk_file_chooser_default_set_property (GObject *object,
5478 const GValue *value,
5482 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5486 case GTK_FILE_CHOOSER_PROP_ACTION:
5488 GtkFileChooserAction action = g_value_get_enum (value);
5490 if (action != impl->action)
5492 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5494 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5495 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5496 && impl->select_multiple)
5498 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5499 "this is not allowed in multiple selection mode. Resetting the file chooser "
5500 "to single selection mode.");
5501 set_select_multiple (impl, FALSE, TRUE);
5503 impl->action = action;
5504 update_appearance (impl);
5505 settings_load (impl);
5510 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5511 /* Ignore property */
5514 case GTK_FILE_CHOOSER_PROP_FILTER:
5515 set_current_filter (impl, g_value_get_object (value));
5518 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5519 set_local_only (impl, g_value_get_boolean (value));
5522 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5523 set_preview_widget (impl, g_value_get_object (value));
5526 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5527 impl->preview_widget_active = g_value_get_boolean (value);
5528 update_preview_widget_visibility (impl);
5531 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5532 impl->use_preview_label = g_value_get_boolean (value);
5533 update_preview_widget_visibility (impl);
5536 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5537 set_extra_widget (impl, g_value_get_object (value));
5540 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5542 gboolean select_multiple = g_value_get_boolean (value);
5543 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5544 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5547 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5548 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5549 "leaving the file chooser in single selection mode.");
5553 set_select_multiple (impl, select_multiple, FALSE);
5557 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5559 gboolean show_hidden = g_value_get_boolean (value);
5560 if (show_hidden != impl->show_hidden)
5562 impl->show_hidden = show_hidden;
5564 if (impl->browse_files_model)
5565 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5570 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5572 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5573 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5578 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5584 gtk_file_chooser_default_get_property (GObject *object,
5589 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5593 case GTK_FILE_CHOOSER_PROP_ACTION:
5594 g_value_set_enum (value, impl->action);
5597 case GTK_FILE_CHOOSER_PROP_FILTER:
5598 g_value_set_object (value, impl->current_filter);
5601 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5602 g_value_set_boolean (value, impl->local_only);
5605 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5606 g_value_set_object (value, impl->preview_widget);
5609 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5610 g_value_set_boolean (value, impl->preview_widget_active);
5613 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5614 g_value_set_boolean (value, impl->use_preview_label);
5617 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5618 g_value_set_object (value, impl->extra_widget);
5621 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5622 g_value_set_boolean (value, impl->select_multiple);
5625 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5626 g_value_set_boolean (value, impl->show_hidden);
5629 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5630 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5634 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5639 /* Removes the settings signal handler. It's safe to call multiple times */
5641 remove_settings_signal (GtkFileChooserDefault *impl,
5644 if (impl->settings_signal_id)
5646 GtkSettings *settings;
5648 settings = gtk_settings_get_for_screen (screen);
5649 g_signal_handler_disconnect (settings,
5650 impl->settings_signal_id);
5651 impl->settings_signal_id = 0;
5656 gtk_file_chooser_default_dispose (GObject *object)
5659 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5661 if (impl->extra_widget)
5663 g_object_unref (impl->extra_widget);
5664 impl->extra_widget = NULL;
5667 pending_select_files_free (impl);
5669 /* cancel all pending operations */
5670 if (impl->pending_cancellables)
5672 for (l = impl->pending_cancellables; l; l = l->next)
5674 GCancellable *cancellable = G_CANCELLABLE (l->data);
5675 g_cancellable_cancel (cancellable);
5677 g_slist_free (impl->pending_cancellables);
5678 impl->pending_cancellables = NULL;
5681 if (impl->reload_icon_cancellables)
5683 for (l = impl->reload_icon_cancellables; l; l = l->next)
5685 GCancellable *cancellable = G_CANCELLABLE (l->data);
5686 g_cancellable_cancel (cancellable);
5688 g_slist_free (impl->reload_icon_cancellables);
5689 impl->reload_icon_cancellables = NULL;
5692 if (impl->loading_shortcuts)
5694 for (l = impl->loading_shortcuts; l; l = l->next)
5696 GCancellable *cancellable = G_CANCELLABLE (l->data);
5697 g_cancellable_cancel (cancellable);
5699 g_slist_free (impl->loading_shortcuts);
5700 impl->loading_shortcuts = NULL;
5703 if (impl->file_list_drag_data_received_cancellable)
5705 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5706 impl->file_list_drag_data_received_cancellable = NULL;
5709 if (impl->update_current_folder_cancellable)
5711 g_cancellable_cancel (impl->update_current_folder_cancellable);
5712 impl->update_current_folder_cancellable = NULL;
5715 if (impl->show_and_select_files_cancellable)
5717 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5718 impl->show_and_select_files_cancellable = NULL;
5721 if (impl->should_respond_get_info_cancellable)
5723 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5724 impl->should_respond_get_info_cancellable = NULL;
5727 if (impl->update_from_entry_cancellable)
5729 g_cancellable_cancel (impl->update_from_entry_cancellable);
5730 impl->update_from_entry_cancellable = NULL;
5733 if (impl->shortcuts_activate_iter_cancellable)
5735 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5736 impl->shortcuts_activate_iter_cancellable = NULL;
5739 search_stop_searching (impl, TRUE);
5740 recent_stop_loading (impl);
5742 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5744 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5747 /* We override show-all since we have internal widgets that
5748 * shouldn't be shown when you call show_all(), like the filter
5752 gtk_file_chooser_default_show_all (GtkWidget *widget)
5754 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5756 gtk_widget_show (widget);
5758 if (impl->extra_widget)
5759 gtk_widget_show_all (impl->extra_widget);
5762 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5763 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5766 toplevel_set_focus_cb (GtkWindow *window,
5768 GtkFileChooserDefault *impl)
5770 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5773 /* We monitor the focus widget on our toplevel to be able to know which widget
5774 * was last focused at the time our "should_respond" method gets called.
5777 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5778 GtkWidget *previous_toplevel)
5780 GtkFileChooserDefault *impl;
5781 GtkWidget *toplevel;
5783 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5785 if (previous_toplevel)
5787 g_assert (impl->toplevel_set_focus_id != 0);
5788 g_signal_handler_disconnect (previous_toplevel,
5789 impl->toplevel_set_focus_id);
5790 impl->toplevel_set_focus_id = 0;
5791 impl->toplevel_last_focus_widget = NULL;
5794 g_assert (impl->toplevel_set_focus_id == 0);
5796 toplevel = gtk_widget_get_toplevel (widget);
5797 if (GTK_IS_WINDOW (toplevel))
5799 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5800 G_CALLBACK (toplevel_set_focus_cb), impl);
5801 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5805 /* Changes the icons wherever it is needed */
5807 change_icon_theme (GtkFileChooserDefault *impl)
5809 GtkSettings *settings;
5812 profile_start ("start", NULL);
5814 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5816 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5817 impl->icon_size = MAX (width, height);
5819 impl->icon_size = FALLBACK_ICON_SIZE;
5821 shortcuts_reload_icons (impl);
5822 gtk_widget_queue_resize (impl->browse_files_tree_view);
5824 profile_end ("end", NULL);
5827 /* Callback used when a GtkSettings value changes */
5829 settings_notify_cb (GObject *object,
5831 GtkFileChooserDefault *impl)
5835 profile_start ("start", NULL);
5837 name = g_param_spec_get_name (pspec);
5839 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5840 strcmp (name, "gtk-icon-sizes") == 0)
5841 change_icon_theme (impl);
5843 profile_end ("end", NULL);
5846 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5850 check_icon_theme (GtkFileChooserDefault *impl)
5852 GtkSettings *settings;
5854 profile_start ("start", NULL);
5856 if (impl->settings_signal_id)
5858 profile_end ("end", NULL);
5862 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5864 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5865 impl->settings_signal_id = g_signal_connect (settings, "notify",
5866 G_CALLBACK (settings_notify_cb), impl);
5868 change_icon_theme (impl);
5871 profile_end ("end", NULL);
5875 gtk_file_chooser_default_style_set (GtkWidget *widget,
5876 GtkStyle *previous_style)
5878 GtkFileChooserDefault *impl;
5880 profile_start ("start", NULL);
5882 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5884 profile_msg (" parent class style_set start", NULL);
5885 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5886 profile_msg (" parent class style_set end", NULL);
5888 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5889 change_icon_theme (impl);
5891 emit_default_size_changed (impl);
5893 profile_end ("end", NULL);
5897 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5898 GdkScreen *previous_screen)
5900 GtkFileChooserDefault *impl;
5902 profile_start ("start", NULL);
5904 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5906 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5907 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5909 remove_settings_signal (impl, previous_screen);
5910 check_icon_theme (impl);
5912 emit_default_size_changed (impl);
5914 profile_end ("end", NULL);
5918 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5919 GtkAllocation *allocation)
5921 GtkFileChooserDefault *impl;
5923 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5925 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5929 get_is_file_filtered (GtkFileChooserDefault *impl,
5931 GFileInfo *file_info)
5933 GtkFileFilterInfo filter_info;
5934 GtkFileFilterFlags needed;
5937 if (!impl->current_filter)
5940 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5942 needed = gtk_file_filter_get_needed (impl->current_filter);
5944 filter_info.display_name = g_file_info_get_display_name (file_info);
5945 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
5947 if (needed & GTK_FILE_FILTER_FILENAME)
5949 filter_info.filename = g_file_get_path (file);
5950 if (filter_info.filename)
5951 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5954 filter_info.filename = NULL;
5956 if (needed & GTK_FILE_FILTER_URI)
5958 filter_info.uri = g_file_get_uri (file);
5959 if (filter_info.uri)
5960 filter_info.contains |= GTK_FILE_FILTER_URI;
5963 filter_info.uri = NULL;
5965 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5967 g_free ((gchar *)filter_info.filename);
5968 g_free ((gchar *)filter_info.uri);
5969 g_free ((gchar *)filter_info.mime_type);
5975 settings_load (GtkFileChooserDefault *impl)
5977 GtkFileChooserSettings *settings;
5978 LocationMode location_mode;
5979 gboolean show_hidden;
5980 gboolean expand_folders;
5981 gboolean show_size_column;
5983 settings = _gtk_file_chooser_settings_new ();
5985 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5986 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5987 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5988 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
5990 g_object_unref (settings);
5992 location_mode_set (impl, location_mode, TRUE);
5993 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5994 impl->expand_folders = expand_folders;
5995 if (impl->save_expander)
5996 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5997 impl->show_size_column = show_size_column;
5998 if (impl->list_size_column)
5999 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6003 settings_save (GtkFileChooserDefault *impl)
6005 GtkFileChooserSettings *settings;
6007 settings = _gtk_file_chooser_settings_new ();
6009 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6010 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6011 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6014 _gtk_file_chooser_settings_save (settings, NULL);
6016 g_object_unref (settings);
6019 /* GtkWidget::map method */
6021 gtk_file_chooser_default_map (GtkWidget *widget)
6023 GtkFileChooserDefault *impl;
6024 char *current_working_dir;
6026 profile_start ("start", NULL);
6028 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6030 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6032 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6034 switch (impl->reload_state)
6037 /* The user didn't explicitly give us a folder to
6038 * display, so we'll use the cwd
6040 current_working_dir = g_get_current_dir ();
6041 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6042 current_working_dir);
6043 g_free (current_working_dir);
6046 case RELOAD_HAS_FOLDER:
6047 /* Nothing; we are already loading or loaded, so we
6048 * don't need to reload
6052 case RELOAD_WAS_UNMAPPED:
6053 /* Just reload the current folder; else continue
6056 if (impl->current_folder)
6058 pending_select_files_store_selection (impl);
6059 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6064 g_assert_not_reached ();
6068 volumes_bookmarks_changed_cb (impl->file_system, impl);
6070 settings_load (impl);
6072 emit_default_size_changed (impl);
6074 profile_end ("end", NULL);
6077 /* GtkWidget::unmap method */
6079 gtk_file_chooser_default_unmap (GtkWidget *widget)
6081 GtkFileChooserDefault *impl;
6083 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6085 settings_save (impl);
6087 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6089 impl->reload_state = RELOAD_WAS_UNMAPPED;
6093 list_model_filter_func (GtkFileSystemModel *model,
6095 GFileInfo *file_info,
6098 GtkFileChooserDefault *impl = user_data;
6100 if (!impl->current_filter)
6103 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6106 return !get_is_file_filtered (impl, file, file_info);
6110 install_list_model_filter (GtkFileChooserDefault *impl)
6112 GtkFileSystemModelFilter filter;
6115 g_assert (impl->browse_files_model != NULL);
6117 if (impl->current_filter)
6119 filter = list_model_filter_func;
6128 _gtk_file_system_model_set_filter (impl->browse_files_model,
6133 #define COMPARE_DIRECTORIES \
6134 GtkFileChooserDefault *impl = user_data; \
6135 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6136 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6137 gboolean dir_a, dir_b; \
6140 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6142 return impl->list_sort_ascending ? -1 : 1; \
6145 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6147 return impl->list_sort_ascending ? 1 : -1; \
6149 if (dir_a != dir_b) \
6150 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6152 /* Sort callback for the filename column */
6154 name_sort_func (GtkTreeModel *model,
6159 COMPARE_DIRECTORIES;
6162 gchar *key_a, *key_b;
6165 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6166 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6167 result = strcmp (key_a, key_b);
6176 /* Sort callback for the size column */
6178 size_sort_func (GtkTreeModel *model,
6183 COMPARE_DIRECTORIES;
6186 goffset size_a = g_file_info_get_size (info_a);
6187 goffset size_b = g_file_info_get_size (info_b);
6189 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6193 /* Sort callback for the mtime column */
6195 mtime_sort_func (GtkTreeModel *model,
6200 COMPARE_DIRECTORIES;
6205 g_file_info_get_modification_time (info_a, &ta);
6206 g_file_info_get_modification_time (info_b, &tb);
6208 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6212 /* Callback used when the sort column changes. We cache the sort order for use
6213 * in name_sort_func().
6216 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6217 GtkFileChooserDefault *impl)
6219 GtkSortType sort_type;
6221 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6222 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6226 set_busy_cursor (GtkFileChooserDefault *impl,
6229 GtkWindow *toplevel;
6230 GdkDisplay *display;
6233 toplevel = get_toplevel (GTK_WIDGET (impl));
6234 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6237 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6240 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6244 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6245 gdk_display_flush (display);
6248 gdk_cursor_unref (cursor);
6251 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6253 load_set_model (GtkFileChooserDefault *impl)
6255 profile_start ("start", NULL);
6257 g_assert (impl->browse_files_model != NULL);
6258 g_assert (impl->sort_model == NULL);
6260 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6261 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6262 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6263 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6264 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6265 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6266 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6267 impl->list_sort_ascending = TRUE;
6268 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6270 g_signal_connect (impl->sort_model, "sort-column-changed",
6271 G_CALLBACK (list_sort_column_changed_cb), impl);
6273 profile_msg (" gtk_tree_view_set_model start", NULL);
6274 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6275 GTK_TREE_MODEL (impl->sort_model));
6276 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6277 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6278 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6279 profile_msg (" gtk_tree_view_set_model end", NULL);
6281 profile_end ("end", NULL);
6284 /* Timeout callback used when the loading timer expires */
6286 load_timeout_cb (gpointer data)
6288 GtkFileChooserDefault *impl;
6290 profile_start ("start", NULL);
6292 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6293 g_assert (impl->load_state == LOAD_PRELOAD);
6294 g_assert (impl->load_timeout_id != 0);
6295 g_assert (impl->browse_files_model != NULL);
6297 impl->load_timeout_id = 0;
6298 impl->load_state = LOAD_LOADING;
6300 load_set_model (impl);
6302 profile_end ("end", NULL);
6307 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6309 load_setup_timer (GtkFileChooserDefault *impl)
6311 g_assert (impl->load_timeout_id == 0);
6312 g_assert (impl->load_state != LOAD_PRELOAD);
6314 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6315 impl->load_state = LOAD_PRELOAD;
6318 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6320 load_remove_timer (GtkFileChooserDefault *impl)
6322 if (impl->load_timeout_id != 0)
6324 g_assert (impl->load_state == LOAD_PRELOAD);
6326 g_source_remove (impl->load_timeout_id);
6327 impl->load_timeout_id = 0;
6328 impl->load_state = LOAD_EMPTY;
6331 g_assert (impl->load_state == LOAD_EMPTY ||
6332 impl->load_state == LOAD_LOADING ||
6333 impl->load_state == LOAD_FINISHED);
6336 /* Selects the first row in the file list */
6338 browse_files_select_first_row (GtkFileChooserDefault *impl)
6341 GtkTreeIter dummy_iter;
6342 GtkTreeModel *tree_model;
6344 if (!impl->sort_model)
6347 path = gtk_tree_path_new_from_indices (0, -1);
6348 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6350 /* If the list is empty, do nothing. */
6351 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6352 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6354 gtk_tree_path_free (path);
6357 struct center_selected_row_closure {
6358 GtkFileChooserDefault *impl;
6359 gboolean already_centered;
6362 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6363 * selected row in the tree view.
6366 center_selected_row_foreach_cb (GtkTreeModel *model,
6371 struct center_selected_row_closure *closure;
6374 if (closure->already_centered)
6377 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6378 closure->already_centered = TRUE;
6381 /* Centers the selected row in the tree view */
6383 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6385 struct center_selected_row_closure closure;
6386 GtkTreeSelection *selection;
6388 closure.impl = impl;
6389 closure.already_centered = FALSE;
6391 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6392 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6395 struct ShowAndSelectPathsData
6397 GtkFileChooserDefault *impl;
6402 show_and_select_files_finished_loading (GtkFolder *folder,
6405 gboolean have_hidden;
6406 gboolean have_filtered;
6408 struct ShowAndSelectPathsData *data = user_data;
6410 have_hidden = FALSE;
6411 have_filtered = FALSE;
6413 for (l = data->files; l; l = l->next)
6420 info = _gtk_folder_get_info (folder, file);
6424 have_hidden = g_file_info_get_is_hidden (info);
6427 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6428 get_is_file_filtered (data->impl, file, info);
6430 g_object_unref (info);
6432 if (have_hidden && have_filtered)
6433 break; /* we now have all the information we need */
6437 g_signal_handlers_disconnect_by_func (folder,
6438 show_and_select_files_finished_loading,
6442 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6445 set_current_filter (data->impl, NULL);
6447 for (l = data->files; l; l = l->next)
6452 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6453 select_func, data->impl);
6456 browse_files_center_selected_row (data->impl);
6458 g_object_unref (data->impl);
6459 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6460 g_slist_free (data->files);
6465 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6467 const GError *error,
6470 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6471 struct ShowAndSelectPathsData *data = user_data;
6473 if (data->impl->show_and_select_files_cancellable != cancellable)
6476 data->impl->show_and_select_files_cancellable = NULL;
6478 if (cancelled || error)
6481 g_object_unref (cancellable);
6483 if (_gtk_folder_is_finished_loading (folder))
6484 show_and_select_files_finished_loading (folder, user_data);
6486 g_signal_connect (folder, "finished-loading",
6487 G_CALLBACK (show_and_select_files_finished_loading),
6493 g_object_unref (data->impl);
6494 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6495 g_slist_free (data->files);
6498 g_object_unref (cancellable);
6502 show_and_select_files (GtkFileChooserDefault *impl,
6507 struct ShowAndSelectPathsData *info;
6509 profile_start ("start", NULL);
6513 profile_end ("end", NULL);
6517 info = g_new (struct ShowAndSelectPathsData, 1);
6518 info->impl = g_object_ref (impl);
6519 info->files = g_slist_copy (files);
6520 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6522 if (impl->show_and_select_files_cancellable)
6523 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6525 impl->show_and_select_files_cancellable =
6526 _gtk_file_system_get_folder (impl->file_system, parent_file,
6527 "standard::is-hidden,standard::type,standard::name",
6528 show_and_select_files_get_folder_cb, info);
6530 profile_end ("end", NULL);
6534 /* Processes the pending operation when a folder is finished loading */
6536 pending_select_files_process (GtkFileChooserDefault *impl)
6538 g_assert (impl->load_state == LOAD_FINISHED);
6539 g_assert (impl->browse_files_model != NULL);
6540 g_assert (impl->sort_model != NULL);
6542 if (impl->pending_select_files)
6545 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6546 pending_select_files_free (impl);
6547 browse_files_center_selected_row (impl);
6551 /* We only select the first row if the chooser is actually mapped ---
6552 * selecting the first row is to help the user when he is interacting with
6553 * the chooser, but sometimes a chooser works not on behalf of the user,
6554 * but rather on behalf of something else like GtkFileChooserButton. In
6555 * that case, the chooser's selection should be what the caller expects,
6556 * as the user can't see that something else got selected. See bug #165264.
6558 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6559 browse_files_select_first_row (impl);
6562 g_assert (impl->pending_select_files == NULL);
6565 /* Callback used when the file system model finishes loading */
6567 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6568 GtkFileChooserDefault *impl)
6570 profile_start ("start", NULL);
6572 if (impl->load_state == LOAD_PRELOAD)
6574 load_remove_timer (impl);
6575 load_set_model (impl);
6577 else if (impl->load_state == LOAD_LOADING)
6583 /* We can't g_assert_not_reached(), as something other than us may have
6584 * initiated a folder reload. See #165556.
6586 profile_end ("end", NULL);
6590 g_assert (impl->load_timeout_id == 0);
6592 impl->load_state = LOAD_FINISHED;
6594 pending_select_files_process (impl);
6595 set_busy_cursor (impl, FALSE);
6596 #ifdef PROFILE_FILE_CHOOSER
6597 access ("MARK: *** FINISHED LOADING", F_OK);
6600 profile_end ("end", NULL);
6604 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6606 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6608 if (impl->browse_files_model)
6610 g_object_unref (impl->browse_files_model);
6611 impl->browse_files_model = NULL;
6614 if (impl->sort_model)
6616 g_object_unref (impl->sort_model);
6617 impl->sort_model = NULL;
6620 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6623 /* Gets rid of the old list model and creates a new one for the current folder */
6625 set_list_model (GtkFileChooserDefault *impl,
6628 g_assert (impl->current_folder != NULL);
6630 profile_start ("start", NULL);
6632 stop_loading_and_clear_list_model (impl);
6634 set_busy_cursor (impl, TRUE);
6635 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6637 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6638 impl->current_folder, 0,
6639 "standard,time,thumbnail::*",
6641 if (!impl->browse_files_model)
6643 set_busy_cursor (impl, FALSE);
6644 profile_end ("end", NULL);
6648 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6650 g_signal_connect (impl->browse_files_model, "finished-loading",
6651 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6653 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6655 install_list_model_filter (impl);
6657 profile_end ("end", NULL);
6662 struct update_chooser_entry_selected_foreach_closure {
6664 GtkTreeIter first_selected_iter;
6668 compare_utf8_filenames (const gchar *a,
6671 gchar *a_folded, *b_folded;
6674 a_folded = g_utf8_strdown (a, -1);
6675 b_folded = g_utf8_strdown (b, -1);
6677 retval = strcmp (a_folded, b_folded);
6686 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6691 struct update_chooser_entry_selected_foreach_closure *closure;
6694 closure->num_selected++;
6696 if (closure->num_selected == 1)
6697 closure->first_selected_iter = *iter;
6701 update_chooser_entry (GtkFileChooserDefault *impl)
6703 GtkTreeSelection *selection;
6704 struct update_chooser_entry_selected_foreach_closure closure;
6705 const char *file_part;
6707 /* no need to update the file chooser's entry if there's no entry */
6708 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6709 impl->operation_mode == OPERATION_MODE_RECENT ||
6710 !impl->location_entry)
6713 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6714 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6715 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6716 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6717 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6720 g_assert (impl->location_entry != NULL);
6722 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6723 closure.num_selected = 0;
6724 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6728 if (closure.num_selected == 0)
6730 goto maybe_clear_entry;
6732 else if (closure.num_selected == 1)
6734 GtkTreeIter child_iter;
6736 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6739 gboolean change_entry;
6741 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6743 &closure.first_selected_iter);
6745 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6747 /* If the cursor moved to the row of the newly created folder,
6748 * retrieving info will return NULL.
6753 g_free (impl->browse_files_last_selected_name);
6754 impl->browse_files_last_selected_name =
6755 g_strdup (g_file_info_get_display_name (info));
6757 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6758 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6759 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6761 /* We don't want the name to change when clicking on a folder... */
6762 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6765 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6769 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6771 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6772 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6780 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6781 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6783 /* Multiple selection, so just clear the entry. */
6785 g_free (impl->browse_files_last_selected_name);
6786 impl->browse_files_last_selected_name = NULL;
6788 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6794 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6795 && impl->browse_files_last_selected_name)
6797 const char *entry_text;
6799 gboolean clear_entry;
6801 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6802 len = strlen (entry_text);
6805 /* The file chooser entry may have appended a "/" to its text. So
6806 * take it out, and compare the result to the old selection.
6808 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6812 tmp = g_strndup (entry_text, len - 1);
6813 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6817 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6820 clear_entry = FALSE;
6823 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6828 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6832 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6836 struct UpdateCurrentFolderData
6838 GtkFileChooserDefault *impl;
6840 gboolean keep_trail;
6841 gboolean clear_entry;
6842 GFile *original_file;
6843 GError *original_error;
6847 update_current_folder_get_info_cb (GCancellable *cancellable,
6849 const GError *error,
6852 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6853 struct UpdateCurrentFolderData *data = user_data;
6854 GtkFileChooserDefault *impl = data->impl;
6856 if (cancellable != impl->update_current_folder_cancellable)
6859 impl->update_current_folder_cancellable = NULL;
6860 impl->reload_state = RELOAD_EMPTY;
6862 set_busy_cursor (impl, FALSE);
6871 if (!data->original_file)
6873 data->original_file = g_object_ref (data->file);
6874 data->original_error = g_error_copy (error);
6877 parent_file = g_file_get_parent (data->file);
6879 /* get parent path and try to change the folder to that */
6882 g_object_unref (data->file);
6883 data->file = parent_file;
6885 g_object_unref (cancellable);
6887 /* restart the update current folder operation */
6888 impl->reload_state = RELOAD_HAS_FOLDER;
6890 impl->update_current_folder_cancellable =
6891 _gtk_file_system_get_info (impl->file_system, data->file,
6893 update_current_folder_get_info_cb,
6896 set_busy_cursor (impl, TRUE);
6902 /* error and bail out */
6903 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6904 g_object_unref (data->original_file);
6910 if (data->original_file)
6912 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6914 g_object_unref (data->original_file);
6917 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6920 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6923 if (impl->current_folder != data->file)
6925 if (impl->current_folder)
6926 g_object_unref (impl->current_folder);
6928 impl->current_folder = g_object_ref (data->file);
6930 impl->reload_state = RELOAD_HAS_FOLDER;
6933 /* Update the widgets that may trigger a folder change themselves. */
6935 if (!impl->changing_folder)
6937 impl->changing_folder = TRUE;
6939 shortcuts_update_current_folder (impl);
6941 impl->changing_folder = FALSE;
6944 /* Set the folder on the save entry */
6946 if (impl->location_entry)
6948 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6949 impl->current_folder);
6951 if (data->clear_entry)
6952 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6955 /* Create a new list model. This is slightly evil; we store the result value
6956 * but perform more actions rather than returning immediately even if it
6957 * generates an error.
6959 set_list_model (impl, NULL);
6961 /* Refresh controls */
6963 shortcuts_find_current_folder (impl);
6965 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6967 check_preview_change (impl);
6968 bookmarks_check_add_sensitivity (impl);
6970 g_signal_emit_by_name (impl, "selection-changed", 0);
6973 g_object_unref (data->file);
6976 g_object_unref (cancellable);
6980 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6982 gboolean keep_trail,
6983 gboolean clear_entry,
6986 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6987 struct UpdateCurrentFolderData *data;
6989 profile_start ("start", NULL);
6991 g_object_ref (file);
6993 switch (impl->operation_mode)
6995 case OPERATION_MODE_SEARCH:
6996 search_switch_to_browse_mode (impl);
6998 case OPERATION_MODE_RECENT:
6999 recent_switch_to_browse_mode (impl);
7001 case OPERATION_MODE_BROWSE:
7005 if (impl->local_only && !g_file_is_native (file))
7007 g_set_error_literal (error,
7008 GTK_FILE_CHOOSER_ERROR,
7009 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7010 _("Cannot change to folder because it is not local"));
7012 g_object_unref (file);
7013 profile_end ("end - not local", NULL);
7017 if (impl->update_current_folder_cancellable)
7018 g_cancellable_cancel (impl->update_current_folder_cancellable);
7020 /* Test validity of path here. */
7021 data = g_new0 (struct UpdateCurrentFolderData, 1);
7023 data->file = g_object_ref (file);
7024 data->keep_trail = keep_trail;
7025 data->clear_entry = clear_entry;
7027 impl->reload_state = RELOAD_HAS_FOLDER;
7029 impl->update_current_folder_cancellable =
7030 _gtk_file_system_get_info (impl->file_system, file,
7032 update_current_folder_get_info_cb,
7035 set_busy_cursor (impl, TRUE);
7036 g_object_unref (file);
7038 profile_end ("end", NULL);
7043 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7045 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7047 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7048 impl->operation_mode == OPERATION_MODE_RECENT)
7051 if (impl->reload_state == RELOAD_EMPTY)
7053 char *current_working_dir;
7056 /* We are unmapped, or we had an error while loading the last folder. We'll return
7057 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7058 * explicitly calls set_current_folder() on us.
7060 current_working_dir = g_get_current_dir ();
7061 file = g_file_new_for_path (current_working_dir);
7062 g_free (current_working_dir);
7066 if (impl->current_folder)
7067 return g_object_ref (impl->current_folder);
7073 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7076 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7078 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7079 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7081 pending_select_files_free (impl);
7082 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7086 select_func (GtkFileSystemModel *model,
7091 GtkFileChooserDefault *impl = user_data;
7092 GtkTreeSelection *selection;
7093 GtkTreeIter sorted_iter;
7095 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7097 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7098 gtk_tree_selection_select_iter (selection, &sorted_iter);
7102 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7106 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7110 parent_file = g_file_get_parent (file);
7113 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7115 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7116 impl->operation_mode == OPERATION_MODE_RECENT ||
7117 impl->load_state == LOAD_EMPTY)
7123 g_assert (impl->current_folder != NULL);
7125 same_path = g_file_equal (parent_file, impl->current_folder);
7128 if (same_path && impl->load_state == LOAD_FINISHED)
7133 files.data = (gpointer) file;
7136 result = show_and_select_files (impl, parent_file, &files, error);
7137 g_object_unref (parent_file);
7141 pending_select_files_add (impl, file);
7147 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7148 g_object_unref (parent_file);
7152 g_object_unref (parent_file);
7157 unselect_func (GtkFileSystemModel *model,
7162 GtkFileChooserDefault *impl = user_data;
7163 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7164 GtkTreePath *sorted_path;
7166 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7168 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7170 gtk_tree_path_free (sorted_path);
7174 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7177 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7179 if (!impl->browse_files_model)
7182 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7183 unselect_func, impl);
7187 maybe_select (GtkTreeModel *model,
7192 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7193 GtkTreeSelection *selection;
7197 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7199 info = get_list_file_info (impl, iter);
7200 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7202 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7203 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7204 gtk_tree_selection_select_iter (selection, iter);
7206 gtk_tree_selection_unselect_iter (selection, iter);
7212 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7214 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7216 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7217 impl->operation_mode == OPERATION_MODE_RECENT)
7219 GtkTreeSelection *selection;
7221 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7222 gtk_tree_selection_select_all (selection);
7226 if (impl->select_multiple)
7227 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7228 maybe_select, impl);
7232 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7234 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7235 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7237 gtk_tree_selection_unselect_all (selection);
7238 pending_select_files_free (impl);
7241 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7243 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7245 * is_empty_ret - whether the file entry is totally empty
7247 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7248 * the path will be "$cwd/foobar")
7251 check_save_entry (GtkFileChooserDefault *impl,
7253 gboolean *is_well_formed_ret,
7254 gboolean *is_empty_ret,
7255 gboolean *is_file_part_empty_ret,
7256 gboolean *is_folder)
7258 GtkFileChooserEntry *chooser_entry;
7259 GFile *current_folder;
7260 const char *file_part;
7264 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7265 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7266 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7267 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7268 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7270 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7272 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7275 *is_well_formed_ret = TRUE;
7276 *is_empty_ret = TRUE;
7277 *is_file_part_empty_ret = TRUE;
7283 *is_empty_ret = FALSE;
7285 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7286 if (!current_folder)
7289 *is_well_formed_ret = FALSE;
7290 *is_file_part_empty_ret = FALSE;
7296 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7298 if (!file_part || file_part[0] == '\0')
7300 *file_ret = g_object_ref (current_folder);
7301 *is_well_formed_ret = TRUE;
7302 *is_file_part_empty_ret = TRUE;
7308 *is_file_part_empty_ret = FALSE;
7311 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7315 error_building_filename_dialog (impl, error);
7317 *is_well_formed_ret = FALSE;
7324 *is_well_formed_ret = TRUE;
7325 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7328 struct get_files_closure {
7329 GtkFileChooserDefault *impl;
7331 GFile *file_from_entry;
7335 get_files_foreach (GtkTreeModel *model,
7340 struct get_files_closure *info;
7342 GtkFileSystemModel *fs_model;
7343 GtkTreeIter sel_iter;
7346 fs_model = info->impl->browse_files_model;
7347 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7349 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7351 return; /* We are on the editable row */
7353 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7354 info->result = g_slist_prepend (info->result, g_object_ref (file));
7358 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7360 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7361 struct get_files_closure info;
7362 GtkWindow *toplevel;
7363 GtkWidget *current_focus;
7364 gboolean file_list_seen;
7366 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7367 return search_get_selected_files (impl);
7369 if (impl->operation_mode == OPERATION_MODE_RECENT)
7370 return recent_get_selected_files (impl);
7374 info.file_from_entry = NULL;
7376 toplevel = get_toplevel (GTK_WIDGET (impl));
7378 current_focus = gtk_window_get_focus (toplevel);
7380 current_focus = NULL;
7382 file_list_seen = FALSE;
7383 if (current_focus == impl->browse_files_tree_view)
7385 GtkTreeSelection *selection;
7389 file_list_seen = TRUE;
7390 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7391 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7393 /* If there is no selection in the file list, we probably have this situation:
7395 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7396 * 2. He then double-clicked on a folder ("bar") in the file list
7398 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7399 * filename entry to see if that is the case.
7401 if (info.result == NULL && impl->location_entry)
7404 else if (impl->location_entry && current_focus == impl->location_entry)
7406 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7410 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7415 if (!is_well_formed)
7418 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7420 g_object_unref (info.file_from_entry);
7424 if (info.file_from_entry)
7425 info.result = g_slist_prepend (info.result, info.file_from_entry);
7426 else if (!file_list_seen)
7431 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7433 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7437 /* The focus is on a dialog's action area button or something else */
7438 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7439 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7447 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7448 * fall back to the current directory */
7449 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7450 info.result == NULL)
7452 GFile *current_folder;
7454 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7457 info.result = g_slist_prepend (info.result, current_folder);
7460 return g_slist_reverse (info.result);
7464 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7466 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7468 if (impl->preview_file)
7469 return g_object_ref (impl->preview_file);
7474 static GtkFileSystem *
7475 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7477 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7479 return impl->file_system;
7482 /* Shows or hides the filter widgets */
7484 show_filters (GtkFileChooserDefault *impl,
7488 gtk_widget_show (impl->filter_combo_hbox);
7490 gtk_widget_hide (impl->filter_combo_hbox);
7494 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7495 GtkFileFilter *filter)
7497 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7500 if (g_slist_find (impl->filters, filter))
7502 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7506 g_object_ref_sink (filter);
7507 impl->filters = g_slist_append (impl->filters, filter);
7509 name = gtk_file_filter_get_name (filter);
7511 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7513 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7515 if (!g_slist_find (impl->filters, impl->current_filter))
7516 set_current_filter (impl, filter);
7518 show_filters (impl, TRUE);
7522 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7523 GtkFileFilter *filter)
7525 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7526 GtkTreeModel *model;
7530 filter_index = g_slist_index (impl->filters, filter);
7532 if (filter_index < 0)
7534 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7538 impl->filters = g_slist_remove (impl->filters, filter);
7540 if (filter == impl->current_filter)
7543 set_current_filter (impl, impl->filters->data);
7545 set_current_filter (impl, NULL);
7548 /* Remove row from the combo box */
7549 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7550 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7551 g_assert_not_reached ();
7553 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7555 g_object_unref (filter);
7558 show_filters (impl, FALSE);
7562 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7564 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7566 return g_slist_copy (impl->filters);
7569 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7571 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7574 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7577 struct AddShortcutData
7579 GtkFileChooserDefault *impl;
7584 add_shortcut_get_info_cb (GCancellable *cancellable,
7586 const GError *error,
7590 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7591 struct AddShortcutData *data = user_data;
7593 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7596 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7598 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7601 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7603 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7606 g_object_unref (data->impl);
7607 g_object_unref (data->file);
7610 g_object_unref (cancellable);
7614 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7618 GCancellable *cancellable;
7619 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7620 struct AddShortcutData *data;
7624 /* Avoid adding duplicates */
7625 pos = shortcut_find_position (impl, file);
7626 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7630 uri = g_file_get_uri (file);
7631 /* translators, "Shortcut" means "Bookmark" here */
7633 GTK_FILE_CHOOSER_ERROR,
7634 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7635 _("Shortcut %s already exists"),
7642 for (l = impl->loading_shortcuts; l; l = l->next)
7644 GCancellable *c = l->data;
7647 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7648 if (f && g_file_equal (file, f))
7652 uri = g_file_get_uri (file);
7654 GTK_FILE_CHOOSER_ERROR,
7655 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7656 _("Shortcut %s already exists"),
7664 data = g_new0 (struct AddShortcutData, 1);
7665 data->impl = g_object_ref (impl);
7666 data->file = g_object_ref (file);
7668 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7670 add_shortcut_get_info_cb, data);
7675 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7676 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7682 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7686 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7693 for (l = impl->loading_shortcuts; l; l = l->next)
7695 GCancellable *c = l->data;
7698 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7699 if (f && g_file_equal (file, f))
7701 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7702 g_cancellable_cancel (c);
7707 if (impl->num_shortcuts == 0)
7710 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7711 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7712 g_assert_not_reached ();
7714 for (i = 0; i < impl->num_shortcuts; i++)
7717 ShortcutType shortcut_type;
7720 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7721 SHORTCUTS_COL_DATA, &col_data,
7722 SHORTCUTS_COL_TYPE, &shortcut_type,
7724 g_assert (col_data != NULL);
7725 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7727 shortcut = col_data;
7728 if (g_file_equal (shortcut, file))
7730 shortcuts_remove_rows (impl, pos + i, 1);
7731 impl->num_shortcuts--;
7735 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7736 g_assert_not_reached ();
7741 uri = g_file_get_uri (file);
7742 /* translators, "Shortcut" means "Bookmark" here */
7744 GTK_FILE_CHOOSER_ERROR,
7745 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7746 _("Shortcut %s does not exist"),
7754 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7756 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7762 if (impl->num_shortcuts == 0)
7765 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7766 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7767 g_assert_not_reached ();
7771 for (i = 0; i < impl->num_shortcuts; i++)
7774 ShortcutType shortcut_type;
7777 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7778 SHORTCUTS_COL_DATA, &col_data,
7779 SHORTCUTS_COL_TYPE, &shortcut_type,
7781 g_assert (col_data != NULL);
7782 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7784 shortcut = col_data;
7785 list = g_slist_prepend (list, g_object_ref (shortcut));
7787 if (i != impl->num_shortcuts - 1)
7789 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7790 g_assert_not_reached ();
7794 return g_slist_reverse (list);
7797 /* Guesses a size based upon font sizes */
7799 find_good_size_from_style (GtkWidget *widget,
7803 GtkFileChooserDefault *impl;
7808 g_assert (widget->style != NULL);
7809 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7811 screen = gtk_widget_get_screen (widget);
7814 resolution = gdk_screen_get_resolution (screen);
7815 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7819 resolution = 96.0; /* wheeee */
7821 font_size = pango_font_description_get_size (widget->style->font_desc);
7822 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7824 *width = font_size * NUM_CHARS;
7825 *height = font_size * NUM_LINES;
7829 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7830 gint *default_width,
7831 gint *default_height)
7833 GtkFileChooserDefault *impl;
7836 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7838 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7839 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7840 || impl->expand_folders)
7842 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7844 if (impl->preview_widget_active &&
7845 impl->preview_widget &&
7846 GTK_WIDGET_VISIBLE (impl->preview_widget))
7848 gtk_widget_size_request (impl->preview_box, &req);
7849 *default_width += PREVIEW_HBOX_SPACING + req.width;
7852 if (impl->extra_widget &&
7853 GTK_WIDGET_VISIBLE (impl->extra_widget))
7855 gtk_widget_size_request (impl->extra_align, &req);
7856 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7861 gtk_widget_size_request (GTK_WIDGET (impl), &req);
7862 *default_width = req.width;
7863 *default_height = req.height;
7867 struct switch_folder_closure {
7868 GtkFileChooserDefault *impl;
7873 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7875 switch_folder_foreach_cb (GtkTreeModel *model,
7880 struct switch_folder_closure *closure;
7881 GtkTreeIter child_iter;
7885 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7887 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7888 closure->num_selected++;
7891 /* Changes to the selected folder in the list view */
7893 switch_to_selected_folder (GtkFileChooserDefault *impl)
7895 GtkTreeSelection *selection;
7896 struct switch_folder_closure closure;
7898 /* We do this with foreach() rather than get_selected() as we may be in
7899 * multiple selection mode
7902 closure.impl = impl;
7903 closure.file = NULL;
7904 closure.num_selected = 0;
7906 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7907 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7909 g_assert (closure.file && closure.num_selected == 1);
7911 change_folder_and_display_error (impl, closure.file, FALSE);
7914 /* Gets the GFileInfo for the selected row in the file list; assumes single
7918 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7919 gboolean *had_selection)
7921 GtkTreeSelection *selection;
7922 GtkTreeIter iter, child_iter;
7925 g_assert (!impl->select_multiple);
7926 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7927 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7929 *had_selection = FALSE;
7933 *had_selection = TRUE;
7935 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7939 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7943 /* Gets the display name of the selected file in the file list; assumes single
7944 * selection mode and that something is selected.
7946 static const gchar *
7947 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7950 gboolean had_selection;
7952 info = get_selected_file_info_from_file_list (impl, &had_selection);
7953 g_assert (had_selection);
7954 g_assert (info != NULL);
7956 return g_file_info_get_display_name (info);
7960 add_custom_button_to_dialog (GtkDialog *dialog,
7961 const gchar *mnemonic_label,
7962 const gchar *stock_id,
7967 button = gtk_button_new_with_mnemonic (mnemonic_label);
7968 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7969 gtk_button_set_image (GTK_BUTTON (button),
7970 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7971 gtk_widget_show (button);
7973 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7976 /* Presents an overwrite confirmation dialog; returns whether we should accept
7980 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7981 const gchar *file_part,
7982 const gchar *folder_display_name)
7984 GtkWindow *toplevel;
7988 toplevel = get_toplevel (GTK_WIDGET (impl));
7990 dialog = gtk_message_dialog_new (toplevel,
7991 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7992 GTK_MESSAGE_QUESTION,
7994 _("A file named \"%s\" already exists. Do you want to replace it?"),
7996 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7997 _("The file already exists in \"%s\". Replacing it will "
7998 "overwrite its contents."),
7999 folder_display_name);
8001 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8002 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8003 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8004 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8005 GTK_RESPONSE_ACCEPT,
8006 GTK_RESPONSE_CANCEL,
8008 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8010 if (toplevel->group)
8011 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8013 response = gtk_dialog_run (GTK_DIALOG (dialog));
8015 gtk_widget_destroy (dialog);
8017 return (response == GTK_RESPONSE_ACCEPT);
8020 struct GetDisplayNameData
8022 GtkFileChooserDefault *impl;
8027 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8029 const GError *error,
8032 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8033 gboolean should_respond = FALSE;
8034 struct GetDisplayNameData *data = user_data;
8036 if (cancellable != data->impl->should_respond_get_info_cancellable)
8039 data->impl->should_respond_get_info_cancellable = NULL;
8045 /* Huh? Did the folder disappear? Let the caller deal with it */
8046 should_respond = TRUE;
8048 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8050 set_busy_cursor (data->impl, FALSE);
8052 g_signal_emit_by_name (data->impl, "response-requested");
8055 g_object_unref (data->impl);
8056 g_free (data->file_part);
8059 g_object_unref (cancellable);
8062 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8063 * should respond. Can get the file part from the file list or the save entry.
8066 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8067 const gchar *file_part,
8070 GtkFileChooserConfirmation conf;
8072 if (!impl->do_overwrite_confirmation)
8075 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8077 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8081 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8083 struct GetDisplayNameData *data;
8085 g_assert (file_part != NULL);
8087 data = g_new0 (struct GetDisplayNameData, 1);
8088 data->impl = g_object_ref (impl);
8089 data->file_part = g_strdup (file_part);
8091 if (impl->should_respond_get_info_cancellable)
8092 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8094 impl->should_respond_get_info_cancellable =
8095 _gtk_file_system_get_info (impl->file_system, parent_file,
8096 "standard::display-name",
8097 confirmation_confirm_get_info_cb,
8099 set_busy_cursor (data->impl, TRUE);
8103 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8106 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8110 g_assert_not_reached ();
8115 struct FileExistsData
8117 GtkFileChooserDefault *impl;
8118 gboolean file_exists_and_is_not_folder;
8124 save_entry_get_info_cb (GCancellable *cancellable,
8126 const GError *error,
8129 gboolean parent_is_folder;
8130 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8131 struct FileExistsData *data = user_data;
8133 if (cancellable != data->impl->should_respond_get_info_cancellable)
8136 data->impl->should_respond_get_info_cancellable = NULL;
8138 set_busy_cursor (data->impl, FALSE);
8144 parent_is_folder = FALSE;
8146 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8148 if (parent_is_folder)
8150 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8152 if (data->file_exists_and_is_not_folder)
8155 const char *file_part;
8157 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8158 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8161 g_signal_emit_by_name (data->impl, "response-requested");
8164 g_signal_emit_by_name (data->impl, "response-requested");
8166 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8168 GError *error = NULL;
8170 set_busy_cursor (data->impl, TRUE);
8171 g_file_make_directory (data->file, NULL, &error);
8172 set_busy_cursor (data->impl, FALSE);
8175 g_signal_emit_by_name (data->impl, "response-requested");
8177 error_creating_folder_dialog (data->impl, data->file, error);
8182 /* This will display an error, which is what we want */
8183 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8187 g_object_unref (data->impl);
8188 g_object_unref (data->file);
8189 g_object_unref (data->parent_file);
8192 g_object_unref (cancellable);
8196 file_exists_get_info_cb (GCancellable *cancellable,
8198 const GError *error,
8201 gboolean data_ownership_taken = FALSE;
8202 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8203 gboolean file_exists_and_is_not_folder;
8204 struct FileExistsData *data = user_data;
8206 if (cancellable != data->impl->file_exists_get_info_cancellable)
8209 data->impl->file_exists_get_info_cancellable = NULL;
8211 set_busy_cursor (data->impl, FALSE);
8216 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8218 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8219 /* user typed a filename; we are done */
8220 g_signal_emit_by_name (data->impl, "response-requested");
8221 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8222 && file_exists_and_is_not_folder)
8224 /* Oops, the user typed the name of an existing path which is not
8227 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8228 g_error_copy (error));
8232 /* check that everything up to the last component exists */
8234 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8235 data_ownership_taken = TRUE;
8237 if (data->impl->should_respond_get_info_cancellable)
8238 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8240 data->impl->should_respond_get_info_cancellable =
8241 _gtk_file_system_get_info (data->impl->file_system,
8244 save_entry_get_info_cb,
8246 set_busy_cursor (data->impl, TRUE);
8250 if (!data_ownership_taken)
8252 g_object_unref (data->impl);
8253 g_object_unref (data->file);
8254 g_object_unref (data->parent_file);
8258 g_object_unref (cancellable);
8262 paste_text_received (GtkClipboard *clipboard,
8264 GtkFileChooserDefault *impl)
8271 file = g_file_new_for_uri (text);
8273 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8274 location_popup_handler (impl, text);
8276 g_object_unref (file);
8279 /* Handler for the "location-popup-on-paste" keybinding signal */
8281 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8283 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8284 GDK_SELECTION_CLIPBOARD);
8285 gtk_clipboard_request_text (clipboard,
8286 (GtkClipboardTextReceivedFunc) paste_text_received,
8291 /* Implementation for GtkFileChooserEmbed::should_respond() */
8293 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8295 GtkFileChooserDefault *impl;
8296 GtkWidget *toplevel;
8297 GtkWidget *current_focus;
8299 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8301 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8302 g_assert (GTK_IS_WINDOW (toplevel));
8304 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8306 if (current_focus == impl->browse_files_tree_view)
8308 /* The following array encodes what we do based on the impl->action and the
8309 * number of files selected.
8312 NOOP, /* Do nothing (don't respond) */
8313 RESPOND, /* Respond immediately */
8314 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8315 ALL_FILES, /* Respond only if everything selected is a file */
8316 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8317 SAVE_ENTRY, /* Go to the code for handling the save entry */
8318 NOT_REACHED /* Sanity check */
8320 static const ActionToTake what_to_do[4][3] = {
8321 /* 0 selected 1 selected many selected */
8322 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8323 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8324 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8325 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8329 gboolean all_files, all_folders;
8331 ActionToTake action;
8335 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8337 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8338 return search_should_respond (impl);
8340 if (impl->operation_mode == OPERATION_MODE_RECENT)
8341 return recent_should_respond (impl);
8343 selection_check (impl, &num_selected, &all_files, &all_folders);
8345 if (num_selected > 2)
8350 action = what_to_do [impl->action] [k];
8360 case RESPOND_OR_SWITCH:
8361 g_assert (num_selected == 1);
8365 switch_to_selected_folder (impl);
8368 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8369 return should_respond_after_confirm_overwrite (impl,
8370 get_display_name_from_file_list (impl),
8371 impl->current_folder);
8385 g_assert_not_reached ();
8388 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8391 gboolean is_well_formed, is_empty, is_file_part_empty;
8394 GtkFileChooserEntry *entry;
8399 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8400 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8401 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8402 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8403 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8405 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8406 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8408 if (is_empty || !is_well_formed)
8411 g_assert (file != NULL);
8416 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8417 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8419 change_folder_and_display_error (impl, file, TRUE);
8422 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8423 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8425 /* The folder already exists, so we do not need to create it.
8426 * Just respond to terminate the dialog.
8432 g_assert_not_reached ();
8438 struct FileExistsData *data;
8440 /* We need to check whether file exists and is not a folder */
8442 data = g_new0 (struct FileExistsData, 1);
8443 data->impl = g_object_ref (impl);
8444 data->file = g_object_ref (file);
8445 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8447 if (impl->file_exists_get_info_cancellable)
8448 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8450 impl->file_exists_get_info_cancellable =
8451 _gtk_file_system_get_info (impl->file_system, file,
8453 file_exists_get_info_cb,
8456 set_busy_cursor (impl, TRUE);
8460 g_error_free (error);
8463 g_object_unref (file);
8466 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8468 /* The focus is on a dialog's action area button, *and* the widget that
8469 * was focused immediately before it is the file list.
8473 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8475 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8478 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8480 /* The focus is on a dialog's action area button, *and* the widget that
8481 * was focused immediately before it is the location entry.
8486 /* The focus is on a dialog's action area button or something else */
8487 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8488 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8493 g_assert_not_reached ();
8497 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8499 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8501 GtkFileChooserDefault *impl;
8504 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8506 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8507 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8509 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8510 widget = impl->browse_files_tree_view;
8512 widget = impl->location_entry;
8514 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8515 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8516 widget = impl->location_entry;
8519 g_assert_not_reached ();
8523 g_assert (widget != NULL);
8524 gtk_widget_grab_focus (widget);
8527 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8529 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8539 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8540 *list = g_slist_prepend (*list, g_object_ref (file));
8543 /* Constructs a list of the selected paths in search mode */
8545 search_get_selected_files (GtkFileChooserDefault *impl)
8548 GtkTreeSelection *selection;
8552 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8553 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8554 result = g_slist_reverse (result);
8559 /* Called from ::should_respond(). We return whether there are selected files
8560 * in the search list.
8563 search_should_respond (GtkFileChooserDefault *impl)
8565 GtkTreeSelection *selection;
8567 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8569 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8570 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8573 struct SearchHitInsertRequest
8575 GtkFileChooserDefault *impl;
8577 GtkTreeRowReference *row_ref;
8581 search_hit_get_info_cb (GCancellable *cancellable,
8583 const GError *error,
8586 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8587 GdkPixbuf *pixbuf = NULL;
8590 GCancellable *model_cancellable;
8591 gboolean is_folder = FALSE;
8594 struct SearchHitInsertRequest *request = data;
8596 if (!request->impl->search_model)
8599 path = gtk_tree_row_reference_get_path (request->row_ref);
8603 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8605 gtk_tree_path_free (path);
8607 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8608 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8610 if (cancellable != model_cancellable)
8613 /* set the cancellable to NULL in the model */
8614 gtk_list_store_set (request->impl->search_model, &iter,
8615 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8623 search_clear_model_row (request->impl->search_model, &iter);
8624 gtk_list_store_remove (request->impl->search_model, &iter);
8628 display_name = g_strdup (g_file_info_get_display_name (info));
8629 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
8630 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8631 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8632 request->impl->icon_size);
8634 gtk_list_store_set (request->impl->search_model, &iter,
8635 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8636 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8637 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8638 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8642 g_object_unref (pixbuf);
8645 g_object_unref (request->impl);
8646 g_object_unref (request->file);
8647 gtk_tree_row_reference_free (request->row_ref);
8650 g_object_unref (cancellable);
8653 /* Adds one hit from the search engine to the search_model */
8655 search_add_hit (GtkFileChooserDefault *impl,
8661 char *collation_key;
8662 struct stat statbuf;
8663 struct stat *statbuf_copy;
8666 GCancellable *cancellable;
8667 struct SearchHitInsertRequest *request;
8669 file = g_file_new_for_uri (uri);
8671 if (!g_file_is_native (file))
8673 g_object_unref (file);
8677 filename = g_file_get_path (file);
8679 if (stat (filename, &statbuf) != 0)
8681 g_object_unref (file);
8686 statbuf_copy = g_new (struct stat, 1);
8687 *statbuf_copy = statbuf;
8689 tmp = g_file_get_parse_name (file);
8690 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8693 request = g_new0 (struct SearchHitInsertRequest, 1);
8694 request->impl = g_object_ref (impl);
8695 request->file = g_object_ref (file);
8697 gtk_list_store_append (impl->search_model, &iter);
8698 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8700 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8701 gtk_tree_path_free (p);
8703 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8704 "standard::type,standard::icon,"
8705 "standard::content-type,standard::display-name",
8706 search_hit_get_info_cb,
8709 gtk_list_store_set (impl->search_model, &iter,
8710 SEARCH_MODEL_COL_FILE, file,
8711 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8712 SEARCH_MODEL_COL_STAT, statbuf_copy,
8713 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8719 /* Callback used from GtkSearchEngine when we get new hits */
8721 search_engine_hits_added_cb (GtkSearchEngine *engine,
8725 GtkFileChooserDefault *impl;
8728 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8730 for (l = hits; l; l = l->next)
8731 search_add_hit (impl, (gchar*)l->data);
8734 /* Callback used from GtkSearchEngine when the query is done running */
8736 search_engine_finished_cb (GtkSearchEngine *engine,
8739 GtkFileChooserDefault *impl;
8741 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8744 /* EB: setting the model here will avoid loads of row events,
8745 * but it'll make the search look like blocked.
8747 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8748 GTK_TREE_MODEL (impl->search_model_filter));
8751 /* FMQ: if search was empty, say that we got no hits */
8752 set_busy_cursor (impl, FALSE);
8755 /* Displays a generic error when we cannot create a GtkSearchEngine.
8756 * It would be better if _gtk_search_engine_new() gave us a GError
8757 * with a better message, but it doesn't do that right now.
8760 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8762 error_message (impl,
8763 _("Could not start the search process"),
8764 _("The program was not able to create a connection to the indexer "
8765 "daemon. Please make sure it is running."));
8769 search_engine_error_cb (GtkSearchEngine *engine,
8770 const gchar *message,
8773 GtkFileChooserDefault *impl;
8775 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8777 search_stop_searching (impl, TRUE);
8778 error_message (impl, _("Could not send the search request"), message);
8780 set_busy_cursor (impl, FALSE);
8784 search_clear_model_row (GtkTreeModel *model,
8788 gchar *display_name;
8789 gchar *collation_key;
8790 struct stat *statbuf;
8791 GCancellable *cancellable;
8794 gtk_tree_model_get (model, iter,
8795 SEARCH_MODEL_COL_FILE, &file,
8796 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8797 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
8798 SEARCH_MODEL_COL_STAT, &statbuf,
8799 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8800 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8804 g_object_unref (file);
8806 g_free (display_name);
8807 g_free (collation_key);
8812 g_cancellable_cancel (cancellable);
8815 /* Frees the data in the search_model */
8817 search_clear_model (GtkFileChooserDefault *impl,
8818 gboolean remove_from_treeview)
8820 GtkTreeModel *model;
8823 if (!impl->search_model)
8826 model = GTK_TREE_MODEL (impl->search_model);
8828 if (gtk_tree_model_get_iter_first (model, &iter))
8831 search_clear_model_row (model, &iter);
8833 while (gtk_tree_model_iter_next (model, &iter));
8835 g_object_unref (impl->search_model);
8836 impl->search_model = NULL;
8838 g_object_unref (impl->search_model_filter);
8839 impl->search_model_filter = NULL;
8841 g_object_unref (impl->search_model_sort);
8842 impl->search_model_sort = NULL;
8844 if (remove_from_treeview)
8845 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8848 /* Stops any ongoing searches; does not touch the search_model */
8850 search_stop_searching (GtkFileChooserDefault *impl,
8851 gboolean remove_query)
8853 if (remove_query && impl->search_query)
8855 g_object_unref (impl->search_query);
8856 impl->search_query = NULL;
8859 if (impl->search_engine)
8861 _gtk_search_engine_stop (impl->search_engine);
8863 g_object_unref (impl->search_engine);
8864 impl->search_engine = NULL;
8868 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8870 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8872 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8874 search_stop_searching (impl, FALSE);
8875 search_clear_model (impl, TRUE);
8877 gtk_widget_destroy (impl->search_hbox);
8878 impl->search_hbox = NULL;
8879 impl->search_entry = NULL;
8881 gtk_widget_show (impl->browse_path_bar);
8882 gtk_widget_show (impl->browse_new_folder_button);
8884 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8885 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8887 gtk_widget_show (impl->location_button);
8889 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8890 gtk_widget_show (impl->location_entry_box);
8893 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
8895 impl->operation_mode = OPERATION_MODE_BROWSE;
8897 file_list_set_sort_column_ids (impl);
8900 /* Sort callback from the path column */
8902 search_column_path_sort_func (GtkTreeModel *model,
8907 GtkFileChooserDefault *impl = user_data;
8908 GtkTreeIter child_a, child_b;
8909 const char *collation_key_a, *collation_key_b;
8910 gboolean is_folder_a, is_folder_b;
8912 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8913 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8915 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8916 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8917 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8919 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8920 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8921 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8924 if (!collation_key_a)
8927 if (!collation_key_b)
8930 /* always show folders first */
8931 if (is_folder_a != is_folder_b)
8932 return is_folder_a ? 1 : -1;
8934 return strcmp (collation_key_a, collation_key_b);
8937 /* Sort callback from the modification time column */
8939 search_column_mtime_sort_func (GtkTreeModel *model,
8944 GtkFileChooserDefault *impl = user_data;
8945 GtkTreeIter child_a, child_b;
8946 const struct stat *statbuf_a, *statbuf_b;
8947 gboolean is_folder_a, is_folder_b;
8949 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8950 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8952 /* Note that although we store a whole struct stat in the model, we only
8953 * compare the mtime here. If we add another column relative to a struct stat
8954 * (e.g. a file size column), we'll want another sort callback similar to this
8957 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8958 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8959 SEARCH_MODEL_COL_STAT, &statbuf_a,
8961 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8962 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8963 SEARCH_MODEL_COL_STAT, &statbuf_b,
8972 if (is_folder_a != is_folder_b)
8973 return is_folder_a ? 1 : -1;
8975 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8977 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8984 search_get_is_filtered (GtkFileChooserDefault *impl,
8986 const gchar *display_name,
8987 const gchar *mime_type)
8989 GtkFileFilterInfo filter_info;
8990 GtkFileFilterFlags needed;
8993 if (!impl->current_filter)
8996 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8997 needed = gtk_file_filter_get_needed (impl->current_filter);
8999 filter_info.display_name = display_name;
9000 filter_info.mime_type = mime_type;
9002 if (needed & GTK_FILE_FILTER_FILENAME)
9004 filter_info.filename = g_file_get_path (file);
9005 if (filter_info.filename)
9006 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9009 filter_info.filename = NULL;
9011 if (needed & GTK_FILE_FILTER_URI)
9013 filter_info.uri = g_file_get_uri (file);
9014 if (filter_info.uri)
9015 filter_info.contains |= GTK_FILE_FILTER_URI;
9018 filter_info.uri = NULL;
9020 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9022 if (filter_info.filename)
9023 g_free ((gchar *) filter_info.filename);
9024 if (filter_info.uri)
9025 g_free ((gchar *) filter_info.uri);
9031 /* Visibility function for the recent filter model */
9033 search_model_visible_func (GtkTreeModel *model,
9037 GtkFileChooserDefault *impl = user_data;
9039 gchar *display_name, *mime_type;
9042 if (!impl->current_filter)
9045 gtk_tree_model_get (model, iter,
9046 SEARCH_MODEL_COL_FILE, &file,
9047 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9048 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9049 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9058 return !search_get_is_filtered (impl, file, display_name, mime_type);
9061 /* Creates the search_model and puts it in the tree view */
9063 search_setup_model (GtkFileChooserDefault *impl)
9065 g_assert (impl->search_model == NULL);
9066 g_assert (impl->search_model_filter == NULL);
9067 g_assert (impl->search_model_sort == NULL);
9069 /* We store these columns in the search model:
9071 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9072 * as a pointer not as a G_TYPE_FILE
9073 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9074 * as a pointer not as a G_TYPE_STRING
9075 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9076 * as a pointer not as a G_TYPE_STRING
9077 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9078 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9079 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9080 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9081 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9083 * Keep this in sync with the enumeration defined near the beginning
9086 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9096 impl->search_model_filter =
9097 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9098 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9099 search_model_visible_func,
9102 impl->search_model_sort =
9103 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9104 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9105 SEARCH_MODEL_COL_FILE,
9106 search_column_path_sort_func,
9108 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9109 SEARCH_MODEL_COL_STAT,
9110 search_column_mtime_sort_func,
9112 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9113 SEARCH_MODEL_COL_STAT,
9114 GTK_SORT_DESCENDING);
9116 /* EB: setting the model here will make the hits list update feel
9117 * more "alive" than setting the model at the end of the search
9120 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9121 GTK_TREE_MODEL (impl->search_model_sort));
9125 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9126 GtkTreeIter *child_iter,
9131 if (!impl->search_model)
9134 if (!impl->search_model_filter || !impl->search_model_sort)
9137 /* pass 1: get the iterator in the filter model */
9138 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9141 /* pass 2: get the iterator in the real model */
9142 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9143 child_iter, &middle);
9146 /* Creates a new query with the specified text and launches it */
9148 search_start_query (GtkFileChooserDefault *impl,
9149 const gchar *query_text)
9151 search_stop_searching (impl, FALSE);
9152 search_clear_model (impl, TRUE);
9153 search_setup_model (impl);
9154 set_busy_cursor (impl, TRUE);
9156 if (impl->search_engine == NULL)
9157 impl->search_engine = _gtk_search_engine_new ();
9159 if (!impl->search_engine)
9161 set_busy_cursor (impl, FALSE);
9162 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9166 if (!impl->search_query)
9168 impl->search_query = _gtk_query_new ();
9169 _gtk_query_set_text (impl->search_query, query_text);
9172 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9174 g_signal_connect (impl->search_engine, "hits-added",
9175 G_CALLBACK (search_engine_hits_added_cb), impl);
9176 g_signal_connect (impl->search_engine, "finished",
9177 G_CALLBACK (search_engine_finished_cb), impl);
9178 g_signal_connect (impl->search_engine, "error",
9179 G_CALLBACK (search_engine_error_cb), impl);
9181 _gtk_search_engine_start (impl->search_engine);
9184 /* Callback used when the user presses Enter while typing on the search
9185 * entry; starts the query
9188 search_entry_activate_cb (GtkEntry *entry,
9191 GtkFileChooserDefault *impl;
9194 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9196 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9197 if (strlen (text) == 0)
9200 /* reset any existing query object */
9201 if (impl->search_query)
9203 g_object_unref (impl->search_query);
9204 impl->search_query = NULL;
9207 search_start_query (impl, text);
9210 /* Hides the path bar and creates the search entry */
9212 search_setup_widgets (GtkFileChooserDefault *impl)
9216 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9220 label = gtk_label_new_with_mnemonic (_("_Search:"));
9221 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9225 impl->search_entry = gtk_entry_new ();
9226 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9227 g_signal_connect (impl->search_entry, "activate",
9228 G_CALLBACK (search_entry_activate_cb),
9230 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9232 /* if there already is a query, restart it */
9233 if (impl->search_query)
9235 gchar *query = _gtk_query_get_text (impl->search_query);
9239 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9240 search_start_query (impl, query);
9246 g_object_unref (impl->search_query);
9247 impl->search_query = NULL;
9251 gtk_widget_hide (impl->browse_path_bar);
9252 gtk_widget_hide (impl->browse_new_folder_button);
9254 /* hide the file size column if it's visible */
9255 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
9257 /* Box for search widgets */
9258 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9259 gtk_widget_show_all (impl->search_hbox);
9261 /* Hide the location widgets temporarily */
9263 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9264 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9266 gtk_widget_hide (impl->location_button);
9267 gtk_widget_hide (impl->location_entry_box);
9270 gtk_widget_grab_focus (impl->search_entry);
9272 /* FMQ: hide the filter combo? */
9275 /* Main entry point to the searching functions; this gets called when the user
9276 * activates the Search shortcut.
9279 search_activate (GtkFileChooserDefault *impl)
9281 OperationMode previous_mode;
9283 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9285 gtk_widget_grab_focus (impl->search_entry);
9289 previous_mode = impl->operation_mode;
9290 impl->operation_mode = OPERATION_MODE_SEARCH;
9292 switch (previous_mode)
9294 case OPERATION_MODE_RECENT:
9295 recent_stop_loading (impl);
9296 recent_clear_model (impl, TRUE);
9299 case OPERATION_MODE_BROWSE:
9300 stop_loading_and_clear_list_model (impl);
9303 case OPERATION_MODE_SEARCH:
9304 g_assert_not_reached ();
9308 g_assert (impl->search_hbox == NULL);
9309 g_assert (impl->search_entry == NULL);
9310 g_assert (impl->search_model == NULL);
9311 g_assert (impl->search_model_filter == NULL);
9313 search_setup_widgets (impl);
9314 file_list_set_sort_column_ids (impl);
9318 * Recent files support
9321 /* Frees the data in the recent_model */
9323 recent_clear_model (GtkFileChooserDefault *impl,
9324 gboolean remove_from_treeview)
9326 GtkTreeModel *model;
9329 if (!impl->recent_model)
9332 model = GTK_TREE_MODEL (impl->recent_model);
9334 if (remove_from_treeview)
9335 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9337 if (gtk_tree_model_get_iter_first (model, &iter))
9342 GCancellable *cancellable;
9343 GtkRecentInfo *recent_info;
9344 gchar *display_name;
9346 gtk_tree_model_get (model, &iter,
9347 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9348 RECENT_MODEL_COL_FILE, &file,
9349 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9350 RECENT_MODEL_COL_INFO, &recent_info,
9354 g_cancellable_cancel (cancellable);
9356 g_object_unref (file);
9357 gtk_recent_info_unref (recent_info);
9358 g_free (display_name);
9360 while (gtk_tree_model_iter_next (model, &iter));
9363 g_object_unref (impl->recent_model);
9364 impl->recent_model = NULL;
9366 g_object_unref (impl->recent_model_filter);
9367 impl->recent_model_filter = NULL;
9369 g_object_unref (impl->recent_model_sort);
9370 impl->recent_model_sort = NULL;
9373 /* Stops any ongoing loading of the recent files list; does
9374 * not touch the recent_model
9377 recent_stop_loading (GtkFileChooserDefault *impl)
9379 if (impl->load_recent_id)
9381 g_source_remove (impl->load_recent_id);
9382 impl->load_recent_id = 0;
9386 /* Stops any pending load, clears the file list, and switches
9387 * back to OPERATION_MODE_BROWSE
9390 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9392 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9394 recent_stop_loading (impl);
9395 recent_clear_model (impl, TRUE);
9397 gtk_widget_show (impl->browse_path_bar);
9398 gtk_widget_show (impl->browse_new_folder_button);
9400 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9401 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9403 gtk_widget_show (impl->location_button);
9405 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9406 gtk_widget_show (impl->location_entry_box);
9409 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9411 impl->operation_mode = OPERATION_MODE_BROWSE;
9413 file_list_set_sort_column_ids (impl);
9416 /* Sort callback from the modification time column */
9418 recent_column_mtime_sort_func (GtkTreeModel *model,
9423 GtkFileChooserDefault *impl = user_data;
9424 GtkTreeIter child_a, child_b;
9425 GtkRecentInfo *info_a, *info_b;
9426 gboolean is_folder_a, is_folder_b;
9428 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9429 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9431 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9432 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9433 RECENT_MODEL_COL_INFO, &info_a,
9435 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9436 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9437 RECENT_MODEL_COL_INFO, &info_b,
9446 /* folders always go first */
9447 if (is_folder_a != is_folder_b)
9448 return is_folder_a ? 1 : -1;
9450 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9452 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9459 recent_column_path_sort_func (GtkTreeModel *model,
9464 GtkFileChooserDefault *impl = user_data;
9465 GtkTreeIter child_a, child_b;
9466 gboolean is_folder_a, is_folder_b;
9467 gchar *name_a, *name_b;
9469 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9470 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9472 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9473 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9474 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9476 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9477 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9478 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9487 if (is_folder_a != is_folder_b)
9488 return is_folder_a ? 1 : -1;
9490 return strcmp (name_a, name_b);
9494 recent_get_is_filtered (GtkFileChooserDefault *impl,
9496 GtkRecentInfo *recent_info)
9498 GtkFileFilterInfo filter_info;
9499 GtkFileFilterFlags needed;
9502 if (!impl->current_filter)
9505 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9506 needed = gtk_file_filter_get_needed (impl->current_filter);
9508 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9509 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9511 if (needed & GTK_FILE_FILTER_FILENAME)
9513 filter_info.filename = g_file_get_path (file);
9514 if (filter_info.filename)
9515 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9518 filter_info.filename = NULL;
9520 if (needed & GTK_FILE_FILTER_URI)
9522 filter_info.uri = g_file_get_uri (file);
9523 if (filter_info.uri)
9524 filter_info.contains |= GTK_FILE_FILTER_URI;
9527 filter_info.uri = NULL;
9529 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9531 if (filter_info.filename)
9532 g_free ((gchar *) filter_info.filename);
9533 if (filter_info.uri)
9534 g_free ((gchar *) filter_info.uri);
9539 /* Visibility function for the recent filter model */
9541 recent_model_visible_func (GtkTreeModel *model,
9545 GtkFileChooserDefault *impl = user_data;
9547 GtkRecentInfo *recent_info;
9550 if (!impl->current_filter)
9553 gtk_tree_model_get (model, iter,
9554 RECENT_MODEL_COL_INFO, &recent_info,
9555 RECENT_MODEL_COL_FILE, &file,
9556 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9565 return !recent_get_is_filtered (impl, file, recent_info);
9569 recent_setup_model (GtkFileChooserDefault *impl)
9571 g_assert (impl->recent_model == NULL);
9572 g_assert (impl->recent_model_filter == NULL);
9573 g_assert (impl->recent_model_sort == NULL);
9575 /* We store these columns in the search model:
9577 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9578 * stored as a pointer and not as a G_TYPE_FILE;
9579 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9580 * stored as a pointer and not as a G_TYPE_STRING;
9581 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9582 * as a GTK_TYPE_RECENT_INFO;
9583 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9584 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9585 * and not as a G_TYPE_CANCELLABLE;
9587 * Keep this in sync with the enumeration defined near the beginning of
9590 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9597 impl->recent_model_filter =
9598 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9599 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9600 recent_model_visible_func,
9604 /* this is the model that will actually be added to
9605 * the browse_files_tree_view widget; remember: we are
9606 * stuffing the real model into a filter model and then
9607 * into a sort model; this means we'll have to translate
9608 * the child iterator *twice* to get from a path or an
9609 * iterator coming from the tree view widget to the
9610 * real data inside the model.
9612 impl->recent_model_sort =
9613 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9614 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9615 RECENT_MODEL_COL_FILE,
9616 recent_column_path_sort_func,
9618 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9619 RECENT_MODEL_COL_INFO,
9620 recent_column_mtime_sort_func,
9622 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9623 RECENT_MODEL_COL_INFO,
9624 GTK_SORT_DESCENDING);
9629 GtkFileChooserDefault *impl;
9632 gint n_loaded_items;
9633 guint needs_sorting : 1;
9637 recent_idle_cleanup (gpointer data)
9639 RecentLoadData *load_data = data;
9640 GtkFileChooserDefault *impl = load_data->impl;
9642 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9643 GTK_TREE_MODEL (impl->recent_model_sort));
9645 set_busy_cursor (impl, FALSE);
9647 impl->load_recent_id = 0;
9649 if (load_data->items)
9651 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9652 g_list_free (load_data->items);
9658 struct RecentItemInsertRequest
9660 GtkFileChooserDefault *impl;
9662 GtkTreeRowReference *row_ref;
9666 recent_item_get_info_cb (GCancellable *cancellable,
9668 const GError *error,
9671 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9674 GCancellable *model_cancellable;
9675 gboolean is_folder = FALSE;
9676 struct RecentItemInsertRequest *request = data;
9678 if (!request->impl->recent_model)
9681 path = gtk_tree_row_reference_get_path (request->row_ref);
9685 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9687 gtk_tree_path_free (path);
9689 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9690 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9692 if (cancellable != model_cancellable)
9695 gtk_list_store_set (request->impl->recent_model, &iter,
9696 RECENT_MODEL_COL_CANCELLABLE, NULL,
9704 gtk_list_store_remove (request->impl->recent_model, &iter);
9708 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9710 gtk_list_store_set (request->impl->recent_model, &iter,
9711 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9715 g_object_unref (request->impl);
9716 g_object_unref (request->file);
9717 gtk_tree_row_reference_free (request->row_ref);
9720 g_object_unref (cancellable);
9724 recent_sort_mru (gconstpointer a,
9727 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9728 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9730 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9734 get_recent_files_limit (GtkWidget *widget)
9736 GtkSettings *settings;
9739 if (gtk_widget_has_screen (widget))
9740 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9742 settings = gtk_settings_get_default ();
9744 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9750 recent_idle_load (gpointer data)
9752 RecentLoadData *load_data = data;
9753 GtkFileChooserDefault *impl = load_data->impl;
9756 GtkRecentInfo *info;
9757 const gchar *uri, *display_name;
9759 GCancellable *cancellable;
9760 struct RecentItemInsertRequest *request;
9762 if (!impl->recent_manager)
9765 /* first iteration: load all the items */
9766 if (!load_data->items)
9768 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9769 if (!load_data->items)
9772 load_data->needs_sorting = TRUE;
9777 /* second iteration: preliminary MRU sorting and clamping */
9778 if (load_data->needs_sorting)
9782 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9783 load_data->n_items = g_list_length (load_data->items);
9785 limit = get_recent_files_limit (GTK_WIDGET (impl));
9787 if (limit != -1 && (load_data->n_items > limit))
9791 clamp = g_list_nth (load_data->items, limit - 1);
9792 if (G_LIKELY (clamp))
9797 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9800 load_data->n_items = limit;
9804 load_data->n_loaded_items = 0;
9805 load_data->needs_sorting = FALSE;
9810 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9811 g_assert (info != NULL);
9813 uri = gtk_recent_info_get_uri (info);
9814 display_name = gtk_recent_info_get_display_name (info);
9815 file = g_file_new_for_uri (uri);
9817 gtk_list_store_append (impl->recent_model, &iter);
9818 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9820 request = g_new0 (struct RecentItemInsertRequest, 1);
9821 request->impl = g_object_ref (impl);
9822 request->file = g_object_ref (file);
9823 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9824 gtk_tree_path_free (p);
9826 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9828 recent_item_get_info_cb,
9831 gtk_list_store_set (impl->recent_model, &iter,
9832 RECENT_MODEL_COL_FILE, file,
9833 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9834 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9835 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9838 load_data->n_loaded_items += 1;
9840 /* finished loading items */
9841 if (load_data->n_loaded_items == load_data->n_items)
9843 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9844 g_list_free (load_data->items);
9845 load_data->items = NULL;
9854 recent_start_loading (GtkFileChooserDefault *impl)
9856 RecentLoadData *load_data;
9858 recent_stop_loading (impl);
9859 recent_clear_model (impl, TRUE);
9860 recent_setup_model (impl);
9861 set_busy_cursor (impl, TRUE);
9863 g_assert (impl->load_recent_id == 0);
9865 load_data = g_new (RecentLoadData, 1);
9866 load_data->impl = impl;
9867 load_data->items = NULL;
9868 load_data->n_items = 0;
9869 load_data->n_loaded_items = 0;
9870 load_data->needs_sorting = TRUE;
9872 /* begin lazy loading the recent files into the model */
9873 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9876 recent_idle_cleanup);
9880 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9890 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9891 *list = g_slist_prepend (*list, g_object_ref (file));
9894 /* Constructs a list of the selected paths in recent files mode */
9896 recent_get_selected_files (GtkFileChooserDefault *impl)
9899 GtkTreeSelection *selection;
9903 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9904 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9905 result = g_slist_reverse (result);
9910 /* Called from ::should_respond(). We return whether there are selected
9911 * files in the recent files list.
9914 recent_should_respond (GtkFileChooserDefault *impl)
9916 GtkTreeSelection *selection;
9918 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9920 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9921 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9924 /* Hide the location widgets temporarily */
9926 recent_hide_entry (GtkFileChooserDefault *impl)
9928 gtk_widget_hide (impl->browse_path_bar);
9929 gtk_widget_hide (impl->browse_new_folder_button);
9931 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9932 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9934 gtk_widget_hide (impl->location_button);
9935 gtk_widget_hide (impl->location_entry_box);
9939 /* Main entry point to the recent files functions; this gets called when
9940 * the user activates the Recently Used shortcut.
9943 recent_activate (GtkFileChooserDefault *impl)
9945 OperationMode previous_mode;
9947 if (impl->operation_mode == OPERATION_MODE_RECENT)
9950 previous_mode = impl->operation_mode;
9951 impl->operation_mode = OPERATION_MODE_RECENT;
9953 switch (previous_mode)
9955 case OPERATION_MODE_SEARCH:
9956 search_stop_searching (impl, FALSE);
9957 search_clear_model (impl, TRUE);
9959 gtk_widget_destroy (impl->search_hbox);
9960 impl->search_hbox = NULL;
9961 impl->search_entry = NULL;
9964 case OPERATION_MODE_BROWSE:
9965 stop_loading_and_clear_list_model (impl);
9968 case OPERATION_MODE_RECENT:
9969 g_assert_not_reached ();
9973 recent_hide_entry (impl);
9975 /* hide the file size column if it's visible */
9976 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
9978 file_list_set_sort_column_ids (impl);
9979 recent_start_loading (impl);
9982 /* convert an iterator coming from the model bound to
9983 * browse_files_tree_view to an interator inside the
9987 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9988 GtkTreeIter *child_iter,
9993 if (!impl->recent_model)
9996 if (!impl->recent_model_filter || !impl->recent_model_sort)
9999 /* pass 1: get the iterator in the filter model */
10000 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10003 /* pass 2: get the iterator in the real model */
10004 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10011 set_current_filter (GtkFileChooserDefault *impl,
10012 GtkFileFilter *filter)
10014 if (impl->current_filter != filter)
10018 /* NULL filters are allowed to reset to non-filtered status
10020 filter_index = g_slist_index (impl->filters, filter);
10021 if (impl->filters && filter && filter_index < 0)
10024 if (impl->current_filter)
10025 g_object_unref (impl->current_filter);
10026 impl->current_filter = filter;
10027 if (impl->current_filter)
10029 g_object_ref_sink (impl->current_filter);
10033 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10036 if (impl->browse_files_model)
10037 install_list_model_filter (impl);
10039 if (impl->search_model_filter)
10040 gtk_tree_model_filter_refilter (impl->search_model_filter);
10042 if (impl->recent_model_filter)
10043 gtk_tree_model_filter_refilter (impl->recent_model_filter);
10045 g_object_notify (G_OBJECT (impl), "filter");
10050 filter_combo_changed (GtkComboBox *combo_box,
10051 GtkFileChooserDefault *impl)
10053 gint new_index = gtk_combo_box_get_active (combo_box);
10054 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10056 set_current_filter (impl, new_filter);
10060 check_preview_change (GtkFileChooserDefault *impl)
10062 GtkTreePath *cursor_path;
10064 const char *new_display_name;
10066 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10068 new_display_name = NULL;
10071 GtkTreeIter child_iter;
10073 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10075 if (impl->sort_model)
10078 GFileInfo *new_info;
10080 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10081 gtk_tree_path_free (cursor_path);
10083 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10085 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10086 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10088 new_display_name = g_file_info_get_display_name (new_info);
10091 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10095 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10096 &iter, cursor_path);
10097 gtk_tree_path_free (cursor_path);
10099 search_get_valid_child_iter (impl, &child_iter, &iter);
10100 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10101 SEARCH_MODEL_COL_FILE, &new_file,
10102 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10105 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10109 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10110 &iter, cursor_path);
10111 gtk_tree_path_free (cursor_path);
10113 recent_get_valid_child_iter (impl, &child_iter, &iter);
10114 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10115 RECENT_MODEL_COL_FILE, &new_file,
10116 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10121 if (new_file != impl->preview_file &&
10122 !(new_file && impl->preview_file &&
10123 g_file_equal (new_file, impl->preview_file)))
10125 if (impl->preview_file)
10127 g_object_unref (impl->preview_file);
10128 g_free (impl->preview_display_name);
10133 impl->preview_file = g_object_ref (new_file);
10134 impl->preview_display_name = g_strdup (new_display_name);
10138 impl->preview_file = NULL;
10139 impl->preview_display_name = NULL;
10142 if (impl->use_preview_label && impl->preview_label)
10143 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10145 g_signal_emit_by_name (impl, "update-preview");
10150 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10151 GtkFileSystemVolume *volume,
10152 const GError *error,
10156 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10157 GtkFileChooserDefault *impl = data;
10159 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10162 impl->shortcuts_activate_iter_cancellable = NULL;
10164 set_busy_cursor (impl, FALSE);
10171 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10175 name = _gtk_file_system_volume_get_display_name (volume);
10176 msg = g_strdup_printf (_("Could not mount %s"), name);
10178 error_message (impl, msg, error->message);
10187 file = _gtk_file_system_volume_get_root (volume);
10190 change_folder_and_display_error (impl, file, FALSE);
10191 g_object_unref (file);
10195 g_object_unref (impl);
10196 g_object_unref (cancellable);
10200 /* Activates a volume by mounting it if necessary and then switching to its
10204 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10205 GtkFileSystemVolume *volume)
10209 switch (impl->operation_mode)
10211 case OPERATION_MODE_BROWSE:
10213 case OPERATION_MODE_SEARCH:
10214 search_switch_to_browse_mode (impl);
10216 case OPERATION_MODE_RECENT:
10217 recent_switch_to_browse_mode (impl);
10221 /* We ref the file chooser since volume_mount() may run a main loop, and the
10222 * user could close the file chooser window in the meantime.
10224 g_object_ref (impl);
10226 if (!_gtk_file_system_volume_is_mounted (volume))
10228 set_busy_cursor (impl, TRUE);
10230 impl->shortcuts_activate_iter_cancellable =
10231 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10232 shortcuts_activate_volume_mount_cb,
10233 g_object_ref (impl));
10237 file = _gtk_file_system_volume_get_root (volume);
10240 change_folder_and_display_error (impl, file, FALSE);
10241 g_object_unref (file);
10245 g_object_unref (impl);
10248 /* Opens the folder or volume at the specified iter in the shortcuts model */
10249 struct ShortcutsActivateData
10251 GtkFileChooserDefault *impl;
10256 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10258 const GError *error,
10259 gpointer user_data)
10261 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10262 struct ShortcutsActivateData *data = user_data;
10264 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10267 data->impl->shortcuts_activate_iter_cancellable = NULL;
10272 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10273 change_folder_and_display_error (data->impl, data->file, FALSE);
10275 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10280 g_object_unref (data->impl);
10281 g_object_unref (data->file);
10284 g_object_unref (cancellable);
10288 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10289 GtkFileSystemVolume *volume,
10290 const GError *error,
10291 gpointer user_data)
10293 struct ShortcutsActivateData *data = user_data;
10297 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10299 g_object_unref (data->impl);
10300 g_object_unref (data->file);
10306 data->impl->shortcuts_activate_iter_cancellable =
10307 _gtk_file_system_get_info (data->impl->file_system, data->file,
10309 shortcuts_activate_get_info_cb, data);
10313 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10317 ShortcutType shortcut_type;
10319 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10320 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10321 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10322 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10324 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10325 SHORTCUTS_COL_DATA, &col_data,
10326 SHORTCUTS_COL_TYPE, &shortcut_type,
10329 if (impl->shortcuts_activate_iter_cancellable)
10331 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10332 impl->shortcuts_activate_iter_cancellable = NULL;
10335 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10337 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10339 GtkFileSystemVolume *volume;
10343 shortcuts_activate_volume (impl, volume);
10345 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10347 struct ShortcutsActivateData *data;
10348 GtkFileSystemVolume *volume;
10350 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10352 data = g_new0 (struct ShortcutsActivateData, 1);
10353 data->impl = g_object_ref (impl);
10354 data->file = g_object_ref (col_data);
10356 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10358 GMountOperation *mount_operation;
10359 GtkWidget *toplevel;
10361 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10363 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10365 impl->shortcuts_activate_iter_cancellable =
10366 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10368 shortcuts_activate_mount_enclosing_volume,
10373 impl->shortcuts_activate_iter_cancellable =
10374 _gtk_file_system_get_info (impl->file_system, data->file,
10376 shortcuts_activate_get_info_cb, data);
10379 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10381 search_activate (impl);
10383 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10385 recent_activate (impl);
10389 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10391 shortcuts_key_press_event_cb (GtkWidget *widget,
10392 GdkEventKey *event,
10393 GtkFileChooserDefault *impl)
10397 modifiers = gtk_accelerator_get_default_mod_mask ();
10399 if ((event->keyval == GDK_BackSpace
10400 || event->keyval == GDK_Delete
10401 || event->keyval == GDK_KP_Delete)
10402 && (event->state & modifiers) == 0)
10404 remove_selected_bookmarks (impl);
10408 if ((event->keyval == GDK_F2)
10409 && (event->state & modifiers) == 0)
10411 rename_selected_bookmark (impl);
10419 shortcuts_select_func (GtkTreeSelection *selection,
10420 GtkTreeModel *model,
10422 gboolean path_currently_selected,
10425 GtkFileChooserDefault *impl = data;
10426 GtkTreeIter filter_iter;
10427 ShortcutType shortcut_type;
10429 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10430 g_assert_not_reached ();
10432 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10434 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10438 list_select_func (GtkTreeSelection *selection,
10439 GtkTreeModel *model,
10441 gboolean path_currently_selected,
10444 GtkFileChooserDefault *impl = data;
10446 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10447 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10449 GtkTreeIter iter, child_iter;
10451 switch (impl->operation_mode)
10453 case OPERATION_MODE_SEARCH:
10455 gboolean is_folder;
10457 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10460 search_get_valid_child_iter (impl, &child_iter, &iter);
10461 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10462 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10469 case OPERATION_MODE_RECENT:
10471 gboolean is_folder;
10473 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10476 recent_get_valid_child_iter (impl, &child_iter, &iter);
10477 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10478 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10485 case OPERATION_MODE_BROWSE:
10489 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10492 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10493 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10494 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10505 list_selection_changed (GtkTreeSelection *selection,
10506 GtkFileChooserDefault *impl)
10508 /* See if we are in the new folder editable row for Save mode */
10509 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10510 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10513 gboolean had_selection;
10515 info = get_selected_file_info_from_file_list (impl, &had_selection);
10516 if (!had_selection)
10517 goto out; /* normal processing */
10520 return; /* We are on the editable row for New Folder */
10525 if (impl->location_entry)
10526 update_chooser_entry (impl);
10528 check_preview_change (impl);
10529 bookmarks_check_add_sensitivity (impl);
10531 g_signal_emit_by_name (impl, "selection-changed", 0);
10534 /* Callback used when a row in the file list is activated */
10536 list_row_activated (GtkTreeView *tree_view,
10538 GtkTreeViewColumn *column,
10539 GtkFileChooserDefault *impl)
10542 GtkTreeIter child_iter;
10544 switch (impl->operation_mode)
10546 case OPERATION_MODE_SEARCH:
10549 gboolean is_folder;
10551 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10554 search_get_valid_child_iter (impl, &child_iter, &iter);
10555 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10556 SEARCH_MODEL_COL_FILE, &file,
10557 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10562 change_folder_and_display_error (impl, file, FALSE);
10566 g_signal_emit_by_name (impl, "file-activated");
10570 case OPERATION_MODE_RECENT:
10573 gboolean is_folder;
10575 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10578 recent_get_valid_child_iter (impl, &child_iter, &iter);
10579 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10580 RECENT_MODEL_COL_FILE, &file,
10581 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10586 change_folder_and_display_error (impl, file, FALSE);
10590 g_signal_emit_by_name (impl, "file-activated");
10594 case OPERATION_MODE_BROWSE:
10598 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10601 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10602 &child_iter, &iter);
10603 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10606 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10610 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10611 change_folder_and_display_error (impl, file, FALSE);
10615 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10616 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10617 g_signal_emit_by_name (impl, "file-activated");
10624 path_bar_clicked (GtkPathBar *path_bar,
10627 gboolean child_is_hidden,
10628 GtkFileChooserDefault *impl)
10631 pending_select_files_add (impl, child_file);
10633 if (!change_folder_and_display_error (impl, file, FALSE))
10636 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10637 * show hidden files so that ".baz" appears in the file list, as it will still
10638 * be shown in the path bar: "/foo/[bar]/.baz"
10640 if (child_is_hidden)
10641 g_object_set (impl, "show-hidden", TRUE, NULL);
10645 get_list_file_info (GtkFileChooserDefault *impl,
10648 GtkTreeIter child_iter;
10650 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10654 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10658 list_icon_data_func (GtkTreeViewColumn *tree_column,
10659 GtkCellRenderer *cell,
10660 GtkTreeModel *tree_model,
10664 GtkFileChooserDefault *impl = data;
10665 GtkTreeIter child_iter;
10666 GdkPixbuf *pixbuf = NULL;
10667 gboolean sensitive = TRUE;
10669 profile_start ("start", NULL);
10671 switch (impl->operation_mode)
10673 case OPERATION_MODE_SEARCH:
10675 GtkTreeIter child_iter;
10676 gboolean is_folder;
10678 search_get_valid_child_iter (impl, &child_iter, iter);
10679 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10680 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10681 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10684 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10685 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10686 sensitive = is_folder;
10690 case OPERATION_MODE_RECENT:
10692 GtkTreeIter child_iter;
10693 GtkRecentInfo *info;
10694 gboolean is_folder;
10696 recent_get_valid_child_iter (impl, &child_iter, iter);
10697 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10698 RECENT_MODEL_COL_INFO, &info,
10699 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10702 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10704 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10705 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10706 sensitive = is_folder;
10710 case OPERATION_MODE_BROWSE:
10715 info = get_list_file_info (impl, iter);
10717 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10720 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10725 /* FIXME: NULL GError */
10726 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10731 /* We are on the editable row */
10736 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10737 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10738 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10743 g_object_set (cell,
10745 "sensitive", sensitive,
10749 g_object_unref (pixbuf);
10751 profile_end ("end", NULL);
10755 list_name_data_func (GtkTreeViewColumn *tree_column,
10756 GtkCellRenderer *cell,
10757 GtkTreeModel *tree_model,
10761 GtkFileChooserDefault *impl = data;
10763 gboolean sensitive = TRUE;
10765 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10767 GtkTreeIter child_iter;
10768 gchar *display_name;
10769 gboolean is_folder;
10771 search_get_valid_child_iter (impl, &child_iter, iter);
10772 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10773 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10774 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10777 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10778 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10780 sensitive = is_folder;
10783 g_object_set (cell,
10784 "text", display_name,
10785 "sensitive", sensitive,
10786 "ellipsize", PANGO_ELLIPSIZE_END,
10792 if (impl->operation_mode == OPERATION_MODE_RECENT)
10794 GtkTreeIter child_iter;
10795 GtkRecentInfo *recent_info;
10796 gchar *display_name;
10797 gboolean is_folder;
10799 recent_get_valid_child_iter (impl, &child_iter, iter);
10800 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10801 RECENT_MODEL_COL_INFO, &recent_info,
10802 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10805 display_name = gtk_recent_info_get_short_name (recent_info);
10807 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10808 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10810 sensitive = is_folder;
10813 g_object_set (cell,
10814 "text", display_name,
10815 "sensitive", sensitive,
10816 "ellipsize", PANGO_ELLIPSIZE_END,
10819 g_free (display_name);
10824 info = get_list_file_info (impl, iter);
10829 g_object_set (cell,
10830 "text", _("Type name of new folder"),
10832 "ellipsize", PANGO_ELLIPSIZE_NONE,
10839 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10840 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10842 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10845 g_object_set (cell,
10846 "text", g_file_info_get_display_name (info),
10847 "sensitive", sensitive,
10848 "ellipsize", PANGO_ELLIPSIZE_END,
10853 list_size_data_func (GtkTreeViewColumn *tree_column,
10854 GtkCellRenderer *cell,
10855 GtkTreeModel *tree_model,
10859 GtkFileChooserDefault *impl = data;
10863 gboolean sensitive;
10865 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
10866 impl->operation_mode == OPERATION_MODE_RECENT)
10869 info = get_list_file_info (impl, iter);
10872 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10874 g_object_set (cell,
10876 "sensitive", sensitive,
10881 size = g_file_info_get_size (info);
10882 str = g_format_size_for_display (size);
10884 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10885 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10888 g_object_set (cell,
10890 "sensitive", sensitive,
10891 "alignment", PANGO_ALIGN_RIGHT,
10897 /* Tree column data callback for the file list; fetches the mtime of a file */
10899 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10900 GtkCellRenderer *cell,
10901 GtkTreeModel *tree_model,
10905 GtkFileChooserDefault *impl;
10906 GTimeVal timeval = { 0, };
10908 gchar *date_str = NULL;
10909 gboolean sensitive = TRUE;
10911 const char *locale, *dot = NULL;
10912 gint64 codepage = -1;
10918 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10920 GtkTreeIter child_iter;
10921 struct stat *statbuf;
10922 gboolean is_folder;
10924 search_get_valid_child_iter (impl, &child_iter, iter);
10925 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10926 SEARCH_MODEL_COL_STAT, &statbuf,
10927 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10930 time_mtime = statbuf->st_mtime;
10935 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10936 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10937 sensitive = is_folder;
10939 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10941 GtkTreeIter child_iter;
10942 GtkRecentInfo *info;
10943 gboolean is_folder;
10945 recent_get_valid_child_iter (impl, &child_iter, iter);
10946 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10947 RECENT_MODEL_COL_INFO, &info,
10948 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10952 time_mtime = gtk_recent_info_get_modified (info);
10956 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10957 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10958 sensitive = is_folder;
10964 info = get_list_file_info (impl, iter);
10967 g_object_set (cell,
10974 g_file_info_get_modification_time (info, &timeval);
10975 time_mtime = timeval.tv_sec;
10977 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10978 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10979 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10982 if (G_UNLIKELY (time_mtime == 0))
10983 date_str = g_strdup (_("Unknown"));
10988 struct tm tm_mtime;
10990 const gchar *format;
10991 gchar *locale_format = NULL;
10994 #ifdef HAVE_LOCALTIME_R
10995 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10998 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
11002 g_warning ("ptm != NULL failed");
11004 g_object_set (cell,
11005 "text", _("Unknown"),
11006 "sensitive", sensitive,
11011 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11013 #endif /* HAVE_LOCALTIME_R */
11015 g_date_set_time_t (&mtime, time_mtime);
11016 time_now = time (NULL);
11017 g_date_set_time_t (&now, time_now);
11019 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11021 /* Translators: %H means "hours" and %M means "minutes" */
11022 if (days_diff == 0)
11023 format = _("%H:%M");
11024 else if (days_diff == 1)
11025 format = _("Yesterday at %H:%M");
11028 if (days_diff > 1 && days_diff < 7)
11029 format = "%A"; /* Days from last week */
11031 format = "%x"; /* Any other date */
11035 /* g_locale_from_utf8() returns a string in the system
11036 * code-page, which is not always the same as that used by the C
11037 * library. For instance when running a GTK+ program with
11038 * LANG=ko on an English version of Windows, the system
11039 * code-page is 1252, but the code-page used by the C library is
11040 * 949. (It's GTK+ itself that sets the C library locale when it
11041 * notices the LANG environment variable. See gtkmain.c The
11042 * Microsoft C library doesn't look at any locale environment
11043 * variables.) We need to pass strftime() a string in the C
11044 * library's code-page. See bug #509885.
11046 locale = setlocale (LC_ALL, NULL);
11047 if (locale != NULL)
11048 dot = strchr (locale, '.');
11051 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11053 /* All codepages should fit in 16 bits AFAIK */
11054 if (codepage > 0 && codepage < 65536)
11056 sprintf (charset, "CP%u", (guint) codepage);
11057 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11061 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11063 if (locale_format != NULL &&
11064 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11067 /* As above but in opposite direction... */
11068 if (codepage > 0 && codepage < 65536)
11069 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11071 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11075 if (date_str == NULL)
11076 date_str = g_strdup (_("Unknown"));
11078 g_free (locale_format);
11081 g_object_set (cell,
11083 "sensitive", sensitive,
11089 _gtk_file_chooser_default_new (void)
11091 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11095 location_set_user_text (GtkFileChooserDefault *impl,
11098 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11099 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11103 location_popup_handler (GtkFileChooserDefault *impl,
11106 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11108 GtkWidget *widget_to_focus;
11110 /* This will give us the location widgets back */
11111 switch (impl->operation_mode)
11113 case OPERATION_MODE_SEARCH:
11114 search_switch_to_browse_mode (impl);
11116 case OPERATION_MODE_RECENT:
11117 recent_switch_to_browse_mode (impl);
11119 case OPERATION_MODE_BROWSE:
11120 g_assert_not_reached ();
11124 if (impl->current_folder)
11125 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11127 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11128 widget_to_focus = impl->browse_files_tree_view;
11130 widget_to_focus = impl->location_entry;
11132 gtk_widget_grab_focus (widget_to_focus);
11136 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11137 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11139 LocationMode new_mode;
11143 /* since the user typed something, we unconditionally want to turn on the entry */
11144 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11146 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11147 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11148 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11149 new_mode = LOCATION_MODE_PATH_BAR;
11152 g_assert_not_reached ();
11156 location_mode_set (impl, new_mode, TRUE);
11157 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11160 location_set_user_text (impl, path);
11163 location_entry_set_initial_text (impl);
11164 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11168 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11169 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11171 gtk_widget_grab_focus (impl->location_entry);
11173 location_set_user_text (impl, path);
11176 g_assert_not_reached ();
11179 /* Handler for the "up-folder" keybinding signal */
11181 up_folder_handler (GtkFileChooserDefault *impl)
11183 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11186 /* Handler for the "down-folder" keybinding signal */
11188 down_folder_handler (GtkFileChooserDefault *impl)
11190 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11193 /* Switches to the shortcut in the specified index */
11195 switch_to_shortcut (GtkFileChooserDefault *impl,
11200 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11201 g_assert_not_reached ();
11203 shortcuts_activate_iter (impl, &iter);
11206 /* Handler for the "home-folder" keybinding signal */
11208 home_folder_handler (GtkFileChooserDefault *impl)
11210 if (impl->has_home)
11211 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11214 /* Handler for the "desktop-folder" keybinding signal */
11216 desktop_folder_handler (GtkFileChooserDefault *impl)
11218 if (impl->has_desktop)
11219 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11222 /* Handler for the "search-shortcut" keybinding signal */
11224 search_shortcut_handler (GtkFileChooserDefault *impl)
11226 if (impl->has_search)
11228 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11230 /* we want the entry widget to grab the focus the first
11231 * time, not the browse_files_tree_view widget.
11233 if (impl->search_entry)
11234 gtk_widget_grab_focus (impl->search_entry);
11238 /* Handler for the "recent-shortcut" keybinding signal */
11240 recent_shortcut_handler (GtkFileChooserDefault *impl)
11242 if (impl->has_recent)
11243 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11247 quick_bookmark_handler (GtkFileChooserDefault *impl,
11248 gint bookmark_index)
11253 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11256 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11258 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11259 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11262 gtk_tree_path_free (path);
11264 switch_to_shortcut (impl, bookmark_pos);
11268 show_hidden_handler (GtkFileChooserDefault *impl)
11270 g_object_set (impl,
11271 "show-hidden", !impl->show_hidden,
11276 /* Drag and drop interfaces */
11279 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11284 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11286 model->impl = NULL;
11289 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11291 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11294 ShortcutsPaneModelFilter *model;
11298 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11300 pos = *gtk_tree_path_get_indices (path);
11301 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11303 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11306 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11308 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11310 GtkSelectionData *selection_data)
11312 ShortcutsPaneModelFilter *model;
11314 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11321 /* Fill the GtkTreeDragSourceIface vtable */
11323 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11325 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11326 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11330 /* Fill the GtkTreeDragDestIface vtable */
11332 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11334 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11335 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11339 static GtkTreeModel *
11340 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11341 GtkTreeModel *child_model,
11344 ShortcutsPaneModelFilter *model;
11346 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11347 "child-model", child_model,
11348 "virtual-root", root,
11351 model->impl = impl;
11353 return GTK_TREE_MODEL (model);
11359 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11362 RecentModelSort *model;
11363 GtkTreeIter iter, child_iter;
11364 gboolean is_folder;
11366 model = RECENT_MODEL_SORT (drag_source);
11367 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11370 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11371 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11372 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11379 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11381 GtkSelectionData *selection_data)
11383 RecentModelSort *model;
11384 GtkTreeIter iter, child_iter;
11388 model = RECENT_MODEL_SORT (drag_source);
11389 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11392 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11393 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11394 RECENT_MODEL_COL_FILE, &file,
11396 g_assert (file != NULL);
11398 uris[0] = g_file_get_uri (file);
11401 gtk_selection_data_set_uris (selection_data, uris);
11409 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11411 iface->row_draggable = recent_model_sort_row_draggable;
11412 iface->drag_data_get = recent_model_sort_drag_data_get;
11416 _recent_model_sort_class_init (RecentModelSortClass *klass)
11422 _recent_model_sort_init (RecentModelSort *model)
11424 model->impl = NULL;
11427 static GtkTreeModel *
11428 recent_model_sort_new (GtkFileChooserDefault *impl,
11429 GtkTreeModel *child_model)
11431 RecentModelSort *model;
11433 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11434 "model", child_model,
11436 model->impl = impl;
11438 return GTK_TREE_MODEL (model);
11444 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11447 SearchModelSort *model;
11448 GtkTreeIter iter, child_iter;
11449 gboolean is_folder;
11451 model = SEARCH_MODEL_SORT (drag_source);
11452 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11455 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11456 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11457 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11464 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11466 GtkSelectionData *selection_data)
11468 SearchModelSort *model;
11469 GtkTreeIter iter, child_iter;
11473 model = SEARCH_MODEL_SORT (drag_source);
11474 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11477 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11478 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11479 RECENT_MODEL_COL_FILE, &file,
11481 g_assert (file != NULL);
11483 uris[0] = g_file_get_uri (file);
11486 gtk_selection_data_set_uris (selection_data, uris);
11494 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11496 iface->row_draggable = search_model_sort_row_draggable;
11497 iface->drag_data_get = search_model_sort_drag_data_get;
11501 _search_model_sort_class_init (SearchModelSortClass *klass)
11507 _search_model_sort_init (SearchModelSort *model)
11509 model->impl = NULL;
11512 static GtkTreeModel *
11513 search_model_sort_new (GtkFileChooserDefault *impl,
11514 GtkTreeModel *child_model)
11516 SearchModelSort *model;
11518 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11519 "model", child_model,
11521 model->impl = impl;
11523 return GTK_TREE_MODEL (model);