1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystem.h"
44 #include "gtkfilesystemmodel.h"
47 #include "gtkhpaned.h"
48 #include "gtkiconfactory.h"
49 #include "gtkicontheme.h"
51 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkmountoperation.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkrecentfilter.h"
60 #include "gtkrecentmanager.h"
61 #include "gtkscrolledwindow.h"
62 #include "gtkseparatormenuitem.h"
63 #include "gtksizegroup.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 /* Column numbers for the search model.
195 * Keep this in sync with search_setup_model()
198 SEARCH_MODEL_COL_FILE,
199 SEARCH_MODEL_COL_DISPLAY_NAME,
200 SEARCH_MODEL_COL_COLLATION_KEY,
201 SEARCH_MODEL_COL_MTIME,
202 SEARCH_MODEL_COL_SIZE,
203 SEARCH_MODEL_COL_CANCELLABLE,
204 SEARCH_MODEL_COL_PIXBUF,
205 SEARCH_MODEL_COL_MIME_TYPE,
206 SEARCH_MODEL_COL_IS_FOLDER,
207 SEARCH_MODEL_COL_NUM_COLUMNS
211 RECENT_MODEL_COL_FILE,
212 RECENT_MODEL_COL_DISPLAY_NAME,
213 RECENT_MODEL_COL_INFO,
214 RECENT_MODEL_COL_IS_FOLDER,
215 RECENT_MODEL_COL_CANCELLABLE,
216 RECENT_MODEL_COL_NUM_COLUMNS
219 /* Identifiers for target types */
225 search_is_possible (GtkFileChooserDefault *impl)
227 if (impl->search_engine == NULL)
228 impl->search_engine = _gtk_search_engine_new ();
230 return impl->search_engine != NULL;
233 /* Interesting places in the shortcuts bar */
237 SHORTCUTS_RECENT_SEPARATOR,
242 SHORTCUTS_BOOKMARKS_SEPARATOR,
244 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
245 SHORTCUTS_CURRENT_FOLDER
248 /* Icon size for if we can't get it from the theme */
249 #define FALLBACK_ICON_SIZE 16
251 #define PREVIEW_HBOX_SPACING 12
255 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
256 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
258 static GObject* gtk_file_chooser_default_constructor (GType type,
259 guint n_construct_properties,
260 GObjectConstructParam *construct_params);
261 static void gtk_file_chooser_default_finalize (GObject *object);
262 static void gtk_file_chooser_default_set_property (GObject *object,
266 static void gtk_file_chooser_default_get_property (GObject *object,
270 static void gtk_file_chooser_default_dispose (GObject *object);
271 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
272 static void gtk_file_chooser_default_realize (GtkWidget *widget);
273 static void gtk_file_chooser_default_map (GtkWidget *widget);
274 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
275 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
276 GtkWidget *previous_toplevel);
277 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
278 GtkStyle *previous_style);
279 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
280 GdkScreen *previous_screen);
281 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
282 GtkAllocation *allocation);
284 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
287 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
290 gboolean clear_entry,
292 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
293 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
295 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
298 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
300 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
301 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
302 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
303 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
304 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
305 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
306 GtkFileFilter *filter);
307 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
308 GtkFileFilter *filter);
309 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
310 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
313 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
316 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
318 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
320 gint *default_height);
321 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
322 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
324 static void location_popup_handler (GtkFileChooserDefault *impl,
326 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
327 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
328 static void up_folder_handler (GtkFileChooserDefault *impl);
329 static void down_folder_handler (GtkFileChooserDefault *impl);
330 static void home_folder_handler (GtkFileChooserDefault *impl);
331 static void desktop_folder_handler (GtkFileChooserDefault *impl);
332 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
333 gint bookmark_index);
334 static void show_hidden_handler (GtkFileChooserDefault *impl);
335 static void search_shortcut_handler (GtkFileChooserDefault *impl);
336 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
337 static void update_appearance (GtkFileChooserDefault *impl);
339 static void set_current_filter (GtkFileChooserDefault *impl,
340 GtkFileFilter *filter);
341 static void check_preview_change (GtkFileChooserDefault *impl);
343 static void filter_combo_changed (GtkComboBox *combo_box,
344 GtkFileChooserDefault *impl);
346 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
348 GtkFileChooserDefault *impl);
350 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
353 gboolean path_currently_selected,
355 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
357 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
359 static int shortcuts_get_index (GtkFileChooserDefault *impl,
360 ShortcutsIndex where);
361 static int shortcut_find_position (GtkFileChooserDefault *impl,
364 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
366 static gboolean list_select_func (GtkTreeSelection *selection,
369 gboolean path_currently_selected,
372 static void list_selection_changed (GtkTreeSelection *tree_selection,
373 GtkFileChooserDefault *impl);
374 static void list_row_activated (GtkTreeView *tree_view,
376 GtkTreeViewColumn *column,
377 GtkFileChooserDefault *impl);
379 static void select_func (GtkFileSystemModel *model,
384 static void path_bar_clicked (GtkPathBar *path_bar,
387 gboolean child_is_hidden,
388 GtkFileChooserDefault *impl);
390 static void add_bookmark_button_clicked_cb (GtkButton *button,
391 GtkFileChooserDefault *impl);
392 static void remove_bookmark_button_clicked_cb (GtkButton *button,
393 GtkFileChooserDefault *impl);
394 static void save_folder_combo_changed_cb (GtkComboBox *combo,
395 GtkFileChooserDefault *impl);
397 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
398 GtkCellRenderer *cell,
399 GtkTreeModel *tree_model,
402 static void list_name_data_func (GtkTreeViewColumn *tree_column,
403 GtkCellRenderer *cell,
404 GtkTreeModel *tree_model,
407 static void list_size_data_func (GtkTreeViewColumn *tree_column,
408 GtkCellRenderer *cell,
409 GtkTreeModel *tree_model,
412 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
413 GtkCellRenderer *cell,
414 GtkTreeModel *tree_model,
418 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
421 static void load_remove_timer (GtkFileChooserDefault *impl);
422 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
424 static void location_button_toggled_cb (GtkToggleButton *toggle,
425 GtkFileChooserDefault *impl);
426 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
428 static void search_stop_searching (GtkFileChooserDefault *impl,
429 gboolean remove_query);
430 static void search_clear_model_row (GtkTreeModel *model,
432 static void search_clear_model (GtkFileChooserDefault *impl,
433 gboolean remove_from_treeview);
434 static gboolean search_should_respond (GtkFileChooserDefault *impl);
435 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
436 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
437 static void search_entry_activate_cb (GtkEntry *entry,
439 static void settings_load (GtkFileChooserDefault *impl);
440 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
441 GtkTreeIter *child_iter,
444 static void recent_stop_loading (GtkFileChooserDefault *impl);
445 static void recent_clear_model (GtkFileChooserDefault *impl,
446 gboolean remove_from_treeview);
447 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
448 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
449 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
450 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
451 GtkTreeIter *child_iter,
453 static void set_file_system_backend (GtkFileChooserDefault *impl);
454 static void unset_file_system_backend (GtkFileChooserDefault *impl);
460 /* Drag and drop interface declarations */
463 GtkTreeModelFilter parent;
465 GtkFileChooserDefault *impl;
466 } ShortcutsPaneModelFilter;
469 GtkTreeModelFilterClass parent_class;
470 } ShortcutsPaneModelFilterClass;
472 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
473 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
475 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
477 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
478 _shortcuts_pane_model_filter,
479 GTK_TYPE_TREE_MODEL_FILTER,
480 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
481 shortcuts_pane_model_filter_drag_source_iface_init))
483 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
484 GtkTreeModel *child_model,
489 GtkTreeModelSort parent;
491 GtkFileChooserDefault *impl;
495 GtkTreeModelSortClass parent_class;
496 } RecentModelSortClass;
498 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
499 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
501 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
503 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
505 GTK_TYPE_TREE_MODEL_SORT,
506 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
507 recent_model_sort_drag_source_iface_init));
509 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
510 GtkTreeModel *child_model);
514 GtkTreeModelSort parent;
516 GtkFileChooserDefault *impl;
520 GtkTreeModelSortClass parent_class;
521 } SearchModelSortClass;
523 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
524 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
526 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
528 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
530 GTK_TYPE_TREE_MODEL_SORT,
531 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
532 search_model_sort_drag_source_iface_init));
534 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
535 GtkTreeModel *child_model);
539 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
540 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
541 gtk_file_chooser_default_iface_init)
542 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
543 gtk_file_chooser_embed_default_iface_init));
546 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
548 static const guint quick_bookmark_keyvals[10] = {
549 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
551 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
552 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
553 GtkBindingSet *binding_set;
556 gobject_class->finalize = gtk_file_chooser_default_finalize;
557 gobject_class->constructor = gtk_file_chooser_default_constructor;
558 gobject_class->set_property = gtk_file_chooser_default_set_property;
559 gobject_class->get_property = gtk_file_chooser_default_get_property;
560 gobject_class->dispose = gtk_file_chooser_default_dispose;
562 widget_class->show_all = gtk_file_chooser_default_show_all;
563 widget_class->realize = gtk_file_chooser_default_realize;
564 widget_class->map = gtk_file_chooser_default_map;
565 widget_class->unmap = gtk_file_chooser_default_unmap;
566 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
567 widget_class->style_set = gtk_file_chooser_default_style_set;
568 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
569 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
571 signals[LOCATION_POPUP] =
572 g_signal_new_class_handler (I_("location-popup"),
573 G_OBJECT_CLASS_TYPE (class),
574 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
575 G_CALLBACK (location_popup_handler),
577 _gtk_marshal_VOID__STRING,
578 G_TYPE_NONE, 1, G_TYPE_STRING);
580 signals[LOCATION_POPUP_ON_PASTE] =
581 g_signal_new_class_handler (I_("location-popup-on-paste"),
582 G_OBJECT_CLASS_TYPE (class),
583 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
584 G_CALLBACK (location_popup_on_paste_handler),
586 _gtk_marshal_VOID__VOID,
589 signals[LOCATION_TOGGLE_POPUP] =
590 g_signal_new_class_handler (I_("location-toggle-popup"),
591 G_OBJECT_CLASS_TYPE (class),
592 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
593 G_CALLBACK (location_toggle_popup_handler),
595 _gtk_marshal_VOID__VOID,
599 g_signal_new_class_handler (I_("up-folder"),
600 G_OBJECT_CLASS_TYPE (class),
601 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
602 G_CALLBACK (up_folder_handler),
604 _gtk_marshal_VOID__VOID,
607 signals[DOWN_FOLDER] =
608 g_signal_new_class_handler (I_("down-folder"),
609 G_OBJECT_CLASS_TYPE (class),
610 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
611 G_CALLBACK (down_folder_handler),
613 _gtk_marshal_VOID__VOID,
616 signals[HOME_FOLDER] =
617 g_signal_new_class_handler (I_("home-folder"),
618 G_OBJECT_CLASS_TYPE (class),
619 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
620 G_CALLBACK (home_folder_handler),
622 _gtk_marshal_VOID__VOID,
625 signals[DESKTOP_FOLDER] =
626 g_signal_new_class_handler (I_("desktop-folder"),
627 G_OBJECT_CLASS_TYPE (class),
628 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
629 G_CALLBACK (desktop_folder_handler),
631 _gtk_marshal_VOID__VOID,
634 signals[QUICK_BOOKMARK] =
635 g_signal_new_class_handler (I_("quick-bookmark"),
636 G_OBJECT_CLASS_TYPE (class),
637 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
638 G_CALLBACK (quick_bookmark_handler),
640 _gtk_marshal_VOID__INT,
641 G_TYPE_NONE, 1, G_TYPE_INT);
643 signals[SHOW_HIDDEN] =
644 g_signal_new_class_handler (I_("show-hidden"),
645 G_OBJECT_CLASS_TYPE (class),
646 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
647 G_CALLBACK (show_hidden_handler),
649 _gtk_marshal_VOID__VOID,
652 signals[SEARCH_SHORTCUT] =
653 g_signal_new_class_handler (I_("search-shortcut"),
654 G_OBJECT_CLASS_TYPE (class),
655 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
656 G_CALLBACK (search_shortcut_handler),
658 _gtk_marshal_VOID__VOID,
661 signals[RECENT_SHORTCUT] =
662 g_signal_new_class_handler (I_("recent-shortcut"),
663 G_OBJECT_CLASS_TYPE (class),
664 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
665 G_CALLBACK (recent_shortcut_handler),
667 _gtk_marshal_VOID__VOID,
670 binding_set = gtk_binding_set_by_class (class);
672 gtk_binding_entry_add_signal (binding_set,
673 GDK_l, GDK_CONTROL_MASK,
674 "location-toggle-popup",
677 gtk_binding_entry_add_signal (binding_set,
680 1, G_TYPE_STRING, "/");
681 gtk_binding_entry_add_signal (binding_set,
684 1, G_TYPE_STRING, "/");
687 gtk_binding_entry_add_signal (binding_set,
690 1, G_TYPE_STRING, "~");
693 gtk_binding_entry_add_signal (binding_set,
694 GDK_v, GDK_CONTROL_MASK,
695 "location-popup-on-paste",
697 gtk_binding_entry_add_signal (binding_set,
698 GDK_Up, GDK_MOD1_MASK,
701 gtk_binding_entry_add_signal (binding_set,
705 gtk_binding_entry_add_signal (binding_set,
706 GDK_KP_Up, GDK_MOD1_MASK,
710 gtk_binding_entry_add_signal (binding_set,
711 GDK_Down, GDK_MOD1_MASK,
714 gtk_binding_entry_add_signal (binding_set,
715 GDK_KP_Down, GDK_MOD1_MASK,
719 gtk_binding_entry_add_signal (binding_set,
720 GDK_Home, GDK_MOD1_MASK,
723 gtk_binding_entry_add_signal (binding_set,
724 GDK_KP_Home, GDK_MOD1_MASK,
727 gtk_binding_entry_add_signal (binding_set,
728 GDK_d, GDK_MOD1_MASK,
731 gtk_binding_entry_add_signal (binding_set,
732 GDK_h, GDK_CONTROL_MASK,
735 gtk_binding_entry_add_signal (binding_set,
736 GDK_s, GDK_MOD1_MASK,
739 gtk_binding_entry_add_signal (binding_set,
740 GDK_r, GDK_MOD1_MASK,
744 for (i = 0; i < 10; i++)
745 gtk_binding_entry_add_signal (binding_set,
746 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
750 _gtk_file_chooser_install_properties (gobject_class);
754 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
756 iface->select_file = gtk_file_chooser_default_select_file;
757 iface->unselect_file = gtk_file_chooser_default_unselect_file;
758 iface->select_all = gtk_file_chooser_default_select_all;
759 iface->unselect_all = gtk_file_chooser_default_unselect_all;
760 iface->get_files = gtk_file_chooser_default_get_files;
761 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
762 iface->get_file_system = gtk_file_chooser_default_get_file_system;
763 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
764 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
765 iface->set_current_name = gtk_file_chooser_default_set_current_name;
766 iface->add_filter = gtk_file_chooser_default_add_filter;
767 iface->remove_filter = gtk_file_chooser_default_remove_filter;
768 iface->list_filters = gtk_file_chooser_default_list_filters;
769 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
770 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
771 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
775 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
777 iface->get_default_size = gtk_file_chooser_default_get_default_size;
778 iface->should_respond = gtk_file_chooser_default_should_respond;
779 iface->initial_focus = gtk_file_chooser_default_initial_focus;
783 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
785 profile_start ("start", NULL);
786 #ifdef PROFILE_FILE_CHOOSER
787 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
789 impl->local_only = TRUE;
790 impl->preview_widget_active = TRUE;
791 impl->use_preview_label = TRUE;
792 impl->select_multiple = FALSE;
793 impl->show_hidden = FALSE;
794 impl->show_size_column = TRUE;
795 impl->icon_size = FALLBACK_ICON_SIZE;
796 impl->load_state = LOAD_EMPTY;
797 impl->reload_state = RELOAD_EMPTY;
798 impl->pending_select_files = NULL;
799 impl->location_mode = LOCATION_MODE_PATH_BAR;
800 impl->operation_mode = OPERATION_MODE_BROWSE;
801 impl->sort_column = FILE_LIST_COL_NAME;
802 impl->sort_order = GTK_SORT_ASCENDING;
803 impl->recent_manager = gtk_recent_manager_get_default ();
804 impl->create_folders = TRUE;
806 gtk_box_set_spacing (GTK_BOX (impl), 12);
808 set_file_system_backend (impl);
810 profile_end ("end", NULL);
813 /* Frees the data columns for the specified iter in the shortcuts model*/
815 shortcuts_free_row_data (GtkFileChooserDefault *impl,
819 ShortcutType shortcut_type;
820 GCancellable *cancellable;
822 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
823 SHORTCUTS_COL_DATA, &col_data,
824 SHORTCUTS_COL_TYPE, &shortcut_type,
825 SHORTCUTS_COL_CANCELLABLE, &cancellable,
829 g_cancellable_cancel (cancellable);
831 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
832 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
836 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
838 GtkFileSystemVolume *volume;
841 _gtk_file_system_volume_free (volume);
847 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
850 g_object_unref (file);
854 /* Frees all the data columns in the shortcuts model */
856 shortcuts_free (GtkFileChooserDefault *impl)
860 if (!impl->shortcuts_model)
863 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
866 shortcuts_free_row_data (impl, &iter);
868 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
870 g_object_unref (impl->shortcuts_model);
871 impl->shortcuts_model = NULL;
875 pending_select_files_free (GtkFileChooserDefault *impl)
877 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
878 g_slist_free (impl->pending_select_files);
879 impl->pending_select_files = NULL;
883 pending_select_files_add (GtkFileChooserDefault *impl,
886 impl->pending_select_files =
887 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
890 /* Used from gtk_tree_selection_selected_foreach() */
892 store_selection_foreach (GtkTreeModel *model,
897 GtkFileChooserDefault *impl;
898 GtkTreeIter child_iter;
901 impl = GTK_FILE_CHOOSER_DEFAULT (data);
903 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
905 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
906 pending_select_files_add (impl, file);
909 /* Stores the current selection in the list of paths to select; this is used to
910 * preserve the selection when reloading the current folder.
913 pending_select_files_store_selection (GtkFileChooserDefault *impl)
915 GtkTreeSelection *selection;
917 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
918 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
922 gtk_file_chooser_default_finalize (GObject *object)
924 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
927 unset_file_system_backend (impl);
929 if (impl->shortcuts_pane_filter_model)
930 g_object_unref (impl->shortcuts_pane_filter_model);
932 if (impl->shortcuts_combo_filter_model)
933 g_object_unref (impl->shortcuts_combo_filter_model);
935 shortcuts_free (impl);
937 g_free (impl->browse_files_last_selected_name);
939 for (l = impl->filters; l; l = l->next)
941 GtkFileFilter *filter;
943 filter = GTK_FILE_FILTER (l->data);
944 g_object_unref (filter);
946 g_slist_free (impl->filters);
948 if (impl->current_filter)
949 g_object_unref (impl->current_filter);
951 if (impl->current_volume_file)
952 g_object_unref (impl->current_volume_file);
954 if (impl->current_folder)
955 g_object_unref (impl->current_folder);
957 if (impl->preview_file)
958 g_object_unref (impl->preview_file);
960 if (impl->browse_path_bar_size_group)
961 g_object_unref (impl->browse_path_bar_size_group);
963 load_remove_timer (impl);
965 /* Free all the Models we have */
966 if (impl->browse_files_model)
967 g_object_unref (impl->browse_files_model);
969 if (impl->sort_model)
970 g_object_unref (impl->sort_model);
972 search_clear_model (impl, FALSE);
973 recent_clear_model (impl, FALSE);
975 g_free (impl->preview_display_name);
977 g_free (impl->edited_new_text);
979 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
982 /* Shows an error dialog set as transient for the specified window */
984 error_message_with_parent (GtkWindow *parent,
990 dialog = gtk_message_dialog_new (parent,
991 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
996 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
999 if (parent && parent->group)
1000 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1002 gtk_dialog_run (GTK_DIALOG (dialog));
1003 gtk_widget_destroy (dialog);
1006 /* Returns a toplevel GtkWindow, or NULL if none */
1008 get_toplevel (GtkWidget *widget)
1010 GtkWidget *toplevel;
1012 toplevel = gtk_widget_get_toplevel (widget);
1013 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1016 return GTK_WINDOW (toplevel);
1019 /* Shows an error dialog for the file chooser */
1021 error_message (GtkFileChooserDefault *impl,
1025 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1028 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1030 error_dialog (GtkFileChooserDefault *impl,
1041 uri = g_file_get_uri (file);
1042 text = g_strdup_printf (msg, uri);
1043 error_message (impl, text, error->message);
1046 g_error_free (error);
1050 /* Displays an error message about not being able to get information for a file.
1051 * Frees the GError as well.
1054 error_getting_info_dialog (GtkFileChooserDefault *impl,
1059 _("Could not retrieve information about the file"),
1063 /* Shows an error dialog about not being able to add a bookmark */
1065 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1070 _("Could not add a bookmark"),
1074 /* Shows an error dialog about not being able to remove a bookmark */
1076 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1081 _("Could not remove bookmark"),
1085 /* Shows an error dialog about not being able to create a folder */
1087 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1092 _("The folder could not be created"),
1096 /* Shows an error about not being able to create a folder because a file with
1097 * the same name is already there.
1100 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1105 _("The folder could not be created, as a file with the same "
1106 "name already exists. Try using a different name for the "
1107 "folder, or rename the file first."),
1111 /* Shows an error dialog about not being able to create a filename */
1113 error_building_filename_dialog (GtkFileChooserDefault *impl,
1116 error_dialog (impl, _("Invalid file name"),
1120 /* Shows an error dialog when we cannot switch to a folder */
1122 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1126 error_dialog (impl, _("The folder contents could not be displayed"),
1130 /* Changes folders, displaying an error dialog if this fails */
1132 change_folder_and_display_error (GtkFileChooserDefault *impl,
1134 gboolean clear_entry)
1139 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1141 /* We copy the path because of this case:
1143 * list_row_activated()
1144 * fetches path from model; path belongs to the model (*)
1145 * calls change_folder_and_display_error()
1146 * calls gtk_file_chooser_set_current_folder_file()
1147 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1151 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1154 error_changing_folder_dialog (impl, file, error);
1160 emit_default_size_changed (GtkFileChooserDefault *impl)
1162 profile_msg (" emit default-size-changed start", NULL);
1163 g_signal_emit_by_name (impl, "default-size-changed");
1164 profile_msg (" emit default-size-changed end", NULL);
1168 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1170 if (impl->use_preview_label)
1172 if (!impl->preview_label)
1174 impl->preview_label = gtk_label_new (impl->preview_display_name);
1175 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1176 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1177 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1178 gtk_widget_show (impl->preview_label);
1183 if (impl->preview_label)
1185 gtk_widget_destroy (impl->preview_label);
1186 impl->preview_label = NULL;
1190 if (impl->preview_widget_active && impl->preview_widget)
1191 gtk_widget_show (impl->preview_box);
1193 gtk_widget_hide (impl->preview_box);
1195 if (!GTK_WIDGET_MAPPED (impl))
1196 emit_default_size_changed (impl);
1200 set_preview_widget (GtkFileChooserDefault *impl,
1201 GtkWidget *preview_widget)
1203 if (preview_widget == impl->preview_widget)
1206 if (impl->preview_widget)
1207 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1208 impl->preview_widget);
1210 impl->preview_widget = preview_widget;
1211 if (impl->preview_widget)
1213 gtk_widget_show (impl->preview_widget);
1214 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1215 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1216 impl->preview_widget,
1217 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1220 update_preview_widget_visibility (impl);
1223 /* Renders a "Search" icon at an appropriate size for a tree view */
1225 render_search_icon (GtkFileChooserDefault *impl)
1227 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1231 render_recent_icon (GtkFileChooserDefault *impl)
1233 GtkIconTheme *theme;
1236 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1237 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1239 theme = gtk_icon_theme_get_default ();
1241 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1247 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1253 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1254 struct ReloadIconsData
1256 GtkFileChooserDefault *impl;
1257 GtkTreeRowReference *row_ref;
1261 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1263 const GError *error,
1269 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1270 struct ReloadIconsData *data = user_data;
1272 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1275 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1277 if (cancelled || error)
1280 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1282 path = gtk_tree_row_reference_get_path (data->row_ref);
1283 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1284 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1285 SHORTCUTS_COL_PIXBUF, pixbuf,
1287 gtk_tree_path_free (path);
1290 g_object_unref (pixbuf);
1293 gtk_tree_row_reference_free (data->row_ref);
1294 g_object_unref (data->impl);
1297 g_object_unref (cancellable);
1301 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1306 profile_start ("start", NULL);
1308 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1311 for (l = impl->reload_icon_cancellables; l; l = l->next)
1313 GCancellable *cancellable = G_CANCELLABLE (l->data);
1314 g_cancellable_cancel (cancellable);
1316 g_slist_free (impl->reload_icon_cancellables);
1317 impl->reload_icon_cancellables = NULL;
1322 ShortcutType shortcut_type;
1323 gboolean pixbuf_visible;
1326 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1327 SHORTCUTS_COL_DATA, &data,
1328 SHORTCUTS_COL_TYPE, &shortcut_type,
1329 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1335 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1337 GtkFileSystemVolume *volume;
1340 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1341 impl->icon_size, NULL);
1343 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1345 if (g_file_is_native (G_FILE (data)))
1348 struct ReloadIconsData *info;
1349 GtkTreePath *tree_path;
1350 GCancellable *cancellable;
1354 info = g_new0 (struct ReloadIconsData, 1);
1355 info->impl = g_object_ref (impl);
1356 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1357 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1358 gtk_tree_path_free (tree_path);
1360 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1362 shortcuts_reload_icons_get_info_cb,
1364 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1368 GtkIconTheme *icon_theme;
1370 /* Don't call get_info for remote paths to avoid latency and
1372 * If we switch to a better bookmarks file format (XBEL), we
1373 * should use mime info to get a better icon.
1375 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1376 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1377 impl->icon_size, 0, NULL);
1380 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1382 pixbuf = render_search_icon (impl);
1384 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1386 pixbuf = render_recent_icon (impl);
1389 gtk_list_store_set (impl->shortcuts_model, &iter,
1390 SHORTCUTS_COL_PIXBUF, pixbuf,
1394 g_object_unref (pixbuf);
1398 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1402 profile_end ("end", NULL);
1406 shortcuts_find_folder (GtkFileChooserDefault *impl,
1409 GtkTreeSelection *selection;
1413 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1415 g_assert (folder != NULL);
1416 pos = shortcut_find_position (impl, folder);
1419 gtk_tree_selection_unselect_all (selection);
1423 path = gtk_tree_path_new_from_indices (pos, -1);
1424 gtk_tree_selection_select_path (selection, path);
1425 gtk_tree_path_free (path);
1428 /* If a shortcut corresponds to the current folder, selects it */
1430 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1432 shortcuts_find_folder (impl, impl->current_folder);
1435 /* Removes the specified number of rows from the shortcuts list */
1437 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1443 path = gtk_tree_path_new_from_indices (start_row, -1);
1445 for (; n_rows; n_rows--)
1449 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1450 g_assert_not_reached ();
1452 shortcuts_free_row_data (impl, &iter);
1453 gtk_list_store_remove (impl->shortcuts_model, &iter);
1456 gtk_tree_path_free (path);
1460 shortcuts_update_count (GtkFileChooserDefault *impl,
1461 ShortcutsIndex type,
1466 case SHORTCUTS_HOME:
1468 impl->has_home = FALSE;
1470 impl->has_home = TRUE;
1473 case SHORTCUTS_DESKTOP:
1475 impl->has_desktop = FALSE;
1477 impl->has_desktop = TRUE;
1480 case SHORTCUTS_VOLUMES:
1481 impl->num_volumes += value;
1484 case SHORTCUTS_SHORTCUTS:
1485 impl->num_shortcuts += value;
1488 case SHORTCUTS_BOOKMARKS:
1489 impl->num_bookmarks += value;
1492 case SHORTCUTS_CURRENT_FOLDER:
1494 impl->shortcuts_current_folder_active = FALSE;
1496 impl->shortcuts_current_folder_active = TRUE;
1505 struct ShortcutsInsertRequest
1507 GtkFileChooserDefault *impl;
1511 GtkTreeRowReference *row_ref;
1512 ShortcutsIndex type;
1518 get_file_info_finished (GCancellable *cancellable,
1520 const GError *error,
1524 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1528 GCancellable *model_cancellable;
1529 struct ShortcutsInsertRequest *request = data;
1531 path = gtk_tree_row_reference_get_path (request->row_ref);
1533 /* Handle doesn't exist anymore in the model */
1536 pos = gtk_tree_path_get_indices (path)[0];
1537 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1539 gtk_tree_path_free (path);
1541 /* validate cancellable, else goto out */
1542 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1543 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1545 if (cancellable != model_cancellable)
1548 /* set the cancellable to NULL in the model (we unref later on) */
1549 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1550 SHORTCUTS_COL_CANCELLABLE, NULL,
1558 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1559 shortcuts_update_count (request->impl, request->type, -1);
1561 if (request->type == SHORTCUTS_HOME)
1565 home = g_file_new_for_path (g_get_home_dir ());
1566 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1567 g_object_unref (home);
1569 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1571 /* Remove the current folder separator */
1572 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1573 shortcuts_remove_rows (request->impl, separator_pos, 1);
1579 if (!request->label_copy)
1580 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1581 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1582 request->impl->icon_size);
1584 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1585 SHORTCUTS_COL_PIXBUF, pixbuf,
1586 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1587 SHORTCUTS_COL_NAME, request->label_copy,
1588 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1589 SHORTCUTS_COL_REMOVABLE, request->removable,
1592 if (request->impl->shortcuts_pane_filter_model)
1593 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1595 if (request->impl->shortcuts_combo_filter_model)
1596 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1598 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1599 request->impl->save_folder_combo != NULL)
1601 /* The current folder is updated via _activate_iter(), don't
1602 * have save_folder_combo_changed_cb() call _activate_iter()
1605 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1606 G_CALLBACK (save_folder_combo_changed_cb),
1609 if (request->impl->has_search)
1612 if (request->impl->has_recent)
1615 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1616 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1617 G_CALLBACK (save_folder_combo_changed_cb),
1622 g_object_unref (pixbuf);
1625 g_object_unref (request->impl);
1626 g_object_unref (request->file);
1627 gtk_tree_row_reference_free (request->row_ref);
1628 g_free (request->label_copy);
1631 g_object_unref (cancellable);
1634 /* FIXME: GtkFileSystem needs a function to split a remote path
1635 * into hostname and path components, or maybe just have a
1636 * gtk_file_system_path_get_display_name().
1638 * This function is also used in gtkfilechooserbutton.c
1641 _gtk_file_chooser_label_for_file (GFile *file)
1643 const gchar *path, *start, *end, *p;
1644 gchar *uri, *host, *label;
1646 uri = g_file_get_uri (file);
1648 start = strstr (uri, "://");
1652 path = strchr (start, '/');
1657 end = uri + strlen (uri);
1661 /* strip username */
1662 p = strchr (start, '@');
1666 p = strchr (start, ':');
1670 host = g_strndup (start, end - start);
1672 /* Translators: the first string is a path and the second string
1673 * is a hostname. Nautilus and the panel contain the same string
1676 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1682 label = g_strdup (uri);
1690 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1691 * inserts a volume. A position of -1 indicates the end of the tree.
1694 shortcuts_insert_file (GtkFileChooserDefault *impl,
1696 ShortcutType shortcut_type,
1697 GtkFileSystemVolume *volume,
1701 ShortcutsIndex type)
1704 GdkPixbuf *pixbuf = NULL;
1705 gpointer data = NULL;
1707 GtkIconTheme *icon_theme;
1709 profile_start ("start shortcut", NULL);
1711 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1714 label_copy = _gtk_file_system_volume_get_display_name (volume);
1715 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1716 impl->icon_size, NULL);
1718 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1720 if (g_file_is_native (file))
1722 struct ShortcutsInsertRequest *request;
1723 GCancellable *cancellable;
1726 request = g_new0 (struct ShortcutsInsertRequest, 1);
1727 request->impl = g_object_ref (impl);
1728 request->file = g_object_ref (file);
1729 request->name_only = TRUE;
1730 request->removable = removable;
1732 request->type = type;
1734 request->label_copy = g_strdup (label);
1737 gtk_list_store_append (impl->shortcuts_model, &iter);
1739 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1741 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1742 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1743 gtk_tree_path_free (p);
1745 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1746 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1747 get_file_info_finished, request);
1749 gtk_list_store_set (impl->shortcuts_model, &iter,
1750 SHORTCUTS_COL_DATA, g_object_ref (file),
1751 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1752 SHORTCUTS_COL_CANCELLABLE, cancellable,
1755 shortcuts_update_count (impl, type, 1);
1761 /* Don't call get_info for remote paths to avoid latency and
1764 data = g_object_ref (file);
1766 label_copy = g_strdup (label);
1768 label_copy = _gtk_file_chooser_label_for_file (file);
1770 /* If we switch to a better bookmarks file format (XBEL), we
1771 * should use mime info to get a better icon.
1773 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1774 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1775 impl->icon_size, 0, NULL);
1780 g_assert_not_reached ();
1786 gtk_list_store_append (impl->shortcuts_model, &iter);
1788 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1790 shortcuts_update_count (impl, type, 1);
1792 gtk_list_store_set (impl->shortcuts_model, &iter,
1793 SHORTCUTS_COL_PIXBUF, pixbuf,
1794 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1795 SHORTCUTS_COL_NAME, label_copy,
1796 SHORTCUTS_COL_DATA, data,
1797 SHORTCUTS_COL_TYPE, shortcut_type,
1798 SHORTCUTS_COL_REMOVABLE, removable,
1799 SHORTCUTS_COL_CANCELLABLE, NULL,
1802 if (impl->shortcuts_pane_filter_model)
1803 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1805 if (impl->shortcuts_combo_filter_model)
1806 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1808 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1810 /* The current folder is updated via _activate_iter(), don't
1811 * have save_folder_combo_changed_cb() call _activate_iter()
1814 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1816 if (impl->has_search)
1819 if (impl->has_recent)
1822 g_signal_handlers_block_by_func (impl->save_folder_combo,
1823 G_CALLBACK (save_folder_combo_changed_cb),
1826 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1827 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1828 G_CALLBACK (save_folder_combo_changed_cb),
1832 g_free (label_copy);
1835 g_object_unref (pixbuf);
1837 profile_end ("end", NULL);
1841 shortcuts_append_search (GtkFileChooserDefault *impl)
1846 pixbuf = render_search_icon (impl);
1848 gtk_list_store_append (impl->shortcuts_model, &iter);
1849 gtk_list_store_set (impl->shortcuts_model, &iter,
1850 SHORTCUTS_COL_PIXBUF, pixbuf,
1851 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1852 SHORTCUTS_COL_NAME, _("Search"),
1853 SHORTCUTS_COL_DATA, NULL,
1854 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1855 SHORTCUTS_COL_REMOVABLE, FALSE,
1859 g_object_unref (pixbuf);
1861 impl->has_search = TRUE;
1865 shortcuts_append_recent (GtkFileChooserDefault *impl)
1870 pixbuf = render_recent_icon (impl);
1872 gtk_list_store_append (impl->shortcuts_model, &iter);
1873 gtk_list_store_set (impl->shortcuts_model, &iter,
1874 SHORTCUTS_COL_PIXBUF, pixbuf,
1875 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1876 SHORTCUTS_COL_NAME, _("Recently Used"),
1877 SHORTCUTS_COL_DATA, NULL,
1878 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1879 SHORTCUTS_COL_REMOVABLE, FALSE,
1883 g_object_unref (pixbuf);
1885 impl->has_recent = TRUE;
1888 /* Appends an item for the user's home directory to the shortcuts model */
1890 shortcuts_append_home (GtkFileChooserDefault *impl)
1892 const char *home_path;
1895 profile_start ("start", NULL);
1897 home_path = g_get_home_dir ();
1898 if (home_path == NULL)
1900 profile_end ("end - no home directory!?", NULL);
1904 home = g_file_new_for_path (home_path);
1905 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1906 impl->has_home = TRUE;
1908 g_object_unref (home);
1910 profile_end ("end", NULL);
1913 /* Appends the ~/Desktop directory to the shortcuts model */
1915 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1920 profile_start ("start", NULL);
1922 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1923 /* "To disable a directory, point it to the homedir."
1924 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1926 if (!g_strcmp0 (name, g_get_home_dir ()))
1928 profile_end ("end", NULL);
1932 file = g_file_new_for_path (name);
1933 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1934 impl->has_desktop = TRUE;
1936 /* We do not actually pop up an error dialog if there is no desktop directory
1937 * because some people may really not want to have one.
1940 g_object_unref (file);
1942 profile_end ("end", NULL);
1945 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1947 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1954 profile_start ("start", NULL);
1956 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1959 for (; bookmarks; bookmarks = bookmarks->next)
1963 file = bookmarks->data;
1965 if (impl->local_only && !g_file_is_native (file))
1968 if (shortcut_find_position (impl, file) != -1)
1971 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1973 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1979 profile_end ("end", NULL);
1981 return num_inserted;
1984 /* Returns the index for the corresponding item in the shortcuts bar */
1986 shortcuts_get_index (GtkFileChooserDefault *impl,
1987 ShortcutsIndex where)
1993 if (where == SHORTCUTS_SEARCH)
1996 n += impl->has_search ? 1 : 0;
1998 if (where == SHORTCUTS_RECENT)
2001 n += impl->has_recent ? 1 : 0;
2003 if (where == SHORTCUTS_RECENT_SEPARATOR)
2006 n += impl->has_recent ? 1 : 0;
2008 if (where == SHORTCUTS_HOME)
2011 n += impl->has_home ? 1 : 0;
2013 if (where == SHORTCUTS_DESKTOP)
2016 n += impl->has_desktop ? 1 : 0;
2018 if (where == SHORTCUTS_VOLUMES)
2021 n += impl->num_volumes;
2023 if (where == SHORTCUTS_SHORTCUTS)
2026 n += impl->num_shortcuts;
2028 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2031 /* If there are no bookmarks there won't be a separator */
2032 n += (impl->num_bookmarks > 0) ? 1 : 0;
2034 if (where == SHORTCUTS_BOOKMARKS)
2037 n += impl->num_bookmarks;
2039 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2044 if (where == SHORTCUTS_CURRENT_FOLDER)
2047 g_assert_not_reached ();
2054 /* Adds all the file system volumes to the shortcuts model */
2056 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2061 gboolean old_changing_folders;
2063 profile_start ("start", NULL);
2065 old_changing_folders = impl->changing_folder;
2066 impl->changing_folder = TRUE;
2068 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2069 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2070 impl->num_volumes = 0;
2072 list = _gtk_file_system_list_volumes (impl->file_system);
2076 for (l = list; l; l = l->next)
2078 GtkFileSystemVolume *volume;
2082 if (impl->local_only)
2084 if (_gtk_file_system_volume_is_mounted (volume))
2087 gboolean base_is_native = TRUE;
2089 base_file = _gtk_file_system_volume_get_root (volume);
2090 if (base_file != NULL)
2092 base_is_native = g_file_is_native (base_file);
2093 g_object_unref (base_file);
2096 if (!base_is_native)
2101 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2105 impl->num_volumes = n;
2106 g_slist_free (list);
2108 if (impl->shortcuts_pane_filter_model)
2109 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2111 if (impl->shortcuts_combo_filter_model)
2112 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2114 impl->changing_folder = old_changing_folders;
2116 profile_end ("end", NULL);
2119 /* Inserts a separator node in the shortcuts list */
2121 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2122 ShortcutsIndex where)
2126 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2127 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2128 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2130 gtk_list_store_insert (impl->shortcuts_model, &iter,
2131 shortcuts_get_index (impl, where));
2132 gtk_list_store_set (impl->shortcuts_model, &iter,
2133 SHORTCUTS_COL_PIXBUF, NULL,
2134 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2135 SHORTCUTS_COL_NAME, NULL,
2136 SHORTCUTS_COL_DATA, NULL,
2137 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2141 /* Updates the list of bookmarks */
2143 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2146 gboolean old_changing_folders;
2148 GFile *list_selected = NULL;
2149 GFile *combo_selected = NULL;
2150 ShortcutType shortcut_type;
2153 profile_start ("start", NULL);
2155 old_changing_folders = impl->changing_folder;
2156 impl->changing_folder = TRUE;
2158 if (shortcuts_get_selected (impl, &iter))
2160 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2162 SHORTCUTS_COL_DATA, &col_data,
2163 SHORTCUTS_COL_TYPE, &shortcut_type,
2166 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2167 list_selected = g_object_ref (col_data);
2170 if (impl->save_folder_combo &&
2171 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2174 GtkTreeIter child_iter;
2176 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2179 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2181 SHORTCUTS_COL_DATA, &col_data,
2182 SHORTCUTS_COL_TYPE, &shortcut_type,
2185 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2186 combo_selected = g_object_ref (col_data);
2189 if (impl->num_bookmarks > 0)
2190 shortcuts_remove_rows (impl,
2191 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2192 impl->num_bookmarks + 1);
2194 impl->num_bookmarks = 0;
2195 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2197 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2198 shortcuts_append_bookmarks (impl, bookmarks);
2199 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2200 g_slist_free (bookmarks);
2202 if (impl->num_bookmarks == 0)
2203 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2205 if (impl->shortcuts_pane_filter_model)
2206 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2208 if (impl->shortcuts_combo_filter_model)
2209 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2213 shortcuts_find_folder (impl, list_selected);
2214 g_object_unref (list_selected);
2221 pos = shortcut_find_position (impl, combo_selected);
2224 if (impl->has_search)
2227 if (impl->has_recent)
2230 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2233 g_object_unref (combo_selected);
2236 impl->changing_folder = old_changing_folders;
2238 profile_end ("end", NULL);
2241 /* Appends a separator and a row to the shortcuts list for the current folder */
2243 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2248 g_assert (!impl->shortcuts_current_folder_active);
2252 g_assert (impl->current_folder != NULL);
2254 pos = shortcut_find_position (impl, impl->current_folder);
2257 GtkFileSystemVolume *volume;
2262 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2266 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2268 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2270 base_file = _gtk_file_system_volume_get_root (volume);
2274 if (base_file && g_file_equal (base_file, impl->current_folder))
2275 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2277 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2280 g_object_unref (base_file);
2282 else if (impl->save_folder_combo != NULL)
2284 if (impl->has_search)
2287 if (impl->has_recent)
2288 pos -= 2; /* + separator */
2290 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2294 /* Updates the current folder row in the shortcuts model */
2296 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2300 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2302 if (impl->shortcuts_current_folder_active)
2304 shortcuts_remove_rows (impl, pos, 2);
2305 impl->shortcuts_current_folder_active = FALSE;
2308 shortcuts_add_current_folder (impl);
2311 /* Filter function used for the shortcuts filter model */
2313 shortcuts_pane_filter_cb (GtkTreeModel *model,
2317 GtkFileChooserDefault *impl;
2321 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2323 path = gtk_tree_model_get_path (model, iter);
2327 pos = *gtk_tree_path_get_indices (path);
2328 gtk_tree_path_free (path);
2330 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2333 /* Creates the list model for shortcuts */
2335 shortcuts_model_create (GtkFileChooserDefault *impl)
2337 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2338 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2339 GDK_TYPE_PIXBUF, /* pixbuf */
2340 G_TYPE_STRING, /* name */
2341 G_TYPE_POINTER, /* path or volume */
2342 G_TYPE_INT, /* ShortcutType */
2343 G_TYPE_BOOLEAN, /* removable */
2344 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2345 G_TYPE_POINTER); /* GCancellable */
2347 if (search_is_possible (impl))
2349 shortcuts_append_search (impl);
2352 if (impl->recent_manager)
2354 shortcuts_append_recent (impl);
2355 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2358 if (impl->file_system)
2360 shortcuts_append_home (impl);
2361 shortcuts_append_desktop (impl);
2362 shortcuts_add_volumes (impl);
2365 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2366 GTK_TREE_MODEL (impl->shortcuts_model),
2369 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2370 shortcuts_pane_filter_cb,
2375 /* Callback used when the "New Folder" button is clicked */
2377 new_folder_button_clicked (GtkButton *button,
2378 GtkFileChooserDefault *impl)
2383 if (!impl->browse_files_model)
2384 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2386 /* Prevent button from being clicked twice */
2387 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2389 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2391 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2392 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2393 path, impl->list_name_column,
2396 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2397 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2399 impl->list_name_column,
2402 gtk_tree_path_free (path);
2405 /* Idle handler for creating a new folder after editing its name cell, or for
2406 * canceling the editing.
2409 edited_idle_cb (GtkFileChooserDefault *impl)
2411 GDK_THREADS_ENTER ();
2413 g_source_destroy (impl->edited_idle);
2414 impl->edited_idle = NULL;
2416 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2417 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2419 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2421 if (impl->edited_new_text /* not cancelled? */
2422 && (strlen (impl->edited_new_text) != 0)
2423 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2425 GError *error = NULL;
2428 file = g_file_get_child_for_display_name (impl->current_folder,
2429 impl->edited_new_text,
2433 GError *error = NULL;
2435 if (g_file_make_directory (file, NULL, &error))
2436 change_folder_and_display_error (impl, file, FALSE);
2438 error_creating_folder_dialog (impl, file, error);
2440 g_object_unref (file);
2443 error_creating_folder_dialog (impl, file, error);
2445 g_free (impl->edited_new_text);
2446 impl->edited_new_text = NULL;
2449 GDK_THREADS_LEAVE ();
2455 queue_edited_idle (GtkFileChooserDefault *impl,
2456 const gchar *new_text)
2458 /* We create the folder in an idle handler so that we don't modify the tree
2462 if (!impl->edited_idle)
2464 impl->edited_idle = g_idle_source_new ();
2465 g_source_set_closure (impl->edited_idle,
2466 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2468 g_source_attach (impl->edited_idle, NULL);
2471 g_free (impl->edited_new_text);
2472 impl->edited_new_text = g_strdup (new_text);
2475 /* Callback used from the text cell renderer when the new folder is named */
2477 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2479 const gchar *new_text,
2480 GtkFileChooserDefault *impl)
2482 /* work around bug #154921 */
2483 g_object_set (cell_renderer_text,
2484 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2485 queue_edited_idle (impl, new_text);
2488 /* Callback used from the text cell renderer when the new folder edition gets
2492 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2493 GtkFileChooserDefault *impl)
2495 /* work around bug #154921 */
2496 g_object_set (cell_renderer_text,
2497 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2498 queue_edited_idle (impl, NULL);
2501 /* Creates the widgets for the filter combo box */
2503 filter_create (GtkFileChooserDefault *impl)
2505 impl->filter_combo = gtk_combo_box_new_text ();
2506 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2508 g_signal_connect (impl->filter_combo, "changed",
2509 G_CALLBACK (filter_combo_changed), impl);
2511 gtk_widget_set_tooltip_text (impl->filter_combo,
2512 _("Select which types of files are shown"));
2514 return impl->filter_combo;
2518 button_new (GtkFileChooserDefault *impl,
2520 const char *stock_id,
2528 button = gtk_button_new_with_mnemonic (text);
2529 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2530 gtk_button_set_image (GTK_BUTTON (button), image);
2532 gtk_widget_set_sensitive (button, sensitive);
2533 g_signal_connect (button, "clicked", callback, impl);
2536 gtk_widget_show (button);
2541 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2543 shortcut_find_position (GtkFileChooserDefault *impl,
2548 int current_folder_separator_idx;
2550 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2553 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2556 /* FIXME: is this still needed? */
2557 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2561 for (i = 0; i < current_folder_separator_idx; i++)
2564 ShortcutType shortcut_type;
2566 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2567 SHORTCUTS_COL_DATA, &col_data,
2568 SHORTCUTS_COL_TYPE, &shortcut_type,
2573 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2575 GtkFileSystemVolume *volume;
2580 base_file = _gtk_file_system_volume_get_root (volume);
2582 exists = base_file && g_file_equal (file, base_file);
2585 g_object_unref (base_file);
2590 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2594 model_file = col_data;
2596 if (model_file && g_file_equal (model_file, file))
2601 if (i < current_folder_separator_idx - 1)
2603 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2604 g_assert_not_reached ();
2611 /* Tries to add a bookmark from a path name */
2613 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2619 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2621 if (shortcut_find_position (impl, file) != -1)
2625 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2627 error_adding_bookmark_dialog (impl, file, error);
2635 add_bookmark_foreach_cb (GtkTreeModel *model,
2640 GtkFileChooserDefault *impl;
2641 GtkFileSystemModel *fs_model;
2642 GtkTreeIter child_iter;
2645 impl = (GtkFileChooserDefault *) data;
2647 switch (impl->operation_mode)
2649 case OPERATION_MODE_BROWSE:
2650 fs_model = impl->browse_files_model;
2651 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2652 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2655 case OPERATION_MODE_SEARCH:
2656 search_get_valid_child_iter (impl, &child_iter, iter);
2657 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2658 SEARCH_MODEL_COL_FILE, &file,
2662 case OPERATION_MODE_RECENT:
2663 recent_get_valid_child_iter (impl, &child_iter, iter);
2664 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2665 RECENT_MODEL_COL_FILE, &file,
2670 shortcuts_add_bookmark_from_file (impl, file, -1);
2673 /* Adds a bookmark from the currently selected item in the file list */
2675 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2677 GtkTreeSelection *selection;
2679 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2681 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2682 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2684 gtk_tree_selection_selected_foreach (selection,
2685 add_bookmark_foreach_cb,
2689 /* Callback used when the "Add bookmark" button is clicked */
2691 add_bookmark_button_clicked_cb (GtkButton *button,
2692 GtkFileChooserDefault *impl)
2694 bookmarks_add_selected_folder (impl);
2697 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2698 * returns FALSE if no shortcut is selected.
2701 shortcuts_get_selected (GtkFileChooserDefault *impl,
2704 GtkTreeSelection *selection;
2705 GtkTreeIter parent_iter;
2707 if (!impl->browse_shortcuts_tree_view)
2710 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2712 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2715 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2721 /* Removes the selected bookmarks */
2723 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2731 if (!shortcuts_get_selected (impl, &iter))
2734 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2735 SHORTCUTS_COL_DATA, &col_data,
2736 SHORTCUTS_COL_REMOVABLE, &removable,
2742 g_assert (col_data != NULL);
2747 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2748 error_removing_bookmark_dialog (impl, file, error);
2751 /* Callback used when the "Remove bookmark" button is clicked */
2753 remove_bookmark_button_clicked_cb (GtkButton *button,
2754 GtkFileChooserDefault *impl)
2756 remove_selected_bookmarks (impl);
2759 struct selection_check_closure {
2760 GtkFileChooserDefault *impl;
2763 gboolean all_folders;
2766 /* Used from gtk_tree_selection_selected_foreach() */
2768 selection_check_foreach_cb (GtkTreeModel *model,
2773 struct selection_check_closure *closure;
2774 GtkTreeIter child_iter;
2779 closure->num_selected++;
2781 switch (closure->impl->operation_mode)
2783 case OPERATION_MODE_BROWSE:
2784 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2785 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2786 is_folder = info ? (_gtk_file_info_consider_as_directory (info)) : FALSE;
2789 case OPERATION_MODE_SEARCH:
2790 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2791 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2792 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2796 case OPERATION_MODE_RECENT:
2797 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2798 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2799 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2804 closure->all_folders = closure->all_folders && is_folder;
2805 closure->all_files = closure->all_files && !is_folder;
2808 /* Checks whether the selected items in the file list are all files or all folders */
2810 selection_check (GtkFileChooserDefault *impl,
2812 gboolean *all_files,
2813 gboolean *all_folders)
2815 struct selection_check_closure closure;
2816 GtkTreeSelection *selection;
2818 closure.impl = impl;
2819 closure.num_selected = 0;
2820 closure.all_files = TRUE;
2821 closure.all_folders = TRUE;
2823 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2824 gtk_tree_selection_selected_foreach (selection,
2825 selection_check_foreach_cb,
2828 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2831 *num_selected = closure.num_selected;
2834 *all_files = closure.all_files;
2837 *all_folders = closure.all_folders;
2840 struct get_selected_file_closure {
2841 GtkFileChooserDefault *impl;
2846 get_selected_file_foreach_cb (GtkTreeModel *model,
2851 struct get_selected_file_closure *closure;
2852 GtkTreeIter child_iter;
2856 switch (closure->impl->operation_mode)
2858 case OPERATION_MODE_BROWSE:
2859 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2860 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2863 case OPERATION_MODE_SEARCH:
2864 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2865 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2866 SEARCH_MODEL_COL_FILE, &closure->file,
2870 case OPERATION_MODE_RECENT:
2871 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2872 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2873 RECENT_MODEL_COL_FILE, &closure->file,
2879 /* Returns a selected path from the file list */
2881 get_selected_file (GtkFileChooserDefault *impl)
2883 struct get_selected_file_closure closure;
2884 GtkTreeSelection *selection;
2886 closure.impl = impl;
2887 closure.file = NULL;
2889 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2890 gtk_tree_selection_selected_foreach (selection,
2891 get_selected_file_foreach_cb,
2894 return closure.file;
2898 GtkFileChooserDefault *impl;
2900 } UpdateTooltipData;
2903 update_tooltip (GtkTreeModel *model,
2908 UpdateTooltipData *udata = data;
2909 GtkTreeIter child_iter;
2912 if (udata->tip == NULL)
2914 const gchar *display_name;
2916 switch (udata->impl->operation_mode)
2918 case OPERATION_MODE_BROWSE:
2919 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2922 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2923 display_name = g_file_info_get_display_name (info);
2926 case OPERATION_MODE_SEARCH:
2927 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2928 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2929 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2933 case OPERATION_MODE_RECENT:
2934 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2935 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2936 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2941 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2947 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2948 * if there are no selected items *and* the current folder is not in the
2949 * bookmarks list. De-sensitize the button otherwise.
2952 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2955 gboolean all_folders;
2959 selection_check (impl, &num_selected, NULL, &all_folders);
2961 if (num_selected == 0)
2962 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2963 else if (num_selected == 1)
2967 file = get_selected_file (impl);
2968 active = all_folders && (shortcut_find_position (impl, file) == -1);
2971 active = all_folders;
2973 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2975 if (impl->browse_files_popup_menu_add_shortcut_item)
2976 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2977 (num_selected == 0) ? FALSE : active);
2981 if (num_selected == 0)
2982 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2983 else if (num_selected > 1)
2984 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2987 GtkTreeSelection *selection;
2988 UpdateTooltipData data;
2990 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2993 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2997 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
3002 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
3003 * bookmark row is selected in the shortcuts tree.
3006 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
3009 gboolean removable = FALSE;
3013 if (shortcuts_get_selected (impl, &iter))
3015 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3016 SHORTCUTS_COL_REMOVABLE, &removable,
3017 SHORTCUTS_COL_NAME, &name,
3019 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3022 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3024 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
3026 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3030 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3031 _("Remove the selected bookmark"));
3036 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3039 gboolean removable = FALSE;
3041 if (impl->browse_shortcuts_popup_menu == NULL)
3044 if (shortcuts_get_selected (impl, &iter))
3045 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3046 SHORTCUTS_COL_REMOVABLE, &removable,
3049 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3050 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3053 /* GtkWidget::drag-begin handler for the shortcuts list. */
3055 shortcuts_drag_begin_cb (GtkWidget *widget,
3056 GdkDragContext *context,
3057 GtkFileChooserDefault *impl)
3060 impl->shortcuts_drag_context = g_object_ref (context);
3065 /* Removes the idle handler for outside drags */
3067 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3069 if (!impl->shortcuts_drag_outside_idle)
3072 g_source_destroy (impl->shortcuts_drag_outside_idle);
3073 impl->shortcuts_drag_outside_idle = NULL;
3077 /* GtkWidget::drag-end handler for the shortcuts list. */
3079 shortcuts_drag_end_cb (GtkWidget *widget,
3080 GdkDragContext *context,
3081 GtkFileChooserDefault *impl)
3084 g_object_unref (impl->shortcuts_drag_context);
3086 shortcuts_cancel_drag_outside_idle (impl);
3088 if (!impl->shortcuts_drag_outside)
3091 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3093 impl->shortcuts_drag_outside = FALSE;
3097 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3099 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3100 GdkDragContext *context,
3101 GtkFileChooserDefault *impl)
3103 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3107 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3111 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3114 GtkTreeView *tree_view;
3117 GdkPixmap *row_pixmap;
3122 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3124 /* Find the selected path and get its drag pixmap */
3126 if (!shortcuts_get_selected (impl, &iter))
3127 g_assert_not_reached ();
3129 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3131 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3132 gtk_tree_path_free (path);
3141 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3147 GdkPixmap *composite;
3148 int row_pixmap_width, row_pixmap_height;
3149 int pixbuf_width, pixbuf_height;
3150 int composite_width, composite_height;
3151 int pixbuf_x, pixbuf_y;
3152 GdkGC *gc, *mask_gc;
3154 GdkBitmap *pixbuf_mask;
3156 /* Create pixmap and mask for composite image */
3158 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3159 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3160 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3162 composite_width = MAX (row_pixmap_width, pixbuf_width);
3163 composite_height = MAX (row_pixmap_height, pixbuf_height);
3165 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3167 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3170 pixbuf_x = composite_width - pixbuf_width;
3172 pixbuf_y = (composite_height - pixbuf_height) / 2;
3174 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3175 gc = gdk_gc_new (composite);
3177 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3178 mask_gc = gdk_gc_new (mask);
3180 gdk_gc_set_foreground (mask_gc, &color);
3181 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3184 color.green = 0xffff;
3185 color.blue = 0xffff;
3186 gdk_gc_set_rgb_fg_color (gc, &color);
3187 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3189 /* Composite the row pixmap and the pixbuf */
3191 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3193 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3194 NULL, &pixbuf_mask, 128);
3195 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3198 pixbuf_width, pixbuf_height);
3199 g_object_unref (pixbuf_mask);
3201 gdk_draw_drawable (composite, gc, row_pixmap,
3204 row_pixmap_width, row_pixmap_height);
3206 gdk_gc_set_foreground (mask_gc, &color);
3207 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3209 gdk_draw_pixbuf (composite, gc, pixbuf,
3212 pixbuf_width, pixbuf_height,
3216 g_object_unref (pixbuf);
3217 g_object_unref (row_pixmap);
3219 row_pixmap = composite;
3223 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3225 gtk_tree_view_get_path_at_pos (tree_view,
3226 tree_view->priv->press_start_x,
3227 tree_view->priv->press_start_y,
3233 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3234 gdk_drawable_get_colormap (row_pixmap),
3237 tree_view->priv->press_start_x + 1,
3238 row_pixmap_y + cell_y + 1);
3240 g_object_unref (row_pixmap);
3242 g_object_unref (mask);
3245 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3246 * handler so that we can tell apart the drag_leave event that comes right
3247 * before a drag_drop, from a normal drag_leave. We don't want to set the
3248 * cursor nor the flag in the latter case.
3251 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3253 GDK_THREADS_ENTER ();
3255 shortcuts_drag_set_delete_cursor (impl, TRUE);
3256 impl->shortcuts_drag_outside = TRUE;
3258 shortcuts_cancel_drag_outside_idle (impl);
3260 GDK_THREADS_LEAVE ();
3266 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3270 shortcuts_drag_leave_cb (GtkWidget *widget,
3271 GdkDragContext *context,
3273 GtkFileChooserDefault *impl)
3276 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3278 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3279 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3280 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3282 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3286 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3288 GTK_TREE_VIEW_DROP_BEFORE);
3290 g_signal_stop_emission_by_name (widget, "drag-leave");
3293 /* Computes the appropriate row and position for dropping */
3295 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3299 GtkTreeViewDropPosition *pos)
3301 GtkTreeView *tree_view;
3302 GtkTreeViewColumn *column;
3306 int bookmarks_index;
3308 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3310 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3312 if (!gtk_tree_view_get_path_at_pos (tree_view,
3314 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3320 row = bookmarks_index + impl->num_bookmarks - 1;
3321 *path = gtk_tree_path_new_from_indices (row, -1);
3322 *pos = GTK_TREE_VIEW_DROP_AFTER;
3326 row = *gtk_tree_path_get_indices (*path);
3327 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3328 gtk_tree_path_free (*path);
3330 if (row < bookmarks_index)
3332 row = bookmarks_index;
3333 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3335 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3337 row = bookmarks_index + impl->num_bookmarks - 1;
3338 *pos = GTK_TREE_VIEW_DROP_AFTER;
3342 if (cell_y < cell.height / 2)
3343 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3345 *pos = GTK_TREE_VIEW_DROP_AFTER;
3348 *path = gtk_tree_path_new_from_indices (row, -1);
3351 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3352 * implement the destination side of DnD by hand, due to limitations in
3353 * GtkTreeView's DnD API.
3356 shortcuts_drag_motion_cb (GtkWidget *widget,
3357 GdkDragContext *context,
3361 GtkFileChooserDefault *impl)
3364 GtkTreeViewDropPosition pos;
3365 GdkDragAction action;
3368 if (gtk_drag_get_source_widget (context) == widget)
3370 shortcuts_cancel_drag_outside_idle (impl);
3372 if (impl->shortcuts_drag_outside)
3374 shortcuts_drag_set_delete_cursor (impl, FALSE);
3375 impl->shortcuts_drag_outside = FALSE;
3380 if (context->suggested_action == GDK_ACTION_COPY ||
3381 (context->actions & GDK_ACTION_COPY) != 0)
3382 action = GDK_ACTION_COPY;
3383 else if (context->suggested_action == GDK_ACTION_MOVE ||
3384 (context->actions & GDK_ACTION_MOVE) != 0)
3385 action = GDK_ACTION_MOVE;
3392 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3393 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3394 gtk_tree_path_free (path);
3398 g_signal_stop_emission_by_name (widget, "drag-motion");
3402 gdk_drag_status (context, action, time_);
3409 /* GtkWidget::drag-drop handler for the shortcuts list. */
3411 shortcuts_drag_drop_cb (GtkWidget *widget,
3412 GdkDragContext *context,
3416 GtkFileChooserDefault *impl)
3419 shortcuts_cancel_drag_outside_idle (impl);
3422 g_signal_stop_emission_by_name (widget, "drag-drop");
3426 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3428 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3429 GtkSelectionData *selection_data,
3435 uris = gtk_selection_data_get_uris (selection_data);
3439 for (i = 0; uris[i]; i++)
3445 file = g_file_new_for_uri (uri);
3447 if (shortcuts_add_bookmark_from_file (impl, file, position))
3450 g_object_unref (file);
3456 /* Reorders the selected bookmark to the specified position */
3458 shortcuts_reorder (GtkFileChooserDefault *impl,
3463 ShortcutType shortcut_type;
3466 int bookmarks_index;
3471 /* Get the selected path */
3473 if (!shortcuts_get_selected (impl, &iter))
3474 g_assert_not_reached ();
3476 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3477 old_position = *gtk_tree_path_get_indices (path);
3478 gtk_tree_path_free (path);
3480 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3481 old_position -= bookmarks_index;
3482 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3484 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3485 SHORTCUTS_COL_NAME, &name,
3486 SHORTCUTS_COL_DATA, &col_data,
3487 SHORTCUTS_COL_TYPE, &shortcut_type,
3489 g_assert (col_data != NULL);
3490 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3493 g_object_ref (file); /* removal below will free file, so we need a new ref */
3495 /* Remove the path from the old position and insert it in the new one */
3497 if (new_position > old_position)
3500 if (old_position == new_position)
3504 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3506 shortcuts_add_bookmark_from_file (impl, file, new_position);
3507 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3510 error_adding_bookmark_dialog (impl, file, error);
3514 g_object_unref (file);
3517 /* Callback used when we get the drag data for the bookmarks list. We add the
3518 * received URIs as bookmarks if they are folders.
3521 shortcuts_drag_data_received_cb (GtkWidget *widget,
3522 GdkDragContext *context,
3525 GtkSelectionData *selection_data,
3530 GtkFileChooserDefault *impl;
3531 GtkTreePath *tree_path;
3532 GtkTreeViewDropPosition tree_pos;
3534 int bookmarks_index;
3536 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3538 /* Compute position */
3540 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3542 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3543 position = *gtk_tree_path_get_indices (tree_path);
3544 gtk_tree_path_free (tree_path);
3546 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3549 g_assert (position >= bookmarks_index);
3550 position -= bookmarks_index;
3552 if (gtk_targets_include_uri (&selection_data->target, 1))
3553 shortcuts_drop_uris (impl, selection_data, position);
3554 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3555 shortcuts_reorder (impl, position);
3557 g_signal_stop_emission_by_name (widget, "drag-data-received");
3560 /* Callback used to display a tooltip in the shortcuts tree */
3562 shortcuts_query_tooltip_cb (GtkWidget *widget,
3565 gboolean keyboard_mode,
3566 GtkTooltip *tooltip,
3567 GtkFileChooserDefault *impl)
3569 GtkTreeModel *model;
3572 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3580 ShortcutType shortcut_type;
3582 gtk_tree_model_get (model, &iter,
3583 SHORTCUTS_COL_DATA, &col_data,
3584 SHORTCUTS_COL_TYPE, &shortcut_type,
3587 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3589 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3593 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3598 file = G_FILE (col_data);
3599 parse_name = g_file_get_parse_name (file);
3601 gtk_tooltip_set_text (tooltip, parse_name);
3603 g_free (parse_name);
3607 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3611 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3621 /* Callback used when the selection in the shortcuts tree changes */
3623 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3624 GtkFileChooserDefault *impl)
3627 GtkTreeIter child_iter;
3629 bookmarks_check_remove_sensitivity (impl);
3630 shortcuts_check_popup_sensitivity (impl);
3632 if (impl->changing_folder)
3635 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3637 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3640 shortcuts_activate_iter (impl, &child_iter);
3645 shortcuts_row_separator_func (GtkTreeModel *model,
3649 ShortcutType shortcut_type;
3651 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3653 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3657 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3659 GtkFileChooserDefault *impl)
3663 /* don't screw up focus switching with Tab */
3664 if (event->keyval == GDK_Tab
3665 || event->keyval == GDK_KP_Tab
3666 || event->keyval == GDK_ISO_Left_Tab
3667 || event->length < 1)
3670 if (impl->location_entry)
3671 entry = impl->location_entry;
3672 else if (impl->search_entry)
3673 entry = impl->search_entry;
3679 gtk_widget_grab_focus (entry);
3680 return gtk_widget_event (entry, (GdkEvent *) event);
3686 /* Callback used when the file list's popup menu is detached */
3688 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3691 GtkFileChooserDefault *impl;
3693 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3694 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3696 impl->browse_shortcuts_popup_menu = NULL;
3697 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3698 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3702 remove_shortcut_cb (GtkMenuItem *item,
3703 GtkFileChooserDefault *impl)
3705 remove_selected_bookmarks (impl);
3708 /* Rename the selected bookmark */
3710 rename_selected_bookmark (GtkFileChooserDefault *impl)
3714 GtkTreeViewColumn *column;
3715 GtkCellRenderer *cell;
3718 if (shortcuts_get_selected (impl, &iter))
3720 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3721 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3722 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3723 cell = g_list_nth_data (renderers, 1);
3724 g_list_free (renderers);
3725 g_object_set (cell, "editable", TRUE, NULL);
3726 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3727 path, column, cell, TRUE);
3728 gtk_tree_path_free (path);
3733 rename_shortcut_cb (GtkMenuItem *item,
3734 GtkFileChooserDefault *impl)
3736 rename_selected_bookmark (impl);
3739 /* Constructs the popup menu for the file list if needed */
3741 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3745 if (impl->browse_shortcuts_popup_menu)
3748 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3749 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3750 impl->browse_shortcuts_tree_view,
3751 shortcuts_popup_menu_detach_cb);
3753 item = gtk_image_menu_item_new_with_label (_("Remove"));
3754 impl->browse_shortcuts_popup_menu_remove_item = item;
3755 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3756 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3757 g_signal_connect (item, "activate",
3758 G_CALLBACK (remove_shortcut_cb), impl);
3759 gtk_widget_show (item);
3760 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3762 item = gtk_menu_item_new_with_label (_("Rename..."));
3763 impl->browse_shortcuts_popup_menu_rename_item = item;
3764 g_signal_connect (item, "activate",
3765 G_CALLBACK (rename_shortcut_cb), impl);
3766 gtk_widget_show (item);
3767 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3771 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3773 shortcuts_build_popup_menu (impl);
3774 shortcuts_check_popup_sensitivity (impl);
3778 popup_position_func (GtkMenu *menu,
3782 gpointer user_data);
3785 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3786 GdkEventButton *event)
3788 shortcuts_update_popup_menu (impl);
3790 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3791 NULL, NULL, NULL, NULL,
3792 event->button, event->time);
3795 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3797 popup_position_func, impl->browse_shortcuts_tree_view,
3798 0, GDK_CURRENT_TIME);
3799 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3804 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3806 shortcuts_popup_menu_cb (GtkWidget *widget,
3807 GtkFileChooserDefault *impl)
3809 shortcuts_popup_menu (impl, NULL);
3813 /* Callback used when a button is pressed on the shortcuts list.
3814 * We trap button 3 to bring up a popup menu.
3817 shortcuts_button_press_event_cb (GtkWidget *widget,
3818 GdkEventButton *event,
3819 GtkFileChooserDefault *impl)
3821 static gboolean in_press = FALSE;
3827 if (event->button != 3)
3831 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3837 shortcuts_popup_menu (impl, event);
3842 shortcuts_edited (GtkCellRenderer *cell,
3845 GtkFileChooserDefault *impl)
3851 g_object_set (cell, "editable", FALSE, NULL);
3853 path = gtk_tree_path_new_from_string (path_string);
3854 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3855 g_assert_not_reached ();
3857 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3858 SHORTCUTS_COL_DATA, &shortcut,
3860 gtk_tree_path_free (path);
3862 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3866 shortcuts_editing_canceled (GtkCellRenderer *cell,
3867 GtkFileChooserDefault *impl)
3869 g_object_set (cell, "editable", FALSE, NULL);
3872 /* Creates the widgets for the shortcuts and bookmarks tree */
3874 shortcuts_list_create (GtkFileChooserDefault *impl)
3877 GtkTreeSelection *selection;
3878 GtkTreeViewColumn *column;
3879 GtkCellRenderer *renderer;
3881 /* Target types for dragging a row to/from the shortcuts list */
3882 const GtkTargetEntry tree_model_row_targets[] = {
3883 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3886 /* Scrolled window */
3888 swin = gtk_scrolled_window_new (NULL, NULL);
3889 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3890 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3891 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3893 gtk_widget_show (swin);
3897 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3898 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3899 #ifdef PROFILE_FILE_CHOOSER
3900 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3903 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3905 * 1. user brings up a SAVE dialog
3906 * 2. user clicks on a shortcut in the shortcuts pane
3907 * 3. user starts typing a filename
3909 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3910 * support interactive search. However, we'd rather focus the location entry
3911 * so that the user can type *there*.
3913 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3914 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3915 * if the user starts typing while the focus is in the shortcuts pane.
3917 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3918 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3920 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3921 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3922 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3923 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3924 /* Accessible object name for the file chooser's shortcuts pane */
3925 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3927 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3929 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3931 tree_model_row_targets,
3932 G_N_ELEMENTS (tree_model_row_targets),
3935 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3936 GTK_DEST_DEFAULT_ALL,
3937 tree_model_row_targets,
3938 G_N_ELEMENTS (tree_model_row_targets),
3939 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3940 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3942 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3943 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3944 gtk_tree_selection_set_select_function (selection,
3945 shortcuts_select_func,
3948 g_signal_connect (selection, "changed",
3949 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3951 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3952 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3954 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3955 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3956 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3957 G_CALLBACK (shortcuts_drag_end_cb), impl);
3958 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3959 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3961 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3962 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3963 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3964 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3965 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3966 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3967 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3968 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3970 /* Support tooltips */
3971 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3972 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3973 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3975 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3976 gtk_widget_show (impl->browse_shortcuts_tree_view);
3980 column = gtk_tree_view_column_new ();
3981 /* Column header for the file chooser's shortcuts pane */
3982 gtk_tree_view_column_set_title (column, _("_Places"));
3984 renderer = gtk_cell_renderer_pixbuf_new ();
3985 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3986 gtk_tree_view_column_set_attributes (column, renderer,
3987 "pixbuf", SHORTCUTS_COL_PIXBUF,
3988 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3991 renderer = gtk_cell_renderer_text_new ();
3992 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3993 g_signal_connect (renderer, "edited",
3994 G_CALLBACK (shortcuts_edited), impl);
3995 g_signal_connect (renderer, "editing-canceled",
3996 G_CALLBACK (shortcuts_editing_canceled), impl);
3997 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3998 gtk_tree_view_column_set_attributes (column, renderer,
3999 "text", SHORTCUTS_COL_NAME,
4002 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
4003 shortcuts_row_separator_func,
4006 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
4011 /* Creates the widgets for the shortcuts/bookmarks pane */
4013 shortcuts_pane_create (GtkFileChooserDefault *impl,
4014 GtkSizeGroup *size_group)
4020 vbox = gtk_vbox_new (FALSE, 6);
4021 gtk_widget_show (vbox);
4023 /* Shortcuts tree */
4025 widget = shortcuts_list_create (impl);
4026 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
4028 /* Box for buttons */
4030 hbox = gtk_hbox_new (TRUE, 6);
4031 gtk_size_group_add_widget (size_group, hbox);
4032 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4033 gtk_widget_show (hbox);
4035 /* Add bookmark button */
4037 impl->browse_shortcuts_add_button = button_new (impl,
4042 G_CALLBACK (add_bookmark_button_clicked_cb));
4043 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4044 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4045 _("Add the selected folder to the Bookmarks"));
4047 /* Remove bookmark button */
4049 impl->browse_shortcuts_remove_button = button_new (impl,
4054 G_CALLBACK (remove_bookmark_button_clicked_cb));
4055 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4056 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4057 _("Remove the selected bookmark"));
4063 key_is_left_or_right (GdkEventKey *event)
4067 modifiers = gtk_accelerator_get_default_mod_mask ();
4069 return ((event->keyval == GDK_Right
4070 || event->keyval == GDK_KP_Right
4071 || event->keyval == GDK_Left
4072 || event->keyval == GDK_KP_Left)
4073 && (event->state & modifiers) == 0);
4076 /* Handles key press events on the file list, so that we can trap Enter to
4077 * activate the default button on our own. Also, checks to see if '/' has been
4078 * pressed. See comment by tree_view_keybinding_cb() for more details.
4081 browse_files_key_press_event_cb (GtkWidget *widget,
4085 GtkFileChooserDefault *impl;
4088 impl = (GtkFileChooserDefault *) data;
4090 modifiers = gtk_accelerator_get_default_mod_mask ();
4092 if ((event->keyval == GDK_slash
4093 || event->keyval == GDK_KP_Divide
4095 || event->keyval == GDK_asciitilde
4097 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4099 location_popup_handler (impl, event->string);
4103 if (key_is_left_or_right (event))
4105 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
4109 if ((event->keyval == GDK_Return
4110 || event->keyval == GDK_ISO_Enter
4111 || event->keyval == GDK_KP_Enter
4112 || event->keyval == GDK_space
4113 || event->keyval == GDK_KP_Space)
4114 && ((event->state & modifiers) == 0)
4115 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4116 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4120 window = get_toplevel (widget);
4122 && widget != window->default_widget
4123 && !(widget == window->focus_widget &&
4124 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4126 gtk_window_activate_default (window);
4134 /* Callback used when the file list's popup menu is detached */
4136 popup_menu_detach_cb (GtkWidget *attach_widget,
4139 GtkFileChooserDefault *impl;
4141 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4142 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4144 impl->browse_files_popup_menu = NULL;
4145 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4146 impl->browse_files_popup_menu_hidden_files_item = NULL;
4149 /* Callback used when the "Add to Bookmarks" menu item is activated */
4151 add_to_shortcuts_cb (GtkMenuItem *item,
4152 GtkFileChooserDefault *impl)
4154 bookmarks_add_selected_folder (impl);
4157 /* Callback used when the "Show Hidden Files" menu item is toggled */
4159 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4160 GtkFileChooserDefault *impl)
4163 "show-hidden", gtk_check_menu_item_get_active (item),
4167 /* Callback used when the "Show Size Column" menu item is toggled */
4169 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4170 GtkFileChooserDefault *impl)
4172 impl->show_size_column = gtk_check_menu_item_get_active (item);
4174 if (impl->list_size_column)
4175 gtk_tree_view_column_set_visible (impl->list_size_column,
4176 impl->show_size_column);
4179 /* Shows an error dialog about not being able to select a dragged file */
4181 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4186 _("Could not select file"),
4191 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4196 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4198 for (i = 1; uris[i]; i++)
4201 GError *error = NULL;
4204 file = g_file_new_for_uri (uri);
4206 gtk_file_chooser_default_select_file (chooser, file, &error);
4208 error_selecting_dragged_file_dialog (impl, file, error);
4210 g_object_unref (file);
4214 struct FileListDragData
4216 GtkFileChooserDefault *impl;
4222 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4224 const GError *error,
4227 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4228 struct FileListDragData *data = user_data;
4229 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4231 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4234 data->impl->file_list_drag_data_received_cancellable = NULL;
4236 if (cancelled || error)
4239 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4240 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4241 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4242 change_folder_and_display_error (data->impl, data->file, FALSE);
4245 GError *error = NULL;
4247 gtk_file_chooser_default_unselect_all (chooser);
4248 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4250 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4252 browse_files_center_selected_row (data->impl);
4255 if (data->impl->select_multiple)
4256 file_list_drag_data_select_uris (data->impl, data->uris);
4259 g_object_unref (data->impl);
4260 g_strfreev (data->uris);
4261 g_object_unref (data->file);
4264 g_object_unref (cancellable);
4268 file_list_drag_data_received_cb (GtkWidget *widget,
4269 GdkDragContext *context,
4272 GtkSelectionData *selection_data,
4277 GtkFileChooserDefault *impl;
4278 GtkFileChooser *chooser;
4283 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4284 chooser = GTK_FILE_CHOOSER (data);
4286 /* Allow only drags from other widgets; see bug #533891. */
4287 if (gtk_drag_get_source_widget (context) == widget)
4289 g_signal_stop_emission_by_name (widget, "drag-data-received");
4293 /* Parse the text/uri-list string, navigate to the first one */
4294 uris = gtk_selection_data_get_uris (selection_data);
4295 if (uris && uris[0])
4297 struct FileListDragData *data;
4300 file = g_file_new_for_uri (uri);
4302 data = g_new0 (struct FileListDragData, 1);
4303 data->impl = g_object_ref (impl);
4307 if (impl->file_list_drag_data_received_cancellable)
4308 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4310 impl->file_list_drag_data_received_cancellable =
4311 _gtk_file_system_get_info (impl->file_system, file,
4313 file_list_drag_data_received_get_info_cb,
4317 g_signal_stop_emission_by_name (widget, "drag-data-received");
4320 /* Don't do anything with the drag_drop signal */
4322 file_list_drag_drop_cb (GtkWidget *widget,
4323 GdkDragContext *context,
4327 GtkFileChooserDefault *impl)
4329 g_signal_stop_emission_by_name (widget, "drag-drop");
4333 /* Disable the normal tree drag motion handler, it makes it look like you're
4334 dropping the dragged item onto a tree item */
4336 file_list_drag_motion_cb (GtkWidget *widget,
4337 GdkDragContext *context,
4341 GtkFileChooserDefault *impl)
4343 g_signal_stop_emission_by_name (widget, "drag-motion");
4347 /* Constructs the popup menu for the file list if needed */
4349 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4353 if (impl->browse_files_popup_menu)
4356 impl->browse_files_popup_menu = gtk_menu_new ();
4357 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4358 impl->browse_files_tree_view,
4359 popup_menu_detach_cb);
4361 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4362 impl->browse_files_popup_menu_add_shortcut_item = item;
4363 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4364 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4365 g_signal_connect (item, "activate",
4366 G_CALLBACK (add_to_shortcuts_cb), impl);
4367 gtk_widget_show (item);
4368 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4370 item = gtk_separator_menu_item_new ();
4371 gtk_widget_show (item);
4372 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4374 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4375 impl->browse_files_popup_menu_hidden_files_item = item;
4376 g_signal_connect (item, "toggled",
4377 G_CALLBACK (show_hidden_toggled_cb), impl);
4378 gtk_widget_show (item);
4379 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4381 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4382 impl->browse_files_popup_menu_size_column_item = item;
4383 g_signal_connect (item, "toggled",
4384 G_CALLBACK (show_size_column_toggled_cb), impl);
4385 gtk_widget_show (item);
4386 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4388 bookmarks_check_add_sensitivity (impl);
4391 /* Updates the popup menu for the file list, creating it if necessary */
4393 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4395 file_list_build_popup_menu (impl);
4397 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4399 /* The sensitivity of the Add to Bookmarks item is set in
4400 * bookmarks_check_add_sensitivity()
4403 /* 'Show Hidden Files' */
4404 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4405 G_CALLBACK (show_hidden_toggled_cb), impl);
4406 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4408 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4409 G_CALLBACK (show_hidden_toggled_cb), impl);
4411 /* 'Show Size Column' */
4412 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4413 G_CALLBACK (show_size_column_toggled_cb), impl);
4414 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4415 impl->show_size_column);
4416 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4417 G_CALLBACK (show_size_column_toggled_cb), impl);
4421 popup_position_func (GtkMenu *menu,
4427 GtkWidget *widget = GTK_WIDGET (user_data);
4428 GdkScreen *screen = gtk_widget_get_screen (widget);
4431 GdkRectangle monitor;
4433 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4435 gdk_window_get_origin (widget->window, x, y);
4437 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4439 *x += (widget->allocation.width - req.width) / 2;
4440 *y += (widget->allocation.height - req.height) / 2;
4442 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4443 gtk_menu_set_monitor (menu, monitor_num);
4444 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4446 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4447 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4453 file_list_popup_menu (GtkFileChooserDefault *impl,
4454 GdkEventButton *event)
4456 file_list_update_popup_menu (impl);
4458 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4459 NULL, NULL, NULL, NULL,
4460 event->button, event->time);
4463 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4465 popup_position_func, impl->browse_files_tree_view,
4466 0, GDK_CURRENT_TIME);
4467 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4473 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4475 list_popup_menu_cb (GtkWidget *widget,
4476 GtkFileChooserDefault *impl)
4478 file_list_popup_menu (impl, NULL);
4482 /* Callback used when a button is pressed on the file list. We trap button 3 to
4483 * bring up a popup menu.
4486 list_button_press_event_cb (GtkWidget *widget,
4487 GdkEventButton *event,
4488 GtkFileChooserDefault *impl)
4490 static gboolean in_press = FALSE;
4496 if (event->button != 3)
4500 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4503 file_list_popup_menu (impl, event);
4508 OperationMode operation_mode;
4509 gint general_column;
4513 /* Sigh. Each operation mode has different sort column IDs. This table
4514 * translates between them.
4516 static const ColumnMap column_map[] = {
4517 { OPERATION_MODE_BROWSE, FILE_LIST_COL_NAME, FILE_LIST_COL_NAME },
4518 { OPERATION_MODE_BROWSE, FILE_LIST_COL_SIZE, FILE_LIST_COL_SIZE },
4519 { OPERATION_MODE_BROWSE, FILE_LIST_COL_MTIME, FILE_LIST_COL_MTIME },
4521 { OPERATION_MODE_SEARCH, FILE_LIST_COL_NAME, SEARCH_MODEL_COL_FILE },
4522 { OPERATION_MODE_SEARCH, FILE_LIST_COL_SIZE, SEARCH_MODEL_COL_SIZE },
4523 { OPERATION_MODE_SEARCH, FILE_LIST_COL_MTIME, SEARCH_MODEL_COL_MTIME },
4525 { OPERATION_MODE_RECENT, FILE_LIST_COL_NAME, RECENT_MODEL_COL_FILE },
4526 { OPERATION_MODE_RECENT, FILE_LIST_COL_SIZE, 0 },
4527 { OPERATION_MODE_RECENT, FILE_LIST_COL_MTIME, RECENT_MODEL_COL_INFO }
4531 general_column_to_model_column (GtkFileChooserDefault *impl, gint general_column)
4535 for (i = 0; i < G_N_ELEMENTS (column_map); i++)
4536 if (column_map[i].operation_mode == impl->operation_mode
4537 && column_map[i].general_column == general_column)
4538 return column_map[i].model_column;
4540 g_assert_not_reached ();
4545 model_column_to_general_column (GtkFileChooserDefault *impl, gint model_column)
4549 for (i = 0; i < G_N_ELEMENTS (column_map); i++)
4550 if (column_map[i].operation_mode == impl->operation_mode
4551 && column_map[i].model_column == model_column)
4552 return column_map[i].general_column;
4554 g_assert_not_reached ();
4558 /* Sets the sort column IDs for the file list based on the operation mode */
4560 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4562 int name_id, mtime_id, size_id;
4564 name_id = general_column_to_model_column (impl, FILE_LIST_COL_NAME);
4565 mtime_id = general_column_to_model_column (impl, FILE_LIST_COL_MTIME);
4566 size_id = general_column_to_model_column (impl, FILE_LIST_COL_SIZE);
4568 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4569 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4570 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, size_id);
4574 file_list_query_tooltip_cb (GtkWidget *widget,
4577 gboolean keyboard_tip,
4578 GtkTooltip *tooltip,
4581 GtkFileChooserDefault *impl = user_data;
4582 GtkTreeIter iter, child_iter;
4583 GtkTreePath *path = NULL;
4587 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4591 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4599 switch (impl->operation_mode)
4601 case OPERATION_MODE_SEARCH:
4602 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4604 gtk_tree_path_free (path);
4608 search_get_valid_child_iter (impl, &child_iter, &iter);
4609 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4610 SEARCH_MODEL_COL_FILE, &file,
4614 case OPERATION_MODE_RECENT:
4615 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4617 gtk_tree_path_free (path);
4621 recent_get_valid_child_iter (impl, &child_iter, &iter);
4622 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4623 RECENT_MODEL_COL_FILE, &file,
4627 case OPERATION_MODE_BROWSE:
4628 g_assert_not_reached ();
4634 gtk_tree_path_free (path);
4638 filename = g_file_get_path (file);
4639 gtk_tooltip_set_text (tooltip, filename);
4640 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4645 gtk_tree_path_free (path);
4650 /* Creates the widgets for the file list */
4652 create_file_list (GtkFileChooserDefault *impl)
4655 GtkTreeSelection *selection;
4656 GtkTreeViewColumn *column;
4657 GtkCellRenderer *renderer;
4659 /* Scrolled window */
4660 swin = gtk_scrolled_window_new (NULL, NULL);
4661 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4662 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4663 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4666 /* Tree/list view */
4668 impl->browse_files_tree_view = gtk_tree_view_new ();
4669 #ifdef PROFILE_FILE_CHOOSER
4670 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4672 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4673 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4675 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4676 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4678 gtk_drag_dest_set (impl->browse_files_tree_view,
4679 GTK_DEST_DEFAULT_ALL,
4681 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4682 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4684 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4685 G_CALLBACK (list_row_activated), impl);
4686 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4687 G_CALLBACK (browse_files_key_press_event_cb), impl);
4688 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4689 G_CALLBACK (list_popup_menu_cb), impl);
4690 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4691 G_CALLBACK (list_button_press_event_cb), impl);
4693 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4694 G_CALLBACK (file_list_drag_data_received_cb), impl);
4695 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4696 G_CALLBACK (file_list_drag_drop_cb), impl);
4697 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4698 G_CALLBACK (file_list_drag_motion_cb), impl);
4700 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4701 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4702 G_CALLBACK (file_list_query_tooltip_cb), impl);
4704 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4705 gtk_tree_selection_set_select_function (selection,
4708 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4711 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4712 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4714 g_signal_connect (selection, "changed",
4715 G_CALLBACK (list_selection_changed), impl);
4717 /* Filename column */
4719 impl->list_name_column = gtk_tree_view_column_new ();
4720 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4721 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4722 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4724 renderer = gtk_cell_renderer_pixbuf_new ();
4725 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4726 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4727 list_icon_data_func, impl, NULL);
4729 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4730 g_object_set (impl->list_name_renderer,
4731 "ellipsize", PANGO_ELLIPSIZE_END,
4733 g_signal_connect (impl->list_name_renderer, "edited",
4734 G_CALLBACK (renderer_edited_cb), impl);
4735 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4736 G_CALLBACK (renderer_editing_canceled_cb), impl);
4737 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4738 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4739 list_name_data_func, impl, NULL);
4741 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4745 column = gtk_tree_view_column_new ();
4746 gtk_tree_view_column_set_resizable (column, TRUE);
4747 gtk_tree_view_column_set_title (column, _("Size"));
4749 renderer = gtk_cell_renderer_text_new ();
4750 g_object_set (renderer,
4751 "alignment", PANGO_ALIGN_RIGHT,
4753 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4754 gtk_tree_view_column_set_cell_data_func (column, renderer,
4755 list_size_data_func, impl, NULL);
4756 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4757 impl->list_size_column = column;
4759 /* Modification time column */
4761 column = gtk_tree_view_column_new ();
4762 gtk_tree_view_column_set_resizable (column, TRUE);
4763 gtk_tree_view_column_set_title (column, _("Modified"));
4765 renderer = gtk_cell_renderer_text_new ();
4766 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4767 gtk_tree_view_column_set_cell_data_func (column, renderer,
4768 list_mtime_data_func, impl, NULL);
4769 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4770 impl->list_mtime_column = column;
4772 file_list_set_sort_column_ids (impl);
4774 gtk_widget_show_all (swin);
4780 create_path_bar (GtkFileChooserDefault *impl)
4782 GtkWidget *path_bar;
4784 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4785 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4790 /* Creates the widgets for the files/folders pane */
4792 file_pane_create (GtkFileChooserDefault *impl,
4793 GtkSizeGroup *size_group)
4799 vbox = gtk_vbox_new (FALSE, 6);
4800 gtk_widget_show (vbox);
4802 /* Box for lists and preview */
4804 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4805 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4806 gtk_widget_show (hbox);
4810 widget = create_file_list (impl);
4811 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4815 impl->preview_box = gtk_vbox_new (FALSE, 12);
4816 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4817 /* Don't show preview box initially */
4821 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4823 widget = filter_create (impl);
4825 gtk_widget_show (widget);
4826 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4828 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4829 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4834 /* Callback used when the "Browse for more folders" expander is toggled */
4836 expander_changed_cb (GtkExpander *expander,
4838 GtkFileChooserDefault *impl)
4840 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4841 update_appearance (impl);
4844 /* Callback used when the selection changes in the save folder combo box */
4846 save_folder_combo_changed_cb (GtkComboBox *combo,
4847 GtkFileChooserDefault *impl)
4851 if (impl->changing_folder)
4854 if (gtk_combo_box_get_active_iter (combo, &iter))
4856 GtkTreeIter child_iter;
4858 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4861 shortcuts_activate_iter (impl, &child_iter);
4866 save_folder_update_tooltip (GtkComboBox *combo,
4867 GtkFileChooserDefault *impl)
4874 if (gtk_combo_box_get_active_iter (combo, &iter))
4876 GtkTreeIter child_iter;
4878 ShortcutType shortcut_type;
4880 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4883 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4884 SHORTCUTS_COL_DATA, &col_data,
4885 SHORTCUTS_COL_TYPE, &shortcut_type,
4888 if (shortcut_type == SHORTCUT_TYPE_FILE)
4889 tooltip = g_file_get_parse_name (G_FILE (col_data));
4892 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4893 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4894 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4898 /* Filter function used to filter out the Search item and its separator.
4899 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4902 shortcuts_combo_filter_func (GtkTreeModel *model,
4906 GtkFileChooserDefault *impl;
4907 GtkTreePath *tree_path;
4912 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4914 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4916 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4917 g_assert (tree_path != NULL);
4919 indices = gtk_tree_path_get_indices (tree_path);
4923 if (impl->has_search)
4925 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4926 if (idx == indices[0])
4930 if (impl->has_recent)
4932 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4933 if (idx == indices[0])
4937 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4938 if (idx == indices[0])
4943 gtk_tree_path_free (tree_path);
4948 /* Creates the combo box with the save folders */
4950 save_folder_combo_create (GtkFileChooserDefault *impl)
4953 GtkCellRenderer *cell;
4955 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4956 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4957 shortcuts_combo_filter_func,
4961 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4962 "model", impl->shortcuts_combo_filter_model,
4963 "focus-on-click", FALSE,
4965 gtk_widget_show (combo);
4967 cell = gtk_cell_renderer_pixbuf_new ();
4968 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4969 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4970 "pixbuf", SHORTCUTS_COL_PIXBUF,
4971 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4972 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4975 cell = gtk_cell_renderer_text_new ();
4976 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4977 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4978 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4979 "text", SHORTCUTS_COL_NAME,
4980 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4983 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4984 shortcuts_row_separator_func,
4987 g_signal_connect (combo, "changed",
4988 G_CALLBACK (save_folder_combo_changed_cb), impl);
4989 g_signal_connect (combo, "changed",
4990 G_CALLBACK (save_folder_update_tooltip), impl);
4995 /* Creates the widgets specific to Save mode */
4997 save_widgets_create (GtkFileChooserDefault *impl)
5002 GtkWidget *alignment;
5004 if (impl->save_widgets != NULL)
5007 location_switch_to_path_bar (impl);
5009 vbox = gtk_vbox_new (FALSE, 12);
5011 table = gtk_table_new (2, 2, FALSE);
5012 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
5013 gtk_widget_show (table);
5014 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
5015 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
5019 widget = gtk_label_new_with_mnemonic (_("_Name:"));
5020 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
5021 gtk_table_attach (GTK_TABLE (table), widget,
5025 gtk_widget_show (widget);
5027 /* Location entry */
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_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
5033 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
5034 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5035 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
5037 GTK_EXPAND | GTK_FILL, 0,
5039 gtk_widget_show (impl->location_entry);
5040 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
5043 impl->save_folder_label = gtk_label_new (NULL);
5044 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
5045 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
5049 gtk_widget_show (impl->save_folder_label);
5051 impl->save_folder_combo = save_folder_combo_create (impl);
5052 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
5054 GTK_EXPAND | GTK_FILL, GTK_FILL,
5056 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
5059 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5060 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
5062 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
5063 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
5064 g_signal_connect (impl->save_expander, "notify::expanded",
5065 G_CALLBACK (expander_changed_cb),
5067 gtk_widget_show_all (alignment);
5069 impl->save_widgets = vbox;
5070 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
5071 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
5072 gtk_widget_show (impl->save_widgets);
5075 /* Destroys the widgets specific to Save mode */
5077 save_widgets_destroy (GtkFileChooserDefault *impl)
5079 if (impl->save_widgets == NULL)
5082 gtk_widget_destroy (impl->save_widgets);
5083 impl->save_widgets = NULL;
5084 impl->location_entry = NULL;
5085 impl->save_folder_label = NULL;
5086 impl->save_folder_combo = NULL;
5087 impl->save_expander = NULL;
5090 /* Turns on the path bar widget. Can be called even if we are already in that
5094 location_switch_to_path_bar (GtkFileChooserDefault *impl)
5096 if (impl->location_entry)
5098 gtk_widget_destroy (impl->location_entry);
5099 impl->location_entry = NULL;
5102 gtk_widget_hide (impl->location_entry_box);
5105 /* Sets the full path of the current folder as the text in the location entry. */
5107 location_entry_set_initial_text (GtkFileChooserDefault *impl)
5109 gchar *text, *filename;
5111 if (!impl->current_folder)
5114 filename = g_file_get_path (impl->current_folder);
5118 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
5122 text = g_file_get_uri (impl->current_folder);
5126 gboolean need_slash;
5129 len = strlen (text);
5130 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
5136 slash_text = g_new (char, len + 2);
5137 strcpy (slash_text, text);
5138 slash_text[len] = G_DIR_SEPARATOR;
5139 slash_text[len + 1] = 0;
5145 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5152 /* Turns on the location entry. Can be called even if we are already in that
5156 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5158 /* when in search or recent files mode, we are not showing the
5159 * location_entry_box container, so there's no point in switching
5162 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5163 impl->operation_mode == OPERATION_MODE_RECENT)
5166 if (impl->location_entry)
5167 gtk_widget_destroy (impl->location_entry);
5171 gtk_widget_show (impl->location_entry_box);
5175 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5176 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5178 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5179 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5181 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5182 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5184 /* Configure the entry */
5186 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5190 gtk_widget_show (impl->location_entry);
5191 gtk_widget_grab_focus (impl->location_entry);
5194 /* Sets a new location mode. set_buttons determines whether the toggle button
5195 * for the mode will also be changed.
5198 location_mode_set (GtkFileChooserDefault *impl,
5199 LocationMode new_mode,
5200 gboolean set_button)
5202 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5203 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5205 GtkWindow *toplevel;
5206 GtkWidget *current_focus;
5207 gboolean button_active;
5208 gboolean switch_to_file_list;
5212 case LOCATION_MODE_PATH_BAR:
5213 button_active = FALSE;
5215 /* The location_entry will disappear when we switch to path bar mode. So,
5216 * we'll focus the file list in that case, to avoid having a window with
5217 * no focused widget.
5219 toplevel = get_toplevel (GTK_WIDGET (impl));
5220 switch_to_file_list = FALSE;
5223 current_focus = gtk_window_get_focus (toplevel);
5224 if (!current_focus || current_focus == impl->location_entry)
5225 switch_to_file_list = TRUE;
5228 location_switch_to_path_bar (impl);
5230 if (switch_to_file_list)
5231 gtk_widget_grab_focus (impl->browse_files_tree_view);
5235 case LOCATION_MODE_FILENAME_ENTRY:
5236 button_active = TRUE;
5237 location_switch_to_filename_entry (impl);
5241 g_assert_not_reached ();
5247 g_signal_handlers_block_by_func (impl->location_button,
5248 G_CALLBACK (location_button_toggled_cb), impl);
5250 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5252 g_signal_handlers_unblock_by_func (impl->location_button,
5253 G_CALLBACK (location_button_toggled_cb), impl);
5257 impl->location_mode = new_mode;
5261 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5263 /* when in search or recent files mode, we are not showing the
5264 * location_entry_box container, so there's no point in switching
5267 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5268 impl->operation_mode == OPERATION_MODE_RECENT)
5271 /* If the file entry is not visible, show it.
5272 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5274 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5276 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5278 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5280 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5282 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5286 gtk_widget_grab_focus (impl->location_entry);
5291 /* Callback used when one of the location mode buttons is toggled */
5293 location_button_toggled_cb (GtkToggleButton *toggle,
5294 GtkFileChooserDefault *impl)
5297 LocationMode new_mode;
5299 is_active = gtk_toggle_button_get_active (toggle);
5303 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5304 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5308 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5309 new_mode = LOCATION_MODE_PATH_BAR;
5312 location_mode_set (impl, new_mode, FALSE);
5315 /* Creates a toggle button for the location entry. */
5317 location_button_create (GtkFileChooserDefault *impl)
5322 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5323 gtk_widget_show (image);
5325 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5329 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5331 g_signal_connect (impl->location_button, "toggled",
5332 G_CALLBACK (location_button_toggled_cb), impl);
5334 str = _("Type a file name");
5336 gtk_widget_set_tooltip_text (impl->location_button, str);
5337 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5340 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5342 browse_widgets_create (GtkFileChooserDefault *impl)
5347 GtkSizeGroup *size_group;
5349 /* size group is used by the [+][-] buttons and the filter combo */
5350 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5351 vbox = gtk_vbox_new (FALSE, 12);
5353 /* Location widgets */
5354 impl->browse_path_bar_hbox = gtk_hbox_new (FALSE, 12);
5355 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5356 gtk_widget_show (impl->browse_path_bar_hbox);
5358 /* Size group that allows the path bar to be the same size between modes */
5359 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5360 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5362 /* Location button */
5364 location_button_create (impl);
5365 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5369 impl->browse_path_bar = create_path_bar (impl);
5370 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5371 gtk_widget_show_all (impl->browse_path_bar);
5372 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5375 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5376 g_signal_connect (impl->browse_new_folder_button, "clicked",
5377 G_CALLBACK (new_folder_button_clicked), impl);
5378 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5380 /* Box for the location label and entry */
5382 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5383 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5385 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5386 gtk_widget_show (impl->location_label);
5387 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5390 hpaned = gtk_hpaned_new ();
5391 gtk_widget_show (hpaned);
5392 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5394 widget = shortcuts_pane_create (impl, size_group);
5395 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5396 widget = file_pane_create (impl, size_group);
5397 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5399 g_object_unref (size_group);
5405 gtk_file_chooser_default_constructor (GType type,
5406 guint n_construct_properties,
5407 GObjectConstructParam *construct_params)
5409 GtkFileChooserDefault *impl;
5412 profile_start ("start", NULL);
5414 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5415 n_construct_properties,
5417 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5419 g_assert (impl->file_system);
5421 gtk_widget_push_composite_child ();
5423 /* Shortcuts model */
5424 shortcuts_model_create (impl);
5426 /* The browse widgets */
5427 impl->browse_widgets = browse_widgets_create (impl);
5428 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5430 /* Alignment to hold extra widget */
5431 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5432 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5434 gtk_widget_pop_composite_child ();
5435 update_appearance (impl);
5437 profile_end ("end", NULL);
5442 /* Sets the extra_widget by packing it in the appropriate place */
5444 set_extra_widget (GtkFileChooserDefault *impl,
5445 GtkWidget *extra_widget)
5449 g_object_ref (extra_widget);
5450 /* FIXME: is this right ? */
5451 gtk_widget_show (extra_widget);
5454 if (impl->extra_widget)
5456 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5457 g_object_unref (impl->extra_widget);
5460 impl->extra_widget = extra_widget;
5461 if (impl->extra_widget)
5463 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5464 gtk_widget_show (impl->extra_align);
5467 gtk_widget_hide (impl->extra_align);
5471 set_local_only (GtkFileChooserDefault *impl,
5472 gboolean local_only)
5474 if (local_only != impl->local_only)
5476 impl->local_only = local_only;
5478 if (impl->location_entry)
5479 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5481 if (impl->shortcuts_model && impl->file_system)
5483 shortcuts_add_volumes (impl);
5484 shortcuts_add_bookmarks (impl);
5487 if (local_only && impl->current_folder &&
5488 !g_file_is_native (impl->current_folder))
5490 /* If we are pointing to a non-local folder, make an effort to change
5491 * back to a local folder, but it's really up to the app to not cause
5492 * such a situation, so we ignore errors.
5494 const gchar *home = g_get_home_dir ();
5500 home_file = g_file_new_for_path (home);
5502 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5504 g_object_unref (home_file);
5510 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5511 GtkFileChooserDefault *impl)
5513 shortcuts_add_volumes (impl);
5514 shortcuts_add_bookmarks (impl);
5516 bookmarks_check_add_sensitivity (impl);
5517 bookmarks_check_remove_sensitivity (impl);
5518 shortcuts_check_popup_sensitivity (impl);
5521 /* Sets the file chooser to multiple selection mode */
5523 set_select_multiple (GtkFileChooserDefault *impl,
5524 gboolean select_multiple,
5525 gboolean property_notify)
5527 GtkTreeSelection *selection;
5528 GtkSelectionMode mode;
5530 if (select_multiple == impl->select_multiple)
5533 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5535 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5536 gtk_tree_selection_set_mode (selection, mode);
5538 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5540 impl->select_multiple = select_multiple;
5541 g_object_notify (G_OBJECT (impl), "select-multiple");
5543 check_preview_change (impl);
5547 set_file_system_backend (GtkFileChooserDefault *impl)
5549 profile_start ("start for backend", "default");
5551 impl->file_system = _gtk_file_system_new ();
5553 g_signal_connect (impl->file_system, "volumes-changed",
5554 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5555 g_signal_connect (impl->file_system, "bookmarks-changed",
5556 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5558 profile_end ("end", NULL);
5562 unset_file_system_backend (GtkFileChooserDefault *impl)
5564 g_signal_handlers_disconnect_by_func (impl->file_system,
5565 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5567 g_object_unref (impl->file_system);
5569 impl->file_system = NULL;
5572 /* This function is basically a do_all function.
5574 * It sets the visibility on all the widgets based on the current state, and
5575 * moves the custom_widget if needed.
5578 update_appearance (GtkFileChooserDefault *impl)
5580 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5581 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5585 gtk_widget_hide (impl->location_button);
5586 save_widgets_create (impl);
5588 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5589 text = _("Save in _folder:");
5591 text = _("Create in _folder:");
5593 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5595 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5597 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5598 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5599 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5600 gtk_widget_show (impl->browse_widgets);
5604 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5605 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5606 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5607 gtk_widget_hide (impl->browse_widgets);
5610 if (impl->select_multiple)
5612 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5613 "Re-setting to single selection mode.");
5614 set_select_multiple (impl, FALSE, TRUE);
5617 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5618 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5620 gtk_widget_show (impl->location_button);
5621 save_widgets_destroy (impl);
5622 gtk_widget_show (impl->browse_widgets);
5623 location_mode_set (impl, impl->location_mode, TRUE);
5626 if (impl->location_entry)
5627 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5629 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5630 gtk_widget_hide (impl->browse_new_folder_button);
5632 gtk_widget_show (impl->browse_new_folder_button);
5634 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5635 * of files may change depending whether we are in a file or folder-only mode.
5637 gtk_widget_queue_draw (impl->browse_files_tree_view);
5639 emit_default_size_changed (impl);
5643 gtk_file_chooser_default_set_property (GObject *object,
5645 const GValue *value,
5649 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5653 case GTK_FILE_CHOOSER_PROP_ACTION:
5655 GtkFileChooserAction action = g_value_get_enum (value);
5657 if (action != impl->action)
5659 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5661 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5662 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5663 && impl->select_multiple)
5665 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5666 "this is not allowed in multiple selection mode. Resetting the file chooser "
5667 "to single selection mode.");
5668 set_select_multiple (impl, FALSE, TRUE);
5670 impl->action = action;
5671 update_appearance (impl);
5672 settings_load (impl);
5677 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5678 /* Ignore property */
5681 case GTK_FILE_CHOOSER_PROP_FILTER:
5682 set_current_filter (impl, g_value_get_object (value));
5685 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5686 set_local_only (impl, g_value_get_boolean (value));
5689 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5690 set_preview_widget (impl, g_value_get_object (value));
5693 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5694 impl->preview_widget_active = g_value_get_boolean (value);
5695 update_preview_widget_visibility (impl);
5698 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5699 impl->use_preview_label = g_value_get_boolean (value);
5700 update_preview_widget_visibility (impl);
5703 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5704 set_extra_widget (impl, g_value_get_object (value));
5707 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5709 gboolean select_multiple = g_value_get_boolean (value);
5710 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5711 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5714 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5715 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5716 "leaving the file chooser in single selection mode.");
5720 set_select_multiple (impl, select_multiple, FALSE);
5724 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5726 gboolean show_hidden = g_value_get_boolean (value);
5727 if (show_hidden != impl->show_hidden)
5729 impl->show_hidden = show_hidden;
5731 if (impl->browse_files_model)
5732 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5737 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5739 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5740 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5744 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5746 gboolean create_folders = g_value_get_boolean (value);
5747 impl->create_folders = create_folders;
5748 update_appearance (impl);
5753 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5759 gtk_file_chooser_default_get_property (GObject *object,
5764 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5768 case GTK_FILE_CHOOSER_PROP_ACTION:
5769 g_value_set_enum (value, impl->action);
5772 case GTK_FILE_CHOOSER_PROP_FILTER:
5773 g_value_set_object (value, impl->current_filter);
5776 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5777 g_value_set_boolean (value, impl->local_only);
5780 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5781 g_value_set_object (value, impl->preview_widget);
5784 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5785 g_value_set_boolean (value, impl->preview_widget_active);
5788 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5789 g_value_set_boolean (value, impl->use_preview_label);
5792 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5793 g_value_set_object (value, impl->extra_widget);
5796 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5797 g_value_set_boolean (value, impl->select_multiple);
5800 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5801 g_value_set_boolean (value, impl->show_hidden);
5804 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5805 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5808 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5809 g_value_set_boolean (value, impl->create_folders);
5813 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5818 /* Removes the settings signal handler. It's safe to call multiple times */
5820 remove_settings_signal (GtkFileChooserDefault *impl,
5823 if (impl->settings_signal_id)
5825 GtkSettings *settings;
5827 settings = gtk_settings_get_for_screen (screen);
5828 g_signal_handler_disconnect (settings,
5829 impl->settings_signal_id);
5830 impl->settings_signal_id = 0;
5835 gtk_file_chooser_default_dispose (GObject *object)
5838 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5840 if (impl->extra_widget)
5842 g_object_unref (impl->extra_widget);
5843 impl->extra_widget = NULL;
5846 pending_select_files_free (impl);
5848 /* cancel all pending operations */
5849 if (impl->pending_cancellables)
5851 for (l = impl->pending_cancellables; l; l = l->next)
5853 GCancellable *cancellable = G_CANCELLABLE (l->data);
5854 g_cancellable_cancel (cancellable);
5856 g_slist_free (impl->pending_cancellables);
5857 impl->pending_cancellables = NULL;
5860 if (impl->reload_icon_cancellables)
5862 for (l = impl->reload_icon_cancellables; l; l = l->next)
5864 GCancellable *cancellable = G_CANCELLABLE (l->data);
5865 g_cancellable_cancel (cancellable);
5867 g_slist_free (impl->reload_icon_cancellables);
5868 impl->reload_icon_cancellables = NULL;
5871 if (impl->loading_shortcuts)
5873 for (l = impl->loading_shortcuts; l; l = l->next)
5875 GCancellable *cancellable = G_CANCELLABLE (l->data);
5876 g_cancellable_cancel (cancellable);
5878 g_slist_free (impl->loading_shortcuts);
5879 impl->loading_shortcuts = NULL;
5882 if (impl->file_list_drag_data_received_cancellable)
5884 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5885 impl->file_list_drag_data_received_cancellable = NULL;
5888 if (impl->update_current_folder_cancellable)
5890 g_cancellable_cancel (impl->update_current_folder_cancellable);
5891 impl->update_current_folder_cancellable = NULL;
5894 if (impl->show_and_select_files_cancellable)
5896 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5897 impl->show_and_select_files_cancellable = NULL;
5900 if (impl->should_respond_get_info_cancellable)
5902 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5903 impl->should_respond_get_info_cancellable = NULL;
5906 if (impl->update_from_entry_cancellable)
5908 g_cancellable_cancel (impl->update_from_entry_cancellable);
5909 impl->update_from_entry_cancellable = NULL;
5912 if (impl->shortcuts_activate_iter_cancellable)
5914 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5915 impl->shortcuts_activate_iter_cancellable = NULL;
5918 search_stop_searching (impl, TRUE);
5919 recent_stop_loading (impl);
5921 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5923 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5926 /* We override show-all since we have internal widgets that
5927 * shouldn't be shown when you call show_all(), like the filter
5931 gtk_file_chooser_default_show_all (GtkWidget *widget)
5933 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5935 gtk_widget_show (widget);
5937 if (impl->extra_widget)
5938 gtk_widget_show_all (impl->extra_widget);
5941 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5942 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5945 toplevel_set_focus_cb (GtkWindow *window,
5947 GtkFileChooserDefault *impl)
5949 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5952 /* We monitor the focus widget on our toplevel to be able to know which widget
5953 * was last focused at the time our "should_respond" method gets called.
5956 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5957 GtkWidget *previous_toplevel)
5959 GtkFileChooserDefault *impl;
5960 GtkWidget *toplevel;
5962 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5964 if (previous_toplevel)
5966 g_assert (impl->toplevel_set_focus_id != 0);
5967 g_signal_handler_disconnect (previous_toplevel,
5968 impl->toplevel_set_focus_id);
5969 impl->toplevel_set_focus_id = 0;
5970 impl->toplevel_last_focus_widget = NULL;
5973 g_assert (impl->toplevel_set_focus_id == 0);
5975 toplevel = gtk_widget_get_toplevel (widget);
5976 if (GTK_IS_WINDOW (toplevel))
5978 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5979 G_CALLBACK (toplevel_set_focus_cb), impl);
5980 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5984 /* Changes the icons wherever it is needed */
5986 change_icon_theme (GtkFileChooserDefault *impl)
5988 GtkSettings *settings;
5991 profile_start ("start", NULL);
5993 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5995 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5996 impl->icon_size = MAX (width, height);
5998 impl->icon_size = FALLBACK_ICON_SIZE;
6000 shortcuts_reload_icons (impl);
6001 gtk_widget_queue_resize (impl->browse_files_tree_view);
6003 profile_end ("end", NULL);
6006 /* Callback used when a GtkSettings value changes */
6008 settings_notify_cb (GObject *object,
6010 GtkFileChooserDefault *impl)
6014 profile_start ("start", NULL);
6016 name = g_param_spec_get_name (pspec);
6018 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
6019 strcmp (name, "gtk-icon-sizes") == 0)
6020 change_icon_theme (impl);
6022 profile_end ("end", NULL);
6025 /* Installs a signal handler for GtkSettings so that we can monitor changes in
6029 check_icon_theme (GtkFileChooserDefault *impl)
6031 GtkSettings *settings;
6033 profile_start ("start", NULL);
6035 if (impl->settings_signal_id)
6037 profile_end ("end", NULL);
6041 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
6043 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
6044 impl->settings_signal_id = g_signal_connect (settings, "notify",
6045 G_CALLBACK (settings_notify_cb), impl);
6047 change_icon_theme (impl);
6050 profile_end ("end", NULL);
6054 gtk_file_chooser_default_style_set (GtkWidget *widget,
6055 GtkStyle *previous_style)
6057 GtkFileChooserDefault *impl;
6059 profile_start ("start", NULL);
6061 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6063 profile_msg (" parent class style_set start", NULL);
6064 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
6065 profile_msg (" parent class style_set end", NULL);
6067 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
6068 change_icon_theme (impl);
6070 emit_default_size_changed (impl);
6072 profile_end ("end", NULL);
6076 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
6077 GdkScreen *previous_screen)
6079 GtkFileChooserDefault *impl;
6081 profile_start ("start", NULL);
6083 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6085 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
6086 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
6088 remove_settings_signal (impl, previous_screen);
6089 check_icon_theme (impl);
6091 emit_default_size_changed (impl);
6093 profile_end ("end", NULL);
6097 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
6098 GtkAllocation *allocation)
6100 GtkFileChooserDefault *impl;
6102 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6104 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6108 get_is_file_filtered (GtkFileChooserDefault *impl,
6110 GFileInfo *file_info)
6112 GtkFileFilterInfo filter_info;
6113 GtkFileFilterFlags needed;
6116 if (!impl->current_filter)
6119 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
6121 needed = gtk_file_filter_get_needed (impl->current_filter);
6123 filter_info.display_name = g_file_info_get_display_name (file_info);
6124 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
6126 if (needed & GTK_FILE_FILTER_FILENAME)
6128 filter_info.filename = g_file_get_path (file);
6129 if (filter_info.filename)
6130 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
6133 filter_info.filename = NULL;
6135 if (needed & GTK_FILE_FILTER_URI)
6137 filter_info.uri = g_file_get_uri (file);
6138 if (filter_info.uri)
6139 filter_info.contains |= GTK_FILE_FILTER_URI;
6142 filter_info.uri = NULL;
6144 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
6146 g_free ((gchar *)filter_info.filename);
6147 g_free ((gchar *)filter_info.uri);
6148 g_free ((gchar *)filter_info.mime_type);
6154 set_sort_column (GtkFileChooserDefault *impl)
6156 GtkTreeSortable *sortable;
6158 switch (impl->operation_mode)
6160 case OPERATION_MODE_BROWSE:
6161 sortable = GTK_TREE_SORTABLE (impl->sort_model);
6164 case OPERATION_MODE_SEARCH:
6165 sortable = GTK_TREE_SORTABLE (impl->search_model_sort);
6168 case OPERATION_MODE_RECENT:
6169 sortable = GTK_TREE_SORTABLE (impl->recent_model_sort);
6173 g_assert_not_reached ();
6177 gtk_tree_sortable_set_sort_column_id (sortable,
6178 general_column_to_model_column (impl, impl->sort_column),
6183 settings_load (GtkFileChooserDefault *impl)
6185 GtkFileChooserSettings *settings;
6186 LocationMode location_mode;
6187 gboolean show_hidden;
6188 gboolean expand_folders;
6189 gboolean show_size_column;
6191 GtkSortType sort_order;
6193 settings = _gtk_file_chooser_settings_new ();
6195 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6196 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6197 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6198 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
6199 sort_column = _gtk_file_chooser_settings_get_sort_column (settings);
6200 sort_order = _gtk_file_chooser_settings_get_sort_order (settings);
6202 g_object_unref (settings);
6204 location_mode_set (impl, location_mode, TRUE);
6206 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6208 impl->expand_folders = expand_folders;
6209 if (impl->save_expander)
6210 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6212 impl->show_size_column = show_size_column;
6213 if (impl->list_size_column)
6214 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6216 impl->sort_column = sort_column;
6217 impl->sort_order = sort_order;
6218 /* We don't call set_sort_column() here as the models may not have been
6219 * created yet. The individual functions that create and set the models will
6220 * call set_sort_column() themselves.
6225 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
6227 GtkWindow *toplevel;
6228 int x, y, width, height;
6230 /* We don't save the geometry in non-expanded "save" mode, so that the "little
6231 * dialog" won't make future Open dialogs too small.
6233 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6234 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6235 || impl->expand_folders))
6238 toplevel = get_toplevel (GTK_WIDGET (impl));
6240 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6243 gtk_window_get_position (toplevel, &x, &y);
6244 gtk_window_get_size (toplevel, &width, &height);
6246 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
6250 settings_save (GtkFileChooserDefault *impl)
6252 GtkFileChooserSettings *settings;
6254 settings = _gtk_file_chooser_settings_new ();
6256 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6257 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6258 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6259 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6260 _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column);
6261 _gtk_file_chooser_settings_set_sort_order (settings, impl->sort_order);
6263 save_dialog_geometry (impl, settings);
6266 _gtk_file_chooser_settings_save (settings, NULL);
6268 g_object_unref (settings);
6271 /* GtkWidget::realize method */
6273 gtk_file_chooser_default_realize (GtkWidget *widget)
6275 GtkFileChooserDefault *impl;
6277 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6279 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6281 emit_default_size_changed (impl);
6284 /* GtkWidget::map method */
6286 gtk_file_chooser_default_map (GtkWidget *widget)
6288 GtkFileChooserDefault *impl;
6289 char *current_working_dir;
6291 profile_start ("start", NULL);
6293 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6295 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6297 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6299 switch (impl->reload_state)
6302 /* The user didn't explicitly give us a folder to
6303 * display, so we'll use the cwd
6305 current_working_dir = g_get_current_dir ();
6306 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6307 current_working_dir);
6308 g_free (current_working_dir);
6311 case RELOAD_HAS_FOLDER:
6312 /* Nothing; we are already loading or loaded, so we
6313 * don't need to reload
6317 case RELOAD_WAS_UNMAPPED:
6318 /* Just reload the current folder; else continue
6321 if (impl->current_folder)
6323 pending_select_files_store_selection (impl);
6324 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6329 g_assert_not_reached ();
6333 volumes_bookmarks_changed_cb (impl->file_system, impl);
6335 settings_load (impl);
6337 profile_end ("end", NULL);
6340 /* GtkWidget::unmap method */
6342 gtk_file_chooser_default_unmap (GtkWidget *widget)
6344 GtkFileChooserDefault *impl;
6346 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6348 settings_save (impl);
6350 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6352 impl->reload_state = RELOAD_WAS_UNMAPPED;
6356 list_model_filter_func (GtkFileSystemModel *model,
6358 GFileInfo *file_info,
6361 GtkFileChooserDefault *impl = user_data;
6363 if (!impl->current_filter)
6366 if (_gtk_file_info_consider_as_directory (file_info))
6369 return !get_is_file_filtered (impl, file, file_info);
6373 install_list_model_filter (GtkFileChooserDefault *impl)
6375 GtkFileSystemModelFilter filter;
6378 g_assert (impl->browse_files_model != NULL);
6380 if (impl->current_filter)
6382 filter = list_model_filter_func;
6391 _gtk_file_system_model_set_filter (impl->browse_files_model,
6396 #define COMPARE_DIRECTORIES \
6397 GtkFileChooserDefault *impl = user_data; \
6398 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6399 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6400 gboolean dir_a, dir_b; \
6403 dir_a = _gtk_file_info_consider_as_directory (info_a); \
6405 return impl->list_sort_ascending ? -1 : 1; \
6408 dir_b = _gtk_file_info_consider_as_directory (info_b); \
6410 return impl->list_sort_ascending ? 1 : -1; \
6412 if (dir_a != dir_b) \
6413 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6415 /* Sort callback for the filename column */
6417 name_sort_func (GtkTreeModel *model,
6422 COMPARE_DIRECTORIES;
6425 gchar *key_a, *key_b;
6428 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6429 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6430 result = strcmp (key_a, key_b);
6439 /* Sort callback for the size column */
6441 size_sort_func (GtkTreeModel *model,
6446 COMPARE_DIRECTORIES;
6449 goffset size_a = g_file_info_get_size (info_a);
6450 goffset size_b = g_file_info_get_size (info_b);
6452 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6456 /* Sort callback for the mtime column */
6458 mtime_sort_func (GtkTreeModel *model,
6463 COMPARE_DIRECTORIES;
6468 g_file_info_get_modification_time (info_a, &ta);
6469 g_file_info_get_modification_time (info_b, &tb);
6471 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6475 /* Callback used when the sort column changes. We cache the sort order for use
6476 * in name_sort_func().
6479 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6480 GtkFileChooserDefault *impl)
6482 gint sort_column_id;
6483 GtkSortType sort_type;
6485 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6487 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6488 impl->sort_column = model_column_to_general_column (impl, sort_column_id);
6489 impl->sort_order = sort_type;
6494 set_busy_cursor (GtkFileChooserDefault *impl,
6497 GtkWindow *toplevel;
6498 GdkDisplay *display;
6501 toplevel = get_toplevel (GTK_WIDGET (impl));
6502 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6505 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6508 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6512 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6513 gdk_display_flush (display);
6516 gdk_cursor_unref (cursor);
6519 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6521 load_set_model (GtkFileChooserDefault *impl)
6523 profile_start ("start", NULL);
6525 g_assert (impl->browse_files_model != NULL);
6526 g_assert (impl->sort_model == NULL);
6528 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6529 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6530 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6531 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6532 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6533 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6534 set_sort_column (impl);
6535 impl->list_sort_ascending = TRUE;
6536 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6538 g_signal_connect (impl->sort_model, "sort-column-changed",
6539 G_CALLBACK (list_sort_column_changed_cb), impl);
6541 profile_msg (" gtk_tree_view_set_model start", NULL);
6542 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6543 GTK_TREE_MODEL (impl->sort_model));
6544 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6545 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6546 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6547 profile_msg (" gtk_tree_view_set_model end", NULL);
6549 profile_end ("end", NULL);
6552 /* Timeout callback used when the loading timer expires */
6554 load_timeout_cb (gpointer data)
6556 GtkFileChooserDefault *impl;
6558 profile_start ("start", NULL);
6560 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6561 g_assert (impl->load_state == LOAD_PRELOAD);
6562 g_assert (impl->load_timeout_id != 0);
6563 g_assert (impl->browse_files_model != NULL);
6565 impl->load_timeout_id = 0;
6566 impl->load_state = LOAD_LOADING;
6568 load_set_model (impl);
6570 profile_end ("end", NULL);
6575 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6577 load_setup_timer (GtkFileChooserDefault *impl)
6579 g_assert (impl->load_timeout_id == 0);
6580 g_assert (impl->load_state != LOAD_PRELOAD);
6582 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6583 impl->load_state = LOAD_PRELOAD;
6586 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6588 load_remove_timer (GtkFileChooserDefault *impl)
6590 if (impl->load_timeout_id != 0)
6592 g_assert (impl->load_state == LOAD_PRELOAD);
6594 g_source_remove (impl->load_timeout_id);
6595 impl->load_timeout_id = 0;
6596 impl->load_state = LOAD_EMPTY;
6599 g_assert (impl->load_state == LOAD_EMPTY ||
6600 impl->load_state == LOAD_LOADING ||
6601 impl->load_state == LOAD_FINISHED);
6604 /* Selects the first row in the file list */
6606 browse_files_select_first_row (GtkFileChooserDefault *impl)
6609 GtkTreeIter dummy_iter;
6610 GtkTreeModel *tree_model;
6612 if (!impl->sort_model)
6615 path = gtk_tree_path_new_from_indices (0, -1);
6616 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6618 /* If the list is empty, do nothing. */
6619 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6620 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6622 gtk_tree_path_free (path);
6625 struct center_selected_row_closure {
6626 GtkFileChooserDefault *impl;
6627 gboolean already_centered;
6630 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6631 * selected row in the tree view.
6634 center_selected_row_foreach_cb (GtkTreeModel *model,
6639 struct center_selected_row_closure *closure;
6642 if (closure->already_centered)
6645 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6646 closure->already_centered = TRUE;
6649 /* Centers the selected row in the tree view */
6651 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6653 struct center_selected_row_closure closure;
6654 GtkTreeSelection *selection;
6656 closure.impl = impl;
6657 closure.already_centered = FALSE;
6659 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6660 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6663 struct ShowAndSelectPathsData
6665 GtkFileChooserDefault *impl;
6670 show_and_select_files_finished_loading (GtkFolder *folder,
6673 gboolean have_hidden;
6674 gboolean have_filtered;
6676 struct ShowAndSelectPathsData *data = user_data;
6678 have_hidden = FALSE;
6679 have_filtered = FALSE;
6681 for (l = data->files; l; l = l->next)
6688 info = _gtk_folder_get_info (folder, file);
6692 have_hidden = g_file_info_get_is_hidden (info)
6693 || g_file_info_get_is_backup (info);
6696 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6697 get_is_file_filtered (data->impl, file, info);
6699 g_object_unref (info);
6701 if (have_hidden && have_filtered)
6702 break; /* we now have all the information we need */
6706 g_signal_handlers_disconnect_by_func (folder,
6707 show_and_select_files_finished_loading,
6711 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6714 set_current_filter (data->impl, NULL);
6716 for (l = data->files; l; l = l->next)
6721 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6722 select_func, data->impl);
6725 browse_files_center_selected_row (data->impl);
6727 g_object_unref (data->impl);
6728 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6729 g_slist_free (data->files);
6734 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6736 const GError *error,
6739 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6740 struct ShowAndSelectPathsData *data = user_data;
6742 if (data->impl->show_and_select_files_cancellable != cancellable)
6745 data->impl->show_and_select_files_cancellable = NULL;
6747 if (cancelled || error)
6750 g_object_unref (cancellable);
6752 if (_gtk_folder_is_finished_loading (folder))
6753 show_and_select_files_finished_loading (folder, user_data);
6755 g_signal_connect (folder, "finished-loading",
6756 G_CALLBACK (show_and_select_files_finished_loading),
6762 g_object_unref (data->impl);
6763 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6764 g_slist_free (data->files);
6767 g_object_unref (cancellable);
6771 show_and_select_files (GtkFileChooserDefault *impl,
6776 struct ShowAndSelectPathsData *info;
6778 profile_start ("start", NULL);
6782 profile_end ("end", NULL);
6786 info = g_new (struct ShowAndSelectPathsData, 1);
6787 info->impl = g_object_ref (impl);
6788 info->files = g_slist_copy (files);
6789 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6791 if (impl->show_and_select_files_cancellable)
6792 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6794 impl->show_and_select_files_cancellable =
6795 _gtk_file_system_get_folder (impl->file_system, parent_file,
6796 "standard::is-hidden,standard::is-backup,standard::type,standard::name,standard::content-type",
6797 show_and_select_files_get_folder_cb, info);
6799 profile_end ("end", NULL);
6803 /* Processes the pending operation when a folder is finished loading */
6805 pending_select_files_process (GtkFileChooserDefault *impl)
6807 g_assert (impl->load_state == LOAD_FINISHED);
6808 g_assert (impl->browse_files_model != NULL);
6809 g_assert (impl->sort_model != NULL);
6811 if (impl->pending_select_files)
6814 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6815 pending_select_files_free (impl);
6816 browse_files_center_selected_row (impl);
6820 /* We only select the first row if the chooser is actually mapped ---
6821 * selecting the first row is to help the user when he is interacting with
6822 * the chooser, but sometimes a chooser works not on behalf of the user,
6823 * but rather on behalf of something else like GtkFileChooserButton. In
6824 * that case, the chooser's selection should be what the caller expects,
6825 * as the user can't see that something else got selected. See bug #165264.
6827 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6828 browse_files_select_first_row (impl);
6831 g_assert (impl->pending_select_files == NULL);
6834 /* Callback used when the file system model finishes loading */
6836 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6837 GtkFileChooserDefault *impl)
6839 profile_start ("start", NULL);
6841 if (impl->load_state == LOAD_PRELOAD)
6843 load_remove_timer (impl);
6844 load_set_model (impl);
6846 else if (impl->load_state == LOAD_LOADING)
6852 /* We can't g_assert_not_reached(), as something other than us may have
6853 * initiated a folder reload. See #165556.
6855 profile_end ("end", NULL);
6859 g_assert (impl->load_timeout_id == 0);
6861 impl->load_state = LOAD_FINISHED;
6863 pending_select_files_process (impl);
6864 set_busy_cursor (impl, FALSE);
6865 #ifdef PROFILE_FILE_CHOOSER
6866 access ("MARK: *** FINISHED LOADING", F_OK);
6869 profile_end ("end", NULL);
6873 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6875 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6877 if (impl->browse_files_model)
6879 g_object_unref (impl->browse_files_model);
6880 impl->browse_files_model = NULL;
6883 if (impl->sort_model)
6885 g_object_unref (impl->sort_model);
6886 impl->sort_model = NULL;
6889 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6892 /* Gets rid of the old list model and creates a new one for the current folder */
6894 set_list_model (GtkFileChooserDefault *impl,
6897 g_assert (impl->current_folder != NULL);
6899 profile_start ("start", NULL);
6901 stop_loading_and_clear_list_model (impl);
6903 set_busy_cursor (impl, TRUE);
6904 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6906 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6907 impl->current_folder, 0,
6908 "standard,time,thumbnail::*",
6910 if (!impl->browse_files_model)
6912 set_busy_cursor (impl, FALSE);
6913 profile_end ("end", NULL);
6917 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6919 g_signal_connect (impl->browse_files_model, "finished-loading",
6920 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6922 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6924 install_list_model_filter (impl);
6926 profile_end ("end", NULL);
6931 struct update_chooser_entry_selected_foreach_closure {
6933 GtkTreeIter first_selected_iter;
6937 compare_utf8_filenames (const gchar *a,
6940 gchar *a_folded, *b_folded;
6943 a_folded = g_utf8_strdown (a, -1);
6944 b_folded = g_utf8_strdown (b, -1);
6946 retval = strcmp (a_folded, b_folded);
6955 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6960 struct update_chooser_entry_selected_foreach_closure *closure;
6963 closure->num_selected++;
6965 if (closure->num_selected == 1)
6966 closure->first_selected_iter = *iter;
6970 update_chooser_entry (GtkFileChooserDefault *impl)
6972 GtkTreeSelection *selection;
6973 struct update_chooser_entry_selected_foreach_closure closure;
6974 const char *file_part;
6976 /* no need to update the file chooser's entry if there's no entry */
6977 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6978 impl->operation_mode == OPERATION_MODE_RECENT ||
6979 !impl->location_entry)
6982 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6983 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6984 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6985 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6986 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6989 g_assert (impl->location_entry != NULL);
6991 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6992 closure.num_selected = 0;
6993 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6997 if (closure.num_selected == 0)
6999 goto maybe_clear_entry;
7001 else if (closure.num_selected == 1)
7003 GtkTreeIter child_iter;
7005 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7008 gboolean change_entry;
7010 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7012 &closure.first_selected_iter);
7014 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7016 /* If the cursor moved to the row of the newly created folder,
7017 * retrieving info will return NULL.
7022 g_free (impl->browse_files_last_selected_name);
7023 impl->browse_files_last_selected_name =
7024 g_strdup (g_file_info_get_display_name (info));
7026 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7027 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7028 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7030 /* We don't want the name to change when clicking on a folder... */
7031 change_entry = (! _gtk_file_info_consider_as_directory (info));
7034 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7038 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7040 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7041 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7049 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7050 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7052 /* Multiple selection, so just clear the entry. */
7054 g_free (impl->browse_files_last_selected_name);
7055 impl->browse_files_last_selected_name = NULL;
7057 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7063 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7064 && impl->browse_files_last_selected_name)
7066 const char *entry_text;
7068 gboolean clear_entry;
7070 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7071 len = strlen (entry_text);
7074 /* The file chooser entry may have appended a "/" to its text. So
7075 * take it out, and compare the result to the old selection.
7077 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7081 tmp = g_strndup (entry_text, len - 1);
7082 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7086 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7089 clear_entry = FALSE;
7092 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7097 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7101 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7105 struct UpdateCurrentFolderData
7107 GtkFileChooserDefault *impl;
7109 gboolean keep_trail;
7110 gboolean clear_entry;
7111 GFile *original_file;
7112 GError *original_error;
7116 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7117 GtkFileSystemVolume *volume,
7118 const GError *error,
7121 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7122 struct UpdateCurrentFolderData *data = user_data;
7123 GtkFileChooserDefault *impl = data->impl;
7125 if (cancellable != impl->update_current_folder_cancellable)
7128 impl->update_current_folder_cancellable = NULL;
7129 set_busy_cursor (impl, FALSE);
7136 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7137 impl->reload_state = RELOAD_EMPTY;
7141 change_folder_and_display_error (impl, data->file, data->clear_entry);
7144 g_object_unref (data->file);
7147 g_object_unref (cancellable);
7151 update_current_folder_get_info_cb (GCancellable *cancellable,
7153 const GError *error,
7156 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7157 struct UpdateCurrentFolderData *data = user_data;
7158 GtkFileChooserDefault *impl = data->impl;
7160 if (cancellable != impl->update_current_folder_cancellable)
7163 impl->update_current_folder_cancellable = NULL;
7164 impl->reload_state = RELOAD_EMPTY;
7166 set_busy_cursor (impl, FALSE);
7175 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7177 GMountOperation *mount_operation;
7178 GtkWidget *toplevel;
7180 g_object_unref (cancellable);
7181 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7183 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7185 set_busy_cursor (impl, TRUE);
7187 impl->update_current_folder_cancellable =
7188 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7190 update_current_folder_mount_enclosing_volume_cb,
7196 if (!data->original_file)
7198 data->original_file = g_object_ref (data->file);
7199 data->original_error = g_error_copy (error);
7202 parent_file = g_file_get_parent (data->file);
7204 /* get parent path and try to change the folder to that */
7207 g_object_unref (data->file);
7208 data->file = parent_file;
7210 g_object_unref (cancellable);
7212 /* restart the update current folder operation */
7213 impl->reload_state = RELOAD_HAS_FOLDER;
7215 impl->update_current_folder_cancellable =
7216 _gtk_file_system_get_info (impl->file_system, data->file,
7218 update_current_folder_get_info_cb,
7221 set_busy_cursor (impl, TRUE);
7227 /* Error and bail out, ignoring "not found" errors since they're useless:
7228 * they only happen when a program defaults to a folder that has been (re)moved.
7230 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7231 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7233 g_error_free (data->original_error);
7235 g_object_unref (data->original_file);
7241 if (data->original_file)
7243 /* Error and bail out, ignoring "not found" errors since they're useless:
7244 * they only happen when a program defaults to a folder that has been (re)moved.
7246 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7247 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7249 g_error_free (data->original_error);
7251 g_object_unref (data->original_file);
7254 if (! _gtk_file_info_consider_as_directory (info))
7257 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7260 if (impl->current_folder != data->file)
7262 if (impl->current_folder)
7263 g_object_unref (impl->current_folder);
7265 impl->current_folder = g_object_ref (data->file);
7268 impl->reload_state = RELOAD_HAS_FOLDER;
7270 /* Update the widgets that may trigger a folder change themselves. */
7272 if (!impl->changing_folder)
7274 impl->changing_folder = TRUE;
7276 shortcuts_update_current_folder (impl);
7278 impl->changing_folder = FALSE;
7281 /* Set the folder on the save entry */
7283 if (impl->location_entry)
7285 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7286 impl->current_folder);
7288 if (data->clear_entry)
7289 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7292 /* Create a new list model. This is slightly evil; we store the result value
7293 * but perform more actions rather than returning immediately even if it
7294 * generates an error.
7296 set_list_model (impl, NULL);
7298 /* Refresh controls */
7300 shortcuts_find_current_folder (impl);
7302 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7304 check_preview_change (impl);
7305 bookmarks_check_add_sensitivity (impl);
7307 g_signal_emit_by_name (impl, "selection-changed", 0);
7310 g_object_unref (data->file);
7313 g_object_unref (cancellable);
7317 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7319 gboolean keep_trail,
7320 gboolean clear_entry,
7323 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7324 struct UpdateCurrentFolderData *data;
7326 profile_start ("start", NULL);
7328 g_object_ref (file);
7330 switch (impl->operation_mode)
7332 case OPERATION_MODE_SEARCH:
7333 search_switch_to_browse_mode (impl);
7335 case OPERATION_MODE_RECENT:
7336 recent_switch_to_browse_mode (impl);
7338 case OPERATION_MODE_BROWSE:
7342 if (impl->local_only && !g_file_is_native (file))
7344 g_set_error_literal (error,
7345 GTK_FILE_CHOOSER_ERROR,
7346 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7347 _("Cannot change to folder because it is not local"));
7349 g_object_unref (file);
7350 profile_end ("end - not local", NULL);
7354 if (impl->update_current_folder_cancellable)
7355 g_cancellable_cancel (impl->update_current_folder_cancellable);
7357 /* Test validity of path here. */
7358 data = g_new0 (struct UpdateCurrentFolderData, 1);
7360 data->file = g_object_ref (file);
7361 data->keep_trail = keep_trail;
7362 data->clear_entry = clear_entry;
7364 impl->reload_state = RELOAD_HAS_FOLDER;
7366 impl->update_current_folder_cancellable =
7367 _gtk_file_system_get_info (impl->file_system, file,
7369 update_current_folder_get_info_cb,
7372 set_busy_cursor (impl, TRUE);
7373 g_object_unref (file);
7375 profile_end ("end", NULL);
7380 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7382 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7384 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7385 impl->operation_mode == OPERATION_MODE_RECENT)
7388 if (impl->reload_state == RELOAD_EMPTY)
7390 char *current_working_dir;
7393 /* We are unmapped, or we had an error while loading the last folder. We'll return
7394 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7395 * explicitly calls set_current_folder() on us.
7397 current_working_dir = g_get_current_dir ();
7398 file = g_file_new_for_path (current_working_dir);
7399 g_free (current_working_dir);
7403 if (impl->current_folder)
7404 return g_object_ref (impl->current_folder);
7410 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7413 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7415 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7416 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7418 pending_select_files_free (impl);
7419 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7423 select_func (GtkFileSystemModel *model,
7428 GtkFileChooserDefault *impl = user_data;
7429 GtkTreeSelection *selection;
7430 GtkTreeIter sorted_iter;
7432 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7434 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7435 gtk_tree_selection_select_iter (selection, &sorted_iter);
7439 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7443 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7447 parent_file = g_file_get_parent (file);
7450 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7452 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7453 impl->operation_mode == OPERATION_MODE_RECENT ||
7454 impl->load_state == LOAD_EMPTY)
7460 g_assert (impl->current_folder != NULL);
7462 same_path = g_file_equal (parent_file, impl->current_folder);
7465 if (same_path && impl->load_state == LOAD_FINISHED)
7470 files.data = (gpointer) file;
7473 result = show_and_select_files (impl, parent_file, &files, error);
7474 g_object_unref (parent_file);
7478 pending_select_files_add (impl, file);
7484 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7485 g_object_unref (parent_file);
7489 g_object_unref (parent_file);
7494 unselect_func (GtkFileSystemModel *model,
7499 GtkFileChooserDefault *impl = user_data;
7500 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7501 GtkTreePath *sorted_path;
7503 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7505 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7507 gtk_tree_path_free (sorted_path);
7511 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7514 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7516 if (!impl->browse_files_model)
7519 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7520 unselect_func, impl);
7524 maybe_select (GtkTreeModel *model,
7529 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7530 GtkTreeSelection *selection;
7534 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7536 info = get_list_file_info (impl, iter);
7537 is_folder = _gtk_file_info_consider_as_directory (info);
7539 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7540 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7541 gtk_tree_selection_select_iter (selection, iter);
7543 gtk_tree_selection_unselect_iter (selection, iter);
7549 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7551 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7553 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7554 impl->operation_mode == OPERATION_MODE_RECENT)
7556 GtkTreeSelection *selection;
7558 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7559 gtk_tree_selection_select_all (selection);
7563 if (impl->select_multiple)
7564 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7565 maybe_select, impl);
7569 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7571 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7572 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7574 gtk_tree_selection_unselect_all (selection);
7575 pending_select_files_free (impl);
7578 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7580 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7582 * is_empty_ret - whether the file entry is totally empty
7584 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7585 * the path will be "$cwd/foobar")
7588 check_save_entry (GtkFileChooserDefault *impl,
7590 gboolean *is_well_formed_ret,
7591 gboolean *is_empty_ret,
7592 gboolean *is_file_part_empty_ret,
7593 gboolean *is_folder)
7595 GtkFileChooserEntry *chooser_entry;
7596 GFile *current_folder;
7597 const char *file_part;
7601 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7602 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7603 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7604 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7605 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7607 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7609 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7612 *is_well_formed_ret = TRUE;
7613 *is_empty_ret = TRUE;
7614 *is_file_part_empty_ret = TRUE;
7620 *is_empty_ret = FALSE;
7622 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7623 if (!current_folder)
7626 *is_well_formed_ret = FALSE;
7627 *is_file_part_empty_ret = FALSE;
7633 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7635 if (!file_part || file_part[0] == '\0')
7637 *file_ret = g_object_ref (current_folder);
7638 *is_well_formed_ret = TRUE;
7639 *is_file_part_empty_ret = TRUE;
7645 *is_file_part_empty_ret = FALSE;
7648 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7652 error_building_filename_dialog (impl, error);
7654 *is_well_formed_ret = FALSE;
7661 *is_well_formed_ret = TRUE;
7662 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7665 struct get_files_closure {
7666 GtkFileChooserDefault *impl;
7668 GFile *file_from_entry;
7672 get_files_foreach (GtkTreeModel *model,
7677 struct get_files_closure *info;
7679 GtkFileSystemModel *fs_model;
7680 GtkTreeIter sel_iter;
7683 fs_model = info->impl->browse_files_model;
7684 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7686 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7688 return; /* We are on the editable row */
7690 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7691 info->result = g_slist_prepend (info->result, g_object_ref (file));
7695 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7697 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7698 struct get_files_closure info;
7699 GtkWindow *toplevel;
7700 GtkWidget *current_focus;
7701 gboolean file_list_seen;
7703 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7704 return search_get_selected_files (impl);
7706 if (impl->operation_mode == OPERATION_MODE_RECENT)
7707 return recent_get_selected_files (impl);
7711 info.file_from_entry = NULL;
7713 toplevel = get_toplevel (GTK_WIDGET (impl));
7715 current_focus = gtk_window_get_focus (toplevel);
7717 current_focus = NULL;
7719 file_list_seen = FALSE;
7720 if (current_focus == impl->browse_files_tree_view)
7722 GtkTreeSelection *selection;
7726 file_list_seen = TRUE;
7727 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7728 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7730 /* If there is no selection in the file list, we probably have this situation:
7732 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7733 * 2. He then double-clicked on a folder ("bar") in the file list
7735 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7736 * filename entry to see if that is the case.
7738 if (info.result == NULL && impl->location_entry)
7741 else if (impl->location_entry && current_focus == impl->location_entry)
7743 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7747 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7752 if (!is_well_formed)
7755 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7757 g_object_unref (info.file_from_entry);
7761 if (info.file_from_entry)
7762 info.result = g_slist_prepend (info.result, info.file_from_entry);
7763 else if (!file_list_seen)
7768 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7770 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7774 /* The focus is on a dialog's action area button or something else */
7775 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7776 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7784 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7785 * fall back to the current directory */
7786 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7787 info.result == NULL)
7789 GFile *current_folder;
7791 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7794 info.result = g_slist_prepend (info.result, current_folder);
7797 return g_slist_reverse (info.result);
7801 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7803 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7805 if (impl->preview_file)
7806 return g_object_ref (impl->preview_file);
7811 static GtkFileSystem *
7812 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7814 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7816 return impl->file_system;
7819 /* Shows or hides the filter widgets */
7821 show_filters (GtkFileChooserDefault *impl,
7825 gtk_widget_show (impl->filter_combo_hbox);
7827 gtk_widget_hide (impl->filter_combo_hbox);
7831 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7832 GtkFileFilter *filter)
7834 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7837 if (g_slist_find (impl->filters, filter))
7839 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7843 g_object_ref_sink (filter);
7844 impl->filters = g_slist_append (impl->filters, filter);
7846 name = gtk_file_filter_get_name (filter);
7848 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7850 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7852 if (!g_slist_find (impl->filters, impl->current_filter))
7853 set_current_filter (impl, filter);
7855 show_filters (impl, TRUE);
7859 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7860 GtkFileFilter *filter)
7862 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7863 GtkTreeModel *model;
7867 filter_index = g_slist_index (impl->filters, filter);
7869 if (filter_index < 0)
7871 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7875 impl->filters = g_slist_remove (impl->filters, filter);
7877 if (filter == impl->current_filter)
7880 set_current_filter (impl, impl->filters->data);
7882 set_current_filter (impl, NULL);
7885 /* Remove row from the combo box */
7886 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7887 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7888 g_assert_not_reached ();
7890 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7892 g_object_unref (filter);
7895 show_filters (impl, FALSE);
7899 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7901 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7903 return g_slist_copy (impl->filters);
7906 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7908 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7911 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7914 struct AddShortcutData
7916 GtkFileChooserDefault *impl;
7921 add_shortcut_get_info_cb (GCancellable *cancellable,
7923 const GError *error,
7927 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7928 struct AddShortcutData *data = user_data;
7930 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7933 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7935 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7938 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7940 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7943 g_object_unref (data->impl);
7944 g_object_unref (data->file);
7947 g_object_unref (cancellable);
7951 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7955 GCancellable *cancellable;
7956 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7957 struct AddShortcutData *data;
7961 /* Avoid adding duplicates */
7962 pos = shortcut_find_position (impl, file);
7963 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7967 uri = g_file_get_uri (file);
7968 /* translators, "Shortcut" means "Bookmark" here */
7970 GTK_FILE_CHOOSER_ERROR,
7971 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7972 _("Shortcut %s already exists"),
7979 for (l = impl->loading_shortcuts; l; l = l->next)
7981 GCancellable *c = l->data;
7984 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7985 if (f && g_file_equal (file, f))
7989 uri = g_file_get_uri (file);
7991 GTK_FILE_CHOOSER_ERROR,
7992 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7993 _("Shortcut %s already exists"),
8001 data = g_new0 (struct AddShortcutData, 1);
8002 data->impl = g_object_ref (impl);
8003 data->file = g_object_ref (file);
8005 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8007 add_shortcut_get_info_cb, data);
8012 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8013 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8019 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8023 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8030 for (l = impl->loading_shortcuts; l; l = l->next)
8032 GCancellable *c = l->data;
8035 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8036 if (f && g_file_equal (file, f))
8038 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8039 g_cancellable_cancel (c);
8044 if (impl->num_shortcuts == 0)
8047 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8048 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8049 g_assert_not_reached ();
8051 for (i = 0; i < impl->num_shortcuts; i++)
8054 ShortcutType shortcut_type;
8057 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8058 SHORTCUTS_COL_DATA, &col_data,
8059 SHORTCUTS_COL_TYPE, &shortcut_type,
8061 g_assert (col_data != NULL);
8062 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8064 shortcut = col_data;
8065 if (g_file_equal (shortcut, file))
8067 shortcuts_remove_rows (impl, pos + i, 1);
8068 impl->num_shortcuts--;
8072 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8073 g_assert_not_reached ();
8078 uri = g_file_get_uri (file);
8079 /* translators, "Shortcut" means "Bookmark" here */
8081 GTK_FILE_CHOOSER_ERROR,
8082 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8083 _("Shortcut %s does not exist"),
8091 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8093 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8099 if (impl->num_shortcuts == 0)
8102 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8103 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8104 g_assert_not_reached ();
8108 for (i = 0; i < impl->num_shortcuts; i++)
8111 ShortcutType shortcut_type;
8114 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8115 SHORTCUTS_COL_DATA, &col_data,
8116 SHORTCUTS_COL_TYPE, &shortcut_type,
8118 g_assert (col_data != NULL);
8119 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8121 shortcut = col_data;
8122 list = g_slist_prepend (list, g_object_ref (shortcut));
8124 if (i != impl->num_shortcuts - 1)
8126 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8127 g_assert_not_reached ();
8131 return g_slist_reverse (list);
8134 /* Guesses a size based upon font sizes */
8136 find_good_size_from_style (GtkWidget *widget,
8140 GtkFileChooserDefault *impl;
8145 g_assert (widget->style != NULL);
8146 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
8148 screen = gtk_widget_get_screen (widget);
8151 resolution = gdk_screen_get_resolution (screen);
8152 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8156 resolution = 96.0; /* wheeee */
8158 font_size = pango_font_description_get_size (widget->style->font_desc);
8159 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8161 *width = font_size * NUM_CHARS;
8162 *height = font_size * NUM_LINES;
8166 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8167 gint *default_width,
8168 gint *default_height)
8170 GtkFileChooserDefault *impl;
8173 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8175 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8176 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8177 || impl->expand_folders)
8179 GtkFileChooserSettings *settings;
8180 int x, y, width, height;
8182 settings = _gtk_file_chooser_settings_new ();
8183 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
8184 g_object_unref (settings);
8186 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8188 *default_width = width;
8189 *default_height = height;
8193 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8195 if (impl->preview_widget_active &&
8196 impl->preview_widget &&
8197 GTK_WIDGET_VISIBLE (impl->preview_widget))
8199 gtk_widget_size_request (impl->preview_box, &req);
8200 *default_width += PREVIEW_HBOX_SPACING + req.width;
8203 if (impl->extra_widget &&
8204 GTK_WIDGET_VISIBLE (impl->extra_widget))
8206 gtk_widget_size_request (impl->extra_align, &req);
8207 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8212 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8213 *default_width = req.width;
8214 *default_height = req.height;
8218 struct switch_folder_closure {
8219 GtkFileChooserDefault *impl;
8224 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8226 switch_folder_foreach_cb (GtkTreeModel *model,
8231 struct switch_folder_closure *closure;
8232 GtkTreeIter child_iter;
8236 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
8238 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
8239 closure->num_selected++;
8242 /* Changes to the selected folder in the list view */
8244 switch_to_selected_folder (GtkFileChooserDefault *impl)
8246 GtkTreeSelection *selection;
8247 struct switch_folder_closure closure;
8249 /* We do this with foreach() rather than get_selected() as we may be in
8250 * multiple selection mode
8253 closure.impl = impl;
8254 closure.file = NULL;
8255 closure.num_selected = 0;
8257 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8258 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8260 g_assert (closure.file && closure.num_selected == 1);
8262 change_folder_and_display_error (impl, closure.file, FALSE);
8265 /* Gets the GFileInfo for the selected row in the file list; assumes single
8269 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8270 gboolean *had_selection)
8272 GtkTreeSelection *selection;
8273 GtkTreeIter iter, child_iter;
8276 g_assert (!impl->select_multiple);
8277 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8278 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8280 *had_selection = FALSE;
8284 *had_selection = TRUE;
8286 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8290 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8294 /* Gets the display name of the selected file in the file list; assumes single
8295 * selection mode and that something is selected.
8297 static const gchar *
8298 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8301 gboolean had_selection;
8303 info = get_selected_file_info_from_file_list (impl, &had_selection);
8304 g_assert (had_selection);
8305 g_assert (info != NULL);
8307 return g_file_info_get_display_name (info);
8311 add_custom_button_to_dialog (GtkDialog *dialog,
8312 const gchar *mnemonic_label,
8313 const gchar *stock_id,
8318 button = gtk_button_new_with_mnemonic (mnemonic_label);
8319 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8320 gtk_button_set_image (GTK_BUTTON (button),
8321 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8322 gtk_widget_show (button);
8324 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8327 /* Presents an overwrite confirmation dialog; returns whether we should accept
8331 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8332 const gchar *file_part,
8333 const gchar *folder_display_name)
8335 GtkWindow *toplevel;
8339 toplevel = get_toplevel (GTK_WIDGET (impl));
8341 dialog = gtk_message_dialog_new (toplevel,
8342 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8343 GTK_MESSAGE_QUESTION,
8345 _("A file named \"%s\" already exists. Do you want to replace it?"),
8347 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8348 _("The file already exists in \"%s\". Replacing it will "
8349 "overwrite its contents."),
8350 folder_display_name);
8352 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8353 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8354 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8355 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8356 GTK_RESPONSE_ACCEPT,
8357 GTK_RESPONSE_CANCEL,
8359 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8361 if (toplevel->group)
8362 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8364 response = gtk_dialog_run (GTK_DIALOG (dialog));
8366 gtk_widget_destroy (dialog);
8368 return (response == GTK_RESPONSE_ACCEPT);
8371 struct GetDisplayNameData
8373 GtkFileChooserDefault *impl;
8378 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8380 const GError *error,
8383 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8384 gboolean should_respond = FALSE;
8385 struct GetDisplayNameData *data = user_data;
8387 if (cancellable != data->impl->should_respond_get_info_cancellable)
8390 data->impl->should_respond_get_info_cancellable = NULL;
8396 /* Huh? Did the folder disappear? Let the caller deal with it */
8397 should_respond = TRUE;
8399 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8401 set_busy_cursor (data->impl, FALSE);
8403 g_signal_emit_by_name (data->impl, "response-requested");
8406 g_object_unref (data->impl);
8407 g_free (data->file_part);
8410 g_object_unref (cancellable);
8413 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8414 * should respond. Can get the file part from the file list or the save entry.
8417 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8418 const gchar *file_part,
8421 GtkFileChooserConfirmation conf;
8423 if (!impl->do_overwrite_confirmation)
8426 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8428 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8432 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8434 struct GetDisplayNameData *data;
8436 g_assert (file_part != NULL);
8438 data = g_new0 (struct GetDisplayNameData, 1);
8439 data->impl = g_object_ref (impl);
8440 data->file_part = g_strdup (file_part);
8442 if (impl->should_respond_get_info_cancellable)
8443 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8445 impl->should_respond_get_info_cancellable =
8446 _gtk_file_system_get_info (impl->file_system, parent_file,
8447 "standard::display-name",
8448 confirmation_confirm_get_info_cb,
8450 set_busy_cursor (data->impl, TRUE);
8454 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8457 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8461 g_assert_not_reached ();
8466 struct FileExistsData
8468 GtkFileChooserDefault *impl;
8469 gboolean file_exists_and_is_not_folder;
8475 save_entry_get_info_cb (GCancellable *cancellable,
8477 const GError *error,
8480 gboolean parent_is_folder;
8481 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8482 struct FileExistsData *data = user_data;
8484 if (cancellable != data->impl->should_respond_get_info_cancellable)
8487 data->impl->should_respond_get_info_cancellable = NULL;
8489 set_busy_cursor (data->impl, FALSE);
8495 parent_is_folder = FALSE;
8497 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8499 if (parent_is_folder)
8501 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8503 if (data->file_exists_and_is_not_folder)
8508 /* Dup the string because the string may be modified
8509 * depending on what clients do in the confirm-overwrite
8510 * signal and this corrupts the pointer
8512 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8513 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8517 g_signal_emit_by_name (data->impl, "response-requested");
8520 g_signal_emit_by_name (data->impl, "response-requested");
8522 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8524 GError *error = NULL;
8526 set_busy_cursor (data->impl, TRUE);
8527 g_file_make_directory (data->file, NULL, &error);
8528 set_busy_cursor (data->impl, FALSE);
8531 g_signal_emit_by_name (data->impl, "response-requested");
8533 error_creating_folder_dialog (data->impl, data->file, error);
8538 /* This will display an error, which is what we want */
8539 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8543 g_object_unref (data->impl);
8544 g_object_unref (data->file);
8545 g_object_unref (data->parent_file);
8548 g_object_unref (cancellable);
8552 file_exists_get_info_cb (GCancellable *cancellable,
8554 const GError *error,
8557 gboolean data_ownership_taken = FALSE;
8558 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8559 gboolean file_exists_and_is_not_folder;
8560 struct FileExistsData *data = user_data;
8562 if (cancellable != data->impl->file_exists_get_info_cancellable)
8565 data->impl->file_exists_get_info_cancellable = NULL;
8567 set_busy_cursor (data->impl, FALSE);
8572 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8574 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8575 /* user typed a filename; we are done */
8576 g_signal_emit_by_name (data->impl, "response-requested");
8577 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8578 && file_exists_and_is_not_folder)
8580 /* Oops, the user typed the name of an existing path which is not
8583 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8584 g_error_copy (error));
8588 /* check that everything up to the last component exists */
8590 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8591 data_ownership_taken = TRUE;
8593 if (data->impl->should_respond_get_info_cancellable)
8594 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8596 data->impl->should_respond_get_info_cancellable =
8597 _gtk_file_system_get_info (data->impl->file_system,
8600 save_entry_get_info_cb,
8602 set_busy_cursor (data->impl, TRUE);
8606 if (!data_ownership_taken)
8608 g_object_unref (data->impl);
8609 g_object_unref (data->file);
8610 g_object_unref (data->parent_file);
8614 g_object_unref (cancellable);
8618 paste_text_received (GtkClipboard *clipboard,
8620 GtkFileChooserDefault *impl)
8627 file = g_file_new_for_uri (text);
8629 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8630 location_popup_handler (impl, text);
8632 g_object_unref (file);
8635 /* Handler for the "location-popup-on-paste" keybinding signal */
8637 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8639 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8640 GDK_SELECTION_CLIPBOARD);
8641 gtk_clipboard_request_text (clipboard,
8642 (GtkClipboardTextReceivedFunc) paste_text_received,
8647 /* Implementation for GtkFileChooserEmbed::should_respond() */
8649 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8651 GtkFileChooserDefault *impl;
8652 GtkWidget *toplevel;
8653 GtkWidget *current_focus;
8655 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8657 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8658 g_assert (GTK_IS_WINDOW (toplevel));
8660 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8662 if (current_focus == impl->browse_files_tree_view)
8664 /* The following array encodes what we do based on the impl->action and the
8665 * number of files selected.
8668 NOOP, /* Do nothing (don't respond) */
8669 RESPOND, /* Respond immediately */
8670 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8671 ALL_FILES, /* Respond only if everything selected is a file */
8672 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8673 SAVE_ENTRY, /* Go to the code for handling the save entry */
8674 NOT_REACHED /* Sanity check */
8676 static const ActionToTake what_to_do[4][3] = {
8677 /* 0 selected 1 selected many selected */
8678 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8679 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8680 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8681 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8685 gboolean all_files, all_folders;
8687 ActionToTake action;
8691 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8693 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8694 return search_should_respond (impl);
8696 if (impl->operation_mode == OPERATION_MODE_RECENT)
8697 return recent_should_respond (impl);
8699 selection_check (impl, &num_selected, &all_files, &all_folders);
8701 if (num_selected > 2)
8706 action = what_to_do [impl->action] [k];
8716 case RESPOND_OR_SWITCH:
8717 g_assert (num_selected == 1);
8721 switch_to_selected_folder (impl);
8724 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8725 return should_respond_after_confirm_overwrite (impl,
8726 get_display_name_from_file_list (impl),
8727 impl->current_folder);
8741 g_assert_not_reached ();
8744 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8747 gboolean is_well_formed, is_empty, is_file_part_empty;
8750 GtkFileChooserEntry *entry;
8755 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8756 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8757 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8758 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8759 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8761 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8762 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8764 if (is_empty || !is_well_formed)
8767 g_assert (file != NULL);
8772 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8773 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8775 change_folder_and_display_error (impl, file, TRUE);
8778 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8779 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8781 /* The folder already exists, so we do not need to create it.
8782 * Just respond to terminate the dialog.
8788 g_assert_not_reached ();
8794 struct FileExistsData *data;
8796 /* We need to check whether file exists and is not a folder */
8798 data = g_new0 (struct FileExistsData, 1);
8799 data->impl = g_object_ref (impl);
8800 data->file = g_object_ref (file);
8801 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8803 if (impl->file_exists_get_info_cancellable)
8804 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8806 impl->file_exists_get_info_cancellable =
8807 _gtk_file_system_get_info (impl->file_system, file,
8809 file_exists_get_info_cb,
8812 set_busy_cursor (impl, TRUE);
8816 g_error_free (error);
8819 g_object_unref (file);
8822 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8824 /* The focus is on a dialog's action area button, *and* the widget that
8825 * was focused immediately before it is the file list.
8829 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8831 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8834 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8836 /* The focus is on a dialog's action area button, *and* the widget that
8837 * was focused immediately before it is the location entry.
8842 /* The focus is on a dialog's action area button or something else */
8843 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8844 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8849 g_assert_not_reached ();
8853 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8855 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8857 GtkFileChooserDefault *impl;
8860 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8862 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8863 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8865 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8866 widget = impl->browse_files_tree_view;
8868 widget = impl->location_entry;
8870 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8871 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8872 widget = impl->location_entry;
8875 g_assert_not_reached ();
8879 g_assert (widget != NULL);
8880 gtk_widget_grab_focus (widget);
8883 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8885 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8895 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8896 *list = g_slist_prepend (*list, g_object_ref (file));
8899 /* Constructs a list of the selected paths in search mode */
8901 search_get_selected_files (GtkFileChooserDefault *impl)
8904 GtkTreeSelection *selection;
8908 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8909 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8910 result = g_slist_reverse (result);
8915 /* Called from ::should_respond(). We return whether there are selected files
8916 * in the search list.
8919 search_should_respond (GtkFileChooserDefault *impl)
8921 GtkTreeSelection *selection;
8923 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8925 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8926 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8929 struct SearchHitInsertRequest
8931 GtkFileChooserDefault *impl;
8933 GtkTreeRowReference *row_ref;
8937 search_hit_get_info_cb (GCancellable *cancellable,
8939 const GError *error,
8942 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8943 GdkPixbuf *pixbuf = NULL;
8946 GCancellable *model_cancellable;
8947 gboolean is_folder = FALSE;
8949 guint64 modification_time = 0;
8953 struct SearchHitInsertRequest *request = data;
8955 if (!request->impl->search_model)
8958 path = gtk_tree_row_reference_get_path (request->row_ref);
8962 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8964 gtk_tree_path_free (path);
8966 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8967 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8969 if (cancellable != model_cancellable)
8972 /* set the cancellable to NULL in the model */
8973 gtk_list_store_set (request->impl->search_model, &iter,
8974 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8982 search_clear_model_row (GTK_TREE_MODEL (request->impl->search_model), &iter);
8983 gtk_list_store_remove (request->impl->search_model, &iter);
8987 display_name = g_strdup (g_file_info_get_display_name (info));
8988 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
8989 g_file_info_get_modification_time (info, &mtime);
8990 modification_time = (guint64) mtime.tv_sec;
8991 size = g_file_info_get_size (info);
8992 is_folder = _gtk_file_info_consider_as_directory (info);
8993 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8994 request->impl->icon_size);
8996 gtk_list_store_set (request->impl->search_model, &iter,
8997 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8998 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8999 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
9000 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
9001 SEARCH_MODEL_COL_MTIME, modification_time,
9002 SEARCH_MODEL_COL_SIZE, size,
9006 g_object_unref (pixbuf);
9009 g_object_unref (request->impl);
9010 g_object_unref (request->file);
9011 gtk_tree_row_reference_free (request->row_ref);
9014 g_object_unref (cancellable);
9017 /* Adds one hit from the search engine to the search_model */
9019 search_add_hit (GtkFileChooserDefault *impl,
9024 char *collation_key;
9027 GCancellable *cancellable;
9028 struct SearchHitInsertRequest *request;
9030 file = g_file_new_for_uri (uri);
9034 if (!g_file_is_native (file))
9036 g_object_unref (file);
9040 tmp = g_file_get_parse_name (file);
9041 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
9044 request = g_new0 (struct SearchHitInsertRequest, 1);
9045 request->impl = g_object_ref (impl);
9046 request->file = g_object_ref (file);
9048 gtk_list_store_append (impl->search_model, &iter);
9049 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
9051 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
9052 gtk_tree_path_free (p);
9054 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9057 "standard::content-type,"
9058 "standard::display-name,"
9061 search_hit_get_info_cb,
9064 gtk_list_store_set (impl->search_model, &iter,
9065 SEARCH_MODEL_COL_FILE, file,
9066 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
9067 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
9071 /* Callback used from GtkSearchEngine when we get new hits */
9073 search_engine_hits_added_cb (GtkSearchEngine *engine,
9077 GtkFileChooserDefault *impl;
9080 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9082 for (l = hits; l; l = l->next)
9083 search_add_hit (impl, (gchar*)l->data);
9086 /* Callback used from GtkSearchEngine when the query is done running */
9088 search_engine_finished_cb (GtkSearchEngine *engine,
9091 GtkFileChooserDefault *impl;
9093 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9096 /* EB: setting the model here will avoid loads of row events,
9097 * but it'll make the search look like blocked.
9099 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9100 GTK_TREE_MODEL (impl->search_model_filter));
9103 /* FMQ: if search was empty, say that we got no hits */
9104 set_busy_cursor (impl, FALSE);
9107 /* Displays a generic error when we cannot create a GtkSearchEngine.
9108 * It would be better if _gtk_search_engine_new() gave us a GError
9109 * with a better message, but it doesn't do that right now.
9112 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9114 error_message (impl,
9115 _("Could not start the search process"),
9116 _("The program was not able to create a connection to the indexer "
9117 "daemon. Please make sure it is running."));
9121 search_engine_error_cb (GtkSearchEngine *engine,
9122 const gchar *message,
9125 GtkFileChooserDefault *impl;
9127 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9129 search_stop_searching (impl, TRUE);
9130 error_message (impl, _("Could not send the search request"), message);
9132 set_busy_cursor (impl, FALSE);
9136 search_clear_model_row (GtkTreeModel *model,
9140 gchar *display_name;
9141 gchar *collation_key;
9142 GCancellable *cancellable;
9145 gtk_tree_model_get (model, iter,
9146 SEARCH_MODEL_COL_FILE, &file,
9147 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9148 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
9149 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
9150 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9154 g_object_unref (file);
9156 g_free (display_name);
9157 g_free (collation_key);
9161 g_cancellable_cancel (cancellable);
9164 /* Frees the data in the search_model */
9166 search_clear_model (GtkFileChooserDefault *impl,
9167 gboolean remove_from_treeview)
9169 GtkTreeModel *model;
9172 if (!impl->search_model)
9175 model = GTK_TREE_MODEL (impl->search_model);
9177 if (gtk_tree_model_get_iter_first (model, &iter))
9180 search_clear_model_row (model, &iter);
9182 while (gtk_tree_model_iter_next (model, &iter));
9184 g_object_unref (impl->search_model);
9185 impl->search_model = NULL;
9187 g_object_unref (impl->search_model_filter);
9188 impl->search_model_filter = NULL;
9190 g_object_unref (impl->search_model_sort);
9191 impl->search_model_sort = NULL;
9193 if (remove_from_treeview)
9194 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9197 /* Stops any ongoing searches; does not touch the search_model */
9199 search_stop_searching (GtkFileChooserDefault *impl,
9200 gboolean remove_query)
9202 if (remove_query && impl->search_query)
9204 g_object_unref (impl->search_query);
9205 impl->search_query = NULL;
9208 if (impl->search_engine)
9210 _gtk_search_engine_stop (impl->search_engine);
9212 g_object_unref (impl->search_engine);
9213 impl->search_engine = NULL;
9217 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9219 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9221 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9223 search_stop_searching (impl, FALSE);
9224 search_clear_model (impl, TRUE);
9226 gtk_widget_destroy (impl->search_hbox);
9227 impl->search_hbox = NULL;
9228 impl->search_entry = NULL;
9230 gtk_widget_show (impl->browse_path_bar);
9231 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9232 gtk_widget_hide (impl->browse_new_folder_button);
9234 gtk_widget_show (impl->browse_new_folder_button);
9237 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9238 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9240 gtk_widget_show (impl->location_button);
9242 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9243 gtk_widget_show (impl->location_entry_box);
9246 impl->operation_mode = OPERATION_MODE_BROWSE;
9248 file_list_set_sort_column_ids (impl);
9251 /* Sort callback from the path column */
9253 search_column_path_sort_func (GtkTreeModel *model,
9258 GtkFileChooserDefault *impl = user_data;
9259 GtkTreeIter child_a, child_b;
9260 const char *collation_key_a, *collation_key_b;
9261 gboolean is_folder_a, is_folder_b;
9263 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9264 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9266 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9267 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9268 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
9270 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9271 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9272 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
9275 if (!collation_key_a)
9278 if (!collation_key_b)
9281 /* always show folders first */
9282 if (is_folder_a != is_folder_b)
9283 return is_folder_a ? 1 : -1;
9285 return strcmp (collation_key_a, collation_key_b);
9288 /* Sort callback from the size column */
9290 search_column_size_sort_func (GtkTreeModel *model,
9295 GtkFileChooserDefault *impl = user_data;
9296 GtkTreeIter child_a, child_b;
9297 gboolean is_folder_a, is_folder_b;
9298 goffset size_a, size_b;
9300 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9301 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9303 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9304 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9305 SEARCH_MODEL_COL_SIZE, &size_a,
9307 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9308 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9309 SEARCH_MODEL_COL_SIZE, &size_b,
9312 if (is_folder_a != is_folder_b)
9313 return is_folder_a ? 1 : -1;
9315 if (size_a < size_b)
9317 else if (size_a > size_b)
9323 /* Sort callback from the modification time column */
9325 search_column_mtime_sort_func (GtkTreeModel *model,
9330 GtkFileChooserDefault *impl = user_data;
9331 GtkTreeIter child_a, child_b;
9332 gboolean is_folder_a, is_folder_b;
9333 guint64 mtime_a, mtime_b;
9335 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9336 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9338 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9339 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9340 SEARCH_MODEL_COL_MTIME, &mtime_a,
9342 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9343 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9344 SEARCH_MODEL_COL_MTIME, &mtime_b,
9347 if (is_folder_a != is_folder_b)
9348 return is_folder_a ? 1 : -1;
9350 if (mtime_a < mtime_b)
9352 else if (mtime_a > mtime_b)
9359 search_get_is_filtered (GtkFileChooserDefault *impl,
9361 const gchar *display_name,
9362 const gchar *mime_type)
9364 GtkFileFilterInfo filter_info;
9365 GtkFileFilterFlags needed;
9368 if (!impl->current_filter)
9371 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9372 needed = gtk_file_filter_get_needed (impl->current_filter);
9374 filter_info.display_name = display_name;
9375 filter_info.mime_type = mime_type;
9377 if (needed & GTK_FILE_FILTER_FILENAME)
9379 filter_info.filename = g_file_get_path (file);
9380 if (filter_info.filename)
9381 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9384 filter_info.filename = NULL;
9386 if (needed & GTK_FILE_FILTER_URI)
9388 filter_info.uri = g_file_get_uri (file);
9389 if (filter_info.uri)
9390 filter_info.contains |= GTK_FILE_FILTER_URI;
9393 filter_info.uri = NULL;
9395 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9397 if (filter_info.filename)
9398 g_free ((gchar *) filter_info.filename);
9399 if (filter_info.uri)
9400 g_free ((gchar *) filter_info.uri);
9406 /* Visibility function for the recent filter model */
9408 search_model_visible_func (GtkTreeModel *model,
9412 GtkFileChooserDefault *impl = user_data;
9414 gchar *display_name, *mime_type;
9417 if (!impl->current_filter)
9420 gtk_tree_model_get (model, iter,
9421 SEARCH_MODEL_COL_FILE, &file,
9422 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9423 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9424 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9433 return !search_get_is_filtered (impl, file, display_name, mime_type);
9436 /* Creates the search_model and puts it in the tree view */
9438 search_setup_model (GtkFileChooserDefault *impl)
9440 g_assert (impl->search_model == NULL);
9441 g_assert (impl->search_model_filter == NULL);
9442 g_assert (impl->search_model_sort == NULL);
9444 /* We store these columns in the search model:
9446 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9447 * as a pointer not as a G_TYPE_FILE
9448 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9449 * as a pointer not as a G_TYPE_STRING
9450 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9451 * as a pointer not as a G_TYPE_STRING
9452 * SEARCH_MODEL_COL_MTIME - G_TYPE_UINT64 for the modification time
9453 * SEARCH_MODEL_COL_SIZE - G_TYPE_INT64 for the size
9454 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9455 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9456 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9457 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9459 * Keep this in sync with the enumeration defined near the beginning
9462 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9463 G_TYPE_POINTER, /* file */
9464 G_TYPE_POINTER, /* display-name */
9465 G_TYPE_POINTER, /* collation-key */
9466 G_TYPE_UINT64, /* mtime */
9467 G_TYPE_INT64, /* size */
9468 G_TYPE_POINTER, /* cancellable */
9469 GDK_TYPE_PIXBUF, /* pixbuf */
9470 G_TYPE_POINTER, /* mime-type */
9471 G_TYPE_BOOLEAN /*is-folder */);
9473 impl->search_model_filter =
9474 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9475 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9476 search_model_visible_func,
9479 impl->search_model_sort =
9480 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9481 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9482 SEARCH_MODEL_COL_FILE,
9483 search_column_path_sort_func,
9485 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9486 SEARCH_MODEL_COL_MTIME,
9487 search_column_mtime_sort_func,
9489 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9490 SEARCH_MODEL_COL_SIZE,
9491 search_column_size_sort_func,
9493 set_sort_column (impl);
9495 /* EB: setting the model here will make the hits list update feel
9496 * more "alive" than setting the model at the end of the search
9499 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9500 GTK_TREE_MODEL (impl->search_model_sort));
9504 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9505 GtkTreeIter *child_iter,
9510 if (!impl->search_model)
9513 if (!impl->search_model_filter || !impl->search_model_sort)
9516 /* pass 1: get the iterator in the filter model */
9517 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9520 /* pass 2: get the iterator in the real model */
9521 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9522 child_iter, &middle);
9525 /* Creates a new query with the specified text and launches it */
9527 search_start_query (GtkFileChooserDefault *impl,
9528 const gchar *query_text)
9530 search_stop_searching (impl, FALSE);
9531 search_clear_model (impl, TRUE);
9532 search_setup_model (impl);
9533 set_busy_cursor (impl, TRUE);
9535 if (impl->search_engine == NULL)
9536 impl->search_engine = _gtk_search_engine_new ();
9538 if (!impl->search_engine)
9540 set_busy_cursor (impl, FALSE);
9541 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9545 if (!impl->search_query)
9547 impl->search_query = _gtk_query_new ();
9548 _gtk_query_set_text (impl->search_query, query_text);
9551 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9553 g_signal_connect (impl->search_engine, "hits-added",
9554 G_CALLBACK (search_engine_hits_added_cb), impl);
9555 g_signal_connect (impl->search_engine, "finished",
9556 G_CALLBACK (search_engine_finished_cb), impl);
9557 g_signal_connect (impl->search_engine, "error",
9558 G_CALLBACK (search_engine_error_cb), impl);
9560 _gtk_search_engine_start (impl->search_engine);
9563 /* Callback used when the user presses Enter while typing on the search
9564 * entry; starts the query
9567 search_entry_activate_cb (GtkEntry *entry,
9570 GtkFileChooserDefault *impl;
9573 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9575 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9576 if (strlen (text) == 0)
9579 /* reset any existing query object */
9580 if (impl->search_query)
9582 g_object_unref (impl->search_query);
9583 impl->search_query = NULL;
9586 search_start_query (impl, text);
9589 /* Hides the path bar and creates the search entry */
9591 search_setup_widgets (GtkFileChooserDefault *impl)
9597 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9601 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9602 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), image);
9603 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9607 label = gtk_label_new (NULL);
9608 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9609 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9610 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9615 impl->search_entry = gtk_entry_new ();
9616 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9617 g_signal_connect (impl->search_entry, "activate",
9618 G_CALLBACK (search_entry_activate_cb),
9620 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9622 /* if there already is a query, restart it */
9623 if (impl->search_query)
9625 gchar *query = _gtk_query_get_text (impl->search_query);
9629 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9630 search_start_query (impl, query);
9636 g_object_unref (impl->search_query);
9637 impl->search_query = NULL;
9641 gtk_widget_hide (impl->browse_path_bar);
9642 gtk_widget_hide (impl->browse_new_folder_button);
9644 /* Box for search widgets */
9645 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9646 gtk_widget_show_all (impl->search_hbox);
9648 /* Hide the location widgets temporarily */
9650 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9651 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9653 gtk_widget_hide (impl->location_button);
9654 gtk_widget_hide (impl->location_entry_box);
9657 gtk_widget_grab_focus (impl->search_entry);
9659 /* FMQ: hide the filter combo? */
9662 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9664 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9668 case OPERATION_MODE_BROWSE:
9669 stop_loading_and_clear_list_model (impl);
9672 case OPERATION_MODE_SEARCH:
9673 search_stop_searching (impl, FALSE);
9674 search_clear_model (impl, TRUE);
9676 gtk_widget_destroy (impl->search_hbox);
9677 impl->search_hbox = NULL;
9678 impl->search_entry = NULL;
9681 case OPERATION_MODE_RECENT:
9682 recent_stop_loading (impl);
9683 recent_clear_model (impl, TRUE);
9685 gtk_widget_destroy (impl->recent_hbox);
9686 impl->recent_hbox = NULL;
9691 /* Main entry point to the searching functions; this gets called when the user
9692 * activates the Search shortcut.
9695 search_activate (GtkFileChooserDefault *impl)
9697 OperationMode previous_mode;
9699 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9701 gtk_widget_grab_focus (impl->search_entry);
9705 previous_mode = impl->operation_mode;
9706 impl->operation_mode = OPERATION_MODE_SEARCH;
9708 stop_operation (impl, previous_mode);
9710 g_assert (impl->search_hbox == NULL);
9711 g_assert (impl->search_entry == NULL);
9712 g_assert (impl->search_model == NULL);
9713 g_assert (impl->search_model_filter == NULL);
9715 search_setup_widgets (impl);
9716 file_list_set_sort_column_ids (impl);
9720 * Recent files support
9723 /* Frees the data in the recent_model */
9725 recent_clear_model (GtkFileChooserDefault *impl,
9726 gboolean remove_from_treeview)
9728 GtkTreeModel *model;
9731 if (!impl->recent_model)
9734 model = GTK_TREE_MODEL (impl->recent_model);
9736 if (remove_from_treeview)
9737 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9739 if (gtk_tree_model_get_iter_first (model, &iter))
9744 GCancellable *cancellable;
9745 GtkRecentInfo *recent_info;
9746 gchar *display_name;
9748 gtk_tree_model_get (model, &iter,
9749 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9750 RECENT_MODEL_COL_FILE, &file,
9751 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9752 RECENT_MODEL_COL_INFO, &recent_info,
9756 g_cancellable_cancel (cancellable);
9758 g_object_unref (file);
9759 gtk_recent_info_unref (recent_info);
9760 g_free (display_name);
9762 while (gtk_tree_model_iter_next (model, &iter));
9765 g_object_unref (impl->recent_model);
9766 impl->recent_model = NULL;
9768 g_object_unref (impl->recent_model_filter);
9769 impl->recent_model_filter = NULL;
9771 g_object_unref (impl->recent_model_sort);
9772 impl->recent_model_sort = NULL;
9775 /* Stops any ongoing loading of the recent files list; does
9776 * not touch the recent_model
9779 recent_stop_loading (GtkFileChooserDefault *impl)
9781 if (impl->load_recent_id)
9783 g_source_remove (impl->load_recent_id);
9784 impl->load_recent_id = 0;
9788 /* Stops any pending load, clears the file list, and switches
9789 * back to OPERATION_MODE_BROWSE
9792 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9794 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9796 recent_stop_loading (impl);
9797 recent_clear_model (impl, TRUE);
9799 gtk_widget_destroy (impl->recent_hbox);
9800 impl->recent_hbox = NULL;
9802 gtk_widget_show (impl->browse_path_bar);
9803 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9804 gtk_widget_hide (impl->browse_new_folder_button);
9806 gtk_widget_show (impl->browse_new_folder_button);
9808 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9809 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9811 gtk_widget_show (impl->location_button);
9813 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9814 gtk_widget_show (impl->location_entry_box);
9817 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9819 impl->operation_mode = OPERATION_MODE_BROWSE;
9821 file_list_set_sort_column_ids (impl);
9824 /* Sort callback from the modification time column */
9826 recent_column_mtime_sort_func (GtkTreeModel *model,
9831 GtkFileChooserDefault *impl = user_data;
9832 GtkTreeIter child_a, child_b;
9833 GtkRecentInfo *info_a, *info_b;
9834 gboolean is_folder_a, is_folder_b;
9836 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9837 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9839 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9840 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9841 RECENT_MODEL_COL_INFO, &info_a,
9843 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9844 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9845 RECENT_MODEL_COL_INFO, &info_b,
9854 /* folders always go first */
9855 if (is_folder_a != is_folder_b)
9856 return is_folder_a ? 1 : -1;
9858 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9860 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9867 recent_column_path_sort_func (GtkTreeModel *model,
9872 GtkFileChooserDefault *impl = user_data;
9873 GtkTreeIter child_a, child_b;
9874 gboolean is_folder_a, is_folder_b;
9875 gchar *name_a, *name_b;
9877 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9878 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9880 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9881 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9882 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9884 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9885 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9886 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9895 if (is_folder_a != is_folder_b)
9896 return is_folder_a ? 1 : -1;
9898 return strcmp (name_a, name_b);
9902 recent_get_is_filtered (GtkFileChooserDefault *impl,
9904 GtkRecentInfo *recent_info)
9906 GtkFileFilterInfo filter_info;
9907 GtkFileFilterFlags needed;
9910 if (!impl->current_filter)
9913 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9914 needed = gtk_file_filter_get_needed (impl->current_filter);
9916 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9917 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9919 if (needed & GTK_FILE_FILTER_FILENAME)
9921 filter_info.filename = g_file_get_path (file);
9922 if (filter_info.filename)
9923 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9926 filter_info.filename = NULL;
9928 if (needed & GTK_FILE_FILTER_URI)
9930 filter_info.uri = g_file_get_uri (file);
9931 if (filter_info.uri)
9932 filter_info.contains |= GTK_FILE_FILTER_URI;
9935 filter_info.uri = NULL;
9937 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9939 if (filter_info.filename)
9940 g_free ((gchar *) filter_info.filename);
9941 if (filter_info.uri)
9942 g_free ((gchar *) filter_info.uri);
9947 /* Visibility function for the recent filter model */
9949 recent_model_visible_func (GtkTreeModel *model,
9953 GtkFileChooserDefault *impl = user_data;
9955 GtkRecentInfo *recent_info;
9958 if (!impl->current_filter)
9961 gtk_tree_model_get (model, iter,
9962 RECENT_MODEL_COL_INFO, &recent_info,
9963 RECENT_MODEL_COL_FILE, &file,
9964 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9973 return !recent_get_is_filtered (impl, file, recent_info);
9977 recent_setup_model (GtkFileChooserDefault *impl)
9979 g_assert (impl->recent_model == NULL);
9980 g_assert (impl->recent_model_filter == NULL);
9981 g_assert (impl->recent_model_sort == NULL);
9983 /* We store these columns in the search model:
9985 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9986 * stored as a pointer and not as a G_TYPE_FILE;
9987 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9988 * stored as a pointer and not as a G_TYPE_STRING;
9989 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9990 * as a GTK_TYPE_RECENT_INFO;
9991 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9992 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9993 * and not as a G_TYPE_CANCELLABLE;
9995 * Keep this in sync with the enumeration defined near the beginning of
9998 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
10005 impl->recent_model_filter =
10006 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
10007 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
10008 recent_model_visible_func,
10012 /* this is the model that will actually be added to
10013 * the browse_files_tree_view widget; remember: we are
10014 * stuffing the real model into a filter model and then
10015 * into a sort model; this means we'll have to translate
10016 * the child iterator *twice* to get from a path or an
10017 * iterator coming from the tree view widget to the
10018 * real data inside the model.
10020 impl->recent_model_sort =
10021 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
10022 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
10023 RECENT_MODEL_COL_FILE,
10024 recent_column_path_sort_func,
10026 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
10027 RECENT_MODEL_COL_INFO,
10028 recent_column_mtime_sort_func,
10030 set_sort_column (impl);
10035 GtkFileChooserDefault *impl;
10038 gint n_loaded_items;
10039 guint needs_sorting : 1;
10043 recent_idle_cleanup (gpointer data)
10045 RecentLoadData *load_data = data;
10046 GtkFileChooserDefault *impl = load_data->impl;
10048 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
10049 GTK_TREE_MODEL (impl->recent_model_sort));
10051 set_busy_cursor (impl, FALSE);
10053 impl->load_recent_id = 0;
10055 if (load_data->items)
10057 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10058 g_list_free (load_data->items);
10061 g_free (load_data);
10064 struct RecentItemInsertRequest
10066 GtkFileChooserDefault *impl;
10068 GtkTreeRowReference *row_ref;
10072 recent_item_get_info_cb (GCancellable *cancellable,
10074 const GError *error,
10077 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10080 GCancellable *model_cancellable;
10081 gboolean is_folder = FALSE;
10082 struct RecentItemInsertRequest *request = data;
10084 if (!request->impl->recent_model)
10087 path = gtk_tree_row_reference_get_path (request->row_ref);
10091 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
10093 gtk_tree_path_free (path);
10095 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
10096 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
10098 if (cancellable != model_cancellable)
10101 gtk_list_store_set (request->impl->recent_model, &iter,
10102 RECENT_MODEL_COL_CANCELLABLE, NULL,
10110 gtk_list_store_remove (request->impl->recent_model, &iter);
10114 is_folder = _gtk_file_info_consider_as_directory (info);
10116 gtk_list_store_set (request->impl->recent_model, &iter,
10117 RECENT_MODEL_COL_IS_FOLDER, is_folder,
10121 g_object_unref (request->impl);
10122 g_object_unref (request->file);
10123 gtk_tree_row_reference_free (request->row_ref);
10126 g_object_unref (cancellable);
10130 recent_sort_mru (gconstpointer a,
10133 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
10134 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
10136 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
10140 get_recent_files_limit (GtkWidget *widget)
10142 GtkSettings *settings;
10145 if (gtk_widget_has_screen (widget))
10146 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
10148 settings = gtk_settings_get_default ();
10150 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
10156 recent_idle_load (gpointer data)
10158 RecentLoadData *load_data = data;
10159 GtkFileChooserDefault *impl = load_data->impl;
10162 GtkRecentInfo *info;
10163 const gchar *uri, *display_name;
10165 GCancellable *cancellable;
10166 struct RecentItemInsertRequest *request;
10168 if (!impl->recent_manager)
10171 /* first iteration: load all the items */
10172 if (!load_data->items)
10174 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
10175 if (!load_data->items)
10178 load_data->needs_sorting = TRUE;
10183 /* second iteration: preliminary MRU sorting and clamping */
10184 if (load_data->needs_sorting)
10188 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
10189 load_data->n_items = g_list_length (load_data->items);
10191 limit = get_recent_files_limit (GTK_WIDGET (impl));
10193 if (limit != -1 && (load_data->n_items > limit))
10197 clamp = g_list_nth (load_data->items, limit - 1);
10198 if (G_LIKELY (clamp))
10201 clamp->next = NULL;
10203 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
10206 load_data->n_items = limit;
10210 load_data->n_loaded_items = 0;
10211 load_data->needs_sorting = FALSE;
10216 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
10217 g_assert (info != NULL);
10219 uri = gtk_recent_info_get_uri (info);
10220 display_name = gtk_recent_info_get_display_name (info);
10221 file = g_file_new_for_uri (uri);
10223 gtk_list_store_append (impl->recent_model, &iter);
10224 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
10226 request = g_new0 (struct RecentItemInsertRequest, 1);
10227 request->impl = g_object_ref (impl);
10228 request->file = g_object_ref (file);
10229 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
10230 gtk_tree_path_free (p);
10232 cancellable = _gtk_file_system_get_info (impl->file_system, file,
10234 recent_item_get_info_cb,
10237 gtk_list_store_set (impl->recent_model, &iter,
10238 RECENT_MODEL_COL_FILE, file,
10239 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
10240 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
10241 RECENT_MODEL_COL_CANCELLABLE, cancellable,
10244 load_data->n_loaded_items += 1;
10246 /* finished loading items */
10247 if (load_data->n_loaded_items == load_data->n_items)
10249 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10250 g_list_free (load_data->items);
10251 load_data->items = NULL;
10260 recent_start_loading (GtkFileChooserDefault *impl)
10262 RecentLoadData *load_data;
10264 recent_stop_loading (impl);
10265 recent_clear_model (impl, TRUE);
10266 recent_setup_model (impl);
10267 set_busy_cursor (impl, TRUE);
10269 g_assert (impl->load_recent_id == 0);
10271 load_data = g_new (RecentLoadData, 1);
10272 load_data->impl = impl;
10273 load_data->items = NULL;
10274 load_data->n_items = 0;
10275 load_data->n_loaded_items = 0;
10276 load_data->needs_sorting = TRUE;
10278 /* begin lazy loading the recent files into the model */
10279 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
10282 recent_idle_cleanup);
10286 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
10296 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
10297 *list = g_slist_prepend (*list, g_object_ref (file));
10300 /* Constructs a list of the selected paths in recent files mode */
10302 recent_get_selected_files (GtkFileChooserDefault *impl)
10305 GtkTreeSelection *selection;
10309 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10310 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
10311 result = g_slist_reverse (result);
10316 /* Called from ::should_respond(). We return whether there are selected
10317 * files in the recent files list.
10320 recent_should_respond (GtkFileChooserDefault *impl)
10322 GtkTreeSelection *selection;
10324 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10326 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10327 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10330 /* Hide the location widgets temporarily */
10332 recent_hide_entry (GtkFileChooserDefault *impl)
10338 impl->recent_hbox = gtk_hbox_new (FALSE, 12);
10341 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
10342 gtk_size_group_add_widget (impl->browse_path_bar_size_group, image);
10343 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
10346 label = gtk_label_new (NULL);
10347 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
10348 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
10349 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
10352 gtk_widget_hide (impl->browse_path_bar);
10353 gtk_widget_hide (impl->browse_new_folder_button);
10355 /* Box for recent widgets */
10356 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
10357 gtk_widget_show_all (impl->recent_hbox);
10359 /* Hide the location widgets temporarily */
10360 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10361 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10363 gtk_widget_hide (impl->location_button);
10364 gtk_widget_hide (impl->location_entry_box);
10368 /* Main entry point to the recent files functions; this gets called when
10369 * the user activates the Recently Used shortcut.
10372 recent_activate (GtkFileChooserDefault *impl)
10374 OperationMode previous_mode;
10376 if (impl->operation_mode == OPERATION_MODE_RECENT)
10379 previous_mode = impl->operation_mode;
10380 impl->operation_mode = OPERATION_MODE_RECENT;
10382 stop_operation (impl, previous_mode);
10384 recent_hide_entry (impl);
10386 /* hide the file size column if it's visible */
10387 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
10389 file_list_set_sort_column_ids (impl);
10390 recent_start_loading (impl);
10393 /* convert an iterator coming from the model bound to
10394 * browse_files_tree_view to an interator inside the
10395 * real recent_model
10398 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10399 GtkTreeIter *child_iter,
10402 GtkTreeIter middle;
10404 if (!impl->recent_model)
10407 if (!impl->recent_model_filter || !impl->recent_model_sort)
10410 /* pass 1: get the iterator in the filter model */
10411 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10414 /* pass 2: get the iterator in the real model */
10415 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10422 set_current_filter (GtkFileChooserDefault *impl,
10423 GtkFileFilter *filter)
10425 if (impl->current_filter != filter)
10429 /* NULL filters are allowed to reset to non-filtered status
10431 filter_index = g_slist_index (impl->filters, filter);
10432 if (impl->filters && filter && filter_index < 0)
10435 if (impl->current_filter)
10436 g_object_unref (impl->current_filter);
10437 impl->current_filter = filter;
10438 if (impl->current_filter)
10440 g_object_ref_sink (impl->current_filter);
10444 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10447 if (impl->browse_files_model)
10448 install_list_model_filter (impl);
10450 if (impl->operation_mode == OPERATION_MODE_SEARCH &&
10451 impl->search_model_filter != NULL)
10452 gtk_tree_model_filter_refilter (impl->search_model_filter);
10454 /* we want to have all the matching results, and not just a
10455 * filter of the previous model
10457 if (impl->operation_mode == OPERATION_MODE_RECENT)
10458 recent_start_loading (impl);
10460 g_object_notify (G_OBJECT (impl), "filter");
10465 filter_combo_changed (GtkComboBox *combo_box,
10466 GtkFileChooserDefault *impl)
10468 gint new_index = gtk_combo_box_get_active (combo_box);
10469 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10471 set_current_filter (impl, new_filter);
10475 check_preview_change (GtkFileChooserDefault *impl)
10477 GtkTreePath *cursor_path;
10479 const char *new_display_name;
10481 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10483 new_display_name = NULL;
10486 GtkTreeIter child_iter;
10488 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10490 if (impl->sort_model)
10493 GFileInfo *new_info;
10495 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10496 gtk_tree_path_free (cursor_path);
10498 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10500 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10501 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10503 new_display_name = g_file_info_get_display_name (new_info);
10506 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10510 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10511 &iter, cursor_path);
10512 gtk_tree_path_free (cursor_path);
10514 search_get_valid_child_iter (impl, &child_iter, &iter);
10515 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10516 SEARCH_MODEL_COL_FILE, &new_file,
10517 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10520 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10524 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10525 &iter, cursor_path);
10526 gtk_tree_path_free (cursor_path);
10528 recent_get_valid_child_iter (impl, &child_iter, &iter);
10529 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10530 RECENT_MODEL_COL_FILE, &new_file,
10531 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10536 if (new_file != impl->preview_file &&
10537 !(new_file && impl->preview_file &&
10538 g_file_equal (new_file, impl->preview_file)))
10540 if (impl->preview_file)
10542 g_object_unref (impl->preview_file);
10543 g_free (impl->preview_display_name);
10548 impl->preview_file = g_object_ref (new_file);
10549 impl->preview_display_name = g_strdup (new_display_name);
10553 impl->preview_file = NULL;
10554 impl->preview_display_name = NULL;
10557 if (impl->use_preview_label && impl->preview_label)
10558 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10560 g_signal_emit_by_name (impl, "update-preview");
10565 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10566 GtkFileSystemVolume *volume,
10567 const GError *error,
10571 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10572 GtkFileChooserDefault *impl = data;
10574 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10577 impl->shortcuts_activate_iter_cancellable = NULL;
10579 set_busy_cursor (impl, FALSE);
10586 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10590 name = _gtk_file_system_volume_get_display_name (volume);
10591 msg = g_strdup_printf (_("Could not mount %s"), name);
10593 error_message (impl, msg, error->message);
10602 file = _gtk_file_system_volume_get_root (volume);
10605 change_folder_and_display_error (impl, file, FALSE);
10606 g_object_unref (file);
10610 g_object_unref (impl);
10611 g_object_unref (cancellable);
10615 /* Activates a volume by mounting it if necessary and then switching to its
10619 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10620 GtkFileSystemVolume *volume)
10624 switch (impl->operation_mode)
10626 case OPERATION_MODE_BROWSE:
10628 case OPERATION_MODE_SEARCH:
10629 search_switch_to_browse_mode (impl);
10631 case OPERATION_MODE_RECENT:
10632 recent_switch_to_browse_mode (impl);
10636 /* We ref the file chooser since volume_mount() may run a main loop, and the
10637 * user could close the file chooser window in the meantime.
10639 g_object_ref (impl);
10641 if (!_gtk_file_system_volume_is_mounted (volume))
10643 GMountOperation *mount_op;
10645 set_busy_cursor (impl, TRUE);
10647 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
10648 impl->shortcuts_activate_iter_cancellable =
10649 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
10650 shortcuts_activate_volume_mount_cb,
10651 g_object_ref (impl));
10652 g_object_unref (mount_op);
10656 file = _gtk_file_system_volume_get_root (volume);
10659 change_folder_and_display_error (impl, file, FALSE);
10660 g_object_unref (file);
10664 g_object_unref (impl);
10667 /* Opens the folder or volume at the specified iter in the shortcuts model */
10668 struct ShortcutsActivateData
10670 GtkFileChooserDefault *impl;
10675 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10677 const GError *error,
10678 gpointer user_data)
10680 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10681 struct ShortcutsActivateData *data = user_data;
10683 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10686 data->impl->shortcuts_activate_iter_cancellable = NULL;
10691 if (!error && _gtk_file_info_consider_as_directory (info))
10692 change_folder_and_display_error (data->impl, data->file, FALSE);
10694 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10699 g_object_unref (data->impl);
10700 g_object_unref (data->file);
10703 g_object_unref (cancellable);
10707 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10708 GtkFileSystemVolume *volume,
10709 const GError *error,
10710 gpointer user_data)
10712 struct ShortcutsActivateData *data = user_data;
10716 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10718 g_object_unref (data->impl);
10719 g_object_unref (data->file);
10725 data->impl->shortcuts_activate_iter_cancellable =
10726 _gtk_file_system_get_info (data->impl->file_system, data->file,
10728 shortcuts_activate_get_info_cb, data);
10732 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10736 ShortcutType shortcut_type;
10738 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10739 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10740 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10741 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10743 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10744 SHORTCUTS_COL_DATA, &col_data,
10745 SHORTCUTS_COL_TYPE, &shortcut_type,
10748 if (impl->shortcuts_activate_iter_cancellable)
10750 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10751 impl->shortcuts_activate_iter_cancellable = NULL;
10754 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10756 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10758 GtkFileSystemVolume *volume;
10762 shortcuts_activate_volume (impl, volume);
10764 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10766 struct ShortcutsActivateData *data;
10767 GtkFileSystemVolume *volume;
10769 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10771 data = g_new0 (struct ShortcutsActivateData, 1);
10772 data->impl = g_object_ref (impl);
10773 data->file = g_object_ref (col_data);
10775 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10777 GMountOperation *mount_operation;
10778 GtkWidget *toplevel;
10780 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10782 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10784 impl->shortcuts_activate_iter_cancellable =
10785 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10787 shortcuts_activate_mount_enclosing_volume,
10792 impl->shortcuts_activate_iter_cancellable =
10793 _gtk_file_system_get_info (impl->file_system, data->file,
10795 shortcuts_activate_get_info_cb, data);
10798 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10800 search_activate (impl);
10802 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10804 recent_activate (impl);
10808 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10810 shortcuts_key_press_event_cb (GtkWidget *widget,
10811 GdkEventKey *event,
10812 GtkFileChooserDefault *impl)
10816 modifiers = gtk_accelerator_get_default_mod_mask ();
10818 if (key_is_left_or_right (event))
10820 gtk_widget_grab_focus (impl->browse_files_tree_view);
10824 if ((event->keyval == GDK_BackSpace
10825 || event->keyval == GDK_Delete
10826 || event->keyval == GDK_KP_Delete)
10827 && (event->state & modifiers) == 0)
10829 remove_selected_bookmarks (impl);
10833 if ((event->keyval == GDK_F2)
10834 && (event->state & modifiers) == 0)
10836 rename_selected_bookmark (impl);
10844 shortcuts_select_func (GtkTreeSelection *selection,
10845 GtkTreeModel *model,
10847 gboolean path_currently_selected,
10850 GtkFileChooserDefault *impl = data;
10851 GtkTreeIter filter_iter;
10852 ShortcutType shortcut_type;
10854 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10855 g_assert_not_reached ();
10857 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10859 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10863 list_select_func (GtkTreeSelection *selection,
10864 GtkTreeModel *model,
10866 gboolean path_currently_selected,
10869 GtkFileChooserDefault *impl = data;
10871 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10872 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10874 GtkTreeIter iter, child_iter;
10876 switch (impl->operation_mode)
10878 case OPERATION_MODE_SEARCH:
10880 gboolean is_folder;
10882 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10885 search_get_valid_child_iter (impl, &child_iter, &iter);
10886 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10887 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10894 case OPERATION_MODE_RECENT:
10896 gboolean is_folder;
10898 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10901 recent_get_valid_child_iter (impl, &child_iter, &iter);
10902 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10903 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10910 case OPERATION_MODE_BROWSE:
10914 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10917 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10918 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10919 if (info && (! _gtk_file_info_consider_as_directory (info)))
10930 list_selection_changed (GtkTreeSelection *selection,
10931 GtkFileChooserDefault *impl)
10933 /* See if we are in the new folder editable row for Save mode */
10934 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10935 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10938 gboolean had_selection;
10940 info = get_selected_file_info_from_file_list (impl, &had_selection);
10941 if (!had_selection)
10942 goto out; /* normal processing */
10945 return; /* We are on the editable row for New Folder */
10950 if (impl->location_entry)
10951 update_chooser_entry (impl);
10953 check_preview_change (impl);
10954 bookmarks_check_add_sensitivity (impl);
10956 g_signal_emit_by_name (impl, "selection-changed", 0);
10959 /* Callback used when a row in the file list is activated */
10961 list_row_activated (GtkTreeView *tree_view,
10963 GtkTreeViewColumn *column,
10964 GtkFileChooserDefault *impl)
10967 GtkTreeIter child_iter;
10969 switch (impl->operation_mode)
10971 case OPERATION_MODE_SEARCH:
10974 gboolean is_folder;
10976 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10979 search_get_valid_child_iter (impl, &child_iter, &iter);
10980 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10981 SEARCH_MODEL_COL_FILE, &file,
10982 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10987 change_folder_and_display_error (impl, file, FALSE);
10991 g_signal_emit_by_name (impl, "file-activated");
10995 case OPERATION_MODE_RECENT:
10998 gboolean is_folder;
11000 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
11003 recent_get_valid_child_iter (impl, &child_iter, &iter);
11004 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11005 RECENT_MODEL_COL_FILE, &file,
11006 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11011 change_folder_and_display_error (impl, file, FALSE);
11015 g_signal_emit_by_name (impl, "file-activated");
11019 case OPERATION_MODE_BROWSE:
11023 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
11026 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
11027 &child_iter, &iter);
11028 info = _gtk_file_system_model_get_info (impl->browse_files_model,
11031 if (_gtk_file_info_consider_as_directory (info))
11033 GFile *file, *target_file;
11034 const gchar *target_uri;
11036 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
11037 if (g_file_info_get_file_type (info) == G_FILE_TYPE_MOUNTABLE ||
11038 g_file_info_get_file_type (info) == G_FILE_TYPE_SHORTCUT)
11040 target_uri = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
11043 target_file = g_file_new_for_uri (target_uri);
11046 g_object_unref (file);
11047 file = target_file;
11053 change_folder_and_display_error (impl, file, FALSE);
11057 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11058 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
11059 g_signal_emit_by_name (impl, "file-activated");
11066 path_bar_clicked (GtkPathBar *path_bar,
11069 gboolean child_is_hidden,
11070 GtkFileChooserDefault *impl)
11073 pending_select_files_add (impl, child_file);
11075 if (!change_folder_and_display_error (impl, file, FALSE))
11078 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
11079 * show hidden files so that ".baz" appears in the file list, as it will still
11080 * be shown in the path bar: "/foo/[bar]/.baz"
11082 if (child_is_hidden)
11083 g_object_set (impl, "show-hidden", TRUE, NULL);
11087 get_list_file_info (GtkFileChooserDefault *impl,
11090 GtkTreeIter child_iter;
11092 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
11096 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
11100 list_icon_data_func (GtkTreeViewColumn *tree_column,
11101 GtkCellRenderer *cell,
11102 GtkTreeModel *tree_model,
11106 GtkFileChooserDefault *impl = data;
11107 GtkTreeIter child_iter;
11108 GdkPixbuf *pixbuf = NULL;
11109 gboolean sensitive = TRUE;
11111 profile_start ("start", NULL);
11113 switch (impl->operation_mode)
11115 case OPERATION_MODE_SEARCH:
11117 GtkTreeIter child_iter;
11118 gboolean is_folder;
11120 search_get_valid_child_iter (impl, &child_iter, iter);
11121 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11122 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
11123 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11126 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11127 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11128 sensitive = is_folder;
11132 case OPERATION_MODE_RECENT:
11134 GtkTreeIter child_iter;
11135 GtkRecentInfo *info;
11136 gboolean is_folder;
11138 recent_get_valid_child_iter (impl, &child_iter, iter);
11139 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11140 RECENT_MODEL_COL_INFO, &info,
11141 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11144 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
11146 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11147 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11148 sensitive = is_folder;
11152 case OPERATION_MODE_BROWSE:
11157 info = get_list_file_info (impl, iter);
11159 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
11162 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
11167 /* FIXME: NULL GError */
11168 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
11173 /* We are on the editable row */
11178 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11179 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
11180 sensitive = _gtk_file_info_consider_as_directory (info);
11185 g_object_set (cell,
11187 "sensitive", sensitive,
11191 g_object_unref (pixbuf);
11193 profile_end ("end", NULL);
11197 list_name_data_func (GtkTreeViewColumn *tree_column,
11198 GtkCellRenderer *cell,
11199 GtkTreeModel *tree_model,
11203 GtkFileChooserDefault *impl = data;
11205 gboolean sensitive = TRUE;
11207 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11209 GtkTreeIter child_iter;
11210 gchar *display_name;
11211 gboolean is_folder;
11213 search_get_valid_child_iter (impl, &child_iter, iter);
11214 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11215 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
11216 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11219 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11220 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11222 sensitive = is_folder;
11225 g_object_set (cell,
11226 "text", display_name,
11227 "sensitive", sensitive,
11228 "ellipsize", PANGO_ELLIPSIZE_END,
11234 if (impl->operation_mode == OPERATION_MODE_RECENT)
11236 GtkTreeIter child_iter;
11237 GtkRecentInfo *recent_info;
11238 gchar *display_name;
11239 gboolean is_folder;
11241 recent_get_valid_child_iter (impl, &child_iter, iter);
11242 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11243 RECENT_MODEL_COL_INFO, &recent_info,
11244 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11247 display_name = gtk_recent_info_get_short_name (recent_info);
11249 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11250 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11252 sensitive = is_folder;
11255 g_object_set (cell,
11256 "text", display_name,
11257 "sensitive", sensitive,
11258 "ellipsize", PANGO_ELLIPSIZE_END,
11261 g_free (display_name);
11266 info = get_list_file_info (impl, iter);
11271 g_object_set (cell,
11272 "text", DEFAULT_NEW_FOLDER_NAME,
11274 "ellipsize", PANGO_ELLIPSIZE_NONE,
11281 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11282 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11284 sensitive = _gtk_file_info_consider_as_directory (info);
11287 g_object_set (cell,
11288 "text", g_file_info_get_display_name (info),
11289 "sensitive", sensitive,
11290 "ellipsize", PANGO_ELLIPSIZE_END,
11295 list_size_data_func (GtkTreeViewColumn *tree_column,
11296 GtkCellRenderer *cell,
11297 GtkTreeModel *tree_model,
11301 GtkFileChooserDefault *impl = data;
11305 gboolean sensitive = TRUE;
11307 if (impl->operation_mode == OPERATION_MODE_RECENT)
11310 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11312 GtkTreeIter child_iter;
11313 gboolean is_folder = FALSE;
11315 search_get_valid_child_iter (impl, &child_iter, iter);
11316 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11317 SEARCH_MODEL_COL_SIZE, &size,
11318 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11321 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11322 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11323 sensitive = is_folder ? TRUE : FALSE;
11326 str = g_format_size_for_display (size);
11330 g_object_set (cell,
11332 "sensitive", sensitive,
11340 info = get_list_file_info (impl, iter);
11342 if (!info || _gtk_file_info_consider_as_directory (info))
11344 g_object_set (cell,
11346 "sensitive", sensitive,
11351 size = g_file_info_get_size (info);
11352 str = g_format_size_for_display (size);
11354 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11355 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11358 g_object_set (cell,
11360 "sensitive", sensitive,
11366 /* Tree column data callback for the file list; fetches the mtime of a file */
11368 list_mtime_data_func (GtkTreeViewColumn *tree_column,
11369 GtkCellRenderer *cell,
11370 GtkTreeModel *tree_model,
11374 GtkFileChooserDefault *impl;
11375 GTimeVal timeval = { 0, };
11377 gchar *date_str = NULL;
11378 gboolean sensitive = TRUE;
11380 const char *locale, *dot = NULL;
11381 gint64 codepage = -1;
11387 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11389 GtkTreeIter child_iter;
11391 gboolean is_folder;
11393 search_get_valid_child_iter (impl, &child_iter, iter);
11394 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11395 SEARCH_MODEL_COL_MTIME, &mtime,
11396 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11399 time_mtime = (time_t) mtime;
11401 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11402 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11403 sensitive = is_folder;
11405 else if (impl->operation_mode == OPERATION_MODE_RECENT)
11407 GtkTreeIter child_iter;
11408 GtkRecentInfo *info;
11409 gboolean is_folder;
11411 recent_get_valid_child_iter (impl, &child_iter, iter);
11412 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11413 RECENT_MODEL_COL_INFO, &info,
11414 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11418 time_mtime = gtk_recent_info_get_modified (info);
11422 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11423 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11424 sensitive = is_folder;
11430 info = get_list_file_info (impl, iter);
11433 g_object_set (cell,
11440 g_file_info_get_modification_time (info, &timeval);
11441 time_mtime = timeval.tv_sec;
11443 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11444 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11445 sensitive = _gtk_file_info_consider_as_directory (info);
11448 if (G_UNLIKELY (time_mtime == 0))
11449 date_str = g_strdup (_("Unknown"));
11454 struct tm tm_mtime;
11456 const gchar *format;
11457 gchar *locale_format = NULL;
11460 #ifdef HAVE_LOCALTIME_R
11461 localtime_r ((time_t *) &time_mtime, &tm_mtime);
11464 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
11468 g_warning ("ptm != NULL failed");
11470 g_object_set (cell,
11471 "text", _("Unknown"),
11472 "sensitive", sensitive,
11477 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11479 #endif /* HAVE_LOCALTIME_R */
11481 g_date_set_time_t (&mtime, time_mtime);
11482 time_now = time (NULL);
11483 g_date_set_time_t (&now, time_now);
11485 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11487 /* Translators: %H means "hours" and %M means "minutes" */
11488 if (days_diff == 0)
11489 format = _("%H:%M");
11490 else if (days_diff == 1)
11491 format = _("Yesterday at %H:%M");
11494 if (days_diff > 1 && days_diff < 7)
11495 format = "%A"; /* Days from last week */
11497 format = "%x"; /* Any other date */
11501 /* g_locale_from_utf8() returns a string in the system
11502 * code-page, which is not always the same as that used by the C
11503 * library. For instance when running a GTK+ program with
11504 * LANG=ko on an English version of Windows, the system
11505 * code-page is 1252, but the code-page used by the C library is
11506 * 949. (It's GTK+ itself that sets the C library locale when it
11507 * notices the LANG environment variable. See gtkmain.c The
11508 * Microsoft C library doesn't look at any locale environment
11509 * variables.) We need to pass strftime() a string in the C
11510 * library's code-page. See bug #509885.
11512 locale = setlocale (LC_ALL, NULL);
11513 if (locale != NULL)
11514 dot = strchr (locale, '.');
11517 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11519 /* All codepages should fit in 16 bits AFAIK */
11520 if (codepage > 0 && codepage < 65536)
11522 sprintf (charset, "CP%u", (guint) codepage);
11523 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11527 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11529 if (locale_format != NULL &&
11530 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11533 /* As above but in opposite direction... */
11534 if (codepage > 0 && codepage < 65536)
11535 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11537 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11541 if (date_str == NULL)
11542 date_str = g_strdup (_("Unknown"));
11544 g_free (locale_format);
11547 g_object_set (cell,
11549 "sensitive", sensitive,
11555 _gtk_file_chooser_default_new (void)
11557 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11561 location_set_user_text (GtkFileChooserDefault *impl,
11564 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11565 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11569 location_popup_handler (GtkFileChooserDefault *impl,
11572 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11574 GtkWidget *widget_to_focus;
11576 /* This will give us the location widgets back */
11577 switch (impl->operation_mode)
11579 case OPERATION_MODE_SEARCH:
11580 search_switch_to_browse_mode (impl);
11582 case OPERATION_MODE_RECENT:
11583 recent_switch_to_browse_mode (impl);
11585 case OPERATION_MODE_BROWSE:
11586 g_assert_not_reached ();
11590 if (impl->current_folder)
11591 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11593 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11594 widget_to_focus = impl->browse_files_tree_view;
11596 widget_to_focus = impl->location_entry;
11598 gtk_widget_grab_focus (widget_to_focus);
11602 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11603 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11605 LocationMode new_mode;
11609 /* since the user typed something, we unconditionally want to turn on the entry */
11610 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11612 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11613 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11614 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11615 new_mode = LOCATION_MODE_PATH_BAR;
11618 g_assert_not_reached ();
11622 location_mode_set (impl, new_mode, TRUE);
11623 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11626 location_set_user_text (impl, path);
11629 location_entry_set_initial_text (impl);
11630 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11634 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11635 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11637 gtk_widget_grab_focus (impl->location_entry);
11639 location_set_user_text (impl, path);
11642 g_assert_not_reached ();
11645 /* Handler for the "up-folder" keybinding signal */
11647 up_folder_handler (GtkFileChooserDefault *impl)
11649 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11652 /* Handler for the "down-folder" keybinding signal */
11654 down_folder_handler (GtkFileChooserDefault *impl)
11656 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11659 /* Switches to the shortcut in the specified index */
11661 switch_to_shortcut (GtkFileChooserDefault *impl,
11666 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11667 g_assert_not_reached ();
11669 shortcuts_activate_iter (impl, &iter);
11672 /* Handler for the "home-folder" keybinding signal */
11674 home_folder_handler (GtkFileChooserDefault *impl)
11676 if (impl->has_home)
11677 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11680 /* Handler for the "desktop-folder" keybinding signal */
11682 desktop_folder_handler (GtkFileChooserDefault *impl)
11684 if (impl->has_desktop)
11685 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11688 /* Handler for the "search-shortcut" keybinding signal */
11690 search_shortcut_handler (GtkFileChooserDefault *impl)
11692 if (impl->has_search)
11694 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11696 /* we want the entry widget to grab the focus the first
11697 * time, not the browse_files_tree_view widget.
11699 if (impl->search_entry)
11700 gtk_widget_grab_focus (impl->search_entry);
11704 /* Handler for the "recent-shortcut" keybinding signal */
11706 recent_shortcut_handler (GtkFileChooserDefault *impl)
11708 if (impl->has_recent)
11709 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11713 quick_bookmark_handler (GtkFileChooserDefault *impl,
11714 gint bookmark_index)
11719 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11722 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11724 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11725 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11728 gtk_tree_path_free (path);
11730 switch_to_shortcut (impl, bookmark_pos);
11734 show_hidden_handler (GtkFileChooserDefault *impl)
11736 g_object_set (impl,
11737 "show-hidden", !impl->show_hidden,
11742 /* Drag and drop interfaces */
11745 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11750 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11752 model->impl = NULL;
11755 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11757 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11760 ShortcutsPaneModelFilter *model;
11764 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11766 pos = *gtk_tree_path_get_indices (path);
11767 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11769 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11772 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11774 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11776 GtkSelectionData *selection_data)
11778 ShortcutsPaneModelFilter *model;
11780 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11787 /* Fill the GtkTreeDragSourceIface vtable */
11789 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11791 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11792 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11796 /* Fill the GtkTreeDragDestIface vtable */
11798 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11800 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11801 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11805 static GtkTreeModel *
11806 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11807 GtkTreeModel *child_model,
11810 ShortcutsPaneModelFilter *model;
11812 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11813 "child-model", child_model,
11814 "virtual-root", root,
11817 model->impl = impl;
11819 return GTK_TREE_MODEL (model);
11825 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11828 RecentModelSort *model;
11829 GtkTreeIter iter, child_iter;
11830 gboolean is_folder;
11832 model = RECENT_MODEL_SORT (drag_source);
11833 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11836 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11837 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11838 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11845 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11847 GtkSelectionData *selection_data)
11849 RecentModelSort *model;
11850 GtkTreeIter iter, child_iter;
11854 model = RECENT_MODEL_SORT (drag_source);
11855 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11858 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11859 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11860 RECENT_MODEL_COL_FILE, &file,
11862 g_assert (file != NULL);
11864 uris[0] = g_file_get_uri (file);
11867 gtk_selection_data_set_uris (selection_data, uris);
11875 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11877 iface->row_draggable = recent_model_sort_row_draggable;
11878 iface->drag_data_get = recent_model_sort_drag_data_get;
11882 _recent_model_sort_class_init (RecentModelSortClass *klass)
11888 _recent_model_sort_init (RecentModelSort *model)
11890 model->impl = NULL;
11893 static GtkTreeModel *
11894 recent_model_sort_new (GtkFileChooserDefault *impl,
11895 GtkTreeModel *child_model)
11897 RecentModelSort *model;
11899 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11900 "model", child_model,
11902 model->impl = impl;
11904 return GTK_TREE_MODEL (model);
11910 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11913 SearchModelSort *model;
11914 GtkTreeIter iter, child_iter;
11915 gboolean is_folder;
11917 model = SEARCH_MODEL_SORT (drag_source);
11918 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11921 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11922 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11923 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11930 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11932 GtkSelectionData *selection_data)
11934 SearchModelSort *model;
11935 GtkTreeIter iter, child_iter;
11939 model = SEARCH_MODEL_SORT (drag_source);
11940 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11943 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11944 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11945 RECENT_MODEL_COL_FILE, &file,
11947 g_assert (file != NULL);
11949 uris[0] = g_file_get_uri (file);
11952 gtk_selection_data_set_uris (selection_data, uris);
11960 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11962 iface->row_draggable = search_model_sort_row_draggable;
11963 iface->drag_data_get = search_model_sort_drag_data_get;
11967 _search_model_sort_class_init (SearchModelSortClass *klass)
11973 _search_model_sort_init (SearchModelSort *model)
11975 model->impl = NULL;
11978 static GtkTreeModel *
11979 search_model_sort_new (GtkFileChooserDefault *impl,
11980 GtkTreeModel *child_model)
11982 SearchModelSort *model;
11984 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11985 "model", child_model,
11987 model->impl = impl;
11989 return GTK_TREE_MODEL (model);